-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_infer_len.py
More file actions
37 lines (29 loc) · 969 Bytes
/
test_infer_len.py
File metadata and controls
37 lines (29 loc) · 969 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from typing import Any, Literal
from kirin import rewrite
from kirin.prelude import basic
from kirin.dialects import py, ilist
def test():
rule = rewrite.Fixpoint(
rewrite.Walk(
rewrite.Chain(
ilist.rewrite.HintLen(),
rewrite.ConstantFold(),
rewrite.DeadCodeElimination(),
)
)
)
@basic
def len_func(xs: ilist.IList[int, Literal[3]]):
return len(xs)
@basic
def len_func3(xs: ilist.IList[int, Any]):
return len(xs)
rule.rewrite(len_func.code)
rule.rewrite(len_func3.code)
stmt = len_func.callable_region.blocks[0].stmts.at(0)
assert isinstance(stmt, py.Constant)
assert stmt.value.unwrap() == 3
assert len(len_func.callable_region.blocks[0].stmts) == 2
stmt = len_func3.callable_region.blocks[0].stmts.at(0)
assert isinstance(stmt, py.Len)
assert len(len_func3.callable_region.blocks[0].stmts) == 2