Skip to content

Commit ed06b84

Browse files
authored
Revert "[Clang] Reduce the size of Decl and classes derived from it" (llvm#88654)
Reverts llvm#87361 On 32 bit platforms there is only a single bit available in the `DeclCtx`, resulting in an assertion failure.
1 parent d48d6ba commit ed06b84

File tree

3 files changed

+35
-70
lines changed

3 files changed

+35
-70
lines changed

clang/include/clang/AST/DeclBase.h

Lines changed: 25 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -268,37 +268,17 @@ class alignas(8) Decl {
268268
/// }
269269
/// void A::f(); // SemanticDC == namespace 'A'
270270
/// // LexicalDC == global namespace
271+
llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx;
271272

272-
// Compress the InvalidDecl and HasAttrs bits into DeclCtx to keep Decl below
273-
// 32 bytes in size
274-
llvm::PointerIntPair<
275-
llvm::PointerIntPair<llvm::PointerUnion<DeclContext *, MultipleDC *>, 1,
276-
bool>,
277-
1, bool>
278-
DeclCtxWithInvalidDeclAndHasAttrs;
279-
280-
bool isInSemaDC() const {
281-
return DeclCtxWithInvalidDeclAndHasAttrs.getPointer()
282-
.getPointer()
283-
.is<DeclContext *>();
284-
}
285-
286-
bool isOutOfSemaDC() const {
287-
return DeclCtxWithInvalidDeclAndHasAttrs.getPointer()
288-
.getPointer()
289-
.is<MultipleDC *>();
290-
}
273+
bool isInSemaDC() const { return DeclCtx.is<DeclContext*>(); }
274+
bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); }
291275

292276
MultipleDC *getMultipleDC() const {
293-
return DeclCtxWithInvalidDeclAndHasAttrs.getPointer()
294-
.getPointer()
295-
.get<MultipleDC *>();
277+
return DeclCtx.get<MultipleDC*>();
296278
}
297279

298280
DeclContext *getSemanticDC() const {
299-
return DeclCtxWithInvalidDeclAndHasAttrs.getPointer()
300-
.getPointer()
301-
.get<DeclContext *>();
281+
return DeclCtx.get<DeclContext*>();
302282
}
303283

