Move the default optimisation steps from OptimiserSuite to OptimiserSettings

- Now it's a mandatory parameter in OptimiserSuite::run()
This commit is contained in:
Kamil Śliwak 2020-04-24 14:19:36 +02:00
parent 69b79f848b
commit c41a832f65
6 changed files with 38 additions and 20 deletions

View File

@ -502,6 +502,7 @@ void CompilerContext::optimizeYul(yul::Object& _object, yul::EVMDialect const& _
&meter,
_object,
_optimiserSettings.optimizeStackAllocation,
_optimiserSettings.yulOptimiserSteps,
_externalIdentifiers
);

View File

@ -23,12 +23,31 @@
#pragma once
#include <cstddef>
#include <string>
namespace solidity::frontend
{
struct OptimiserSettings
{
static char constexpr DefaultYulOptimiserSteps[] =
"dhfoDgvulfnTUtnIf" // None of these can make stack problems worse
"["
"xarrscLM" // Turn into SSA and simplify
"cCTUtTOntnfDIul" // Perform structural simplification
"Lcul" // Simplify again
"Vcul jj" // Reverse SSA
// should have good "compilability" property here.
"eul" // Run functional expression inliner
"xarulrul" // Prune a bit more in SSA
"xarrcL" // Turn into SSA again and simplify
"gvif" // Run full inliner
"CTUcarrLsTOtfDncarrIulc" // SSA plus simplify
"]"
"jmuljuljul VcTOcul jmul"; // Make source short and pretty
/// No optimisations at all - not recommended.
static OptimiserSettings none()
{
@ -74,6 +93,7 @@ struct OptimiserSettings
runConstantOptimiser == _other.runConstantOptimiser &&
optimizeStackAllocation == _other.optimizeStackAllocation &&
runYulOptimiser == _other.runYulOptimiser &&
yulOptimiserSteps == _other.yulOptimiserSteps &&
expectedExecutionsPerDeployment == _other.expectedExecutionsPerDeployment;
}
@ -95,6 +115,11 @@ struct OptimiserSettings
bool optimizeStackAllocation = false;
/// Yul optimiser with default settings. Will only run on certain parts of the code for now.
bool runYulOptimiser = false;
/// Sequence of optimisation steps to be performed by Yul optimiser.
/// Note that there are some hard-coded steps in the optimiser and you cannot disable
/// them just by setting this to an empty string. Set @a runYulOptimiser to false if you want
/// no optimisations.
std::string yulOptimiserSteps = DefaultYulOptimiserSteps;
/// This specifies an estimate on how often each opcode in this assembly will be executed,
/// i.e. use a small value to optimise for size and a large value to optimise for runtime gas usage.
size_t expectedExecutionsPerDeployment = 200;

View File

@ -183,7 +183,8 @@ void AssemblyStack::optimize(Object& _object, bool _isCreation)
dialect,
meter.get(),
_object,
m_optimiserSettings.optimizeStackAllocation
m_optimiserSettings.optimizeStackAllocation,
m_optimiserSettings.yulOptimiserSteps
);
}

View File

@ -79,6 +79,7 @@ void OptimiserSuite::run(
GasMeter const* _meter,
Object& _object,
bool _optimizeStackAllocation,
string const& _optimisationSequence,
set<YulString> const& _externallyUsedIdentifiers
)
{
@ -94,25 +95,12 @@ void OptimiserSuite::run(
OptimiserSuite suite(_dialect, reservedIdentifiers, Debug::None, ast);
suite.runSequence(
"dhfoDgvulfnTUtnIf" // None of these can make stack problems worse
"["
"xarrscLM" // Turn into SSA and simplify
"cCTUtTOntnfDIul" // Perform structural simplification
"Lcul" // Simplify again
"Vcul jj" // Reverse SSA
// Some steps depend on properties ensured by FunctionHoister, FunctionGrouper and
// ForLoopInitRewriter. Run them first to be able to run arbitrary sequences safely.
suite.runSequence("fgo", ast);
// should have good "compilability" property here.
"eul" // Run functional expression inliner
"xarulrul" // Prune a bit more in SSA
"xarrcL" // Turn into SSA again and simplify
"gvif" // Run full inliner
"CTUcarrLsTOtfDncarrIulc" // SSA plus simplify
"]"
"jmuljuljul VcTOcul jmul", // Make source short and pretty
ast
);
// Now the user-supplied part
suite.runSequence(_optimisationSequence, ast);
// This is a tuning parameter, but actually just prevents infinite loops.
size_t stackCompressorMaxIterations = 16;

View File

@ -62,6 +62,7 @@ public:
GasMeter const* _meter,
Object& _object,
bool _optimizeStackAllocation,
std::string const& _optimisationSequence,
std::set<YulString> const& _externallyUsedIdentifiers = {}
);

View File

@ -74,6 +74,8 @@
#include <libsolutil/AnsiColorized.h>
#include <libsolidity/interface/OptimiserSettings.h>
#include <boost/test/unit_test.hpp>
#include <boost/algorithm/string.hpp>
@ -342,7 +344,7 @@ TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _line
yul::Object obj;
obj.code = m_ast;
obj.analysisInfo = m_analysisInfo;
OptimiserSuite::run(*m_dialect, &meter, obj, true);
OptimiserSuite::run(*m_dialect, &meter, obj, true, solidity::frontend::OptimiserSettings::DefaultYulOptimiserSteps);
}
else
{