-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dataflow): Implement and thoroughly test live variable analysis
- Loading branch information
1 parent
8c5c14c
commit 4b8077d
Showing
14 changed files
with
670 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import sys, subprocess, json | ||
import multiprocessing | ||
import difflib | ||
|
||
|
||
def parse_args(): | ||
if len(sys.argv) < 4: | ||
print( | ||
"usage: python3 match_outputs.py <oracle> <tested> <file>... [--exclude <file>]..." | ||
) | ||
oracle = sys.argv[1] | ||
tested = sys.argv[2] | ||
args = sys.argv[3:] | ||
filenames = [] | ||
exclude = [] | ||
all_are_args = False | ||
next_is_exclude = False | ||
for arg in args: | ||
if all_are_args: | ||
filenames.append(arg) | ||
elif arg == "--": | ||
all_are_args = True | ||
elif arg == "--exclude": | ||
next_is_exclude = True | ||
else: | ||
if next_is_exclude: | ||
exclude.append(arg) | ||
next_is_exclude = False | ||
else: | ||
filenames.append(arg) | ||
return ( | ||
oracle, | ||
tested, | ||
[ | ||
filename | ||
for filename in filenames | ||
if not any(isinstance(e, str) and filename.endswith(e) for e in exclude) | ||
], | ||
) | ||
|
||
|
||
def init_worker(shared_failure_event, shared_oracle, shared_tested): | ||
global failure_event | ||
global oracle | ||
global tested | ||
failure_event = shared_failure_event | ||
oracle = shared_oracle | ||
tested = shared_tested | ||
|
||
|
||
def check_file(file): | ||
oracle_output = subprocess.getoutput(f"bril2json <{file} | {oracle}") | ||
my_output = subprocess.getoutput(f"bril2json <{file} | {tested}") | ||
if oracle_output == my_output: | ||
print(f"\x1b[32m{file} OK\x1b[m") | ||
else: | ||
print(f"\x1b[31m{file} ERROR\x1b[m") | ||
failure_event.set() | ||
|
||
red = lambda text: f"\033[38;2;255;0;0m{text}\033[m" | ||
green = lambda text: f"\033[38;2;0;255;0m{text}\033[m" | ||
blue = lambda text: f"\033[38;2;0;0;255m{text}\033[m" | ||
white = lambda text: f"\033[38;2;255;255;255m{text}\033[m" | ||
gray = lambda text: f"\033[38;2;128;128;128m{text}\033[m" | ||
|
||
diff = difflib.ndiff(oracle_output.splitlines(), my_output.splitlines()) | ||
print("--- DIFF ---") | ||
for line in diff: | ||
if line.startswith("+"): | ||
print(green(line)) | ||
elif line.startswith("-"): | ||
print(red(line)) | ||
elif line.startswith("^"): | ||
print(blue(line)) | ||
elif line.startswith("?"): | ||
print(gray(line)) | ||
else: | ||
print(white(line)) | ||
|
||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) < 3: | ||
print("usage: python3 test.py <oracle> <tested> <file>...") | ||
sys.exit(1) | ||
(oracle, tested, files) = parse_args() | ||
|
||
with multiprocessing.Manager() as manager: | ||
failure_event = manager.Event() | ||
|
||
with multiprocessing.Pool( | ||
multiprocessing.cpu_count(), | ||
initializer=init_worker, | ||
initargs=(failure_event, oracle, tested), | ||
) as pool: | ||
pool.imap_unordered(check_file, files) | ||
pool.close() | ||
pool.join() | ||
if failure_event.is_set(): | ||
print("Exiting due to errors") | ||
pool.terminate() | ||
sys.exit(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use std::collections::HashSet; | ||
|
||
use bril_util::InstructionExt; | ||
use build_cfg::{BasicBlock, BasicBlockIdx, FunctionCfg}; | ||
|
||
use crate::{solve_dataflow, Direction}; | ||
|
||
#[derive(Debug, PartialEq, Eq, Hash, Clone)] | ||
pub struct Variable(String); | ||
|
||
fn transfer( | ||
block: &BasicBlock, | ||
_block_idx: BasicBlockIdx, | ||
mut outputs: HashSet<Variable>, | ||
) -> HashSet<Variable> { | ||
let mut kill_set = HashSet::new(); | ||
let mut gen_set = HashSet::new(); | ||
for instruction in &block.instructions { | ||
gen_set.extend( | ||
instruction | ||
.gen() | ||
.iter() | ||
.filter(|variable| !kill_set.contains(variable)) | ||
.map(|variable| Variable(variable.to_string())), | ||
); | ||
if let Some(kill) = instruction.kill() { | ||
kill_set.insert(kill); | ||
outputs.remove(&Variable(kill.clone())); | ||
} | ||
} | ||
outputs.extend(gen_set); | ||
outputs | ||
} | ||
|
||
pub fn live_variables(cfg: &FunctionCfg) { | ||
println!("@{} {{", cfg.signature.name); | ||
for (block, solution) in solve_dataflow( | ||
cfg, | ||
Direction::Backward, | ||
HashSet::new(), | ||
|lhs, rhs| lhs.union(rhs).cloned().collect(), | ||
transfer, | ||
) { | ||
if let Some(label) = &cfg.vertices[block].label { | ||
println!(" .{}", label.name); | ||
} | ||
let mut variables = solution | ||
.into_iter() | ||
.map(|variable| variable.0) | ||
.collect::<Vec<_>>(); | ||
variables.sort(); | ||
println!( | ||
" in: {}", | ||
if variables.is_empty() { | ||
"∅".to_string() | ||
} else { | ||
variables.join(", ") | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
lesson4/dataflow/turnt/df_copied_from_bril/cond-args.live.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
@main { | ||
in: cond | ||
.left | ||
in: a | ||
.right | ||
in: ∅ | ||
.end | ||
in: a, c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
@main { | ||
in: ∅ | ||
.left | ||
in: a | ||
.right | ||
in: ∅ | ||
.end | ||
in: a, c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
@main { | ||
in: ∅ | ||
.header | ||
in: i, result | ||
.body | ||
in: i, result | ||
.end | ||
in: result |
Oops, something went wrong.