solidity/libyul/optimiser/Suite.cpp

508 lines
16 KiB
C++
Raw Normal View History

2018-10-16 19:39:22 +00:00
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
2018-10-16 19:39:22 +00:00
/**
* Optimiser suite that combines all steps and also provides the settings for the heuristics.
*/
#include <libyul/optimiser/Suite.h>
#include <libyul/optimiser/Disambiguator.h>
2018-12-13 14:26:01 +00:00
#include <libyul/optimiser/VarDeclInitializer.h>
2019-01-08 14:19:35 +00:00
#include <libyul/optimiser/BlockFlattener.h>
#include <libyul/optimiser/CallGraphGenerator.h>
#include <libyul/optimiser/CircularReferencesPruner.h>
2019-05-09 19:56:56 +00:00
#include <libyul/optimiser/ControlFlowSimplifier.h>
2019-09-11 09:42:59 +00:00
#include <libyul/optimiser/ConditionalSimplifier.h>
2019-10-07 08:06:00 +00:00
#include <libyul/optimiser/ConditionalUnsimplifier.h>
2019-03-28 13:18:17 +00:00
#include <libyul/optimiser/DeadCodeEliminator.h>
2018-10-16 19:39:22 +00:00
#include <libyul/optimiser/FunctionGrouper.h>
#include <libyul/optimiser/FunctionHoister.h>
2021-11-11 14:54:38 +00:00
#include <libyul/optimiser/EqualStoreEliminator.h>
#include <libyul/optimiser/EquivalentFunctionCombiner.h>
2018-10-16 19:39:22 +00:00
#include <libyul/optimiser/ExpressionSplitter.h>
#include <libyul/optimiser/ExpressionJoiner.h>
#include <libyul/optimiser/ExpressionInliner.h>
#include <libyul/optimiser/FullInliner.h>
#include <libyul/optimiser/ForLoopConditionIntoBody.h>
2019-09-11 16:55:31 +00:00
#include <libyul/optimiser/ForLoopConditionOutOfBody.h>
#include <libyul/optimiser/ForLoopInitRewriter.h>
2019-09-11 09:42:59 +00:00
#include <libyul/optimiser/ForLoopConditionIntoBody.h>
#include <libyul/optimiser/FunctionSpecializer.h>
2018-10-16 19:39:22 +00:00
#include <libyul/optimiser/Rematerialiser.h>
#include <libyul/optimiser/UnusedFunctionParameterPruner.h>
2018-10-16 19:39:22 +00:00
#include <libyul/optimiser/UnusedPruner.h>
#include <libyul/optimiser/ExpressionSimplifier.h>
#include <libyul/optimiser/CommonSubexpressionEliminator.h>
#include <libyul/optimiser/Semantics.h>
2019-01-16 10:44:45 +00:00
#include <libyul/optimiser/SSAReverser.h>
2018-10-16 19:39:22 +00:00
#include <libyul/optimiser/SSATransform.h>
2019-02-04 16:30:29 +00:00
#include <libyul/optimiser/StackCompressor.h>
2020-07-02 10:48:20 +00:00
#include <libyul/optimiser/StackLimitEvader.h>
#include <libyul/optimiser/StructuralSimplifier.h>
2019-10-07 06:16:28 +00:00
#include <libyul/optimiser/SyntacticalEquality.h>
#include <libyul/optimiser/UnusedAssignEliminator.h>
2021-05-05 16:02:35 +00:00
#include <libyul/optimiser/UnusedStoreEliminator.h>
#include <libyul/optimiser/VarNameCleaner.h>
2019-08-29 14:43:06 +00:00
#include <libyul/optimiser/LoadResolver.h>
#include <libyul/optimiser/LoopInvariantCodeMotion.h>
#include <libyul/optimiser/Metrics.h>
2020-08-06 19:44:50 +00:00
#include <libyul/optimiser/NameSimplifier.h>
2019-05-28 10:57:15 +00:00
#include <libyul/backends/evm/ConstantOptimiser.h>
#include <libyul/AsmAnalysis.h>
#include <libyul/AsmAnalysisInfo.h>
#include <libyul/AsmPrinter.h>
#include <libyul/AST.h>
#include <libyul/Object.h>
2018-10-16 19:39:22 +00:00
#include <libyul/backends/evm/NoOutputAssembly.h>
#include <libsolutil/CommonData.h>
2018-10-16 19:39:22 +00:00
2020-07-02 10:48:20 +00:00
#include <libyul/CompilabilityChecker.h>
#include <range/v3/view/map.hpp>
#include <range/v3/action/remove.hpp>
#include <limits>
#include <tuple>
#ifdef PROFILE_OPTIMIZER_STEPS
#include <chrono>
#include <fmt/format.h>
#endif
2018-10-16 19:39:22 +00:00
using namespace std;
2019-12-11 16:31:36 +00:00
using namespace solidity;
using namespace solidity::yul;
#ifdef PROFILE_OPTIMIZER_STEPS
using namespace std::chrono;
#endif
namespace
{
#ifdef PROFILE_OPTIMIZER_STEPS
void outputPerformanceMetrics(map<string, int64_t> const& _metrics)
{
vector<pair<string, int64_t>> durations(_metrics.begin(), _metrics.end());
sort(
durations.begin(),
durations.end(),
[](pair<string, int64_t> const& _lhs, pair<string, int64_t> const& _rhs) -> bool
{
return _lhs.second < _rhs.second;
}
);
int64_t totalDurationInMicroseconds = 0;
for (auto&& [step, durationInMicroseconds]: durations)
totalDurationInMicroseconds += durationInMicroseconds;
cerr << "Performance metrics of optimizer steps" << endl;
cerr << "======================================" << endl;
constexpr double microsecondsInSecond = 1000000;
for (auto&& [step, durationInMicroseconds]: durations)
{
double percentage = 100.0 * static_cast<double>(durationInMicroseconds) / static_cast<double>(totalDurationInMicroseconds);
double sec = static_cast<double>(durationInMicroseconds) / microsecondsInSecond;
cerr << fmt::format("{:>7.3f}% ({} s): {}", percentage, sec, step) << endl;
}
double totalDurationInSeconds = static_cast<double>(totalDurationInMicroseconds) / microsecondsInSecond;
cerr << "--------------------------------------" << endl;
cerr << fmt::format("{:>7}% ({:.3f} s)", 100, totalDurationInSeconds) << endl;
}
#endif
}
2018-10-16 19:39:22 +00:00
void OptimiserSuite::run(
Dialect const& _dialect,
2019-05-29 21:14:06 +00:00
GasMeter const* _meter,
Object& _object,
bool _optimizeStackAllocation,
string_view _optimisationSequence,
string_view _optimisationCleanupSequence,
optional<size_t> _expectedExecutionsPerDeployment,
set<YulString> const& _externallyUsedIdentifiers
2018-10-16 19:39:22 +00:00
)
{
EVMDialect const* evmDialect = dynamic_cast<EVMDialect const*>(&_dialect);
bool usesOptimizedCodeGenerator =
_optimizeStackAllocation &&
evmDialect &&
evmDialect->evmVersion().canOverchargeGasForCall() &&
evmDialect->providesObjectAccess();
set<YulString> reservedIdentifiers = _externallyUsedIdentifiers;
2019-05-29 21:14:06 +00:00
reservedIdentifiers += _dialect.fixedFunctionNames();
2018-10-16 19:39:22 +00:00
*_object.code = std::get<Block>(Disambiguator(
_dialect,
*_object.analysisInfo,
reservedIdentifiers
)(*_object.code));
Block& ast = *_object.code;
2018-10-16 19:39:22 +00:00
NameDispenser dispenser{_dialect, ast, reservedIdentifiers};
OptimiserStepContext context{_dialect, dispenser, reservedIdentifiers, _expectedExecutionsPerDeployment};
OptimiserSuite suite(context, Debug::None);
2019-09-23 14:32:50 +00:00
// Some steps depend on properties ensured by FunctionHoister, BlockFlattener, FunctionGrouper and
// ForLoopInitRewriter. Run them first to be able to run arbitrary sequences safely.
suite.runSequence("hgfo", ast);
NameSimplifier::run(suite.m_context, ast);
// Now the user-supplied part
suite.runSequence(_optimisationSequence, ast);
2018-10-16 19:39:22 +00:00
// This is a tuning parameter, but actually just prevents infinite loops.
size_t stackCompressorMaxIterations = 16;
suite.runSequence("g", ast);
// We ignore the return value because we will get a much better error
// message once we perform code generation.
if (!usesOptimizedCodeGenerator)
StackCompressor::run(
_dialect,
_object,
_optimizeStackAllocation,
stackCompressorMaxIterations
);
2022-08-23 09:58:12 +00:00
// Run the user-supplied clean up sequence
suite.runSequence(_optimisationCleanupSequence, ast);
2022-09-12 08:57:02 +00:00
// Hard-coded FunctionGrouper step is used to bring the AST into a canonical form required by the StackCompressor
// and StackLimitEvader. This is hard-coded as the last step, as some previously executed steps may break the
// aforementioned form, thus causing the StackCompressor/StackLimitEvader to throw.
suite.runSequence("g", ast);
2019-05-15 08:33:35 +00:00
if (evmDialect)
2019-05-29 21:14:06 +00:00
{
yulAssert(_meter, "");
ConstantOptimiser{*evmDialect, *_meter}(ast);
if (usesOptimizedCodeGenerator)
{
StackCompressor::run(
2020-07-02 10:48:20 +00:00
_dialect,
_object,
_optimizeStackAllocation,
stackCompressorMaxIterations
);
if (evmDialect->providesObjectAccess())
StackLimitEvader::run(suite.m_context, _object);
}
else if (evmDialect->providesObjectAccess() && _optimizeStackAllocation)
StackLimitEvader::run(suite.m_context, _object);
2019-05-29 21:14:06 +00:00
}
dispenser.reset(ast);
NameSimplifier::run(suite.m_context, ast);
VarNameCleaner::run(suite.m_context, ast);
2019-02-04 16:30:29 +00:00
#ifdef PROFILE_OPTIMIZER_STEPS
outputPerformanceMetrics(suite.m_durationPerStepInMicroseconds);
#endif
*_object.analysisInfo = AsmAnalyzer::analyzeStrictAssertCorrect(_dialect, _object);
2018-10-16 19:39:22 +00:00
}
2019-09-23 14:32:50 +00:00
namespace
{
template <class... Step>
map<string, unique_ptr<OptimiserStep>> optimiserStepCollection()
{
map<string, unique_ptr<OptimiserStep>> ret;
2019-12-11 16:31:36 +00:00
for (unique_ptr<OptimiserStep>& s: util::make_vector<unique_ptr<OptimiserStep>>(
2019-09-23 14:32:50 +00:00
(make_unique<OptimiserStepInstance<Step>>())...
))
{
yulAssert(!ret.count(s->name), "");
ret[s->name] = std::move(s);
}
return ret;
}
}
map<string, unique_ptr<OptimiserStep>> const& OptimiserSuite::allSteps()
{
static map<string, unique_ptr<OptimiserStep>> instance;
if (instance.empty())
instance = optimiserStepCollection<
BlockFlattener,
CircularReferencesPruner,
2019-09-23 14:32:50 +00:00
CommonSubexpressionEliminator,
2019-09-11 09:42:59 +00:00
ConditionalSimplifier,
2019-10-07 08:06:00 +00:00
ConditionalUnsimplifier,
2019-09-23 14:32:50 +00:00
ControlFlowSimplifier,
DeadCodeEliminator,
2021-11-11 14:54:38 +00:00
EqualStoreEliminator,
2019-09-23 14:32:50 +00:00
EquivalentFunctionCombiner,
ExpressionInliner,
ExpressionJoiner,
ExpressionSimplifier,
ExpressionSplitter,
ForLoopConditionIntoBody,
ForLoopConditionOutOfBody,
ForLoopInitRewriter,
FullInliner,
FunctionGrouper,
FunctionHoister,
FunctionSpecializer,
2019-09-23 14:32:50 +00:00
LiteralRematerialiser,
LoadResolver,
LoopInvariantCodeMotion,
UnusedAssignEliminator,
2021-05-05 16:02:35 +00:00
UnusedStoreEliminator,
2019-09-23 14:32:50 +00:00
Rematerialiser,
SSAReverser,
SSATransform,
StructuralSimplifier,
UnusedFunctionParameterPruner,
2019-09-23 14:32:50 +00:00
UnusedPruner,
VarDeclInitializer
2019-09-23 14:32:50 +00:00
>();
// Does not include VarNameCleaner because it destroys the property of unique names.
// Does not include NameSimplifier.
2019-09-23 14:32:50 +00:00
return instance;
}
map<string, char> const& OptimiserSuite::stepNameToAbbreviationMap()
{
static map<string, char> lookupTable{
{BlockFlattener::name, 'f'},
{CircularReferencesPruner::name, 'l'},
{CommonSubexpressionEliminator::name, 'c'},
{ConditionalSimplifier::name, 'C'},
{ConditionalUnsimplifier::name, 'U'},
{ControlFlowSimplifier::name, 'n'},
{DeadCodeEliminator::name, 'D'},
2021-11-11 14:54:38 +00:00
{EqualStoreEliminator::name, 'E'},
{EquivalentFunctionCombiner::name, 'v'},
{ExpressionInliner::name, 'e'},
{ExpressionJoiner::name, 'j'},
{ExpressionSimplifier::name, 's'},
{ExpressionSplitter::name, 'x'},
{ForLoopConditionIntoBody::name, 'I'},
{ForLoopConditionOutOfBody::name, 'O'},
{ForLoopInitRewriter::name, 'o'},
{FullInliner::name, 'i'},
{FunctionGrouper::name, 'g'},
{FunctionHoister::name, 'h'},
{FunctionSpecializer::name, 'F'},
{LiteralRematerialiser::name, 'T'},
{LoadResolver::name, 'L'},
{LoopInvariantCodeMotion::name, 'M'},
{UnusedAssignEliminator::name, 'r'},
2021-05-05 16:02:35 +00:00
{UnusedStoreEliminator::name, 'S'},
{Rematerialiser::name, 'm'},
{SSAReverser::name, 'V'},
{SSATransform::name, 'a'},
{StructuralSimplifier::name, 't'},
{UnusedFunctionParameterPruner::name, 'p'},
{UnusedPruner::name, 'u'},
{VarDeclInitializer::name, 'd'},
};
yulAssert(lookupTable.size() == allSteps().size(), "");
yulAssert((
util::convertContainer<set<char>>(string(NonStepAbbreviations)) -
util::convertContainer<set<char>>(lookupTable | ranges::views::values)
).size() == string(NonStepAbbreviations).size(),
"Step abbreviation conflicts with a character reserved for another syntactic element"
);
return lookupTable;
}
map<char, string> const& OptimiserSuite::stepAbbreviationToNameMap()
{
static map<char, string> lookupTable = util::invertMap(stepNameToAbbreviationMap());
return lookupTable;
}
void OptimiserSuite::validateSequence(string_view _stepAbbreviations)
{
int8_t nestingLevel = 0;
int8_t colonDelimiters = 0;
for (char abbreviation: _stepAbbreviations)
switch (abbreviation)
{
case ' ':
case '\n':
break;
case '[':
assertThrow(nestingLevel < numeric_limits<int8_t>::max(), OptimizerException, "Brackets nested too deep");
nestingLevel++;
break;
case ']':
nestingLevel--;
assertThrow(nestingLevel >= 0, OptimizerException, "Unbalanced brackets");
break;
case ':':
2022-08-23 09:58:12 +00:00
++colonDelimiters;
assertThrow(nestingLevel == 0, OptimizerException, "Cleanup sequence delimiter cannot be placed inside the brackets");
2022-09-12 08:57:02 +00:00
assertThrow(colonDelimiters <= 1, OptimizerException, "Too many cleanup sequence delimiters");
break;
default:
{
yulAssert(
string(NonStepAbbreviations).find(abbreviation) == string::npos,
"Unhandled syntactic element in the abbreviation sequence"
);
assertThrow(
stepAbbreviationToNameMap().find(abbreviation) != stepAbbreviationToNameMap().end(),
OptimizerException,
"'"s + abbreviation + "' is not a valid step abbreviation"
);
optional<string> invalid = allSteps().at(stepAbbreviationToNameMap().at(abbreviation))->invalidInCurrentEnvironment();
assertThrow(
!invalid.has_value(),
OptimizerException,
"'"s + abbreviation + "' is invalid in the current environment: " + *invalid
);
}
}
assertThrow(nestingLevel == 0, OptimizerException, "Unbalanced brackets");
}
void OptimiserSuite::runSequence(string_view _stepAbbreviations, Block& _ast, bool _repeatUntilStable)
{
validateSequence(_stepAbbreviations);
// This splits 'aaa[bbb]ccc...' into 'aaa' and '[bbb]ccc...'.
auto extractNonNestedPrefix = [](string_view _tail) -> tuple<string_view, string_view>
{
for (size_t i = 0; i < _tail.size(); ++i)
{
yulAssert(_tail[i] != ']');
if (_tail[i] == '[')
return {_tail.substr(0, i), _tail.substr(i)};
}
return {_tail, {}};
};
// This splits '[bbb]ccc...' into 'bbb' and 'ccc...'.
auto extractBracketContent = [](string_view _tail) -> tuple<string_view, string_view>
{
yulAssert(!_tail.empty() && _tail[0] == '[');
size_t contentLength = 0;
int8_t nestingLevel = 1;
for (char abbreviation: _tail.substr(1))
{
if (abbreviation == '[')
{
yulAssert(nestingLevel < numeric_limits<int8_t>::max());
++nestingLevel;
}
else if (abbreviation == ']')
{
--nestingLevel;
if (nestingLevel == 0)
break;
}
++contentLength;
}
yulAssert(nestingLevel == 0);
yulAssert(_tail[contentLength + 1] == ']');
return {_tail.substr(1, contentLength), _tail.substr(contentLength + 2)};
};
auto abbreviationsToSteps = [](string_view _sequence) -> vector<string>
{
vector<string> steps;
for (char abbreviation: _sequence)
if (abbreviation != ' ' && abbreviation != '\n')
steps.emplace_back(stepAbbreviationToNameMap().at(abbreviation));
return steps;
};
vector<tuple<string_view, bool>> subsequences;
string_view tail = _stepAbbreviations;
while (!tail.empty())
{
string_view subsequence;
tie(subsequence, tail) = extractNonNestedPrefix(tail);
if (subsequence.size() > 0)
subsequences.push_back({subsequence, false});
if (tail.empty())
break;
tie(subsequence, tail) = extractBracketContent(tail);
if (subsequence.size() > 0)
subsequences.push_back({subsequence, true});
}
size_t codeSize = 0;
for (size_t round = 0; round < MaxRounds; ++round)
{
for (auto const& [subsequence, repeat]: subsequences)
{
if (repeat)
runSequence(subsequence, _ast, true);
else
runSequence(abbreviationsToSteps(subsequence), _ast);
}
if (!_repeatUntilStable)
break;
size_t newSize = CodeSize::codeSizeIncludingFunctions(_ast);
if (newSize == codeSize)
break;
codeSize = newSize;
}
}
2019-09-23 14:32:50 +00:00
void OptimiserSuite::runSequence(std::vector<string> const& _steps, Block& _ast)
{
2019-10-07 06:16:28 +00:00
unique_ptr<Block> copy;
if (m_debug == Debug::PrintChanges)
copy = make_unique<Block>(std::get<Block>(ASTCopier{}(_ast)));
2019-09-23 14:32:50 +00:00
for (string const& step: _steps)
{
if (m_debug == Debug::PrintStep)
cout << "Running " << step << endl;
#ifdef PROFILE_OPTIMIZER_STEPS
steady_clock::time_point startTime = steady_clock::now();
#endif
2019-09-23 14:32:50 +00:00
allSteps().at(step)->run(m_context, _ast);
#ifdef PROFILE_OPTIMIZER_STEPS
steady_clock::time_point endTime = steady_clock::now();
m_durationPerStepInMicroseconds[step] += duration_cast<microseconds>(endTime - startTime).count();
#endif
2019-10-07 06:16:28 +00:00
if (m_debug == Debug::PrintChanges)
{
// TODO should add switch to also compare variable names!
if (SyntacticallyEqual{}.statementEqual(_ast, *copy))
cout << "== Running " << step << " did not cause changes." << endl;
else
{
cout << "== Running " << step << " changed the AST." << endl;
cout << AsmPrinter{}(_ast) << endl;
copy = make_unique<Block>(std::get<Block>(ASTCopier{}(_ast)));
2019-10-07 06:16:28 +00:00
}
}
2019-09-23 14:32:50 +00:00
}
}