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

[MLIR] `affine-loop-fusion` misses affine loop fusion opportunity for memref access

Open dev-816 opened this issue 3 months ago • 2 comments

affine-loop-fusion does not fuse two adjacent affine.for loops with identical bounds and a simple producer-consumer dependence through a temporary memref.

The first loop stores to %alloc[%i], and the second loop immediately loads from %alloc[%i] and stores the value to %alloc_0[%i]. Since both loops iterate over the same range and access the same element at the same iteration, this is a missed optimization.

Input Program

input.mlir

module {
  func.func @element_type(%arg0: complex<f32>) {
    %alloc = memref.alloc() : memref<64xcomplex<f32>>
    %alloc_0 = memref.alloc() : memref<64xcomplex<f32>>
    affine.for %arg1 = 0 to 64 {
      affine.store %arg0, %alloc[%arg1] : memref<64xcomplex<f32>>
    }
    affine.for %arg1 = 0 to 64 {
      %0 = affine.load %alloc[%arg1] : memref<64xcomplex<f32>>
      affine.store %0, %alloc_0[%arg1] : memref<64xcomplex<f32>>
    }
    return
  }
}

Command

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

Version: cfe9def9a4149c7aa1db2dcb7adcef439d763d63

dev-816 avatar Aug 26 '26 03:08 dev-816

@llvm/issue-subscribers-mlir

Author: CC_Fans (dev-816)

`affine-loop-fusion` does not fuse two adjacent `affine.for` loops with identical bounds and a simple producer-consumer dependence through a temporary `memref`.

The first loop stores to %alloc[%i], and the second loop immediately loads from %alloc[%i] and stores the value to %alloc_0[%i]. Since both loops iterate over the same range and access the same element at the same iteration, this is a missed optimization.

Input Program

input.mlir

module {
  func.func @<!-- -->element_type(%arg0: complex&lt;f32&gt;) {
    %alloc = memref.alloc() : memref&lt;64xcomplex&lt;f32&gt;&gt;
    %alloc_0 = memref.alloc() : memref&lt;64xcomplex&lt;f32&gt;&gt;
    affine.for %arg1 = 0 to 64 {
      affine.store %arg0, %alloc[%arg1] : memref&lt;64xcomplex&lt;f32&gt;&gt;
    }
    affine.for %arg1 = 0 to 64 {
      %0 = affine.load %alloc[%arg1] : memref&lt;64xcomplex&lt;f32&gt;&gt;
      affine.store %0, %alloc_0[%arg1] : memref&lt;64xcomplex&lt;f32&gt;&gt;
    }
    return
  }
}

Command

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

Version: cfe9def9a4149c7aa1db2dcb7adcef439d763d63

Had a look at this. It's the element type rather than the loops — f32 fuses, complex doesn't, because the pass can't work out a byte size for complex.

So the approach I have in mind is: stop treating an unknown element size as a reason to skip fusion, and just leave the buffer in the original memory space when the size isn't known. That would cover complex and any other element type, without changing behaviour for the ones that already work.

Is that the right approach, or would you prefer adding more element types to the size helper instead?

Happy to put up a PR once you confirm which you'd prefer.

@bondhugula @ftynse

hamzaqureshi5 avatar Aug 26 '26 09:08 hamzaqureshi5