Skip to content

Commit

Permalink
loops test and fixes
Browse files Browse the repository at this point in the history
HodanPlodky committed Jan 29, 2025
1 parent 042f584 commit d35bed0
Showing 2 changed files with 35 additions and 3 deletions.
32 changes: 32 additions & 0 deletions tests/unit/compiler/venom/test_common_subexpression_elimination.py
Original file line number Diff line number Diff line change
@@ -319,3 +319,35 @@ def call(callname: str, i: int, var_name: str):
"""

_check_no_change(pre)


def test_common_subexpression_elimination_loop():
pre = """
main:
%par = param
%data0 = mload 0
%add0 = add 1, %par
%mul0 = mul %add0, %data0
jmp @loop
loop:
%data1 = mload 0
%add1 = add 1, %par
%mul1 = mul %add1, %data1
jmp @loop
"""

post = """
main:
%par = param
%data0 = mload 0
%add0 = add 1, %par
%mul0 = mul %add0, %data0
jmp @loop
loop:
%data1 = mload 0
%add1 = %add0
%mul1 = %mul0
jmp @loop
"""

_check_pre_post(pre, post)
6 changes: 3 additions & 3 deletions vyper/venom/analysis/available_expression.py
Original file line number Diff line number Diff line change
@@ -221,10 +221,10 @@ def copy(self) -> "_AvailableExpression":
return res

@staticmethod
def intersection(*others: "_AvailableExpression"):
def intersection(*others: "_AvailableExpression | None"):
if len(others) == 0:
return _AvailableExpression()
tmp = list(others)
tmp = list(o for o in others if o is not None)
res = tmp[0].copy()
for item in tmp[1:]:
buckets = res.buckets.keys() & item.buckets.keys()
@@ -283,7 +283,7 @@ def _contains_msize(self) -> bool:

def _handle_bb(self, bb: IRBasicBlock) -> bool:
available_expr: _AvailableExpression = _AvailableExpression.intersection(
*(self.bb_outs.get(out_bb, _AvailableExpression()) for out_bb in bb.cfg_in)
*(self.bb_outs.get(out_bb, None) for out_bb in bb.cfg_in)
)

change = False

0 comments on commit d35bed0

Please sign in to comment.