mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
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:
@@ -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 {};
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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)
|
||||
{}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user