Skip to content

Commit

Permalink
Merge pull request #94 from Y-Nak/refactor-ir-writer
Browse files Browse the repository at this point in the history
Refactor ir writer
  • Loading branch information
sbillig authored Dec 1, 2024
2 parents 4510679 + b737cb6 commit 25e23a6
Show file tree
Hide file tree
Showing 29 changed files with 467 additions and 518 deletions.
2 changes: 1 addition & 1 deletion crates/codegen/src/loop_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl<'a, 'b> BlocksInLoopPostOrder<'a, 'b> {
}
}

impl<'a, 'b> Iterator for BlocksInLoopPostOrder<'a, 'b> {
impl Iterator for BlocksInLoopPostOrder<'_, '_> {
type Item = BlockId;

fn next(&mut self) -> Option<Self::Item> {
Expand Down
2 changes: 1 addition & 1 deletion crates/codegen/src/optim/sccp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ impl<'a, 'i> CellState<'a, 'i> {
}
}

impl<'a, 'i> State for CellState<'a, 'i> {
impl State for CellState<'_, '_> {
fn lookup_val(&mut self, value: ValueId) -> EvalValue {
self.used_val.insert(value);

Expand Down
2 changes: 1 addition & 1 deletion crates/ir/src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl<'a> CfgPostOrder<'a> {
}
}

impl<'a> Iterator for CfgPostOrder<'a> {
impl Iterator for CfgPostOrder<'_> {
type Item = BlockId;

fn next(&mut self) -> Option<BlockId> {
Expand Down
9 changes: 6 additions & 3 deletions crates/ir/src/dfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
control_flow::{self, Branch, Jump, Phi},
InstId, SideEffect,
},
ir_writer::{FuncWriteCtx, WriteWithFunc},
ir_writer::{FuncWriteCtx, IrWrite},
module::ModuleCtx,
GlobalVariable, Inst, InstDowncast, InstDowncastMut, InstSetBase,
};
Expand Down Expand Up @@ -301,8 +301,11 @@ impl DataFlowGraph {
pub struct BlockId(pub u32);
entity_impl!(BlockId, "block");

impl WriteWithFunc for BlockId {
fn write(&self, _ctx: &FuncWriteCtx, w: &mut impl io::Write) -> io::Result<()> {
impl<'a> IrWrite<FuncWriteCtx<'a>> for BlockId {
fn write<W>(&self, w: &mut W, _ctx: &FuncWriteCtx<'a>) -> io::Result<()>
where
W: io::Write + ?Sized,
{
write!(w, "block{}", self.0)
}
}
Expand Down
40 changes: 14 additions & 26 deletions crates/ir/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ use std::io;
use smallvec::SmallVec;

use super::{DataFlowGraph, Layout, Type, ValueId};
use crate::{
ir_writer::{FuncWriteCtx, WriteWithFunc, WriteWithModule},
module::ModuleCtx,
InstSetBase, Linkage,
};
use crate::{ir_writer::IrWrite, module::ModuleCtx, InstSetBase, Linkage};

pub struct Function {
/// Signature of the function.
Expand Down Expand Up @@ -97,27 +93,19 @@ impl Signature {
}
}

impl WriteWithFunc for Signature {
fn write(&self, ctx: &FuncWriteCtx, w: &mut impl io::Write) -> io::Result<()> {
let Signature {
name,
linkage,
args,
ret_ty,
} = self;

write!(w, "func {linkage} %{name}(")?;
let mut args = args.iter();
if let Some(arg) = args.next() {
arg.write(ctx.module_ctx(), &mut *w)?;
};

for arg in args {
write!(w, " ")?;
arg.write(ctx.module_ctx(), &mut *w)?;
}
impl<Ctx> IrWrite<Ctx> for Signature
where
Ctx: AsRef<ModuleCtx>,
{
fn write<W>(&self, w: &mut W, ctx: &Ctx) -> io::Result<()>
where
W: io::Write,
{
write!(w, "func ")?;
self.linkage.write(w, ctx)?;
write!(w, " %{}(", self.name)?;
self.args.write_with_delim(w, " ", ctx)?;
write!(w, ") -> ")?;

ret_ty.write(ctx.module_ctx(), &mut *w)
self.ret_ty.write(w, ctx)
}
}
77 changes: 49 additions & 28 deletions crates/ir/src/global_variable.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::{fmt, io};
use std::io;

use cranelift_entity::PrimaryMap;
use rustc_hash::FxHashMap;

use crate::{ir_writer::WriteWithModule, module::ModuleCtx, Immediate, Linkage, Type};
use crate::{ir_writer::IrWrite, module::ModuleCtx, Immediate, Linkage, Type};

#[derive(Debug, Default)]
pub struct GlobalVariableStore {
Expand Down Expand Up @@ -34,7 +34,7 @@ impl GlobalVariableStore {
}

pub fn init_data(&self, gv: GlobalVariable) -> Option<&GvInitializer> {
self.gv_data[gv].data.as_ref()
self.gv_data[gv].initializer.as_ref()
}

pub fn is_const(&self, gv: GlobalVariable) -> bool {
Expand Down Expand Up @@ -68,10 +68,16 @@ impl GlobalVariable {
module.with_gv_store(|s| s.ty(self))
}
}

impl WriteWithModule for GlobalVariable {
fn write(&self, module: &ModuleCtx, w: &mut impl io::Write) -> io::Result<()> {
module.with_gv_store(|s| s.gv_data(*self).write(module, w))
impl<Ctx> IrWrite<Ctx> for GlobalVariable
where
Ctx: AsRef<ModuleCtx>,
{
fn write<W>(&self, w: &mut W, ctx: &Ctx) -> io::Result<()>
where
W: io::Write,
{
ctx.as_ref()
.with_gv_store(|s| s.gv_data(*self).write(w, ctx))
}
}

Expand All @@ -81,7 +87,7 @@ pub struct GlobalVariableData {
pub ty: Type,
pub linkage: Linkage,
pub is_const: bool,
pub data: Option<GvInitializer>,
pub initializer: Option<GvInitializer>,
}

impl GlobalVariableData {
Expand All @@ -97,7 +103,7 @@ impl GlobalVariableData {
ty,
linkage,
is_const,
data,
initializer: data,
}
}

Expand All @@ -107,22 +113,31 @@ impl GlobalVariableData {
ty,
linkage,
is_const: true,
data: Some(data),
initializer: Some(data),
}
}
}

impl WriteWithModule for GlobalVariableData {
fn write(&self, module: &ModuleCtx, w: &mut impl io::Write) -> io::Result<()> {
write!(w, "global {} ", self.linkage)?;
impl<Ctx> IrWrite<Ctx> for GlobalVariableData
where
Ctx: AsRef<ModuleCtx>,
{
fn write<W>(&self, w: &mut W, ctx: &Ctx) -> io::Result<()>
where
W: io::Write,
{
write!(w, "global ")?;
self.linkage.write(w, ctx)?;
if self.is_const {
write!(w, "const ")?;
write!(w, " const")?;
}
self.ty.write(module, &mut *w)?;
write!(w, " ")?;
self.ty.write(w, ctx)?;

write!(w, " ${}", self.symbol)?;
if let Some(data) = &self.data {
write!(w, " = {data}")?;
if let Some(initializer) = &self.initializer {
write!(w, " = ")?;
initializer.write(w, ctx)?;
}
Ok(())
}
Expand All @@ -149,29 +164,35 @@ impl GvInitializer {
}
}

impl fmt::Display for GvInitializer {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
impl<Ctx> IrWrite<Ctx> for GvInitializer
where
Ctx: AsRef<ModuleCtx>,
{
fn write<W>(&self, w: &mut W, ctx: &Ctx) -> io::Result<()>
where
W: io::Write,
{
match self {
Self::Immediate(data) => write!(f, "{}", data),
Self::Immediate(data) => write!(w, "{}", data),
Self::Array(data) => {
write!(f, "[")?;
write!(w, "[")?;
for (i, v) in data.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
write!(w, ", ")?;
}
write!(f, "{}", v)?;
v.write(w, ctx)?;
}
write!(f, "]")
write!(w, "]")
}
Self::Struct(data) => {
write!(f, "{{")?;
write!(w, "{{")?;
for (i, v) in data.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
write!(w, ", ")?;
}
write!(f, "{}", v)?;
v.write(w, ctx)?;
}
write!(f, "}}")
write!(w, "}}")
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/ir/src/graphviz/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use dot2::label;

use super::function::DUMMY_BLOCK;
use crate::{
ir_writer::{FuncWriteCtx, ValueWithTy, WriteWithFunc},
ir_writer::{FuncWriteCtx, IrWrite, ValueWithTy},
BlockId, ControlFlowGraph, Function,
};

Expand All @@ -28,7 +28,7 @@ impl<'a> BlockNode<'a> {
}
}

impl<'a> BlockNode<'a> {
impl BlockNode<'_> {
pub(super) fn label(self) -> label::Text<'static> {
let Self { block, ctx, .. } = self;
let Function { sig, layout, .. } = &ctx.func;
Expand Down
4 changes: 2 additions & 2 deletions crates/ir/src/graphviz/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use dot2::{label::Text, GraphWalk, Id, Labeller, Style};
use super::block::BlockNode;
use crate::{
inst::control_flow::Phi,
ir_writer::{FuncWriteCtx, WriteWithFunc},
ir_writer::{FuncWriteCtx, IrWrite},
prelude::*,
BlockId, ControlFlowGraph,
};
Expand Down Expand Up @@ -124,7 +124,7 @@ pub(super) struct BlockEdge<'a> {
ctx: &'a FuncWriteCtx<'a>,
}

impl<'a> BlockEdge<'a> {
impl BlockEdge<'_> {
fn label(self) -> Text<'static> {
let Self { from, to, ctx } = self;
let to = to.block;
Expand Down
13 changes: 1 addition & 12 deletions crates/ir/src/inst/arith.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use macros::Inst;

use crate::{inst::impl_inst_write, ValueId};
use crate::ValueId;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)]
pub struct Neg {
#[inst(value)]
arg: ValueId,
}
impl_inst_write!(Neg);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)]
pub struct Add {
Expand All @@ -16,7 +15,6 @@ pub struct Add {
#[inst(value)]
rhs: ValueId,
}
impl_inst_write!(Add);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)]
pub struct Mul {
Expand All @@ -25,7 +23,6 @@ pub struct Mul {
#[inst(value)]
rhs: ValueId,
}
impl_inst_write!(Mul);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)]
pub struct Sub {
Expand All @@ -34,7 +31,6 @@ pub struct Sub {
#[inst(value)]
rhs: ValueId,
}
impl_inst_write!(Sub);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)]
pub struct Sdiv {
Expand All @@ -43,7 +39,6 @@ pub struct Sdiv {
#[inst(value)]
rhs: ValueId,
}
impl_inst_write!(Sdiv);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)]
pub struct Udiv {
Expand All @@ -52,7 +47,6 @@ pub struct Udiv {
#[inst(value)]
rhs: ValueId,
}
impl_inst_write!(Udiv);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)]
pub struct Umod {
Expand All @@ -61,7 +55,6 @@ pub struct Umod {
#[inst(value)]
rhs: ValueId,
}
impl_inst_write!(Umod);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)]
pub struct Smod {
Expand All @@ -70,7 +63,6 @@ pub struct Smod {
#[inst(value)]
rhs: ValueId,
}
impl_inst_write!(Smod);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)]
pub struct Shl {
Expand All @@ -79,7 +71,6 @@ pub struct Shl {
#[inst(value)]
value: ValueId,
}
impl_inst_write!(Shl);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)]
pub struct Shr {
Expand All @@ -88,7 +79,6 @@ pub struct Shr {
#[inst(value)]
value: ValueId,
}
impl_inst_write!(Shr);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)]
pub struct Sar {
Expand All @@ -97,4 +87,3 @@ pub struct Sar {
#[inst(value)]
value: ValueId,
}
impl_inst_write!(Sar);
Loading

0 comments on commit 25e23a6

Please sign in to comment.