Remove Fuel -- no longer needed

This commit is contained in:
Chris Fallin 2023-02-28 16:15:44 -08:00
parent 610c9710d3
commit 2713240bd0
10 changed files with 21 additions and 63 deletions

View file

@ -38,7 +38,7 @@ fuzz_target!(
let mut parsed_module = let mut parsed_module =
Module::from_wasm_bytes(&orig_bytes[..], &FrontendOptions::default()).unwrap(); Module::from_wasm_bytes(&orig_bytes[..], &FrontendOptions::default()).unwrap();
parsed_module.expand_all_funcs().unwrap(); parsed_module.expand_all_funcs().unwrap();
parsed_module.per_func_body(|body| body.optimize(&mut waffle::passes::Fuel::infinite())); parsed_module.per_func_body(|body| body.optimize());
let roundtrip_bytes = parsed_module.to_wasm_bytes().unwrap(); let roundtrip_bytes = parsed_module.to_wasm_bytes().unwrap();
if let Ok(filename) = std::env::var("FUZZ_DUMP_WASM") { if let Ok(filename) = std::env::var("FUZZ_DUMP_WASM") {

View file

@ -48,7 +48,7 @@ fuzz_target!(
} }
let mut opt_module = parsed_module.clone(); let mut opt_module = parsed_module.clone();
opt_module.per_func_body(|body| body.optimize(&mut waffle::Fuel::infinite())); opt_module.per_func_body(|body| body.optimize());
opt_module.per_func_body(|body| body.convert_to_max_ssa()); opt_module.per_func_body(|body| body.convert_to_max_ssa());
let mut opt_ctx = InterpContext::new(&opt_module).unwrap(); let mut opt_ctx = InterpContext::new(&opt_module).unwrap();

View file

@ -1,7 +1,7 @@
#![no_main] #![no_main]
use libfuzzer_sys::fuzz_target; use libfuzzer_sys::fuzz_target;
use waffle::{FrontendError, FrontendOptions, Fuel, Module}; use waffle::{FrontendError, FrontendOptions, Module};
fuzz_target!(|module: wasm_smith::Module| { fuzz_target!(|module: wasm_smith::Module| {
let _ = env_logger::try_init(); let _ = env_logger::try_init();
@ -26,6 +26,6 @@ fuzz_target!(|module: wasm_smith::Module| {
} }
}; };
parsed_module.expand_all_funcs().unwrap(); parsed_module.expand_all_funcs().unwrap();
parsed_module.per_func_body(|body| body.optimize(&mut Fuel::infinite())); parsed_module.per_func_body(|body| body.optimize());
let _ = parsed_module.to_wasm_bytes(); let _ = parsed_module.to_wasm_bytes();
}); });

View file

