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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// 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>
|
2019-08-13 16:38:11 +00:00
|
|
|
#include <libyul/optimiser/CallGraphGenerator.h>
|
2020-02-07 00:28:24 +00:00
|
|
|
#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>
|
2019-01-10 19:29:30 +00:00
|
|
|
#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>
|
2019-09-11 14:30:47 +00:00
|
|
|
#include <libyul/optimiser/ForLoopConditionIntoBody.h>
|
2019-09-11 16:55:31 +00:00
|
|
|
#include <libyul/optimiser/ForLoopConditionOutOfBody.h>
|
2018-11-07 10:18:02 +00:00
|
|
|
#include <libyul/optimiser/ForLoopInitRewriter.h>
|
2019-09-11 09:42:59 +00:00
|
|
|
#include <libyul/optimiser/ForLoopConditionIntoBody.h>
|
2020-11-26 17:51:32 +00:00
|
|
|
#include <libyul/optimiser/FunctionSpecializer.h>
|
2018-10-16 19:39:22 +00:00
|
|
|
#include <libyul/optimiser/Rematerialiser.h>
|
2020-07-10 16:56:32 +00:00
|
|
|
#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>
|
2019-08-13 16:38:11 +00:00
|
|
|
#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>
|
2018-12-03 16:19:37 +00:00
|
|
|
#include <libyul/optimiser/StructuralSimplifier.h>
|
2019-10-07 06:16:28 +00:00
|
|
|
#include <libyul/optimiser/SyntacticalEquality.h>
|
2021-11-23 09:37:12 +00:00
|
|
|
#include <libyul/optimiser/UnusedAssignEliminator.h>
|
2021-05-05 16:02:35 +00:00
|
|
|
#include <libyul/optimiser/UnusedStoreEliminator.h>
|
2019-01-21 07:13:31 +00:00
|
|
|
#include <libyul/optimiser/VarNameCleaner.h>
|
2019-08-29 14:43:06 +00:00
|
|
|
#include <libyul/optimiser/LoadResolver.h>
|
2019-09-18 08:47:49 +00:00
|
|
|
#include <libyul/optimiser/LoopInvariantCodeMotion.h>
|
2019-03-04 17:50:55 +00:00
|
|
|
#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>
|
2019-01-21 07:13:31 +00:00
|
|
|
#include <libyul/AsmAnalysis.h>
|
2018-11-23 10:18:57 +00:00
|
|
|
#include <libyul/AsmAnalysisInfo.h>
|
|
|
|
#include <libyul/AsmPrinter.h>
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/AST.h>
|
2019-07-09 15:23:14 +00:00
|
|
|
#include <libyul/Object.h>
|
2018-10-16 19:39:22 +00:00
|
|
|
|
2019-01-21 07:13:31 +00:00
|
|
|
#include <libyul/backends/evm/NoOutputAssembly.h>
|
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
2018-10-16 19:39:22 +00:00
|
|
|
|
2020-07-02 10:48:20 +00:00
|
|
|
#include <libyul/CompilabilityChecker.h>
|
2020-02-29 23:28:12 +00:00
|
|
|
|
2021-03-31 18:03:04 +00:00
|
|
|
#include <range/v3/view/map.hpp>
|
2021-04-24 02:40:41 +00:00
|
|
|
#include <range/v3/action/remove.hpp>
|
2021-03-31 18:03:04 +00:00
|
|
|
|
2021-08-21 16:03:35 +00:00
|
|
|
#include <limits>
|
2021-10-06 14:00:16 +00:00
|
|
|
#include <tuple>
|
2021-08-21 16:03:35 +00:00
|
|
|
|
2022-11-10 15:43:58 +00:00
|
|
|
#ifdef PROFILE_OPTIMIZER_STEPS
|
|
|
|
#include <chrono>
|
|
|
|
#include <fmt/format.h>
|
|
|
|
#endif
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::yul;
|
2022-11-10 15:43:58 +00:00
|
|
|
#ifdef PROFILE_OPTIMIZER_STEPS
|
|
|
|
using namespace std::chrono;
|
|
|
|
#endif
|
2023-08-23 02:03:44 +00:00
|
|
|
using namespace std::string_literals;
|
2022-11-10 15:43:58 +00:00
|
|
|
|
|
|
|
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(
|
2019-05-16 08:56:56 +00:00
|
|
|
Dialect const& _dialect,
|
2019-05-29 21:14:06 +00:00
|
|
|
GasMeter const* _meter,
|
2019-07-09 15:23:14 +00:00
|
|
|
Object& _object,
|
2019-03-13 16:44:16 +00:00
|
|
|
bool _optimizeStackAllocation,
|
2023-08-23 02:03:44 +00:00
|
|
|
std::string_view _optimisationSequence,
|
|
|
|
std::string_view _optimisationCleanupSequence,
|
|
|
|
std::optional<size_t> _expectedExecutionsPerDeployment,
|
|
|
|
std::set<YulString> const& _externallyUsedIdentifiers
|
2018-10-16 19:39:22 +00:00
|
|
|
)
|
|
|
|
{
|
2021-08-12 15:17:21 +00:00
|
|
|
EVMDialect const* evmDialect = dynamic_cast<EVMDialect const*>(&_dialect);
|
|
|
|
bool usesOptimizedCodeGenerator =
|
|
|
|
_optimizeStackAllocation &&
|
|
|
|
evmDialect &&
|
|
|
|
evmDialect->evmVersion().canOverchargeGasForCall() &&
|
|
|
|
evmDialect->providesObjectAccess();
|
2023-08-23 02:03:44 +00:00
|
|
|
std::set<YulString> reservedIdentifiers = _externallyUsedIdentifiers;
|
2019-05-29 21:14:06 +00:00
|
|
|
reservedIdentifiers += _dialect.fixedFunctionNames();
|
2018-10-16 19:39:22 +00:00
|
|
|
|
2019-11-19 15:42:49 +00:00
|
|
|
*_object.code = std::get<Block>(Disambiguator(
|
2019-07-09 15:23:14 +00:00
|
|
|
_dialect,
|
|
|
|
*_object.analysisInfo,
|
|
|
|
reservedIdentifiers
|
|
|
|
)(*_object.code));
|
|
|
|
Block& ast = *_object.code;
|
2018-10-16 19:39:22 +00:00
|
|
|
|
2021-11-03 11:12:37 +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
|
|
|
|
2020-10-06 20:27:20 +00:00
|
|
|
// Some steps depend on properties ensured by FunctionHoister, BlockFlattener, FunctionGrouper and
|
2020-04-24 12:19:36 +00:00
|
|
|
// ForLoopInitRewriter. Run them first to be able to run arbitrary sequences safely.
|
2021-11-09 12:25:00 +00:00
|
|
|
suite.runSequence("hgfo", ast);
|
2020-04-24 12:19:36 +00:00
|
|
|
|
2020-11-30 11:50:02 +00:00
|
|
|
NameSimplifier::run(suite.m_context, ast);
|
2020-04-24 12:19:36 +00:00
|
|
|
// Now the user-supplied part
|
|
|
|
suite.runSequence(_optimisationSequence, ast);
|
2018-10-16 19:39:22 +00:00
|
|
|
|
2019-03-14 17:25:33 +00:00
|
|
|
// This is a tuning parameter, but actually just prevents infinite loops.
|
|
|
|
size_t stackCompressorMaxIterations = 16;
|
2020-03-01 00:02:45 +00:00
|
|
|
suite.runSequence("g", ast);
|
|
|
|
|
2019-03-14 17:25:33 +00:00
|
|
|
// We ignore the return value because we will get a much better error
|
|
|
|
// message once we perform code generation.
|
2021-08-12 15:17:21 +00:00
|
|
|
if (!usesOptimizedCodeGenerator)
|
|
|
|
StackCompressor::run(
|
|
|
|
_dialect,
|
|
|
|
_object,
|
|
|
|
_optimizeStackAllocation,
|
|
|
|
stackCompressorMaxIterations
|
|
|
|
);
|
2022-08-10 13:57:01 +00:00
|
|
|
|
2022-08-23 09:58:12 +00:00
|
|
|
// Run the user-supplied clean up sequence
|
2022-08-10 13:57:01 +00:00
|
|
|
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.
|
2022-08-10 13:57:01 +00:00
|
|
|
suite.runSequence("g", ast);
|
2019-05-15 08:33:35 +00:00
|
|
|
|
2021-08-12 15:17:21 +00:00
|
|
|
if (evmDialect)
|
2019-05-29 21:14:06 +00:00
|
|
|
{
|
|
|
|
yulAssert(_meter, "");
|
2021-08-12 15:17:21 +00:00
|
|
|
ConstantOptimiser{*evmDialect, *_meter}(ast);
|
|
|
|
if (usesOptimizedCodeGenerator)
|
|
|
|
{
|
|
|
|
StackCompressor::run(
|
2020-07-02 10:48:20 +00:00
|
|
|
_dialect,
|
|
|
|
_object,
|
2021-08-12 15:17:21 +00:00
|
|
|
_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
|
|
|
}
|
2020-11-30 11:50:02 +00:00
|
|
|
|
2021-11-03 11:12:37 +00:00
|
|
|
dispenser.reset(ast);
|
2020-11-30 11:50:02 +00:00
|
|
|
NameSimplifier::run(suite.m_context, ast);
|
2020-01-29 17:05:18 +00:00
|
|
|
VarNameCleaner::run(suite.m_context, ast);
|
2019-02-04 16:30:29 +00:00
|
|
|
|
2022-11-10 15:43:58 +00:00
|
|
|
#ifdef PROFILE_OPTIMIZER_STEPS
|
|
|
|
outputPerformanceMetrics(suite.m_durationPerStepInMicroseconds);
|
|
|
|
#endif
|
|
|
|
|
2019-07-09 15:23:14 +00:00
|
|
|
*_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>
|
2023-08-23 02:03:44 +00:00
|
|
|
std::map<std::string, std::unique_ptr<OptimiserStep>> optimiserStepCollection()
|
2019-09-23 14:32:50 +00:00
|
|
|
{
|
2023-08-23 02:03:44 +00:00
|
|
|
std::map<std::string, std::unique_ptr<OptimiserStep>> ret;
|
|
|
|
for (std::unique_ptr<OptimiserStep>& s: util::make_vector<std::unique_ptr<OptimiserStep>>(
|
|
|
|
(std::make_unique<OptimiserStepInstance<Step>>())...
|
2019-09-23 14:32:50 +00:00
|
|
|
))
|
|
|
|
{
|
|
|
|
yulAssert(!ret.count(s->name), "");
|
|
|
|
ret[s->name] = std::move(s);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
std::map<std::string, std::unique_ptr<OptimiserStep>> const& OptimiserSuite::allSteps()
|
2019-09-23 14:32:50 +00:00
|
|
|
{
|
2023-08-23 02:03:44 +00:00
|
|
|
static std::map<std::string, std::unique_ptr<OptimiserStep>> instance;
|
2019-09-23 14:32:50 +00:00
|
|
|
if (instance.empty())
|
|
|
|
instance = optimiserStepCollection<
|
|
|
|
BlockFlattener,
|
2020-02-07 00:28:24 +00:00
|
|
|
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,
|
2020-11-26 17:51:32 +00:00
|
|
|
FunctionSpecializer,
|
2019-09-23 14:32:50 +00:00
|
|
|
LiteralRematerialiser,
|
|
|
|
LoadResolver,
|
2019-09-18 08:47:49 +00:00
|
|
|
LoopInvariantCodeMotion,
|
2021-11-23 09:37:12 +00:00
|
|
|
UnusedAssignEliminator,
|
2021-05-05 16:02:35 +00:00
|
|
|
UnusedStoreEliminator,
|
2019-09-23 14:32:50 +00:00
|
|
|
Rematerialiser,
|
|
|
|
SSAReverser,
|
|
|
|
SSATransform,
|
|
|
|
StructuralSimplifier,
|
2020-07-10 16:56:32 +00:00
|
|
|
UnusedFunctionParameterPruner,
|
2019-09-23 14:32:50 +00:00
|
|
|
UnusedPruner,
|
2020-01-29 17:05:18 +00:00
|
|
|
VarDeclInitializer
|
2019-09-23 14:32:50 +00:00
|
|
|
>();
|
2020-01-29 17:05:18 +00:00
|
|
|
// Does not include VarNameCleaner because it destroys the property of unique names.
|
2020-11-30 11:50:02 +00:00
|
|
|
// Does not include NameSimplifier.
|
2019-09-23 14:32:50 +00:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
std::map<std::string, char> const& OptimiserSuite::stepNameToAbbreviationMap()
|
2020-01-24 21:03:44 +00:00
|
|
|
{
|
2023-08-23 02:03:44 +00:00
|
|
|
static std::map<std::string, char> lookupTable{
|
2020-01-24 21:03:44 +00:00
|
|
|
{BlockFlattener::name, 'f'},
|
2020-02-07 00:28:24 +00:00
|
|
|
{CircularReferencesPruner::name, 'l'},
|
2020-01-24 21:03:44 +00:00
|
|
|
{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'},
|
2020-01-24 21:03:44 +00:00
|
|
|
{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'},
|
2020-11-26 17:51:32 +00:00
|
|
|
{FunctionSpecializer::name, 'F'},
|
2020-01-24 21:03:44 +00:00
|
|
|
{LiteralRematerialiser::name, 'T'},
|
|
|
|
{LoadResolver::name, 'L'},
|
|
|
|
{LoopInvariantCodeMotion::name, 'M'},
|
2021-11-23 09:37:12 +00:00
|
|
|
{UnusedAssignEliminator::name, 'r'},
|
2021-05-05 16:02:35 +00:00
|
|
|
{UnusedStoreEliminator::name, 'S'},
|
2020-01-24 21:03:44 +00:00
|
|
|
{Rematerialiser::name, 'm'},
|
|
|
|
{SSAReverser::name, 'V'},
|
|
|
|
{SSATransform::name, 'a'},
|
|
|
|
{StructuralSimplifier::name, 't'},
|
2020-07-10 16:56:32 +00:00
|
|
|
{UnusedFunctionParameterPruner::name, 'p'},
|
2020-01-24 21:03:44 +00:00
|
|
|
{UnusedPruner::name, 'u'},
|
|
|
|
{VarDeclInitializer::name, 'd'},
|
|
|
|
};
|
|
|
|
yulAssert(lookupTable.size() == allSteps().size(), "");
|
2020-04-24 12:13:34 +00:00
|
|
|
yulAssert((
|
2023-08-23 02:03:44 +00:00
|
|
|
util::convertContainer<std::set<char>>(std::string(NonStepAbbreviations)) -
|
|
|
|
util::convertContainer<std::set<char>>(lookupTable | ranges::views::values)
|
|
|
|
).size() == std::string(NonStepAbbreviations).size(),
|
2020-04-24 12:13:34 +00:00
|
|
|
"Step abbreviation conflicts with a character reserved for another syntactic element"
|
|
|
|
);
|
2020-01-24 21:03:44 +00:00
|
|
|
|
|
|
|
return lookupTable;
|
|
|
|
}
|
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
std::map<char, std::string> const& OptimiserSuite::stepAbbreviationToNameMap()
|
2020-01-24 21:03:44 +00:00
|
|
|
{
|
2023-08-23 02:03:44 +00:00
|
|
|
static std::map<char, std::string> lookupTable = util::invertMap(stepNameToAbbreviationMap());
|
2020-01-24 21:03:44 +00:00
|
|
|
|
|
|
|
return lookupTable;
|
|
|
|
}
|
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
void OptimiserSuite::validateSequence(std::string_view _stepAbbreviations)
|
2020-02-29 23:28:12 +00:00
|
|
|
{
|
2021-08-21 16:03:35 +00:00
|
|
|
int8_t nestingLevel = 0;
|
2022-08-10 13:57:01 +00:00
|
|
|
int8_t colonDelimiters = 0;
|
2020-04-24 12:15:45 +00:00
|
|
|
for (char abbreviation: _stepAbbreviations)
|
2020-02-29 23:28:12 +00:00
|
|
|
switch (abbreviation)
|
|
|
|
{
|
2020-04-24 12:15:45 +00:00
|
|
|
case ' ':
|
|
|
|
case '\n':
|
|
|
|
break;
|
2020-04-16 15:13:56 +00:00
|
|
|
case '[':
|
2023-08-23 02:03:44 +00:00
|
|
|
assertThrow(nestingLevel < std::numeric_limits<int8_t>::max(), OptimizerException, "Brackets nested too deep");
|
2021-08-21 16:03:35 +00:00
|
|
|
nestingLevel++;
|
2020-02-29 23:28:12 +00:00
|
|
|
break;
|
2020-04-16 15:13:56 +00:00
|
|
|
case ']':
|
2021-08-21 16:03:35 +00:00
|
|
|
nestingLevel--;
|
|
|
|
assertThrow(nestingLevel >= 0, OptimizerException, "Unbalanced brackets");
|
2020-02-29 23:28:12 +00:00
|
|
|
break;
|
2022-08-10 13:57:01 +00:00
|
|
|
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");
|
2022-08-10 13:57:01 +00:00
|
|
|
break;
|
2020-02-29 23:28:12 +00:00
|
|
|
default:
|
2020-09-11 11:39:11 +00:00
|
|
|
{
|
2020-04-24 12:13:34 +00:00
|
|
|
yulAssert(
|
2023-08-23 02:03:44 +00:00
|
|
|
std::string(NonStepAbbreviations).find(abbreviation) == std::string::npos,
|
2020-04-24 12:13:34 +00:00
|
|
|
"Unhandled syntactic element in the abbreviation sequence"
|
|
|
|
);
|
2020-02-29 23:28:12 +00:00
|
|
|
assertThrow(
|
|
|
|
stepAbbreviationToNameMap().find(abbreviation) != stepAbbreviationToNameMap().end(),
|
|
|
|
OptimizerException,
|
2020-04-24 12:15:45 +00:00
|
|
|
"'"s + abbreviation + "' is not a valid step abbreviation"
|
2020-02-29 23:28:12 +00:00
|
|
|
);
|
2023-08-23 02:03:44 +00:00
|
|
|
std::optional<std::string> invalid = allSteps().at(stepAbbreviationToNameMap().at(abbreviation))->invalidInCurrentEnvironment();
|
2020-09-11 11:39:11 +00:00
|
|
|
assertThrow(
|
|
|
|
!invalid.has_value(),
|
|
|
|
OptimizerException,
|
|
|
|
"'"s + abbreviation + "' is invalid in the current environment: " + *invalid
|
|
|
|
);
|
|
|
|
}
|
2020-02-29 23:28:12 +00:00
|
|
|
}
|
2021-08-21 16:03:35 +00:00
|
|
|
assertThrow(nestingLevel == 0, OptimizerException, "Unbalanced brackets");
|
2020-04-24 12:15:45 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
void OptimiserSuite::runSequence(std::string_view _stepAbbreviations, Block& _ast, bool _repeatUntilStable)
|
2020-04-24 12:15:45 +00:00
|
|
|
{
|
|
|
|
validateSequence(_stepAbbreviations);
|
|
|
|
|
2021-10-06 14:00:16 +00:00
|
|
|
// This splits 'aaa[bbb]ccc...' into 'aaa' and '[bbb]ccc...'.
|
2023-08-23 02:03:44 +00:00
|
|
|
auto extractNonNestedPrefix = [](std::string_view _tail) -> std::tuple<std::string_view, std::string_view>
|
2021-10-06 14:00:16 +00:00
|
|
|
{
|
|
|
|
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...'.
|
2023-08-23 02:03:44 +00:00
|
|
|
auto extractBracketContent = [](std::string_view _tail) -> std::tuple<std::string_view, std::string_view>
|
2021-10-06 14:00:16 +00:00
|
|
|
{
|
|
|
|
yulAssert(!_tail.empty() && _tail[0] == '[');
|
|
|
|
|
|
|
|
size_t contentLength = 0;
|
|
|
|
int8_t nestingLevel = 1;
|
|
|
|
for (char abbreviation: _tail.substr(1))
|
|
|
|
{
|
|
|
|
if (abbreviation == '[')
|
|
|
|
{
|
2023-08-23 02:03:44 +00:00
|
|
|
yulAssert(nestingLevel < std::numeric_limits<int8_t>::max());
|
2021-10-06 14:00:16 +00:00
|
|
|
++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)};
|
|
|
|
};
|
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
auto abbreviationsToSteps = [](std::string_view _sequence) -> std::vector<std::string>
|
2020-02-29 23:28:12 +00:00
|
|
|
{
|
2023-08-23 02:03:44 +00:00
|
|
|
std::vector<std::string> steps;
|
2020-02-29 23:28:12 +00:00
|
|
|
for (char abbreviation: _sequence)
|
2021-10-06 14:44:29 +00:00
|
|
|
if (abbreviation != ' ' && abbreviation != '\n')
|
|
|
|
steps.emplace_back(stepAbbreviationToNameMap().at(abbreviation));
|
2020-02-29 23:28:12 +00:00
|
|
|
return steps;
|
|
|
|
};
|
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
std::vector<std::tuple<std::string_view, bool>> subsequences;
|
|
|
|
std::string_view tail = _stepAbbreviations;
|
2021-10-06 14:00:16 +00:00
|
|
|
while (!tail.empty())
|
|
|
|
{
|
2023-08-23 02:03:44 +00:00
|
|
|
std::string_view subsequence;
|
2021-10-06 14:00:16 +00:00
|
|
|
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)
|
2020-02-29 23:28:12 +00:00
|
|
|
{
|
2021-10-06 14:00:16 +00:00
|
|
|
for (auto const& [subsequence, repeat]: subsequences)
|
|
|
|
{
|
|
|
|
if (repeat)
|
|
|
|
runSequence(subsequence, _ast, true);
|
|
|
|
else
|
|
|
|
runSequence(abbreviationsToSteps(subsequence), _ast);
|
|
|
|
}
|
2020-02-29 23:28:12 +00:00
|
|
|
|
2021-10-06 14:00:16 +00:00
|
|
|
if (!_repeatUntilStable)
|
|
|
|
break;
|
2020-02-29 23:28:12 +00:00
|
|
|
|
2021-10-06 14:00:16 +00:00
|
|
|
size_t newSize = CodeSize::codeSizeIncludingFunctions(_ast);
|
|
|
|
if (newSize == codeSize)
|
|
|
|
break;
|
|
|
|
codeSize = newSize;
|
2020-02-29 23:28:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
void OptimiserSuite::runSequence(std::vector<std::string> const& _steps, Block& _ast)
|
2019-09-23 14:32:50 +00:00
|
|
|
{
|
2023-08-23 02:03:44 +00:00
|
|
|
std::unique_ptr<Block> copy;
|
2019-10-07 06:16:28 +00:00
|
|
|
if (m_debug == Debug::PrintChanges)
|
2023-08-23 02:03:44 +00:00
|
|
|
copy = std::make_unique<Block>(std::get<Block>(ASTCopier{}(_ast)));
|
|
|
|
for (std::string const& step: _steps)
|
2019-09-23 14:32:50 +00:00
|
|
|
{
|
|
|
|
if (m_debug == Debug::PrintStep)
|
2023-08-23 02:03:44 +00:00
|
|
|
std::cout << "Running " << step << std::endl;
|
2022-11-10 15:43:58 +00:00
|
|
|
#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);
|
2022-11-10 15:43:58 +00:00
|
|
|
#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))
|
2023-08-23 02:03:44 +00:00
|
|
|
std::cout << "== Running " << step << " did not cause changes." << std::endl;
|
2019-10-07 06:16:28 +00:00
|
|
|
else
|
|
|
|
{
|
2023-08-23 02:03:44 +00:00
|
|
|
std::cout << "== Running " << step << " changed the AST." << std::endl;
|
|
|
|
std::cout << AsmPrinter{}(_ast) << std::endl;
|
|
|
|
copy = std::make_unique<Block>(std::get<Block>(ASTCopier{}(_ast)));
|
2019-10-07 06:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-23 14:32:50 +00:00
|
|
|
}
|
|
|
|
}
|