Skip to content

Commit 5b9176f

Browse files
authored
Unrolled build for rust-lang#127399
Rollup merge of rust-lang#127399 - cjgillot:issue-127396, r=oli-obk Verify that allocations output by GVN are sufficiently aligned. Fixes rust-lang#127396 r? `@oli-obk`
2 parents cfd7cf5 + 12edc8d commit 5b9176f

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

compiler/rustc_mir_transform/src/gvn.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1391,11 +1391,15 @@ fn op_to_prop_const<'tcx>(
13911391
let (prov, offset) = pointer.into_parts();
13921392
let alloc_id = prov.alloc_id();
13931393
intern_const_alloc_for_constprop(ecx, alloc_id).ok()?;
1394-
if matches!(ecx.tcx.global_alloc(alloc_id), GlobalAlloc::Memory(_)) {
1395-
// `alloc_id` may point to a static. Codegen will choke on an `Indirect` with anything
1396-
// by `GlobalAlloc::Memory`, so do fall through to copying if needed.
1397-
// FIXME: find a way to treat this more uniformly
1398-
// (probably by fixing codegen)
1394+
1395+
// `alloc_id` may point to a static. Codegen will choke on an `Indirect` with anything
1396+
// by `GlobalAlloc::Memory`, so do fall through to copying if needed.
1397+
// FIXME: find a way to treat this more uniformly (probably by fixing codegen)
1398+
if let GlobalAlloc::Memory(alloc) = ecx.tcx.global_alloc(alloc_id)
1399+
// Transmuting a constant is just an offset in the allocation. If the alignment of the
1400+
// allocation is not enough, fallback to copying into a properly aligned value.
1401+
&& alloc.inner().align >= op.layout.align.abi
1402+
{
13991403
return Some(ConstValue::Indirect { alloc_id, offset });
14001404
}
14011405
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//@ build-pass
2+
//@ compile-flags: -Zmir-opt-level=0 -Zmir-enable-passes=+GVN
3+
4+
fn main() {
5+
let variant: Option<u32> = None;
6+
let transmuted: u64 = unsafe { std::mem::transmute(variant) };
7+
println!("{transmuted}");
8+
}

0 commit comments

Comments
 (0)