[llvm] [FuncAttrs] Relax norecurse attribute inference (PR #139943)
Usha Gupta via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 3 08:36:47 PDT 2025
================
@@ -2322,8 +2343,39 @@ PreservedAnalyses PostOrderFunctionAttrsPass::run(LazyCallGraph::SCC &C,
Functions.push_back(&N.getFunction());
}
- auto ChangedFunctions =
- deriveAttrsInPostOrder(Functions, AARGetter, ArgAttrsOnly);
+ bool NoFunctionsAddressIsTaken = false;
+ // Check if any function in the whole program has its address taken or has
+ // potentially external linkage.
+ // We use this information when inferring norecurse attribute: If there is
+ // no function whose address is taken and all functions have internal
+ // linkage, there is no path for a callback to any user function.
+ if (IsLTOPostLink || ForceLTOFuncAttrs) {
+ bool AnyFunctionsAddressIsTaken = false;
+ // Get the parent Module of the Function
+ Module &M = *C.begin()->getFunction().getParent();
+ for (Function &F : M) {
+ // We only care about functions defined in user program whose addresses
+ // escape, making them potential callback targets.
+ if (F.isDeclaration())
+ continue;
+
+ // If the function is already marked as norecurse, this should not block
+ // norecurse inference even though it may have external linkage.
+ // For ex: main() in C++.
+ if (F.doesNotRecurse())
+ continue;
----------------
usha1830 wrote:
@nikic I was further examining the behavior of this example, both with and without my patch.
In all the cases I've tested — including forcing `norecurse`, compiling as a shared library vs. an executable, using LTO and non-LTO options — the output remains the same, regardless of the patch.
However, it appears that `ReversePostOrderPass` is adding the `norecurse` attribute to function `a`, even when the `norecurse_fn` is **not** internal.
The reason seems to be that all of `a`'s callers (`norecurse_fn()` and `foo()`) are themselves marked `norecurse`. Please note that `norecurse` is inferred for `foo()` as well in this pass only.
This behavior doesn't seem entirely correct — or am I missing something?
https://godbolt.org/z/G8o9PczGG
https://github.com/llvm/llvm-project/pull/139943
More information about the llvm-commits
mailing list