Issue 235704
Summary [clang] ADL repeatedly visits identical template arguments in a small nested type
Labels clang
Assignees
Reporter adamtheturtle
    Tested with official LLVM 23.1.4 (`7a941dfb2877a0b8cf30323fefa85ad019e0b0f5`) on Ubuntu 24.04.5, Linux AArch64.

Lookup should find `assoc::consume`; revisiting the same nested specialization through two identical template arguments should not multiply lookup cost.

```cpp
namespace assoc {
struct Leaf {};
template<class T, class U=T> struct Node {};
void consume(...);
}
void test(assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Leaf>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> value) { consume(value); }
```

Run from the [reproducer bundle](https://github.com/adamtheturtle/literalizer/blob/609a43270048ac5e733e14b320fc667f6cc5429e/ci/llvm-upstream/README.md):

```sh
timeout 10s clang++ -std=c++17 -fsyntax-only reproducers/adl.cpp
```

The unmodified release exceeded the ten-second limit (exit 124).
The same input completed in 0.025 seconds with the [candidate implementation](https://github.com/adamtheturtle/literalizer/blob/609a43270048ac5e733e14b320fc667f6cc5429e/ci/llvm-upstream/patches/adl.patch).
These are single sequential wall-clock samples, not statistical benchmarks.
Raw commands and outputs are in [measurements.json](https://github.com/adamtheturtle/literalizer/blob/609a43270048ac5e733e14b320fc667f6cc5429e/ci/llvm-upstream/measurements.json).

Each `Node<T, T>` has two paths to the same argument specialization.
Repeating those paths at every nesting level produces redundant associated-namespace traversal.

Candidate fix: Remember which specializations have had their template arguments visited, separately from transitive base-class traversal.
The regression test also checks a specialization appearing through a base and as a direct argument.

The isolated patch applies to LLVM main `fac4500420cdeed42f35537a26eea1e4e17badf0` and contains a regression test.
The new test and the downstream semantic controls were run with the patched 23.1.4 tools; a complete build of current LLVM main has not been performed.

Prepared with AI assistance.

<details>
<summary>Candidate patch, including regression test</summary>

```diff
diff --git a/clang/lib/Sema/SemaLookup.cpp b/clang/lib/Sema/SemaLookup.cpp
index 75cad0bb..d6f3b415 100644
--- a/clang/lib/Sema/SemaLookup.cpp
+++ b/clang/lib/Sema/SemaLookup.cpp
@@ -2906,6 +2906,10 @@ namespace {
       return ClassesTransitive.insert(RD);
     }

+    bool visitTemplateArguments(CXXRecordDecl *RD) {
+      return ClassesWithVisitedTemplateArguments.insert(RD);
+    }
+
     Sema &S;
 Sema::AssociatedNamespaceSet &Namespaces;
     Sema::AssociatedClassSet &Classes;
@@ -2913,6 +2917,7 @@ namespace {

   private:
 Sema::AssociatedClassSet ClassesTransitive;
+    Sema::AssociatedClassSet ClassesWithVisitedTemplateArguments;
   };
 } // end anonymous namespace

@@ -3089,9 +3094,11 @@ addAssociatedClassesAndNamespaces(AssociatedLookup &Result,
     // Add the associated namespace for this class.
 CollectEnclosingNamespace(Result.Namespaces, Ctx);

-    const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
-    for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
- addAssociatedClassesAndNamespaces(Result, TemplateArgs[I]);
+    if (Result.visitTemplateArguments(Class)) {
+      const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
+      for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
+ addAssociatedClassesAndNamespaces(Result, TemplateArgs[I]);
+    }
   }

 // Add the class itself. If we've already transitively visited this class,
diff --git a/clang/test/SemaCXX/adl-repeated-template-arguments.cpp b/clang/test/SemaCXX/adl-repeated-template-arguments.cpp
new file mode 100644
index 0000000000000..d29f748b4c8dbec51439a2b3532906396789bc17
--- /dev/null
+++ b/clang/test/SemaCXX/adl-repeated-template-arguments.cpp
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 -std=c++17 -fsyntax-only -verify %s
+// expected-no-diagnostics
+
+namespace assoc {
+struct Leaf {};
+template<class T, class U=T> struct Node {};
+void consume(...);
+}
+void test(assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Node<assoc::Leaf>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> value) { consume(value); }
+
+template<class T> struct Base {};
+namespace other { struct Tag {}; }
+struct Derived : Base<other::Tag> {};
+namespace other {
+constexpr int select(Derived, Base<Tag>) { return 7; }
+}
+static_assert(select(Derived{}, Base<other::Tag>{}) == 7);

```

</details>


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

Reply via email to