-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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