llvm-project
llvm-project copied to clipboard
[mlir][scf] transform.loop.peel miscompiles unsigned scf.for loops
The scf-for-loop-peeling pass rejects scf.for loops with the unsigned attribute, but transform.loop.peel calls peelForLoopFirstIteration directly and it does not. Unsigned loops therefore reach peeling code that reads the bounds as signed, which can produce wrong code.
repro:
func.func @empty_unsigned(%out: memref<8xindex>) {
%lb = arith.constant -1 : index
%ub = arith.constant 5 : index
%step = arith.constant 1 : index
scf.for unsigned %i = %lb to %ub step %step {
%c0 = arith.constant 0 : index
memref.store %i, %out[%c0] : memref<8xindex>
}
return
}
module attributes {transform.with_named_sequence} {
transform.named_sequence @__transform_main(%root: !transform.any_op {transform.readonly}) {
%loops = transform.structured.match ops{["scf.for"]} in %root
: (!transform.any_op) -> !transform.op<"scf.for">
%main, %rest = transform.loop.peel %loops peel_front = true
: (!transform.op<"scf.for">) -> (!transform.op<"scf.for">, !transform.op<"scf.for">)
transform.yield
}
}
Command:
mlir-opt input.mlir --transform-interpreter
Commit: cbdad670518e35181f608e468d1342a9b07beec1
Actual result:
scf.for unsigned %arg1 = %c-1 to %c0 step %c1 { ... }
scf.for unsigned %arg1 = %c0 to %c5 step %c1 { ... }
Expected result:
the loop not peeled, since it has no iterations.
@llvm/issue-subscribers-mlir
Author: Prashanth (StarOne01)
The `scf-for-loop-peeling` pass rejects `scf.for` loops with the unsigned attribute, but `transform.loop.peel` calls `peelForLoopFirstIteration` directly and it does not. Unsigned loops therefore reach peeling code that reads the bounds as signed, which can produce wrong code.
repro:
func.func @<!-- -->empty_unsigned(%out: memref<8xindex>) {
%lb = arith.constant -1 : index
%ub = arith.constant 5 : index
%step = arith.constant 1 : index
scf.for unsigned %i = %lb to %ub step %step {
%c0 = arith.constant 0 : index
memref.store %i, %out[%c0] : memref<8xindex>
}
return
}
module attributes {transform.with_named_sequence} {
transform.named_sequence @<!-- -->__transform_main(%root: !transform.any_op {transform.readonly}) {
%loops = transform.structured.match ops{["scf.for"]} in %root
: (!transform.any_op) -> !transform.op<"scf.for">
%main, %rest = transform.loop.peel %loops peel_front = true
: (!transform.op<"scf.for">) -> (!transform.op<"scf.for">, !transform.op<"scf.for">)
transform.yield
}
}
Command:
mlir-opt input.mlir --transform-interpreter
Commit: cbdad670518e35181f608e468d1342a9b07beec1
Actual result:
scf.for unsigned %arg1 = %c-1 to %c0 step %c1 { ... }
scf.for unsigned %arg1 = %c0 to %c5 step %c1 { ... }
Expected result:
the loop not peeled, since it has no iterations.