Skip to content

Managed allocation fast path for linux arm64/loongarch64/riscv64 - #138175

Draft
am11 wants to merge 7 commits into
dotnet:mainfrom
am11:feature/managed-alloc-helpers
Draft

am11 wants to merge 7 commits into
dotnet:mainfrom
am11:feature/managed-alloc-helpers

Conversation

@am11

@am11 am11 commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Prototype following the discussion in #138068: allocation fast paths (NEWSFAST, NEWARR_1_VC/PTR, FastAllocateString) in C# (AllocHelpers.cs), installed as JIT helpers the same way as CastHelpers. Off by default (DOTNET_ManagedAllocHelpers=1), and only used on linux arm64/loongarch64/riscv64 when CanJITOptimizeTLSAccess() is true, where the managed thread-static access avoids the TLSDESC call the native helpers pay and calls land without jump stubs (#138032). Needs #138068 (bti c resolver fix) on BTI-enabled glibc.

The bump and MethodTable store must not be split by a GC: the JIT keeps AllocHelpers intrinsics partially interruptible (also under JitStress/JitFullyInt), so the fast path has no safepoints.

linux-arm64 (Apple M1, Docker), ns per allocation:

Test native managed
object16 3.81 3.45 (−9%)
object56 4.64 4.35 (−6%)
byte[16] 4.00 3.72 (−7%)
object[4] 4.38 4.11 (−6%)
string(8) 5.43 5.75 (+6%)

The alloc context now lives in ThreadLocalData (moved out of t_runtime_thread_locals, so libcoreclr's TLS size is unchanged) and the managed fast path reads it with no pointer load. On M1 that is ~8% slower than the earlier version that loaded a cached pointer; the fast path starts with mrs tpidr_el0 followed by two unfolded adds, which still needs a look and a measurement on a Linux arm64 server. The native helpers read the new location with no change in speed. string(8) is slower, likely from the AllocHelpersConfig check in the ReadyToRun-compiled String constructor; routing FastAllocateString directly (or keeping strings native) would avoid it.

Open questions: the ALIGN8, finalizable and ReadyToRun (R2R) helper variants, and loongarch64/riscv64 and arm64 GCStress runs.

🤖 Generated with Claude Code

…rm64/loongarch64/riscv64

Opt-in via DOTNET_ManagedAllocHelpers=1 (default off). Implements
CORINFO_HELP_NEWSFAST, CORINFO_HELP_NEWARR_1_VC/_PTR and
String.FastAllocateString in CoreLib (System.Runtime.CompilerServices.AllocHelpers).
The fast path reads a cached ee_alloc_context* from a [ThreadStatic] (lazily
initialized through an FCall), bumps alloc_ptr against combined_limit (so
allocation sampling keeps working) and stores the MethodTable/length; misses
fall back to the existing native slow helpers (RhpNew/RhpNewVariableSizeObject).

Context: dotnet#138068 (native alloc helpers pay a TLSDESC call per
allocation on linux arm64/la64/rv64) and dotnet#138032 (arm64 JIT'd code reaches
libcoreclr helpers through jump stubs).

Gating: the managed helpers are only installed on linux arm64/loongarch64/
riscv64 when CanJITOptimizeTLSAccess() is true (the JIT inlines the
thread-static access, so the managed fast path has no TLS call), and only
where the native fast helpers would be used (not with allocation tracking or
logging; GCStress=alloc still selects the slow helpers). Everywhere else the
native helpers are kept regardless of the config. Not compiled for WebAssembly.

- VM: SetJitHelperToILImplementation switches a dynamic helper to an IL
  implementation at startup.
- VM: IL helpers that are not eligible for tiering (AggressiveOptimization)
  are called directly once jitted (no indirection cell, and on arm64 a
  direct `bl` within the code heap instead of a jump stub).
- JIT: methods of AllocHelpers marked [Intrinsic] are never made fully
  interruptible (JitStress/JitFullyInt/debuggable code), and their tail
  calls to the [NoInlining] slow paths are not modeled as GC-poll-free loops.
  The only GC safe points are then calls outside the alloc_ptr bump ->
  MethodTable store region; return-address hijack covers the rest.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Nov 24, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 24, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
10 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: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

am11 and others added 2 commits November 24, 2026 02:58
Compile the managed allocation helpers only for 64-bit targets: they are
only enabled on linux arm64/loongarch64/riscv64, and the ee_alloc_context
layout check does not hold on 32-bit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/coreclr/vm/jitinterfacegen.cpp Resolved
Comment thread src/coreclr/vm/jitinterfacegen.cpp Resolved
am11 and others added 2 commits November 24, 2026 04:17
- Verify the managed AllocHelpers.EEAllocContext layout against the native
  ee_alloc_context with DEFINE_CLASS_U/DEFINE_FIELD_U in corelib.h (checked by
  CoreLibBinder::Check in debug/checked builds) instead of static_asserts.
- Publish the thread's ee_alloc_context pointer in ThreadLocalData::pAllocContext
  (set in InitializeCurrentThreadsStaticData) and map AllocHelpers.t_allocContext
  onto it via TLSIndexType::DirectOnThreadLocalData, the same mechanism used for
  Thread.DirectOnThreadLocalData and ManagedThreadId. This removes the
  AllocHelpers_GetAllocContext FCall and the lazy initialization from the slow
  paths; a null pointer (thread not set up yet) falls back to the slow path.
- Carve pAllocContext out of ExtendedDirectThreadLocalTLSData (48 -> 40 bytes)
  so sizeof(ThreadLocalData) and libcoreclr's TLS segment size are unchanged.
  Growing the TLS segment by 8 bytes pushed libcoreclr out of glibc's static
  TLS surplus on linux-arm64 (Ubuntu 26.04), making the TLSDESC resolver
  dynamic and silently disabling inlined thread-static access for all code.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/coreclr/vm/threadstatics.h Resolved
am11 and others added 2 commits November 24, 2026 05:45
Move RuntimeThreadLocals (the thread's ee_alloc_context) from its own
t_runtime_thread_locals thread local into ThreadLocalData (t_ThreadStatics),
inside the region referenceable by TLSIndexType::DirectOnThreadLocalData.

- The managed allocation helpers map AllocHelpers.t_allocContext onto the
  first field of the embedded ee_alloc_context and use its address, so the
  fast path is TLS base + constant with no pointer load. The pAllocContext
  pointer field is removed and ExtendedDirectThreadLocalTLSData is back to 48
  bytes.
- libcoreclr's TLS segment size is unchanged (the two thread locals are
  merged, not duplicated). Growing it can push libcoreclr out of glibc's
  static TLS surplus, which disables the JIT's inlined TLS access.
- Native code reaches it through GetRuntimeThreadLocals(); the
  INLINE_GET_ALLOC_CONTEXT_BASE asm macros (unix amd64/arm64/loongarch64,
  Windows amd64/x86/arm64) now use t_ThreadStatics with
  OFFSETOF__ThreadLocalData__ee_alloc_context; arm/riscv64 call
  GetThreadEEAllocContext. Builds without FEATURE_MULTITHREADING (wasm) keep
  the t_runtime_thread_locals thread local.
- Thread::m_pRuntimeThreadLocals now points into t_ThreadStatics, so the
  cDAC Thread/RuntimeThreadLocals data descriptors and contracts are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>

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

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI 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.

3 participants