llvm / llvm/llvm-project

[MLIR] `affine-loop-fusion` misses affine loop fusion opportunity for memref access

Open
#218,833 2 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

`affine-loop-fusion` does not fuse two adjacent `affine.for` loops with identical bounds and a simple producer-consumer dependence through a temporary `memref`.

The first loop stores to `%alloc[%i]`, and the second loop immediately loads from `%alloc[%i]` and stores the value to `%alloc_0[%i]`. Since both loops iterate over the same range and access the same element at the same iteration, this is a missed optimization.

### Input Program

**input.mlir**

```llvm
module {
func.func @element_type(%arg0: complex) {
%alloc = memref.alloc() : memref<64xcomplex>
%alloc_0 = memref.alloc() : memref<64xcomplex>
affine.for %arg1 = 0 to 64 {
affine.store %arg0, %alloc[%arg1] : memref<64xcomplex>
}
affine.for %arg1 = 0 to 64 {
%0 = affine.load %alloc[%arg1] : memref<64xcomplex>
affine.store %0, %alloc_0[%arg1] : memref<64xcomplex>
}
return
}
}
```

### Command
```bash
mlir-opt --affine-loop-fusion affine-loop-fusion.mlir
```

Version: cfe9def9a4149c7aa1db2dcb7adcef439d763d63

Contributor guide

Open the contributing guide

Research direction

Run the provided input program with `mlir-opt --affine-loop-fusion` and inspect the `affine-loop-fusion` entry point to understand why the adjacent loops are not fused. Done means the producer and consumer loops in this reproducer are fused while preserving the stated memref accesses and dependence.

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
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.