Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[YUL] Remove unused function parameters #9397

Merged
merged 2 commits into from Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions Changelog.md
@@ -1,5 +1,12 @@
### 0.7.2 (unreleased)

Language Features:


Compiler Features:
* Yul Optimizer: Prune unused parameters in functions.

Bugfixes:


### 0.7.1 (2020-09-02)
Expand Down
2 changes: 1 addition & 1 deletion libsolidity/interface/OptimiserSettings.h
Expand Up @@ -41,7 +41,7 @@ struct OptimiserSettings

// should have good "compilability" property here.

"eul" // Run functional expression inliner
"Tpeul" // Run functional expression inliner
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added LiteralRematerialiser (T) before UnusedFunctionParamterPruner (p) because of #9653

"xarulrul" // Prune a bit more in SSA
"xarrcL" // Turn into SSA again and simplify
"gvif" // Run full inliner
Expand Down
7 changes: 7 additions & 0 deletions libsolutil/CommonData.h
Expand Up @@ -209,6 +209,13 @@ std::map<V, K> invertMap(std::map<K, V> const& originalMap)
return inverseMap;
}

/// Returns a set of keys of a map.
template <typename K, typename V>
std::set<K> keys(std::map<K, V> const& _map)
{
return applyMap(_map, [](auto const& _elem) { return _elem.first; }, std::set<K>{});
}

// String conversion functions, mainly to/from hex/nibble/byte representations.

enum class WhenError
Expand Down
3 changes: 3 additions & 0 deletions libyul/CMakeLists.txt
Expand Up @@ -156,6 +156,9 @@ add_library(yul
optimiser/SyntacticalEquality.h
optimiser/TypeInfo.cpp
optimiser/TypeInfo.h
optimiser/UnusedFunctionParameterPruner.cpp
optimiser/UnusedFunctionParameterPruner.h
optimiser/UnusedFunctionsCommon.h
optimiser/UnusedPruner.cpp
optimiser/UnusedPruner.h
optimiser/VarDeclInitializer.cpp
Expand Down
2 changes: 2 additions & 0 deletions libyul/optimiser/NameDisplacer.h
Expand Up @@ -60,6 +60,8 @@ class NameDisplacer: public ASTModifier
void operator()(FunctionCall& _funCall) override;
void operator()(Block& _block) override;

std::map<YulString, YulString> const& translations() const { return m_translations; }

protected:
/// Check if the newly introduced identifier @a _name has to be replaced.
void checkAndReplaceNew(YulString& _name);
Expand Down
3 changes: 3 additions & 0 deletions libyul/optimiser/Suite.cpp
Expand Up @@ -42,6 +42,7 @@
#include <libyul/optimiser/ForLoopInitRewriter.h>
#include <libyul/optimiser/ForLoopConditionIntoBody.h>
#include <libyul/optimiser/Rematerialiser.h>
#include <libyul/optimiser/UnusedFunctionParameterPruner.h>
#include <libyul/optimiser/UnusedPruner.h>
#include <libyul/optimiser/ExpressionSimplifier.h>
#include <libyul/optimiser/CommonSubexpressionEliminator.h>
Expand Down Expand Up @@ -185,6 +186,7 @@ map<string, unique_ptr<OptimiserStep>> const& OptimiserSuite::allSteps()
SSAReverser,
SSATransform,
StructuralSimplifier,
UnusedFunctionParameterPruner,
UnusedPruner,
VarDeclInitializer
>();
Expand Down Expand Up @@ -221,6 +223,7 @@ map<string, char> const& OptimiserSuite::stepNameToAbbreviationMap()
{SSAReverser::name, 'V'},
{SSATransform::name, 'a'},
{StructuralSimplifier::name, 't'},
{UnusedFunctionParameterPruner::name, 'p'},
{UnusedPruner::name, 'u'},
{VarDeclInitializer::name, 'd'},
};
Expand Down
127 changes: 127 additions & 0 deletions libyul/optimiser/UnusedFunctionParameterPruner.cpp
@@ -0,0 +1,127 @@
/*
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
/**
* UnusedFunctionParameterPruner: Optimiser step that removes unused parameters from function
* definition.
*/

