Skip to content

[Codegen] [LLVMGPU] SIMT contraction config divides the M tile by workgroup X, leaving most TileAndFuse threads idle #25191

Open

Description

@Cerulime

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.vmfb

Create 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.vmfb

Inputs, 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
done

Kernel-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_80 and executed on the H100 through driver PTX JIT. With this nightly, --iree-cuda-target=sm_90 produces a #hal.executable.target<"cuda", "cuda-nvptx-fb"> without iree_codegen.target_info and fails with missing 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

  1. added
    bug 🐞
    on Nov 4, 2026
  2. github-actions commented on Nov 4, 2026

    @github-actions

    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:

    • Issue reporters must fully understand the primary issue and the impact of the problem.
      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.
    • Contributors must write the summary of the issue themselves, so that the exact impact and scenario is clear to other participants.
    • Issue authors are expected to review and understand all LLM/AI-assisted segments of the report, if any. E.g.: CLI commands and IR reproducers, model/framework details, lowering pipeline details, assumptions about problematic behaviors of IREE components.
    • Substantial use of LLM/generative AI tools must be disclaimed in the header or footer of the issue description.

    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

  3. Cerulime commented on Nov 4, 2026

    @Cerulime
    Author

    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:

    • Issue reporters must fully understand the primary issue and the impact of the problem.
      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.
    • Contributors must write the summary of the issue themselves, so that the exact impact and scenario is clear to other participants.
    • Issue authors are expected to review and understand all LLM/AI-assisted segments of the report, if any. E.g.: CLI commands and IR reproducers, model/framework details, lowering pipeline details, assumptions about problematic behaviors of IREE components.
    • Substantial use of LLM/generative AI tools must be disclaimed in the header or footer of the issue description.

    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

    I understand it.

  4. Cerulime commented on Nov 4, 2026

    @Cerulime
    Author

    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 the thread tile changes in each case. The selected workgroup tile and workgroup_size stay the same, and the thread count still equals the workgroup size.

    Test file (compiler/src/iree/compiler/Codegen/LLVMGPU/test/) Line Function Entry {M, N, K} / workgroup size Current thread With fix
    config_matmul.mlir 158 matmul_32_32_DYN {64, 128, 64} / {64, 16, 1} [1, 8, 0] [4, 2, 0]
    config_matmul.mlir 173 matmul_4096_4096_DYN {64, 128, 64} / {64, 16, 1} [1, 8, 0] [4, 2, 0]
    config_matvec.mlir 439 not_vmt {64, 128, 64} / {64, 16, 1} [1, 8, 0] [4, 2, 0]
    NVVM/config_tile_and_fuse_sm80.mlir 134 mixed_precision_matmul_36x151936x896_f32xbf16xf32 {32, 128, 32} / {32, 8, 1} [1, 16, 0] [4, 4, 0]
    NVVM/config_tile_and_fuse_sm120.mlir 129 mixed_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) and thread_N = max(1, N / workgroup_x): 64 / 16 = 4, 128 / 64 = 2 and 32 / 8 = 4, 128 / 32 = 4.

    Corresponding test edits:

    --- a/compiler/src/iree/compiler/Codegen/LLVMGPU/test/config_matmul.mlir
    +++ b/compiler/src/iree/compiler/Codegen/LLVMGPU/test/config_matmul.mlir
    @@ -158 +158 @@
    -   // CHECK:     #iree_gpu.lowering_config<{reduction = [0, 0, 1], thread = [1, 8, 0], workgroup = [64, 128, 1]}
    +   // CHECK:     #iree_gpu.lowering_config<{reduction = [0, 0, 1], thread = [4, 2, 0], workgroup = [64, 128, 1]}
    @@ -173 +173 @@
    -   // CHECK:      #iree_gpu.lowering_config<{reduction = [0, 0, 1], thread = [1, 8, 0], workgroup = [64, 128, 1]}
    +   // CHECK:      #iree_gpu.lowering_config<{reduction = [0, 0, 1], thread = [4, 2, 0], workgroup = [64, 128, 1]}
    --- a/compiler/src/iree/compiler/Codegen/LLVMGPU/test/config_matvec.mlir
    +++ b/compiler/src/iree/compiler/Codegen/LLVMGPU/test/config_matvec.mlir
    @@ -439 +439 @@
    -//  CHECK-SAME:       lowering_config = #iree_gpu.lowering_config<{reduction = [0, 0, 64], thread = [1, 8, 0], workgroup = [64, 128, 1]}>
    +//  CHECK-SAME:       lowering_config = #iree_gpu.lowering_config<{reduction = [0, 0, 64], thread = [4, 2, 0], workgroup = [64, 128, 1]}>
    --- a/compiler/src/iree/compiler/Codegen/LLVMGPU/test/NVVM/config_tile_and_fuse_sm80.mlir
    +++ b/compiler/src/iree/compiler/Codegen/LLVMGPU/test/NVVM/config_tile_and_fuse_sm80.mlir
    @@ -134 +134 @@
    -//  CHECK-SAME:     thread = [1, 16, 0]
    +//  CHECK-SAME:     thread = [4, 4, 0]
    --- a/compiler/src/iree/compiler/Codegen/LLVMGPU/test/NVVM/config_tile_and_fuse_sm120.mlir
    +++ b/compiler/src/iree/compiler/Codegen/LLVMGPU/test/NVVM/config_tile_and_fuse_sm120.mlir
    @@ -129 +129 @@
    -//  CHECK-SAME:     thread = [1, 16, 0]
    +//  CHECK-SAME:     thread = [4, 4, 0]

    Two more test inputs produce a different thread tile 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 has CHECK-NOT for the VectorDistribute pipeline.
    • config_complex_matmul.mlir, complex_batch_matmul: [1, 1, 16, 0] becomes [1, 4, 4, 0]; the test only checks the TileAndFuse pipeline, the presence of a lowering_config, and the absence of mma_kind.

    How the list was obtained: the iree-opt RUN pipelines of all 57 upstream test files that run LLVMGPU configuration selection were executed twice, once with the upstream nightly iree-opt and 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_DYN only), the new value above is computed from the entry upstream selects. Two further test files CHECK thread = 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐞

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions