Skip to content

Commit c4436f6

Browse files
committed
[clang] Use InMemoryModuleCache for readASTFileControlBlock NFC
When a pcm has already been loaded from disk, reuse it from the InMemoryModuleCache in readASTFileControlBlock. This avoids potentially reading it again. As noted in the FIXME, ideally we would also add the module to the cache if it will be used again later, but that could modify its build state and we do not have enough context currenlty to know if it's correct. Differential Revision: https://reviews.llvm.org/D138160
1 parent 7052164 commit c4436f6

File tree

6 files changed

+39
-24
lines changed

6 files changed

+39
-24
lines changed

clang/include/clang/Serialization/ASTReader.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,16 +1731,17 @@ class ASTReader
17311731
/// Read the control block for the named AST file.
17321732
///
17331733
/// \returns true if an error occurred, false otherwise.
1734-
static bool
1735-
readASTFileControlBlock(StringRef Filename, FileManager &FileMgr,
1736-
const PCHContainerReader &PCHContainerRdr,
1737-
bool FindModuleFileExtensions,
1738-
ASTReaderListener &Listener,
1739-
bool ValidateDiagnosticOptions);
1734+
static bool readASTFileControlBlock(StringRef Filename, FileManager &FileMgr,
1735+
const InMemoryModuleCache &ModuleCache,
1736+
const PCHContainerReader &PCHContainerRdr,
1737+
bool FindModuleFileExtensions,
1738+
ASTReaderListener &Listener,
1739+
bool ValidateDiagnosticOptions);
17401740

