mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Stack compressor.
This commit is contained in:
@@ -30,25 +30,39 @@ using namespace std;
|
||||
using namespace dev;
|
||||
using namespace yul;
|
||||
|
||||
void Rematerialiser::run(Dialect const& _dialect, Block& _ast)
|
||||
void Rematerialiser::run(Dialect const& _dialect, Block& _ast, set<YulString> _varsToAlwaysRematerialize)
|
||||
{
|
||||
Rematerialiser{_dialect, _ast}(_ast);
|
||||
Rematerialiser{_dialect, _ast, std::move(_varsToAlwaysRematerialize)}(_ast);
|
||||
}
|
||||
|
||||
void Rematerialiser::run(Dialect const& _dialect, FunctionDefinition& _function)
|
||||
void Rematerialiser::run(
|
||||
Dialect const& _dialect,
|
||||
FunctionDefinition& _function,
|
||||
set<YulString> _varsToAlwaysRematerialize
|
||||
)
|
||||
{
|
||||
Rematerialiser{_dialect, _function}(_function);
|
||||
Rematerialiser{_dialect, _function, std::move(_varsToAlwaysRematerialize)}(_function);
|
||||
}
|
||||
|
||||
Rematerialiser::Rematerialiser(Dialect const& _dialect, Block& _ast):
|
||||
Rematerialiser::Rematerialiser(
|
||||
Dialect const& _dialect,
|
||||
Block& _ast,
|
||||
set<YulString> _varsToAlwaysRematerialize
|
||||
):
|
||||
DataFlowAnalyzer(_dialect),
|
||||
m_referenceCounts(ReferencesCounter::countReferences(_ast))
|
||||
m_referenceCounts(ReferencesCounter::countReferences(_ast)),
|
||||
m_varsToAlwaysRematerialize(std::move(_varsToAlwaysRematerialize))
|
||||
{
|
||||
}
|
||||
|
||||
Rematerialiser::Rematerialiser(Dialect const& _dialect, FunctionDefinition& _function):
|
||||
Rematerialiser::Rematerialiser(
|
||||
Dialect const& _dialect,
|
||||
FunctionDefinition& _function,
|
||||
set<YulString> _varsToAlwaysRematerialize
|
||||
):
|
||||
DataFlowAnalyzer(_dialect),
|
||||
m_referenceCounts(ReferencesCounter::countReferences(_function))
|
||||
m_referenceCounts(ReferencesCounter::countReferences(_function)),
|
||||
m_varsToAlwaysRematerialize(std::move(_varsToAlwaysRematerialize))
|
||||
{
|
||||
}
|
||||
|
||||
@@ -64,7 +78,7 @@ void Rematerialiser::visit(Expression& _e)
|
||||
auto const& value = *m_value.at(name);
|
||||
size_t refs = m_referenceCounts[name];
|
||||
size_t cost = CodeCost::codeCost(value);
|
||||
if (refs <= 1 || cost == 0 || (refs <= 5 && cost <= 1))
|
||||
if (refs <= 1 || cost == 0 || (refs <= 5 && cost <= 1) || m_varsToAlwaysRematerialize.count(name))
|
||||
{
|
||||
assertThrow(m_referenceCounts[name] > 0, OptimizerException, "");
|
||||
for (auto const& ref: m_references[name])
|
||||
|
||||
@@ -38,17 +38,34 @@ namespace yul
|
||||
class Rematerialiser: public DataFlowAnalyzer
|
||||
{
|
||||
public:
|
||||
static void run(Dialect const& _dialect, Block& _ast);
|
||||
static void run(Dialect const& _dialect, FunctionDefinition& _function);
|
||||
static void run(
|
||||
Dialect const& _dialect,
|
||||
Block& _ast,
|
||||
std::set<YulString> _varsToAlwaysRematerialize = {}
|
||||
);
|
||||
static void run(
|
||||
Dialect const& _dialect,
|
||||
FunctionDefinition& _function,
|
||||
std::set<YulString> _varsToAlwaysRematerialize = {}
|
||||
);
|
||||
|
||||
protected:
|
||||
Rematerialiser(Dialect const& _dialect, Block& _ast);
|
||||
Rematerialiser(Dialect const& _dialect, FunctionDefinition& _function);
|
||||
Rematerialiser(
|
||||
Dialect const& _dialect,
|
||||
Block& _ast,
|
||||
std::set<YulString> _varsToAlwaysRematerialize = {}
|
||||
);
|
||||
Rematerialiser(
|
||||
Dialect const& _dialect,
|
||||
FunctionDefinition& _function,
|
||||
std::set<YulString> _varsToAlwaysRematerialize = {}
|
||||
);
|
||||
|
||||
using ASTModifier::visit;
|
||||
void visit(Expression& _e) override;
|
||||
|
||||
std::map<YulString, size_t> m_referenceCounts;
|
||||
std::set<YulString> m_varsToAlwaysRematerialize;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
/*(
|
||||
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/>.
|
||||
*/
|
||||
/**
|
||||
* Optimisation stage that aggressively rematerializes certain variables ina a function to free
|
||||
* space on the stack until it is compilable.
|
||||
*/
|
||||
|
||||
#include <libyul/optimiser/StackCompressor.h>
|
||||
|
||||
#include <libyul/optimiser/SSAValueTracker.h>
|
||||
#include <libyul/optimiser/NameCollector.h>
|
||||
#include <libyul/optimiser/Rematerialiser.h>
|
||||
#include <libyul/optimiser/UnusedPruner.h>
|
||||
#include <libyul/optimiser/Metrics.h>
|
||||
#include <libyul/optimiser/Semantics.h>
|
||||
|
||||
#include <libyul/CompilabilityChecker.h>
|
||||
|
||||
#include <libyul/AsmData.h>
|
||||
|
||||
using namespace std;
|
||||
using namespace dev;
|
||||
using namespace yul;
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
template <typename ASTNode>
|
||||
void eliminateVariables(shared_ptr<Dialect> const& _dialect, ASTNode& _node, size_t _numVariables)
|
||||
{
|
||||
SSAValueTracker ssaValues;
|
||||
ssaValues(_node);
|
||||
|
||||
map<YulString, size_t> references = ReferencesCounter::countReferences(_node);
|
||||
|
||||
set<pair<size_t, YulString>> rematCosts;
|
||||
for (auto const& ssa: ssaValues.values())
|
||||
{
|
||||
if (!MovableChecker{*_dialect, *ssa.second}.movable())
|
||||
continue;
|
||||
size_t numRef = references[ssa.first];
|
||||
size_t cost = 0;
|
||||
if (numRef > 1)
|
||||
cost = CodeCost::codeCost(*ssa.second) * (numRef - 1);
|
||||
rematCosts.insert(make_pair(cost, ssa.first));
|
||||
}
|
||||
|
||||
// Select at most _numVariables
|
||||
set<YulString> varsToEliminate;
|
||||
for (auto const& costs: rematCosts)
|
||||
{
|
||||
if (varsToEliminate.size() >= _numVariables)
|
||||
break;
|
||||
varsToEliminate.insert(costs.second);
|
||||
}
|
||||
|
||||
Rematerialiser::run(*_dialect, _node, std::move(varsToEliminate));
|
||||
UnusedPruner::runUntilStabilised(*_dialect, _node);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool StackCompressor::run(shared_ptr<Dialect> const& _dialect, Block& _ast)
|
||||
{
|
||||
yulAssert(
|
||||
_ast.statements.size() > 0 && _ast.statements.at(0).type() == typeid(Block),
|
||||
"Need to run the function grouper before the stack compressor."
|
||||
);
|
||||
for (size_t iterations = 0; iterations < 4; iterations++)
|
||||
{
|
||||
map<YulString, int> stackSurplus = CompilabilityChecker::run(_dialect, _ast);
|
||||
if (stackSurplus.empty())
|
||||
return true;
|
||||
|
||||
if (stackSurplus.count(YulString{}))
|
||||
{
|
||||
yulAssert(stackSurplus.at({}) > 0, "Invalid surplus value.");
|
||||
eliminateVariables(_dialect, boost::get<Block>(_ast.statements.at(0)), stackSurplus.at({}));
|
||||
}
|
||||
|
||||
for (size_t i = 1; i < _ast.statements.size(); ++i)
|
||||
{
|
||||
FunctionDefinition& fun = boost::get<FunctionDefinition>(_ast.statements[i]);
|
||||
if (!stackSurplus.count(fun.name))
|
||||
continue;
|
||||
|
||||
yulAssert(stackSurplus.at(fun.name) > 0, "Invalid surplus value.");
|
||||
eliminateVariables(_dialect, fun, stackSurplus.at(fun.name));
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
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/>.
|
||||
*/
|
||||
/**
|
||||
* Optimisation stage that aggressively rematerializes certain variables ina a function to free
|
||||
* space on the stack until it is compilable.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace yul
|
||||
{
|
||||
|
||||
struct Dialect;
|
||||
struct Block;
|
||||
struct FunctionDefinition;
|
||||
|
||||
/**
|
||||
* Optimisation stage that aggressively rematerializes certain variables in a function to free
|
||||
* space on the stack until it is compilable.
|
||||
*
|
||||
* Prerequisite: Disambiguator, Function Grouper
|
||||
*/
|
||||
class StackCompressor
|
||||
{
|
||||
public:
|
||||
/// Try to remove local variables until the AST is compilable.
|
||||
/// @returns true if it was successful.
|
||||
static bool run(std::shared_ptr<Dialect> const& _dialect, Block& _ast);
|
||||
};
|
||||
|
||||
}
|
||||
@@ -37,6 +37,7 @@
|
||||
#include <libyul/optimiser/CommonSubexpressionEliminator.h>
|
||||
#include <libyul/optimiser/SSAReverser.h>
|
||||
#include <libyul/optimiser/SSATransform.h>
|
||||
#include <libyul/optimiser/StackCompressor.h>
|
||||
#include <libyul/optimiser/StructuralSimplifier.h>
|
||||
#include <libyul/optimiser/RedundantAssignEliminator.h>
|
||||
#include <libyul/AsmAnalysisInfo.h>
|
||||
@@ -170,5 +171,9 @@ void OptimiserSuite::run(
|
||||
Rematerialiser::run(*_dialect, ast);
|
||||
UnusedPruner::runUntilStabilised(*_dialect, ast, reservedIdentifiers);
|
||||
|
||||
(FunctionGrouper{})(ast);
|
||||
StackCompressor::run(_dialect, ast);
|
||||
(BlockFlattener{})(ast);
|
||||
|
||||
_ast = std::move(ast);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user