Skip to content

Commit 885b70f

Browse files
authored
fix const fold when hitting non-pure invoke (#127)
1 parent 4481351 commit 885b70f

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/kirin/rules/fold.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ def get_const(self, value: ir.SSAValue):
1717
return ret.const
1818
return None
1919

20+
def delete_node(self, node: ir.Statement):
21+
node.delete()
22+
for result in node.results:
23+
self.results.pop(result, None)
24+
2025
def rewrite_Statement(self, node: ir.Statement) -> RewriteResult:
2126
if node.has_trait(ir.ConstantLike):
2227
return RewriteResult()
@@ -41,10 +46,16 @@ def rewrite_Statement(self, node: ir.Statement) -> RewriteResult:
4146
# NOTE: if we find call prop a const, depsite it is pure or not
4247
# the constant call only executes a pure branch of the code
4348
# thus it is safe to delete the call
44-
if all_constants and (node.has_trait(ir.Pure) or isinstance(node, func.Invoke)):
45-
node.delete()
46-
for result in node.results:
47-
self.results.pop(result, None)
49+
if all_constants and node.has_trait(ir.Pure):
50+
self.delete_node(node)
51+
52+
if (
53+
all_constants
54+
and isinstance(node, func.Invoke)
55+
and (value := self.results.get(node.result, None)) is not None
56+
and isinstance(value.purity, const.Pure)
57+
):
58+
self.delete_node(node)
4859
return RewriteResult(has_done_something=has_done_something)
4960

5061
def rewrite_cf_ConditionalBranch(self, node: cf.ConditionalBranch):

0 commit comments

Comments
 (0)