Extract duplicated code for initializing OptimiserSettings from CommandLineOptions into a common function

This commit is contained in:
Kamil Śliwak 2021-08-03 17:11:17 +02:00
parent 2f663c5f36
commit 6b46d4fdbe
4 changed files with 37 additions and 51 deletions

View File

@ -551,13 +551,7 @@ bool CommandLineInterface::processInput()
}
case InputMode::Assembler:
{
return assemble(
m_options.assembly.inputLanguage,
m_options.assembly.targetMachine,
m_options.optimizer.enabled,
m_options.optimizer.expectedExecutionsPerDeployment,
m_options.optimizer.yulSteps
);
return assemble(m_options.assembly.inputLanguage, m_options.assembly.targetMachine);
}
case InputMode::Linker:
return link();
@ -595,16 +589,7 @@ bool CommandLineInterface::compile()
m_compiler->enableIRGeneration(m_options.compiler.outputs.ir || m_options.compiler.outputs.irOptimized);
m_compiler->enableEwasmGeneration(m_options.compiler.outputs.ewasm);
OptimiserSettings settings = m_options.optimizer.enabled ? OptimiserSettings::standard() : OptimiserSettings::minimal();
if (m_options.optimizer.expectedExecutionsPerDeployment.has_value())
settings.expectedExecutionsPerDeployment = m_options.optimizer.expectedExecutionsPerDeployment.value();
if (m_options.optimizer.noOptimizeYul)
settings.runYulOptimiser = false;
if (m_options.optimizer.yulSteps.has_value())
settings.yulOptimiserSteps = m_options.optimizer.yulSteps.value();
settings.optimizeStackAllocation = settings.runYulOptimiser;
m_compiler->setOptimiserSettings(settings);
m_compiler->setOptimiserSettings(m_options.optimiserSettings());
if (m_options.input.mode == InputMode::CompilerWithASTImport)
{
@ -939,27 +924,21 @@ string CommandLineInterface::objectWithLinkRefsHex(evmasm::LinkerObject const& _
return out;
}
bool CommandLineInterface::assemble(
yul::AssemblyStack::Language _language,
yul::AssemblyStack::Machine _targetMachine,
bool _optimize,
optional<unsigned int> _expectedExecutionsPerDeployment,
optional<string> _yulOptimiserSteps
)
bool CommandLineInterface::assemble(yul::AssemblyStack::Language _language, yul::AssemblyStack::Machine _targetMachine)
{
solAssert(_optimize || !_yulOptimiserSteps.has_value(), "");
bool successful = true;
map<string, yul::AssemblyStack> assemblyStacks;
for (auto const& src: m_fileReader.sourceCodes())
{
OptimiserSettings settings = _optimize ? OptimiserSettings::full() : OptimiserSettings::minimal();
if (_expectedExecutionsPerDeployment.has_value())
settings.expectedExecutionsPerDeployment = _expectedExecutionsPerDeployment.value();
if (_yulOptimiserSteps.has_value())
settings.yulOptimiserSteps = _yulOptimiserSteps.value();
// --no-optimize-yul option is not accepted in assembly mode.
solAssert(!m_options.optimizer.noOptimizeYul, "");
auto& stack = assemblyStacks[src.first] = yul::AssemblyStack(
m_options.output.evmVersion,
_language,
m_options.optimiserSettings()
);
auto& stack = assemblyStacks[src.first] = yul::AssemblyStack(m_options.output.evmVersion, _language, settings);
try
{
if (!stack.parseAndAnalyze(src.first, src.second))

View File

@ -74,13 +74,7 @@ private:
/// @returns the full object with library placeholder hints in hex.
static std::string objectWithLinkRefsHex(evmasm::LinkerObject const& _obj);
bool assemble(
yul::AssemblyStack::Language _language,
yul::AssemblyStack::Machine _targetMachine,
bool _optimize,
std::optional<unsigned int> _expectedExecutionsPerDeployment = std::nullopt,
std::optional<std::string> _yulOptimiserSteps = std::nullopt
);
bool assemble(yul::AssemblyStack::Language _language, yul::AssemblyStack::Machine _targetMachine);
void outputCompilationResults();

View File

@ -302,6 +302,29 @@ bool CommandLineOptions::operator==(CommandLineOptions const& _other) const noex
modelChecker.settings == _other.modelChecker.settings;
}
OptimiserSettings CommandLineOptions::optimiserSettings() const
{
OptimiserSettings settings;
if (optimizer.enabled && input.mode == InputMode::Assembler)
settings = OptimiserSettings::full();
else if (optimizer.enabled)
settings = OptimiserSettings::standard();
else
settings = OptimiserSettings::minimal();
if (optimizer.noOptimizeYul)
settings.runYulOptimiser = false;
if (optimizer.expectedExecutionsPerDeployment.has_value())
settings.expectedExecutionsPerDeployment = optimizer.expectedExecutionsPerDeployment.value();
if (optimizer.yulSteps.has_value())
settings.yulOptimiserSteps = optimizer.yulSteps.value();
return settings;
}
bool CommandLineParser::parseInputPathsAndRemappings()
{
m_options.input.ignoreMissingFiles = (m_args.count(g_strIgnoreMissingFiles) > 0);
@ -988,19 +1011,9 @@ General Information)").c_str(),
if (!m_args[g_strOptimizeRuns].defaulted())
m_options.optimizer.expectedExecutionsPerDeployment = m_args.at(g_strOptimizeRuns).as<unsigned>();
OptimiserSettings optimiserSettings;
if (m_options.optimizer.enabled && m_options.input.mode == InputMode::Assembler)
optimiserSettings = OptimiserSettings::full();
else if (m_options.optimizer.enabled)
optimiserSettings = OptimiserSettings::standard();
else
optimiserSettings = OptimiserSettings::minimal();
if (m_options.optimizer.noOptimizeYul)
optimiserSettings.runYulOptimiser = false;
if (m_args.count(g_strYulOptimizations))
{
OptimiserSettings optimiserSettings = m_options.optimiserSettings();
if (!optimiserSettings.runYulOptimiser)
{
serr() << "--" << g_strYulOptimizations << " is invalid if Yul optimizer is disabled" << endl;

View File

@ -102,6 +102,7 @@ struct CommandLineOptions
bool operator==(CommandLineOptions const& _other) const noexcept;
bool operator!=(CommandLineOptions const& _other) const noexcept { return !(*this == _other); }
OptimiserSettings optimiserSettings() const;
struct
{
@ -169,7 +170,6 @@ struct CommandLineOptions
bool initialize = false;
ModelCheckerSettings settings;
} modelChecker;
};
/// Parses the command-line arguments and produces a filled-out CommandLineOptions structure.