Skip to content

Commit

Permalink
Auto merge of rust-lang#105229 - saethlin:zst-writes-to-unions, r=oli…
Browse files Browse the repository at this point in the history
…-obk

Re-enable removal of ZST writes to unions

This was previously disabled because Miri was lazily allocating unsized locals. But we aren't doing that anymore since  rust-lang#98831, so we can have this optimization back.
  • Loading branch information
bors committed Dec 6, 2022
2 parents b685242 + 74a270a commit e60fbaf
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 60 deletions.
27 changes: 1 addition & 26 deletions compiler/rustc_mir_transform/src/remove_zsts.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! Removes assignments to ZST places.
use crate::MirPass;
use rustc_middle::mir::tcx::PlaceTy;
use rustc_middle::mir::{Body, LocalDecls, Place, StatementKind};
use rustc_middle::mir::{Body, StatementKind};
use rustc_middle::ty::{self, Ty, TyCtxt};

pub struct RemoveZsts;
Expand Down Expand Up @@ -35,9 +34,6 @@ impl<'tcx> MirPass<'tcx> for RemoveZsts {
if !layout.is_zst() {
continue;
}
if involves_a_union(place, local_decls, tcx) {
continue;
}
if tcx.consider_optimizing(|| {
format!(
"RemoveZsts - Place: {:?} SourceInfo: {:?}",
Expand All @@ -63,24 +59,3 @@ fn maybe_zst(ty: Ty<'_>) -> bool {
_ => false,
}
}

/// Miri lazily allocates memory for locals on assignment,
/// so we must preserve writes to unions and union fields,
/// or it will ICE on reads of those fields.
fn involves_a_union<'tcx>(
place: Place<'tcx>,
local_decls: &LocalDecls<'tcx>,
tcx: TyCtxt<'tcx>,
) -> bool {
let mut place_ty = PlaceTy::from_ty(local_decls[place.local].ty);
if place_ty.ty.is_union() {
return true;
}
for elem in place.projection {
place_ty = place_ty.projection_ty(tcx, elem);
if place_ty.ty.is_union() {
return true;
}
}
return false;
}
10 changes: 10 additions & 0 deletions src/test/mir-opt/remove_zsts.get_union.PreCodegen.after.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// MIR for `get_union` after PreCodegen

fn get_union() -> Foo {
let mut _0: Foo; // return place in scope 0 at $DIR/remove_zsts.rs:+0:19: +0:22

bb0: {
Deinit(_0); // scope 0 at $DIR/remove_zsts.rs:+1:5: +1:18
return; // scope 0 at $DIR/remove_zsts.rs:+2:2: +2:2
}
}
19 changes: 19 additions & 0 deletions src/test/mir-opt/remove_zsts.get_union.RemoveZsts.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
- // MIR for `get_union` before RemoveZsts
+ // MIR for `get_union` after RemoveZsts

fn get_union() -> Foo {
let mut _0: Foo; // return place in scope 0 at $DIR/remove_zsts.rs:+0:19: +0:22
let mut _1: (); // in scope 0 at $DIR/remove_zsts.rs:+1:14: +1:16

bb0: {
StorageLive(_1); // scope 0 at $DIR/remove_zsts.rs:+1:14: +1:16
- Deinit(_1); // scope 0 at $DIR/remove_zsts.rs:+1:14: +1:16
+ nop; // scope 0 at $DIR/remove_zsts.rs:+1:14: +1:16
Deinit(_0); // scope 0 at $DIR/remove_zsts.rs:+1:5: +1:18
- (_0.0: ()) = move _1; // scope 0 at $DIR/remove_zsts.rs:+1:5: +1:18
+ nop; // scope 0 at $DIR/remove_zsts.rs:+1:5: +1:18
StorageDead(_1); // scope 0 at $DIR/remove_zsts.rs:+1:17: +1:18
return; // scope 0 at $DIR/remove_zsts.rs:+2:2: +2:2
}
}

14 changes: 14 additions & 0 deletions src/test/mir-opt/remove_zsts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
union Foo {
x: (),
y: u64,
}

// EMIT_MIR remove_zsts.get_union.RemoveZsts.diff
// EMIT_MIR remove_zsts.get_union.PreCodegen.after.mir
fn get_union() -> Foo {
Foo { x: () }
}

fn main() {
get_union();
}

This file was deleted.

19 changes: 0 additions & 19 deletions src/test/mir-opt/remove_zsts_dont_touch_unions.rs

This file was deleted.

0 comments on commit e60fbaf

Please sign in to comment.