Skip to content

Conversation

@AndyAyersMS
Copy link
Member

Backport of #121771 to release/10.0

/cc @AndyAyersMS

Customer Impact

  • Customer reported
  • Found internally

Reported by customer in #121736

Regression

  • Yes
  • No

An issue in a new optimization in .NET 10.

Escape analysis assumes each stack allocation candidate node has a unique temp that has that allocation as its only assigned value. Conditional escape analysis may clone these nodes, creating a second allocation site assigning to the same temp. This breaks the 1-1 assumption noted above and can lead to silent bad codegen.

To fix this we disallow stack allocation for sites in blocks that were cloned, or their clones.

Testing

Verified on the test case from the issue.

Risk

Low. Disables an optimization. No diffs for this in our SPMI testing.

…#121771)

Our escape analysis assumes each stack allocation candidate node has a
unique temp that has that allocation as its only assigned value.

Conditional escape analysis may clone these nodes, creating a second
allocation site assigning to the associated temp. This breaks the 1-1
assumption noted above.

If those allocations do not escape then the JIT will stack allocate
them, creating two distinct stack locals. In subsequent IR rewriting the
JIT will then use the address of just one of the locals for all
appearances of the temp, which is incorrect, and leads to the generated
code possibly reading uninitialized stack slots.

To fix this we disallow stack allocation for sites in blocks that were
cloned, or their clones. (We can actually handle this case with better
bookkeeping, but that is more involved).

Fixes dotnet#121736.
Copilot AI review requested due to automatic review settings November 20, 2025 21:27
@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 20, 2025
@AndyAyersMS AndyAyersMS added this to the 10.0.x milestone Nov 20, 2025
@AndyAyersMS
Copy link
Member Author

PTAL @dotnet/jit-contrib
FYI @JulieLeeMSFT

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

Copilot finished reviewing on behalf of AndyAyersMS November 20, 2025 21:30
Copy link
Contributor

Copilot AI left a comment

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 is a backport of #121771 to fix a JIT issue where conditional escape analysis (CEA) could silently generate incorrect code. The fix disables stack allocation for blocks that were cloned or are clones, as these violate the single-assignment assumption used by escape analysis.

Key changes:

  • Adds tracking to distinguish original blocks from cloned blocks using m_initialMaxBlockID
  • Implements BlockIsCloneOrWasCloned() to identify problematic blocks
  • Updates MorphAllocObjNodeHelper() to prevent stack allocation at cloned sites

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/tests/JIT/opt/ObjectStackAllocation/Runtime_121736.csproj Test project for the regression case
src/tests/JIT/opt/ObjectStackAllocation/Runtime_121736.cs Regression test using LINQ operations that trigger the CEA cloning scenario
src/coreclr/jit/objectalloc.h Adds m_initialMaxBlockID field and BlockIsCloneOrWasCloned() method declaration
src/coreclr/jit/objectalloc.cpp Implements the fix: captures initial block ID, adds clone detection logic, and prevents stack allocation for cloned blocks

Copy link
Member

@JulieLeeMSFT JulieLeeMSFT left a comment

Choose a reason for hiding this comment

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

LGTM.

@AndyAyersMS
Copy link
Member Author

Failures look like #120577

@AndyAyersMS
Copy link
Member Author

There was a fix in #120736 but maybe this is a new case?

@AndyAyersMS
Copy link
Member Author

/ba-g seeing recurrence of a supposedly fixed error

@steveisok steveisok added the Servicing-consider Issue for next servicing release review label Nov 21, 2025
@rbhanda rbhanda added Servicing-approved Approved for servicing release and removed Servicing-consider Issue for next servicing release review labels Nov 22, 2025
@rbhanda rbhanda modified the milestones: 10.0.x, 10.0.2 Nov 22, 2025
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 Servicing-approved Approved for servicing release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants