Skip to content

JIT: don't distribute negation over DIV by the type minimum - #133529

Open
EgorBo wants to merge 1 commit into
dotnet:mainfrom
EgorBo:fix-neg-div-min
Open

JIT: don't distribute negation over DIV by the type minimum#133529
EgorBo wants to merge 1 commit into
dotnet:mainfrom
EgorBo:fix-neg-div-min

Conversation

@EgorBo

@EgorBo EgorBo commented Sep 9, 2026

Copy link
Copy Markdown
Member

Fixes #133517

Morph's NEG(DIV(a, C)) => DIV(a, -C) is only valid when -C is exactly representable. For C == int.MinValue/long.MinValue the negation wraps back to C, so the NEG was silently dropped.

No SPMI asm diffs.

NEG(DIV(a, C)) => DIV(a, -C) is only valid when -C is exactly representable.
For C == INT_MIN/INT64_MIN the negation wraps back to C, silently dropping it.

Fixes dotnet#133517

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 3b9596b9-f3b3-47d1-b1ca-1a8781e56de7
Copilot AI lite review requested due to automatic review settings September 9, 2026 20:04
@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 Sep 9, 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.

🟡 Changes recommended

The new MIN check misses TYP_I_IMPL on 32-bit targets, so the transformation still miscompiles -(x / nint.MinValue) and can also hit undefined signed-overflow behavior.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR updates CoreCLR JIT morphing to avoid distributing NEG over DIV when the divisor’s negation is not exactly representable (the signed type minimum), and adds a JIT regression test to cover int, long, and nint cases.

Changes:

  • Update fgMorphSmpOp’s NEG(DIV(a, C)) => DIV(a, -C) transform to exclude C == MIN in addition to C == ±1.
  • Add a new JitBlue regression test covering int/long/nint and a few nearby invariants.
  • Register the new test source in Regression_ro_2.csproj.
File summaries
File Description
src/coreclr/jit/morph.cpp Extends the safety check for distributing negation over division constants.
src/tests/JIT/Regression/JitBlue/Runtime_133517/Runtime_133517.cs Adds coverage for the incorrect MIN-divisor negation-distribution behavior across int/long/nint.
src/tests/JIT/Regression/Regression_ro_2.csproj Includes the new regression test in the build.
Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 1
  • Review effort level: Lite

Comment thread src/coreclr/jit/morph.cpp
Comment on lines +8099 to +8106
int64_t constVal = op1op2->AsIntCon()->IconValue();
int64_t minVal = INT64_MIN;

if (mulOrDiv->TypeIs(TYP_INT))
{
constVal = static_cast<int32_t>(constVal);
minVal = INT32_MIN;
}
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JIT: negating division by the signed minimum gives the wrong result

2 participants