Repository navigation
JIT: enable stack allocation of more arrays in R2R - #116651
Conversation
In particular arrays that are created via `CORINFO_HELP_READYTORUN_NEWARR_1`. Also: * move inlining boost for "returns small array" to the default policy as well as the extended default policy. This addresses part of dotnet#113236. * enable the stack allocation test for R2R, by marking certain methods where particular inlines are needed for non-escape that won't happen in R2R as aggressive opt. Fixes dotnet#109314. Contributes to dotnet#104936
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
There was a problem hiding this comment.
Pull Request Overview
Enables stack allocation of arrays created via the CORINFO_HELP_READYTORUN_NEWARR_1 helper, moves the inlining boost for small-array returns into the default inline policy, and re-enables the corresponding R2R stack-allocation tests.
- Introduce a new allocation kind (
OAT_NEWARR_R2R) and handle it inObjectAllocatorand helper expansion. - Promote the
CALLEE_MAY_RETURN_SMALL_ARRAYobservation into the default inline policy with a higher inline multiplier. - Remove the test exclusion for
ObjectStackAllocationTestsand tag test methods withAggressiveOptimizationso they run under R2R.
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/tests/issues.targets | Re-enable ObjectStackAllocationTests by removing the exclude. |
| src/tests/JIT/opt/ObjectStackAllocation/ObjectStackAllocationTests.cs | Added AggressiveOptimization to force inlining in R2R. |
| src/coreclr/jit/objectalloc.h | Added OAT_NEWARR_R2R enum value. |
| src/coreclr/jit/objectalloc.cpp | Treat OAT_NEWARR_R2R like OAT_NEWARR and dump a debug message. |
| src/coreclr/jit/inlinepolicy.h | Added m_MayReturnSmallArray field to DefaultPolicy. |
| src/coreclr/jit/inlinepolicy.cpp | Handle CALLEE_MAY_RETURN_SMALL_ARRAY in NoteBool and boost multiplier. |
| src/coreclr/jit/helperexpansion.cpp | Support CORINFO_HELP_READYTORUN_NEWARR_1 in fgExpandStackArrayAllocation. |
|
fyi @dotnet/jit-contrib Note the handle embedding call may fail and so cause R2R compilation of a method to be abandoned. We are probably better off not stack allocating in such cases since the method will end up being jitted at Tier0 and allocate anyways. But I don't think there is any way to tell if an embedding request will fail and if so avoid making it. This doesn't quite fix all of #113236, as the (One might hope that "argument is a small locally allocated array" might be a reasonable inlining boost, but these arrays are span captured so not passed directly). |
|
Failures in readytorun/tests/mainv1/mainv1.cmd and similar are likely methods unexpectedly dropping out of R2R because of the embedding issue (still need to verify). |
|
@dotnet/jit-contrib ping |
jakobbotsch
left a comment
There was a problem hiding this comment.
LGTM, assuming we don't want to wait for the crossgen2 fix
|
I would prefer to wait for the proper fix in crossgen2. |
Sounds good, then we can pare back the If you give me some pointers on what to look at here for fixing crossgen2, I can give it a try. |
|
I think the VersionswWithType check in embedClassHandle is superfluous. Trying to delete in #116808 |
|
Per @davidwrighton a fix is not likely before the preview 7 snap. |
|
|
Workflow state for the Holistic Review Orchestrator. {
"version": 5,
"last_dispatched_commit": "7834e469c57ade8a3b31509fbd6229c36e602b41",
"last_dispatched_base_ref": "main",
"last_dispatched_base_sha": "ede442a9233fb67b2af40709a7d23418e0e491e5",
"last_reviewed_commit": "7834e469c57ade8a3b31509fbd6229c36e602b41",
"last_reviewed_base_ref": "main",
"last_reviewed_base_sha": "ede442a9233fb67b2af40709a7d23418e0e491e5",
"last_recorded_worker_run_id": "29672711140",
"review_attempt_commit": "",
"review_attempt_base_ref": "",
"review_attempt_count": 0,
"max_review_attempts": 5,
"review_history_format": "holistic-review-disclosure-v1",
"review_history": [
{
"commit": "7834e469c57ade8a3b31509fbd6229c36e602b41",
"review_id": 4729983876
}
]
} |
There was a problem hiding this comment.
Holistic Review
Motivation: R2R (crossgen2/ReadyToRun) compilation previously blocked stack allocation of arrays created via CORINFO_HELP_READYTORUN_NEWARR_1; the object allocator bailed out with [R2R array not yet supported]. As a result the ObjectStackAllocationTests suite was excluded under R2R (issue #109314). This PR enables stack allocation of constant-length R2R arrays and, separately, moves the "returns small array" inlining boost from the extended policy into the default policy (part of #113236).
Approach: A new allocation kind OAT_NEWARR_R2R is introduced and recognized in AllocationKind for the R2R helper (constant length, single user arg). It flows through CanAllocateLclVarOnStack, MorphAllocObjNodeHelper, and MorphAllocObjNodeHelperArr, where the length arg index differs (0 for R2R vs 1 otherwise). Helper expansion learns to synthesize the method-table pointer from compileTimeHelperArgumentHandle when there is no explicit type arg. Because embedding the class handle can fail and abandon R2R compilation, MorphAllocObjNodeHelperArr proactively probes embedClassHandle under an error trap and declines stack allocation ([cannot embed class handle]) rather than risk dropping the method out of R2R. The m_MayReturnSmallArray field and its +4.0 multiplier are lifted into DefaultPolicy. Tests are re-enabled, with several methods marked AggressiveOptimization to work around the embedding limitation. The approach is sound, matches existing patterns (mirrors the non-R2R OAT_NEWARR path and the extended policy's small-array bonus), and the conservative embed probe is a reasonable tradeoff consistent with the author's own comments and jkotas's note about a future crossgen2 fix.
Summary: The JIT-side changes are well-structured and consistent with surrounding code. The R2R length-arg indexing, the typeArgIndex == -1 method-table synthesis, and the OAT_NEWARR_R2R propagation all appear correct. I found one concern worth the author's attention: the placement of the new m_MayReturnSmallArray block in DefaultPolicy::DetermineMultiplier inadvertently breaks the existing else if chain guarding the prejit-root bonus, changing its condition. Details are in the inline comment. This is a behavioral change to an unrelated path rather than a correctness bug in the stack-allocation feature itself, so it should be verified/adjusted but does not undermine the core PR.
Detailed Findings
-
⚠️ src/coreclr/jit/inlinepolicy.cpp(~line 770-782): The insertedif (m_MayReturnSmallArray)block splits the pre-existingif (m_ConstantArgFeedsConstantTest > 0) { ... } else if (m_IsPrejitRoot && ...)chain, so the prejit-root bonus is now gated on!m_MayReturnSmallArrayinstead of!m_ConstantArgFeedsConstantTest. See inline comment for the full explanation and suggested fix (make the small-array bonus a standaloneifplaced after the prejitelse if, as already done inExtendedDefaultPolicy::DetermineMultiplier). -
💡 Test methods marked with
AggressiveOptimizationare an explicit, well-commented workaround for the handle-embedding limitation. This is fine as an interim measure, but note it means these tests no longer exercise the default Tier0→Tier1 path; the coupling to the embedding behavior is a latent maintenance cost if the crossgen2 fix (per jkotas) later removes the need. Not a blocker.
Note
This review was generated by this repository's Holistic Review agentic workflow to complement the built-in Copilot review.
Generated by Holistic Review · 103.9 AIC · ⌖ 10.2 AIC · ⊞ 10K
|
Revived this now that #137057 is in place. @dotnet/jit-contrib PTAL |
Co-authored-by: Copilot <[email protected]> Copilot-Session: 852b06b9-6689-43fe-b92f-1fe344dea5b3
There was a problem hiding this comment.
🟡 Changes recommended
R2R array stack allocation can violate ARM32’s eight-byte alignment requirement for long[] and double[], while the inlining-policy change incorrectly suppresses an existing benefit for some prejit roots.
2 open issues
Review effort: Balanced
Co-authored-by: Copilot <[email protected]> Copilot-Session: 852b06b9-6689-43fe-b92f-1fe344dea5b3
Co-authored-by: Copilot <[email protected]> Copilot-Session: 852b06b9-6689-43fe-b92f-1fe344dea5b3
|
@EgorBo can you take a look? |
There was a problem hiding this comment.
🔵 Needs a closer look
The R2R stack-array changes affect method-table materialization and ARM32 alignment in GC-sensitive code, but Crossgen execution, GC-stress coverage, and ARM32 validation were unavailable in this source-only review.
No open issues 🎉
2 resolved since last review
Review effort: Balanced
Co-authored-by: Copilot <[email protected]> Copilot-Session: 852b06b9-6689-43fe-b92f-1fe344dea5b3


In particular arrays that are created via
CORINFO_HELP_READYTORUN_NEWARR_1.Also:
Fixes #109314.
Contributes to #104936