Skip to content

Commit

Permalink
Unrolled build for #127399
Browse files Browse the repository at this point in the history
Rollup merge of #127399 - cjgillot:issue-127396, r=oli-obk

Verify that allocations output by GVN are sufficiently aligned.

Fixes #127396

r? `@oli-obk`
  • Loading branch information
rust-timer authored Jul 8, 2024
2 parents cfd7cf5 + 12edc8d commit 5b9176f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
14 changes: 9 additions & 5 deletions compiler/rustc_mir_transform/src/gvn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1391,11 +1391,15 @@ fn op_to_prop_const<'tcx>(
let (prov, offset) = pointer.into_parts();
let alloc_id = prov.alloc_id();
intern_const_alloc_for_constprop(ecx, alloc_id).ok()?;
if matches!(ecx.tcx.global_alloc(alloc_id), GlobalAlloc::Memory(_)) {
// `alloc_id` may point to a static. Codegen will choke on an `Indirect` with anything
// by `GlobalAlloc::Memory`, so do fall through to copying if needed.
// FIXME: find a way to treat this more uniformly
// (probably by fixing codegen)

// `alloc_id` may point to a static. Codegen will choke on an `Indirect` with anything
// by `GlobalAlloc::Memory`, so do fall through to copying if needed.
// FIXME: find a way to treat this more uniformly (probably by fixing codegen)
if let GlobalAlloc::Memory(alloc) = ecx.tcx.global_alloc(alloc_id)
// Transmuting a constant is just an offset in the allocation. If the alignment of the
// allocation is not enough, fallback to copying into a properly aligned value.
&& alloc.inner().align >= op.layout.align.abi
{
return Some(ConstValue::Indirect { alloc_id, offset });
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/mir/alignment/misaligned-constant-gvn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//@ build-pass
//@ compile-flags: -Zmir-opt-level=0 -Zmir-enable-passes=+GVN

fn main() {
let variant: Option<u32> = None;
let transmuted: u64 = unsafe { std::mem::transmute(variant) };
println!("{transmuted}");
}

0 comments on commit 5b9176f

Please sign in to comment.