[clang] Revert "[Clang] Reduce the size of Decl and classes derived from it" (PR #88654)
Nikolas Klauser via cfe-commits
cfe-commits at lists.llvm.org
Sun Apr 14 03:25:33 PDT 2024
https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/88654
Reverts llvm/llvm-project#87361
On 32 bit platforms there is only a single bit available in the `DeclCtx`, resulting in an assertion failure.
>From b243ca1d3616d1dd61b81e3a112707e27cd4c865 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Sun, 14 Apr 2024 12:23:21 +0200
Subject: [PATCH] Revert "[Clang] Reduce the size of Decl and classes derived
from it (#87361)"
This reverts commit c6f9c84e498ee05a812511ae969773ff166fd25e.
---
clang/include/clang/AST/DeclBase.h | 72 ++++++++---------------
clang/lib/AST/DeclBase.cpp | 31 +++-------
clang/lib/Serialization/ASTReaderDecl.cpp | 2 +-
3 files changed, 35 insertions(+), 70 deletions(-)
diff --git a/clang/include/clang/AST/DeclBase.h b/clang/include/clang/AST/DeclBase.h
index 4bee18767dd11b..858450926455c6 100644
--- a/clang/include/clang/AST/DeclBase.h
+++ b/clang/include/clang/AST/DeclBase.h
@@ -268,37 +268,17 @@ class alignas(8) Decl {
/// }
/// void A::f(); // SemanticDC == namespace 'A'
/// // LexicalDC == global namespace
+ llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx;
- // Compress the InvalidDecl and HasAttrs bits into DeclCtx to keep Decl below
- // 32 bytes in size
- llvm::PointerIntPair<
- llvm::PointerIntPair<llvm::PointerUnion<DeclContext *, MultipleDC *>, 1,
- bool>,
- 1, bool>
- DeclCtxWithInvalidDeclAndHasAttrs;
-
- bool isInSemaDC() const {
- return DeclCtxWithInvalidDeclAndHasAttrs.getPointer()
- .getPointer()
- .is<DeclContext *>();
- }
-
- bool isOutOfSemaDC() const {
- return DeclCtxWithInvalidDeclAndHasAttrs.getPointer()
- .getPointer()
- .is<MultipleDC *>();
- }
+ bool isInSemaDC() const { return DeclCtx.is<DeclContext*>(); }
+ bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); }
MultipleDC *getMultipleDC() const {
- return DeclCtxWithInvalidDeclAndHasAttrs.getPointer()
- .getPointer()
- .get<MultipleDC *>();
+ return DeclCtx.get<MultipleDC*>();
}
DeclContext *getSemanticDC() const {
- return DeclCtxWithInvalidDeclAndHasAttrs.getPointer()
- .getPointer()
- .get<DeclContext *>();
+ return DeclCtx.get<DeclContext*>();
}
/// Loc - The location of this decl.
@@ -308,6 +288,14 @@ class alignas(8) Decl {
LLVM_PREFERRED_TYPE(Kind)
unsigned DeclKind : 7;
+ /// InvalidDecl - This indicates a semantic error occurred.
+ LLVM_PREFERRED_TYPE(bool)
+ unsigned InvalidDecl : 1;
+
+ /// HasAttrs - This indicates whether the decl has attributes or not.
+ LLVM_PREFERRED_TYPE(bool)
+ unsigned HasAttrs : 1;
+
/// Implicit - Whether this declaration was implicitly generated by
/// the implementation rather than explicitly written by the user.
LLVM_PREFERRED_TYPE(bool)
@@ -405,22 +393,21 @@ class alignas(8) Decl {
protected:
Decl(Kind DK, DeclContext *DC, SourceLocation L)
: NextInContextAndBits(nullptr, getModuleOwnershipKindForChildOf(DC)),
- DeclCtxWithInvalidDeclAndHasAttrs({DC, false}, false), Loc(L),
- DeclKind(DK), Implicit(false), Used(false), Referenced(false),
+ DeclCtx(DC), Loc(L), DeclKind(DK), InvalidDecl(false), HasAttrs(false),
+ Implicit(false), Used(false), Referenced(false),
TopLevelDeclInObjCContainer(false), Access(AS_none), FromASTFile(0),
IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
CacheValidAndLinkage(llvm::to_underlying(Linkage::Invalid)) {
- if (StatisticsEnabled)
- add(DK);
+ if (StatisticsEnabled) add(DK);
}
Decl(Kind DK, EmptyShell Empty)
- : DeclKind(DK), Implicit(false), Used(false), Referenced(false),
- TopLevelDeclInObjCContainer(false), Access(AS_none), FromASTFile(0),
+ : DeclKind(DK), InvalidDecl(false), HasAttrs(false), Implicit(false),
+ Used(false), Referenced(false), TopLevelDeclInObjCContainer(false),
+ Access(AS_none), FromASTFile(0),
IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
CacheValidAndLinkage(llvm::to_underlying(Linkage::Invalid)) {
- if (StatisticsEnabled)
- add(DK);
+ if (StatisticsEnabled) add(DK);
}
virtual ~Decl();
@@ -533,9 +520,7 @@ class alignas(8) Decl {
return AccessSpecifier(Access);
}
- bool hasAttrs() const {
- return DeclCtxWithInvalidDeclAndHasAttrs.getPointer().getInt();
- }
+ bool hasAttrs() const { return HasAttrs; }
void setAttrs(const AttrVec& Attrs) {
return setAttrsImpl(Attrs, getASTContext());
@@ -564,17 +549,13 @@ class alignas(8) Decl {
}
template <typename... Ts> void dropAttrs() {
- if (!hasAttrs())
- return;
+ if (!HasAttrs) return;
AttrVec &Vec = getAttrs();
llvm::erase_if(Vec, [](Attr *A) { return isa<Ts...>(A); });
- if (Vec.empty()) {
- auto InnerPtr = DeclCtxWithInvalidDeclAndHasAttrs.getPointer();
- InnerPtr.setInt(false);
- DeclCtxWithInvalidDeclAndHasAttrs.setPointer(InnerPtr);
- }
+ if (Vec.empty())
+ HasAttrs = false;
}
template <typename T> void dropAttr() { dropAttrs<T>(); }
@@ -609,10 +590,7 @@ class alignas(8) Decl {
/// setInvalidDecl - Indicates the Decl had a semantic error. This
/// allows for graceful error recovery.
void setInvalidDecl(bool Invalid = true);
-
- bool isInvalidDecl() const {
- return DeclCtxWithInvalidDeclAndHasAttrs.getInt();
- }
+ bool isInvalidDecl() const { return (bool) InvalidDecl; }
/// isImplicit - Indicates whether the declaration was implicitly
/// generated by the implementation. If false, this declaration
diff --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp
index 8c83c711d882d9..66a727d9dd0c39 100644
--- a/clang/lib/AST/DeclBase.cpp
+++ b/clang/lib/AST/DeclBase.cpp
@@ -52,8 +52,6 @@
using namespace clang;
-static_assert(sizeof(Decl) <= 32, "Decl grew beyond 32 bytes!");
-
//===----------------------------------------------------------------------===//
// Statistics
//===----------------------------------------------------------------------===//
@@ -132,7 +130,7 @@ const char *Decl::getDeclKindName() const {
}
void Decl::setInvalidDecl(bool Invalid) {
- DeclCtxWithInvalidDeclAndHasAttrs.setInt(Invalid);
+ InvalidDecl = Invalid;
assert(!isa<TagDecl>(this) || !cast<TagDecl>(this)->isCompleteDefinition());
if (!Invalid) {
return;
@@ -336,9 +334,7 @@ void PrettyStackTraceDecl::print(raw_ostream &OS) const {
Decl::~Decl() = default;
void Decl::setDeclContext(DeclContext *DC) {
- auto InnerPtr = DeclCtxWithInvalidDeclAndHasAttrs.getPointer();
- InnerPtr.setPointer(DC);
- DeclCtxWithInvalidDeclAndHasAttrs.setPointer(InnerPtr);
+ DeclCtx = DC;
}
void Decl::setLexicalDeclContext(DeclContext *DC) {
@@ -368,16 +364,12 @@ void Decl::setLexicalDeclContext(DeclContext *DC) {
void Decl::setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
ASTContext &Ctx) {
if (SemaDC == LexicalDC) {
- auto InnerPtr = DeclCtxWithInvalidDeclAndHasAttrs.getPointer();
- InnerPtr.setPointer(SemaDC);
- DeclCtxWithInvalidDeclAndHasAttrs.setPointer(InnerPtr);
+ DeclCtx = SemaDC;
} else {
auto *MDC = new (Ctx) Decl::MultipleDC();
MDC->SemanticDC = SemaDC;
MDC->LexicalDC = LexicalDC;
- auto InnerPtr = DeclCtxWithInvalidDeclAndHasAttrs.getPointer();
- InnerPtr.setPointer(MDC);
- DeclCtxWithInvalidDeclAndHasAttrs.setPointer(InnerPtr);
+ DeclCtx = MDC;
}
}
@@ -964,24 +956,19 @@ unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
}
void Decl::setAttrsImpl(const AttrVec &attrs, ASTContext &Ctx) {
- assert(!hasAttrs() && "Decl already contains attrs.");
+ assert(!HasAttrs && "Decl already contains attrs.");
AttrVec &AttrBlank = Ctx.getDeclAttrs(this);
assert(AttrBlank.empty() && "HasAttrs was wrong?");
AttrBlank = attrs;
- auto InnerPtr = DeclCtxWithInvalidDeclAndHasAttrs.getPointer();
- InnerPtr.setInt(true);
- DeclCtxWithInvalidDeclAndHasAttrs.setPointer(InnerPtr);
+ HasAttrs = true;
}
void Decl::dropAttrs() {
- if (!hasAttrs())
- return;
+ if (!HasAttrs) return;
- auto InnerPtr = DeclCtxWithInvalidDeclAndHasAttrs.getPointer();
- InnerPtr.setInt(false);
- DeclCtxWithInvalidDeclAndHasAttrs.setPointer(InnerPtr);
+ HasAttrs = false;
getASTContext().eraseDeclAttrs(this);
}
@@ -1009,7 +996,7 @@ void Decl::addAttr(Attr *A) {
}
const AttrVec &Decl::getAttrs() const {
- assert(hasAttrs() && "No attrs to get!");
+ assert(HasAttrs && "No attrs to get!");
return getASTContext().getDeclAttrs(this);
}
diff --git a/clang/lib/Serialization/ASTReaderDecl.cpp b/clang/lib/Serialization/ASTReaderDecl.cpp
index c5dd89e1c8a4db..ff72fc30c718f8 100644
--- a/clang/lib/Serialization/ASTReaderDecl.cpp
+++ b/clang/lib/Serialization/ASTReaderDecl.cpp
@@ -588,7 +588,7 @@ void ASTDeclReader::VisitDecl(Decl *D) {
bool HasStandaloneLexicalDC = DeclBits.getNextBit();
bool HasAttrs = DeclBits.getNextBit();
D->setTopLevelDeclInObjCContainer(DeclBits.getNextBit());
- D->DeclCtxWithInvalidDeclAndHasAttrs.setInt(DeclBits.getNextBit());
+ D->InvalidDecl = DeclBits.getNextBit();
D->FromASTFile = true;
if (D->isTemplateParameter() || D->isTemplateParameterPack() ||
More information about the cfe-commits
mailing list