Skip to content

Commit 03650dd

Browse files
committed
Use layout information to detect transparent transmutes
1 parent 259c425 commit 03650dd

File tree

4 files changed

+111
-18
lines changed

4 files changed

+111
-18
lines changed

compiler/rustc_mir_transform/src/gvn.rs

+34-9
Original file line numberDiff line numberDiff line change
@@ -1410,24 +1410,23 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
14101410
}
14111411

14121412
// Aggregate-then-Transmute can just transmute the original field value,
1413-
// so long as the type is transparent over only that one single field.
1413+
// so long as the bytes of a value from only from a single field.
14141414
if let Transmute = kind
14151415
&& let Value::Aggregate(
14161416
AggregateTy::Def(aggregate_did, aggregate_args),
1417-
FIRST_VARIANT,
1417+
variant_idx,
14181418
field_values,
14191419
) = self.get(value)
1420-
&& let [single_field_value] = **field_values
1421-
&& let adt = self.tcx.adt_def(aggregate_did)
1422-
&& adt.is_struct()
1423-
&& adt.repr().transparent()
1420+
&& let aggregate_ty =
1421+
self.tcx.type_of(aggregate_did).instantiate(self.tcx, aggregate_args)
1422+
&& let Some((field_idx, field_ty)) =
1423+
self.value_is_all_in_one_field(aggregate_ty, *variant_idx)
14241424
{
1425-
let field_ty = adt.non_enum_variant().single_field().ty(self.tcx, aggregate_args);
14261425
from = field_ty;
1427-
value = single_field_value;
1426+
value = field_values[field_idx.as_usize()];
14281427
was_updated = true;
14291428
if field_ty == to {
1430-
return Some(single_field_value);
1429+
return Some(value);
14311430
}
14321431
}
14331432

@@ -1492,6 +1491,32 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
14921491
false
14931492
}
14941493
}
1494+
1495+
fn value_is_all_in_one_field(
1496+
&self,
1497+
ty: Ty<'tcx>,
1498+
variant: VariantIdx,
1499+
) -> Option<(FieldIdx, Ty<'tcx>)> {
1500+
if let Ok(layout) = self.ecx.layout_of(ty)
1501+
&& let abi::Variants::Single { index } = layout.variants
1502+
&& index == variant
1503+
&& let Some((field_idx, field_layout)) = layout.non_1zst_field(&self.ecx)
1504+
&& layout.size == field_layout.size
1505+
{
1506+
// We needed to check the variant to avoid trying to read the tag
1507+
// field from an enum where no fields have variants, since that tag
1508+
// field isn't in the `Aggregate` from which we're getting values.
1509+
Some((FieldIdx::from_usize(field_idx), field_layout.ty))
1510+
} else if let ty::Adt(adt, args) = ty.kind()
1511+
&& adt.is_struct()
1512+
&& adt.repr().transparent()
1513+
&& let [single_field] = adt.non_enum_variant().fields.raw.as_slice()
1514+
{
1515+
Some((FieldIdx::ZERO, single_field.ty(self.tcx, args)))
1516+
} else {
1517+
None
1518+
}
1519+
}
14951520
}
14961521

14971522
fn op_to_prop_const<'tcx>(

tests/mir-opt/gvn.aggregate_struct_then_transmute.GVN.panic-abort.diff

+35-2
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,19 @@
1414
let _10: ();
1515
let mut _11: u16;
1616
let mut _12: TypedId<std::string::String>;
17+
let mut _14: u16;
18+
let _15: ();
19+
let mut _16: u16;
20+
let mut _17: std::result::Result<aggregate_struct_then_transmute::Never, u16>;
1721
scope 1 {
1822
debug a => _2;
1923
let _7: TypedId<std::string::String>;
2024
scope 2 {
2125
debug b => _7;
26+
let _13: std::result::Result<aggregate_struct_then_transmute::Never, u16>;
27+
scope 3 {
28+
debug c => _13;
29+
}
2230
}
2331
}
2432

@@ -62,18 +70,43 @@
6270
- _12 = move _7;
6371
- _11 = move _12 as u16 (Transmute);
6472
+ _12 = copy _7;
65-
+ _11 = copy _7 as u16 (Transmute);
73+
+ _11 = copy _1;
6674
StorageDead(_12);
67-
_10 = opaque::<u16>(move _11) -> [return: bb2, unwind unreachable];
75+
- _10 = opaque::<u16>(move _11) -> [return: bb2, unwind unreachable];
76+
+ _10 = opaque::<u16>(copy _1) -> [return: bb2, unwind unreachable];
6877
}
6978

