Skip to content

Revert "[Clang] Reduce the size of Decl and classes derived from it" #88654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 25 additions & 47 deletions clang/include/clang/AST/DeclBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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>(); }
Expand Down Expand Up @@ -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
Expand Down
31 changes: 9 additions & 22 deletions clang/lib/AST/DeclBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@

using namespace clang;

static_assert(sizeof(Decl) <= 32, "Decl grew beyond 32 bytes!");

//===----------------------------------------------------------------------===//
// Statistics
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Serialization/ASTReaderDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() ||
Expand Down