Skip to content

[ValueTracking] Check round-trip cast in lookThroughCast - #9118

Open
Steven Perron (s-perron) wants to merge 1 commit into
microsoft:mainfrom
s-perron:fix-valuetracking-zext-trunc
Open

Steven Perron (s-perron) wants to merge 1 commit into
microsoft:mainfrom
s-perron:fix-valuetracking-zext-trunc

Conversation

@s-perron

Copy link
Copy Markdown
Collaborator

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.

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 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;

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

None yet

Projects

Status: New

Development

Successfully merging this pull request may close these issues.

2 participants