[clang] ed06b84 - Revert "[Clang] Reduce the size of Decl and classes derived from it" (#88654)

via cfe-commits cfe-commits at lists.llvm.org
Sun Apr 14 03:25:52 PDT 2024


Author: Nikolas Klauser
Date: 2024-04-14T12:25:49+02:00
New Revision: ed06b847d4e77d0b81fa6b095366bb070db57846

URL: https://github.com/llvm/llvm-project/commit/ed06b847d4e77d0b81fa6b095366bb070db57846
DIFF: https://github.com/llvm/llvm-project/commit/ed06b847d4e77d0b81fa6b095366bb070db57846.diff

LOG: Revert "[Clang] Reduce the size of Decl and classes derived from it" (#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.

Added: 
    

Modified: 
    clang/include/clang/AST/DeclBase.h
    clang/lib/AST/DeclBase.cpp
    clang/lib/Serialization/ASTReaderDecl.cpp

Removed: 
    


################################################################################
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