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

[MLIR] `affine-loop-tile` tiles a no-op outer loop and misses inner matmul loop nest

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

affine-loop-tile with tile-size=16 strip-mines an outer loop whose static trip count is only 8, producing a single-tile loop nest, but it does not tile the inner 128 x 128 x 128 matmul-like loop nest, resulting in a missed optimization.

The outer loop is not profitable to tile because its trip count is smaller than the tile size. The inner perfect loop nest has large constant trip counts and is the loop nest that should be tiled.

Input Program

module {
  func.func @matmul_suffix(%arg0: memref<8x128x128xf32>, %arg1: memref<128x128xf32>, %arg2: memref<8x128x128xf32>, %arg3: memref<8xf32>) {
    %cst = arith.constant 0.000000e+00 : f32
    affine.for %arg4 = 0 to 8 {
      affine.store %cst, %arg3[%arg4] : memref<8xf32>
      affine.for %arg5 = 0 to 128 {
        affine.for %arg6 = 0 to 128 {
          affine.for %arg7 = 0 to 128 {
            %0 = affine.load %arg0[%arg4, %arg5, %arg7] : memref<8x128x128xf32>
            %1 = affine.load %arg1[%arg7, %arg6] : memref<128x128xf32>
            %2 = affine.load %arg2[%arg4, %arg5, %arg6] : memref<8x128x128xf32>
            %3 = arith.mulf %0, %1 : f32
            %4 = arith.addf %2, %3 : f32
            affine.store %4, %arg2[%arg4, %arg5, %arg6] : memref<8x128x128xf32>
          }
        }
      }
    }
    return
  }
}

Command

mlir-opt input.mlir --affine-loop-tile="tile-size=16"

Actual Output

The pass only tiles the outer loop with trip count 8. The outer tiled loop has only one iteration because the original trip count is 8 and the tile size is 16. This is effectively a no-op strip-mining transformation and can even make the IR worse before canonicalization. The inner 128 x 128 x 128 loop nest is left untiled.

#map = affine_map<(d0) -> (d0)>
#map1 = affine_map<(d0) -> (d0 + 8)>
module {
  func.func @matmul_suffix(%arg0: memref<8x128x128xf32>, %arg1: memref<128x128xf32>, %arg2: memref<8x128x128xf32>, %arg3: memref<8xf32>) {
    %cst = arith.constant 0.000000e+00 : f32
    affine.for %arg4 = 0 to 8 step 16 {
      affine.for %arg5 = #map(%arg4) to #map1(%arg4) {
        affine.store %cst, %arg3[%arg5] : memref<8xf32>
        affine.for %arg6 = 0 to 128 {
          affine.for %arg7 = 0 to 128 {
            affine.for %arg8 = 0 to 128 {
              %0 = affine.load %arg0[%arg5, %arg6, %arg8] : memref<8x128x128xf32>
              %1 = affine.load %arg1[%arg8, %arg7] : memref<128x128xf32>
              %2 = affine.load %arg2[%arg5, %arg6, %arg7] : memref<8x128x128xf32>
              %3 = arith.mulf %0, %1 : f32
              %4 = arith.addf %2, %3 : f32
              affine.store %4, %arg2[%arg5, %arg6, %arg7] : memref<8x128x128xf32>
            }
          }
        }
      }
    }
    return
  }
}

Version: b37a8a70cc98915d157cac2f049e9f46d6da8fe5

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

@llvm/issue-subscribers-mlir

Author: MonSec-2077

`affine-loop-tile` with `tile-size=16` strip-mines an outer loop whose static trip count is only 8, producing a single-tile loop nest, but it does not tile the inner `128 x 128 x 128` matmul-like loop nest, resulting in a missed optimization.

The outer loop is not profitable to tile because its trip count is smaller than the tile size. The inner perfect loop nest has large constant trip counts and is the loop nest that should be tiled.

Input Program

module {
  func.func @<!-- -->matmul_suffix(%arg0: memref&lt;8x128x128xf32&gt;, %arg1: memref&lt;128x128xf32&gt;, %arg2: memref&lt;8x128x128xf32&gt;, %arg3: memref&lt;8xf32&gt;) {
    %cst = arith.constant 0.000000e+00 : f32
    affine.for %arg4 = 0 to 8 {
      affine.store %cst, %arg3[%arg4] : memref&lt;8xf32&gt;
      affine.for %arg5 = 0 to 128 {
        affine.for %arg6 = 0 to 128 {
          affine.for %arg7 = 0 to 128 {
            %0 = affine.load %arg0[%arg4, %arg5, %arg7] : memref&lt;8x128x128xf32&gt;
            %1 = affine.load %arg1[%arg7, %arg6] : memref&lt;128x128xf32&gt;
            %2 = affine.load %arg2[%arg4, %arg5, %arg6] : memref&lt;8x128x128xf32&gt;
            %3 = arith.mulf %0, %1 : f32
            %4 = arith.addf %2, %3 : f32
            affine.store %4, %arg2[%arg4, %arg5, %arg6] : memref&lt;8x128x128xf32&gt;
          }
        }
      }
    }
    return
  }
}

Command

mlir-opt input.mlir --affine-loop-tile="tile-size=16"

Actual Output

The pass only tiles the outer loop with trip count 8. The outer tiled loop has only one iteration because the original trip count is 8 and the tile size is 16. This is effectively a no-op strip-mining transformation and can even make the IR worse before canonicalization. The inner 128 x 128 x 128 loop nest is left untiled.

#map = affine_map&lt;(d0) -&gt; (d0)&gt;
#map1 = affine_map&lt;(d0) -&gt; (d0 + 8)&gt;
module {
  func.func @<!-- -->matmul_suffix(%arg0: memref&lt;8x128x128xf32&gt;, %arg1: memref&lt;128x128xf32&gt;, %arg2: memref&lt;8x128x128xf32&gt;, %arg3: memref&lt;8xf32&gt;) {
    %cst = arith.constant 0.000000e+00 : f32
    affine.for %arg4 = 0 to 8 step 16 {
      affine.for %arg5 = #map(%arg4) to #map1(%arg4) {
        affine.store %cst, %arg3[%arg5] : memref&lt;8xf32&gt;
        affine.for %arg6 = 0 to 128 {
          affine.for %arg7 = 0 to 128 {
            affine.for %arg8 = 0 to 128 {
              %0 = affine.load %arg0[%arg5, %arg6, %arg8] : memref&lt;8x128x128xf32&gt;
              %1 = affine.load %arg1[%arg8, %arg7] : memref&lt;128x128xf32&gt;
              %2 = affine.load %arg2[%arg5, %arg6, %arg7] : memref&lt;8x128x128xf32&gt;
              %3 = arith.mulf %0, %1 : f32
              %4 = arith.addf %2, %3 : f32
              affine.store %4, %arg2[%arg5, %arg6, %arg7] : memref&lt;8x128x128xf32&gt;
            }
          }
        }
      }
    }
    return
  }
}

working on this.

cr-zhao avatar Sep 09 '26 03:09 cr-zhao