Merge pull request #10491 from ethereum/loadStoreDialectFunctions

Add storage load/store functions to Yul dialect.
This commit is contained in:
chriseth 2020-12-07 12:19:20 +01:00 committed by GitHub
commit 5e61f2a8cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 61 additions and 70 deletions

View File

@ -74,6 +74,8 @@ struct Dialect: boost::noncopyable
virtual BuiltinFunction const* memoryStoreFunction(YulString /* _type */) const { return nullptr; } virtual BuiltinFunction const* memoryStoreFunction(YulString /* _type */) const { return nullptr; }
virtual BuiltinFunction const* memoryLoadFunction(YulString /* _type */) const { return nullptr; } 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; }
/// Check whether the given type is legal for the given literal value. /// 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. /// Should only be called if the type exists in the dialect at all.

View File

@ -75,6 +75,8 @@ struct EVMDialect: public Dialect
BuiltinFunctionForEVM const* booleanNegationFunction() const override { return builtin("iszero"_yulstring); } BuiltinFunctionForEVM const* booleanNegationFunction() const override { return builtin("iszero"_yulstring); }
BuiltinFunctionForEVM const* memoryStoreFunction(YulString /*_type*/) const override { return builtin("mstore"_yulstring); } BuiltinFunctionForEVM const* memoryStoreFunction(YulString /*_type*/) const override { return builtin("mstore"_yulstring); }
BuiltinFunctionForEVM const* memoryLoadFunction(YulString /*_type*/) const override { return builtin("mload"_yulstring); } 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); }
static EVMDialect const& strictAssemblyForEVM(langutil::EVMVersion _version); static EVMDialect const& strictAssemblyForEVM(langutil::EVMVersion _version);
static EVMDialect const& strictAssemblyForEVMObjects(langutil::EVMVersion _version); static EVMDialect const& strictAssemblyForEVMObjects(langutil::EVMVersion _version);

View File

