Repository navigation
[ValueTracking] Check round-trip cast in lookThroughCast - #9118
Open
Steven Perron (s-perron) wants to merge 1 commit into
Open
Steven Perron (s-perron) wants to merge 1 commit into
Steven Perron (s-perron) wants to merge 1 commit into
Conversation
lookThroughCast truncated the constant arm of a select without verifying that casting back reproduces the original constant, causing InstCombine to narrow selects whose constants do not fit in the smaller integer type. Route ZExtInst, SExtInst, and TruncInst through a round-trip cast check to ensure no bits are lost. Backports llvm/llvm-project@d2a074b1f463 and llvm/llvm-project@826e9831a7f6.
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The user-visible optimizer miscompile fix is missing the required Bug Fixes entry in docs/ReleaseNotes.md.
1 open issue
What changed in this PR
Backports LLVM fixes to prevent InstCombine from rewriting cast-based selects when the constant cannot be reconstructed exactly.
Changes:
- Adds round-trip validation for zero-extension, sign-extension, and truncation in
lookThroughCast. - Adds regression tests for an out-of-range constant and a vector containing an undefined lane.
| File | Description |
|---|---|
tools/clang/test/DXC/Passes/InstructionCombining/select-zext-minmax.ll |
Tests rejection of select rewrites that fail round-trip validation. |
lib/Analysis/ValueTracking.cpp |
Requires exact constant reconstruction before recognizing cast-based select patterns. |
Review effort: Balanced
Comment on lines
+3432
to
+3434
| // Make sure the cast doesn't lose any information. | ||
| if (CastedBack != C) | ||
| return nullptr; |
Steven Perron (s-perron)
requested review from
Chris B (llvm-beanz) and
Natalie Chouinard (sudonatalie)
November 13, 2026 18:36
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.

lookThroughCast truncated the constant arm of a select without
verifying that casting back reproduces the original constant, causing
InstCombine to narrow selects whose constants do not fit in the smaller
integer type. Route ZExtInst, SExtInst, and TruncInst through a round-trip
cast check to ensure no bits are lost.
Backports llvm/llvm-project@d2a074b1f463 and
llvm/llvm-project@826e9831a7f6.