Skip to content

Commit 66cf61a

Browse files
committed
[clang][serialization] NFCI: Avoid re-reading input file info
This patch resolves a FIXME that points out an inefficiency in first deserializing the input file info and the whole input file, which redundantly deserializes the input file info again. Reviewed By: Bigcheese Differential Revision: https://reviews.llvm.org/D137192
1 parent 935a07e commit 66cf61a

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

clang/include/clang/Serialization/ASTReader.h

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,18 +1247,8 @@ class ASTReader
12471247
/// Reads a statement from the specified cursor.
12481248
Stmt *ReadStmtFromStream(ModuleFile &F);
12491249

1250-
struct InputFileInfo {
1251-
std::string Filename;
1252-
uint64_t ContentHash;
1253-
off_t StoredSize;
1254-
time_t StoredTime;
1255-
bool Overridden;
1256-
bool Transient;
1257-
bool TopLevelModuleMap;
1258-
};
1259-
1260-
/// Reads the stored information about an input file.
1261-
InputFileInfo readInputFileInfo(ModuleFile &F, unsigned ID);
1250+
/// Retrieve the stored information about an input file.
1251+
serialization::InputFileInfo getInputFileInfo(ModuleFile &F, unsigned ID);
12621252

12631253
/// Retrieve the file entry and 'overridden' bit for an input
12641254
/// file in the given module file.

clang/include/clang/Serialization/ModuleFile.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ enum ModuleKind {
5959
MK_PrebuiltModule
6060
};
6161

62+
/// The input file info that has been loaded from an AST file.
63+
struct InputFileInfo {
64+
std::string Filename;
65+
uint64_t ContentHash;
66+
off_t StoredSize;
67+
time_t StoredTime;
68+
bool Overridden;
69+
bool Transient;
70+
bool TopLevelModuleMap;
71+
};
72+
6273
/// The input file that has been loaded from this AST file, along with
6374
/// bools indicating whether this was an overridden buffer or if it was
6475
/// out-of-date or not-found.
@@ -235,6 +246,9 @@ class ModuleFile {
235246
/// The input files that have been loaded from this AST file.
236247
std::vector<InputFile> InputFilesLoaded;
237248

249+
/// The input file infos that have been loaded from this AST file.
250+
std::vector<InputFileInfo> InputFileInfosLoaded;
251+
238252
// All user input files reside at the index range [0, NumUserInputFiles), and
239253
// system input files reside at [NumUserInputFiles, InputFilesLoaded.size()).
240254
unsigned NumUserInputFiles = 0;

clang/lib/Serialization/ASTReader.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,8 +2266,15 @@ bool ASTReader::shouldDisableValidationForFile(
22662266
return false;
22672267
}
22682268

2269-
ASTReader::InputFileInfo
2270-
ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2269+
InputFileInfo ASTReader::getInputFileInfo(ModuleFile &F, unsigned ID) {
2270+
// If this ID is bogus, just return an empty input file.
2271+
if (ID == 0 || ID > F.InputFileInfosLoaded.size())
2272+
return InputFileInfo();
2273+
2274+
// If we've already loaded this input file, return it.
2275+
if (!F.InputFileInfosLoaded[ID - 1].Filename.empty())
2276+
return F.InputFileInfosLoaded[ID - 1];
2277+
22712278
// Go find this input file.
22722279
BitstreamCursor &Cursor = F.InputFilesCursor;
22732280
SavedStreamPosition SavedPosition(Cursor);
@@ -2320,6 +2327,9 @@ ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
23202327
}
23212328
R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
23222329
static_cast<uint64_t>(Record[0]);
2330+
2331+
// Note that we've loaded this input file info.
2332+
F.InputFileInfosLoaded[ID - 1] = R;
23232333
return R;
23242334
}
23252335

@@ -2344,7 +2354,7 @@ InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
23442354
consumeError(std::move(Err));
23452355
}
23462356

2347-
InputFileInfo FI = readInputFileInfo(F, ID);
2357+
InputFileInfo FI = getInputFileInfo(F, ID);
23482358
off_t StoredSize = FI.StoredSize;
23492359
time_t StoredTime = FI.StoredTime;
23502360
bool Overridden = FI.Overridden;
@@ -2691,7 +2701,7 @@ ASTReader::ReadControlBlock(ModuleFile &F,
26912701
: NumUserInputs;
26922702
for (unsigned I = 0; I < N; ++I) {
26932703
bool IsSystem = I >= NumUserInputs;
2694-
InputFileInfo FI = readInputFileInfo(F, I+1);
2704+
InputFileInfo FI = getInputFileInfo(F, I + 1);
26952705
Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
26962706
F.Kind == MK_ExplicitModule ||
26972707
F.Kind == MK_PrebuiltModule);
@@ -2968,6 +2978,7 @@ ASTReader::ReadControlBlock(ModuleFile &F,
29682978
F.InputFileOffsets =
29692979
(const llvm::support::unaligned_uint64_t *)Blob.data();
29702980
F.InputFilesLoaded.resize(NumInputs);
2981+
F.InputFileInfosLoaded.resize(NumInputs);
29712982
F.NumUserInputFiles = NumUserInputs;
29722983
break;
29732984
}
@@ -2983,7 +2994,7 @@ void ASTReader::readIncludedFiles(ModuleFile &F, StringRef Blob,
29832994

29842995
for (unsigned I = 0; I < FileCount; ++I) {
29852996
size_t ID = endian::readNext<uint32_t, little, unaligned>(D);
2986-
InputFileInfo IFI = readInputFileInfo(F, ID);
2997+
InputFileInfo IFI = getInputFileInfo(F, ID);
29872998
if (llvm::ErrorOr<const FileEntry *> File =
29882999
PP.getFileManager().getFile(IFI.Filename))
29893000
PP.getIncludedFiles().insert(*File);
@@ -9217,9 +9228,8 @@ void ASTReader::visitTopLevelModuleMaps(
92179228
llvm::function_ref<void(FileEntryRef FE)> Visitor) {
92189229
unsigned NumInputs = MF.InputFilesLoaded.size();
92199230
for (unsigned I = 0; I < NumInputs; ++I) {
9220-
InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9231+
InputFileInfo IFI = getInputFileInfo(MF, I + 1);
92219232
if (IFI.TopLevelModuleMap)
9222-
// FIXME: This unnecessarily re-reads the InputFileInfo.
92239233
if (auto FE = getInputFile(MF, I + 1).getFile())
92249234
Visitor(*FE);
92259235
}

0 commit comments

Comments
 (0)