Skip to content

JIT: Fix bug in GenTree::Equals - #128654

Merged
AndyAyersMS merged 2 commits into
dotnet:mainfrom
AndyAyersMS:Fix128631
May 28, 2026
Merged

AndyAyersMS merged 2 commits into
dotnet:mainfrom
AndyAyersMS:Fix128631

Conversation

@AndyAyersMS

Copy link
Copy Markdown
Member

Make sure we properly distinguish virtual stub calls to avoid incorrect head/tail merges.

Fixes #128631.

Make sure we properly distinguish virtual stub calls to avoid
incorrect head/tail merges.

Fixes dotnet#128631.

Co-authored-by: Copilot <[email protected]>
Copilot AI review requested due to automatic review settings May 27, 2026 16:41
@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 May 27, 2026
@AndyAyersMS

Copy link
Copy Markdown
Member Author

@EgorBo PTAL
fyi @dotnet/jit-contrib

@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

This PR fixes JIT call-node equality so virtual stub dispatch calls with different dispatch cells are not considered interchangeable, preventing incorrect head/tail merging for shared-generic interface dispatch.

Changes:

  • Compares gtStubCallStubAddr for virtual stub calls in GenTreeCall::Equals.
  • Adds a JIT regression test for distinct shared-generic interface dispatch sites.
  • Registers the new regression test in the JIT regression project.

Reviewed changes

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

File Description
src/coreclr/jit/gentree.cpp Adds virtual stub address comparison to call equality.
src/tests/JIT/Regression/JitBlue/Runtime_128631/Runtime_128631.cs Adds regression coverage for the dispatch-cell merge bug.
src/tests/JIT/Regression/Regression_ro_2.csproj Includes the new regression test in the read-only regression set.

Ensures the regression test reliably exercises the head/tail-merge shape even if a caller would otherwise inline `Dispatch` with constant tags.

Co-authored-by: Copilot <[email protected]>
@AndyAyersMS

Copy link
Copy Markdown
Member Author

Looking more broadly at GenTreeCall::Equals, it seems like we could merge two calls with different gtCastHelperILOffset values and so later in helper expansion make poor choices because we would be looking at the wrong profile data. But not a correctness issue.

@AndyAyersMS
AndyAyersMS requested a review from EgorBo May 27, 2026 17:10
Comment thread src/coreclr/jit/gentree.cpp Resolved
@AndyAyersMS

Copy link
Copy Markdown
Member Author

Looking more broadly at GenTreeCall::Equals, it seems like we could merge two calls with different gtCastHelperILOffset values and so later in helper expansion make poor choices because we would be looking at the wrong profile data. But not a correctness issue.

Opened #128659 for this.

@AndyAyersMS

Copy link
Copy Markdown
Member Author

No diffs locally (not too surprising as this has slipped past our testing).

@AndyAyersMS

Copy link
Copy Markdown
Member Author

No diffs in CI either.

@AndyAyersMS
AndyAyersMS merged commit dfd91b8 into dotnet:main May 28, 2026
136 of 145 checks passed
@AndyAyersMS

Copy link
Copy Markdown
Member Author

/backport to release/10.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/10.0 (link to workflow run)

@github-actions

Copy link
Copy Markdown
Contributor

@AndyAyersMS backporting to release/10.0 failed, the patch most likely resulted in conflicts. Please backport manually!

git am output
$ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch

Applying: JIT: Compare stub addresses in GenTreeCall::Equals
Using index info to reconstruct a base tree...
M	src/coreclr/jit/gentree.cpp
A	src/tests/JIT/Regression/Regression_ro_2.csproj
Falling back to patching base and 3-way merge...
Auto-merging src/coreclr/jit/gentree.cpp
CONFLICT (modify/delete): src/tests/JIT/Regression/Regression_ro_2.csproj deleted in HEAD and modified in JIT: Compare stub addresses in GenTreeCall::Equals.  Version JIT: Compare stub addresses in GenTreeCall::Equals of src/tests/JIT/Regression/Regression_ro_2.csproj left in tree.
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0001 JIT: Compare stub addresses in GenTreeCall::Equals
Error: The process '/usr/bin/git' failed with exit code 128

Link to workflow output

JulieLeeMSFT pushed a commit that referenced this pull request May 29, 2026
Make sure to properly distinguish virtual stub calls to avoid incorrect
head/tail merges.

Fixes #128631.

Backport of #128654 to release/10.0

## Customer Impact

- [x] Customer reported
- [ ] Found internally

A bug in the JIT's tail merge optimization can cause the wrong method to
be invoked.

The pattern required is a shared generic method invoked at two sibling
tail call sites on differently typed objects with otherwise identical
arguments. These calls are only distinguishable by the different virtual
stub addresses, and the JIT was not considering these when checking if
the calls were identical.

EG in a case like

```C#
        if (tag == "bytes")  return ((IFoo<byte[]>)this).Get();
        if (tag == "string") return ((IFoo<string>)this).Get();
```

The JIT can collapse the two calls into one and so invoke the wrong
method for one of the cases.

## Regression

- [x] Yes - introduced in .NET 8 by #77103.
- [ ] No

It is harder to reproduce in .NET 8 as the optimization was less general
back then.

## Testing

Verified fix with customer repro and locally crafted repro case.

## Risk

Low. Adds some extra checking to the method that tail merging uses to
determine if two calls are identical. Overall the JIT becomes (properly)
more cautions about this optimization. No diffs in SPMI.

Co-authored-by: Copilot <[email protected]>
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview6 milestone Jun 17, 2026
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
Make sure we properly distinguish virtual stub calls to avoid incorrect
head/tail merges.

Fixes #128631.

---------

Co-authored-by: Copilot <[email protected]>
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 18, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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.

JIT Tier1 incorrectly merges distinct shared-generic interface dispatch cells under Synthesized PG

4 participants