Skip to content

Commit 46c12e7

Browse files
authored
Merge pull request #7257 from apple/jan_svoboda/stable-20221013-in-file-vis-cherry-pick
[clang][deps] Cherry-pick performance improvements to file dep collection
2 parents 5e6d56a + ea7ab27 commit 46c12e7

File tree

6 files changed

+82
-24
lines changed

6 files changed

+82
-24
lines changed

clang/include/clang/Serialization/ASTBitCodes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace serialization {
4141
/// Version 4 of AST files also requires that the version control branch and
4242
/// revision match exactly, since there is no backward compatibility of
4343
/// AST files at this time.
44-
const unsigned VERSION_MAJOR = 27;
44+
const unsigned VERSION_MAJOR = 28;
4545

4646
/// AST file minor version number supported by this version of
4747
/// Clang.

clang/include/clang/Serialization/ASTReader.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2356,6 +2356,13 @@ class ASTReader
23562356
/// Loads comments ranges.
23572357
void ReadComments() override;
23582358

2359+
/// Visit all the input file infos of the given module file.
2360+
void visitInputFileInfos(
2361+
serialization::ModuleFile &MF, bool IncludeSystem,
2362+
llvm::function_ref<void(const serialization::InputFileInfo &IFI,
2363+
bool IsSystem)>
2364+
Visitor);
2365+
23592366
/// Visit all the input files of the given module file.
23602367
void visitInputFiles(serialization::ModuleFile &MF,
23612368
bool IncludeSystem, bool Complain,

clang/include/clang/Serialization/ModuleFile.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,15 @@ enum ModuleKind {
6161

6262
/// The input file info that has been loaded from an AST file.
6363
struct InputFileInfo {
64+
std::string FilenameAsRequested;
6465
std::string Filename;
6566
uint64_t ContentHash;
6667
off_t StoredSize;
6768
time_t StoredTime;
6869
bool Overridden;
6970
bool Transient;
70-
bool TopLevelModuleMap;
71+
bool TopLevel;
72+
bool ModuleMap;
7173
};
7274

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

clang/lib/Serialization/ASTReader.cpp

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2305,9 +2305,22 @@ InputFileInfo ASTReader::getInputFileInfo(ModuleFile &F, unsigned ID) {
23052305
R.StoredTime = static_cast<time_t>(Record[2]);
23062306
R.Overridden = static_cast<bool>(Record[3]);
23072307
R.Transient = static_cast<bool>(Record[4]);
2308-
R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2309-
R.Filename = std::string(Blob);
2310-
ResolveImportedPath(F, R.Filename);
2308+
R.TopLevel = static_cast<bool>(Record[5]);
2309+
R.ModuleMap = static_cast<bool>(Record[6]);
2310+
std::tie(R.FilenameAsRequested, R.Filename) = [&]() {
2311+
uint16_t AsRequestedLength = Record[7];
2312+
2313+
std::string NameAsRequested = Blob.substr(0, AsRequestedLength).str();
2314+
std::string Name = Blob.substr(AsRequestedLength).str();
2315+
2316+
ResolveImportedPath(F, NameAsRequested);
2317+
ResolveImportedPath(F, Name);
2318+
2319+
if (Name.empty())
2320+
Name = NameAsRequested;
2321+
2322+
return std::make_pair(std::move(NameAsRequested), std::move(Name));
2323+
}();
23112324

23122325
Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
23132326
if (!MaybeEntry) // FIXME this drops errors on the floor.
@@ -2358,7 +2371,7 @@ InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
23582371
time_t StoredTime = FI.StoredTime;
23592372
bool Overridden = FI.Overridden;
23602373
bool Transient = FI.Transient;
2361-
StringRef Filename = FI.Filename;
2374+
StringRef Filename = FI.FilenameAsRequested;
23622375
uint64_t StoredContentHash = FI.ContentHash;
23632376

23642377
OptionalFileEntryRefDegradesToFileEntryPtr File =
@@ -2697,9 +2710,9 @@ ASTReader::ReadControlBlock(ModuleFile &F,
26972710
for (unsigned I = 0; I < N; ++I) {
26982711
bool IsSystem = I >= NumUserInputs;
26992712
InputFileInfo FI = getInputFileInfo(F, I + 1);
2700-
Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2701-
F.Kind == MK_ExplicitModule ||
2702-
F.Kind == MK_PrebuiltModule);
2713+
Listener->visitInputFile(
2714+
FI.FilenameAsRequested, IsSystem, FI.Overridden,
2715+
F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule);
27032716
}
27042717
}
27052718

@@ -9248,6 +9261,22 @@ void ASTReader::ReadComments() {
92489261
}
92499262
}
92509263

9264+
void ASTReader::visitInputFileInfos(
9265+
serialization::ModuleFile &MF, bool IncludeSystem,
9266+
llvm::function_ref<void(const serialization::InputFileInfo &IFI,
9267+
bool IsSystem)>
9268+
Visitor) {
9269+
unsigned NumUserInputs = MF.NumUserInputFiles;
9270+
unsigned NumInputs = MF.InputFilesLoaded.size();
9271+
assert(NumUserInputs <= NumInputs);
9272+
unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9273+
for (unsigned I = 0; I < N; ++I) {
9274+
bool IsSystem = I >= NumUserInputs;
9275+
InputFileInfo IFI = getInputFileInfo(MF, I+1);
9276+
Visitor(IFI, IsSystem);
9277+
}
9278+
}
9279+
92519280
void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
92529281
bool IncludeSystem, bool Complain,
92539282
llvm::function_ref<void(const serialization::InputFile &IF,
@@ -9269,7 +9298,7 @@ void ASTReader::visitTopLevelModuleMaps(
92699298
unsigned NumInputs = MF.InputFilesLoaded.size();
92709299
for (unsigned I = 0; I < NumInputs; ++I) {
92719300
InputFileInfo IFI = getInputFileInfo(MF, I + 1);
9272-
if (IFI.TopLevelModuleMap)
9301+
if (IFI.TopLevel && IFI.ModuleMap)
92739302
if (auto FE = getInputFile(MF, I + 1).getFile())
92749303
Visitor(*FE);
92759304
}

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,7 +1556,8 @@ struct InputFileEntry {
15561556
bool IsSystemFile;
15571557
bool IsTransient;
15581558
bool BufferOverridden;
1559-
bool IsTopLevelModuleMap;
1559+
bool IsTopLevel;
1560+
bool IsModuleMap;
15601561
uint32_t ContentHash[2];
15611562

15621563
InputFileEntry(FileEntryRef File) : File(File) {}
@@ -1578,8 +1579,10 @@ void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
15781579
IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
15791580
IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
15801581
IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
1582+
IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Top-level
15811583
IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Module map
1582-
IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1584+
IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // Name as req. len
1585+
IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name as req. + name
15831586
unsigned IFAbbrevCode = Stream.EmitAbbrev(std::move(IFAbbrev));
15841587

15851588
// Create input file hash abbreviation.
@@ -1613,8 +1616,8 @@ void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
16131616
Entry.IsSystemFile = isSystem(File.getFileCharacteristic());
16141617
Entry.IsTransient = Cache->IsTransient;
16151618
Entry.BufferOverridden = Cache->BufferOverridden;
1616-
Entry.IsTopLevelModuleMap = isModuleMap(File.getFileCharacteristic()) &&
1617-
File.getIncludeLoc().isInvalid();
1619+
Entry.IsTopLevel = File.getIncludeLoc().isInvalid();
1620+
Entry.IsModuleMap = isModuleMap(File.getFileCharacteristic());
16181621

16191622
auto ContentHash = hash_code(-1);
16201623
if (PP->getHeaderSearchInfo()
@@ -1662,16 +1665,28 @@ void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
16621665
// Emit size/modification time for this file.
16631666
// And whether this file was overridden.
16641667
{
1668+
SmallString<128> NameAsRequested = Entry.File.getNameAsRequested();
1669+
SmallString<128> Name = Entry.File.getName();
1670+
1671+
PreparePathForOutput(NameAsRequested);
1672+
PreparePathForOutput(Name);
1673+
1674+
if (Name == NameAsRequested)
1675+
Name.clear();
1676+
16651677
RecordData::value_type Record[] = {
16661678
INPUT_FILE,
16671679
InputFileOffsets.size(),
16681680
(uint64_t)Entry.File.getSize(),
16691681
(uint64_t)getTimestampForOutput(Entry.File),
16701682
Entry.BufferOverridden,
16711683
Entry.IsTransient,
1672-
Entry.IsTopLevelModuleMap};
1684+
Entry.IsTopLevel,
1685+
Entry.IsModuleMap,
1686+
NameAsRequested.size()};
16731687

1674-
EmitRecordWithPath(IFAbbrevCode, Record, Entry.File.getNameAsRequested());
1688+
Stream.EmitRecordWithBlob(IFAbbrevCode, Record,
1689+
(NameAsRequested + Name).str());
16751690
}
16761691

16771692
// Emit content hash for this file.

clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -465,30 +465,35 @@ ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
465465
serialization::ModuleFile *MF =
466466
MDC.ScanInstance.getASTReader()->getModuleManager().lookup(
467467
M->getASTFile());
468-
MDC.ScanInstance.getASTReader()->visitInputFiles(
469-
*MF, true, true, [&](const serialization::InputFile &IF, bool isSystem) {
468+
MDC.ScanInstance.getASTReader()->visitInputFileInfos(
469+
*MF, /*IncludeSystem=*/true,
470+
[&](const serialization::InputFileInfo &IFI, bool IsSystem) {
470471
// __inferred_module.map is the result of the way in which an implicit
471472
// module build handles inferred modules. It adds an overlay VFS with
472473
// this file in the proper directory and relies on the rest of Clang to
473474
// handle it like normal. With explicitly built modules we don't need
474475
// to play VFS tricks, so replace it with the correct module map.
475-
if (IF.getFile()->getName().endswith("__inferred_module.map")) {
476+
if (StringRef(IFI.Filename).endswith("__inferred_module.map")) {
476477
MDC.addFileDep(MD, ModuleMap->getName());
477478
return;
478479
}
479-
MDC.addFileDep(MD, IF.getFile()->getName());
480+
MDC.addFileDep(MD, IFI.Filename);
480481
});
481482

482483
llvm::DenseSet<const Module *> SeenDeps;
483484
addAllSubmodulePrebuiltDeps(M, MD, SeenDeps);
484485
addAllSubmoduleDeps(M, MD, SeenDeps);
485486
addAllAffectingModules(M, MD, SeenDeps);
486487

487-
MDC.ScanInstance.getASTReader()->visitTopLevelModuleMaps(
488-
*MF, [&](FileEntryRef FE) {
489-
if (FE.getNameAsRequested().endswith("__inferred_module.map"))
488+
MDC.ScanInstance.getASTReader()->visitInputFileInfos(
489+
*MF, /*IncludeSystem=*/true,
490+
[&](const serialization::InputFileInfo &IFI, bool IsSystem) {
491+
if (!(IFI.TopLevel && IFI.ModuleMap))
490492
return;
491-
MD.ModuleMapFileDeps.emplace_back(FE.getNameAsRequested());
493+
if (StringRef(IFI.FilenameAsRequested)
494+
.endswith("__inferred_module.map"))
495+
return;
496+
MD.ModuleMapFileDeps.emplace_back(IFI.FilenameAsRequested);
492497
});
493498

494499
if (!MF->IncludeTreeID.empty())

0 commit comments

Comments
 (0)