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

[MLIR] `affine-loop-tiling` causes performance regression due to degenerate single-iteration loop

Open MonSec-2077 opened this issue 3 months ago • 3 comments

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.apply and perform proper 2D tiling.
  • Keep the IR unchanged (no-op) to avoid performance degradation when 2D tiling cannot be formed.

Version: b37a8a70cc98915d157cac2f049e9f46d6da8fe5

MonSec-2077 avatar Sep 01 '26 08:09 MonSec-2077

@llvm/issue-subscribers-mlir

Author: MonSec-2077

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&lt;(d0) -&gt; (d0)&gt;
module {
  func.func @<!-- -->between_loops(%arg0: memref&lt;64x64xf32&gt;) {
    %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&lt;64x64xf32&gt;
      }
    }
    return
  }
}

Actual Output

#map = affine_map&lt;(d0) -&gt; (d0)&gt;
#map1 = affine_map&lt;(d0) -&gt; (d0 + 1)&gt;
module {
  func.func @<!-- -->between_loops(%arg0: memref&lt;64x64xf32&gt;) {
    %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&lt;64x64xf32&gt;
        }
      }
    }
    return
  }
}

Expected Behavior

Either:

  • Safely sink/ignore side-effect-free affine.apply and 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?

purnima-nlp avatar Sep 01 '26 10:09 purnima-nlp

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.

EugeneZelenko avatar Sep 01 '26 15:09 EugeneZelenko