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 special Python functions
There are some special built-in Python functions that does not necessarily provide new data types when using them as Kirin dialects.
For example, the py.range
dialect may not use a Python range
type, the actual type can be decided by another dialect that implements the type inference method for Range
statement, e.g ilist
dialect provides an IList
implementation for Range
statement.
The reason for this is that in many cases, eDSLs are not interested in the actual data type of the result, but rather the semantics of the operation. For ilist
dialect, the Range
statement is just a syntax sugar for creating a list of integers. The compiler will decide what the actual implementation (such as the memory layout) of the list should be.
References
Iterable
kirin.dialects.py.iterable
This module provides access to Python iterables.
This is used to lower Python loops into cf
dialect.
This module contains the common methods for the Python iterable:
- The
Iter
statement class. - The
Next
statement class. - The lowering pass for the iterable.
- The concrete implementation of the iterable.
This dialect maps iter()
and next()
calls to the Iter
and Next
statements.
PyRangeIterType module-attribute
PyRangeIterType = PyClass(type(iter(range(0))))
dialect module-attribute
dialect = Dialect('py.iterable')
Concrete dataclass
Concrete()
Bases: MethodTable
iter_
iter_(interp, frame: interp.Frame, stmt: Iter)
Source code in src/kirin/dialects/py/iterable.py
46 47 48 |
|
next_
next_(interp, frame: interp.Frame, stmt: Next)
Source code in src/kirin/dialects/py/iterable.py
50 51 52 |
|
Iter kirin-statement
Iter(value: ir.SSAValue)
Bases: Statement
This is equivalent to iter(value)
in Python.
iter kirin-result
iter: ResultValue = result(Any)
traits class-attribute
instance-attribute
traits = frozenset({Pure()})
value kirin-argument
value: SSAValue = argument(Any)
Lowering
Bases: FromPythonAST
lower_Call_iter
lower_Call_iter(
state: lowering.LoweringState, node: Call
) -> lowering.Result
Source code in src/kirin/dialects/py/iterable.py
70 71 72 73 74 75 76 77 |
|
lower_Call_next
lower_Call_next(
state: lowering.LoweringState, node: Call
) -> lowering.Result
Source code in src/kirin/dialects/py/iterable.py
79 80 81 82 83 84 85 86 87 88 89 90 |
|
Next kirin-statement
Next(iter: ir.SSAValue)
Bases: Statement
This is equivalent to next(iterable, None)
in Python.
iter kirin-argument
iter: SSAValue = argument(Any)
value kirin-result
value: ResultValue = result(Any)
TypeInfer dataclass
TypeInfer()
Bases: MethodTable
iter_
iter_(interp, frame: interp.Frame, stmt: Iter)
Source code in src/kirin/dialects/py/iterable.py
58 59 60 |
|
next_
next_(interp, frame: interp.Frame, stmt: Next)
Source code in src/kirin/dialects/py/iterable.py
62 63 64 |
|
Len
kirin.dialects.py.len
The Len
dialect.
This dialect maps the len()
call to the Len
statement:
- The
Len
statement class. - The lowering pass for the
len()
call. - The concrete implementation of the
len()
call.
dialect module-attribute
dialect = Dialect('py.len')
Concrete dataclass
Concrete()
Bases: MethodTable
len
len(interp, frame: interp.Frame, stmt: Len)
Source code in src/kirin/dialects/py/len.py
30 31 32 |
|
ConstProp dataclass
ConstProp()
Bases: MethodTable
len
len(interp, frame: interp.Frame, stmt: Len)
Source code in src/kirin/dialects/py/len.py
38 39 40 41 42 43 44 45 46 |
|
Len kirin-statement
Len(value: ir.SSAValue)
Bases: Statement
name class-attribute
instance-attribute
name = 'len'
result kirin-result
result: ResultValue = result(Int)
traits class-attribute
instance-attribute
traits = frozenset({Pure(), FromPythonCall()})
value kirin-argument
value: SSAValue = argument(Any)
Lowering
Bases: FromPythonAST
lower_Call_len
lower_Call_len(
state: lowering.LoweringState, node: ast.Call
) -> lowering.Result
Source code in src/kirin/dialects/py/len.py
52 53 54 55 56 57 |
|
Range
kirin.dialects.py.range
The range dialect for Python.
This dialect models the builtin range()
function in Python.
The dialect includes: - The Range
statement class. - The lowering pass for the range()
function.
This dialect does not include a concrete implementation or type inference for the range()
function. One needs to use other dialect for the concrete implementation and type inference, e.g., ilist
dialect.
dialect module-attribute
dialect = Dialect('py.range')
Range kirin-statement
Range(
start: ir.SSAValue, stop: ir.SSAValue, step: ir.SSAValue
)
Bases: Statement
name class-attribute
instance-attribute
name = 'range'
result kirin-result
result: ResultValue = result(PyClass(range))
start kirin-argument
start: SSAValue = argument(Int)
step kirin-argument
step: SSAValue = argument(Int)
stop kirin-argument
stop: SSAValue = argument(Int)
traits class-attribute
instance-attribute
traits = frozenset({Pure(), FromPythonRangeLike()})
TypeInfer dataclass
TypeInfer()
Bases: MethodTable
eltype_range
eltype_range(
interp_, frame: interp.Frame, stmt: eltype.ElType
)
Source code in src/kirin/dialects/py/range.py
34 35 36 |
|
Slice
kirin.dialects.py.slice
The slice dialect for Python.
This dialect provides a Slice
statement that represents a slice object in Python:
- The
Slice
statement class. - The lowering pass for the
slice
call. - The concrete implementation of the
slice
call. - The type inference implementation of the
slice
call.
T module-attribute
T = TypeVar('T')
dialect module-attribute
dialect = Dialect('py.slice')
Concrete dataclass
Concrete()
Bases: MethodTable
_slice
_slice(interp, frame: interp.Frame, stmt: Slice)
Source code in src/kirin/dialects/py/slice.py
68 69 70 71 72 73 74 75 76 |
|
Lowering
Bases: FromPythonAST
lower_Call_slice
lower_Call_slice(
state: lowering.LoweringState, node: ast.Call
) -> lowering.Result
Source code in src/kirin/dialects/py/slice.py
98 99 100 101 |
|
lower_Slice
lower_Slice(
state: lowering.LoweringState, node: ast.Slice
) -> lowering.Result
Source code in src/kirin/dialects/py/slice.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
Slice kirin-statement
Slice(
start: ir.SSAValue, stop: ir.SSAValue, step: ir.SSAValue
)
Bases: Statement
Source code in src/kirin/dialects/py/slice.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
|
name class-attribute
instance-attribute
name = 'slice'
result kirin-result
result: ResultValue = result(Slice[T])
start kirin-argument
start: SSAValue = argument(T | NoneType)
step kirin-argument
step: SSAValue = argument(T | NoneType)
stop kirin-argument
stop: SSAValue = argument(T | NoneType)
traits class-attribute
instance-attribute
traits = frozenset({Pure(), SliceLowering()})
SliceLowering dataclass
SliceLowering()
Bases: FromPythonCall['Slice']
lower
lower(
stmt: type[Slice],
state: lowering.LoweringState,
node: ast.Call,
) -> lowering.Result
Source code in src/kirin/dialects/py/slice.py
24 25 26 27 |
|
_lower_slice
_lower_slice(
state: lowering.LoweringState, node: ast.Call
) -> lowering.Result
Source code in src/kirin/dialects/py/slice.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
|
Built-in Function
kirin.dialects.py.builtin
builtin dialect for python builtins
This dialect provides implementations for builtin functions like abs and sum.
- Statements:
Abs
,Sum
. - The lowering pass for the builtin functions.
- The concrete implementation of the builtin functions.
- The type inference implementation of the builtin functions.
This dialect maps ast.Call
nodes of builtin functions to the Abs
and Sum
statements.
T module-attribute
T = TypeVar('T', bound=Int | Float)
dialect module-attribute
dialect = Dialect('py.builtin')
Abs kirin-statement
Abs(value: ir.SSAValue)
Bases: Statement
name class-attribute
instance-attribute
name = 'abs'
result kirin-result
result: ResultValue = result(T)
traits class-attribute
instance-attribute
traits = frozenset({Pure(), FromPythonCall()})
value kirin-argument
value: SSAValue = argument(T, print=False)
Concrete dataclass
Concrete()
Bases: MethodTable
_sum
_sum(interp, frame: interp.Frame, stmt: Sum)
Source code in src/kirin/dialects/py/builtin.py
64 65 66 |
|
abs
abs(interp, frame: interp.Frame, stmt: Abs)
Source code in src/kirin/dialects/py/builtin.py
60 61 62 |
|
Lowering
Bases: FromPythonAST
lower_Call_abs
lower_Call_abs(
state: lowering.LoweringState, node: Call
) -> lowering.Result
Source code in src/kirin/dialects/py/builtin.py
42 43 44 45 46 47 |
|
lower_Call_sum
lower_Call_sum(
state: lowering.LoweringState, node: Call
) -> lowering.Result
Source code in src/kirin/dialects/py/builtin.py
49 50 51 52 53 54 |
|
Sum kirin-statement
Sum(value: ir.SSAValue)
Bases: Statement
name class-attribute
instance-attribute
name = 'sum'
result kirin-result
result: ResultValue = result(Any)
traits class-attribute
instance-attribute
traits = frozenset({Pure(), FromPythonCall()})
value kirin-argument
value: SSAValue = argument(Any, print=False)
TypeInfer dataclass
TypeInfer()
Bases: MethodTable
absf
absf(interp, frame, stmt)
Source code in src/kirin/dialects/py/builtin.py
76 77 78 |
|
absi
absi(interp, frame, stmt)
Source code in src/kirin/dialects/py/builtin.py
72 73 74 |
|