Skip to content

Commit 03c9d67

Browse files
authored
Merge pull request #16228 from argotorg/generalize_opt_tests
testing: run yul optimizer tests on all code subobjects
2 parents fb60450 + 173a534 commit 03c9d67

File tree

2 files changed

+53
-10
lines changed

2 files changed

+53
-10
lines changed

test/libyul/YulOptimizerTestCommon.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@
6666

6767
#include <libsolidity/interface/OptimiserSettings.h>
6868

69+
#include <range/v3/view/enumerate.hpp>
70+
6971
#include <random>
7072

7173
using namespace solidity;
@@ -81,15 +83,6 @@ YulOptimizerTestCommon::YulOptimizerTestCommon(std::shared_ptr<Object const> _ob
8183
{
8284
soltestAssert(m_object && m_optimizedObject);
8385

84-
if (
85-
std::any_of(
86-
m_object->subObjects.begin(),
87-
m_object->subObjects.end(),
88-
[](auto const& subObject) { return dynamic_cast<Data*>(subObject.get()) == nullptr;}
89-
)
90-
)
91-
solUnimplemented("The current implementation of YulOptimizerTests ignores subobjects that are not Data.");
92-
9386
m_namedSteps = {
9487
{"disambiguator", [&]() { return disambiguate(); }},
9588
{"nameDisplacer", [&]() {
@@ -496,8 +489,19 @@ void YulOptimizerTestCommon::setStep(std::string const& _optimizerStep)
496489

497490
bool YulOptimizerTestCommon::runStep()
498491
{
499-
if (m_namedSteps.count(m_optimizerStep))
492+
if (m_namedSteps.contains(m_optimizerStep))
500493
{
494+
for (auto const& [ix, subNode]: m_object->subObjects | ranges::views::enumerate)
495+
if (auto const subObject = std::dynamic_pointer_cast<Object const>(subNode))
496+
{
497+
YulOptimizerTestCommon subTest(subObject);
498+
subTest.setStep(m_optimizerStep);
499+
subTest.run();
500+
auto const optimizedSubObject = std::dynamic_pointer_cast<Object>(m_optimizedObject->subObjects[ix]);
501+
yulAssert(optimizedSubObject);
502+
optimizedSubObject->setCode(subTest.m_optimizedObject->code());
503+
}
504+
501505
auto block = m_namedSteps[m_optimizerStep]();
502506
m_optimizedObject->setCode(std::make_shared<AST>(*m_object->dialect(), std::move(block)));
503507
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// nested object to show that optimizations are applied at all levels
2+
object "A" {
3+
code
4+
{
5+
mstore(43, 33) // removed by optimizer as it's transient and otherwise unused
6+
}
7+
8+
object "B"
9+
{
10+
code
11+
{
12+
let y := 42
13+
sstore(y, y) // persists
14+
mstore(y, y) // removed
15+
}
16+
17+
object "C"
18+
{
19+
code
20+
{
21+
let x := 55
22+
sstore(x, x) // persists
23+
mstore(x, x) // removed
24+
}
25+
}
26+
}
27+
}
28+
// ----
29+
// step: fullSuite
30+
//
31+
// object "A" {
32+
// code { { } }
33+
// object "B" {
34+
// code { { sstore(42, 42) } }
35+
// object "C" {
36+
// code { { sstore(55, 55) } }
37+
// }
38+
// }
39+
// }

0 commit comments

Comments
 (0)