Skip to content

Math xdsl

math_xdsl

The math_xdsl dialect contains extensions to the math dialect. Currently, it only contains the math_xdsl.constant operation and a related attribute, which can be used to define and use mathematical constants in IR.

The idea is to upstream these extension to the math dialect in MLIR at some point when they have matured.

MathXDSL = Dialect('math_xdsl', [ConstantOp], [ConstantAttr]) module-attribute

Constant

Bases: StrEnum

Source code in xdsl/dialects/math_xdsl.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Constant(StrEnum):
    E = auto()  # 𝑒
    PI = auto()  # π
    M_2_SQRTPI = auto()  # 2/sqrt(π)
    LOG2E = auto()  # log2(𝑒)
    PI_2 = auto()  # π/2
    SQRT2 = auto()  # sqrt(2)
    LOG10E = auto()  # log10(𝑒)
    PI_4 = auto()  # π/4
    SQRT1_2 = auto()  # sqrt(1/2)
    LN2 = auto()  # ln(2)
    M_1_PI = auto()  # 1/π
    INFINITY = auto()  # ∞
    LN10 = auto()  # ln(10)
    M_2_PI = auto()  # 2/π

E = auto() class-attribute instance-attribute

PI = auto() class-attribute instance-attribute

M_2_SQRTPI = auto() class-attribute instance-attribute

LOG2E = auto() class-attribute instance-attribute

PI_2 = auto() class-attribute instance-attribute

SQRT2 = auto() class-attribute instance-attribute

LOG10E = auto() class-attribute instance-attribute

PI_4 = auto() class-attribute instance-attribute

SQRT1_2 = auto() class-attribute instance-attribute

LN2 = auto() class-attribute instance-attribute

M_1_PI = auto() class-attribute instance-attribute

INFINITY = auto() class-attribute instance-attribute

LN10 = auto() class-attribute instance-attribute

M_2_PI = auto() class-attribute instance-attribute

ConstantAttr dataclass

Bases: EnumAttribute[Constant], SpacedOpaqueSyntaxAttribute

Source code in xdsl/dialects/math_xdsl.py
41
42
43
@irdl_attr_definition
class ConstantAttr(EnumAttribute[Constant], SpacedOpaqueSyntaxAttribute):
    name = "math_xdsl.constant"

name = 'math_xdsl.constant' class-attribute instance-attribute

ConstantOp

Bases: IRDLOperation

Source code in xdsl/dialects/math_xdsl.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
@irdl_op_definition
class ConstantOp(IRDLOperation):
    name = "math_xdsl.constant"

    symbol = prop_def(ConstantAttr)

    value = result_def()

    assembly_format = "$symbol attr-dict `:` type($value)"

    def __init__(self, symbol: Constant | ConstantAttr, result_type: Attribute):
        if not isinstance(symbol, ConstantAttr):
            symbol = ConstantAttr(symbol)
        super().__init__(
            properties={
                "symbol": symbol,
            },
            result_types=[result_type],
        )

name = 'math_xdsl.constant' class-attribute instance-attribute

symbol = prop_def(ConstantAttr) class-attribute instance-attribute

value = result_def() class-attribute instance-attribute

assembly_format = '$symbol attr-dict `:` type($value)' class-attribute instance-attribute

__init__(symbol: Constant | ConstantAttr, result_type: Attribute)

Source code in xdsl/dialects/math_xdsl.py
56
57
58
59
60
61
62
63
64
def __init__(self, symbol: Constant | ConstantAttr, result_type: Attribute):
    if not isinstance(symbol, ConstantAttr):
        symbol = ConstantAttr(symbol)
    super().__init__(
        properties={
            "symbol": symbol,
        },
        result_types=[result_type],
    )