| Issue |
244381
|
| Summary |
[fuzzer-found][mlir][remove-dead-values] `scf.reduce` operand replaced with `ub.poison` while the `scf.parallel` result is live
|
| Labels |
mlir
|
| Assignees |
|
| Reporter |
quiet-maple-47
|
## Description
`--remove-dead-values` replaces the value fed to `scf.reduce` with `ub.poison` even though the function returns the `scf.parallel` result. The sum of 0 through 3 then prints `0` instead of `6`.
I ran into this today while debugging our fuzzer, so I'm filing it on its own instead of holding it for the next batch.
## Reproducer
Save as `input.mlir`.
```mlir
// sum(n) = 0 + 1 + ... + (n-1)
func.func @sum(%n: index) -> i32 {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%zero = arith.constant 0 : i32
%s = scf.parallel (%i) = (%c0) to (%n) step (%c1) init (%zero) -> i32 {
%v = arith.index_cast %i : index to i32 // <~ the value being summed
scf.reduce(%v : i32) { // <~ reduces %v into %s
^bb0(%a: i32, %b: i32):
%z = arith.addi %a, %b : i32
scf.reduce.return %z : i32
}
}
cf.br ^bb1(%s : i32) // <~ %s is live: it reaches the return
^bb1(%r: i32): // through a block argument
return %r : i32
}
func.func @main() {
%c4 = arith.constant 4 : index
%r = call @sum(%c4) : (index) -> i32
vector.print %r : i32 // <~ expected 6 (0 + 1 + 2 + 3)
return
}
```
```sh
mlir-opt --remove-dead-values input.mlir
```
[Compiler Explorer (LLVM trunk, `@sum` only)](https://godbolt.org/z/hs8foM1Ej)
## Actual Behavior
The pass deletes `%v` and feeds poison to the reduction:
```mlir
%0 = scf.parallel (%arg1) = (%c0) to (%arg0) step (%c1) init (%c0_i32) -> i32 {
%2 = ub.poison : i32 // <~ was %v = arith.index_cast %arg1
scf.reduce(%2 : i32) { // <~ now sums poison
```
At run time, `sum(4)` prints `0` instead of `6`:
```bash
LOWER="--convert-scf-to-cf --convert-ub-to-llvm --convert-vector-to-llvm --convert-arith-to-llvm --convert-cf-to-llvm --convert-func-to-llvm --reconcile-unrealized-casts"
RUN="mlir-runner -e main -entry-point-result=void -shared-libs=<llvm>/lib/libmlir_c_runner_utils.so"
mlir-opt $LOWER input.mlir | $RUN # 6
mlir-opt --remove-dead-values $LOWER input.mlir | $RUN # 0 <~ wrong
```
## Expected Behavior
The pass should preserve `%v = arith.index_cast %i : index to i32` and `scf.reduce(%v : i32)` so that `sum(4)` still prints `6`.
## Root Cause
- Liveness analysis visits `scf.reduce` before it knows the loop result is live. [`visitBranchOperand`](https://github.com/llvm/llvm-project/blob/3b4125a22c50c702b6593e0b366ceebf8f0e3887/mlir/lib/Analysis/DataFlow/LivenessAnalysis.cpp#L127-L162) registers a [dependency](https://github.com/llvm/llvm-project/blob/3b4125a22c50c702b6593e0b366ceebf8f0e3887/mlir/lib/Analysis/DataFlow/LivenessAnalysis.cpp#L122) for the parent `scf.parallel` only.
- When the result becomes live, the solver [revisits the loop](https://github.com/llvm/llvm-project/blob/3b4125a22c50c702b6593e0b366ceebf8f0e3887/mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp#L622-L657) and skips `scf.reduce`. Its operand stays marked dead.
- `scf.yield` forwards its operands, which [registers a dependency](https://github.com/llvm/llvm-project/blob/3b4125a22c50c702b6593e0b366ceebf8f0e3887/mlir/lib/Analysis/DataFlow/SparseAnalysis.cpp#L660-L686) to revisit the terminator. `scf.reduce` forwards none because [`ReduceOp::getMutableSuccessorOperands`](https://github.com/llvm/llvm-project/blob/3b4125a22c50c702b6593e0b366ceebf8f0e3887/mlir/lib/Dialect/SCF/IR/SCF.cpp#L3362-L3366) returns an empty range.
One possible fix is for `visitBranchOperand`, when the owner is a region branch terminator, to register the program point after the terminator as a dependent of the parent's result lattices so the solver revisits the terminator.
## Version
Homebrew LLVM 23.1.6 (macOS arm64) and LLVM trunk on Compiler Explorer. Source inspected on `main` at `3b4125a22c50` (2026-11-19).
## Claim
This investigation and write-up were AI-assisted. I've reviewed and verified everything above, and I'm happy to answer questions.
**Assisted-by:** Claude Code:claude-fable-5-2, Claude Code:claude-opus-5-5, Codex:gpt-6.2-sol, Codex:gpt-6.2-astra
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs