Skip to content

JIT: Stack allocate non-escaping strings#129202

Draft
EgorBo wants to merge 1 commit into
dotnet:mainfrom
EgorBo:egorbo/jit-stackalloc-strings
Draft

JIT: Stack allocate non-escaping strings#129202
EgorBo wants to merge 1 commit into
dotnet:mainfrom
EgorBo:egorbo/jit-stackalloc-strings

Conversation

@EgorBo

@EgorBo EgorBo commented Jun 10, 2026

Copy link
Copy Markdown
Member

Now that we intrinsified String.FastAllocateString, I guess it wouldn't hurt to enable escape analysis for it by repeating what we do for normal arrays. Most strings leak into other string APIs which actually don't escape, but we don't do IPA today. As is, this PR enables 74 strings to be stack-allocated (mostly in libraries_tests_no_tiered_compilation.run)

Stack allocate non-escaping System.String instances created via
String.FastAllocateString when the length is a compile-time constant,
reusing the existing array stack-allocation machinery.

At object-allocator time a string allocation is modelled as
FastAllocateString(pMT, length) and is treated like a char[] array via a
new OAT_NEWSTR allocation kind. The call is retyped to a native pointer,
gets a StackArrayLocal arg and the GTF_CALL_M_STACK_ARRAY flag, and is
later expanded in fgExpandStackArrayAllocations into method-table and
length stores (the length field offset is the only difference from
arrays). The stack object uses a non-GC block layout sized for the
header, chars and null terminator, capped by JitObjectStackAllocationSize.

Gated by the new DOTNET_JitObjectStackAllocationString config (on by
default). R2R is not yet supported.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 10, 2026 01:18
@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 Jun 10, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR extends CoreCLR JIT object stack allocation support to cover System.String.FastAllocateString call sites (in addition to newarr), enabling escape analysis to stack-allocate eligible constant-length strings and expanding them via the existing stack-array allocation expansion path.

Changes:

  • Teach ObjectAllocator to recognize String.FastAllocateString(pMT, length) as a stack-allocation candidate (OAT_NEWSTR) when length is a compile-time constant.
  • Reuse the stack-array allocation machinery to represent and expand stack-allocated strings (extra StackArrayLocal arg, retype to native pointer, and late expansion in fgExpandStackArrayAllocations).
  • Add config/metrics/method-flag plumbing for string stack allocation (JitObjectStackAllocationString, new JIT metrics, OMF_HAS_FASTSTRINGALLOC).

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/coreclr/jit/valuenum.cpp Assign a fresh non-heap VN for stack-allocated FastAllocateString calls flagged with GTF_CALL_M_STACK_ARRAY.
src/coreclr/jit/objectalloc.h Add OAT_NEWSTR and the MorphAllocObjNodeHelperStr helper declaration.
src/coreclr/jit/objectalloc.cpp Detect FastAllocateString candidates, size-check strings, and morph eligible ones into the existing stack-array allocation path.
src/coreclr/jit/jitmetadatalist.h Add new JIT metrics for string allocation and stack-allocated strings.
src/coreclr/jit/jitconfigvalues.h Add JitObjectStackAllocationString config switch.
src/coreclr/jit/importercalls.cpp Flag methods containing FastAllocateString so escape analysis runs; also mark ThrowIfNull as special (non-fallthrough).
src/coreclr/jit/helperexpansion.cpp Extend stack-array expansion to also expand stack-allocated FastAllocateString by using the string length field offset.
src/coreclr/jit/compiler.h Add OMF_HAS_FASTSTRINGALLOC method flag.

Comment on lines +1214 to +1218
if (length < 0)
{
*reason = "[invalid string length]";
return false;
}
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants