Skip to content

Commit

Permalink
Pass Reorder Option (#2075)
Browse files Browse the repository at this point in the history
* reorder pass options

* interp bug

* clippy

* clippy again

* some more erros

* redo this
  • Loading branch information
calebmkim authored Jun 4, 2024
1 parent 3565623 commit 6ff2089
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 8 deletions.
53 changes: 49 additions & 4 deletions calyx-opt/src/pass_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,22 @@ impl PassManager {
&self,
incls: &[String],
excls: &[String],
insns: &[String],
) -> CalyxResult<(Vec<String>, HashSet<String>)> {
// Incls and excls can both have aliases in them. Resolve them.
let passes = incls
let mut insertions = insns
.iter()
.filter_map(|str| match str.split_once(':') {
Some((before, after)) => {
Some((before.to_string(), after.to_string()))
}
None => {
log::warn!("No ':' in {str}. Ignoring this option.");
None
}
})
.collect::<Vec<_>>();
// Incls and excls can have aliases in them. Resolve them.
let mut passes = incls
.iter()
.flat_map(|maybe_alias| self.resolve_alias(maybe_alias))
.collect::<Vec<_>>();
Expand All @@ -163,7 +176,7 @@ impl PassManager {
.collect::<HashSet<String>>();

// Validate that names of passes in incl and excl sets are known
passes.iter().chain(excl_set.iter()).try_for_each(|pass| {
passes.iter().chain(excl_set.iter().chain(insertions.iter().flat_map(|(pass1, pass2)| vec![pass1, pass2]))).try_for_each(|pass| {
if !self.passes.contains_key(pass) {
Err(Error::misc(format!(
"Unknown pass: {pass}. Run compiler with pass-help subcommand to view registered passes."
Expand All @@ -173,18 +186,50 @@ impl PassManager {
}
})?;

// Remove passes from `insertions` that are not slated to run.
insertions.retain(|(pass1, pass2)|
if !passes.contains(pass1) || excl_set.contains(pass1) {
log::warn!("Pass {pass1} is not slated to run. Reordering will have no effect.");
false
}
else if !passes.contains(pass2) || excl_set.contains(pass2) {
log::warn!("Pass {pass2} is not slated to run. Reordering will have no effect.");
false
}
else {
true
}
);

// Perform re-insertion.
// Insert `after` right after `before`. If `after` already appears after
// before, do nothing.
for (before, after) in insertions {
let before_idx =
passes.iter().position(|pass| *pass == before).unwrap();
let after_idx =
passes.iter().position(|pass| *pass == after).unwrap();
// Only need to perform re-insertion if it is actually out of order.
if before_idx > after_idx {
passes.insert(before_idx + 1, after);
passes.remove(after_idx);
}
}

Ok((passes, excl_set))
}

/// Executes a given "plan" constructed using the incl and excl lists.
/// ord is a relative ordering that should be enforced.
pub fn execute_plan(
&self,
ctx: &mut ir::Context,
incl: &[String],
excl: &[String],
insn: &[String],
dump_ir: bool,
) -> CalyxResult<()> {
let (passes, excl_set) = self.create_plan(incl, excl)?;
let (passes, excl_set) = self.create_plan(incl, excl, insn)?;

for name in passes {
// Pass is known to exist because create_plan validates the
Expand Down
2 changes: 1 addition & 1 deletion interp/src/debugger/cidr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl Debugger {
let pm = PassManager::default_passes()?;

// if !opts.skip_verification
pm.execute_plan(&mut ctx, &["validate".to_string()], &[], false)?;
pm.execute_plan(&mut ctx, &["validate".to_string()], &[], &[], false)?;

let entry_point = ctx.entrypoint;

Expand Down
2 changes: 1 addition & 1 deletion interp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ fn main() -> InterpreterResult<()> {
let pm = PassManager::default_passes()?;

if !opts.skip_verification {
pm.execute_plan(&mut ctx, &["validate".to_string()], &[], false)?;
pm.execute_plan(&mut ctx, &["validate".to_string()], &[], &[], false)?;
}

let command = opts.comm.unwrap_or(Command::Interpret(CommandInterpret {}));
Expand Down
4 changes: 4 additions & 0 deletions src/cmdline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ pub struct Opts {
#[argh(option, short = 'x', long = "extra-opt")]
pub extra_opts: Vec<String>,

/// establish a relative ordering of passes
#[argh(option, short = 'i', long = "insert")]
pub insertions: Vec<String>,

/// enable verbose printing
#[argh(option, long = "log", default = "log::LevelFilter::Warn")]
pub log_level: log::LevelFilter,
Expand Down
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,13 @@ fn main() -> CalyxResult<()> {
ctx.extra_opts = opts.extra_opts.drain(..).collect();

// Run all passes specified by the command line
pm.execute_plan(&mut ctx, &opts.pass, &opts.disable_pass, opts.dump_ir)?;
pm.execute_plan(
&mut ctx,
&opts.pass,
&opts.disable_pass,
&opts.insertions,
opts.dump_ir,
)?;

// Print out the Calyx program after transformation.
if opts.backend == BackendOpt::Calyx {
Expand Down
2 changes: 1 addition & 1 deletion web/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn compile(
// Build the IR representation
let mut rep = ir::from_ast::ast_to_ir(ws)?;

pm.execute_plan(&mut rep, passes, &[], false)?;
pm.execute_plan(&mut rep, passes, &[], &[], false)?;

let mut buffer: Vec<u8> = vec![];
for comp in &rep.components {
Expand Down

0 comments on commit 6ff2089

Please sign in to comment.