Skip to content

Commit b5b8833

Browse files
authored
Merge pull request #11319 from ethereum/optimization-level-in-object-compiler-test
optimizationPreset setting in object compiler tests + refactor
2 parents 28c3b38 + 43de99d commit b5b8833

File tree

11 files changed

+76
-16
lines changed

11 files changed

+76
-16
lines changed

libsolidity/interface/OptimiserSettings.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,22 @@
2323

2424
#pragma once
2525

26+
#include <liblangutil/Exceptions.h>
27+
2628
#include <cstddef>
2729
#include <string>
2830

2931
namespace solidity::frontend
3032
{
3133

34+
enum class OptimisationPreset
35+
{
36+
None,
37+
Minimal,
38+
Standard,
39+
Full,
40+
};
41+
3242
struct OptimiserSettings
3343
{
3444
static char constexpr DefaultYulOptimiserSteps[] =
@@ -84,6 +94,18 @@ struct OptimiserSettings
8494
return standard();
8595
}
8696

97+
static OptimiserSettings preset(OptimisationPreset _preset)
98+
{
99+
switch (_preset)
100+
{
101+
case OptimisationPreset::None: return none();
102+
case OptimisationPreset::Minimal: return minimal();
103+
case OptimisationPreset::Standard: return standard();
104+
case OptimisationPreset::Full: return full();
105+
default: solAssert(false, "");
106+
}
107+
}
108+
87109
bool operator==(OptimiserSettings const& _other) const
88110
{
89111
return

test/TestCaseReader.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,12 @@
1818

1919
#include <test/TestCaseReader.h>
2020

21-
#include <libsolidity/parsing/Parser.h>
22-
#include <libsolutil/StringUtils.h>
2321
#include <libsolutil/CommonIO.h>
2422

2523
#include <boost/algorithm/string.hpp>
26-
#include <boost/throw_exception.hpp>
2724
#include <boost/filesystem.hpp>
2825

29-
#include <range/v3/view/map.hpp>
30-
3126
using namespace std;
32-
using namespace solidity::langutil;
3327
using namespace solidity::frontend::test;
3428

3529
namespace fs = boost::filesystem;
@@ -162,7 +156,7 @@ pair<SourceMap, size_t> TestCaseReader::parseSourcesAndSettingsWithLineNumber(is
162156
else
163157
externalSourceName = externalSourceString;
164158

165-
solAssert(!externalSourceName.empty(), "");
159+
soltestAssert(!externalSourceName.empty(), "");
166160
fs::path externalSourceTarget(externalSourceString);
167161
fs::path testCaseParentDir = m_fileName.parent_path();
168162
if (!externalSourceTarget.is_relative())

test/TestCaseReader.h

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,21 @@
1616
*/
1717
// SPDX-License-Identifier: GPL-3.0
1818

19+
#pragma once
20+
21+
#include <test/libsolidity/util/SoltestErrors.h>
22+
23+
#include <libsolutil/StringUtils.h>
24+
25+
#include <range/v3/view/map.hpp>
26+
1927
#include <boost/filesystem.hpp>
28+
#include <boost/throw_exception.hpp>
29+
2030
#include <fstream>
2131
#include <map>
2232
#include <string>
2333

24-
#pragma once
25-
2634
namespace solidity::frontend::test
2735
{
2836

@@ -58,6 +66,9 @@ class TestCaseReader
5866
size_t sizetSetting(std::string const& _name, size_t _defaultValue);
5967
std::string stringSetting(std::string const& _name, std::string const& _defaultValue);
6068

69+
template <typename E>
70+
E enumSetting(std::string const& _name, std::map<std::string, E> const& _choices, std::string const& _defaultChoice);
71+
6172
void ensureAllSettingsRead() const;
6273

6374
private:
@@ -71,4 +82,20 @@ class TestCaseReader
7182
std::map<std::string, std::string> m_settings;
7283
std::map<std::string, std::string> m_unreadSettings; ///< tracks which settings are left unread
7384
};
85+
86+
template <typename E>
87+
E TestCaseReader::enumSetting(std::string const& _name, std::map<std::string, E> const& _choices, std::string const& _defaultChoice)
88+
{
89+
soltestAssert(_choices.count(_defaultChoice) > 0, "");
90+
91+
std::string value = stringSetting(_name, _defaultChoice);
92+
93+
if (_choices.count(value) == 0)
94+
BOOST_THROW_EXCEPTION(std::runtime_error(
95+
"Invalid Enum value: " + value + ". Available choices: " + util::joinHumanReadable(_choices | ranges::views::keys) + "."
96+
));
97+
98+
return _choices.at(value);
99+
}
100+
74101
}

test/libsolidity/util/SoltestErrors.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace solidity::frontend::test
2525
do \
2626
{ \
2727
if (!(CONDITION)) \
28-
BOOST_THROW_EXCEPTION(runtime_error(DESCRIPTION)); \
28+
BOOST_THROW_EXCEPTION(std::runtime_error(DESCRIPTION)); \
2929
} \
3030
while (false)
3131

test/libyul/ObjectCompilerTest.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
#include <test/libyul/ObjectCompilerTest.h>
2020

21+
#include <test/libsolidity/util/SoltestErrors.h>
22+
2123
#include <libsolutil/AnsiColorized.h>
2224

2325
#include <libyul/AssemblyStack.h>
@@ -43,7 +45,16 @@ ObjectCompilerTest::ObjectCompilerTest(string const& _filename):
4345
TestCase(_filename)
4446
{
4547
m_source = m_reader.source();
46-
m_optimize = m_reader.boolSetting("optimize", false);
48+
m_optimisationPreset = m_reader.enumSetting<OptimisationPreset>(
49+
"optimizationPreset",
50+
{
51+
{"none", OptimisationPreset::None},
52+
{"minimal", OptimisationPreset::Minimal},
53+
{"standard", OptimisationPreset::Standard},
54+
{"full", OptimisationPreset::Full},
55+
},
56+
"minimal"
57+
);
4758
m_wasm = m_reader.boolSetting("wasm", false);
4859
m_expectation = m_reader.simpleExpectations();
4960
}
@@ -53,7 +64,7 @@ TestCase::TestResult ObjectCompilerTest::run(ostream& _stream, string const& _li
5364
AssemblyStack stack(
5465
EVMVersion(),
5566
m_wasm ? AssemblyStack::Language::Ewasm : AssemblyStack::Language::StrictAssembly,
56-
m_optimize ? OptimiserSettings::full() : OptimiserSettings::minimal()
67+
OptimiserSettings::preset(m_optimisationPreset)
5768
);
5869
if (!stack.parseAndAnalyze("source", m_source))
5970
{

test/libyul/ObjectCompilerTest.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
#include <test/TestCase.h>
2222

23+
#include <libsolidity/interface/OptimiserSettings.h>
24+
2325
namespace solidity::langutil
2426
{
2527
class Scanner;
@@ -54,7 +56,7 @@ class ObjectCompilerTest: public solidity::frontend::test::TestCase
5456

5557
static void printErrors(std::ostream& _stream, langutil::ErrorList const& _errors);
5658

57-
bool m_optimize = false;
59+
frontend::OptimisationPreset m_optimisationPreset;
5860
bool m_wasm = false;
5961
};
6062

test/libyul/objectCompiler/function_series.yul

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ object "Contract" {
66
}
77
}
88

9+
// ====
10+
// optimizationPreset: none
911
// ----
1012
// Assembly:
1113
// /* "source":33:48 */

test/libyul/objectCompiler/jump_tags.yul

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ object "Contract" {
66
}
77
}
88

9+
// ====
10+
// optimizationPreset: none
911
// ----
1012
// Assembly:
1113
// /* "source":33:54 */

test/libyul/objectCompiler/long_object_name.yul

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ object "t" {
77
}
88
}
99
// ====
10-
// optimize: true
10+
// optimizationPreset: full
1111
// ----
1212
// Assembly:
1313
// /* "source":23:147 */

test/libyul/objectCompiler/nested_optimizer.yul

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ object "a" {
1515
}
1616
}
1717
// ====
18-
// optimize: true
18+
// optimizationPreset: full
1919
// ----
2020
// Assembly:
2121
// /* "source":48:49 */

0 commit comments

Comments
 (0)