Yul optimizer test: Separate test parsing and optimisation

This commit is contained in:
Bhargava Shastry 2020-12-28 16:39:14 +01:00
parent 055c4b4d44
commit 42c05df0a9
6 changed files with 552 additions and 376 deletions

View File

@ -137,6 +137,8 @@ set(libyul_sources
libyul/YulInterpreterTest.h
libyul/YulOptimizerTest.cpp
libyul/YulOptimizerTest.h
libyul/YulOptimizerTestCommon.cpp
libyul/YulOptimizerTestCommon.h
)
detect_stray_source_files("${libyul_sources}" "libyul/")

View File

@ -17,72 +17,25 @@
// SPDX-License-Identifier: GPL-3.0
#include <test/libyul/YulOptimizerTest.h>
#include <test/libyul/YulOptimizerTestCommon.h>
#include <test/libsolidity/util/SoltestErrors.h>
#include <test/libyul/Common.h>
#include <test/Common.h>
#include <libyul/optimiser/BlockFlattener.h>
#include <libyul/optimiser/VarDeclInitializer.h>
#include <libyul/optimiser/VarNameCleaner.h>
#include <libyul/optimiser/ControlFlowSimplifier.h>
#include <libyul/optimiser/DeadCodeEliminator.h>
#include <libyul/optimiser/Disambiguator.h>
#include <libyul/optimiser/CallGraphGenerator.h>
#include <libyul/optimiser/CircularReferencesPruner.h>
#include <libyul/optimiser/ConditionalUnsimplifier.h>
#include <libyul/optimiser/ConditionalSimplifier.h>
#include <libyul/optimiser/CommonSubexpressionEliminator.h>
#include <libyul/optimiser/NameCollector.h>
#include <libyul/optimiser/EquivalentFunctionCombiner.h>
#include <libyul/optimiser/ExpressionSplitter.h>
#include <libyul/optimiser/FunctionGrouper.h>
#include <libyul/optimiser/FunctionHoister.h>
#include <libyul/optimiser/ExpressionInliner.h>
#include <libyul/optimiser/FullInliner.h>
#include <libyul/optimiser/ForLoopConditionIntoBody.h>
#include <libyul/optimiser/ForLoopConditionOutOfBody.h>
#include <libyul/optimiser/ForLoopInitRewriter.h>
#include <libyul/optimiser/LoadResolver.h>
#include <libyul/optimiser/LoopInvariantCodeMotion.h>
#include <libyul/optimiser/MainFunction.h>
#include <libyul/optimiser/StackLimitEvader.h>
#include <libyul/optimiser/NameDisplacer.h>
#include <libyul/optimiser/Rematerialiser.h>
#include <libyul/optimiser/ExpressionSimplifier.h>
#include <libyul/optimiser/UnusedFunctionParameterPruner.h>
#include <libyul/optimiser/UnusedPruner.h>
#include <libyul/optimiser/ExpressionJoiner.h>
#include <libyul/optimiser/OptimiserStep.h>
#include <libyul/optimiser/ReasoningBasedSimplifier.h>
#include <libyul/optimiser/SSAReverser.h>
#include <libyul/optimiser/SSATransform.h>
#include <libyul/optimiser/Semantics.h>
#include <libyul/optimiser/RedundantAssignEliminator.h>
#include <libyul/optimiser/StructuralSimplifier.h>
#include <libyul/optimiser/StackCompressor.h>
#include <libyul/optimiser/StackToMemoryMover.h>
#include <libyul/optimiser/Suite.h>
#include <libyul/backends/evm/ConstantOptimiser.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <libyul/backends/evm/EVMMetrics.h>
#include <libyul/backends/wasm/WordSizeTransform.h>
#include <libyul/backends/wasm/WasmDialect.h>
#include <libyul/AsmPrinter.h>
#include <libyul/AsmParser.h>
#include <libyul/AsmAnalysis.h>
#include <libyul/AssemblyStack.h>
#include <libyul/CompilabilityChecker.h>
#include <liblangutil/SourceReferenceFormatter.h>
#include <liblangutil/ErrorReporter.h>
#include <liblangutil/Scanner.h>
#include <libsolutil/AnsiColorized.h>
#include <libsolidity/interface/OptimiserSettings.h>
#include <boost/test/unit_test.hpp>
#include <boost/algorithm/string.hpp>
#include <fstream>
@ -128,312 +81,12 @@ TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _line
soltestAssert(m_dialect, "Dialect not set.");
updateContext();
m_object->analysisInfo = m_analysisInfo;
YulOptimizerTester tester(m_object, *m_dialect, m_optimizerStep, false);
if (m_optimizerStep == "disambiguator")
disambiguate();
else if (m_optimizerStep == "nameDisplacer")
{
disambiguate();
NameDisplacer{
*m_nameDispenser,
{"illegal1"_yulstring, "illegal2"_yulstring, "illegal3"_yulstring, "illegal4"_yulstring, "illegal5"_yulstring}
}(*m_object->code);
}
else if (m_optimizerStep == "blockFlattener")
{
disambiguate();
BlockFlattener::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "constantOptimiser")
{
GasMeter meter(dynamic_cast<EVMDialect const&>(*m_dialect), false, 200);
ConstantOptimiser{dynamic_cast<EVMDialect const&>(*m_dialect), meter}(*m_object->code);
}
else if (m_optimizerStep == "varDeclInitializer")
VarDeclInitializer::run(*m_context, *m_object->code);
else if (m_optimizerStep == "varNameCleaner")
{
disambiguate();
FunctionGrouper::run(*m_context, *m_object->code);
VarNameCleaner::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "forLoopConditionIntoBody")
{
disambiguate();
ForLoopConditionIntoBody::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "forLoopInitRewriter")
{
disambiguate();
ForLoopInitRewriter::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "commonSubexpressionEliminator")
{
disambiguate();
CommonSubexpressionEliminator::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "conditionalUnsimplifier")
{
disambiguate();
ConditionalUnsimplifier::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "conditionalSimplifier")
{
disambiguate();
ConditionalSimplifier::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "expressionSplitter")
ExpressionSplitter::run(*m_context, *m_object->code);
else if (m_optimizerStep == "expressionJoiner")
{
disambiguate();
ExpressionJoiner::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "splitJoin")
{
disambiguate();
ExpressionSplitter::run(*m_context, *m_object->code);
ExpressionJoiner::run(*m_context, *m_object->code);
ExpressionJoiner::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "functionGrouper")
{
disambiguate();
FunctionGrouper::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "functionHoister")
{
disambiguate();
FunctionHoister::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "expressionInliner")
{
disambiguate();
ExpressionInliner::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "fullInliner")
{
disambiguate();
FunctionHoister::run(*m_context, *m_object->code);
FunctionGrouper::run(*m_context, *m_object->code);
ExpressionSplitter::run(*m_context, *m_object->code);
FullInliner::run(*m_context, *m_object->code);
ExpressionJoiner::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "mainFunction")
{
disambiguate();
FunctionGrouper::run(*m_context, *m_object->code);
MainFunction::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "rematerialiser")
{
disambiguate();
Rematerialiser::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "expressionSimplifier")
{
disambiguate();
ExpressionSplitter::run(*m_context, *m_object->code);
CommonSubexpressionEliminator::run(*m_context, *m_object->code);
ExpressionSimplifier::run(*m_context, *m_object->code);
ExpressionSimplifier::run(*m_context, *m_object->code);
ExpressionSimplifier::run(*m_context, *m_object->code);
UnusedPruner::run(*m_context, *m_object->code);
ExpressionJoiner::run(*m_context, *m_object->code);
ExpressionJoiner::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "fullSimplify")
{
disambiguate();
ExpressionSplitter::run(*m_context, *m_object->code);
ForLoopInitRewriter::run(*m_context, *m_object->code);
CommonSubexpressionEliminator::run(*m_context, *m_object->code);
ExpressionSimplifier::run(*m_context, *m_object->code);
UnusedPruner::run(*m_context, *m_object->code);
CircularReferencesPruner::run(*m_context, *m_object->code);
DeadCodeEliminator::run(*m_context, *m_object->code);
ExpressionJoiner::run(*m_context, *m_object->code);
ExpressionJoiner::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "unusedFunctionParameterPruner")
{
disambiguate();
FunctionHoister::run(*m_context, *m_object->code);
LiteralRematerialiser::run(*m_context, *m_object->code);
UnusedFunctionParameterPruner::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "unusedPruner")
{
disambiguate();
UnusedPruner::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "circularReferencesPruner")
{
disambiguate();
FunctionHoister::run(*m_context, *m_object->code);
CircularReferencesPruner::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "deadCodeEliminator")
{
disambiguate();
ForLoopInitRewriter::run(*m_context, *m_object->code);
DeadCodeEliminator::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "ssaTransform")
{
disambiguate();
ForLoopInitRewriter::run(*m_context, *m_object->code);
SSATransform::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "redundantAssignEliminator")
{
disambiguate();
RedundantAssignEliminator::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "ssaPlusCleanup")
{
disambiguate();
SSATransform::run(*m_context, *m_object->code);
RedundantAssignEliminator::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "loadResolver")
{
disambiguate();
ForLoopInitRewriter::run(*m_context, *m_object->code);
ExpressionSplitter::run(*m_context, *m_object->code);
CommonSubexpressionEliminator::run(*m_context, *m_object->code);
ExpressionSimplifier::run(*m_context, *m_object->code);
bool success = tester.runStep();
LoadResolver::run(*m_context, *m_object->code);
UnusedPruner::run(*m_context, *m_object->code);
ExpressionJoiner::run(*m_context, *m_object->code);
ExpressionJoiner::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "loopInvariantCodeMotion")
{
disambiguate();
ForLoopInitRewriter::run(*m_context, *m_object->code);
LoopInvariantCodeMotion::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "controlFlowSimplifier")
{
disambiguate();
ControlFlowSimplifier::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "structuralSimplifier")
{
disambiguate();
ForLoopInitRewriter::run(*m_context, *m_object->code);
LiteralRematerialiser::run(*m_context, *m_object->code);
StructuralSimplifier::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "reasoningBasedSimplifier")
{
disambiguate();
ReasoningBasedSimplifier::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "equivalentFunctionCombiner")
{
disambiguate();
EquivalentFunctionCombiner::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "ssaReverser")
{
disambiguate();
SSAReverser::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "ssaAndBack")
{
disambiguate();
// apply SSA
SSATransform::run(*m_context, *m_object->code);
RedundantAssignEliminator::run(*m_context, *m_object->code);
// reverse SSA
SSAReverser::run(*m_context, *m_object->code);
CommonSubexpressionEliminator::run(*m_context, *m_object->code);
UnusedPruner::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "stackCompressor")
{
disambiguate();
FunctionGrouper::run(*m_context, *m_object->code);
size_t maxIterations = 16;
Object obj;
obj.code = m_object->code;
StackCompressor::run(*m_dialect, obj, true, maxIterations);
m_object->code = obj.code;
BlockFlattener::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "wordSizeTransform")
{
disambiguate();
ExpressionSplitter::run(*m_context, *m_object->code);
WordSizeTransform::run(*m_dialect, *m_dialect, *m_object->code, *m_nameDispenser);
}
else if (m_optimizerStep == "fullSuite")
{
GasMeter meter(dynamic_cast<EVMDialect const&>(*m_dialect), false, 200);
yul::Object obj;
obj.code = m_object->code;
obj.analysisInfo = m_analysisInfo;
OptimiserSuite::run(*m_dialect, &meter, obj, true, solidity::frontend::OptimiserSettings::DefaultYulOptimiserSteps);
}
else if (m_optimizerStep == "stackLimitEvader")
{
yul::Object obj;
obj.code = m_object->code;
obj.analysisInfo = m_analysisInfo;
disambiguate();
StackLimitEvader::run(*m_context, obj, CompilabilityChecker{
*m_dialect,
obj,
true
}.unreachableVariables);
}
else if (m_optimizerStep == "fakeStackLimitEvader")
{
yul::Object obj;
obj.code = m_object->code;
obj.analysisInfo = m_analysisInfo;
disambiguate();
// Mark all variables with a name starting with "$" for escalation to memory.
struct FakeUnreachableGenerator: ASTWalker
{
map<YulString, set<YulString>> fakeUnreachables;
using ASTWalker::operator();
void operator()(FunctionDefinition const& _function) override
{
YulString originalFunctionName = m_currentFunction;
m_currentFunction = _function.name;
ASTWalker::operator()(_function);
m_currentFunction = originalFunctionName;
}
void visitVariableName(YulString _var)
{
if (!_var.empty() && _var.str().front() == '$')
fakeUnreachables[m_currentFunction].insert(_var);
}
void operator()(VariableDeclaration const& _varDecl) override
{
for (auto const& var: _varDecl.variables)
visitVariableName(var.name);
ASTWalker::operator()(_varDecl);
}
void operator()(Identifier const& _identifier) override
{
visitVariableName(_identifier.name);
ASTWalker::operator()(_identifier);
}
YulString m_currentFunction = YulString{};
};
FakeUnreachableGenerator fakeUnreachableGenerator;
fakeUnreachableGenerator(*obj.code);
StackLimitEvader::run(*m_context, obj, fakeUnreachableGenerator.fakeUnreachables);
}
else
if (!success)
{
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Invalid optimizer step: " << m_optimizerStep << endl;
return TestResult::FatalError;
@ -477,23 +130,6 @@ std::pair<std::shared_ptr<Object>, std::shared_ptr<AsmAnalysisInfo>> YulOptimize
return {std::move(object), std::move(analysisInfo)};
}
void YulOptimizerTest::disambiguate()
{
*m_object->code = std::get<Block>(Disambiguator(*m_dialect, *m_analysisInfo)(*m_object->code));
m_analysisInfo.reset();
updateContext();
}
void YulOptimizerTest::updateContext()
{
m_nameDispenser = make_unique<NameDispenser>(*m_dialect, *m_object->code, m_reservedIdentifiers);
m_context = make_unique<OptimiserStepContext>(OptimiserStepContext{
*m_dialect,
*m_nameDispenser,
m_reservedIdentifiers
});
}
void YulOptimizerTest::printErrors(ostream& _stream, ErrorList const& _errors)
{
SourceReferenceFormatter formatter(_stream, true, false);

View File

@ -56,22 +56,15 @@ public:
explicit YulOptimizerTest(std::string const& _filename);
TestResult run(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) override;
private:
std::pair<std::shared_ptr<Object>, std::shared_ptr<AsmAnalysisInfo>> parse(
std::ostream& _stream, std::string const& _linePrefix, bool const _formatted, std::string const& _source
);
void disambiguate();
void updateContext();
static void printErrors(std::ostream& _stream, langutil::ErrorList const& _errors);
std::string m_optimizerStep;
Dialect const* m_dialect = nullptr;
std::set<YulString> m_reservedIdentifiers;
std::unique_ptr<NameDispenser> m_nameDispenser;
std::unique_ptr<OptimiserStepContext> m_context;
std::shared_ptr<Object> m_object;
std::shared_ptr<AsmAnalysisInfo> m_analysisInfo;

View File

@ -0,0 +1,467 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <test/libyul/YulOptimizerTestCommon.h>
#include <libyul/optimiser/BlockFlattener.h>
#include <libyul/optimiser/VarDeclInitializer.h>
#include <libyul/optimiser/VarNameCleaner.h>
#include <libyul/optimiser/ControlFlowSimplifier.h>
#include <libyul/optimiser/DeadCodeEliminator.h>
#include <libyul/optimiser/Disambiguator.h>
#include <libyul/optimiser/CircularReferencesPruner.h>
#include <libyul/optimiser/ConditionalUnsimplifier.h>
#include <libyul/optimiser/ConditionalSimplifier.h>
#include <libyul/optimiser/CommonSubexpressionEliminator.h>
#include <libyul/optimiser/EquivalentFunctionCombiner.h>
#include <libyul/optimiser/ExpressionSplitter.h>
#include <libyul/optimiser/FunctionGrouper.h>
#include <libyul/optimiser/FunctionHoister.h>
#include <libyul/optimiser/ExpressionInliner.h>
#include <libyul/optimiser/FullInliner.h>
#include <libyul/optimiser/ForLoopConditionIntoBody.h>
#include <libyul/optimiser/ForLoopInitRewriter.h>
#include <libyul/optimiser/LoadResolver.h>
#include <libyul/optimiser/LoopInvariantCodeMotion.h>
#include <libyul/optimiser/MainFunction.h>
#include <libyul/optimiser/StackLimitEvader.h>
#include <libyul/optimiser/NameDisplacer.h>
#include <libyul/optimiser/Rematerialiser.h>
#include <libyul/optimiser/ExpressionSimplifier.h>
#include <libyul/optimiser/UnusedFunctionParameterPruner.h>
#include <libyul/optimiser/UnusedPruner.h>
#include <libyul/optimiser/ExpressionJoiner.h>
#include <libyul/optimiser/OptimiserStep.h>
#include <libyul/optimiser/ReasoningBasedSimplifier.h>
#include <libyul/optimiser/SSAReverser.h>
#include <libyul/optimiser/SSATransform.h>
#include <libyul/optimiser/Semantics.h>
#include <libyul/optimiser/RedundantAssignEliminator.h>
#include <libyul/optimiser/StructuralSimplifier.h>
#include <libyul/optimiser/StackCompressor.h>
#include <libyul/optimiser/Suite.h>
#include <libyul/backends/evm/ConstantOptimiser.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <libyul/backends/evm/EVMMetrics.h>
#include <libyul/backends/wasm/WordSizeTransform.h>
#include <libyul/backends/wasm/WasmDialect.h>
#include <libyul/AsmPrinter.h>
#include <libyul/AsmAnalysis.h>
#include <libyul/CompilabilityChecker.h>
#include <libsolidity/interface/OptimiserSettings.h>
#include <variant>
using namespace solidity;
using namespace solidity::util;
using namespace solidity::langutil;
using namespace solidity::yul;
using namespace solidity::yul::test;
using namespace solidity::frontend;
using namespace std;
YulOptimizerTester::YulOptimizerTester(
shared_ptr<Object> _obj,
Dialect const& _dialect,
string const& _optimizerStep,
bool _fuzzerMode
)
{
m_object = _obj;
m_ast = m_object->code;
m_analysisInfo = m_object->analysisInfo;
m_dialect = &_dialect;
m_optimizerStep = _optimizerStep;
m_fuzzerMode = _fuzzerMode;
}
bool YulOptimizerTester::runStep()
{
yulAssert(m_dialect, "Dialect not set.");
updateContext();
if (m_optimizerStep == "disambiguator")
disambiguate();
else if (m_optimizerStep == "nameDisplacer")
{
disambiguate();
NameDisplacer{
*m_nameDispenser,
{"illegal1"_yulstring, "illegal2"_yulstring, "illegal3"_yulstring, "illegal4"_yulstring, "illegal5"_yulstring}
}(*m_ast);
}
else if (m_optimizerStep == "blockFlattener")
{
disambiguate();
BlockFlattener::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "constantOptimiser")
{
GasMeter meter(dynamic_cast<EVMDialect const&>(*m_dialect), false, 200);
ConstantOptimiser{dynamic_cast<EVMDialect const&>(*m_dialect), meter}(*m_ast);
}
else if (m_optimizerStep == "varDeclInitializer")
VarDeclInitializer::run(*m_context, *m_ast);
else if (m_optimizerStep == "varNameCleaner")
{
disambiguate();
if (m_fuzzerMode)
FunctionHoister::run(*m_context, *m_ast);
FunctionGrouper::run(*m_context, *m_ast);
VarNameCleaner::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "forLoopConditionIntoBody")
{
disambiguate();
ForLoopConditionIntoBody::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "forLoopInitRewriter")
{
disambiguate();
ForLoopInitRewriter::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "commonSubexpressionEliminator")
{
disambiguate();
if (m_fuzzerMode)
{
ForLoopInitRewriter::run(*m_context, *m_ast);
FunctionHoister::run(*m_context, *m_ast);
}
CommonSubexpressionEliminator::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "conditionalUnsimplifier")
{
disambiguate();
ConditionalUnsimplifier::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "conditionalSimplifier")
{
disambiguate();
ConditionalSimplifier::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "expressionSplitter")
ExpressionSplitter::run(*m_context, *m_ast);
else if (m_optimizerStep == "expressionJoiner")
{
disambiguate();
ExpressionJoiner::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "splitJoin")
{
disambiguate();
ExpressionSplitter::run(*m_context, *m_ast);
ExpressionJoiner::run(*m_context, *m_ast);
ExpressionJoiner::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "functionGrouper")
{
disambiguate();
FunctionGrouper::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "functionHoister")
{
disambiguate();
FunctionHoister::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "expressionInliner")
{
disambiguate();
ExpressionInliner::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "fullInliner")
{
disambiguate();
FunctionHoister::run(*m_context, *m_ast);
FunctionGrouper::run(*m_context, *m_ast);
ExpressionSplitter::run(*m_context, *m_ast);
FullInliner::run(*m_context, *m_ast);
ExpressionJoiner::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "mainFunction")
{
disambiguate();
FunctionGrouper::run(*m_context, *m_ast);
MainFunction::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "rematerialiser")
{
disambiguate();
if (m_fuzzerMode)
{
ForLoopInitRewriter::run(*m_context, *m_ast);
FunctionHoister::run(*m_context, *m_ast);
}
Rematerialiser::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "expressionSimplifier")
{
disambiguate();
if (m_fuzzerMode)
{
ForLoopInitRewriter::run(*m_context, *m_ast);
FunctionHoister::run(*m_context, *m_ast);
}
ExpressionSplitter::run(*m_context, *m_ast);
CommonSubexpressionEliminator::run(*m_context, *m_ast);
ExpressionSimplifier::run(*m_context, *m_ast);
ExpressionSimplifier::run(*m_context, *m_ast);
ExpressionSimplifier::run(*m_context, *m_ast);
UnusedPruner::run(*m_context, *m_ast);
ExpressionJoiner::run(*m_context, *m_ast);
ExpressionJoiner::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "fullSimplify")
{
disambiguate();
ExpressionSplitter::run(*m_context, *m_ast);
ForLoopInitRewriter::run(*m_context, *m_ast);
if (m_fuzzerMode)
FunctionHoister::run(*m_context, *m_ast);
CommonSubexpressionEliminator::run(*m_context, *m_ast);
ExpressionSimplifier::run(*m_context, *m_ast);
UnusedPruner::run(*m_context, *m_ast);
CircularReferencesPruner::run(*m_context, *m_ast);
DeadCodeEliminator::run(*m_context, *m_ast);
ExpressionJoiner::run(*m_context, *m_ast);
ExpressionJoiner::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "unusedFunctionParameterPruner")
{
disambiguate();
if (m_fuzzerMode)
ForLoopInitRewriter::run(*m_context, *m_ast);
FunctionHoister::run(*m_context, *m_object->code);
LiteralRematerialiser::run(*m_context, *m_object->code);
UnusedFunctionParameterPruner::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "unusedPruner")
{
disambiguate();
UnusedPruner::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "circularReferencesPruner")
{
disambiguate();
FunctionHoister::run(*m_context, *m_ast);
CircularReferencesPruner::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "deadCodeEliminator")
{
disambiguate();
ForLoopInitRewriter::run(*m_context, *m_ast);
DeadCodeEliminator::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "ssaTransform")
{
disambiguate();
ForLoopInitRewriter::run(*m_context, *m_ast);
SSATransform::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "redundantAssignEliminator")
{
disambiguate();
ForLoopInitRewriter::run(*m_context, *m_ast);
RedundantAssignEliminator::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "ssaPlusCleanup")
{
disambiguate();
if (m_fuzzerMode)
ForLoopInitRewriter::run(*m_context, *m_ast);
SSATransform::run(*m_context, *m_ast);
RedundantAssignEliminator::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "loadResolver")
{
disambiguate();
ForLoopInitRewriter::run(*m_context, *m_ast);
if (m_fuzzerMode)
FunctionHoister::run(*m_context, *m_ast);
ExpressionSplitter::run(*m_context, *m_ast);
CommonSubexpressionEliminator::run(*m_context, *m_ast);
ExpressionSimplifier::run(*m_context, *m_ast);
LoadResolver::run(*m_context, *m_ast);
UnusedPruner::run(*m_context, *m_ast);
ExpressionJoiner::run(*m_context, *m_ast);
ExpressionJoiner::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "loopInvariantCodeMotion")
{
disambiguate();
ForLoopInitRewriter::run(*m_context, *m_ast);
if (m_fuzzerMode)
FunctionHoister::run(*m_context, *m_ast);
LoopInvariantCodeMotion::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "controlFlowSimplifier")
{
disambiguate();
if (m_fuzzerMode)
ForLoopInitRewriter::run(*m_context, *m_ast);
ControlFlowSimplifier::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "structuralSimplifier")
{
disambiguate();
ForLoopInitRewriter::run(*m_context, *m_ast);
if (m_fuzzerMode)
FunctionHoister::run(*m_context, *m_ast);
LiteralRematerialiser::run(*m_context, *m_ast);
StructuralSimplifier::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "reasoningBasedSimplifier")
{
disambiguate();
ReasoningBasedSimplifier::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "equivalentFunctionCombiner")
{
disambiguate();
if (m_fuzzerMode)
{
ForLoopInitRewriter::run(*m_context, *m_ast);
FunctionHoister::run(*m_context, *m_ast);
}
EquivalentFunctionCombiner::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "ssaReverser")
{
disambiguate();
SSAReverser::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "ssaAndBack")
{
disambiguate();
if (m_fuzzerMode)
ForLoopInitRewriter::run(*m_context, *m_ast);
// apply SSA
SSATransform::run(*m_context, *m_ast);
RedundantAssignEliminator::run(*m_context, *m_ast);
// reverse SSA
SSAReverser::run(*m_context, *m_ast);
if (m_fuzzerMode)
FunctionHoister::run(*m_context, *m_ast);
CommonSubexpressionEliminator::run(*m_context, *m_ast);
UnusedPruner::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "stackCompressor")
{
disambiguate();
if (m_fuzzerMode)
{
ForLoopInitRewriter::run(*m_context, *m_ast);
FunctionHoister::run(*m_context, *m_ast);
}
FunctionGrouper::run(*m_context, *m_ast);
size_t maxIterations = 16;
StackCompressor::run(*m_dialect, *m_object, true, maxIterations);
BlockFlattener::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "wordSizeTransform")
{
disambiguate();
ExpressionSplitter::run(*m_context, *m_ast);
WordSizeTransform::run(*m_dialect, *m_dialect, *m_ast, *m_nameDispenser);
}
else if (m_optimizerStep == "fullSuite")
{
GasMeter meter(dynamic_cast<EVMDialect const&>(*m_dialect), false, 200);
OptimiserSuite::run(*m_dialect, &meter, *m_object, true, solidity::frontend::OptimiserSettings::DefaultYulOptimiserSteps);
}
else if (m_optimizerStep == "stackLimitEvader")
{
disambiguate();
StackLimitEvader::run(*m_context, *m_object, CompilabilityChecker{
*m_dialect,
*m_object,
true
}.unreachableVariables);
}
else if (m_optimizerStep == "fakeStackLimitEvader")
{
disambiguate();
// Mark all variables with a name starting with "$" for escalation to memory.
struct FakeUnreachableGenerator: ASTWalker
{
map<YulString, set<YulString>> fakeUnreachables;
using ASTWalker::operator();
void operator()(FunctionDefinition const& _function) override
{
YulString originalFunctionName = m_currentFunction;
m_currentFunction = _function.name;
ASTWalker::operator()(_function);
m_currentFunction = originalFunctionName;
}
void visitVariableName(YulString _var)
{
if (!_var.empty() && _var.str().front() == '$')
fakeUnreachables[m_currentFunction].insert(_var);
}
void operator()(VariableDeclaration const& _varDecl) override
{
for (auto const& var: _varDecl.variables)
visitVariableName(var.name);
ASTWalker::operator()(_varDecl);
}
void operator()(Identifier const& _identifier) override
{
visitVariableName(_identifier.name);
ASTWalker::operator()(_identifier);
}
YulString m_currentFunction = YulString{};
};
FakeUnreachableGenerator fakeUnreachableGenerator;
fakeUnreachableGenerator(*m_ast);
StackLimitEvader::run(*m_context, *m_object, fakeUnreachableGenerator.fakeUnreachables);
}
else
return false;
return true;
}
shared_ptr<Block> YulOptimizerTester::run()
{
bool success = runStep();
if (!success)
return nullptr;
else
return m_ast;
}
void YulOptimizerTester::disambiguate()
{
*m_object->code = std::get<Block>(Disambiguator(*m_dialect, *m_analysisInfo)(*m_object->code));
m_analysisInfo.reset();
updateContext();
}
void YulOptimizerTester::updateContext()
{
m_nameDispenser = make_unique<NameDispenser>(*m_dialect, *m_object->code, m_reservedIdentifiers);
m_context = make_unique<OptimiserStepContext>(OptimiserStepContext{
*m_dialect,
*m_nameDispenser,
m_reservedIdentifiers
});
}

View File

@ -0,0 +1,77 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include <libyul/optimiser/OptimiserStep.h>
#include <libyul/optimiser/NameDispenser.h>
#include <libyul/YulString.h>
#include <set>
#include <memory>
namespace solidity::langutil
{
class Scanner;
class Error;
using ErrorList = std::vector<std::shared_ptr<Error const>>;
}
namespace solidity::yul
{
struct AsmAnalysisInfo;
struct Object;
struct Dialect;
}
namespace solidity::yul::test
{
class YulOptimizerTester
{
public:
explicit YulOptimizerTester(
std::shared_ptr<Object> _obj,
Dialect const& _dialect,
std::string const& _optimizerStep,
bool _fuzzerMode
);
/// Runs chosen optimiser step returning pointer
/// to yul AST Block post optimisation.
std::shared_ptr<Block> run();
/// Runs chosen optimiser step returning true if
/// successful, false otherwise.
bool runStep();
private:
void disambiguate();
void updateContext();
std::string m_optimizerStep;
Dialect const* m_dialect = nullptr;
std::set<YulString> m_reservedIdentifiers;
std::unique_ptr<NameDispenser> m_nameDispenser;
std::unique_ptr<OptimiserStepContext> m_context;
std::shared_ptr<Object> m_object;
std::shared_ptr<Block> m_ast;
std::shared_ptr<AsmAnalysisInfo> m_analysisInfo;
bool m_fuzzerMode;
};
}

View File

@ -37,6 +37,7 @@ add_executable(isoltest
../libyul/ObjectCompilerTest.cpp
../libyul/SyntaxTest.cpp
../libyul/YulOptimizerTest.cpp
../libyul/YulOptimizerTestCommon.cpp
../libyul/YulInterpreterTest.cpp
)
target_link_libraries(isoltest PRIVATE evmc libsolc solidity yulInterpreter evmasm Boost::boost Boost::program_options Boost::unit_test_framework)