| Issue |
242612
|
| Summary |
[mlir][scf] scf-parallel-loop-fusion fuses loops with write-after-write dependences and across ops with unknown memory effects (miscompile)
|
| Labels |
mlir
|
| Assignees |
|
| Reporter |
wildoranges
|
The legality check of `-scf-parallel-loop-fusion`, `noIncompatibleDataDependencies` → `haveNoDataDependenciesExceptSameIndex`, misses two kinds of cross-loop dependences. In both cases the pass fuses two loops whose fused body has a data race, or reads data before it is written, which changes the result. (The `scf.parallel` docs say: "If there are data races, the behavior is undefined.")
(`$LLVM_BUILD` is the LLVM/MLIR build directory.)
```console
$ LOWER="-convert-scf-to-cf -convert-cf-to-llvm -finalize-memref-to-llvm -convert-arith-to-llvm -convert-index-to-llvm -convert-func-to-llvm -convert-ub-to-llvm -reconcile-unrealized-casts"
$ RUN="mlir-runner -e main -entry-point-result=void -shared-libs=$LLVM_BUILD/lib/libmlir_runner_utils.so,$LLVM_BUILD/lib/libmlir_c_runner_utils.so"
```
### 1. Write-after-write between the loops is never checked
`pf_waw.mlir`:
```mlir
func.func private @printMemrefI32(memref<*xi32>)
func.func @kernel(%A: memref<5xi32>) {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%c4 = arith.constant 4 : index
%_one_ = arith.constant 1 : i32
%two = arith.constant 2 : i32
scf.parallel (%i) = (%c0) to (%c4) step (%c1) {
memref.store %one, %A[%i] : memref<5xi32>
scf.reduce
}
scf.parallel (%i) = (%c0) to (%c4) step (%c1) {
%ip1 = arith.addi %i, %c1 : index
memref.store %two, %A[%ip1] : memref<5xi32>
scf.reduce
}
return
}
func.func @main() {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%c5 = arith.constant 5 : index
%z = arith.constant 0 : i32
%A = memref.alloc() : memref<5xi32>
scf.for %i = %c0 to %c5 step %c1 {
memref.store %z, %A[%i] : memref<5xi32>
}
call @kernel(%A) : (memref<5xi32>) -> ()
%u = memref.cast %A : memref<5xi32> to memref<*xi32>
call @printMemrefI32(%u) : (memref<*xi32>) -> ()
memref.dealloc %A : memref<5xi32>
return
}
```
```console
$ mlir-opt pf_waw.mlir $LOWER | $RUN
[1, 2, 2, 2, 2]
$ mlir-opt pf_waw.mlir -pass-pipeline='builtin.module(func.func(scf-parallel-loop-fusion))' | mlir-opt $LOWER | $RUN
[1, 1, 1, 1, 2]
```
The reference prints `[1, 2, 2, 2, 2]`; the fused version prints `[1, 1, 1, 1, 2]`. The fused body is `memref.store 1, A[%i]; memref.store 2, A[%i + 1]`, so iterations i and i+1 now race on `A[i+1]`.
The checker only examines ops of the *other* loop that have a `Read` effect, so a store in loop 2 is never compared against the stores of loop 1. It runs in both directions, which covers RAW and WAR, but not WAW:
https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/lib/Dialect/SCF/Transforms/ParallelLoopFusion.cpp#L653-L668
https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/lib/Dialect/SCF/Transforms/ParallelLoopFusion.cpp#L724-L741
### 2. Ops without `MemoryEffectOpInterface` (e.g. `func.call`) are treated as effect-free
`pf_call.mlir`:
```mlir
func.func private @printMemrefI32(memref<*xi32>)
func.func @set(%A: memref<4xi32>, %i: index) {
%v = arith.index_cast %i : index to i32
%c10 = arith.constant 10 : i32
%w = arith.addi %v, %c10 : i32
memref.store %w, %A[%i] : memref<4xi32>
return
}
func.func @kernel(%A: memref<4xi32>, %B: memref<4xi32>) {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%c3 = arith.constant 3 : index
%c4 = arith.constant 4 : index
scf.parallel (%i) = (%c0) to (%c4) step (%c1) {
func.call @set(%A, %i) : (memref<4xi32>, index) -> () // A[i] = i + 10
scf.reduce
}
scf.parallel (%i) = (%c0) to (%c4) step (%c1) {
%j = arith.subi %c3, %i : index
%v = memref.load %A[%j] : memref<4xi32> // B[i] = A[3 - i]
memref.store %v, %B[%i] : memref<4xi32>
scf.reduce
}
return
}
func.func @main() {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%c4 = arith.constant 4 : index
%z = arith.constant 0 : i32
%A = memref.alloc() : memref<4xi32>
%B = memref.alloc() : memref<4xi32>
scf.for %i = %c0 to %c4 step %c1 {
memref.store %z, %A[%i] : memref<4xi32>
memref.store %z, %B[%i] : memref<4xi32>
}
call @kernel(%A, %B) : (memref<4xi32>, memref<4xi32>) -> ()
%u = memref.cast %B : memref<4xi32> to memref<*xi32>
call @printMemrefI32(%u) : (memref<*xi32>) -> ()
return
}
```
```console
$ mlir-opt pf_call.mlir $LOWER | $RUN
[13, 12, 11, 10]
$ mlir-opt pf_call.mlir -pass-pipeline='builtin.module(func.func(scf-parallel-loop-fusion))' | mlir-opt $LOWER | $RUN
[0, 0, 11, 10]
```
The reference prints `[13, 12, 11, 10]`; the fused version prints `[0, 0, 11, 10]`.
When collecting the writes of the first loop, any op without `MemoryEffectOpInterface` is skipped ("Ignore ops that don't write to memory"):
https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/lib/Dialect/SCF/Transforms/ParallelLoopFusion.cpp#L618-L623
The read side has the same behaviour, documented as a TODO ("for now we just skip them"):
https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/lib/Dialect/SCF/Transforms/ParallelLoopFusion.cpp#L654-L661
An op with neither the interface nor `HasRecursiveMemoryEffects` has *unknown* effects; `isMemoryEffectFree` returns `false` for it.
Both inputs verify and are UB-free: accesses are in bounds, buffers are zero-initialized, and each original loop is race-free on its own.
Possible fixes:
- (1) also compare stores of the second loop against the buffers written by the first loop (in both directions);
- (2) treat ops with unknown effects as blockers, at least on the write-collection side.
**Version**: llvm-project main @ 018b722b2d48cf42dccded46e1226f1d9c29f339 (2026-10-17), Release + assertions, x86_64 Linux. Also reproduced on main @ a60677dca3a8536a0cf5bec1b846610014a983fd (2026-11-12).
*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