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

Transport debug data through common subexpression eliminator #15088

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions libyul/optimiser/CommonSubexpressionEliminator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,25 @@ void CommonSubexpressionEliminator::visit(Expression& _e)
// We check for syntactic equality again because the value might have changed.
if (inScope(variable) && SyntacticallyEqual{}(_e, *value->value))
{
if (debugDataOf(*value->value))
{
langutil::DebugData::Attributes mergedDebugAttributes;
if (debugDataOf(*value->value)->attributes.has_value())
mergedDebugAttributes = debugDataOf(*value->value)->attributes;
if (debugDataOf(_e) && mergedDebugAttributes.has_value())
{
for (auto const& i: debugDataOf(_e)->attributes.value())
mergedDebugAttributes->emplace_back(i);
_e = Identifier{
langutil::DebugData::create(
debugDataOf(_e)->nativeLocation,
debugDataOf(_e)->originLocation,
debugDataOf(_e)->astID,
mergedDebugAttributes),
variable};
break;
}
}
_e = Identifier{debugDataOf(_e), variable};
break;
}
Expand Down
29 changes: 29 additions & 0 deletions test/libyul/YulOptimizerAssemblyTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
#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>

Expand Down Expand Up @@ -60,6 +62,10 @@ YulOptimizerAssemblyTest::YulOptimizerAssemblyTest(std::string const& _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();
}

Expand Down Expand Up @@ -104,3 +110,26 @@ TestCase::TestResult YulOptimizerAssemblyTest::run(std::ostream& _stream, std::s

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)};
}
9 changes: 9 additions & 0 deletions test/libyul/YulOptimizerAssemblyTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,16 @@ class YulOptimizerAssemblyTest: public solidity::frontend::test::EVMVersionRestr

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

}
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
// }
//
// 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
function f(a, b)
{
}
let x := /** @debug.set {"id": 0} */ 42 /** @debug.set {} */
let y := /** @debug.set {"id":" 1"} */ add(sload(42), calldataload(0)) /** @debug.set {} */
let z := /** @debug.set {"id":"2"} */ 42 /** @debug.set {} */
f(/** @debug.set {"f": 0} */ 42, /** @debug.set {"f": 1} */add(sload(42), /** @debug.set {"f": 2} */calldataload(0))) /** @debug.set {} */
}
// ----
// step: commonSubexpressionEliminator
//
// {
// let x := /** @debug.set {"id":0} */ 42
// let y := /** @debug.set {"id":" 1"} */ add(sload(x), /** @debug.set {"id":" 1"} */ calldataload(0))
// let z := x
// f(x, /** @debug.set {"f":1} */ add(sload(x), /** @debug.set {"f":2} */ calldataload(0)))
// function f(a, b)
// { }
// }
//
// Assembly:
// /* "":76:78 */
// 0x2a // @debug.set {"id":0}
// /* "":171:172 */
// 0x00 // @debug.set {"id":" 1"}
// /* "":158:173 */
// calldataload // @debug.set {"id":" 1"}
// /* "":153:155 */
// dup2 // @debug.set [{"id":0},{"id":" 1"}]
// /* "":147:156 */
// sload // @debug.set {"id":" 1"}
// /* "":143:174 */
// add // @debug.set {"id":" 1"}
// /* "":238:240 */
// dup2 // @debug.set [{"id":0},{"id":"2"}]
// /* "":266:383 */
// tag_2
// /* "":379:380 */
// 0x00 // @debug.set {"f":2}
// /* "":366:381 */
// calldataload // @debug.set {"f":2}
// /* "":335:337 */
// dup5 // @debug.set [{"id":0},{"f":1}]
// /* "":329:338 */
// sload // @debug.set {"f":1}
// /* "":325:382 */
// add // @debug.set {"f":1}
// /* "":295:297 */
// dup5 // @debug.set [{"id":0},{"f":0}]
// /* "":266:383 */
// tag_1
// jump // in
// tag_2:
// /* "":6:34 */
// jump(tag_3)
// tag_1:
// pop
// pop
// tag_4:
// jump // out
// tag_3:
// /* "":0:406 */
// pop
// pop
// pop