Skip to content

Commit

Permalink
Merge pull request #69 from willcrichton/refactor-utils
Browse files Browse the repository at this point in the history
Refactor utils
  • Loading branch information
willcrichton authored May 18, 2023
2 parents a156ab9 + 34a05ef commit 8df6113
Show file tree
Hide file tree
Showing 39 changed files with 187 additions and 3,313 deletions.
12 changes: 6 additions & 6 deletions crates/flowistry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,32 @@ license = "MIT"
rustc_private = true

[features]
test = ["textwrap", "lazy_static"]
test = ["rustc-utils/test"]
debug = ["html-escape"]

[dependencies]
anyhow = "1"
log = "0.4"
fluid-let = "1.0"
cfg-if = "1.0"
intervaltree = "0.2"
serde = {version = "1", features = ["derive"]}

# For local debugging
html-escape = {version = "0.2", optional = true}

# For test_utils
textwrap = {version = "0.14", default-features = false, optional = true}
lazy_static = {version = "1.4", optional = true}
[dependencies.rustc-utils]
git = "https://github.com/cognitive-engineering-lab/rustc-plugin"
tag = "nightly-2023-04-12-v0.1.4"

[dev-dependencies]
# Hack based on https://github.com/rust-lang/cargo/issues/2911
flowistry = { path = ".", features = ["test"] }
criterion = "0.3"
criterion = "0.4"
env_logger = {version = "0.9", default-features = false}
test-log = "0.2"
glob = "0.3.0"
bench_utils = { path = "../bench_utils" }
textwrap = {version = "0.14", default-features = false}

[[bench]]
name = "main"
Expand Down
7 changes: 3 additions & 4 deletions crates/flowistry/benches/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ use anyhow::{Context, Result};
use criterion::{
criterion_group, criterion_main, measurement::WallTime, BenchmarkGroup, Criterion,
};
use flowistry::{
infoflow::Direction,
mir::{borrowck_facts, utils::PlaceExt},
};
use flowistry::infoflow::Direction;
use glob::glob;
use rustc_borrowck::BodyWithBorrowckFacts;
use rustc_hir::{BodyId, ItemKind};
use rustc_middle::{
mir::{Location, Place},
ty::TyCtxt,
};
use rustc_utils::{mir::borrowck_facts, PlaceExt};

