llvm / llvm/llvm-project

[MLIR] `affine-loop-fusion` misses producer-consumer fusion for fixed-column memref load

Open
#219,481 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

`affine-loop-fusion` misses a simple producer-consumer fusion opportunity. The producer loop initializes the full temporary buffer: `%alloc[i, j] = 1.0`. The consumer loop only reads a fixed column: `%alloc[i, 0]`. Since `%alloc` is locally allocated, does not escape, and is deallocated after the consumer, only the `j = 0` slice is needed.

The producer and consumer could be fused by materializing only `%alloc[i, 0]` before the load. However, `affine-loop-fusion` keeps the full producer loop nest separate from the consumer loop, resulting in a missed optimization.

### Input Program

**input.mlir**

```llvm
module {
func.func @block_removal(%arg0: memref<32xf32>) {
%cst = arith.constant 1.000000e+00 : f32
%alloc = memref.alloc() : memref<32x32xf32>
affine.for %arg1 = 0 to 32 {
affine.for %arg2 = 0 to 32 {
affine.store %cst, %alloc[%arg1, %arg2] : memref<32x32xf32>
}
}
affine.for %arg1 = 0 to 32 {
%0 = affine.load %alloc[%arg1, 0] : memref<32x32xf32>
affine.store %0, %arg0[%arg1] : memref<32xf32>
}
memref.dealloc %alloc : memref<32x32xf32>
return
}
}
```

### Command

```bash
mlir-opt input.mlir --affine-loop-fusion
```

Version: b0dc60f01ab0057eea54ce82ab5498118a60567c

Contributor guide

Open the contributing guide

Research direction

Start by running the provided input.mlir with mlir-opt --affine-loop-fusion and inspect the resulting loop structure. Trace the affine-loop-fusion implementation and existing tests for producer-consumer fusion; done means the fixed-column case fuses and no longer materializes the unnecessary full producer loop.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.