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 Rust to Bril #412

Merged
merged 9 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
521 changes: 270 additions & 251 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ indexmap = "2.0"
fixedbitset = "0.4.2"
smallvec = "1.11.1"

bril2json = { git = "https://github.com/sampsyo/bril", rev = "daaff28" }
brilirs = { git = "https://github.com/sampsyo/bril", rev = "daaff28" }
bril-rs = { git = "https://github.com/sampsyo/bril", rev = "daaff28" }
syn = {version = "2.0", features = ["full", "extra-traits"]}
bril2json = { git = "https://github.com/ajpal/bril", rev = "a27a351" }
brilirs = { git = "https://github.com/ajpal/bril", rev = "a27a351" }
bril-rs = { git = "https://github.com/ajpal/bril", rev = "a27a351" }
rs2bril = { git = "https://github.com/ajpal/bril", rev = "a27a351", features = ["import"] }
ordered-float = { version = "3.7" }
serde_json = "1.0.103"

Expand Down
42 changes: 42 additions & 0 deletions example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
fn test(x: i64, f: f64, b: bool, ptr: [i64; 2], ptr2: &[[i64; 2]]) -> i64 {
let x: i64 = -1;
let y: bool = !(!true);
let z: f64 = -0.0;
z = -1.0;
f = 5.0;
f /= 2.0;
ptr[0] = 1;
ptr[1] = ptr[2 + x];

let foo : i64 = test2(1, 2);
test3();

let test: [i64;2] = [0, 1];
let test2: [[i64; 2]; 1] = [test];
let test3: [f64;10] = [z; 10];

if true {
let cond: i64 = 0;
while cond < 2 {
cond += 1;
}
} else {
if true {
let cond: bool = false;
while cond {}
}
}

println!("{}", x);

return x;
}


fn test2(x:i64, y:i64) -> i64 {
return x;
}

fn test3() {
return;
}
2 changes: 1 addition & 1 deletion infra/profile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trap cleanup EXIT
# TODO: take in file glob as command line argument
PROFILES=(tests/*.bril tests/small/*.bril tests/brils/passing/**/*.bril)

RUNMODES=("nothing" "rvsdg-round-trip" "optimize")
RUNMODES=("parse" "rvsdg-round-trip" "optimize")

# create temporary directory structure necessary for bench runs
mkdir -p ./tmp/bench
Expand Down
13 changes: 10 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::Parser;
use eggcc::util::{visualize, Run, RunType, TestProgram};
use std::path::PathBuf;
use std::{ffi::OsStr, path::PathBuf};

