Skip to content

Commit 7649a05

Browse files
authored
Merge pull request #9573 from swiftlang/jan_svoboda/stable-20240723-scanner-perf-2
🍒 [clang][deps][modules] Speed up dependency scanning (2)
2 parents 5232c25 + 74d3ad4 commit 7649a05

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+485
-430
lines changed

clang/include/clang/AST/ASTContext.h

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -476,14 +476,6 @@ class ASTContext : public RefCountedBase<ASTContext> {
476476
llvm::StringMap<const Module *> PrimaryModuleNameMap;
477477
llvm::DenseMap<const Module *, const Module *> SameModuleLookupSet;
478478

479-
/// The include tree that is being built, if any.
480-
/// See \c FrontendOptions::CASIncludeTreeID.
481-
std::optional<std::string> CASIncludeTreeID;
482-
483-
/// The cas-fs tree that is being built, if any.
484-
/// See \c FileSystemOptions::CASFileSystemRootID.
485-
std::optional<std::string> CASFileSystemRootID;
486-
487479
static constexpr unsigned ConstantArrayTypesLog2InitSize = 8;
488480
static constexpr unsigned GeneralTypesLog2InitSize = 9;
489481
static constexpr unsigned FunctionProtoTypesLog2InitSize = 12;
@@ -1123,20 +1115,6 @@ class ASTContext : public RefCountedBase<ASTContext> {
11231115
/// a module fragment or a module unit but not a C++20 module.
11241116
bool isInSameModule(const Module *M1, const Module *M2);
11251117

1126-
std::optional<std::string> getCASIncludeTreeID() const {
1127-
return CASIncludeTreeID;
1128-
}
1129-
void setCASIncludeTreeID(std::string ID) {
1130-
CASIncludeTreeID = std::move(ID);
1131-
}
1132-
1133-
std::optional<std::string> getCASFileSystemRootID() const {
1134-
return CASFileSystemRootID;
1135-
}
1136-
void setCASFileSystemRootID(std::string ID) {
1137-
CASFileSystemRootID = std::move(ID);
1138-
}
1139-
11401118
TranslationUnitDecl *getTranslationUnitDecl() const {
11411119
return TUDecl->getMostRecentDecl();
11421120
}

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/Lex/Preprocessor.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2428,6 +2428,28 @@ class Preprocessor {
24282428
!IsAtImport;
24292429
}
24302430

2431+
private:
2432+
/// The include tree that is being built, if any.
2433+
/// See \c FrontendOptions::CASIncludeTreeID.
2434+
std::optional<std::string> CASIncludeTreeID;
2435+
2436+
/// The cas-fs tree that is being built, if any.
2437+
/// See \c FileSystemOptions::CASFileSystemRootID.
2438+
std::optional<std::string> CASFileSystemRootID;
2439+
2440+
public:
2441+
std::optional<std::string> getCASIncludeTreeID() const {
2442+
return CASIncludeTreeID;
2443+
}
2444+
void setCASIncludeTreeID(std::string ID) { CASIncludeTreeID = std::move(ID); }
2445+
2446+
std::optional<std::string> getCASFileSystemRootID() const {
2447+
return CASFileSystemRootID;
2448+
}
2449+
void setCASFileSystemRootID(std::string ID) {
2450+
CASFileSystemRootID = std::move(ID);
2451+
}
2452+
24312453
/// Allocate a new MacroInfo object with the provided SourceLocation.
24322454
MacroInfo *AllocateMacroInfo(SourceLocation L);
24332455

clang/include/clang/Serialization/ASTRecordWriter.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ class ASTRecordWriter
6060

6161
public:
6262
/// Construct a ASTRecordWriter that uses the default encoding scheme.
63-
ASTRecordWriter(ASTWriter &W, ASTWriter::RecordDataImpl &Record)
64-
: DataStreamBasicWriter(W.getASTContext()), Writer(&W), Record(&Record) {}
63+
ASTRecordWriter(ASTContext &Context, ASTWriter &W,
64+
ASTWriter::RecordDataImpl &Record)
65+
: DataStreamBasicWriter(Context), Writer(&W), Record(&Record) {}
6566

6667
/// Construct a ASTRecordWriter that uses the same encoding scheme as another
6768
/// ASTRecordWriter.
@@ -208,7 +209,7 @@ class ASTRecordWriter
208209

209210
/// Emit a reference to a type.
210211
void AddTypeRef(QualType T) {
211-
return Writer->AddTypeRef(T, *Record);
212+
return Writer->AddTypeRef(getASTContext(), T, *Record);
212213
}
213214
void writeQualType(QualType T) {
214215
AddTypeRef(T);

clang/include/clang/Serialization/ASTWriter.h

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,6 @@ class ASTWriter : public ASTDeserializationListener,
119119
/// The PCM manager which manages memory buffers for pcm files.
120120
InMemoryModuleCache &ModuleCache;
121121

122-
/// The ASTContext we're writing.
123-
ASTContext *Context = nullptr;
124-
125122
/// The preprocessor we're writing.
126123
Preprocessor *PP = nullptr;
127124

@@ -537,55 +534,55 @@ class ASTWriter : public ASTDeserializationListener,
537534
unsigned getSubmoduleID(Module *Mod);
538535

539536
/// Write the given subexpression to the bitstream.
540-
void WriteSubStmt(Stmt *S);
537+
void WriteSubStmt(ASTContext &Context, Stmt *S);
541538

542539
void WriteBlockInfoBlock();
543-
void WriteControlBlock(Preprocessor &PP, ASTContext &Context,
544-
StringRef isysroot);
540+
void WriteControlBlock(Preprocessor &PP, StringRef isysroot);
545541

546542
/// Write out the signature and diagnostic options, and return the signature.
547-
void writeUnhashedControlBlock(Preprocessor &PP, ASTContext &Context);
543+
void writeUnhashedControlBlock(Preprocessor &PP);
548544
ASTFileSignature backpatchSignature();
549545

550546
/// Calculate hash of the pcm content.
551547
std::pair<ASTFileSignature, ASTFileSignature> createSignature() const;
552548
ASTFileSignature createSignatureForNamedModule() const;
553549

554550
void WriteInputFiles(SourceManager &SourceMgr, HeaderSearchOptions &HSOpts);
555-
void WriteSourceManagerBlock(SourceManager &SourceMgr,
556-
const Preprocessor &PP);
551+
void WriteSourceManagerBlock(SourceManager &SourceMgr);
557552
void WritePreprocessor(const Preprocessor &PP, bool IsModule);
558553
void WriteHeaderSearch(const HeaderSearch &HS);
559554
void WritePreprocessorDetail(PreprocessingRecord &PPRec,
560555
uint64_t MacroOffsetsBase);
561-
void WriteSubmodules(Module *WritingModule);
556+
void WriteSubmodules(Module *WritingModule, ASTContext *Context);
562557

563558
void WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
564559
bool isModule);
565560

566561
unsigned TypeExtQualAbbrev = 0;
567562
void WriteTypeAbbrevs();
568-
void WriteType(QualType T);
563+
void WriteType(ASTContext &Context, QualType T);
569564

570565
bool isLookupResultExternal(StoredDeclsList &Result, DeclContext *DC);
571566

572-
void GenerateNameLookupTable(const DeclContext *DC,
567+
void GenerateNameLookupTable(ASTContext &Context, const DeclContext *DC,
573568
llvm::SmallVectorImpl<char> &LookupTable);
574569
uint64_t WriteDeclContextLexicalBlock(ASTContext &Context,
575570
const DeclContext *DC);
576571
uint64_t WriteDeclContextVisibleBlock(ASTContext &Context, DeclContext *DC);
577572
void WriteTypeDeclOffsets();
578573
void WriteFileDeclIDsMap();
579-
void WriteComments();
574+
void WriteComments(ASTContext &Context);
580575
void WriteSelectors(Sema &SemaRef);
581576
void WriteReferencedSelectorsPool(Sema &SemaRef);
582-
void WriteIdentifierTable(Preprocessor &PP, IdentifierResolver &IdResolver,
577+
void WriteIdentifierTable(Preprocessor &PP, IdentifierResolver *IdResolver,
583578
bool IsModule);
584579
void WriteDeclAndTypes(ASTContext &Context);
585580
void PrepareWritingSpecialDecls(Sema &SemaRef);
586581
void WriteSpecialDeclRecords(Sema &SemaRef);
587-
void WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord);
588-
void WriteDeclContextVisibleUpdate(const DeclContext *DC);
582+
void WriteDeclUpdatesBlocks(ASTContext &Context,
583+
RecordDataImpl &OffsetsRecord);
584+
void WriteDeclContextVisibleUpdate(ASTContext &Context,
585+
const DeclContext *DC);
589586
void WriteFPPragmaOptions(const FPOptionsOverride &Opts);
590587
void WriteOpenCLExtensions(Sema &SemaRef);
591588
void WriteCUDAPragmas(Sema &SemaRef);
@@ -634,7 +631,7 @@ class ASTWriter : public ASTDeserializationListener,
634631
void WriteDeclAbbrevs();
635632
void WriteDecl(ASTContext &Context, Decl *D);
636633

637-
ASTFileSignature WriteASTCore(Sema &SemaRef, StringRef isysroot,
634+
ASTFileSignature WriteASTCore(Sema *SemaPtr, StringRef isysroot,
638635
Module *WritingModule);
639636

640637
public:
@@ -647,22 +644,20 @@ class ASTWriter : public ASTDeserializationListener,
647644
bool GeneratingReducedBMI = false);
648645
~ASTWriter() override;
649646

650-
ASTContext &getASTContext() const {
651-
assert(Context && "requested AST context when not writing AST");
652-
return *Context;
653-
}
654-
655647
const LangOptions &getLangOpts() const;
656648

657649
/// Get a timestamp for output into the AST file. The actual timestamp
658650
/// of the specified file may be ignored if we have been instructed to not
659651
/// include timestamps in the output file.
660652
time_t getTimestampForOutput(const FileEntry *E) const;
661653

662-
/// Write a precompiled header for the given semantic analysis.
654+
/// Write a precompiled header or a module with the AST produced by the
655+
/// \c Sema object, or a dependency scanner module with the preprocessor state
656+
/// produced by the \c Preprocessor object.
663657
///
664-
/// \param SemaRef a reference to the semantic analysis object that processed
665-
/// the AST to be written into the precompiled header.
658+
/// \param Subject The \c Sema object that processed the AST to be written, or
659+
/// in the case of a dependency scanner module the \c Preprocessor that holds
660+
/// the state.
666661
///
667662
/// \param WritingModule The module that we are writing. If null, we are
668663
/// writing a precompiled header.
@@ -673,8 +668,9 @@ class ASTWriter : public ASTDeserializationListener,
673668
///
674669
/// \return the module signature, which eventually will be a hash of
675670
/// the module but currently is merely a random 32-bit number.
676-
ASTFileSignature WriteAST(Sema &SemaRef, StringRef OutputFile,
677-
Module *WritingModule, StringRef isysroot,
671+
ASTFileSignature WriteAST(llvm::PointerUnion<Sema *, Preprocessor *> Subject,
672+
StringRef OutputFile, Module *WritingModule,
673+
StringRef isysroot,
678674
bool ShouldCacheASTInMemory = false);
679675

680676
/// Emit a token.
@@ -717,10 +713,10 @@ class ASTWriter : public ASTDeserializationListener,
717713
uint32_t getMacroDirectivesOffset(const IdentifierInfo *Name);
718714

719715
/// Emit a reference to a type.
720-
void AddTypeRef(QualType T, RecordDataImpl &Record);
716+
void AddTypeRef(ASTContext &Context, QualType T, RecordDataImpl &Record);
721717

722718
/// Force a type to be emitted and get its ID.
723-
serialization::TypeID GetOrCreateTypeID(QualType T);
719+
serialization::TypeID GetOrCreateTypeID(ASTContext &Context, QualType T);
724720

725721
/// Find the first local declaration of a given local redeclarable
726722
/// decl.
@@ -921,9 +917,9 @@ class PCHGenerator : public SemaConsumer {
921917
void anchor() override;
922918

923919
Preprocessor &PP;
920+
llvm::PointerUnion<Sema *, Preprocessor *> Subject;
924921
std::string OutputFile;
925922
std::string isysroot;
926-
Sema *SemaPtr;
927923
std::shared_ptr<PCHBuffer> Buffer;
928924
llvm::BitstreamWriter Stream;
929925
ASTWriter Writer;
@@ -938,9 +934,7 @@ class PCHGenerator : public SemaConsumer {
938934
bool isComplete() const { return Buffer->IsComplete; }
939935
PCHBuffer *getBufferPtr() { return Buffer.get(); }
940936
StringRef getOutputFile() const { return OutputFile; }
941-
DiagnosticsEngine &getDiagnostics() const {
942-
return SemaPtr->getDiagnostics();
943-
}
937+
DiagnosticsEngine &getDiagnostics() const;
944938
Preprocessor &getPreprocessor() { return PP; }
945939

946940
virtual Module *getEmittingModule(ASTContext &Ctx);
@@ -956,7 +950,7 @@ class PCHGenerator : public SemaConsumer {
956950
bool GeneratingReducedBMI = false);
957951
~PCHGenerator() override;
958952

959-
void InitializeSema(Sema &S) override { SemaPtr = &S; }
953+
void InitializeSema(Sema &S) override;
960954
void HandleTranslationUnit(ASTContext &Ctx) override;
961955
void HandleVTable(CXXRecordDecl *RD) override { Writer.handleVTable(RD); }
962956
ASTMutationListener *GetASTMutationListener() override;

clang/include/clang/Serialization/ModuleFile.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,20 @@ enum ModuleKind {
6262

6363
/// The input file info that has been loaded from an AST file.
6464
struct InputFileInfo {
65-
std::string FilenameAsRequested;
66-
std::string Filename;
65+
StringRef UnresolvedImportedFilenameAsRequested;
66+
StringRef UnresolvedImportedFilename;
67+
6768
uint64_t ContentHash;
6869
off_t StoredSize;
6970
time_t StoredTime;
7071
bool Overridden;
7172
bool Transient;
7273
bool TopLevel;
7374
bool ModuleMap;
75+
76+
bool isValid() const {
77+
return !UnresolvedImportedFilenameAsRequested.empty();
78+
}
7479
};
7580

7681
/// The input file that has been loaded from this AST file, along with

clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,6 @@ struct ModuleDeps {
123123
/// additionally appear in \c FileDeps as a dependency.
124124
std::string ClangModuleMapFile;
125125

126-
/// A collection of absolute paths to files that this module directly depends
127-
/// on, not including transitive dependencies.
128-
llvm::StringSet<> FileDeps;
129-
130126
/// A collection of absolute paths to module map files that this module needs
131127
/// to know about. The ordering is significant.
132128
std::vector<std::string> ModuleMapFileDeps;
@@ -155,13 +151,25 @@ struct ModuleDeps {
155151
/// an entity from this module is used.
156152
llvm::SmallVector<Module::LinkLibrary, 2> LinkLibraries;
157153

154+
/// Invokes \c Cb for all file dependencies of this module. Each provided
155+
/// \c StringRef is only valid within the individual callback invocation.
156+
void forEachFileDep(llvm::function_ref<void(StringRef)> Cb) const;
157+
158158
/// Get (or compute) the compiler invocation that can be used to build this
159159
/// module. Does not include argv[0].
160160
const std::vector<std::string> &getBuildArguments();
161161

162162
private:
163+
friend class ModuleDepCollector;
163164
friend class ModuleDepCollectorPP;
164165

166+
/// The base directory for relative paths in \c FileDeps.
167+
std::string FileDepsBaseDir;
168+
169+
/// A collection of paths to files that this module directly depends on, not
170+
/// including transitive dependencies.
171+
std::vector<std::string> FileDeps;
172+
165173
std::variant<std::monostate, CowCompilerInvocation, std::vector<std::string>>
166174
BuildInfo;
167175
};

clang/lib/Frontend/ASTUnit.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2358,7 +2358,7 @@ bool ASTUnit::Save(StringRef File) {
23582358

23592359
static bool serializeUnit(ASTWriter &Writer, SmallVectorImpl<char> &Buffer,
23602360
Sema &S, raw_ostream &OS) {
2361-
Writer.WriteAST(S, std::string(), nullptr, "");
2361+
Writer.WriteAST(&S, std::string(), nullptr, "");
23622362

23632363
// Write the generated bitstream to "Out".
23642364
if (!Buffer.empty())

clang/lib/Frontend/FrontendAction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
12081208
}
12091209

12101210
if (!CI.getFrontendOpts().CASIncludeTreeID.empty())
1211-
CI.getASTContext().setCASIncludeTreeID(
1211+
CI.getPreprocessor().setCASIncludeTreeID(
12121212
CI.getFrontendOpts().CASIncludeTreeID);
12131213

12141214
CI.setASTConsumer(std::move(Consumer));

0 commit comments

Comments
 (0)