From fd19914707b19821d9b93f793c456f1731ad1dfd Mon Sep 17 00:00:00 2001 From: Charles Cooper Date: Fri, 7 Jun 2024 21:53:17 -0400 Subject: [PATCH 1/3] fix: loop invariant was in body, not entry block loop invariant bound check was in the body of the loop, not the entry block. move it up, it improves code --- vyper/venom/ir_node_to_venom.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/vyper/venom/ir_node_to_venom.py b/vyper/venom/ir_node_to_venom.py index 61b3c081ff..a12fb06b25 100644 --- a/vyper/venom/ir_node_to_venom.py +++ b/vyper/venom/ir_node_to_venom.py @@ -494,6 +494,9 @@ def emit_body_blocks(): end = entry_block.append_instruction("add", start, end) if bound: bound = entry_block.append_instruction("add", start, bound) + xor_ret = entry_block.append_instruction("xor", counter_var, bound) + entry_block.append_instruction("assert", xor_ret) + entry_block.append_instruction("jmp", cond_block.label) xor_ret = cond_block.append_instruction("xor", counter_var, end) @@ -501,9 +504,6 @@ def emit_body_blocks(): fn.append_basic_block(cond_block) fn.append_basic_block(body_block) - if bound: - xor_ret = body_block.append_instruction("xor", counter_var, bound) - body_block.append_instruction("assert", xor_ret) emit_body_blocks() body_end = fn.get_basic_block() From 74075295660cb0be38d2d8ffb3e8aefdc596b135 Mon Sep 17 00:00:00 2001 From: Charles Cooper Date: Fri, 7 Jun 2024 22:10:30 -0400 Subject: [PATCH 2/3] fix argument order --- vyper/venom/ir_node_to_venom.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/vyper/venom/ir_node_to_venom.py b/vyper/venom/ir_node_to_venom.py index a12fb06b25..2c99cf5668 100644 --- a/vyper/venom/ir_node_to_venom.py +++ b/vyper/venom/ir_node_to_venom.py @@ -468,14 +468,7 @@ def emit_body_blocks(): start, end, _ = _convert_ir_bb_list(fn, ir.args[1:4], symbols) assert ir.args[3].is_literal, "repeat bound expected to be literal" - bound = ir.args[3].value - if ( - isinstance(end, IRLiteral) - and isinstance(start, IRLiteral) - and end.value + start.value <= bound - ): - bound = None body = ir.args[4] @@ -491,11 +484,14 @@ def emit_body_blocks(): counter_var = entry_block.append_instruction("store", start) symbols[sym.value] = counter_var + + if bound is not None: + # assert le end bound + invalid_end = entry_block.append_instruction("gt", bound, end) + valid_end = entry_block.append_instruction("iszero", invalid_end) + entry_block.append_instruction("assert", valid_end) + end = entry_block.append_instruction("add", start, end) - if bound: - bound = entry_block.append_instruction("add", start, bound) - xor_ret = entry_block.append_instruction("xor", counter_var, bound) - entry_block.append_instruction("assert", xor_ret) entry_block.append_instruction("jmp", cond_block.label) From 6cfbfb9eb2cf79bbfca954261c217e08adfb632c Mon Sep 17 00:00:00 2001 From: Charles Cooper Date: Sat, 8 Jun 2024 02:11:56 +0000 Subject: [PATCH 3/3] fix invalid syntax tests --- tests/functional/syntax/test_for_range.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/functional/syntax/test_for_range.py b/tests/functional/syntax/test_for_range.py index 1de32108c5..97e77f32f7 100644 --- a/tests/functional/syntax/test_for_range.py +++ b/tests/functional/syntax/test_for_range.py @@ -368,14 +368,14 @@ def foo(): """ @external def foo(): - x: int128 = 5 + x: int128 = 4 for i: int128 in range(x, bound=4): pass """, """ @external def foo(): - x: int128 = 5 + x: int128 = 4 for i: int128 in range(0, x, bound=4): pass """,