Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
Remove some CPU cycles (0xPolygonZero#1469)
Browse files Browse the repository at this point in the history
* Amortize mload_packing

* Reduce stack overhead

* Amortize mstore_unpacking

* Speed-up stack operation in hash.asm

* Misc

* Small tweaks

* Misc small optims

* Fix comments

* Fix main access to withdrawals

* Fix stack description

* minor: rename label

* Comments

---------

Co-authored-by: Linda Guiga <[email protected]>
  • Loading branch information
Nashtare and LindaGuiga authored Jan 16, 2024
1 parent 30b4799 commit 990eb34
Show file tree
Hide file tree
Showing 25 changed files with 153 additions and 230 deletions.
12 changes: 6 additions & 6 deletions evm/src/cpu/kernel/asm/account_code.asm
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ global sys_extcodehash:
// stack: kexit_info, address
SWAP1 %u256_to_addr
// stack: address, kexit_info
DUP1 %insert_accessed_addresses
// stack: cold_access, address, kexit_info
SWAP1
DUP2 %insert_accessed_addresses
// stack: cold_access, kexit_info, address
PUSH @GAS_COLDACCOUNTACCESS_MINUS_WARMACCESS
MUL
PUSH @GAS_WARMACCESS
ADD
%stack (gas, address, kexit_info) -> (gas, kexit_info, address)
%charge_gas
// stack: kexit_info, address

Expand Down Expand Up @@ -57,13 +57,13 @@ global sys_extcodesize:
// stack: kexit_info, address
SWAP1 %u256_to_addr
// stack: address, kexit_info
DUP1 %insert_accessed_addresses
// stack: cold_access, address, kexit_info
SWAP1
DUP2 %insert_accessed_addresses
// stack: cold_access, kexit_info, address
PUSH @GAS_COLDACCOUNTACCESS_MINUS_WARMACCESS
MUL
PUSH @GAS_WARMACCESS
ADD
%stack (gas, address, kexit_info) -> (gas, kexit_info, address)
%charge_gas
// stack: kexit_info, address

Expand Down
6 changes: 3 additions & 3 deletions evm/src/cpu/kernel/asm/balance.asm
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ global sys_balance:
// stack: kexit_info, address
SWAP1 %u256_to_addr
// stack: address, kexit_info
DUP1 %insert_accessed_addresses
// stack: cold_access, address, kexit_info
SWAP1
DUP2 %insert_accessed_addresses
// stack: cold_access, kexit_info, address
PUSH @GAS_COLDACCOUNTACCESS_MINUS_WARMACCESS
MUL
PUSH @GAS_WARMACCESS
ADD
%stack (gas, address, kexit_info) -> (gas, kexit_info, address)
%charge_gas
// stack: kexit_info, address

Expand Down
15 changes: 6 additions & 9 deletions evm/src/cpu/kernel/asm/core/create_addresses.asm
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,17 @@ global get_create_address:
global get_create2_address:
// stack: sender, code_hash, salt, retdest
PUSH 0xff PUSH 0 %mstore_kernel_general
%stack (sender, code_hash, salt, retdest) -> (@SEGMENT_KERNEL_GENERAL, 1, sender, 20, get_create2_address_contd, salt, code_hash, retdest)
%stack (sender, code_hash, salt, retdest) -> (@SEGMENT_KERNEL_GENERAL, 1, sender, salt, code_hash, retdest)
ADD
%jump(mstore_unpacking)
get_create2_address_contd:
MSTORE_32BYTES_20
POP
%stack (salt, code_hash, retdest) -> (@SEGMENT_KERNEL_GENERAL, 21, salt, 32, get_create2_address_contd2, code_hash, retdest)
%stack (salt, code_hash, retdest) -> (@SEGMENT_KERNEL_GENERAL, 21, salt, code_hash, retdest)
ADD
%jump(mstore_unpacking)
get_create2_address_contd2:
MSTORE_32BYTES_32
POP
%stack (code_hash, retdest) -> (@SEGMENT_KERNEL_GENERAL, 53, code_hash, 32, get_create2_address_finish, retdest)
%stack (code_hash, retdest) -> (@SEGMENT_KERNEL_GENERAL, 53, code_hash, retdest)
ADD
%jump(mstore_unpacking)
get_create2_address_finish:
MSTORE_32BYTES_32
POP
%stack (retdest) -> (@SEGMENT_KERNEL_GENERAL, 85, retdest) // offset == context == 0
// addr, len, retdest
Expand Down
4 changes: 2 additions & 2 deletions evm/src/cpu/kernel/asm/core/jumpdest_analysis.asm
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ continue:
proof_ok:
// stack: i, ctx, final_pos, retdest
// We already know final_pos is a jumpdest
%stack (i, ctx, final_pos) -> (ctx, @SEGMENT_JUMPDEST_BITS, i)
%stack (i, ctx, final_pos) -> (ctx, @SEGMENT_JUMPDEST_BITS, final_pos)
%build_address
PUSH 1
MSTORE_GENERAL
Expand Down Expand Up @@ -145,7 +145,7 @@ global write_table_if_jumpdest:
(proof_prefix_addr, ctx) ->
(ctx, proof_prefix_addr, 32, proof_prefix_addr, ctx)
ADD // combine context and offset to make an address (SEGMENT_CODE == 0)
%mload_packing
MLOAD_32BYTES
// packed_opcodes, proof_prefix_addr, ctx, jumpdest, retdest
DUP1 %shl_const(1)
DUP2 %shl_const(2)
Expand Down
2 changes: 1 addition & 1 deletion evm/src/cpu/kernel/asm/core/precompiles/blake2_f.asm
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ global precompile_blake2_f:
GET_CONTEXT
// stack: ctx, @SEGMENT_CALLDATA, 4, h_0..h_7, m_0..m_15, t_0, t_1, flag, blake2_f_contd, kexit_info
%build_address_no_offset
%mload_packing
MLOAD_32BYTES
// stack: rounds, h_0..h_7, m_0..m_15, t_0, t_1, flag, blake2_f_contd, kexit_info
DUP1
Expand Down
20 changes: 10 additions & 10 deletions evm/src/cpu/kernel/asm/core/precompiles/bn_add.asm
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,32 @@ global precompile_bn_add:

%charge_gas_const(@BN_ADD_GAS)

// Load x0, y0, x1, y1 from the call data using `mload_packing`.
// Load x0, y0, x1, y1 from the call data using `MLOAD_32BYTES`.
PUSH bn_add_return
// stack: bn_add_return, kexit_info
%stack () -> (@SEGMENT_CALLDATA, 96, 32)
GET_CONTEXT
// stack: ctx, @SEGMENT_CALLDATA, 96, 32, bn_add_return, kexit_info
%build_address
%mload_packing
MLOAD_32BYTES
// stack: y1, bn_add_return, kexit_info
%stack () -> (@SEGMENT_CALLDATA, 64, 32)
GET_CONTEXT
// stack: ctx, @SEGMENT_CALLDATA, 64, 32, y1, bn_add_return, kexit_info
%build_address
%mload_packing
MLOAD_32BYTES
// stack: x1, y1, bn_add_return, kexit_info
%stack () -> (@SEGMENT_CALLDATA, 32, 32)
GET_CONTEXT
// stack: ctx, @SEGMENT_CALLDATA, 32, 32, x1, y1, bn_add_return, kexit_info
%build_address
%mload_packing
MLOAD_32BYTES
// stack: y0, x1, y1, bn_add_return, kexit_info
%stack () -> (@SEGMENT_CALLDATA, 32)
GET_CONTEXT
// stack: ctx, @SEGMENT_CALLDATA, 32, y0, x1, y1, bn_add_return, kexit_info
%build_address_no_offset
%mload_packing
MLOAD_32BYTES
// stack: x0, y0, x1, y1, bn_add_return, kexit_info
%jump(bn_add)
bn_add_return:
Expand All @@ -53,11 +53,11 @@ bn_add_return:
// Store the result (x, y) to the parent's return data using `mstore_unpacking`.
%mstore_parent_context_metadata(@CTX_METADATA_RETURNDATA_SIZE, 64)
%mload_context_metadata(@CTX_METADATA_PARENT_CONTEXT)
%stack (parent_ctx, x, y) -> (parent_ctx, @SEGMENT_RETURNDATA, x, 32, bn_add_contd6, parent_ctx, y)
%stack (parent_ctx, x, y) -> (parent_ctx, @SEGMENT_RETURNDATA, x, parent_ctx, y)
%build_address_no_offset
%jump(mstore_unpacking)
bn_add_contd6:
MSTORE_32BYTES_32
POP
%stack (parent_ctx, y) -> (parent_ctx, @SEGMENT_RETURNDATA, 32, y, 32, pop_and_return_success)
%stack (parent_ctx, y) -> (parent_ctx, @SEGMENT_RETURNDATA, 32, y)
%build_address
%jump(mstore_unpacking)
MSTORE_32BYTES_32
%jump(pop_and_return_success)
17 changes: 9 additions & 8 deletions evm/src/cpu/kernel/asm/core/precompiles/bn_mul.asm
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@ global precompile_bn_mul:

%charge_gas_const(@BN_MUL_GAS)

// Load x, y, n from the call data using `mload_packing`.
// Load x, y, n from the call data using `MLOAD_32BYTES`.
PUSH bn_mul_return
// stack: bn_mul_return, kexit_info
%stack () -> (@SEGMENT_CALLDATA, 64, 32)
GET_CONTEXT
// stack: ctx, @SEGMENT_CALLDATA, 64, 32, bn_mul_return, kexit_info
%build_address
%mload_packing
MLOAD_32BYTES
// stack: n, bn_mul_return, kexit_info
%stack () -> (@SEGMENT_CALLDATA, 32, 32)
GET_CONTEXT
// stack: ctx, @SEGMENT_CALLDATA, 32, 32, n, bn_mul_return, kexit_info
%build_address
%mload_packing
MLOAD_32BYTES
// stack: y, n, bn_mul_return, kexit_info
%stack () -> (@SEGMENT_CALLDATA, 32)
GET_CONTEXT
// stack: ctx, @SEGMENT_CALLDATA, 32, y, n, bn_mul_return, kexit_info
%build_address_no_offset
%mload_packing
MLOAD_32BYTES
// stack: x, y, n, bn_mul_return, kexit_info
%jump(bn_mul)
bn_mul_return:
Expand All @@ -47,11 +47,12 @@ bn_mul_return:
// Store the result (Px, Py) to the parent's return data using `mstore_unpacking`.
%mstore_parent_context_metadata(@CTX_METADATA_RETURNDATA_SIZE, 64)
%mload_context_metadata(@CTX_METADATA_PARENT_CONTEXT)
%stack (parent_ctx, Px, Py) -> (parent_ctx, @SEGMENT_RETURNDATA, Px, 32, bn_mul_contd6, parent_ctx, Py)
%stack (parent_ctx, Px, Py) -> (parent_ctx, @SEGMENT_RETURNDATA, Px, parent_ctx, Py)
%build_address_no_offset
%jump(mstore_unpacking)
MSTORE_32BYTES_32
bn_mul_contd6:
POP
%stack (parent_ctx, Py) -> (parent_ctx, @SEGMENT_RETURNDATA, 32, Py, 32, pop_and_return_success)
%stack (parent_ctx, Py) -> (parent_ctx, @SEGMENT_RETURNDATA, 32, Py)
%build_address
%jump(mstore_unpacking)
MSTORE_32BYTES_32
%jump(pop_and_return_success)
15 changes: 8 additions & 7 deletions evm/src/cpu/kernel/asm/core/precompiles/ecrec.asm
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,32 @@ global precompile_ecrec:

%charge_gas_const(@ECREC_GAS)

// Load hash, v, r, s from the call data using `mload_packing`.
// Load hash, v, r, s from the call data using `MLOAD_32BYTES`.
PUSH ecrec_return
// stack: ecrec_return, kexit_info
%stack () -> (@SEGMENT_CALLDATA, 96, 32)
GET_CONTEXT
// stack: ctx, @SEGMENT_CALLDATA, 96, 32, ecrec_return, kexit_info
%build_address
%mload_packing
MLOAD_32BYTES
// stack: s, ecrec_return, kexit_info
%stack () -> (@SEGMENT_CALLDATA, 64, 32)
GET_CONTEXT
// stack: ctx, @SEGMENT_CALLDATA, 64, 32, s, ecrec_return, kexit_info
%build_address
%mload_packing
MLOAD_32BYTES
// stack: r, s, ecrec_return, kexit_info
%stack () -> (@SEGMENT_CALLDATA, 32, 32)
GET_CONTEXT
// stack: ctx, @SEGMENT_CALLDATA, 32, 32, r, s, ecrec_return, kexit_info
%build_address
%mload_packing
MLOAD_32BYTES
// stack: v, r, s, ecrec_return, kexit_info
%stack () -> (@SEGMENT_CALLDATA, 32)
GET_CONTEXT
// stack: ctx, @SEGMENT_CALLDATA, 32, v, r, s, ecrec_return, kexit_info
%build_address_no_offset
%mload_packing
MLOAD_32BYTES
// stack: hash, v, r, s, ecrec_return, kexit_info
%jump(ecrecover)
ecrec_return:
Expand All @@ -49,9 +49,10 @@ ecrec_return:
// Store the result address to the parent's return data using `mstore_unpacking`.
%mstore_parent_context_metadata(@CTX_METADATA_RETURNDATA_SIZE, 32)
%mload_context_metadata(@CTX_METADATA_PARENT_CONTEXT)
%stack (parent_ctx, address) -> (parent_ctx, @SEGMENT_RETURNDATA, address, 32, pop_and_return_success)
%stack (parent_ctx, address) -> (parent_ctx, @SEGMENT_RETURNDATA, address)
%build_address_no_offset
%jump(mstore_unpacking)
MSTORE_32BYTES_32
%jump(pop_and_return_success)

// On bad input, return empty return data but still return success.
ecrec_bad_input:
Expand Down
12 changes: 6 additions & 6 deletions evm/src/cpu/kernel/asm/core/precompiles/expmod.asm
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mload_bytes_as_limbs:
// stack: min(16, num_bytes), addr, num_bytes, retdest, total_num_limbs, len, ..limbs
DUP2
// stack: addr, min(16, num_bytes), addr, num_bytes, retdest, total_num_limbs, len, ..limbs
%mload_packing
MLOAD_32BYTES
// stack: new_limb, addr, num_bytes, retdest, total_num_limbs, len, ..limbs
%stack (new, addr, numb, ret, tot, len) -> (numb, addr, ret, tot, len, new)
// stack: num_bytes, addr, retdest, total_num_limbs, len, new_limb, ..limbs
Expand Down Expand Up @@ -113,7 +113,7 @@ calculate_l_E_prime:
PUSH @SEGMENT_CALLDATA
GET_CONTEXT
%build_address
%mload_packing
MLOAD_32BYTES
// stack: i[96 + l_B..128 + l_B], l_E, l_B, retdest
%log2_floor
// stack: log2(i[96 + l_B..128 + l_B]), l_E, l_B, retdest
Expand Down Expand Up @@ -144,7 +144,7 @@ case_le_32:
PUSH @SEGMENT_CALLDATA
GET_CONTEXT
%build_address
%mload_packing
MLOAD_32BYTES
// stack: E, retdest
%log2_floor
// stack: log2(E), retdest
Expand Down Expand Up @@ -172,21 +172,21 @@ global precompile_expmod:
GET_CONTEXT
// stack: ctx, @SEGMENT_CALLDATA, 32, kexit_info
%build_address_no_offset
%mload_packing
MLOAD_32BYTES
// stack: l_B, kexit_info

// Load l_E from i[32..64].
%stack () -> (@SEGMENT_CALLDATA, 32, 32)
GET_CONTEXT
%build_address
%mload_packing
MLOAD_32BYTES
// stack: l_E, l_B, kexit_info

// Load l_M from i[64..96].
%stack () -> (@SEGMENT_CALLDATA, 64, 32)
GET_CONTEXT
%build_address
%mload_packing
MLOAD_32BYTES
// stack: l_M, l_E, l_B, kexit_info
DUP3 ISZERO DUP2 ISZERO
MUL // AND
Expand Down
5 changes: 3 additions & 2 deletions evm/src/cpu/kernel/asm/core/precompiles/rip160.asm
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ rip160_contd:
// Store the result hash to the parent's return data using `mstore_unpacking`.
%mstore_parent_context_metadata(@CTX_METADATA_RETURNDATA_SIZE, 32)
%mload_context_metadata(@CTX_METADATA_PARENT_CONTEXT)
%stack (parent_ctx, hash) -> (parent_ctx, @SEGMENT_RETURNDATA, hash, 32, pop_and_return_success)
%stack (parent_ctx, hash) -> (parent_ctx, @SEGMENT_RETURNDATA, hash)
%build_address_no_offset
%jump(mstore_unpacking)
MSTORE_32BYTES_32
%jump(pop_and_return_success)
5 changes: 3 additions & 2 deletions evm/src/cpu/kernel/asm/core/precompiles/sha256.asm
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ sha256_contd:
// Store the result hash to the parent's return data using `mstore_unpacking`.
%mstore_parent_context_metadata(@CTX_METADATA_RETURNDATA_SIZE, 32)
%mload_context_metadata(@CTX_METADATA_PARENT_CONTEXT)
%stack (parent_ctx, hash) -> (parent_ctx, @SEGMENT_RETURNDATA, hash, 32, pop_and_return_success)
%stack (parent_ctx, hash) -> (parent_ctx, @SEGMENT_RETURNDATA, hash)
%build_address_no_offset
%jump(mstore_unpacking)
MSTORE_32BYTES_32
%jump(pop_and_return_success)
Loading

0 comments on commit 990eb34

Please sign in to comment.