[llvm] [TailCallElim] Optionally suppress tail call elim for cold calls (PR #209642)

Teresa Johnson via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 17 12:51:08 PDT 2026


================
@@ -89,12 +90,48 @@ using namespace llvm;
 STATISTIC(NumEliminated, "Number of tail calls removed");
 STATISTIC(NumRetDuped,   "Number of return duplicated");
 STATISTIC(NumAccumAdded, "Number of accumulators introduced");
+STATISTIC(NumTREPreventedCold,
+          "Number of tail calls/recursion eliminations prevented due to cold "
+          "calling convention or attribute");
 
 static cl::opt<bool> ForceDisableBFI(
     "tre-disable-entrycount-recompute", cl::init(false), cl::Hidden,
     cl::desc("Force disabling recomputing of function entry count, on "
              "successful tail recursion elimination."));
 
+static cl::opt<bool> DisableTailCallElimForColdCalls(
+    "disable-tail-call-elim-for-cold-calls", cl::Hidden, cl::init(false),
+    cl::desc("Disable tail call elimination and optimization for cold calls or "
+             "in cold functions"));
+
+static bool shouldDisableTailCallsForCold(const CallBase *CB,
+                                          const Function *Caller,
+                                          const ProfileSummaryInfo *PSI,
+                                          BlockFrequencyInfo *BFI) {
+  if (!DisableTailCallElimForColdCalls)
+    return false;
+
+  if (CB && CB->isMustTailCall())
+    return false;
+
+  if (CB && (CB->hasFnAttr(Attribute::Cold) ||
+             CB->getCallingConv() == CallingConv::Cold))
+    return true;
+
+  if (Caller && (Caller->hasFnAttribute(Attribute::Cold) ||
+                 Caller->getCallingConv() == CallingConv::Cold))
+    return true;
+
+  if (!PSI || !PSI->hasProfileSummary())
+    return false;
+
+  if (CB && BFI &&
+      (PSI->isColdCallSite(*CB, BFI) || PSI->isColdBlock(CB->getParent(), BFI)))
----------------
teresajohnson wrote:

Augmented this to check for a cold enclosing block as well. It wasn't working at first, but then I realized that this pass only set up BFI if the function entry count was non-zero, and it is zero for profiled completely cold. I have adjusted the initialization of BFI and that ensured I got the expected result in a cold function I was looking at in an instrumented PGO user.

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


More information about the llvm-commits mailing list