JIT: Stack allocate non-escaping strings#129202
Draft
EgorBo wants to merge 1 commit into
Draft
Conversation
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>
Contributor
There was a problem hiding this comment.
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
ObjectAllocatorto recognizeString.FastAllocateString(pMT, length)as a stack-allocation candidate (OAT_NEWSTR) whenlengthis a compile-time constant. - Reuse the stack-array allocation machinery to represent and expand stack-allocated strings (extra
StackArrayLocalarg, retype to native pointer, and late expansion infgExpandStackArrayAllocations). - 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; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 inlibraries_tests_no_tiered_compilation.run)