https://github.com/nikic created 
https://github.com/llvm/llvm-project/pull/237619

This method can only return true if there is a WeakImportAttr or 
AvailabilityAttr, so we can skip cases that don't have any attributes.

This avoids the call to canBeWeakImported(), which is fairly expensive due to 
the use of isThisDeclarationADefinition().

This improves compile-time for C++ code, by -0.4% for the clang build: 
https://llvm-compile-time-tracker.com/compare.php?from=1b886f3957be9797805466410f538ec42909f22b&to=7f34bb6019272a989571e66c0ec86fe9b06269dd&stat=instructions:u

Suggested by LLM. Original motivation was to reduce ASLR noise on 
beman-execution.

>From 9133720d0c62446fd879b4fef4f9c8650f0fb7bb Mon Sep 17 00:00:00 2001
From: Nikita Popov <[email protected]>
Date: Sat, 31 Oct 2026 20:29:34 +0100
Subject: [PATCH] [AST] Optimize isWeakImported() for decl without attributes
 (NFC)

This method can only return true if there is a WeakImportAttr or
AvailabilityAttr, so we can skip cases that don't have any attributes.

This avoids the call to canBeWeakImported(), which is fairly
expensive due to the use of isThisDeclarationADefinition().

This improves compile-time for C++ code, by -0.4% for the clang
build: 
https://llvm-compile-time-tracker.com/compare.php?from=1b886f3957be9797805466410f538ec42909f22b&to=7f34bb6019272a989571e66c0ec86fe9b06269dd&stat=instructions:u

Suggested by LLM.
---
 clang/lib/AST/DeclBase.cpp | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp
index bf2a8a01f7b9ca..a3ddcb3df01b64 100644
--- a/clang/lib/AST/DeclBase.cpp
+++ b/clang/lib/AST/DeclBase.cpp
@@ -874,11 +874,15 @@ bool Decl::canBeWeakImported(bool &IsDefinition) const {
 }
 
 bool Decl::isWeakImported() const {
+  const Decl *MostRecentDecl = getMostRecentDecl();
+  if (!MostRecentDecl->hasAttrs())
+    return false;
+
   bool IsDefinition;
   if (!canBeWeakImported(IsDefinition))
     return false;
 
-  for (const auto *A : getMostRecentDecl()->attrs()) {
+  for (const auto *A : MostRecentDecl->attrs()) {
     if (isa<WeakImportAttr>(A))
       return true;
 

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

Reply via email to