llvm / llvm/llvm-project

[MLIR] `buffer-loop-hoisting` fails to hoist buffer in `scf.while`

Open
#223,944 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

`buffer-loop-hoisting` fails to hoist `memref.alloc` out of the `scf.while` loop. Although the allocated buffer `%buffer` escapes as the result of the `scf.while` loop (`%last`), it is **not** passed back across iterations via `scf.yield` (which only yields an `index`).

Since every iteration overwrites the buffer before reading it and no cross-iteration memory dependence exists, hoisting `%buffer` to the loop entry block is completely safe. The current alias analysis conservatively treats the loop exit return value as a cross-iteration back-edge alias, causing a missed optimization.

### Reproduction Example
**input.mlir**
```llvm
module {
func.func @exit_alias(%arg0: index) -> index {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%0 = scf.while (%arg1 = %c0) : (index) -> memref<1xindex> {
%alloc = memref.alloc() : memref<1xindex>
memref.store %arg1, %alloc[%c0] : memref<1xindex>
%2 = arith.cmpi slt, %arg1, %arg0 : index
scf.condition(%2) %alloc : memref<1xindex>
} do {
^bb0(%arg1: memref<1xindex>):
%2 = memref.load %arg1[%c0] : memref<1xindex>
%3 = arith.addi %2, %c1 : index
scf.yield %3 : index
}
%1 = memref.load %0[%c0] : memref<1xindex>
return %1 : index
}
}
```

### Command
```bash
mlir-opt input.mlir -buffer-loop-hoisting
```

Version: 6f54fe6b6fb22f8a335fd19b94049fc769dfea25

Contributor guide

Open the contributing guide

Research direction

Start by running mlir-opt input.mlir -buffer-loop-hoisting with the provided reproduction and inspect the buffer-loop-hoisting alias-analysis behavior for scf.while. The change is done when the memref.alloc is hoisted to the loop entry without incorrectly treating the loop exit result as a cross-iteration dependency, while the reproduced input remains valid.

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.