Skip to content

Commit a9b879b

Browse files
committed
Move critical edge splitting to codegen.
1 parent 8c66bef commit a9b879b

File tree

11 files changed

+31
-17
lines changed

11 files changed

+31
-17
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3572,6 +3572,7 @@ dependencies = [
35723572
"rustc_macros",
35733573
"rustc_metadata",
35743574
"rustc_middle",
3575+
"rustc_mir_transform",
35753576
"rustc_query_system",
35763577
"rustc_serialize",
35773578
"rustc_session",

compiler/rustc_codegen_ssa/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ rustc_index = { path = "../rustc_index" }
3030
rustc_macros = { path = "../rustc_macros" }
3131
rustc_metadata = { path = "../rustc_metadata" }
3232
rustc_middle = { path = "../rustc_middle" }
33+
rustc_mir_transform = { path = "../rustc_mir_transform" }
3334
rustc_query_system = { path = "../rustc_query_system" }
3435
rustc_serialize = { path = "../rustc_serialize" }
3536
rustc_session = { path = "../rustc_session" }

compiler/rustc_codegen_ssa/src/mir/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ 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};
1011
use rustc_target::callconv::{FnAbi, PassMode};
1112
use tracing::{debug, instrument};
1213

@@ -167,6 +168,18 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
167168
ty::TypingEnv::fully_monomorphized(),
168169
ty::EarlyBinder::bind(mir.clone()),
169170
);
171+
pass_manager::run_passes(
172+
cx.tcx(),
173+
&mut mir,
174+
&[
175+
// Some cleanup necessary at least for LLVM and potentially other codegen backends.
176+
&add_call_guards::CriticalCallEdges,
177+
// Dump the end result for testing and debugging purposes.
178+
&dump_mir::Marker("Monomorphic"),
179+
],
180+
Some(mir::MirPhase::Runtime(mir::RuntimePhase::Monomorphic)),
181+
pass_manager::Optimizations::Allowed,
182+
);
170183
if tcx.features().ergonomic_clones() {
171184
mir = optimize_use_clone::<Bx>(cx, mir);
172185
}

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ impl MirPhase {
103103
MirPhase::Runtime(RuntimePhase::Initial) => "runtime",
104104
MirPhase::Runtime(RuntimePhase::PostCleanup) => "runtime-post-cleanup",
105105
MirPhase::Runtime(RuntimePhase::Optimized) => "runtime-optimized",
106+
MirPhase::Runtime(RuntimePhase::Monomorphic) => "runtime-monomorphic",
106107
}
107108
}
108109

@@ -154,6 +155,7 @@ impl RuntimePhase {
154155
"initial" => Self::Initial,
155156
"post_cleanup" | "post-cleanup" | "postcleanup" => Self::PostCleanup,
156157
"optimized" => Self::Optimized,
158+
"monomorphic" => Self::Monomorphic,
157159
_ => bug!("Unknown runtime phase: '{}'", phase),
158160
}
159161
}

compiler/rustc_middle/src/mir/syntax.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ pub enum RuntimePhase {
143143
/// * [`ProjectionElem::Deref`] of `Box`
144144
PostCleanup = 1,
145145
Optimized = 2,
146+
/// This corresponds to monomorphised MIR that is being processed by codegen.
147+
Monomorphic = 3,
146148
}
147149

148150
///////////////////////////////////////////////////////////////////////////

compiler/rustc_mir_transform/src/add_call_guards.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ use rustc_middle::ty::TyCtxt;
44
use tracing::debug;
55

