Skip to content

Follow DivMod flags instead of re-deriving the exception set - #137026

Open
NinoFloris wants to merge 5 commits into
dotnet:mainfrom
NinoFloris:jit/division-exception-set
Open

NinoFloris wants to merge 5 commits into
dotnet:mainfrom
NinoFloris:jit/division-exception-set

Conversation

@NinoFloris

@NinoFloris NinoFloris commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

This is a cleanup PR. Zero diffs on the windows-x64 and osx-arm64 libraries pmi collections. The diffs elsewhere are divisions that assertion prop had pinned with GTF_ORDER_SIDEEFF for operand facts as well, which don't need to be pinned.

I stumbled over this quirky behavior in #136994. The exception set of a signed division is currently re-derived from its operands on every read. This is unlike every other oper, whose set is a property of the node. This PR just aligns the behavior with that.

I'm not the first to run into this, that was in #97201 previously, which only added a workaround. The recent change to pin the node in dominated cases in #129533 is still respected.

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

@NinoFloris
NinoFloris marked this pull request as ready for review November 5, 2026 16:12
Every other oper's exception set is a property of the node. A signed division's overflow was derived from the never-negative status of its operands, read from the local descriptor, the integral range and the value number store, so the set moved whenever a later phase learned something about an operand while GTF_EXCEPT stayed what morph computed.

OperExceptions consults only the GTF_DIV_MOD_NO flags and constant operands. Whoever proves an operand safe records it on the node with SetDivModExceptionFlags: morph on every target, assertion propagation, and the stack level setter before codegen. Zero diffs on the osx-arm64 libraries pmi collection.
A flag set from a property of the operand's value holds wherever the node ends up and needs no GTF_ORDER_SIDEEFF. optAssertionProp_ModDiv records those through SetDivModExceptionFlags first and pins the node only for what its assertions add under a dominating check.
Keep the constant dividend proof when the operand becomes a temp. A nonnegative dividend also rules out signed overflow, so record that fact before assertion propagation considers pinning the operation. Check assertion-dependent overflow before converting a signed operation to unsigned.
@NinoFloris
NinoFloris force-pushed the jit/division-exception-set branch from b898ec5 to 48e8111 Compare November 23, 2026 10:28
Convert DIV and MOD when both operands are nonnegative. Only the operations that stay signed need the assertion-dependent overflow pin. Keep the separate pin for a divisor proven nonzero by a dominating check.
Rewrite (x + -y) + c as (x + c) - y during global morph. This lets lowering fold the offset into an address without a separate negation or another morph pass. Preserve operand evaluation order when the negated addend comes first.
Comment thread src/coreclr/jit/morph.cpp
}
}

// Reassociation can expose (x + -y) + c after the inner add has been morphed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Assertion prop used to pin the DIV or MOD and mark its statement as changed. That caused the JIT to re-morph the statement, which happened to fold this pattern

This branch has not been deployed

No deployments
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.

1 participant