Merge pull request #11026 from ethereum/resolve-keccak

Evaluate ``keccak256(a, const)``, when the value at memory location ``a`` is known at compile time and ``const <= 32``
This commit is contained in:
Harikrishnan Mulackal 2021-04-22 19:18:19 +02:00 committed by GitHub
commit f162c484ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
115 changed files with 610 additions and 136 deletions

View File

@ -4,6 +4,7 @@ Language Features:
Compiler Features:
* Yul Optimizer: Evaluate ``keccak256(a, c)``, when the value at memory location ``a`` is known at compile time and ``c`` is a constant ``<= 32``.
Bugfixes:

View File

@ -548,6 +548,7 @@ void CompilerContext::optimizeYul(yul::Object& _object, yul::EVMDialect const& _
_object,
_optimiserSettings.optimizeStackAllocation,
_optimiserSettings.yulOptimiserSteps,
isCreation? nullopt : make_optional(_optimiserSettings.expectedExecutionsPerDeployment),
_externalIdentifiers
);

View File

@ -44,6 +44,7 @@
#include <libevmasm/Assembly.h>
#include <liblangutil/Scanner.h>
#include <optional>
using namespace std;
using namespace solidity;
@ -185,7 +186,9 @@ void AssemblyStack::optimize(Object& _object, bool _isCreation)
meter.get(),
_object,
m_optimiserSettings.optimizeStackAllocation,
m_optimiserSettings.yulOptimiserSteps
m_optimiserSettings.yulOptimiserSteps,
_isCreation ? nullopt : make_optional(m_optimiserSettings.expectedExecutionsPerDeployment),
{}
);
}

View File

@ -79,6 +79,7 @@ struct Dialect: boost::noncopyable
virtual BuiltinFunction const* memoryLoadFunction(YulString /* _type */) const { return nullptr; }
virtual BuiltinFunction const* storageStoreFunction(YulString /* _type */) const { return nullptr; }
virtual BuiltinFunction const* storageLoadFunction(YulString /* _type */) const { return nullptr; }
virtual YulString hashFunction(YulString /* _type */ ) const { return YulString{}; }
/// Check whether the given type is legal for the given literal value.
/// Should only be called if the type exists in the dialect at all.

View File

@ -58,7 +58,7 @@ public:
struct Representation
{
std::unique_ptr<Expression> expression;
size_t cost = size_t(-1);
bigint cost;
};
private:

View File

@ -81,6 +81,7 @@ struct EVMDialect: public Dialect
BuiltinFunctionForEVM const* memoryLoadFunction(YulString /*_type*/) const override { return builtin("mload"_yulstring); }
BuiltinFunctionForEVM const* storageStoreFunction(YulString /*_type*/) const override { return builtin("sstore"_yulstring); }
BuiltinFunctionForEVM const* storageLoadFunction(YulString /*_type*/) const override { return builtin("sload"_yulstring); }
YulString hashFunction(YulString /*_type*/) const override { return "keccak256"_yulstring; }
static EVMDialect const& strictAssemblyForEVM(langutil::EVMVersion _version);
static EVMDialect const& strictAssemblyForEVMObjects(langutil::EVMVersion _version);

View File

