mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #7071 from ethereum/checkAsmDataObject
Check availability of data objects already in analysis phase.
This commit is contained in:
commit
88477bdb8f
@ -18,6 +18,7 @@ Compiler Features:
|
|||||||
|
|
||||||
Bugfixes:
|
Bugfixes:
|
||||||
* View/Pure Checker: Properly detect state variable access through base class.
|
* View/Pure Checker: Properly detect state variable access through base class.
|
||||||
|
* Yul analyzer: Check availability of data objects already in analysis phase.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -34,6 +34,7 @@
|
|||||||
#include <libyul/backends/evm/EVMDialect.h>
|
#include <libyul/backends/evm/EVMDialect.h>
|
||||||
#include <libyul/backends/evm/EVMMetrics.h>
|
#include <libyul/backends/evm/EVMMetrics.h>
|
||||||
#include <libyul/optimiser/Suite.h>
|
#include <libyul/optimiser/Suite.h>
|
||||||
|
#include <libyul/Object.h>
|
||||||
#include <libyul/YulString.h>
|
#include <libyul/YulString.h>
|
||||||
|
|
||||||
#include <liblangutil/ErrorReporter.h>
|
#include <liblangutil/ErrorReporter.h>
|
||||||
@ -423,23 +424,19 @@ void CompilerContext::appendInlineAssembly(
|
|||||||
{
|
{
|
||||||
bool const isCreation = m_runtimeContext != nullptr;
|
bool const isCreation = m_runtimeContext != nullptr;
|
||||||
yul::GasMeter meter(dialect, isCreation, _optimiserSettings.expectedExecutionsPerDeployment);
|
yul::GasMeter meter(dialect, isCreation, _optimiserSettings.expectedExecutionsPerDeployment);
|
||||||
|
yul::Object obj;
|
||||||
|
obj.code = parserResult;
|
||||||
|
obj.analysisInfo = make_shared<yul::AsmAnalysisInfo>(analysisInfo);
|
||||||
yul::OptimiserSuite::run(
|
yul::OptimiserSuite::run(
|
||||||
dialect,
|
dialect,
|
||||||
&meter,
|
&meter,
|
||||||
*parserResult,
|
obj,
|
||||||
analysisInfo,
|
|
||||||
_optimiserSettings.optimizeStackAllocation,
|
_optimiserSettings.optimizeStackAllocation,
|
||||||
externallyUsedIdentifiers
|
externallyUsedIdentifiers
|
||||||
);
|
);
|
||||||
analysisInfo = yul::AsmAnalysisInfo{};
|
analysisInfo = std::move(*obj.analysisInfo);
|
||||||
if (!yul::AsmAnalyzer(
|
parserResult = std::move(obj.code);
|
||||||
analysisInfo,
|
|
||||||
errorReporter,
|
|
||||||
boost::none,
|
|
||||||
dialect,
|
|
||||||
identifierAccess.resolve
|
|
||||||
).analyze(*parserResult))
|
|
||||||
reportError("Optimizer introduced error into inline assembly.");
|
|
||||||
#ifdef SOL_OUTPUT_ASM
|
#ifdef SOL_OUTPUT_ASM
|
||||||
cout << "After optimizer: " << endl;
|
cout << "After optimizer: " << endl;
|
||||||
cout << yul::AsmPrinter()(*parserResult) << endl;
|
cout << yul::AsmPrinter()(*parserResult) << endl;
|
||||||
|
@ -26,6 +26,7 @@
|
|||||||
#include <libyul/AsmAnalysisInfo.h>
|
#include <libyul/AsmAnalysisInfo.h>
|
||||||
#include <libyul/Utilities.h>
|
#include <libyul/Utilities.h>
|
||||||
#include <libyul/Exceptions.h>
|
#include <libyul/Exceptions.h>
|
||||||
|
#include <libyul/Object.h>
|
||||||
|
|
||||||
#include <liblangutil/ErrorReporter.h>
|
#include <liblangutil/ErrorReporter.h>
|
||||||
|
|
||||||
@ -69,17 +70,19 @@ bool AsmAnalyzer::analyze(Block const& _block)
|
|||||||
return success && !m_errorReporter.hasErrors();
|
return success && !m_errorReporter.hasErrors();
|
||||||
}
|
}
|
||||||
|
|
||||||
AsmAnalysisInfo AsmAnalyzer::analyzeStrictAssertCorrect(Dialect const& _dialect, Block const& _ast)
|
AsmAnalysisInfo AsmAnalyzer::analyzeStrictAssertCorrect(Dialect const& _dialect, Object const& _object)
|
||||||
{
|
{
|
||||||
ErrorList errorList;
|
ErrorList errorList;
|
||||||
langutil::ErrorReporter errors(errorList);
|
langutil::ErrorReporter errors(errorList);
|
||||||
yul::AsmAnalysisInfo analysisInfo;
|
AsmAnalysisInfo analysisInfo;
|
||||||
bool success = yul::AsmAnalyzer(
|
bool success = yul::AsmAnalyzer(
|
||||||
analysisInfo,
|
analysisInfo,
|
||||||
errors,
|
errors,
|
||||||
Error::Type::SyntaxError,
|
Error::Type::SyntaxError,
|
||||||
_dialect
|
_dialect,
|
||||||
).analyze(_ast);
|
{},
|
||||||
|
_object.dataNames()
|
||||||
|
).analyze(*_object.code);
|
||||||
solAssert(success && errorList.empty(), "Invalid assembly/yul code.");
|
solAssert(success && errorList.empty(), "Invalid assembly/yul code.");
|
||||||
return analysisInfo;
|
return analysisInfo;
|
||||||
}
|
}
|
||||||
@ -383,11 +386,19 @@ bool AsmAnalyzer::operator()(FunctionCall const& _funCall)
|
|||||||
{
|
{
|
||||||
if (!expectExpression(arg))
|
if (!expectExpression(arg))
|
||||||
success = false;
|
success = false;
|
||||||
else if (needsLiteralArguments && arg.type() != typeid(Literal))
|
else if (needsLiteralArguments)
|
||||||
|
{
|
||||||
|
if (arg.type() != typeid(Literal))
|
||||||
m_errorReporter.typeError(
|
m_errorReporter.typeError(
|
||||||
_funCall.functionName.location,
|
_funCall.functionName.location,
|
||||||
"Function expects direct literals as arguments."
|
"Function expects direct literals as arguments."
|
||||||
);
|
);
|
||||||
|
else if (!m_dataNames.count(boost::get<Literal>(arg).value))
|
||||||
|
m_errorReporter.typeError(
|
||||||
|
_funCall.functionName.location,
|
||||||
|
"Unknown data object \"" + boost::get<Literal>(arg).value.str() + "\"."
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Use argument size instead of parameter count to avoid misleading errors.
|
// Use argument size instead of parameter count to avoid misleading errors.
|
||||||
m_stackHeight += int(returns) - int(_funCall.arguments.size());
|
m_stackHeight += int(returns) - int(_funCall.arguments.size());
|
||||||
|
@ -61,13 +61,15 @@ public:
|
|||||||
langutil::ErrorReporter& _errorReporter,
|
langutil::ErrorReporter& _errorReporter,
|
||||||
boost::optional<langutil::Error::Type> _errorTypeForLoose,
|
boost::optional<langutil::Error::Type> _errorTypeForLoose,
|
||||||
Dialect const& _dialect,
|
Dialect const& _dialect,
|
||||||
ExternalIdentifierAccess::Resolver const& _resolver = ExternalIdentifierAccess::Resolver()
|
ExternalIdentifierAccess::Resolver const& _resolver = ExternalIdentifierAccess::Resolver(),
|
||||||
|
std::set<YulString> const& _dataNames = {}
|
||||||
):
|
):
|
||||||
m_resolver(_resolver),
|
m_resolver(_resolver),
|
||||||
m_info(_analysisInfo),
|
m_info(_analysisInfo),
|
||||||
m_errorReporter(_errorReporter),
|
m_errorReporter(_errorReporter),
|
||||||
m_dialect(_dialect),
|
m_dialect(_dialect),
|
||||||
m_errorTypeForLoose(_errorTypeForLoose)
|
m_errorTypeForLoose(_errorTypeForLoose),
|
||||||
|
m_dataNames(_dataNames)
|
||||||
{
|
{
|
||||||
if (EVMDialect const* evmDialect = dynamic_cast<EVMDialect const*>(&m_dialect))
|
if (EVMDialect const* evmDialect = dynamic_cast<EVMDialect const*>(&m_dialect))
|
||||||
m_evmVersion = evmDialect->evmVersion();
|
m_evmVersion = evmDialect->evmVersion();
|
||||||
@ -75,7 +77,9 @@ public:
|
|||||||
|
|
||||||
bool analyze(Block const& _block);
|
bool analyze(Block const& _block);
|
||||||
|
|
||||||
static AsmAnalysisInfo analyzeStrictAssertCorrect(Dialect const& _dialect, Block const& _ast);
|
/// Performs analysis on the outermost code of the given object and returns the analysis info.
|
||||||
|
/// Asserts on failure.
|
||||||
|
static AsmAnalysisInfo analyzeStrictAssertCorrect(Dialect const& _dialect, Object const& _object);
|
||||||
|
|
||||||
bool operator()(Instruction const&);
|
bool operator()(Instruction const&);
|
||||||
bool operator()(Literal const& _literal);
|
bool operator()(Literal const& _literal);
|
||||||
@ -124,6 +128,8 @@ private:
|
|||||||
langutil::EVMVersion m_evmVersion;
|
langutil::EVMVersion m_evmVersion;
|
||||||
Dialect const& m_dialect;
|
Dialect const& m_dialect;
|
||||||
boost::optional<langutil::Error::Type> m_errorTypeForLoose;
|
boost::optional<langutil::Error::Type> m_errorTypeForLoose;
|
||||||
|
/// Names of data objects to be referenced by builtin functions with literal arguments.
|
||||||
|
std::set<YulString> m_dataNames;
|
||||||
ForLoop const* m_currentForLoop = nullptr;
|
ForLoop const* m_currentForLoop = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -113,7 +113,15 @@ bool AssemblyStack::analyzeParsed(Object& _object)
|
|||||||
{
|
{
|
||||||
solAssert(_object.code, "");
|
solAssert(_object.code, "");
|
||||||
_object.analysisInfo = make_shared<AsmAnalysisInfo>();
|
_object.analysisInfo = make_shared<AsmAnalysisInfo>();
|
||||||
AsmAnalyzer analyzer(*_object.analysisInfo, m_errorReporter, boost::none, languageToDialect(m_language, m_evmVersion));
|
|
||||||
|
AsmAnalyzer analyzer(
|
||||||
|
*_object.analysisInfo,
|
||||||
|
m_errorReporter,
|
||||||
|
boost::none,
|
||||||
|
languageToDialect(m_language, m_evmVersion),
|
||||||
|
{},
|
||||||
|
_object.dataNames()
|
||||||
|
);
|
||||||
bool success = analyzer.analyze(*_object.code);
|
bool success = analyzer.analyze(*_object.code);
|
||||||
for (auto& subNode: _object.subObjects)
|
for (auto& subNode: _object.subObjects)
|
||||||
if (auto subObject = dynamic_cast<Object*>(subNode.get()))
|
if (auto subObject = dynamic_cast<Object*>(subNode.get()))
|
||||||
@ -153,8 +161,7 @@ void AssemblyStack::optimize(Object& _object, bool _isCreation)
|
|||||||
OptimiserSuite::run(
|
OptimiserSuite::run(
|
||||||
dialect,
|
dialect,
|
||||||
meter.get(),
|
meter.get(),
|
||||||
*_object.code,
|
_object,
|
||||||
*_object.analysisInfo,
|
|
||||||
m_optimiserSettings.optimizeStackAllocation
|
m_optimiserSettings.optimizeStackAllocation
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,7 @@ using namespace dev;
|
|||||||
|
|
||||||
map<YulString, int> CompilabilityChecker::run(
|
map<YulString, int> CompilabilityChecker::run(
|
||||||
Dialect const& _dialect,
|
Dialect const& _dialect,
|
||||||
Block const& _ast,
|
Object const& _object,
|
||||||
bool _optimizeStackAllocation
|
bool _optimizeStackAllocation
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
@ -46,16 +46,26 @@ map<YulString, int> CompilabilityChecker::run(
|
|||||||
if (EVMDialect const* evmDialect = dynamic_cast<EVMDialect const*>(&_dialect))
|
if (EVMDialect const* evmDialect = dynamic_cast<EVMDialect const*>(&_dialect))
|
||||||
{
|
{
|
||||||
NoOutputEVMDialect noOutputDialect(*evmDialect);
|
NoOutputEVMDialect noOutputDialect(*evmDialect);
|
||||||
BuiltinContext builtinContext;
|
|
||||||
|
|
||||||
yul::AsmAnalysisInfo analysisInfo =
|
yul::AsmAnalysisInfo analysisInfo =
|
||||||
yul::AsmAnalyzer::analyzeStrictAssertCorrect(noOutputDialect, _ast);
|
yul::AsmAnalyzer::analyzeStrictAssertCorrect(noOutputDialect, _object);
|
||||||
|
|
||||||
|
BuiltinContext builtinContext;
|
||||||
|
builtinContext.currentObject = &_object;
|
||||||
|
for (auto name: _object.dataNames())
|
||||||
|
builtinContext.subIDs[name] = 1;
|
||||||
NoOutputAssembly assembly;
|
NoOutputAssembly assembly;
|
||||||
CodeTransform transform(assembly, analysisInfo, _ast, noOutputDialect, builtinContext, _optimizeStackAllocation);
|
CodeTransform transform(
|
||||||
|
assembly,
|
||||||
|
analysisInfo,
|
||||||
|
*_object.code,
|
||||||
|
noOutputDialect,
|
||||||
|
builtinContext,
|
||||||
|
_optimizeStackAllocation
|
||||||
|
);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
transform(_ast);
|
transform(*_object.code);
|
||||||
}
|
}
|
||||||
catch (StackTooDeepError const&)
|
catch (StackTooDeepError const&)
|
||||||
{
|
{
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
|
|
||||||
#include <libyul/Dialect.h>
|
#include <libyul/Dialect.h>
|
||||||
#include <libyul/AsmDataForward.h>
|
#include <libyul/AsmDataForward.h>
|
||||||
|
#include <libyul/Object.h>
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@ -33,15 +34,18 @@ namespace yul
|
|||||||
* Component that checks whether all variables are reachable on the stack and
|
* Component that checks whether all variables are reachable on the stack and
|
||||||
* returns a mapping from function name to the largest stack difference found
|
* returns a mapping from function name to the largest stack difference found
|
||||||
* in that function (no entry present if that function is compilable).
|
* in that function (no entry present if that function is compilable).
|
||||||
|
*
|
||||||
* This only works properly if the outermost block is compilable and
|
* This only works properly if the outermost block is compilable and
|
||||||
* functions are not nested. Otherwise, it might miss reporting some functions.
|
* functions are not nested. Otherwise, it might miss reporting some functions.
|
||||||
|
*
|
||||||
|
* Only checks the code of the object itself, does not descend into sub-objects.
|
||||||
*/
|
*/
|
||||||
class CompilabilityChecker
|
class CompilabilityChecker
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static std::map<YulString, int> run(
|
static std::map<YulString, int> run(
|
||||||
Dialect const& _dialect,
|
Dialect const& _dialect,
|
||||||
Block const& _ast,
|
Object const& _object,
|
||||||
bool _optimizeStackAllocation
|
bool _optimizeStackAllocation
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -59,3 +59,14 @@ string Object::toString(bool _yul) const
|
|||||||
|
|
||||||
return "object \"" + name.str() + "\" {\n" + indent(inner) + "\n}";
|
return "object \"" + name.str() + "\" {\n" + indent(inner) + "\n}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
set<YulString> Object::dataNames() const
|
||||||
|
{
|
||||||
|
set<YulString> names;
|
||||||
|
names.insert(name);
|
||||||
|
for (auto const& subObject: subIndexByName)
|
||||||
|
names.insert(subObject.first);
|
||||||
|
// The empty name is not valid
|
||||||
|
names.erase(YulString{});
|
||||||
|
return names;
|
||||||
|
}
|
||||||
|
@ -26,6 +26,7 @@
|
|||||||
#include <libdevcore/Common.h>
|
#include <libdevcore/Common.h>
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <set>
|
||||||
|
|
||||||
namespace yul
|
namespace yul
|
||||||
{
|
{
|
||||||
@ -63,6 +64,10 @@ public:
|
|||||||
/// @returns a (parseable) string representation. Includes types if @a _yul is set.
|
/// @returns a (parseable) string representation. Includes types if @a _yul is set.
|
||||||
std::string toString(bool _yul) const override;
|
std::string toString(bool _yul) const override;
|
||||||
|
|
||||||
|
/// @returns the set of names of data objects accessible from within the code of
|
||||||
|
/// this object.
|
||||||
|
std::set<YulString> dataNames() const;
|
||||||
|
|
||||||
std::shared_ptr<Block> code;
|
std::shared_ptr<Block> code;
|
||||||
std::vector<std::shared_ptr<ObjectNode>> subObjects;
|
std::vector<std::shared_ptr<ObjectNode>> subObjects;
|
||||||
std::map<YulString, size_t> subIndexByName;
|
std::map<YulString, size_t> subIndexByName;
|
||||||
|
@ -553,7 +553,7 @@ Object EVMToEWasmTranslator::run(Object const& _object)
|
|||||||
|
|
||||||
ErrorList errors;
|
ErrorList errors;
|
||||||
ErrorReporter errorReporter(errors);
|
ErrorReporter errorReporter(errors);
|
||||||
AsmAnalyzer analyzer(*ret.analysisInfo, errorReporter, boost::none, WasmDialect::instance());
|
AsmAnalyzer analyzer(*ret.analysisInfo, errorReporter, boost::none, WasmDialect::instance(), {}, _object.dataNames());
|
||||||
if (!analyzer.analyze(*ret.code))
|
if (!analyzer.analyze(*ret.code))
|
||||||
{
|
{
|
||||||
// TODO the errors here are "wrong" because they have invalid source references!
|
// TODO the errors here are "wrong" because they have invalid source references!
|
||||||
|
@ -155,19 +155,20 @@ void eliminateVariables(
|
|||||||
|
|
||||||
bool StackCompressor::run(
|
bool StackCompressor::run(
|
||||||
Dialect const& _dialect,
|
Dialect const& _dialect,
|
||||||
Block& _ast,
|
Object& _object,
|
||||||
bool _optimizeStackAllocation,
|
bool _optimizeStackAllocation,
|
||||||
size_t _maxIterations
|
size_t _maxIterations
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
yulAssert(
|
yulAssert(
|
||||||
_ast.statements.size() > 0 && _ast.statements.at(0).type() == typeid(Block),
|
_object.code &&
|
||||||
|
_object.code->statements.size() > 0 && _object.code->statements.at(0).type() == typeid(Block),
|
||||||
"Need to run the function grouper before the stack compressor."
|
"Need to run the function grouper before the stack compressor."
|
||||||
);
|
);
|
||||||
bool allowMSizeOptimzation = !SideEffectsCollector(_dialect, _ast).containsMSize();
|
bool allowMSizeOptimzation = !SideEffectsCollector(_dialect, *_object.code).containsMSize();
|
||||||
for (size_t iterations = 0; iterations < _maxIterations; iterations++)
|
for (size_t iterations = 0; iterations < _maxIterations; iterations++)
|
||||||
{
|
{
|
||||||
map<YulString, int> stackSurplus = CompilabilityChecker::run(_dialect, _ast, _optimizeStackAllocation);
|
map<YulString, int> stackSurplus = CompilabilityChecker::run(_dialect, _object, _optimizeStackAllocation);
|
||||||
if (stackSurplus.empty())
|
if (stackSurplus.empty())
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
@ -176,15 +177,15 @@ bool StackCompressor::run(
|
|||||||
yulAssert(stackSurplus.at({}) > 0, "Invalid surplus value.");
|
yulAssert(stackSurplus.at({}) > 0, "Invalid surplus value.");
|
||||||
eliminateVariables(
|
eliminateVariables(
|
||||||
_dialect,
|
_dialect,
|
||||||
boost::get<Block>(_ast.statements.at(0)),
|
boost::get<Block>(_object.code->statements.at(0)),
|
||||||
stackSurplus.at({}),
|
stackSurplus.at({}),
|
||||||
allowMSizeOptimzation
|
allowMSizeOptimzation
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 1; i < _ast.statements.size(); ++i)
|
for (size_t i = 1; i < _object.code->statements.size(); ++i)
|
||||||
{
|
{
|
||||||
FunctionDefinition& fun = boost::get<FunctionDefinition>(_ast.statements[i]);
|
FunctionDefinition& fun = boost::get<FunctionDefinition>(_object.code->statements[i]);
|
||||||
if (!stackSurplus.count(fun.name))
|
if (!stackSurplus.count(fun.name))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
@ -27,13 +27,15 @@ namespace yul
|
|||||||
{
|
{
|
||||||
|
|
||||||
struct Dialect;
|
struct Dialect;
|
||||||
struct Block;
|
struct Object;
|
||||||
struct FunctionDefinition;
|
struct FunctionDefinition;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Optimisation stage that aggressively rematerializes certain variables in a function to free
|
* Optimisation stage that aggressively rematerializes certain variables in a function to free
|
||||||
* space on the stack until it is compilable.
|
* space on the stack until it is compilable.
|
||||||
*
|
*
|
||||||
|
* Only runs on the code of the object itself, does not descend into sub-objects.
|
||||||
|
*
|
||||||
* Prerequisite: Disambiguator, Function Grouper
|
* Prerequisite: Disambiguator, Function Grouper
|
||||||
*/
|
*/
|
||||||
class StackCompressor
|
class StackCompressor
|
||||||
@ -43,7 +45,7 @@ public:
|
|||||||
/// @returns true if it was successful.
|
/// @returns true if it was successful.
|
||||||
static bool run(
|
static bool run(
|
||||||
Dialect const& _dialect,
|
Dialect const& _dialect,
|
||||||
Block& _ast,
|
Object& _object,
|
||||||
bool _optimizeStackAllocation,
|
bool _optimizeStackAllocation,
|
||||||
size_t _maxIterations
|
size_t _maxIterations
|
||||||
);
|
);
|
||||||
|
@ -49,6 +49,7 @@
|
|||||||
#include <libyul/AsmAnalysisInfo.h>
|
#include <libyul/AsmAnalysisInfo.h>
|
||||||
#include <libyul/AsmData.h>
|
#include <libyul/AsmData.h>
|
||||||
#include <libyul/AsmPrinter.h>
|
#include <libyul/AsmPrinter.h>
|
||||||
|
#include <libyul/Object.h>
|
||||||
|
|
||||||
#include <libyul/backends/wasm/WasmDialect.h>
|
#include <libyul/backends/wasm/WasmDialect.h>
|
||||||
#include <libyul/backends/evm/NoOutputAssembly.h>
|
#include <libyul/backends/evm/NoOutputAssembly.h>
|
||||||
@ -62,8 +63,7 @@ using namespace yul;
|
|||||||
void OptimiserSuite::run(
|
void OptimiserSuite::run(
|
||||||
Dialect const& _dialect,
|
Dialect const& _dialect,
|
||||||
GasMeter const* _meter,
|
GasMeter const* _meter,
|
||||||
Block& _ast,
|
Object& _object,
|
||||||
AsmAnalysisInfo const& _analysisInfo,
|
|
||||||
bool _optimizeStackAllocation,
|
bool _optimizeStackAllocation,
|
||||||
set<YulString> const& _externallyUsedIdentifiers
|
set<YulString> const& _externallyUsedIdentifiers
|
||||||
)
|
)
|
||||||
@ -71,7 +71,12 @@ void OptimiserSuite::run(
|
|||||||
set<YulString> reservedIdentifiers = _externallyUsedIdentifiers;
|
set<YulString> reservedIdentifiers = _externallyUsedIdentifiers;
|
||||||
reservedIdentifiers += _dialect.fixedFunctionNames();
|
reservedIdentifiers += _dialect.fixedFunctionNames();
|
||||||
|
|
||||||
Block ast = boost::get<Block>(Disambiguator(_dialect, _analysisInfo, reservedIdentifiers)(_ast));
|
*_object.code = boost::get<Block>(Disambiguator(
|
||||||
|
_dialect,
|
||||||
|
*_object.analysisInfo,
|
||||||
|
reservedIdentifiers
|
||||||
|
)(*_object.code));
|
||||||
|
Block& ast = *_object.code;
|
||||||
|
|
||||||
VarDeclInitializer{}(ast);
|
VarDeclInitializer{}(ast);
|
||||||
FunctionHoister{}(ast);
|
FunctionHoister{}(ast);
|
||||||
@ -204,7 +209,12 @@ void OptimiserSuite::run(
|
|||||||
FunctionGrouper{}(ast);
|
FunctionGrouper{}(ast);
|
||||||
// We ignore the return value because we will get a much better error
|
// We ignore the return value because we will get a much better error
|
||||||
// message once we perform code generation.
|
// message once we perform code generation.
|
||||||
StackCompressor::run(_dialect, ast, _optimizeStackAllocation, stackCompressorMaxIterations);
|
StackCompressor::run(
|
||||||
|
_dialect,
|
||||||
|
_object,
|
||||||
|
_optimizeStackAllocation,
|
||||||
|
stackCompressorMaxIterations
|
||||||
|
);
|
||||||
BlockFlattener{}(ast);
|
BlockFlattener{}(ast);
|
||||||
DeadCodeEliminator{_dialect}(ast);
|
DeadCodeEliminator{_dialect}(ast);
|
||||||
ControlFlowSimplifier{_dialect}(ast);
|
ControlFlowSimplifier{_dialect}(ast);
|
||||||
@ -224,7 +234,6 @@ void OptimiserSuite::run(
|
|||||||
ast.statements.erase(ast.statements.begin());
|
ast.statements.erase(ast.statements.begin());
|
||||||
}
|
}
|
||||||
VarNameCleaner{ast, _dialect, reservedIdentifiers}(ast);
|
VarNameCleaner{ast, _dialect, reservedIdentifiers}(ast);
|
||||||
yul::AsmAnalyzer::analyzeStrictAssertCorrect(_dialect, ast);
|
|
||||||
|
|
||||||
_ast = std::move(ast);
|
*_object.analysisInfo = AsmAnalyzer::analyzeStrictAssertCorrect(_dialect, _object);
|
||||||
}
|
}
|
||||||
|
@ -32,9 +32,11 @@ namespace yul
|
|||||||
struct AsmAnalysisInfo;
|
struct AsmAnalysisInfo;
|
||||||
struct Dialect;
|
struct Dialect;
|
||||||
class GasMeter;
|
class GasMeter;
|
||||||
|
struct Object;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Optimiser suite that combines all steps and also provides the settings for the heuristics
|
* Optimiser suite that combines all steps and also provides the settings for the heuristics.
|
||||||
|
* Only optimizes the code of the provided object, does not descend into the sub-objects.
|
||||||
*/
|
*/
|
||||||
class OptimiserSuite
|
class OptimiserSuite
|
||||||
{
|
{
|
||||||
@ -42,8 +44,7 @@ public:
|
|||||||
static void run(
|
static void run(
|
||||||
Dialect const& _dialect,
|
Dialect const& _dialect,
|
||||||
GasMeter const* _meter,
|
GasMeter const* _meter,
|
||||||
Block& _ast,
|
Object& _object,
|
||||||
AsmAnalysisInfo const& _analysisInfo,
|
|
||||||
bool _optimizeStackAllocation,
|
bool _optimizeStackAllocation,
|
||||||
std::set<YulString> const& _externallyUsedIdentifiers = {}
|
std::set<YulString> const& _externallyUsedIdentifiers = {}
|
||||||
);
|
);
|
||||||
|
@ -37,9 +37,10 @@ namespace
|
|||||||
{
|
{
|
||||||
string check(string const& _input)
|
string check(string const& _input)
|
||||||
{
|
{
|
||||||
shared_ptr<Block> ast = yul::test::parse(_input, false).first;
|
Object obj;
|
||||||
BOOST_REQUIRE(ast);
|
std::tie(obj.code, obj.analysisInfo) = yul::test::parse(_input, false);
|
||||||
map<YulString, int> functions = CompilabilityChecker::run(EVMDialect::strictAssemblyForEVM(dev::test::Options::get().evmVersion()), *ast, true);
|
BOOST_REQUIRE(obj.code);
|
||||||
|
map<YulString, int> functions = CompilabilityChecker::run(EVMDialect::strictAssemblyForEVM(dev::test::Options::get().evmVersion()), obj, true);
|
||||||
string out;
|
string out;
|
||||||
for (auto const& function: functions)
|
for (auto const& function: functions)
|
||||||
out += function.first.str() + ": " + to_string(function.second) + " ";
|
out += function.first.str() + ": " + to_string(function.second) + " ";
|
||||||
|
@ -278,6 +278,19 @@ BOOST_AUTO_TEST_CASE(args_to_datacopy_are_arbitrary)
|
|||||||
BOOST_CHECK(successParse(code));
|
BOOST_CHECK(successParse(code));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(non_existing_objects)
|
||||||
|
{
|
||||||
|
BOOST_CHECK(successParse(
|
||||||
|
"object \"main\" { code { pop(datasize(\"main\")) } }"
|
||||||
|
));
|
||||||
|
CHECK_ERROR(
|
||||||
|
"object \"main\" { code { pop(datasize(\"abc\")) } }",
|
||||||
|
TypeError,
|
||||||
|
"Unknown data object"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -574,6 +574,7 @@ BOOST_AUTO_TEST_CASE(builtins_analysis)
|
|||||||
CHECK_ERROR_DIALECT("{ let a, b := builtin(1, 2) }", DeclarationError, "Variable count mismatch: 2 variables and 3 values.", dialect);
|
CHECK_ERROR_DIALECT("{ let a, b := builtin(1, 2) }", DeclarationError, "Variable count mismatch: 2 variables and 3 values.", dialect);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -305,7 +305,10 @@ TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _line
|
|||||||
disambiguate();
|
disambiguate();
|
||||||
(FunctionGrouper{})(*m_ast);
|
(FunctionGrouper{})(*m_ast);
|
||||||
size_t maxIterations = 16;
|
size_t maxIterations = 16;
|
||||||
StackCompressor::run(*m_dialect, *m_ast, true, maxIterations);
|
Object obj;
|
||||||
|
obj.code = m_ast;
|
||||||
|
StackCompressor::run(*m_dialect, obj, true, maxIterations);
|
||||||
|
m_ast = obj.code;
|
||||||
(BlockFlattener{})(*m_ast);
|
(BlockFlattener{})(*m_ast);
|
||||||
}
|
}
|
||||||
else if (m_optimizerStep == "wordSizeTransform")
|
else if (m_optimizerStep == "wordSizeTransform")
|
||||||
@ -318,7 +321,10 @@ TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _line
|
|||||||
else if (m_optimizerStep == "fullSuite")
|
else if (m_optimizerStep == "fullSuite")
|
||||||
{
|
{
|
||||||
GasMeter meter(dynamic_cast<EVMDialect const&>(*m_dialect), false, 200);
|
GasMeter meter(dynamic_cast<EVMDialect const&>(*m_dialect), false, 200);
|
||||||
OptimiserSuite::run(*m_dialect, &meter, *m_ast, *m_analysisInfo, true);
|
yul::Object obj;
|
||||||
|
obj.code = m_ast;
|
||||||
|
obj.analysisInfo = m_analysisInfo;
|
||||||
|
OptimiserSuite::run(*m_dialect, &meter, obj, true);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
{
|
object "main" {
|
||||||
|
code {
|
||||||
// Arguments to ``datasize`` and ``dataoffset`` need to be
|
// Arguments to ``datasize`` and ``dataoffset`` need to be
|
||||||
// literals. We cannot simplify their arguments, but we can
|
// literals. We cannot simplify their arguments, but we can
|
||||||
// simplify them as a full expression.
|
// simplify them as a full expression.
|
||||||
@ -11,6 +12,8 @@
|
|||||||
datacopy("abc", x, y)
|
datacopy("abc", x, y)
|
||||||
mstore(a, x)
|
mstore(a, x)
|
||||||
}
|
}
|
||||||
|
data "abc" "Hello, World!"
|
||||||
|
}
|
||||||
// ====
|
// ====
|
||||||
// step: commonSubexpressionEliminator
|
// step: commonSubexpressionEliminator
|
||||||
// ----
|
// ----
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
{
|
object "main" {
|
||||||
|
code {
|
||||||
// We should never split arguments to ``dataoffset``
|
// We should never split arguments to ``dataoffset``
|
||||||
// or ``datasize`` because they need to be literals
|
// or ``datasize`` because they need to be literals
|
||||||
let x := dataoffset("abc")
|
let x := dataoffset("abc")
|
||||||
@ -6,6 +7,8 @@
|
|||||||
// datacopy is fine, though
|
// datacopy is fine, though
|
||||||
datacopy(mload(0), mload(1), mload(2))
|
datacopy(mload(0), mload(1), mload(2))
|
||||||
}
|
}
|
||||||
|
data "abc" "Hello, World!"
|
||||||
|
}
|
||||||
// ====
|
// ====
|
||||||
// step: expressionSplitter
|
// step: expressionSplitter
|
||||||
// ----
|
// ----
|
||||||
|
@ -27,6 +27,7 @@
|
|||||||
#include <libyul/AsmData.h>
|
#include <libyul/AsmData.h>
|
||||||
#include <libyul/AsmParser.h>
|
#include <libyul/AsmParser.h>
|
||||||
#include <libyul/AsmPrinter.h>
|
#include <libyul/AsmPrinter.h>
|
||||||
|
#include <libyul/Object.h>
|
||||||
#include <liblangutil/SourceReferenceFormatter.h>
|
#include <liblangutil/SourceReferenceFormatter.h>
|
||||||
|
|
||||||
#include <libyul/optimiser/BlockFlattener.h>
|
#include <libyul/optimiser/BlockFlattener.h>
|
||||||
@ -207,8 +208,12 @@ public:
|
|||||||
SSAReverser::run(*m_ast);
|
SSAReverser::run(*m_ast);
|
||||||
break;
|
break;
|
||||||
case 'p':
|
case 'p':
|
||||||
StackCompressor::run(m_dialect, *m_ast, true, 16);
|
{
|
||||||
|
Object obj;
|
||||||
|
obj.code = m_ast;
|
||||||
|
StackCompressor::run(m_dialect, obj, true, 16);
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
cout << "Unknown option." << endl;
|
cout << "Unknown option." << endl;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user