[clang] [lld] [llvm] [LTO][LLD] Prevent invalid LTO libfunc transforms (PR #164916)

Teresa Johnson via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 11 08:04:30 PST 2026


================
@@ -279,9 +279,31 @@ static void runNewPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM,
   RegisterPassPlugins(Conf.PassPlugins, PB);
 
   std::unique_ptr<TargetLibraryInfoImpl> TLII(
-      new TargetLibraryInfoImpl(TM->getTargetTriple(), TM->Options.VecLib));
+      new TargetLibraryInfoImpl(TM->getTargetTriple()));
   if (Conf.Freestanding)
     TLII->disableAllFunctions();
+
+  TargetLibraryInfo TLI(*TLII);
+  for (unsigned I = 0, E = static_cast<unsigned>(LibFunc::NumLibFuncs); I != E;
+       ++I) {
+    LibFunc F = static_cast<LibFunc>(I);
+    StringRef Name = TLI.getName(F);
+    GlobalValue *Val = Mod.getNamedValue(Name);
+
+    // LibFuncs present in the current TU can always be referenced.
+    if (Val && !Val->isDeclaration())
+      continue;
+
+    // LibFuncs not implemented in bitcode can always be referenced.
+    if (!BitcodeLibFuncs.contains(Name))
+      continue;
+
+    // FIXME: Functions that are somewhere in a ThinLTO link (just not imported
+    // in this module) should not be disabled, as they have already been
+    // extracted.
----------------
teresajohnson wrote:

I think this information could be figured out earlier during the LTO link, refining the BitcodeLibFuncs set, which would address it both for regular LTO and for (currently in-process only, see other comment) ThinLTO (and you could remove the above check whether it is defined in this TU). There's 2 places in theory where you could do this:
1) when bitcode files are added to LTO via addModule. However, it looks like currently in the linker you are computing the initial set after all the bitcode objects are added, so that would need to be reordered.
2) right at the start of LTO::run(). You'd want to consult the GlobalResolutions, which is a map from symbol name to the global linker resolution info. You could add a loop over the BitcodeLibFuncs, and check if we have a GlobalResolution for each. If the Prevailing bool is set then the LTO unit contains the prevailing def of the function, and that lib func could then be removed from the set.

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


More information about the cfe-commits mailing list