llvm / llvm/llvm-project

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

Open
#219,871 1 comment 0 reactions 0 assignees View on GitHub
mlir
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

`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**
```llvm
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
```bash
mlir-opt input.mlir --scf-parallel-loop-fusion
```

Version: b37a8a70cc98915d157cac2f049e9f46d6da8fe5

Contributor guide

Open the contributing guide

Research direction

Start by running the supplied input.mlir with mlir-opt --scf-parallel-loop-fusion and inspect the scf-parallel-loop-fusion implementation and its existing tests. Reproduce the missed fusion involving the private memref.alloc, then add coverage and verify that the two adjacent scf.parallel loops are fused without invalid aliasing assumptions.

Written by the indexing model from the issue text.

Assessment

Domain
compilers, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
65/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.