mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #12221 from ethereum/yulOptiNonInteractive
Non-interactive mode for yulopti.
This commit is contained in:
commit
4a49e6e48f
@ -111,7 +111,10 @@ void OptimiserSuite::run(
|
||||
)(*_object.code));
|
||||
Block& ast = *_object.code;
|
||||
|
||||
OptimiserSuite suite(_dialect, reservedIdentifiers, Debug::None, ast, _expectedExecutionsPerDeployment);
|
||||
NameDispenser dispenser{_dialect, ast, reservedIdentifiers};
|
||||
OptimiserStepContext context{_dialect, dispenser, reservedIdentifiers, _expectedExecutionsPerDeployment};
|
||||
|
||||
OptimiserSuite suite(context, Debug::None);
|
||||
|
||||
// Some steps depend on properties ensured by FunctionHoister, BlockFlattener, FunctionGrouper and
|
||||
// ForLoopInitRewriter. Run them first to be able to run arbitrary sequences safely.
|
||||
@ -162,7 +165,7 @@ void OptimiserSuite::run(
|
||||
ast.statements.erase(ast.statements.begin());
|
||||
}
|
||||
|
||||
suite.m_dispenser.reset(ast);
|
||||
dispenser.reset(ast);
|
||||
NameSimplifier::run(suite.m_context, ast);
|
||||
VarNameCleaner::run(suite.m_context, ast);
|
||||
|
||||
|
@ -59,6 +59,8 @@ public:
|
||||
PrintStep,
|
||||
PrintChanges
|
||||
};
|
||||
OptimiserSuite(OptimiserStepContext& _context, Debug _debug = Debug::None): m_context(_context), m_debug(_debug) {}
|
||||
|
||||
/// The value nullopt for `_expectedExecutionsPerDeployment` represents creation code.
|
||||
static void run(
|
||||
Dialect const& _dialect,
|
||||
@ -82,20 +84,7 @@ public:
|
||||
static std::map<char, std::string> const& stepAbbreviationToNameMap();
|
||||
|
||||
private:
|
||||
OptimiserSuite(
|
||||
Dialect const& _dialect,
|
||||
std::set<YulString> const& _externallyUsedIdentifiers,
|
||||
Debug _debug,
|
||||
Block& _ast,
|
||||
std::optional<size_t> expectedExecutionsPerDeployment
|
||||
):
|
||||
m_dispenser{_dialect, _ast, _externallyUsedIdentifiers},
|
||||
m_context{_dialect, m_dispenser, _externallyUsedIdentifiers, expectedExecutionsPerDeployment},
|
||||
m_debug(_debug)
|
||||
{}
|
||||
|
||||
NameDispenser m_dispenser;
|
||||
OptimiserStepContext m_context;
|
||||
OptimiserStepContext& m_context;
|
||||
Debug m_debug;
|
||||
};
|
||||
|
||||
|
@ -76,28 +76,31 @@ namespace po = boost::program_options;
|
||||
class YulOpti
|
||||
{
|
||||
public:
|
||||
void printErrors()
|
||||
static void printErrors(CharStream const& _charStream, ErrorList const& _errors)
|
||||
{
|
||||
SourceReferenceFormatter{
|
||||
cerr,
|
||||
SingletonCharStreamProvider(*m_charStream),
|
||||
SingletonCharStreamProvider(_charStream),
|
||||
true,
|
||||
false
|
||||
}.printErrorInformation(m_errors);
|
||||
}.printErrorInformation(_errors);
|
||||
}
|
||||
|
||||
bool parse(string const& _input)
|
||||
void parse(string const& _input)
|
||||
{
|
||||
ErrorReporter errorReporter(m_errors);
|
||||
m_charStream = make_shared<CharStream>(_input, "");
|
||||
m_ast = yul::Parser(errorReporter, m_dialect).parse(*m_charStream);
|
||||
ErrorList errors;
|
||||
ErrorReporter errorReporter(errors);
|
||||
CharStream _charStream(_input, "");
|
||||
try
|
||||
{
|
||||
m_ast = yul::Parser(errorReporter, m_dialect).parse(_charStream);
|
||||
if (!m_ast || !errorReporter.errors().empty())
|
||||
{
|
||||
cerr << "Error parsing source." << endl;
|
||||
printErrors();
|
||||
return false;
|
||||
printErrors(_charStream, errors);
|
||||
throw std::runtime_error("Could not parse source.");
|
||||
}
|
||||
m_analysisInfo = make_shared<yul::AsmAnalysisInfo>();
|
||||
m_analysisInfo = make_unique<yul::AsmAnalysisInfo>();
|
||||
AsmAnalyzer analyzer(
|
||||
*m_analysisInfo,
|
||||
errorReporter,
|
||||
@ -106,28 +109,33 @@ public:
|
||||
if (!analyzer.analyze(*m_ast) || !errorReporter.errors().empty())
|
||||
{
|
||||
cerr << "Error analyzing source." << endl;
|
||||
printErrors();
|
||||
return false;
|
||||
printErrors(_charStream, errors);
|
||||
throw std::runtime_error("Could not analyze source.");
|
||||
}
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
cerr << "Fatal error during parsing: " << endl;
|
||||
printErrors(_charStream, errors);
|
||||
throw;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void printUsageBanner(
|
||||
map<char, string> const& _optimizationSteps,
|
||||
map<char, string> const& _extraOptions,
|
||||
size_t _columns
|
||||
)
|
||||
{
|
||||
yulAssert(_columns > 0);
|
||||
|
||||
auto const& optimiserSteps = OptimiserSuite::stepAbbreviationToNameMap();
|
||||
auto hasShorterString = [](auto const& a, auto const& b) { return a.second.size() < b.second.size(); };
|
||||
size_t longestDescriptionLength = std::max(
|
||||
max_element(_optimizationSteps.begin(), _optimizationSteps.end(), hasShorterString)->second.size(),
|
||||
max_element(optimiserSteps.begin(), optimiserSteps.end(), hasShorterString)->second.size(),
|
||||
max_element(_extraOptions.begin(), _extraOptions.end(), hasShorterString)->second.size()
|
||||
);
|
||||
|
||||
vector<string> overlappingAbbreviations =
|
||||
ranges::views::set_intersection(_extraOptions | ranges::views::keys, _optimizationSteps | ranges::views::keys) |
|
||||
ranges::views::set_intersection(_extraOptions | ranges::views::keys, optimiserSteps | ranges::views::keys) |
|
||||
ranges::views::transform([](char _abbreviation){ return string(1, _abbreviation); }) |
|
||||
ranges::to<vector>();
|
||||
|
||||
@ -141,7 +149,7 @@ public:
|
||||
);
|
||||
|
||||
vector<tuple<char, string>> sortedOptions =
|
||||
ranges::views::concat(_optimizationSteps, _extraOptions) |
|
||||
ranges::views::concat(optimiserSteps, _extraOptions) |
|
||||
ranges::to<vector<tuple<char, string>>>() |
|
||||
ranges::actions::sort([](tuple<char, string> const& _a, tuple<char, string> const& _b) {
|
||||
return (
|
||||
@ -162,24 +170,28 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void runInteractive(string source)
|
||||
{
|
||||
bool disambiguated = false;
|
||||
while (true)
|
||||
{
|
||||
cout << "----------------------" << endl;
|
||||
cout << source << endl;
|
||||
if (!parse(source))
|
||||
return;
|
||||
set<YulString> reservedIdentifiers;
|
||||
if (!disambiguated)
|
||||
void disambiguate()
|
||||
{
|
||||
*m_ast = std::get<yul::Block>(Disambiguator(m_dialect, *m_analysisInfo)(*m_ast));
|
||||
m_analysisInfo.reset();
|
||||
m_nameDispenser = make_shared<NameDispenser>(m_dialect, *m_ast, reservedIdentifiers);
|
||||
disambiguated = true;
|
||||
m_nameDispenser.reset(*m_ast);
|
||||
}
|
||||
map<char, string> const& abbreviationMap = OptimiserSuite::stepAbbreviationToNameMap();
|
||||
|
||||
void runSteps(string _source, string _steps)
|
||||
{
|
||||
parse(_source);
|
||||
disambiguate();
|
||||
OptimiserSuite{m_context}.runSequence(_steps, *m_ast);
|
||||
cout << AsmPrinter{m_dialect}(*m_ast) << endl;
|
||||
}
|
||||
|
||||
void runInteractive(string _source, bool _disambiguated = false)
|
||||
{
|
||||
bool disambiguated = _disambiguated;
|
||||
while (true)
|
||||
{
|
||||
parse(_source);
|
||||
disambiguated = disambiguated || (disambiguate(), true);
|
||||
map<char, string> const& extraOptions = {
|
||||
// QUIT starts with a non-letter character on purpose to get it to show up on top of the list
|
||||
{'#', ">>> QUIT <<<"},
|
||||
@ -187,32 +199,21 @@ public:
|
||||
{';', "StackCompressor"}
|
||||
};
|
||||
|
||||
printUsageBanner(abbreviationMap, extraOptions, 4);
|
||||
printUsageBanner(extraOptions, 4);
|
||||
cout << "? ";
|
||||
cout.flush();
|
||||
// TODO: handle EOF properly.
|
||||
char option = static_cast<char>(readStandardInputChar());
|
||||
cout << ' ' << option << endl;
|
||||
|
||||
OptimiserStepContext context{
|
||||
m_dialect,
|
||||
*m_nameDispenser,
|
||||
reservedIdentifiers,
|
||||
solidity::frontend::OptimiserSettings::standard().expectedExecutionsPerDeployment
|
||||
};
|
||||
|
||||
auto abbreviationAndName = abbreviationMap.find(option);
|
||||
if (abbreviationAndName != abbreviationMap.end())
|
||||
try
|
||||
{
|
||||
OptimiserStep const& step = *OptimiserSuite::allSteps().at(abbreviationAndName->second);
|
||||
step.run(context, *m_ast);
|
||||
}
|
||||
else switch (option)
|
||||
switch (option)
|
||||
{
|
||||
case 4:
|
||||
case '#':
|
||||
return;
|
||||
case ',':
|
||||
VarNameCleaner::run(context, *m_ast);
|
||||
VarNameCleaner::run(m_context, *m_ast);
|
||||
// VarNameCleaner destroys the unique names guarantee of the disambiguator.
|
||||
disambiguated = false;
|
||||
break;
|
||||
@ -224,30 +225,51 @@ public:
|
||||
break;
|
||||
}
|
||||
default:
|
||||
cerr << "Unknown option." << endl;
|
||||
OptimiserSuite{m_context}.runSequence(
|
||||
std::string_view(&option, 1),
|
||||
*m_ast
|
||||
);
|
||||
}
|
||||
source = AsmPrinter{m_dialect}(*m_ast);
|
||||
_source = AsmPrinter{m_dialect}(*m_ast);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
cerr << endl << "Exception during optimiser step:" << endl;
|
||||
cerr << boost::current_exception_diagnostic_information() << endl;
|
||||
}
|
||||
cout << "----------------------" << endl;
|
||||
cout << _source << endl;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
ErrorList m_errors;
|
||||
shared_ptr<CharStream> m_charStream;
|
||||
shared_ptr<yul::Block> m_ast;
|
||||
Dialect const& m_dialect{EVMDialect::strictAssemblyForEVMObjects(EVMVersion{})};
|
||||
shared_ptr<AsmAnalysisInfo> m_analysisInfo;
|
||||
shared_ptr<NameDispenser> m_nameDispenser;
|
||||
unique_ptr<AsmAnalysisInfo> m_analysisInfo;
|
||||
set<YulString> const m_reservedIdentifiers = {};
|
||||
NameDispenser m_nameDispenser{m_dialect, m_reservedIdentifiers};
|
||||
OptimiserStepContext m_context{
|
||||
m_dialect,
|
||||
m_nameDispenser,
|
||||
m_reservedIdentifiers,
|
||||
solidity::frontend::OptimiserSettings::standard().expectedExecutionsPerDeployment
|
||||
};
|
||||
};
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
try
|
||||
{
|
||||
bool nonInteractive = false;
|
||||
po::options_description options(
|
||||
R"(yulopti, yul optimizer exploration tool.
|
||||
Usage: yulopti [Options] <file>
|
||||
Reads <file> as yul code and applies optimizer steps to it,
|
||||
interactively read from stdin.
|
||||
Usage: yulopti [Options] <file>
|
||||
Reads <file> as yul code and applies optimizer steps to it,
|
||||
interactively read from stdin.
|
||||
In non-interactive mode a list of steps has to be provided.
|
||||
If <file> is -, yul code is read from stdin and run non-interactively.
|
||||
|
||||
Allowed options)",
|
||||
Allowed options)",
|
||||
po::options_description::m_default_line_length,
|
||||
po::options_description::m_default_line_length - 23);
|
||||
options.add_options()
|
||||
@ -256,30 +278,80 @@ Allowed options)",
|
||||
po::value<string>(),
|
||||
"input file"
|
||||
)
|
||||
("help", "Show this help screen.");
|
||||
(
|
||||
"steps",
|
||||
po::value<string>(),
|
||||
"steps to execute non-interactively"
|
||||
)
|
||||
(
|
||||
"non-interactive,n",
|
||||
po::bool_switch(&nonInteractive)->default_value(false),
|
||||
"stop after executing the provided steps"
|
||||
)
|
||||
("help,h", "Show this help screen.");
|
||||
|
||||
// All positional options should be interpreted as input files
|
||||
po::positional_options_description filesPositions;
|
||||
filesPositions.add("input-file", 1);
|
||||
|
||||
po::variables_map arguments;
|
||||
try
|
||||
{
|
||||
po::command_line_parser cmdLineParser(argc, argv);
|
||||
cmdLineParser.options(options).positional(filesPositions);
|
||||
po::store(cmdLineParser.run(), arguments);
|
||||
po::notify(arguments);
|
||||
|
||||
if (arguments.count("help"))
|
||||
{
|
||||
cout << options;
|
||||
return 0;
|
||||
}
|
||||
|
||||
string input;
|
||||
if (arguments.count("input-file"))
|
||||
{
|
||||
string filename = arguments["input-file"].as<string>();
|
||||
if (filename == "-")
|
||||
{
|
||||
nonInteractive = true;
|
||||
input = readUntilEnd(cin);
|
||||
}
|
||||
else
|
||||
input = readFileAsString(arguments["input-file"].as<string>());
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << options;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (nonInteractive && !arguments.count("steps"))
|
||||
{
|
||||
cout << options;
|
||||
return 1;
|
||||
}
|
||||
|
||||
YulOpti yulOpti;
|
||||
bool disambiguated = false;
|
||||
if (!nonInteractive)
|
||||
cout << input << endl;
|
||||
if (arguments.count("steps"))
|
||||
{
|
||||
string sequence = arguments["steps"].as<string>();
|
||||
if (!nonInteractive)
|
||||
cout << "----------------------" << endl;
|
||||
yulOpti.runSteps(input, sequence);
|
||||
disambiguated = true;
|
||||
}
|
||||
if (!nonInteractive)
|
||||
yulOpti.runInteractive(input, disambiguated);
|
||||
|
||||
return 0;
|
||||
}
|
||||
catch (po::error const& _exception)
|
||||
{
|
||||
cerr << _exception.what() << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
string input;
|
||||
try
|
||||
{
|
||||
input = readFileAsString(arguments["input-file"].as<string>());
|
||||
}
|
||||
catch (FileNotFound const& _exception)
|
||||
{
|
||||
cerr << "File not found:" << _exception.comment() << endl;
|
||||
@ -290,11 +362,10 @@ Allowed options)",
|
||||
cerr << "Not a regular file:" << _exception.comment() << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (arguments.count("input-file"))
|
||||
YulOpti{}.runInteractive(input);
|
||||
else
|
||||
cout << options;
|
||||
|
||||
return 0;
|
||||
catch(...)
|
||||
{
|
||||
cerr << endl << "Exception:" << endl;
|
||||
cerr << boost::current_exception_diagnostic_information() << endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user