Skip to content

JIT: preserve negation of division by the signed minimum - #135459

Merged
EgorBo merged 1 commit into
dotnet:mainfrom
EgorBo:egorbo/fix-jit-133517
Oct 10, 2026
Merged

EgorBo merged 1 commit into
dotnet:mainfrom
EgorBo:egorbo/fix-jit-133517

Conversation

@EgorBo

@EgorBo EgorBo commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

Do not distribute negation into a signed division's constant divisor when that divisor is the minimum value for the operation's type. Its negation is not representable, so the rewrite changes the result from -1 to 1 when the dividend is also the signed minimum.

Fixes #133517

Almost no diffs

PR description draft:
<!-- -->
Do not distribute negation into a signed division's constant divisor when that divisor is the minimum value for the operation's type. Its negation is not representable, so the rewrite changes the result from -1 to 1 when the dividend is also the signed minimum.

Add a single-file regression covering int, long, and nint, including the minimum and surrounding boundary values.

Fixes dotnet#133517

Co-authored-by: Copilot <[email protected]>
Copilot-Session: ae8155d6-f240-4542-a1da-c721ba933c91
@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 8, 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 targeted guard correctly preserves division semantics, and the regression test covers all affected types.

0 open findings

What changed in this PR

Fixes incorrect JIT negation distribution when dividing by a signed minimum value.

Changes:

  • Prevents the unsafe rewrite for int, long, and nint minimum divisors.
  • Adds focused regression coverage across representative dividends.
File Description
src/​coreclr/​jit/​morph.cpp Excludes signed minimum divisors from negation distribution.
src/​tests/​JIT/​Regression_ro_2/​Runtime_133517.cs Verifies correct results for affected integral types.

🧠 Review effort: Balanced

@EgorBo

EgorBo commented Oct 9, 2026

Copy link
Copy Markdown
Member Author

PTAL @dotnet/jit-contrib

@EgorBo
EgorBo requested a review from a team October 9, 2026 14:49

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

z = -(x / MinValue)

Old morph:  NEG(DIV(x, MinValue))
                │
                ▼ (incorrectly distributes negation)
            DIV(x, -MinValue)   // -MinValue overflows, equals MinValue again!

New morph:  NEG(DIV(x, MinValue)) 

@EgorBo
EgorBo enabled auto-merge (squash) October 10, 2026 13:08
@EgorBo
EgorBo merged commit f5dec97 into dotnet:main Oct 10, 2026
149 of 151 checks passed
@EgorBo
EgorBo deleted the egorbo/fix-jit-133517 branch October 10, 2026 13:12
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 13, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 12, 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) negating division by the signed minimum gives the wrong result

3 participants