Skip to content

Commit

Permalink
Use MCOPY when copying byte arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
nikola-matic committed Feb 7, 2024
1 parent c7db606 commit 6644a74
Show file tree
Hide file tree
Showing 10 changed files with 453 additions and 15 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Language Features:


Compiler Features:
* Code Generator: Use ``MCOPY`` instead of ``MLOAD``/``MSTORE`` loop when copying byte arrays.


Bugfixes:
Expand Down
39 changes: 27 additions & 12 deletions libsolidity/codegen/YulUtilFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ std::string YulUtilFunctions::copyToMemoryFunction(bool _fromCalldata, bool _cle
(_cleanup ? "_with_cleanup"s : ""s);

return m_functionCollector.createFunction(functionName, [&]() {
// calldata -> memory
if (_fromCalldata)
{
return Whiskers(R"(
Expand All @@ -101,21 +102,35 @@ std::string YulUtilFunctions::copyToMemoryFunction(bool _fromCalldata, bool _cle
("cleanup", _cleanup)
.render();
}
// memory -> memory
else
{
return Whiskers(R"(
function <functionName>(src, dst, length) {
let i := 0
for { } lt(i, length) { i := add(i, 32) }
{
mstore(add(dst, i), mload(add(src, i)))
// use MCOPY (cancun+)
if (m_evmVersion.hasMcopy())
return Whiskers(R"(
function <functionName>(src, dst, length) {
mcopy(dst, src, length)
<?cleanup>mstore(add(dst, length), 0)</cleanup>
}
<?cleanup>mstore(add(dst, length), 0)</cleanup>
}
)")
("functionName", functionName)
("cleanup", _cleanup)
.render();
)")
("functionName", functionName)
("cleanup", _cleanup)
.render();
// mstore mload loop
else
return Whiskers(R"(
function <functionName>(src, dst, length) {
let i := 0
for { } lt(i, length) { i := add(i, 32) }
{
mstore(add(dst, i), mload(add(src, i)))
}
<?cleanup>mstore(add(dst, length), 0)</cleanup>
}
)")
("functionName", functionName)
("cleanup", _cleanup)
.render();
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--evm-version cancun --no-cbor-metadata --via-ir --optimize --ir --debug-info none
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.0.0;

contract C {
function foo() external pure returns (bytes memory)
{
bytes memory ret = "aaaaa";
return ret;
}
}
203 changes: 203 additions & 0 deletions test/cmdlineTests/mcopy_bytes_array_returned_from_function/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
IR:

/// @use-src 0:"mcopy_bytes_array_returned_from_function/input.sol"
object "C_14" {
code {

mstore(64, memoryguard(128))
if callvalue() { revert_error_ca66f745a3ce8ff40e2ccaf1ad45db7774001b90d25810abd9040049be7bf4bb() }

constructor_C_14()

let _1 := allocate_unbounded()
codecopy(_1, dataoffset("C_14_deployed"), datasize("C_14_deployed"))

return(_1, datasize("C_14_deployed"))

function allocate_unbounded() -> memPtr {
memPtr := mload(64)
}

function revert_error_ca66f745a3ce8ff40e2ccaf1ad45db7774001b90d25810abd9040049be7bf4bb() {
revert(0, 0)
}

function constructor_C_14() {

}

}
/// @use-src 0:"mcopy_bytes_array_returned_from_function/input.sol"
object "C_14_deployed" {
code {

mstore(64, memoryguard(128))

if iszero(lt(calldatasize(), 4))
{
let selector := shift_right_224_unsigned(calldataload(0))
switch selector

case 0xc2985578
{
// foo()

external_fun_foo_13()
}

default {}
}

revert_error_42b3090547df1d2001c96683413b8cf91c1b902ef5e3cb8d9f6f304cf7446f74()

function shift_right_224_unsigned(value) -> newValue {
newValue :=

shr(224, value)

}

function allocate_unbounded() -> memPtr {
memPtr := mload(64)
}

function revert_error_ca66f745a3ce8ff40e2ccaf1ad45db7774001b90d25810abd9040049be7bf4bb() {
revert(0, 0)
}

function revert_error_dbdddcbe895c83990c08b3492a0e83918d802a52331272ac6fdb6a7c4aea3b1b() {
revert(0, 0)
}

function abi_decode_tuple_(headStart, dataEnd) {
if slt(sub(dataEnd, headStart), 0) { revert_error_dbdddcbe895c83990c08b3492a0e83918d802a52331272ac6fdb6a7c4aea3b1b() }

}

function array_length_t_bytes_memory_ptr(value) -> length {

length := mload(value)

}

function array_storeLengthForEncoding_t_bytes_memory_ptr_fromStack(pos, length) -> updated_pos {
mstore(pos, length)
updated_pos := add(pos, 0x20)
}

function copy_memory_to_memory_with_cleanup(src, dst, length) {
mcopy(dst, src, length)
mstore(add(dst, length), 0)
}

function round_up_to_mul_of_32(value) -> result {
result := and(add(value, 31), not(31))
}

function abi_encode_t_bytes_memory_ptr_to_t_bytes_memory_ptr_fromStack(value, pos) -> end {
let length := array_length_t_bytes_memory_ptr(value)
pos := array_storeLengthForEncoding_t_bytes_memory_ptr_fromStack(pos, length)
copy_memory_to_memory_with_cleanup(add(value, 0x20), pos, length)
end := add(pos, round_up_to_mul_of_32(length))
}

function abi_encode_tuple_t_bytes_memory_ptr__to_t_bytes_memory_ptr__fromStack(headStart , value0) -> tail {
tail := add(headStart, 32)

mstore(add(headStart, 0), sub(tail, headStart))
tail := abi_encode_t_bytes_memory_ptr_to_t_bytes_memory_ptr_fromStack(value0, tail)

}

function external_fun_foo_13() {

if callvalue() { revert_error_ca66f745a3ce8ff40e2ccaf1ad45db7774001b90d25810abd9040049be7bf4bb() }
abi_decode_tuple_(4, calldatasize())
let ret_0 := fun_foo_13()
let memPos := allocate_unbounded()
let memEnd := abi_encode_tuple_t_bytes_memory_ptr__to_t_bytes_memory_ptr__fromStack(memPos , ret_0)
return(memPos, sub(memEnd, memPos))

}

function revert_error_42b3090547df1d2001c96683413b8cf91c1b902ef5e3cb8d9f6f304cf7446f74() {
revert(0, 0)
}

function zero_value_for_split_t_bytes_memory_ptr() -> ret {
ret := 96
}

function panic_error_0x41() {
mstore(0, 35408467139433450592217433187231851964531694900788300625387963629091585785856)
mstore(4, 0x41)
revert(0, 0x24)
}

function finalize_allocation(memPtr, size) {
let newFreePtr := add(memPtr, round_up_to_mul_of_32(size))
// protect against overflow
if or(gt(newFreePtr, 0xffffffffffffffff), lt(newFreePtr, memPtr)) { panic_error_0x41() }
mstore(64, newFreePtr)
}

function allocate_memory(size) -> memPtr {
memPtr := allocate_unbounded()
finalize_allocation(memPtr, size)
}

function array_allocation_size_t_string_memory_ptr(length) -> size {
// Make sure we can allocate memory without overflow
if gt(length, 0xffffffffffffffff) { panic_error_0x41() }

size := round_up_to_mul_of_32(length)

// add length slot
size := add(size, 0x20)

}

function allocate_memory_array_t_string_memory_ptr(length) -> memPtr {
let allocSize := array_allocation_size_t_string_memory_ptr(length)
memPtr := allocate_memory(allocSize)

mstore(memPtr, length)

}

function store_literal_in_memory_5aff159202c5d82ecfd14de49cc2c8c1f74964a25d78c549137c4ce714030fe7(memPtr) {

mstore(add(memPtr, 0), "aaaaa")

}

function copy_literal_to_memory_5aff159202c5d82ecfd14de49cc2c8c1f74964a25d78c549137c4ce714030fe7() -> memPtr {
memPtr := allocate_memory_array_t_string_memory_ptr(5)
store_literal_in_memory_5aff159202c5d82ecfd14de49cc2c8c1f74964a25d78c549137c4ce714030fe7(add(memPtr, 32))
}

function convert_t_stringliteral_5aff159202c5d82ecfd14de49cc2c8c1f74964a25d78c549137c4ce714030fe7_to_t_bytes_memory_ptr() -> converted {
converted := copy_literal_to_memory_5aff159202c5d82ecfd14de49cc2c8c1f74964a25d78c549137c4ce714030fe7()
}

function fun_foo_13() -> var__4_mpos {

let zero_t_bytes_memory_ptr_1_mpos := zero_value_for_split_t_bytes_memory_ptr()
var__4_mpos := zero_t_bytes_memory_ptr_1_mpos

let var_ret_7_mpos := convert_t_stringliteral_5aff159202c5d82ecfd14de49cc2c8c1f74964a25d78c549137c4ce714030fe7_to_t_bytes_memory_ptr()

let _2_mpos := var_ret_7_mpos
let expr_10_mpos := _2_mpos

var__4_mpos := expr_10_mpos
leave

}

}

data ".metadata" hex""
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--evm-version cancun --no-cbor-metadata --via-ir --optimize --ir --debug-info none
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.0.0;

contract C {
function foo() external pure returns (string memory)
{
return "MCOPY on Cancun vacation.";
}
}
Loading

0 comments on commit 6644a74

Please sign in to comment.