Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions libevmasm/Assembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <libevmasm/JumpdestRemover.h>
#include <libevmasm/BlockDeduplicator.h>
#include <libevmasm/ConstantOptimiser.h>
#include <libevmasm/TrivialBlockRemover.h>

#include <liblangutil/CharStream.h>
#include <liblangutil/Exceptions.h>
Expand Down Expand Up @@ -933,6 +934,16 @@ std::map<u256, u256> const& Assembly::optimiseInternal(
}
}

if (_settings.runInliner && !m_eofVersion.has_value())
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am using runInliner setting here to minimize the code changes, although if this would be accepted, it would probably need its own setting flag.

{
for (auto& codeSection: m_codeSections)
{
TrivialBlockRemover trivialBlockRemover{codeSection.items};
if (trivialBlockRemover.optimise(_tagsReferencedFromOutside))
count++;
}
}

// TODO: investigate for EOF
if (_settings.runCSE && !m_eofVersion.has_value())
{
Expand Down
2 changes: 2 additions & 0 deletions libevmasm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ set(sources
SimplificationRules.cpp
SimplificationRules.h
SubAssemblyID.h
TrivialBlockRemover.cpp
TrivialBlockRemover.h
)

add_library(evmasm ${sources})
Expand Down
63 changes: 63 additions & 0 deletions libevmasm/TrivialBlockRemover.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
This file is part of solidity.

solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0

#include <libevmasm/TrivialBlockRemover.h>

#include <libevmasm/AssemblyItem.h>
#include <libevmasm/BlockDeduplicator.h>
#include <libevmasm/SemanticInformation.h>

#include <range/v3/view/drop.hpp>
#include <range/v3/view/enumerate.hpp>

bool solidity::evmasm::TrivialBlockRemover::optimise(std::set<size_t> const& _tagsReferencedFromOutside)
{
std::map<u256, u256> replacedTags;
for (auto&& [index, item]: m_items | ranges::views::enumerate | ranges::views::drop(1))
{
if (item.type() != Tag)
continue;
if (index >= m_items.size() - 2)
continue;
auto const & next = m_items[index + 1];
if (next.type() != PushTag)
continue;
if (next.data() == item.data())
continue;
auto const & nextNext = m_items[index + 2];
if (nextNext.type() != Operation)
continue;
if (nextNext.instruction() != Instruction::JUMP)
continue;
auto const & previous = m_items[index - 1];
if (previous.type() != Operation)
continue;
if (previous.instruction() != Instruction::JUMP && !SemanticInformation::terminatesControlFlow(previous))
continue;
auto const [subId, tag] = item.splitForeignPushTag();
solAssert(subId.empty(), "Sub-assembly tag used as label.");
if (_tagsReferencedFromOutside.contains(tag))
continue;
replacedTags.insert({item.data(), next.data()});
}
if (!replacedTags.empty())
{
return BlockDeduplicator::applyTagReplacement(m_items, replacedTags);
}
return false;
}
44 changes: 44 additions & 0 deletions libevmasm/TrivialBlockRemover.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
This file is part of solidity.

solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0

#pragma once

#include <libevmasm/SubAssemblyID.h>

#include <set>

namespace solidity::evmasm
{

class AssemblyItem;
using AssemblyItems = std::vector<AssemblyItem>;

class TrivialBlockRemover {
public:
explicit TrivialBlockRemover(AssemblyItems& _items): m_items(_items) {}

bool optimise(std::set<size_t> const& _tagsReferencedFromOutside);

private:
AssemblyItems& m_items;
};

}



8 changes: 4 additions & 4 deletions test/cmdlineTests/function_debug_info/output
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"function-debug-runtime": {
"@f_14": {
"entryPoint": 124,
"entryPoint": 119,
"id": 14,
"parameterSlots": 2,
"returnSlots": 1
Expand All @@ -26,12 +26,12 @@
"returnSlots": 0
},
"abi_decode_tuple_t_array$_t_uint256_$dyn_calldata_ptr": {
"entryPoint": 158,
"entryPoint": 153,
"parameterSlots": 2,
"returnSlots": 2
},
"abi_decode_tuple_t_uint256": {
"entryPoint": 271,
"entryPoint": 266,
"parameterSlots": 2,
"returnSlots": 1
},
Expand All @@ -40,7 +40,7 @@
"returnSlots": 1
},
"panic_error_0x32": {
"entryPoint": 294,
"entryPoint": 289,
"parameterSlots": 0,
"returnSlots": 0
}
Expand Down
6 changes: 3 additions & 3 deletions test/libsolidity/gasTests/abiv2_optimised.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ contract C {
// optimize-yul: true
// ----
// creation:
// codeDepositCost: 618200
// codeDepositCost: 615200
// executionCost: 649
// totalCost: 618849
// totalCost: 615849
// external:
// a(): 2283
// b(uint256): 4649
// b(uint256): 4637
// f1(uint256): 304
// f2(uint256[],string[],uint16,address): infinite
// f3(uint16[],string[],uint16,address): infinite
Expand Down
48 changes: 24 additions & 24 deletions test/libsolidity/gasTests/dispatch_large_optimised.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,29 @@ contract Large {
// optimize-runs: 2
// ----
// creation:
// codeDepositCost: 224600
// executionCost: 267
// totalCost: 224867
// codeDepositCost: 213600
// executionCost: 255
// totalCost: 213855
// external:
// a(): 2281
// b(uint256): 4934
// f0(uint256): 363
// f1(uint256): 47002
// f2(uint256): 24967
// f3(uint256): 25055
// f4(uint256): 25033
// f5(uint256): 25011
// f6(uint256): 24923
// f7(uint256): 24703
// f8(uint256): 24835
// f9(uint256): 24857
// g0(uint256): 603
// g1(uint256): 46714
// g2(uint256): 24701
// g3(uint256): 24789
// g4(uint256): 24767
// g5(uint256): 24855
// g6(uint256): 24635
// g7(uint256): 24745
// g8(uint256): 24723
// g9(uint256): 24569
// b(uint256): 4922
// f0(uint256): 351
// f1(uint256): 46990
// f2(uint256): 24955
// f3(uint256): 25043
// f4(uint256): 25021
// f5(uint256): 24999
// f6(uint256): 24911
// f7(uint256): 24691
// f8(uint256): 24823
// f9(uint256): 24845
// g0(uint256): 591
// g1(uint256): 46702
// g2(uint256): 24689
// g3(uint256): 24777
// g4(uint256): 24755
// g5(uint256): 24843
// g6(uint256): 24623
// g7(uint256): 24733
// g8(uint256): 24711
// g9(uint256): 24557
23 changes: 11 additions & 12 deletions test/libsolidity/gasTests/dispatch_medium_optimised.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,20 @@ contract Medium {
// ====
// EVMVersion: =current
// bytecodeFormat: legacy
// ====
// optimize: true
// optimize-runs: 2
// ----
// creation:
// codeDepositCost: 126000
// executionCost: 169
// totalCost: 126169
// codeDepositCost: 118000
// executionCost: 163
// totalCost: 118163
// external:
// a(): 2281
// b(uint256): 4692
// f1(uint256): 46782
// f2(uint256): 24725
// f3(uint256): 24769
// g0(uint256): 361
// g7(uint256): 24635
// g8(uint256): 24613
// g9(uint256): 24569
// b(uint256): 4680
// f1(uint256): 46770
// f2(uint256): 24713
// f3(uint256): 24757
// g0(uint256): 349
// g7(uint256): 24623
// g8(uint256): 24601
// g9(uint256): 24557
12 changes: 6 additions & 6 deletions test/libsolidity/gasTests/dispatch_small_optimised.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ contract Small {
}
// ====
// EVMVersion: =current
// bytecodeFormat: legacy
// optimize: true
// optimize-runs: 2
// bytecodeFormat: legacy
// ----
// creation:
// codeDepositCost: 58200
// executionCost: 109
// totalCost: 58309
// codeDepositCost: 56200
// executionCost: 103
// totalCost: 56303
// external:
// fallback: 117
// a(): 2259
// b(uint256): 4582
// f1(uint256): 46716
// b(uint256): 4570
// f1(uint256): 46704
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ contract C {
// f(bytes): 0x20, 0x80, 0x21, 0x40, 0x7, "abcdefg" -> 0x21, 0x40, 0x7, "abcdefg"
// gas irOptimized: 135441
// gas legacy: 137095
// gas legacyOptimized: 135828
// gas legacyOptimized: 135792
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ contract C {
// EVMVersion: >homestead
// ----
// test_bytes() ->
// gas irOptimized: 314884
// gas irOptimized: 314668
// gas legacy: 305816
// gas legacyOptimized: 253573
// gas legacyOptimized: 252925
// test_uint256() ->
// gas irOptimized: 448346
// gas irOptimized: 447698
// gas legacy: 421304
// gas legacyOptimized: 351544
// gas legacyOptimized: 350896
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ contract C {
// f() -> 8, 7, 1, 2, 7, 12
// gas irOptimized: 166766
// gas legacy: 170486
// gas legacyOptimized: 167252
// gas legacyOptimized: 167240
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ contract C {
// EVMVersion: >homestead
// ----
// test_bytes() ->
// gas irOptimized: 314884
// gas irOptimized: 314668
// gas legacy: 305816
// gas legacyOptimized: 253573
// gas legacyOptimized: 252925
// test_uint256() ->
// gas irOptimized: 448346
// gas irOptimized: 447698
// gas legacy: 421304
// gas legacyOptimized: 351544
// gas legacyOptimized: 350896
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ contract C {
// f(uint256[][1]): 32, 32, 8, 421, 422, 423, 424, 425, 426, 427, 428 -> true
// gas irOptimized: 120978
// gas legacy: 98324
// gas legacyOptimized: 94348
// gas legacyOptimized: 94312
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ contract C {
// f_storage(uint256[],uint256[2]): 0x20, 1, 2 -> 0x20, 0x60, 0x20, 1, 2
// gas irOptimized: 111356
// gas legacy: 113826
// gas legacyOptimized: 111724
// gas legacyOptimized: 111712
// f_storage(uint256[],uint256[2]): 0x40, 1, 2, 5, 6 -> 0x20, 0x80, 0x20, 2, 5, 6
// f_storage(uint256[],uint256[2]): 0x40, 1, 2, 5 -> FAILURE
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ contract C {
// h(uint256[2][]): 0x20, 3, 123, 124, 223, 224, 323, 324 -> 32, 256, 0x20, 3, 123, 124, 223, 224, 323, 324
// gas irOptimized: 180022
// gas legacy: 186541
// gas legacyOptimized: 180429
// gas legacyOptimized: 180417
// i(uint256[2][2]): 123, 124, 223, 224 -> 32, 128, 123, 124, 223, 224
// gas irOptimized: 112031
// gas legacy: 116683
// gas legacyOptimized: 112303
// gas legacyOptimized: 112291
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ contract C {
// index(uint256): 0xFF -> true
// gas irOptimized: 108272
// gas legacy: 181536
// gas legacyOptimized: 117442
// gas legacyOptimized: 117430
// accessIndex(uint256,int256): 10, 1 -> 2
// accessIndex(uint256,int256): 10, 0 -> 1
// accessIndex(uint256,int256): 10, 11 -> FAILURE, hex"4e487b71", 0x32
Expand Down
Loading