[llvm-branch-commits] [clang] [Clang] Inline cached AST context lookup for declarations (NFC) (PR #223041)
Mehdi Amini via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Fri Sep 11 12:54:26 PDT 2026
https://github.com/joker-eph created https://github.com/llvm/llvm-project/pull/223041
Route declaration context queries through the cached DeclContext path. Define the common path inline to avoid walking to the translation unit and remove a hot out-of-line call.
CTMark O0 (3 samples): 29.239400 s -> 28.975433 s (-0.903%).
Impact on significant TUs in MLIR build time:
- `mlir/lib/RegisterAllDialects.cpp`: 2.4845% fewer retired instructions.
- `mlir/lib/Dialect/LLVMIR/IR/NVVMDialect.cpp`: 1.4241% fewer retired instructions.
Assisted-by: Codex
>From 3edcb8c335f3f319ad1eb88f8b5a14aa7d634ec1 Mon Sep 17 00:00:00 2001
From: Mehdi Amini <joker.eph at gmail.com>
Date: Thu, 10 Sep 2026 07:21:25 -0700
Subject: [PATCH] Inline cached AST context lookup for declarations
Route declaration context queries through the cached DeclContext path.
Define the common path inline to avoid walking to the translation unit
and remove a hot out-of-line call.
The following measurements used the original relationship-cache parent,
before its cache-invalidation fixes.
CTMark O0 (3 samples, CPU 6): 29.239400 s -> 28.975433 s (-0.903%).
Impact on significant TUs in MLIR build time:
- `mlir/lib/RegisterAllDialects.cpp`: 2.4845% fewer retired
instructions.
- `mlir/lib/Dialect/LLVMIR/IR/NVVMDialect.cpp`: 1.4241% fewer retired
instructions.
Assisted-by: Codex
---
clang/include/clang/AST/DeclBase.h | 8 ++++++++
clang/lib/AST/DeclBase.cpp | 4 ----
2 files changed, 8 insertions(+), 4 deletions(-)
diff --git a/clang/include/clang/AST/DeclBase.h b/clang/include/clang/AST/DeclBase.h
index 1a2b1c72a6159..d34f1c6151b09 100644
--- a/clang/include/clang/AST/DeclBase.h
+++ b/clang/include/clang/AST/DeclBase.h
@@ -2847,6 +2847,14 @@ inline bool Decl::isTemplateParameter() const {
getKind() == TemplateTemplateParm;
}
+inline ASTContext &Decl::getASTContext() const {
+ const DeclContext *DC = getDeclContext();
+ // The translation unit has no parent context and owns the AST context.
+ if (!DC)
+ DC = castToDeclContext(this);
+ return DC->getParentASTContext();
+}
+
// Specialization selected when ToTy is not a known subclass of DeclContext.
template <class ToTy,
bool IsKnownSubtype = ::std::is_base_of<DeclContext, ToTy>::value>
diff --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp
index 59ddc64cf30e0..33c924a7fcc9f 100644
--- a/clang/lib/AST/DeclBase.cpp
+++ b/clang/lib/AST/DeclBase.cpp
@@ -547,10 +547,6 @@ TranslationUnitDecl *Decl::getTranslationUnitDecl() {
return cast<TranslationUnitDecl>(DC);
}
-ASTContext &Decl::getASTContext() const {
- return getTranslationUnitDecl()->getASTContext();
-}
-
/// Helper to get the language options from the ASTContext.
/// Defined out of line to avoid depending on ASTContext.h.
const LangOptions &Decl::getLangOpts() const {
More information about the llvm-branch-commits
mailing list