@ -37,23 +37,23 @@ using namespace solidity;
using namespace solidity::yul;
using namespace solidity::util;
size_t GasMeter::costs(Expression const& _expression) const
bigint GasMeter::costs(Expression const& _expression) const
{
return combineCosts(GasMeterVisitor::costs(_expression, m_dialect, m_isCreation));
}
size_t GasMeter::instructionCosts(evmasm::Instruction _instruction) const
bigint GasMeter::instructionCosts(evmasm::Instruction _instruction) const
{
return combineCosts(GasMeterVisitor::instructionCosts(_instruction, m_dialect, m_isCreation));
}
size_t GasMeter::combineCosts(std::pair<size_t, size_t> _costs) const
bigint GasMeter::combineCosts(std::pair<bigint, bigint> _costs) const
{
return _costs.first * m_runs + _costs.second;
}
pair<size_t, size_t> GasMeterVisitor::costs(
pair<bigint, bigint> GasMeterVisitor::costs(
Expression const& _expression,
EVMDialect const& _dialect,
bool _isCreation
@ -64,7 +64,7 @@ pair<size_t, size_t> GasMeterVisitor::costs(
return {gmv.m_runGas, gmv.m_dataGas};
}
pair<size_t, size_t> GasMeterVisitor::instructionCosts(
pair<bigint, bigint> GasMeterVisitor::instructionCosts(
evmasm::Instruction _instruction,
EVMDialect const& _dialect,
bool _isCreation
@ -92,11 +92,11 @@ void GasMeterVisitor::operator()(Literal const& _lit)
m_runGas += evmasm::GasMeter::runGas(evmasm::Instruction::PUSH1);
m_dataGas +=
singleByteDataGas() +
static_cast<size_t>(evmasm::GasMeter::dataGas(
evmasm::GasMeter::dataGas(
toCompactBigEndian(valueOfLiteral(_lit), 1),
m_isCreation,
m_dialect.evmVersion()
));
);
}
void GasMeterVisitor::operator()(Identifier const&)
@ -105,7 +105,7 @@ void GasMeterVisitor::operator()(Identifier const&)
m_dataGas += singleByteDataGas();
}
size_t GasMeterVisitor::singleByteDataGas() const
bigint GasMeterVisitor::singleByteDataGas() const
{
if (m_isCreation)
return evmasm::GasCosts::txDataNonZeroGas(m_dialect.evmVersion());
@ -117,6 +117,9 @@ void GasMeterVisitor::instructionCostsInternal(evmasm::Instruction _instruction)
{
if (_instruction == evmasm::Instruction::EXP)
m_runGas += evmasm::GasCosts::expGas + evmasm::GasCosts::expByteGas(m_dialect.evmVersion());
else if (_instruction == evmasm::Instruction::KECCAK256)
// Assumes that Keccak-256 is computed on a single word (rounded up).
m_runGas += evmasm::GasCosts::keccak256Gas + evmasm::GasCosts::keccak256WordGas;
else
m_runGas += evmasm::GasMeter::runGas(_instruction);
m_dataGas += singleByteDataGas();

View File

@ -36,40 +36,42 @@ struct EVMDialect;
*
* Assumes that EXP is not used with exponents larger than a single byte.
* Is not particularly exact for anything apart from arithmetic.
*
* Assumes that Keccak-256 is computed on a single word (rounded up).
*/
class GasMeter
{
public:
GasMeter(EVMDialect const& _dialect, bool _isCreation, size_t _runs):
GasMeter(EVMDialect const& _dialect, bool _isCreation, bigint _runs):
m_dialect(_dialect),
m_isCreation{_isCreation},
m_runs(_isCreation? 1 : _runs)
{}
/// @returns the full combined costs of deploying and evaluating the expression.
size_t costs(Expression const& _expression) const;
bigint costs(Expression const& _expression) const;
/// @returns the combined costs of deploying and running the instruction, not including
/// the costs for its arguments.
size_t instructionCosts(evmasm::Instruction _instruction) const;
bigint instructionCosts(evmasm::Instruction _instruction) const;
private:
size_t combineCosts(std::pair<size_t, size_t> _costs) const;
bigint combineCosts(std::pair<bigint, bigint> _costs) const;
EVMDialect const& m_dialect;
bool m_isCreation = false;
size_t m_runs;
bigint m_runs;
};
class GasMeterVisitor: public ASTWalker
{
public:
static std::pair<size_t, size_t> costs(
static std::pair<bigint, bigint> costs(
Expression const& _expression,
EVMDialect const& _dialect,
bool _isCreation
);
static std::pair<size_t, size_t> instructionCosts(
static std::pair<bigint, bigint> instructionCosts(
evmasm::Instruction _instruction,
EVMDialect const& _dialect,
bool _isCreation = false
@ -86,7 +88,7 @@ public:
void operator()(Identifier const& _identifier) override;
private:
size_t singleByteDataGas() const;
bigint singleByteDataGas() const;
/// Computes the cost of storing and executing the single instruction (excluding its arguments).
/// For EXP, it assumes that the exponent is at most 255.
/// Does not work particularly exact for anything apart from arithmetic.
@ -94,8 +96,8 @@ private:
EVMDialect const& m_dialect;
bool m_isCreation = false;
size_t m_runGas = 0;
size_t m_dataGas = 0;
bigint m_runGas = 0;
bigint m_dataGas = 0;
};
}

View File

@ -42,6 +42,8 @@
#include <liblangutil/Scanner.h>
#include <liblangutil/SourceReferenceFormatter.h>
#include <libsolidity/interface/OptimiserSettings.h>
// The following headers are generated from the
// yul files placed in libyul/backends/wasm/polyfill.
@ -68,7 +70,13 @@ Object EVMToEwasmTranslator::run(Object const& _object)
Block ast = std::get<Block>(Disambiguator(m_dialect, *_object.analysisInfo)(*_object.code));
set<YulString> reservedIdentifiers;
NameDispenser nameDispenser{m_dialect, ast, reservedIdentifiers};
OptimiserStepContext context{m_dialect, nameDispenser, reservedIdentifiers};
// expectedExecutionsPerDeployment is currently unused.
OptimiserStepContext context{
m_dialect,
nameDispenser,
reservedIdentifiers,
frontend::OptimiserSettings::standard().expectedExecutionsPerDeployment
};
FunctionHoister::run(context, ast);
FunctionGrouper::run(context, ast);

View File

@ -27,6 +27,7 @@
#include <libyul/AST.h>
#include <libyul/Dialect.h>
#include <libyul/Exceptions.h>
#include <libyul/Utilities.h>
#include <libsolutil/CommonData.h>
#include <libsolutil/cxx20.h>
@ -388,6 +389,14 @@ bool DataFlowAnalyzer::inScope(YulString _variableName) const
return false;
}
optional<u256> DataFlowAnalyzer::valueOfIdentifier(YulString const& _name)
{
if (m_value.count(_name))
if (Literal const* literal = get_if<Literal>(m_value.at(_name).value))
return valueOfLiteral(*literal);
return nullopt;
}
std::optional<pair<YulString, YulString>> DataFlowAnalyzer::isSimpleStore(
StoreLoadLocation _location,
ExpressionStatement const& _statement
@ -412,4 +421,3 @@ std::optional<YulString> DataFlowAnalyzer::isSimpleLoad(
return key->name;
return {};
}

View File

@ -29,6 +29,8 @@
#include <libyul/AST.h> // Needed for m_zero below.
#include <libyul/SideEffects.h>
#include <libsolutil/Common.h>
#include <map>
#include <set>
@ -134,6 +136,9 @@ protected:
/// Returns true iff the variable is in scope.
bool inScope(YulString _variableName) const;
/// Returns the literal value of the identifier, if it exists.
std::optional<u256> valueOfIdentifier(YulString const& _name);
enum class StoreLoadLocation {
Memory = 0,
Storage = 1,

View File

@ -23,13 +23,23 @@
#include <libyul/optimiser/LoadResolver.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <libyul/backends/evm/EVMMetrics.h>
#include <libyul/optimiser/Semantics.h>
#include <libyul/optimiser/CallGraphGenerator.h>
#include <libyul/SideEffects.h>
#include <libyul/AST.h>
#include <libyul/Utilities.h>
#include <libevmasm/Instruction.h>
#include <libevmasm/GasMeter.h>
#include <libsolutil/Keccak256.h>
#include <limits>
using namespace std;
using namespace solidity;
using namespace solidity::util;
using namespace solidity::evmasm;
using namespace solidity::yul;
void LoadResolver::run(OptimiserStepContext& _context, Block& _ast)
@ -38,7 +48,8 @@ void LoadResolver::run(OptimiserStepContext& _context, Block& _ast)
LoadResolver{
_context.dialect,
SideEffectsPropagator::sideEffects(_context.dialect, CallGraphGenerator::callGraph(_ast)),
!containsMSize
containsMSize,
_context.expectedExecutionsPerDeployment
}(_ast);
}
@ -47,12 +58,17 @@ void LoadResolver::visit(Expression& _e)
DataFlowAnalyzer::visit(_e);
if (FunctionCall const* funCall = std::get_if<FunctionCall>(&_e))
{
for (auto location: { StoreLoadLocation::Memory, StoreLoadLocation::Storage })
if (funCall->functionName.name == m_loadFunctionName[static_cast<unsigned>(location)])
{
tryResolve(_e, location, funCall->arguments);
break;
}
if (!m_containsMSize && funCall->functionName.name == m_dialect.hashFunction({}))
tryEvaluateKeccak(_e, funCall->arguments);
}
}
void LoadResolver::tryResolve(
@ -71,8 +87,64 @@ void LoadResolver::tryResolve(
if (inScope(*value))
_e = Identifier{locationOf(_e), *value};
}
else if (m_optimizeMLoad && _location == StoreLoadLocation::Memory)
else if (!m_containsMSize && _location == StoreLoadLocation::Memory)
if (auto value = util::valueOrNullptr(m_memory, key))
if (inScope(*value))
_e = Identifier{locationOf(_e), *value};
}
void LoadResolver::tryEvaluateKeccak(
Expression& _e,
std::vector<Expression> const& _arguments
)
{
// The costs are only correct for hashes of 32 bytes or 1 word (when rounded up).
GasMeter gasMeter{
dynamic_cast<EVMDialect const&>(m_dialect),
!m_expectedExecutionsPerDeployment,
m_expectedExecutionsPerDeployment ? *m_expectedExecutionsPerDeployment : 1
};
bigint costOfKeccak = gasMeter.costs(_e);
bigint costOfLiteral = gasMeter.costs(
Literal{
{},
LiteralKind::Number,
// a dummy 256-bit number to represent the Keccak256 hash.
YulString{numeric_limits<u256>::max().str()},
{}
}
);
// We skip if there are no net gas savings.
// Note that for default `m_runs = 200`, the values are
// `costOfLiteral = 7200` and `costOfKeccak = 9000` for runtime context.
// For creation context: `costOfLiteral = 531` and `costOfKeccak = 90`.
if (costOfLiteral > costOfKeccak)
return;
yulAssert(_arguments.size() == 2, "");
Identifier const* memoryKey = std::get_if<Identifier>(&_arguments.at(0));
Identifier const* length = std::get_if<Identifier>(&_arguments.at(1));
if (!memoryKey || !length)
return;
auto memoryValue = util::valueOrNullptr(m_memory, memoryKey->name);
if (memoryValue && inScope(*memoryValue))
{
optional<u256> memoryContent = valueOfIdentifier(*memoryValue);
optional<u256> byteLength = valueOfIdentifier(length->name);
if (memoryContent && byteLength && *byteLength <= 32)
{
bytes contentAsBytes = toBigEndian(*memoryContent);
contentAsBytes.resize(static_cast<size_t>(*byteLength));
_e = Literal{
locationOf(_e),
LiteralKind::Number,
YulString{u256(keccak256(contentAsBytes)).str()},
m_dialect.defaultType
};
}
}
}

View File

@ -32,6 +32,9 @@ namespace solidity::yul
* Optimisation stage that replaces expressions of type ``sload(x)`` and ``mload(x)`` by the value
* currently stored in storage resp. memory, if known.
*
* Also evaluates simple ``keccak256(a, c)`` when the value at memory location `a` is known and `c`
* is a constant `<= 32`.
*
* Works best if the code is in SSA form.
*
* Prerequisite: Disambiguator, ForLoopInitRewriter.
@ -47,10 +50,12 @@ private:
LoadResolver(
Dialect const& _dialect,
std::map<YulString, SideEffects> _functionSideEffects,
bool _optimizeMLoad
bool _containsMSize,
std::optional<size_t> _expectedExecutionsPerDeployment
):
DataFlowAnalyzer(_dialect, std::move(_functionSideEffects)),
m_optimizeMLoad(_optimizeMLoad)
m_containsMSize(_containsMSize),
m_expectedExecutionsPerDeployment(std::move(_expectedExecutionsPerDeployment))
{}
protected:
@ -63,7 +68,17 @@ protected:
std::vector<Expression> const& _arguments
);
bool m_optimizeMLoad = false;
/// Evaluates simple ``keccak256(a, c)`` when the value at memory location ``a`` is known and
/// `c` is a constant `<= 32`.
void tryEvaluateKeccak(
Expression& _e,
std::vector<Expression> const& _arguments
);
/// If the AST contains `msize`, then we skip resolving `mload` and `keccak256`.
bool m_containsMSize = false;
/// The --optimize-runs parameter. Value `nullopt` represents creation code.
std::optional<size_t> m_expectedExecutionsPerDeployment;
};
}

View File

@ -37,6 +37,8 @@ struct OptimiserStepContext
Dialect const& dialect;
NameDispenser& dispenser;
std::set<YulString> const& reservedIdentifiers;
/// The value nullopt represents creation code
std::optional<size_t> expectedExecutionsPerDeployment;
};

View File

@ -88,6 +88,7 @@ void OptimiserSuite::run(
Object& _object,
bool _optimizeStackAllocation,
string const& _optimisationSequence,
optional<size_t> _expectedExecutionsPerDeployment,
set<YulString> const& _externallyUsedIdentifiers
)
{
@ -101,7 +102,7 @@ void OptimiserSuite::run(
)(*_object.code));
Block& ast = *_object.code;
OptimiserSuite suite(_dialect, reservedIdentifiers, Debug::None, ast);
OptimiserSuite suite(_dialect, reservedIdentifiers, Debug::None, ast, _expectedExecutionsPerDeployment);
// Some steps depend on properties ensured by FunctionHoister, BlockFlattener, FunctionGrouper and
// ForLoopInitRewriter. Run them first to be able to run arbitrary sequences safely.

View File

@ -58,12 +58,14 @@ public:
PrintStep,
PrintChanges
};
/// The value nullopt for `_expectedExecutionsPerDeployment` represents creation code.
static void run(
Dialect const& _dialect,
GasMeter const* _meter,
Object& _object,
bool _optimizeStackAllocation,
std::string const& _optimisationSequence,
std::optional<size_t> _expectedExecutionsPerDeployment,
std::set<YulString> const& _externallyUsedIdentifiers = {}
);
@ -88,10 +90,11 @@ private:
Dialect const& _dialect,
std::set<YulString> const& _externallyUsedIdentifiers,
Debug _debug,
Block& _ast
Block& _ast,
std::optional<size_t> expectedExecutionsPerDeployment
):
m_dispenser{_dialect, _ast, _externallyUsedIdentifiers},
m_context{_dialect, m_dispenser, _externallyUsedIdentifiers},
m_context{_dialect, m_dispenser, _externallyUsedIdentifiers, expectedExecutionsPerDeployment},
m_debug(_debug)
{}

