llvm / llvm/llvm-project

Missing optimization with multiply-inlined magic statics

Open
#187,889 0 comments 0 reactions 0 assignees View on GitHub
llvm:optimizations
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

If a function with magic static is inlined into another function multiple times, it is inlined very literally; code for checking that the static variable was initialized, initializing it if it wasn't, and even the load itself are duplicated.

[*Example on godbolt*](https://godbolt.org/z/xsYT8nMd6)
```cpp
int opaque();
int adjust() {
static int base = opaque(); // force non-constant initialization
return base;
}

int double_adjusted() {
return adjust() + adjust();
}
```

The code for `double_adjusted` should check the guard variable just once, and produce the return value with the equivalent of `2 * adjust()`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.