[clang] [AST] Optimize isWeakImported() for decl without attributes (NFC) (PR #237619)

via cfe-commits cfe-commits at lists.llvm.org
Sat Oct 31 12:37:34 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Nikita Popov (nikic)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/237619.diff


1 Files Affected:

- (modified) clang/lib/AST/DeclBase.cpp (+5-1) 


``````````diff
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;
 

``````````

</details>


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


More information about the cfe-commits mailing list