Skip to content

Commit

Permalink
Remove QualifResolver abstraction
Browse files Browse the repository at this point in the history
This is a relic from earlier attempts at dataflow-based const validation
that attempted to do promotion at the same time. #63812 takes a
different approach: `IsNotPromotable` is no longer a `Qualif` and is
computed lazily instead of eagerly. As a result, there's no need for an
eager `TempPromotionResolver`, and we can use the only implementer of
`QualifResolver` directly instead of through a trait.
  • Loading branch information
ecstatic-morse committed Oct 26, 2019
1 parent 6538656 commit b93cdbc
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 206 deletions.
153 changes: 9 additions & 144 deletions src/librustc_mir/transform/check_consts/resolver.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
//! Propagate `Qualif`s between locals and query the results.
//!
//! This also contains the dataflow analysis used to track `Qualif`s on complex control-flow
//! graphs.
//! This contains the dataflow analysis used to track `Qualif`s on complex control-flow graphs.

use rustc::mir::visit::Visitor;
use rustc::mir::{self, BasicBlock, Local, Location};
use rustc_index::bit_set::BitSet;

use std::cell::RefCell;
use std::marker::PhantomData;

use crate::dataflow::{self as old_dataflow, generic as dataflow};
use super::{Item, Qualif};
use self::old_dataflow::IndirectlyMutableLocals;

/// A `Visitor` that propagates qualifs between locals. This defines the transfer function of
/// `FlowSensitiveAnalysis` as well as the logic underlying `TempPromotionResolver`.
/// `FlowSensitiveAnalysis`.
///
/// This transfer does nothing when encountering an indirect assignment. Consumers should rely on
/// the `IndirectlyMutableLocals` dataflow pass to see if a `Local` may have become qualified via
Expand Down Expand Up @@ -147,145 +144,6 @@ where
}
}

/// Types that can compute the qualifs of each local at each location in a `mir::Body`.
///
/// Code that wishes to use a `QualifResolver` must call `visit_{statement,terminator}` for each
/// statement or terminator, processing blocks in reverse post-order beginning from the
/// `START_BLOCK`. Calling code may optionally call `get` after visiting each statement or
/// terminator to query the qualification state immediately before that statement or terminator.
///
/// These conditions are much more restrictive than woud be required by `FlowSensitiveResolver`
/// alone. This is to allow a linear, on-demand `TempPromotionResolver` that can operate
/// efficiently on simple CFGs.
pub trait QualifResolver<Q> {
/// Get the qualifs of each local at the last location visited.
///
/// This takes `&mut self` so qualifs can be computed lazily.
fn get(&mut self) -> &BitSet<Local>;

/// A convenience method for `self.get().contains(local)`.
fn contains(&mut self, local: Local) -> bool {
self.get().contains(local)
}

/// Resets the resolver to the `START_BLOCK`. This allows a resolver to be reused
/// for multiple passes over a `mir::Body`.
fn reset(&mut self);
}

pub type IndirectlyMutableResults<'mir, 'tcx> =
old_dataflow::DataflowResultsCursor<'mir, 'tcx, IndirectlyMutableLocals<'mir, 'tcx>>;

/// A resolver for qualifs that works on arbitrarily complex CFGs.
///
/// As soon as a `Local` becomes writable through a reference (as determined by the
/// `IndirectlyMutableLocals` dataflow pass), we must assume that it takes on all other qualifs
/// possible for its type. This is because no effort is made to track qualifs across indirect
/// assignments (e.g. `*p = x` or calls to opaque functions).
///
/// It is possible to be more precise here by waiting until an indirect assignment actually occurs
/// before marking a borrowed `Local` as qualified.
pub struct FlowSensitiveResolver<'a, 'mir, 'tcx, Q>
where
Q: Qualif,
{
location: Location,
indirectly_mutable_locals: &'a RefCell<IndirectlyMutableResults<'mir, 'tcx>>,
cursor: dataflow::ResultsCursor<'mir, 'tcx, FlowSensitiveAnalysis<'a, 'mir, 'tcx, Q>>,
qualifs_per_local: BitSet<Local>,

/// The value of `Q::in_any_value_of_ty` for each local.
qualifs_in_any_value_of_ty: BitSet<Local>,
}

