11from kirin import ir , types , interp
2- from kirin .analysis import ForwardFrame , TypeInference , const
2+ from kirin .analysis import ForwardFrame , TypeInference
33from kirin .dialects import func
44from kirin .dialects .eltype import ElType
55
6- from .stmts import For , Yield , IfElse
6+ from . import absint
7+ from .stmts import For , IfElse
78from ._dialect import dialect
89
910
1011@dialect .register (key = "typeinfer" )
11- class TypeInfer (interp .MethodTable ):
12-
13- @interp .impl (Yield )
14- def yield_stmt (
15- self ,
16- interp_ : TypeInference ,
17- frame : ForwardFrame [types .TypeAttribute ],
18- stmt : Yield ,
19- ):
20- return interp .YieldValue (frame .get_values (stmt .values ))
12+ class TypeInfer (absint .Methods ):
2113
2214 @interp .impl (IfElse )
23- def if_else (
15+ def if_else_ (
2416 self ,
2517 interp_ : TypeInference ,
2618 frame : ForwardFrame [types .TypeAttribute ],
@@ -29,32 +21,7 @@ def if_else(
2921 frame .set (
3022 stmt .cond , frame .get (stmt .cond ).meet (types .Bool )
3123 ) # set cond backwards
32- if isinstance (hint := stmt .cond .hints .get ("const" ), const .Value ):
33- if hint .data :
34- return self ._infer_if_else_cond (interp_ , frame , stmt , stmt .then_body )
35- else :
36- return self ._infer_if_else_cond (interp_ , frame , stmt , stmt .else_body )
37- then_results = self ._infer_if_else_cond (interp_ , frame , stmt , stmt .then_body )
38- else_results = self ._infer_if_else_cond (interp_ , frame , stmt , stmt .else_body )
39- return interp_ .join_results (then_results , else_results )
40-
41- def _infer_if_else_cond (
42- self ,
43- interp_ : TypeInference ,
44- frame : ForwardFrame [types .TypeAttribute ],
45- stmt : IfElse ,
46- body : ir .Region ,
47- ):
48- body_block = body .blocks [0 ]
49- body_term = body_block .last_stmt
50- if isinstance (body_term , func .Return ): # TODO: use types.Literal?
51- frame .worklist .append (interp .Successor (body_block , types .Bool ))
52- return
53-
54- with interp_ .state .new_frame (interp_ .new_frame (stmt )) as body_frame :
55- body_frame .entries .update (frame .entries )
56- body_frame .set (body_block .args [0 ], types .Bool )
57- return interp_ .run_ssacfg_region (body_frame , stmt .then_body )
24+ return super ().if_else (self , interp_ , frame , stmt )
5825
5926 @interp .impl (For )
6027 def for_loop (
0 commit comments