304284
/// Loc - The location of this decl.
@@ -308,6 +288,14 @@ class alignas(8) Decl {
308288
LLVM_PREFERRED_TYPE(Kind)
309289
unsigned DeclKind : 7;
310290

291+
/// InvalidDecl - This indicates a semantic error occurred.
292+
LLVM_PREFERRED_TYPE(bool)
293+
unsigned InvalidDecl : 1;
294+
295+
/// HasAttrs - This indicates whether the decl has attributes or not.
296+
LLVM_PREFERRED_TYPE(bool)
297+
unsigned HasAttrs : 1;
298+
311299
/// Implicit - Whether this declaration was implicitly generated by
312300
/// the implementation rather than explicitly written by the user.
313301
LLVM_PREFERRED_TYPE(bool)
@@ -405,22 +393,21 @@ class alignas(8) Decl {
405393
protected:
406394
Decl(Kind DK, DeclContext *DC, SourceLocation L)
407395
: NextInContextAndBits(nullptr, getModuleOwnershipKindForChildOf(DC)),
408-
DeclCtxWithInvalidDeclAndHasAttrs({DC, false}, false), Loc(L),
409-
DeclKind(DK), Implicit(false), Used(false), Referenced(false),
396+
DeclCtx(DC), Loc(L), DeclKind(DK), InvalidDecl(false), HasAttrs(false),
397+
Implicit(false), Used(false), Referenced(false),
410398
TopLevelDeclInObjCContainer(false), Access(AS_none), FromASTFile(0),
411399
IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
412400
CacheValidAndLinkage(llvm::to_underlying(Linkage::Invalid)) {
413-
if (StatisticsEnabled)
414-
add(DK);
401+
if (StatisticsEnabled) add(DK);
415402
}
416403

417404
Decl(Kind DK, EmptyShell Empty)
418-
: DeclKind(DK), Implicit(false), Used(false), Referenced(false),
419-
TopLevelDeclInObjCContainer(false), Access(AS_none), FromASTFile(0),
405+
: DeclKind(DK), InvalidDecl(false), HasAttrs(false), Implicit(false),
406+
Used(false), Referenced(false), TopLevelDeclInObjCContainer(false),
407+
Access(AS_none), FromASTFile(0),
420408
IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
421409
CacheValidAndLinkage(llvm::to_underlying(Linkage::Invalid)) {
422-
if (StatisticsEnabled)
423-
add(DK);
410+
if (StatisticsEnabled) add(DK);
424411
}
425412

426413
virtual ~Decl();
@@ -533,9 +520,7 @@ class alignas(8) Decl {
533520
return AccessSpecifier(Access);
534521
}
535522

536-
bool hasAttrs() const {
537-
return DeclCtxWithInvalidDeclAndHasAttrs.getPointer().getInt();
538-
}
523+
bool hasAttrs() const { return HasAttrs; }
539524

540525
void setAttrs(const AttrVec& Attrs) {
541526
return setAttrsImpl(Attrs, getASTContext());
@@ -564,17 +549,13 @@ class alignas(8) Decl {
564549
}
565550

566551
template <typename... Ts> void dropAttrs() {
567-
if (!hasAttrs())
568-
return;
552+
if (!HasAttrs) return;
569553

570554
AttrVec &Vec = getAttrs();
571555
llvm::erase_if(Vec, [](Attr *A) { return isa<Ts...>(A); });
572556

573-
if (Vec.empty()) {
574-
auto InnerPtr = DeclCtxWithInvalidDeclAndHasAttrs.getPointer();
575-
InnerPtr.setInt(false);
576-
DeclCtxWithInvalidDeclAndHasAttrs.setPointer(InnerPtr);
577-
}
557+
if (Vec.empty())
558+
HasAttrs = false;
578559
}
579560

580561
template <typename T> void dropAttr() { dropAttrs<T>(); }
@@ -609,10 +590,7 @@ class alignas(8) Decl {
609590
/// setInvalidDecl - Indicates the Decl had a semantic error. This
610591
/// allows for graceful error recovery.
611592
void setInvalidDecl(bool Invalid = true);
612-
613-
bool isInvalidDecl() const {
614-
return DeclCtxWithInvalidDeclAndHasAttrs.getInt();
615-
}
593+
bool isInvalidDecl() const { return (bool) InvalidDecl; }
616594

617595
/// isImplicit - Indicates whether the declaration was implicitly
618596
/// generated by the implementation. If false, this declaration

clang/lib/AST/DeclBase.cpp

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@
5252

5353
using namespace clang;
5454

55-
static_assert(sizeof(Decl) <= 32, "Decl grew beyond 32 bytes!");
56-
5755
//===----------------------------------------------------------------------===//
5856
// Statistics
5957
//===----------------------------------------------------------------------===//
@@ -132,7 +130,7 @@ const char *Decl::getDeclKindName() const {
132130
}
133131

134132
void Decl::setInvalidDecl(bool Invalid) {
135-
DeclCtxWithInvalidDeclAndHasAttrs.setInt(Invalid);
133+
InvalidDecl = Invalid;
136134
assert(!isa<TagDecl>(this) || !cast<TagDecl>(this)->isCompleteDefinition());
137135
if (!Invalid) {
138136
return;
@@ -336,9 +334,7 @@ void PrettyStackTraceDecl::print(raw_ostream &OS) const {
336334
Decl::~Decl() = default;
337335

338336
void Decl::setDeclContext(DeclContext *DC) {
339-
auto InnerPtr = DeclCtxWithInvalidDeclAndHasAttrs.getPointer();
340-
InnerPtr.setPointer(DC);
341-
DeclCtxWithInvalidDeclAndHasAttrs.setPointer(InnerPtr);
337+
DeclCtx = DC;
342338
}
343339

344340
void Decl::setLexicalDeclContext(DeclContext *DC) {
@@ -368,16 +364,12 @@ void Decl::setLexicalDeclContext(DeclContext *DC) {
368364
void Decl::setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
369365
ASTContext &Ctx) {
370366
if (SemaDC == LexicalDC) {
371-
auto InnerPtr = DeclCtxWithInvalidDeclAndHasAttrs.getPointer();
372-
InnerPtr.setPointer(SemaDC);
373-
DeclCtxWithInvalidDeclAndHasAttrs.setPointer(InnerPtr);
367+
DeclCtx = SemaDC;
374368
} else {
375369
auto *MDC = new (Ctx) Decl::MultipleDC();
376370
MDC->SemanticDC = SemaDC;
377371
MDC->LexicalDC = LexicalDC;
378-
auto InnerPtr = DeclCtxWithInvalidDeclAndHasAttrs.getPointer();
379-
InnerPtr.setPointer(MDC);
380-
DeclCtxWithInvalidDeclAndHasAttrs.setPointer(InnerPtr);
372+
DeclCtx = MDC;
381373
}
382374
}
383375

@@ -964,24 +956,19 @@ unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
964956
}
965957

966958
void Decl::setAttrsImpl(const AttrVec &attrs, ASTContext &Ctx) {
967-
assert(!hasAttrs() && "Decl already contains attrs.");
959+
assert(!HasAttrs && "Decl already contains attrs.");
968960

969961
AttrVec &AttrBlank = Ctx.getDeclAttrs(this);
970962
assert(AttrBlank.empty() && "HasAttrs was wrong?");
971963

972964
AttrBlank = attrs;
973-
auto InnerPtr = DeclCtxWithInvalidDeclAndHasAttrs.getPointer();
974-
InnerPtr.setInt(true);
975-
DeclCtxWithInvalidDeclAndHasAttrs.setPointer(InnerPtr);
965+
HasAttrs = true;
976966
}
977967

978968
void Decl::dropAttrs() {
979-
if (!hasAttrs())
980-
return;
969+
if (!HasAttrs) return;
981970

982-
auto InnerPtr = DeclCtxWithInvalidDeclAndHasAttrs.getPointer();
983-
InnerPtr.setInt(false);
984-
DeclCtxWithInvalidDeclAndHasAttrs.setPointer(InnerPtr);
971+
HasAttrs = false;
985972
getASTContext().eraseDeclAttrs(this);
986973
}
987974

@@ -1009,7 +996,7 @@ void Decl::addAttr(Attr *A) {
1009996
}
1010997

1011998
const AttrVec &Decl::getAttrs() const {
1012-
assert(hasAttrs() && "No attrs to get!");
999+
assert(HasAttrs && "No attrs to get!");
10131000
return getASTContext().getDeclAttrs(this);
10141001
}
10151002

clang/lib/Serialization/ASTReaderDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ void ASTDeclReader::VisitDecl(Decl *D) {
588588
bool HasStandaloneLexicalDC = DeclBits.getNextBit();
589589
bool HasAttrs = DeclBits.getNextBit();
590590
D->setTopLevelDeclInObjCContainer(DeclBits.getNextBit());
591-
D->DeclCtxWithInvalidDeclAndHasAttrs.setInt(DeclBits.getNextBit());
591+
D->InvalidDecl = DeclBits.getNextBit();
592592
D->FromASTFile = true;
593593

594594
if (D->isTemplateParameter() || D->isTemplateParameterPack() ||

0 commit comments

Comments
 (0)