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

[MLIR] `scf-parallel-loop-fusion` misses adjacent loops with identical constant bounds

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

scf-parallel-loop-fusion misses a fusion opportunity when two adjacent scf.parallel loops have the same constant bounds and step, but those constants are defined by different arith.constant ops.

The loops below both iterate from 0 to 16 with step 1, so their iteration spaces are equivalent and they should be fusible. However, the pass appears to compare loop bounds by SSA value identity instead of comparing equivalent constant values.

Input Program

input.mlir

module {
  func.func @constant_bounds(%arg0: memref<16xf32>, %arg1: memref<16xf32>) {
    %c0 = arith.constant 0 : index
    %c16 = arith.constant 16 : index
    %c1 = arith.constant 1 : index
    %cst = arith.constant 1.000000e+00 : f32
    scf.parallel (%arg2) = (%c0) to (%c16) step (%c1) {
      memref.store %cst, %arg0[%arg2] : memref<16xf32>
      scf.reduce
    }
    %c0_0 = arith.constant 0 : index
    %c16_1 = arith.constant 16 : index
    %c1_2 = arith.constant 1 : index
    scf.parallel (%arg2) = (%c0_0) to (%c16_1) step (%c1_2) {
      %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: 157af9cb0e9cad7df0e8e1d44c78f08ffa7c48e7

dev-816 avatar Aug 24 '26 04:08 dev-816

@llvm/issue-subscribers-mlir

Author: CC_Fans (dev-816)

`scf-parallel-loop-fusion` misses a fusion opportunity when two adjacent `scf.parallel` loops have the same constant bounds and step, but those constants are defined by different `arith.constant` ops.

The loops below both iterate from 0 to 16 with step 1, so their iteration spaces are equivalent and they should be fusible. However, the pass appears to compare loop bounds by SSA value identity instead of comparing equivalent constant values.

Input Program

input.mlir

module {
  func.func @<!-- -->constant_bounds(%arg0: memref&lt;16xf32&gt;, %arg1: memref&lt;16xf32&gt;) {
    %c0 = arith.constant 0 : index
    %c16 = arith.constant 16 : index
    %c1 = arith.constant 1 : index
    %cst = arith.constant 1.000000e+00 : f32
    scf.parallel (%arg2) = (%c0) to (%c16) step (%c1) {
      memref.store %cst, %arg0[%arg2] : memref&lt;16xf32&gt;
      scf.reduce
    }
    %c0_0 = arith.constant 0 : index
    %c16_1 = arith.constant 16 : index
    %c1_2 = arith.constant 1 : index
    scf.parallel (%arg2) = (%c0_0) to (%c16_1) step (%c1_2) {
      %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

Version: 157af9cb0e9cad7df0e8e1d44c78f08ffa7c48e7