Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a naïve hybrid algorithm #102

Merged
merged 6 commits into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions polonius-engine/src/output/hybrid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2019 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! A hybrid version combining the optimized Datafrog model with the
//! location-insensitive version.

use crate::output::datafrog_opt;
use crate::output::location_insensitive;
use crate::output::Output;
use facts::{AllFacts, Atom};

pub(super) fn compute<Region: Atom, Loan: Atom, Point: Atom>(
dump_enabled: bool,
all_facts: AllFacts<Region, Loan, Point>,
) -> Output<Region, Loan, Point> {
let lins_output = location_insensitive::compute(dump_enabled, &all_facts);
if lins_output.errors.is_empty() {
lins_output
} else {
datafrog_opt::compute(dump_enabled, all_facts)
}
}
11 changes: 5 additions & 6 deletions polonius-engine/src/output/location_insensitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use facts::{AllFacts, Atom};

pub(super) fn compute<Region: Atom, Loan: Atom, Point: Atom>(
dump_enabled: bool,
mut all_facts: AllFacts<Region, Loan, Point>,
all_facts: &AllFacts<Region, Loan, Point>,
) -> Output<Region, Loan, Point> {
let all_points: BTreeSet<Point> = all_facts
.cfg_edge
Expand All @@ -27,12 +27,11 @@ pub(super) fn compute<Region: Atom, Loan: Atom, Point: Atom>(
.chain(all_facts.cfg_edge.iter().map(|&(_, q)| q))
.collect();

all_facts
.region_live_at
.reserve(all_facts.universal_region.len() * all_points.len());
let mut my_region_live_at = all_facts.region_live_at.clone();
my_region_live_at.reserve(all_facts.universal_region.len() * all_points.len());
for &r in &all_facts.universal_region {
for &p in &all_points {
all_facts.region_live_at.push((r, p));
my_region_live_at.push((r, p));
}
}

Expand All @@ -45,7 +44,7 @@ pub(super) fn compute<Region: Atom, Loan: Atom, Point: Atom>(
let mut iteration = Iteration::new();

// static inputs
let region_live_at: Relation<(Region, Point)> = all_facts.region_live_at.into();
let region_live_at: Relation<(Region, Point)> = my_region_live_at.into();
let invalidates = Relation::from_iter(all_facts.invalidates.iter().map(|&(b, p)| (p, b)));

// .. some variables, ..
Expand Down
18 changes: 14 additions & 4 deletions polonius-engine/src/output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::borrow::Cow;
use std::collections::{BTreeMap, BTreeSet};

mod datafrog_opt;
mod hybrid;
mod location_insensitive;
mod naive;
use facts::{AllFacts, Atom};
Expand All @@ -24,14 +25,21 @@ pub enum Algorithm {
LocationInsensitive,
/// Compare Naive and DatafrogOpt.
Compare,
Hybrid,
}

impl Algorithm {
/// Optimized variants that ought to be equivalent to "naive"
pub const OPTIMIZED: &'static [Algorithm] = &[Algorithm::DatafrogOpt];

pub fn variants() -> [&'static str; 4] {
["Naive", "DatafrogOpt", "LocationInsensitive", "Compare"]
pub fn variants() -> [&'static str; 5] {
[
"Naive",
"DatafrogOpt",
"LocationInsensitive",
"Compare",
"Hybrid",
]
}
}

Expand All @@ -43,8 +51,9 @@ impl ::std::str::FromStr for Algorithm {
"datafrogopt" => Ok(Algorithm::DatafrogOpt),
"locationinsensitive" => Ok(Algorithm::LocationInsensitive),
"compare" => Ok(Algorithm::Compare),
"hybrid" => Ok(Algorithm::Hybrid),
_ => Err(String::from(
"valid values: Naive, DatafrogOpt, LocationInsensitive, Compare",
"valid values: Naive, DatafrogOpt, LocationInsensitive, Compare, Hybrid",
)),
}
}
Expand Down Expand Up @@ -117,7 +126,7 @@ where
Algorithm::Naive => naive::compute(dump_enabled, all_facts.clone()),
Algorithm::DatafrogOpt => datafrog_opt::compute(dump_enabled, all_facts.clone()),
Algorithm::LocationInsensitive => {
location_insensitive::compute(dump_enabled, all_facts.clone())
location_insensitive::compute(dump_enabled, &all_facts)
}
Algorithm::Compare => {
let naive_output = naive::compute(dump_enabled, all_facts.clone());
Expand All @@ -133,6 +142,7 @@ where
}
opt_output
}
Algorithm::Hybrid => hybrid::compute(dump_enabled, all_facts.clone()),
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ fn test_facts(all_facts: &AllFacts, algorithms: &[Algorithm]) {
assert_equal(&naive.borrow_live_at, &opt.borrow_live_at);
assert_equal(&naive.errors, &opt.errors);
}

// The hybrid algorithm gets the same errors as the naive version
let opt = Output::compute(all_facts, Algorithm::Hybrid, true);
assert_equal(&naive.errors, &opt.errors);
}

fn test_fn(dir_name: &str, fn_name: &str, algorithm: Algorithm) -> Result<(), Error> {
Expand Down