llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[mlir][scf] transform.loop.peel miscompiles unsigned scf.for loops

Open StarOne01 opened this issue 1 month ago • 1 comments

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.

StarOne01 avatar Oct 13 '26 15:10 StarOne01

@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&lt;8xindex&gt;) {
  %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&lt;8xindex&gt;
  }
  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) -&gt; !transform.op&lt;"scf.for"&gt;
    %main, %rest = transform.loop.peel %loops peel_front = true
        : (!transform.op&lt;"scf.for"&gt;) -&gt; (!transform.op&lt;"scf.for"&gt;, !transform.op&lt;"scf.for"&gt;)
    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.