[EmitPCH] Do not cache PCH in memory during emit-pch action

When pch are explicitly created, there is no need to cached produced pch
in memory since pch is only going to be consumed by a later process.
This commit is contained in:
Steven Wu
2023-05-12 12:37:11 -07:00
parent ee5b3b134b
commit 359c37eba7
4 changed files with 16 additions and 12 deletions

View File

@@ -396,7 +396,8 @@ public:
/// replica.
///
/// \sa clang::GeneratePCHAction
bool emitBridgingPCH(StringRef headerPath, StringRef outputPCHPath);
bool emitBridgingPCH(StringRef headerPath, StringRef outputPCHPath,
bool cached);
/// Returns true if a clang CompilerInstance can successfully read in a PCH,
/// assuming it exists, with the current options. This can be used to find out
@@ -505,7 +506,7 @@ public:
Optional<std::string>
getOrCreatePCH(const ClangImporterOptions &ImporterOptions,
StringRef SwiftPCHHash);
StringRef SwiftPCHHash, bool Cached);
Optional<std::string>
/// \param isExplicit true if the PCH filename was passed directly
/// with -import-objc-header option.

View File

@@ -194,7 +194,8 @@ namespace {
Importer.addSearchPath(path, /*isFramework*/false, /*isSystem=*/false);
}
auto PCH = Importer.getOrCreatePCH(ImporterOpts, SwiftPCHHash);
auto PCH =
Importer.getOrCreatePCH(ImporterOpts, SwiftPCHHash, /*Cached=*/true);
if (PCH.has_value()) {
Impl.getClangInstance()->getPreprocessorOpts().ImplicitPCHInclude =
PCH.value();
@@ -942,8 +943,9 @@ ClangImporter::getPCHFilename(const ClangImporterOptions &ImporterOptions,
return PCHFilename.str().str();
}
Optional<std::string> ClangImporter::getOrCreatePCH(
const ClangImporterOptions &ImporterOptions, StringRef SwiftPCHHash) {
Optional<std::string>
ClangImporter::getOrCreatePCH(const ClangImporterOptions &ImporterOptions,
StringRef SwiftPCHHash, bool Cached) {
bool isExplicit;
auto PCHFilename = getPCHFilename(ImporterOptions, SwiftPCHHash,
isExplicit);
@@ -959,8 +961,8 @@ Optional<std::string> ClangImporter::getOrCreatePCH(
<< EC.message();
return None;
}
auto FailedToEmit =
emitBridgingPCH(ImporterOptions.BridgingHeader, PCHFilename.value());
auto FailedToEmit = emitBridgingPCH(ImporterOptions.BridgingHeader,
PCHFilename.value(), Cached);
if (FailedToEmit) {
return None;
}
@@ -1702,13 +1704,13 @@ ClangImporter::cloneCompilerInstanceForPrecompiling() {
}
bool ClangImporter::emitBridgingPCH(
StringRef headerPath, StringRef outputPCHPath) {
StringRef headerPath, StringRef outputPCHPath, bool cached) {
auto emitInstance = cloneCompilerInstanceForPrecompiling();
auto &invocation = emitInstance->getInvocation();
auto LangOpts = invocation.getLangOpts();
LangOpts->NeededByPCHOrCompilationUsesPCH = true;
LangOpts->CacheGeneratedPCH = true;
LangOpts->CacheGeneratedPCH = cached;
auto language = getLanguageFromOptions(LangOpts);
auto inputFile = clang::FrontendInputFile(headerPath, language);

View File

@@ -369,12 +369,13 @@ static bool precompileBridgingHeader(const CompilerInstance &Instance) {
if (!PCHOutDir.empty()) {
// Create or validate a persistent PCH.
auto SwiftPCHHash = Invocation.getPCHHash();
auto PCH = clangImporter->getOrCreatePCH(ImporterOpts, SwiftPCHHash);
auto PCH = clangImporter->getOrCreatePCH(ImporterOpts, SwiftPCHHash,
/*cached=*/false);
return !PCH.has_value();
}
return clangImporter->emitBridgingPCH(
opts.InputsAndOutputs.getFilenameOfFirstInput(),
opts.InputsAndOutputs.getSingleOutputFilename());
opts.InputsAndOutputs.getSingleOutputFilename(), /*cached=*/false);
}
static bool precompileClangModule(const CompilerInstance &Instance) {

View File

@@ -86,7 +86,7 @@ TEST(ClangImporterTest, emitPCHInMemory) {
std::string PCH = createFilename(cache, "bridging.h.pch");
ASSERT_FALSE(importer->canReadPCH(PCH));
ASSERT_FALSE(importer->emitBridgingPCH(options.BridgingHeader, PCH));
ASSERT_FALSE(importer->emitBridgingPCH(options.BridgingHeader, PCH, true));
ASSERT_TRUE(importer->canReadPCH(PCH));
// Overwrite the PCH with garbage. We should still be able to read it from