Skip to content

Commit

Permalink
fix: prevent internal errors when for-loop bodies always exit (#269)
Browse files Browse the repository at this point in the history
---------

- build tuple iteration in an order such that special case of not checking reachability is no longer required
- simplify tuple, indexable, and urange looping code
- improve tuple iteration code gen
- test all combinations of supported iteration types
  • Loading branch information
achidlow authored Jul 16, 2024
1 parent 5c4da43 commit 1b24cd7
Show file tree
Hide file tree
Showing 202 changed files with 23,141 additions and 14,678 deletions.
18 changes: 10 additions & 8 deletions examples/sizes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@
arc4_types/Arc4BoolType 412 76 336 76 0
arc4_types/Arc4DynamicBytes 389 190 199 190 0
arc4_types/Arc4DynamicStringArray 285 112 173 112 0
arc4_types/Arc4MutableParams 511 303 208 295 8
arc4_types/Arc4MutableParams 501 295 206 292 3
arc4_types/Arc4Mutation 3260 1464 1796 1464 0
arc4_types/Arc4NumericTypes 752 201 551 201 0
arc4_types/Arc4RefTypes 94 47 47 47 0
arc4_types/Arc4StringTypes 469 35 434 35 0
arc4_types/Arc4StructsFromAnotherModule 73 12 61 12 0
arc4_types/Arc4StructsType 323 247 76 247 0
arc4_types/Arc4StructsType 318 239 79 239 0
arc4_types/Arc4TuplesType 886 138 748 138 0
arc_28/EventEmitter 167 121 46 121 0
asset/Reference 269 261 8 261 0
auction/Auction 573 522 51 522 0
augmented_assignment/Augmented 159 156 3 156 0
avm_types_in_abi/Test 412 336 76 336 0
biguint_binary_ops/BiguintBinaryOps 154 8 146 8 0
boolean_binary_ops/BooleanBinaryOps 361 305 56 305 0
boolean_binary_ops/BooleanBinaryOps 350 292 58 292 0
box_storage/Box 1813 1442 371 1442 0
bytes_ops/BiguintBinaryOps 139 139 0 139 0
calculator 349 317 32 315 2
Expand All @@ -40,7 +40,7 @@
control_op_simplification 48 44 4 38 6
dup2_optimization_bug 26 22 4 22 0
edverify/Verify 43 37 6 37 0
enumeration/Enumeration 562 501 61 501 0
enumeration/Enumeration 547 483 64 483 0
everything 498 448 50 448 0
global_state/AppState 479 464 15 464 0
hello_world/HelloWorld 24 22 2 22 0
Expand All @@ -57,6 +57,9 @@
inner_transactions/itxn_loop 192 184 8 184 0
intrinsics/ImmediateVariants 166 162 4 162 0
intrinsics/Overloaded 68 48 20 48 0
iteration/IndexableIterationTest 772 711 61 711 0
iteration/TupleIterationTest 521 453 68 453 0
iteration/URangeIterationTest 1097 598 499 598 0
koopman 16 9 7 9 0
less_simple 173 148 25 148 0
literals/LiteralFolding 318 149 169 149 0
Expand All @@ -71,7 +74,6 @@
nested_loops/Nested 239 201 38 201 0
regression_118 148 100 48 100 0
reinterpret_cast 127 109 18 109 0
reversed_iteration 833 547 286 547 0
scratch_slots 71 67 4 67 0
scratch_slots/2 71 67 4 67 0
scratch_slots/MyOther 8 8 0 8 0
Expand All @@ -91,12 +93,12 @@
template_variables/TemplateVariables 168 155 13 155 0
tictactoe/TicTacToe 835 682 153 670 12
too_many_permutations 112 106 6 106 0
transaction/Transaction 996 932 64 932 0
transaction/Transaction 973 906 67 906 0
tuple_support/TupleComparisons 136 75 61 75 0
tuple_support/TupleSupport 651 413 238 413 0
tuple_support/TupleSupport 643 404 239 404 0
typed_abi_call/Greeter 2861 2446 415 2446 0
typed_abi_call/Logger 896 762 134 762 0
unary/Unary 136 96 40 96 0
unary/Unary 128 81 47 81 0
unassigned_expression/Unassigned 133 115 18 115 0
undefined_phi_args/Baddie 324 284 40 284 0
unssa/UnSSA 452 369 83 369 0
Expand Down
4 changes: 2 additions & 2 deletions examples/voting/puya.log

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions src/puya/ir/_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from collections import deque
from collections.abc import Iterator

from puya.ir import models


def bfs_block_order(start: models.BasicBlock) -> Iterator[models.BasicBlock]:
q = deque[models.BasicBlock]()
q.append(start)
visited = {start}
while q:
block = q.popleft()
yield block
for succ in block.successors:
if succ not in visited:
q.append(succ)
visited.add(succ)
11 changes: 9 additions & 2 deletions src/puya/ir/builder/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ def validate_block_predecessors(self) -> None:
non_entry_block.source_location,
)

def activate_block(self, block: BasicBlock, *, ignore_predecessor_check: bool = False) -> None:
def activate_block(self, block: BasicBlock) -> None:
"""Add a basic block and make it the active one (target of adds)"""
if not self.active_block.terminated:
raise InternalError(
"Attempted to activate a new block when current block has not been terminated"
)
if not block.predecessors and not ignore_predecessor_check:
if not block.predecessors:
raise InternalError("Attempted to add a (non-entry) block with no predecessors")
block.id = len(self._blocks)
self._blocks.append(block)
Expand All @@ -128,6 +128,13 @@ def goto_and_activate(self, block: BasicBlock) -> None:
self.goto(block)
self.activate_block(block)

def try_goto_and_activate(self, block: BasicBlock) -> bool:
self.goto(block)
if block.predecessors:
self.activate_block(block)
return True
return False

def maybe_add_implicit_subroutine_return(self, params: Sequence[Parameter]) -> None:
if not self._blocks[-1].terminated:
self.terminate(
Expand Down
Loading

0 comments on commit 1b24cd7

Please sign in to comment.