llvm / llvm/llvm-project

[MLIR] CSE fails to eliminate redundant `memref.load` across `scf.if` region

Open
#217,317 3 comments 0 reactions 0 assignees View on GitHub
missed-optimization mlir
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

MLIR CSE misses a redundant `memref.load` inside an `scf.if` region. A load before the `scf.if` dominates the same load in the then branch, and there is no intervening store or side effect that could modify the loaded element.

The load in the then branch `%1 = memref.load %A[%i] : memref` should be replaced by the dominating load `%0 = memref.load %A[%i] : memref`. After that, both branches yield `%0`, so the `scf.if` result can also be simplified.

### Minimized Program

**test.mlir**

```llvm
func.func @load_region(%cond: i1, %A: memref, %i: index) -> (i32, i32) {
%0 = memref.load %A[%i] : memref
%r = scf.if %cond -> (i32) {
%1 = memref.load %A[%i] : memref
scf.yield %1 : i32
} else {
scf.yield %0 : i32
}
return %0, %r : i32, i32
}
```

### Command
```bash
mlir-opt test.mlir --cse
```

Version: ef88536bbf62469eb2396d719d237b3191cf4843

Contributor guide

Open the contributing guide

Research direction

Start with the minimized test.mlir and run mlir-opt test.mlir --cse to reproduce the missed elimination. Trace the MLIR CSE handling for memref.load across scf.if regions; done when the redundant then-branch load is eliminated and the scf.if result is simplified as described.

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.