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.
PyConstValue = IntAttr | BoolAttr | FloatAttr[Float64Type] | StringAttr | NoneType
module-attribute
¶
The attribute kinds a py.const can hold, one per Python literal type.
Py = Dialect('py', [PyBinOp, PyBuildTupleOp, PyCallOp, PyCastOp, PyCompareOp, PyConstOp, PyFuncOp, PyReturnOp, PyUnpackOp], [CmpOpAttr, OperatorAttr, PyObjectType])
module-attribute
¶
PyObjectType
¶
Bases: ParametrizedAttribute, TypeAttribute
Python opaque type, with the annotation it was declared with as written in the source, or an empty string if it has none.
Source code in xdsl/dialects/py/__init__.py
name = 'py.object'
class-attribute
instance-attribute
¶
annotation: StringAttr
instance-attribute
¶
__init__(annotation: str | StringAttr = '')
¶
print_parameters(printer: Printer) -> None
¶
parse_parameters(parser: AttrParser) -> tuple[StringAttr]
classmethod
¶
Source code in xdsl/dialects/py/__init__.py
PyOperation
dataclass
¶
Bases: IRDLOperation
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
PyConstOp
¶
Bases: PyOperation
x = CONST
Holds a Python int (of any size), bool, float, str or None literal.
Source code in xdsl/dialects/py/__init__.py
name = 'py.const'
class-attribute
instance-attribute
¶
const = prop_def(PyConstValue)
class-attribute
instance-attribute
¶
res = result_def(PyObjectType())
class-attribute
instance-attribute
¶
traits = traits_def(Pure())
class-attribute
instance-attribute
¶
value: int | bool | float | str | None
property
¶
The Python literal this constant holds.
__init__(const: Attribute)
¶
from_value(value: int | bool | float | str | None) -> PyConstOp
staticmethod
¶
Build the constant for a Python literal.
Source code in xdsl/dialects/py/__init__.py
parse(parser: Parser) -> PyConstOp
classmethod
¶
Source code in xdsl/dialects/py/__init__.py
print(printer: Printer) -> None
¶
Source code in xdsl/dialects/py/__init__.py
Operator
¶
Bases: StrEnum
A Python binary operator, named as in Python's ast module, in lowercase.
Source code in xdsl/dialects/py/__init__.py
ADD = 'add'
class-attribute
instance-attribute
¶
SUB = 'sub'
class-attribute
instance-attribute
¶
MULT = 'mult'
class-attribute
instance-attribute
¶
MAT_MULT = 'matmult'
class-attribute
instance-attribute
¶
DIV = 'div'
class-attribute
instance-attribute
¶
MOD = 'mod'
class-attribute
instance-attribute
¶
POW = 'pow'
class-attribute
instance-attribute
¶
L_SHIFT = 'lshift'
class-attribute
instance-attribute
¶
R_SHIFT = 'rshift'
class-attribute
instance-attribute
¶
BIT_OR = 'bitor'
class-attribute
instance-attribute
¶
BIT_XOR = 'bitxor'
class-attribute
instance-attribute
¶
BIT_AND = 'bitand'
class-attribute
instance-attribute
¶
FLOOR_DIV = 'floordiv'
class-attribute
instance-attribute
¶
OperatorAttr
dataclass
¶
Bases: EnumAttribute[Operator], SpacedOpaqueSyntaxAttribute
Source code in xdsl/dialects/py/__init__.py
name = 'py.operator'
class-attribute
instance-attribute
¶
PyBinOp
¶
Bases: PyOperation
x BINOP y
Source code in xdsl/dialects/py/__init__.py
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(OperatorAttr)
class-attribute
instance-attribute
¶
assembly_format = '$op `,` $lhs `,` $rhs attr-dict'
class-attribute
instance-attribute
¶
__init__(op: Operator | OperatorAttr, lhs: Operand, rhs: Operand)
¶
PyCallOp
¶
Bases: PyOperation
CALLEE(ARGS, KEYWORD=ARG, ...)
Call the Python object named callee, as written in the source, for example
"f" or "MyClass.method". The callee is not resolved here. The last
len(keywords) arguments are passed by keyword, with these names.
Source code in xdsl/dialects/py/__init__.py
name = 'py.call'
class-attribute
instance-attribute
¶
callee = prop_def(StringAttr)
class-attribute
instance-attribute
¶
keywords = opt_prop_def(ArrayAttr[StringAttr])
class-attribute
instance-attribute
¶
args = var_operand_def(PyObjectType())
class-attribute
instance-attribute
¶
res = result_def(PyObjectType())
class-attribute
instance-attribute
¶
assembly_format = '$callee `(` $args `)` (`keywords` $keywords^)? attr-dict'
class-attribute
instance-attribute
¶
keyword_names: tuple[str, ...]
property
¶
__init__(callee: str | StringAttr, args: Sequence[SSAValue], keywords: Sequence[str | StringAttr] = ())
¶
Source code in xdsl/dialects/py/__init__.py
PyBuildTupleOp
¶
Bases: PyOperation
(ELEMENT, ...)
Build a Python tuple from its elements, in order.
Source code in xdsl/dialects/py/__init__.py
name = 'py.build_tuple'
class-attribute
instance-attribute
¶
elements = var_operand_def(PyObjectType())
class-attribute
instance-attribute
¶
res = result_def(PyObjectType())
class-attribute
instance-attribute
¶
assembly_format = '`(` $elements `)` attr-dict'
class-attribute
instance-attribute
¶
traits = traits_def(Pure())
class-attribute
instance-attribute
¶
PyUnpackOp
¶
Bases: PyOperation
x, y, ... = VALUE
Unpack a Python iterable into a fixed number of values.
Source code in xdsl/dialects/py/__init__.py
PyCastOp
¶
Bases: PyOperation, HasFolderInterface
Cast a Python object to another !py.object type, for example to or from an
annotated one. Casts to the same type fold away.
Source code in xdsl/dialects/py/__init__.py
name = 'py.cast'
class-attribute
instance-attribute
¶
value = operand_def(PyObjectType)
class-attribute
instance-attribute
¶
res = result_def(PyObjectType)
class-attribute
instance-attribute
¶
assembly_format = '$value attr-dict `:` type($value) `to` type($res)'
class-attribute
instance-attribute
¶
traits = traits_def(Pure())
class-attribute
instance-attribute
¶
__init__(value: SSAValue, result_type: PyObjectType)
¶
CmpOp
¶
Bases: StrEnum
A Python comparison operator, named as in Python's ast module, in lowercase.
Source code in xdsl/dialects/py/__init__.py
EQ = 'eq'
class-attribute
instance-attribute
¶
NOT_EQ = 'noteq'
class-attribute
instance-attribute
¶
LT = 'lt'
class-attribute
instance-attribute
¶
LTE = 'lte'
class-attribute
instance-attribute
¶
GT = 'gt'
class-attribute
instance-attribute
¶
GTE = 'gte'
class-attribute
instance-attribute
¶
IS = 'is'
class-attribute
instance-attribute
¶
IS_NOT = 'isnot'
class-attribute
instance-attribute
¶
IN = 'in'
class-attribute
instance-attribute
¶
NOT_IN = 'notin'
class-attribute
instance-attribute
¶
CmpOpAttr
dataclass
¶
Bases: EnumAttribute[CmpOp], SpacedOpaqueSyntaxAttribute
Source code in xdsl/dialects/py/__init__.py
name = 'py.cmpop'
class-attribute
instance-attribute
¶
PyCompareOp
¶
Bases: PyOperation
x CMPOP y
Source code in xdsl/dialects/py/__init__.py
name = 'py.compare'
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(CmpOpAttr)
class-attribute
instance-attribute
¶
assembly_format = '$op `,` $lhs `,` $rhs attr-dict'
class-attribute
instance-attribute
¶
__init__(op: CmpOp | CmpOpAttr, lhs: Operand, rhs: Operand)
¶
PyFuncOp
¶
Bases: PyOperation
def NAME(ARG: ANNOTATION, ...) -> RETURN_ANNOTATION: BODY
The name is printed like a symbol, but is not resolved through a symbol table,
like a py.call callee. The annotations are kept on the types of the arguments
and result.
Source code in xdsl/dialects/py/__init__.py
name = 'py.func'
class-attribute
instance-attribute
¶
func_name = prop_def(SymbolNameConstraint())
class-attribute
instance-attribute
¶
function_type = prop_def(FunctionType)
class-attribute
instance-attribute
¶
body = region_def('single_block')
class-attribute
instance-attribute
¶
traits = traits_def(IsolatedFromAbove())
class-attribute
instance-attribute
¶
__init__(name: str | StringAttr, function_type: FunctionType, body: Region)
¶
parse(parser: Parser) -> PyFuncOp
classmethod
¶
Source code in xdsl/dialects/py/__init__.py
print(printer: Printer) -> None
¶
verify_() -> None
¶
Source code in xdsl/dialects/py/__init__.py
PyReturnOp
¶
Bases: PyOperation
return VALUE
The value must have the function's result type. A bare return returns None,
which is constructed first, as in bytecode.