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

Support for dumping variables using 'printf' for testing scenarios where std is not avalible. #3

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions generate/src/generation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use mir::syntax::{
SwitchTargets, Terminator, TyId, TyKind, UnOp, VariantIdx,
};
use mir::tyctxt::TyCtxt;
use mir::VarDumper;
use rand::seq::SliceRandom;
use rand::{seq::IteratorRandom, Rng, RngCore, SeedableRng};
use rand_distr::{Distribution, WeightedError, WeightedIndex};
Expand Down Expand Up @@ -941,7 +942,7 @@ impl GenerationCtx {
for vars in dumpped.chunks(Program::DUMPER_ARITY) {
let new_bb = self.add_new_bb();

let args = if self.program.use_debug_dumper {
let args = if self.program.var_dumper == VarDumper::StdVarDumper || self.program.var_dumper == VarDumper::PrintfVarDumper{
let mut args = Vec::with_capacity(1 + Program::DUMPER_ARITY * 2);
args.push(Operand::Constant(
self.cursor.function.index().try_into().unwrap(),
Expand Down Expand Up @@ -1186,7 +1187,7 @@ impl GenerationCtx {
}
}

pub fn new(seed: u64, debug_dump: bool) -> Self {
pub fn new(seed: u64, debug_dump: VarDumper) -> Self {
let rng = RefCell::new(Box::new(rand::rngs::SmallRng::seed_from_u64(seed)));
let tcx = Rc::new(seed_tys(&mut *rng.borrow_mut()));
let ty_weights = TySelect::new(&tcx);
Expand Down
14 changes: 11 additions & 3 deletions generate/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::time::Instant;

use clap::{arg, command, value_parser};
use log::{debug, info};
use mir::serialize::Serialize;
use mir::{serialize::Serialize, VarDumper};

use crate::generation::GenerationCtx;

Expand All @@ -27,6 +27,7 @@ fn main() {
let matches = command!()
.args(&[
arg!(-d --debug "generate a program where values are printed instead of hashed (slow)"),
arg!(-p --printf_debug "generate a program where values are printed using the C 'printf' function instead of hashed (slow)"),
Comment on lines 29 to +30
Copy link
Owner

Choose a reason for hiding this comment

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

CLI stability is not a concern. So you could replace -d with a single enum flag with 3 options, defaulting to HashDumper

arg!(<seed> "generation seed").value_parser(value_parser!(u64)),
])
.get_matches();
Expand All @@ -35,12 +36,19 @@ fn main() {
.get_one::<u64>("seed")
.expect("need an integer as seed");
let debug_dump = matches.get_one::<bool>("debug").copied().unwrap_or(false);
let printf_dump = matches.get_one::<bool>("printf_debug").copied().unwrap_or(false);
let dumper = match (debug_dump,printf_dump){
(false,false)=>VarDumper::HashDumper,
(true,false)=>VarDumper::StdVarDumper,
(false,true)=>VarDumper::PrintfVarDumper,
(true,true)=>panic!("You can only choose either the `debug` dumper or `printf_debug` dumper, but both of them have been selected."),
};
info!("Generating a program with seed {seed}");
let genctxt = GenerationCtx::new(seed, debug_dump);
let genctxt = GenerationCtx::new(seed, dumper);
let time = Instant::now();
let (program, tcx) = genctxt.generate();
println!("{}", program.serialize(&tcx));
println!("{}", tcx.serialize());
println!("{}", tcx.serialize(dumper));
let dur = time.elapsed();
debug!("took {}s to generate", dur.as_secs_f32());
}
4 changes: 3 additions & 1 deletion generate/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ impl TySelect {
.unwrap();
}
trace!("Typing context with weights:\n{s}");
trace!("{}", tcx.serialize());
// FractalFir: serialization requires info about which dumper(printf-based one or not) is used. I pass `StdVarDumper` here to preserve
// previous behaviour.
trace!("{}", tcx.serialize(mir::VarDumper::StdVarDumper));
}

WeightedIndex::new(tcx.iter_enumerated().map(|(tyid, _)| weights[&tyid]))
Expand Down
10 changes: 10 additions & 0 deletions mir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,13 @@
pub mod serialize;
pub mod syntax;
pub mod tyctxt;
pub const ENABLE_PRINTF_DEBUG:bool = true;
#[derive(Clone,Copy,PartialEq, Eq)]
pub enum VarDumper{
/// Print variable hashes
HashDumper,
/// Print variables using standard rust formatting
StdVarDumper,
/// Print variables using the c `printf` function
PrintfVarDumper,
}
14 changes: 7 additions & 7 deletions mir/src/serialize.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{syntax::*, tyctxt::TyCtxt};
use crate::{syntax::*, tyctxt::TyCtxt, VarDumper};

pub trait Serialize {
fn serialize(&self, tcx: &TyCtxt) -> String;
Expand Down Expand Up @@ -316,11 +316,11 @@ impl Serialize for Body {
impl Serialize for Program {
fn serialize(&self, tcx: &TyCtxt) -> String {
let mut program = Program::HEADER.to_string();
if self.use_debug_dumper {
program += Program::DEBUG_DUMPER;
} else {
program += Program::DUMPER;
}
program += match self.var_dumper{
VarDumper::HashDumper=>Program::DUMPER,
VarDumper::StdVarDumper=>Program::DEBUG_DUMPER,
VarDumper::PrintfVarDumper=>Program::PRINTF_DUMPER,
};
program.extend(self.functions.iter_enumerated().map(|(idx, body)| {
let args_list: String = body
.args_iter()
Expand Down Expand Up @@ -359,7 +359,7 @@ impl Serialize for Program {
.expect("program has functions")
.identifier();

let hash_printer = if self.use_debug_dumper {
let hash_printer = if self.var_dumper != VarDumper::HashDumper {
""
} else {
r#"
Expand Down
Loading