Avoid a bitcast FFI call in transmuting by scottmcm · Pull Request #142768 · rust-lang/rust

@scottmcm

For things that only change the valid ranges, we can just skip the `LLVMBuildBitCast` call.

I tried to tweak this a bit more and broke stuff, so I also added some extra tests for that as we apparently didn't have coverage.

@rustbot rustbot added S-waiting-on-review

Status: Awaiting review from the assignee but also interested parties.

T-compiler

Relevant to the compiler team, which will review and decide on the PR/issue.

labels

Jun 20, 2025

WaffleLapkin

@bors bors added S-waiting-on-bors

Status: Waiting on bors to run and complete tests. Bors will change the label on completion.

and removed S-waiting-on-review

Status: Awaiting review from the assignee but also interested parties.

labels

Jun 24, 2025

workingjubilee added a commit to workingjubilee/rustc that referenced this pull request

Jun 25, 2025
…=WaffleLapkin

Avoid a bitcast FFI call in transmuting

For things that only change the valid ranges, we can just return the input, rather than making the `LLVMBuildBitCast` call and having *it* then do nothing.

I tried to tweak this a bit more and broke stuff, so I also added some extra tests for that as we apparently didn't have coverage.

This was referenced

Jun 25, 2025

bors added a commit that referenced this pull request

Jun 25, 2025
Rollup of 15 pull requests

Successful merges:

 - #135731 (Implement parsing of pinned borrows)
 - #138780 (Add `#[loop_match]` for improved DFA codegen)
 - #142453 (Windows: make `read_dir` stop iterating after the first error is encountered)
 - #142633 (Error on invalid signatures for interrupt ABIs)
 - #142768 (Avoid a bitcast FFI call in transmuting)
 - #142825 (Port `#[track_caller]` to the new attribute system)
 - #142844 (Enable short-ice for Windows)
 - #142934 (Tweak `-Zmacro-stats` measurement.)
 - #142955 (Couple of test suite fixes for cg_clif)
 - #142977 (rustdoc: Don't mark `#[target_feature]` functions as ⚠)
 - #142980 (Reduce mismatched-lifetime-syntaxes suggestions to MaybeIncorrect)
 - #142982 (Corrected spelling mistake in c_str.rs)
 - #142983 (Taint body on invalid call ABI)
 - #142988 (Update wasm-component-ld to 0.5.14)
 - #142993 (Update cargo)

r? `@ghost`
`@rustbot` modify labels: rollup

rust-timer added a commit that referenced this pull request

Jun 25, 2025
Rollup merge of #142768 - scottmcm:avoid-unneeded-bitcast, r=WaffleLapkin

Avoid a bitcast FFI call in transmuting

For things that only change the valid ranges, we can just return the input, rather than making the `LLVMBuildBitCast` call and having *it* then do nothing.

I tried to tweak this a bit more and broke stuff, so I also added some extra tests for that as we apparently didn't have coverage.

@scottmcm scottmcm deleted the avoid-unneeded-bitcast branch

June 26, 2025 01:04

flip1995 pushed a commit to flip1995/rust-clippy that referenced this pull request

Jun 26, 2025

@bors

folkertdev

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request

Jun 29, 2025

jhpratt added a commit to jhpratt/rust that referenced this pull request

Jul 1, 2025

jhpratt added a commit to jhpratt/rust that referenced this pull request

Jul 2, 2025

jhpratt added a commit to jhpratt/rust that referenced this pull request

Jul 2, 2025

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request

Jul 2, 2025

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request

Jul 2, 2025

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request

Jul 2, 2025

rust-timer added a commit that referenced this pull request

Jul 2, 2025
Rollup merge of #143194 - folkertdev:fix-single-element-simd-bitcast, r=workingjubilee

fix bitcast of single-element SIMD vectors

in effect this reverts #142768 and adds additional tests. That PR relaxed the conditions on an early return in an incorrect way that would create broken LLVM IR.

https://godbolt.org/z/PaaGWTv5a

```rust
#![feature(repr_simd)]

#[repr(simd)]
#[derive(Clone, Copy)]
struct S([i64; 1]);

#[no_mangle]
pub extern "C" fn single_element_simd(b: S) -> i64 {
    unsafe { std::mem::transmute(b) }
}
```
at the time of writing generates this LLVM IR, where the type of the return is different from the function's return type.

```llvm
define noundef i64 ``````@single_element_simd(<1`````` x i64> %b) unnamed_addr {
start:
  ret <1 x i64> %b
}
```

The test output is actually the same for the existing tests, showing that the change didn't actually matter for any tested behavior. It is probably a bit faster to do the early return, but, well, it's incorrect in general.

zullip thread: [#t-compiler > Is transmuting a &#96;T&#96; to &#96;Tx1&#96; (one-element SIMD vector) UB?](https://rust-lang.zulipchat.com/#narrow/channel/131828-t-compiler/topic/Is.20transmuting.20a.20.60T.60.20to.20.60Tx1.60.20.28one-element.20SIMD.20vector.29.20UB.3F/with/526262799)
cc ``````@sayantn``````
r? ``````@scottmcm``````

tautschnig pushed a commit to model-checking/verify-rust-std that referenced this pull request

Jul 3, 2025

christian-schilling pushed a commit to christian-schilling/rustc_codegen_cranelift that referenced this pull request

Jan 27, 2026

christian-schilling pushed a commit to christian-schilling/rustc_codegen_cranelift that referenced this pull request

Jan 27, 2026