Activate if any optimization is on

This commit is contained in:
Matheus Aguiar 2023-09-21 18:12:16 -03:00
parent 0cb4bd9308
commit 52e6090bfb
8 changed files with 29 additions and 15 deletions

View File

@ -1249,7 +1249,7 @@ bool ContractCompiler::visit(ForStatement const& _forStatement)
Arithmetic previousArithmetic = m_context.arithmetic(); Arithmetic previousArithmetic = m_context.arithmetic();
if ( if (
*_forStatement.annotation().isSimpleCounterLoop && *_forStatement.annotation().isSimpleCounterLoop &&
m_optimiserSettings == OptimiserSettings::standard() m_optimiserSettings != OptimiserSettings::none()
) )
m_context.setArithmetic(Arithmetic::Wrapping); m_context.setArithmetic(Arithmetic::Wrapping);
_forStatement.loopExpression()->accept(*this); _forStatement.loopExpression()->accept(*this);

View File

@ -224,7 +224,7 @@ std::string IRGenerator::generate(
std::string IRGenerator::generate(Block const& _block) std::string IRGenerator::generate(Block const& _block)
{ {
IRGeneratorForStatements generator(m_context, m_utils); IRGeneratorForStatements generator(m_context, m_utils, m_optimiserSettings);
generator.generate(_block); generator.generate(_block);
return generator.code(); return generator.code();
} }
@ -447,7 +447,7 @@ std::string IRGenerator::generateModifier(
(!_modifierInvocation.arguments() || _modifierInvocation.arguments()->empty()), (!_modifierInvocation.arguments() || _modifierInvocation.arguments()->empty()),
"" ""
); );
IRGeneratorForStatements expressionEvaluator(m_context, m_utils); IRGeneratorForStatements expressionEvaluator(m_context, m_utils, m_optimiserSettings);
if (_modifierInvocation.arguments()) if (_modifierInvocation.arguments())
for (size_t i = 0; i < _modifierInvocation.arguments()->size(); i++) for (size_t i = 0; i < _modifierInvocation.arguments()->size(); i++)
{ {
@ -462,7 +462,7 @@ std::string IRGenerator::generateModifier(
} }
t("evalArgs", expressionEvaluator.code()); t("evalArgs", expressionEvaluator.code());
IRGeneratorForStatements generator(m_context, m_utils, [&]() { IRGeneratorForStatements generator(m_context, m_utils, m_optimiserSettings, [&]() {
std::string ret = joinHumanReadable(retParams); std::string ret = joinHumanReadable(retParams);
return return
(ret.empty() ? "" : ret + " := ") + (ret.empty() ? "" : ret + " := ") +
@ -572,7 +572,7 @@ std::string IRGenerator::generateGetter(VariableDeclaration const& _varDecl)
dispenseLocationComment(m_context.mostDerivedContract()) dispenseLocationComment(m_context.mostDerivedContract())
) )
("functionName", functionName) ("functionName", functionName)
("constantValueFunction", IRGeneratorForStatements(m_context, m_utils).constantValueFunction(_varDecl)) ("constantValueFunction", IRGeneratorForStatements(m_context, m_utils, m_optimiserSettings).constantValueFunction(_varDecl))
("ret", suffixedVariableNameList("ret_", 0, _varDecl.type()->sizeOnStack())) ("ret", suffixedVariableNameList("ret_", 0, _varDecl.type()->sizeOnStack()))
.render(); .render();
} }
@ -747,7 +747,7 @@ std::string IRGenerator::generateExternalFunction(ContractDefinition const& _con
std::string IRGenerator::generateInitialAssignment(VariableDeclaration const& _varDecl) std::string IRGenerator::generateInitialAssignment(VariableDeclaration const& _varDecl)
{ {
IRGeneratorForStatements generator(m_context, m_utils); IRGeneratorForStatements generator(m_context, m_utils, m_optimiserSettings);
generator.initializeLocalVar(_varDecl); generator.initializeLocalVar(_varDecl);
return generator.code(); return generator.code();
} }
@ -796,7 +796,7 @@ std::pair<std::string, std::map<ContractDefinition const*, std::vector<std::stri
modifier->arguments() modifier->arguments()
).second, ""); ).second, "");
IRGeneratorForStatements generator{m_context, m_utils}; IRGeneratorForStatements generator{m_context, m_utils, m_optimiserSettings};
for (auto&& [baseContract, arguments]: baseConstructorArguments) for (auto&& [baseContract, arguments]: baseConstructorArguments)
{ {
solAssert(baseContract && arguments, ""); solAssert(baseContract && arguments, "");
@ -817,7 +817,7 @@ std::pair<std::string, std::map<ContractDefinition const*, std::vector<std::stri
std::string IRGenerator::initStateVariables(ContractDefinition const& _contract) std::string IRGenerator::initStateVariables(ContractDefinition const& _contract)
{ {
IRGeneratorForStatements generator{m_context, m_utils}; IRGeneratorForStatements generator{m_context, m_utils, m_optimiserSettings};
for (VariableDeclaration const* variable: _contract.stateVariables()) for (VariableDeclaration const* variable: _contract.stateVariables())
if (!variable->isConstant()) if (!variable->isConstant())
generator.initializeStateVar(*variable); generator.initializeStateVar(*variable);

View File

@ -27,6 +27,7 @@
#include <libsolidity/ast/CallGraph.h> #include <libsolidity/ast/CallGraph.h>
#include <libsolidity/codegen/ir/IRGenerationContext.h> #include <libsolidity/codegen/ir/IRGenerationContext.h>
#include <libsolidity/codegen/YulUtilFunctions.h> #include <libsolidity/codegen/YulUtilFunctions.h>
#include <libsolidity/interface/OptimiserSettings.h>
#include <liblangutil/CharStreamProvider.h> #include <liblangutil/CharStreamProvider.h>
#include <liblangutil/EVMVersion.h> #include <liblangutil/EVMVersion.h>
@ -51,7 +52,8 @@ public:
RevertStrings _revertStrings, RevertStrings _revertStrings,
std::map<std::string, unsigned> _sourceIndices, std::map<std::string, unsigned> _sourceIndices,
langutil::DebugInfoSelection const& _debugInfoSelection, langutil::DebugInfoSelection const& _debugInfoSelection,
langutil::CharStreamProvider const* _soliditySourceProvider langutil::CharStreamProvider const* _soliditySourceProvider,
OptimiserSettings& _optimiserSettings
): ):
m_evmVersion(_evmVersion), m_evmVersion(_evmVersion),
m_eofVersion(_eofVersion), m_eofVersion(_eofVersion),
@ -63,7 +65,8 @@ public:
_debugInfoSelection, _debugInfoSelection,
_soliditySourceProvider _soliditySourceProvider
), ),
m_utils(_evmVersion, m_context.revertStrings(), m_context.functionCollector()) m_utils(_evmVersion, m_context.revertStrings(), m_context.functionCollector()),
m_optimiserSettings(_optimiserSettings)
{} {}
/// Generates and returns (unoptimized) IR code. /// Generates and returns (unoptimized) IR code.
@ -141,6 +144,7 @@ private:
IRGenerationContext m_context; IRGenerationContext m_context;
YulUtilFunctions m_utils; YulUtilFunctions m_utils;
OptimiserSettings m_optimiserSettings;
}; };
} }

