Skip to content

Commit 550e035

Browse files
committed
Auto merge of rust-lang#136450 - compiler-errors:simplify-cast, r=saethlin
Don't reset cast kind without also updating the operand in `simplify_cast` in GVN Consider this heavily elided segment of the pre-GVN example code that was committed as a test: ```rust let _4: *const (); let _5: *const [()]; let mut _6: *const (); let _7: *mut (); let mut _8: *const [()]; let mut _9: std::boxed::Box<()>; let mut _10: *const (); /* ... */ // Deref a box _10 = copy ((_9.0: std::ptr::Unique<()>).0: std::ptr::NonNull<()>) as *const () (Transmute); _4 = copy _10; _6 = copy _4; // Inlined body of `slice::from_raw_parts`, to turn a unit pointer into a slice-of-unit pointer _5 = *const [()] from (copy _6, copy _11); _8 = copy _5; // Cast the raw slice-of-unit pointer back to a unit pointer _7 = copy _8 as *mut () (PtrToPtr); ``` A malformed optimization was changing `_7` (which casted the slice-of-unit ptr to a unit ptr) to: ``` _7 = copy _5 as *mut () (Transmute); ``` ...where `_8` was just replaced with `_5` bc of simple copy propagation, that part is not important... the CastKind changing to Transmute is the important part here. In rust-lang#133324, two new functionalities were implemented: * Peeking through unsized -> sized PtrToPtr casts whose operand is `AggregateKind::RawPtr`, to turn it into PtrToPtr casts of the base of the aggregate. In this case, this allows us to see that the value of `_7` is just a ptr-to-ptr cast of `_6`. * Folding a PtrToPtr cast of an operand which is a Transmute cast into just a single Transmute, which (theoretically) allows us to treat `_7` as a transmute into `*mut ()` of the base of the cast of `_10`, which is the place projection of `((_9.0: std::ptr::Unique<()>).0: std::ptr::NonNull<()>)`. However, when applying those two subsequent optimizations, we must *not* update the CastKind of the final cast *unless* we also update the operand of the cast, since the operand may no longer make sense with the updated CastKind. In this case, this is problematic because the type of `_8` is `*const [()]`, but that operand in assignment statement of `_7` does *not* get turned into something like `((_9.0: std::ptr::Unique<()>).0: std::ptr::NonNull<()>)` -- **in other words, `try_to_operand` fails** -- because GVN only turns value nodes into locals or consts, not projections of locals. So we fail to update the operand, but we still update the CastKind to Transmute, which means we now are transmuting types of different sizes (a wide pointer and a thin pointer). r? `@scottmcm` or `@cjgillot` Fixes rust-lang#136361 Fixes rust-lang#135997
2 parents f37c190 + de7d4a8 commit 550e035

4 files changed

+294
-9
lines changed

compiler/rustc_mir_transform/src/gvn.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -1367,16 +1367,17 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
13671367

