https://github.com/fmeum created 
https://github.com/llvm/llvm-project/pull/244428

When name lookup finds a declaration that isn't visible, findAcceptableDecl 
walks its redeclaration chain to find one that is. With explicit modules, a 
lookup can find many hidden declarations of the same entity: every module that 
includes the same header textually has its own copy of the header's 
declarations, and these copies are merged into one redeclaration chain. Walking 
the chain of N redeclarations from each of N copies takes O(N^2) time.

Once getAcceptableDeclSlow is called for a second declaration of the same 
entity, compute the result for all of its redeclarations in a single walk and 
cache it in the LookupResult. This doesn't change which declaration lookup 
finds.

With 120 explicit modules whose headers include the same seven C library 
headers, a file that doesn't import any of them and includes the same C library 
headers itself compiles in 1.5 s instead of 1.9 s. This pattern is common with 
Bazel's layering check and header modules, where every target's module copies 
the textual system headers it includes.

Most of the remaining cost is the deserialization of each module's copy and the 
per-copy work in Sema. Keeping only one copy per entity in 
`ASTReader::FindExternalVisibleDeclsByName`, generalizing the existing 
optimization for namespaces, brought the same file down to 1.2 s, but it 
changes which redeclaration lookup returns: diagnostics then point to other 
declarations in four tests under clang/test/Modules, and redeclarations with 
different default arguments would be collapsed. I'd be interested in opinions 
on whether there is an acceptable way to do that.

Assisted-by: Claude


>From 2281f9473963d17f64061ce5c960e2fd2b8dd222 Mon Sep 17 00:00:00 2001
From: Fabian Meumertzheim <[email protected]>
Date: Thu, 19 Nov 2026 23:57:22 +0100
Subject: [PATCH] [clang][Sema] Find acceptable redeclarations of an entity in
 one walk

When name lookup finds a declaration that isn't visible,
findAcceptableDecl walks its redeclaration chain to find one that is.
With explicit modules, a lookup can find many hidden declarations of the
same entity: every module that includes the same header textually has
its own copy of the header's declarations, and these copies are merged
into one redeclaration chain. Walking the chain of N redeclarations from
each of N copies takes O(N^2) time.

Once getAcceptableDeclSlow is called for a second declaration of the
same entity, compute the result for all of its redeclarations in a
single walk and cache it in the LookupResult. This doesn't change which
declaration lookup finds.

With 120 explicit modules whose headers include the same seven C library
headers, a file that doesn't import any of them and includes the same C
library headers itself compiles in 1.5 s instead of 1.9 s.

Assisted-by: Claude
---
 clang/include/clang/Sema/Lookup.h | 12 +++++++++
 clang/lib/Sema/SemaLookup.cpp     | 41 +++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)

diff --git a/clang/include/clang/Sema/Lookup.h 
b/clang/include/clang/Sema/Lookup.h
index 5478b96d535bde..c997217c7a18ff 100644
--- a/clang/include/clang/Sema/Lookup.h
+++ b/clang/include/clang/Sema/Lookup.h
@@ -25,6 +25,7 @@
 #include "clang/Basic/SourceLocation.h"
 #include "clang/Basic/Specifiers.h"
 #include "clang/Sema/Sema.h"
+#include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/MapVector.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/Casting.h"
@@ -611,6 +612,8 @@ class LookupResult {
     Paths = nullptr;
     NamingClass = nullptr;
     Shadowed = false;
+    HiddenEntities.clear();
+    AcceptableRedecls.clear();
   }
 
   /// Clears out any current state and re-initializes for a
@@ -827,6 +830,15 @@ class LookupResult {
 
   /// True if we're looking up a template-name.
   bool TemplateNameLookup = false;
+
+  /// The canonical declarations of the entities that getAcceptableDeclSlow
+  /// was called for, and whether it was called for more than one of their
+  /// declarations.
+  mutable llvm::DenseMap<const Decl *, bool> HiddenEntities;
+
+  /// The acceptable redeclaration of each redeclaration of the entities that
+  /// getAcceptableDeclSlow was called for more than once.
+  mutable llvm::DenseMap<const NamedDecl *, NamedDecl *> AcceptableRedecls;
 };
 
 /// Consumes visible declarations found when searching for
diff --git a/clang/lib/Sema/SemaLookup.cpp b/clang/lib/Sema/SemaLookup.cpp
index 5e061103437670..f47781e645d82b 100644
--- a/clang/lib/Sema/SemaLookup.cpp
+++ b/clang/lib/Sema/SemaLookup.cpp
@@ -303,6 +303,8 @@ static inline unsigned getIDNS(Sema::LookupNameKind 
NameKind,
 void LookupResult::configure() {
   IDNS = getIDNS(LookupKind, getSema().getLangOpts().CPlusPlus,
                  isForRedeclaration());
+  HiddenEntities.clear();
+  AcceptableRedecls.clear();
 
   // If we're looking for one of the allocation or deallocation
   // operators, make sure that the implicitly-declared new and delete
@@ -2150,6 +2152,45 @@ NamedDecl *LookupResult::getAcceptableDeclSlow(NamedDecl 
*D) const {
     return Acceptable;
   }
 
+  // A lookup can find many hidden declarations of the same entity, for example
+  // if several modules include the same header textually: each of them has a
+  // copy of its declarations, and these copies are merged into one
+  // redeclaration chain. Walking the chain from each of N such declarations
+  // takes O(N^2) time. So once a second declaration of an entity is looked up,
+  // compute the result for all of its redeclarations in a single walk.
+  auto [It, Inserted] =
+      HiddenEntities.try_emplace(D->getCanonicalDecl(), false);
+  if (Inserted)
+    return findAcceptableDecl(getSema(), D, IDNS);
+  if (!It->second) {
+    It->second = true;
+    // List the redeclarations in the order in which findAcceptableDecl walks
+    // them: from D to the first declaration, then from the most recent one
+    // back to D. Starting at the most recent one, the walk from the I-th
+    // redeclaration visits I + 1, ..., N - 1, 0, ..., I - 1.
+    SmallVector<NamedDecl *, 16> Redecls;
+    SmallVector<bool, 16> IsAcceptable;
+    for (Decl *RD : D->getMostRecentDecl()->redecls()) {
+      auto *ND = cast<NamedDecl>(RD);
+      Redecls.push_back(ND);
+      IsAcceptable.push_back(ND->isInIdentifierNamespace(IDNS) &&
+                             isAvailableForLookup(getSema(), ND));
+    }
+    // The first acceptable redeclaration after the I-th one, starting with the
+    // last one, after which the walk continues with the first one.
+    auto First = llvm::find(IsAcceptable, true);
+    NamedDecl *Next = First == IsAcceptable.end()
+                          ? nullptr
+                          : Redecls[First - IsAcceptable.begin()];
+    for (unsigned I = Redecls.size(); I-- > 0;) {
+      AcceptableRedecls[Redecls[I]] = Next == Redecls[I] ? nullptr : Next;
+      if (IsAcceptable[I])
+        Next = Redecls[I];
+    }
+  }
+  if (auto Cached = AcceptableRedecls.find(D);
+      Cached != AcceptableRedecls.end())
+    return Cached->second;
   return findAcceptableDecl(getSema(), D, IDNS);
 }
 

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to