Skip to content

Use TP-relative TLS offset in allocation helpers - #138068

Open
am11 wants to merge 1 commit into
dotnet:mainfrom
am11:feature/alloc-tls-offset-rv-la
Open

am11 wants to merge 1 commit into
dotnet:mainfrom
am11:feature/alloc-tls-offset-rv-la

Conversation

@am11

@am11 am11 commented Nov 21, 2026

Copy link
Copy Markdown
Member

On linux-arm64, loongarch64 and riscv64, INLINE_GET_ALLOC_CONTEXT_BASE went through a TLS descriptor call (arm64, loongarch64) or a call to GetThreadEEAllocContext (riscv64) on every allocation. When the runtime's thread locals are in static TLS (CanJITOptimizeTLSAccess()), their offset from the thread pointer is the same for every thread, so compute it once in InitThreadManager and have the helpers add it to the thread pointer instead. The existing sequence remains the fallback when the offset is 0 (musl riscv64, FreeBSD, Android, DOTNET_DisableOptimizedThreadStaticAccess, ...).

Also accept "bti c" (hint 34) before the static TLS resolver in IsValidTLSResolver. glibc built with branch protection (e.g. Ubuntu arm64) starts _dl_tlsdesc_return with it, which disabled the TLS optimizations there.

Allocation microbenchmark (20M allocations per test, best of runs, main vs this change in the same testhost):

Arch Device Change vs main Fast path disabled
riscv64 Device -8% to -19% ~ main
arm64 Device -5% to -8.5% +4% to +10%
loongarch64 qemu-user -1% to -38% ~ main
Test riscv64 main -> new arm64 main -> new
object16 123.4 -> 100.0 ns 3.61 -> 3.31 ns
object56 234.6 -> 209.6 ns 4.27 -> 3.93 ns
byte[16] 173.3 -> 148.7 ns 3.86 -> 3.55 ns
object[4] 207.1 -> 189.8 ns 4.13 -> 3.78 ns
string8 200.2 -> 177.1 ns 6.18 -> 5.87 ns

loongarch64 was measured under qemu-user only, which overstates the cost of calls; it confirms the fast path is taken, not the size of the gain.

Fixes #138009

On linux-arm64, loongarch64 and riscv64, INLINE_GET_ALLOC_CONTEXT_BASE went
through a TLS descriptor call (arm64, loongarch64) or a call to
GetThreadEEAllocContext (riscv64) on every allocation. When the runtime's thread
locals are in static TLS (CanJITOptimizeTLSAccess()), their offset from the
thread pointer is the same for every thread, so compute it once in
InitThreadManager and have the helpers add it to the thread pointer instead. The
existing sequence remains the fallback when the offset is 0 (musl riscv64,
FreeBSD, Android, DOTNET_DisableOptimizedThreadStaticAccess, ...).

Also accept "bti c" (hint 34) before the static TLS resolver in
IsValidTLSResolver. glibc built with branch protection (e.g. Ubuntu arm64)
starts _dl_tlsdesc_return with it, which disabled the TLS optimizations there.

Allocation microbenchmark (20M allocations per test, best of runs, main vs
this change in the same testhost):

| Arch        | Device    | Change vs main         | Fast path disabled |
|-------------|-----------|------------------------|--------------------|
| riscv64     | Device    | -8% to -19%            | ~ main             |
| arm64       | Device    | -5% to -8.5%           | +4% to +10%        |
| loongarch64 | qemu-user | -1% to -38%            | ~ main             |

| Test      | riscv64 main -> new | arm64 main -> new  |
|-----------|---------------------|--------------------|
| object16  | 123.4 -> 100.0 ns   | 3.61 -> 3.31 ns    |
| object56  | 234.6 -> 209.6 ns   | 4.27 -> 3.93 ns    |
| byte[16]  | 173.3 -> 148.7 ns   | 3.86 -> 3.55 ns    |
| object[4] | 207.1 -> 189.8 ns   | 4.13 -> 3.78 ns    |
| string8   | 200.2 -> 177.1 ns   | 6.18 -> 5.87 ns    |

