llvm / llvm/llvm-project

[MLIR] `scf-parallel-loop-fusion` misses fusion for identical producer-consumer parallel loops

Open
#222,517 3 comments 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 simple fusion opportunity between two adjacent scf.parallel l oops. The first loop writes vector chunks to `%arg0`:
```llvm
%arg0[i : i + 4] = %arg2
```

The second loop has the same bounds and step, reads the same chunk from `%arg0`, and writes it to `%arg1`:

```llvm
%v = %arg0[i : i + 4]
%arg1[i : i + 4] = %v
```

Since both loops have identical iteration spaces and the second loop directly consumes the data produced by the first loop, they could be fused into a single `scf.parallel`. Currently, the two parallel loops remain separate, leading to a missed optimization.

### Input Program
**input.mlir**

```llvm
module {
func.func @equal_mask(%arg0: memref<16xf32>, %arg1: memref<16xf32>, %arg2: vector<4xf32>) {
%c0 = arith.constant 0 : index
%c4 = arith.constant 4 : index
%c16 = arith.constant 16 : index
%cst = arith.constant 0.000000e+00 : f32
%cst_0 = arith.constant dense : vector<4xi1>
scf.parallel (%arg3) = (%c0) to (%c16) step (%c4) {
vector.transfer_write %arg2, %arg0[%arg3], %cst_0 {in_bounds = [true]} : vector<4xf32>, memref<16xf32>
scf.reduce
}
%cst_1 = arith.constant dense : vector<4xi1>
scf.parallel (%arg3) = (%c0) to (%c16) step (%c4) {
%0 = vector.transfer_read %arg0[%arg3], %cst, %cst_1 {in_bounds = [true]} : memref<16xf32>, vector<4xf32>
vector.transfer_write %0, %arg1[%arg3] {in_bounds = [true]} : vector<4xf32>, memref<16xf32>
scf.reduce
}
return
}
}
```

### Command
```bash
mlir-opt input.mlir --scf-parallel-loop-fusion
```
Version: 6f54fe6b6fb22f8a335fd19b94049fc769dfea25

Contributor guide

Open the contributing guide

Research direction

Start with input.mlir and run mlir-opt input.mlir --scf-parallel-loop-fusion to reproduce the missed optimization. Trace the scf-parallel-loop-fusion pass's handling of adjacent producer-consumer loops and verify that the two matching loops are fused into one scf.parallel while preserving the shown transfers.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.