Merge pull request #9575 from ethereum/yul-optim-check

Parse optimiser output in yulOptimiserTests
This commit is contained in:
chriseth 2020-08-06 00:40:45 +02:00 committed by GitHub
commit 488fd5a27a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 63 additions and 29 deletions

View File

@ -111,7 +111,8 @@ YulOptimizerTest::YulOptimizerTest(string const& _filename):
TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _linePrefix, bool const _formatted) TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _linePrefix, bool const _formatted)
{ {
if (!parse(_stream, _linePrefix, _formatted)) std::tie(m_object, m_analysisInfo) = parse(_stream, _linePrefix, _formatted, m_source);
if (!m_object)
return TestResult::FatalError; return TestResult::FatalError;
soltestAssert(m_dialect, "Dialect not set."); soltestAssert(m_dialect, "Dialect not set.");
@ -141,7 +142,11 @@ TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _line
else if (m_optimizerStep == "varDeclInitializer") else if (m_optimizerStep == "varDeclInitializer")
VarDeclInitializer::run(*m_context, *m_object->code); VarDeclInitializer::run(*m_context, *m_object->code);
else if (m_optimizerStep == "varNameCleaner") else if (m_optimizerStep == "varNameCleaner")
{
disambiguate();
FunctionGrouper::run(*m_context, *m_object->code);
VarNameCleaner::run(*m_context, *m_object->code); VarNameCleaner::run(*m_context, *m_object->code);
}
else if (m_optimizerStep == "forLoopConditionIntoBody") else if (m_optimizerStep == "forLoopConditionIntoBody")
{ {
disambiguate(); disambiguate();
@ -354,26 +359,42 @@ TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _line
return TestResult::FatalError; return TestResult::FatalError;
} }
m_obtainedResult = auto const printed = (m_object->subObjects.empty() ? AsmPrinter{ *m_dialect }(*m_object->code) : m_object->toString(m_dialect));
"step: " + m_optimizerStep + "\n\n" +
(m_object->subObjects.empty() ? AsmPrinter{ *m_dialect }(*m_object->code) : m_object->toString(m_dialect)) + // Re-parse new code for compilability
"\n"; // TODO: support for wordSizeTransform which needs different input and output dialects
if (m_optimizerStep != "wordSizeTransform" && !std::get<0>(parse(_stream, _linePrefix, _formatted, printed)))
{
util::AnsiColorized(_stream, _formatted, {util::formatting::BOLD, util::formatting::CYAN})
<< _linePrefix << "Result after the optimiser:" << endl;
printIndented(_stream, printed, _linePrefix + " ");
return TestResult::FatalError;
}
m_obtainedResult = "step: " + m_optimizerStep + "\n\n" + printed + "\n";
return checkResult(_stream, _linePrefix, _formatted); return checkResult(_stream, _linePrefix, _formatted);
} }
bool YulOptimizerTest::parse(ostream& _stream, string const& _linePrefix, bool const _formatted) std::pair<std::shared_ptr<Object>, std::shared_ptr<AsmAnalysisInfo>> YulOptimizerTest::parse(
ostream& _stream,
string const& _linePrefix,
bool const _formatted,
string const& _source
)
{ {
ErrorList errors; ErrorList errors;
soltestAssert(m_dialect, ""); soltestAssert(m_dialect, "");
std::tie(m_object, m_analysisInfo) = yul::test::parse(m_source, *m_dialect, errors); shared_ptr<Object> object;
if (!m_object || !m_analysisInfo || !Error::containsOnlyWarnings(errors)) shared_ptr<AsmAnalysisInfo> analysisInfo;
std::tie(object, analysisInfo) = yul::test::parse(_source, *m_dialect, errors);
if (!object || !analysisInfo || !Error::containsOnlyWarnings(errors))
{ {
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Error parsing source." << endl; AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Error parsing source." << endl;
printErrors(_stream, errors); printErrors(_stream, errors);
return false; return {};
} }
return true; return {std::move(object), std::move(analysisInfo)};
} }
void YulOptimizerTest::disambiguate() void YulOptimizerTest::disambiguate()

View File

@ -58,7 +58,9 @@ public:
TestResult run(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) override; TestResult run(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) override;
private: private:
bool parse(std::ostream& _stream, std::string const& _linePrefix, bool const _formatted); 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 disambiguate();
void updateContext(); void updateContext();

View File

@ -4,4 +4,4 @@
// ---- // ----
// step: varNameCleaner // step: varNameCleaner
// //
// { let datasize_1 := 1 } // { { let datasize_1 := 1 } }

View File

@ -1,14 +1,16 @@
{ {
let f_2
function f() { let f_1 } function f() { let f_1 }
let f_2
let f_10 let f_10
} }
// ---- // ----
// step: varNameCleaner // step: varNameCleaner
// //
// { // {
// {
// let f_1 // let f_1
// function f()
// { let f_1 }
// let f_2 // let f_2
// } // }
// function f()
// { let f_1 }
// }

View File

@ -12,12 +12,14 @@
// step: varNameCleaner // step: varNameCleaner
// //
// { // {
// {
// let f_1 // let f_1
// let f_2
// }
// function f(x) -> x_1, y // function f(x) -> x_1, y
// { // {
// let y_1 := x // let y_1 := x
// y := y_1 // y := y_1
// x_1 := y // x_1 := y
// } // }
// let f_2
// } // }

View File

@ -6,6 +6,7 @@
// step: varNameCleaner // step: varNameCleaner
// //
// { // {
// { }
// function f() // function f()
// { let x := 0 } // { let x := 0 }
// function g() // function g()

View File

@ -4,4 +4,4 @@
// ---- // ----
// step: varNameCleaner // step: varNameCleaner
// //
// { let mul_1 := 1 } // { { let mul_1 := 1 } }

View File

@ -8,8 +8,10 @@
// step: varNameCleaner // step: varNameCleaner
// //
// { // {
// {
// let a := 1 // let a := 1
// let a_1 := 2 // let a_1 := 2
// let a_2 := 0xdeadbeef // let a_2 := 0xdeadbeef
// let _1 := 21718 // let _1 := 21718
// } // }
// }

View File

@ -8,8 +8,10 @@
// step: varNameCleaner // step: varNameCleaner
// //
// { // {
// {
// let x := 1 // let x := 1
// let x_1 := 2 // let x_1 := 2
// let x_2 := 3 // let x_2 := 3
// let x_3 := 4 // let x_3 := 4
// } // }
// }

View File

@ -7,7 +7,9 @@
// step: varNameCleaner // step: varNameCleaner
// //
// { // {
// {
// let x := 1 // let x := 1
// let x_1 := 2 // let x_1 := 2
// let x_2 := 3 // let x_2 := 3
// } // }
// }