Skip to content

Commit

Permalink
Add mechanism to include crates
Browse files Browse the repository at this point in the history
  • Loading branch information
JustusAdam committed Jul 26, 2024
1 parent 61b973e commit a67406e
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
4 changes: 4 additions & 0 deletions crates/flowistry_pdg_construction/src/body_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ impl<'tcx> BodyCache<'tcx> {
unsafe { std::mem::transmute(cbody) }
})
}

pub fn is_loadable(&self, key: DefId) -> bool {
(self.load_policy)(key.krate)
}
}

fn compute_body_with_borrowck_facts(tcx: TyCtxt<'_>, def_id: DefId) -> CachedBody<'_> {
Expand Down
4 changes: 2 additions & 2 deletions crates/flowistry_pdg_construction/src/construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use flowistry_pdg::{CallString, GlobalLocation};

use df::{AnalysisDomain, Results, ResultsVisitor};
use rustc_hash::FxHashMap;
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
use rustc_index::IndexVec;
use rustc_middle::{
mir::{visit::Visitor, AggregateKind, Location, Place, Rvalue, Terminator, TerminatorKind},
Expand Down Expand Up @@ -53,7 +53,7 @@ impl<'tcx> MemoPdgConstructor<'tcx> {
dump_mir: false,
async_info: AsyncInfo::make(tcx).expect("Async functions are not defined"),
pdg_cache: Default::default(),
body_cache: BodyCache::new(tcx, |_| true),
body_cache: BodyCache::new(tcx, |krate| krate == LOCAL_CRATE),
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/flowistry_pdg_construction/src/local_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,8 @@ impl<'tcx, 'a> LocalAnalysis<'tcx, 'a> {

// Recursively generate the PDG for the child function.

if !resolved_fn.def_id().is_local() {
trace!(" bailing because function is not local");
if self.memo.body_cache.is_loadable(resolved_fn.def_id()) {
trace!(" bailing because function is not loadable");
return None;
}
let cache_key = resolved_fn;
Expand Down
22 changes: 20 additions & 2 deletions crates/paralegal-flow/src/ana/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ use flowistry_pdg_construction::{
use itertools::Itertools;
use petgraph::visit::GraphBase;

use rustc_hir::{self as hir, def, def_id::DefId};
use rustc_data_structures::fx::FxHashSet;
use rustc_hir::{
self as hir, def,
def_id::{DefId, LOCAL_CRATE},
};
use rustc_middle::ty::TyCtxt;
use rustc_span::{FileNameDisplayPreference, Span as RustSpan, Symbol};

Expand Down Expand Up @@ -55,12 +59,26 @@ impl<'tcx> SPDGGenerator<'tcx> {
) -> Self {
let inline_judge = InlineJudge::new(marker_ctx, tcx, opts.anactrl());
let mut pdg_constructor = MemoPdgConstructor::new(tcx);
let included_crate_names = opts
.anactrl()
.included()
.iter()
.map(|s| Symbol::intern(s))
.collect::<HashSet<_>>();
let included_crates = tcx
.crates(())
.iter()
.copied()
.filter(|cnum| included_crate_names.contains(&tcx.crate_name(*cnum)))
.chain(Some(LOCAL_CRATE))
.collect::<FxHashSet<_>>();
pdg_constructor
.with_call_change_callback(MyCallback {
judge: inline_judge.clone(),
tcx,
})
.with_dump_mir(opts.dbg().dump_mir());
.with_dump_mir(opts.dbg().dump_mir())
.with_body_loading_policy(move |krate| included_crates.contains(&krate));
Self {
inline_judge,
pdg_constructor,
Expand Down
11 changes: 11 additions & 0 deletions crates/paralegal-flow/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,9 @@ struct ClapAnalysisCtrl {
/// this is used explicitly supply the argument.
#[clap(long, conflicts_with_all = ["adaptive_depth", "no_cross_function_analysis"])]
unconstrained_depth: bool,
/// Crates that should be recursed into.
#[clap(long)]
include: Vec<String>,
}

#[derive(serde::Serialize, serde::Deserialize)]
Expand All @@ -448,13 +451,15 @@ pub struct AnalysisCtrl {
/// Disables all recursive analysis (both paralegal_flow's inlining as well as
/// Flowistry's recursive analysis).
inlining_depth: InliningDepth,
include: Vec<String>,
}

impl Default for AnalysisCtrl {
fn default() -> Self {
Self {
analyze: Vec::new(),
inlining_depth: InliningDepth::Adaptive,
include: Default::default(),
}
}
}
Expand All @@ -467,6 +472,7 @@ impl TryFrom<ClapAnalysisCtrl> for AnalysisCtrl {
no_cross_function_analysis,
adaptive_depth,
unconstrained_depth: _,
include,
} = value;

let inlining_depth = if adaptive_depth {
Expand All @@ -480,6 +486,7 @@ impl TryFrom<ClapAnalysisCtrl> for AnalysisCtrl {
Ok(Self {
analyze,
inlining_depth,
include,
})
}
}
Expand Down Expand Up @@ -508,6 +515,10 @@ impl AnalysisCtrl {
pub fn inlining_depth(&self) -> &InliningDepth {
&self.inlining_depth
}

pub fn included(&self) -> &[String] {
&self.include
}
}

impl DumpArgs {
Expand Down
1 change: 1 addition & 0 deletions crates/paralegal-flow/tests/cross-crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const CRATE_DIR: &str = "tests/cross-crate";
lazy_static! {
static ref TEST_CRATE_ANALYZED: bool = {
paralegal_flow_command(CRATE_DIR)
.args(["--include", "dependency"])
.status()
.unwrap()
.success()
Expand Down

0 comments on commit a67406e

Please sign in to comment.