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

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 13 05:26:16 PDT 2025


================
@@ -2071,24 +2072,42 @@ 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())
+  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)) {
+        if (F->hasAddressTaken())
+          return;
+
         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()) {
+          if (Callee->hasFnAttribute(Attribute::NoCallback) ||
+              (NoFunctionsAddressIsTaken &&
+               TLI.getLibFunc(Callee->getName(), LF)))
+            continue;
+          return;
+        }
       }
+    }
+  }
 
-  // Every call was to a non-recursive function other than this function, and
-  // we have no indirect recursion as the SCC size is one. This function cannot
-  // recurse.
+  // Every call was either to an external function guaranteed to not make a
+  // call to this function or a direct call to internal function and we have no
+  // indirect recursion as the SCC size is one. This function cannot recurse.
----------------
david-arm wrote:

I think this comment `we have no indirect recursion as the SCC size is one` might be misleading because I don't believe SCC size of one actually makes any guarantee about indirect recursion. You'd need to both a) have a size is 1 and b) know there are no indirect calls. I think that's what you're trying to say, but perhaps just needs wording differently?

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


More information about the llvm-commits mailing list