This commit is contained in:
Chris Fallin 2022-11-02 14:07:07 -07:00
parent 53f37a50fe
commit a244d4ab52
7 changed files with 58 additions and 9 deletions

View file

@ -29,6 +29,20 @@ impl<'a> Display for FunctionBodyDisplay<'a> {
ret_tys.join(", ")
)?;
for (value, value_def) in self.0.values.entries() {
match value_def {
ValueDef::Operator(..) | ValueDef::BlockParam(..) => {}
ValueDef::Alias(_alias_target) => {}
ValueDef::PickOutput(val, idx, ty) => {
writeln!(f, "{} {} = {}.{} # {}", self.1, value, val, idx, ty)?
}
ValueDef::Placeholder(ty) => {
writeln!(f, "{} {} = placeholder # {}", self.1, value, ty)?
}
ValueDef::None => panic!(),
}
}
for (block_id, block) in self.0.blocks.entries() {
let block_params = block
.params
@ -76,9 +90,6 @@ impl<'a> Display for FunctionBodyDisplay<'a> {
ValueDef::PickOutput(val, idx, ty) => {
writeln!(f, "{} {} = {}.{} # {}", self.1, inst, val, idx, ty)?;
}
ValueDef::Alias(v) => {
writeln!(f, "{} {} <- {}", self.1, inst, v)?;
}
_ => unreachable!(),
}
}

View file

@ -98,7 +98,8 @@ impl<'a> Module<'a> {
let mut module = frontend::wasm_to_ir(bytes)?;
for func_decl in module.funcs.values_mut() {
if let Some(body) = func_decl.body_mut() {
crate::passes::rpo::reorder_into_rpo(body);
crate::passes::rpo::run(body);
crate::passes::resolve_aliases::run(body);
}
}
Ok(module)

View file

@ -1,13 +1,15 @@
use super::{Block, Value, Type};
use super::{Block, Type, Value};
use crate::Operator;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub enum ValueDef {
BlockParam(Block, usize, Type),
Operator(Operator, Vec<Value>, Vec<Type>),
PickOutput(Value, usize, Type),
Alias(Value),
Placeholder(Type),
#[default]
None,
}
impl ValueDef {
@ -22,6 +24,7 @@ impl ValueDef {
&ValueDef::PickOutput(from, ..) => f(from),
&ValueDef::Alias(value) => f(value),
&ValueDef::Placeholder(_) => {}
&ValueDef::None => panic!(),
}
}
@ -36,6 +39,7 @@ impl ValueDef {
&mut ValueDef::PickOutput(ref mut from, ..) => f(from),
&mut ValueDef::Alias(ref mut value) => f(value),
&mut ValueDef::Placeholder(_) => {}
&mut ValueDef::None => panic!(),
}
}
}

View file

@ -12,8 +12,7 @@ mod frontend;
mod ir;
mod op_traits;
mod ops;
mod passes;
pub use passes::rpo::reorder_into_rpo;
pub mod passes;
pub use ir::*;
pub use ops::Operator;

View file

@ -1,3 +1,4 @@
//! Passes.
pub mod rpo;
pub mod resolve_aliases;

View file

@ -0,0 +1,33 @@
//! Resolve all aliases.
use crate::{FunctionBody, ValueDef};
pub fn run(body: &mut FunctionBody) {
for value in body.values.iter() {
let mut value_def = std::mem::take(&mut body.values[value]);
match &mut value_def {
ValueDef::Operator(_op, args, _tys) => {
for arg in args {
*arg = body.resolve_alias(*arg);
}
}
ValueDef::PickOutput(val, _idx, _ty) => {
*val = body.resolve_alias(*val);
}
ValueDef::Alias(val) => {
*val = body.resolve_alias(*val);
}
_ => {}
}
body.values[value] = value_def;
}
let mut blocks = std::mem::take(&mut body.blocks);
for block in blocks.values_mut() {
block.terminator.update_targets(|target| {
for arg in &mut target.args {
*arg = body.resolve_alias(*arg);
}
});
}
body.blocks = blocks;
}

View file

@ -136,7 +136,7 @@ impl RPO {
}
}
pub fn reorder_into_rpo(body: &mut FunctionBody) {
pub fn run(body: &mut FunctionBody) {
let rpo = RPO::compute(body);
// Remap entry block.
body.entry = rpo.map_block(body.entry);