View File

@ -352,7 +352,7 @@ std::string IRGeneratorForStatements::constantValueFunction(VariableDeclaration
)"); )");
templ("sourceLocationComment", dispenseLocationComment(_constant, m_context)); templ("sourceLocationComment", dispenseLocationComment(_constant, m_context));
templ("functionName", functionName); templ("functionName", functionName);
IRGeneratorForStatements generator(m_context, m_utils); IRGeneratorForStatements generator(m_context, m_utils, m_optimiserSettings);
solAssert(_constant.value()); solAssert(_constant.value());
Type const& constantType = *_constant.type(); Type const& constantType = *_constant.type();
templ("value", generator.evaluateExpression(*_constant.value(), constantType).commaSeparatedList()); templ("value", generator.evaluateExpression(*_constant.value(), constantType).commaSeparatedList());
@ -3214,7 +3214,7 @@ void IRGeneratorForStatements::generateLoop(
if (_loopExpression) if (_loopExpression)
{ {
Arithmetic previousArithmetic = m_context.arithmetic(); Arithmetic previousArithmetic = m_context.arithmetic();
if (_isSimpleCounterLoop) if (m_optimiserSettings != OptimiserSettings::none() && _isSimpleCounterLoop)
m_context.setArithmetic(Arithmetic::Wrapping); m_context.setArithmetic(Arithmetic::Wrapping);
_loopExpression->accept(*this); _loopExpression->accept(*this);
m_context.setArithmetic(previousArithmetic); m_context.setArithmetic(previousArithmetic);

View File

@ -24,6 +24,7 @@
#include <libsolidity/ast/ASTVisitor.h> #include <libsolidity/ast/ASTVisitor.h>
#include <libsolidity/codegen/ir/IRLValue.h> #include <libsolidity/codegen/ir/IRLValue.h>
#include <libsolidity/codegen/ir/IRVariable.h> #include <libsolidity/codegen/ir/IRVariable.h>
#include <libsolidity/interface/OptimiserSettings.h>
#include <functional> #include <functional>
@ -65,11 +66,13 @@ public:
IRGeneratorForStatements( IRGeneratorForStatements(
IRGenerationContext& _context, IRGenerationContext& _context,
YulUtilFunctions& _utils, YulUtilFunctions& _utils,
OptimiserSettings& _optimiserSettings,
std::function<std::string()> _placeholderCallback = {} std::function<std::string()> _placeholderCallback = {}
): ):
IRGeneratorForStatementsBase(_context), IRGeneratorForStatementsBase(_context),
m_placeholderCallback(std::move(_placeholderCallback)), m_placeholderCallback(std::move(_placeholderCallback)),
m_utils(_utils) m_utils(_utils),
m_optimiserSettings(_optimiserSettings)
{} {}
std::string code() const override; std::string code() const override;
@ -246,6 +249,7 @@ private:
std::function<std::string()> m_placeholderCallback; std::function<std::string()> m_placeholderCallback;
YulUtilFunctions& m_utils; YulUtilFunctions& m_utils;
std::optional<IRLValue> m_currentLValue; std::optional<IRLValue> m_currentLValue;
OptimiserSettings m_optimiserSettings;
}; };
} }

