Optimize a cubin or intercept an application
Start with an MLIR-enabled build and a cubin
for an architecture present in its opcode tables. The commands below use
build/sass-mlir from the source root. Inspection, lifting, lowering, and
verification run on the host; they do not launch CUDA kernels.
sass-mlir also launches existing CUDA applications through an interception
plugin. The plugin can optimize selected kernels as their modules load, using
the same MLIR rewrite core as the offline optimize command. Follow the cubin
workflow below, or jump to runtime interception and optimization.
Supply a cubin and identify its kernels
Use an existing cubin, named kernel.cubin here. If you have nvcc and want a
small input, save this as saxpy.cu:
extern "C" __global__ void saxpy(float a, const float* x, float* y, int n) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n) y[i] = a * x[i] + y[i];
}nvcc -arch=sm_90 -cubin saxpy.cu -o kernel.cubin
build/sass-mlir inspect kernel.cubinChoose an architecture supported by both your toolkit and the supplied tables.
inspect lists the cubin’s SM, text-section names, and instruction counts. Keep
the original cubin as the comparison input throughout the workflow.
Check the unmodified encoding first
build/sass-mlir verify kernel.cubin --threads 1The default check covers the PSIR path and, in an MLIR build, the MLIR round trip. Untouched instructions retain their original 128-bit words, including bits the structural encoder does not own. This is the preservation baseline.
For a stricter, separate view of structural regeneration:
build/sass-mlir verify kernel.cubin --regen --threads 1Regeneration can expose an unsupported form or a byte mismatch even when raw preservation succeeds. Record those limitations before choosing a rewrite; equal disassembly text is not byte identity. A failed check is evidence to investigate, not a reason to suppress the check.
Read the tool's actual output
build/sass-mlir lift kernel.cubin --out ir --threads 1
build/sass-mlir research parse-check ir/kernel.mlir > parsed.mlirThe file is ir/kernel.mlir, named after the input cubin; it contains the lifted
functions. Read that file instead of expecting fixed SSA numbers or an example
instruction count from another compiler version.
Look for sass.func, sass.inst, typed SSA results, structured operands,
#sass.sched, and #sass.raw. The mnemonic is carried by the instruction op;
the current dialect does not create an op named sass.FFMA_0x223 for each
encoding. Recovered control flow uses blocks and branches. Predicated writes
also retain the old destination value, because a false predicate leaves it
unchanged.
parse-check verifies that the printed MLIR can be parsed and printed stably.
It does not establish numerical correctness.
Emit section bytes, or publish a transformed cubin
build/sass-mlir lower ir/kernel.mlir --out binsThis writes one .bin per kernel section, with a sanitized section name. These
are raw section bytes, not a linked cubin and not a launchable application.
Unencodable words cause lowering to fail.
To exercise the shared optimization and cubin-publication path, select a pass explicitly and write a separate candidate:
build/sass-mlir passes
build/sass-mlir optimize kernel.cubin --passes uniform --out candidate.cubin
build/sass-mlir verify candidate.cubin --threads 1uniform may find no admitted rewrite in this input; the report states what
changed, and an unchanged matched input still gets an output. For targeting,
--kernel saxpy is a substring filter, not a glob. A filter matching nothing
is an error.
Intercept an application and optimize loaded kernels
From the source root, use the built kernel-rewrite plugin and select both
a kernel substring and a pass explicitly:
SASS2MLIR_KERNEL=saxpy SASS2MLIR_PASS=uniform \
build/sass-mlir intercept \
"$PWD/build/passes/kernel-rewrite/sass2mlir-pass-kernel-rewrite.so" \
-- ./applicationReplace saxpy with a substring of your kernel’s symbol and ./application
with your CUDA program and its arguments. You can first
capture and inspect its loaded cubins
to find the names. Everything after -- is passed to the application unchanged.
SASS2MLIR_PASS selects the rewrite stages; it also accepts an ordered,
comma-separated pipeline such as uniform,hgmma for an admitted workload.
The launcher sets LD_PRELOAD for the plugin and executes the application.
Supported CUDA image-load calls reach the interception hooks, which offer the
loaded cubin to kernel-rewrite. That plugin invokes the shared MLIR core,
rewrites the matching kernel sections, and supplies the resulting image to the
real CUDA loader. The application then launches those kernels through its
existing CUDA calls. You do not need to extract and replace the cubin by hand.
Keep libsass2mlir-mlircore.so beside the plugin in the build or installed
plugin directory. The plugin needs that core to apply MLIR transformations.
Check the kernel-rewrite report for matched sections, changed sections and
rewritten words, then confirm the interceptor reports replaced a … module
or patched … bytes. Counts alone can describe a prepared candidate: an
observation-only … loading the original message means that candidate was
not applied. A missing selection, unavailable core or refused rewrite also
keeps the original image. A successful application exit alone does not show
that optimization occurred. uniform may legitimately find no admitted change.
This route must preserve the application’s parameter and launch contract.
The fresh FlashAttention program needs its typed launch adapter and bound
sidecar; generic kernel-rewrite preload explicitly refuses
flash-attention-resynth. Use its reconstruction workflow
for that case. Numerical validation and GPU timing still follow an actual
successful rewrite; module-load compilation time is a separate host cost.
Turn this loop into a disciplined pass
Continue with Writing a good SASS pass. It follows the shipped FMA stage from admission through checked SSA edits and emission, then explains how to validate the contract a change affects.
Use the CLI reference for output contracts and the examples index for the rest of the manual.