Skip to content

Mod arith

mod_arith

mod_arith is a dialect implementing modular arithmetic, originally implemented as part of the HEIR project (https://github.com/google/heir/tree/main).

ModArith = Dialect('mod_arith', [AddOp]) module-attribute

BinaryOp

Bases: IRDLOperation, ABC

Simple binary operation

Source code in xdsl/dialects/mod_arith.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class BinaryOp(IRDLOperation, ABC):
    """
    Simple binary operation
    """

    T: ClassVar = VarConstraint("T", signlessIntegerLike)

    lhs = operand_def(T)
    rhs = operand_def(T)
    output = result_def(T)
    modulus = prop_def(IntegerAttr)

    irdl_options = (ParsePropInAttrDict(),)

    assembly_format = "$lhs `,` $rhs attr-dict `:` type($output)"
    traits = traits_def(Pure())

    def __init__(
        self,
        lhs: SSAValue | Operation,
        rhs: SSAValue | Operation,
        result_type: Attribute | None,
        modulus: Attribute,
    ):
        if result_type is None:
            result_type = SSAValue.get(lhs).type

        super().__init__(
            operands=[lhs, rhs],
            result_types=[result_type],
            properties={"modulus": modulus},
        )

T: ClassVar = VarConstraint('T', signlessIntegerLike) class-attribute instance-attribute

lhs = operand_def(T) class-attribute instance-attribute

rhs = operand_def(T) class-attribute instance-attribute

output = result_def(T) class-attribute instance-attribute

modulus = prop_def(IntegerAttr) class-attribute instance-attribute

irdl_options = (ParsePropInAttrDict(),) class-attribute instance-attribute

assembly_format = '$lhs `,` $rhs attr-dict `:` type($output)' class-attribute instance-attribute

traits = traits_def(Pure()) class-attribute instance-attribute

__init__(lhs: SSAValue | Operation, rhs: SSAValue | Operation, result_type: Attribute | None, modulus: Attribute)

Source code in xdsl/dialects/mod_arith.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def __init__(
    self,
    lhs: SSAValue | Operation,
    rhs: SSAValue | Operation,
    result_type: Attribute | None,
    modulus: Attribute,
):
    if result_type is None:
        result_type = SSAValue.get(lhs).type

    super().__init__(
        operands=[lhs, rhs],
        result_types=[result_type],
        properties={"modulus": modulus},
    )

AddOp dataclass

Bases: BinaryOp

Source code in xdsl/dialects/mod_arith.py
59
60
61
@irdl_op_definition
class AddOp(BinaryOp):
    name = "mod_arith.add"

name = 'mod_arith.add' class-attribute instance-attribute