View File

@ -0,0 +1 @@
--ir-optimized --optimize --optimize-runs 50000

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@
0

View File

@ -0,0 +1,21 @@
// SPDX-License-Identifier: GPL-v3
pragma solidity >= 0.0.0;
contract C {
constructor() {
assembly {
mstore(0, 100)
// because this is part of deploy code, the keccak will not be evaluated
sstore(0, keccak256(0, 32))
}
}
fallback() external {
assembly {
mstore(0, 100)
// The keccak here would be evaluated
sstore(0, keccak256(0, 32))
}
}
}

View File

@ -0,0 +1,32 @@
Optimized IR:
/*******************************************************
* WARNING *
* Solidity to Yul compilation is still EXPERIMENTAL *
* It can result in LOSS OF FUNDS or worse *
* !USE AT YOUR OWN RISK! *
*******************************************************/
object "C_12" {
code {
{
mstore(64, 128)
if callvalue() { revert(0, 0) }
mstore(0, 100)
sstore(0, keccak256(0, 32))
let _1 := datasize("C_12_deployed")
codecopy(0, dataoffset("C_12_deployed"), _1)
return(0, _1)
}
}
object "C_12_deployed" {
code {
{
mstore(64, 128)
if callvalue() { revert(0, 0) }
mstore(0, 100)
sstore(0, 17385872270140913825666367956517731270094621555228275961425792378517567244498)
stop()
}
}
}
}

