Fix waffle-util build.

This commit is contained in:
Chris Fallin 2023-02-07 17:31:52 -08:00
parent fd748dd493
commit 2d7e02deb0
2 changed files with 30 additions and 19 deletions

View file

@ -41,6 +41,19 @@ enum Command {
}, },
} }
fn apply_options(opts: &Options, module: &mut Module) -> Result<()> {
if opts.basic_opts || opts.max_ssa {
module.expand_all_funcs()?;
}
if opts.basic_opts {
module.per_func_body(|body| body.optimize());
}
if opts.max_ssa {
module.per_func_body(|body| body.convert_to_max_ssa());
}
Ok(())
}
fn main() -> Result<()> { fn main() -> Result<()> {
let opts = Options::from_args(); let opts = Options::from_args();
@ -50,29 +63,19 @@ fn main() -> Result<()> {
} }
let _ = logger.try_init(); let _ = logger.try_init();
match opts.command { match &opts.command {
Command::PrintIR { wasm } => { Command::PrintIR { wasm } => {
let bytes = std::fs::read(wasm)?; let bytes = std::fs::read(wasm)?;
debug!("Loaded {} bytes of Wasm data", bytes.len()); debug!("Loaded {} bytes of Wasm data", bytes.len());
let mut module = Module::from_wasm_bytes(&bytes[..])?; let mut module = Module::from_wasm_bytes(&bytes[..])?;
if opts.basic_opts { apply_options(&opts, &mut module)?;
module.optimize();
}
if opts.max_ssa {
module.convert_to_max_ssa();
}
println!("{}", module.display()); println!("{}", module.display());
} }
Command::RoundTrip { input, output } => { Command::RoundTrip { input, output } => {
let bytes = std::fs::read(input)?; let bytes = std::fs::read(input)?;
debug!("Loaded {} bytes of Wasm data", bytes.len()); debug!("Loaded {} bytes of Wasm data", bytes.len());
let mut module = Module::from_wasm_bytes(&bytes[..])?; let mut module = Module::from_wasm_bytes(&bytes[..])?;
if opts.basic_opts { apply_options(&opts, &mut module)?;
module.optimize();
}
if opts.max_ssa {
module.convert_to_max_ssa();
}
let produced = module.to_wasm_bytes()?; let produced = module.to_wasm_bytes()?;
std::fs::write(output, &produced[..])?; std::fs::write(output, &produced[..])?;
} }

View file

@ -34,10 +34,7 @@ impl<'a> FuncDecl<'a> {
pub fn optimize(&mut self) { pub fn optimize(&mut self) {
match self { match self {
FuncDecl::Body(_, body) => { FuncDecl::Body(_, body) => {
let cfg = crate::cfg::CFGInfo::new(body); body.optimize();
crate::passes::basic_opt::gvn(body, &cfg);
crate::passes::resolve_aliases::run(body);
crate::passes::empty_blocks::run(body);
} }
_ => {} _ => {}
} }
@ -46,8 +43,7 @@ impl<'a> FuncDecl<'a> {
pub fn convert_to_max_ssa(&mut self) { pub fn convert_to_max_ssa(&mut self) {
match self { match self {
FuncDecl::Body(_, body) => { FuncDecl::Body(_, body) => {
let cfg = crate::cfg::CFGInfo::new(body); body.convert_to_max_ssa();
crate::passes::maxssa::run(body, &cfg);
} }
_ => {} _ => {}
} }
@ -115,6 +111,18 @@ impl FunctionBody {
} }
} }
pub fn optimize(&mut self) {
let cfg = crate::cfg::CFGInfo::new(self);
crate::passes::basic_opt::gvn(self, &cfg);
crate::passes::resolve_aliases::run(self);
crate::passes::empty_blocks::run(self);
}
pub fn convert_to_max_ssa(&mut self) {
let cfg = crate::cfg::CFGInfo::new(self);
crate::passes::maxssa::run(self, &cfg);
}
pub fn add_block(&mut self) -> Block { pub fn add_block(&mut self) -> Block {
let id = self.blocks.push(BlockDef::default()); let id = self.blocks.push(BlockDef::default());
log::trace!("add_block: block {}", id); log::trace!("add_block: block {}", id);