[llvm] [FuncAttrs] Relax norecurse attribute inference (PR #139943)

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 12 08:09:34 PDT 2025


================
@@ -2071,24 +2073,50 @@ static void addNoRecurseAttrs(const SCCNodeSet &SCCNodes,
   if (!F || !F->hasExactDefinition() || F->doesNotRecurse())
     return;
 
-  // If all of the calls in F are identifiable and are to norecurse functions, F
-  // is norecurse. This check also detects self-recursion as F is not currently
-  // marked norecurse, so any called from F to F will not be marked norecurse.
-  for (auto &BB : *F)
-    for (auto &I : BB.instructionsWithoutDebug())
+  if (F->hasAddressTaken())
+    return;
+
+  Module *M = F->getParent();
+  llvm::TargetLibraryInfoImpl TLII(llvm::Triple(M->getTargetTriple()));
+  llvm::TargetLibraryInfo TLI(TLII);
+  // If all of the calls in F are identifiable and can be proven to not
+  // callback F, F is norecurse. This check also detects self-recursion
+  // as F is not currently marked norecurse, so any call from F to F
+  // will not be marked norecurse.
+  for (auto &BB : *F) {
+    for (auto &I : BB.instructionsWithoutDebug()) {
       if (auto *CB = dyn_cast<CallBase>(&I)) {
         Function *Callee = CB->getCalledFunction();
-        if (!Callee || Callee == F ||
-            (!Callee->doesNotRecurse() &&
-             !(Callee->isDeclaration() &&
-               Callee->hasFnAttribute(Attribute::NoCallback))))
-          // Function calls a potentially recursive function.
+
+        if (!Callee || Callee == F)
           return;
+
+        if (Callee->doesNotRecurse())
+          continue;
+
+        LibFunc LF;
+        if (Callee->isDeclaration()) {
+          // External call with NoCallback attribute.
+          if (Callee->hasFnAttribute(Attribute::NoCallback))
+            continue;
+          // We rely on this only in post link stage when all functions in the
----------------
david-arm wrote:

I think it might be worth pulling the LTO-time changes into a separate PR to keep things simple. That way it also makes to easier to see which test changes are caused by which code change.

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


More information about the llvm-commits mailing list