Skip to content

Commit 1c5f176

Browse files
committed
Do not convert copies of packed projections to moves.
1 parent fe5f591 commit 1c5f176

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

compiler/rustc_mir_transform/src/dead_store_elimination.rs

+4
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_disaligned;
1516
use rustc_index::bit_set::BitSet;
1617
use rustc_middle::mir::visit::Visitor;
1718
use rustc_middle::mir::*;
@@ -31,6 +32,8 @@ pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, borrowed: &BitS
3132
.iterate_to_fixpoint()
3233
.into_results_cursor(body);
3334

35+
let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id());
36+
3437
// For blocks with a call terminator, if an argument copy can be turned into a move,
3538
// record it as (block, argument index).
3639
let mut call_operands_to_move = Vec::new();
@@ -49,6 +52,7 @@ pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, borrowed: &BitS
4952
&& !place.is_indirect()
5053
&& !borrowed.contains(place.local)
5154
&& !state.contains(place.local)
55+
&& !is_disaligned(tcx, body, param_env, place)
5256
{
5357
call_operands_to_move.push((bb, index));
5458
}
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)