Fix lossless_negate for casts from unsigned inner types#9166
Open
alexreinking wants to merge 2 commits into
Open
Fix lossless_negate for casts from unsigned inner types#9166alexreinking wants to merge 2 commits into
alexreinking wants to merge 2 commits into
Conversation
lossless_negate tried to negate cast(outer, inner) by negating inner and casting back, regardless of inner's signedness. This is unsound when inner is unsigned: unsigned negation wraps modularly, so cast(outer, -uint8(x)) != -cast(outer, uint8(x)) in general. For example, -uint8(65) = uint8(191) (wrapping), but -int16(65) = -65, so int16(uint8(-x)) gives 191 while the correct -int16(uint8(x)) = -65. This caused FindIntrinsics to produce incorrect vectorized code: when lowering Sub(int16(A), int16(B_uint8)) it would compute the negation of int16(B_uint8) by negating inside uint8 arithmetic, turning a subtraction into an incorrect addition. The fix is to guard the Cast case: only attempt to push negation through a cast when the inner type is not unsigned. Signed integers are safe (overflow is checked via lossless_cast) and floats are safe (negation is an exact sign-bit flip with no wrapping). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
d8c208b to
a1a9a2b
Compare
Member
|
Signed integer negation also wraps though, for the most-negative int. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #9166 +/- ##
=======================================
Coverage ? 69.38%
=======================================
Files ? 254
Lines ? 78217
Branches ? 18713
=======================================
Hits ? 54268
Misses ? 18474
Partials ? 5475 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
lossless_negate tried to negate cast(outer, inner) by negating inner and casting back, regardless of inner's signedness. This is unsound when inner is unsigned: unsigned negation wraps modularly, so cast(outer, -uint8(x)) != -cast(outer, uint8(x)) in general.
For example, -uint8(65) = uint8(191) (wrapping), but -int16(65) = -65, so int16(uint8(-x)) gives 191 while the correct -int16(uint8(x)) = -65.
This caused FindIntrinsics to produce incorrect vectorized code: when lowering Sub(int16(A), int16(B_uint8)) it would compute the negation of int16(B_uint8) by negating inside uint8 arithmetic, turning a subtraction into an incorrect addition.
The fix is to guard the Cast case: only attempt to push negation through a cast when the inner value has a signed integer type.
Fixes #9165
Breaking changes
None
Checklist