Skip to content

Commit 25ed43d

Browse files
committed
Auto merge of #115138 - cjgillot:dse-move-packed, r=compiler-errors
Do not convert copies of packed projections to moves. This code path was introduced in #113758 After seeing https://rust-lang.zulipchat.com/#narrow/stream/136281-t-opsem/topic/Packed.20fields.20and.20in-place.20function.20argument.2Freturn.20passing, this may be UB, so should be disallowed. This should not appear in normally-built MIR, which introduces temporary copies for packed projections.
2 parents 738df13 + 15a6861 commit 25ed43d

File tree

6 files changed

+64
-2
lines changed

6 files changed

+64
-2
lines changed

compiler/rustc_const_eval/src/util/alignment.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ where
4040
}
4141
}
4242

43-
fn is_within_packed<'tcx, L>(
43+
pub fn is_within_packed<'tcx, L>(
4444
tcx: TyCtxt<'tcx>,
4545
local_decls: &L,
4646
place: Place<'tcx>,

compiler/rustc_const_eval/src/util/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod check_validity_requirement;
55
mod compare_types;
66
mod type_name;
77

8-
pub use self::alignment::is_disaligned;
8+
pub use self::alignment::{is_disaligned, is_within_packed};
99
pub use self::check_validity_requirement::check_validity_requirement;
1010
pub use self::compare_types::{is_equal_up_to_subtyping, is_subtype};
1111
pub use self::type_name::type_name;

compiler/rustc_mir_transform/src/dead_store_elimination.rs

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//! will still not cause any further changes.
1313
//!
1414
15+
use crate::util::is_within_packed;
1516
use rustc_index::bit_set::BitSet;
1617
use rustc_middle::mir::visit::Visitor;
1718
use rustc_middle::mir::*;
@@ -49,6 +50,11 @@ pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, borrowed: &BitS
4950
&& !place.is_indirect()
5051
&& !borrowed.contains(place.local)
5152
&& !state.contains(place.local)
53+
// If `place` is a projection of a disaligned field in a packed ADT,
54+
// the move may be codegened as a pointer to that field.
55+
// Using that disaligned pointer may trigger UB in the callee,
56+
// so do nothing.
57+
&& is_within_packed(tcx, body, place).is_none()
5258
{
5359
call_operands_to_move.push((bb, index));
5460
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
- // MIR for `move_packed` before DeadStoreElimination
2+
+ // MIR for `move_packed` after DeadStoreElimination
3+
4+
fn move_packed(_1: Packed) -> () {
5+
let mut _0: ();
6+
7+
bb0: {
8+
_0 = use_both(const 0_i32, (_1.1: i32)) -> [return: bb1, unwind unreachable];
9+
}
10+
11+
bb1: {
12+
return;
13+
}
14+
}
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
- // MIR for `move_packed` before DeadStoreElimination
2+
+ // MIR for `move_packed` after DeadStoreElimination
3+
4+
fn move_packed(_1: Packed) -> () {
5+
let mut _0: ();
6+
7+
bb0: {
8+
_0 = use_both(const 0_i32, (_1.1: i32)) -> [return: bb1, unwind continue];
9+
}
10+
11+
bb1: {
12+
return;
13+
}
14+
}
15+

tests/mir-opt/dead-store-elimination/call_arg_copy.rs

+26
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
// unit-test: DeadStoreElimination
33
// compile-flags: -Zmir-enable-passes=+CopyProp
44

5+
#![feature(core_intrinsics)]
6+
#![feature(custom_mir)]
7+
#![allow(internal_features)]
8+
9+
use std::intrinsics::mir::*;
10+
511
#[inline(never)]
612
fn use_both(_: i32, _: i32) {}
713

@@ -10,6 +16,26 @@ fn move_simple(x: i32) {
1016
use_both(x, x);
1117
}
1218

19+
#[repr(packed)]
20+
struct Packed {
21+
x: u8,
22+
y: i32,
23+
}
24+
25+
// EMIT_MIR call_arg_copy.move_packed.DeadStoreElimination.diff
26+
#[custom_mir(dialect = "analysis")]
27+
fn move_packed(packed: Packed) {
28+
mir!(
29+
{
30+
Call(RET = use_both(0, packed.y), ret)
31+
}
32+
ret = {
33+
Return()
34+
}
35+
)
36+
}
37+
1338
fn main() {
1439
move_simple(1);
40+
move_packed(Packed { x: 0, y: 1 });
1541
}

0 commit comments

Comments
 (0)