Skip to content

Commit

Permalink
fixup! Accept empty optimizer sequence with Yul optimizer disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
nikola-matic committed Nov 6, 2023
1 parent ec90ea0 commit 10136ff
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 29 deletions.
6 changes: 3 additions & 3 deletions libsolidity/interface/CompilerStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
#include <libyul/YulStack.h>
#include <libyul/AST.h>
#include <libyul/AsmParser.h>
#include <libyul/optimiser/Suite.h>
#include <libyul/optimiser/OptimizerUtilities.h>

#include <liblangutil/Scanner.h>
#include <liblangutil/SemVerHandler.h>
Expand Down Expand Up @@ -1676,8 +1676,8 @@ std::string CompilerStack::createMetadata(Contract const& _contract, bool _forIR
}
else if
(
OptimiserSuite::isEmptyOptimizerSequence(m_optimiserSettings.yulOptimiserSteps) &&
OptimiserSuite::isEmptyOptimizerSequence(m_optimiserSettings.yulOptimiserCleanupSteps)
isEmptyOptimizerSequence(m_optimiserSettings.yulOptimiserSteps) &&
isEmptyOptimizerSequence(m_optimiserSettings.yulOptimiserCleanupSteps)
)
{
details["yulDetails"] = Json::objectValue;
Expand Down
3 changes: 2 additions & 1 deletion libsolidity/interface/StandardCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <libsolidity/ast/ASTJsonExporter.h>
#include <libyul/YulStack.h>
#include <libyul/Exceptions.h>
#include <libyul/optimiser/OptimizerUtilities.h>
#include <libyul/optimiser/Suite.h>

#include <libevmasm/Disassemble.h>
Expand Down Expand Up @@ -463,7 +464,7 @@ std::optional<Json::Value> checkOptimizerDetailSteps(Json::Value const& _details
if (_details[_name].isString())
{
std::string const fullSequence = _details[_name].asString();
if (!_runYulOptimizer && !OptimiserSuite::isEmptyOptimizerSequence(fullSequence))
if (!_runYulOptimizer && !isEmptyOptimizerSequence(fullSequence))
return formatFatalError(Error::Type::JSONError, "If Yul optimizer is disabled, only an empty optimizerSteps sequence is accepted.");

try
Expand Down
3 changes: 2 additions & 1 deletion libyul/YulStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <libyul/backends/evm/EVMObjectCompiler.h>
#include <libyul/backends/evm/EVMMetrics.h>
#include <libyul/ObjectParser.h>
#include <libyul/optimiser/OptimizerUtilities.h>
#include <libyul/optimiser/Semantics.h>
#include <libyul/optimiser/Suite.h>
#include <libevmasm/Assembly.h>
Expand Down Expand Up @@ -171,7 +172,7 @@ void YulStack::optimize(Object& _object, bool _isCreation)
if (!m_optimiserSettings.runYulOptimiser)
{
// Yul optimizer disabled, but empty sequence (:) explicitly provided
if (OptimiserSuite::isEmptyOptimizerSequence(m_optimiserSettings.yulOptimiserSteps + ":" + m_optimiserSettings.yulOptimiserCleanupSteps))
if (isEmptyOptimizerSequence(m_optimiserSettings.yulOptimiserSteps + ":" + m_optimiserSettings.yulOptimiserCleanupSteps))
return std::make_tuple(true, "", "");
// Yul optimizer disabled, and no sequence explicitly provided (assumes default sequence)
else
Expand Down
19 changes: 19 additions & 0 deletions libyul/optimiser/OptimizerUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,22 @@ void StatementRemover::operator()(Block& _block)
);
ASTModifier::operator()(_block);
}

bool yul::isEmptyOptimizerSequence(std::string const& _sequence)
{
size_t delimiterCount{0};
for (char const step: _sequence)
switch (step)
{
case ':':
if (++delimiterCount > 1)
return false;
break;
case ' ':
case '\n':
break;
default:
return false;
}
return true;
}
4 changes: 4 additions & 0 deletions libyul/optimiser/OptimizerUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ std::optional<evmasm::Instruction> toEVMInstruction(Dialect const& _dialect, Yul
/// It returns the default EVM version if dialect is not an EVMDialect.
langutil::EVMVersion const evmVersionFromDialect(Dialect const& _dialect);

/// Check whether the provided sequence is empty provided that the allowed characters are
/// whitespace, newline and :
bool isEmptyOptimizerSequence(std::string const& _sequence);

class StatementRemover: public ASTModifier
{
public:
Expand Down
19 changes: 0 additions & 19 deletions libyul/optimiser/Suite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,25 +383,6 @@ void OptimiserSuite::validateSequence(std::string_view _stepAbbreviations)
assertThrow(nestingLevel == 0, OptimizerException, "Unbalanced brackets");
}

bool OptimiserSuite::isEmptyOptimizerSequence(std::string const& _sequence)
{
size_t delimiterCount{0};
for (char const step: _sequence)
switch (step)
{
case ':':
if (++delimiterCount > 1)
return false;
break;
case ' ':
case '\n':
break;
default:
return false;
}
return true;
}

void OptimiserSuite::runSequence(std::string_view _stepAbbreviations, Block& _ast, bool _repeatUntilStable)
{
validateSequence(_stepAbbreviations);
Expand Down
4 changes: 0 additions & 4 deletions libyul/optimiser/Suite.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ class OptimiserSuite
/// Ensures that specified sequence of step abbreviations is well-formed and can be executed.
/// @throw OptimizerException if the sequence is invalid
static void validateSequence(std::string_view _stepAbbreviations);
/// Check whether the provided sequence is empty provided that the allowed characters are
/// whitespace, newline and :
static bool isEmptyOptimizerSequence(std::string const& _sequence);


void runSequence(std::vector<std::string> const& _steps, Block& _ast);
void runSequence(std::string_view _stepAbbreviations, Block& _ast, bool _repeatUntilStable = false);
Expand Down
3 changes: 2 additions & 1 deletion solc/CommandLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <solc/Exceptions.h>

#include <libyul/optimiser/OptimizerUtilities.h>
#include <libyul/optimiser/Suite.h>

#include <liblangutil/EVMVersion.h>
Expand Down Expand Up @@ -1229,7 +1230,7 @@ void CommandLineParser::processArgs()
if (m_args.count(g_strYulOptimizations))
{
OptimiserSettings optimiserSettings = m_options.optimiserSettings();
if (!optimiserSettings.runYulOptimiser && !OptimiserSuite::isEmptyOptimizerSequence(m_args[g_strYulOptimizations].as<std::string>()))
if (!optimiserSettings.runYulOptimiser && !isEmptyOptimizerSequence(m_args[g_strYulOptimizations].as<std::string>()))
solThrow(CommandLineValidationError, "--" + g_strYulOptimizations + " is invalid with a non-empty sequence if Yul optimizer is disabled.");

try
Expand Down

0 comments on commit 10136ff

Please sign in to comment.