Skip to content

JIT: enable stack allocation of more arrays in R2R - #116651

Merged
AndyAyersMS merged 8 commits into
dotnet:mainfrom
AndyAyersMS:EscapeAnalysisR2RArrayFix
Nov 17, 2026
Merged

AndyAyersMS merged 8 commits into
dotnet:mainfrom
AndyAyersMS:EscapeAnalysisR2RArrayFix

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

In particular arrays that are created via CORINFO_HELP_READYTORUN_NEWARR_1.

Also:

Fixes #109314.

Contributes to #104936

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
@AndyAyersMS
AndyAyersMS marked this pull request as ready for review June 13, 2025 20:16
Copilot AI review requested due to automatic review settings June 13, 2025 20:16
@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 13, 2025
@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.

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

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 in ObjectAllocator and helper expansion.
  • Promote the CALLEE_MAY_RETURN_SMALL_ARRAY observation into the default inline policy with a higher inline multiplier.
  • Remove the test exclusion for ObjectStackAllocationTests and tag test methods with AggressiveOptimization so 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.

Comment thread src/coreclr/jit/helperexpansion.cpp Resolved
Comment thread src/coreclr/jit/objectalloc.cpp Resolved
@AndyAyersMS

Copy link
Copy Markdown
Member Author

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 TryWriteBytes methods are still not inlined. I currently don't see an obvious tweak we can make in the heuristics so we may need to decide if AggressiveInlining is appropriate.

(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).

@AndyAyersMS

Copy link
Copy Markdown
Member Author

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).

Comment thread src/coreclr/jit/objectalloc.cpp Resolved
@AndyAyersMS

Copy link
Copy Markdown
Member Author

@dotnet/jit-contrib ping

@jakobbotsch jakobbotsch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, assuming we don't want to wait for the crossgen2 fix

@jkotas

jkotas commented Jun 18, 2025

Copy link
Copy Markdown
Member

I would prefer to wait for the proper fix in crossgen2.

@AndyAyersMS

Copy link
Copy Markdown
Member Author

I would prefer to wait for the proper fix in crossgen2.

Sounds good, then we can pare back the AggressiveOptimization in the test case to just the ones that require inlining.

If you give me some pointers on what to look at here for fixing crossgen2, I can give it a try.

@jkotas

jkotas commented Jun 19, 2025

Copy link
Copy Markdown
Member

I think the VersionswWithType check in embedClassHandle is superfluous. Trying to delete in #116808

@AndyAyersMS

Copy link
Copy Markdown
Member Author

Per @davidwrighton a fix is not likely before the preview 7 snap.

@AndyAyersMS

Copy link
Copy Markdown
Member Author

Per @davidwrighton a fix is not likely before the preview 7 snap.

@github-actions

github-actions Bot commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

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
    }
  ]
}

@github-actions github-actions Bot 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.

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 inserted if (m_MayReturnSmallArray) block splits the pre-existing if (m_ConstantArgFeedsConstantTest > 0) { ... } else if (m_IsPrejitRoot && ...) chain, so the prejit-root bonus is now gated on !m_MayReturnSmallArray instead of !m_ConstantArgFeedsConstantTest. See inline comment for the full explanation and suggested fix (make the small-array bonus a standalone if placed after the prejit else if, as already done in ExtendedDefaultPolicy::DetermineMultiplier).

  • 💡 Test methods marked with AggressiveOptimization are 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

Comment thread src/coreclr/jit/inlinepolicy.cpp Resolved
@AndyAyersMS

Copy link
Copy Markdown
Member Author

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

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.

🟡 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

Comment thread src/coreclr/jit/objectalloc.cpp Resolved
Comment thread src/coreclr/jit/inlinepolicy.cpp Resolved
AndyAyersMS and others added 2 commits November 13, 2026 15:28
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
@AndyAyersMS

Copy link
Copy Markdown
Member Author

@EgorBo can you take a look?

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.

🔵 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

Comment thread src/coreclr/jit/objectalloc.cpp Resolved
Comment thread src/coreclr/jit/objectalloc.cpp Resolved
Co-authored-by: Copilot <[email protected]>
Copilot-Session: 852b06b9-6689-43fe-b92f-1fe344dea5b3
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.

Crossgen outerloop regression: JIT/opt/ObjectStackAllocation/ObjectStackAllocationTests/ObjectStackAllocationTests.cmd

6 participants