Skip to content

Commit

Permalink
refactor: Use BoolConstant for constant arc4 boolean values
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmenzel committed Oct 30, 2024
1 parent ec84af6 commit 82c3de3
Show file tree
Hide file tree
Showing 16 changed files with 59 additions and 58 deletions.
34 changes: 17 additions & 17 deletions examples/sizes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,38 @@
abi_routing/Reference 1440 1216 - | 794 623 -
amm/ConstantProductAMM 1225 1106 - | 715 645 -
application/Reference 177 167 - | 92 83 -
arc4_dynamic_arrays/DynamicArray 2695 1931 - | 1733 1138 -
arc4_dynamic_arrays/DynamicArray 2674 1931 - | 1718 1138 -
arc4_numeric_comparisons/UIntNOrdering 1100 908 - | 786 597 -
arc4_types/Arc4Address 85 62 - | 37 18 -
arc4_types/Arc4Arrays 623 376 - | 368 182 -
arc4_types/Arc4BoolEval 751 14 - | 167 8 -
arc4_types/Arc4Arrays 584 376 - | 337 182 -
arc4_types/Arc4BoolEval 733 14 - | 154 8 -
arc4_types/Arc4BoolType 381 69 - | 307 46 -
arc4_types/Arc4DynamicBytes 377 185 - | 213 100 -
arc4_types/Arc4DynamicStringArray 283 124 - | 172 53 -
arc4_types/Arc4MutableParams 471 286 - | 292 141 -
arc4_types/Arc4Mutation 2958 1426 - | 1977 593 -
arc4_types/Arc4DynamicStringArray 263 124 - | 152 53 -
arc4_types/Arc4MutableParams 431 286 - | 262 141 -
arc4_types/Arc4Mutation 2782 1426 - | 1867 593 -
arc4_types/Arc4NumericTypes 749 186 - | 243 26 -
arc4_types/Arc4RefTypes 85 46 - | 32 27 -
arc4_types/Arc4StringTypes 455 35 - | 245 13 -
arc4_types/Arc4StringTypes 349 35 - | 149 13 -
arc4_types/Arc4StructsFromAnotherModule 67 12 - | 49 6 -
arc4_types/Arc4StructsType 302 239 - | 204 121 -
arc4_types/Arc4TuplesType 795 136 - | 537 58 -
arc4_types/Arc4TuplesType 781 136 - | 524 58 -
arc_28/EventEmitter 186 133 - | 100 64 -
asset/Reference 268 261 - | 144 141 -
auction/Auction 592 522 - | 328 281 -
augmented_assignment/Augmented 151 156 - | 77 78 -
avm_types_in_abi/Test 386 317 - | 237 178 -
biguint_binary_ops/BiguintBinaryOps 186 77 - | 100 20 -
boolean_binary_ops/BooleanBinaryOps 1124 471 - | 680 258 -
box_storage/Box 1765 1423 - | 1154 873 -
box_storage/Box 1749 1423 - | 1137 873 -
bug_load_store_load_store 78 72 - | 39 35 -
bytes_ops/BiguintBinaryOps 139 - - | 82 - -
calculator 334 317 315 | 182 168 166
callsub 31 31 - | 18 17 -
chained_assignment/ChainedAssignment 87 87 - | 30 29 -
compile/Hello 217 181 - | 107 85 -
compile/HelloBase 198 156 - | 98 75 -
compile/HelloFactory 7401 6824 - | 1115 842 -
compile/HelloFactory 7284 6824 - | 1025 842 -
compile/HelloOtherConstants 306 281 - | 137 121 -
compile/HelloPrfx 211 168 - | 102 77 -
compile/HelloTmpl 211 168 - | 102 77 -
Expand All @@ -46,7 +46,7 @@
conditional_expressions/Literals 84 49 - | 61 30 -
constants/AddressConstant 65 47 - | 23 9 -
constants/ByteConstants 92 76 - | 23 9 -
contains 1682 1623 - | 849 787 -
contains 1680 1623 - | 839 787 -
control_op_simplification 48 44 37 | 23 18 15
debug/Debug 470 428 - | 261 236 -
diamond_mro/Base1 176 156 - | 64 52 -
Expand All @@ -56,7 +56,7 @@
dup2_optimization_bug 25 22 - | 16 14 -
edverify/Verify 39 26 - | 21 14 -
enumeration/Enumeration 513 476 - | 252 232 -
everything 524 459 - | 247 205 -
everything 505 459 - | 232 205 -
global_state/AppState 467 463 - | 194 190 -
group_side_effects/AppCall 106 82 - | 55 42 -
group_side_effects/AppExpectingEffects 263 215 - | 155 125 -
Expand Down Expand Up @@ -93,7 +93,7 @@
module_consts 52 50 - | 12 11 -
named_tuples/NamedTuples 397 328 - | 225 171 -
nested_loops/Nested 213 200 - | 132 120 -
regression_tests/Issue118 167 111 - | 95 57 -
regression_tests/Issue118 152 111 - | 83 57 -
regression_tests/Issue194 34 22 - | 22 11 -
reinterpret_cast 151 119 - | 78 58 -
scratch_slots 94 84 - | 56 43 -
Expand All @@ -103,7 +103,7 @@
simplish/Simplish 742 714 715 | 254 231 230
ssa 267 225 - | 158 129 -
ssa2 92 84 - | 53 43 -
state_mutations 1165 848 - | 754 528 -
state_mutations 1124 848 - | 724 528 -
state_proxies/StateProxy 129 117 - | 59 51 -
state_totals 65 32 - | 32 16 -
stress_tests/BruteForceRotationSearch 228 163 - | 152 106 -
Expand All @@ -120,8 +120,8 @@
tuple_support/NestedTuples 1259 900 897 | 784 510 509
tuple_support/TupleComparisons 124 67 - | 81 35 -
tuple_support/TupleSupport 667 405 - | 375 175 -
typed_abi_call/Greeter 5040 3977 3968 | 2727 1832 1829
typed_abi_call/Logger 1405 1144 1141 | 822 603 602
typed_abi_call/Greeter 4787 3977 3968 | 2510 1832 1829
typed_abi_call/Logger 1402 1144 1141 | 812 603 602
typed_abi_call_txn/Caller 580 514 - | 306 263 -
typed_abi_call_txn/Txn 310 248 - | 172 134 -
unary/Unary 126 67 - | 62 28 -
Expand All @@ -130,4 +130,4 @@
unssa/UnSSA 432 368 - | 241 204 -
voting/VotingRoundApp 1593 1483 - | 734 649 -
with_reentrancy/WithReentrancy 255 242 - | 132 122 -
Total 69200 53576 53517 | 32843 21764 21720
Total 68300 53576 53517 | 32114 21764 21720
5 changes: 4 additions & 1 deletion src/puya/awst/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,11 @@ def BigUIntConstant( # noqa: N802

@attrs.frozen
class BoolConstant(Expression):
wtype: WType = attrs.field(default=wtypes.bool_wtype, init=False)
value: bool
wtype: WType = attrs.field(
default=wtypes.bool_wtype,
validator=wtype_is_one_of(wtypes.bool_wtype, wtypes.arc4_bool_wtype),
)

def accept(self, visitor: ExpressionVisitor[T]) -> T:
return visitor.visit_bool_constant(self)
Expand Down
21 changes: 18 additions & 3 deletions src/puya/ir/builder/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,24 @@ def visit_decimal_constant(self, expr: awst_nodes.DecimalConstant) -> TExpressio
)

