diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index 7d9f6c38e36a4..9acf876d3d480 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -86,6 +86,7 @@ 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; @@ -545,6 +546,7 @@ 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 new file mode 100644 index 0000000000000..fea0c71c98ad7 --- /dev/null +++ b/compiler/rustc_mir_transform/src/ralf_refs.rs @@ -0,0 +1,38 @@ +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; + } + } + } +}