View File

@ -1498,7 +1498,8 @@ void CompilerStack::generateIR(ContractDefinition const& _contract)
m_revertStrings, m_revertStrings,
sourceIndices(), sourceIndices(),
m_debugInfoSelection, m_debugInfoSelection,
this this,
m_optimiserSettings
); );
compiledContract.yulIR = generator.run( compiledContract.yulIR = generator.run(
_contract, _contract,

View File

@ -123,6 +123,11 @@ struct OptimiserSettings
expectedExecutionsPerDeployment == _other.expectedExecutionsPerDeployment; expectedExecutionsPerDeployment == _other.expectedExecutionsPerDeployment;
} }
bool operator!=(OptimiserSettings const& _other) const
{
return !(*this == _other);
}
/// Move literals to the right of commutative binary operators during code generation. /// Move literals to the right of commutative binary operators during code generation.
/// This helps exploiting associativity. /// This helps exploiting associativity.
bool runOrderLiterals = false; bool runOrderLiterals = false;

View File

@ -178,7 +178,7 @@ contract DepositContract is IDepositContract, ERC165 {
// constructor() // constructor()
// gas irOptimized: 1397699 // gas irOptimized: 1397699
// gas legacy: 2391952 // gas legacy: 2391952
// gas legacyOptimized: 1752320 // gas legacyOptimized: 1740144
// supportsInterface(bytes4): 0x0 -> 0 // supportsInterface(bytes4): 0x0 -> 0
// supportsInterface(bytes4): 0xffffffff00000000000000000000000000000000000000000000000000000000 -> false # defined to be false by ERC-165 # // supportsInterface(bytes4): 0xffffffff00000000000000000000000000000000000000000000000000000000 -> false # defined to be false by ERC-165 #
// supportsInterface(bytes4): 0x01ffc9a700000000000000000000000000000000000000000000000000000000 -> true # ERC-165 id # // supportsInterface(bytes4): 0x01ffc9a700000000000000000000000000000000000000000000000000000000 -> true # ERC-165 id #