66
#[derive(PartialEq)]
7-
pub(super) enum AddCallGuards {
7+
pub enum AddCallGuards {
88
AllCallEdges,
99
CriticalCallEdges,
1010
}
11-
pub(super) use self::AddCallGuards::*;
11+
pub use self::AddCallGuards::*;
1212

1313
/**
1414
* Breaks outgoing critical edges for call terminators in the MIR.

compiler/rustc_mir_transform/src/dump_mir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_middle::mir::{Body, write_mir_pretty};
77
use rustc_middle::ty::TyCtxt;
88
use rustc_session::config::{OutFileName, OutputType};
99

10-
pub(super) struct Marker(pub &'static str);
10+
pub struct Marker(pub &'static str);
1111

1212
impl<'tcx> crate::MirPass<'tcx> for Marker {
1313
fn name(&self) -> &'static str {

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use rustc_span::{DUMMY_SP, sym};
3535
use tracing::debug;
3636

3737
#[macro_use]
38-
mod pass_manager;
38+
pub mod pass_manager;
3939

4040
use std::sync::LazyLock;
4141

@@ -110,7 +110,7 @@ macro_rules! declare_passes {
110110

111111
declare_passes! {
112112
mod abort_unwinding_calls : AbortUnwindingCalls;
113-
mod add_call_guards : AddCallGuards { AllCallEdges, CriticalCallEdges };
113+
pub mod add_call_guards : AddCallGuards { AllCallEdges, CriticalCallEdges };
114114
mod add_moves_for_packed_drops : AddMovesForPackedDrops;
115115
mod add_retag : AddRetag;
116116
mod add_subtyping_projections : Subtyper;
@@ -722,8 +722,6 @@ pub(crate) fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'
722722
&simplify::SimplifyLocals::Final,
723723
&multiple_return_terminators::MultipleReturnTerminators,
724724
&large_enums::EnumSizeOpt { discrepancy: 128 },
725-
// Some cleanup necessary at least for LLVM and potentially other codegen backends.
726-
&add_call_guards::CriticalCallEdges,
727725
// Cleanup for human readability, off by default.
728726
&prettify::ReorderBasicBlocks,
729727
&prettify::ReorderLocals,

compiler/rustc_mir_transform/src/pass_manager.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const fn c_name(name: &'static str) -> &'static str {
6060
/// A streamlined trait that you can implement to create a pass; the
6161
/// pass will be named after the type, and it will consist of a main
6262
/// loop that goes over each available MIR and applies `run_pass`.
63-
pub(super) trait MirPass<'tcx> {
63+
pub trait MirPass<'tcx> {
6464
fn name(&self) -> &'static str {
6565
// FIXME(const-hack) Simplify the implementation once more `str` methods get const-stable.
6666
// See copypaste in `MirLint`
@@ -172,7 +172,7 @@ where
172172
///
173173
/// [required]: MirPass::is_required
174174
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
175-
pub(crate) enum Optimizations {
175+
pub enum Optimizations {
176176
Suppressed,
177177
Allowed,
178178
}
@@ -189,7 +189,7 @@ pub(super) fn run_passes_no_validate<'tcx>(
189189
}
190190

191191
/// The optional `phase_change` is applied after executing all the passes, if present
192-
pub(super) fn run_passes<'tcx>(
192+
pub fn run_passes<'tcx>(
193193
tcx: TyCtxt<'tcx>,
194194
body: &mut Body<'tcx>,
195195
passes: &[&dyn MirPass<'tcx>],

compiler/rustc_mir_transform/src/validate.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,8 @@ impl<'a, 'tcx> Visitor<'tcx> for CfgChecker<'a, 'tcx> {
390390

391391
// The code generation assumes that there are no critical call edges. The
392392
// assumption is used to simplify inserting code that should be executed along
393-
// the return edge from the call. FIXME(tmiasko): Since this is a strictly code
394-
// generation concern, the code generation should be responsible for handling
395-
// it.
396-
if self.body.phase >= MirPhase::Runtime(RuntimePhase::Optimized)
393+
// the return edge from the call.
394+
if self.body.phase >= MirPhase::Runtime(RuntimePhase::Monomorphic)
397395
&& self.is_critical_call_edge(target, unwind)
398396
{
399397
self.fail(

0 commit comments

Comments
 (0)