#include <libyul/optimiser/UnusedFunctionParameterPruner.h>
#include <libyul/optimiser/UnusedFunctionsCommon.h>
#include <libyul/optimiser/OptimiserStep.h>
#include <libyul/optimiser/NameCollector.h>
#include <libyul/optimiser/NameDisplacer.h>
#include <libyul/optimiser/NameDispenser.h>
#include <libyul/YulString.h>
#include <libyul/AsmData.h>

#include <libsolutil/CommonData.h>

#include <boost/algorithm/cxx11/all_of.hpp>

#include <optional>
#include <variant>

using namespace std;
using namespace solidity::util;
using namespace solidity::yul;
using namespace solidity::yul::unusedFunctionsCommon;

void UnusedFunctionParameterPruner::run(OptimiserStepContext& _context, Block& _ast)
{
map<YulString, size_t> references = ReferencesCounter::countReferences(_ast);
auto used = [&](auto v) -> bool { return references.count(v.name); };

// Function name and a pair of boolean masks, the first corresponds to parameters and the second
// corresponds to returnVariables.
//
// For the first vector in the pair, a value `false` at index `i` indicates that the function
// argument at index `i` in `FunctionDefinition::parameters` is unused inside the function body.
//
// Similarly for the second vector in the pair, a value `false` at index `i` indicates that the
// return parameter at index `i` in `FunctionDefinition::returnVariables` is unused inside
// function body.
map<YulString, pair<vector<bool>, vector<bool>>> usedParametersAndReturnVariables;

// Step 1 of UnusedFunctionParameterPruner: Find functions whose parameters (both arguments and
// return-parameters) are not used in its body.
for (auto const& statement: _ast.statements)
if (holds_alternative<FunctionDefinition>(statement))
{
FunctionDefinition const& f = std::get<FunctionDefinition>(statement);

if (tooSimpleToBePruned(f) || boost::algorithm::all_of(f.parameters + f.returnVariables, used))
continue;

usedParametersAndReturnVariables[f.name] = {
applyMap(f.parameters, used),
applyMap(f.returnVariables, used)
};
}

set<YulString> functionNamesToFree = util::keys(usedParametersAndReturnVariables);

// Step 2 of UnusedFunctionParameterPruner: Renames the function and replaces all references to
// the function, say `f`, by its new name, say `f_1`.
NameDisplacer replace{_context.dispenser, functionNamesToFree};
replace(_ast);

// Inverse-Map of the above translations. In the above example, this will store an element with
// key `f_1` and value `f`.
std::map<YulString, YulString> newToOriginalNames = invertMap(replace.translations());

// Step 3 of UnusedFunctionParameterPruner: introduce a new function in the block with body of
// the old one. Replace the body of the old one with a function call to the new one with reduced
// parameters.
//
// For example: introduce a new 'linking' function `f` with the same the body as `f_1`, but with
// reduced parameters, i.e., `function f() -> y { y := 1 }`. Now replace the body of `f_1` with
// a call to `f`, i.e., `f_1(x) -> y { y := f() }`.
iterateReplacing(_ast.statements, [&](Statement& _s) -> optional<vector<Statement>> {
if (holds_alternative<FunctionDefinition>(_s))
{
// The original function except that it has a new name (e.g., `f_1`)
FunctionDefinition& originalFunction = get<FunctionDefinition>(_s);
if (newToOriginalNames.count(originalFunction.name))
{

YulString linkingFunctionName = originalFunction.name;
YulString originalFunctionName = newToOriginalNames.at(linkingFunctionName);
pair<vector<bool>, vector<bool>> used =
usedParametersAndReturnVariables.at(originalFunctionName);

FunctionDefinition linkingFunction = createLinkingFunction(
chriseth marked this conversation as resolved.
Show resolved Hide resolved
originalFunction,
used,
originalFunctionName,
linkingFunctionName,
_context.dispenser
);

originalFunction.name = originalFunctionName;
originalFunction.parameters =
filter(originalFunction.parameters, used.first);
originalFunction.returnVariables =
filter(originalFunction.returnVariables, used.second);

return make_vector<Statement>(move(originalFunction), move(linkingFunction));
}
}

return nullopt;
});
}
52 changes: 52 additions & 0 deletions libyul/optimiser/UnusedFunctionParameterPruner.h
@@ -0,0 +1,52 @@
/*
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

#pragma once

#include <libyul/optimiser/OptimiserStep.h>

namespace solidity::yul
{

/**
* UnusedFunctionParameterPruner: Optimiser step that removes unused parameters in a function.
*
* If a parameter is unused, like `c` and `y` in, `function f(a,b,c) -> x, y { x := div(a,b) }`
*
* We remove the parameter and create a new "linking" function as follows:
*
* `function f(a,b) -> x { x := div(a,b) }`
* `function f2(a,b,c) -> x, y { x := f(a,b) }`
*
* and replace all references to `f` by `f2`.
* The inliner should be run afterwards to make sure that all references to `f2` are replaced by
* `f`.
*
* Prerequisites: Disambiguator, FunctionHoister, LiteralRematerialiser
*
* The step LiteralRematerialiser is not required for correctness. It helps deal with cases such as:
* `function f(x) -> y { revert(y, y} }` where the literal `y` will be replaced by its value `0`,
* allowing us to rewrite the function.
*/
struct UnusedFunctionParameterPruner
{
static constexpr char const* name{"UnusedFunctionParameterPruner"};
static void run(OptimiserStepContext& _context, Block& _ast);
};

}
103 changes: 103 additions & 0 deletions libyul/optimiser/UnusedFunctionsCommon.h
@@ -0,0 +1,103 @@
/*
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
#pragma once

#include <libyul/optimiser/Metrics.h>
#include <libyul/optimiser/NameDispenser.h>
#include <libyul/AsmData.h>
#include <libyul/Dialect.h>
#include <libyul/Exceptions.h>

#include <liblangutil/SourceLocation.h>

#include <libsolutil/CommonData.h>

#include <variant>

namespace solidity::yul::unusedFunctionsCommon
{

template<typename T>
std::vector<T> filter(std::vector<T> const& _vec, std::vector<bool> const& _mask)
{
yulAssert(_vec.size() == _mask.size(), "");

std::vector<T> ret;

for (size_t i = 0; i < _mask.size(); ++i)
if (_mask[i])
ret.push_back(_vec[i]);

return ret;
}

/// Returns true if applying UnusedFunctionParameterPruner is not helpful or redundant because the
/// inliner will be able to handle it anyway.
bool tooSimpleToBePruned(FunctionDefinition const& _f)
{
return _f.body.statements.size() <= 1 && CodeSize::codeSize(_f.body) <= 1;
}

FunctionDefinition createLinkingFunction(
FunctionDefinition const& _original,
std::pair<std::vector<bool>, std::vector<bool>> const& _usedParametersAndReturns,
YulString const& _originalFunctionName,
YulString const& _linkingFunctionName,
NameDispenser& _nameDispenser
)
{
auto generateTypedName = [&](TypedName t)
{
return TypedName{
t.location,
_nameDispenser.newName(t.name),
t.type
};
};

langutil::SourceLocation loc = _original.location;

FunctionDefinition linkingFunction{
loc,
_linkingFunctionName,
util::applyMap(_original.parameters, generateTypedName),
util::applyMap(_original.returnVariables, generateTypedName),
{loc, {}} // body
};

FunctionCall call{loc, Identifier{loc, _originalFunctionName}, {}};
for (auto const& p: filter(linkingFunction.parameters, _usedParametersAndReturns.first))
call.arguments.emplace_back(Identifier{loc, p.name});

Assignment assignment{loc, {}, nullptr};

for (auto const& r: filter(linkingFunction.returnVariables, _usedParametersAndReturns.second))
assignment.variableNames.emplace_back(Identifier{loc, r.name});

if (assignment.variableNames.empty())
linkingFunction.body.statements.emplace_back(ExpressionStatement{loc, std::move(call)});
else
{
assignment.value = std::make_unique<Expression>(std::move(call));
linkingFunction.body.statements.emplace_back(std::move(assignment));
}

return linkingFunction;
}

}