Skip to content

Commit cee600a

Browse files
make remove-dead-values can delate useless function.
1 parent 41f3438 commit cee600a

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

mlir/lib/Transforms/RemoveDeadValues.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,17 @@ static void processFuncOp(FunctionOpInterface funcOp, Operation *module,
297297
return;
298298
}
299299

300+
// If a private function has neither users and function calls, it is a useless
301+
// function.
302+
SymbolTable::UseRange uses = *funcOp.getSymbolUses(module);
303+
auto callSites = funcOp.getFunctionBody().getOps<CallOpInterface>();
304+
if (uses.empty() && callSites.empty()) {
305+
LDBG() << "Delete function op: "
306+
<< OpWithFlags(funcOp, OpPrintingFlags().skipRegions());
307+
funcOp.erase();
308+
return;
309+
}
310+
300311
// Get the list of unnecessary (non-live) arguments in `nonLiveArgs`.
301312
SmallVector<Value> arguments(funcOp.getArguments());
302313
BitVector nonLiveArgs = markLives(arguments, nonLiveSet, la);
@@ -312,7 +323,6 @@ static void processFuncOp(FunctionOpInterface funcOp, Operation *module,
312323
// Do (2). (Skip creating generic operand cleanup entries for call ops.
313324
// Call arguments will be removed in the call-site specific segment-aware
314325
// cleanup, avoiding generic eraseOperands bitvector mechanics.)
315-
SymbolTable::UseRange uses = *funcOp.getSymbolUses(module);
316326
for (SymbolTable::SymbolUse use : uses) {
317327
Operation *callOp = use.getUser();
318328
assert(isa<CallOpInterface>(callOp) && "expected a call-like user");
@@ -881,9 +891,12 @@ void RemoveDeadValues::runOnOperation() {
881891
// end of this pass.
882892
RDVFinalCleanupList finalCleanupList;
883893

894+
module->walk([&](FunctionOpInterface op) {
895+
processFuncOp(op, module, la, deadVals, finalCleanupList);
896+
});
884897
module->walk([&](Operation *op) {
885898
if (auto funcOp = dyn_cast<FunctionOpInterface>(op)) {
886-
processFuncOp(funcOp, module, la, deadVals, finalCleanupList);
899+
// The FunctionOpInterface has been processed in advance.
887900
} else if (auto regionBranchOp = dyn_cast<RegionBranchOpInterface>(op)) {
888901
processRegionBranchOp(regionBranchOp, la, deadVals, finalCleanupList);
889902
} else if (auto branchOp = dyn_cast<BranchOpInterface>(op)) {

mlir/test/Transforms/remove-dead-values.mlir

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,3 +649,18 @@ func.func @callee(%arg0: index, %arg1: index, %arg2: index) -> index {
649649
%res = call @mutl_parameter(%arg0, %arg1, %arg2) : (index, index, index) -> (index)
650650
return %res : index
651651
}
652+
653+
// -----
654+
655+
// CHECK-NOT: func private @single_private_func
656+
func.func private @single_private_func(%arg0: i64) -> (i64) {
657+
%c0_i64 = arith.constant 0 : i64
658+
%2 = arith.cmpi eq, %arg0, %c0_i64 : i64
659+
cf.cond_br %2, ^bb1, ^bb2
660+
^bb1: // pred: ^bb0
661+
%c1_i64 = arith.constant 1 : i64
662+
return %c1_i64 : i64
663+
^bb2: // pred: ^bb0
664+
%c3_i64 = arith.constant 3 : i64
665+
return %c3_i64 : i64
666+
}

0 commit comments

Comments
 (0)