#[derive(Debug, Parser)]
struct Args {
Expand Down Expand Up @@ -31,7 +31,7 @@ fn main() {
let args = Args::parse();

if let Some(debug_dir) = args.debug_dir {
if let Result::Err(error) = visualize(TestProgram::File(args.file.clone()), debug_dir) {
if let Result::Err(error) = visualize(TestProgram::BrilFile(args.file.clone()), debug_dir) {
eprintln!("{}", error);
return;
}
Expand All @@ -45,8 +45,15 @@ fn main() {
return;
}

let file = match args.file.extension().and_then(OsStr::to_str) {
Some("rs") => TestProgram::RustFile(args.file.clone()),
Some("bril") => TestProgram::BrilFile(args.file.clone()),
Some(x) => panic!("unexpected file extension {x}"),
None => panic!("could not parse file extension"),
};

let run = Run {
prog_with_args: TestProgram::File(args.file.clone()).read_program(),
prog_with_args: file.read_program(),
test_type: args.run_mode,
interp: args.interp,
profile_out: args.profile_out,
Expand Down
41 changes: 36 additions & 5 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{EggCCError, Optimizer};
use bril_rs::Program;
use clap::ValueEnum;
use std::fmt::Debug;
use std::io::Read;
use std::{
ffi::OsStr,
fmt::{Display, Formatter},
Expand Down Expand Up @@ -144,7 +145,7 @@ where
pub enum RunType {
/// Do nothing to the input bril program besides parse it.
/// Output the original program.
Nothing,
Parse,
/// Convert the input bril program to the tree encoding, optimize the program
/// using egglog, and output the resulting bril program.
/// The default way to run this tool.
Expand Down Expand Up @@ -185,6 +186,8 @@ pub enum RunType {
OptimizeDirectJumps,
/// Convert the original program to a RVSDG and then to a CFG, outputting one SVG per function.
RvsdgToCfg,
/// Convert Rust to bril
RustToBril,
}

impl Display for RunType {
Expand All @@ -198,7 +201,7 @@ impl RunType {
/// that can be interpreted.
pub fn produces_interpretable(&self) -> bool {
match self {
RunType::Nothing => true,
RunType::Parse => true,
RunType::Optimize => true,
RunType::RvsdgConversion => false,
RunType::RvsdgRoundTrip => true,
Expand All @@ -214,6 +217,7 @@ impl RunType {
RunType::TreeOptimize => true,
RunType::Egglog => true,
RunType::CheckTreeIdentical => false,
RunType::RustToBril => true,
}
}
}
Expand All @@ -228,14 +232,15 @@ pub struct ProgWithArguments {
#[derive(Clone)]
pub enum TestProgram {
Prog(ProgWithArguments),
File(PathBuf),
BrilFile(PathBuf),
RustFile(PathBuf),
}

impl TestProgram {
pub fn read_program(self) -> ProgWithArguments {
match self {
TestProgram::Prog(prog) => prog,
TestProgram::File(path) => {
TestProgram::BrilFile(path) => {
let program_read = std::fs::read_to_string(path.clone()).unwrap();
let args = Optimizer::parse_bril_args(&program_read);
let program = Optimizer::parse_bril(&program_read).unwrap();
Expand All @@ -247,6 +252,21 @@ impl TestProgram {
args,
}
}
TestProgram::RustFile(path) => {
let mut src = String::new();
let mut file = std::fs::File::open(path.clone()).unwrap();

file.read_to_string(&mut src).unwrap();
let syntax = syn::parse_file(&src).unwrap();
let name = path.display().to_string();
let program = rs2bril::from_file_to_program(syntax, false, Some(name.clone()));

ProgWithArguments {
program,
name,
args: vec![],
}
}
}
}
}
Expand Down Expand Up @@ -340,7 +360,7 @@ impl Run {
};

let (visualizations, interpretable_out) = match self.test_type {
RunType::Nothing => (
RunType::Parse => (
vec![],
Some(Interpretable::Bril(self.prog_with_args.program.clone())),
),
Expand Down Expand Up @@ -522,6 +542,17 @@ impl Run {
Some(Interpretable::Bril(bril)),
)
}
RunType::RustToBril => {
let bril = &self.prog_with_args.program;
(
vec![Visualization {
result: bril.to_string(),
file_extension: ".bril".to_string(),
name: self.prog_with_args.name.clone(),
}],
Some(Interpretable::Bril(bril.clone())),
)
}
};

let result_interpreted = if !self.interp {
Expand Down
2 changes: 1 addition & 1 deletion tests/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn generate_tests(glob: &str) -> Vec<Trial> {

let snapshot = f.to_str().unwrap().contains("small");

for run in Run::all_configurations_for(TestProgram::File(f)) {
for run in Run::all_configurations_for(TestProgram::BrilFile(f)) {
mk_trial(run, snapshot);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tree_in_context/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ strum = "0.25"
strum_macros = "0.25"
main_error = "0.1.2"
thiserror = "1.0"
bril-rs = { git = "https://github.com/sampsyo/bril", rev = "daaff28" }
bril-rs = { git = "https://github.com/ajpal/bril", rev = "a27a351" }
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not merge yet!
Wait for sampsyo/bril#315 and then go back to depending on real bril

Loading