-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
[ethdebug] Transport debug data through common subexpression eliminator. #15088
Draft
aarlt
wants to merge
3
commits into
yul_transport_debugdata_attributes_to_assembly_items
Choose a base branch
from
transport_debug_data_through_common_subexpression_eliminator
base: yul_transport_debugdata_attributes_to_assembly_items
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
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/libsolidity/util/SoltestErrors.h> | ||
#include <test/libyul/Common.h> | ||
#include <test/Common.h> | ||
|
||
#include <libyul/Object.h> | ||
#include <libyul/AsmPrinter.h> | ||
|
||
#include <liblangutil/CharStreamProvider.h> | ||
#include <liblangutil/SourceReferenceFormatter.h> | ||
#include <liblangutil/Scanner.h> | ||
|
||
#include <libsolutil/AnsiColorized.h> | ||
#include <libsolutil/StringUtils.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(); | ||
|
||
auto dialectName = m_reader.stringSetting("dialect", "evm"); | ||
m_dialect = &dialect(dialectName, solidity::test::CommonOptions::get().evmVersion()); | ||
|
||
m_expectation = m_reader.simpleExpectations(); | ||
} | ||
|
||
TestCase::TestResult YulOptimizerAssemblyTest::run(std::ostream& _stream, std::string const& _linePrefix, bool const _formatted) | ||
{ | ||
std::tie(m_object, m_analysisInfo) = parse(_stream, _linePrefix, _formatted, m_source); | ||
if (!m_object) | ||
return TestResult::FatalError; | ||
|
||
soltestAssert(m_dialect, "Dialect not set."); | ||
|
||
m_object->analysisInfo = m_analysisInfo; | ||
YulOptimizerTestCommon tester(m_object, *m_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 = (m_object->subObjects.empty() ? AsmPrinter{ *m_dialect }(*m_object->code) : m_object->toString(m_dialect)); | ||
|
||
// Re-parse new code for compilability | ||
// 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:" << std::endl; | ||
printPrefixed(_stream, printed, _linePrefix + " "); | ||
return TestResult::FatalError; | ||
} | ||
|
||
m_obtainedResult = "step: " + m_optimizerStep + "\n\n" + printed + "\n"; | ||
|
||
m_object->analysisInfo = std::make_shared<AsmAnalysisInfo>( | ||
AsmAnalyzer::analyzeStrictAssertCorrect(EVMDialect::strictAssemblyForEVMObjects(solidity::test::CommonOptions::get().evmVersion()), *m_object) | ||
); | ||
|
||
evmasm::Assembly assembly{solidity::test::CommonOptions::get().evmVersion(), false, {}}; | ||
EthAssemblyAdapter adapter(assembly); | ||
EVMObjectCompiler::compile( | ||
*m_object, | ||
adapter, | ||
EVMDialect::strictAssemblyForEVMObjects(EVMVersion{}), | ||
false, | ||
std::nullopt | ||
); | ||
|
||
std::ostringstream output; | ||
output << assembly; | ||
m_obtainedResult += "\nAssembly:\n" + output.str(); | ||
|
||
return checkResult(_stream, _linePrefix, _formatted); | ||
} | ||
|
||
std::pair<std::shared_ptr<Object>, std::shared_ptr<AsmAnalysisInfo>> YulOptimizerAssemblyTest::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)}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
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::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; | ||
|
||
std::shared_ptr<Object> m_object; | ||
std::shared_ptr<AsmAnalysisInfo> m_analysisInfo; | ||
}; | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
test/libyul/yulOptimizerAssemblyTests/blockFlattener/basic.yul
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} | ||
// ---- | ||
// 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 |
24 changes: 24 additions & 0 deletions
24
test/libyul/yulOptimizerAssemblyTests/commonSubexpressionEliminator/ethdebug_trivial.yul
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
let a := /** @debug.set {"assignment":"a"} */ mul(1, codesize()) /** @debug.set {} */ | ||
let b := /** @debug.set {"assignment":"b"} */ mul(1, codesize()) /** @debug.set {} */ | ||
} | ||
// ---- | ||
// step: commonSubexpressionEliminator | ||
// | ||
// { | ||
// let a := /** @debug.set {"assignment":"a"} */ mul(1, codesize()) | ||
// let b := a | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm.. something seem not to work.. would have expected // let b := /** @debug.set [{"assignment":"a"}, {"assignment":"b"}] */ a here. |
||
// } | ||
// | ||
// Assembly: | ||
// /* "":59:69 */ | ||
// codesize // @debug.set {"assignment":"a"} | ||
// /* "":56:57 */ | ||
// 0x01 // @debug.set {"assignment":"a"} | ||
// /* "":52:70 */ | ||
// mul // @debug.set {"assignment":"a"} | ||
// /* "":142:160 */ | ||
// dup1 // @debug.set [{"assignment":"a"},{"assignment":"b"}] | ||
// /* "":0:183 */ | ||
// pop | ||
// pop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ekpyron What result would you expect for this example? I would basically just merge the debug attributes here, basically creating a list of possible items:
I think a debugger could easily find and use relevant information.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
However, I'm not very sure that this is correct in the general case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this would be more useful: