Skip to content

Commit 74d3ad4

Browse files
committed
[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%. (cherry picked from commit 25d1ac1)
1 parent 50227b7 commit 74d3ad4

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
///
@@ -282,6 +286,7 @@ class HeaderSearchOptions {
282286
ModulesSkipHeaderSearchPaths(false),
283287
ModulesSkipPragmaDiagnosticMappings(false),
284288
ModulesPruneNonAffectingModuleMaps(true), ModulesHashContent(false),
289+
ModulesSerializeOnlyPreprocessor(false),
285290
ModulesStrictContextHash(false), ModulesIncludeVFSUsage(false) {}
286291

287292
/// AddPath - Add the \p Path path to the specified \p Group list.

clang/include/clang/Serialization/ASTWriter.h

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

919919
Preprocessor &PP;
920+
llvm::PointerUnion<Sema *, Preprocessor *> Subject;
920921
std::string OutputFile;
921922
std::string isysroot;
922-
Sema *SemaPtr;
923923
std::shared_ptr<PCHBuffer> Buffer;
924924
llvm::BitstreamWriter Stream;
925925
ASTWriter Writer;
@@ -934,9 +934,7 @@ class PCHGenerator : public SemaConsumer {
934934
bool isComplete() const { return Buffer->IsComplete; }
935935
PCHBuffer *getBufferPtr() { return Buffer.get(); }
936936
StringRef getOutputFile() const { return OutputFile; }
937-
DiagnosticsEngine &getDiagnostics() const {
938-
return SemaPtr->getDiagnostics();
939-
}
937+
DiagnosticsEngine &getDiagnostics() const;
940938
Preprocessor &getPreprocessor() { return PP; }
941939

942940
virtual Module *getEmittingModule(ASTContext &Ctx);
@@ -952,7 +950,7 @@ class PCHGenerator : public SemaConsumer {
952950
bool GeneratingReducedBMI = false);
953951
~PCHGenerator() override;
954952

955-
void InitializeSema(Sema &S) override { SemaPtr = &S; }
953+
void InitializeSema(Sema &S) override;
956954
void HandleTranslationUnit(ASTContext &Ctx) override;
957955
void HandleVTable(CXXRecordDecl *RD) override { Writer.handleVTable(RD); }
958956
ASTMutationListener *GetASTMutationListener() override;

clang/lib/Serialization/ASTReader.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3541,6 +3541,9 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
35413541
break;
35423542
}
35433543

3544+
if (Record.empty())
3545+
break;
3546+
35443547
if (SpecialTypes.size() != Record.size())
35453548
return llvm::createStringError(std::errc::illegal_byte_sequence,
35463549
"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
@@ -593,6 +593,7 @@ class DependencyScanningAction : public tooling::ToolAction {
593593
// TODO: Implement diagnostic bucketing to reduce the impact of strict
594594
// context hashing.
595595
ScanInstance.getHeaderSearchOpts().ModulesStrictContextHash = true;
596+
ScanInstance.getHeaderSearchOpts().ModulesSerializeOnlyPreprocessor = true;
596597
ScanInstance.getHeaderSearchOpts().ModulesSkipDiagnosticOptions = true;
597598
ScanInstance.getHeaderSearchOpts().ModulesSkipHeaderSearchPaths = true;
598599
ScanInstance.getHeaderSearchOpts().ModulesSkipPragmaDiagnosticMappings =

0 commit comments

Comments
 (0)