Skip to content

Index

py

This module contains the definition of the Python semantics dialect.

We only guarantee preservation of most1 Python semantics, but do not guarantee preservation of AST or bytecode.

The Python dialect is under heavy development, and the definitions are not yet stable!

    1. We assume no exceptions are raised in the code.

Py = Dialect('py', [PyBinOp, PyConstOp], [PyObjectType]) module-attribute

PyObjectType dataclass

Bases: ParametrizedAttribute, TypeAttribute

Python opaque type

Source code in xdsl/dialects/py/__init__.py
35
36
37
38
39
@irdl_attr_definition
class PyObjectType(ParametrizedAttribute, TypeAttribute):
    """Python opaque type"""

    name = "py.object"

name = 'py.object' class-attribute instance-attribute

PyOperation dataclass

Bases: IRDLOperation

Source code in xdsl/dialects/py/__init__.py
47
48
class PyOperation(IRDLOperation):
    pass

PyModuleOp dataclass

Bases: PyOperation

Python code is organized into modules and functions.

Modules are the top-level code.

Functions are self-explanatory.

For example if you have the following MLIR:

%0 = py.const 0 %1 = py.const 1 %2 = py.binop "add" %0 %1

We mean the following Python code: _0 = 1 _1 = 1 _2 = _0 + _1

Source code in xdsl/dialects/py/__init__.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
@irdl_op_definition
class PyModuleOp(PyOperation):
    """
    Python code is organized into modules and functions.

    Modules are the top-level code.

    Functions are self-explanatory.

    For example if you have the following MLIR:

    %0 = py.const 0
    %1 = py.const 1
    %2 = py.binop "add" %0 %1

    We mean the following Python code:
    _0 = 1
    _1 = 1
    _2 = _0 + _1
    """

    name = "py.module"
    body = region_def()

name = 'py.module' class-attribute instance-attribute

body = region_def() class-attribute instance-attribute

PyConstOp

Bases: PyOperation

x = CONST

Source code in xdsl/dialects/py/__init__.py
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
@irdl_op_definition
class PyConstOp(PyOperation):
    """
    x = CONST
    """

    name = "py.const"
    assembly_format = "$const attr-dict"

    # We can expand this to other types later.
    const = prop_def(IntegerAttr[I64])
    res = result_def(PyObjectType())

    def __init__(self, const: IntegerAttr[I64]):
        super().__init__(properties={"const": const}, result_types=[PyObjectType()])

name = 'py.const' class-attribute instance-attribute

assembly_format = '$const attr-dict' class-attribute instance-attribute

const = prop_def(IntegerAttr[I64]) class-attribute instance-attribute

res = result_def(PyObjectType()) class-attribute instance-attribute

__init__(const: IntegerAttr[I64])

Source code in xdsl/dialects/py/__init__.py
89
90
def __init__(self, const: IntegerAttr[I64]):
    super().__init__(properties={"const": const}, result_types=[PyObjectType()])

PyBinOp

Bases: PyOperation

x BINOP y

Source code in xdsl/dialects/py/__init__.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
@irdl_op_definition
class PyBinOp(PyOperation):
    """
    x BINOP y
    """

    name = "py.binop"
    lhs = operand_def(PyObjectType())
    rhs = operand_def(PyObjectType())
    res = result_def(PyObjectType())
    op = prop_def(StringAttr)
    assembly_format = "$op $lhs $rhs attr-dict"

    def __init__(
        self,
        op: StringAttr,
        lhs: Operand,
        rhs: Operand,
    ):
        super().__init__(
            operands=[lhs, rhs], properties={"op": op}, result_types=[PyObjectType()]
        )

name = 'py.binop' class-attribute instance-attribute

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

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

res = result_def(PyObjectType()) class-attribute instance-attribute

op = prop_def(StringAttr) class-attribute instance-attribute

assembly_format = '$op $lhs $rhs attr-dict' class-attribute instance-attribute

__init__(op: StringAttr, lhs: Operand, rhs: Operand)

Source code in xdsl/dialects/py/__init__.py
106
107
108
109
110
111
112
113
114
def __init__(
    self,
    op: StringAttr,
    lhs: Operand,
    rhs: Operand,
):
    super().__init__(
        operands=[lhs, rhs], properties={"op": op}, result_types=[PyObjectType()]
    )