Skip to content

Commit 86206f2

Browse files
committed
rustc: move the MIR pass infrastructure and list to rustc_mir.
1 parent 9b53f0a commit 86206f2

25 files changed

+201
-215
lines changed

src/librustc/mir/transform.rs

Lines changed: 1 addition & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@
1313
use hir;
1414
use hir::def_id::DefId;
1515
use hir::map::DefPathData;
16-
use mir::{Mir, Promoted};
16+
use mir::Promoted;
1717
use ty::TyCtxt;
18-
use std::rc::Rc;
1918
use syntax::ast::NodeId;
2019

21-
use std::borrow::Cow;
22-
2320
/// Where a specific Mir comes from.
2421
#[derive(Debug, Copy, Clone)]
2522
pub enum MirSource {
@@ -79,112 +76,3 @@ impl<'a, 'gcx, 'tcx> MirSource {
7976
}
8077
}
8178
}
82-
83-
/// Generates a default name for the pass based on the name of the
84-
/// type `T`.
85-
pub fn default_name<T: ?Sized>() -> Cow<'static, str> {
86-
let name = unsafe { ::std::intrinsics::type_name::<T>() };
87-
if let Some(tail) = name.rfind(":") {
88-
Cow::from(&name[tail+1..])
89-
} else {
90-
Cow::from(name)
91-
}
92-
}
93-
94-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
95-
pub struct MirSuite(pub usize);
96-
97-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
98-
pub struct MirPassIndex(pub usize);
99-
100-
/// A pass hook is invoked both before and after each pass executes.
101-
/// This is primarily used to dump MIR for debugging.
102-
///
103-
/// You can tell whether this is before or after by inspecting the
104-
/// `mir` parameter -- before the pass executes, it will be `None` (in
105-
/// which case you can inspect the MIR from previous pass by executing
106-
/// `mir_cx.read_previous_mir()`); after the pass executes, it will be
107-
/// `Some()` with the result of the pass (in which case the output
108-
/// from the previous pass is most likely stolen, so you would not
109-
/// want to try and access it). If the pass is interprocedural, then
110-
/// the hook will be invoked once per output.
111-
pub trait PassHook {
112-
fn on_mir_pass<'a, 'tcx: 'a>(&self,
113-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
114-
suite: MirSuite,
115-
pass_num: MirPassIndex,
116-
pass_name: &str,
117-
source: MirSource,
118-
mir: &Mir<'tcx>,
119-
is_after: bool);
120-
}
121-
122-
/// The full suite of types that identifies a particular
123-
/// application of a pass to a def-id.
124-
pub type PassId = (MirSuite, MirPassIndex, DefId);
125-
126-
/// A streamlined trait that you can implement to create a pass; the
127-
/// pass will be named after the type, and it will consist of a main
128-
/// loop that goes over each available MIR and applies `run_pass`.
129-
pub trait MirPass {
130-
fn name<'a>(&'a self) -> Cow<'a, str> {
131-
default_name::<Self>()
132-
}
133-
134-
fn run_pass<'a, 'tcx>(&self,
135-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
136-
source: MirSource,
137-
mir: &mut Mir<'tcx>);
138-
}
139-
140-
/// A manager for MIR passes.
141-
///
142-
/// FIXME(#41712) -- it is unclear whether we should have this struct.
143-
#[derive(Clone)]
144-
pub struct Passes {
145-
pass_hooks: Vec<Rc<PassHook>>,
146-
suites: Vec<Vec<Rc<MirPass>>>,
147-
}
148-
149-
/// The number of "pass suites" that we have:
150-
///
151-
/// - ready for constant evaluation
152-
/// - unopt
153-
/// - optimized
154-
pub const MIR_SUITES: usize = 3;
155-
156-
/// Run the passes we need to do constant qualification and evaluation.
157-
pub const MIR_CONST: MirSuite = MirSuite(0);
158-
159-
/// Run the passes we need to consider the MIR validated and ready for borrowck etc.
160-
pub const MIR_VALIDATED: MirSuite = MirSuite(1);
161-
162-
/// Run the passes we need to consider the MIR *optimized*.
163-
pub const MIR_OPTIMIZED: MirSuite = MirSuite(2);
164-
165-
impl<'a, 'tcx> Passes {
166-
pub fn new() -> Passes {
167-
Passes {
168-
pass_hooks: Vec::new(),
169-
suites: (0..MIR_SUITES).map(|_| Vec::new()).collect(),
170-
}
171-
}
172-
173-
/// Pushes a built-in pass.
174-
pub fn push_pass<T: MirPass + 'static>(&mut self, suite: MirSuite, pass: T) {
175-
self.suites[suite.0].push(Rc::new(pass));
176-
}
177-
178-
/// Pushes a pass hook.
179-
pub fn push_hook<T: PassHook + 'static>(&mut self, hook: T) {
180-
self.pass_hooks.push(Rc::new(hook));
181-
}
182-
183-
pub fn passes(&self, suite: MirSuite) -> &[Rc<MirPass>] {
184-
&self.suites[suite.0]
185-
}
186-
187-
pub fn hooks(&self) -> &[Rc<PassHook>] {
188-
&self.pass_hooks
189-
}
190-
}

