Skip to content

Commit 359c37e

Browse files
[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.
1 parent ee5b3b1 commit 359c37e

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

include/swift/ClangImporter/ClangImporter.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,8 @@ class ClangImporter final : public ClangModuleLoader {
396396
/// replica.
397397
///
398398
/// \sa clang::GeneratePCHAction
399-
bool emitBridgingPCH(StringRef headerPath, StringRef outputPCHPath);
399+
bool emitBridgingPCH(StringRef headerPath, StringRef outputPCHPath,
400+
bool cached);
400401

401402
/// Returns true if a clang CompilerInstance can successfully read in a PCH,
402403
/// assuming it exists, with the current options. This can be used to find out
@@ -505,7 +506,7 @@ class ClangImporter final : public ClangModuleLoader {
505506

506507
Optional<std::string>
507508
getOrCreatePCH(const ClangImporterOptions &ImporterOptions,
508-
StringRef SwiftPCHHash);
509+
StringRef SwiftPCHHash, bool Cached);
509510
Optional<std::string>
510511
/// \param isExplicit true if the PCH filename was passed directly
511512
/// with -import-objc-header option.

lib/ClangImporter/ClangImporter.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ namespace {
194194
Importer.addSearchPath(path, /*isFramework*/false, /*isSystem=*/false);
195195
}
196196

197-
auto PCH = Importer.getOrCreatePCH(ImporterOpts, SwiftPCHHash);
197+
auto PCH =
198+
Importer.getOrCreatePCH(ImporterOpts, SwiftPCHHash, /*Cached=*/true);
198199
if (PCH.has_value()) {
199200
Impl.getClangInstance()->getPreprocessorOpts().ImplicitPCHInclude =
200201
PCH.value();
@@ -942,8 +943,9 @@ ClangImporter::getPCHFilename(const ClangImporterOptions &ImporterOptions,
942943
return PCHFilename.str().str();
943944
}
944945

945-
Optional<std::string> ClangImporter::getOrCreatePCH(
946-
const ClangImporterOptions &ImporterOptions, StringRef SwiftPCHHash) {
946+
Optional<std::string>
947+
ClangImporter::getOrCreatePCH(const ClangImporterOptions &ImporterOptions,
948+
StringRef SwiftPCHHash, bool Cached) {
947949
bool isExplicit;
948950
auto PCHFilename = getPCHFilename(ImporterOptions, SwiftPCHHash,
949951
isExplicit);
@@ -959,8 +961,8 @@ Optional<std::string> ClangImporter::getOrCreatePCH(
959961
<< EC.message();
960962
return None;
961963
}
962-
auto FailedToEmit =
963-
emitBridgingPCH(ImporterOptions.BridgingHeader, PCHFilename.value());
964+
auto FailedToEmit = emitBridgingPCH(ImporterOptions.BridgingHeader,
965+
PCHFilename.value(), Cached);
964966
if (FailedToEmit) {
965967
return None;
966968
}
@@ -1702,13 +1704,13 @@ ClangImporter::cloneCompilerInstanceForPrecompiling() {
17021704
}
17031705

17041706
bool ClangImporter::emitBridgingPCH(
1705-
StringRef headerPath, StringRef outputPCHPath) {
1707+
StringRef headerPath, StringRef outputPCHPath, bool cached) {
17061708
auto emitInstance = cloneCompilerInstanceForPrecompiling();
17071709
auto &invocation = emitInstance->getInvocation();
17081710

17091711
auto LangOpts = invocation.getLangOpts();
17101712
LangOpts->NeededByPCHOrCompilationUsesPCH = true;
1711-
LangOpts->CacheGeneratedPCH = true;
1713+
LangOpts->CacheGeneratedPCH = cached;
17121714

17131715
auto language = getLanguageFromOptions(LangOpts);
17141716
auto inputFile = clang::FrontendInputFile(headerPath, language);

lib/FrontendTool/FrontendTool.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,12 +369,13 @@ static bool precompileBridgingHeader(const CompilerInstance &Instance) {
369369
if (!PCHOutDir.empty()) {
370370
// Create or validate a persistent PCH.
371371
auto SwiftPCHHash = Invocation.getPCHHash();
372-
auto PCH = clangImporter->getOrCreatePCH(ImporterOpts, SwiftPCHHash);
372+
auto PCH = clangImporter->getOrCreatePCH(ImporterOpts, SwiftPCHHash,
373+
/*cached=*/false);
373374
return !PCH.has_value();
374375
}
375376
return clangImporter->emitBridgingPCH(
376377
opts.InputsAndOutputs.getFilenameOfFirstInput(),
377-
opts.InputsAndOutputs.getSingleOutputFilename());
378+
opts.InputsAndOutputs.getSingleOutputFilename(), /*cached=*/false);
378379
}
379380

380381
static bool precompileClangModule(const CompilerInstance &Instance) {

unittests/ClangImporter/ClangImporterTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ TEST(ClangImporterTest, emitPCHInMemory) {
8686

8787
std::string PCH = createFilename(cache, "bridging.h.pch");
8888
ASSERT_FALSE(importer->canReadPCH(PCH));
89-
ASSERT_FALSE(importer->emitBridgingPCH(options.BridgingHeader, PCH));
89+
ASSERT_FALSE(importer->emitBridgingPCH(options.BridgingHeader, PCH, true));
9090
ASSERT_TRUE(importer->canReadPCH(PCH));
9191

9292
// Overwrite the PCH with garbage. We should still be able to read it from

0 commit comments

Comments
 (0)