@ -57,7 +57,7 @@ enum Command {
fn apply_options(opts: &Options, module: &mut Module) -> Result<()> { fn apply_options(opts: &Options, module: &mut Module) -> Result<()> {
module.expand_all_funcs()?; module.expand_all_funcs()?;
if opts.basic_opts { if opts.basic_opts {
module.per_func_body(|body| body.optimize(&mut waffle::passes::Fuel::infinite())); module.per_func_body(|body| body.optimize());
} }
if opts.max_ssa { if opts.max_ssa {
module.per_func_body(|body| body.convert_to_max_ssa()); module.per_func_body(|body| body.convert_to_max_ssa());

View file

@ -3,7 +3,6 @@ use crate::cfg::CFGInfo;
use crate::entity::{EntityRef, EntityVec, PerEntity}; use crate::entity::{EntityRef, EntityVec, PerEntity};
use crate::frontend::parse_body; use crate::frontend::parse_body;
use crate::ir::SourceLoc; use crate::ir::SourceLoc;
use crate::passes::Fuel;
use anyhow::Result; use anyhow::Result;
use std::ops::Range; use std::ops::Range;
@ -49,11 +48,11 @@ impl<'a> FuncDecl<'a> {
} }
} }
pub fn optimize(&mut self, fuel: &mut Fuel) { pub fn optimize(&mut self) {
self.mark_dirty(); self.mark_dirty();
match self { match self {
FuncDecl::Body(_, _, body) => { FuncDecl::Body(_, _, body) => {
body.optimize(fuel); body.optimize();
} }
_ => {} _ => {}
} }
@ -163,12 +162,12 @@ impl FunctionBody {
} }
} }
pub fn optimize(&mut self, fuel: &mut Fuel) { pub fn optimize(&mut self) {
let cfg = crate::cfg::CFGInfo::new(self); let cfg = crate::cfg::CFGInfo::new(self);
crate::passes::remove_phis::run(self, &cfg, fuel); crate::passes::remove_phis::run(self, &cfg);
crate::passes::basic_opt::gvn(self, &cfg, fuel); crate::passes::basic_opt::gvn(self, &cfg);
crate::passes::remove_phis::run(self, &cfg, fuel); crate::passes::remove_phis::run(self, &cfg);
crate::passes::empty_blocks::run(self, fuel); crate::passes::empty_blocks::run(self);
} }
pub fn convert_to_max_ssa(&mut self) { pub fn convert_to_max_ssa(&mut self) {

View file

@ -14,7 +14,6 @@ mod ir;
mod op_traits; mod op_traits;
mod ops; mod ops;
pub mod passes; pub mod passes;
pub use passes::Fuel;
mod scoped_map; mod scoped_map;
pub use errors::*; pub use errors::*;

View file

@ -8,29 +8,3 @@ pub mod remove_phis;
pub mod resolve_aliases; pub mod resolve_aliases;
pub mod ssa; pub mod ssa;
pub mod trace; pub mod trace;
#[derive(Clone, Debug)]
pub struct Fuel {
pub remaining: u64,
pub consumed: u64,
}
impl Fuel {
pub fn consume(&mut self) -> bool {
self.consumed += 1;
if self.remaining == u64::MAX {
return true;
}
if self.remaining == 0 {
false
} else {
self.remaining -= 1;
true
}
}
pub fn infinite() -> Fuel {
Fuel {
consumed: 0,
remaining: u64::MAX,
}
}
}

View file

@ -4,28 +4,25 @@ use crate::cfg::CFGInfo;
use crate::interp::{const_eval, ConstVal}; use crate::interp::{const_eval, ConstVal};
use crate::ir::*; use crate::ir::*;
use crate::passes::dom_pass::{dom_pass, DomtreePass}; use crate::passes::dom_pass::{dom_pass, DomtreePass};
use crate::passes::Fuel;
use crate::scoped_map::ScopedMap; use crate::scoped_map::ScopedMap;
use crate::Operator; use crate::Operator;
pub fn gvn(body: &mut FunctionBody, cfg: &CFGInfo, fuel: &mut Fuel) { pub fn gvn(body: &mut FunctionBody, cfg: &CFGInfo) {
dom_pass::<GVNPass<'_>>( dom_pass::<GVNPass>(
body, body,
cfg, cfg,
&mut GVNPass { &mut GVNPass {
map: ScopedMap::default(), map: ScopedMap::default(),
fuel,
}, },
); );
} }
#[derive(Debug)] #[derive(Debug)]
struct GVNPass<'a> { struct GVNPass {
map: ScopedMap<ValueDef, Value>, map: ScopedMap<ValueDef, Value>,
fuel: &'a mut Fuel,
} }
impl<'a> DomtreePass for GVNPass<'a> { impl DomtreePass for GVNPass {
fn enter(&mut self, block: Block, body: &mut FunctionBody) { fn enter(&mut self, block: Block, body: &mut FunctionBody) {
self.map.push_level(); self.map.push_level();
self.optimize(block, body); self.optimize(block, body);
@ -43,7 +40,7 @@ fn value_is_pure(value: Value, body: &FunctionBody) -> bool {
} }
} }
impl<'a> GVNPass<'a> { impl GVNPass {
fn optimize(&mut self, block: Block, body: &mut FunctionBody) { fn optimize(&mut self, block: Block, body: &mut FunctionBody) {
let mut i = 0; let mut i = 0;
while i < body.blocks[block].insts.len() { while i < body.blocks[block].insts.len() {
@ -111,10 +108,6 @@ impl<'a> GVNPass<'a> {
} }
if let Some(value) = self.map.get(&value) { if let Some(value) = self.map.get(&value) {
if !self.fuel.consume() {
return;
}
body.set_alias(inst, *value); body.set_alias(inst, *value);
i -= 1; i -= 1;
body.blocks[block].insts.remove(i); body.blocks[block].insts.remove(i);

View file

@ -1,6 +1,5 @@
//! Pass to remove empty blocks. //! Pass to remove empty blocks.
use super::Fuel;
use crate::entity::EntityRef; use crate::entity::EntityRef;
use crate::ir::{Block, BlockTarget, FunctionBody, Terminator}; use crate::ir::{Block, BlockTarget, FunctionBody, Terminator};
@ -38,7 +37,7 @@ fn rewrite_target(
forwardings[target.block.index()].clone() forwardings[target.block.index()].clone()
} }
pub fn run(body: &mut FunctionBody, fuel: &mut Fuel) { pub fn run(body: &mut FunctionBody) {
log::trace!( log::trace!(
"empty_blocks: running on func:\n{}\n", "empty_blocks: running on func:\n{}\n",
body.display_verbose("| ", None) body.display_verbose("| ", None)
@ -62,11 +61,9 @@ pub fn run(body: &mut FunctionBody, fuel: &mut Fuel) {
for block_data in body.blocks.values_mut() { for block_data in body.blocks.values_mut() {
block_data.terminator.update_targets(|target| { block_data.terminator.update_targets(|target| {
if let Some(new_target) = rewrite_target(&forwardings[..], target) { if let Some(new_target) = rewrite_target(&forwardings[..], target) {
if fuel.consume() {
log::trace!("empty_blocks: replacing {:?} with {:?}", target, new_target); log::trace!("empty_blocks: replacing {:?} with {:?}", target, new_target);
*target = new_target; *target = new_target;
} }
}
}); });
} }

View file

@ -1,6 +1,5 @@
//! Remove-useless-phis (blockparams) pass. //! Remove-useless-phis (blockparams) pass.
use super::Fuel;
use crate::cfg::CFGInfo; use crate::cfg::CFGInfo;
use crate::ir::*; use crate::ir::*;
@ -30,7 +29,7 @@ fn delete_indices<T: Copy>(vec: &mut Vec<T>, indices: &[usize]) {
} }
} }
pub fn run(func: &mut FunctionBody, cfg: &CFGInfo, fuel: &mut Fuel) { pub fn run(func: &mut FunctionBody, cfg: &CFGInfo) {
// For every block, collect the arg-lists of preds. If a given // For every block, collect the arg-lists of preds. If a given
// blockparam has all the same values for an arg, replace the // blockparam has all the same values for an arg, replace the
// blockparam value with an alias to that one value, and then // blockparam value with an alias to that one value, and then
@ -73,9 +72,6 @@ pub fn run(func: &mut FunctionBody, cfg: &CFGInfo, fuel: &mut Fuel) {
.map(|arglist| func.resolve_alias(arglist[i])), .map(|arglist| func.resolve_alias(arglist[i])),
); );
if let Some(val) = same { if let Some(val) = same {
if !fuel.consume() {
continue;
}
if val != blockparam { if val != blockparam {
log::trace!( log::trace!(
"deleting blockparam {} from block {}: now {}", "deleting blockparam {} from block {}: now {}",