Opt merge multi launch kernels

From Xilinx/mlir-air · View interactive version

Opt merge multi launch kernels is an AI agent skill from the GitHub repository Xilinx/mlir-air. Merge supported consecutive AIR launch regions into a single invocation when dispatch or host handoff costs matter, preserving argument, symbol, layout and lifetime relationships.

How to use this skill

Skill file

Merge supported AIR launches

Use when measured dispatch/host handoffs motivate merging and the selected backend supports the composition. This recipe reduces invocation boundaries; it does not by itself establish persistent megakernel residency or progress. Read current helpers and one matching builder from project integration.

  1. List the actual consecutive kernels, their interfaces, numerical order, host work and state boundaries. Mark unsupported launch/control patterns and data-dependent host decisions. Choose a small group with a real removable boundary. Historical FA-separate groups are worked implementation choices, not a prohibition on every attention fusion.
  2. Generate/read each sub-kernel's IR with the selected builder. Current worked builders use shared.infra.stitching APIs FuncArg, KernelSlice and stitch_elf; inspect shared/builders/rms_gemms_rope_multi.py, o_ffn_multi.py or o_gemv_ffn_multi.py for the matching composition.
  3. Declare the combined ABI with FuncArg, then ordered KernelSlice objects with unique prefix and arg_map mapping launch operands to combined arguments. Use arg_aliases only for valid SSA views declared in prelude; include required scratch arguments. stitch_elf extracts/renames/remaps through helpers such as _rename_all_with_externs and _fix_launch_func_args, preserving channel/private declarations. Keep its dangling-argument checks; allow an unreferenced ABI slot only when intentionally retained and explained. Inspect emitted IR, layouts and output/state slots; parser checks do not establish semantic wiring.
  4. If the chosen sub-kernel emits a bare air.herd but the lowering requires launch/segment structure, apply _wrap_ir_in_launch before stitching. Confirm the path survives lowering. This wrapper requirement belongs to that implementation, not to all AIR programs.
  5. Identify which external symbols must be shared and which shape-specialized objects require distinct names. KernelSlice.extern_syms and _rename_all_with_externs(..., extern_funcs) list symbols preserved from prefix renaming; stitch_elf preserves their union. The current o_gemv_ffn_multi.py deliberately shares mv_bf16.o symbols across stages 1 and 3, not the old two-K rename example. If two object implementations need distinct shape-specific symbols, coordinate their compile symbol names, declarations and call sites before adding them to the preserved set. Do not assign an incompatible object to another launch.
  6. Compile through the actual KernelCache.compile_and_cache or task build interface, preserving the final source/configuration/retained IR/artifact binding. Multi-launch debug handles source/lowering/resource failures.
  7. Run the same operations unmerged and merged at matching input/state. Use the applicable exact output/state comparison if arithmetic and representation are preserved; otherwise the frozen calibrated gate. Inspect repeats, intermediate ownership and output readback as well as first output. Rerun required production gates.
  8. Verify the production runtime actually loads the merged artifact and reduces the intended invocation/host handoff count. Compare complete target time, compilation cost separately and measurement uncertainty. No fixed dispatch cost or expected percentage is promised.

On a mismatch, undo or isolate the most recent merge boundary to test symbol/argument/layout/scheduling hypotheses; this is localization evidence, not automatically a unique cause. On resource pressure or no net benefit, smaller groups or separate dispatches remain valid options. Return applied/skipped/reverted with coupled changes, gates and timing to Optimization.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.