17411741
/// Determine whether the given AST file is acceptable to load into a
17421742
/// translation unit with the given language and target options.
17431743
static bool isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
1744+
const InMemoryModuleCache &ModuleCache,
17441745
const PCHContainerReader &PCHContainerRdr,
17451746
const LangOptions &LangOpts,
17461747
const TargetOptions &TargetOpts,

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,8 @@ static void collectIncludePCH(CompilerInstance &CI,
252252
// used here since we're not interested in validating the PCH at this time,
253253
// but only to check whether this is a file containing an AST.
254254
if (!ASTReader::readASTFileControlBlock(
255-
Dir->path(), FileMgr, CI.getPCHContainerReader(),
255+
Dir->path(), FileMgr, CI.getModuleCache(),
256+
CI.getPCHContainerReader(),
256257
/*FindModuleFileExtensions=*/false, Validator,
257258
/*ValidateDiagnosticOptions=*/false))
258259
MDC->addFile(Dir->path());

clang/lib/Frontend/FrontendAction.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,8 +778,9 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
778778
Dir != DirEnd && !EC; Dir.increment(EC)) {
779779
// Check whether this is an acceptable AST file.
780780
if (ASTReader::isAcceptableASTFile(
781-
Dir->path(), FileMgr, CI.getPCHContainerReader(),
782-
CI.getLangOpts(), CI.getTargetOpts(), CI.getPreprocessorOpts(),
781+
Dir->path(), FileMgr, CI.getModuleCache(),
782+
CI.getPCHContainerReader(), CI.getLangOpts(),
783+
CI.getTargetOpts(), CI.getPreprocessorOpts(),
783784
SpecificModuleCachePath, /*RequireStrictOptionMatches=*/true)) {
784785
PPOpts.ImplicitPCHInclude = std::string(Dir->path());
785786
Found = true;

clang/lib/Frontend/FrontendActions.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,8 @@ void DumpModuleInfoAction::ExecuteAction() {
851851
assert(isCurrentFileAST() && "dumping non-AST?");
852852
// Set up the output file.
853853
std::unique_ptr<llvm::raw_fd_ostream> OutFile;
854-
StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile;
854+
CompilerInstance &CI = getCompilerInstance();
855+
StringRef OutputFileName = CI.getFrontendOpts().OutputFile;
855856
if (!OutputFileName.empty() && OutputFileName != "-") {
856857
std::error_code EC;
857858
OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC,
@@ -861,14 +862,14 @@ void DumpModuleInfoAction::ExecuteAction() {
861862
llvm::raw_ostream &Out = OutputStream ? *OutputStream : llvm::outs();
862863

863864
Out << "Information for module file '" << getCurrentFile() << "':\n";
864-
auto &FileMgr = getCompilerInstance().getFileManager();
865+
auto &FileMgr = CI.getFileManager();
865866
auto Buffer = FileMgr.getBufferForFile(getCurrentFile());
866867
StringRef Magic = (*Buffer)->getMemBufferRef().getBuffer();
867868
bool IsRaw = (Magic.size() >= 4 && Magic[0] == 'C' && Magic[1] == 'P' &&
868869
Magic[2] == 'C' && Magic[3] == 'H');
869870
Out << " Module format: " << (IsRaw ? "raw" : "obj") << "\n";
870871

871-
Preprocessor &PP = getCompilerInstance().getPreprocessor();
872+
Preprocessor &PP = CI.getPreprocessor();
872873
DumpModuleInfoListener Listener(Out);
873874
HeaderSearchOptions &HSOpts = PP.getHeaderSearchInfo().getHeaderSearchOpts();
874875

@@ -966,7 +967,8 @@ void DumpModuleInfoAction::ExecuteAction() {
966967
// The reminder of the output is produced from the listener as the AST
967968
// FileCcontrolBlock is (re-)parsed.
968969
ASTReader::readASTFileControlBlock(
969-
getCurrentFile(), FileMgr, getCompilerInstance().getPCHContainerReader(),
970+
getCurrentFile(), FileMgr, CI.getModuleCache(),
971+
CI.getPCHContainerReader(),
970972
/*FindModuleFileExtensions=*/true, Listener,
971973
HSOpts.ModulesValidateDiagnosticOptions);
972974
}

clang/lib/Serialization/ASTReader.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5169,19 +5169,28 @@ namespace {
51695169

51705170
bool ASTReader::readASTFileControlBlock(
51715171
StringRef Filename, FileManager &FileMgr,
5172-
const PCHContainerReader &PCHContainerRdr,
5173-
bool FindModuleFileExtensions,
5172+
const InMemoryModuleCache &ModuleCache,
5173+
const PCHContainerReader &PCHContainerRdr, bool FindModuleFileExtensions,
51745174
ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
51755175
// Open the AST file.
5176-
// FIXME: This allows use of the VFS; we do not allow use of the
5177-
// VFS when actually loading a module.
5178-
auto Buffer = FileMgr.getBufferForFile(Filename);
5176+
std::unique_ptr<llvm::MemoryBuffer> OwnedBuffer;
5177+
llvm::MemoryBuffer *Buffer = ModuleCache.lookupPCM(Filename);
51795178
if (!Buffer) {
5180-
return true;
5179+
// FIXME: We should add the pcm to the InMemoryModuleCache if it could be
5180+
// read again later, but we do not have the context here to determine if it
5181+
// is safe to change the result of InMemoryModuleCache::getPCMState().
5182+
5183+
// FIXME: This allows use of the VFS; we do not allow use of the
5184+
// VFS when actually loading a module.
5185+
auto BufferOrErr = FileMgr.getBufferForFile(Filename);
5186+
if (!BufferOrErr)
5187+
return true;
5188+
OwnedBuffer = std::move(*BufferOrErr);
5189+
Buffer = OwnedBuffer.get();
51815190
}
51825191

51835192
// Initialize the stream
5184-
StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5193+
StringRef Bytes = PCHContainerRdr.ExtractPCH(*Buffer);
51855194
BitstreamCursor Stream(Bytes);
51865195

51875196
// Sniff for the signature.
@@ -5434,6 +5443,7 @@ bool ASTReader::readASTFileControlBlock(
54345443
}
54355444

54365445
bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5446+
const InMemoryModuleCache &ModuleCache,
54375447
const PCHContainerReader &PCHContainerRdr,
54385448
const LangOptions &LangOpts,
54395449
const TargetOptions &TargetOpts,
@@ -5443,9 +5453,9 @@ bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
54435453
SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
54445454
ExistingModuleCachePath, FileMgr,
54455455
RequireStrictOptionMatches);
5446-
return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5447-
/*FindModuleFileExtensions=*/false,
5448-
validator,
5456+
return !readASTFileControlBlock(Filename, FileMgr, ModuleCache,
5457+
PCHContainerRdr,
5458+
/*FindModuleFileExtensions=*/false, validator,
54495459
/*ValidateDiagnosticOptions=*/true);
54505460
}
54515461

clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static void visitPrebuiltModule(StringRef PrebuiltModuleFilename,
103103

104104
while (!Worklist.empty())
105105
ASTReader::readASTFileControlBlock(
106-
Worklist.pop_back_val(), CI.getFileManager(),
106+
Worklist.pop_back_val(), CI.getFileManager(), CI.getModuleCache(),
107107
CI.getPCHContainerReader(),
108108
/*FindModuleFileExtensions=*/false, Listener,
109109
/*ValidateDiagnosticOptions=*/false);

0 commit comments

Comments
 (0)