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

[MLIR] `affine-loop-fusion` misses producer-consumer fusion for fixed-column memref load

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

affine-loop-fusion misses a simple producer-consumer fusion opportunity. The producer loop initializes the full temporary buffer: %alloc[i, j] = 1.0. The consumer loop only reads a fixed column: %alloc[i, 0]. Since %alloc is locally allocated, does not escape, and is deallocated after the consumer, only the j = 0 slice is needed.

The producer and consumer could be fused by materializing only %alloc[i, 0] before the load. However, affine-loop-fusion keeps the full producer loop nest separate from the consumer loop, resulting in a missed optimization.

Input Program

input.mlir

module {
  func.func @block_removal(%arg0: memref<32xf32>) {
    %cst = arith.constant 1.000000e+00 : f32
    %alloc = memref.alloc() : memref<32x32xf32>
    affine.for %arg1 = 0 to 32 {
      affine.for %arg2 = 0 to 32 {
        affine.store %cst, %alloc[%arg1, %arg2] : memref<32x32xf32>
      }
    }
    affine.for %arg1 = 0 to 32 {
      %0 = affine.load %alloc[%arg1, 0] : memref<32x32xf32>
      affine.store %0, %arg0[%arg1] : memref<32xf32>
    }
    memref.dealloc %alloc : memref<32x32xf32>
    return
  }
}

Command

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

Version: b0dc60f01ab0057eea54ce82ab5498118a60567c

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

@llvm/issue-subscribers-mlir

Author: MonSec-2077

`affine-loop-fusion` misses a simple producer-consumer fusion opportunity. The producer loop initializes the full temporary buffer: `%alloc[i, j] = 1.0`. The consumer loop only reads a fixed column: `%alloc[i, 0]`. Since `%alloc` is locally allocated, does not escape, and is deallocated after the consumer, only the `j = 0` slice is needed.

The producer and consumer could be fused by materializing only %alloc[i, 0] before the load. However, affine-loop-fusion keeps the full producer loop nest separate from the consumer loop, resulting in a missed optimization.

Input Program

input.mlir

module {
  func.func @<!-- -->block_removal(%arg0: memref&lt;32xf32&gt;) {
    %cst = arith.constant 1.000000e+00 : f32
    %alloc = memref.alloc() : memref&lt;32x32xf32&gt;
    affine.for %arg1 = 0 to 32 {
      affine.for %arg2 = 0 to 32 {
        affine.store %cst, %alloc[%arg1, %arg2] : memref&lt;32x32xf32&gt;
      }
    }
    affine.for %arg1 = 0 to 32 {
      %0 = affine.load %alloc[%arg1, 0] : memref&lt;32x32xf32&gt;
      affine.store %0, %arg0[%arg1] : memref&lt;32xf32&gt;
    }
    memref.dealloc %alloc : memref&lt;32x32xf32&gt;
    return
  }
}

Command

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

Version: b0dc60f01ab0057eea54ce82ab5498118a60567c

hey , i have started my work on this may u assign it to me

purnima-nlp avatar Aug 29 '26 09:08 purnima-nlp

Hi, I don't have the required permissions to assign this to anyone, but I think submitting a PR would be a good way to move it forward and then you can mention some relevant maintainers and ask them to assign the issue to you.

MonSec-2077 avatar Aug 31 '26 03:08 MonSec-2077