@ -25,8 +25,8 @@
#include <libyul/optimiser/NameCollector.h> #include <libyul/optimiser/NameCollector.h>
#include <libyul/optimiser/Semantics.h> #include <libyul/optimiser/Semantics.h>
#include <libyul/AST.h> #include <libyul/AST.h>
#include <libyul/Dialect.h>
#include <libyul/Exceptions.h> #include <libyul/Exceptions.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <libsolutil/CommonData.h> #include <libsolutil/CommonData.h>
@ -39,9 +39,27 @@ using namespace solidity;
using namespace solidity::util; using namespace solidity::util;
using namespace solidity::yul; using namespace solidity::yul;
DataFlowAnalyzer::DataFlowAnalyzer(
Dialect const& _dialect,
map<YulString, SideEffects> _functionSideEffects
):
m_dialect(_dialect),
m_functionSideEffects(std::move(_functionSideEffects)),
m_knowledgeBase(_dialect, m_value)
{
if (auto const* builtin = _dialect.memoryStoreFunction(YulString{}))
m_storeFunctionName[static_cast<unsigned>(StoreLoadLocation::Memory)] = builtin->name;
if (auto const* builtin = _dialect.memoryLoadFunction(YulString{}))
m_loadFunctionName[static_cast<unsigned>(StoreLoadLocation::Memory)] = builtin->name;
if (auto const* builtin = _dialect.storageStoreFunction(YulString{}))
m_storeFunctionName[static_cast<unsigned>(StoreLoadLocation::Storage)] = builtin->name;
if (auto const* builtin = _dialect.storageLoadFunction(YulString{}))
m_loadFunctionName[static_cast<unsigned>(StoreLoadLocation::Storage)] = builtin->name;
}
void DataFlowAnalyzer::operator()(ExpressionStatement& _statement) void DataFlowAnalyzer::operator()(ExpressionStatement& _statement)
{ {
if (auto vars = isSimpleStore(evmasm::Instruction::SSTORE, _statement)) if (auto vars = isSimpleStore(StoreLoadLocation::Storage, _statement))
{ {
ASTModifier::operator()(_statement); ASTModifier::operator()(_statement);
set<YulString> keysToErase; set<YulString> keysToErase;
@ -55,7 +73,7 @@ void DataFlowAnalyzer::operator()(ExpressionStatement& _statement)
m_storage.eraseKey(key); m_storage.eraseKey(key);
m_storage.set(vars->first, vars->second); m_storage.set(vars->first, vars->second);
} }
else if (auto vars = isSimpleStore(evmasm::Instruction::MSTORE, _statement)) else if (auto vars = isSimpleStore(StoreLoadLocation::Memory, _statement))
{ {
ASTModifier::operator()(_statement); ASTModifier::operator()(_statement);
set<YulString> keysToErase; set<YulString> keysToErase;
@ -265,9 +283,9 @@ void DataFlowAnalyzer::handleAssignment(set<YulString> const& _variables, Expres
// This might erase additional knowledge about the slot. // This might erase additional knowledge about the slot.
// On the other hand, if we knew the value in the slot // On the other hand, if we knew the value in the slot
// already, then the sload() / mload() would have been replaced by a variable anyway. // already, then the sload() / mload() would have been replaced by a variable anyway.
if (auto key = isSimpleLoad(evmasm::Instruction::MLOAD, *_value)) if (auto key = isSimpleLoad(StoreLoadLocation::Memory, *_value))
m_memory.set(*key, variable); m_memory.set(*key, variable);
else if (auto key = isSimpleLoad(evmasm::Instruction::SLOAD, *_value)) else if (auto key = isSimpleLoad(StoreLoadLocation::Storage, *_value))
m_storage.set(*key, variable); m_storage.set(*key, variable);
} }
} }
@ -391,53 +409,27 @@ bool DataFlowAnalyzer::inScope(YulString _variableName) const
} }
std::optional<pair<YulString, YulString>> DataFlowAnalyzer::isSimpleStore( std::optional<pair<YulString, YulString>> DataFlowAnalyzer::isSimpleStore(
evmasm::Instruction _store, StoreLoadLocation _location,
ExpressionStatement const& _statement ExpressionStatement const& _statement
) const ) const
{ {
yulAssert( if (FunctionCall const* funCall = get_if<FunctionCall>(&_statement.expression))
_store == evmasm::Instruction::MSTORE || if (funCall->functionName.name == m_storeFunctionName[static_cast<unsigned>(_location)])
_store == evmasm::Instruction::SSTORE, if (Identifier const* key = std::get_if<Identifier>(&funCall->arguments.front()))
"" if (Identifier const* value = std::get_if<Identifier>(&funCall->arguments.back()))
); return make_pair(key->name, value->name);
if (holds_alternative<FunctionCall>(_statement.expression))
{
FunctionCall const& funCall = std::get<FunctionCall>(_statement.expression);
if (EVMDialect const* dialect = dynamic_cast<EVMDialect const*>(&m_dialect))
if (auto const* builtin = dialect->builtin(funCall.functionName.name))
if (builtin->instruction == _store)
if (
holds_alternative<Identifier>(funCall.arguments.at(0)) &&
holds_alternative<Identifier>(funCall.arguments.at(1))
)
{
YulString key = std::get<Identifier>(funCall.arguments.at(0)).name;
YulString value = std::get<Identifier>(funCall.arguments.at(1)).name;
return make_pair(key, value);
}
}
return {}; return {};
} }
std::optional<YulString> DataFlowAnalyzer::isSimpleLoad( std::optional<YulString> DataFlowAnalyzer::isSimpleLoad(
evmasm::Instruction _load, StoreLoadLocation _location,
Expression const& _expression Expression const& _expression
) const ) const
{ {
yulAssert( if (FunctionCall const* funCall = get_if<FunctionCall>(&_expression))
_load == evmasm::Instruction::MLOAD || if (funCall->functionName.name == m_loadFunctionName[static_cast<unsigned>(_location)])
_load == evmasm::Instruction::SLOAD, if (Identifier const* key = std::get_if<Identifier>(&funCall->arguments.front()))
"" return key->name;
);
if (holds_alternative<FunctionCall>(_expression))
{
FunctionCall const& funCall = std::get<FunctionCall>(_expression);
if (EVMDialect const* dialect = dynamic_cast<EVMDialect const*>(&m_dialect))
if (auto const* builtin = dialect->builtin(funCall.functionName.name))
if (builtin->instruction == _load)
if (holds_alternative<Identifier>(funCall.arguments.at(0)))
return std::get<Identifier>(funCall.arguments.at(0)).name;
}
return {}; return {};
} }

View File

