Skip to content

Commit

Permalink
Merge pull request #93 from Y-Nak/tweak-ir-writer
Browse files Browse the repository at this point in the history
Add newlines properly in `ModuleWriter`
  • Loading branch information
sbillig authored Nov 27, 2024
2 parents 434a2a8 + a8da965 commit 4510679
Show file tree
Hide file tree
Showing 20 changed files with 97 additions and 41 deletions.
24 changes: 16 additions & 8 deletions crates/codegen/src/critical_edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ mod tests {
let module = mb.build();
let func_ref = module.funcs()[0];
let mut cfg = ControlFlowGraph::default();
module.funcs.modify(func_ref, |func| {
module.func_store.modify(func_ref, |func| {
cfg.compute(func);
CriticalEdgeSplitter::new().run(func, &mut cfg);
});
Expand All @@ -189,7 +189,9 @@ mod tests {
);

let mut cfg_split = ControlFlowGraph::default();
module.funcs.view(func_ref, |func| cfg_split.compute(func));
module
.func_store
.view(func_ref, |func| cfg_split.compute(func));
assert_eq!(cfg, cfg_split);
}

Expand Down Expand Up @@ -233,7 +235,7 @@ mod tests {
let module = mb.build();
let func_ref = module.funcs()[0];
let mut cfg = ControlFlowGraph::default();
module.funcs.modify(func_ref, |func| {
module.func_store.modify(func_ref, |func| {
cfg.compute(func);
CriticalEdgeSplitter::new().run(func, &mut cfg);
});
Expand Down Expand Up @@ -266,7 +268,9 @@ mod tests {
);

let mut cfg_split = ControlFlowGraph::default();
module.funcs.view(func_ref, |func| cfg_split.compute(func));
module
.func_store
.view(func_ref, |func| cfg_split.compute(func));
assert_eq!(cfg, cfg_split);
}

Expand Down Expand Up @@ -300,7 +304,7 @@ mod tests {
let module = mb.build();
let func_ref = module.funcs()[0];
let mut cfg = ControlFlowGraph::default();
module.funcs.modify(func_ref, |func| {
module.func_store.modify(func_ref, |func| {
cfg.compute(func);
CriticalEdgeSplitter::new().run(func, &mut cfg);
});
Expand All @@ -325,7 +329,9 @@ mod tests {
);

let mut cfg_split = ControlFlowGraph::default();
module.funcs.view(func_ref, |func| cfg_split.compute(func));
module
.func_store
.view(func_ref, |func| cfg_split.compute(func));
assert_eq!(cfg, cfg_split);
}

Expand Down Expand Up @@ -367,7 +373,7 @@ mod tests {
let module = mb.build();
let func_ref = module.funcs()[0];
let mut cfg = ControlFlowGraph::default();
module.funcs.modify(func_ref, |func| {
module.func_store.modify(func_ref, |func| {
cfg.compute(func);
CriticalEdgeSplitter::new().run(func, &mut cfg);
});
Expand Down Expand Up @@ -403,7 +409,9 @@ mod tests {
);

let mut cfg_split = ControlFlowGraph::default();
module.funcs.view(func_ref, |func| cfg_split.compute(func));
module
.func_store
.view(func_ref, |func| cfg_split.compute(func));
assert_eq!(cfg, cfg_split);
}
}
8 changes: 4 additions & 4 deletions crates/codegen/src/domtree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ mod tests {

let module = mb.build();
let func_ref = module.funcs()[0];
let (dom_tree, df) = module.funcs.view(func_ref, calc_dom);
let (dom_tree, df) = module.func_store.view(func_ref, calc_dom);

assert_eq!(dom_tree.idom_of(entry_block), None);
assert_eq!(dom_tree.idom_of(then_block), Some(entry_block));
Expand Down Expand Up @@ -308,7 +308,7 @@ mod tests {

let module = mb.build();
let func_ref = module.funcs()[0];
let (dom_tree, df) = module.funcs.view(func_ref, calc_dom);
let (dom_tree, df) = module.func_store.view(func_ref, calc_dom);

assert_eq!(dom_tree.idom_of(a), None);
assert_eq!(dom_tree.idom_of(b), Some(a));
Expand Down Expand Up @@ -389,7 +389,7 @@ mod tests {

let module = mb.build();
let func_ref = module.funcs()[0];
let (dom_tree, df) = module.funcs.view(func_ref, calc_dom);
let (dom_tree, df) = module.func_store.view(func_ref, calc_dom);

assert_eq!(dom_tree.idom_of(a), None);
assert_eq!(dom_tree.idom_of(b), Some(a));
Expand Down Expand Up @@ -459,7 +459,7 @@ mod tests {

let module = mb.build();
let func_ref = module.funcs()[0];
let (dom_tree, df) = module.funcs.view(func_ref, calc_dom);
let (dom_tree, df) = module.func_store.view(func_ref, calc_dom);

assert_eq!(dom_tree.idom_of(a), None);
assert_eq!(dom_tree.idom_of(b), Some(a));
Expand Down
8 changes: 4 additions & 4 deletions crates/codegen/src/loop_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ mod tests {

let module = mb.build();
let func_ref = module.funcs()[0];
let lpt = &module.funcs.view(func_ref, compute_loop);
let lpt = &module.func_store.view(func_ref, compute_loop);

debug_assert_eq!(lpt.loop_num(), 1);
let lp0 = lpt.loops().next().unwrap();
Expand Down Expand Up @@ -371,7 +371,7 @@ mod tests {

let module = mb.build();
let func_ref = module.funcs()[0];
let lpt = &module.funcs.view(func_ref, compute_loop);
let lpt = &module.func_store.view(func_ref, compute_loop);

debug_assert_eq!(lpt.loop_num(), 1);
let lp0 = lpt.loops().next().unwrap();
Expand Down Expand Up @@ -413,7 +413,7 @@ mod tests {

let module = mb.build();
let func_ref = module.funcs()[0];
let lpt = &module.funcs.view(func_ref, compute_loop);
let lpt = &module.func_store.view(func_ref, compute_loop);

debug_assert_eq!(lpt.loop_num(), 1);
let lp0 = lpt.loops().next().unwrap();
Expand Down Expand Up @@ -485,7 +485,7 @@ mod tests {

let module = mb.build();
let func_ref = module.funcs()[0];
let lpt = &module.funcs.view(func_ref, compute_loop);
let lpt = &module.func_store.view(func_ref, compute_loop);

debug_assert_eq!(lpt.loop_num(), 4);
let l0 = lpt.loop_of_block(b1).unwrap();
Expand Down
8 changes: 4 additions & 4 deletions crates/codegen/src/post_domtree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ mod tests {

let module = mb.build();
let func_ref = module.funcs()[0];
let (post_dom_tree, pdf) = module.funcs.view(func_ref, calc_dom);
let (post_dom_tree, pdf) = module.func_store.view(func_ref, calc_dom);

assert!(post_dom_tree.is_reachable(entry_block));
assert!(post_dom_tree.is_reachable(else_block));
Expand Down Expand Up @@ -242,7 +242,7 @@ mod tests {

let module = mb.build();
let func_ref = module.funcs()[0];
let (post_dom_tree, pdf) = module.funcs.view(func_ref, calc_dom);
let (post_dom_tree, pdf) = module.func_store.view(func_ref, calc_dom);

assert!(!post_dom_tree.is_reachable(a));
assert!(test_pdf(&pdf, a, &[]));
Expand Down Expand Up @@ -281,7 +281,7 @@ mod tests {

let module = mb.build();
let func_ref = module.funcs()[0];
let (post_dom_tree, pdf) = module.funcs.view(func_ref, calc_dom);
let (post_dom_tree, pdf) = module.func_store.view(func_ref, calc_dom);

assert!(post_dom_tree.is_reachable(a));
assert!(post_dom_tree.is_reachable(b));
Expand Down Expand Up @@ -341,7 +341,7 @@ mod tests {

let module = mb.build();
let func_ref = module.funcs()[0];
let (post_dom_tree, pdf) = module.funcs.view(func_ref, calc_dom);
let (post_dom_tree, pdf) = module.func_store.view(func_ref, calc_dom);

assert!(post_dom_tree.is_reachable(a));
assert!(post_dom_tree.is_reachable(b));
Expand Down
2 changes: 1 addition & 1 deletion crates/filecheck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl<'a> FileChecker<'a> {
fn check_func(&mut self, parsed_module: &ParsedModule, func_ref: FuncRef) -> FileCheckResult {
let comments = &parsed_module.debug.func_comments[func_ref];

let (func_ir, func_name) = parsed_module.module.funcs.modify(func_ref, |func| {
let (func_ir, func_name) = parsed_module.module.func_store.modify(func_ref, |func| {
self.transformer.transform(func);
(
FuncWriter::with_debug_provider(func, func_ref, &parsed_module.debug).dump_string(),
Expand Down
2 changes: 1 addition & 1 deletion crates/interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Machine {
// Dummy pc
pc: InstId(0),
action: Action::Continue,
funcs: module.funcs.into_read_only(),
funcs: module.func_store.into_read_only(),
module_ctx: module.ctx,
memory: Vec::new(),
free_region: 0,
Expand Down
4 changes: 2 additions & 2 deletions crates/interpreter/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl TestCase {

fn parse(module: &ParsedModule, func_ref: FuncRef, comment: &str) -> Result<Self, String> {
let Some(caps) = PATTERN.captures(comment) else {
return module.module.funcs.view(func_ref, |func| {
return module.module.func_store.view(func_ref, |func| {
let func_name = func.sig.name();
Err(format_error(
func_name,
Expand Down Expand Up @@ -142,7 +142,7 @@ fn parse_value(module: &ParsedModule, func_ref: FuncRef, input: &str) -> Result<

Err(err) => {
let err = err.to_string();
return module.module.funcs.view(func_ref, |func| {
return module.module.func_store.view(func_ref, |func| {
let func_name = func.sig.name();
Err(format_error(func_name, &err.to_string()))
});
Expand Down
2 changes: 1 addition & 1 deletion crates/ir/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub mod test_util {
}

pub fn dump_func(module: &Module, func_ref: FuncRef) -> String {
module.funcs.view(func_ref, |func| {
module.func_store.view(func_ref, |func| {
FuncWriter::new(func_ref, func).dump_string()
})
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ir/src/builder/module_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl ModuleBuilder {

pub fn build(self) -> Module {
Module {
funcs: Arc::into_inner(self.funcs).unwrap(),
func_store: Arc::into_inner(self.funcs).unwrap(),
ctx: self.ctx,
}
}
Expand Down
8 changes: 8 additions & 0 deletions crates/ir/src/global_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ impl GlobalVariableStore {
pub fn all_gv_data(&self) -> impl Iterator<Item = &GlobalVariableData> {
self.gv_data.values()
}

pub fn len(&self) -> usize {
self.gv_data.len()
}

pub fn is_empty(&self) -> bool {
self.gv_data.is_empty()
}
}

/// An opaque reference to [`GlobalVariableData`].
Expand Down
2 changes: 1 addition & 1 deletion crates/ir/src/graphviz/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ mod test {
let func_ref = module.funcs()[0];

let mut text = vec![];
module.funcs.view(func_ref, |func| {
module.func_store.view(func_ref, |func| {
render_to(func, func_ref, &mut text).unwrap();
});
let text = String::from_utf8(text).unwrap();
Expand Down
18 changes: 15 additions & 3 deletions crates/ir/src/inst/data.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::io;

use macros::Inst;
use smallvec::SmallVec;

use crate::{inst::impl_inst_write, module::FuncRef, Type, ValueId};
use super::{Inst, InstWrite};
use crate::{inst::impl_inst_write, ir_writer::FuncWriteCtx, module::FuncRef, Type, ValueId};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)]
#[inst(side_effect(super::SideEffect::Read))]
Expand All @@ -21,7 +24,7 @@ pub struct Mstore {
value: ValueId,
ty: Type,
}
impl_inst_write!(Mstore, (value: ValueId, addr: ValueId, ty: Type));
impl_inst_write!(Mstore, (addr: ValueId, value: ValueId, ty: Type));

#[derive(Debug, Clone, PartialEq, Eq, Hash, Inst)]
pub struct Gep {
Expand All @@ -34,7 +37,16 @@ impl_inst_write!(Gep);
pub struct GetFunctionPtr {
func: FuncRef,
}
impl_inst_write!(GetFunctionPtr);
impl InstWrite for GetFunctionPtr {
fn write(&self, ctx: &FuncWriteCtx, w: &mut dyn io::Write) -> io::Result<()> {
let name = self.as_text();
ctx.func.ctx().func_sig(self.func, |sig| {
let callee = sig.name();
write!(w, "{name} %{callee}")
})?;
Ok(())
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Inst)]
#[inst(side_effect(super::SideEffect::Write))]
Expand Down
16 changes: 14 additions & 2 deletions crates/ir/src/inst/evm/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::io;

use macros::Inst;
pub mod inst_set;

use crate::{inst::impl_inst_write, module::FuncRef, value::ValueId};
use super::{Inst, InstWrite};
use crate::{inst::impl_inst_write, ir_writer::FuncWriteCtx, module::FuncRef, value::ValueId};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)]
pub struct EvmUdiv {
Expand Down Expand Up @@ -570,4 +573,13 @@ impl_inst_write!(EvmMalloc);
pub struct EvmContractSize {
contract: FuncRef,
}
impl_inst_write!(EvmContractSize);
impl InstWrite for EvmContractSize {
fn write(&self, ctx: &FuncWriteCtx, w: &mut dyn io::Write) -> io::Result<()> {
let name = self.as_text();
ctx.func.ctx().func_sig(self.contract, |sig| {
let callee = sig.name();
write!(w, "{name} %{callee}")
})?;
Ok(())
}
}
16 changes: 14 additions & 2 deletions crates/ir/src/ir_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,21 @@ impl<'a> ModuleWriter<'a> {
pub fn write(&mut self, w: &mut impl io::Write) -> io::Result<()> {
// Write target.
writeln!(w, "target = {}", self.module.ctx.triple)?;
writeln!(w)?;

// Write struct types defined in the module.
self.module.ctx.with_ty_store(|s| {
let mut has_type_def = false;
for s in s.all_struct_data() {
has_type_def = true;
s.write(&self.module.ctx, &mut *w)?;
writeln!(w)?;
}

if has_type_def {
writeln!(w)?;
}

io::Result::Ok(())
})?;

Expand All @@ -40,11 +48,15 @@ impl<'a> ModuleWriter<'a> {
writeln!(w)?;
}

if !s.is_empty() {
writeln!(w)?;
}

io::Result::Ok(())
})?;

for func_ref in self.module.funcs.funcs() {
self.module.funcs.view(func_ref, |func| {
for func_ref in self.module.func_store.funcs() {
self.module.func_store.view(func_ref, |func| {
let mut writer = FuncWriter::with_debug_provider(func, func_ref, self.dbg);
writer.write(&mut *w)
})?;
Expand Down
6 changes: 3 additions & 3 deletions crates/ir/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ use crate::{
};

pub struct Module {
pub funcs: FuncStore,
pub func_store: FuncStore,
pub ctx: ModuleCtx,
}

impl Module {
#[doc(hidden)]
pub fn new<T: Isa>(isa: &T) -> Self {
Self {
funcs: FuncStore::new(),
func_store: FuncStore::new(),
ctx: ModuleCtx::new(isa),
}
}

pub fn funcs(&self) -> Vec<FuncRef> {
self.funcs.funcs()
self.func_store.funcs()
}
}

Expand Down
Loading

0 comments on commit 4510679

Please sign in to comment.