[MLIR] `scf.parallel` loop fusion misses opportunity when equivalent index expressions are commuted
MLIR misses a valid scf.parallel loop fusion opportunity when the producer and consumer use equivalent but syntactically different index expressions.
In the example below, the first loop stores to %arg0[%arg2 + 1], and the second loop loads from %arg0[1 + %arg2]. These indices are semantically identical because integer addition is commutative, so the two loops could be fused safely. However, the fusion pass does not recognize this equivalence.
Input Program
input.mlir
module {
func.func @commuted_index(%arg0: memref<32xf32>, %arg1: memref<32xf32>) {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%c16 = arith.constant 16 : index
%cst = arith.constant 3.000000e+00 : f32
scf.parallel (%arg2) = (%c0) to (%c16) step (%c1) {
%0 = arith.addi %arg2, %c1 : index
memref.store %cst, %arg0[%0] : memref<32xf32>
scf.reduce
}
scf.parallel (%arg2) = (%c0) to (%c16) step (%c1) {
%0 = arith.addi %c1, %arg2 : index
%1 = memref.load %arg0[%0] : memref<32xf32>
memref.store %1, %arg1[%0] : memref<32xf32>
scf.reduce
}
return
}
}
Command
mlir-opt input.mlir --scf-parallel-loop-fusion
Version: 157af9cb0e9cad7df0e8e1d44c78f08ffa7c48e7
@llvm/issue-subscribers-mlir
Author: CC_Fans (dev-816)
In the example below, the first loop stores to %arg0[%arg2 + 1], and the second loop loads from %arg0[1 + %arg2]. These indices are semantically identical because integer addition is commutative, so the two loops could be fused safely. However, the fusion pass does not recognize this equivalence.
Input Program
input.mlir
module {
func.func @<!-- -->commuted_index(%arg0: memref<32xf32>, %arg1: memref<32xf32>) {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%c16 = arith.constant 16 : index
%cst = arith.constant 3.000000e+00 : f32
scf.parallel (%arg2) = (%c0) to (%c16) step (%c1) {
%0 = arith.addi %arg2, %c1 : index
memref.store %cst, %arg0[%0] : memref<32xf32>
scf.reduce
}
scf.parallel (%arg2) = (%c0) to (%c16) step (%c1) {
%0 = arith.addi %c1, %arg2 : index
%1 = memref.load %arg0[%0] : memref<32xf32>
memref.store %1, %arg1[%0] : memref<32xf32>
scf.reduce
}
return
}
}
Command
mlir-opt input.mlir --scf-parallel-loop-fusion
Version: 157af9cb0e9cad7df0e8e1d44c78f08ffa7c48e7
Hey, I started my work on this, may you please assign it to me.
Hi, I don’t have the necessary permissions to assign someone, but I think opening a PR would be a good way to resolve it. Alternatively, you can @ some relevant maintainers and ask them to assign this issue to you.