Skip to content

fix(mir-lower): refuse an 8-bit bswap whose result is not an 8-bit integer - #768

Merged
nihalpasham merged 3 commits into
NVIDIA:mainfrom
alejandro-soto-franco:fix/bswap8-bitcast-validation
Aug 10, 2026
Merged

nihalpasham merged 3 commits into
NVIDIA:mainfrom
alejandro-soto-franco:fix/bswap8-bitcast-validation

Conversation

@alejandro-soto-franco

@alejandro-soto-franco alejandro-soto-franco commented Aug 9, 2026 •

Copy link
Copy Markdown
Contributor

Mitigates #767 by failing closed in mir-lower; the importer root-cause fix in #769 carries the close.

An 8-bit bswap took an early bitcast path that checked nothing about the type it was casting to, so an aggregate result produced bitcast i8 7 to { double, i8, [7 x i8] } and the failure surfaced as an LLVM parse error against a generated .ll file rather than against the source.

Summary

Checks the result width before the bitcast, so the case reports the same invalid input program error every other arm of the intrinsic already reports.

Changes

  • crates/mir-lower/src/convert/ops/call.rs: convert_rust_bit_intrinsic reads the result width through integer_bit_width before constructing the BitcastOp, and refuses a width that does not match the operand.
  • crates/mir-lower/tests/lowering_test.rs: a lowering test building a mir.call to the bswap placeholder with a ui8 operand and a mir.tuple result.

Rationale

Rust's bswap on a single byte is identity and LLVM has no useful intrinsic for it, so that width returns early:

if matches!(intrinsic, RustBitIntrinsic::Bswap) && value_width == 8 {
    let bitcast = llvm::BitcastOp::new(ctx, value, result_type);
    ...
    return Ok(());
}

Every other arm falls through to cast_integer_value_to_type, which asks integer_bit_width for the target width and refuses a type that has none. The early return skips that check, and bitcast requires 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 in mir-importer and touches the projected-assignment path emit_function_call does 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:

before:  llc: rustlantis_smoke.ll:342:17: error: invalid cast opcode for cast from 'i8'
              to '{ double, i8, [7 x i8] }'
after:   error: [rustc_codegen_cuda] Device codegen failed: ... ["src/generated_case.rs": line: 27,
              column: 13] Compilation error: invalid input program.
              expected integer type for Rust bit intrinsic

An 8-bit bswap writing to a plain u8 local still compiles and runs, CPU and GPU traces matching, so the valid case that reaches this path is unaffected.

cargo test -p mir-lower                    278 passed, 7 ignored, 0 failed
cargo clippy -p mir-lower --all-targets    clean
cargo fmt --check -p mir-lower             clean

…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.
@nihalpasham nihalpasham added bug Something isn't working IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) labels Aug 10, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

Validated on 2x RTX 5090:

  • workspace suite green (3411 passed), and
  • a u8::swap_bytes kernel lowers to the identity bitcast and matches the CPU reference on device.

Pushed two test fixups:

  • the refusal test now asserts the guard's actual diagnostic,
  • a positive test covers the valid u8 path, and
  • both were mutation-checked (removing the guard fails one, inverting it fails the other).

Landing together with #769, which carries the #767 close.

@nihalpasham
nihalpasham merged commit 8c76725 into NVIDIA:main Aug 10, 2026
36 checks passed
@alejandro-soto-franco
alejandro-soto-franco deleted the fix/bswap8-bitcast-validation branch September 9, 2026 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working IR-lowering Lowering between dialects (dialect-mir to LLVM dialect)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants