Skip to content

Commit

Permalink
Fix IR writer
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Nak committed Dec 4, 2024
1 parent 6315bef commit 5c79442
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 43 deletions.
8 changes: 4 additions & 4 deletions crates/codegen/src/critical_edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ mod tests {

assert_eq!(
dump_func(&module, func_ref),
"func public %test_func() -> unit {
"func public %test_func() {
block0:
br 1.i32 block3 block1;
Expand Down Expand Up @@ -242,7 +242,7 @@ mod tests {

assert_eq!(
dump_func(&module, func_ref),
"func public %test_func() -> unit {
"func public %test_func() {
block0:
br 1.i8 block5 block1;
Expand Down Expand Up @@ -310,7 +310,7 @@ mod tests {
});
assert_eq!(
dump_func(&module, func_ref),
"func public %test_func() -> unit {
"func public %test_func() {
block0:
jump block1;
Expand Down Expand Up @@ -380,7 +380,7 @@ mod tests {

assert_eq!(
dump_func(&module, func_ref),
"func public %test_func() -> unit {
"func public %test_func() {
block0:
br 1.i1 block5 block6;
Expand Down
2 changes: 1 addition & 1 deletion crates/filecheck/fixtures/sccp/non_folding.sntn
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

target = "evm-ethereum-london"

# sameln: func public %non_folding(v0.*i64) -> unit {
# sameln: func public %non_folding(v0.*i64) {
# nextln: block1:
# nextln: v1.i64 = evm_sload v0;
# nextln: v2.i64 = add 1.i64 v0;
Expand Down
6 changes: 3 additions & 3 deletions crates/ir/src/builder/func_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ mod tests {
let func_ref = module.funcs()[0];
assert_eq!(
dump_func(&module, func_ref),
"func public %test_func() -> unit {
"func public %test_func() {
block0:
v2.i8 = add 1.i8 2.i8;
v3.i8 = sub v2 1.i8;
Expand Down Expand Up @@ -343,7 +343,7 @@ mod tests {
let func_ref = module.funcs()[0];
assert_eq!(
dump_func(&module, func_ref),
"func public %test_func(v0.i32, v1.i64) -> unit {
"func public %test_func(v0.i32, v1.i64) {
block0:
v2.i64 = sext v0 i64;
v3.i64 = mul v2 v1;
Expand Down Expand Up @@ -422,7 +422,7 @@ mod tests {
let func_ref = module.funcs()[0];
assert_eq!(
dump_func(&module, func_ref),
"func public %test_func(v0.i64) -> unit {
"func public %test_func(v0.i64) {
block0:
br v0 block1 block2;
Expand Down
12 changes: 6 additions & 6 deletions crates/ir/src/builder/ssa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ mod tests {

assert_eq!(
dump_func(&module, func_ref),
"func public %test_func() -> unit {
"func public %test_func() {
block0:
v1.i32 = add 1.i32 1.i32;
return;
Expand Down Expand Up @@ -316,7 +316,7 @@ mod tests {

assert_eq!(
dump_func(&module, func_ref),
"func public %test_func() -> unit {
"func public %test_func() {
block0:
br 1.i32 block2 block1;
Expand Down Expand Up @@ -407,7 +407,7 @@ mod tests {

assert_eq!(
dump_func(&module, func_ref),
"func public %test_func() -> unit {
"func public %test_func() {
block0:
br 0.i32 block1 block2;
Expand Down Expand Up @@ -487,7 +487,7 @@ mod tests {

assert_eq!(
dump_func(&module, func_ref),
"func public %test_func() -> unit {
"func public %test_func() {
block0:
jump block1;
Expand Down Expand Up @@ -572,7 +572,7 @@ mod tests {

assert_eq!(
dump_func(&module, func_ref),
"func public %test_func() -> unit {
"func public %test_func() {
block0:
jump block1;
Expand Down Expand Up @@ -662,7 +662,7 @@ mod tests {

assert_eq!(
dump_func(&module, func_ref),
"func public %test_func() -> unit {
"func public %test_func() {
block0:
jump block1;
Expand Down
10 changes: 8 additions & 2 deletions crates/ir/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,13 @@ where
self.linkage.write(w, ctx)?;
write!(w, " %{}(", self.name)?;
self.args.write_with_delim(w, " ", ctx)?;
write!(w, ") -> ")?;
self.ret_ty.write(w, ctx)
write!(w, ")")?;

if !self.ret_ty.is_unit() {
write!(w, " -> ")?;
self.ret_ty.write(w, ctx)?;
}

Ok(())
}
}
24 changes: 12 additions & 12 deletions crates/ir/src/graphviz/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,19 @@ mod test {
});
let text = String::from_utf8(text).unwrap();

let expected = "digraph test_func {
block3[label=<<table border=\"0\" cellborder=\"1\" cellspacing=\"0\"><tr><td bgcolor=\"gray\" align=\"center\" colspan=\"1\">block3</td></tr><tr><td align=\"left\" balign=\"left\">v3.i64 = phi (1.i64 block1) (2.i64 block2);<br/>v4.i64 = add v3 v0;<br/>return;<br/></td></tr></table>>][shape=\"none\"];
block2[label=<<table border=\"0\" cellborder=\"1\" cellspacing=\"0\"><tr><td bgcolor=\"gray\" align=\"center\" colspan=\"1\">block2</td></tr><tr><td align=\"left\" balign=\"left\">jump block3;<br/></td></tr></table>>][shape=\"none\"];
block1[label=<<table border=\"0\" cellborder=\"1\" cellspacing=\"0\"><tr><td bgcolor=\"gray\" align=\"center\" colspan=\"1\">block1</td></tr><tr><td align=\"left\" balign=\"left\">jump block3;<br/></td></tr></table>>][shape=\"none\"];
block0[label=<<table border=\"0\" cellborder=\"1\" cellspacing=\"0\"><tr><td bgcolor=\"gray\" align=\"center\" colspan=\"1\">block0</td></tr><tr><td align=\"left\" balign=\"left\">br v0 block1 block2;<br/></td></tr></table>>][shape=\"none\"];
dummy_block[label=\"func public %test_func(i64) -> unit\"][shape=\"none\"];
dummy_block -> block0[label=\"\"][style=\"invis\"];
block2 -> block3[label=\"2.i64\"];
block1 -> block3[label=\"1.i64\"];
block0 -> block1[label=\"\"];
block0 -> block2[label=\"\"];
let expected = r#"digraph test_func {
block3[label=<<table border="0" cellborder="1" cellspacing="0"><tr><td bgcolor="gray" align="center" colspan="1">block3</td></tr><tr><td align="left" balign="left">v3.i64 = phi (1.i64 block1) (2.i64 block2);<br/>v4.i64 = add v3 v0;<br/>return;<br/></td></tr></table>>][shape="none"];
block2[label=<<table border="0" cellborder="1" cellspacing="0"><tr><td bgcolor="gray" align="center" colspan="1">block2</td></tr><tr><td align="left" balign="left">jump block3;<br/></td></tr></table>>][shape="none"];
block1[label=<<table border="0" cellborder="1" cellspacing="0"><tr><td bgcolor="gray" align="center" colspan="1">block1</td></tr><tr><td align="left" balign="left">jump block3;<br/></td></tr></table>>][shape="none"];
block0[label=<<table border="0" cellborder="1" cellspacing="0"><tr><td bgcolor="gray" align="center" colspan="1">block0</td></tr><tr><td align="left" balign="left">br v0 block1 block2;<br/></td></tr></table>>][shape="none"];
dummy_block[label="func public %test_func(i64)"][shape="none"];
dummy_block -> block0[label=""][style="invis"];
block2 -> block3[label="2.i64"];
block1 -> block3[label="1.i64"];
block0 -> block1[label=""];
block0 -> block2[label=""];
}
";
"#;
assert_eq!(text, expected);
}
}
37 changes: 33 additions & 4 deletions crates/ir/src/ir_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,29 @@ impl<'a> ModuleWriter<'a> {
io::Result::Ok(())
})?;

for func_ref in self.module.func_store.funcs() {
let (func_defs, func_decls): (Vec<_>, Vec<_>) = self
.module
.func_store
.funcs()
.into_iter()
.partition(|func_ref| {
self.module
.ctx
.func_sig(*func_ref, |sig| sig.linkage().has_definition())
});
// Write an external functions.
for &func_ref in &func_decls {
self.module.ctx.func_sig(func_ref, |sig| {
write!(w, "declare ")?;
sig.write(w, &self.module.ctx)?;
writeln!(w, ";")
})?;
}
if !func_decls.is_empty() && !func_defs.is_empty() {
writeln!(w)?;
}

for func_ref in func_defs {
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 Expand Up @@ -150,11 +172,18 @@ impl<'a> FuncWriter<'a> {
.map(|value| ValueWithTy(*value))
.collect();
arg_values.write_with_delim(w, ", ", &self.ctx)?;
write!(w, ")")?;

write!(w, ") -> ")?;
m_ctx.func_sig(func_ref, |sig| sig.ret_ty().write(w, &self.ctx))?;
writeln!(w, " {{")?;
m_ctx.func_sig(func_ref, |sig| {
if !sig.ret_ty().is_unit() {
write!(w, " -> ")?;
sig.ret_ty().write(w, &self.ctx)
} else {
Ok(())
}
})?;

writeln!(w, " {{")?;
self.level += 1;

for block in self.ctx.func.layout.iter_block() {
Expand Down
2 changes: 1 addition & 1 deletion crates/ir/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ where
}
}

/// An opaque reference to [`CompoundTypeData`].
/// An opaque reference to [`CompoundType`].
#[derive(Debug, Clone, PartialEq, Eq, Copy, Hash, PartialOrd, Ord)]
pub struct CompoundTypeRef(u32);
cranelift_entity::entity_impl!(CompoundTypeRef);
Expand Down
2 changes: 1 addition & 1 deletion crates/parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ derive_more = { version = "=1.0.0", default-features = false, features = [
smallvec = "1.13.2"

[dev-dependencies]
dir-test = "0.3"
dir-test = "0.4"
insta = { version = "1.41" }
indenter = "0.3.3"
2 changes: 1 addition & 1 deletion crates/parser/test_files/syntax/module/func_type.ir.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ input_file: test_files/syntax/module/func_type.sntn
---
target = evm-ethereum-london

func public %higher_order(v0.*(i256, i256) -> i32) -> unit {
func public %higher_order(v0.*(i256, i256) -> i32) {
block0:
return;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ global private [i8; 4] $ARRAY = [0, 1, 2, 3]
global private @foo $FOO = {1, 2, 3}
global private [@foo; 2] $FOO_ARRAY = [{1, 2, 3}, {3, 4, 5}]

func public %main() -> unit {
func public %main() {
block0:
v0.i256 = mload $PTR i256;
return;
Expand Down
2 changes: 1 addition & 1 deletion crates/parser/test_files/syntax/module/newlines.ir.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ input_file: test_files/syntax/module/newlines.sntn
---
target = evm-ethereum-london

func public %main() -> unit {
func public %main() {
block0:
v0.i8 = add 1.i8 2.i8;
return;
Expand Down
9 changes: 4 additions & 5 deletions crates/parser/test_files/syntax/module/simple.ir.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ target = evm-ethereum-london
type @foo = {i8, i16, *i64};
type @bar = {i8, [i8; 31]};

func external %add_i8(v0.i8, v1.i8) -> i8 {
}
declare func external %add_i8(i8 i8) -> i8;

func public %main() -> unit {
func public %main() {
block0:
v0.i8 = call %foo 100.i8;
return;
Expand All @@ -23,12 +22,12 @@ func private %foo(v0.i8) -> i8 {
return v2;
}

func private %types(v0.*i8, v1.[i8; 2], v2.[*i8; 2], v3.[[i8; 2]; 2], v4.@foo, v5.*@foo) -> unit {
func private %types(v0.*i8, v1.[i8; 2], v2.[*i8; 2], v3.[[i8; 2]; 2], v4.@foo, v5.*@foo) {
block0:
return;
}

func private %table(v0.i8) -> unit {
func private %table(v0.i8) {
block0:
br_table v0 block0 (1.i8 block1) (2.i8 block2);

Expand Down
2 changes: 1 addition & 1 deletion crates/verifier/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ trace_info:
1: 0.i8
2: v2.i32 = add 28.i32 0.i8;
3: block0
4: func public %test_func() -> unit",
4: func public %test_func()",
err_msg
);
}
Expand Down

0 comments on commit 5c79442

Please sign in to comment.