impl<Q> FlowSensitiveResolver<'a, 'mir, 'tcx, Q>
where
Q: Qualif,
{
pub fn new(
_: Q,
item: &'a Item<'mir, 'tcx>,
indirectly_mutable_locals: &'a RefCell<IndirectlyMutableResults<'mir, 'tcx>>,
dead_unwinds: &BitSet<BasicBlock>,
) -> Self {
let analysis = FlowSensitiveAnalysis {
item,
_qualif: PhantomData,
};
let results =
dataflow::Engine::new(item.tcx, item.body, item.def_id, dead_unwinds, analysis)
.iterate_to_fixpoint();
let cursor = dataflow::ResultsCursor::new(item.body, results);

let mut qualifs_in_any_value_of_ty = BitSet::new_empty(item.body.local_decls.len());
for (local, decl) in item.body.local_decls.iter_enumerated() {
if Q::in_any_value_of_ty(item, decl.ty) {
qualifs_in_any_value_of_ty.insert(local);
}
}

FlowSensitiveResolver {
cursor,
indirectly_mutable_locals,
qualifs_per_local: BitSet::new_empty(item.body.local_decls.len()),
qualifs_in_any_value_of_ty,
location: Location { block: mir::START_BLOCK, statement_index: 0 },
}
}
}

impl<Q> Visitor<'tcx> for FlowSensitiveResolver<'_, '_, 'tcx, Q>
where
Q: Qualif
{
fn visit_statement(&mut self, _: &mir::Statement<'tcx>, location: Location) {
self.location = location;
}

fn visit_terminator(&mut self, _: &mir::Terminator<'tcx>, location: Location) {
self.location = location;
}
}

impl<Q> QualifResolver<Q> for FlowSensitiveResolver<'_, '_, '_, Q>
where
Q: Qualif
{
fn get(&mut self) -> &BitSet<Local> {
let mut indirectly_mutable_locals = self.indirectly_mutable_locals.borrow_mut();

indirectly_mutable_locals.seek(self.location);
self.cursor.seek_before(self.location);

self.qualifs_per_local.overwrite(indirectly_mutable_locals.get());
self.qualifs_per_local.union(self.cursor.get());
self.qualifs_per_local.intersect(&self.qualifs_in_any_value_of_ty);
&self.qualifs_per_local
}

fn contains(&mut self, local: Local) -> bool {
// No need to update the cursor if we know that `Local` cannot possibly be qualified.
if !self.qualifs_in_any_value_of_ty.contains(local) {
return false;
}

// Otherwise, return `true` if this local is qualified or was indirectly mutable at any
// point before this statement.
self.cursor.seek_before(self.location);
if self.cursor.get().contains(local) {
return true;
}

let mut indirectly_mutable_locals = self.indirectly_mutable_locals.borrow_mut();
indirectly_mutable_locals.seek(self.location);
indirectly_mutable_locals.get().contains(local)
}

fn reset(&mut self) {
self.location = Location { block: mir::START_BLOCK, statement_index: 0 };
}
}

/// The dataflow analysis used to propagate qualifs on arbitrary CFGs.
pub(super) struct FlowSensitiveAnalysis<'a, 'mir, 'tcx, Q> {
item: &'a Item<'mir, 'tcx>,
Expand All @@ -296,6 +154,13 @@ impl<'a, 'mir, 'tcx, Q> FlowSensitiveAnalysis<'a, 'mir, 'tcx, Q>
where
Q: Qualif,
{
pub(super) fn new(_: Q, item: &'a Item<'mir, 'tcx>) -> Self {
FlowSensitiveAnalysis {
item,
_qualif: PhantomData,
}
}

fn transfer_function(
&self,
state: &'a mut BitSet<Local>,
Expand Down
Loading

0 comments on commit b93cdbc

Please sign in to comment.