View File

@ -0,0 +1 @@
--ir-optimized --optimize --optimize-runs 1

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@
0

View File

@ -0,0 +1,12 @@
// SPDX-License-Identifier: GPL-v3
pragma solidity >= 0.0.0;
contract C {
fallback() external {
assembly {
mstore(0, 100)
// because of the low runs value, the constant will not be optimized
sstore(0, keccak256(0, 32))
}
}
}

View File

@ -0,0 +1,30 @@
Optimized IR:
/*******************************************************
* WARNING *
* Solidity to Yul compilation is still EXPERIMENTAL *
* It can result in LOSS OF FUNDS or worse *
* !USE AT YOUR OWN RISK! *
*******************************************************/
object "C_7" {
code {
{
mstore(64, 128)
if callvalue() { revert(0, 0) }
let _1 := datasize("C_7_deployed")
codecopy(0, dataoffset("C_7_deployed"), _1)
return(0, _1)
}
}
object "C_7_deployed" {
code {
{
mstore(64, 128)
if callvalue() { revert(0, 0) }
mstore(0, 100)
sstore(0, keccak256(0, 32))
stop()
}
}
}
}

View File

@ -38,7 +38,7 @@ object "Arraysum_34" {
}
{
mstore(_1, _1)
let _3 := sload(add(keccak256(_1, 0x20), var_i))
let _3 := sload(add(18569430475105882587588266137607568536673111973893317399460219858819262702947, var_i))
if gt(var_sum, not(_3)) { panic_error_0x11() }
var_sum := add(var_sum, _3)
}

View File

