| Issue |
234080
|
| Summary |
[mlir][gpu] -control-flow-sink moves gpu.shuffle / gpu.ballot into lane-divergent scf.if (wrong results on GPU)
|
| Labels |
mlir
|
| Assignees |
|
| Reporter |
wildoranges
|
`-control-flow-sink` moves any op that `isMemoryEffectFree` into a region that executes at most once. Cross-lane GPU ops pass that check: `gpu.shuffle` is `[Pure]`, `gpu.ballot` and `gpu.subgroup_reduce` are `[NoMemoryEffect]`, and `nvgpu.mma.sync` is `[Pure]`. So they get sunk into an `scf.if` whose condition depends on the lane id. Only part of the subgroup then executes them, which breaks the documented requirement and changes the results.
For example, the `gpu.shuffle` description says: "the first `width` lanes of the subgroup must be active".
`shuffle_sink.mlir`:
```mlir
module attributes {gpu.container_module} {
func.func @main() {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%c32 = arith.constant 32 : index
%out = gpu.alloc host_shared () : memref<2x32xi32>
gpu.launch blocks(%bx, %by, %bz) in (%gx = %c1, %gy = %c1, %gz = %c1)
threads(%tx, %ty, %tz) in (%sx = %c32, %sy = %c1, %sz = %c1) {
%lane = arith.index_cast %tx : index to i32
%i1 = arith.constant 1 : i32
%i10 = arith.constant 10 : i32
%i16 = arith.constant 16 : i32
%i32 = arith.constant 32 : i32
%v = arith.muli %lane, %i10 : i32
// executed by all 32 lanes
%s, %valid = gpu.shuffle xor %v, %i16, %i32 : i32
%odd = arith.andi %lane, %i1 : i32
%pred = arith.cmpi eq, %odd, %i1 : i32
%b = gpu.ballot %pred : i32
%cond = arith.cmpi ult, %lane, %i16 : i32
scf.if %cond {
memref.store %s, %out[%c0, %tx] : memref<2x32xi32>
memref.store %b, %out[%c1, %tx] : memref<2x32xi32>
} else {
memref.store %lane, %out[%c0, %tx] : memref<2x32xi32>
memref.store %lane, %out[%c1, %tx] : memref<2x32xi32>
}
gpu.terminator
}
%u = memref.cast %out : memref<2x32xi32> to memref<*xi32>
call @printMemrefI32(%u) : (memref<*xi32>) -> ()
return
}
func.func private @printMemrefI32(memref<*xi32>)
}
```
(`$LLVM_BUILD` is an LLVM/MLIR build with `MLIR_ENABLE_CUDA_RUNNER=ON`; `CUDA_HOME` must point to a CUDA toolkit with libdevice.)
```console
$ RUN="mlir-runner -e main -entry-point-result=void -shared-libs=$LLVM_BUILD/lib/libmlir_cuda_runtime.so,$LLVM_BUILD/lib/libmlir_runner_utils.so,$LLVM_BUILD/lib/libmlir_c_runner_utils.so"
$ mlir-opt shuffle_sink.mlir -gpu-lower-to-nvvm-pipeline="cubin-chip=sm_86 cubin-format=isa" | $RUN
[[160, 170, 180, ..., 310, 16, 17, ..., 31],
[-1431655766 (x16), 16, 17, ..., 31]] # ballot = 0xAAAAAAAA, as expected
$ mlir-opt shuffle_sink.mlir -control-flow-sink -gpu-lower-to-nvvm-pipeline="cubin-chip=sm_86 cubin-format=isa" | $RUN
[[0 (x16), 16, 17, ..., 31],
[43690 (x16), 16, 17, ..., 31]] # shuffle reads inactive lanes; ballot = 0x0000AAAA
```
After `-control-flow-sink`, both ops sit inside `scf.if %1 { ... }` (`%1 = lane < 16`):
```mlir
scf.if %1 {
%2 = arith.muli %0, %c10_i32 : i32
%shuffleResult, %valid = gpu.shuffle xor %2, %c16_i32, %c32_i32 : i32
...
%5 = gpu.ballot %4 : i32
memref.store %shuffleResult, %memref[%c0, %arg3] : memref<2x32xi32>
memref.store %5, %memref[%c1, %arg3] : memref<2x32xi32>
} else { ... }
```
I see the same with `gpu.subgroup_reduce add uniform` (sum over 32 lanes 496 → 120), lowered with `convert-gpu-to-nvvm{has-redux=1}`, and with `nvgpu.mma.sync` (16 → 0 on the affected lanes).
The input verifies and is UB-free: all 32 lanes execute the cross-lane ops unconditionally, `width` = 32 is uniform, and each lane stores to its own element.
Relevant code:
- the sinking predicate: https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/lib/Transforms/ControlFlowSink.cpp#L46
- `gpu.shuffle` traits and doc: https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/include/mlir/Dialect/GPU/IR/GPUOps.td#L1331-L1343
- the GPU dialect already reasons this way for speculation: `SubgroupBroadcastOp::getSpeculatability` says "speculating it across control flow can change the active lanes". Sinking into divergent control flow has the same problem, but it is gated only by memory effects.
https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/lib/Dialect/GPU/IR/GPUDialect.cpp#L2682-L2687
Related: #152358 (open) proposes a `Convergent` trait, but it does not mention any MLIR pass that moves these ops.
A possible fix is to model convergence and make `-control-flow-sink` respect it, so that sinking cannot change the participating lanes. As an interim measure, the pass could conservatively refuse to sink known cross-lane operations. Merely replacing `Pure` with `NoMemoryEffect` and conditional speculatability would not block the current pass: its legality predicate checks only `isMemoryEffectFree`, not speculatability. The sink legality check must also account for convergence. I have not tested these fixes.
**Version**: llvm-project main @ 018b722b2d48cf42dccded46e1226f1d9c29f339 (2026-10-17), Release + assertions, x86_64 Linux. Executed on an NVIDIA RTX 3090 (sm_86), CUDA driver 595.80. Also reproduced on main @ 4c407e843c975298b6892d50afd9e72cfb0cbe46 (2026-10-20).
*Disclosure: I wrote this report with the help of Claude Code. I reproduced and reviewed everything in this report myself.*
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs