Skip to content

Commit b421a56

Browse files
committed
Make the aggregate-then-transmute handling more general
1 parent 293f8e8 commit b421a56

File tree

4 files changed

+514
-308
lines changed

4 files changed

+514
-308
lines changed

Diff for: compiler/rustc_mir_transform/src/gvn.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -1370,12 +1370,14 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
13701370
loop {
13711371
let mut was_updated_this_iteration = false;
13721372

1373-
// Transmuting `*const T` <=> `*mut T` is just a pointer cast,
1374-
// which we might be able to merge with other ones later.
1373+
// Transmuting between raw pointers is just a pointer cast so long as
1374+
// they have the same metadata type (like `*const i32` <=> `*mut u64`
1375+
// or `*mut [i32]` <=> `*const [u64]`), including the common special
1376+
// case of `*const T` <=> `*mut T`.
13751377
if let Transmute = kind
1376-
&& let ty::RawPtr(from_pointee, _) = from.kind()
1377-
&& let ty::RawPtr(to_pointee, _) = to.kind()
1378-
&& from_pointee == to_pointee
1378+
&& from.is_unsafe_ptr()
1379+
&& to.is_unsafe_ptr()
1380+
&& self.pointers_have_same_metadata(from, to)
13791381
{
13801382
*kind = PtrToPtr;
13811383
was_updated_this_iteration = true;
@@ -1400,15 +1402,9 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
14001402
// Aggregate-then-Transmute can just transmute the original field value,
14011403
// so long as the bytes of a value from only from a single field.
14021404
if let Transmute = kind
1403-
&& let Value::Aggregate(
1404-
AggregateTy::Def(aggregate_did, aggregate_args),
1405-
variant_idx,
1406-
field_values,
1407-
) = self.get(value)
1408-
&& let aggregate_ty =
1409-
self.tcx.type_of(aggregate_did).instantiate(self.tcx, aggregate_args)
1405+
&& let Value::Aggregate(_aggregate_ty, variant_idx, field_values) = self.get(value)
14101406
&& let Some((field_idx, field_ty)) =
1411-
self.value_is_all_in_one_field(aggregate_ty, *variant_idx)
1407+
self.value_is_all_in_one_field(from, *variant_idx)
14121408
{
14131409
from = field_ty;
14141410
value = field_values[field_idx.as_usize()];

0 commit comments

Comments
 (0)