Repository navigation
Share modular range arithmetic across comparison optimizations - #136926
Open
NinoFloris wants to merge 2 commits into
Open
NinoFloris wants to merge 2 commits into
NinoFloris wants to merge 2 commits into
Conversation
Move the modular interval arithmetic of redundant branch opts into modularrange.h, a ring interval that models constant comparisons of either signedness, with symbolic upper bounds by identity. The redundant branch optimizer is converted to construct and intersect these intervals; it produces no code differences.
|
Azure Pipelines: Successfully started running 6 pipeline(s). 9 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
NinoFloris
force-pushed
the
jit/modular-range-conversion
branch
from
November 3, 2026 20:45
5767fef to
283ec3e
Compare
Replace GetIntersection, FoldRangeTests and FoldNeverNegativeRangeTest with a single FoldComparisons that intersects the two tests as modular intervals and emits the comparison form that matches the result. The conversion keeps the previously accepted shapes only: two tests of the same signedness on a plain local that enclose a range of at least two non-negative values, or "X >= 0" paired with a never-negative symbolic bound. It produces no code differences. The shapes the interval representation makes available, equality tests, negative bounds, offset bases and single values, are enabled separately.
NinoFloris
force-pushed
the
jit/modular-range-conversion
branch
from
November 3, 2026 20:54
283ec3e to
2c781a5
Compare
NinoFloris
marked this pull request as ready for review
November 4, 2026 00:48
This was referenced Nov 4, 2026
Closed
NinoFloris
marked this pull request as draft
November 4, 2026 15:18
NinoFloris
marked this pull request as ready for review
November 4, 2026 15:18
This branch has not been deployed
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.

Adds
ModularRange, an interval on the ring of integers modulo 2^n. It replaces the existing range arithmetic in RBO, and optimize bools. A signed comparison is the unsigned comparison of the value with the sign bit flipped. Either produces the same kind of interval. A comparison pair becomes one intersection instead of per-signedness case analysis, and wraparound is a property of the interval instead of a guard.For example
x >= 10 && x <= 100is[10, 100],x != 5is[6, 4]wrapping through zero, and intersecting[6, 4]with[7, 5]gives[7, 4], the values other than 5 and 6.The representation follows the replaced code. It works in
target_size_t, so the 32-bit JIT does no 64-bit arithmetic for it.It is intended as a code cleanup and should produce no diffs. Each consumer keeps the shapes it accepted before:
x >= 0paired with a never-negative bound.Prospective
The expanded functionality for optimize bools (~30 lines of gates removed and a few lines added) can then be handled in a follow-up to enable:
x != a && x != a + 1andx > a && x == a + 1lo <= x <= hiwith negativelox > 10 && x > 100 to x > 100x >= 10 && x <= 10tox == 10, and constant results,x >= 20 && x < 10to falsex + candx - crather than a plain localx >= 0, such asx > -1 && x < lengthorx < 0 && x < lengthThe follow-up also resolves the TODO whose code this PR replaces:
runtime/src/coreclr/jit/optimizebools.cpp
Lines 508 to 515 in 8586c68
I ran into these shortcomings trying to unblock #119689.
This is the generalized form of what the experiment in #136739 needed.
Notes
Range check is not converted here. Its constant arithmetic almost fits, but two of its contracts complicate it: checked arithmetic reads a known result as proof that the operation cannot overflow, so a wrapped sum must stay unknown, and
RangeOpskeeps a known limit when only the other one overflows. A ring interval cannot express either, and adding those checks basically comes down to the current code.More sharing may be possible, but I don't expect these to be zero-diff changes: