Skip to content

Commit ef0291e

Browse files
committed
[NFC] [Serialization] Reordering lexcical and visible TU block after 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).
1 parent a4dec9d commit ef0291e

File tree

2 files changed

+42
-31
lines changed

2 files changed

+42
-31
lines changed

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4959,38 +4959,12 @@ ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
49594959
Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
49604960
}
49614961

4962-
// Create a lexical update block containing all of the declarations in the
4963-
// translation unit that do not come from other AST files.
49644962
const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4965-
SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4966-
for (const auto *D : TU->noload_decls()) {
4967-
if (!D->isFromASTFile()) {
4968-
NewGlobalKindDeclPairs.push_back(D->getKind());
4969-
NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4970-
}
4971-
}
4972-
4973-
auto Abv = std::make_shared<BitCodeAbbrev>();
4974-
Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4975-
Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4976-
unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
4977-
{
4978-
RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4979-
Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4980-
bytes(NewGlobalKindDeclPairs));
4981-
}
49824963

4983-
// And a visible updates block for the translation unit.
4984-
Abv = std::make_shared<BitCodeAbbrev>();
4985-
Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4986-
Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4987-
Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4988-
UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
4989-
WriteDeclContextVisibleUpdate(TU);
4990-
4991-
// If we have any extern "C" names, write out a visible update for them.
4992-
if (Context.ExternCContext)
4993-
WriteDeclContextVisibleUpdate(Context.ExternCContext);
4964+
// Force all top level declarations to be emitted.
4965+
for (const auto *D : TU->noload_decls())
4966+
if (!D->isFromASTFile())
4967+
GetDeclRef(D);
49944968

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

5108+
{
5109+
auto Abv = std::make_shared<BitCodeAbbrev>();
5110+
Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
5111+
Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
5112+
Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
5113+
UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
5114+
}
5115+
51345116
RecordData DeclUpdatesOffsetsRecord;
51355117

51365118
// Keep writing types, declarations, and declaration update records
@@ -5158,6 +5140,35 @@ ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
51585140
WriteTypeDeclOffsets();
51595141
if (!DeclUpdatesOffsetsRecord.empty())
51605142
Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
5143+
5144+
// Create a lexical update block containing all of the declarations in the
5145+
// translation unit that do not come from other AST files.
5146+
{
5147+
SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
5148+
for (const auto *D : TU->noload_decls()) {
5149+
if (!D->isFromASTFile()) {
5150+
NewGlobalKindDeclPairs.push_back(D->getKind());
5151+
NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
5152+
}
5153+
}
5154+
5155+
auto Abv = std::make_shared<BitCodeAbbrev>();
5156+
Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
5157+
Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
5158+
unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
5159+
5160+
RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
5161+
Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
5162+
bytes(NewGlobalKindDeclPairs));
5163+
}
5164+
5165+
// And a visible updates block for the translation unit.
5166+
WriteDeclContextVisibleUpdate(TU);
5167+
5168+
// If we have any extern "C" names, write out a visible update for them.
5169+
if (Context.ExternCContext)
5170+
WriteDeclContextVisibleUpdate(Context.ExternCContext);
5171+
51615172
WriteFileDeclIDsMap();
51625173
WriteSourceManagerBlock(Context.getSourceManager(), PP);
51635174
WriteComments();

clang/test/Modules/language-linkage.cppm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ void foo() {}
1414

1515
extern "C" void bar() {}
1616

17-
// CHECK: define {{.*}}@bar(
1817
// CHECK: define {{.*}}@_Z3foov(
18+
// CHECK: define {{.*}}@bar(

0 commit comments

Comments
 (0)