@ -29,9 +29,6 @@
#include <libyul/AST.h> // Needed for m_zero below. #include <libyul/AST.h> // Needed for m_zero below.
#include <libyul/SideEffects.h> #include <libyul/SideEffects.h>
// TODO avoid
#include <libevmasm/Instruction.h>
#include <libsolutil/InvertibleMap.h> #include <libsolutil/InvertibleMap.h>
#include <map> #include <map>
@ -89,11 +86,7 @@ public:
explicit DataFlowAnalyzer( explicit DataFlowAnalyzer(
Dialect const& _dialect, Dialect const& _dialect,
std::map<YulString, SideEffects> _functionSideEffects = {} std::map<YulString, SideEffects> _functionSideEffects = {}
): );
m_dialect(_dialect),
m_functionSideEffects(std::move(_functionSideEffects)),
m_knowledgeBase(_dialect, m_value)
{}
using ASTModifier::operator(); using ASTModifier::operator();
void operator()(ExpressionStatement& _statement) override; void operator()(ExpressionStatement& _statement) override;
@ -143,17 +136,23 @@ protected:
/// Returns true iff the variable is in scope. /// Returns true iff the variable is in scope.
bool inScope(YulString _variableName) const; bool inScope(YulString _variableName) const;
enum class StoreLoadLocation {
Memory = 0,
Storage = 1,
Last = Storage
};
/// Checks if the statement is sstore(a, b) / mstore(a, b) /// Checks if the statement is sstore(a, b) / mstore(a, b)
/// where a and b are variables and returns these variables in that case. /// where a and b are variables and returns these variables in that case.
std::optional<std::pair<YulString, YulString>> isSimpleStore( std::optional<std::pair<YulString, YulString>> isSimpleStore(
evmasm::Instruction _store, StoreLoadLocation _location,
ExpressionStatement const& _statement ExpressionStatement const& _statement
) const; ) const;
/// Checks if the expression is sload(a) / mload(a) /// Checks if the expression is sload(a) / mload(a)
/// where a is a variable and returns the variable in that case. /// where a is a variable and returns the variable in that case.
std::optional<YulString> isSimpleLoad( std::optional<YulString> isSimpleLoad(
evmasm::Instruction _load, StoreLoadLocation _location,
Expression const& _expression Expression const& _expression
) const; ) const;
@ -173,6 +172,9 @@ protected:
KnowledgeBase m_knowledgeBase; KnowledgeBase m_knowledgeBase;
YulString m_storeFunctionName[static_cast<unsigned>(StoreLoadLocation::Last) + 1];
YulString m_loadFunctionName[static_cast<unsigned>(StoreLoadLocation::Last) + 1];
/// Current nesting depth of loops. /// Current nesting depth of loops.
size_t m_loopDepth{0}; size_t m_loopDepth{0};

View File

@ -46,21 +46,18 @@ void LoadResolver::visit(Expression& _e)
{ {
DataFlowAnalyzer::visit(_e); DataFlowAnalyzer::visit(_e);
if (!dynamic_cast<EVMDialect const*>(&m_dialect)) if (FunctionCall const* funCall = std::get_if<FunctionCall>(&_e))
return; for (auto location: { StoreLoadLocation::Memory, StoreLoadLocation::Storage })
if (funCall->functionName.name == m_loadFunctionName[static_cast<unsigned>(location)])
if (holds_alternative<FunctionCall>(_e)) {
{ tryResolve(_e, location, funCall->arguments);
FunctionCall const& funCall = std::get<FunctionCall>(_e); break;
if (auto const* builtin = dynamic_cast<EVMDialect const&>(m_dialect).builtin(funCall.functionName.name)) }
if (builtin->instruction)
tryResolve(_e, *builtin->instruction, funCall.arguments);
}
} }
void LoadResolver::tryResolve( void LoadResolver::tryResolve(
Expression& _e, Expression& _e,
evmasm::Instruction _instruction, StoreLoadLocation _location,
vector<Expression> const& _arguments vector<Expression> const& _arguments
) )
{ {
@ -69,13 +66,13 @@ void LoadResolver::tryResolve(
YulString key = std::get<Identifier>(_arguments.at(0)).name; YulString key = std::get<Identifier>(_arguments.at(0)).name;
if ( if (
_instruction == evmasm::Instruction::SLOAD && _location == StoreLoadLocation::Storage &&
m_storage.values.count(key) m_storage.values.count(key)
) )
_e = Identifier{locationOf(_e), m_storage.values[key]}; _e = Identifier{locationOf(_e), m_storage.values[key]};
else if ( else if (
m_optimizeMLoad && m_optimizeMLoad &&
_instruction == evmasm::Instruction::MLOAD && _location == StoreLoadLocation::Memory &&
m_memory.values.count(key) m_memory.values.count(key)
) )
_e = Identifier{locationOf(_e), m_memory.values[key]}; _e = Identifier{locationOf(_e), m_memory.values[key]};

View File

@ -24,14 +24,10 @@
#include <libyul/optimiser/DataFlowAnalyzer.h> #include <libyul/optimiser/DataFlowAnalyzer.h>
#include <libyul/optimiser/OptimiserStep.h> #include <libyul/optimiser/OptimiserStep.h>
#include <libevmasm/Instruction.h>
namespace solidity::yul namespace solidity::yul
{ {
struct EVMDialect;
struct BuiltinFunctionForEVM;
/** /**
* Optimisation stage that replaces expressions of type ``sload(x)`` and ``mload(x)`` by the value * Optimisation stage that replaces expressions of type ``sload(x)`` and ``mload(x)`` by the value
* currently stored in storage resp. memory, if known. * currently stored in storage resp. memory, if known.
@ -63,7 +59,7 @@ protected:
void tryResolve( void tryResolve(
Expression& _e, Expression& _e,
evmasm::Instruction _instruction, StoreLoadLocation _location,
std::vector<Expression> const& _arguments std::vector<Expression> const& _arguments
); );