From 9e5c162d729b2d892bab6b43166be03242b69ebd Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Thu, 8 Jun 2023 21:40:22 -0400 Subject: [PATCH] Move the implementation to codegen, to use monomorphized types --- compiler/rustc_codegen_ssa/src/mir/mod.rs | 27 ++++++++++++- compiler/rustc_mir_transform/src/lib.rs | 2 - compiler/rustc_mir_transform/src/ralf_refs.rs | 38 ------------------- 3 files changed, 26 insertions(+), 41 deletions(-) delete mode 100644 compiler/rustc_mir_transform/src/ralf_refs.rs diff --git a/compiler/rustc_codegen_ssa/src/mir/mod.rs b/compiler/rustc_codegen_ssa/src/mir/mod.rs index 2809ec2deb550..089d99488d56e 100644 --- a/compiler/rustc_codegen_ssa/src/mir/mod.rs +++ b/compiler/rustc_codegen_ssa/src/mir/mod.rs @@ -153,7 +153,7 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>( let llfn = cx.get_fn(instance); - let mir = cx.tcx().instance_mir(instance.def); + let mut mir = cx.tcx().instance_mir(instance.def); let fn_abi = cx.fn_abi_of_instance(instance, ty::List::empty()); debug!("fn_abi: {:?}", fn_abi); @@ -260,6 +260,31 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>( // Apply debuginfo to the newly allocated locals. fx.debug_introduce_locals(&mut start_bx); + if mir.args_iter().any(|arg| { + // Find the monomorphized type of our argument + let arg_decl = &mir.local_decls[arg]; + let arg_ty = fx.monomorphize(arg_decl.ty); + + // And check if it is a reference to an uninhabited type + let ty = arg_ty.peel_refs(); + start_bx.layout_of(ty).abi.is_uninhabited() + }) { + // We're probably not supposed to mutate MIR here. + // But since we have an arena, we can just create a clone and replace all references to + // the old MIR with the new. + let mut body = mir.clone(); + let blocks = body.basic_blocks.as_mut(); + blocks.truncate(1); + let block = &mut blocks[rustc_middle::mir::START_BLOCK]; + block.statements.clear(); + block.terminator.as_mut().unwrap().kind = rustc_middle::mir::TerminatorKind::Unreachable; + block.is_cleanup = false; + + // Now we have new MIR, stash it in the TyCtxt arena and swap it in + mir = fx.cx.tcx().arena.alloc(body); + fx.mir = mir; + } + // The builders will be created separately for each basic block at `codegen_block`. // So drop the builder of `start_llbb` to avoid having two at the same time. drop(start_bx); diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index 9acf876d3d480..7d9f6c38e36a4 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -86,7 +86,6 @@ mod multiple_return_terminators; mod normalize_array_len; mod nrvo; mod prettify; -mod ralf_refs; mod ref_prop; mod remove_noop_landing_pads; mod remove_storage_markers; @@ -546,7 +545,6 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { &check_alignment::CheckAlignment, &reveal_all::RevealAll, // has to be done before inlining, since inlined code is in RevealAll mode. &lower_slice_len::LowerSliceLenCalls, // has to be done before inlining, otherwise actual call will be almost always inlined. Also simple, so can just do first - &ralf_refs::RalfRefs, &unreachable_prop::UnreachablePropagation, &uninhabited_enum_branching::UninhabitedEnumBranching, &o1(simplify::SimplifyCfg::AfterUninhabitedEnumBranching), diff --git a/compiler/rustc_mir_transform/src/ralf_refs.rs b/compiler/rustc_mir_transform/src/ralf_refs.rs deleted file mode 100644 index fea0c71c98ad7..0000000000000 --- a/compiler/rustc_mir_transform/src/ralf_refs.rs +++ /dev/null @@ -1,38 +0,0 @@ -use crate::MirPass; -use rustc_middle::mir::*; -use rustc_middle::ty::print::with_no_trimmed_paths; -use rustc_middle::ty::TyCtxt; - -pub struct RalfRefs; - -impl<'tcx> MirPass<'tcx> for RalfRefs { - fn is_enabled(&self, sess: &rustc_session::Session) -> bool { - sess.mir_opt_level() >= 1 - } - - fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { - let def_id = body.source.def_id(); - if tcx.type_of(body.source.def_id()).subst_identity().is_generator() { - return; - } - let param_env = tcx.param_env_reveal_all_normalized(def_id); - for arg in body.local_decls.iter().skip(1).take(body.arg_count) { - let ty = arg.ty.peel_refs(); - let Ok(layout) = tcx.layout_of(param_env.and(ty)) else { continue; }; - if layout.abi.is_uninhabited() { - debug!( - "Deleting {}::{}", - tcx.crate_name(def_id.krate), - with_no_trimmed_paths!(tcx.def_path_str(def_id)) - ); - let blocks = body.basic_blocks.as_mut(); - blocks.truncate(1); - let block = &mut blocks[START_BLOCK]; - block.statements.clear(); - block.terminator.as_mut().unwrap().kind = TerminatorKind::Unreachable; - block.is_cleanup = false; - return; - } - } - } -}