[llvm] [FunctionSpecialization] Preserve call counts of specialized functions (PR #157768)

Mircea Trofin via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 9 16:52:53 PDT 2025


================
@@ -784,9 +784,25 @@ bool FunctionSpecializer::run() {
 
     // Update the known call sites to call the clone.
     for (CallBase *Call : S.CallSites) {
+      Function *Clone = S.Clone;
       LLVM_DEBUG(dbgs() << "FnSpecialization: Redirecting " << *Call
-                        << " to call " << S.Clone->getName() << "\n");
+                        << " to call " << Clone->getName() << "\n");
       Call->setCalledFunction(S.Clone);
+      if (std::optional<uint64_t> Count =
+              GetBFI(*Call->getFunction())
+                  .getBlockProfileCount(Call->getParent())) {
+        uint64_t CallCount = *Count + Clone->getEntryCount()->getCount();
+        Clone->setEntryCount(CallCount);
+        if (std::optional<llvm::Function::ProfileCount> MaybeOriginalCount =
+                S.F->getEntryCount()) {
+          uint64_t OriginalCount = MaybeOriginalCount->getCount();
+          if (OriginalCount > CallCount) {
+            S.F->setEntryCount(OriginalCount - CallCount);
+          } else {
+            S.F->setEntryCount(0);
----------------
mtrofin wrote:

I think this case should be LLVM_DEBUG-ed out, because it'd be a bit surprising.

https://github.com/llvm/llvm-project/pull/157768


More information about the llvm-commits mailing list