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

[MLIR] `scf-parallel-loop-fusion` misses fusion when one loop contains a private `memref.alloc`

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

scf-parallel-loop-fusion misses a fusion opportunity for two adjacent scf.parallel loops with identical bounds. The first loop only allocates a private temporary buffer inside each iteration and writes to it:

%alloc = memref.alloc() : memref<1xf32>
memref.store %cst, %alloc[%c0]

The second loop independently copies %arg0[i] to %arg1[i]. The inner %alloc is private to the first loop iteration and cannot alias %arg0 or %arg1, so it should not block fusion of the two parallel loops. Currently, the loops remain separate, resulting in a missed optimization.

Input Program

input.mlir

module {
  func.func @alloc_alias(%arg0: memref<16xf32>, %arg1: memref<16xf32>) {
    %c0 = arith.constant 0 : index
    %c1 = arith.constant 1 : index
    %c16 = arith.constant 16 : index
    %cst = arith.constant 0.000000e+00 : f32
    scf.parallel (%arg2) = (%c0) to (%c16) step (%c1) {
      %alloc = memref.alloc() : memref<1xf32>
      memref.store %cst, %alloc[%c0] : memref<1xf32>
      scf.reduce
    }
    scf.parallel (%arg2) = (%c0) to (%c16) step (%c1) {
      %0 = memref.load %arg0[%arg2] : memref<16xf32>
      memref.store %0, %arg1[%arg2] : memref<16xf32>
      scf.reduce
    }
    return
  }
}

Command

mlir-opt input.mlir --scf-parallel-loop-fusion

Version: b37a8a70cc98915d157cac2f049e9f46d6da8fe5

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

@llvm/issue-subscribers-mlir

Author: MonSec-2077

`scf-parallel-loop-fusion` misses a fusion opportunity for two adjacent `scf.parallel` loops with identical bounds. The first loop only allocates a private temporary buffer inside each iteration and writes to it: ```bash %alloc = memref.alloc() : memref<1xf32> memref.store %cst, %alloc[%c0] ``` The second loop independently copies `%arg0[i]` to `%arg1[i]`. The inner `%alloc` is private to the first loop iteration and cannot alias `%arg0` or `%arg1`, so it should not block fusion of the two parallel loops. Currently, the loops remain separate, resulting in a missed optimization.

Input Program

input.mlir

module {
  func.func @<!-- -->alloc_alias(%arg0: memref&lt;16xf32&gt;, %arg1: memref&lt;16xf32&gt;) {
    %c0 = arith.constant 0 : index
    %c1 = arith.constant 1 : index
    %c16 = arith.constant 16 : index
    %cst = arith.constant 0.000000e+00 : f32
    scf.parallel (%arg2) = (%c0) to (%c16) step (%c1) {
      %alloc = memref.alloc() : memref&lt;1xf32&gt;
      memref.store %cst, %alloc[%c0] : memref&lt;1xf32&gt;
      scf.reduce
    }
    scf.parallel (%arg2) = (%c0) to (%c16) step (%c1) {
      %0 = memref.load %arg0[%arg2] : memref&lt;16xf32&gt;
      memref.store %0, %arg1[%arg2] : memref&lt;16xf32&gt;
      scf.reduce
    }
    return
  }
}

Command

mlir-opt input.mlir --scf-parallel-loop-fusion