Warning
This page is under construction. The content may be incomplete or incorrect. Submit an issue on GitHub if you need help or want to contribute.
Dialects for Python Syntax Sugar
This page contains the dialects designed to represent Python syntax sugar. They provide an implementation of lowering transform from the corresponding Python AST to the dialects' statements. All the statements are typed Any thus one can always use a custom rewrite pass after type inference to support the desired syntax sugar.
Reference
Indexing
kirin.dialects.py.indexing
The indexing dialect for Python.
This module contains the dialect for the Python indexing syntax, including:
- The
GetItemstatement class. - A base class
Subscriptfor indexing statements. - A trait
GetItemLikefor indexing statements. - The lowering pass for the indexing statement.
- The concrete implementation of the indexing statement.
- The constant propagation implementation (special case) of the indexing statement.
- The type inference implementation of the indexing statement.
- A canonical rewrite rule for the rewriting of a given getitem-like statement to another getitem-like statement.
GetItemLikeStmt module-attribute
GetItemLikeStmt = TypeVar(
"GetItemLikeStmt", bound=Statement
)
PyGetItemLikeStmt module-attribute
PyGetItemLikeStmt = TypeVar(
"PyGetItemLikeStmt", bound="GetItem"
)
dialect module-attribute
dialect = Dialect('py.indexing')
Concrete
Bases: MethodTable
flowchart TD
kirin.dialects.py.indexing.Concrete[Concrete]
kirin.interp.table.MethodTable[MethodTable]
kirin.interp.table.MethodTable --> kirin.dialects.py.indexing.Concrete
click kirin.dialects.py.indexing.Concrete href "" "kirin.dialects.py.indexing.Concrete"
click kirin.interp.table.MethodTable href "" "kirin.interp.table.MethodTable"
getindex
getindex(interp, frame: Frame, stmt: GetItem)
Source code in src/kirin/dialects/py/indexing.py
99 100 101 | |
ConstProp
Bases: MethodTable
flowchart TD
kirin.dialects.py.indexing.ConstProp[ConstProp]
kirin.interp.table.MethodTable[MethodTable]
kirin.interp.table.MethodTable --> kirin.dialects.py.indexing.ConstProp
click kirin.dialects.py.indexing.ConstProp href "" "kirin.dialects.py.indexing.ConstProp"
click kirin.interp.table.MethodTable href "" "kirin.interp.table.MethodTable"
getitem
getitem(
_: Propagate, frame: Frame, stmt: GetItem
) -> interp.StatementResult[const.Result]
Source code in src/kirin/dialects/py/indexing.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
GetItem kirin-statement
GetItem(obj: SSAValue, index: SSAValue)
Bases: Subscript
flowchart TD
kirin.dialects.py.indexing.GetItem[GetItem]
kirin.dialects.py.indexing.Subscript[Subscript]
kirin.ir.nodes.stmt.Statement[Statement]
kirin.ir.nodes.base.IRNode[IRNode]
kirin.print.printable.Printable[Printable]
kirin.dialects.py.indexing.Subscript --> kirin.dialects.py.indexing.GetItem
kirin.ir.nodes.stmt.Statement --> kirin.dialects.py.indexing.Subscript
kirin.ir.nodes.base.IRNode --> kirin.ir.nodes.stmt.Statement
kirin.print.printable.Printable --> kirin.ir.nodes.base.IRNode
click kirin.dialects.py.indexing.GetItem href "" "kirin.dialects.py.indexing.GetItem"
click kirin.dialects.py.indexing.Subscript href "" "kirin.dialects.py.indexing.Subscript"
click kirin.ir.nodes.stmt.Statement href "" "kirin.ir.nodes.stmt.Statement"
click kirin.ir.nodes.base.IRNode href "" "kirin.ir.nodes.base.IRNode"
click kirin.print.printable.Printable href "" "kirin.print.printable.Printable"
index kirin-argument
index: SSAValue = argument(print=False)
name class-attribute instance-attribute
name = 'getitem'
obj kirin-argument
obj: SSAValue = argument(print=False)
result kirin-result
result: ResultValue = result(Any)
traits class-attribute instance-attribute
traits = frozenset(
{Pure(), PyGetItemLike(), FromPythonCall()}
)
GetItemLike dataclass
GetItemLike()
Bases: Trait[Statement], Generic[GetItemLikeStmt]
flowchart TD
kirin.dialects.py.indexing.GetItemLike[GetItemLike]
kirin.ir.traits.abc.Trait[Trait]
kirin.ir.traits.abc.Trait --> kirin.dialects.py.indexing.GetItemLike
click kirin.dialects.py.indexing.GetItemLike href "" "kirin.dialects.py.indexing.GetItemLike"
click kirin.ir.traits.abc.Trait href "" "kirin.ir.traits.abc.Trait"
get_index abstractmethod
get_index(stmt: GetItemLikeStmt) -> ir.SSAValue
Source code in src/kirin/dialects/py/indexing.py
38 39 | |
get_object abstractmethod
get_object(stmt: GetItemLikeStmt) -> ir.SSAValue
Source code in src/kirin/dialects/py/indexing.py
35 36 | |
new abstractmethod
new(
stmt_type: type[GetItemLikeStmt],
obj: SSAValue,
index: SSAValue,
) -> GetItemLikeStmt
Source code in src/kirin/dialects/py/indexing.py
41 42 43 44 | |
Lowering dataclass
Lowering()
Bases: FromPythonAST
flowchart TD
kirin.dialects.py.indexing.Lowering[Lowering]
kirin.lowering.python.dialect.FromPythonAST[FromPythonAST]
kirin.lowering.python.dialect.FromPythonAST --> kirin.dialects.py.indexing.Lowering
click kirin.dialects.py.indexing.Lowering href "" "kirin.dialects.py.indexing.Lowering"
click kirin.lowering.python.dialect.FromPythonAST href "" "kirin.lowering.python.dialect.FromPythonAST"
lower_Subscript
lower_Subscript(
state: State, node: Subscript
) -> lowering.Result
Source code in src/kirin/dialects/py/indexing.py
84 85 86 87 88 89 90 91 92 93 | |
PyGetItemLike dataclass
PyGetItemLike()
Bases: GetItemLike[PyGetItemLikeStmt]
flowchart TD
kirin.dialects.py.indexing.PyGetItemLike[PyGetItemLike]
kirin.dialects.py.indexing.GetItemLike[GetItemLike]
kirin.ir.traits.abc.Trait[Trait]
kirin.dialects.py.indexing.GetItemLike --> kirin.dialects.py.indexing.PyGetItemLike
kirin.ir.traits.abc.Trait --> kirin.dialects.py.indexing.GetItemLike
click kirin.dialects.py.indexing.PyGetItemLike href "" "kirin.dialects.py.indexing.PyGetItemLike"
click kirin.dialects.py.indexing.GetItemLike href "" "kirin.dialects.py.indexing.GetItemLike"
click kirin.ir.traits.abc.Trait href "" "kirin.ir.traits.abc.Trait"
get_index
get_index(stmt: PyGetItemLikeStmt) -> ir.SSAValue
Source code in src/kirin/dialects/py/indexing.py
56 57 | |
get_object
get_object(stmt: PyGetItemLikeStmt) -> ir.SSAValue
Source code in src/kirin/dialects/py/indexing.py
53 54 | |
new
new(
stmt_type: type[PyGetItemLikeStmt],
obj: SSAValue,
index: SSAValue,
) -> PyGetItemLikeStmt
Source code in src/kirin/dialects/py/indexing.py
59 60 61 62 | |
RewriteGetItem dataclass
RewriteGetItem(
stmt_type: type[GetItemLikeStmt],
obj_type: TypeAttribute,
)
Bases: RewriteRule, Generic[GetItemLikeStmt]
flowchart TD
kirin.dialects.py.indexing.RewriteGetItem[RewriteGetItem]
kirin.rewrite.abc.RewriteRule[RewriteRule]
kirin.rewrite.abc.RewriteRule --> kirin.dialects.py.indexing.RewriteGetItem
click kirin.dialects.py.indexing.RewriteGetItem href "" "kirin.dialects.py.indexing.RewriteGetItem"
click kirin.rewrite.abc.RewriteRule href "" "kirin.rewrite.abc.RewriteRule"
Source code in src/kirin/dialects/py/indexing.py
230 231 232 233 234 235 236 237 | |
getitem_like instance-attribute
getitem_like: GetItemLike[GetItemLikeStmt] = trait
obj_type instance-attribute
obj_type: TypeAttribute = obj_type
target_stmt_type instance-attribute
target_stmt_type: type[GetItemLikeStmt] = stmt_type
rewrite_Statement
rewrite_Statement(node: Statement) -> RewriteResult
Source code in src/kirin/dialects/py/indexing.py
239 240 241 242 243 244 245 246 247 248 249 | |
Subscript kirin-statement
Subscript()
Bases: Statement
flowchart TD
kirin.dialects.py.indexing.Subscript[Subscript]
kirin.ir.nodes.stmt.Statement[Statement]
kirin.ir.nodes.base.IRNode[IRNode]
kirin.print.printable.Printable[Printable]
kirin.ir.nodes.stmt.Statement --> kirin.dialects.py.indexing.Subscript
kirin.ir.nodes.base.IRNode --> kirin.ir.nodes.stmt.Statement
kirin.print.printable.Printable --> kirin.ir.nodes.base.IRNode
click kirin.dialects.py.indexing.Subscript href "" "kirin.dialects.py.indexing.Subscript"
click kirin.ir.nodes.stmt.Statement href "" "kirin.ir.nodes.stmt.Statement"
click kirin.ir.nodes.base.IRNode href "" "kirin.ir.nodes.base.IRNode"
click kirin.print.printable.Printable href "" "kirin.print.printable.Printable"
TypeInfer
Bases: MethodTable
flowchart TD
kirin.dialects.py.indexing.TypeInfer[TypeInfer]
kirin.interp.table.MethodTable[MethodTable]
kirin.interp.table.MethodTable --> kirin.dialects.py.indexing.TypeInfer
click kirin.dialects.py.indexing.TypeInfer href "" "kirin.dialects.py.indexing.TypeInfer"
click kirin.interp.table.MethodTable href "" "kirin.interp.table.MethodTable"
getitem
getitem(
interp: TypeInference,
frame: Frame[TypeAttribute],
stmt: GetItem,
)
Source code in src/kirin/dialects/py/indexing.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | |
getitem_tuple
getitem_tuple(
interp,
stmt: GetItem,
obj: TypeAttribute,
index: TypeAttribute,
)
Source code in src/kirin/dialects/py/indexing.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
getitem_tuple_index
getitem_tuple_index(
interp: TypeInference,
stmt: GetItem,
obj: Generic,
index: TypeAttribute,
)
Source code in src/kirin/dialects/py/indexing.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
getitem_tuple_slice
getitem_tuple_slice(
interp: TypeInference,
stmt: GetItem,
obj: Generic,
index: TypeAttribute,
)
Source code in src/kirin/dialects/py/indexing.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
getitem_tuple_union
getitem_tuple_union(obj: Generic)
Source code in src/kirin/dialects/py/indexing.py
188 189 190 191 192 | |
Attribute
kirin.dialects.py.attr
Attribute access dialect for Python.
This module contains the dialect for the Python attribute access statement, including:
- The
GetAttrstatement class. - The lowering pass for the attribute access statement.
- The concrete implementation of the attribute access statement.
This dialect maps ast.Attribute nodes to the GetAttr statement.
dialect module-attribute
dialect = Dialect('py.attr')
Concrete
Bases: MethodTable
flowchart TD
kirin.dialects.py.attr.Concrete[Concrete]
kirin.interp.table.MethodTable[MethodTable]
kirin.interp.table.MethodTable --> kirin.dialects.py.attr.Concrete
click kirin.dialects.py.attr.Concrete href "" "kirin.dialects.py.attr.Concrete"
click kirin.interp.table.MethodTable href "" "kirin.interp.table.MethodTable"
getattr
getattr(interp: Interpreter, frame: Frame, stmt: GetAttr)
Source code in src/kirin/dialects/py/attr.py
32 33 34 | |
GetAttr kirin-statement
GetAttr(obj: SSAValue, *, attrname: str)
Bases: Statement
flowchart TD
kirin.dialects.py.attr.GetAttr[GetAttr]
kirin.ir.nodes.stmt.Statement[Statement]
kirin.ir.nodes.base.IRNode[IRNode]
kirin.print.printable.Printable[Printable]
kirin.ir.nodes.stmt.Statement --> kirin.dialects.py.attr.GetAttr
kirin.ir.nodes.base.IRNode --> kirin.ir.nodes.stmt.Statement
kirin.print.printable.Printable --> kirin.ir.nodes.base.IRNode
click kirin.dialects.py.attr.GetAttr href "" "kirin.dialects.py.attr.GetAttr"
click kirin.ir.nodes.stmt.Statement href "" "kirin.ir.nodes.stmt.Statement"
click kirin.ir.nodes.base.IRNode href "" "kirin.ir.nodes.base.IRNode"
click kirin.print.printable.Printable href "" "kirin.print.printable.Printable"
attrname kirin-attribute kw-only
attrname: str = attribute()
name class-attribute instance-attribute
name = 'getattr'
obj kirin-argument
obj: SSAValue = argument(print=False)
result kirin-result
result: ResultValue = result()
traits class-attribute instance-attribute
traits = frozenset({FromPythonCall()})
Lowering dataclass
Lowering()
Bases: FromPythonAST
flowchart TD
kirin.dialects.py.attr.Lowering[Lowering]
kirin.lowering.python.dialect.FromPythonAST[FromPythonAST]
kirin.lowering.python.dialect.FromPythonAST --> kirin.dialects.py.attr.Lowering
click kirin.dialects.py.attr.Lowering href "" "kirin.dialects.py.attr.Lowering"
click kirin.lowering.python.dialect.FromPythonAST href "" "kirin.lowering.python.dialect.FromPythonAST"
lower_Attribute
lower_Attribute(
state: State, node: Attribute
) -> lowering.Result
Source code in src/kirin/dialects/py/attr.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |