-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathmod.rs
247 lines (221 loc) · 8.19 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright 2017 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.
use rustc_hash::FxHashMap;
use std::borrow::Cow;
use std::collections::{BTreeMap, BTreeSet};
mod datafrog_opt;
mod location_insensitive;
mod naive;
use facts::{AllFacts, Atom};
#[derive(Debug, Clone, Copy)]
pub enum Algorithm {
Naive,
DatafrogOpt,
LocationInsensitive,
/// Compare Naive and DatafrogOpt.
Compare,
}
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"]
}
}
impl ::std::str::FromStr for Algorithm {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_ref() {
"naive" => Ok(Algorithm::Naive),
"datafrogopt" => Ok(Algorithm::DatafrogOpt),
"locationinsensitive" => Ok(Algorithm::LocationInsensitive),
"compare" => Ok(Algorithm::Compare),
_ => Err(String::from(
"valid values: Naive, DatafrogOpt, LocationInsensitive, Compare",
)),
}
}
}
#[derive(Clone, Debug)]
pub struct Output<Region: Atom, Loan: Atom, Point: Atom> {
pub errors: FxHashMap<Point, Vec<Loan>>,
pub dump_enabled: bool,
// these are just for debugging
pub borrow_live_at: FxHashMap<Point, Vec<Loan>>,
pub restricts: FxHashMap<Point, BTreeMap<Region, BTreeSet<Loan>>>,
pub restricts_anywhere: FxHashMap<Region, BTreeSet<Loan>>,
pub region_live_at: FxHashMap<Point, Vec<Region>>,
pub invalidates: FxHashMap<Point, Vec<Loan>>,
pub subset: FxHashMap<Point, BTreeMap<Region, BTreeSet<Region>>>,
pub subset_anywhere: FxHashMap<Region, BTreeSet<Region>>,
}
/// Compares errors reported by Naive implementation with the errors
/// reported by the optimized implementation.
fn compare_errors<Loan: Atom, Point: Atom>(
all_naive_errors: &FxHashMap<Point, Vec<Loan>>,
all_opt_errors: &FxHashMap<Point, Vec<Loan>>,
) -> bool {
let points = all_naive_errors.keys().chain(all_opt_errors.keys());
let mut differ = false;
for point in points {
let mut naive_errors = all_naive_errors.get(&point).cloned().unwrap_or(Vec::new());
naive_errors.sort();
let mut opt_errors = all_opt_errors.get(&point).cloned().unwrap_or(Vec::new());
opt_errors.sort();
for err in naive_errors.iter() {
if !opt_errors.contains(err) {
error!(
"Error {0:?} at {1:?} reported by naive, but not opt.",
err, point
);
differ = true;
}
}
for err in opt_errors.iter() {
if !naive_errors.contains(err) {
error!(
"Error {0:?} at {1:?} reported by opt, but not naive.",
err, point
);
differ = true;
}
}
}
differ
}
impl<Region, Loan, Point> Output<Region, Loan, Point>
where
Region: Atom,
Loan: Atom,
Point: Atom,
{
pub fn compute(
all_facts: &AllFacts<Region, Loan, Point>,
algorithm: Algorithm,
dump_enabled: bool,
) -> Self {
match algorithm {
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())
}
Algorithm::Compare => {
let naive_output = naive::compute(dump_enabled, all_facts.clone());
let opt_output = datafrog_opt::compute(dump_enabled, all_facts.clone());
if compare_errors(&naive_output.errors, &opt_output.errors) {
panic!(concat!(
"The errors reported by the naive algorithm differ from ",
"the errors reported by the optimized algorithm. ",
"See the error log for details."
));
} else {
debug!("Naive and optimized algorithms reported the same errors.");
}
opt_output
}
}
}
fn new(dump_enabled: bool) -> Self {
Output {
borrow_live_at: FxHashMap::default(),
restricts: FxHashMap::default(),
restricts_anywhere: FxHashMap::default(),
region_live_at: FxHashMap::default(),
invalidates: FxHashMap::default(),
errors: FxHashMap::default(),
subset: FxHashMap::default(),
subset_anywhere: FxHashMap::default(),
dump_enabled,
}
}
pub fn errors_at(&self, location: Point) -> &[Loan] {
match self.errors.get(&location) {
Some(v) => v,
None => &[],
}
}
pub fn borrows_in_scope_at(&self, location: Point) -> &[Loan] {
match self.borrow_live_at.get(&location) {
Some(p) => p,
None => &[],
}
}
pub fn restricts_at(&self, location: Point) -> Cow<'_, BTreeMap<Region, BTreeSet<Loan>>> {
assert!(self.dump_enabled);
match self.restricts.get(&location) {
Some(map) => Cow::Borrowed(map),
None => Cow::Owned(BTreeMap::default()),
}
}
pub fn regions_live_at(&self, location: Point) -> &[Region] {
assert!(self.dump_enabled);
match self.region_live_at.get(&location) {
Some(v) => v,
None => &[],
}
}
pub fn subsets_at(&self, location: Point) -> Cow<'_, BTreeMap<Region, BTreeSet<Region>>> {
assert!(self.dump_enabled);
match self.subset.get(&location) {
Some(v) => Cow::Borrowed(v),
None => Cow::Owned(BTreeMap::default()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
impl Atom for usize {
fn index(self) -> usize {
self
}
}
fn compare(
errors1: &FxHashMap<usize, Vec<usize>>,
errors2: &FxHashMap<usize, Vec<usize>>,
) -> bool {
let diff1 = compare_errors(errors1, errors2);
let diff2 = compare_errors(errors2, errors1);
assert_eq!(diff1, diff2);
diff1
}
#[test]
fn test_compare_errors() {
let empty = FxHashMap::default();
assert_eq!(false, compare(&empty, &empty));
let mut empty_vec = FxHashMap::default();
empty_vec.insert(1, vec![]);
empty_vec.insert(2, vec![]);
assert_eq!(false, compare(&empty, &empty_vec));
let mut singleton1 = FxHashMap::default();
singleton1.insert(1, vec![10]);
assert_eq!(false, compare(&singleton1, &singleton1));
let mut singleton2 = FxHashMap::default();
singleton2.insert(1, vec![11]);
assert_eq!(false, compare(&singleton2, &singleton2));
let mut singleton3 = FxHashMap::default();
singleton3.insert(2, vec![10]);
assert_eq!(false, compare(&singleton3, &singleton3));
assert_eq!(true, compare(&singleton1, &singleton2));
assert_eq!(true, compare(&singleton2, &singleton3));
assert_eq!(true, compare(&singleton1, &singleton3));
assert_eq!(true, compare(&empty, &singleton1));
assert_eq!(true, compare(&empty, &singleton2));
assert_eq!(true, compare(&empty, &singleton3));
let mut errors1 = FxHashMap::default();
errors1.insert(1, vec![11]);
errors1.insert(2, vec![10]);
assert_eq!(false, compare(&errors1, &errors1));
assert_eq!(true, compare(&errors1, &singleton1));
assert_eq!(true, compare(&errors1, &singleton2));
assert_eq!(true, compare(&errors1, &singleton3));
}
}