Repository navigation
fix(mir-lower): refuse an 8-bit bswap whose result is not an 8-bit integer - #768
Merged
nihalpasham merged 3 commits intoAug 10, 2026
Merged
nihalpasham merged 3 commits into
nihalpasham merged 3 commits into
Conversation
…teger
Rust's bswap on a single byte is identity and LLVM has no useful intrinsic
for it, so that case takes an early path and emits a bitcast. The path
returned before reaching cast_integer_value_to_type, which every other arm
of the intrinsic goes through, so nothing checked the result type it was
casting to.
A tuple result therefore produced
bitcast i8 7 to { double, i8, [7 x i8] }
which LLVM rejects when the module is read back, so the failure surfaced as
an llc diagnostic against a generated .ll file rather than as an error
against the source.
bitcast requires both types to be non-aggregate and of one size. Checking
the result width first reports the same invalid input program error the
other arms already report, at the source location.
This does not address where the wrong type comes from. The importer types
an intrinsic call from its destination local rather than from the projected
place it writes, so `RET.1 = bswap(x)` on a `(f64, u8)` return arrives here
carrying the whole tuple.
…alid u8 path The aggregate-result test asserted only that lowering returned an error, so it could pass via any unrelated lowering failure. It now uses the file's expect_err + contains style and checks for the guard's actual diagnostic, 'expected integer type for Rust bit intrinsic'. The doc comment described the importer's destination-local typing in the present tense; that producer bug is fixed on the importer side, so the comment now frames the test as guarding against any malformed producer. A positive counterpart lowers an 8-bit bswap with a u8 result and checks it becomes an identity bitcast rather than an intrinsic call, so the new guard cannot silently over-reject the case Rust actually produces for u8::swap_bytes. Both tests share one module builder.
…ter landing order The comment said the importer's destination-local typing bug 'is fixed on the importer side', but that fix is a separate open PR. Reword it to name the importer bug as the known producer without asserting its status, so the comment reads correctly whether the importer fix lands before, after, or never.
Collaborator
|
Validated on 2x RTX 5090:
Pushed two test fixups:
|
nihalpasham
approved these changes
Aug 10, 2026
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.
Mitigates #767 by failing closed in mir-lower; the importer root-cause fix in #769 carries the close.
An 8-bit
bswaptook an early bitcast path that checked nothing about the type it was casting to, so an aggregate result producedbitcast i8 7 to { double, i8, [7 x i8] }and the failure surfaced as an LLVM parse error against a generated.llfile rather than against the source.Summary
Checks the result width before the bitcast, so the case reports the same
invalid input programerror every other arm of the intrinsic already reports.Changes
crates/mir-lower/src/convert/ops/call.rs:convert_rust_bit_intrinsicreads the result width throughinteger_bit_widthbefore constructing theBitcastOp, and refuses a width that does not match the operand.crates/mir-lower/tests/lowering_test.rs: a lowering test building amir.callto thebswapplaceholder with aui8operand and amir.tupleresult.Rationale
Rust's
bswapon a single byte is identity and LLVM has no useful intrinsic for it, so that width returns early:Every other arm falls through to
cast_integer_value_to_type, which asksinteger_bit_widthfor the target width and refuses a type that has none. The early return skips that check, andbitcastrequires both types to be non-aggregate and of one size, so the arm could emit IR that cannot be parsed. Reading the width first restores the property that the other arms already hold: a bad result type is reported against the source rather than handed to LLVM.This does not address where the aggregate type comes from. The importer types an intrinsic call from its destination local rather than the projected place it writes, so
RET.1 = bswap(x)on a(f64, u8)return arrives here carrying the tuple, as #767 sets out. That fix belongs inmir-importerand touches the projected-assignment pathemit_function_calldoes not share, so it is left separate.The test asserts the lowering returns an error. Removing the four added lines makes it fail, and the other 100 tests in the file pass either way.
Validation
RTX 5060 Laptop GPU, compute capability 12.0, CUDA 13.3, driver 610.43.03.
The reduced case from #767, before and after:
An 8-bit
bswapwriting to a plainu8local still compiles and runs, CPU and GPU traces matching, so the valid case that reaches this path is unaffected.