Emit range assume after IntToInt cast in MatchBranchSimplification#155206
Closed
blackms wants to merge 1 commit intorust-lang:mainfrom
Closed
Emit range assume after IntToInt cast in MatchBranchSimplification#155206blackms wants to merge 1 commit intorust-lang:mainfrom
blackms wants to merge 1 commit intorust-lang:mainfrom
Conversation
Collaborator
|
Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
saethlin
reviewed
Apr 12, 2026
| Foo::B(B::B0) => 3, | ||
| Foo::B(B::B1) => 4, | ||
| }; | ||
| // CHECK-NOT: panic_bounds_check |
Member
There was a problem hiding this comment.
It's good to have a negative check but without a positive test as well in here (checking for the IR sequence that should appear) this test could incorrectly pass if the function is renamed or if it's optimized to some other kind of panicking.
You should be able to draw inspiration from other codegen tests. Not having a positive check is a common flaw in the existing test suite, but it's not universal :)
When MatchBranchSimplification replaces multiple constant assignments with a single IntToInt cast, the range information that LLVM could previously infer from the phi node constants is lost. This causes unnecessary bounds checks to remain in the generated code. Fix this by emitting an assume(result <= max_value) after the cast, where max_value is the maximum of the original constant values. This allows LLVM to eliminate bounds checks that depend on the cast result.
0c81f4a to
bcbe68f
Compare
Author
|
Thanks for the review @saethlin! Addressed in the latest push (force-pushed to drop the
@rustbot ready |
Member
|
r? compiler |
Member
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Fixes #149480
PR #127324 introduced
unify_by_int_to_intinMatchBranchSimplification, which replaces individual constant assignments with a singleIntToIntcast. This is a valid optimization, but it loses range information that LLVM previously inferred from phi node constants — causing unnecessary bounds checks to remain.The fix emits
assume(result <= max_value)after the IntToInt cast, wheremax_valueis the maximum of the original constant values being replaced. This allows LLVM to prove the index is in-bounds and eliminate the bounds check.Before (with bounds check):
After (bounds check eliminated):
r? mir-opt