Skip to content

Preserve exception order when head merging ordered statements - #137254

Merged
AndyAyersMS merged 1 commit into
dotnet:mainfrom
NinoFloris:jit/head-merge-exception-order
Nov 12, 2026
Merged

AndyAyersMS merged 1 commit into
dotnet:mainfrom
NinoFloris:jit/head-merge-exception-order

Conversation

@NinoFloris

Copy link
Copy Markdown
Contributor

Treat exceptions in the predecessor terminator as interference with an ordering-sensitive head. Invariant method-table loads can establish a null proof without GTF_GLOB_REF; moving the protected load ahead of the terminator can change which exception is thrown.

Fixes #137253

Treat exceptions in the predecessor terminator as interference with an ordering-sensitive head. Invariant method-table loads can establish a null proof without GTF_GLOB_REF; moving the protected load ahead of the terminator can change which exception is thrown.

Cover a division preceding a type test whose successors both read a field from the receiver. With a zero divisor and null receiver, the division must throw first.
@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 10, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 10, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
10 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

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

🟢 Approval recommended

No active review issues are recorded for the change to preserve exception ordering during JIT head merging.

No open issues 🎉

What changed in this PR

This PR addresses #137253 by preventing JIT head merging from moving ordering-sensitive statements ahead of a predecessor condition that can throw, preserving exception order.

Changes:

  • Include GTF_EXCEPT in the interference check for ordering-sensitive heads.
  • Add regression tests covering type checks, branch markers, and division-before-null exception ordering.
File Description
src/​tests/​JIT/​opt/​HeadTailMerge/​headmergeexception.cs Adds regression coverage for protected field reads and exception ordering.
src/​coreclr/​jit/​fgopt.cpp Blocks head movement across potentially throwing terminators.

Review effort: Balanced

@AndyAyersMS AndyAyersMS 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

@AndyAyersMS

Copy link
Copy Markdown
Member

/ba-g unrelated build failure

@AndyAyersMS
AndyAyersMS merged commit c4cecc4 into dotnet:main Nov 12, 2026
140 of 145 checks passed
@AndyAyersMS

Copy link
Copy Markdown
Member

Thanks for spotting and fixing this!

@NinoFloris
NinoFloris deleted the jit/head-merge-exception-order branch November 12, 2026 20:53
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 13, 2026
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 community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JIT: Head merging can reorder a null dereference ahead of a division and throw the wrong exception

3 participants