[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
This commit is contained in:
Ben Langmuir 2022-11-16 16:07:34 -08:00
parent 7052164f98
commit c4436f675d
6 changed files with 39 additions and 24 deletions

View File

@ -1731,8 +1731,8 @@ public:
/// Read the control block for the named AST file. /// Read the control block for the named AST file.
/// ///
/// \returns true if an error occurred, false otherwise. /// \returns true if an error occurred, false otherwise.
static bool static bool readASTFileControlBlock(StringRef Filename, FileManager &FileMgr,
readASTFileControlBlock(StringRef Filename, FileManager &FileMgr, const InMemoryModuleCache &ModuleCache,
const PCHContainerReader &PCHContainerRdr, const PCHContainerReader &PCHContainerRdr,
bool FindModuleFileExtensions, bool FindModuleFileExtensions,
ASTReaderListener &Listener, ASTReaderListener &Listener,
@ -1741,6 +1741,7 @@ public:
/// Determine whether the given AST file is acceptable to load into a /// Determine whether the given AST file is acceptable to load into a
/// translation unit with the given language and target options. /// translation unit with the given language and target options.
static bool isAcceptableASTFile(StringRef Filename, FileManager &FileMgr, static bool isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
const InMemoryModuleCache &ModuleCache,
const PCHContainerReader &PCHContainerRdr, const PCHContainerReader &PCHContainerRdr,
const LangOptions &LangOpts, const LangOptions &LangOpts,
const TargetOptions &TargetOpts, const TargetOptions &TargetOpts,

View File

@ -252,7 +252,8 @@ static void collectIncludePCH(CompilerInstance &CI,
// used here since we're not interested in validating the PCH at this time, // used here since we're not interested in validating the PCH at this time,
// but only to check whether this is a file containing an AST. // but only to check whether this is a file containing an AST.
if (!ASTReader::readASTFileControlBlock( if (!ASTReader::readASTFileControlBlock(
Dir->path(), FileMgr, CI.getPCHContainerReader(), Dir->path(), FileMgr, CI.getModuleCache(),
CI.getPCHContainerReader(),
/*FindModuleFileExtensions=*/false, Validator, /*FindModuleFileExtensions=*/false, Validator,
/*ValidateDiagnosticOptions=*/false)) /*ValidateDiagnosticOptions=*/false))
MDC->addFile(Dir->path()); MDC->addFile(Dir->path());

View File

@ -778,8 +778,9 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
Dir != DirEnd && !EC; Dir.increment(EC)) { Dir != DirEnd && !EC; Dir.increment(EC)) {
// Check whether this is an acceptable AST file. // Check whether this is an acceptable AST file.
if (ASTReader::isAcceptableASTFile( if (ASTReader::isAcceptableASTFile(
Dir->path(), FileMgr, CI.getPCHContainerReader(), Dir->path(), FileMgr, CI.getModuleCache(),
CI.getLangOpts(), CI.getTargetOpts(), CI.getPreprocessorOpts(), CI.getPCHContainerReader(), CI.getLangOpts(),
CI.getTargetOpts(), CI.getPreprocessorOpts(),
SpecificModuleCachePath, /*RequireStrictOptionMatches=*/true)) { SpecificModuleCachePath, /*RequireStrictOptionMatches=*/true)) {
PPOpts.ImplicitPCHInclude = std::string(Dir->path()); PPOpts.ImplicitPCHInclude = std::string(Dir->path());
Found = true; Found = true;

View File

@ -851,7 +851,8 @@ void DumpModuleInfoAction::ExecuteAction() {
assert(isCurrentFileAST() && "dumping non-AST?"); assert(isCurrentFileAST() && "dumping non-AST?");
// Set up the output file. // Set up the output file.
std::unique_ptr<llvm::raw_fd_ostream> OutFile; std::unique_ptr<llvm::raw_fd_ostream> OutFile;
StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile; CompilerInstance &CI = getCompilerInstance();
StringRef OutputFileName = CI.getFrontendOpts().OutputFile;
if (!OutputFileName.empty() && OutputFileName != "-") { if (!OutputFileName.empty() && OutputFileName != "-") {
std::error_code EC; std::error_code EC;
OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC, OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC,
@ -861,14 +862,14 @@ void DumpModuleInfoAction::ExecuteAction() {
llvm::raw_ostream &Out = OutputStream ? *OutputStream : llvm::outs(); llvm::raw_ostream &Out = OutputStream ? *OutputStream : llvm::outs();
Out << "Information for module file '" << getCurrentFile() << "':\n"; Out << "Information for module file '" << getCurrentFile() << "':\n";
auto &FileMgr = getCompilerInstance().getFileManager(); auto &FileMgr = CI.getFileManager();
auto Buffer = FileMgr.getBufferForFile(getCurrentFile()); auto Buffer = FileMgr.getBufferForFile(getCurrentFile());
StringRef Magic = (*Buffer)->getMemBufferRef().getBuffer(); StringRef Magic = (*Buffer)->getMemBufferRef().getBuffer();
bool IsRaw = (Magic.size() >= 4 && Magic[0] == 'C' && Magic[1] == 'P' && bool IsRaw = (Magic.size() >= 4 && Magic[0] == 'C' && Magic[1] == 'P' &&
Magic[2] == 'C' && Magic[3] == 'H'); Magic[2] == 'C' && Magic[3] == 'H');
Out << " Module format: " << (IsRaw ? "raw" : "obj") << "\n"; Out << " Module format: " << (IsRaw ? "raw" : "obj") << "\n";
Preprocessor &PP = getCompilerInstance().getPreprocessor(); Preprocessor &PP = CI.getPreprocessor();
DumpModuleInfoListener Listener(Out); DumpModuleInfoListener Listener(Out);
HeaderSearchOptions &HSOpts = PP.getHeaderSearchInfo().getHeaderSearchOpts(); HeaderSearchOptions &HSOpts = PP.getHeaderSearchInfo().getHeaderSearchOpts();
@ -966,7 +967,8 @@ void DumpModuleInfoAction::ExecuteAction() {
// The reminder of the output is produced from the listener as the AST // The reminder of the output is produced from the listener as the AST
// FileCcontrolBlock is (re-)parsed. // FileCcontrolBlock is (re-)parsed.
ASTReader::readASTFileControlBlock( ASTReader::readASTFileControlBlock(
getCurrentFile(), FileMgr, getCompilerInstance().getPCHContainerReader(), getCurrentFile(), FileMgr, CI.getModuleCache(),
CI.getPCHContainerReader(),
/*FindModuleFileExtensions=*/true, Listener, /*FindModuleFileExtensions=*/true, Listener,
HSOpts.ModulesValidateDiagnosticOptions); HSOpts.ModulesValidateDiagnosticOptions);
} }

View File

@ -5169,19 +5169,28 @@ namespace {
bool ASTReader::readASTFileControlBlock( bool ASTReader::readASTFileControlBlock(
StringRef Filename, FileManager &FileMgr, StringRef Filename, FileManager &FileMgr,
const PCHContainerReader &PCHContainerRdr, const InMemoryModuleCache &ModuleCache,
bool FindModuleFileExtensions, const PCHContainerReader &PCHContainerRdr, bool FindModuleFileExtensions,
ASTReaderListener &Listener, bool ValidateDiagnosticOptions) { ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
// Open the AST file. // Open the AST file.
std::unique_ptr<llvm::MemoryBuffer> OwnedBuffer;
llvm::MemoryBuffer *Buffer = ModuleCache.lookupPCM(Filename);
if (!Buffer) {
// FIXME: We should add the pcm to the InMemoryModuleCache if it could be
// read again later, but we do not have the context here to determine if it
// is safe to change the result of InMemoryModuleCache::getPCMState().
// FIXME: This allows use of the VFS; we do not allow use of the // FIXME: This allows use of the VFS; we do not allow use of the
// VFS when actually loading a module. // VFS when actually loading a module.
auto Buffer = FileMgr.getBufferForFile(Filename); auto BufferOrErr = FileMgr.getBufferForFile(Filename);
if (!Buffer) { if (!BufferOrErr)
return true; return true;
OwnedBuffer = std::move(*BufferOrErr);
Buffer = OwnedBuffer.get();
} }
// Initialize the stream // Initialize the stream
StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer); StringRef Bytes = PCHContainerRdr.ExtractPCH(*Buffer);
BitstreamCursor Stream(Bytes); BitstreamCursor Stream(Bytes);
// Sniff for the signature. // Sniff for the signature.
@ -5434,6 +5443,7 @@ bool ASTReader::readASTFileControlBlock(
} }
bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr, bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
const InMemoryModuleCache &ModuleCache,
const PCHContainerReader &PCHContainerRdr, const PCHContainerReader &PCHContainerRdr,
const LangOptions &LangOpts, const LangOptions &LangOpts,
const TargetOptions &TargetOpts, const TargetOptions &TargetOpts,
@ -5443,9 +5453,9 @@ bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
ExistingModuleCachePath, FileMgr, ExistingModuleCachePath, FileMgr,
RequireStrictOptionMatches); RequireStrictOptionMatches);
return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr, return !readASTFileControlBlock(Filename, FileMgr, ModuleCache,
/*FindModuleFileExtensions=*/false, PCHContainerRdr,
validator, /*FindModuleFileExtensions=*/false, validator,
/*ValidateDiagnosticOptions=*/true); /*ValidateDiagnosticOptions=*/true);
} }

View File

@ -103,7 +103,7 @@ static void visitPrebuiltModule(StringRef PrebuiltModuleFilename,
while (!Worklist.empty()) while (!Worklist.empty())
ASTReader::readASTFileControlBlock( ASTReader::readASTFileControlBlock(
Worklist.pop_back_val(), CI.getFileManager(), Worklist.pop_back_val(), CI.getFileManager(), CI.getModuleCache(),
CI.getPCHContainerReader(), CI.getPCHContainerReader(),
/*FindModuleFileExtensions=*/false, Listener, /*FindModuleFileExtensions=*/false, Listener,
/*ValidateDiagnosticOptions=*/false); /*ValidateDiagnosticOptions=*/false);