Skip to content

Commit a3c629c

Browse files
weinbe58Roger-luo
authored andcommitted
Fix guard in ilist.FlattenAdd to give up on BottomType inputs. (#590)
Currently there are some cases where the assert `assert isinstance(lhs_type := lhs.type, types.Generic)` is False because `Bottom` is a subset of `IListType` in the type lattice. this PR adds some logic to make sure any Bottom types are skipped in this rewrite rule.
1 parent 4e9af22 commit a3c629c

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/kirin/dialects/ilist/rewrite/flatten_add.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@ class FlattenAdd(RewriteRule):
99
def rewrite_Statement(self, node: ir.Statement) -> RewriteResult:
1010
if not (
1111
isinstance(node, py.binop.Add)
12-
and node.lhs.type.is_subseteq(ilist.IListType)
13-
and node.rhs.type.is_subseteq(ilist.IListType)
12+
and (lhs_type := node.lhs.type).is_subseteq(ilist.IListType)
13+
and (rhs_type := node.rhs.type).is_subseteq(ilist.IListType)
14+
and not lhs_type.is_structurally_equal(types.Bottom)
15+
and not rhs_type.is_structurally_equal(types.Bottom)
1416
):
1517
return RewriteResult()
1618

19+
assert isinstance(rhs_type, types.Generic), "Expecting generic type for IList"
20+
assert isinstance(lhs_type, types.Generic), "Expecting generic type for IList"
21+
1722
# check if we are adding two ilist.New objects
1823
new_data = ()
1924

@@ -46,9 +51,6 @@ def rewrite_Statement(self, node: ir.Statement) -> RewriteResult:
4651
):
4752
return RewriteResult()
4853

49-
assert isinstance(rhs_type := rhs.type, types.Generic), "Impossible"
50-
assert isinstance(lhs_type := lhs.type, types.Generic), "Impossible"
51-
5254
lhs_elem_type = lhs_type.vars[0]
5355
rhs_elem_type = rhs_type.vars[0]
5456

0 commit comments

Comments
 (0)