Skip to content

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

Description

@EgorBo

Problem

On linux-arm64, RhpNewFast, RhpNewArrayFast, RhpNewPtrArrayFast and RhNewString get the allocation context through INLINE_GET_ALLOC_CONTEXT_BASE, which uses a general-dynamic TLS descriptor sequence. libcoreclr.so is a shared library, so the linker can't relax it, and every allocation pays for an indirect call to the resolver plus a save/restore of x0/lr:

RhpNewFast:
    stp  x0, x30, [sp, #-0x10]!
    adrp x0, <tlsdesc page>
    ldr  x3, [x0, #...]          ; resolver
    add  x0, x0, #...
    blr  x3                      ; _dl_tlsdesc_return: ldr x0, [x0, #8]; ret
    mrs  x3, TPIDR_EL0
    add  x3, x3, x0
    ldp  x0, x30, [sp], #0x10
    ldr  w2, [x0, #4]            ; MethodTable::m_BaseSize
    ...

In an OrchardCore profile on Cobalt 200, RhpNewFast alone is 3.3% of all cycles, plus 0.13% inside ld-linux for the resolver.

The VM already knows how to avoid this for thread statics: CanJITOptimizeTLSAccess() validates that the TLS resolver is the static one (IsValidTLSResolver), so the TP-relative offset is the same for every thread, and the JIT then inlines mrs tpidr_el0 + offset. t_runtime_thread_locals lives in the same TLS block of libcoreclr.so, so the same reasoning applies to it.

Proposal

Compute the TP-relative offset of t_runtime_thread_locals once at startup (when CanJITOptimizeTLSAccess() is true) and use it in the asm helpers, falling back to the existing TLS descriptor sequence when it's 0 (musl, FreeBSD, emulated TLS, dynamic resolver, DOTNET_DisableOptimizedThreadStaticAccess, ...):

RhpNewFast:
    adrp x3, g_runtimeThreadLocalsTlsOffset
    ldr  x3, [x3, :lo12:g_runtimeThreadLocalsTlsOffset]
    cbz  x3, <tlsdesc fallback>
    mrs  x12, tpidr_el0
    add  x3, x3, x12
    ldr  w2, [x0, #4]
    ...

No call, no stack traffic. Only x12 is used as an extra scratch register, which is free at all current uses of the macro (RhpNewFast, NEW_ARRAY_FAST).

--- a/src/coreclr/vm/threads.cpp
+++ b/src/coreclr/vm/threads.cpp
+#if defined(TARGET_ARM64) && defined(TARGET_UNIX) && !defined(TARGET_APPLE) && !defined(FEATURE_EMULATED_TLS)
+// Thread pointer relative offset of t_runtime_thread_locals when it lives in static TLS, 0 otherwise.
+// Used by the allocation helpers to avoid a TLS descriptor call (see INLINE_GET_ALLOC_CONTEXT_BASE).
+extern "C" size_t g_runtimeThreadLocalsTlsOffset = 0;
+#endif
 ...
 void InitThreadManager()
 {
     STANDARD_VM_CONTRACT;
 
+#if defined(TARGET_ARM64) && defined(TARGET_UNIX) && !defined(TARGET_APPLE) && !defined(FEATURE_EMULATED_TLS)
+    // A static TLS resolver means the offset is the same for all threads.
+    if (CanJITOptimizeTLSAccess())
+    {
+        g_runtimeThreadLocalsTlsOffset = (size_t)((uint8_t*)&t_runtime_thread_locals - (uint8_t*)__builtin_thread_pointer());
+    }
+#endif

--- a/src/coreclr/pal/inc/unixasmmacrosarm64.inc
+++ b/src/coreclr/pal/inc/unixasmmacrosarm64.inc
-// Inlined version of GetThreadEEAllocContext. Target cannot be x0 or x1.
+// Inlined version of GetThreadEEAllocContext. Target cannot be x0 or x1. Trashes x12.
 ...
 #else
+#if !defined(__APPLE__)
+        // Fast path: t_runtime_thread_locals is in static TLS and its thread pointer relative
+        // offset was computed at startup (g_runtimeThreadLocalsTlsOffset). Trashes x12.
+        PREPARE_EXTERNAL_VAR_INDIRECT g_runtimeThreadLocalsTlsOffset, \target
+        cbz     \target, .LAllocContextTlsDesc\@
+        mrs     x12, tpidr_el0
+        add     \target, \target, x12
+        b       .LAllocContextDone\@
+.LAllocContextTlsDesc\@:
+#endif
         PROLOG_SAVE_REG_PAIR_INDEXED      x0, lr, -0x10
         INLINE_GET_TLS_VAR \target, C_FUNC(t_runtime_thread_locals)
+        EPILOG_RESTORE_REG_PAIR_INDEXED   x0, lr, 0x10
+#if !defined(__APPLE__)
+.LAllocContextDone\@:
+#endif
 
         .ifnc OFFSETOF__RuntimeThreadLocals__ee_alloc_context, 0
                 add     \target, \target, OFFSETOF__RuntimeThreadLocals__ee_alloc_context
         .endif
-
-        EPILOG_RESTORE_REG_PAIR_INDEXED   x0, lr, 0x10
 #endif

Measurements (Cobalt 200, linux-arm64)

Allocation loop (s_sink = new Small() / s_sink = new byte[8], 1M per batch, best of 7 rounds, 3 alternating runs):

Baseline Patched
new Small() ~275 M/s ~309 M/s +12%
new byte[8] ~260 M/s ~284 M/s +9%

OrchardCore /about (6 runs each, 3 processes, rotated order): 27,035 -> 27,282 RPS (+0.9%; +1.2% excluding a low-CPU outlier process present in both configs). In the profile, RhpNewFast dropped from 3.26% to 2.72% of cycles and the ld-linux resolver samples from 0.13% to ~0.02%.

Notes

  • Why a single offset is valid: CanJITOptimizeTLSAccess() probes the TLS descriptor of t_ThreadStatics, not t_runtime_thread_locals. Both live in libcoreclr.so, and an ELF module's TLS block is either entirely in static TLS or entirely dynamic, so the probe covers both. This invariant should be stated in a comment next to the offset computation. Regression test validating usage of static TLS resolver on Linux ARM64. #131995 added a test asserting the static resolver is used on linux-arm64, so the fast path is the expected configuration.
  • The offset stays 0 (fallback) before InitThreadManager, on musl/FreeBSD/Android, with DOTNET_DisableOptimizedThreadStaticAccess, and when the resolver isn't recognized.
  • The conditional x0/lr save is fine for unwinding: the fallback block sits linearly between the fast path and the join label, and the CFA is restored before the join, so every PC has a correct CFI row. Worth double-checking with readelf --debug-frame.
  • NativeAOT shares runtime/arm64/AllocFast.S but has its own INLINE_GET_ALLOC_CONTEXT_BASE (static linking, TLS already relaxed), so the change stays in the CoreCLR PAL macro only.
  • The remaining cost in RhpNewFast is the m_BaseSize load and the alloc-pointer load (memory latency), not instruction count.
  • linux-x64 has the same pattern, with a heavier call: INLINE_GET_TLS_VAR in runtime/amd64/AsmMacros_Unix.inc does callq __tls_get_addr@PLT (general-dynamic) on every allocation. x64 has no static-resolver probe today (its JIT thread-static path also calls __tls_get_addr), so it would need its own static-TLS detection. Not measured.

Note

This issue was drafted with the help of GitHub Copilot.

Activity

  1. added
    untriaged
    on Nov 20, 2026
  2. dotnet-policy-service commented on Nov 20, 2026

    @dotnet-policy-service
    Contributor

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

  3. added a commit that references this issue on Nov 21, 2026
    b3206d8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-VM-coreclruntriaged

    Type

    No type

    Projects

    • Status
      No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions