-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[mlir][func] Add eliminate-function-parameter pass #160654
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
Closed
linuxlonelyeagle
wants to merge
1
commit into
llvm:main
from
linuxlonelyeagle:eliminate-function-parameter
Closed
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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
88 changes: 88 additions & 0 deletions
88
mlir/lib/Dialect/Func/Transforms/EliminateFunctionParameter.cpp
This file contains hidden or 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,88 @@ | ||
//===- EliminateFunctionParameter.cpp.cpp - Eliminate function Parameter --===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "mlir/Dialect/Func/IR/FuncOps.h" | ||
#include "mlir/Dialect/Func/Transforms/Passes.h" | ||
|
||
namespace mlir { | ||
namespace func { | ||
#define GEN_PASS_DEF_ELIMINATEFUNCTIONPARAMETERPASS | ||
#include "mlir/Dialect/Func/Transforms/Passes.h.inc" | ||
} // namespace func | ||
|
||
/// This function eliminates unnecessary parameters within the function. | ||
static LogicalResult updateFunc(func::FuncOp funcOp, BitVector &arguemntNoUse) { | ||
Block &entryBlock = funcOp.front(); | ||
bool change = false; | ||
FunctionType origType = funcOp.getFunctionType(); | ||
llvm::ArrayRef<Type> origInputTypes = origType.getInputs(); | ||
SmallVector<Type, 4> newInputTypes; | ||
for (auto iter : llvm::enumerate(funcOp.getArguments())) { | ||
size_t position = iter.index(); | ||
if (!iter.value().use_empty()) { | ||
newInputTypes.push_back(origInputTypes[position]); | ||
continue; | ||
} | ||
arguemntNoUse.set(position); | ||
entryBlock.eraseArgument(position); | ||
change = true; | ||
} | ||
|
||
if (change) { | ||
auto newFunctionType = FunctionType::get(funcOp.getContext(), newInputTypes, | ||
origType.getResults()); | ||
funcOp.setFunctionType(newFunctionType); | ||
} | ||
return success(change); | ||
} | ||
|
||
/// After eliminating redundant parameters from the function, update the | ||
/// function calls. | ||
static LogicalResult updateCall(func::CallOp callOp, | ||
BitVector &argumentsNoUse) { | ||
ValueRange origOperands = callOp.getOperands(); | ||
SmallVector<Value, 4> newOperands; | ||
for (auto iter : llvm::enumerate(origOperands)) { | ||
if (!argumentsNoUse[iter.index()]) | ||
newOperands.push_back(iter.value()); | ||
} | ||
callOp->setOperands(newOperands); | ||
return success(); | ||
} | ||
|
||
namespace { | ||
struct EliminateFunctionParameterPass | ||
: public func::impl::EliminateFunctionParameterPassBase< | ||
EliminateFunctionParameterPass> { | ||
using EliminateFunctionParameterPassBase< | ||
EliminateFunctionParameterPass>::EliminateFunctionParameterPassBase; | ||
void runOnOperation() override { | ||
ModuleOp moduleOp = getOperation(); | ||
for (auto funcOp : moduleOp.getOps<func::FuncOp>()) { | ||
size_t argumentSize = funcOp.getArguments().size(); | ||
if (!argumentSize) | ||
continue; | ||
BitVector argumentNoUse(argumentSize); | ||
if (failed(updateFunc(funcOp, argumentNoUse))) | ||
continue; | ||
|
||
auto symbolOp = mlir::cast<SymbolOpInterface>(funcOp.getOperation()); | ||
auto users = symbolOp.getSymbolUses(moduleOp); | ||
|
||
if (!users.has_value()) | ||
continue; | ||
for (SymbolTable::SymbolUse user : *users) { | ||
Operation *call = user.getUser(); | ||
(void)updateCall(mlir::cast<func::CallOp>(call), argumentNoUse); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
} // namespace | ||
} // namespace mlir |
This file contains hidden or 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,32 @@ | ||
// RUN: mlir-opt %s --split-input-file --eliminate-function-parameter | \ | ||
// RUN: FileCheck %s | ||
|
||
func.func @single_parameter(%arg: index) { | ||
return | ||
} | ||
|
||
func.func @mutl_parameter(%arg0 : index, %arg1 : index) -> index { | ||
return %arg0 : index | ||
} | ||
|
||
func.func @eliminate_parameter(%arg0: index, %arg1: index) -> index { | ||
func.call @single_parameter(%arg0) : (index) -> () | ||
%ret = func.call @mutl_parameter(%arg0, %arg0) : (index, index) -> (index) | ||
return %ret : index | ||
} | ||
|
||
// CHECK-LABEL: func @single_parameter() { | ||
// CHECK: return | ||
// CHECK: } | ||
|
||
// CHECK-LABEL: func @mutl_parameter( | ||
// CHECK-SAME: %[[ARG0:.*]]: index) -> index { | ||
// CHECK: return %[[ARG0]] : index | ||
// CHECK: } | ||
|
||
// CHECK-LABEL: func @eliminate_parameter( | ||
// CHECK-SAME: %[[ARG0:.*]]: index) -> index { | ||
// CHECK: call @single_parameter() : () -> () | ||
// CHECK: %[[RET:.*]] = call @mutl_parameter(%[[ARG0]]) : (index) -> index | ||
// CHECK: return %[[RET]] : index | ||
// CHECK: } |
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.
Note: all these functions are public, it is incorrect to change the signature of a public function: you can't know about call sites outside of the current module.
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.
This is something I hadn't considered.Thank you for the reminder. It has helped me better understand the meaning of “public function.”