Skip to content

Commit

Permalink
[NFC] [Serialization] Reordering lexcical and visible TU block after …
Browse files Browse the repository at this point in the history
…type decl offsets

This patch reorder the lexical block for the translation unit, visible update block for the TU and
the viisble upaete block for the extern C context after the type decl
offsets block.

This should be a NFC patch.

This is helpful for later optimizations for eliding unreachable
declarations in the global module fragment. See the comments in
#76930.

Simply, if we want to get the reachable sets of declaratins during the
writing process, we need to write the file-level context later than the
process of writing declarations (which is the main process to determine
the reachable set).
  • Loading branch information
ChuanqiXu9 committed Apr 1, 2024
1 parent a4dec9d commit ef0291e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 31 deletions.
71 changes: 41 additions & 30 deletions clang/lib/Serialization/ASTWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4959,38 +4959,12 @@ ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
}

// Create a lexical update block containing all of the declarations in the
// translation unit that do not come from other AST files.
const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
for (const auto *D : TU->noload_decls()) {
if (!D->isFromASTFile()) {
NewGlobalKindDeclPairs.push_back(D->getKind());
NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
}
}

auto Abv = std::make_shared<BitCodeAbbrev>();
Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
{
RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
bytes(NewGlobalKindDeclPairs));
}

// And a visible updates block for the translation unit.
Abv = std::make_shared<BitCodeAbbrev>();
Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
WriteDeclContextVisibleUpdate(TU);

// If we have any extern "C" names, write out a visible update for them.
if (Context.ExternCContext)
WriteDeclContextVisibleUpdate(Context.ExternCContext);
// Force all top level declarations to be emitted.
for (const auto *D : TU->noload_decls())
if (!D->isFromASTFile())
GetDeclRef(D);

// If the translation unit has an anonymous namespace, and we don't already
// have an update block for it, write it as an update block.
Expand Down Expand Up @@ -5131,6 +5105,14 @@ ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
for (auto *D : SemaRef.DeclsToCheckForDeferredDiags)
DeclsToCheckForDeferredDiags.push_back(GetDeclRef(D));

{
auto Abv = std::make_shared<BitCodeAbbrev>();
Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
}

RecordData DeclUpdatesOffsetsRecord;

// Keep writing types, declarations, and declaration update records
Expand Down Expand Up @@ -5158,6 +5140,35 @@ ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
WriteTypeDeclOffsets();
if (!DeclUpdatesOffsetsRecord.empty())
Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);

// Create a lexical update block containing all of the declarations in the
// translation unit that do not come from other AST files.
{
SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
for (const auto *D : TU->noload_decls()) {
if (!D->isFromASTFile()) {
NewGlobalKindDeclPairs.push_back(D->getKind());
NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
}
}

auto Abv = std::make_shared<BitCodeAbbrev>();
Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));

RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
bytes(NewGlobalKindDeclPairs));
}

// And a visible updates block for the translation unit.
WriteDeclContextVisibleUpdate(TU);

// If we have any extern "C" names, write out a visible update for them.
if (Context.ExternCContext)
WriteDeclContextVisibleUpdate(Context.ExternCContext);

WriteFileDeclIDsMap();
WriteSourceManagerBlock(Context.getSourceManager(), PP);
WriteComments();
Expand Down
2 changes: 1 addition & 1 deletion clang/test/Modules/language-linkage.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ void foo() {}

extern "C" void bar() {}

// CHECK: define {{.*}}@bar(
// CHECK: define {{.*}}@_Z3foov(
// CHECK: define {{.*}}@bar(

0 comments on commit ef0291e

Please sign in to comment.