@ -24,6 +24,6 @@ contract C {
// compileViaYul: also
// ----
// f() -> 0x20, 0x8, 0x40, 0x3, 0x9, 0xa, 0xb
// gas irOptimized: 193626
// gas irOptimized: 193543
// gas legacy: 196426
// gas legacyOptimized: 193405

View File

@ -26,6 +26,6 @@ contract C {
// ----
// library: L
// f() -> 8, 7, 1, 2, 7, 12
// gas irOptimized: 165112
// gas irOptimized: 164899
// gas legacy: 164775
// gas legacyOptimized: 162697

View File

@ -53,6 +53,6 @@ contract C {
// f2() -> 0x20, 0xa0, 0x1, 0x60, 0x2, 0x3, "abc"
// f3() -> 0x20, 0xa0, 0x1, 0x60, 0x2, 0x3, "abc"
// f4() -> 0x20, 0x160, 0x1, 0x80, 0xc0, 0x2, 0x3, "abc", 0x7, 0x40, 0x2, 0x2, 0x3
// gas irOptimized: 110400
// gas irOptimized: 110283
// gas legacy: 111328
// gas legacyOptimized: 109206

View File

@ -19,7 +19,7 @@ contract C {
// compileViaYul: also
// ----
// h(uint256[2][]): 0x20, 3, 123, 124, 223, 224, 323, 324 -> 32, 256, 0x20, 3, 123, 124, 223, 224, 323, 324
// gas irOptimized: 172488
// gas irOptimized: 172410
// gas legacy: 175929
// gas legacyOptimized: 172504
// i(uint256[2][2]): 123, 124, 223, 224 -> 32, 128, 123, 124, 223, 224

View File

@ -11,6 +11,6 @@ contract C {
// compileViaYul: also
// ----
// f(bytes): 0x20, 0x80, 0x21, 0x40, 0x7, "abcdefg" -> 0x21, 0x40, 0x7, "abcdefg"
// gas irOptimized: 130136
// gas irOptimized: 130053
// gas legacy: 131690
// gas legacyOptimized: 130582

View File

@ -14,7 +14,7 @@ contract Test {
// compileViaYul: also
// ----
// set(uint24[3][]): 0x20, 0x06, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12 -> 0x06
// gas irOptimized: 199732
// gas irOptimized: 199693
// gas legacy: 278685
// gas legacyOptimized: 273594
// data(uint256,uint256): 0x02, 0x02 -> 0x09

View File

@ -47,7 +47,7 @@ contract c {
// gas legacyOptimized: 109706
// storage: nonempty
// test_long() -> 67
// gas irOptimized: 134403
// gas irOptimized: 134320
// gas legacy: 213590
// gas legacyOptimized: 211044
// storage: nonempty

View File

@ -19,6 +19,6 @@ contract c {
// compileViaYul: also
// ----
// test() -> 0
// gas irOptimized: 310785
// gas irOptimized: 309167
// gas legacy: 483915
// gas legacyOptimized: 478672

View File

@ -15,7 +15,7 @@ contract c {
// ----
// getLength() -> 0
// set(): 1, 2 -> true
// gas irOptimized: 103032
// gas irOptimized: 102993
// gas legacy: 103126
// gas legacyOptimized: 102967
// getLength() -> 68

View File

@ -22,7 +22,7 @@ contract c {
// compileViaYul: also
// ----
// store(uint256[9],uint8[3][]): 21, 22, 23, 24, 25, 26, 27, 28, 29, 0x140, 4, 1, 2, 3, 11, 12, 13, 21, 22, 23, 31, 32, 33 -> 32
// gas irOptimized: 612299
// gas irOptimized: 612216
// gas legacy: 817315
// gas legacyOptimized: 816813
// retrieve() -> 9, 28, 9, 28, 4, 3, 32

View File

@ -23,6 +23,6 @@ contract C {
// compileViaYul: also
// ----
// f() -> true
// gas irOptimized: 107700
// gas irOptimized: 107258
// gas legacy: 107335
// gas legacyOptimized: 105857

View File

@ -48,6 +48,6 @@ contract C {
// compileViaYul: also
// ----
// f() -> true
// gas irOptimized: 233831
// gas irOptimized: 231941
// gas legacy: 239061
// gas legacyOptimized: 235988

View File

@ -15,6 +15,6 @@ contract C {
// compileViaYul: also
// ----
// f() -> 0
// gas irOptimized: 139474
// gas irOptimized: 139105
// gas legacy: 138913
// gas legacyOptimized: 137448

View File

@ -42,11 +42,11 @@ contract C {
// compileViaYul: also
// ----
// f() -> 0
// gas irOptimized: 107703
// gas irOptimized: 107266
// gas legacy: 107306
// gas legacyOptimized: 105861
// g() -> 0
// h() -> 0
// gas irOptimized: 107749
// gas irOptimized: 107312
// gas legacy: 107328
// gas legacyOptimized: 105903

View File

@ -21,6 +21,6 @@ contract c {
// compileViaYul: also
// ----
// test() -> 0x01000000000000000000000000000000000000000000000000, 0x02000000000000000000000000000000000000000000000000, 0x03000000000000000000000000000000000000000000000000, 0x04000000000000000000000000000000000000000000000000, 0x05000000000000000000000000000000000000000000000000
// gas irOptimized: 246670
// gas irOptimized: 245944
// gas legacy: 276683
// gas legacyOptimized: 275534

View File

@ -37,12 +37,12 @@ contract c {
// compileViaYul: also
// ----
// test() -> 0x02000202
// gas irOptimized: 2471556
// gas irOptimized: 2470372
// gas legacy: 2288641
// gas legacyOptimized: 2258654
// storage: empty
// clear() -> 0, 0
// gas irOptimized: 1854143
// gas irOptimized: 1852821
// gas legacy: 1727169
// gas legacyOptimized: 1698931
// storage: empty

View File

@ -15,6 +15,6 @@ contract c {
// compileViaYul: also
// ----
// test(uint256[2][]): 32, 3, 7, 8, 9, 10, 11, 12 -> 10
// gas irOptimized: 610560
// gas irOptimized: 610177
// gas legacy: 604268
// gas legacyOptimized: 603688

View File

@ -19,6 +19,6 @@ contract c {
// compileViaYul: also
// ----
// test() -> 5, 4
// gas irOptimized: 235127
// gas irOptimized: 234667
// gas legacy: 237001
// gas legacyOptimized: 235316

View File

@ -20,6 +20,6 @@ contract c {
// compileViaYul: also
// ----
// test() -> 5, 4
// gas irOptimized: 265126
// gas irOptimized: 264686
// gas legacy: 264734
// gas legacyOptimized: 263160

View File

@ -14,4 +14,4 @@ contract c {
// compileViaYul: also
// ----
// test() -> 9, 4
// gas irOptimized: 99186
// gas irOptimized: 99075

View File

@ -19,7 +19,7 @@ contract c {
// compileViaYul: also
// ----
// test() -> 4, 5
// gas irOptimized: 258946
// gas irOptimized: 257752
// gas legacy: 255936
// gas legacyOptimized: 254359
// storage: empty

View File

@ -17,6 +17,6 @@ contract C {
// compileViaYul: also
// ----
// f() -> 0x20, 2, 0x40, 0xa0, 2, 0, 1, 2, 2, 3
// gas irOptimized: 168812
// gas irOptimized: 166791
// gas legacy: 163978
// gas legacyOptimized: 158155

View File

@ -38,10 +38,10 @@ contract c {
// compileViaYul: true
// ----
// test1(uint256[][]): 0x20, 2, 0x40, 0x40, 2, 23, 42 -> 2, 65
// gas irOptimized: 179354
// gas irOptimized: 179148
// test2(uint256[][2]): 0x20, 0x40, 0x40, 2, 23, 42 -> 2, 65
// gas irOptimized: 154106
// gas irOptimized: 153938
// test3(uint256[2][]): 0x20, 2, 23, 42, 23, 42 -> 2, 65
// gas irOptimized: 132579
// gas irOptimized: 132378
// test4(uint256[2][2]): 23, 42, 23, 42 -> 65
// gas irOptimized: 105395

View File

@ -40,12 +40,12 @@ contract Test {
// compileViaYul: also
// ----
// test() -> 24
// gas irOptimized: 216453
// gas irOptimized: 216291
// gas legacy: 215533
// gas legacyOptimized: 214947
// test1() -> 3
// test2() -> 6
// test3() -> 24
// gas irOptimized: 122996
// gas irOptimized: 122838
// gas legacy: 122795
// gas legacyOptimized: 121883

View File

@ -17,4 +17,4 @@ contract C {
// compileViaYul: true
// ----
// f((uint128,uint64,uint128)[]): 0x20, 3, 0, 0, 12, 0, 11, 0, 10, 0, 0 -> 10, 11, 12
// gas irOptimized: 123029
// gas irOptimized: 122861

View File

@ -19,4 +19,4 @@ contract C {
// compileViaYul: true
// ----
// f() -> 10, 11, 12
// gas irOptimized: 122025
// gas irOptimized: 121857

View File

@ -23,4 +23,4 @@ contract C {
// compileViaYul: true
// ----
// f((uint256[])[]): 0x20, 3, 0x60, 0x60, 0x60, 0x20, 3, 1, 2, 3 -> 3, 1
// gas irOptimized: 353951
// gas irOptimized: 352878

View File

@ -26,4 +26,4 @@ contract C {
// compileViaYul: true
// ----
// f() -> 3, 3, 3, 1
// gas irOptimized: 187586
// gas irOptimized: 187277

View File

@ -12,7 +12,7 @@ contract Test {
// compileViaYul: also
// ----
// set(uint24[]): 0x20, 18, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 -> 18
// gas irOptimized: 120898
// gas irOptimized: 120859
// gas legacy: 125815
// gas legacyOptimized: 123614
// data(uint256): 7 -> 8

View File

@ -23,21 +23,21 @@ contract c {
// gas legacy: 255464
// gas legacyOptimized: 250998
// f(uint256): 32 -> 0x20, 0x20, 1780731860627700044960722568376592200742329637303199754547598369979440671
// gas irOptimized: 232312
// gas irOptimized: 229335
// gas legacy: 267931
// gas legacyOptimized: 263329
// f(uint256): 33 -> 0x20, 33, 1780731860627700044960722568376592200742329637303199754547598369979440671, 0x2000000000000000000000000000000000000000000000000000000000000000
// gas irOptimized: 241107
// gas irOptimized: 238042
// gas legacy: 277538
// gas legacyOptimized: 272818
// f(uint256): 63 -> 0x20, 0x3f, 1780731860627700044960722568376592200742329637303199754547598369979440671, 14532552714582660066924456880521368950258152170031413196862950297402215316992
// gas irOptimized: 354417
// gas irOptimized: 348712
// gas legacy: 423428
// gas legacyOptimized: 414868
// f(uint256): 12 -> 0x20, 0x0c, 0x0102030405060708090a0b0000000000000000000000000000000000000000
// gas legacy: 106445
// gas legacyOptimized: 104379
// f(uint256): 129 -> 0x20, 0x81, 1780731860627700044960722568376592200742329637303199754547598369979440671, 0x202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f, 29063324697304692433803953038474361308315562010425523193971352996434451193439, 0x606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f, -57896044618658097711785492504343953926634992332820282019728792003956564819968
// gas irOptimized: 813872
// gas irOptimized: 802359
// gas legacy: 954517
// gas legacyOptimized: 937521

View File

@ -11,6 +11,6 @@ contract C {
// compileViaYul: also
// ----
// f(uint256[]): 0x20, 0x03, 0x1, 0x2, 0x3 -> 0x1
// gas irOptimized: 105262
// gas irOptimized: 105184
// gas legacy: 105365
// gas legacyOptimized: 105147

View File

@ -37,7 +37,7 @@ contract C {
// compileViaYul: also
// ----
// f() -> 0x40, 0x80, 6, 0x6162636465660000000000000000000000000000000000000000000000000000, 0x49, 0x3132333435363738393031323334353637383930313233343536373839303120, 0x3132333435363738393031323334353637383930313233343536373839303120, 0x3132333435363738390000000000000000000000000000000000000000000000
// gas irOptimized: 172401
// gas irOptimized: 172318
// gas legacy: 174794
// gas legacyOptimized: 174188
// g() -> 0x40, 0xc0, 0x49, 0x3132333435363738393031323334353637383930313233343536373839303120, 0x3132333435363738393031323334353637383930313233343536373839303120, 0x3132333435363738390000000000000000000000000000000000000000000000, 0x11, 0x3132333435363738393233343536373839000000000000000000000000000000

View File

@ -48,6 +48,6 @@ contract C {
// compileViaYul: also
// ----
// f() -> 0xff
// gas irOptimized: 136320
// gas irOptimized: 136164
// gas legacy: 137645
// gas legacyOptimized: 134376

View File

@ -18,6 +18,6 @@ contract C {
// compileViaYul: also
// ----
// test() -> 7
// gas irOptimized: 134158
// gas irOptimized: 133946
// gas legacy: 211296
// gas legacyOptimized: 211087

View File

@ -9,7 +9,7 @@ contract c {
// compileViaYul: also
// ----
// set(): 1, 2, 3, 4, 5 -> true
// gas irOptimized: 163719
// gas irOptimized: 163680
// gas legacy: 163756
// gas legacyOptimized: 163596
// storage: nonempty

View File

@ -20,6 +20,6 @@ contract C {
// compileViaYul: also
// ----
// f() -> 3
// gas irOptimized: 174188
// gas irOptimized: 173135
// gas legacy: 179707
// gas legacyOptimized: 178763

View File

@ -19,6 +19,6 @@ contract C {
// compileViaYul: also
// ----
// f() -> 1, 2, 3, 4, 5, 6, 7
// gas irOptimized: 212646
// gas irOptimized: 212108
// gas legacy: 223725
// gas legacyOptimized: 222886

View File

@ -13,6 +13,6 @@ contract C {
// compileViaYul: also
// ----
// f() -> 0x20, 0x02, 0x40, 0x80, 3, 0x6162630000000000000000000000000000000000000000000000000000000000, 0x99, 44048183304486788312148433451363384677562265908331949128489393215789685032262, 32241931068525137014058842823026578386641954854143559838526554899205067598957, 49951309422467613961193228765530489307475214998374779756599339590522149884499, 0x54555658595a6162636465666768696a6b6c6d6e6f707172737475767778797a, 0x4142434445464748494a4b4c4d4e4f5051525354555658595a00000000000000
// gas irOptimized: 198396
// gas irOptimized: 198279
// gas legacy: 199159
// gas legacyOptimized: 198137

View File

@ -20,6 +20,6 @@ contract C {
// compileViaYul: also
// ----
// f() -> 1, 2, 3, 4, 5, 6, 7
// gas irOptimized: 212646
// gas irOptimized: 212108
// gas legacy: 223730
// gas legacyOptimized: 222891

View File

@ -26,6 +26,6 @@ contract C {
// compileViaYul: also
// ----
// f() -> 11, 0x0c, 1, 0x15, 22, 4
// gas irOptimized: 289252
// gas irOptimized: 288695
// gas legacy: 296916
// gas legacyOptimized: 283163

View File

@ -15,6 +15,6 @@ contract C {
// compileViaYul: also
// ----
// f() -> 2, 3, 4
// gas irOptimized: 209796
// gas irOptimized: 208083
// gas legacy: 241549
// gas legacyOptimized: 236002

View File

@ -18,6 +18,6 @@ contract c {
// compileViaYul: also
// ----
// test1() -> true
// gas irOptimized: 532255
// gas irOptimized: 527501
// gas legacy: 613377
// gas legacyOptimized: 606411

View File

@ -16,4 +16,4 @@ contract C {
// compileViaYul: also
// ----
// f() -> 0, 0, 0
// gas irOptimized: 101573
// gas irOptimized: 101279

View File

@ -16,7 +16,7 @@ contract c {
// ----
// storage: empty
// fill() ->
// gas irOptimized: 535917
// gas irOptimized: 535098
// gas legacy: 504373
// gas legacyOptimized: 499648
// storage: nonempty

View File

@ -44,7 +44,7 @@ contract c {
// ----
// getLengths() -> 0, 0
// setLengths(uint256,uint256): 48, 49 ->
// gas irOptimized: 275838
// gas irOptimized: 273726
// gas legacy: 308271
// gas legacyOptimized: 300117
// getLengths() -> 48, 49

View File

@ -18,7 +18,7 @@ contract c {
// ----
// storage: empty
// fill() -> 8
// gas irOptimized: 170102
// gas irOptimized: 168980
// gas legacy: 165456
// gas legacyOptimized: 164387
// storage: nonempty

View File

@ -25,7 +25,7 @@ contract c {
// compileViaYul: also
// ----
// test() -> 1, 2, 3
// gas irOptimized: 2461941
// gas irOptimized: 2455497
// gas legacy: 2416722
// gas legacyOptimized: 2405396
// storage: empty

View File

@ -20,7 +20,7 @@ contract c {
// compileViaYul: also
// ----
// test() -> 38, 28, 18
// gas irOptimized: 532515
// gas irOptimized: 527367
// gas legacy: 454080
// gas legacyOptimized: 443170
// storage: empty

View File

@ -20,7 +20,7 @@ contract c {
// compileViaYul: also
// ----
// test() -> 20, 10
// gas irOptimized: 369849
// gas irOptimized: 367121
// gas legacy: 320859
// gas legacyOptimized: 314681
// storage: empty

View File

@ -12,6 +12,6 @@ contract c {
// compileViaYul: also
// ----
// test() -> 0x20, 29, 0x0303030303030303030303030303030303030303030303030303030303000000
// gas irOptimized: 162592
// gas irOptimized: 162426
// gas legacy: 245809
// gas legacyOptimized: 242636

View File

@ -18,7 +18,7 @@ contract c {
// compileViaYul: also
// ----
// test() -> true
// gas irOptimized: 447028
// gas irOptimized: 445718
// gas legacy: 552064
// gas legacyOptimized: 533164
// storage: empty

View File

@ -17,7 +17,7 @@ contract c {
// compileViaYul: also
// ----
// test() ->
// gas irOptimized: 291984
// gas irOptimized: 291114
// gas legacy: 372763
// gas legacyOptimized: 366846
// storage: empty

View File

@ -12,6 +12,6 @@ contract c {
// compileViaYul: also
// ----
// test() -> 0x20, 33, 0x303030303030303030303030303030303030303030303030303030303030303, 0x0300000000000000000000000000000000000000000000000000000000000000
// gas irOptimized: 159929
// gas irOptimized: 159714
// gas legacy: 243287
// gas legacyOptimized: 240361

View File

@ -18,6 +18,6 @@ contract c {
// compileViaYul: also
// ----
// test() -> 5, 4, 3, 3
// gas irOptimized: 110915
// gas irOptimized: 110669
// gas legacy: 111938
// gas legacyOptimized: 110528

View File

@ -14,6 +14,6 @@ contract C {
// compileViaYul: also
// ----
// f(uint120[]): 0x20, 3, 1, 2, 3 -> 1
// gas irOptimized: 116340
// gas irOptimized: 116184
// gas legacy: 116886
// gas legacyOptimized: 116699

View File

@ -16,6 +16,6 @@ contract c {
// compileViaYul: also
// ----
// test() -> 1, 2, 3, 4
// gas irOptimized: 112009
// gas irOptimized: 111583
// gas legacy: 107098
// gas legacyOptimized: 106362

View File

@ -22,6 +22,6 @@ contract c {
// compileViaYul: also
// ----
// test() -> 2, 3, 4, 5
// gas irOptimized: 146470
// gas irOptimized: 146270
// gas legacy: 190684
// gas legacyOptimized: 188256

View File

@ -18,6 +18,6 @@ contract c {
// compileViaYul: also
// ----
// test((uint16,uint16,uint16[3],uint16[])): 0x20, 2, 3, 0, 0, 4, 0xC0, 4, 0, 0, 5, 0, 0 -> 2, 3, 4, 5
// gas irOptimized: 148198
// gas irOptimized: 147998
// gas legacy: 152444
// gas legacyOptimized: 146671

View File

@ -17,6 +17,6 @@ contract c {
// compileViaYul: also
// ----
// test() -> 0
// gas irOptimized: 396502
// gas irOptimized: 394087
// gas legacy: 565428
// gas legacyOptimized: 552524

View File

@ -29,14 +29,14 @@ contract C {
// ----
// l() -> 0
// f(uint256,uint256): 42, 64 ->
// gas irOptimized: 202660
// gas irOptimized: 202621
// gas legacy: 163034
// gas legacyOptimized: 157045
// l() -> 1
// ll(uint256): 0 -> 43
// a(uint256,uint256): 0, 42 -> 64
// f(uint256,uint256): 84, 128 ->
// gas irOptimized: 298876
// gas irOptimized: 298837
// gas legacy: 222080
// gas legacyOptimized: 210631
// l() -> 2

View File

@ -23,7 +23,7 @@ contract C {
// ----
// l() -> 0
// g(uint256): 70 ->
// gas irOptimized: 430584
// gas irOptimized: 428829
// gas legacy: 419791
// gas legacyOptimized: 415408
// l() -> 70

View File

@ -26,6 +26,6 @@ contract Creator {
// compileViaYul: also
// ----
// f(uint256,address[]): 7, 0x40, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 -> 7, 8
// gas irOptimized: 469081
// gas irOptimized: 474619
// gas legacy: 570900
// gas legacyOptimized: 436724

View File

@ -26,6 +26,6 @@ contract Creator {
// compileViaYul: also
// ----
// f(uint256,bytes): 7, 0x40, 78, "abcdefghijklmnopqrstuvwxyzabcdef", "ghijklmnopqrstuvwxyzabcdefghijkl", "mnopqrstuvwxyz" -> 7, "h"
// gas irOptimized: 325793
// gas irOptimized: 330923
// gas legacy: 414850
// gas legacyOptimized: 292281

View File

@ -0,0 +1,35 @@
contract C {
function g() public returns (uint ret) {
uint x = type(uint).max;
assembly {
mstore(0x20, x)
// both old and new optimizer should be able to evaluate this
ret := keccak256(0x20, 16)
}
}
function f() public returns (uint ret) {
uint x = type(uint).max;
assembly {
mstore(0x20, x)
// For Yul optimizer, load resolver and loop invariant code motion
// would take the Keccak-256 outside the loop. For the old-optimizer,
// this is not possible.
// Net savings approximately: 20 * cost of Keccak-256 = 572
for {let i := 0} lt(i, 20) { i := add(i, 1) } {
ret := keccak256(0x20, 16)
}
}
}
}
// ====
// compileViaYul: also
// ----
// f() -> 0xcdb56c384a9682c600315e3470157a4cf7638d0d33e9dae5c40ffd2644fc5a80
// gas irOptimized: 22521
// gas legacy: 23385
// gas legacyOptimized: 23092
// g() -> 0xcdb56c384a9682c600315e3470157a4cf7638d0d33e9dae5c40ffd2644fc5a80
// gas irOptimized: 21391
// gas legacy: 21462
// gas legacyOptimized: 21256

View File

@ -18,6 +18,6 @@ contract C {
// compileViaYul: also
// ----
// f(uint32,(uint128,uint256[][2],uint32)): 55, 0x40, 77, 0x60, 88, 0x40, 0x40, 2, 1, 2 -> 55, 77, 1, 2, 88
// gas irOptimized: 197936
// gas irOptimized: 197768
// gas legacy: 205149
// gas legacyOptimized: 196983

View File

@ -25,7 +25,7 @@ contract c {
// ----
// storage: empty
// set(uint256,bytes,uint256): 12, 0x60, 13, 33, "12345678901234567890123456789012", "3" -> true
// gas irOptimized: 124266
// gas irOptimized: 124227
// gas legacy: 124736
// gas legacyOptimized: 124179
// test(uint256): 32 -> "3"

View File

@ -33,4 +33,4 @@ contract C {
// compileViaYul: true
// ----
// f() -> 0, 0, 0
// gas irOptimized: 124110
// gas irOptimized: 123854

View File

@ -44,7 +44,7 @@ contract C {
// compileViaYul: also
// ----
// f() ->
// gas irOptimized: 123095
// gas irOptimized: 122810
// gas legacy: 126832
// gas legacyOptimized: 125500
// g() ->

View File

@ -27,4 +27,4 @@ contract C {
// compileViaYul: true
// ----
// f() -> 0
// gas irOptimized: 118035
// gas irOptimized: 117902

View File

@ -36,6 +36,6 @@ contract C {
// compileViaYul: also
// ----
// f(bytes): 0x20, 0x5, "abcde" -> 0
// gas irOptimized: 241858
// gas irOptimized: 241342
// gas legacy: 239258
// gas legacyOptimized: 238582

View File

@ -22,6 +22,6 @@ contract C {
// compileViaYul: also
// ----
// g() -> 2, 6
// gas irOptimized: 169848
// gas irOptimized: 169804
// gas legacy: 172490
// gas legacyOptimized: 171209

View File

@ -18,33 +18,33 @@ contract C {
// ----
// test_indices(uint256): 1 ->
// test_indices(uint256): 129 ->
// gas irOptimized: 3457322
// gas irOptimized: 3442268
// gas legacy: 3340105
// gas legacyOptimized: 3280773
// test_indices(uint256): 5 ->
// gas irOptimized: 474246
// gas irOptimized: 467656
// gas legacy: 458941
// gas legacyOptimized: 455849
// test_indices(uint256): 10 ->
// test_indices(uint256): 15 ->
// gas irOptimized: 110435
// gas irOptimized: 109070
// test_indices(uint256): 0xFF ->
// gas irOptimized: 4338190
// gas irOptimized: 4308940
// gas legacy: 4107867
// gas legacyOptimized: 3991807
// test_indices(uint256): 1000 ->
// gas irOptimized: 21240617
// gas irOptimized: 21133562
// gas legacy: 20360399
// gas legacyOptimized: 19921344
// test_indices(uint256): 129 ->
// gas irOptimized: 3654995
// gas irOptimized: 3601383
// gas legacy: 3472135
// gas legacyOptimized: 3415947
// test_indices(uint256): 128 ->
// gas irOptimized: 658131
// gas irOptimized: 648097
// gas legacy: 556972
// gas legacyOptimized: 508124
// test_indices(uint256): 1 ->
// gas irOptimized: 464827
// gas irOptimized: 458399
// gas legacy: 452407
// gas legacyOptimized: 450811

View File

@ -18,11 +18,11 @@ contract C {
// test_boundary_check(uint256,uint256): 1, 1 -> FAILURE, hex"4e487b71", 0x32
// test_boundary_check(uint256,uint256): 10, 10 -> FAILURE, hex"4e487b71", 0x32
// test_boundary_check(uint256,uint256): 256, 256 -> FAILURE, hex"4e487b71", 0x32
// gas irOptimized: 677730
// gas irOptimized: 668136
// gas legacy: 648515
// gas legacyOptimized: 628739
// test_boundary_check(uint256,uint256): 256, 255 -> 0
// gas irOptimized: 678750
// gas irOptimized: 669117
// gas legacy: 649549
// gas legacyOptimized: 629633
// test_boundary_check(uint256,uint256): 256, 0xFFFF -> FAILURE, hex"4e487b71", 0x32

Some files were not shown because too many files have changed in this diff Show More