Skip to content

Commit

Permalink
IR format printers (#22)
Browse files Browse the repository at this point in the history
Backport from #20 

This PR introduces the `irfmt/printers` module with common printer functions.

In #20 we implement `asm_format` directives as macros. These macros will  use the printer functions defined here (and more to come). As secondary goal we want to make it easier for downstream developers to implement
their own printers, while trying to keep some kind of standardized format. For this reason we also implement Printable for some more IR objects in pliron.

The change also removes the `PrintableIter` trait in favor of the `list_with_sep` and `iter_with_sep` printer functions in the irfmt package. This change ensures that we have a single trait for printing in the library.

As discussed in #21 we do not require developers to add the type_id or
attribute_id manually to the printer anymore. `pliron` now implements a
printer for TypeObj/AttribObj that will always print the IDs.

---------

Co-authored-by: urso <steffen.siering at gmail.com>
Co-authored-by: Vaivaswatha N <[email protected]>
  • Loading branch information
urso and vaivaswatha authored Jan 28, 2024
1 parent 20223bb commit 28164a0
Show file tree
Hide file tree
Showing 17 changed files with 308 additions and 175 deletions.
1 change: 1 addition & 0 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl Printable for AttrObj {
state: &printable::State,
f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
write!(f, "{} ", self.get_attr_id())?;
Printable::fmt(self.as_ref(), ctx, state, f)
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/basic_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ use crate::{
error::Result,
identifier::Identifier,
indented_block,
irfmt::printers::{iter_with_sep, list_with_sep},
linked_list::{private, ContainsLinkedList, LinkedList},
location::{Located, Location},
operation::Operation,
parsable::{self, spaced, IntoParseResult, Parsable, ParseResult},
printable::{self, indented_nl, ListSeparator, Printable, PrintableIter},
printable::{self, indented_nl, ListSeparator, Printable},
r#type::{type_parser, TypeObj, Typed},
region::Region,
use_def_lists::{DefNode, Value},
Expand Down Expand Up @@ -306,18 +307,15 @@ impl Printable for BasicBlock {
f,
"^{}({}):",
self.unique_name(ctx),
self.args
.iter()
.iprint(ctx, state, ListSeparator::Char(','))
list_with_sep(&self.args, ListSeparator::Char(',')).print(ctx, state),
)?;

indented_block!(state, {
write!(
f,
"{}{}",
indented_nl(state),
self.iter(ctx)
.iprint(ctx, state, ListSeparator::CharNewline(';'))
iter_with_sep(self.iter(ctx), ListSeparator::CharNewline(';')).print(ctx, state),
)?;
});

Expand Down
4 changes: 2 additions & 2 deletions src/debug_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ mod tests {
let i64_ty = IntegerType::get(&mut ctx, 64, Signedness::Signed);
let cop = ConstantOp::new_unlinked(&mut ctx, IntegerAttr::create(i64_ty, ApInt::from(0)));
let op = cop.get_operation();
set_operation_result_name(&mut ctx, op, 0, "foo".to_string());
set_operation_result_name(&ctx, op, 0, "foo".to_string());
assert!(get_operation_result_name(&ctx, op, 0).unwrap() == "foo");
op.deref(&ctx).verify(&ctx)?;
Ok(())
Expand All @@ -159,7 +159,7 @@ mod tests {

let i64_ty = IntegerType::get(&mut ctx, 64, Signedness::Signed);
let block = BasicBlock::new(&mut ctx, Some("entry".into()), vec![i64_ty]);
set_block_arg_name(&mut ctx, block, 0, "foo".to_string());
set_block_arg_name(&ctx, block, 0, "foo".to_string());
assert!(get_block_arg_name(&ctx, block, 0).unwrap() == "foo");
block.deref(&ctx).verify(&ctx)?;
Ok(())
Expand Down
45 changes: 29 additions & 16 deletions src/dialects/builtin/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ use crate::{
dialect::Dialect,
error::Result,
impl_attr, impl_attr_interface, input_err,
irfmt::printers::quoted,
location::Located,
parsable::{spaced, IntoParseResult, Parsable, ParseResult, StateStream},
printable::{self, Printable},
r#type::{type_parser, TypeObj},
r#type::{type_parser, TypeObj, Typed},
verify_err_noloc,
};

Expand Down Expand Up @@ -45,11 +46,11 @@ impl From<StringAttr> for String {
impl Printable for StringAttr {
fn fmt(
&self,
_ctx: &Context,
_state: &printable::State,
ctx: &Context,
state: &printable::State,
f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
write!(f, "{} {:?}", Self::get_attr_id_static(), self.0)
quoted(&self.0).fmt(ctx, state, f)
}
}

Expand Down Expand Up @@ -118,13 +119,7 @@ impl Printable for IntegerAttr {
_state: &printable::State,
f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
write!(
f,
"{} <0x{:x}: {}>",
Self::get_attr_id_static(),
self.val,
self.ty.disp(ctx)
)
write!(f, "<0x{:x}: {}>", self.val, self.ty.disp(ctx))
}
}

Expand Down Expand Up @@ -176,6 +171,12 @@ impl Parsable for IntegerAttr {
}
}

impl Typed for IntegerAttr {
fn get_type(&self, _ctx: &Context) -> Ptr<TypeObj> {
self.ty
}
}

impl_attr_interface!(TypedAttrInterface for IntegerAttr {
fn get_type(&self) -> Ptr<TypeObj> {
self.ty
Expand Down Expand Up @@ -223,6 +224,12 @@ impl From<FloatAttr> for APFloat {
}
}

impl Typed for FloatAttr {
fn get_type(&self, _cfg: &Context) -> Ptr<TypeObj> {
TypedAttrInterface::get_type(self)
}
}

impl_attr_interface!(
TypedAttrInterface for FloatAttr {
fn get_type(&self) -> Ptr<TypeObj> {
Expand Down Expand Up @@ -378,11 +385,11 @@ impl UnitAttr {
impl Printable for UnitAttr {
fn fmt(
&self,
ctx: &Context,
_ctx: &Context,
_state: &printable::State,
f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
write!(f, "{}", self.get_attr_id().disp(ctx))
write!(f, "()")
}
}

Expand Down Expand Up @@ -423,7 +430,7 @@ impl Printable for TypeAttr {
_state: &printable::State,
f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
write!(f, "{} <{}>", self.get_attr_id().disp(ctx), self.0.disp(ctx))
write!(f, "<{}>", self.0.disp(ctx))
}
}

Expand All @@ -448,6 +455,12 @@ impl Verify for TypeAttr {
}
}

impl Typed for TypeAttr {
fn get_type(&self, _ctx: &Context) -> Ptr<TypeObj> {
self.0
}
}

impl_attr_interface!(
TypedAttrInterface for TypeAttr {
fn get_type(&self) -> Ptr<TypeObj> {
Expand Down Expand Up @@ -499,11 +512,11 @@ mod tests {
assert!(int64_0_ptr == int64_0_ptr2);
assert_eq!(
int64_0_ptr.disp(&ctx).to_string(),
"builtin.integer <0x0: builtin.int <si64>>"
"builtin.integer <0x0: builtin.int<si64>>"
);
assert_eq!(
int64_1_ptr.disp(&ctx).to_string(),
"builtin.integer <0xf: builtin.int <si64>>"
"builtin.integer <0xf: builtin.int<si64>>"
);
assert!(
ApInt::from(int64_0_ptr.downcast_ref::<IntegerAttr>().unwrap().clone()).is_zero()
Expand Down
32 changes: 15 additions & 17 deletions src/dialects/builtin/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ use crate::{
error::Result,
identifier::Identifier,
impl_op_interface, input_err,
irfmt::printers::op::{region, symb_op_header, typed_symb_op_header},
linked_list::ContainsLinkedList,
location::{Located, Location},
op::{Op, OpObj},
operation::{process_parsed_ssa_defs, Operation},
parsable::{spaced, IntoParseResult, Parsable, ParseResult, StateStream},
printable::{self, Printable},
r#type::{type_parser, TypeObj},
r#type::{type_parser, TypeObj, Typed},
region::Region,
verify_err,
};
Expand Down Expand Up @@ -58,13 +59,9 @@ impl Printable for ModuleOp {
state: &printable::State,
f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
write!(
f,
"{} @{} ",
self.get_opid().disp(ctx),
self.get_symbol_name(ctx),
)?;
self.get_region(ctx).fmt(ctx, state, f)?;
symb_op_header(self).fmt(ctx, state, f)?;
write!(f, " ")?;
region(self).fmt(ctx, state, f)?;
Ok(())
}
}
Expand Down Expand Up @@ -202,6 +199,12 @@ impl FuncOp {
}
}

impl Typed for FuncOp {
fn get_type(&self, ctx: &Context) -> Ptr<TypeObj> {
self.get_type(ctx)
}
}

impl_op_interface!(OneRegionInterface for FuncOp {});
impl_op_interface!(SymbolOpInterface for FuncOp {});
impl_op_interface!(IsolatedFromAboveInterface for FuncOp {});
Expand All @@ -213,14 +216,9 @@ impl Printable for FuncOp {
state: &printable::State,
f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
write!(
f,
"{} @{}: {} ",
self.get_opid().disp(ctx),
self.get_symbol_name(ctx),
self.get_type(ctx).disp(ctx),
)?;
self.get_region(ctx).fmt(ctx, state, f)?;
typed_symb_op_header(self).fmt(ctx, state, f)?;
write!(f, " ")?;
region(self).fmt(ctx, state, f)?;
Ok(())
}
}
Expand Down Expand Up @@ -347,7 +345,7 @@ impl Printable for ConstantOp {
write!(
f,
"{} = {} {}",
self.get_result(ctx).unique_name(ctx),
self.get_result(ctx).disp(ctx),
self.get_opid().disp(ctx),
self.get_value(ctx).disp(ctx)
)
Expand Down
23 changes: 11 additions & 12 deletions src/dialects/builtin/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ use crate::{
dialect::Dialect,
error::Result,
impl_type,
irfmt::printers::{functional_type, list_with_sep},
parsable::{spaced, IntoParseResult, Parsable, ParseResult, StateStream},
printable::{self, ListSeparator, Printable, PrintableIter},
printable::{self, ListSeparator, Printable},
r#type::{type_parser, Type, TypeObj},
};

Expand Down Expand Up @@ -85,11 +86,11 @@ impl Parsable for IntegerType {
impl Printable for IntegerType {
fn fmt(
&self,
ctx: &Context,
_ctx: &Context,
_state: &printable::State,
f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
write!(f, "{} <", Self::get_type_id_static().disp(ctx))?;
write!(f, "<",)?;
match &self.signedness {
Signedness::Signed => write!(f, "si{}", self.width)?,
Signedness::Unsigned => write!(f, "ui{}", self.width)?,
Expand Down Expand Up @@ -155,13 +156,11 @@ impl Printable for FunctionType {
f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
let sep = ListSeparator::Char(',');
write!(
f,
"{} <({}) -> ({})>",
Self::get_type_id_static().disp(ctx),
self.inputs.iter().iprint(ctx, state, sep),
self.results.iter().iprint(ctx, state, sep)
functional_type(
list_with_sep(&self.inputs, sep),
list_with_sep(&self.results, sep),
)
.fmt(ctx, state, f)
}
}

Expand Down Expand Up @@ -220,11 +219,11 @@ impl UnitType {
impl Printable for UnitType {
fn fmt(
&self,
ctx: &Context,
_ctx: &Context,
_state: &printable::State,
f: &mut core::fmt::Formatter<'_>,
_f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
write!(f, "{}", Self::get_type_id_static().disp(ctx),)
Ok(())
}
}

Expand Down
Loading

0 comments on commit 28164a0

Please sign in to comment.