loongarch64 was measured under qemu-user only, which overstates the cost of
calls; it confirms the fast path is taken, not the size of the gain.

Fixes dotnet#138009

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 21, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke
See info in area-owners.md if you want to be subscribed.

@am11
am11 requested review from EgorBo, janvorli and jkotas November 21, 2026 05:06
@am11 am11 added arch-arm64 arch-loongarch64 arch-riscv Related to the RISC-V architecture labels Nov 21, 2026
@jkotas

jkotas commented Nov 21, 2026

Copy link
Copy Markdown
Member

Isn't the linker supposed to replace the sequence produced by INLINE_GET_TLS_VAR with something equally efficient? What is the linker replacing INLINE_GET_TLS_VAR with?

@am11

am11 commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Only when linking an executable. libcoreclr.so is a shared library, so the linker can't know the TLS block's offset from TP and keeps the TLSDESC sequence (R_AARCH64_TLSDESC relocs resolved by ld.so): stp x0, lr + adrp/ldr/add + blr to _dl_tlsdesc_return + mrs/add + ldp, on every allocation. In singlefilehost (static link) the same code is relaxed to local-exec (movz/movk + nops + mrs/add). This PR does the equivalent at runtime once the static resolver is confirmed.

@jkotas

jkotas commented Nov 23, 2026

Copy link
Copy Markdown
Member

stp x0, lr + adrp/ldr/add + blr to _dl_tlsdesc_return + mrs/add + ldp, on every allocation

That's unfortunate.

I am wondering whether we should copy out the alloc helpers to our own exec memory instead. It would allow us to optimize them even more and it would address issues like #138032

@jkotas

jkotas commented Nov 23, 2026

Copy link
Copy Markdown
Member

cc @EgorBo

@am11

am11 commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

Agreed, copying them (like the write barrier copy) would let us write the TP offset into a literal table and get direct bl from JIT'd code, which also covers fix 4 in #138032. I'd like to keep this PR to the offset and the bti c fix and do the copy as a follow-up.

@EgorBo

EgorBo commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

I am wondering whether we should copy out the alloc helpers to our own exec memory instead. It would allow us to optimize them even more and it would address issues like #138032

.. or just convert them into C# or even inline?

inlining them might help to hoist/share the TLS context if multiple allocation exists (or inside a loop). + pMT->BaseSize constant folded + array checks folded.

Comment on lines +1306 to +1313
uint8_t* tp;
#if defined(TARGET_ARM64)
__asm__("mrs %0, tpidr_el0" : "=r"(tp));
#elif defined(TARGET_LOONGARCH64)
__asm__("move %0, $tp" : "=r"(tp));
#else
__asm__("mv %0, tp" : "=r"(tp));
#endif

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks very similar to GetThreadStaticsVariableOffset . Should we use uniform approach?

#else
#if !defined(__APPLE__)
// Use the thread pointer relative offset when the thread locals are in static TLS (set in InitThreadManager).
// Trashes x12.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we go with copying out the alloc helpers, these changes are unnecessary.

@jkotas

jkotas commented Nov 23, 2026

Copy link
Copy Markdown
Member

.. or just convert them into C# or even inline?

It would be certainly neat. We would need to figure out how to make the methods no-GC regions.

@am11

am11 commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

I tried the C# route in draft #138175: the allocation fast paths are in CoreLib (installed like CastHelpers, off by default), used on linux arm64/la64/rv64 when CanJITOptimizeTLSAccess() is true, and the JIT keeps them partially interruptible so the bump and MethodTable store can't be split by a GC. On linux-arm64 it's 4-16% faster than the native helpers; it relies on the bti c fix from this PR.

This branch has not been deployed

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

Labels

arch-arm64 arch-loongarch64 arch-riscv Related to the RISC-V architecture area-VM-coreclr community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

VM: (opt) arm64 Linux: allocation helpers call the TLS descriptor resolver on every allocation

3 participants