Skip to content

Commit 669d316

Browse files
committed
simplify down to one query per pass suite
1 parent 9c154a6 commit 669d316

File tree

6 files changed

+87
-126
lines changed

6 files changed

+87
-126
lines changed

src/librustc/mir/transform.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ pub enum MirSource {
3737
}
3838

3939
impl<'a, 'tcx> MirSource {
40+
pub fn from_local_def_id(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> MirSource {
41+
let id = tcx.hir.as_local_node_id(def_id).expect("mir source requires local def-id");
42+
Self::from_node(tcx, id)
43+
}
44+
4045
pub fn from_node(tcx: TyCtxt<'a, 'tcx, 'tcx>, id: NodeId) -> MirSource {
4146
use hir::*;
4247

@@ -169,12 +174,8 @@ impl<'a, 'tcx> Passes {
169174
self.pass_hooks.push(Rc::new(hook));
170175
}
171176

172-
pub fn len_passes(&self, suite: MirSuite) -> usize {
173-
self.suites[suite.0].len()
174-
}
175-
176-
pub fn pass(&self, suite: MirSuite, pass: MirPassIndex) -> &MirPass {
177-
&*self.suites[suite.0][pass.0]
177+
pub fn passes(&self, suite: MirSuite) -> &[Rc<MirPass>] {
178+
&self.suites[suite.0]
178179
}
179180

180181
pub fn hooks(&self) -> &[Rc<PassHook>] {

src/librustc/ty/maps.rs

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -404,18 +404,6 @@ impl<'tcx> QueryDescription for queries::is_item_mir_available<'tcx> {
404404
}
405405
}
406406

407-
impl<'tcx> QueryDescription for queries::mir_suite<'tcx> {
408-
fn describe(_: TyCtxt, (suite, _): (MirSuite, DefId)) -> String {
409-
format!("MIR suite #{}.*", suite.0)
410-
}
411-
}
412-
413-
impl<'tcx> QueryDescription for queries::mir_pass<'tcx> {
414-
fn describe(_: TyCtxt, (suite, pass_num, _): (MirSuite, MirPassIndex, DefId)) -> String {
415-
format!("MIR pass #{}.{}", suite.0, pass_num.0)
416-
}
417-
}
418-
419407
macro_rules! define_maps {
420408
(<$tcx:tt>
421409
$($(#[$attr:meta])*
@@ -798,27 +786,13 @@ define_maps! { <'tcx>
798786
/// the value isn't known except to the pass itself.
799787
[] mir_const_qualif: Mir(DefId) -> u8,
800788

801-
/// Performs the initial MIR construction. You almost certainly do not
802-
/// want to use this query, because its output is intended to be stolen
803-
/// immediately by the MIR passes below. Consider `optimized_mir` instead.
789+
/// Fetch the MIR for a given def-id up till the point where it is
790+
/// ready for const evaluation.
804791
///
805792
/// See the README for the `mir` module for details.
806-
[] mir_build: Mir(DefId) -> &'tcx Steal<mir::Mir<'tcx>>,
793+
[] mir_const: Mir(DefId) -> &'tcx Steal<mir::Mir<'tcx>>,
807794

808-
/// Fetch the MIR for a given def-id after the given set of passes has ben
809-
/// applied to it. This is mostly an "intermediate" query. Normally, you would
810-
/// prefer to use `optimized_mir(def_id)`, which will fetch the MIR after all
811-
/// optimizations and so forth.
812-
///
813-
/// See the README for the `mir` module for details.
814-
[] mir_suite: mir_suite((MirSuite, DefId)) -> &'tcx Steal<mir::Mir<'tcx>>,
815-
816-
/// Fetch the MIR for a given def-id after a given pass has been executed. This is
817-
/// **only** intended to be used by the `mir_suite` provider -- if you are using it
818-
/// manually, you're doing it wrong.
819-
///
820-
/// See the README for the `mir` module for details.
821-
[] mir_pass: mir_pass((MirSuite, MirPassIndex, DefId)) -> &'tcx Steal<mir::Mir<'tcx>>,
795+
[] mir_validated: Mir(DefId) -> &'tcx Steal<mir::Mir<'tcx>>,
822796

823797
/// MIR after our optimization passes have run. This is MIR that is ready
824798
/// for trans. This is also the only query that can fetch non-local MIR, at present.
@@ -921,11 +895,3 @@ fn const_eval_dep_node((def_id, _): (DefId, &Substs)) -> DepNode<DefId> {
921895
fn mir_keys(_: CrateNum) -> DepNode<DefId> {
922896
DepNode::MirKeys
923897
}
924-
925-
fn mir_suite((_suite, def_id): (MirSuite, DefId)) -> DepNode<DefId> {
926-
DepNode::Mir(def_id)
927-
}
928-
929-
fn mir_pass((_suite, _pass_num, def_id): (MirSuite, MirPassIndex, DefId)) -> DepNode<DefId> {
930-
DepNode::Mir(def_id)
931-
}

src/librustc_mir/build/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use rustc::mir::transform::MirSource;
2020
use rustc::mir::visit::MutVisitor;
2121
use rustc::traits::Reveal;
2222
use rustc::ty::{self, Ty, TyCtxt};
23-
use rustc::ty::steal::Steal;
2423
use rustc::ty::subst::Substs;
2524
use rustc::util::nodemap::NodeMap;
2625
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
@@ -33,7 +32,8 @@ use syntax::symbol::keywords;
3332
use syntax_pos::Span;
3433
use util as mir_util;
3534

36-
pub fn mir_build<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx Steal<Mir<'tcx>> {
35+
/// Construct the MIR for a given def-id.
36+
pub fn mir_build<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Mir<'tcx> {
3737
let id = tcx.hir.as_local_node_id(def_id).unwrap();
3838
let unsupported = || {
3939
span_bug!(tcx.hir.span(id), "can't build MIR for {:?}", def_id);
@@ -131,7 +131,7 @@ pub fn mir_build<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx
131131

132132
mir_util::dump_mir(tcx, None, "mir_map", &0, src, &mir);
133133

134-
tcx.alloc_steal_mir(mir)
134+
mir
135135
})
136136
}
137137

@@ -168,7 +168,7 @@ impl<'a, 'gcx: 'tcx, 'tcx> MutVisitor<'tcx> for GlobalizeMir<'a, 'gcx> {
168168
fn create_constructor_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
169169
ctor_id: ast::NodeId,
170170
v: &'tcx hir::VariantData)
171-
-> &'tcx Steal<Mir<'tcx>>
171+
-> Mir<'tcx>
172172
{
173173
let span = tcx.hir.span(ctor_id);
174174
if let hir::VariantData::Tuple(ref fields, ctor_id) = *v {
@@ -190,7 +190,7 @@ fn create_constructor_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
190190

191191
mir_util::dump_mir(tcx, None, "mir_map", &0, src, &mir);
192192

193-
tcx.alloc_steal_mir(mir)
193+
mir
194194
})
195195
} else {
196196
span_bug!(span, "attempting to create MIR for non-tuple variant {:?}", v);

src/librustc_mir/queries.rs

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,34 @@
1616
//! - `#[rustc_mir(graphviz="file.gv")]`
1717
//! - `#[rustc_mir(pretty="file.mir")]`
1818
19-
use rustc::hir::def_id::{CrateNum, LOCAL_CRATE};
20-
21-
use rustc::ty::TyCtxt;
19+
use build;
20+
use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
21+
use rustc::mir::Mir;
22+
use rustc::mir::transform::{MirSource, MIR_CONST, MIR_VALIDATED, MIR_OPTIMIZED};
23+
use rustc::ty::{self, TyCtxt};
2224
use rustc::ty::maps::Providers;
25+
use rustc::ty::steal::Steal;
2326
use rustc::hir;
2427
use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
2528
use rustc::util::nodemap::DefIdSet;
2629
use syntax::ast;
27-
use syntax_pos::Span;
30+
use syntax_pos::{DUMMY_SP, Span};
31+
use transform;
2832

2933
use std::rc::Rc;
3034

3135
pub fn provide(providers: &mut Providers) {
32-
use build::mir_build;
3336
*providers = Providers {
34-
mir_build,
3537
mir_keys,
38+
mir_const,
39+
mir_validated,
40+
optimized_mir,
3641
..*providers
3742
};
3843
}
3944

45+
/// Finds the full set of def-ids within the current crate that have
46+
/// MIR associated with them.
4047
fn mir_keys<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, krate: CrateNum)
4148
-> Rc<DefIdSet> {
4249
assert_eq!(krate, LOCAL_CRATE);
@@ -75,3 +82,31 @@ fn mir_keys<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, krate: CrateNum)
7582

7683
Rc::new(set)
7784
}
85+
86+
fn mir_const<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx Steal<Mir<'tcx>> {
87+
let mut mir = build::mir_build(tcx, def_id);
88+
let source = MirSource::from_local_def_id(tcx, def_id);
89+
transform::run_suite(tcx, source, MIR_CONST, &mut mir);
90+
tcx.alloc_steal_mir(mir)
91+
}
92+
93+
fn mir_validated<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx Steal<Mir<'tcx>> {
94+
let source = MirSource::from_local_def_id(tcx, def_id);
95+
if let MirSource::Const(_) = source {
96+
// Ensure that we compute the `mir_const_qualif` for constants at
97+
// this point, before we steal the mir-const result. We don't
98+
// directly need the result or `mir_const_qualif`, so we can just force it.
99+
ty::queries::mir_const_qualif::force(tcx, DUMMY_SP, def_id);
100+
}
101+
102+
let mut mir = tcx.mir_const(def_id).steal();
103+
transform::run_suite(tcx, source, MIR_VALIDATED, &mut mir);
104+
tcx.alloc_steal_mir(mir)
105+
}
106+
107+
fn optimized_mir<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx Mir<'tcx> {
108+
let mut mir = tcx.mir_validated(def_id).steal();
109+
let source = MirSource::from_local_def_id(tcx, def_id);
110+
transform::run_suite(tcx, source, MIR_OPTIMIZED, &mut mir);
111+
tcx.alloc_mir(mir)
112+
}

src/librustc_mir/transform/mod.rs

Lines changed: 16 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use rustc::hir::def_id::DefId;
1211
use rustc::mir::Mir;
13-
use rustc::mir::transform::{MirPassIndex, MirSuite, MirSource, MIR_VALIDATED, MIR_OPTIMIZED};
14-
use rustc::ty::{self, TyCtxt};
15-
use rustc::ty::steal::Steal;
12+
use rustc::mir::transform::{MirPassIndex, MirSuite, MirSource};
13+
use rustc::ty::TyCtxt;
1614
use rustc::ty::maps::Providers;
17-
use syntax_pos::DUMMY_SP;
1815

1916
pub mod simplify_branches;
2017
pub mod simplify;
@@ -31,77 +28,31 @@ pub mod copy_prop;
3128
pub mod inline;
3229
pub mod interprocedural;
3330

34-
pub fn provide(providers: &mut Providers) {
31+
pub(crate) fn provide(providers: &mut Providers) {
3532
self::qualify_consts::provide(providers);
3633
*providers = Providers {
37-
optimized_mir,
38-
mir_suite,
39-
mir_pass,
4034
..*providers
4135
};
4236
}
4337

44-
fn optimized_mir<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx Mir<'tcx> {
45-
let mir = tcx.mir_suite((MIR_OPTIMIZED, def_id)).steal();
46-
tcx.alloc_mir(mir)
47-
}
48-
49-
fn mir_suite<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
50-
(suite, def_id): (MirSuite, DefId))
51-
-> &'tcx Steal<Mir<'tcx>>
38+
pub(crate) fn run_suite<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
39+
source: MirSource,
40+
suite: MirSuite,
41+
mir: &mut Mir<'tcx>)
5242
{
53-
let passes = &tcx.mir_passes;
54-
55-
if suite == MIR_VALIDATED {
56-
let id = tcx.hir.as_local_node_id(def_id).expect("mir source requires local def-id");
57-
let source = MirSource::from_node(tcx, id);
58-
if let MirSource::Const(_) = source {
59-
// Ensure that we compute the `mir_const_qualif` for
60-
// constants at this point, before we do any further
61-
// optimization (and before we steal the previous
62-
// MIR). We don't directly need the result, so we can
63-
// just force it.
64-
ty::queries::mir_const_qualif::force(tcx, DUMMY_SP, def_id);
65-
}
66-
}
67-
68-
let len = passes.len_passes(suite);
69-
assert!(len > 0, "no passes in {:?}", suite);
70-
tcx.mir_pass((suite, MirPassIndex(len - 1), def_id))
71-
}
43+
let passes = tcx.mir_passes.passes(suite);
7244

73-
fn mir_pass<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
74-
(suite, pass_num, def_id): (MirSuite, MirPassIndex, DefId))
75-
-> &'tcx Steal<Mir<'tcx>>
76-
{
77-
let passes = &tcx.mir_passes;
78-
let pass = passes.pass(suite, pass_num);
45+
for (pass, index) in passes.iter().zip(0..) {
46+
let pass_num = MirPassIndex(index);
7947

80-
let id = tcx.hir.as_local_node_id(def_id).expect("mir source requires local def-id");
81-
let source = MirSource::from_node(tcx, id);
82-
83-
let mut mir = {
84-
let MirSuite(suite) = suite;
85-
let MirPassIndex(pass_num) = pass_num;
86-
if pass_num > 0 {
87-
tcx.mir_pass((MirSuite(suite), MirPassIndex(pass_num - 1), def_id)).steal()
88-
} else if suite > 0 {
89-
tcx.mir_suite((MirSuite(suite - 1), def_id)).steal()
90-
} else {
91-
tcx.mir_build(def_id).steal()
48+
for hook in tcx.mir_passes.hooks() {
49+
hook.on_mir_pass(tcx, suite, pass_num, &pass.name(), source, &mir, false);
9250
}
93-
};
94-
95-
for hook in passes.hooks() {
96-
hook.on_mir_pass(tcx, suite, pass_num, &pass.name(), source, &mir, false);
97-
}
9851

99-
pass.run_pass(tcx, source, &mut mir);
52+
pass.run_pass(tcx, source, mir);
10053

101-
for hook in passes.hooks() {
102-
hook.on_mir_pass(tcx, suite, pass_num, &pass.name(), source, &mir, true);
54+
for hook in tcx.mir_passes.hooks() {
55+
hook.on_mir_pass(tcx, suite, pass_num, &pass.name(), source, &mir, true);
56+
}
10357
}
104-
105-
tcx.alloc_steal_mir(mir)
10658
}
107-

src/librustc_mir/transform/qualify_consts.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc::ty::cast::CastTy;
2626
use rustc::ty::maps::Providers;
2727
use rustc::mir::*;
2828
use rustc::mir::traversal::ReversePostorder;
29-
use rustc::mir::transform::{MirPass, MirSource, MIR_CONST};
29+
use rustc::mir::transform::{MirPass, MirSource};
3030
use rustc::mir::visit::{LvalueContext, Visitor};
3131
use rustc::middle::lang_items;
3232
use syntax::abi::Abi;
@@ -918,13 +918,21 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
918918
}
919919

920920
pub fn provide(providers: &mut Providers) {
921-
providers.mir_const_qualif = qualify_const_item;
921+
*providers = Providers {
922+
mir_const_qualif,
923+
..*providers
924+
};
922925
}
923926

924-
fn qualify_const_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
925-
def_id: DefId)
926-
-> u8 {
927-
let mir = &tcx.mir_suite((MIR_CONST, def_id)).borrow();
927+
fn mir_const_qualif<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
928+
def_id: DefId)
929+
-> u8 {
930+
// NB: This `borrow()` is guaranteed to be valid (i.e., the value
931+
// cannot yet be stolen), because `mir_validated()`, which steals
932+
// from `mir_const(), forces this query to execute before
933+
// performing the steal.
934+
let mir = &tcx.mir_const(def_id).borrow();
935+
928936
if mir.return_ty.references_error() {
929937
return Qualif::NOT_CONST.bits();
930938
}

0 commit comments

Comments
 (0)