[JITLink] Don't issue lookups for empty symbol sets.
authorLang Hames <lhames@gmail.com>
Fri, 19 Mar 2021 21:54:07 +0000 (14:54 -0700)
committerLang Hames <lhames@gmail.com>
Fri, 19 Mar 2021 23:10:47 +0000 (16:10 -0700)
Issuing a lookup for an empty symbol set is legal, but can actually result in
unrelated work being done if there was a work queue left over from the previous
lookup. We can avoid doing this unrelated work (reducing stack depth and
interleaving of debugging output) by not issuing these no-op lookups in the
first place.

llvm/lib/ExecutionEngine/JITLink/JITLinkGeneric.cpp

index 2e5b7cb..63f862b 100644 (file)
@@ -66,14 +66,27 @@ void JITLinkerBase::linkPhase1(std::unique_ptr<JITLinkerBase> Self) {
     return Ctx->notifyFailed(std::move(Err));
 
   // Notify client that the defined symbols have been assigned addresses.
-  LLVM_DEBUG(
-      { dbgs() << "Resolving symbols defined in " << G->getName() << "\n"; });
+  LLVM_DEBUG(dbgs() << "Resolving symbols defined in " << G->getName() << "\n");
 
   if (auto Err = Ctx->notifyResolved(*G))
     return Ctx->notifyFailed(std::move(Err));
 
   auto ExternalSymbols = getExternalSymbolNames();
 
+  // If there are no external symbols then proceed immediately with phase 2.
+  if (ExternalSymbols.empty()) {
+    LLVM_DEBUG({
+      dbgs() << "No external symbols for " << G->getName()
+             << ". Proceeding immediately with link phase 2.\n";
+    });
+    // FIXME: Once callee expressions are defined to be sequenced before
+    //        argument expressions (c++17) we can simplify this. See below.
+    auto &TmpSelf = *Self;
+    TmpSelf.linkPhase2(std::move(Self), AsyncLookupResult(), std::move(Layout));
+    return;
+  }
+
+  // Otherwise look up the externals.
   LLVM_DEBUG({
     dbgs() << "Issuing lookup for external symbols for " << G->getName()
            << " (may trigger materialization/linking of other graphs)...\n";