[mlir][scf] Loop peeling makes a dynamic zero-trip loop execute, because the bail-out guards are constant-only (both directions)
Summary
-scf-for-loop-peeling and -scf-for-loop-peeling=peel-front=true both split an
scf.for into a main loop and a partial iteration. Each has a guard that bails
out when peeling is unnecessary, but both guards require every bound to be a
bare constant. When any bound is dynamic the guard is skipped and the partial
iteration is emitted unconditionally — including for loops whose trip count is
zero.
scf.for's contract defines the trip count as max(0, ceil((ub - lb) / step)),
so a loop with lb >= ub must not execute at all. After peeling it does.
Front peeling, LoopSpecialization.cpp:236:
// Peeling is not needed if there is one or less iteration.
if (lbInt && ubInt && stepInt && ceil(float(*ubInt - *lbInt) / *stepInt) <= 1)
return failure();
Back peeling, LoopSpecialization.cpp:137:
// No specialization necessary if step already divides upper bound evenly.
// Fast path: lb, ub and step are constants.
if (lbInt && ubInt && stepInt && (*ubInt - *lbInt) % *stepInt == 0)
return failure();
lbInt, ubInt and stepInt all come from getConstantIntValue, so an
ordinary dynamic bound makes each condition false and execution falls through to
the peeling transform. The resulting IR is verifier-clean, so this is a silent
wrong-result miscompilation.
Both reproduce at 60b5f9a06722.
Reproducer
With lb = 1, ub = 0, step = 2 the trip count is max(0, ceil(-1/2)) = 0, so
the body must never run and the result must be 0. The bounds are function
arguments, which is what defeats the guards.
func.func @count(%lb: index, %ub: index) -> i32 {
%c0 = arith.constant 0 : index
%c2 = arith.constant 2 : index
%one = arith.constant 1 : i32
%zero = arith.constant 0 : i32
%m = memref.alloca() : memref<1xi32>
memref.store %zero, %m[%c0] : memref<1xi32>
scf.for %i = %lb to %ub step %c2 {
%o = memref.load %m[%c0] : memref<1xi32>
%n = arith.addi %o, %one : i32
memref.store %n, %m[%c0] : memref<1xi32>
}
%r = memref.load %m[%c0] : memref<1xi32>
return %r : i32
}
func.func @main() {
%lb = arith.constant 1 : index
%ub = arith.constant 0 : index
%r = func.call @count(%lb, %ub) : (index, index) -> i32
vector.print %r : i32
return
}
PIPE="-lower-affine -convert-scf-to-cf -convert-cf-to-llvm -convert-vector-to-llvm \
-finalize-memref-to-llvm -convert-func-to-llvm -convert-arith-to-llvm \
-reconcile-unrealized-casts"
mlir-opt $PIPE repro.mlir | mlir-runner -e main -entry-point-result=void \
--shared-libs=$LIB/libmlir_runner_utils.so
# => 0 (correct: the loop is empty)
mlir-opt -scf-for-loop-peeling=peel-front=true $PIPE repro.mlir | mlir-runner ...
# => 1 (wrong: the peeled first iteration ran)
mlir-opt -scf-for-loop-peeling $PIPE repro.mlir | mlir-runner ...
# => 1 (wrong: the partial iteration ran)
The same defect is reachable through transform.loop.peel.
Why the guards do not catch it
Both guards are written as fast paths for the fully-constant case and have no
fallback. Nothing establishes ub > lb before the partial iteration is emitted,
so for any runtime state with lb >= ub the peeled iteration executes a body the
source never executes.
Environment
- llvm-project at
60b5f9a06722 - Release build with assertions, Linux x86-64
@llvm/issue-subscribers-mlir
Author: Samarth Narang (snarang181)
-scf-for-loop-peeling and -scf-for-loop-peeling=peel-front=true both split an
scf.for into a main loop and a partial iteration. Each has a guard that bails
out when peeling is unnecessary, but both guards require every bound to be a
bare constant. When any bound is dynamic the guard is skipped and the partial
iteration is emitted unconditionally — including for loops whose trip count is
zero.
scf.for's contract defines the trip count as max(0, ceil((ub - lb) / step)),
so a loop with lb >= ub must not execute at all. After peeling it does.
Front peeling, LoopSpecialization.cpp:236:
// Peeling is not needed if there is one or less iteration.
if (lbInt && ubInt && stepInt && ceil(float(*ubInt - *lbInt) / *stepInt) <= 1)
return failure();
Back peeling, LoopSpecialization.cpp:137:
// No specialization necessary if step already divides upper bound evenly.
// Fast path: lb, ub and step are constants.
if (lbInt && ubInt && stepInt && (*ubInt - *lbInt) % *stepInt == 0)
return failure();
lbInt, ubInt and stepInt all come from getConstantIntValue, so an
ordinary dynamic bound makes each condition false and execution falls through to
the peeling transform. The resulting IR is verifier-clean, so this is a silent
wrong-result miscompilation.
Both reproduce at 60b5f9a06722.
Reproducer — confirmed by execution
With lb = 1, ub = 0, step = 2 the trip count is max(0, ceil(-1/2)) = 0, so
the body must never run and the result must be 0. The bounds are function
arguments, which is what defeats the guards.
func.func @<!-- -->count(%lb: index, %ub: index) -> i32 {
%c0 = arith.constant 0 : index
%c2 = arith.constant 2 : index
%one = arith.constant 1 : i32
%zero = arith.constant 0 : i32
%m = memref.alloca() : memref<1xi32>
memref.store %zero, %m[%c0] : memref<1xi32>
scf.for %i = %lb to %ub step %c2 {
%o = memref.load %m[%c0] : memref<1xi32>
%n = arith.addi %o, %one : i32
memref.store %n, %m[%c0] : memref<1xi32>
}
%r = memref.load %m[%c0] : memref<1xi32>
return %r : i32
}
func.func @<!-- -->main() {
%lb = arith.constant 1 : index
%ub = arith.constant 0 : index
%r = func.call @<!-- -->count(%lb, %ub) : (index, index) -> i32
vector.print %r : i32
return
}
PIPE="-lower-affine -convert-scf-to-cf -convert-cf-to-llvm -convert-vector-to-llvm \
-finalize-memref-to-llvm -convert-func-to-llvm -convert-arith-to-llvm \
-reconcile-unrealized-casts"
mlir-opt $PIPE repro.mlir | mlir-runner -e main -entry-point-result=void \
--shared-libs=$LIB/libmlir_runner_utils.so
# => 0 (correct: the loop is empty)
mlir-opt -scf-for-loop-peeling=peel-front=true $PIPE repro.mlir | mlir-runner ...
# => 1 (wrong: the peeled first iteration ran)
mlir-opt -scf-for-loop-peeling $PIPE repro.mlir | mlir-runner ...
# => 1 (wrong: the partial iteration ran)
The same defect is reachable through transform.loop.peel.
Why the guards do not catch it
Both guards are written as fast paths for the fully-constant case and have no
fallback. Nothing establishes ub > lb before the partial iteration is emitted,
so for any runtime state with lb >= ub the peeled iteration executes a body the
source never executes.
Suggested fix
Guard the transform on a non-empty trip count rather than on constant-ness. Where
the bounds are dynamic, peeling can either be declined or the emitted partial
iteration guarded by lb < ub, so it is skipped at runtime for an empty loop.
Unrelated latent issue in the same line
The front-peeling guard computes the trip count in single-precision float:
ceil(float(*ubInt - *lbInt) / *stepInt) <= 1
float has a 24-bit significand, so ub - lb above 2^24 is rounded before the
division. That is not what triggers this report, but it is the wrong type for an
int64_t trip-count comparison and is worth replacing with integer arithmetic
in the same change.
Regression test
Peeling a loop whose bounds are dynamic and whose runtime trip count is zero should leave no executable partial iteration, in both peeling directions.
Environment
- llvm-project at
60b5f9a06722 - Release build with assertions, Linux x86-64
Hey I have started my work on this may you assign it to me