Repository navigation
Description
What happened?
Summary
When a contraction falls back to the SIMT path in setContractConfig (for example, an f32 matmul with no MMA intrinsic), the TileAndFuse branch of setMatmulConfig derives thread tile sizes as:
threadTileSizes[numParallelLoops - 2] = // M
(tileX / workgroupSize[0]) < 1 ? 1 : (tileX / workgroupSize[0]);
threadTileSizes[numParallelLoops - 1] = // N
(tileY / workgroupSize[1]) < 1 ? 1 : (tileY / workgroupSize[1]);The M tile is divided by the workgroup X size and the N tile by the Y size. For several entries of getMatmulConfig, the resulting thread tiles cover far fewer tiles than the workgroup has threads, and the < 1 ? 1 clamp hides the mismatch. With the entry {{8, 128, 4}, {32, 1, 1}}, the selected config is thread = [.., 1, 128] with 32 threads per workgroup. Only 8 threads have work, and each one owns a 128-element accumulator.
For the attention-like shape 16x1000x1000 @ 16x1000x128 (f32), changing only the thread attribute to the X→N mapping ([1, 8, 4, 0]) reduces kernel time on an H100 NVL from 9.43 ms to 0.234 ms (40×). Nsight Compute measures 8.0 vs. 32 active threads per warp, and 512.5M vs. 0.26M global store sectors.
Steps to reproduce your issue
Reproducer
Save as repro.mlir:
func.func @main(%lhs: tensor<16x1000x1000xf32>, %rhs: tensor<16x1000x128xf32>) -> tensor<16x1000x128xf32> {
%zero = arith.constant 0.0 : f32
%empty = tensor.empty() : tensor<16x1000x128xf32>
%acc = linalg.fill ins(%zero : f32) outs(%empty : tensor<16x1000x128xf32>) -> tensor<16x1000x128xf32>
%result = linalg.batch_matmul ins(%lhs, %rhs : tensor<16x1000x1000xf32>, tensor<16x1000x128xf32>)
outs(%acc : tensor<16x1000x128xf32>) -> tensor<16x1000x128xf32>
return %result : tensor<16x1000x128xf32>
}Compile with the default configuration and dump the selected configuration and PTX:
iree-compile repro.mlir \
--iree-hal-target-device=cuda --iree-cuda-target=sm_80 \
--iree-hal-dump-executable-configurations-to=cfg \
--iree-hal-dump-executable-binaries-to=bin \
-o default.vmfbCreate a controlled variant that changes only the thread tile attribute, and substitute it back into the same compilation:
python3 - <<'PY'
from pathlib import Path
src = Path("cfg/configured_module_main_dispatch_0.mlir").read_text()
old = "thread = [1, 1, 128, 0]"
assert src.count(old) == 1
Path("fixed_dispatch_0.mlir").write_text(src.replace(old, "thread = [1, 8, 4, 0]"))
PY
iree-compile repro.mlir \
--iree-hal-target-device=cuda --iree-cuda-target=sm_80 \
--iree-hal-substitute-executable-configuration=main_dispatch_0=fixed_dispatch_0.mlir \
--iree-hal-dump-executable-binaries-to=bin_fixed \
-o fixed.vmfbInputs, numerical comparison, and timing:
python3 - <<'PY'
import numpy as np
rng = np.random.default_rng(0)
lhs = rng.random((16, 1000, 1000), dtype=np.float32)
lhs /= lhs.sum(-1, keepdims=True)
rhs = rng.standard_normal((16, 1000, 128), dtype=np.float32)
np.save("lhs.npy", lhs); np.save("rhs.npy", rhs)
PY
for v in default fixed; do
iree-run-module --device=cuda --module=$v.vmfb --function=main \
[email protected] [email protected] --output=@out_$v.npy
iree-benchmark-module --device=cuda --module=$v.vmfb --function=main \
[email protected] [email protected] \
--benchmark_repetitions=10 --benchmark_report_aggregates_only=true
doneKernel-level measurements used Nsight Systems (nsys profile --trace=cuda around iree-benchmark-module) and Nsight Compute (ncu --set full -k regex:batch_matmul -c 1 around iree-run-module).
Actual behavior
The default configuration is:
pipeline = TileAndFuse, workgroup_size = [32, 1, 1]
lowering_config = {reduction = [0, 0, 0, 4], thread = [1, 1, 128, 0], workgroup = [1, 8, 128, 1]}
This matches the first exact entry of getMatmulConfig for M = 1000, N = 128, K = 1000, which is {{8, 128, 4}, {32, 1, 1}}. The thread tile becomes M: max(1, 8 / 32) = 1, N: 128 / 1 = 128. So the 8×128 workgroup tile is covered by 8 thread tiles, while the workgroup has 32 threads.
The generated PTX starts with setp.gt.u32 %p1, %r1, 7; @%p1 bra $L__BB0_5;, which means threads 8–31 exit immediately. Each remaining thread loops over its 128 output elements. Every reduction step performs an ld.global.b32 / st.global.b32 read-modify-write of the output element (26 registers, no FMA contraction).
Expected behavior
The workgroup sizes in getMatmulConfig lay out X along N and Y along M (for example, {{32, 128, 32}, {32, 8, 1}} assigns 32 threads to the 128-wide N tile). Thread tiles should be derived the same way, so that the number of thread tiles equals the workgroup size. For this shape that gives thread = [1, 8, 4, 0]: all 32 threads busy, 32 accumulators per thread.
What component(s) does this issue relate to?
Compiler
Version information
Upstream source checked
The measurements used the upstream nightly packages iree-base-compiler / iree-base-runtime 3.14.0rc20261103, built from 020781198d5c99cbae312023ed4e005482e3ff74. setContractConfig at that commit is identical to upstream main at 5144a2837e7e882ac27b8978d400483bef5769bd.
Additional context
Controlled comparison
The two binaries differ only in the thread attribute above. Measured on an NVIDIA H100 NVL:
| Metric | Default thread = [1, 1, 128, 0] |
Changed thread = [1, 8, 4, 0] |
|---|---|---|
| Kernel duration (ncu) | 9.432 ms | 0.234 ms |
| Kernel duration (nsys median) | 9.162 ms | 0.230 ms |
iree-benchmark-module mean (10 reps) |
9.49 ms ± 0.017 | 0.579 ms ± 0.018 |
| Avg. active threads per warp | 8.00 | 32 |
| Warp instructions executed | 1,412,622,000 | 75,638,000 |
| Global load sectors (L1) | 772,000,000 | 36,000,000 |
| Global store sectors (L1) | 512,523,394 | 256,000 |
| L2 write sectors | 512,416,333 | 256,000 |
| L2 throughput (% of peak) | 76.7 % | 15.9 % |
| DRAM read | 73.27 MB | 73.23 MB |
| Registers per thread | 26 | 89 |
| Long-scoreboard stall per issue | 16.6 | 8.0 |
| PTX arithmetic | 4 mul.rn.f32 + 4 add.rn.f32 per inner step |
128 fma.rn.f32 |
Both configurations read the same DRAM bytes. The default variant's extra traffic is the per-step accumulator round trip through the output buffer, which stays in L2 and leaves the kernel L2-throughput bound. The grid (2000 workgroups × 32 threads) and the workgroup tile are identical in both variants.
Numerics: the outputs differ by at most 1.04e-7. Against an fp64 reference, the maximum absolute errors are 2.63e-7 (default) and 2.56e-7 (changed). The changed kernel keeps the same reduction order; the difference comes from FMA contraction, which is possible only once the accumulator lives in registers.
Effect on every getMatmulConfig entry
Busy threads are computed as (M / thread_M) * (N / thread_N), compared against the workgroup size:
Tile {M, N, K} |
Workgroup size | Current thread [M, N] |
Busy threads | X→N thread [M, N] |
Busy threads |
|---|---|---|---|---|---|
{64, 128, 64} (subgroup 64 only) |
{64, 16, 1} |
[1, 8] |
1024 / 1024 | [4, 2] |
1024 / 1024 |
{32, 128, 32} |
{32, 8, 1} |
[1, 16] |
256 / 256 | [4, 4] |
256 / 256 |
{128, 64, 8} |
{16, 8, 1} |
[8, 8] |
128 / 128 | [16, 4] |
128 / 128 |
{16, 256, 32} |
{64, 2, 1} |
[1, 128] |
32 / 128 | [8, 4] |
128 / 128 |
{8, 32, 32} |
{8, 8, 1} |
[1, 4] |
64 / 64 | [1, 4] |
64 / 64 |
{32, 128, 4} |
{32, 8, 1} |
[1, 16] |
256 / 256 | [4, 4] |
256 / 256 |
{8, 128, 4} |
{32, 1, 1} |
[1, 128] |
8 / 32 | [8, 4] |
32 / 32 |
{16, 64, 4} |
{16, 2, 1} |
[1, 32] |
32 / 32 | [8, 4] |
32 / 32 |
{1, 128, 8} |
{32, 1, 1} |
[1, 128] |
1 / 32 | [1, 4] |
32 / 32 |
With the X→N mapping every entry uses all of its threads. With the current mapping, three entries leave 75–97% of the workgroup idle, and the other entries assign long one-dimensional N strips (16–128 accumulators) to each thread.
Root cause
compiler/src/iree/compiler/Codegen/LLVMGPU/KernelConfig.cpp, the TileAndFuse branch of setMatmulConfig inside setContractConfig: tileX is the M tile (workgroupTileSizes[numParallelLoops - 2]) and tileY is the N tile, but they are divided by workgroupSize[0] and workgroupSize[1] respectively. The getMatmulConfig table is written with workgroup X along N. The clamp (... < 1) ? 1 : ... turns the resulting under-coverage into silently idle threads, without any diagnostic.
Candidate fix
Divide the M tile by the Y size and the N tile by the X size:
// getMatmulConfig() lays out workgroup x along N and y along M.
threadTileSizes[numParallelLoops - 2] =
std::max<int64_t>(1, tileX / workgroupSize[1]);
threadTileSizes[numParallelLoops - 1] =
std::max<int64_t>(1, tileY / workgroupSize[0]);The very-small-matrix special case in the same function calls setMatmulConfig(sizeN, sizeM, 4, {sizeM, sizeN, 1}, ...). Under the X→N mapping its thread tiles become [1, 1] and the thread count equals sizeM * sizeN. Under the current mapping, unequal sizeM and sizeN also leave threads idle (for example, M = 2, N = 16 gives thread = [8, 1], 4 of 32 threads busy). Reviewers may want to check the argument order of that call at the same time.
Environment
- GPU: NVIDIA H100 NVL, driver 610.57.04; CUDA 13.3
ptxas, Nsight Compute 2026.2.1, Nsight Systems 2026.1.3. - Compiled with
--iree-cuda-target=sm_80and executed on the H100 through driver PTX JIT. With this nightly,--iree-cuda-target=sm_90produces a#hal.executable.target<"cuda", "cuda-nvptx-fb">withoutiree_codegen.target_infoand fails withmissing GPU target in #hal.executable.target. That appears to be unrelated to this report. The configuration path above only depends on the subgroup size (32) and on f32 having no MMA intrinsic.
Validation scope
- The default configuration, the attribute-only variant, the PTX, the outputs, and all timings above were produced with the upstream nightly packages listed above.
- The candidate source change has not been built. The
thread = [1, 8, 4, 0]variant is what the candidate fix computes for this shape, applied through--iree-hal-substitute-executable-configuration. - Only the
{8, 128, 4}entry was measured. The other rows of the entry table are static thread-count arithmetic; their performance under the new mapping was not measured.
AI assistance disclosure
Claude Code (Anthropic) assisted with locating the configuration path, reducing the standalone reproducer, running the controlled comparison and profiling commands, checking the upstream source, and drafting this report.
Activity
github-actions commentedon Nov 4, 2026
Hello @Cerulime 👋
Thank you for submitting an issue to IREE! It looks like it's your first one.
The volume of issues is huge, and triage times may vary. If you need quicker assistance on the issue, consider posting it on IREE Discord and providing short context there.
Feel free to identify the most frequent contributors to the corresponding part of the codebase via Git history/CODEOWNERS and (gently!) tag them below.
Action required: acknowledge IREE project policies
IREE is a Linux Foundation project. All participants are expected to follow the LF Projects Code of Conduct.
Please note that all contributions to IREE, including GitHub issues, must follow our IREE AI Tool Use Policy. This means:
For bug reports, specifics of the use-case, workflow and/or crash scenario usually suffice.
For improvement suggestions, this usually implies deeper understanding of suboptimal code, IR design choices, assumed performance tradeoffs, etc.
Before the issue can be triaged, we kindly ask you to reply to this message and confirm that you understand and accept the proposed policy. As required, please adjust your initial issue description.
If you have any questions, feel free to leave a comment here, or ask away on IREE Discord.
Thank you,
The IREE Community
Cerulime commentedon Nov 4, 2026
I understand it.
Cerulime commentedon Nov 4, 2026
For test:
Supplement: lit tests affected by the candidate fix
With the candidate fix, five CHECK lines in four upstream lit tests (at
5144a283) need updating. Only thethreadtile changes in each case. The selectedworkgrouptile andworkgroup_sizestay the same, and the thread count still equals the workgroup size.compiler/src/iree/compiler/Codegen/LLVMGPU/test/){M, N, K}/ workgroup sizethreadconfig_matmul.mlirmatmul_32_32_DYN{64, 128, 64}/{64, 16, 1}[1, 8, 0][4, 2, 0]config_matmul.mlirmatmul_4096_4096_DYN{64, 128, 64}/{64, 16, 1}[1, 8, 0][4, 2, 0]config_matvec.mlirnot_vmt{64, 128, 64}/{64, 16, 1}[1, 8, 0][4, 2, 0]NVVM/config_tile_and_fuse_sm80.mlirmixed_precision_matmul_36x151936x896_f32xbf16xf32{32, 128, 32}/{32, 8, 1}[1, 16, 0][4, 4, 0]NVVM/config_tile_and_fuse_sm120.mlirmixed_precision_matmul_36x151936x896_f32xbf16xf32{32, 128, 32}/{32, 8, 1}[1, 16, 0][4, 4, 0]The new values follow from
thread_M = max(1, M / workgroup_y)andthread_N = max(1, N / workgroup_x):64 / 16 = 4,128 / 64 = 2and32 / 8 = 4,128 / 32 = 4.Corresponding test edits:
Two more test inputs produce a different
threadtile but do not CHECK it, so they need no change:ROCDL/config_vector_distribute_gfx942.mlir,matmul_256x256x256:[1, 8, 0]becomes[4, 2, 0]; the test only hasCHECK-NOTfor theVectorDistributepipeline.config_complex_matmul.mlir,complex_batch_matmul:[1, 1, 16, 0]becomes[1, 4, 4, 0]; the test only checks theTileAndFusepipeline, the presence of alowering_config, and the absence ofmma_kind.How the list was obtained: the
iree-optRUN pipelines of all 57 upstream test files that run LLVMGPU configuration selection were executed twice, once with the upstream nightlyiree-optand once with a build containing the candidate fix. The{reduction, thread, workgroup}configs this path emits were then compared. That build also carries unrelated downstream changes, and the differences they cause were excluded. Where they change the selected entry (matmul_32_32_DYNonly), the new value above is computed from the entry upstream selects. Two further test files CHECKthread =tiles, but they do not run configuration selection (Common/GPU/test/gpu_pack_partial_reductions.mlir,Dialect/GPU/IR/test/iree_gpu_attrs.mlir), so they are unaffected. lit itself was not run against an upstream build with the fix.