Skip to content

Commit 7522b59

Browse files
authored
Add doc/example (#133)
1 parent 2b7a2ff commit 7522b59

File tree

4 files changed

+118
-30
lines changed

4 files changed

+118
-30
lines changed

src/kirin/dialects/fcf/stmts.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,30 @@ class Foldr(ir.Statement):
2121

2222
@statement(dialect=dialect)
2323
class Map(ir.Statement):
24+
"""Apply a given kernel function to every item of an iterable.
25+
26+
## Example
27+
28+
```python
29+
from kirin.prelude import basic
30+
31+
@basic(typeinfer=True)
32+
def myfunc(x: int):
33+
return x*3
34+
35+
@basic(typeinfer=True)
36+
def main():
37+
return fcf.Map(fn=myfunc, coll=range(10))
38+
39+
```
40+
"""
41+
2442
fn: ir.SSAValue = info.argument(ir.types.PyClass(ir.Method))
43+
"""The kernel function to apply. The function should have signature `fn(x: int) -> Any`."""
2544
coll: ir.SSAValue = info.argument(ir.types.Any)
45+
"""The iterable to map over."""
2646
result: ir.ResultValue = info.result(ir.types.List)
47+
"""The list of results."""
2748

2849

2950
@statement(dialect=dialect)

src/kirin/interp/base.py

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ def run_method(
9494
This is defined by subclasses to describe what's the corresponding
9595
value of a method during the interpretation.
9696
97-
Args
97+
Args:
9898
method (Method): the method to run.
9999
args (tuple[ValueType]): the arguments to the method, does not include self.
100100
101-
Returns
101+
Returns:
102102
ValueType: the result of the method.
103103
"""
104104
...
@@ -108,12 +108,12 @@ def run_callable(
108108
) -> MethodResult[ValueType]:
109109
"""Run a callable statement.
110110
111-
Args
111+
Args:
112112
code (Statement): the statement to run.
113113
args (tuple[ValueType]): the arguments to the statement,
114114
includes self if the corresponding callable region contains a self argument.
115115
116-
Returns
116+
Returns:
117117
ValueType: the result of the statement.
118118
"""
119119
interface = code.get_trait(traits.CallableStmtInterface)
@@ -133,10 +133,13 @@ def run_callable_region(
133133
self, frame: FrameType, code: Statement, region: Region
134134
) -> MethodResult[ValueType]:
135135
"""A hook defines how to run the callable region given
136-
the interpreter context. This is experimental API, don't
137-
subclass it. The current reason of having it is mainly
138-
because we need to dispatch back to the MethodTable for
139-
emit.
136+
the interpreter context.
137+
138+
Note:
139+
This is experimental API, don't
140+
subclass it. The current reason of having it is mainly
141+
because we need to dispatch back to the MethodTable for
142+
emit.
140143
"""
141144
return self.run_ssacfg_region(frame, region)
142145

@@ -149,8 +152,10 @@ def finalize(
149152
self, frame: FrameType, results: MethodResult[ValueType]
150153
) -> MethodResult[ValueType]:
151154
"""Postprocess a frame after it is popped from the stack. This is
152-
called after a method is evaluated and the frame is popped. Default
153-
implementation does nothing.
155+
called after a method is evaluated and the frame is popped.
156+
157+
Note:
158+
Default implementation does nothing.
154159
"""
155160
return results
156161

@@ -175,11 +180,10 @@ def permute_values(
175180
the given keyword arguments, where the keyword argument names
176181
refer to the last n arguments in the values tuple.
177182
178-
Args
179-
180-
mt: the method
181-
values: the values tuple (should not contain method itself)
182-
kwarg_names: the keyword argument names
183+
Args:
184+
arg_names: the argument names
185+
values: the values tuple (should not contain method itself)
186+
kwarg_names: the keyword argument names
183187
"""
184188
n_total = len(values)
185189
if kwarg_names:
@@ -194,7 +198,32 @@ def permute_values(
194198
return args
195199

196200
def run_stmt(self, frame: FrameType, stmt: Statement) -> StatementResult[ValueType]:
197-
"run a statement within the current frame"
201+
"""Run a statement within the current frame
202+
203+
Args:
204+
frame: the current frame
205+
stmt: the statement to run
206+
207+
Returns:
208+
StatementResult: the result of running the statement
209+
210+
Note:
211+
In the case of implementing the fallback, subclass this method,
212+
and filter the statement type you want to handle.
213+
214+
Example:
215+
* implement an interpreter that only handles MyStmt:
216+
```python
217+
class MyInterpreter(BaseInterpreter):
218+
...
219+
def run_stmt(self, frame: FrameType, stmt: Statement) -> StatementResult[ValueType]:
220+
if isinstance(stmt, MyStmt):
221+
return self.run_my_stmt(frame, stmt)
222+
else:
223+
return ()
224+
```
225+
226+
"""
198227
# TODO: update tracking information
199228
return self.eval_stmt(frame, stmt)
200229

src/kirin/ir/dialect.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,27 @@
1818
# TODO: add an option to generate default lowering at dialect construction
1919
@dataclass
2020
class Dialect:
21-
"""Dialect is a collection of statements, attributes, interpreters, lowerings, and codegen."""
21+
"""Dialect is a collection of statements, attributes, interpreters, lowerings, and codegen.
22+
23+
Example:
24+
```python
25+
from kirin import ir
26+
27+
my_dialect = ir.Dialect(name="my_dialect")
28+
29+
```
30+
"""
2231

2332
name: str
33+
"""The name of the dialect."""
2434
stmts: list[type[Statement]] = field(default_factory=list, init=True)
35+
"""A list of statements in the dialect."""
2536
attrs: list[type[Attribute]] = field(default_factory=list, init=True)
37+
"""A list of attributes in the dialect."""
2638
interps: dict[str, MethodTable] = field(default_factory=dict, init=True)
39+
"""A dictionary of registered method table in the dialect."""
2740
lowering: dict[str, FromPythonAST] = field(default_factory=dict, init=True)
41+
"""A dictionary of registered python lowering implmentations in the dialect."""
2842

2943
def __post_init__(self) -> None:
3044
from kirin.lowering.dialect import NoSpecialLowering
@@ -47,6 +61,31 @@ def register(self, node: type | None = None, key: str | None = None):
4761
4862
Raises:
4963
ValueError: If the node is not a subclass of Statement, Attribute, DialectInterpreter, FromPythonAST, or DialectEmit.
64+
65+
Example:
66+
* Register a method table for concrete interpreter (by default key="main") to the dialect:
67+
```python
68+
from kirin import ir
69+
70+
my_dialect = ir.Dialect(name="my_dialect")
71+
72+
@my_dialect.register
73+
class MyMethodTable(ir.MethodTable):
74+
...
75+
```
76+
77+
* Register a method table for the interpreter specified by `key` to the dialect:
78+
```python
79+
from kirin import ir
80+
81+
my_dialect = ir.Dialect(name="my_dialect")
82+
83+
@my_dialect.register(key="my_interp")
84+
class MyMethodTable(ir.MethodTable):
85+
...
86+
```
87+
88+
5089
"""
5190
from kirin.interp.dialect import MethodTable
5291
from kirin.lowering.dialect import FromPythonAST

src/kirin/ir/group.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -200,19 +200,18 @@ def dialect_group(
200200
Callable[[RunPassGen[PassParams]], DialectGroup[PassParams]]: the dialect group.
201201
202202
Example:
203-
204-
```python
205-
from kirin.dialects import cf, fcf, func, math
206-
207-
@dialect_group([cf, fcf, func, math])
208-
def basic_no_opt(self):
209-
# initializations
210-
def run_pass(mt: Method) -> None:
211-
# how passes are applied to the method
212-
pass
213-
214-
return run_pass
215-
```
203+
```python
204+
from kirin.dialects import cf, fcf, func, math
205+
206+
@dialect_group([cf, fcf, func, math])
207+
def basic_no_opt(self):
208+
# initializations
209+
def run_pass(mt: Method) -> None:
210+
# how passes are applied to the method
211+
pass
212+
213+
return run_pass
214+
```
216215
"""
217216

218217
# NOTE: do not alias the annotation below

0 commit comments

Comments
 (0)