Repository navigation
Conversation
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]>
|
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. |
|
Tagging subscribers to this area: @agocke |
|
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? |
|
Only when linking an executable. |
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 |
|
cc @EgorBo |
|
Agreed, copying them (like the write barrier copy) would let us write the TP offset into a literal table and get direct |
.. 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. |
| 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 |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
If we go with copying out the alloc helpers, these changes are unnecessary.
It would be certainly neat. We would need to figure out how to make the methods no-GC regions. |
|
I tried the C# route in draft #138175: the allocation fast paths are in CoreLib (installed like |
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):
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