Skip to content

Commit 78aa952

Browse files
xunilrjJoshuaBatty
andauthored
Trivially encoded types (#7488)
## Description This PR will slice #7419 into multiple PRS, being the first one. To start optimising encoding/decoding we will introduce the concept of "trivially encoded/decoded types". A type is trivially encoded or decoded if the encoding memory representation is the same as the runtime memory representation. PR #7419 introduced an intrinsic that would return a `bool` if both representations match. For this PR I decided to expose these memory representations directly. For that we have two new intrinsics: `__runtime_mem_id` and `__encoding_mem_id`. I am not 100% sure if they are going to be useful in other scenarios, but given that the optimiser is able to optimise them away, I think exposing them like this is better than what #7419 does. These `ids` are opaque numbers that do not convey anything about the type or their representation. They can only be compared for equality, and when a type does not have an encoding representation the intrinsic returns zero. With this in mind, the `encode` function now checks if a type is "trivially encoded" or not. If it is, we just allocate and memcopy its bytes. As this function should not return an aliased pointer. On top of that, we also have an `encode_and_return` function that avoids creating tuples and other stuff, given that the optimiser still generates some `mem_copy`s in these cases. A simple example of the benefit is the test `main_args_empty`, where the binary size went from 96 bytes to 64 bytes. Before ``` script { pub entry fn __entry() -> __ptr slice, !3 { local mut slice __aggr_memcpy_0 local { u64, u64 } __anon_0 local slice __ret_value entry(): v0 = get_local __ptr slice, __ret_value v1 = call main_0(), !6 v2 = get_local __ptr { u64, u64 }, __anon_0, !7 v3 = const u64 0 v4 = get_elem_ptr v2, __ptr u64, v3, !7 v5 = const u64 0, !8 store v5 to v4, !7 v6 = const u64 1 v7 = get_elem_ptr v2, __ptr u64, v6, !7 v8 = const u64 0, !9 store v8 to v7, !7 v9 = asm(s: v2) -> __ptr slice s { } v10 = get_local __ptr slice, __aggr_memcpy_0 mem_copy_val v10, v9 mem_copy_val v0, v10 ret __ptr slice v0 } entry_orig fn main_0() -> (), !13 { entry(): v0 = const unit () ret () v0 } } ;; ASM: Final program ;; Program kind: Script .program: move $$tmp $pc jmpf $zero i4 DATA_SECTION_OFFSET[0..32] DATA_SECTION_OFFSET[32..64] CONFIGURABLES_OFFSET[0..32] CONFIGURABLES_OFFSET[32..64] lw $$ds $$tmp i1 add $$ds $$ds $$tmp cfei i0 ; allocate stack space for globals move $$locbase $sp ; save locals base register for function __entry cfei i48 ; allocate 48 bytes for locals and 0 slots for call arguments addi $r0 $$locbase i32 ; get offset to local __ptr slice jal $$reta $pc i9 ; [call]: call main_0 addi $r1 $$locbase i16 ; get offset to local __ptr { u64, u64 } sw $$locbase $zero i2 ; store word sw $$locbase $zero i3 ; store word mcpi $$locbase $r1 i16 ; copy memory mcpi $r0 $$locbase i16 ; copy memory lw $r1 $r0 i1 ; load size of returned slice lw $r0 $r0 i0 ; load pointer to returned slice retd $r0 $r1 pshh i524288 ; save registers 40..64 move $$locbase $sp ; save locals base register for function main_0 poph i524288 ; restore registers 40..64 jal $zero $$reta i0 ; return from call .data: ``` After ``` script { pub entry fn __entry() -> __ptr never, !3 { entry(): v0 = call main_0(), !6 v1 = const u64 0, !7 v2 = const u64 0, !8 retd v1 v2, !9 } entry_orig fn main_0() -> (), !13 { entry(): v0 = const unit () ret () v0 } } ;; ASM: Final program ;; Program kind: Script .program: move $$tmp $pc jmpf $zero i4 DATA_SECTION_OFFSET[0..32] DATA_SECTION_OFFSET[32..64] CONFIGURABLES_OFFSET[0..32] CONFIGURABLES_OFFSET[32..64] lw $$ds $$tmp i1 add $$ds $$ds $$tmp cfei i0 ; allocate stack space for globals move $$locbase $sp ; save locals base register for function __entry jal $$reta $pc i2 ; [call]: call main_0 retd $zero $zero pshh i524288 ; save registers 40..64 move $$locbase $sp ; save locals base register for function main_0 poph i524288 ; restore registers 40..64 jal $zero $$reta i0 ; return from call .data: ``` Next PR will introduce this concept into the `AbiEncode` trait, allowing the type itself to control if it wants to be trivially encoded or not. # Missing optimizations If we look at the test `main_args_various_types`, after the `main` is called we see that IR is still not optimal. ``` v37 = get_local __ptr { u64 }, __ret_val1 v38 = call main_19(v36, v37) v39 = get_local __ptr { u64 }, _result, !58 <- not needed mem_copy_val v39, v37 <- not needed v40 = get_local __ptr { u64 }, _result, !59 <- not needed v41 = get_local __ptr { u64 }, item_, !62 <- not needed mem_copy_val v41, v40 <- not needed v42 = get_local __ptr { u64 }, item_, !64 <- not needed v43 = const u64 8 retd v42 v43, !66 ``` Ideally, IR would be ``` v37 = get_local __ptr { u64 }, __ret_val1 v38 = call main_19(v36, v37) v39 = const u64 8 retd v37 v39, !66 ``` ## Gas Usage Diff | Test | Before | After | Percentage | | ---- | -----: | ----: | ---------: | | should_fail/arith_overflow/u16_add_overflow (test.toml) | 44 | 43 | 2.27% | | should_fail/arith_overflow/u16_mul_overflow (test.toml) | 45 | 44 | 2.22% | | should_fail/arith_overflow/u16_sub_underflow (test.toml) | 23 | 22 | 4.35% | | should_fail/arith_overflow/u32_add_overflow (test.toml) | 44 | 43 | 2.27% | | should_fail/arith_overflow/u32_mul_overflow (test.toml) | 45 | 44 | 2.22% | | should_fail/arith_overflow/u32_sub_underflow (test.toml) | 23 | 22 | 4.35% | | should_fail/arith_overflow/u64_add_overflow (test.toml) | 17 | 15 | 11.76% | | should_fail/arith_overflow/u64_mul_overflow (test.toml) | 18 | 16 | 11.11% | | should_fail/arith_overflow/u64_sub_underflow (test.toml) | 16 | 14 | 12.50% | | should_fail/arith_overflow/u8_add_overflow (test.toml) | 38 | 36 | 5.26% | | should_fail/arith_overflow/u8_mul_overflow (test.toml) | 39 | 37 | 5.13% | | should_fail/arith_overflow/u8_sub_underflow (test.toml) | 17 | 15 | 11.76% | | should_fail/vec_set_index_out_of_bounds (test.toml) | 182 | 179 | 1.65% | | should_fail/vec_swap_param1_out_of_bounds (test.toml) | 237 | 234 | 1.27% | | should_fail/vec_swap_param2_out_of_bounds (test.toml) | 241 | 238 | 1.24% | | should_pass/blanket_impl (test.toml) | 142 | 51 | 64.08% | | should_pass/blanket_impl_u16 (test.toml) | 142 | 51 | 64.08% | | should_pass/break_in_non_statement_positions (test.toml) | 142 | 51 | 64.08% | | should_pass/conditional_compilation/run (test.toml) | 142 | 51 | 64.08% | | should_pass/continue_in_non_statement_positions (test.toml) | 1226 | 1135 | 7.42% | | should_pass/empty_fields_in_storage_struct (test.toml)::test_read_write_bytes | 11204 | 10275 | 8.29% | | should_pass/empty_fields_in_storage_struct (test.toml)::test_read_write_map | 7865 | 7107 | 9.64% | | should_pass/empty_fields_in_storage_struct (test.toml)::test_read_write_vec | 11474 | 10125 | 11.76% | | should_pass/forc/dependency_package_field (test.toml) | 137 | 46 | 66.42% | | should_pass/language/abort_control_flow_good (test.toml) | 29 | 27 | 6.90% | | should_pass/language/addrof_intrinsic (test.toml) | 609 | 595 | 2.30% | | should_pass/language/aliased_imports (test.toml) | 144 | 53 | 63.19% | | should_pass/language/args_on_stack (test.toml) | 5037 | 4946 | 1.81% | | should_pass/language/array/array_basics (test.toml) | 478 | 387 | 19.04% | | should_pass/language/array/array_generics (test.toml) | 146 | 55 | 62.33% | | should_pass/language/asm_expr_basic (test.toml) | 163 | 135 | 17.18% | | should_pass/language/associated_const_abi (test.toml)::test | 4546 | 4096 | 9.90% | | should_pass/language/associated_const_abi_multiple (test.toml)::test | 1510 | 1360 | 9.93% | | should_pass/language/associated_const_impl (test.toml) | 143 | 115 | 19.58% | | should_pass/language/associated_const_impl_local_same_name (test.toml) | 143 | 115 | 19.58% | | should_pass/language/associated_const_impl_self (test.toml) | 166 | 138 | 16.87% | | should_pass/language/associated_const_in_decls_of_other_constants (test.toml)::test | 529 | 452 | 14.56% | | should_pass/language/associated_const_trait_impl_method (test.toml) | 143 | 115 | 19.58% | | should_pass/language/associated_const_trait_method (test.toml) | 143 | 115 | 19.58% | | should_pass/language/associated_type_and_associated_const (test.toml) | 143 | 115 | 19.58% | | should_pass/language/associated_type_ascription (test.toml) | 143 | 115 | 19.58% | | should_pass/language/associated_type_container (test.toml) | 602 | 560 | 6.98% | | should_pass/language/associated_type_container_in_library (test.toml) | 602 | 560 | 6.98% | | should_pass/language/associated_type_fully_qualified (test.toml) | 195 | 152 | 22.05% | | should_pass/language/associated_type_iterator (test.toml) | 620 | 578 | 6.77% | | should_pass/language/associated_type_method (test.toml) | 143 | 115 | 19.58% | | should_pass/language/associated_type_parameter (test.toml) | 143 | 115 | 19.58% | | should_pass/language/b256_bad_jumps (test.toml) | 137 | 46 | 66.42% | | should_pass/language/b256_bitwise_ops (test.toml) | 1503 | 1412 | 6.05% | | should_pass/language/b256_ops (test.toml) | 1314 | 1223 | 6.93% | | should_pass/language/basic_func_decl (test.toml) | 136 | 45 | 66.91% | | should_pass/language/binary_and_hex_literals (test.toml) | 136 | 45 | 66.91% | | should_pass/language/binop_intrinsics (test.toml) | 142 | 51 | 64.08% | | should_pass/language/bitwise_not (test.toml) | 136 | 45 | 66.91% | | should_pass/language/blanket_trait (test.toml) | 136 | 45 | 66.91% | | should_pass/language/bool_and_or (test.toml) | 142 | 51 | 64.08% | | should_pass/language/break_and_continue (test.toml) | 1098 | 1007 | 8.29% | | should_pass/language/builtin_type_method_call (test.toml) | 142 | 51 | 64.08% | | should_pass/language/chained_if_let (test.toml) | 166 | 75 | 54.82% | | should_pass/language/complex_ir_cfg (test.toml) | 158 | 142 | 10.13% | | should_pass/language/configurable_consts (test.toml) | 2520 | 2506 | 0.56% | | should_pass/language/configurable_tests (test.toml)::t | 2805 | 2553 | 8.98% | | should_pass/language/const_decl_and_use_in_library (test.toml) | 142 | 51 | 64.08% | | should_pass/language/const_decl_in_library (test.toml) | 148 | 57 | 61.49% | | should_pass/language/const_generics (test.toml) | 318 | 305 | 4.09% | | should_pass/language/const_inits (test.toml) | 481 | 390 | 18.92% | | should_pass/language/contract_caller_dynamic_address (test.toml) | 379 | 301 | 20.58% | | should_pass/language/contract_ret_intrinsic (test.toml)::test | 1464 | 1314 | 10.25% | | should_pass/language/dereferenced_projection_reassignment (test.toml) | 238 | 124 | 47.90% | | should_pass/language/diverging_exprs (test.toml) | 291 | 200 | 31.27% | | should_pass/language/dummy_method_issue (test.toml) | 134 | 120 | 10.45% | | should_pass/language/empty_method_initializer (test.toml) | 242 | 151 | 37.60% | | should_pass/language/enum_destructuring (test.toml) | 156 | 65 | 58.33% | | should_pass/language/enum_if_let (test.toml) | 221 | 130 | 41.18% | | should_pass/language/enum_if_let_large_type (test.toml) | 201 | 110 | 45.27% | | should_pass/language/enum_in_fn_decl (test.toml) | 157 | 66 | 57.96% | | should_pass/language/enum_init_fn_call (test.toml) | 185 | 94 | 49.19% | | should_pass/language/enum_instantiation (test.toml) | 660 | 569 | 13.79% | | should_pass/language/enum_padding (test.toml) | 518 | 487 | 5.98% | | should_pass/language/enum_type_inference (test.toml) | 142 | 51 | 64.08% | | should_pass/language/enum_variant_imports (test.toml) | 161 | 70 | 56.52% | | should_pass/language/eq_and_neq (test.toml) | 675 | 584 | 13.48% | | should_pass/language/eq_intrinsic (test.toml) | 142 | 51 | 64.08% | | should_pass/language/far_jumps/many_blobs (test.toml) | 3670216 | 3670125 | 0.00% | | should_pass/language/far_jumps/single_blob (test.toml) | 142 | 51 | 64.08% | | should_pass/language/for_loops (test.toml) | 7882 | 7791 | 1.15% | | should_pass/language/funcs_with_generic_types (test.toml) | 136 | 45 | 66.91% | | should_pass/language/function_return_type_unification (test.toml) | 148 | 120 | 18.92% | | should_pass/language/generic_functions (test.toml) | 136 | 45 | 66.91% | | should_pass/language/generic_impl_self (test.toml) | 740 | 712 | 3.78% | | should_pass/language/generic_impl_self_where (test.toml) | 790 | 699 | 11.52% | | should_pass/language/generic_inside_generic (test.toml) | 163 | 72 | 55.83% | | should_pass/language/generic_result_method (test.toml) | 330 | 239 | 27.58% | | should_pass/language/generic_struct (test.toml) | 136 | 45 | 66.91% | | should_pass/language/generic_struct_instantiation (test.toml) | 137 | 46 | 66.42% | | should_pass/language/generic_structs (test.toml) | 136 | 45 | 66.91% | | should_pass/language/generic_trait_constraints (test.toml) | 187 | 62 | 66.84% | | should_pass/language/generic_traits (test.toml) | 415 | 324 | 21.93% | | should_pass/language/generic_transpose (test.toml) | 215 | 124 | 42.33% | | should_pass/language/generic_tuple_trait (test.toml) | 174 | 83 | 52.30% | | should_pass/language/generic_type_inference (test.toml) | 1349 | 1335 | 1.04% | | should_pass/language/generic_where_in_impl_self (test.toml) | 201 | 110 | 45.27% | | should_pass/language/generic_where_in_impl_self2 (test.toml) | 201 | 110 | 45.27% | | should_pass/language/gtf_intrinsic (test.toml) | 286 | 195 | 31.82% | | should_pass/language/if_elseif_enum (test.toml) | 388 | 360 | 7.22% | | should_pass/language/if_implicit_unit (test.toml) | 55 | 41 | 25.45% | | should_pass/language/if_let_no_side_effects (test.toml) | 154 | 63 | 59.09% | | should_pass/language/impl_self_method (test.toml) | 148 | 120 | 18.92% | | should_pass/language/impl_self_method_order (test.toml) | 148 | 120 | 18.92% | | should_pass/language/implicit_casting (test.toml) | 142 | 51 | 64.08% | | should_pass/language/implicit_return (test.toml) | 142 | 51 | 64.08% | | should_pass/language/import_method_from_other_file (test.toml) | 155 | 64 | 58.71% | | should_pass/language/import_star_name_clash (test.toml) | 1039 | 948 | 8.76% | | should_pass/language/import_trailing_comma (test.toml) | 143 | 52 | 63.64% | | should_pass/language/import_with_different_callpaths (test.toml) | 793 | 779 | 1.77% | | should_pass/language/impure_ifs (test.toml) | 429 | 338 | 21.21% | | should_pass/language/inline_if_expr_const (test.toml) | 55 | 41 | 25.45% | | should_pass/language/insert_element_reg_reuse (test.toml) | 1794 | 1703 | 5.07% | | should_pass/language/integer_type_inference (test.toml) | 547 | 533 | 2.56% | | should_pass/language/intrinsics/dbg (test.toml) | 1193 | 1102 | 7.63% | | should_pass/language/intrinsics/dbg_release (test.toml) | 9785 | 9694 | 0.93% | | should_pass/language/intrinsics/transmute (test.toml) | 402 | 388 | 3.48% | | should_pass/language/is_prime (test.toml) | 1047 | 956 | 8.69% | | should_pass/language/is_reference_type (test.toml) | 136 | 45 | 66.91% | | should_pass/language/left_to_right_func_args_evaluation (test.toml) | 149 | 58 | 61.07% | | should_pass/language/local_impl_for_ord (test.toml) | 136 | 45 | 66.91% | | should_pass/language/logging (test.toml) | 2126 | 1790 | 15.80% | | should_pass/language/main_args/main_args_empty (test.encoding_v1.toml) | 142 | 41 | 71.13% | | should_pass/language/main_args/main_args_empty (test.toml) | 19 | 18 | 5.26% | | should_pass/language/main_args/main_args_generics (test.toml) | 504 | 472 | 6.35% | | should_pass/language/main_args/main_args_one_u64 (test.toml) | 163 | 63 | 61.35% | | should_pass/language/main_args/main_args_ref (test.toml) | 172 | 72 | 58.14% | | should_pass/language/main_args/main_args_ref_copy (test.toml) | 181 | 81 | 55.25% | | should_pass/language/main_args/main_args_ref_ref (test.toml) | 226 | 125 | 44.69% | | should_pass/language/main_args/main_args_two_u64 (test.toml) | 172 | 72 | 58.14% | | should_pass/language/main_args/main_args_various_types (test.toml) | 1283 | 1130 | 11.93% | | should_pass/language/main_returns_unit (test.toml) | 55 | 41 | 25.45% | | should_pass/language/many_stack_variables (test.toml) | 151 | 60 | 60.26% | | should_pass/language/match_expressions_all (test.toml) | 3699 | 3685 | 0.38% | | should_pass/language/match_expressions_constants (test.toml) | 377 | 263 | 30.24% | | should_pass/language/match_expressions_empty_enums (test.toml) | 142 | 51 | 64.08% | | should_pass/language/match_expressions_enums (test.toml) | 2019 | 1928 | 4.51% | | should_pass/language/match_expressions_explicit_rets (test.toml) | 136 | 45 | 66.91% | | should_pass/language/match_expressions_inside_generic_functions (test.toml) | 170 | 79 | 53.53% | | should_pass/language/match_expressions_mismatched (test.toml) | 154 | 63 | 59.09% | | should_pass/language/match_expressions_nested (test.toml) | 649 | 621 | 4.31% | | should_pass/language/match_expressions_rest (test.toml) | 777 | 686 | 11.71% | | should_pass/language/match_expressions_simple (test.toml) | 156 | 65 | 58.33% | | should_pass/language/match_expressions_structs (test.toml) | 151 | 60 | 60.26% | | should_pass/language/match_expressions_with_self (test.toml) | 163 | 72 | 55.83% | | should_pass/language/memcpy (test.toml) | 266 | 175 | 34.21% | | should_pass/language/method_indirect_inference (test.toml) | 218 | 127 | 41.74% | | should_pass/language/method_nested_type_args (test.toml) | 55 | 41 | 25.45% | | should_pass/language/method_on_empty_struct (test.toml) | 137 | 46 | 66.42% | | should_pass/language/method_on_primitives (test.toml) | 99 | 85 | 14.14% | | should_pass/language/method_unambiguous (test.toml) | 186 | 95 | 48.92% | | should_pass/language/modulo_uint_test (test.toml) | 136 | 45 | 66.91% | | should_pass/language/multi_impl_self (test.toml) | 142 | 51 | 64.08% | | should_pass/language/multi_item_import (test.toml) | 136 | 45 | 66.91% | | should_pass/language/mutable_and_initd (test.toml) | 157 | 66 | 57.96% | | should_pass/language/mutable_arrays (test.toml) | 142 | 51 | 64.08% | | should_pass/language/mutable_arrays_enum (test.toml) | 153 | 62 | 59.48% | | should_pass/language/mutable_arrays_multiple_nested (test.toml) | 137 | 46 | 66.42% | | should_pass/language/mutable_arrays_nested (test.toml) | 137 | 46 | 66.42% | | should_pass/language/mutable_arrays_struct (test.toml) | 142 | 51 | 64.08% | | should_pass/language/mutable_arrays_swap (test.toml) | 142 | 51 | 64.08% | | should_pass/language/name_resolution_after_monomorphization (test.toml) | 148 | 57 | 61.49% | | should_pass/language/nested_generics (test.toml) | 136 | 45 | 66.91% | | should_pass/language/nested_struct_destructuring (test.toml) | 137 | 46 | 66.42% | | should_pass/language/nested_structs (test.toml) | 279 | 188 | 32.62% | | should_pass/language/nested_while_and_if (test.toml) | 194 | 103 | 46.91% | | should_pass/language/new_allocator_test (test.toml) | 204 | 113 | 44.61% | | should_pass/language/non_literal_const_decl (test.toml) | 142 | 51 | 64.08% | | should_pass/language/numeric_constants (test.toml) | 170 | 45 | 73.53% | | should_pass/language/numeric_type_propagation (test.toml) | 170 | 45 | 73.53% | | should_pass/language/op_precedence (test.toml) | 136 | 45 | 66.91% | | should_pass/language/ops (test.toml) | 155 | 64 | 58.71% | | should_pass/language/out_of_order_decl (test.toml) | 136 | 45 | 66.91% | | should_pass/language/overlapped_trait_impls (test.toml) | 276 | 185 | 32.97% | | should_pass/language/prelude_access (test.toml) | 55 | 41 | 25.45% | | should_pass/language/prelude_access2 (test.toml) | 55 | 41 | 25.45% | | should_pass/language/primitive_type_argument (test.toml) | 142 | 51 | 64.08% | | should_pass/language/pusha_popa_multiple_defreg (test.toml)::incorrect_pusha_popa | 451 | 409 | 9.31% | | should_pass/language/raw_identifiers (test.error_type.toml)::test | 903 | 738 | 18.27% | | should_pass/language/raw_identifiers (test.toml)::test | 903 | 738 | 18.27% | | should_pass/language/raw_ptr/vec_ret (test.toml) | 637 | 605 | 5.02% | | should_pass/language/reassignment_operators (test.toml) | 137 | 46 | 66.42% | | should_pass/language/reassignment_rhs_lhs_evaluation_order (test.toml) | 202 | 89 | 55.94% | | should_pass/language/redundant_return (test.toml) | 137 | 46 | 66.42% | | should_pass/language/reexport/aliases (test.toml) | 301 | 210 | 30.23% | | should_pass/language/reexport/multiple_imports_of_same_reexport (test.toml) | 329 | 238 | 27.66% | | should_pass/language/reexport/reexport_paths (test.toml) | 277 | 165 | 40.43% | | should_pass/language/reexport/shadowing_in_reexporting_module (test.toml) | 368 | 277 | 24.73% | | should_pass/language/reexport/simple_glob_import (test.toml) | 216 | 125 | 42.13% | | should_pass/language/reexport/simple_item_import (test.toml) | 216 | 125 | 42.13% | | should_pass/language/reexport/visibility (test.toml) | 216 | 125 | 42.13% | | should_pass/language/ref_mutable_arrays (test.toml) | 142 | 51 | 64.08% | | should_pass/language/ref_mutable_arrays_inline (test.toml) | 142 | 51 | 64.08% | | should_pass/language/ref_mutable_fn_args_bool (test.toml) | 136 | 45 | 66.91% | | should_pass/language/ref_mutable_fn_args_call (test.toml) | 142 | 51 | 64.08% | | should_pass/language/ref_mutable_fn_args_struct (test.toml) | 142 | 51 | 64.08% | | should_pass/language/ref_mutable_fn_args_struct_assign (test.toml) | 142 | 51 | 64.08% | | should_pass/language/ref_mutable_fn_args_u32 (test.toml) | 148 | 120 | 18.92% | | should_pass/language/references/dereferencing_control_flow_expressions (test.toml) | 652 | 540 | 17.18% | | should_pass/language/references/impl_reference_types (test.toml) | 2084 | 1902 | 8.73% | | should_pass/language/references/mutability_of_references (test.toml) | 233 | 142 | 39.06% | | should_pass/language/references/mutability_of_references_memcpy_bug (test.toml)::test | 1023 | 680 | 33.53% | | should_pass/language/references/reassigning_via_references_in_aggregates (test.toml) | 2435 | 2297 | 5.67% | | should_pass/language/references/references_and_type_aliases (test.toml) | 222 | 131 | 40.99% | | should_pass/language/references/references_in_aggregates (test.toml) | 3010 | 2919 | 3.02% | | should_pass/language/references/references_in_asm_blocks (test.toml) | 800 | 709 | 11.38% | | should_pass/language/references/referencing_control_flow_expressions (test.toml) | 841 | 750 | 10.82% | | should_pass/language/references/referencing_function_parameters (test.toml) | 2292 | 2201 | 3.97% | | should_pass/language/references/referencing_function_parameters_simple (test.toml)::test_arg_addr | 1268 | 464 | 63.41% | | should_pass/language/references/referencing_parts_of_aggregates (test.toml) | 2124 | 2033 | 4.28% | | should_pass/language/references/referencing_references (test.toml) | 417 | 326 | 21.82% | | should_pass/language/references/type_unification_of_references (test.toml) | 539 | 448 | 16.88% | | should_pass/language/ret_small_string (test.toml) | 169 | 138 | 18.34% | | should_pass/language/ret_string_in_struct (test.toml) | 185 | 153 | 17.30% | | should_pass/language/retd_b256 (test.toml) | 188 | 74 | 60.64% | | should_pass/language/retd_small_array (test.toml) | 196 | 164 | 16.33% | | should_pass/language/retd_struct (test.toml) | 362 | 331 | 8.56% | | should_pass/language/retd_zero_len_array (test.toml) | 107 | 75 | 29.91% | | should_pass/language/revert_in_first_if_branch (test.toml) | 28 | 25 | 10.71% | | should_pass/language/same_const_name (test.toml) | 55 | 41 | 25.45% | | should_pass/language/self_impl_reassignment (test.toml) | 276 | 185 | 32.97% | | should_pass/language/shadowing/shadowed_glob_imports (test.toml) | 178 | 87 | 51.12% | | should_pass/language/shadowing/shadowed_prelude_imports (test.toml) | 146 | 55 | 62.33% | | should_pass/language/size_of (test.toml) | 137 | 46 | 66.42% | | should_pass/language/slice/slice_intrinsics (test.toml) | 123848 | 104394 | 15.71% | | should_pass/language/slice/slice_script (test.toml) | 231 | 200 | 13.42% | | should_pass/language/storage_slot_sized (test.toml)::test_store_something | 7736 | 4706 | 39.17% | | should_pass/language/string_slice/string_slice_features (test.toml) | 163 | 72 | 55.83% | | should_pass/language/string_slice/string_slice_script (test.toml) | 264 | 232 | 12.12% | | should_pass/language/struct_destructuring (test.toml) | 142 | 51 | 64.08% | | should_pass/language/struct_field_access (test.toml) | 142 | 51 | 64.08% | | should_pass/language/struct_field_reassignment (test.toml) | 137 | 46 | 66.42% | | should_pass/language/struct_instantiation (test.toml) | 524 | 433 | 17.37% | | should_pass/language/supertraits (test.toml) | 1146 | 1055 | 7.94% | | should_pass/language/supertraits_with_trait_methods (test.toml) | 151 | 60 | 60.26% | | should_pass/language/totalord (test.toml) | 783 | 658 | 15.96% | | should_pass/language/trait_constraint_param_order (test.toml) | 136 | 45 | 66.91% | | should_pass/language/trait_generic_override (test.toml) | 136 | 45 | 66.91% | | should_pass/language/trait_import_with_star (test.toml) | 55 | 41 | 25.45% | | should_pass/language/trait_inference (test.toml) | 182 | 57 | 68.68% | | should_pass/language/trait_method_ascription_disambiguate (test.toml) | 136 | 45 | 66.91% | | should_pass/language/trait_method_generic_qualified (test.toml) | 136 | 45 | 66.91% | | should_pass/language/trait_method_qualified (test.toml) | 136 | 45 | 66.91% | | should_pass/language/trait_nested (test.toml) | 189 | 64 | 66.14% | | should_pass/language/tuple_access (test.toml) | 176 | 85 | 51.70% | | should_pass/language/tuple_desugaring (test.toml) | 155 | 127 | 18.06% | | should_pass/language/tuple_field_reassignment (test.toml) | 168 | 77 | 54.17% | | should_pass/language/tuple_in_struct (test.toml) | 199 | 108 | 45.73% | | should_pass/language/tuple_indexing (test.toml) | 143 | 115 | 19.58% | | should_pass/language/tuple_single_element (test.toml) | 149 | 58 | 61.07% | | should_pass/language/tuple_trait (test.toml) | 152 | 61 | 59.87% | | should_pass/language/tuple_types (test.toml) | 148 | 120 | 18.92% | | should_pass/language/type_alias (test.toml) | 1064 | 1050 | 1.32% | | should_pass/language/type_inference_propagation_of_type_constraints (test.toml) | 268 | 237 | 11.57% | | should_pass/language/typeinfo_custom_callpath (test.toml) | 67 | 53 | 20.90% | | should_pass/language/typeinfo_custom_callpath2 (test.toml) | 67 | 53 | 20.90% | | should_pass/language/typeinfo_custom_callpath_with_import (test.toml) | 69 | 55 | 20.29% | | should_pass/language/u256/u256_abi (test.toml) | 392 | 164 | 58.16% | | should_pass/language/unary_not_basic (test.toml) | 136 | 45 | 66.91% | | should_pass/language/unary_not_basic_2 (test.toml) | 136 | 45 | 66.91% | | should_pass/language/unify_never (test.toml) | 142 | 51 | 64.08% | | should_pass/language/unit_type_variants (test.toml) | 224 | 53 | 76.34% | | should_pass/language/use_absolute_path (test.toml) | 137 | 46 | 66.42% | | should_pass/language/use_full_path_names (test.toml) | 143 | 115 | 19.58% | | should_pass/language/where_clause_enums (test.toml) | 280 | 189 | 32.50% | | should_pass/language/where_clause_functions (test.toml) | 453 | 362 | 20.09% | | should_pass/language/where_clause_generic_traits (test.toml) | 55 | 41 | 25.45% | | should_pass/language/where_clause_generic_tuple (test.toml) | 142 | 51 | 64.08% | | should_pass/language/where_clause_impls (test.toml) | 158 | 67 | 57.59% | | should_pass/language/where_clause_methods (test.toml) | 541 | 450 | 16.82% | | should_pass/language/where_clause_structs (test.toml) | 254 | 163 | 35.83% | | should_pass/language/where_clause_traits (test.toml) | 136 | 45 | 66.91% | | should_pass/language/while_loops (test.toml) | 389 | 298 | 23.39% | | should_pass/language/zero_field_types (test.toml) | 142 | 51 | 64.08% | | should_pass/return_in_non_statement_positions (test.toml) | 142 | 51 | 64.08% | | should_pass/return_into (test.toml) | 412 | 321 | 22.09% | | should_pass/stdlib/address_test (test.toml) | 1175 | 1084 | 7.74% | | should_pass/stdlib/alloc_test (test.toml) | 265 | 174 | 34.34% | | should_pass/stdlib/assert_eq (test.toml) | 1108 | 983 | 11.28% | | should_pass/stdlib/assert_eq_revert (test.toml) | 303 | 152 | 49.83% | | should_pass/stdlib/assert_ne (test.toml) | 1045 | 921 | 11.87% | | should_pass/stdlib/assert_ne_revert (test.toml) | 302 | 151 | 50.00% | | should_pass/stdlib/assert_test (test.toml) | 136 | 45 | 66.91% | | should_pass/stdlib/asset_id_into_bytes (test.toml) | 173 | 82 | 52.60% | | should_pass/stdlib/b512_struct_alignment (test.toml) | 202 | 111 | 45.05% | | should_pass/stdlib/b512_test (test.toml) | 996 | 905 | 9.14% | | should_pass/stdlib/block_height (test.toml) | 149 | 58 | 61.07% | | should_pass/stdlib/chess (test.toml) | 231 | 140 | 39.39% | | should_pass/stdlib/contract_id_test (test.toml) | 174 | 83 | 52.30% | | should_pass/stdlib/contract_id_type (test.toml) | 169 | 78 | 53.85% | | should_pass/stdlib/eq_generic (test.toml) | 154 | 140 | 9.09% | | should_pass/stdlib/ge_test (test.toml) | 136 | 45 | 66.91% | | should_pass/stdlib/generic_empty_struct_with_constraint (test.toml) | 55 | 41 | 25.45% | | should_pass/stdlib/identity_eq (test.toml) | 1274 | 1183 | 7.14% | | should_pass/stdlib/if_type_revert (test.toml) | 26 | 24 | 7.69% | | should_pass/stdlib/intrinsics (test.toml) | 136 | 45 | 66.91% | | should_pass/stdlib/iterator (test.toml) | 984 | 859 | 12.70% | | should_pass/stdlib/option_eq (test.toml) | 5291 | 5200 | 1.72% | | should_pass/stdlib/raw_ptr (test.toml) | 4316 | 4225 | 2.11% | | should_pass/stdlib/raw_slice (test.toml) | 424 | 392 | 7.55% | | should_pass/stdlib/require (test.toml) | 182 | 165 | 9.34% | | should_pass/stdlib/storage_vec_insert (test.toml)::test_test_function | 2903 | 2754 | 5.13% | | should_pass/stdlib/u128_div_test (test.toml) | 34133 | 34042 | 0.27% | | should_pass/stdlib/u128_log_test (test.toml) | 11809 | 11718 | 0.77% | | should_pass/stdlib/u128_mul_test (test.toml) | 453 | 362 | 20.09% | | should_pass/stdlib/u128_root_test (test.toml) | 7634 | 7543 | 1.19% | | should_pass/stdlib/u128_test (test.toml) | 2177 | 2086 | 4.18% | | should_pass/stdlib/vec (test.toml) | 65253 | 65162 | 0.14% | | should_pass/stdlib/vec_swap (test.toml) | 14862 | 14771 | 0.61% | | should_pass/storage_element_key_modification (test.toml)::test_storage_key_address | 943 | 863 | 8.48% | | should_pass/storage_element_key_modification (test.toml)::test_storage_key_modification | 465 | 385 | 17.20% | | should_pass/storage_slot_key_calculation (test.toml)::test | 2785 | 2708 | 2.76% | | should_pass/superabi_contract_calls (test.toml)::tests | 1310 | 1004 | 23.36% | | should_pass/superabi_supertrait_same_methods (test.toml)::tests | 634 | 483 | 23.82% | | should_pass/test_abis/abi_impl_methods_callable (test.toml)::tests | 599 | 449 | 25.04% | | should_pass/test_abis/contract_abi-auto_impl (test.toml)::tests | 599 | 449 | 25.04% | | should_pass/test_contracts/basic_storage (test.toml)::collect_basic_storage_contract_gas_usages | 37164 | 35842 | 3.56% | | should_pass/test_contracts/increment_contract (test.toml)::collect_incrementor_contract_gas_usages | 2006 | 1628 | 18.84% | | should_pass/test_contracts/return_struct (test.toml)::collect_my_contract_gas_usages | 1076 | 1000 | 7.06% | | should_pass/test_contracts/storage_access_contract (test.toml)::collect_storage_access_contract_gas_usages | 49894 | 44857 | 10.10% | | should_pass/test_contracts/storage_enum_contract (test.toml)::collect_storage_enum_contract_gas_usages | 22372 | 22213 | 0.71% | | should_pass/unit_tests/aggr_indexing (test.toml)::test1 | 4192 | 2082 | 50.33% | | should_pass/unit_tests/contract-multi-contract-calls (test.toml)::test_contract_2_call | 631 | 480 | 23.93% | | should_pass/unit_tests/contract-multi-contract-calls (test.toml)::test_contract_call | 634 | 483 | 23.82% | | should_pass/unit_tests/contract-multi-contract-calls (test.toml)::test_contract_multi_call | 1246 | 944 | 24.24% | | should_pass/unit_tests/contract_multi_test (test.toml)::test_bar | 175 | 68 | 61.14% | | should_pass/unit_tests/contract_multi_test (test.toml)::test_fail | 635 | 484 | 23.78% | | should_pass/unit_tests/contract_multi_test (test.toml)::test_success | 633 | 482 | 23.85% | | should_pass/unit_tests/contract_with_nested_libs (test.toml)::log_test | 180 | 184 | -2.22% | | should_pass/unit_tests/contract_with_nested_libs (test.toml)::log_test_inner | 180 | 184 | -2.22% | | should_pass/unit_tests/contract_with_nested_libs (test.toml)::log_test_inner2 | 173 | 67 | 61.27% | | should_pass/unit_tests/lib_log_decode (test.toml)::math_u16_overflow_mul | 214 | 218 | -1.87% | | should_pass/unit_tests/lib_log_decode (test.toml)::math_u32_overflow_mul | 214 | 218 | -1.87% | | should_pass/unit_tests/lib_log_decode (test.toml)::test_fn | 362 | 146 | 59.67% | | should_pass/unit_tests/lib_multi_test (test.toml)::test_gt | 156 | 82 | 47.44% | | should_pass/unit_tests/lib_multi_test (test.toml)::test_local | 156 | 82 | 47.44% | | should_pass/unit_tests/nested_libs (test.toml)::log_test | 179 | 183 | -2.23% | | should_pass/unit_tests/nested_libs (test.toml)::log_test_inner | 179 | 183 | -2.23% | | should_pass/unit_tests/nested_libs (test.toml)::log_test_inner2 | 172 | 66 | 61.63% | | should_pass/unit_tests/regalloc_spill (test.toml) | 181 | 90 | 50.28% | | should_pass/unit_tests/script-contract-calls (test.toml)::test_contract_call | 563 | 448 | 20.43% | | should_pass/unit_tests/script_log_decode (test.toml)::test_fn | 294 | 146 | 50.34% | | should_pass/unit_tests/script_with_nested_libs (test.toml)::log_test | 179 | 183 | -2.23% | | should_pass/unit_tests/script_with_nested_libs (test.toml)::log_test_inner | 179 | 183 | -2.23% | | should_pass/unit_tests/script_with_nested_libs (test.toml)::log_test_inner2 | 172 | 66 | 61.63% | | should_pass/unit_tests/workspace_test (test.toml)::test_bar | 175 | 68 | 61.14% | | should_pass/unit_tests/workspace_test (test.toml)::test_fail | 635 | 484 | 23.78% | | should_pass/unit_tests/workspace_test (test.toml)::test_gt | 156 | 82 | 47.44% | | should_pass/unit_tests/workspace_test (test.toml)::test_local | 156 | 82 | 47.44% | | should_pass/unit_tests/workspace_test (test.toml)::test_success | 634 | 483 | 23.82% | | | Improvements | Regressions | | - | -: | -: | | Count | 346 | 8 | | Average | 34.98% | -2.14% | | Median | 28.79% | -2.23% | | Max | 76.34% | -2.23% | | Min | 0.14% | -1.87% | | Test | Before | After | Percentage | | ---- | -----: | ----: | ---------: | | assert_inline_tests::assert_assert_eq | 1576 | 1529 | 2.98% | | assert_inline_tests::assert_assert_ne | 1630 | 1585 | 2.76% | | assert_inline_tests::revert_assert_assert_eq | 1480 | 1117 | 24.53% | | assert_inline_tests::revert_assert_assert_ne | 1488 | 1115 | 25.07% | | asset_id_contract_tests::asset_id_default | 10994 | 10696 | 2.71% | | bytes_inline_tests::bytes_append | 5789 | 5787 | 0.03% | | bytes_inline_tests::bytes_split_at_twice | 1321 | 1319 | 0.15% | | codec_implemented_tests::test_logging | 51837 | 23126 | 55.39% | | contract_id_contract_tests::contract_id_this | 10787 | 10488 | 2.77% | | crypto_ed25519_inline_tests::ed25519_codec | 6125 | 640 | 89.55% | | crypto_secp256k1_inline_tests::secp256k1_codec | 6125 | 640 | 89.55% | | crypto_secp256r1_inline_tests::secp256r1_codec | 6125 | 640 | 89.55% | | crypto_signature_inline_tests::signature_as_ed25519 | 6840 | 6841 | -0.01% | | crypto_signature_inline_tests::signature_as_secp256r1 | 6836 | 6837 | -0.01% | | crypto_signature_inline_tests::signature_codec | 7748 | 654 | 91.56% | | crypto_signature_inline_tests::signature_verify | 43208 | 43205 | 0.01% | | hash_inline_tests::hash_address | 7223 | 7211 | 0.17% | | hash_inline_tests::hash_asset_id | 7223 | 7211 | 0.17% | | hash_inline_tests::hash_b256 | 7171 | 7159 | 0.17% | | hash_inline_tests::hash_call_params | 6930 | 6924 | 0.09% | | hash_inline_tests::hash_contract_id | 7223 | 7211 | 0.17% | | hash_inline_tests::hash_evm_address | 7407 | 7395 | 0.16% | | hash_inline_tests::hash_identity | 17767 | 17743 | 0.14% | | hash_inline_tests::hash_point2d | 9311 | 9305 | 0.06% | | hash_inline_tests::hash_signature | 20238 | 20220 | 0.09% | | hash_inline_tests::hash_str | 10508 | 10505 | 0.03% | | hash_inline_tests::hash_tuple_4 | 8499 | 8497 | 0.02% | | hash_inline_tests::hash_u128 | 5160 | 5154 | 0.12% | | hash_inline_tests::hash_user_defined_type | 42799 | 42787 | 0.03% | | storage_vec_iter_tests::empty_vec_next_returns_none | 50784 | 50644 | 0.28% | | storage_vec_iter_tests::storage_vec_field_for_loop_iteration | 1574940 | 1573470 | 0.09% | | storage_vec_iter_tests::storage_vec_field_nested_for_loop_iteration | 11570183 | 11559644 | 0.09% | | storage_vec_iter_tests::vec_with_elements_for_loop_iteration | 24015966 | 24011576 | 0.02% | | storage_vec_iter_tests::vec_with_elements_next_returns_element | 24016001 | 24011606 | 0.02% | | u128_inline_tests::parity_u128_log_with_ruint | 1851450 | 1850539 | 0.05% | | u128_inline_tests::revert_u128_zero_root | 232 | 230 | 0.86% | | u128_inline_tests::revert_u128_zero_root_overflow_disabled | 238 | 236 | 0.84% | | u128_inline_tests::u128_log | 24393 | 24391 | 0.01% | | u128_inline_tests::u128_pow | 8902 | 8900 | 0.02% | | u128_inline_tests::u128_root | 13907 | 13872 | 0.25% | | u128_inline_tests::u128_zero_root_unsafe_math | 452 | 450 | 0.44% | | | Improvements | Regressions | | - | -: | -: | | Count | 39 | 2 | | Average | 12.33% | -0.01% | | Median | 0.17% | -0.01% | | Max | 91.56% | -0.01% | | Min | 0.01% | -0.01% | ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [ ] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Introduce `__runtime_mem_id`/`__encoding_mem_id` intrinsics and use them to trivially encode/return values, updating ABI entry to `never`, improving gas/binary size, and adjusting docs, IR, and tests. > > - **Core/Compiler**: > - Add intrinsics `__runtime_mem_id<T>() -> u64` and `__encoding_mem_id<T>() -> u64` (AST, semantic, IR gen, const-eval). > - Implement memory-representation modeling and ID hashing (`get_memory_representation`, `get_memory_id`, `get_encoding_representation`/`id`). > - Update ABI entry generation to return `never` and use direct `retd`. > - Extend IR parser/types with `never`. > - **Std Library (`codec.sw`)**: > - `encode<T>`: detect trivially-encodable types (matching IDs) and `memcpy` bytes; otherwise fallback to normal encoding. > - Add `encode_and_return<T>() -> !` using `__contract_ret` for fast returns. > - **Docs**: > - Document `__transmute`, `__runtime_mem_id`, and `__encoding_mem_id` in the Sway book; include constraints/semantics. > - **Tests/Fixtures**: > - Update many snapshots, gas/bytecode expectations, raw log PCs/pointers, and release flags. > - Refresh expected contract IDs in require-deployment tests. > - Notable size/gas reductions across numerous tests; `main_args_empty` shrinks (e.g., 96→64 B) and many gas wins. > - **Misc**: > - Minor utilities (e.g., const-generic length literal extraction) and effect tracking for new intrinsics. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 2caf44c. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: Joshua Batty <[email protected]>
1 parent fb10b2f commit 78aa952

File tree

62 files changed

+6597
-3088
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+6597
-3088
lines changed

docs/book/src/reference/compiler_intrinsics.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,30 @@ on real nodes and it only increases gas usage.
408408
**Constraints:**
409409

410410
- `T` must implement Debug
411+
412+
---
413+
414+
```sway
415+
__transmute<A, B>(src: A) -> B
416+
```
417+
418+
**Description:** Reinterprets the bits of a value of one type as another type.
419+
420+
**Constraints:** A and B must have the exactly same size.
421+
422+
---
423+
424+
```sway
425+
__runtime_mem_id<T>() -> u64
426+
__encoding_mem_id<T>() -> u64
427+
```
428+
429+
**Description:** Returns an opaque number that identifies the memory representation of a type. No information is conveyed by this number and should only be compared for equality.
430+
431+
This number is not guaranteed to be stable on different compiler versions.
432+
433+
`__runtime_mem_id` represents how the type is represented inside the VM.
434+
435+
`__encoding_mem_id` represents how the type is encoded. It returns 0 when a type does not have encoding representation.
436+
437+
**Constraints:** None

forc-plugins/forc-client/tests/deploy.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ async fn test_simple_deploy() {
377377
node.kill().unwrap();
378378
let expected = vec![DeployedPackage::Contract(DeployedContract {
379379
id: ContractId::from_str(
380-
"307a59fbb6888da41942653ca21d9c71e06d72edb58b8d775eea87c7e755ebdb",
380+
"e3e0f5c8c9585a7dc6673775a73104acc847905b5e7ea085db822241444fee7e",
381381
)
382382
.unwrap(),
383383
proxy: None,
@@ -421,7 +421,7 @@ async fn test_deploy_submit_only() {
421421
node.kill().unwrap();
422422
let expected = vec![DeployedPackage::Contract(DeployedContract {
423423
id: ContractId::from_str(
424-
"307a59fbb6888da41942653ca21d9c71e06d72edb58b8d775eea87c7e755ebdb",
424+
"e3e0f5c8c9585a7dc6673775a73104acc847905b5e7ea085db822241444fee7e",
425425
)
426426
.unwrap(),
427427
proxy: None,
@@ -468,12 +468,12 @@ async fn test_deploy_fresh_proxy() {
468468
node.kill().unwrap();
469469
let impl_contract = DeployedPackage::Contract(DeployedContract {
470470
id: ContractId::from_str(
471-
"307a59fbb6888da41942653ca21d9c71e06d72edb58b8d775eea87c7e755ebdb",
471+
"e3e0f5c8c9585a7dc6673775a73104acc847905b5e7ea085db822241444fee7e",
472472
)
473473
.unwrap(),
474474
proxy: Some(
475475
ContractId::from_str(
476-
"70a447780b00c9725859bbef1cd327a9455cc62fbe607a0524b2c82a5846f5a9",
476+
"92343c25e373958aee4cecfe58ab6533261b5f54de471291c22ee05182d39122",
477477
)
478478
.unwrap(),
479479
),

forc/tests/cli_integration.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ fn test_forc_test_raw_logs() -> Result<(), rexpect::error::Error> {
5151
// Assert that the output is correct
5252
process.exp_string(" test test_log_4")?;
5353
process.exp_string("raw logs:")?;
54-
process.exp_string(r#"[{"LogData":{"data":"0000000000000004","digest":"8005f02d43fa06e7d0585fb64c961d57e318b27a145c857bcd3a6bdb413ff7fc","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12044,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
54+
process.exp_string(r#"[{"LogData":{"data":"0000000000000004","digest":"8005f02d43fa06e7d0585fb64c961d57e318b27a145c857bcd3a6bdb413ff7fc","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12312,"ptr":67108856,"ra":0,"rb":1515152261580153489}}]"#)?;
5555
process.exp_string(" test test_log_2")?;
5656
process.exp_string("raw logs:")?;
57-
process.exp_string(r#"[{"LogData":{"data":"0000000000000002","digest":"cd04a4754498e06db5a13c5f371f1f04ff6d2470f24aa9bd886540e5dce77f70","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12044,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
57+
process.exp_string(r#"[{"LogData":{"data":"0000000000000002","digest":"cd04a4754498e06db5a13c5f371f1f04ff6d2470f24aa9bd886540e5dce77f70","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12312,"ptr":67108856,"ra":0,"rb":1515152261580153489}}]"#)?;
5858

5959
process.process.exit()?;
6060
Ok(())
@@ -77,12 +77,12 @@ fn test_forc_test_both_logs() -> Result<(), rexpect::error::Error> {
7777
process.exp_string("decoded log values:")?;
7878
process.exp_string("4, log rb: 1515152261580153489")?;
7979
process.exp_string("raw logs:")?;
80-
process.exp_string(r#"[{"LogData":{"data":"0000000000000004","digest":"8005f02d43fa06e7d0585fb64c961d57e318b27a145c857bcd3a6bdb413ff7fc","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12044,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
80+
process.exp_string(r#"[{"LogData":{"data":"0000000000000004","digest":"8005f02d43fa06e7d0585fb64c961d57e318b27a145c857bcd3a6bdb413ff7fc","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12312,"ptr":67108856,"ra":0,"rb":1515152261580153489}}]"#)?;
8181
process.exp_string(" test test_log_2")?;
8282
process.exp_string("decoded log values:")?;
8383
process.exp_string("2, log rb: 1515152261580153489")?;
8484
process.exp_string("raw logs:")?;
85-
process.exp_string(r#"[{"LogData":{"data":"0000000000000002","digest":"cd04a4754498e06db5a13c5f371f1f04ff6d2470f24aa9bd886540e5dce77f70","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12044,"ptr":67107840,"ra":0,"rb":1515152261580153489}}]"#)?;
85+
process.exp_string(r#"[{"LogData":{"data":"0000000000000002","digest":"cd04a4754498e06db5a13c5f371f1f04ff6d2470f24aa9bd886540e5dce77f70","id":"0000000000000000000000000000000000000000000000000000000000000000","is":10368,"len":8,"pc":12312,"ptr":67108856,"ra":0,"rb":1515152261580153489}}]"#)?;
8686
process.process.exit()?;
8787
Ok(())
8888
}

sway-ast/src/intrinsics.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ pub enum Intrinsic {
4646
ElemAt, // let elem: &T = __elem_at::<T: array or ref_to_slice>(item: T, index)
4747
Transmute, // let dst: B = __transmute::<A, B>(src)
4848
Dbg, // __dbg(value)
49+
RuntimeMemoryId, // __runtime_mem_id::<T>() -> u64
50+
EncodingMemoryId, // __encoding_mem_id::<T>() -> u64
4951
}
5052

5153
impl fmt::Display for Intrinsic {
@@ -94,6 +96,8 @@ impl fmt::Display for Intrinsic {
9496
Intrinsic::ElemAt => "elem_at",
9597
Intrinsic::Transmute => "transmute",
9698
Intrinsic::Dbg => "dbg",
99+
Intrinsic::RuntimeMemoryId => "runtime_mem_id",
100+
Intrinsic::EncodingMemoryId => "encoding_mem_id",
97101
};
98102
write!(f, "{s}")
99103
}
@@ -146,6 +150,8 @@ impl Intrinsic {
146150
"__elem_at" => ElemAt,
147151
"__transmute" => Transmute,
148152
"__dbg" => Dbg,
153+
"__runtime_mem_id" => RuntimeMemoryId,
154+
"__encoding_mem_id" => EncodingMemoryId,
149155
_ => return None,
150156
})
151157
}

sway-core/src/ir_generation/const_eval.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use std::{
2+
hash::{DefaultHasher, Hash},
23
io::Read,
34
ops::{BitAnd, BitOr, BitXor, Not, Rem},
45
};
56

67
use crate::{
78
engine_threading::*,
9+
ir_generation::function::{get_encoding_representation, get_memory_id},
810
language::{
911
ty::{self, TyConstantDecl, TyIntrinsicFunctionKind},
1012
CallPath, Literal,
@@ -1693,6 +1695,50 @@ fn const_eval_intrinsic(
16931695
Intrinsic::Dbg => {
16941696
unreachable!("__dbg should not exist in the typed tree")
16951697
}
1698+
Intrinsic::RuntimeMemoryId => {
1699+
assert!(intrinsic.type_arguments.len() == 1);
1700+
assert!(intrinsic.arguments.is_empty());
1701+
1702+
let t = &intrinsic.type_arguments[0];
1703+
let t = convert_resolved_type_id(
1704+
lookup.engines,
1705+
lookup.context,
1706+
lookup.md_mgr,
1707+
lookup.module,
1708+
lookup.function_compiler,
1709+
t.type_id(),
1710+
&t.span(),
1711+
)
1712+
.unwrap();
1713+
1714+
let id = get_memory_id(lookup.context, t);
1715+
let c = ConstantContent {
1716+
ty: Type::get_uint64(lookup.context),
1717+
value: ConstantValue::Uint(id),
1718+
};
1719+
1720+
Ok(Some(Constant::unique(lookup.context, c)))
1721+
}
1722+
Intrinsic::EncodingMemoryId => {
1723+
assert!(intrinsic.type_arguments.len() == 1);
1724+
assert!(intrinsic.arguments.is_empty());
1725+
1726+
let t = intrinsic.type_arguments[0].as_type_argument().unwrap();
1727+
1728+
let r = get_encoding_representation(lookup.engines, t.type_id);
1729+
1730+
use std::hash::Hasher;
1731+
let mut state = DefaultHasher::default();
1732+
r.hash(&mut state);
1733+
let id = state.finish();
1734+
1735+
let c = ConstantContent {
1736+
ty: Type::get_uint64(lookup.context),
1737+
value: ConstantValue::Uint(id),
1738+
};
1739+
1740+
Ok(Some(Constant::unique(lookup.context, c)))
1741+
}
16961742
}
16971743
}
16981744

0 commit comments

Comments
 (0)