Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test] Add support to run yul optimizer assembly test. #15009

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions test/CMakeLists.txt
Expand Up @@ -158,6 +158,8 @@ set(libyul_sources
libyul/SyntaxTest.cpp
libyul/YulInterpreterTest.cpp
libyul/YulInterpreterTest.h
libyul/YulOptimizerAssemblyTest.cpp
libyul/YulOptimizerAssemblyTest.h
libyul/YulOptimizerTest.cpp
libyul/YulOptimizerTest.h
libyul/YulOptimizerTestCommon.cpp
Expand Down
2 changes: 2 additions & 0 deletions test/InteractiveTests.h
Expand Up @@ -31,6 +31,7 @@
#include <test/libsolidity/SMTCheckerTest.h>
#include <test/libyul/ControlFlowGraphTest.h>
#include <test/libyul/EVMCodeTransformTest.h>
#include <test/libyul/YulOptimizerAssemblyTest.h>
#include <test/libyul/YulOptimizerTest.h>
#include <test/libyul/YulInterpreterTest.h>
#include <test/libyul/ObjectCompilerTest.h>
Expand Down Expand Up @@ -63,6 +64,7 @@ Testsuite const g_interactiveTestsuites[] = {
/*
Title Path Subpath SMT NeedsVM Creator function */
{"Yul Optimizer", "libyul", "yulOptimizerTests", false, false, &yul::test::YulOptimizerTest::create},
{"Yul Optimizer with Assembly", "libyul", "yulOptimizerAssemblyTests", false, false, &yul::test::YulOptimizerAssemblyTest::create},
{"Yul Interpreter", "libyul", "yulInterpreterTests", false, false, &yul::test::YulInterpreterTest::create},
{"Yul Object Compiler", "libyul", "objectCompiler", false, false, &yul::test::ObjectCompilerTest::create},
{"Yul Control Flow Graph", "libyul", "yulControlFlowGraph", false, false, &yul::test::ControlFlowGraphTest::create},
Expand Down
26 changes: 26 additions & 0 deletions test/libyul/Common.cpp
Expand Up @@ -33,6 +33,9 @@
#include <liblangutil/DebugInfoSelection.h>
#include <liblangutil/ErrorReporter.h>
#include <liblangutil/Scanner.h>
#include <liblangutil/SourceReferenceFormatter.h>

#include <libsolutil/AnsiColorized.h>

#include <boost/test/unit_test.hpp>

Expand Down Expand Up @@ -88,6 +91,29 @@ std::pair<std::shared_ptr<Object>, std::shared_ptr<yul::AsmAnalysisInfo>> yul::t
return {std::move(parserResult), std::move(analysisInfo)};
}

std::pair<std::shared_ptr<Object>, std::shared_ptr<AsmAnalysisInfo>> yul::test::parse(
std::ostream& _stream,
std::string const& _linePrefix,
bool const _formatted,
std::string const& _source,
Dialect const& _dialect
)
{
ErrorList errors;
std::shared_ptr<Object> object;
std::shared_ptr<AsmAnalysisInfo> analysisInfo;
std::tie(object, analysisInfo) = yul::test::parse(_source, _dialect, errors);
if (!object || !analysisInfo || Error::containsErrors(errors))
{
util::AnsiColorized(_stream, _formatted, {util::formatting::BOLD, util::formatting::RED}) << _linePrefix << "Error parsing source." << std::endl;
CharStream charStream(_source, "");
SourceReferenceFormatter{_stream, SingletonCharStreamProvider(charStream), true, false}
.printErrorInformation(errors);
return {};
}
return {std::move(object), std::move(analysisInfo)};
}

yul::Block yul::test::disambiguate(std::string const& _source, bool _yul)
{
auto result = parse(_source, _yul);
Expand Down
8 changes: 8 additions & 0 deletions test/libyul/Common.h
Expand Up @@ -50,6 +50,14 @@ parse(std::string const& _source, bool _yul = true);
std::pair<std::shared_ptr<Object>, std::shared_ptr<AsmAnalysisInfo>>
parse(std::string const& _source, Dialect const& _dialect, langutil::ErrorList& _errors);

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,
Dialect const& _dialect
);

Block disambiguate(std::string const& _source, bool _yul = true);
std::string format(std::string const& _source, bool _yul = true);

Expand Down
105 changes: 105 additions & 0 deletions test/libyul/YulOptimizerAssemblyTest.cpp
@@ -0,0 +1,105 @@
/*
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/YulOptimizerAssemblyTest.h>
#include <test/libyul/YulOptimizerTestCommon.h>

#include <test/libyul/Common.h>
#include <test/Common.h>

#include <libyul/Object.h>
#include <libyul/AsmPrinter.h>

#include <liblangutil/SourceReferenceFormatter.h>
#include <liblangutil/Scanner.h>

#include <libsolutil/AnsiColorized.h>
#include <libsolutil/CommonIO.h>

#include <libyul/YulStack.h>
#include <libyul/AsmAnalysis.h>
#include <libyul/AsmAnalysisInfo.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <libyul/backends/evm/EthAssemblyAdapter.h>
#include <libyul/backends/evm/EVMObjectCompiler.h>

#include <libevmasm/Assembly.h>

#include <fstream>

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 solidity::frontend::test;

YulOptimizerAssemblyTest::YulOptimizerAssemblyTest(std::string const& _filename):
EVMVersionRestrictedTestCase(_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(std::runtime_error("Filename path has to contain a directory: \"" + _filename + "\"."));
m_optimizerStep = std::prev(std::prev(path.end()))->string();

m_source = m_reader.source();
m_expectation = m_reader.simpleExpectations();
}

TestCase::TestResult YulOptimizerAssemblyTest::run(std::ostream& _stream, std::string const& _linePrefix, bool const _formatted)
{
std::shared_ptr<Object> object;
std::shared_ptr<AsmAnalysisInfo> analysisInfo;

Dialect const& dialect = test::dialect("evm", solidity::test::CommonOptions::get().evmVersion());
std::tie(object, analysisInfo) = test::parse(_stream, _linePrefix, _formatted, m_source, dialect);
if (!object)
return TestResult::FatalError;

object->analysisInfo = analysisInfo;
YulOptimizerTestCommon tester(object, dialect);
tester.setStep(m_optimizerStep);

if (!tester.runStep())
{
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Invalid optimizer step: " << m_optimizerStep << std::endl;
return TestResult::FatalError;
}

auto const printed = (object->subObjects.empty() ? AsmPrinter{ dialect }(*object->code) : object->toString(&dialect));

m_obtainedResult = "step: " + m_optimizerStep + "\n\n" + printed + "\n";

*object->analysisInfo = AsmAnalyzer::analyzeStrictAssertCorrect(dialect, *object);

evmasm::Assembly assembly{solidity::test::CommonOptions::get().evmVersion(), false, {}};
EthAssemblyAdapter adapter(assembly);
EVMObjectCompiler::compile(
*object,
adapter,
EVMDialect::strictAssemblyForEVMObjects(solidity::test::CommonOptions::get().evmVersion()),
false, // optimize - don't run any other optimization
solidity::test::CommonOptions::get().eofVersion()
);

m_obtainedResult += "\nAssembly:\n" + toString(assembly);

return checkResult(_stream, _linePrefix, _formatted);
}
54 changes: 54 additions & 0 deletions test/libyul/YulOptimizerAssemblyTest.h
@@ -0,0 +1,54 @@
/*
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 <test/TestCase.h>

namespace solidity::langutil
{
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 YulOptimizerAssemblyTest: public solidity::frontend::test::EVMVersionRestrictedTestCase
{
public:
static std::unique_ptr<TestCase> create(Config const& _config)
{
return std::make_unique<YulOptimizerAssemblyTest>(_config.filename);
}

explicit YulOptimizerAssemblyTest(std::string const& _filename);

TestResult run(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) override;
private:
std::string m_optimizerStep;
};

}
31 changes: 4 additions & 27 deletions test/libyul/YulOptimizerTest.cpp
Expand Up @@ -26,9 +26,8 @@
#include <libyul/Object.h>
#include <libyul/AsmPrinter.h>

#include <liblangutil/CharStreamProvider.h>
#include <liblangutil/SourceReferenceFormatter.h>
#include <liblangutil/Scanner.h>
#include <liblangutil/SourceReferenceFormatter.h>

#include <libsolutil/AnsiColorized.h>
#include <libsolutil/StringUtils.h>
Expand Down Expand Up @@ -62,11 +61,11 @@ YulOptimizerTest::YulOptimizerTest(std::string const& _filename):

TestCase::TestResult YulOptimizerTest::run(std::ostream& _stream, std::string const& _linePrefix, bool const _formatted)
{
std::tie(m_object, m_analysisInfo) = parse(_stream, _linePrefix, _formatted, m_source);
soltestAssert(m_dialect, "Dialect not set.");
std::tie(m_object, m_analysisInfo) = parse(_stream, _linePrefix, _formatted, m_source, *m_dialect);
if (!m_object)
return TestResult::FatalError;

soltestAssert(m_dialect, "Dialect not set.");

m_object->analysisInfo = m_analysisInfo;
YulOptimizerTestCommon tester(m_object, *m_dialect);
Expand All @@ -81,7 +80,7 @@ TestCase::TestResult YulOptimizerTest::run(std::ostream& _stream, std::string co
auto const printed = (m_object->subObjects.empty() ? AsmPrinter{ *m_dialect }(*m_object->code) : m_object->toString(m_dialect));

// Re-parse new code for compilability
if (!std::get<0>(parse(_stream, _linePrefix, _formatted, printed)))
if (!std::get<0>(parse(_stream, _linePrefix, _formatted, printed, *m_dialect)))
{
util::AnsiColorized(_stream, _formatted, {util::formatting::BOLD, util::formatting::CYAN})
<< _linePrefix << "Result after the optimiser:" << std::endl;
Expand All @@ -94,25 +93,3 @@ TestCase::TestResult YulOptimizerTest::run(std::ostream& _stream, std::string co
return checkResult(_stream, _linePrefix, _formatted);
}

std::pair<std::shared_ptr<Object>, std::shared_ptr<AsmAnalysisInfo>> YulOptimizerTest::parse(
std::ostream& _stream,
std::string const& _linePrefix,
bool const _formatted,
std::string const& _source
)
{
ErrorList errors;
soltestAssert(m_dialect, "");
std::shared_ptr<Object> object;
std::shared_ptr<AsmAnalysisInfo> analysisInfo;
std::tie(object, analysisInfo) = yul::test::parse(_source, *m_dialect, errors);
if (!object || !analysisInfo || Error::containsErrors(errors))
{
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Error parsing source." << std::endl;
CharStream charStream(_source, "");
SourceReferenceFormatter{_stream, SingletonCharStreamProvider(charStream), true, false}
.printErrorInformation(errors);
return {};
}
return {std::move(object), std::move(analysisInfo)};
}
4 changes: 0 additions & 4 deletions test/libyul/YulOptimizerTest.h
Expand Up @@ -48,10 +48,6 @@ class YulOptimizerTest: public solidity::frontend::test::EVMVersionRestrictedTes

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
);

std::string m_optimizerStep;

Dialect const* m_dialect = nullptr;
Expand Down
62 changes: 62 additions & 0 deletions test/libyul/yulOptimizerAssemblyTests/blockFlattener/basic.yul
@@ -0,0 +1,62 @@
{
{
let _1 := mload(0)
let f_a := mload(1)
let f_r
{
f_a := mload(f_a)
f_r := add(f_a, calldatasize())
}
let z := mload(2)
}
Comment on lines +2 to +11
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, if the new test is meant for inspecting the debug data, then it would be useful to actually have some debug annotations in the sample test to see if it's even preserved in the output.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now it is only meant to print the assembly in this PR. The printing of the debug data is so far done in #14969 - but I guess some changes are still needed there.

}
// ----
// step: blockFlattener
//
// {
// {
// let _1 := mload(0)
// let f_a := mload(1)
// let f_r
// f_a := mload(f_a)
// f_r := add(f_a, calldatasize())
// let z := mload(2)
// }
// }
//
// Assembly:
// /* "":32:33 */
// 0x00
// /* "":26:34 */
// mload
// /* "":60:61 */
// 0x01
// /* "":54:62 */
// mload
// /* "":71:78 */
// 0x00
// /* "":114:117 */
// dup2
// /* "":108:118 */
// mload
// /* "":101:118 */
// swap2
// pop
// /* "":147:161 */
// calldatasize
// /* "":142:145 */
// dup3
// /* "":138:162 */
// add
// /* "":131:162 */
// swap1
// pop
// /* "":196:197 */
// 0x02
// /* "":190:198 */
// mload
// /* "":6:204 */
// pop
// pop
// pop
// pop
1 change: 1 addition & 0 deletions test/tools/CMakeLists.txt
Expand Up @@ -46,6 +46,7 @@ add_executable(isoltest
../libyul/StackShufflingTest.cpp
../libyul/StackLayoutGeneratorTest.cpp
../libyul/YulOptimizerTest.cpp
../libyul/YulOptimizerAssemblyTest.cpp
../libyul/YulOptimizerTestCommon.cpp
../libyul/YulInterpreterTest.cpp
)
Expand Down