src/librustc/ty/context.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ use middle::lang_items;
3131
use middle::resolve_lifetime::{self, ObjectLifetimeDefault};
3232
use middle::stability;
3333
use mir::Mir;
34-
use mir::transform::Passes;
3534
use ty::subst::{Kind, Substs};
3635
use ty::ReprOptions;
3736
use traits;
@@ -882,8 +881,6 @@ pub struct GlobalCtxt<'tcx> {
882881

883882
pub maps: maps::Maps<'tcx>,
884883

885-
pub mir_passes: Rc<Passes>,
886-
887884
// Records the free variables refrenced by every closure
888885
// expression. Do not track deps for this, just recompute it from
889886
// scratch every time.
@@ -1055,7 +1052,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
10551052
cstore: &'tcx CrateStore,
10561053
local_providers: ty::maps::Providers<'tcx>,
10571054
extern_providers: ty::maps::Providers<'tcx>,
1058-
mir_passes: Rc<Passes>,
10591055
arenas: &'tcx GlobalArenas<'tcx>,
10601056
arena: &'tcx DroplessArena,
10611057
resolutions: ty::Resolutions,
@@ -1172,7 +1168,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
11721168
hir,
11731169
def_path_hash_to_def_id,
11741170
maps: maps::Maps::new(providers),
1175-
mir_passes,
11761171
rcache: RefCell::new(FxHashMap()),
11771172
selection_cache: traits::SelectionCache::new(),
11781173
evaluation_cache: traits::EvaluationCache::new(),

src/librustc/ty/maps/keys.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
//! Defines the set of legal keys that can be used in queries.
1212
1313
use hir::def_id::{CrateNum, DefId, LOCAL_CRATE, DefIndex};
14-
use mir::transform::{MirSuite, MirPassIndex};
1514
use ty::{self, Ty, TyCtxt};
1615
use ty::subst::Substs;
1716
use ty::fast_reject::SimplifiedType;
@@ -116,24 +115,6 @@ impl<'tcx> Key for (DefId, &'tcx Substs<'tcx>) {
116115
}
117116
}
118117

