JIT: Use faster mod for uint16 values#128509
Draft
MihaZupan wants to merge 2 commits into
Draft
Conversation
Contributor
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
Member
Author
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates CoreCLR JIT morphing/assertion propagation/lowering to enable a cheaper remainder sequence when both operands are proven to fit in uint16 and the divisor is a non-zero constant, and adds a JIT regression test that exercises relevant % const patterns (including char-based modulo).
Changes:
- Teach morphing to avoid rewriting
% constintoa - (a / b) * bwhen lowering can apply a cheaperuint16FastMod-style sequence (and convertMOD→UMODwhen safe). - Add a new
GTF_UMOD_UINT16_OPERANDShint set by assertion propagation and consumed by lowering to trigger the specialized expansion. - Add a new JIT test under
src/tests/JIT/opt/Divide/Regressions/to cover representative modulo patterns.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/coreclr/jit/morph.cpp | Skips early MOD-to-SUB/MUL/DIV morphing (and may flip MOD→UMOD) so lowering can apply the cheaper uint16 modulo path. |
| src/coreclr/jit/lower.cpp | Implements the uint16-specialized FastMod lowering for GT_UMOD by constant divisors. |
| src/coreclr/jit/assertionprop.cpp | Improves IntegralRange reasoning and sets GTF_UMOD_UINT16_OPERANDS when uint16-range operands are proven. |
| src/coreclr/jit/gentree.h | Introduces the new GTF_UMOD_UINT16_OPERANDS flag. |
| src/coreclr/jit/gentree.cpp | Ensures tree comparison accounts for the new mod/div-related flag. |
| src/tests/JIT/opt/Divide/Regressions/Regression4_Divide.csproj | Adds the new regression test project. |
| src/tests/JIT/opt/Divide/Regressions/Regression4_Divide.cs | Adds the new regression test cases exercising modulo scenarios. |
Comment on lines
+8185
to
+8193
| // uint multiplier = uint.MaxValue / divisor + 1; | ||
| // ulong result = ((ulong)(dividend * multiplier) * divisor) >> 32; | ||
| // return (int)result; | ||
| // | ||
| // The dividend is first truncated to TYP_INT (safe because we know it fits in | ||
| // uint16), and the final value (always in [0, divisor)) is widened back to the | ||
| // mod's result type. The dividend's range may have been recovered either | ||
| // statically by IntegralRange::ForNode at this point, or earlier by assertion | ||
| // propagation (which leaves GTF_UMOD_UINT16_OPERANDS for us). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Another attempt at #111535
Closes #111492
Change the transformation for
Let's see what CI thinks.
Diffs: https://gist.github.com/MihuBot/38b0b6eeabd80de528a6e1967a7df7cb