[MLIR] Missed Optimization: Constants defined inside loops prevent loop range folding
The operand check requires the offset to be defined outside the loop, rejecting even a literal constant defined in the loop body.
In this example, materializing %c4 outside the loop would allow shifting the range to [4, 12) while preserving step 1 and eliminating the addition.
Code Example
func.func @local_constant(%out: memref<16xi32>, %v: i32) {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%c8 = arith.constant 8 : index
scf.for %i = %c0 to %c8 step %c1 {
%c4 = arith.constant 4 : index
%idx = arith.addi %i, %c4 : index
memref.store %v, %out[%idx] : memref<16xi32>
}
return
}
Command
mlir-opt input.mlir --scf-for-loop-range-folding
Version: 6f54fe6b6fb22f8a335fd19b94049fc769dfea25
@llvm/issue-subscribers-mlir
Author: MonSec-2077
In this example, materializing %c4 outside the loop would allow shifting the range to [4, 12) while preserving step 1 and eliminating the addition.
Code Example
func.func @<!-- -->local_constant(%out: memref<16xi32>, %v: i32) {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%c8 = arith.constant 8 : index
scf.for %i = %c0 to %c8 step %c1 {
%c4 = arith.constant 4 : index
%idx = arith.addi %i, %c4 : index
memref.store %v, %out[%idx] : memref<16xi32>
}
return
}
Command
mlir-opt input.mlir --scf-for-loop-range-folding
Version: 6f54fe6b6fb22f8a335fd19b94049fc769dfea25
running loop-invariant code motion pass before scf-for-loop-range-folding solves this and it is the only job of the pass, do we need scf-for-loop-range-folding to handle it? @banach-space @matthias-springer
I would rely on LICM here.
I would rely on LICM here.
+1
So close this issue?
i don't have the permission to close this, i request someone with access to do so.