def visit_bool_constant(self, expr: awst_nodes.BoolConstant) -> TExpression:
return UInt64Constant(
value=int(expr.value), ir_type=IRType.bool, source_location=expr.source_location
)
match expr.wtype:
case wtypes.bool_wtype:
return UInt64Constant(
value=int(expr.value),
ir_type=IRType.bool,
source_location=expr.source_location,
)
case wtypes.arc4_bool_wtype:
return BytesConstant(
value=(128 if expr.value else 0).to_bytes(1),
encoding=AVMBytesEncoding.base16,
ir_type=IRType.bytes,
source_location=expr.source_location,
)
case _:
raise InternalError(
f"Unexpected wtype {expr.wtype} for BoolConstant", expr.source_location
)

def visit_bytes_constant(self, expr: awst_nodes.BytesConstant) -> BytesConstant:
if len(expr.value) > algo_constants.MAX_BYTES_LENGTH:
Expand Down
19 changes: 10 additions & 9 deletions src/puyapy/awst_build/eb/arc4/bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ def try_convert_literal(
match literal.value:
case bool(bool_literal):
return ARC4BoolExpressionBuilder(
ARC4Encode(
value=BoolConstant(value=bool_literal, source_location=location),
wtype=wtypes.arc4_bool_wtype,
source_location=location,
)
BoolConstant(
value=bool_literal, source_location=location, wtype=wtypes.arc4_bool_wtype
),
)
return None

Expand All @@ -54,13 +52,16 @@ def call(
arg = expect.at_most_one_arg(args, location)
match arg:
case None:
native_bool: Expression = BoolConstant(value=False, source_location=location)
expr: Expression = BoolConstant(
value=False, source_location=location, wtype=wtypes.arc4_bool_wtype
)
case _:
arg = expect.argument_of_type_else_dummy(arg, pytypes.BoolType)
native_bool = arg.resolve()
return ARC4BoolExpressionBuilder(
ARC4Encode(value=native_bool, wtype=wtypes.arc4_bool_wtype, source_location=location)
)
expr = ARC4Encode(
value=native_bool, wtype=wtypes.arc4_bool_wtype, source_location=location
)
return ARC4BoolExpressionBuilder(expr)


class ARC4BoolExpressionBuilder(
Expand Down
3 changes: 1 addition & 2 deletions test_cases/arc4_types/out/Arc4BoolEvalContract.ssa.ir
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ contract test_cases.arc4_types.bool_eval.Arc4BoolEvalContract:
let tmp%33#0: bool = (!= dynamic_arr#1 0x0000)
(assert tmp%33#0)
let encoded_bool%2#0: bytes = (setbit 0x00 0u 0u)
let encoded_bool%3#0: bytes = (setbit 0x00 0u 0u)
let tmp%34#0: bool = (== encoded_bool%2#0 encoded_bool%3#0)
let tmp%34#0: bool = (== 0x00 encoded_bool%2#0)
(assert tmp%34#0)
return 1u

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ contract test_cases.arc4_types.bool_eval.Arc4BoolEvalContract:
let tmp%33#0: bool = (!= dynamic_arr#1 0x0000)
(assert tmp%33#0)
let encoded_bool%2#0: bytes = 0x00
let encoded_bool%3#0: bytes = 0x00
let tmp%34#0: bool = (== encoded_bool%2#0 encoded_bool%3#0)
let tmp%34#0: bool = (== 0x00 encoded_bool%2#0)
(assert tmp%34#0)
return 1u

Expand Down
2 changes: 1 addition & 1 deletion test_cases/arc4_types/out/module.awst
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ contract Arc4BoolEvalContract
assert(dynamic_arr == hex<"0000">)
dynamic_arr.extend((0_arc4u64))
assert(dynamic_arr != hex<"0000">)
assert(arc4_encode(false, arc4.bool) == arc4_encode(false, arc4.bool))
assert(false == arc4_encode(false, arc4.bool))
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,6 @@ test_cases.arc4_types.bool_eval.Arc4BoolEvalContract.approval_program:
dup
setbit
bytec_0 // 0x00
intc_0 // 0
dup
setbit
==
assert
// arc4_types/bool_eval.py:58
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ contract test_cases.arc4_types.bool_eval.Arc4BoolEvalContract:
let tmp%33#0: bool = (!= dynamic_arr#0 0x0000)
(assert tmp%33#0)
let encoded_bool%2#0: bytes = (setbit 0x00 0u 0u)
let encoded_bool%3#0: bytes = (setbit 0x00 0u 0u)
let tmp%34#0: bool = (== encoded_bool%2#0 encoded_bool%3#0)
let tmp%34#0: bool = (== 0x00 encoded_bool%2#0)
(assert tmp%34#0)
return 1u

Expand Down
2 changes: 0 additions & 2 deletions test_cases/arc4_types/puya.log
Original file line number Diff line number Diff line change
Expand Up @@ -14736,7 +14736,6 @@ debug: Simplified (!= 0x00000000000000000000000000000000000000000000000000000000
debug: Simplified (concat 0x0000 0x) to 0x0000
debug: Simplified (concat 0x 0x0000000000000000) to 0x0000000000000000
debug: Simplified (setbit 0x00 0u 0u) to 0x00
debug: Simplified (setbit 0x00 0u 0u) to 0x00
debug: Optimizer: Remove Unused Variables
debug: Removing unused variable tmp%4#0
debug: Removing unused variable tmp%5#0
Expand Down Expand Up @@ -14804,7 +14803,6 @@ debug: Removing unused variable tmp%31#0
debug: Removing unused variable dynamic_arr#0
debug: Removing unused variable data%0#0
debug: Removing unused variable encoded_bool%2#0
debug: Removing unused variable encoded_bool%3#0
debug: Optimizer: Inner Txn Field Replacer
debug: Optimizer: Replace Compiled References
debug: Optimizer: Simplify Control Ops
Expand Down
3 changes: 1 addition & 2 deletions test_cases/typed_abi_call/out/Greeter.ssa.ir
Original file line number Diff line number Diff line change
Expand Up @@ -1973,9 +1973,8 @@ contract test_cases.typed_abi_call.typed_c2c.Greeter:
let inner_txn_params%4%%Fee_length#0: uint64 = 1u
let inner_txn_params%4%%param_TypeEnum_idx_0#0: uint64 = appl
let inner_txn_params%4%%TypeEnum_length#0: uint64 = 1u
let encoded_bool%0#0: bytes = (setbit 0x00 0u 1u)
let inner_txn_params%4%%param_ApplicationArgs_idx_0#0: bytes = method "log(bool)void"
let inner_txn_params%4%%param_ApplicationArgs_idx_1#0: bytes = encoded_bool%0#0
let inner_txn_params%4%%param_ApplicationArgs_idx_1#0: bytes = 0x80
let inner_txn_params%4%%ApplicationArgs_length#0: uint64 = 2u
let inner_txn_params%4%%param_ApplicationID_idx_0#0: uint64 = app#0
let inner_txn_params%4%%ApplicationID_length#0: uint64 = 1u
Expand Down
3 changes: 1 addition & 2 deletions test_cases/typed_abi_call/out/Greeter.ssa.opt_pass_1.ir
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,9 @@ contract test_cases.typed_abi_call.typed_c2c.Greeter:
let tmp%7#0: bool = (== txn.LastLog#3 0x34)
(assert tmp%7#0)
itxn_begin
let encoded_bool%0#0: bytes = 0x80
((itxn_field ApplicationID) app#0)
((itxn_field ApplicationArgs) method "log(bool)void")
((itxn_field ApplicationArgs) encoded_bool%0#0)
((itxn_field ApplicationArgs) 0x80)
((itxn_field TypeEnum) appl)
((itxn_field Fee) 0u)
itxn_submit
Expand Down
2 changes: 1 addition & 1 deletion test_cases/typed_abi_call/out/module.awst
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ contract Greeter
assert(txn.LastLog == bzero(56u) + itob(3u))
txn: inner_transaction_appl = submit_txn(create_inner_transaction(Fee=0u, TypeEnum=appl, ApplicationArgs=(Method("log(byte[])void"), arc4_encode(hex<"34">, arc4.dynamic_array<arc4.uint8>)), ApplicationID=app))
assert(txn.LastLog == hex<"34">)
txn: inner_transaction_appl = submit_txn(create_inner_transaction(Fee=0u, TypeEnum=appl, ApplicationArgs=(Method("log(bool)void"), arc4_encode(true, arc4.bool)), ApplicationID=app))
txn: inner_transaction_appl = submit_txn(create_inner_transaction(Fee=0u, TypeEnum=appl, ApplicationArgs=(Method("log(bool)void"), true), ApplicationID=app))
assert(txn.LastLog == hex<"54727565">)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -914,10 +914,6 @@ test_arg_conversion:
// typed_abi_call/typed_c2c.py:69
// txn = arc4.abi_call(Logger.log_bool, True, app_id=app)
itxn_begin
pushbytes 0x00
intc_0 // 0
intc_3 // 1
setbit
// typed_abi_call/typed_c2c.py:56
// def test_arg_conversion(self, app: Application) -> None:
frame_dig -1
Expand All @@ -926,6 +922,7 @@ test_arg_conversion:
// txn = arc4.abi_call(Logger.log_bool, True, app_id=app)
pushbytes 0x6eed7ec3 // method "log(bool)void"
itxn_field ApplicationArgs
pushbytes 0x80
itxn_field ApplicationArgs
intc_1 // appl
itxn_field TypeEnum
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,12 +499,10 @@ contract test_cases.typed_abi_call.typed_c2c.Greeter:
let tmp%7#0: bool = (== txn.LastLog#0 0x34)
(assert tmp%7#0)
itxn_begin
let encoded_bool%0#0: bytes = (setbit 0x00 0u 1u)
let inner_txn_params%4%%param_ApplicationArgs_idx_1#0: bytes = encoded_bool%0#0
let inner_txn_params%4%%param_ApplicationID_idx_0#0: uint64 = app#0
((itxn_field ApplicationID) inner_txn_params%4%%param_ApplicationID_idx_0#0)
((itxn_field ApplicationArgs) method "log(bool)void")
((itxn_field ApplicationArgs) inner_txn_params%4%%param_ApplicationArgs_idx_1#0)
((itxn_field ApplicationArgs) 0x80)
((itxn_field TypeEnum) appl)
((itxn_field Fee) 0u)
goto block@5
Expand Down
Loading

0 comments on commit 82c3de3

Please sign in to comment.