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/libsolidity/FormattedScope.h>
|
|
|
|
|
|
|
|
#include <test/Options.h>
|
|
|
|
|
2018-10-15 13:17:20 +00:00
|
|
|
#include <libyul/optimiser/BlockFlattener.h>
|
2018-10-17 09:33:35 +00:00
|
|
|
#include <libyul/optimiser/VarDeclPropagator.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/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/MainFunction.h>
|
|
|
|
#include <libyul/optimiser/Rematerialiser.h>
|
|
|
|
#include <libyul/optimiser/ExpressionSimplifier.h>
|
|
|
|
#include <libyul/optimiser/UnusedPruner.h>
|
|
|
|
#include <libyul/optimiser/ExpressionJoiner.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>
|
2018-10-16 19:39:22 +00:00
|
|
|
#include <libyul/optimiser/Suite.h>
|
2018-10-11 12:48:57 +00:00
|
|
|
|
2018-10-09 13:43:55 +00:00
|
|
|
#include <libsolidity/parsing/Scanner.h>
|
|
|
|
#include <libsolidity/inlineasm/AsmPrinter.h>
|
|
|
|
#include <libsolidity/inlineasm/AsmParser.h>
|
|
|
|
#include <libsolidity/inlineasm/AsmAnalysis.h>
|
|
|
|
#include <libsolidity/interface/SourceReferenceFormatter.h>
|
|
|
|
#include <libsolidity/interface/ErrorReporter.h>
|
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
using namespace dev;
|
2018-10-15 09:58:51 +00:00
|
|
|
using namespace dev::yul;
|
|
|
|
using namespace dev::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);
|
|
|
|
if (!file)
|
|
|
|
BOOST_THROW_EXCEPTION(runtime_error("Cannot open test case: \"" + _filename + "\"."));
|
|
|
|
file.exceptions(ios::badbit);
|
|
|
|
|
|
|
|
string line;
|
|
|
|
while (getline(file, line))
|
|
|
|
{
|
|
|
|
if (boost::algorithm::starts_with(line, "// ----"))
|
|
|
|
break;
|
|
|
|
if (m_source.empty() && boost::algorithm::starts_with(line, "// yul"))
|
|
|
|
m_yul = true;
|
|
|
|
m_source += line + "\n";
|
|
|
|
}
|
|
|
|
while (getline(file, line))
|
|
|
|
if (boost::algorithm::starts_with(line, "// "))
|
|
|
|
m_expectation += line.substr(3) + "\n";
|
|
|
|
else
|
|
|
|
m_expectation += line + "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool YulOptimizerTest::run(ostream& _stream, string const& _linePrefix, bool const _formatted)
|
|
|
|
{
|
|
|
|
assembly::AsmPrinter printer{m_yul};
|
|
|
|
shared_ptr<Block> ast;
|
|
|
|
shared_ptr<assembly::AsmAnalysisInfo> analysisInfo;
|
|
|
|
if (!parse(_stream, _linePrefix, _formatted))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (m_optimizerStep == "disambiguator")
|
|
|
|
disambiguate();
|
2018-10-15 13:17:20 +00:00
|
|
|
else if (m_optimizerStep == "blockFlattener")
|
|
|
|
{
|
|
|
|
disambiguate();
|
|
|
|
BlockFlattener{}(*m_ast);
|
|
|
|
}
|
2018-10-17 09:33:35 +00:00
|
|
|
else if (m_optimizerStep == "varDeclPropagator")
|
|
|
|
{
|
|
|
|
disambiguate();
|
|
|
|
VarDeclPropagator{}(*m_ast);
|
|
|
|
}
|
2018-10-11 12:48:57 +00:00
|
|
|
else if (m_optimizerStep == "commonSubexpressionEliminator")
|
|
|
|
{
|
|
|
|
disambiguate();
|
|
|
|
(CommonSubexpressionEliminator{})(*m_ast);
|
|
|
|
}
|
2018-10-11 13:13:42 +00:00
|
|
|
else if (m_optimizerStep == "expressionSplitter")
|
|
|
|
{
|
2018-10-16 15:01:13 +00:00
|
|
|
NameDispenser nameDispenser(*m_ast);
|
2018-10-11 13:13:42 +00:00
|
|
|
ExpressionSplitter{nameDispenser}(*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();
|
|
|
|
ExpressionInliner(*m_ast).run();
|
|
|
|
}
|
2018-10-11 15:14:59 +00:00
|
|
|
else if (m_optimizerStep == "fullInliner")
|
|
|
|
{
|
|
|
|
disambiguate();
|
|
|
|
(FunctionHoister{})(*m_ast);
|
|
|
|
(FunctionGrouper{})(*m_ast);
|
2018-10-16 15:01:13 +00:00
|
|
|
NameDispenser nameDispenser(*m_ast);
|
2018-10-02 08:46:59 +00:00
|
|
|
ExpressionSplitter{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();
|
|
|
|
(Rematerialiser{})(*m_ast);
|
|
|
|
}
|
2018-10-11 16:35:13 +00:00
|
|
|
else if (m_optimizerStep == "expressionSimplifier")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2018-10-02 16:07:50 +00:00
|
|
|
ExpressionSimplifier::run(*m_ast);
|
2018-10-11 16:35:13 +00:00
|
|
|
}
|
2018-10-11 19:16:37 +00:00
|
|
|
else if (m_optimizerStep == "fullSimplify")
|
|
|
|
{
|
|
|
|
disambiguate();
|
2018-10-16 15:01:13 +00:00
|
|
|
NameDispenser nameDispenser(*m_ast);
|
2018-10-11 19:16:37 +00:00
|
|
|
ExpressionSplitter{nameDispenser}(*m_ast);
|
|
|
|
CommonSubexpressionEliminator{}(*m_ast);
|
|
|
|
ExpressionSimplifier::run(*m_ast);
|
|
|
|
UnusedPruner::runUntilStabilised(*m_ast);
|
|
|
|
ExpressionJoiner::run(*m_ast);
|
|
|
|
ExpressionJoiner::run(*m_ast);
|
|
|
|
}
|
2018-10-11 16:41:01 +00:00
|
|
|
else if (m_optimizerStep == "unusedPruner")
|
|
|
|
{
|
|
|
|
disambiguate();
|
|
|
|
UnusedPruner::runUntilStabilised(*m_ast);
|
|
|
|
}
|
2018-10-01 16:27:07 +00:00
|
|
|
else if (m_optimizerStep == "expressionJoiner")
|
|
|
|
{
|
|
|
|
disambiguate();
|
|
|
|
ExpressionJoiner::run(*m_ast);\
|
|
|
|
}
|
2018-10-16 21:47:02 +00:00
|
|
|
else if (m_optimizerStep == "ssaTransform")
|
|
|
|
{
|
|
|
|
disambiguate();
|
|
|
|
NameDispenser nameDispenser(*m_ast);
|
|
|
|
SSATransform::run(*m_ast, nameDispenser);
|
|
|
|
}
|
2018-10-23 13:55:48 +00:00
|
|
|
else if (m_optimizerStep == "redundantAssignEliminator")
|
|
|
|
{
|
|
|
|
disambiguate();
|
|
|
|
RedundantAssignEliminator::run(*m_ast);
|
|
|
|
}
|
|
|
|
else if (m_optimizerStep == "ssaPlusCleanup")
|
|
|
|
{
|
|
|
|
disambiguate();
|
|
|
|
NameDispenser nameDispenser(*m_ast);
|
|
|
|
SSATransform::run(*m_ast, nameDispenser);
|
|
|
|
RedundantAssignEliminator::run(*m_ast);
|
|
|
|
}
|
2018-10-16 19:39:22 +00:00
|
|
|
else if (m_optimizerStep == "fullSuite")
|
|
|
|
OptimiserSuite::run(*m_ast, *m_analysisInfo);
|
2018-10-09 13:43:55 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
FormattedScope(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Invalid optimizer step: " << m_optimizerStep << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_obtainedResult = m_optimizerStep + "\n" + printer(*m_ast) + "\n";
|
|
|
|
|
|
|
|
if (m_expectation != m_obtainedResult)
|
|
|
|
{
|
|
|
|
string nextIndentLevel = _linePrefix + " ";
|
|
|
|
FormattedScope(_stream, _formatted, {formatting::BOLD, formatting::CYAN}) << _linePrefix << "Expected result:" << endl;
|
|
|
|
// TODO could compute a simple diff with highlighted lines
|
|
|
|
printIndented(_stream, m_expectation, nextIndentLevel);
|
|
|
|
FormattedScope(_stream, _formatted, {formatting::BOLD, formatting::CYAN}) << _linePrefix << "Obtained result:" << endl;
|
|
|
|
printIndented(_stream, m_obtainedResult, nextIndentLevel);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void YulOptimizerTest::printSource(ostream& _stream, string const& _linePrefix, bool const) const
|
|
|
|
{
|
|
|
|
printIndented(_stream, m_source, _linePrefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
assembly::AsmFlavour flavour = m_yul ? assembly::AsmFlavour::Yul : assembly::AsmFlavour::Strict;
|
|
|
|
ErrorList errors;
|
|
|
|
ErrorReporter errorReporter(errors);
|
|
|
|
shared_ptr<Scanner> scanner = make_shared<Scanner>(CharStream(m_source), "");
|
|
|
|
m_ast = assembly::Parser(errorReporter, flavour).parse(scanner, false);
|
|
|
|
if (!m_ast || !errorReporter.errors().empty())
|
|
|
|
{
|
|
|
|
FormattedScope(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Error parsing source." << endl;
|
|
|
|
printErrors(_stream, errorReporter.errors(), *scanner);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_analysisInfo = make_shared<assembly::AsmAnalysisInfo>();
|
|
|
|
assembly::AsmAnalyzer analyzer(
|
|
|
|
*m_analysisInfo,
|
|
|
|
errorReporter,
|
|
|
|
dev::test::Options::get().evmVersion(),
|
|
|
|
boost::none,
|
|
|
|
flavour
|
|
|
|
);
|
|
|
|
if (!analyzer.analyze(*m_ast) || !errorReporter.errors().empty())
|
|
|
|
{
|
|
|
|
FormattedScope(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Error analyzing source." << endl;
|
|
|
|
printErrors(_stream, errorReporter.errors(), *scanner);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void YulOptimizerTest::disambiguate()
|
|
|
|
{
|
|
|
|
*m_ast = boost::get<Block>(Disambiguator(*m_analysisInfo)(*m_ast));
|
|
|
|
m_analysisInfo.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void YulOptimizerTest::printErrors(ostream& _stream, ErrorList const& _errors, Scanner const& _scanner)
|
|
|
|
{
|
|
|
|
SourceReferenceFormatter formatter(_stream, [&](string const&) -> Scanner const& { return _scanner; });
|
|
|
|
|
|
|
|
for (auto const& error: _errors)
|
|
|
|
formatter.printExceptionInformation(
|
|
|
|
*error,
|
|
|
|
(error->type() == Error::Type::Warning) ? "Warning" : "Error"
|
|
|
|
);
|
|
|
|
}
|