Skip to content

Commit

Permalink
Provide opt options for verify and print after each pass (FuelLabs#…
Browse files Browse the repository at this point in the history
…4131)

## Description
This doesn't add any new option for `forc`. At the moment we have just
`-O1` and I believe that can stay as the default without additional
flags. We have the infrastructure now to add more optimization levels
and options with the new pass manager, and hence add the actual `forc`
options when necessary.

Closes FuelLabs#2399

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
  • Loading branch information
vaivaswatha authored Feb 20, 2023
1 parent a0202af commit 091b9a8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
22 changes: 20 additions & 2 deletions sway-ir/src/bin/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use std::{
};

use anyhow::anyhow;
use sway_ir::{register_known_passes, PassGroup, PassManager};
use sway_ir::{
insert_after_each, register_known_passes, PassGroup, PassManager, MODULEPRINTER_NAME,
MODULEVERIFIER_NAME,
};

// -------------------------------------------------------------------------------------------------

Expand All @@ -27,6 +30,12 @@ fn main() -> Result<(), anyhow::Error> {
for pass in config.passes {
passes.append_pass(pass);
}
if config.print_after_each {
passes = insert_after_each(passes, MODULEPRINTER_NAME);
}
if config.verify_after_each {
passes = insert_after_each(passes, MODULEVERIFIER_NAME);
}
pass_mgr.run(&mut ir, &passes)?;

// Write the output file or standard out.
Expand Down Expand Up @@ -72,7 +81,8 @@ struct Config {
input_path: Option<String>,
output_path: Option<String>,

_verify_each: bool,
verify_after_each: bool,
print_after_each: bool,
_time_passes: bool,
_stats: bool,

Expand Down Expand Up @@ -109,6 +119,14 @@ impl<'a, I: Iterator<Item = String>> ConfigBuilder<'a, I> {
match opt.as_str() {
"-i" => self.build_input(),
"-o" => self.build_output(),
"-verify-after-each" => {
self.cfg.verify_after_each = true;
self.build_root()
}
"-print-after-each" => {
self.cfg.print_after_each = true;
self.build_root()
}
"-h" => {
print!(
"Usage: opt [passname...] -i input_file -o output_file\n\n{}",
Expand Down
9 changes: 9 additions & 0 deletions sway-ir/src/pass_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,12 @@ pub fn create_o1_pass_group(is_predicate: bool) -> PassGroup {

o1
}

/// Utility to insert a pass after every pass in the given group
pub fn insert_after_each(pg: PassGroup, pass: &'static str) -> PassGroup {
PassGroup(
pg.0.into_iter()
.flat_map(|p_o_g| vec![p_o_g, PassOrGroup::Pass(pass)])
.collect(),
)
}

0 comments on commit 091b9a8

Please sign in to comment.