Skip to content

Access thread-local variables via llvm.threadlocal.address - #5316

Open
kubo39 wants to merge 1 commit into
ldc-developers:masterfrom
kubo39:use-threadlocal-address
Open

kubo39 wants to merge 1 commit into
ldc-developers:masterfrom
kubo39:use-threadlocal-address

Conversation

@kubo39

@kubo39 kubo39 commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Like clang, wrap TLS accesses in llvm.threadlocal.address so that LLVM can CSE/LICM the TLS address computation, e.g. avoiding repeated __tls_get_addr calls with the global-dynamic model.

@kubo39
kubo39 force-pushed the use-threadlocal-address branch from 286c2e7 to 156d80f Compare November 2, 2026 23:12
@kubo39
kubo39 marked this pull request as ready for review November 3, 2026 19:17
@kinke

kinke commented Nov 7, 2026

Copy link
Copy Markdown
Member

I'm curious about the motivation for your recent contributions - do you see bad performance wrt. TLS in your code? And significant improvements with e.g. this change here?

I didn't expect that we'd have to take care about caching the thread-specific address manually in the IR frontend. And if the point is to avoid repeated __tls_get_addr calls etc., I suggest adding an according test with multiple usages (read/write) of a thread-local global in a single function - I guess you want a single llvm.threadlocal.address call there.

Like clang, wrap TLS accesses in llvm.threadlocal.address so
that LLVM can CSE/LICM the TLS address computation, e.g. avoiding
repeated __tls_get_addr calls with the global-dynamic model.
@kubo39
kubo39 force-pushed the use-threadlocal-address branch from 156d80f to 3dc863a Compare November 7, 2026 16:39
@kubo39

kubo39 commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

I'm curious about the motivation for your recent contributions - do you see bad performance wrt. TLS in your code? And significant improvements with e.g. this change here?

Actually, no. Recently we've found that compared to clang/rust, ldc cannot generate optimized code for TLS accesses, so just tried to improve it. In most case linker relaxation already works well for executables; the main remaining case is shared libraries, but we didn't have a particular use case for it.
(I've written some articles in Japanese, on thread-local storage and linker relaxation and comparing ldc and rust)

And if the point is to avoid repeated __tls_get_addr calls etc., I suggest adding an according test with multiple usages (read/write) of a thread-local global in a single function - I guess you want a single llvm.threadlocal.address call there.

Ah, yes. I've updated the test the same way as clang.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants