@@ -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)) {
0 commit comments