[llvm] [FunctionSpecializer] Keep the blocks in dead functions and remove the callsites of dead function properly. (PR #154668)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Aug 26 11:06:45 PDT 2025
https://github.com/XChy updated https://github.com/llvm/llvm-project/pull/154668
>From bc9c37a53479327a1c0acab540f8a4d7257c9936 Mon Sep 17 00:00:00 2001
From: XChy <xxs_chy at outlook.com>
Date: Thu, 21 Aug 2025 12:22:24 +0800
Subject: [PATCH 1/4] [FunctionnSpecializer] Do not mark function dead if any
unexecutable callsite exists
---
.../Transforms/IPO/FunctionSpecialization.h | 2 +
.../Transforms/IPO/FunctionSpecialization.cpp | 9 ++++
llvm/lib/Transforms/IPO/SCCP.cpp | 4 +-
.../reachable-after-specialization.ll | 41 +++++++++++++++++++
4 files changed, 55 insertions(+), 1 deletion(-)
create mode 100644 llvm/test/Transforms/FunctionSpecialization/reachable-after-specialization.ll
diff --git a/llvm/include/llvm/Transforms/IPO/FunctionSpecialization.h b/llvm/include/llvm/Transforms/IPO/FunctionSpecialization.h
index 1575afa50e198..99640d0fde690 100644
--- a/llvm/include/llvm/Transforms/IPO/FunctionSpecialization.h
+++ b/llvm/include/llvm/Transforms/IPO/FunctionSpecialization.h
@@ -270,6 +270,8 @@ class FunctionSpecializer {
return InstCostVisitor(GetBFI, F, M.getDataLayout(), TTI, Solver);
}
+ bool isDeadFunction(Function *F) { return FullySpecialized.contains(F); }
+
private:
Constant *getPromotableAlloca(AllocaInst *Alloca, CallInst *Call);
diff --git a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
index c876a47ef2129..86805547ca67d 100644
--- a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
+++ b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
@@ -844,6 +844,15 @@ void FunctionSpecializer::removeDeadFunctions() {
<< F->getName() << "\n");
if (FAM)
FAM->clear(*F, F->getName());
+
+ // Remove all the callsites that were proven unreachable once, and replace
+ // them with poison.
+ for (User *U : make_early_inc_range(F->users())) {
+ assert((isa<CallInst>(U) || isa<InvokeInst>(U)) && "User of dead function must be call or invoke");
+ Instruction *CS = cast<Instruction>(U);
+ CS->replaceAllUsesWith(PoisonValue::get(CS->getType()));
+ CS->eraseFromParent();
+ }
F->eraseFromParent();
}
FullySpecialized.clear();
diff --git a/llvm/lib/Transforms/IPO/SCCP.cpp b/llvm/lib/Transforms/IPO/SCCP.cpp
index d50de34dfa482..604fc37d8ab34 100644
--- a/llvm/lib/Transforms/IPO/SCCP.cpp
+++ b/llvm/lib/Transforms/IPO/SCCP.cpp
@@ -167,7 +167,9 @@ static bool runIPSCCP(
// constants if we have found them to be of constant values.
bool MadeChanges = false;
for (Function &F : M) {
- if (F.isDeclaration())
+ // Skip the dead functions marked by FunctionSpecializer, avoiding removing
+ // blocks in dead functions.
+ if (F.isDeclaration() || Specializer.isDeadFunction(&F))
continue;
SmallVector<BasicBlock *, 512> BlocksToErase;
diff --git a/llvm/test/Transforms/FunctionSpecialization/reachable-after-specialization.ll b/llvm/test/Transforms/FunctionSpecialization/reachable-after-specialization.ll
new file mode 100644
index 0000000000000..53641e265a3fc
--- /dev/null
+++ b/llvm/test/Transforms/FunctionSpecialization/reachable-after-specialization.ll
@@ -0,0 +1,41 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -passes=ipsccp --funcspec-min-function-size=1 -S < %s | FileCheck %s
+
+define i32 @caller() {
+; CHECK-LABEL: define i32 @caller() {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[CALL1:%.*]] = call i32 @callee.specialized.1(i32 1)
+; CHECK-NEXT: [[CALL2:%.*]] = call i32 @callee.specialized.2(i32 0)
+; CHECK-NEXT: [[COND:%.*]] = icmp eq i32 undef, 0
+; CHECK-NEXT: br i1 [[COND]], label %[[COMMON_RET:.*]], label %[[IF_THEN:.*]]
+; CHECK: [[COMMON_RET]]:
+; CHECK-NEXT: ret i32 0
+; CHECK: [[IF_THEN]]:
+; CHECK-NEXT: ret i32 0
+;
+entry:
+ %call1 = call i32 @callee(i32 1)
+ %call2 = call i32 @callee(i32 0)
+ %cond = icmp eq i32 %call2, 0
+ br i1 %cond, label %common.ret, label %if.then
+
+common.ret: ; preds = %entry
+ ret i32 0
+
+if.then: ; preds = %entry
+ %unreachable_call = call i32 @callee(i32 2)
+ ret i32 %unreachable_call
+}
+
+define internal i32 @callee(i32 %ac) {
+entry:
+ br label %ai
+
+ai: ; preds = %ai, %entry
+ %add = or i32 0, 0
+ %cond = icmp eq i32 %ac, 1
+ br i1 %cond, label %aj, label %ai
+
+aj: ; preds = %ai
+ ret i32 0
+}
>From 1067b5c640ab1b10bb2d4eae3bea319dcf7417dc Mon Sep 17 00:00:00 2001
From: XChy <xxs_chy at outlook.com>
Date: Sun, 24 Aug 2025 19:11:53 +0800
Subject: [PATCH 2/4] format
---
llvm/lib/Transforms/IPO/FunctionSpecialization.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
index 86805547ca67d..745041a0d5709 100644
--- a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
+++ b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
@@ -848,7 +848,8 @@ void FunctionSpecializer::removeDeadFunctions() {
// Remove all the callsites that were proven unreachable once, and replace
// them with poison.
for (User *U : make_early_inc_range(F->users())) {
- assert((isa<CallInst>(U) || isa<InvokeInst>(U)) && "User of dead function must be call or invoke");
+ assert((isa<CallInst>(U) || isa<InvokeInst>(U)) &&
+ "User of dead function must be call or invoke");
Instruction *CS = cast<Instruction>(U);
CS->replaceAllUsesWith(PoisonValue::get(CS->getType()));
CS->eraseFromParent();
>From b207e136acbf29d27159c24649dfb86dcfe1f636 Mon Sep 17 00:00:00 2001
From: XChy <xxs_chy at outlook.com>
Date: Mon, 25 Aug 2025 16:41:09 +0800
Subject: [PATCH 3/4] resolve comments
---
llvm/lib/Transforms/IPO/SCCP.cpp | 3 ++-
.../reachable-after-specialization.ll | 12 ++++++------
2 files changed, 8 insertions(+), 7 deletions(-)
diff --git a/llvm/lib/Transforms/IPO/SCCP.cpp b/llvm/lib/Transforms/IPO/SCCP.cpp
index 604fc37d8ab34..e40130cbefe72 100644
--- a/llvm/lib/Transforms/IPO/SCCP.cpp
+++ b/llvm/lib/Transforms/IPO/SCCP.cpp
@@ -169,7 +169,8 @@ static bool runIPSCCP(
for (Function &F : M) {
// Skip the dead functions marked by FunctionSpecializer, avoiding removing
// blocks in dead functions.
- if (F.isDeclaration() || Specializer.isDeadFunction(&F))
+ if (F.isDeclaration() ||
+ (IsFuncSpecEnabled && Specializer.isDeadFunction(&F)))
continue;
SmallVector<BasicBlock *, 512> BlocksToErase;
diff --git a/llvm/test/Transforms/FunctionSpecialization/reachable-after-specialization.ll b/llvm/test/Transforms/FunctionSpecialization/reachable-after-specialization.ll
index 53641e265a3fc..5c4fb67643827 100644
--- a/llvm/test/Transforms/FunctionSpecialization/reachable-after-specialization.ll
+++ b/llvm/test/Transforms/FunctionSpecialization/reachable-after-specialization.ll
@@ -27,15 +27,15 @@ if.then: ; preds = %entry
ret i32 %unreachable_call
}
-define internal i32 @callee(i32 %ac) {
+define internal i32 @callee(i32 %arg) {
entry:
- br label %ai
+ br label %loop
-ai: ; preds = %ai, %entry
+loop: ; preds = %ai, %entry
%add = or i32 0, 0
- %cond = icmp eq i32 %ac, 1
- br i1 %cond, label %aj, label %ai
+ %cond = icmp eq i32 %arg, 1
+ br i1 %cond, label %exit, label %loop
-aj: ; preds = %ai
+exit: ; preds = %ai
ret i32 0
}
>From af78e24e731f992fc67bdfa571347c7de8f6966e Mon Sep 17 00:00:00 2001
From: XChy <xxs_chy at outlook.com>
Date: Wed, 27 Aug 2025 02:06:30 +0800
Subject: [PATCH 4/4] Replace the concept FullySpecialized with DeadFunctions
and force forbidding address taken in updateCallsites
---
.../llvm/Transforms/IPO/FunctionSpecialization.h | 4 ++--
llvm/lib/Transforms/IPO/FunctionSpecialization.cpp | 11 +++++++----
llvm/lib/Transforms/IPO/SCCP.cpp | 5 +++--
3 files changed, 12 insertions(+), 8 deletions(-)
diff --git a/llvm/include/llvm/Transforms/IPO/FunctionSpecialization.h b/llvm/include/llvm/Transforms/IPO/FunctionSpecialization.h
index 99640d0fde690..5a682e8c7b5eb 100644
--- a/llvm/include/llvm/Transforms/IPO/FunctionSpecialization.h
+++ b/llvm/include/llvm/Transforms/IPO/FunctionSpecialization.h
@@ -246,7 +246,7 @@ class FunctionSpecializer {
std::function<AssumptionCache &(Function &)> GetAC;
SmallPtrSet<Function *, 32> Specializations;
- SmallPtrSet<Function *, 32> FullySpecialized;
+ SmallPtrSet<Function *, 32> DeadFunctions;
DenseMap<Function *, CodeMetrics> FunctionMetrics;
DenseMap<Function *, unsigned> FunctionGrowth;
unsigned NGlobals = 0;
@@ -270,7 +270,7 @@ class FunctionSpecializer {
return InstCostVisitor(GetBFI, F, M.getDataLayout(), TTI, Solver);
}
- bool isDeadFunction(Function *F) { return FullySpecialized.contains(F); }
+ bool isDeadFunction(Function *F) { return DeadFunctions.contains(F); }
private:
Constant *getPromotableAlloca(AllocaInst *Alloca, CallInst *Call);
diff --git a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
index 745041a0d5709..a5fbba39ee283 100644
--- a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
+++ b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
@@ -839,7 +839,7 @@ bool FunctionSpecializer::run() {
}
void FunctionSpecializer::removeDeadFunctions() {
- for (Function *F : FullySpecialized) {
+ for (Function *F : DeadFunctions) {
LLVM_DEBUG(dbgs() << "FnSpecialization: Removing dead function "
<< F->getName() << "\n");
if (FAM)
@@ -856,7 +856,7 @@ void FunctionSpecializer::removeDeadFunctions() {
}
F->eraseFromParent();
}
- FullySpecialized.clear();
+ DeadFunctions.clear();
}
/// Clone the function \p F and remove the ssa_copy intrinsics added by
@@ -1217,8 +1217,11 @@ void FunctionSpecializer::updateCallSites(Function *F, const Spec *Begin,
// If the function has been completely specialized, the original function
// is no longer needed. Mark it unreachable.
- if (NCallsLeft == 0 && Solver.isArgumentTrackedFunction(F)) {
+ // NOTE: If the address of a function is taken, we cannot treat it as dead
+ // function.
+ if (NCallsLeft == 0 && Solver.isArgumentTrackedFunction(F) &&
+ !F->hasAddressTaken()) {
Solver.markFunctionUnreachable(F);
- FullySpecialized.insert(F);
+ DeadFunctions.insert(F);
}
}
diff --git a/llvm/lib/Transforms/IPO/SCCP.cpp b/llvm/lib/Transforms/IPO/SCCP.cpp
index e40130cbefe72..e98a70f228ada 100644
--- a/llvm/lib/Transforms/IPO/SCCP.cpp
+++ b/llvm/lib/Transforms/IPO/SCCP.cpp
@@ -167,10 +167,11 @@ static bool runIPSCCP(
// constants if we have found them to be of constant values.
bool MadeChanges = false;
for (Function &F : M) {
+ if (F.isDeclaration())
+ continue;
// Skip the dead functions marked by FunctionSpecializer, avoiding removing
// blocks in dead functions.
- if (F.isDeclaration() ||
- (IsFuncSpecEnabled && Specializer.isDeadFunction(&F)))
+ if (IsFuncSpecEnabled && Specializer.isDeadFunction(&F))
continue;
SmallVector<BasicBlock *, 512> BlocksToErase;
More information about the llvm-commits
mailing list