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

[MLIR] `affine-loop-fusion` misses sibling fusion for loops with identical read-only patterns

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

affine-loop-fusion misses a legal sibling fusion optimization opportunity when two adjacent affine loops have the same bounds and perform identical loads from the same read-only memref, while storing results to different output memrefs. Fusing these loops is profitable because the identical loads from %arg0[%arg3] and %arg0[%arg3 + 1] can be further optimized after fusion.

Input Program

input.mlir

module {
  func.func @sibling_loads(%arg0: memref<65xf32>, %arg1: memref<64xf32>, %arg2: memref<64xf32>) {
    affine.for %arg3 = 0 to 64 {
      %0 = affine.load %arg0[%arg3] : memref<65xf32>
      %1 = affine.load %arg0[%arg3 + 1] : memref<65xf32>
      %2 = arith.addf %0, %1 : f32
      affine.store %2, %arg1[%arg3] : memref<64xf32>
    }
    affine.for %arg3 = 0 to 64 {
      %0 = affine.load %arg0[%arg3] : memref<65xf32>
      %1 = affine.load %arg0[%arg3 + 1] : memref<65xf32>
      %2 = arith.mulf %0, %1 : f32
      affine.store %2, %arg2[%arg3] : memref<64xf32>
    }
    return
  }
}

Command

mlir-opt --affine-fusion-loop input.mlir

Version: b0dc60f01ab0057eea54ce82ab5498118a60567c

MonSec-2077 avatar Aug 28 '26 04:08 MonSec-2077

@llvm/issue-subscribers-mlir

Author: MonSec-2077

`affine-loop-fusion` misses a legal sibling fusion opportunity when two adjacent affine loops have the same bounds and perform identical loads from the same read-only memref, while storing results to different output memrefs. Fusing these loops is profitable because the identical loads from `%arg0[%arg3]` and `%arg0[%arg3 + 1]` can be shared or exposed to CSE after fusion.

Input Program

input.mlir

module {
  func.func @<!-- -->sibling_loads(%arg0: memref&lt;65xf32&gt;, %arg1: memref&lt;64xf32&gt;, %arg2: memref&lt;64xf32&gt;) {
    affine.for %arg3 = 0 to 64 {
      %0 = affine.load %arg0[%arg3] : memref&lt;65xf32&gt;
      %1 = affine.load %arg0[%arg3 + 1] : memref&lt;65xf32&gt;
      %2 = arith.addf %0, %1 : f32
      affine.store %2, %arg1[%arg3] : memref&lt;64xf32&gt;
    }
    affine.for %arg3 = 0 to 64 {
      %0 = affine.load %arg0[%arg3] : memref&lt;65xf32&gt;
      %1 = affine.load %arg0[%arg3 + 1] : memref&lt;65xf32&gt;
      %2 = arith.mulf %0, %1 : f32
      affine.store %2, %arg2[%arg3] : memref&lt;64xf32&gt;
    }
    return
  }
}

Command

mlir-opt --affine-fusion-loop input.mlir

Version: b0dc60f01ab0057eea54ce82ab5498118a60567c

@llvm/issue-subscribers-mlir-affine

Author: MonSec-2077

`affine-loop-fusion` misses a legal sibling fusion optimization opportunity when two adjacent affine loops have the same bounds and perform identical loads from the same read-only memref, while storing results to different output memrefs. Fusing these loops is profitable because the identical loads from `%arg0[%arg3]` and `%arg0[%arg3 + 1]` can be further optimized after fusion.

Input Program

input.mlir

module {
  func.func @<!-- -->sibling_loads(%arg0: memref&lt;65xf32&gt;, %arg1: memref&lt;64xf32&gt;, %arg2: memref&lt;64xf32&gt;) {
    affine.for %arg3 = 0 to 64 {
      %0 = affine.load %arg0[%arg3] : memref&lt;65xf32&gt;
      %1 = affine.load %arg0[%arg3 + 1] : memref&lt;65xf32&gt;
      %2 = arith.addf %0, %1 : f32
      affine.store %2, %arg1[%arg3] : memref&lt;64xf32&gt;
    }
    affine.for %arg3 = 0 to 64 {
      %0 = affine.load %arg0[%arg3] : memref&lt;65xf32&gt;
      %1 = affine.load %arg0[%arg3 + 1] : memref&lt;65xf32&gt;
      %2 = arith.mulf %0, %1 : f32
      affine.store %2, %arg2[%arg3] : memref&lt;64xf32&gt;
    }
    return
  }
}

Command

mlir-opt --affine-fusion-loop input.mlir

Version: b0dc60f01ab0057eea54ce82ab5498118a60567c