solidity/test/libyul/YulOptimizerTest.cpp

375 lines
12 KiB
C++
Raw Normal View History

2018-10-09 13:43:55 +00:00
/*
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/>.
*/
2018-10-15 09:52:35 +00:00
#include <test/libyul/YulOptimizerTest.h>
2018-10-09 13:43:55 +00:00
#include <test/Options.h>
#include <libyul/optimiser/BlockFlattener.h>
2018-12-13 14:26:01 +00:00
#include <libyul/optimiser/VarDeclInitializer.h>
#include <libyul/optimiser/VarNameCleaner.h>
2019-05-09 19:56:56 +00:00
#include <libyul/optimiser/ControlFlowSimplifier.h>
2019-03-28 13:18:17 +00:00
#include <libyul/optimiser/DeadCodeEliminator.h>
2018-10-15 09:52:35 +00:00
#include <libyul/optimiser/Disambiguator.h>
#include <libyul/optimiser/CommonSubexpressionEliminator.h>
#include <libyul/optimiser/NameCollector.h>
#include <libyul/optimiser/EquivalentFunctionCombiner.h>
2018-10-15 09:52:35 +00:00
#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/ForLoopInitRewriter.h>
2018-10-15 09:52:35 +00:00
#include <libyul/optimiser/MainFunction.h>
#include <libyul/optimiser/Rematerialiser.h>
#include <libyul/optimiser/ExpressionSimplifier.h>
#include <libyul/optimiser/UnusedPruner.h>
#include <libyul/optimiser/ExpressionJoiner.h>
2019-01-16 10:44:45 +00:00
#include <libyul/optimiser/SSAReverser.h>
2018-10-16 21:47:02 +00:00
#include <libyul/optimiser/SSATransform.h>
2018-10-23 13:55:48 +00:00
#include <libyul/optimiser/RedundantAssignEliminator.h>
#include <libyul/optimiser/StructuralSimplifier.h>
2019-02-04 16:30:29 +00:00
#include <libyul/optimiser/StackCompressor.h>
2018-10-16 19:39:22 +00:00
#include <libyul/optimiser/Suite.h>
2018-12-06 23:56:16 +00:00
#include <libyul/backends/evm/EVMDialect.h>
#include <libyul/backends/wasm/WordSizeTransform.h>
#include <libyul/AsmPrinter.h>
#include <libyul/AsmParser.h>
#include <libyul/AsmAnalysis.h>
2019-02-13 11:21:53 +00:00
#include <libyul/AssemblyStack.h>
#include <liblangutil/SourceReferenceFormatter.h>
2018-10-09 13:43:55 +00:00
#include <liblangutil/ErrorReporter.h>
#include <liblangutil/Scanner.h>
#include <libdevcore/AnsiColorized.h>
2018-10-09 13:43:55 +00:00
#include <boost/test/unit_test.hpp>
#include <boost/algorithm/string.hpp>
#include <fstream>
using namespace dev;
using namespace langutil;
using namespace yul;
using namespace yul::test;
2018-10-09 13:43:55 +00:00
using namespace dev::solidity;
using namespace dev::solidity::test;
using namespace std;
YulOptimizerTest::YulOptimizerTest(string const& _filename)
{
boost::filesystem::path path(_filename);
if (path.empty() || std::next(path.begin()) == path.end() || std::next(std::next(path.begin())) == path.end())
BOOST_THROW_EXCEPTION(runtime_error("Filename path has to contain a directory: \"" + _filename + "\"."));
m_optimizerStep = std::prev(std::prev(path.end()))->string();
ifstream file(_filename);
2019-04-01 14:33:46 +00:00
soltestAssert(file, "Cannot open test contract: \"" + _filename + "\".");
2018-10-09 13:43:55 +00:00
file.exceptions(ios::badbit);
2019-04-01 14:33:46 +00:00
m_source = parseSourceAndSettings(file);
if (m_settings.count("yul"))
{
m_yul = true;
m_validatedSettings["yul"] = "true";
m_settings.erase("yul");
}
if (m_settings.count("step"))
2018-10-09 13:43:55 +00:00
{
2019-04-01 14:33:46 +00:00
m_validatedSettings["step"] = m_settings["step"];
m_settings.erase("step");
2018-10-09 13:43:55 +00:00
}
2019-04-01 14:33:46 +00:00
string line;
2018-10-09 13:43:55 +00:00
while (getline(file, line))
if (boost::algorithm::starts_with(line, "// "))
m_expectation += line.substr(3) + "\n";
else
m_expectation += line + "\n";
}
TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _linePrefix, bool const _formatted)
2018-10-09 13:43:55 +00:00
{
if (!parse(_stream, _linePrefix, _formatted))
return TestResult::FatalError;
2018-10-09 13:43:55 +00:00
if (m_optimizerStep == "disambiguator")
disambiguate();
else if (m_optimizerStep == "blockFlattener")
{
disambiguate();
BlockFlattener{}(*m_ast);
}
2018-12-13 14:26:01 +00:00
else if (m_optimizerStep == "varDeclInitializer")
VarDeclInitializer{}(*m_ast);
else if (m_optimizerStep == "varNameCleaner")
VarNameCleaner{*m_ast, *m_dialect}(*m_ast);
else if (m_optimizerStep == "forLoopInitRewriter")
{
disambiguate();
ForLoopInitRewriter{}(*m_ast);
}
else if (m_optimizerStep == "commonSubexpressionEliminator")
{
disambiguate();
2018-12-20 17:55:32 +00:00
(CommonSubexpressionEliminator{*m_dialect})(*m_ast);
}
2018-10-11 13:13:42 +00:00
else if (m_optimizerStep == "expressionSplitter")
{
2018-12-20 17:55:32 +00:00
NameDispenser nameDispenser{*m_dialect, *m_ast};
ExpressionSplitter{*m_dialect, nameDispenser}(*m_ast);
2018-10-11 13:13:42 +00:00
}
else if (m_optimizerStep == "expressionJoiner")
{
disambiguate();
ExpressionJoiner::run(*m_ast);
}
else if (m_optimizerStep == "splitJoin")
{
disambiguate();
2018-12-20 17:55:32 +00:00
NameDispenser nameDispenser{*m_dialect, *m_ast};
ExpressionSplitter{*m_dialect, nameDispenser}(*m_ast);
ExpressionJoiner::run(*m_ast);
ExpressionJoiner::run(*m_ast);
}
2018-10-11 13:42:31 +00:00
else if (m_optimizerStep == "functionGrouper")
{
disambiguate();
(FunctionGrouper{})(*m_ast);
}
2018-10-11 13:47:49 +00:00
else if (m_optimizerStep == "functionHoister")
{
disambiguate();
(FunctionHoister{})(*m_ast);
}
2018-10-11 14:58:21 +00:00
else if (m_optimizerStep == "expressionInliner")
{
disambiguate();
2018-12-20 17:55:32 +00:00
ExpressionInliner(*m_dialect, *m_ast).run();
2018-10-11 14:58:21 +00:00
}
2018-10-11 15:14:59 +00:00
else if (m_optimizerStep == "fullInliner")
{
disambiguate();
(FunctionHoister{})(*m_ast);
(FunctionGrouper{})(*m_ast);
2018-12-20 17:55:32 +00:00
NameDispenser nameDispenser{*m_dialect, *m_ast};
ExpressionSplitter{*m_dialect, nameDispenser}(*m_ast);
2018-10-16 19:38:47 +00:00
FullInliner(*m_ast, nameDispenser).run();
2018-10-02 08:46:59 +00:00
ExpressionJoiner::run(*m_ast);
2018-10-11 15:14:59 +00:00
}
2018-10-11 15:45:12 +00:00
else if (m_optimizerStep == "mainFunction")
{
disambiguate();
(FunctionGrouper{})(*m_ast);
(MainFunction{})(*m_ast);
}
2018-10-11 15:59:45 +00:00
else if (m_optimizerStep == "rematerialiser")
{
disambiguate();
2018-12-20 17:55:32 +00:00
Rematerialiser::run(*m_dialect, *m_ast);
2018-10-11 15:59:45 +00:00
}
2018-10-11 16:35:13 +00:00
else if (m_optimizerStep == "expressionSimplifier")
{
disambiguate();
2018-12-20 17:55:32 +00:00
ExpressionSimplifier::run(*m_dialect, *m_ast);
ExpressionSimplifier::run(*m_dialect, *m_ast);
ExpressionSimplifier::run(*m_dialect, *m_ast);
2018-10-11 16:35:13 +00:00
}
else if (m_optimizerStep == "fullSimplify")
{
disambiguate();
2018-12-20 17:55:32 +00:00
NameDispenser nameDispenser{*m_dialect, *m_ast};
ExpressionSplitter{*m_dialect, nameDispenser}(*m_ast);
ForLoopInitRewriter{}(*m_ast);
2018-12-20 17:55:32 +00:00
CommonSubexpressionEliminator{*m_dialect}(*m_ast);
ExpressionSimplifier::run(*m_dialect, *m_ast);
UnusedPruner::runUntilStabilised(*m_dialect, *m_ast);
2019-03-28 13:18:17 +00:00
DeadCodeEliminator{}(*m_ast);
ExpressionJoiner::run(*m_ast);
ExpressionJoiner::run(*m_ast);
}
2018-10-11 16:41:01 +00:00
else if (m_optimizerStep == "unusedPruner")
{
disambiguate();
2018-12-20 17:55:32 +00:00
UnusedPruner::runUntilStabilised(*m_dialect, *m_ast);
2018-10-11 16:41:01 +00:00
}
2019-03-28 13:18:17 +00:00
else if (m_optimizerStep == "deadCodeEliminator")
{
disambiguate();
ForLoopInitRewriter{}(*m_ast);
2019-03-28 13:18:17 +00:00
DeadCodeEliminator{}(*m_ast);
}
2018-10-16 21:47:02 +00:00
else if (m_optimizerStep == "ssaTransform")
{
disambiguate();
2018-12-20 17:55:32 +00:00
NameDispenser nameDispenser{*m_dialect, *m_ast};
2018-10-16 21:47:02 +00:00
SSATransform::run(*m_ast, nameDispenser);
}
2018-10-23 13:55:48 +00:00
else if (m_optimizerStep == "redundantAssignEliminator")
{
disambiguate();
2018-12-20 17:55:32 +00:00
RedundantAssignEliminator::run(*m_dialect, *m_ast);
2018-10-23 13:55:48 +00:00
}
else if (m_optimizerStep == "ssaPlusCleanup")
{
disambiguate();
2018-12-20 17:55:32 +00:00
NameDispenser nameDispenser{*m_dialect, *m_ast};
2018-10-23 13:55:48 +00:00
SSATransform::run(*m_ast, nameDispenser);
2018-12-20 17:55:32 +00:00
RedundantAssignEliminator::run(*m_dialect, *m_ast);
2018-10-23 13:55:48 +00:00
}
2019-05-09 20:01:18 +00:00
else if (m_optimizerStep == "controlFlowSimplifier")
{
disambiguate();
2019-05-09 19:56:56 +00:00
ControlFlowSimplifier{}(*m_ast);
2019-05-09 20:01:18 +00:00
}
else if (m_optimizerStep == "structuralSimplifier")
{
disambiguate();
2018-12-20 17:55:32 +00:00
StructuralSimplifier{*m_dialect}(*m_ast);
}
else if (m_optimizerStep == "equivalentFunctionCombiner")
{
disambiguate();
EquivalentFunctionCombiner::run(*m_ast);
}
2019-01-16 10:44:45 +00:00
else if (m_optimizerStep == "ssaReverser")
{
disambiguate();
2019-01-17 15:11:55 +00:00
SSAReverser::run(*m_ast);
2019-01-16 10:44:45 +00:00
}
else if (m_optimizerStep == "ssaAndBack")
{
disambiguate();
// apply SSA
NameDispenser nameDispenser{*m_dialect, *m_ast};
SSATransform::run(*m_ast, nameDispenser);
RedundantAssignEliminator::run(*m_dialect, *m_ast);
// reverse SSA
2019-01-17 15:11:55 +00:00
SSAReverser::run(*m_ast);
2019-01-16 10:44:45 +00:00
CommonSubexpressionEliminator{*m_dialect}(*m_ast);
UnusedPruner::runUntilStabilised(*m_dialect, *m_ast);
}
2019-02-04 16:30:29 +00:00
else if (m_optimizerStep == "stackCompressor")
{
disambiguate();
(FunctionGrouper{})(*m_ast);
size_t maxIterations = 16;
StackCompressor::run(m_dialect, *m_ast, true, maxIterations);
2019-02-04 16:30:29 +00:00
(BlockFlattener{})(*m_ast);
}
else if (m_optimizerStep == "wordSizeTransform")
{
disambiguate();
2019-05-09 14:47:51 +00:00
NameDispenser nameDispenser{*m_dialect, *m_ast};
ExpressionSplitter{*m_dialect, nameDispenser}(*m_ast);
WordSizeTransform::run(*m_ast, nameDispenser);
}
2018-10-16 19:39:22 +00:00
else if (m_optimizerStep == "fullSuite")
OptimiserSuite::run(m_dialect, *m_ast, *m_analysisInfo, true);
2018-10-09 13:43:55 +00:00
else
{
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Invalid optimizer step: " << m_optimizerStep << endl;
return TestResult::FatalError;
2018-10-09 13:43:55 +00:00
}
2019-04-01 14:33:46 +00:00
m_obtainedResult = AsmPrinter{m_yul}(*m_ast) + "\n";
2018-10-09 13:43:55 +00:00
2019-04-01 14:33:46 +00:00
if (m_optimizerStep != m_validatedSettings["step"])
{
string nextIndentLevel = _linePrefix + " ";
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::CYAN}) <<
_linePrefix <<
"Invalid optimizer step. Given: \"" <<
m_validatedSettings["step"] <<
"\", should be: \"" <<
m_optimizerStep <<
"\"." <<
endl;
return TestResult::FatalError;
2019-04-01 14:33:46 +00:00
}
2018-10-09 13:43:55 +00:00
if (m_expectation != m_obtainedResult)
{
string nextIndentLevel = _linePrefix + " ";
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::CYAN}) << _linePrefix << "Expected result:" << endl;
2018-10-09 13:43:55 +00:00
// TODO could compute a simple diff with highlighted lines
printIndented(_stream, m_expectation, nextIndentLevel);
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::CYAN}) << _linePrefix << "Obtained result:" << endl;
2018-10-09 13:43:55 +00:00
printIndented(_stream, m_obtainedResult, nextIndentLevel);
return TestResult::Failure;
2018-10-09 13:43:55 +00:00
}
return TestResult::Success;
2018-10-09 13:43:55 +00:00
}
void YulOptimizerTest::printSource(ostream& _stream, string const& _linePrefix, bool const) const
{
printIndented(_stream, m_source, _linePrefix);
}
2019-04-01 14:33:46 +00:00
void YulOptimizerTest::printUpdatedSettings(ostream& _stream, const string& _linePrefix, const bool _formatted)
{
m_validatedSettings["step"] = m_optimizerStep;
EVMVersionRestrictedTestCase::printUpdatedSettings(_stream, _linePrefix, _formatted);
}
2018-10-09 13:43:55 +00:00
void YulOptimizerTest::printUpdatedExpectations(ostream& _stream, string const& _linePrefix) const
{
printIndented(_stream, m_obtainedResult, _linePrefix);
}
void YulOptimizerTest::printIndented(ostream& _stream, string const& _output, string const& _linePrefix) const
{
stringstream output(_output);
string line;
while (getline(output, line))
_stream << _linePrefix << line << endl;
}
bool YulOptimizerTest::parse(ostream& _stream, string const& _linePrefix, bool const _formatted)
{
2019-02-13 11:21:53 +00:00
AssemblyStack stack(
2018-10-09 13:43:55 +00:00
dev::test::Options::get().evmVersion(),
m_yul ? AssemblyStack::Language::Yul : AssemblyStack::Language::StrictAssembly,
dev::solidity::OptimiserSettings::none()
2018-10-09 13:43:55 +00:00
);
2019-02-13 11:21:53 +00:00
if (!stack.parseAndAnalyze("", m_source) || !stack.errors().empty())
2018-10-09 13:43:55 +00:00
{
2019-02-13 11:21:53 +00:00
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Error parsing source." << endl;
printErrors(_stream, stack.errors());
2018-10-09 13:43:55 +00:00
return false;
}
2019-02-21 20:56:30 +00:00
m_dialect = m_yul ? Dialect::yul() : EVMDialect::strictAssemblyForEVMObjects(dev::test::Options::get().evmVersion());
2019-02-13 11:21:53 +00:00
m_ast = stack.parserResult()->code;
m_analysisInfo = stack.parserResult()->analysisInfo;
2018-10-09 13:43:55 +00:00
return true;
}
void YulOptimizerTest::disambiguate()
{
2018-12-20 17:55:32 +00:00
*m_ast = boost::get<Block>(Disambiguator(*m_dialect, *m_analysisInfo)(*m_ast));
2018-10-09 13:43:55 +00:00
m_analysisInfo.reset();
}
void YulOptimizerTest::printErrors(ostream& _stream, ErrorList const& _errors)
2018-10-09 13:43:55 +00:00
{
SourceReferenceFormatter formatter(_stream);
2018-10-09 13:43:55 +00:00
for (auto const& error: _errors)
formatter.printErrorInformation(*error);
2018-10-09 13:43:55 +00:00
}