[llvm-branch-commits] [lld] [lld][MachO][NFC] Accept CachedHashStringRef in SymbolTable insertion methods (PR #240981)

Liza Burakova via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Nov 16 11:51:18 PST 2026


https://github.com/liza371 updated https://github.com/llvm/llvm-project/pull/240981

>From 689a69f65fae5ddd009a828063a8b00b4e1007c5 Mon Sep 17 00:00:00 2001
From: Liza Burakova <liza at chromium.org>
Date: Mon, 9 Nov 2026 16:53:15 -0500
Subject: [PATCH] [lld][MachO][NFC] Accept CachedHashStringRef in SymbolTable
 insertion methods

This is part of the ld64.lld performance improvements tracked by

Currently, SymbolTable::insert() constructs a CachedHashStringRef on the
serial insertion path. This change adds CachedHashStringRef overloads
for insert(), addDefined(), addUndefined(), addCommon(), and
addLazyObject() so callers can pre-hash symbol names in parallel, while
keeping StringRef wrappers for existing callers.

AI use: this change is based off a prototype developed by an LLM.

This commit is output-preserving.
---
 lld/MachO/SymbolTable.cpp | 35 ++++++++++++++++++----------------
 lld/MachO/SymbolTable.h   | 40 ++++++++++++++++++++++++++++++++-------
 2 files changed, 52 insertions(+), 23 deletions(-)

diff --git a/lld/MachO/SymbolTable.cpp b/lld/MachO/SymbolTable.cpp
index 1942e8ec6cdaae..3e3a38babbeff6 100644
--- a/lld/MachO/SymbolTable.cpp
+++ b/lld/MachO/SymbolTable.cpp
@@ -28,9 +28,9 @@ Symbol *SymbolTable::find(CachedHashStringRef cachedName) {
   return symVector[it->second];
 }
 
-std::pair<Symbol *, bool> SymbolTable::insert(StringRef name,
+std::pair<Symbol *, bool> SymbolTable::insert(CachedHashStringRef name,
                                               const InputFile *file) {
-  auto p = symMap.insert({CachedHashStringRef(name), (int)symVector.size()});
+  auto p = symMap.insert({name, (int)symVector.size()});
 
   Symbol *sym;
   if (!p.second) {
@@ -99,13 +99,14 @@ static void transplantSymbolsAtOffset(InputSection *fromIsec,
   });
 }
 
-Defined *SymbolTable::addDefined(StringRef name, InputFile *file,
-                                 InputSection *isec, uint64_t value,
-                                 uint64_t size, bool isWeakDef,
+Defined *SymbolTable::addDefined(CachedHashStringRef hashedName,
+                                 InputFile *file, InputSection *isec,
+                                 uint64_t value, uint64_t size, bool isWeakDef,
                                  bool isPrivateExtern,
                                  bool isReferencedDynamically, bool noDeadStrip,
                                  bool isWeakDefCanBeHidden, bool isCold,
                                  Symbol *known) {
+  StringRef name = hashedName.val();
   Symbol *s;
   bool wasInserted;
   if (known) {
@@ -113,7 +114,7 @@ Defined *SymbolTable::addDefined(StringRef name, InputFile *file,
     s->isUsedInRegularObj |= !file || isa<ObjFile>(file);
     wasInserted = false;
   } else {
-    std::tie(s, wasInserted) = insert(name, file);
+    std::tie(s, wasInserted) = insert(hashedName, file);
   }
   bool overridesWeakDef =
       !wasInserted && !isWeakDef && s->pendingOverridesWeakDef;
@@ -238,14 +239,14 @@ Defined *SymbolTable::aliasDefined(Defined *src, StringRef target,
                     src->weakDefCanBeHidden, src->cold);
 }
 
-Symbol *SymbolTable::addUndefined(StringRef name, InputFile *file,
+Symbol *SymbolTable::addUndefined(CachedHashStringRef name, InputFile *file,
                                   bool isWeakRef) {
   auto [s, wasInserted] = insert(name, file);
 
   RefState refState = isWeakRef ? RefState::Weak : RefState::Strong;
 
   if (wasInserted)
-    replaceSymbol<Undefined>(s, name, file, refState,
+    replaceSymbol<Undefined>(s, name.val(), file, refState,
                              /*wasBitcodeSymbol=*/false);
   else if (auto *lazy = dyn_cast<LazyArchive>(s)) {
     if (!s->hasPendingDefinition)
@@ -263,8 +264,9 @@ Symbol *SymbolTable::addUndefined(StringRef name, InputFile *file,
   return s;
 }
 
-Symbol *SymbolTable::addCommon(StringRef name, InputFile *file, uint64_t size,
-                               uint32_t align, bool isPrivateExtern) {
+Symbol *SymbolTable::addCommon(CachedHashStringRef name, InputFile *file,
+                               uint64_t size, uint32_t align,
+                               bool isPrivateExtern) {
   auto [s, wasInserted] = insert(name, file);
 
   if (!wasInserted) {
@@ -280,7 +282,8 @@ Symbol *SymbolTable::addCommon(StringRef name, InputFile *file, uint64_t size,
     // a name conflict, we fall through to the replaceSymbol() call below.
   }
 
-  replaceSymbol<CommonSymbol>(s, name, file, size, align, isPrivateExtern);
+  replaceSymbol<CommonSymbol>(s, name.val(), file, size, align,
+                              isPrivateExtern);
   return s;
 }
 
@@ -348,24 +351,24 @@ Symbol *SymbolTable::addLazyArchive(StringRef name, ArchiveFile *file,
   return s;
 }
 
-Symbol *SymbolTable::addLazyObject(StringRef name, InputFile &file) {
+Symbol *SymbolTable::addLazyObject(CachedHashStringRef name, InputFile &file) {
   auto [s, wasInserted] = insert(name, &file);
 
   if (wasInserted) {
-    replaceSymbol<LazyObject>(s, file, name);
+    replaceSymbol<LazyObject>(s, file, name.val());
   } else if (!s->hasPendingDefinition) {
     if (isa<Undefined>(s)) {
       if (isa<ObjFile>(file))
         s->hasPendingDefinition = true;
-      extract(file, name);
+      extract(file, name.val());
     } else if (auto *dysym = dyn_cast<DylibSymbol>(s)) {
       if (dysym->isWeakDef()) {
         if (dysym->getRefState() != RefState::Unreferenced) {
           if (isa<ObjFile>(file))
             s->hasPendingDefinition = true;
-          extract(file, name);
+          extract(file, name.val());
         } else {
-          replaceSymbol<LazyObject>(s, file, name);
+          replaceSymbol<LazyObject>(s, file, name.val());
         }
       }
     }
diff --git a/lld/MachO/SymbolTable.h b/lld/MachO/SymbolTable.h
index 4f37d9a3be8910..0d513f58b3288d 100644
--- a/lld/MachO/SymbolTable.h
+++ b/lld/MachO/SymbolTable.h
@@ -36,26 +36,48 @@ class Undefined;
  */
 class SymbolTable {
 public:
-  Defined *addDefined(StringRef name, InputFile *, InputSection *,
+  Defined *addDefined(llvm::CachedHashStringRef name, InputFile *,
+                      InputSection *, uint64_t value, uint64_t size,
+                      bool isWeakDef, bool isPrivateExtern,
+                      bool isReferencedDynamically, bool noDeadStrip,
+                      bool isWeakDefCanBeHidden, bool isCold = false,
+                      Symbol *known = nullptr);
+  Defined *addDefined(StringRef name, InputFile *file, InputSection *isec,
                       uint64_t value, uint64_t size, bool isWeakDef,
                       bool isPrivateExtern, bool isReferencedDynamically,
                       bool noDeadStrip, bool isWeakDefCanBeHidden,
-                      bool isCold = false, Symbol *known = nullptr);
+                      bool isCold = false, Symbol *known = nullptr) {
+    return addDefined(llvm::CachedHashStringRef(name), file, isec, value, size,
+                      isWeakDef, isPrivateExtern, isReferencedDynamically,
+                      noDeadStrip, isWeakDefCanBeHidden, isCold, known);
+  }
 
   Defined *aliasDefined(Defined *src, StringRef target, InputFile *newFile,
                         bool makePrivateExtern = false);
 
-  Symbol *addUndefined(StringRef name, InputFile *, bool isWeakRef);
+  Symbol *addUndefined(llvm::CachedHashStringRef name, InputFile *,
+                       bool isWeakRef);
+  Symbol *addUndefined(StringRef name, InputFile *file, bool isWeakRef) {
+    return addUndefined(llvm::CachedHashStringRef(name), file, isWeakRef);
+  }
 
-  Symbol *addCommon(StringRef name, InputFile *, uint64_t size, uint32_t align,
-                    bool isPrivateExtern);
+  Symbol *addCommon(llvm::CachedHashStringRef name, InputFile *, uint64_t size,
+                    uint32_t align, bool isPrivateExtern);
+  Symbol *addCommon(StringRef name, InputFile *file, uint64_t size,
+                    uint32_t align, bool isPrivateExtern) {
+    return addCommon(llvm::CachedHashStringRef(name), file, size, align,
+                     isPrivateExtern);
+  }
 
   Symbol *addDylib(StringRef name, DylibFile *file, bool isWeakDef, bool isTlv);
   Symbol *addDynamicLookup(StringRef name);
 
   Symbol *addLazyArchive(StringRef name, ArchiveFile *file,
                          const llvm::object::Archive::Symbol &sym);
-  Symbol *addLazyObject(StringRef name, InputFile &file);
+  Symbol *addLazyObject(llvm::CachedHashStringRef name, InputFile &file);
+  Symbol *addLazyObject(StringRef name, InputFile &file) {
+    return addLazyObject(llvm::CachedHashStringRef(name), file);
+  }
 
   Defined *addSynthetic(StringRef name, InputSection *, uint64_t value,
                         bool isPrivateExtern, bool includeInSymtab,
@@ -66,7 +88,11 @@ class SymbolTable {
   Symbol *find(StringRef name) { return find(llvm::CachedHashStringRef(name)); }
 
 private:
-  std::pair<Symbol *, bool> insert(StringRef name, const InputFile *);
+  std::pair<Symbol *, bool> insert(llvm::CachedHashStringRef name,
+                                   const InputFile *);
+  std::pair<Symbol *, bool> insert(StringRef name, const InputFile *file) {
+    return insert(llvm::CachedHashStringRef(name), file);
+  }
   llvm::DenseMap<llvm::CachedHashStringRef, int> symMap;
   std::vector<Symbol *> symVector;
 };



More information about the llvm-branch-commits mailing list