Skip to content

Commit

Permalink
[NFC] [Serialization] Avoid accessing PendingBodies as much as possible
Browse files Browse the repository at this point in the history
The 'HaveBody' parameter in isConsumerInterestedIn is only used for the function decl if it
doesn't have a body already. It should be relatively less frequent than
the call to isConsumerInterestedIn. So we can delay the computing of
`HaveBdoy` to make it more efficient.
  • Loading branch information
ChuanqiXu9 committed Apr 11, 2024
1 parent dda7333 commit 75edf0c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
1 change: 1 addition & 0 deletions clang/include/clang/Serialization/ASTReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -1492,6 +1492,7 @@ class ASTReader
getModuleFileLevelDecls(ModuleFile &Mod);

private:
bool isConsumerInterestedIn(Decl *D);
void PassInterestingDeclsToConsumer();
void PassInterestingDeclToConsumer(Decl *D);

Expand Down
14 changes: 6 additions & 8 deletions clang/lib/Serialization/ASTReaderDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3198,7 +3198,7 @@ inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) {
/// This routine should return true for anything that might affect
/// code generation, e.g., inline function definitions, Objective-C
/// declarations with metadata, etc.
static bool isConsumerInterestedIn(ASTContext &Ctx, Decl *D, bool HasBody) {
bool ASTReader::isConsumerInterestedIn(Decl *D) {
// An ObjCMethodDecl is never considered as "interesting" because its
// implementation container always is.

Expand All @@ -3207,7 +3207,7 @@ static bool isConsumerInterestedIn(ASTContext &Ctx, Decl *D, bool HasBody) {
if (isPartOfPerModuleInitializer(D)) {
auto *M = D->getImportedOwningModule();
if (M && M->Kind == Module::ModuleMapModule &&
Ctx.DeclMustBeEmitted(D))
getContext().DeclMustBeEmitted(D))
return false;
}

Expand All @@ -3222,7 +3222,7 @@ static bool isConsumerInterestedIn(ASTContext &Ctx, Decl *D, bool HasBody) {
(Var->isThisDeclarationADefinition() == VarDecl::Definition ||
OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(Var));
if (const auto *Func = dyn_cast<FunctionDecl>(D))
return Func->doesThisDeclarationHaveABody() || HasBody;
return Func->doesThisDeclarationHaveABody() || PendingBodies.count(D);

if (auto *ES = D->getASTContext().getExternalSource())
if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never)
Expand Down Expand Up @@ -4173,7 +4173,7 @@ void ASTReader::PassInterestingDeclsToConsumer() {
while (!PotentiallyInterestingDecls.empty()) {
Decl *D = PotentiallyInterestingDecls.front();
PotentiallyInterestingDecls.pop_front();
if (isConsumerInterestedIn(getContext(), D, PendingBodies.count(D)))
if (isConsumerInterestedIn(D))
PassInterestingDeclToConsumer(D);
}
}
Expand All @@ -4197,8 +4197,7 @@ void ASTReader::loadDeclUpdateRecords(PendingUpdateRecord &Record) {
// the declaration, then we know it was interesting and we skip the call
// to isConsumerInterestedIn because it is unsafe to call in the
// current ASTReader state.
bool WasInteresting =
Record.JustLoaded || isConsumerInterestedIn(getContext(), D, false);
bool WasInteresting = Record.JustLoaded || isConsumerInterestedIn(D);
for (auto &FileAndOffset : UpdateOffsets) {
ModuleFile *F = FileAndOffset.first;
uint64_t Offset = FileAndOffset.second;
Expand Down Expand Up @@ -4230,8 +4229,7 @@ void ASTReader::loadDeclUpdateRecords(PendingUpdateRecord &Record) {

// We might have made this declaration interesting. If so, remember that
// we need to hand it off to the consumer.
if (!WasInteresting &&
isConsumerInterestedIn(getContext(), D, PendingBodies.count(D))) {
if (!WasInteresting && isConsumerInterestedIn(D)) {
PotentiallyInterestingDecls.push_back(D);
WasInteresting = true;
}
Expand Down

0 comments on commit 75edf0c

Please sign in to comment.