forked from FuelLabs/sway
-
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.
PassManager: Refactor / separate into a module (FuelLabs#3788)
This PR moves code in preparation for a full fledged pass manager for the IR. Reference: FuelLabs#3596
- Loading branch information
1 parent
24d74e3
commit bb607e1
Showing
8 changed files
with
151 additions
and
159 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
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,57 @@ | ||
use crate::{Context, Function, IrError}; | ||
use std::collections::HashMap; | ||
|
||
pub trait NamedPass { | ||
fn name() -> &'static str; | ||
fn descr() -> &'static str; | ||
fn run(ir: &mut Context) -> Result<bool, IrError>; | ||
|
||
fn run_on_all_fns<F: FnMut(&mut Context, &Function) -> Result<bool, IrError>>( | ||
ir: &mut Context, | ||
mut run_on_fn: F, | ||
) -> Result<bool, IrError> { | ||
let funcs = ir | ||
.module_iter() | ||
.flat_map(|module| module.function_iter(ir)) | ||
.collect::<Vec<_>>(); | ||
let mut modified = false; | ||
for func in funcs { | ||
if run_on_fn(ir, &func)? { | ||
modified = true; | ||
} | ||
} | ||
Ok(modified) | ||
} | ||
} | ||
|
||
pub type NamePassPair = (&'static str, fn(&mut Context) -> Result<bool, IrError>); | ||
|
||
#[derive(Default)] | ||
pub struct PassManager { | ||
passes: HashMap<&'static str, NamePassPair>, | ||
} | ||
|
||
impl PassManager { | ||
pub fn register<T: NamedPass>(&mut self) { | ||
self.passes.insert(T::name(), (T::descr(), T::run)); | ||
} | ||
|
||
pub fn run(&self, name: &str, ir: &mut Context) -> Result<bool, IrError> { | ||
self.passes.get(name).expect("Unknown pass name!").1(ir) | ||
} | ||
|
||
pub fn contains(&self, name: &str) -> bool { | ||
self.passes.contains_key(name) | ||
} | ||
|
||
pub fn help_text(&self) -> String { | ||
let summary = self | ||
.passes | ||
.iter() | ||
.map(|(name, (descr, _))| format!(" {name:16} - {descr}")) | ||
.collect::<Vec<_>>() | ||
.join("\n"); | ||
|
||
format!("Valid pass names are:\n\n{summary}",) | ||
} | ||
} |