Skip to content

Commit d8f68f4

Browse files
committed
Prune MIR based on types before codegen.
1 parent a9b879b commit d8f68f4

File tree

10 files changed

+42
-21
lines changed

10 files changed

+42
-21
lines changed

compiler/rustc_codegen_ssa/src/mir/mod.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ use rustc_middle::mir::{Body, Local, UnwindTerminateReason, traversal};
77
use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, HasTypingEnv, TyAndLayout};
88
use rustc_middle::ty::{self, Instance, Ty, TyCtxt, TypeVisitableExt};
99
use rustc_middle::{bug, mir, span_bug};
10-
use rustc_mir_transform::{add_call_guards, dump_mir, pass_manager};
10+
use rustc_mir_transform::{
11+
add_call_guards, dump_mir, pass_manager, remove_noop_landing_pads, remove_unneeded_drops,
12+
remove_zsts, simplify, simplify_branches, unreachable_enum_branching, unreachable_prop,
13+
};
1114
use rustc_target::callconv::{FnAbi, PassMode};
1215
use tracing::{debug, instrument};
1316

@@ -172,6 +175,15 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
172175
cx.tcx(),
173176
&mut mir,
174177
&[
178+
// Monomorphization may introduce ZST, non-drop and uninhabited types.
179+
&remove_zsts::RemoveZsts,
180+
&remove_unneeded_drops::RemoveUnneededDrops,
181+
&unreachable_enum_branching::UnreachableEnumBranching,
182+
&unreachable_prop::UnreachablePropagation,
183+
&remove_noop_landing_pads::RemoveNoopLandingPads,
184+
&simplify_branches::SimplifyConstCondition::Monomorphic,
185+
&simplify::SimplifyCfg::Monomorphic,
186+
&simplify::SimplifyLocals::Monomorphic,
175187
// Some cleanup necessary at least for LLVM and potentially other codegen backends.
176188
&add_call_guards::CriticalCallEdges,
177189
// Dump the end result for testing and debugging purposes.

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ mod ssa;
7979
macro_rules! declare_passes {
8080
(
8181
$(
82-
$vis:vis mod $mod_name:ident : $($pass_name:ident $( { $($ident:ident),* } )?),+ $(,)?;
82+
$vis:vis mod $mod_name:ident : $($pass_name:ident $( { $($ident:ident),* $(,)? } )?),+ $(,)?;
8383
)*
8484
) => {
8585
$(
@@ -159,12 +159,12 @@ declare_passes! {
159159
mod prettify : ReorderBasicBlocks, ReorderLocals;
160160
mod promote_consts : PromoteTemps;
161161
mod ref_prop : ReferencePropagation;
162-
mod remove_noop_landing_pads : RemoveNoopLandingPads;
162+
pub mod remove_noop_landing_pads : RemoveNoopLandingPads;
163163
mod remove_place_mention : RemovePlaceMention;
164164
mod remove_storage_markers : RemoveStorageMarkers;
165165
mod remove_uninit_drops : RemoveUninitDrops;
166-
mod remove_unneeded_drops : RemoveUnneededDrops;
167-
mod remove_zsts : RemoveZsts;
166+
pub mod remove_unneeded_drops : RemoveUnneededDrops;
167+
pub mod remove_zsts : RemoveZsts;
168168
mod required_consts : RequiredConstsVisitor;
169169
mod post_analysis_normalize : PostAnalysisNormalize;
170170
mod sanity_check : SanityCheck;
@@ -178,23 +178,26 @@ declare_passes! {
178178
PreOptimizations,
179179
Final,
180180
MakeShim,
181-
AfterUnreachableEnumBranching
181+
AfterUnreachableEnumBranching,
182+
Monomorphic,
182183
},
183184
SimplifyLocals {
184185
BeforeConstProp,
185186
AfterGVN,
186-
Final
187+
Final,
188+
Monomorphic,
187189
};
188-
mod simplify_branches : SimplifyConstCondition {
190+
pub mod simplify_branches : SimplifyConstCondition {
189191
AfterConstProp,
190-
Final
192+
Final,
193+
Monomorphic,
191194
};
192195
mod simplify_comparison_integral : SimplifyComparisonIntegral;
193196
mod single_use_consts : SingleUseConsts;
194197
mod sroa : ScalarReplacementOfAggregates;
195198
mod strip_debuginfo : StripDebugInfo;
196-
mod unreachable_enum_branching : UnreachableEnumBranching;
197-
mod unreachable_prop : UnreachablePropagation;
199+
pub mod unreachable_enum_branching : UnreachableEnumBranching;
200+
pub mod unreachable_prop : UnreachablePropagation;
198201
mod validate : Validator;
199202
}
200203

compiler/rustc_mir_transform/src/remove_noop_landing_pads.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::patch::MirPatch;
99
/// A pass that removes noop landing pads and replaces jumps to them with
1010
/// `UnwindAction::Continue`. This is important because otherwise LLVM generates
1111
/// terrible code for these.
12-
pub(super) struct RemoveNoopLandingPads;
12+
pub struct RemoveNoopLandingPads;
1313

1414
impl<'tcx> crate::MirPass<'tcx> for RemoveNoopLandingPads {
1515
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {

compiler/rustc_mir_transform/src/remove_unneeded_drops.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use tracing::{debug, trace};
1111

1212
use super::simplify::simplify_cfg;
1313

14-
pub(super) struct RemoveUnneededDrops;
14+
pub struct RemoveUnneededDrops;
1515

1616
impl<'tcx> crate::MirPass<'tcx> for RemoveUnneededDrops {
1717
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

compiler/rustc_mir_transform/src/remove_zsts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_middle::mir::visit::*;
44
use rustc_middle::mir::*;
55
use rustc_middle::ty::{self, Ty, TyCtxt};
66

7-
pub(super) struct RemoveZsts;
7+
pub struct RemoveZsts;
88

99
impl<'tcx> crate::MirPass<'tcx> for RemoveZsts {
1010
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {

compiler/rustc_mir_transform/src/simplify.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use rustc_span::DUMMY_SP;
4242
use smallvec::SmallVec;
4343
use tracing::{debug, trace};
4444

45-
pub(super) enum SimplifyCfg {
45+
pub enum SimplifyCfg {
4646
Initial,
4747
PromoteConsts,
4848
RemoveFalseEdges,
@@ -54,6 +54,7 @@ pub(super) enum SimplifyCfg {
5454
Final,
5555
MakeShim,
5656
AfterUnreachableEnumBranching,
57+
Monomorphic,
5758
}
5859

5960
impl SimplifyCfg {
@@ -69,6 +70,7 @@ impl SimplifyCfg {
6970
SimplifyCfg::AfterUnreachableEnumBranching => {
7071
"SimplifyCfg-after-unreachable-enum-branching"
7172
}
73+
SimplifyCfg::Monomorphic => "SimplifyCfg-monomorphic",
7274
}
7375
}
7476
}
@@ -391,10 +393,11 @@ pub(super) fn remove_dead_blocks(body: &mut Body<'_>) {
391393
}
392394
}
393395

394-
pub(super) enum SimplifyLocals {
396+
pub enum SimplifyLocals {
395397
BeforeConstProp,
396398
AfterGVN,
397399
Final,
400+
Monomorphic,
398401
}
399402

400403
impl<'tcx> crate::MirPass<'tcx> for SimplifyLocals {
@@ -403,6 +406,7 @@ impl<'tcx> crate::MirPass<'tcx> for SimplifyLocals {
403406
SimplifyLocals::BeforeConstProp => "SimplifyLocals-before-const-prop",
404407
SimplifyLocals::AfterGVN => "SimplifyLocals-after-value-numbering",
405408
SimplifyLocals::Final => "SimplifyLocals-final",
409+
SimplifyLocals::Monomorphic => "SimplifyLocals-monomorphic",
406410
}
407411
}
408412

compiler/rustc_mir_transform/src/simplify_branches.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ use rustc_middle::mir::*;
22
use rustc_middle::ty::TyCtxt;
33
use tracing::trace;
44

5-
pub(super) enum SimplifyConstCondition {
5+
pub enum SimplifyConstCondition {
66
AfterConstProp,
77
Final,
8+
Monomorphic,
89
}
910

1011
/// A pass that replaces a branch with a goto when its condition is known.
@@ -13,6 +14,7 @@ impl<'tcx> crate::MirPass<'tcx> for SimplifyConstCondition {
1314
match self {
1415
SimplifyConstCondition::AfterConstProp => "SimplifyConstCondition-after-const-prop",
1516
SimplifyConstCondition::Final => "SimplifyConstCondition-final",
17+
SimplifyConstCondition::Monomorphic => "SimplifyConstCondition-monomorphic",
1618
}
1719
}
1820

compiler/rustc_mir_transform/src/unreachable_enum_branching.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use tracing::trace;
1313

1414
use crate::patch::MirPatch;
1515

16-
pub(super) struct UnreachableEnumBranching;
16+
pub struct UnreachableEnumBranching;
1717

1818
fn get_discriminant_local(terminator: &TerminatorKind<'_>) -> Option<Local> {
1919
if let TerminatorKind::SwitchInt { discr: Operand::Move(p), .. } = terminator {

compiler/rustc_mir_transform/src/unreachable_prop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_middle::ty::{self, TyCtxt};
1111

1212
use crate::patch::MirPatch;
1313

14-
pub(super) struct UnreachablePropagation;
14+
pub struct UnreachablePropagation;
1515

1616
impl crate::MirPass<'_> for UnreachablePropagation {
1717
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {

tests/codegen/intrinsics/transmute.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub unsafe fn check_bigger_array(x: [u32; 3]) -> [u32; 7] {
5656

5757
// CHECK-LABEL: @check_to_empty_array(
5858
#[no_mangle]
59-
#[custom_mir(dialect = "runtime", phase = "optimized")]
59+
#[custom_mir(dialect = "runtime", phase = "monomorphic")]
6060
pub unsafe fn check_to_empty_array(x: [u32; 5]) -> [u32; 0] {
6161
// CHECK-NOT: trap
6262
// CHECK: call void @llvm.trap
@@ -397,7 +397,7 @@ pub unsafe fn check_issue_109992(x: ()) -> [(); 1] {
397397

398398
// CHECK-LABEL: @check_unit_to_never(
399399
#[no_mangle]
400-
#[custom_mir(dialect = "runtime", phase = "optimized")]
400+
#[custom_mir(dialect = "runtime", phase = "monomorphic")]
401401
pub unsafe fn check_unit_to_never(x: ()) {
402402
// This uses custom MIR to avoid MIR optimizations having removed ZST ops.
403403

0 commit comments

Comments
 (0)