Auto merge of #105229 - saethlin:zst-writes-to-unions, r=oli-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  https://github.com/rust-lang/rust/pull/98831, so we can have this optimization back.
This commit is contained in:
bors 2022-12-06 15:35:55 +00:00
commit e60fbaf4ce
6 changed files with 44 additions and 60 deletions

View File

@ -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;
@ -35,9 +34,6 @@ fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
if !layout.is_zst() {
continue;
}
if involves_a_union(place, local_decls, tcx) {
continue;
}
if tcx.consider_optimizing(|| {
format!(
"RemoveZsts - Place: {:?} SourceInfo: {:?}",
@ -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;
}

View File

@ -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
}
}

View File

@ -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
}
}

View File

@ -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();
}

View File

@ -1,15 +0,0 @@
// MIR for `get_union` after RemoveZsts
fn get_union() -> Foo {
let mut _0: Foo; // return place in scope 0 at $DIR/remove_zsts_dont_touch_unions.rs:+0:19: +0:22
let mut _1: (); // in scope 0 at $DIR/remove_zsts_dont_touch_unions.rs:+1:14: +1:16
bb0: {
StorageLive(_1); // scope 0 at $DIR/remove_zsts_dont_touch_unions.rs:+1:14: +1:16
nop; // scope 0 at $DIR/remove_zsts_dont_touch_unions.rs:+1:14: +1:16
Deinit(_0); // scope 0 at $DIR/remove_zsts_dont_touch_unions.rs:+1:5: +1:18
(_0.0: ()) = move _1; // scope 0 at $DIR/remove_zsts_dont_touch_unions.rs:+1:5: +1:18
StorageDead(_1); // scope 0 at $DIR/remove_zsts_dont_touch_unions.rs:+1:17: +1:18
return; // scope 0 at $DIR/remove_zsts_dont_touch_unions.rs:+2:2: +2:2
}
}

View File

@ -1,19 +0,0 @@
// unit-test: RemoveZsts
// Ensure RemoveZsts doesn't remove ZST assignments to union fields,
// which causes problems in Miri.
union Foo {
x: (),
y: u64,
}
// EMIT_MIR remove_zsts_dont_touch_unions.get_union.RemoveZsts.after.mir
fn get_union() -> Foo {
Foo { x: () }
}
fn main() {
get_union();
}