Skip to content

Support retagging repr(simd) types. - #166168

Open
icmccorm wants to merge 1 commit into
rust-lang:mainfrom
BorrowSanitizer:retag-simd
Open

icmccorm wants to merge 1 commit into
rust-lang:mainfrom
BorrowSanitizer:retag-simd

Conversation

@icmccorm

@icmccorm icmccorm commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

This PR picks up where #159245 left off by adding support for retagging repr(simd) types. On the current nightly, the body of the function example is empty when -Zcodegen-emit-retag is enabled. With these changes, the parameter _val will receive a function-entry retag.

#![allow(internal_features)]
#![feature(repr_simd)]

#[repr(simd)]
struct HasSimdRepr<T>([T; 2]);

fn main() {
    let x = 0u8;
    let val = HasSimdRepr([&x, &x]);
    fn_entry_retag(val)
}

fn fn_entry_retag<'a>(_val: HasSimdRepr<&'a u8>) {
   // We should retag each reference within `_val`
}

We create a temporary allocation to hold the value. Then, we retag it indirectly, using __rust_retag_mem

; Create a temporary allocation to hold the vector
%1 = alloca [16 x i8], align 16
call void @llvm.lifetime.start.p0(ptr %1)
store <2 x ptr> %0, ptr %1, align 16

; Retag each field
call void @__rust_retag_mem(ptr %1, ...)
%2 = getelementptr inbounds i8, ptr %1, i64 8
call void @__rust_retag_mem(ptr %2, ...)

; Load the value, with its new provenance
%_val = load <2 x ptr>, ptr %1, align 16
call void @llvm.lifetime.end.p0(ptr %1)

Earlier in #163567, we switched to emitting retags in a loop. In this PR, I set a lower bound for emitting a loop at 2 retags. Otherwise, looping is just going to increase the amount of IR that we add in the typical case of a small array. This is somewhat arbitrary, so if there's a better choice, then I'm all for it.

This also fixes an issue with retagging enums. If a value has multiple variants and its discriminant is a constant, then we downcast to the corresponding variant instead of branching. We were using the discriminant's value as the variant index.

#[repr(u8)]
enum Ref<'a> {
    Shr(&'a u8) = 1,
    Mut(&'a mut u32) = 0 
}

If we have a constant value Ref::Mut, then we will end up retagging it as Ref::Shr instead, triggering a false positive frozen write in this program:

let mut x = 0;
let v = Ref::Mut(&mut x);
let m = v;
if let Ref::Mut(r) = m {
    *r = 1;
}

This PR correctly maps the discriminant to the corresponding variant index. This bug was pointed out to me by an LLM, but all fixes here are human-written :)

This passes BorrowSanitizer's test suite, and we've been using a version of Rust with these changes on our experimental branch for the past few weeks.

r? @saethlin

@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 Nov 18, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants