llvm / llvm/llvm-project

[MLIR] SROA misses optimization on `alloca` with `getelementptr`

Open
#217,011 2 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 SROA optmization fails to promote memory locations created by `llvm.alloca` when memory accesses go through `llvm.getelementptr` instructions.

Even though all accesses are to fixed, non-escaping scalar fields of the structure, SROA leaves the `llvm.alloca`, `llvm.store`, and `llvm.load` intact. Ideally, SROA should promote the accessed scalar field and fold the store/load sequence to a direct return of the constant.

### Minimal Reproducer

**test.mlir**

```llvm
module {
func.func @nested_struct() -> i32 {
%c1_i64 = arith.constant 1 : i64
%c7_i32 = arith.constant 7 : i32
%0 = llvm.alloca %c1_i64 x !llvm.struct<(i32, i32)> : (i64) -> !llvm.ptr
%1 = llvm.getelementptr %0[0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i32, i32)>
%2 = llvm.getelementptr %1[0, 1] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(i32, i32)>
llvm.store %c7_i32, %2 : i32, !llvm.ptr
%3 = llvm.load %2 : !llvm.ptr -> i32
return %3 : i32
}
}
```

### Command for Reproduction
```bash
mlir-opt --sroa test.mlir
```

Version: ef88536bbf62469eb2396d719d237b3191cf4843

Contributor guide

Open the contributing guide

Research direction

Start with the minimal reproducer in test.mlir and run `mlir-opt --sroa test.mlir` to inspect the current output. Trace how SROA handles the llvm.alloca, llvm.getelementptr, llvm.store, and llvm.load sequence; done means the accessed scalar field is promoted and the store/load sequence folds to a direct return of the constant.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.