#[derive(Clone, Copy, PartialEq, Eq)]
enum AnalysisType {
Expand Down Expand Up @@ -76,6 +74,7 @@ struct Callbacks {
}
impl rustc_driver::Callbacks for Callbacks {
fn config(&mut self, config: &mut rustc_interface::Config) {
borrowck_facts::enable_mir_simplification();
config.override_queries = Some(borrowck_facts::override_queries);
}

Expand Down
16 changes: 7 additions & 9 deletions crates/flowistry/examples/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,19 @@ extern crate rustc_span;

use std::process::Command;

use flowistry::{
indexed::impls::LocationOrArg,
infoflow::Direction,
mir::{
borrowck_facts,
utils::{BodyExt, PlaceExt, SpanExt},
},
source_map::{EnclosingHirSpans, Spanner},
};
use flowistry::{indexed::impls::LocationOrArg, infoflow::Direction};
use rustc_borrowck::BodyWithBorrowckFacts;
use rustc_hir::{BodyId, ItemKind};
use rustc_middle::{
mir::{Local, Place},
ty::TyCtxt,
};
use rustc_span::Span;
use rustc_utils::{
mir::borrowck_facts,
source_map::spanner::{EnclosingHirSpans, Spanner},
BodyExt, PlaceExt, SpanExt,
};

// This is the core analysis. Everything below this function is plumbing to
// call into rustc's API.
Expand Down Expand Up @@ -95,6 +92,7 @@ struct Callbacks;
impl rustc_driver::Callbacks for Callbacks {
fn config(&mut self, config: &mut rustc_interface::Config) {
// You MUST configure rustc to ensure `get_body_with_borrowck_facts` will work.
borrowck_facts::enable_mir_simplification();
config.override_queries = Some(borrowck_facts::override_queries);
}

Expand Down
72 changes: 0 additions & 72 deletions crates/flowistry/src/cached.rs

This file was deleted.

68 changes: 4 additions & 64 deletions crates/flowistry/src/indexed/impls.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,19 @@
use std::{ops::Deref, path::PathBuf, rc::Rc};
use std::rc::Rc;

use rustc_data_structures::fx::FxHashSet as HashSet;
use rustc_middle::mir::{Body, Local, Location, Place};
use serde::Serialize;
pub use rustc_utils::source_map::spanner::LocationOrArg;
use rustc_utils::BodyExt;

use super::{DefaultDomain, IndexSet, IndexedDomain, IndexedValue, OwnedSet, ToIndex};
use crate::{
mir::utils::{BodyExt, PlaceExt},
to_index_impl,
};

/// Used to represent dependencies of places.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum LocationOrArg {
Location(Location),
Arg(Local),
}

impl LocationOrArg {
pub fn from_place<'tcx>(place: Place<'tcx>, body: &Body<'tcx>) -> Option<Self> {
place
.is_arg(body)
.then_some(LocationOrArg::Arg(place.local))
}
}

impl From<Location> for LocationOrArg {
fn from(location: Location) -> Self {
LocationOrArg::Location(location)
}
}
use crate::to_index_impl;

impl ToIndex<LocationOrArg> for Location {
fn to_index(&self, domain: &LocationOrArgDomain) -> LocationOrArgIndex {
domain.index(&LocationOrArg::Location(*self))
}
}

impl From<Local> for LocationOrArg {
fn from(local: Local) -> Self {
LocationOrArg::Arg(local)
}
}

impl ToIndex<LocationOrArg> for Local {
fn to_index(&self, domain: &LocationOrArgDomain) -> LocationOrArgIndex {
domain.index(&LocationOrArg::Arg(*self))
Expand Down Expand Up @@ -72,34 +43,3 @@ pub fn build_location_arg_domain(body: &Body) -> Rc<LocationOrArgDomain> {
}

pub type PlaceSet<'tcx> = HashSet<Place<'tcx>>;

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct Filename(pub PathBuf);

rustc_index::newtype_index! {
#[derive(Serialize)]
#[debug_format = "f{}"]
pub struct FilenameIndex {}
}

// Filenames are interned at the thread-level, so they should only be
// used within a given thread. Generally sending an index across a thread
// boundary is a logical error.
impl !Send for FilenameIndex {}

to_index_impl!(Filename);

pub type FilenameDomain = DefaultDomain<FilenameIndex, Filename>;

impl IndexedValue for Filename {
type Index = FilenameIndex;
type Domain = FilenameDomain;
}

impl Deref for Filename {
type Target = PathBuf;

fn deref(&self) -> &Self::Target {
&self.0
}
}
25 changes: 10 additions & 15 deletions crates/flowistry/src/infoflow/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use rustc_middle::{
ty::TyCtxt,
};
use rustc_mir_dataflow::{Analysis, AnalysisDomain, Forward};
use rustc_utils::{
mir::control_dependencies::ControlDependencies, BodyExt, OperandExt, PlaceExt,
};

use super::{
mutation::{ModularMutationVisitor, Mutation, MutationStatus},
Expand All @@ -19,11 +22,7 @@ use crate::{
impls::{LocationOrArg, LocationOrArgDomain, LocationOrArgSet},
IndexMatrix, IndexedDomain,
},
mir::{
aliases::Aliases,
control_dependencies::ControlDependencies,
utils::{BodyExt, OperandExt, PlaceExt},
},
mir::aliases::Aliases,
};

/// Represents the information flows at a given instruction. See [`FlowResults`] for a high-level explanation of this datatype.
Expand Down Expand Up @@ -115,15 +114,11 @@ impl<'a, 'tcx> FlowAnalysis<'a, 'tcx> {

let add_deps = |place: Place<'tcx>, location_deps: &mut LocationOrArgSet| {
let reachable_values = all_aliases.reachable_values(place, Mutability::Not);
let provenance =
place
.refs_in_projection()
.into_iter()
.flat_map(|(place_ref, _)| {
all_aliases
.aliases(Place::from_ref(place_ref, self.tcx))
.iter()
});
let provenance = place.refs_in_projection().flat_map(|(place_ref, _)| {
all_aliases
.aliases(Place::from_ref(place_ref, self.tcx))
.iter()
});
for relevant in reachable_values.iter().chain(provenance) {
let deps = state.row_set(all_aliases.normalize(*relevant));
trace!(" For relevant {relevant:?} for input {place:?} adding deps {deps:?}");
Expand All @@ -148,7 +143,7 @@ impl<'a, 'tcx> FlowAnalysis<'a, 'tcx> {
// Include dependencies of the switch's operand
let terminator = body.basic_blocks[block].terminator();
if let TerminatorKind::SwitchInt { discr, .. } = &terminator.kind {
if let Some(discr_place) = discr.to_place() {
if let Some(discr_place) = discr.as_place() {
add_deps(discr_place, &mut input_location_deps);
}
}
Expand Down
14 changes: 7 additions & 7 deletions crates/flowistry/src/infoflow/dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ use either::Either;
use log::{debug, trace};
use rustc_middle::mir::{visit::Visitor, *};
use rustc_span::Span;
use rustc_utils::{
block_timer,
source_map::spanner::{EnclosingHirSpans, Spanner},
BodyExt, OperandExt, SpanExt,
};

use super::{mutation::ModularMutationVisitor, FlowDomain, FlowResults};
use crate::{
block_timer,
indexed::{
impls::{LocationOrArg, LocationOrArgSet},
RefSet,
},
infoflow::mutation::Mutation,
mir::{
aliases::Aliases,
utils::{BodyExt, OperandExt, SpanExt},
},
source_map::{EnclosingHirSpans, Spanner},
mir::aliases::Aliases,
};

/// Which way to look for dependencies
Expand Down Expand Up @@ -174,7 +174,7 @@ pub fn compute_dependencies<'tcx>(
kind: TerminatorKind::SwitchInt { discr, .. },
..
}) => {
if let Some(place) = discr.to_place() {
if let Some(place) = discr.as_place() {
check(place);
}
}
Expand Down
9 changes: 4 additions & 5 deletions crates/flowistry/src/infoflow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ use log::debug;
use rustc_borrowck::BodyWithBorrowckFacts;
use rustc_hir::BodyId;
use rustc_middle::ty::TyCtxt;
use rustc_utils::{block_timer, BodyExt};

pub use self::{
analysis::{FlowAnalysis, FlowDomain},
dependencies::{compute_dependencies, compute_dependency_spans, Direction},
};
use crate::{
block_timer,
mir::{aliases::Aliases, engine, utils::BodyExt},
};
use crate::mir::{aliases::Aliases, engine};

mod analysis;
mod dependencies;
Expand All @@ -38,7 +36,8 @@ mod recursive;
/// # #![feature(rustc_private)]
/// # extern crate rustc_middle;
/// # use rustc_middle::{ty::TyCtxt, mir::{Place, Location, Local}};
/// # use flowistry::{infoflow::{FlowDomain, FlowResults}, mir::utils::PlaceExt, indexed::impls::LocationOrArgSet};
/// # use flowistry::{infoflow::{FlowDomain, FlowResults}, indexed::impls::LocationOrArgSet};
/// # use rustc_utils::PlaceExt;
/// fn example<'tcx>(tcx: TyCtxt<'tcx>, results: &FlowResults<'_, 'tcx>) {
/// let ℓ: Location = Location::START;
/// let Θ: &FlowDomain = results.state_at(ℓ);
Expand Down
Loading

0 comments on commit 8df6113

Please sign in to comment.