Skip to content

Commit 25d1ac1

Browse files
authored
[clang][deps] Only write preprocessor info into PCMs (llvm#115239)
This patch builds on top of llvm#115237 and llvm#115235, only passing the `Preprocessor` object to `ASTWriter`. This reduces the size of scanning PCM files by 1/3 and speeds up scans by 16%.
1 parent 9eefa92 commit 25d1ac1

File tree

5 files changed

+26
-10
lines changed

5 files changed

+26
-10
lines changed

clang/include/clang/Lex/HeaderSearchOptions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ class HeaderSearchOptions {
255255
LLVM_PREFERRED_TYPE(bool)
256256
unsigned ModulesHashContent : 1;
257257

258+
/// Whether AST files should only contain the preprocessor information.
259+
LLVM_PREFERRED_TYPE(bool)
260+
unsigned ModulesSerializeOnlyPreprocessor : 1;
261+
258262
/// Whether we should include all things that could impact the module in the
259263
/// hash.
260264
///
@@ -288,6 +292,7 @@ class HeaderSearchOptions {
288292
ModulesSkipHeaderSearchPaths(false),
289293
ModulesSkipPragmaDiagnosticMappings(false),
290294
ModulesPruneNonAffectingModuleMaps(true), ModulesHashContent(false),
295+
ModulesSerializeOnlyPreprocessor(false),
291296
ModulesStrictContextHash(false), ModulesIncludeVFSUsage(false),
292297
AllowModuleMapSubdirectorySearch(true) {}
293298

clang/include/clang/Serialization/ASTWriter.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -929,9 +929,9 @@ class PCHGenerator : public SemaConsumer {
929929
void anchor() override;
930930

931931
Preprocessor &PP;
932+
llvm::PointerUnion<Sema *, Preprocessor *> Subject;
932933
std::string OutputFile;
933934
std::string isysroot;
934-
Sema *SemaPtr;
935935
std::shared_ptr<PCHBuffer> Buffer;
936936
llvm::BitstreamWriter Stream;
937937
ASTWriter Writer;
@@ -946,9 +946,7 @@ class PCHGenerator : public SemaConsumer {
946946
bool isComplete() const { return Buffer->IsComplete; }
947947
PCHBuffer *getBufferPtr() { return Buffer.get(); }
948948
StringRef getOutputFile() const { return OutputFile; }
949-
DiagnosticsEngine &getDiagnostics() const {
950-
return SemaPtr->getDiagnostics();
951-
}
949+
DiagnosticsEngine &getDiagnostics() const;
952950
Preprocessor &getPreprocessor() { return PP; }
953951

954952
virtual Module *getEmittingModule(ASTContext &Ctx);
@@ -964,7 +962,7 @@ class PCHGenerator : public SemaConsumer {
964962
bool GeneratingReducedBMI = false);
965963
~PCHGenerator() override;
966964

967-
void InitializeSema(Sema &S) override { SemaPtr = &S; }
965+
void InitializeSema(Sema &S) override;
968966
void HandleTranslationUnit(ASTContext &Ctx) override;
969967
void HandleVTable(CXXRecordDecl *RD) override { Writer.handleVTable(RD); }
970968
ASTMutationListener *GetASTMutationListener() override;

clang/lib/Serialization/ASTReader.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3522,6 +3522,9 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
35223522
break;
35233523
}
35243524

3525+
if (Record.empty())
3526+
break;
3527+
35253528
if (SpecialTypes.size() != Record.size())
35263529
return llvm::createStringError(std::errc::illegal_byte_sequence,
35273530
"invalid special-types record");

clang/lib/Serialization/GeneratePCH.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ PCHGenerator::PCHGenerator(
2929
bool AllowASTWithErrors, bool IncludeTimestamps,
3030
bool BuildingImplicitModule, bool ShouldCacheASTInMemory,
3131
bool GeneratingReducedBMI)
32-
: PP(PP), OutputFile(OutputFile), isysroot(isysroot.str()),
33-
SemaPtr(nullptr), Buffer(std::move(Buffer)), Stream(this->Buffer->Data),
32+
: PP(PP), Subject(&PP), OutputFile(OutputFile), isysroot(isysroot.str()),
33+
Buffer(std::move(Buffer)), Stream(this->Buffer->Data),
3434
Writer(Stream, this->Buffer->Data, ModuleCache, Extensions,
3535
IncludeTimestamps, BuildingImplicitModule, GeneratingReducedBMI),
3636
AllowASTWithErrors(AllowASTWithErrors),
@@ -56,6 +56,17 @@ Module *PCHGenerator::getEmittingModule(ASTContext &) {
5656
return M;
5757
}
5858

59+
DiagnosticsEngine &PCHGenerator::getDiagnostics() const {
60+
return PP.getDiagnostics();
61+
}
62+
63+
void PCHGenerator::InitializeSema(Sema &S) {
64+
if (!PP.getHeaderSearchInfo()
65+
.getHeaderSearchOpts()
66+
.ModulesSerializeOnlyPreprocessor)
67+
Subject = &S;
68+
}
69+
5970
void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
6071
// Don't create a PCH if there were fatal failures during module loading.
6172
if (PP.getModuleLoader().HadFatalFailure)
@@ -72,9 +83,7 @@ void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
7283
if (AllowASTWithErrors)
7384
PP.getDiagnostics().getClient()->clear();
7485

75-
// Emit the PCH file to the Buffer.
76-
assert(SemaPtr && "No Sema?");
77-
Buffer->Signature = Writer.WriteAST(SemaPtr, OutputFile, Module, isysroot,
86+
Buffer->Signature = Writer.WriteAST(Subject, OutputFile, Module, isysroot,
7887
ShouldCacheASTInMemory);
7988

8089
Buffer->IsComplete = true;

clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ class DependencyScanningAction : public tooling::ToolAction {
422422
// TODO: Implement diagnostic bucketing to reduce the impact of strict
423423
// context hashing.
424424
ScanInstance.getHeaderSearchOpts().ModulesStrictContextHash = true;
425+
ScanInstance.getHeaderSearchOpts().ModulesSerializeOnlyPreprocessor = true;
425426
ScanInstance.getHeaderSearchOpts().ModulesSkipDiagnosticOptions = true;
426427
ScanInstance.getHeaderSearchOpts().ModulesSkipHeaderSearchPaths = true;
427428
ScanInstance.getHeaderSearchOpts().ModulesSkipPragmaDiagnosticMappings =

0 commit comments

Comments
 (0)