[MLIR] `affine-loop-tiling` causes performance regression due to degenerate single-iteration loop
Applying -affine-loop-tile to a 2D loop nest with an affine.apply between loops results in a performance regression and IR bloat.
Instead of tiling the 2D loop band or leaving it untouched, the pass transforms the code by introducing a redundant intermediate loop that executes exactly once (arg1 to arg1 + 1). This fails to improve spatial locality while adding unnecessary loop control overhead.
Repro
Run mlir-opt --affine-loop-tile on the following IR:
#map = affine_map<(d0) -> (d0)>
module {
func.func @between_loops(%arg0: memref<64x64xf32>) {
%cst = arith.constant 4.000000e+00 : f32
affine.for %arg1 = 0 to 64 {
%0 = affine.apply #map(%arg1)
affine.for %arg2 = 0 to 64 {
affine.store %cst, %arg0[%0, %arg2] : memref<64x64xf32>
}
}
return
}
}
Actual Output
#map = affine_map<(d0) -> (d0)>
#map1 = affine_map<(d0) -> (d0 + 1)>
module {
func.func @between_loops(%arg0: memref<64x64xf32>) {
%cst = arith.constant 4.000000e+00 : f32
affine.for %arg1 = 0 to 64 {
affine.for %arg2 = #map(%arg1) to #map1(%arg1) {
%0 = affine.apply #map(%arg2)
affine.for %arg3 = 0 to 64 {
affine.store %cst, %arg0[%0, %arg3] : memref<64x64xf32>
}
}
}
return
}
}
Expected Behavior
Either:
- Safely sink/ignore side-effect-free
affine.applyand perform proper 2D tiling. - Keep the IR unchanged (no-op) to avoid performance degradation when 2D tiling cannot be formed.
Version: b37a8a70cc98915d157cac2f049e9f46d6da8fe5
@llvm/issue-subscribers-mlir
Author: MonSec-2077
Instead of tiling the 2D loop band or leaving it untouched, the pass transforms the code by introducing a redundant intermediate loop that executes exactly once (arg1 to arg1 + 1). This fails to improve spatial locality while adding unnecessary loop control overhead.
Repro
Run mlir-opt --affine-loop-tile on the following IR:
#map = affine_map<(d0) -> (d0)>
module {
func.func @<!-- -->between_loops(%arg0: memref<64x64xf32>) {
%cst = arith.constant 4.000000e+00 : f32
affine.for %arg1 = 0 to 64 {
%0 = affine.apply #map(%arg1)
affine.for %arg2 = 0 to 64 {
affine.store %cst, %arg0[%0, %arg2] : memref<64x64xf32>
}
}
return
}
}
Actual Output
#map = affine_map<(d0) -> (d0)>
#map1 = affine_map<(d0) -> (d0 + 1)>
module {
func.func @<!-- -->between_loops(%arg0: memref<64x64xf32>) {
%cst = arith.constant 4.000000e+00 : f32
affine.for %arg1 = 0 to 64 {
affine.for %arg2 = #map(%arg1) to #map1(%arg1) {
%0 = affine.apply #map(%arg2)
affine.for %arg3 = 0 to 64 {
affine.store %cst, %arg0[%0, %arg3] : memref<64x64xf32>
}
}
}
return
}
}
Expected Behavior
Either:
- Safely sink/ignore side-effect-free
affine.applyand perform proper 2D tiling. - Keep the IR unchanged (no-op) to avoid performance degradation when 2D tiling cannot be formed.
Version: b37a8a70cc98915d157cac2f049e9f46d6da8fe5
hey i have started my work on this, may you please assign it to me?
hey i have started my work on this, may you please assign it to me?
Just create pull request and mention it on this page.