[mlir][scf] WhileMoveIfDown canonicalization miscompiles duplicated scf.condition operands
The WhileMoveIfDown canonicalization of scf.while produces wrong results when a
single scf.if result is forwarded to more than one scf.condition operand. The
after-region argument for the second (and later) occurrence is bound to the scf.if
else value, even though the after region executes only when the condition is
true.
The resulting IR is verifier-clean, so this is a silent wrong-value miscompilation
reachable through plain -canonicalize.
Reproducer
func.func @duplicate_forward() -> index {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%c2 = arith.constant 2 : index
%c20 = arith.constant 20 : index
%result:3 = scf.while (%state = %c0) : (index) -> (index, index, index) {
%continue = arith.cmpi slt, %state, %c1 : index
%value = scf.if %continue -> index {
%next = arith.addi %state, %c2 : index
scf.yield %next : index
} else {
scf.yield %c20 : index
}
scf.condition(%continue) %value, %value, %state : index, index, index
} do {
^bb0(%first: index, %second: index, %old: index):
scf.yield %second : index
}
return %result#2 : index
}
mlir-opt -canonicalize repro.mlir
Expected vs actual
Source semantics (scf.while / scf.condition): when the condition is true the
operands become the after-region arguments; when it is false they become the loop
results. So with state starting at 0:
| iteration | state |
continue (state < 1) |
%value |
after args | next state |
|---|---|---|---|---|---|
| 0 | 0 | true | 0 + 2 = 2 |
(2, 2, 0) |
%second = 2 |
| 1 | 2 | false | (else) | — | results = (20, 20, 2) |
return %result#2 is therefore 2.
After -canonicalize, the after region yields the else constant instead:
%0 = scf.while (%arg0 = %c0) : (index) -> index {
%1 = arith.cmpi slt, %arg0, %c1 : index
scf.condition(%1) %arg0 : index
} do {
^bb0(%arg0: index):
scf.yield %c20 : index // <-- else value drives the recurrence
}
return %0 : index
Executed end to end (-convert-scf-to-cf -convert-to-llvm -reconcile-unrealized-casts
into mlir-runner):
without -canonicalize : 2 (correct)
with -canonicalize : 20 (wrong)
Environment
- llvm-project at
2b042d988fe4(also reproduces at10470737d982,1829cfa8daf2) - Release build with assertions, Linux x86-64
@llvm/issue-subscribers-mlir
Author: Samarth Narang (snarang181)
The resulting IR is verifier-clean, so this is a silent wrong-value miscompilation
reachable through plain -canonicalize.
Reproducer
func.func @<!-- -->duplicate_forward() -> index {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%c2 = arith.constant 2 : index
%c20 = arith.constant 20 : index
%result:3 = scf.while (%state = %c0) : (index) -> (index, index, index) {
%continue = arith.cmpi slt, %state, %c1 : index
%value = scf.if %continue -> index {
%next = arith.addi %state, %c2 : index
scf.yield %next : index
} else {
scf.yield %c20 : index
}
scf.condition(%continue) %value, %value, %state : index, index, index
} do {
^bb0(%first: index, %second: index, %old: index):
scf.yield %second : index
}
return %result#<!-- -->2 : index
}
mlir-opt -canonicalize repro.mlir
Expected vs actual
Source semantics (scf.while / scf.condition): when the condition is true the
operands become the after-region arguments; when it is false they become the loop
results. So with state starting at 0:
| iteration | state |
continue (state < 1) |
%value |
after args | next state |
|---|---|---|---|---|---|
| 0 | 0 | true | 0 + 2 = 2 |
(2, 2, 0) |
%second = 2 |
| 1 | 2 | false | (else) | — | results = (20, 20, 2) |
return %result#<!-- -->2 is therefore 2.
After -canonicalize, the after region yields the else constant instead:
%0 = scf.while (%arg0 = %c0) : (index) -> index {
%1 = arith.cmpi slt, %arg0, %c1 : index
scf.condition(%1) %arg0 : index
} do {
^bb0(%arg0: index):
scf.yield %c20 : index // <-- else value drives the recurrence
}
return %0 : index
Executed end to end (-convert-scf-to-cf -convert-to-llvm -reconcile-unrealized-casts
into mlir-runner):
without -canonicalize : 2 (correct)
with -canonicalize : 20 (wrong)
Environment
- llvm-project at
2b042d988fe4(also reproduces at10470737d982,1829cfa8daf2) - Release build with assertions, Linux x86-64