llvm / llvm/llvm-project

GlobalMerge does not merge thread_local variables

Open
#220,992 0 comments 0 reactions 0 assignees View on GitHub
llvm:optimizations missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

LLVM does not merge thread-local variables in the following example

```c++
#include
#include

uint64_t xor128() {
thread_local uint64_t x = 123456789;
thread_local uint64_t y = 362436069;
thread_local uint64_t z = 521288629;
thread_local uint64_t w = 88675123;

uint64_t t;
t = x ^ (x << 11);
x = y;
y = z;
z = w;
return w = w ^ (w >> 19) ^ (t ^ (t >> 8));
}
```

These variables appear to be safe candidates for aggregation into a single TLS object, allowing accesses via fixed offsets from a common TLS base. However, LLVM emits separate TLS address materialization sequences for each variable.

Godbolt: https://godbolt.org/z/zzsjvzfnY

Contributor guide

Open the contributing guide

Research direction

Start with the C++ reproducer in the issue and inspect LLVM's GlobalMerge implementation and the generated output shown on Godbolt. Compare how the four thread_local variables are addressed today with the proposed shared TLS object and fixed offsets. Done means safe candidates are aggregated without changing the xor128 behavior or TLS semantics.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.