Skip to content

Commit

Permalink
Merge pull request #96 from Y-Nak/ir-module-linking
Browse files Browse the repository at this point in the history
Ir module linking
  • Loading branch information
sbillig authored Dec 6, 2024
2 parents 6869ddc + 85607de commit 52551c8
Show file tree
Hide file tree
Showing 67 changed files with 1,608 additions and 589 deletions.
2 changes: 2 additions & 0 deletions crates/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ rustc-hash = "2.0.0"
sonatina-ir = { path = "../ir", version = "0.0.3-alpha" }
sonatina-triple = { path = "../triple", version = "0.0.3-alpha" }
sonatina-macros = { path = "../macros", version = "0.0.3-alpha" }
dashmap = { version = "6.1", features = ["rayon"] }
indexmap = { version = "2.0" }
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
4 changes: 0 additions & 4 deletions crates/codegen/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// We allow `needless_collect` due to some false positive.
// See <https://github.com/rust-lang/rust-clippy/issues/7512> and <https://github.com/rust-lang/rust-clippy/issues/7336>
#![allow(clippy::needless_collect)]

pub mod critical_edge;
pub mod domtree;
pub mod loop_analysis;
Expand Down
2 changes: 1 addition & 1 deletion crates/codegen/src/optim/adce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl AdceSolver {

fn mark_by_inst(&mut self, func: &Function, inst_id: InstId, pdf_set: &PDFSet) {
let inst = func.dfg.inst(inst_id);
inst.visit_values(&mut |value| {
inst.for_each_value(&mut |value| {
if let Some(value_inst) = func.dfg.value_inst(value) {
self.mark_inst(func, value_inst);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/codegen/src/optim/licm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl LicmSolver {

let mut invariant = true;
let inst = func.dfg.inst(inst_id);
inst.visit_values(&mut |value| invariant &= !loop_var.contains(&value));
inst.for_each_value(&mut |value| invariant &= !loop_var.contains(&value));
invariant
}

Expand Down
4 changes: 2 additions & 2 deletions crates/codegen/src/optim/sccp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl SccpSolver {
.reachable_blocks
.contains(&func.layout.inst_block(inst)));

func.dfg.inst(inst).visit_values(&mut |value| {
func.dfg.inst(inst).for_each_value(&mut |value| {
if let Some(imm) = func.dfg.value_imm(value) {
self.set_lattice_cell(value, LatticeCell::Const(imm));
}
Expand Down Expand Up @@ -164,7 +164,7 @@ impl SccpSolver {

fn eval_inst(&mut self, func: &Function, inst_id: InstId) {
debug_assert!(!func.dfg.is_phi(inst_id));
func.dfg.inst(inst_id).visit_values(&mut |value| {
func.dfg.inst(inst_id).for_each_value(&mut |value| {
if let Some(imm) = func.dfg.value_imm(value) {
self.set_lattice_cell(value, LatticeCell::Const(imm));
}
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
5 changes: 4 additions & 1 deletion crates/filecheck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ impl<'a> FileChecker<'a> {
self.transformer.transform(func);
(
FuncWriter::with_debug_provider(func, func_ref, &parsed_module.debug).dump_string(),
func.sig.name().to_string(),
parsed_module
.module
.ctx
.func_sig(func_ref, |sig| sig.name().to_string()),
)
});
let checker = self.build_checker(comments);
Expand Down
18 changes: 9 additions & 9 deletions crates/interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use sonatina_ir::{
isa::Endian,
module::{FuncRef, ModuleCtx, RoFuncStore},
prelude::*,
types::CompoundTypeData,
types::CompoundType,
BlockId, DataFlowGraph, Function, Immediate, InstId, Module, Type, Value, ValueId, I256,
};

Expand Down Expand Up @@ -179,7 +179,7 @@ impl State for Machine {
let mut fields = Vec::new();

match ty.resolve_compound(&self.module_ctx).unwrap() {
CompoundTypeData::Array { elem: elem_ty, len } => {
CompoundType::Array { elem: elem_ty, len } => {
let mut addr = addr;
let elem_size = self.module_ctx.size_of_unchecked(elem_ty);
for _ in 0..len {
Expand All @@ -190,7 +190,7 @@ impl State for Machine {
}
}

CompoundTypeData::Struct(s) => {
CompoundType::Struct(s) => {
let mut addr = addr;
for field_ty in s.fields.into_iter() {
let elem_addr = EvalValue::Imm(Immediate::I256(I256::from(addr)));
Expand All @@ -200,11 +200,11 @@ impl State for Machine {
}
}

CompoundTypeData::Ptr(_) => {
CompoundType::Ptr(_) => {
unreachable!()
}

CompoundTypeData::Func { .. } => {
CompoundType::Func { .. } => {
panic!("function type can't be placed in memory");
}
}
Expand Down Expand Up @@ -243,7 +243,7 @@ impl State for Machine {
unreachable!();
};
match ty.resolve_compound(&self.module_ctx).unwrap() {
CompoundTypeData::Array { elem: elem_ty, .. } => {
CompoundType::Array { elem: elem_ty, .. } => {
let mut addr = addr;
let elem_size = self.module_ctx.size_of_unchecked(elem_ty);
for field in &fields {
Expand All @@ -253,7 +253,7 @@ impl State for Machine {
}
}

CompoundTypeData::Struct(s) => {
CompoundType::Struct(s) => {
let mut addr = addr;
for (i, field_ty) in s.fields.into_iter().enumerate() {
let elem_addr = EvalValue::Imm(Immediate::I256(I256::from(addr)));
Expand All @@ -262,11 +262,11 @@ impl State for Machine {
}
}

CompoundTypeData::Ptr(_) => {
CompoundType::Ptr(_) => {
unreachable!()
}

CompoundTypeData::Func { .. } => {
CompoundType::Func { .. } => {
panic!("Function can't be stored in memory");
}
}
Expand Down
16 changes: 9 additions & 7 deletions crates/interpreter/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,20 @@ impl TestCase {
if let Some((expected, evaluated)) = err_pair {
let text = &self.text;
let func = machine.funcs.get(&self.func).unwrap();
let func_name = func.sig.name();
let msg = format!("{text}\nexpected: {expected}\nevaluated: {evaluated}\n");
Err(format_error(func_name, &msg))

func.ctx().func_sig(self.func, |sig| {
let msg = format!("{text}\nexpected: {expected}\nevaluated: {evaluated}\n");
Err(format_error(sig.name(), &msg))
})
} else {
Ok(())
}
}

fn parse(module: &ParsedModule, func_ref: FuncRef, comment: &str) -> Result<Self, String> {
let Some(caps) = PATTERN.captures(comment) else {
return module.module.func_store.view(func_ref, |func| {
let func_name = func.sig.name();
return module.module.ctx.func_sig(func_ref, |sig| {
let func_name = sig.name();
Err(format_error(
func_name,
&format!("invalid `{comment}`, `#[(args_list) -> ret]` is expected"),
Expand Down Expand Up @@ -142,8 +144,8 @@ fn parse_value(module: &ParsedModule, func_ref: FuncRef, input: &str) -> Result<

Err(err) => {
let err = err.to_string();
return module.module.func_store.view(func_ref, |func| {
let func_name = func.sig.name();
return module.module.ctx.func_sig(func_ref, |sig| {
let func_name = sig.name();
Err(format_error(func_name, &err.to_string()))
});
}
Expand Down
4 changes: 4 additions & 0 deletions crates/ir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ dot2 = { git = "https://github.com/sanpii/dot2.rs.git" }
dashmap = { version = "6.1", features = ["rayon"] }
rayon = { version = "1" }
dyn-clone = "1.0"

[dev-dependencies]
sonatina-parser = { path = "../parser", version = "0.0.3-alpha" }
insta = { version = "1.41" }
23 changes: 10 additions & 13 deletions crates/ir/src/builder/func_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use super::{
use crate::{
func_cursor::{CursorLocation, FuncCursor},
module::{FuncRef, ModuleCtx},
BlockId, Function, GlobalVariable, Immediate, Inst, InstId, InstSetBase, Signature, Type,
Value, ValueId,
BlockId, Function, GlobalVariableRef, Immediate, Inst, InstId, InstSetBase, Type, Value,
ValueId,
};

pub struct FunctionBuilder<C> {
Expand All @@ -22,8 +22,9 @@ where
C: FuncCursor,
{
pub fn new(module_builder: ModuleBuilder, func_ref: FuncRef, cursor: C) -> Self {
let sig = module_builder.funcs.view(func_ref, |func| func.sig.clone());
let func = Function::new(&module_builder.ctx, sig);
let func = module_builder
.ctx
.func_sig(func_ref, |sig| Function::new(&module_builder.ctx, sig));

Self {
module_builder,
Expand All @@ -34,10 +35,6 @@ where
}
}

pub fn func_sig(&self) -> &Signature {
&self.func.sig
}

pub fn finish(self) {
if cfg!(debug_assertions) {
for block in self.func.layout.iter_block() {
Expand All @@ -55,7 +52,7 @@ where
..
} = self;

module_builder.funcs.update(func_ref, func);
module_builder.func_store.update(func_ref, func);
}

pub fn append_parameter(&mut self, ty: Type) -> ValueId {
Expand Down Expand Up @@ -103,7 +100,7 @@ where
}

/// Return pointer value to the global variable.
pub fn make_global_value(&mut self, gv: GlobalVariable) -> ValueId {
pub fn make_global_value(&mut self, gv: GlobalVariableRef) -> ValueId {
self.func.dfg.make_global_value(gv)
}

Expand Down Expand Up @@ -311,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 @@ -346,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 @@ -425,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
Loading

0 comments on commit 52551c8

Please sign in to comment.