Skip to content

Commit

Permalink
Add register_opt_mir_pass to let plugin register mir passes before th…
Browse files Browse the repository at this point in the history
…e Trans pass.
  • Loading branch information
nbp committed Aug 3, 2017
1 parent 37c7d0e commit 9e0d95e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/librustc/mir/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::rc::Rc;
use syntax::ast::NodeId;

use std::borrow::Cow;
use std::mem;

/// Where a specific Mir comes from.
#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -180,6 +181,11 @@ impl<'a, 'tcx> Passes {
&self.suites[suite.0]
}

/// Pushes the list of plugin passes in the list of passes.
pub fn push_passes(&mut self, suite: MirSuite, passes: &mut Vec<Rc<MirPass>>) {
self.suites[suite.0].extend(mem::replace(passes, vec![]));
}

pub fn hooks(&self) -> &[Rc<PassHook>] {
&self.pass_hooks
}
Expand Down
3 changes: 3 additions & 0 deletions src/librustc/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use syntax::{ast, codemap};
use syntax::feature_gate::AttributeType;
use syntax_pos::{Span, MultiSpan};

use mir::transform::MirPass;
use rustc_back::{LinkerFlavor, PanicStrategy};
use rustc_back::target::Target;
use rustc_data_structures::flock;
Expand Down Expand Up @@ -85,6 +86,7 @@ pub struct Session {
/// in order to avoid redundantly verbose output (Issue #24690).
pub one_time_diagnostics: RefCell<FxHashSet<(lint::LintId, Option<Span>, String)>>,
pub plugin_llvm_passes: RefCell<Vec<String>>,
pub opt_mir_passes: RefCell<Vec<Rc<MirPass>>>,
pub plugin_attributes: RefCell<Vec<(String, AttributeType)>>,
pub crate_types: RefCell<Vec<config::CrateType>>,
pub dependency_formats: RefCell<dependency_format::Dependencies>,
Expand Down Expand Up @@ -711,6 +713,7 @@ pub fn build_session_(sopts: config::Options,
lints: RefCell::new(lint::LintTable::new()),
one_time_diagnostics: RefCell::new(FxHashSet()),
plugin_llvm_passes: RefCell::new(Vec::new()),
opt_mir_passes: RefCell::new(Vec::new()),
plugin_attributes: RefCell::new(Vec::new()),
crate_types: RefCell::new(Vec::new()),
dependency_formats: RefCell::new(FxHashMap()),
Expand Down
9 changes: 7 additions & 2 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,8 @@ pub fn phase_2_configure_and_expand<F>(sess: &Session,

let whitelisted_legacy_custom_derives = registry.take_whitelisted_custom_derives();
let Registry { syntax_exts, early_lint_passes, late_lint_passes, lint_groups,
llvm_passes, attributes, .. } = registry;
llvm_passes, attributes, opt_mir_passes,
.. } = registry;

sess.track_errors(|| {
let mut ls = sess.lint_store.borrow_mut();
Expand All @@ -634,6 +635,7 @@ pub fn phase_2_configure_and_expand<F>(sess: &Session,
}

*sess.plugin_llvm_passes.borrow_mut() = llvm_passes;
sess.opt_mir_passes.borrow_mut().extend(opt_mir_passes);
*sess.plugin_attributes.borrow_mut() = attributes.clone();
})?;

Expand Down Expand Up @@ -922,7 +924,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
rustc_const_eval::provide(&mut extern_providers);

// Setup the MIR passes that we want to run.
let mut passes = Passes::new();
let mut passes = Passes::new();
passes.push_hook(mir::transform::dump_mir::DumpMir);

// Remove all `EndRegion` statements that are not involved in borrows.
Expand Down Expand Up @@ -958,6 +960,9 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
passes.push_pass(MIR_OPTIMIZED, mir::transform::copy_prop::CopyPropagation);
passes.push_pass(MIR_OPTIMIZED, mir::transform::simplify::SimplifyLocals);
passes.push_pass(MIR_OPTIMIZED, mir::transform::add_call_guards::AddCallGuards);

// Push plugin passes before the convertion phase.
passes.push_passes(MIR_OPTIMIZED, &mut sess.opt_mir_passes.borrow_mut());
passes.push_pass(MIR_OPTIMIZED, mir::transform::dump_mir::Marker("PreTrans"));

TyCtxt::create_and_enter(sess,
Expand Down
11 changes: 11 additions & 0 deletions src/librustc_plugin/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use rustc::lint::{EarlyLintPassObject, LateLintPassObject, LintId, Lint};
use rustc::session::Session;
use rustc::mir::transform::MirPass;

use syntax::ext::base::{SyntaxExtension, NamedSyntaxExtension, NormalTT, IdentTT};
use syntax::ext::base::MacroExpanderFn;
Expand All @@ -22,6 +23,7 @@ use syntax_pos::Span;

use std::collections::HashMap;
use std::borrow::ToOwned;
use std::rc::Rc;

/// Structure used to register plugins.
///
Expand Down Expand Up @@ -51,6 +53,9 @@ pub struct Registry<'a> {
#[doc(hidden)]
pub late_lint_passes: Vec<LateLintPassObject>,

#[doc(hidden)]
pub opt_mir_passes: Vec<Rc<MirPass>>,

#[doc(hidden)]
pub lint_groups: HashMap<&'static str, Vec<LintId>>,

Expand All @@ -76,6 +81,7 @@ impl<'a> Registry<'a> {
lint_groups: HashMap::new(),
llvm_passes: vec![],
attributes: vec![],
opt_mir_passes: Vec::new(),
whitelisted_custom_derives: Vec::new(),
}
}
Expand Down Expand Up @@ -152,6 +158,11 @@ impl<'a> Registry<'a> {
self.lint_groups.insert(name, to.into_iter().map(|x| LintId::of(x)).collect());
}

/// Register a MIR pass to be executed at the end of the MIR passes pipeline.
pub fn register_opt_mir_pass(&mut self, pass: Rc<MirPass>) {
self.opt_mir_passes.push(pass);
}

/// Register an LLVM pass.
///
/// Registration with LLVM itself is handled through static C++ objects with
Expand Down

0 comments on commit 9e0d95e

Please sign in to comment.