7079
bb2: {
7180
StorageDead(_11);
7281
StorageDead(_10);
82+
- StorageLive(_13);
83+
+ nop;
84+
StorageLive(_14);
85+
_14 = copy _1;
86+
- _13 = Result::<Never, u16>::Err(move _14);
87+
+ _13 = Result::<Never, u16>::Err(copy _1);
88+
StorageDead(_14);
89+
StorageLive(_15);
90+
StorageLive(_16);
91+
StorageLive(_17);
92+
- _17 = move _13;
93+
- _16 = move _17 as u16 (Transmute);
94+
+ _17 = copy _13;
95+
+ _16 = copy _1;
96+
StorageDead(_17);
97+
- _15 = opaque::<u16>(move _16) -> [return: bb3, unwind unreachable];
98+
+ _15 = opaque::<u16>(copy _1) -> [return: bb3, unwind unreachable];
99+
}
100+
101+
bb3: {
102+
StorageDead(_16);
103+
StorageDead(_15);
73104
_0 = const ();
105+
- StorageDead(_13);
74106
- StorageDead(_7);
75107
- StorageDead(_2);
76108
+ nop;
109+
+ nop;
77110
+ nop;
78111
return;
79112
}

tests/mir-opt/gvn.aggregate_struct_then_transmute.GVN.panic-unwind.diff

+35-2
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,19 @@
1414
let _10: ();
1515
let mut _11: u16;
1616
let mut _12: TypedId<std::string::String>;
17+
let mut _14: u16;
18+
let _15: ();
19+
let mut _16: u16;
20+
let mut _17: std::result::Result<aggregate_struct_then_transmute::Never, u16>;
1721
scope 1 {
1822
debug a => _2;
1923
let _7: TypedId<std::string::String>;
2024
scope 2 {
2125
debug b => _7;
26+
let _13: std::result::Result<aggregate_struct_then_transmute::Never, u16>;
27+
scope 3 {
28+
debug c => _13;
29+
}
2230
}
2331
}
2432

@@ -62,18 +70,43 @@
6270
- _12 = move _7;
6371
- _11 = move _12 as u16 (Transmute);
6472
+ _12 = copy _7;
65-
+ _11 = copy _7 as u16 (Transmute);
73+
+ _11 = copy _1;
6674
StorageDead(_12);
67-
_10 = opaque::<u16>(move _11) -> [return: bb2, unwind continue];
75+
- _10 = opaque::<u16>(move _11) -> [return: bb2, unwind continue];
76+
+ _10 = opaque::<u16>(copy _1) -> [return: bb2, unwind continue];
6877
}
6978

7079
bb2: {
7180
StorageDead(_11);
7281
StorageDead(_10);
82+
- StorageLive(_13);
83+
+ nop;
84+
StorageLive(_14);
85+
_14 = copy _1;
86+
- _13 = Result::<Never, u16>::Err(move _14);
87+
+ _13 = Result::<Never, u16>::Err(copy _1);
88+
StorageDead(_14);
89+
StorageLive(_15);
90+
StorageLive(_16);
91+
StorageLive(_17);
92+
- _17 = move _13;
93+
- _16 = move _17 as u16 (Transmute);
94+
+ _17 = copy _13;
95+
+ _16 = copy _1;
96+
StorageDead(_17);
97+
- _15 = opaque::<u16>(move _16) -> [return: bb3, unwind continue];
98+
+ _15 = opaque::<u16>(copy _1) -> [return: bb3, unwind continue];
99+
}
100+
101+
bb3: {
102+
StorageDead(_16);
103+
StorageDead(_15);
73104
_0 = const ();
105+
- StorageDead(_13);
74106
- StorageDead(_7);
75107
- StorageDead(_2);
76108
+ nop;
109+
+ nop;
77110
+ nop;
78111
return;
79112
}

tests/mir-opt/gvn.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -938,13 +938,15 @@ unsafe fn aggregate_struct_then_transmute(id: u16) {
938938
let a = MyId(id);
939939
opaque(std::intrinsics::transmute::<_, u16>(a));
940940

941-
// GVN can't do this yet because it doesn't know which field is the ZST,
942-
// but future changes might enable it.
943-
// CHECK: [[AGG:_.+]] = TypedId::<String>(copy _1, const PhantomData::<String>);
944-
// CHECK: [[INT:_.+]] = copy [[AGG]] as u16 (Transmute);
945-
// CHECK: opaque::<u16>(move [[INT]])
941+
// CHECK: opaque::<u16>(copy _1)
946942
let b = TypedId::<String>(id, PhantomData);
947943
opaque(std::intrinsics::transmute::<_, u16>(b));
944+
945+
// CHECK: opaque::<u16>(copy _1)
946+
let c = Err::<Never, u16>(id);
947+
opaque(std::intrinsics::transmute::<_, u16>(c));
948+
949+
enum Never {}
948950
}
949951

950952
// Transmuting can skip a pointer cast so long as it wasn't a fat-to-thin cast.

0 commit comments

Comments
 (0)