[llvm-branch-commits] [clang] Cache DeclContext owning declarations (PR #241232)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Nov 11 03:20:54 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Pengcheng Wang (wangpc-pp)
<details>
<summary>Changes</summary>
Store the owning Decl pointer when constructing each DeclContext.
Use it for parent traversal and generic casts, avoiding a kind switch
for every conversion.
Three interleaved CTMark pairs reduce median compile time by 0.3935%
and geomean by 0.4199%.
Assisted-by: TRAE CLI (GPT-6)
---
Full diff: https://github.com/llvm/llvm-project/pull/241232.diff
10 Files Affected:
- (modified) clang/include/clang/AST/Decl.h (+5-4)
- (modified) clang/include/clang/AST/DeclBase.h (+12-7)
- (modified) clang/include/clang/AST/DeclCXX.h (+2-1)
- (modified) clang/include/clang/AST/DeclOpenMP.h (+1-1)
- (modified) clang/lib/AST/Decl.cpp (+10-8)
- (modified) clang/lib/AST/DeclBase.cpp (+3-12)
- (modified) clang/lib/AST/DeclCXX.cpp (+2-2)
- (modified) clang/lib/AST/DeclObjC.cpp (+3-3)
- (modified) clang/lib/AST/DeclOpenMP.cpp (+1-1)
- (modified) clang/lib/AST/DeclTemplate.cpp (+1-1)
``````````diff
diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h
index 3048e14a2da1f..821d498e6afe4 100644
--- a/clang/include/clang/AST/Decl.h
+++ b/clang/include/clang/AST/Decl.h
@@ -248,8 +248,8 @@ class PragmaDetectMismatchDecl final
/// lookup in the extern "C" context.
class ExternCContextDecl : public Decl, public DeclContext {
explicit ExternCContextDecl(TranslationUnitDecl *TU)
- : Decl(ExternCContext, TU, SourceLocation()),
- DeclContext(ExternCContext) {}
+ : Decl(ExternCContext, TU, SourceLocation()),
+ DeclContext(ExternCContext, this) {}
virtual void anchor();
@@ -4823,7 +4823,8 @@ class TopLevelStmtDecl : public Decl, public DeclContext {
unsigned Ordinal = 0;
TopLevelStmtDecl(DeclContext *DC, SourceLocation L, Stmt *S)
- : Decl(TopLevelStmt, DC, L), DeclContext(TopLevelStmt), Statement(S) {}
+ : Decl(TopLevelStmt, DC, L), DeclContext(TopLevelStmt, this),
+ Statement(S) {}
virtual void anchor();
@@ -5323,7 +5324,7 @@ class ExportDecl final : public Decl, public DeclContext {
SourceLocation RBraceLoc;
ExportDecl(DeclContext *DC, SourceLocation ExportLoc)
- : Decl(Export, DC, ExportLoc), DeclContext(Export),
+ : Decl(Export, DC, ExportLoc), DeclContext(Export, this),
RBraceLoc(SourceLocation()) {}
public:
diff --git a/clang/include/clang/AST/DeclBase.h b/clang/include/clang/AST/DeclBase.h
index 317486e9c4ec6..5b10d1a0d807c 100644
--- a/clang/include/clang/AST/DeclBase.h
+++ b/clang/include/clang/AST/DeclBase.h
@@ -2083,13 +2083,16 @@ class DeclContext {
/// another pointer.
mutable Decl *LastDecl = nullptr;
+ /// The declaration corresponding to this context.
+ Decl *const CorrespondingDecl;
+
/// Build up a chain of declarations.
///
/// \returns the first/last pair of declarations.
static std::pair<Decl *, Decl *>
BuildDeclChain(ArrayRef<Decl*> Decls, bool FieldsAlreadyLoaded);
- DeclContext(Decl::Kind K);
+ DeclContext(Decl::Kind K, Decl *D);
public:
~DeclContext();
@@ -2104,10 +2107,12 @@ class DeclContext {
const char *getDeclKindName() const;
+ /// Return the declaration containing this context.
+ Decl *getAsDecl() { return CorrespondingDecl; }
+ const Decl *getAsDecl() const { return CorrespondingDecl; }
+
/// getParent - Returns the containing DeclContext.
- DeclContext *getParent() {
- return cast<Decl>(this)->getDeclContext();
- }
+ DeclContext *getParent() { return getAsDecl()->getDeclContext(); }
const DeclContext *getParent() const {
return const_cast<DeclContext*>(this)->getParent();
}
@@ -2122,7 +2127,7 @@ class DeclContext {
/// // getLexicalParent() == translation unit
///
DeclContext *getLexicalParent() {
- return cast<Decl>(this)->getLexicalDeclContext();
+ return getAsDecl()->getLexicalDeclContext();
}
const DeclContext *getLexicalParent() const {
return const_cast<DeclContext*>(this)->getLexicalParent();
@@ -2814,11 +2819,11 @@ template <class ToTy,
bool IsKnownSubtype = ::std::is_base_of<DeclContext, ToTy>::value>
struct cast_convert_decl_context {
static const ToTy *doit(const DeclContext *Val) {
- return static_cast<const ToTy*>(Decl::castFromDeclContext(Val));
+ return static_cast<const ToTy *>(Val->getAsDecl());
}
static ToTy *doit(DeclContext *Val) {
- return static_cast<ToTy*>(Decl::castFromDeclContext(Val));
+ return static_cast<ToTy *>(Val->getAsDecl());
}
};
diff --git a/clang/include/clang/AST/DeclCXX.h b/clang/include/clang/AST/DeclCXX.h
index ddb64fe0a40c3..8a10d97f532e9 100644
--- a/clang/include/clang/AST/DeclCXX.h
+++ b/clang/include/clang/AST/DeclCXX.h
@@ -2121,7 +2121,8 @@ class CXXDeductionGuideDecl : public FunctionDecl {
/// template argument list imposed by the compound requirement.
class RequiresExprBodyDecl : public Decl, public DeclContext {
RequiresExprBodyDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc)
- : Decl(RequiresExprBody, DC, StartLoc), DeclContext(RequiresExprBody) {}
+ : Decl(RequiresExprBody, DC, StartLoc),
+ DeclContext(RequiresExprBody, this) {}
public:
friend class ASTDeclReader;
diff --git a/clang/include/clang/AST/DeclOpenMP.h b/clang/include/clang/AST/DeclOpenMP.h
index 06414cef6baf3..c76f590fec2c1 100644
--- a/clang/include/clang/AST/DeclOpenMP.h
+++ b/clang/include/clang/AST/DeclOpenMP.h
@@ -365,7 +365,7 @@ class OMPDeclareMapperDecl final : public OMPDeclarativeDirective<ValueDecl>,
QualType Ty, DeclarationName VarName,
OMPDeclareMapperDecl *PrevDeclInScope)
: OMPDeclarativeDirective<ValueDecl>(OMPDeclareMapper, DC, L, Name, Ty),
- DeclContext(OMPDeclareMapper), VarName(VarName),
+ DeclContext(OMPDeclareMapper, this), VarName(VarName),
PrevDeclInScope(PrevDeclInScope) {}
void setPrevDeclInScope(OMPDeclareMapperDecl *Prev) {
diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index 72e082e9c969e..899b02b514d1f 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -104,7 +104,7 @@ bool Decl::isOutOfLine() const {
TranslationUnitDecl::TranslationUnitDecl(ASTContext &ctx)
: Decl(TranslationUnit, nullptr, SourceLocation()),
- DeclContext(TranslationUnit), redeclarable_base(ctx), Ctx(ctx) {}
+ DeclContext(TranslationUnit, this), redeclarable_base(ctx), Ctx(ctx) {}
//===----------------------------------------------------------------------===//
// NamedDecl Implementation
@@ -3091,7 +3091,7 @@ FunctionDecl::FunctionDecl(Kind DK, ASTContext &C, DeclContext *DC,
const AssociatedConstraint &TrailingRequiresClause)
: DeclaratorDecl(DK, DC, NameInfo.getLoc(), NameInfo.getName(), T, TInfo,
StartLoc),
- DeclContext(DK), redeclarable_base(C), Body(), ODRHash(0),
+ DeclContext(DK, this), redeclarable_base(C), Body(), ODRHash(0),
EndRangeLoc(NameInfo.getEndLoc()), DNLoc(NameInfo.getInfo()) {
assert(T.isNull() || T->isFunctionType());
FunctionDeclBits.SClass = S;
@@ -4980,7 +4980,8 @@ const FieldDecl *FieldDecl::findCountedByField() const {
TagDecl::TagDecl(Kind DK, TagKind TK, const ASTContext &C, DeclContext *DC,
SourceLocation L, IdentifierInfo *Id, TagDecl *PrevDecl,
SourceLocation StartL)
- : TypeDecl(DK, DC, L, Id, StartL), DeclContext(DK), redeclarable_base(C),
+ : TypeDecl(DK, DC, L, Id, StartL), DeclContext(DK, this),
+ redeclarable_base(C),
TypedefNameDeclOrQualifier((TypedefNameDecl *)nullptr) {
assert((DK != Enum || TK == TagTypeKind::Enum) &&
"EnumDecl not matched with TagTypeKind::Enum");
@@ -5542,7 +5543,7 @@ unsigned RecordDecl::getODRHash() {
//===----------------------------------------------------------------------===//
BlockDecl::BlockDecl(DeclContext *DC, SourceLocation CaretLoc)
- : Decl(Block, DC, CaretLoc), DeclContext(Block) {
+ : Decl(Block, DC, CaretLoc), DeclContext(Block, this) {
setIsVariadic(false);
setCapturesCXXThis(false);
setBlockMissingReturnType(true);
@@ -5765,7 +5766,7 @@ BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) {
OutlinedFunctionDecl::OutlinedFunctionDecl(DeclContext *DC, unsigned NumParams)
: Decl(OutlinedFunction, DC, SourceLocation()),
- DeclContext(OutlinedFunction), NumParams(NumParams),
+ DeclContext(OutlinedFunction, this), NumParams(NumParams),
BodyAndNothrow(nullptr, false) {}
OutlinedFunctionDecl *OutlinedFunctionDecl::Create(ASTContext &C,
@@ -5793,7 +5794,7 @@ void OutlinedFunctionDecl::setNothrow(bool Nothrow) {
}
CapturedDecl::CapturedDecl(DeclContext *DC, unsigned NumParams)
- : Decl(Captured, DC, SourceLocation()), DeclContext(Captured),
+ : Decl(Captured, DC, SourceLocation()), DeclContext(Captured, this),
NumParams(NumParams), ContextParam(0), BodyAndNothrow(nullptr, false) {}
CapturedDecl *CapturedDecl::Create(ASTContext &C, DeclContext *DC,
@@ -6014,8 +6015,9 @@ HLSLBufferDecl::HLSLBufferDecl(DeclContext *DC, bool CBuffer,
SourceLocation KwLoc, IdentifierInfo *ID,
SourceLocation IDLoc, SourceLocation LBrace)
: NamedDecl(Decl::Kind::HLSLBuffer, DC, IDLoc, DeclarationName(ID)),
- DeclContext(Decl::Kind::HLSLBuffer), LBraceLoc(LBrace), KwLoc(KwLoc),
- IsCBuffer(CBuffer), HasValidPackoffset(false), LayoutStruct(nullptr) {}
+ DeclContext(Decl::Kind::HLSLBuffer, this), LBraceLoc(LBrace),
+ KwLoc(KwLoc), IsCBuffer(CBuffer), HasValidPackoffset(false),
+ LayoutStruct(nullptr) {}
HLSLBufferDecl *HLSLBufferDecl::Create(ASTContext &C,
DeclContext *LexicalParent, bool CBuffer,
diff --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp
index aac5beec9a741..986c1ed807ef5 100644
--- a/clang/lib/AST/DeclBase.cpp
+++ b/clang/lib/AST/DeclBase.cpp
@@ -1083,17 +1083,8 @@ const AttrVec &Decl::getAttrs() const {
return *Attrs;
}
-Decl *Decl::castFromDeclContext (const DeclContext *D) {
- Decl::Kind DK = D->getDeclKind();
- switch (DK) {
-#define DECL(NAME, BASE)
-#define DECL_CONTEXT(NAME) \
- case Decl::NAME: \
- return static_cast<NAME##Decl *>(const_cast<DeclContext *>(D));
-#include "clang/AST/DeclNodes.inc"
- default:
- llvm_unreachable("a decl that inherits DeclContext isn't handled");
- }
+Decl *Decl::castFromDeclContext(const DeclContext *D) {
+ return const_cast<Decl *>(D->getAsDecl());
}
DeclContext *Decl::castToDeclContext(const Decl *D) {
@@ -1312,7 +1303,7 @@ Decl *DeclContext::getNonClosureAncestor() {
// DeclContext Implementation
//===----------------------------------------------------------------------===//
-DeclContext::DeclContext(Decl::Kind K) {
+DeclContext::DeclContext(Decl::Kind K, Decl *D) : CorrespondingDecl(D) {
DeclContextBits.DeclKind = K;
setHasExternalLexicalStorage(false);
setHasExternalVisibleStorage(false);
diff --git a/clang/lib/AST/DeclCXX.cpp b/clang/lib/AST/DeclCXX.cpp
index ac468f6879f7b..1f96c3803052c 100644
--- a/clang/lib/AST/DeclCXX.cpp
+++ b/clang/lib/AST/DeclCXX.cpp
@@ -3302,7 +3302,7 @@ bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
LinkageSpecDecl::LinkageSpecDecl(DeclContext *DC, SourceLocation ExternLoc,
SourceLocation LangLoc,
LinkageSpecLanguageIDs lang, bool HasBraces)
- : Decl(LinkageSpec, DC, LangLoc), DeclContext(LinkageSpec),
+ : Decl(LinkageSpec, DC, LangLoc), DeclContext(LinkageSpec, this),
ExternLoc(ExternLoc), RBraceLoc(SourceLocation()) {
setLanguage(lang);
LinkageSpecDeclBits.HasBraces = HasBraces;
@@ -3364,7 +3364,7 @@ NamespaceDecl::NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline,
SourceLocation StartLoc, SourceLocation IdLoc,
IdentifierInfo *Id, NamespaceDecl *PrevDecl,
bool Nested)
- : NamespaceBaseDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
+ : NamespaceBaseDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace, this),
redeclarable_base(C), LocStart(StartLoc) {
setInline(Inline);
setNested(Nested);
diff --git a/clang/lib/AST/DeclObjC.cpp b/clang/lib/AST/DeclObjC.cpp
index 1adf6c9e048a9..06d02c64a14f2 100644
--- a/clang/lib/AST/DeclObjC.cpp
+++ b/clang/lib/AST/DeclObjC.cpp
@@ -66,7 +66,7 @@ ObjCContainerDecl::ObjCContainerDecl(Kind DK, DeclContext *DC,
const IdentifierInfo *Id,
SourceLocation nameLoc,
SourceLocation atStartLoc)
- : NamedDecl(DK, DC, nameLoc, Id), DeclContext(DK) {
+ : NamedDecl(DK, DC, nameLoc, Id), DeclContext(DK, this) {
setAtStartLoc(atStartLoc);
}
@@ -823,8 +823,8 @@ ObjCMethodDecl::ObjCMethodDecl(
bool isSynthesizedAccessorStub, bool isImplicitlyDeclared, bool isDefined,
ObjCImplementationControl impControl, bool HasRelatedResultType)
: NamedDecl(ObjCMethod, contextDecl, beginLoc, SelInfo),
- DeclContext(ObjCMethod), MethodDeclType(T), ReturnTInfo(ReturnTInfo),
- DeclEndLoc(endLoc) {
+ DeclContext(ObjCMethod, this), MethodDeclType(T),
+ ReturnTInfo(ReturnTInfo), DeclEndLoc(endLoc) {
// Initialized the bits stored in DeclContext.
ObjCMethodDeclBits.Family =
diff --git a/clang/lib/AST/DeclOpenMP.cpp b/clang/lib/AST/DeclOpenMP.cpp
index ef08a1c30042f..0a59b070023fe 100644
--- a/clang/lib/AST/DeclOpenMP.cpp
+++ b/clang/lib/AST/DeclOpenMP.cpp
@@ -132,7 +132,7 @@ OMPRequiresDecl *OMPRequiresDecl::CreateDeserialized(ASTContext &C,
OMPDeclareReductionDecl::OMPDeclareReductionDecl(
Kind DK, DeclContext *DC, SourceLocation L, DeclarationName Name,
QualType Ty, OMPDeclareReductionDecl *PrevDeclInScope)
- : ValueDecl(DK, DC, L, Name, Ty), DeclContext(DK), Combiner(nullptr),
+ : ValueDecl(DK, DC, L, Name, Ty), DeclContext(DK, this), Combiner(nullptr),
PrevDeclInScope(PrevDeclInScope) {
setInitializer(nullptr, OMPDeclareReductionInitKind::Call);
}
diff --git a/clang/lib/AST/DeclTemplate.cpp b/clang/lib/AST/DeclTemplate.cpp
index abcd035740916..e8f1dd43562b3 100644
--- a/clang/lib/AST/DeclTemplate.cpp
+++ b/clang/lib/AST/DeclTemplate.cpp
@@ -1961,7 +1961,7 @@ SourceRange ExplicitInstantiationDecl::getSourceRange() const {
CXXExpansionStmtDecl::CXXExpansionStmtDecl(DeclContext *DC, SourceLocation Loc,
NonTypeTemplateParmDecl *NTTP)
- : Decl(CXXExpansionStmt, DC, Loc), DeclContext(CXXExpansionStmt),
+ : Decl(CXXExpansionStmt, DC, Loc), DeclContext(CXXExpansionStmt, this),
IndexNTTP(NTTP) {}
CXXExpansionStmtDecl *
``````````
</details>
https://github.com/llvm/llvm-project/pull/241232
More information about the llvm-branch-commits
mailing list