Skip to content

Commit

Permalink
verify attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
vaivaswatha committed Oct 21, 2023
1 parent 509d20d commit da26722
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
32 changes: 27 additions & 5 deletions src/dialects/builtin/attributes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use apint::ApInt;
use intertrait::cast_to;
use sorted_vector_map::SortedVectorMap;
use thiserror::Error;

use crate::{
attribute::{AttrObj, Attribute},
Expand All @@ -11,9 +12,10 @@ use crate::{
impl_attr, impl_attr_interface,
printable::{self, Printable},
r#type::TypeObj,
verify_err,
};

use super::attr_interfaces::TypedAttrInterface;
use super::{attr_interfaces::TypedAttrInterface, types::IntegerType};

/// An attribute containing a string.
/// Similar to MLIR's [StringAttr](https://mlir.llvm.org/docs/Dialects/Builtin/#stringattr).
Expand Down Expand Up @@ -47,7 +49,7 @@ impl Printable for StringAttr {

impl Verify for StringAttr {
fn verify(&self, _ctx: &Context) -> Result<()> {
todo!()
Ok(())
}
}

Expand All @@ -71,9 +73,16 @@ impl Printable for IntegerAttr {
}
}

#[derive(Error, Debug)]
#[error("value of IntegerAttr must be of IntegerType")]
struct IntegerAttrVerifyErr;

impl Verify for IntegerAttr {
fn verify(&self, _ctx: &Context) -> Result<()> {
todo!()
fn verify(&self, ctx: &Context) -> Result<()> {
if !self.ty.deref(ctx).is::<IntegerType>() {
return verify_err!(IntegerAttrVerifyErr);
}
Ok(())
}
}

Expand Down Expand Up @@ -163,9 +172,22 @@ impl Printable for SmallDictAttr {
}
}

#[derive(Error, Debug)]
#[error("SmallDictAttr keys are not sorted")]
struct SmallDictAttrVerifyErr;
impl Verify for SmallDictAttr {
fn verify(&self, _ctx: &Context) -> Result<()> {
todo!()
for (str1, str2) in self
.0
.iter()
.map(|(&key, _)| key)
.zip(self.0.iter().skip(1).map(|(&key, _)| key))
{
if str1 > str2 {
return verify_err!(SmallDictAttrVerifyErr);
}
}
Ok(())
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,10 +440,9 @@ impl<T: DefUseParticipant + DefTrait> Verify for Operand<T> {

impl Verify for Operation {
fn verify(&self, ctx: &Context) -> Result<()> {
for _attr in self.attributes.values() {
// TODO.
// attr.verify(ctx)?;
// attr.verify_interfaces(ctx)?;
for attr in self.attributes.values() {
attr.verify(ctx)?;
attr.verify_interfaces(ctx)?;
}
for opd in &self.operands {
opd.verify(ctx)?;
Expand Down

0 comments on commit da26722

Please sign in to comment.