Skip to content

JIT: Respect ordering side effects on the second operand when swapping - #135722

Merged
EgorBo merged 1 commit into
dotnet:mainfrom
EgorBo:fix-jit-134191
Oct 14, 2026
Merged

EgorBo merged 1 commit into
dotnet:mainfrom
EgorBo:fix-jit-134191

Conversation

@EgorBo

@EgorBo EgorBo commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

gtCanSwapOrder only rejected GTF_ORDER_SIDEEFF on the first operand. A divide that assertion prop proved non-faulting keeps only GTF_ORDER_SIDEEFF, so it could be commuted above the checked add that establishes its proof and throw the wrong exception. Treat ordering effects on the second operand like other non-reorderable effects.

Fixes #134191

A few diffs.

gtCanSwapOrder only rejected GTF_ORDER_SIDEEFF on the first operand. A divide that assertion prop proved non-faulting keeps only GTF_ORDER_SIDEEFF, so it could be commuted above the checked add that establishes its proof and throw the wrong exception. Treat ordering effects on the second operand like other non-reorderable effects.

Fixes dotnet#134191

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 88502efb-96e2-4483-b698-59f5003a2e2c
@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 Oct 13, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
11 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

The focused fix matches the ordering contract and includes a targeted regression test.

0 open findings

What changed in this PR

Fixes JIT operand reordering so ordering side effects on the second operand prevent unsafe swaps.

Changes:

  • Includes GTF_ORDER_SIDEEFF in swap safety checks.
  • Adds a regression test verifying OverflowException remains first.
File Description
src/​coreclr/​jit/​gentree.cpp Prevents unsafe operand reordering.
src/​tests/​JIT/​Regression_ro_2/​Runtime_134191.cs Covers the exception-ordering regression.

🧠 Review effort: Balanced

@EgorBo

EgorBo commented Oct 14, 2026

Copy link
Copy Markdown
Member Author

PTAL @dotnet/jit-contrib, trivial bug-fix

@EgorBo
EgorBo requested a review from a team October 14, 2026 02:04
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
private static int Test(int p, int q, int y, int z, int w, int u, int v)
{
return (checked(p + q) / y) + ((((z * w) + (u * v)) & 0x7FFFFF) / y);

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.

Why is assertion prop able to prove the / y non-faulting here?

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.

Is the logic basically that if we didn't fault on the first divide, then y != 0?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@adamperlin I think it's just bad wording in the PR description, here it's y is not-faulting.

X / Y generates an assertion that Y is never 0, but that assertion shouldn't reach divide's op1

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

@EgorBo
EgorBo enabled auto-merge (squash) October 14, 2026 21:52
@EgorBo
EgorBo merged commit 9a19078 into dotnet:main Oct 14, 2026
136 of 138 checks passed
@EgorBo
EgorBo deleted the fix-jit-134191 branch October 14, 2026 21:55
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 19, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 19, 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: (bug) Operand reordering ignores GTF_ORDER_SIDEEFF on the second operand, moving a "proven safe" divide above its proof

4 participants