Skip to content

Commit

Permalink
Merge pull request #386 from 0xPolygonMiden/greenhat/i378-restore-op-…
Browse files Browse the repository at this point in the history
…verif

[1/x] fix: restore op type constraint verification, fix `zext` vs `sext` usage in the frontend
  • Loading branch information
bitwalker authored Feb 5, 2025
2 parents a8a1d5e + bc7a8b3 commit b139cff
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 261 deletions.
23 changes: 15 additions & 8 deletions dialects/hir/src/ops/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ pub enum CastKind {
*/

#[operation(
dialect = HirDialect,
traits(UnaryOp)
dialect = HirDialect,
traits(UnaryOp),
implements(InferTypeOpInterface)
)]
pub struct PtrToInt {
#[operand]
Expand All @@ -54,7 +55,8 @@ impl InferTypeOpInterface for PtrToInt {

#[operation(
dialect = HirDialect,
traits(UnaryOp)
traits(UnaryOp),
implements(InferTypeOpInterface)
)]
pub struct IntToPtr {
#[operand]
Expand All @@ -75,7 +77,8 @@ impl InferTypeOpInterface for IntToPtr {

#[operation(
dialect = HirDialect,
traits(UnaryOp)
traits(UnaryOp),
implements(InferTypeOpInterface)
)]
pub struct Cast {
#[operand]
Expand All @@ -96,7 +99,8 @@ impl InferTypeOpInterface for Cast {

#[operation(
dialect = HirDialect,
traits(UnaryOp)
traits(UnaryOp),
implements(InferTypeOpInterface)
)]
pub struct Bitcast {
#[operand]
Expand All @@ -117,7 +121,8 @@ impl InferTypeOpInterface for Bitcast {

#[operation(
dialect = HirDialect,
traits(UnaryOp)
traits(UnaryOp),
implements(InferTypeOpInterface)
)]
pub struct Trunc {
#[operand]
Expand All @@ -138,7 +143,8 @@ impl InferTypeOpInterface for Trunc {

#[operation(
dialect = HirDialect,
traits(UnaryOp)
traits(UnaryOp),
implements(InferTypeOpInterface)
)]
pub struct Zext {
#[operand]
Expand All @@ -159,7 +165,8 @@ impl InferTypeOpInterface for Zext {

#[operation(
dialect = HirDialect,
traits(UnaryOp)
traits(UnaryOp),
implements(InferTypeOpInterface)
)]
pub struct Sext {
#[operand]
Expand Down
10 changes: 6 additions & 4 deletions dialects/hir/src/ops/primop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use crate::HirDialect;

#[operation(
dialect = HirDialect,
traits(HasSideEffects, MemoryRead, MemoryWrite, SameOperandsAndResultType)
traits(HasSideEffects, MemoryRead, MemoryWrite, SameOperandsAndResultType),
implements(InferTypeOpInterface)
)]
pub struct MemGrow {
#[operand]
Expand All @@ -15,14 +16,15 @@ pub struct MemGrow {

impl InferTypeOpInterface for MemGrow {
fn infer_return_types(&mut self, _context: &Context) -> Result<(), Report> {
self.result_mut().set_type(Type::I32);
self.result_mut().set_type(Type::U32);
Ok(())
}
}

#[operation(
dialect = HirDialect,
traits(HasSideEffects, MemoryRead)
traits(HasSideEffects, MemoryRead),
implements(InferTypeOpInterface)
)]
pub struct MemSize {
#[result]
Expand All @@ -31,7 +33,7 @@ pub struct MemSize {

impl InferTypeOpInterface for MemSize {
fn infer_return_types(&mut self, _context: &Context) -> Result<(), Report> {
self.result_mut().set_type(Type::I32);
self.result_mut().set_type(Type::U32);
Ok(())
}
}
Expand Down
40 changes: 27 additions & 13 deletions dialects/hir/src/ops/unary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ macro_rules! infer_return_ty_for_unary_op {
/// Increment
#[operation (
dialect = HirDialect,
traits(UnaryOp, SameOperandsAndResultType)
traits(UnaryOp, SameOperandsAndResultType),
implements(InferTypeOpInterface)
)]
pub struct Incr {
#[operand]
Expand All @@ -42,7 +43,8 @@ infer_return_ty_for_unary_op!(Incr);
/// Negation
#[operation (
dialect = HirDialect,
traits(UnaryOp, SameOperandsAndResultType)
traits(UnaryOp, SameOperandsAndResultType),
implements(InferTypeOpInterface)
)]
pub struct Neg {
#[operand]
Expand All @@ -56,7 +58,8 @@ infer_return_ty_for_unary_op!(Neg);
/// Modular inverse
#[operation (
dialect = HirDialect,
traits(UnaryOp, SameOperandsAndResultType)
traits(UnaryOp, SameOperandsAndResultType),
implements(InferTypeOpInterface)
)]
pub struct Inv {
#[operand]
Expand All @@ -70,7 +73,8 @@ infer_return_ty_for_unary_op!(Inv);
/// log2(operand)
#[operation (
dialect = HirDialect,
traits(UnaryOp, SameOperandsAndResultType)
traits(UnaryOp, SameOperandsAndResultType),
implements(InferTypeOpInterface)
)]
pub struct Ilog2 {
#[operand]
Expand All @@ -84,7 +88,8 @@ infer_return_ty_for_unary_op!(Ilog2);
/// pow2(operand)
#[operation (
dialect = HirDialect,
traits(UnaryOp, SameOperandsAndResultType)
traits(UnaryOp, SameOperandsAndResultType),
implements(InferTypeOpInterface)
)]
pub struct Pow2 {
#[operand]
Expand All @@ -98,7 +103,9 @@ infer_return_ty_for_unary_op!(Pow2);
/// Logical NOT
#[operation (
dialect = HirDialect,
traits(UnaryOp, SameOperandsAndResultType)
traits(UnaryOp, SameOperandsAndResultType),
implements(InferTypeOpInterface)
)]
pub struct Not {
#[operand]
Expand All @@ -112,7 +119,8 @@ infer_return_ty_for_unary_op!(Not);
/// Bitwise NOT
#[operation (
dialect = HirDialect,
traits(UnaryOp, SameOperandsAndResultType)
traits(UnaryOp, SameOperandsAndResultType),
implements(InferTypeOpInterface)
)]
pub struct Bnot {
#[operand]
Expand All @@ -126,7 +134,8 @@ infer_return_ty_for_unary_op!(Bnot);
/// is_odd(operand)
#[operation (
dialect = HirDialect,
traits(UnaryOp)
traits(UnaryOp),
implements(InferTypeOpInterface)
)]
pub struct IsOdd {
#[operand]
Expand All @@ -140,7 +149,8 @@ infer_return_ty_for_unary_op!(IsOdd as Type::I1);
/// Count of non-zero bits (population count)
#[operation (
dialect = HirDialect,
traits(UnaryOp)
traits(UnaryOp),
implements(InferTypeOpInterface)
)]
pub struct Popcnt {
#[operand]
Expand All @@ -154,7 +164,8 @@ infer_return_ty_for_unary_op!(Popcnt as Type::U32);
/// Count Leading Zeros
#[operation (
dialect = HirDialect,
traits(UnaryOp)
traits(UnaryOp),
implements(InferTypeOpInterface)
)]
pub struct Clz {
#[operand]
Expand All @@ -168,7 +179,8 @@ infer_return_ty_for_unary_op!(Clz as Type::U32);
/// Count Trailing Zeros
#[operation (
dialect = HirDialect,
traits(UnaryOp)
traits(UnaryOp),
implements(InferTypeOpInterface)
)]
pub struct Ctz {
#[operand]
Expand All @@ -182,7 +194,8 @@ infer_return_ty_for_unary_op!(Ctz as Type::U32);
/// Count Leading Ones
#[operation (
dialect = HirDialect,
traits(UnaryOp)
traits(UnaryOp),
implements(InferTypeOpInterface)
)]
pub struct Clo {
#[operand]
Expand All @@ -196,7 +209,8 @@ infer_return_ty_for_unary_op!(Clo as Type::U32);
/// Count Trailing Ones
#[operation (
dialect = HirDialect,
traits(UnaryOp)
traits(UnaryOp),
implements(InferTypeOpInterface)
)]
pub struct Cto {
#[operand]
Expand Down
Loading

0 comments on commit b139cff

Please sign in to comment.