llvm / llvm/llvm-project

Missed optimization: fold a chain of getelementptr operations with the same index into a single getelementptr with a multiplied index

Open
#186,298 2 comments 0 reactions 0 assignees View on GitHub
llvm:instcombine missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

```llvm
define ptr @src(i64 %arg0, ptr %arg1) {
%v0 = getelementptr inbounds double, ptr %arg1, i64 %arg0
%v1 = getelementptr inbounds double, ptr %v0, i64 %arg0
%v2 = getelementptr inbounds double, ptr %v1, i64 %arg0
%v3 = getelementptr inbounds double, ptr %v2, i64 %arg0
ret ptr %v3
}

define ptr @tgt(i64 %arg0, ptr %arg1) {
%idx = mul i64 4, %arg0
%v3 = getelementptr inbounds double, ptr %arg1, i64 %idx
ret ptr %v3
}
```

godbolt: https://godbolt.org/z/jEqE81K71
alive2: https://alive2.llvm.org/ce/z/CSfwcA

Pattern found in: https://github.com/dtcxzyw/llvm-opt-benchmark/blob/main/bench/openblas/optimized/dgemv_n.ll

Contributor guide

Open the contributing guide

Research direction

Start with the LLVM IR reproducer in the issue and compare its output with the linked Godbolt and Alive2 results. Review the referenced openblas optimized/dgemv_n.ll benchmark pattern to understand the motivating case. Done means the repeated getelementptr chain is recognized as equivalent to the single multiplied-index form without changing semantics.

Written by the indexing model from the issue text.

Assessment

Domain
compilers, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.