constant: correctly handle signedness in const_to_opt_u128.#558
Draft
constant: correctly handle signedness in const_to_opt_u128.#558
const_to_opt_u128.#558Conversation
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.
One of the more insidious bugs found via Rustlantis (using a sample from @FractalFir's Rust-GPU port of it).
const_to_opt_u128is used byrustc_codegen_ssato optimizeswitchInton the fly (i.e. replacingmatch CONST {...}with an unconditional branch to one of its cases), and that was working fine for unsigned discriminants, but we were wrong for signed ones, due to MIR storingswitchIntcases as zero-extended-to-u128(e.g.-1i8 => ...becomes255 => ...).More specifically, when
const_to_opt_u128(x, /*sign_ext*/ false)was called withxof a signed integer type, we were wrongly sign-extending (based on type) instead of zero-extending (based onsign_ext == false).TODO: still needs a test, perhaps something along the lines of:
(you can see the lack of a
switchin the LLVM IR: https://godbolt.org/z/hj6WEKj3v)(also, unrelatedly, that godbolt sample made me notice 1.94 -> 1.95-beta generates more unnecessary LLVM IR in some situations, and nightly still does, too)