119-
impl Key for (MirSuite, DefId) {
120-
fn map_crate(&self) -> CrateNum {
121-
self.1.map_crate()
122-
}
123-
fn default_span(&self, tcx: TyCtxt) -> Span {
124-
self.1.default_span(tcx)
125-
}
126-
}
127-
128-
impl Key for (MirSuite, MirPassIndex, DefId) {
129-
fn map_crate(&self) -> CrateNum {
130-
self.2.map_crate()
131-
}
132-
fn default_span(&self, tcx: TyCtxt) -> Span {
133-
self.2.default_span(tcx)
134-
}
135-
}
136-
137118
impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
138119
fn map_crate(&self) -> CrateNum {
139120
self.1.def_id().krate

src/librustc_driver/driver.rs

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use rustc::lint;
2222
use rustc::middle::{self, stability, reachable};
2323
use rustc::middle::cstore::CrateStore;
2424
use rustc::middle::privacy::AccessLevels;
25-
use rustc::mir::transform::{MIR_CONST, MIR_VALIDATED, MIR_OPTIMIZED, Passes};
2625
use rustc::ty::{self, TyCtxt, Resolutions, GlobalArenas};
2726
use rustc::traits;
2827
use rustc::util::common::{ErrorReported, time};
@@ -989,63 +988,12 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
989988
// FIXME(eddyb) get rid of this once we replace const_eval with miri.
990989
rustc_const_eval::provide(&mut extern_providers);
991990

992-
// Setup the MIR passes that we want to run.
993-
let mut passes = Passes::new();
994-
passes.push_hook(mir::transform::dump_mir::DumpMir);
995-
996-
// Remove all `EndRegion` statements that are not involved in borrows.
997-
passes.push_pass(MIR_CONST, mir::transform::clean_end_regions::CleanEndRegions);
998-
999-
// What we need to do constant evaluation.
1000-
passes.push_pass(MIR_CONST, mir::transform::simplify::SimplifyCfg::new("initial"));
1001-
passes.push_pass(MIR_CONST, mir::transform::type_check::TypeckMir);
1002-
passes.push_pass(MIR_CONST, mir::transform::rustc_peek::SanityCheck);
1003-
1004-
// We compute "constant qualifications" between MIR_CONST and MIR_VALIDATED.
1005-
1006-
// What we need to run borrowck etc.
1007-
1008-
passes.push_pass(MIR_VALIDATED, mir::transform::qualify_consts::QualifyAndPromoteConstants);
1009-
passes.push_pass(MIR_VALIDATED, mir::transform::simplify::SimplifyCfg::new("qualify-consts"));
1010-
1011-
// borrowck runs between MIR_VALIDATED and MIR_OPTIMIZED.
1012-
1013-
passes.push_pass(MIR_OPTIMIZED, mir::transform::no_landing_pads::NoLandingPads);
1014-
passes.push_pass(MIR_OPTIMIZED,
1015-
mir::transform::simplify_branches::SimplifyBranches::new("initial"));
1016-
1017-
// These next passes must be executed together
1018-
passes.push_pass(MIR_OPTIMIZED, mir::transform::add_call_guards::CriticalCallEdges);
1019-
passes.push_pass(MIR_OPTIMIZED, mir::transform::elaborate_drops::ElaborateDrops);
1020-
passes.push_pass(MIR_OPTIMIZED, mir::transform::no_landing_pads::NoLandingPads);
1021-
// AddValidation needs to run after ElaborateDrops and before EraseRegions, and it needs
1022-
// an AllCallEdges pass right before it.
1023-
passes.push_pass(MIR_OPTIMIZED, mir::transform::add_call_guards::AllCallEdges);
1024-
passes.push_pass(MIR_OPTIMIZED, mir::transform::add_validation::AddValidation);
1025-
passes.push_pass(MIR_OPTIMIZED, mir::transform::simplify::SimplifyCfg::new("elaborate-drops"));
1026-
// No lifetime analysis based on borrowing can be done from here on out.
1027-
1028-
// From here on out, regions are gone.
1029-
passes.push_pass(MIR_OPTIMIZED, mir::transform::erase_regions::EraseRegions);
1030-
1031-
// Optimizations begin.
1032-
passes.push_pass(MIR_OPTIMIZED, mir::transform::inline::Inline);
1033-
passes.push_pass(MIR_OPTIMIZED, mir::transform::instcombine::InstCombine);
1034-
passes.push_pass(MIR_OPTIMIZED, mir::transform::deaggregator::Deaggregator);
1035-
passes.push_pass(MIR_OPTIMIZED, mir::transform::copy_prop::CopyPropagation);
1036-
passes.push_pass(MIR_OPTIMIZED, mir::transform::simplify::SimplifyLocals);
1037-
1038-
passes.push_pass(MIR_OPTIMIZED, mir::transform::generator::StateTransform);
1039-
passes.push_pass(MIR_OPTIMIZED, mir::transform::add_call_guards::CriticalCallEdges);
1040-
passes.push_pass(MIR_OPTIMIZED, mir::transform::dump_mir::Marker("PreTrans"));
1041-
1042991
let (tx, rx) = mpsc::channel();
1043992

1044993
TyCtxt::create_and_enter(sess,
1045994
cstore,
1046995
local_providers,
1047996
extern_providers,
1048-
Rc::new(passes),
1049997
arenas,
1050998
arena,
1051999
resolutions,

src/librustc_driver/test.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use rustc::infer::{self, InferOk, InferResult};
2828
use rustc::infer::type_variable::TypeVariableOrigin;
2929
use rustc_metadata::cstore::CStore;
3030
use rustc::hir::map as hir_map;
31-
use rustc::mir::transform::Passes;
3231
use rustc::session::{self, config};
3332
use rustc::session::config::{OutputFilenames, OutputTypes};
3433
use rustc_trans_utils::trans_crate::TransCrate;
@@ -151,7 +150,6 @@ fn test_env<F>(source_string: &str,
151150
&*cstore,
152151
ty::maps::Providers::default(),
153152
ty::maps::Providers::default(),
154-
Rc::new(Passes::new()),
155153
&arenas,
156154
&arena,
157155
resolutions,

src/librustc_mir/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Rust MIR: a lowered representation of Rust. Also: an experiment!
2020
#![feature(box_syntax)]
2121
#![feature(conservative_impl_trait)]
2222
#![feature(const_fn)]
23+
#![feature(core_intrinsics)]
2324
#![feature(i128_type)]
2425
#![feature(rustc_diagnostic_macros)]
2526
#![feature(placement_in_syntax)]

src/librustc_mir/transform/add_call_guards.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
use rustc::ty::TyCtxt;
1212
use rustc::mir::*;
13-
use rustc::mir::transform::{MirPass, MirSource};
13+
use rustc::mir::transform::MirSource;
1414
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
15+
use transform::MirPass;
1516

1617
#[derive(PartialEq)]
1718
pub enum AddCallGuards {

src/librustc_mir/transform/add_validation.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
use rustc::ty::{self, TyCtxt, RegionKind};
1818
use rustc::hir;
1919
use rustc::mir::*;
20-
use rustc::mir::transform::{MirPass, MirSource};
20+
use rustc::mir::transform::MirSource;
2121
use rustc::middle::region;
22+
use transform::MirPass;
2223

2324
pub struct AddValidation;
2425

src/librustc_mir/transform/clean_end_regions.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
use rustc_data_structures::fx::FxHashSet;
2323

2424
use rustc::middle::region;
25-
use rustc::mir::transform::{MirPass, MirSource};
25+
use rustc::mir::transform::MirSource;
2626
use rustc::mir::{BasicBlock, Location, Mir, Rvalue, Statement, StatementKind};
2727
use rustc::mir::visit::{MutVisitor, Visitor, TyContext};
2828
use rustc::ty::{Ty, RegionKind, TyCtxt};
29+
use transform::MirPass;
2930

3031
pub struct CleanEndRegions;
3132

src/librustc_mir/transform/copy_prop.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@
3030
//! future.
3131
3232
use rustc::mir::{Constant, Local, LocalKind, Location, Lvalue, Mir, Operand, Rvalue, StatementKind};
33-
use rustc::mir::transform::{MirPass, MirSource};
33+
use rustc::mir::transform::MirSource;
3434
use rustc::mir::visit::MutVisitor;
3535
use rustc::ty::TyCtxt;
36+
use transform::MirPass;
3637
use util::def_use::DefUseAnalysis;
3738

3839
pub struct CopyPropagation;

src/librustc_mir/transform/deaggregator.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
use rustc::ty::TyCtxt;
1212
use rustc::mir::*;
13-
use rustc::mir::transform::{MirPass, MirSource};
13+
use rustc::mir::transform::MirSource;
1414
use rustc_data_structures::indexed_vec::Idx;
15+
use transform::MirPass;
1516

1617
pub struct Deaggregator;
1718

src/librustc_mir/transform/dump_mir.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ use std::fs::File;
1616
use std::io;
1717

1818
use rustc::mir::Mir;
19-
use rustc::mir::transform::{MirPass, MirPassIndex, MirSource, MirSuite, PassHook};
19+
use rustc::mir::transform::MirSource;
2020
use rustc::session::config::{OutputFilenames, OutputType};
2121
use rustc::ty::TyCtxt;
22+
use transform::{MirPass, MirPassIndex, MirSuite, PassHook};
2223
use util as mir_util;
2324

2425
pub struct Marker(pub &'static str);

src/librustc_mir/transform/elaborate_drops.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ use dataflow::MoveDataParamEnv;
1717
use dataflow;
1818
use rustc::ty::{self, TyCtxt};
1919
use rustc::mir::*;
20-
use rustc::mir::transform::{MirPass, MirSource};
20+
use rustc::mir::transform::MirSource;
2121
use rustc::middle::const_val::ConstVal;
2222
use rustc::util::nodemap::FxHashMap;
2323
use rustc_data_structures::indexed_set::IdxSetBuf;
2424
use rustc_data_structures::indexed_vec::Idx;
25+
use transform::MirPass;
2526
use util::patch::MirPatch;
2627
use util::elaborate_drops::{DropFlagState, Unwind, elaborate_drop};
2728
use util::elaborate_drops::{DropElaborator, DropStyle, DropFlagMode};

src/librustc_mir/transform/erase_regions.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ use rustc::ty::subst::Substs;
1818
use rustc::ty::{self, Ty, TyCtxt};
1919
use rustc::mir::*;
2020
use rustc::mir::visit::{MutVisitor, TyContext};
21-
use rustc::mir::transform::{MirPass, MirSource};
21+
use rustc::mir::transform::MirSource;
22+
use transform::MirPass;
2223

2324
struct EraseRegionsVisitor<'a, 'tcx: 'a> {
2425
tcx: TyCtxt<'a, 'tcx, 'tcx>,

0 commit comments

Comments
 (0)