Skip to content

Commit

Permalink
pass full InstanceDef to run_passes
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Feb 9, 2019
1 parent cd21696 commit 2708946
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 55 deletions.
2 changes: 1 addition & 1 deletion src/librustc_mir/borrow_check/nll/type_check/mod.rs
Expand Up @@ -2428,7 +2428,7 @@ pub struct TypeckMir;

impl MirPass for TypeckMir {
fn run_pass<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, src: MirSource, mir: &mut Mir<'tcx>) {
let def_id = src.def_id;
let def_id = src.def_id();
debug!("run_pass: {:?}", def_id);

// When NLL is enabled, the borrow checker runs the typeck
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/shim.rs
Expand Up @@ -116,7 +116,7 @@ fn make_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
};
debug!("make_shim({:?}) = untransformed {:?}", instance, result);

run_passes(tcx, &mut result, instance.def_id(), MirPhase::Const, &[
run_passes(tcx, &mut result, instance, MirPhase::Const, &[
&add_moves_for_packed_drops::AddMovesForPackedDrops,
&no_landing_pads::NoLandingPads,
&remove_noop_landing_pads::RemoveNoopLandingPads,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/transform/add_moves_for_packed_drops.rs
Expand Up @@ -46,7 +46,7 @@ impl MirPass for AddMovesForPackedDrops {
mir: &mut Mir<'tcx>)
{
debug!("add_moves_for_packed_drops({:?} @ {:?})", src, mir.span);
add_moves_for_packed_drops(tcx, mir, src.def_id);
add_moves_for_packed_drops(tcx, mir, src.def_id());
}
}

Expand Down
40 changes: 20 additions & 20 deletions src/librustc_mir/transform/const_prop.rs
Expand Up @@ -30,31 +30,31 @@ pub struct ConstProp;
impl MirPass for ConstProp {
fn run_pass<'a, 'tcx>(&self,
tcx: TyCtxt<'a, 'tcx, 'tcx>,
source: MirSource,
source: MirSource<'tcx>,
mir: &mut Mir<'tcx>) {
// will be evaluated by miri and produce its errors there
if source.promoted.is_some() {
return;
}

use rustc::hir::map::blocks::FnLikeNode;
let node_id = tcx.hir().as_local_node_id(source.def_id)
let node_id = tcx.hir().as_local_node_id(source.def_id())
.expect("Non-local call to local provider is_const_fn");

let is_fn_like = FnLikeNode::from_node(tcx.hir().get(node_id)).is_some();
let is_assoc_const = match tcx.describe_def(source.def_id) {
let is_assoc_const = match tcx.describe_def(source.def_id()) {
Some(Def::AssociatedConst(_)) => true,
_ => false,
};

// Only run const prop on functions, methods, closures and associated constants
if !is_fn_like && !is_assoc_const {
// skip anon_const/statics/consts because they'll be evaluated by miri anyway
trace!("ConstProp skipped for {:?}", source.def_id);
trace!("ConstProp skipped for {:?}", source.def_id());
return
}

trace!("ConstProp starting for {:?}", source.def_id);
trace!("ConstProp starting for {:?}", source.def_id());

// FIXME(oli-obk, eddyb) Optimize locals (or even local paths) to hold
// constants, instead of just checking for const-folding succeeding.
Expand All @@ -63,7 +63,7 @@ impl MirPass for ConstProp {
let mut optimization_finder = ConstPropagator::new(mir, tcx, source);
optimization_finder.visit_mir(mir);

trace!("ConstProp done for {:?}", source.def_id);
trace!("ConstProp done for {:?}", source.def_id());
}
}

Expand All @@ -74,7 +74,7 @@ struct ConstPropagator<'a, 'mir, 'tcx:'a+'mir> {
ecx: EvalContext<'a, 'mir, 'tcx, CompileTimeInterpreter<'a, 'mir, 'tcx>>,
mir: &'mir Mir<'tcx>,
tcx: TyCtxt<'a, 'tcx, 'tcx>,
source: MirSource,
source: MirSource<'tcx>,
places: IndexVec<Local, Option<Const<'tcx>>>,
can_const_prop: IndexVec<Local, bool>,
param_env: ParamEnv<'tcx>,
Expand Down Expand Up @@ -107,10 +107,10 @@ impl<'a, 'mir, 'tcx> ConstPropagator<'a, 'mir, 'tcx> {
fn new(
mir: &'mir Mir<'tcx>,
tcx: TyCtxt<'a, 'tcx, 'tcx>,
source: MirSource,
source: MirSource<'tcx>,
) -> ConstPropagator<'a, 'mir, 'tcx> {
let param_env = tcx.param_env(source.def_id);
let ecx = mk_eval_cx(tcx, tcx.def_span(source.def_id), param_env);
let param_env = tcx.param_env(source.def_id());
let ecx = mk_eval_cx(tcx, tcx.def_span(source.def_id()), param_env);
ConstPropagator {
ecx,
mir,
Expand Down Expand Up @@ -284,13 +284,13 @@ impl<'a, 'mir, 'tcx> ConstPropagator<'a, 'mir, 'tcx> {
_ => None,
},
Place::Promoted(ref promoted) => {
let generics = self.tcx.generics_of(self.source.def_id);
let generics = self.tcx.generics_of(self.source.def_id());
if generics.requires_monomorphization(self.tcx) {
// FIXME: can't handle code with generics
return None;
}
let substs = Substs::identity_for_item(self.tcx, self.source.def_id);
let instance = Instance::new(self.source.def_id, substs);
let substs = Substs::identity_for_item(self.tcx, self.source.def_id());
let instance = Instance::new(self.source.def_id(), substs);
let cid = GlobalId {
instance,
promoted: Some(promoted.0),
Expand Down Expand Up @@ -358,10 +358,10 @@ impl<'a, 'mir, 'tcx> ConstPropagator<'a, 'mir, 'tcx> {
)))
}
Rvalue::UnaryOp(op, ref arg) => {
let def_id = if self.tcx.is_closure(self.source.def_id) {
self.tcx.closure_base_def_id(self.source.def_id)
let def_id = if self.tcx.is_closure(self.source.def_id()) {
self.tcx.closure_base_def_id(self.source.def_id())
} else {
self.source.def_id
self.source.def_id()
};
let generics = self.tcx.generics_of(def_id);
if generics.requires_monomorphization(self.tcx) {
Expand Down Expand Up @@ -398,10 +398,10 @@ impl<'a, 'mir, 'tcx> ConstPropagator<'a, 'mir, 'tcx> {
Rvalue::BinaryOp(op, ref left, ref right) => {
trace!("rvalue binop {:?} for {:?} and {:?}", op, left, right);
let right = self.eval_operand(right, source_info)?;
let def_id = if self.tcx.is_closure(self.source.def_id) {
self.tcx.closure_base_def_id(self.source.def_id)
let def_id = if self.tcx.is_closure(self.source.def_id()) {
self.tcx.closure_base_def_id(self.source.def_id())
} else {
self.source.def_id
self.source.def_id()
};
let generics = self.tcx.generics_of(def_id);
if generics.requires_monomorphization(self.tcx) {
Expand Down Expand Up @@ -608,7 +608,7 @@ impl<'b, 'a, 'tcx> Visitor<'tcx> for ConstPropagator<'b, 'a, 'tcx> {
let node_id = self
.tcx
.hir()
.as_local_node_id(self.source.def_id)
.as_local_node_id(self.source.def_id())
.expect("some part of a failing const eval must be local");
use rustc::mir::interpret::EvalErrorKind::*;
let msg = match msg {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/transform/elaborate_drops.rs
Expand Up @@ -28,8 +28,8 @@ impl MirPass for ElaborateDrops {
{
debug!("elaborate_drops({:?} @ {:?})", src, mir.span);

let id = tcx.hir().as_local_node_id(src.def_id).unwrap();
let param_env = tcx.param_env(src.def_id).with_reveal_all();
let id = tcx.hir().as_local_node_id(src.def_id()).unwrap();
let param_env = tcx.param_env(src.def_id()).with_reveal_all();
let move_data = match MoveData::gather_moves(mir, tcx) {
Ok(move_data) => move_data,
Err((move_data, _move_errors)) => {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/transform/generator.rs
Expand Up @@ -383,7 +383,7 @@ fn locals_live_across_suspend_points(
FxHashMap<BasicBlock, liveness::LiveVarSet<Local>>,
) {
let dead_unwinds = BitSet::new_empty(mir.basic_blocks().len());
let node_id = tcx.hir().as_local_node_id(source.def_id).unwrap();
let node_id = tcx.hir().as_local_node_id(source.def_id()).unwrap();

// Calculate when MIR locals have live storage. This gives us an upper bound of their
// lifetimes.
Expand Down Expand Up @@ -880,7 +880,7 @@ impl MirPass for StateTransform {

assert!(mir.generator_drop.is_none());

let def_id = source.def_id;
let def_id = source.def_id();

// The first argument is the generator type passed by value
let gen_ty = mir.local_decls.raw[1].ty;
Expand Down
10 changes: 5 additions & 5 deletions src/librustc_mir/transform/inline.rs
Expand Up @@ -40,7 +40,7 @@ struct CallSite<'tcx> {
impl MirPass for Inline {
fn run_pass<'a, 'tcx>(&self,
tcx: TyCtxt<'a, 'tcx, 'tcx>,
source: MirSource,
source: MirSource<'tcx>,
mir: &mut Mir<'tcx>) {
if tcx.sess.opts.debugging_opts.mir_opt_level >= 2 {
Inliner { tcx, source }.run_pass(mir);
Expand All @@ -50,7 +50,7 @@ impl MirPass for Inline {

struct Inliner<'a, 'tcx: 'a> {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
source: MirSource,
source: MirSource<'tcx>,
}

impl<'a, 'tcx> Inliner<'a, 'tcx> {
Expand All @@ -69,10 +69,10 @@ impl<'a, 'tcx> Inliner<'a, 'tcx> {

let mut callsites = VecDeque::new();

let param_env = self.tcx.param_env(self.source.def_id);
let param_env = self.tcx.param_env(self.source.def_id());

// Only do inlining into fn bodies.
let id = self.tcx.hir().as_local_node_id(self.source.def_id).unwrap();
let id = self.tcx.hir().as_local_node_id(self.source.def_id()).unwrap();
if self.tcx.hir().body_owner_kind(id).is_fn_or_closure() && self.source.promoted.is_none() {
for (bb, bb_data) in caller_mir.basic_blocks().iter_enumerated() {
if let Some(callsite) = self.get_valid_function_call(bb,
Expand Down Expand Up @@ -274,7 +274,7 @@ impl<'a, 'tcx> Inliner<'a, 'tcx> {

// FIXME: Give a bonus to functions with only a single caller

let param_env = tcx.param_env(self.source.def_id);
let param_env = tcx.param_env(self.source.def_id());

let mut first_block = true;
let mut cost = 0;
Expand Down
27 changes: 16 additions & 11 deletions src/librustc_mir/transform/mod.rs
Expand Up @@ -2,7 +2,7 @@ use crate::borrow_check::nll::type_check;
use crate::build;
use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
use rustc::mir::{Mir, MirPhase, Promoted};
use rustc::ty::TyCtxt;
use rustc::ty::{TyCtxt, InstanceDef};
use rustc::ty::query::Providers;
use rustc::ty::steal::Steal;
use rustc::hir;
Expand Down Expand Up @@ -104,20 +104,25 @@ fn mir_built<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx Stea

/// Where a specific Mir comes from.
#[derive(Debug, Copy, Clone)]
pub struct MirSource {
pub def_id: DefId,
pub struct MirSource<'tcx> {
pub instance: InstanceDef<'tcx>,

/// If `Some`, this is a promoted rvalue within the parent function.
pub promoted: Option<Promoted>,
}

impl MirSource {
impl<'tcx> MirSource<'tcx> {
pub fn item(def_id: DefId) -> Self {
MirSource {
def_id,
instance: InstanceDef::Item(def_id),
promoted: None
}
}

#[inline]
pub fn def_id(&self) -> DefId {
self.instance.def_id()
}
}

/// Generates a default name for the pass based on the name of the
Expand All @@ -141,14 +146,14 @@ pub trait MirPass {

fn run_pass<'a, 'tcx>(&self,
tcx: TyCtxt<'a, 'tcx, 'tcx>,
source: MirSource,
source: MirSource<'tcx>,
mir: &mut Mir<'tcx>);
}

pub fn run_passes(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
mir: &mut Mir<'tcx>,
def_id: DefId,
instance: InstanceDef<'tcx>,
mir_phase: MirPhase,
passes: &[&dyn MirPass],
) {
Expand All @@ -160,7 +165,7 @@ pub fn run_passes(
}

let source = MirSource {
def_id,
instance,
promoted,
};
let mut index = 0;
Expand Down Expand Up @@ -198,7 +203,7 @@ fn mir_const<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx Stea
let _ = tcx.unsafety_check_result(def_id);

let mut mir = tcx.mir_built(def_id).steal();
run_passes(tcx, &mut mir, def_id, MirPhase::Const, &[
run_passes(tcx, &mut mir, InstanceDef::Item(def_id), MirPhase::Const, &[
// What we need to do constant evaluation.
&simplify::SimplifyCfg::new("initial"),
&type_check::TypeckMir,
Expand All @@ -217,7 +222,7 @@ fn mir_validated<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx
}

let mut mir = tcx.mir_const(def_id).steal();
run_passes(tcx, &mut mir, def_id, MirPhase::Validated, &[
run_passes(tcx, &mut mir, InstanceDef::Item(def_id), MirPhase::Validated, &[
// What we need to run borrowck etc.
&qualify_consts::QualifyAndPromoteConstants,
&simplify::SimplifyCfg::new("qualify-consts"),
Expand All @@ -235,7 +240,7 @@ fn optimized_mir<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx
}

let mut mir = tcx.mir_validated(def_id).steal();
run_passes(tcx, &mut mir, def_id, MirPhase::Optimized, &[
run_passes(tcx, &mut mir, InstanceDef::Item(def_id), MirPhase::Optimized, &[
// Remove all things not needed by analysis
&no_landing_pads::NoLandingPads,
&simplify_branches::SimplifyBranches::new("initial"),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/transform/qualify_consts.rs
Expand Up @@ -1171,7 +1171,7 @@ impl MirPass for QualifyAndPromoteConstants {
return;
}

let def_id = src.def_id;
let def_id = src.def_id();
let id = tcx.hir().as_local_node_id(def_id).unwrap();
let mut const_promoted_temps = None;
let mode = match tcx.hir().body_owner_kind(id) {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/transform/rustc_peek.rs
Expand Up @@ -25,7 +25,7 @@ pub struct SanityCheck;
impl MirPass for SanityCheck {
fn run_pass<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
src: MirSource, mir: &mut Mir<'tcx>) {
let def_id = src.def_id;
let def_id = src.def_id();
let id = tcx.hir().as_local_node_id(def_id).unwrap();
if !tcx.has_attr(def_id, "rustc_mir") {
debug!("skipping rustc_peek::SanityCheck on {}", tcx.item_path_str(def_id));
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/util/liveness.rs
Expand Up @@ -317,7 +317,7 @@ pub fn dump_mir<'a, 'tcx, V: Idx>(
}
let node_path = item_path::with_forced_impl_filename_line(|| {
// see notes on #41697 below
tcx.item_path_str(source.def_id)
tcx.item_path_str(source.def_id())
});
dump_matched_mir_node(tcx, pass_name, &node_path, source, mir, map, result);
}
Expand All @@ -333,7 +333,7 @@ fn dump_matched_mir_node<'a, 'tcx, V: Idx>(
) {
let mut file_path = PathBuf::new();
file_path.push(Path::new(&tcx.sess.opts.debugging_opts.dump_mir_dir));
let item_id = tcx.hir().as_local_node_id(source.def_id).unwrap();
let item_id = tcx.hir().as_local_node_id(source.def_id()).unwrap();
let file_name = format!("rustc.node{}{}-liveness.mir", item_id, pass_name);
file_path.push(&file_name);
let _ = fs::File::create(&file_path).and_then(|mut file| {
Expand Down

0 comments on commit 2708946

Please sign in to comment.