13681368
fn simplify_cast(
13691369
&mut self,
1370-
kind: &mut CastKind,
1371-
operand: &mut Operand<'tcx>,
1370+
initial_kind: &mut CastKind,
1371+
initial_operand: &mut Operand<'tcx>,
13721372
to: Ty<'tcx>,
13731373
location: Location,
13741374
) -> Option<VnIndex> {
13751375
use CastKind::*;
13761376
use rustc_middle::ty::adjustment::PointerCoercion::*;
13771377

1378-
let mut from = operand.ty(self.local_decls, self.tcx);
1379-
let mut value = self.simplify_operand(operand, location)?;
1378+
let mut from = initial_operand.ty(self.local_decls, self.tcx);
1379+
let mut kind = *initial_kind;
1380+
let mut value = self.simplify_operand(initial_operand, location)?;
13801381
if from == to {
13811382
return Some(value);
13821383
}
@@ -1400,7 +1401,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
14001401
&& to.is_unsafe_ptr()
14011402
&& self.pointers_have_same_metadata(from, to)
14021403
{
1403-
*kind = PtrToPtr;
1404+
kind = PtrToPtr;
14041405
was_updated_this_iteration = true;
14051406
}
14061407

@@ -1443,7 +1444,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
14431444
to: inner_to,
14441445
} = *self.get(value)
14451446
{
1446-
let new_kind = match (inner_kind, *kind) {
1447+
let new_kind = match (inner_kind, kind) {
14471448
// Even if there's a narrowing cast in here that's fine, because
14481449
// things like `*mut [i32] -> *mut i32 -> *const i32` and
14491450
// `*mut [i32] -> *const [i32] -> *const i32` can skip the middle in MIR.
@@ -1471,7 +1472,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
14711472
_ => None,
14721473
};
14731474
if let Some(new_kind) = new_kind {
1474-
*kind = new_kind;
1475+
kind = new_kind;
14751476
from = inner_from;
14761477
value = inner_value;
14771478
was_updated_this_iteration = true;
@@ -1489,10 +1490,11 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
14891490
}
14901491

14911492
if was_ever_updated && let Some(op) = self.try_as_operand(value, location) {
1492-
*operand = op;
1493+
*initial_operand = op;
1494+
*initial_kind = kind;
14931495
}
14941496

1495-
Some(self.insert(Value::Cast { kind: *kind, value, from, to }))
1497+
Some(self.insert(Value::Cast { kind, value, from, to }))
14961498
}
14971499

14981500
fn simplify_len(&mut self, place: &mut Place<'tcx>, location: Location) -> Option<VnIndex> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// skip-filecheck
2+
//@ compile-flags: -Zmir-enable-passes=+Inline,+GVN --crate-type lib
3+
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
4+
// EMIT_MIR dont_reset_cast_kind_without_updating_operand.test.GVN.diff
5+
6+
fn test() {
7+
let vp_ctx: &Box<()> = &Box::new(());
8+
let slf: *const () = &raw const **vp_ctx;
9+
let bytes = std::ptr::slice_from_raw_parts(slf, 1);
10+
let _x = foo(bytes);
11+
}
12+
13+
fn foo(bytes: *const [()]) -> *mut () {
14+
bytes as *mut ()
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
- // MIR for `test` before GVN
2+
+ // MIR for `test` after GVN
3+
4+
fn test() -> () {
5+
let mut _0: ();
6+
let _1: &std::boxed::Box<()>;
7+
let _2: &std::boxed::Box<()>;
8+
let _3: std::boxed::Box<()>;
9+
let mut _6: *const ();
10+
let mut _8: *const [()];
11+
let mut _9: std::boxed::Box<()>;
12+
let mut _10: *const ();
13+
let mut _23: usize;
14+
scope 1 {
15+
debug vp_ctx => _1;
16+
let _4: *const ();
17+
scope 2 {
18+
debug slf => _10;
19+
let _5: *const [()];
20+
scope 3 {
21+
debug bytes => _5;
22+
let _7: *mut ();
23+
scope 4 {
24+
debug _x => _7;
25+
}
26+
scope 18 (inlined foo) {
27+
}
28+
}
29+
scope 16 (inlined slice_from_raw_parts::<()>) {
30+
scope 17 (inlined std::ptr::from_raw_parts::<[()], ()>) {
31+
}
32+
}
33+
}
34+
}
35+
scope 5 (inlined Box::<()>::new) {
36+
let mut _11: usize;
37+
let mut _12: usize;
38+
let mut _13: *mut u8;
39+
scope 6 (inlined alloc::alloc::exchange_malloc) {
40+
let _14: std::alloc::Layout;
41+
let mut _15: std::result::Result<std::ptr::NonNull<[u8]>, std::alloc::AllocError>;
42+
let mut _16: isize;
43+
let mut _18: !;
44+
scope 7 {
45+
let _17: std::ptr::NonNull<[u8]>;
46+
scope 8 {
47+
scope 11 (inlined NonNull::<[u8]>::as_mut_ptr) {
48+
scope 12 (inlined NonNull::<[u8]>::as_non_null_ptr) {
49+
scope 13 (inlined NonNull::<[u8]>::cast::<u8>) {
50+
let mut _22: *mut [u8];
51+
scope 14 (inlined NonNull::<[u8]>::as_ptr) {
52+
}
53+
}
54+
}
55+
scope 15 (inlined NonNull::<u8>::as_ptr) {
56+
}
57+
}
58+
}
59+
scope 10 (inlined <std::alloc::Global as Allocator>::allocate) {
60+
}
61+
}
62+
scope 9 (inlined Layout::from_size_align_unchecked) {
63+
let mut _19: bool;
64+
let _20: ();
65+
let mut _21: std::ptr::Alignment;
66+
}
67+
}
68+
}
69+
70+
bb0: {
71+
StorageLive(_1);
72+
- StorageLive(_2);
73+
+ nop;
74+
StorageLive(_3);
75+
StorageLive(_11);
76+
StorageLive(_12);
77+
StorageLive(_13);
78+
- _11 = SizeOf(());
79+
- _12 = AlignOf(());
80+
+ _11 = const 0_usize;
81+
+ _12 = const 1_usize;
82+
StorageLive(_14);
83+
StorageLive(_16);
84+
StorageLive(_17);
85+
StorageLive(_19);
86+
_19 = const false;
87+
- switchInt(move _19) -> [0: bb6, otherwise: bb5];
88+
+ switchInt(const false) -> [0: bb6, otherwise: bb5];
89+
}
90+
91+
bb1: {
92+
StorageDead(_3);
93+
StorageDead(_1);
94+
return;
95+
}
96+
97+
bb2: {
98+
unreachable;
99+
}
100+
101+
bb3: {
102+
- _18 = handle_alloc_error(move _14) -> unwind unreachable;
103+
+ _18 = handle_alloc_error(const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }}) -> unwind unreachable;
104+
}
105+
106+
bb4: {
107+
_17 = copy ((_15 as Ok).0: std::ptr::NonNull<[u8]>);
108+
StorageLive(_22);
109+
_22 = copy _17 as *mut [u8] (Transmute);
110+
_13 = copy _22 as *mut u8 (PtrToPtr);
111+
StorageDead(_22);
112+
StorageDead(_15);
113+
StorageDead(_17);
114+
StorageDead(_16);
115+
StorageDead(_14);
116+
_3 = ShallowInitBox(move _13, ());
117+
StorageDead(_13);
118+
StorageDead(_12);
119+
StorageDead(_11);
120+
_2 = &_3;
121+
_1 = copy _2;
122+
- StorageDead(_2);
123+
+ nop;
124+
StorageLive(_4);
125+
- _9 = deref_copy _3;
126+
+ _9 = copy _3;
127+
_10 = copy ((_9.0: std::ptr::Unique<()>).0: std::ptr::NonNull<()>) as *const () (Transmute);
128+
_4 = copy _10;
129+
- StorageLive(_5);
130+
+ nop;
131+
StorageLive(_6);
132+
- _6 = copy _4;
133+
+ _6 = copy _10;
134+
StorageLive(_23);
135+
_23 = const 1_usize;
136+
- _5 = *const [()] from (copy _6, copy _23);
137+
+ _5 = *const [()] from (copy _10, const 1_usize);
138+
StorageDead(_23);
139+
StorageDead(_6);
140+
StorageLive(_7);
141+
StorageLive(_8);
142+
_8 = copy _5;
143+
- _7 = copy _8 as *mut () (PtrToPtr);
144+
+ _7 = copy _5 as *mut () (PtrToPtr);
145+
StorageDead(_8);
146+
StorageDead(_7);
147+
- StorageDead(_5);
148+
+ nop;
149+
StorageDead(_4);
150+
drop(_3) -> [return: bb1, unwind unreachable];
151+
}
152+
153+
bb5: {
154+
- _20 = Layout::from_size_align_unchecked::precondition_check(copy _11, copy _12) -> [return: bb6, unwind unreachable];
155+
+ _20 = Layout::from_size_align_unchecked::precondition_check(const 0_usize, const 1_usize) -> [return: bb6, unwind unreachable];
156+
}
157+
158+
bb6: {
159+
StorageDead(_19);
160+
StorageLive(_21);
161+
- _21 = copy _12 as std::ptr::Alignment (Transmute);
162+
- _14 = Layout { size: copy _11, align: move _21 };
163+
+ _21 = const std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0);
164+
+ _14 = const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }};
165+
StorageDead(_21);
166+
StorageLive(_15);
167+
- _15 = std::alloc::Global::alloc_impl(const alloc::alloc::exchange_malloc::promoted[0], copy _14, const false) -> [return: bb7, unwind unreachable];
168+
+ _15 = std::alloc::Global::alloc_impl(const alloc::alloc::exchange_malloc::promoted[0], const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }}, const false) -> [return: bb7, unwind unreachable];
169+
}
170+
171+
bb7: {
172+
_16 = discriminant(_15);
173+
switchInt(move _16) -> [0: bb4, 1: bb3, otherwise: bb2];
174+
}
175+
+ }
176+
+
177+
+ ALLOC0 (size: 8, align: 4) {
178+
+ 01 00 00 00 00 00 00 00 │ ........
179+
}
180+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
- // MIR for `test` before GVN
2+
+ // MIR for `test` after GVN
3+
4+
fn test() -> () {
5+
let mut _0: ();
6+
let _1: &std::boxed::Box<()>;
7+
let _2: &std::boxed::Box<()>;
8+
let _3: std::boxed::Box<()>;
9+
let mut _6: *const ();
10+
let mut _8: *const [()];
11+
let mut _9: std::boxed::Box<()>;
12+
let mut _10: *const ();
13+
let mut _11: usize;
14+
scope 1 {
15+
debug vp_ctx => _1;
16+
let _4: *const ();
17+
scope 2 {
18+
debug slf => _10;
19+
let _5: *const [()];
20+
scope 3 {
21+
debug bytes => _5;
22+
let _7: *mut ();
23+
scope 4 {
24+
debug _x => _7;
25+
}
26+
scope 7 (inlined foo) {
27+
}
28+
}
29+
scope 5 (inlined slice_from_raw_parts::<()>) {
30+
scope 6 (inlined std::ptr::from_raw_parts::<[()], ()>) {
31+
}
32+
}
33+
}
34+
}
35+
36+
bb0: {
37+
StorageLive(_1);
38+
- StorageLive(_2);
39+
+ nop;
40+
StorageLive(_3);
41+
_3 = Box::<()>::new(const ()) -> [return: bb1, unwind continue];
42+
}
43+
44+
bb1: {
45+
_2 = &_3;
46+
_1 = copy _2;
47+
- StorageDead(_2);
48+
+ nop;
49+
StorageLive(_4);
50+
- _9 = deref_copy _3;
51+
+ _9 = copy _3;
52+
_10 = copy ((_9.0: std::ptr::Unique<()>).0: std::ptr::NonNull<()>) as *const () (Transmute);
53+
_4 = copy _10;
54+
- StorageLive(_5);
55+
+ nop;
56+
StorageLive(_6);
57+
- _6 = copy _4;
58+
+ _6 = copy _10;
59+
StorageLive(_11);
60+
_11 = const 1_usize;
61+
- _5 = *const [()] from (copy _6, copy _11);
62+
+ _5 = *const [()] from (copy _10, const 1_usize);
63+
StorageDead(_11);
64+
StorageDead(_6);
65+
StorageLive(_7);
66+
StorageLive(_8);
67+
_8 = copy _5;
68+
- _7 = copy _8 as *mut () (PtrToPtr);
69+
+ _7 = copy _5 as *mut () (PtrToPtr);
70+
StorageDead(_8);
71+
StorageDead(_7);
72+
- StorageDead(_5);
73+
+ nop;
74+
StorageDead(_4);
75+
drop(_3) -> [return: bb2, unwind: bb3];
76+
}
77+
78+
bb2: {
79+
StorageDead(_3);
80+
StorageDead(_1);
81+
return;
82+
}
83+
84+
bb3 (cleanup): {
85+
resume;
86+
}
87+
}
88+

0 commit comments

Comments
 (0)