Skip to content

Commit 193b886

Browse files
Merge pull request swiftlang#75443 from cachemeifyoucan/eng/PR-13225588
[Caching] Avoid using clang CAS options in the command-line
2 parents d065a3e + 23b0d80 commit 193b886

File tree

3 files changed

+87
-26
lines changed

3 files changed

+87
-26
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "swift/Basic/Version.h"
4747
#include "swift/ClangImporter/ClangImporterRequests.h"
4848
#include "swift/ClangImporter/ClangModule.h"
49+
#include "swift/Option/Options.h"
4950
#include "swift/Parse/Lexer.h"
5051
#include "swift/Parse/ParseVersion.h"
5152
#include "swift/Parse/Parser.h"
@@ -65,6 +66,7 @@
6566
#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
6667
#include "clang/Frontend/CompilerInvocation.h"
6768
#include "clang/Frontend/FrontendActions.h"
69+
#include "clang/Frontend/FrontendOptions.h"
6870
#include "clang/Frontend/IncludeTreePPActions.h"
6971
#include "clang/Frontend/TextDiagnosticPrinter.h"
7072
#include "clang/Frontend/Utils.h"
@@ -753,31 +755,6 @@ void importer::getNormalInvocationArguments(
753755
invocationArgStrs.push_back((llvm::Twine(searchPathOpts.RuntimeResourcePath) +
754756
llvm::sys::path::get_separator() +
755757
"apinotes").str());
756-
757-
auto CASOpts = ctx.CASOpts;
758-
if (CASOpts.EnableCaching) {
759-
invocationArgStrs.push_back("-Xclang");
760-
invocationArgStrs.push_back("-fno-pch-timestamp");
761-
if (!CASOpts.CASOpts.CASPath.empty()) {
762-
invocationArgStrs.push_back("-Xclang");
763-
invocationArgStrs.push_back("-fcas-path");
764-
invocationArgStrs.push_back("-Xclang");
765-
invocationArgStrs.push_back(CASOpts.CASOpts.CASPath);
766-
}
767-
if (!CASOpts.CASOpts.PluginPath.empty()) {
768-
invocationArgStrs.push_back("-Xclang");
769-
invocationArgStrs.push_back("-fcas-plugin-path");
770-
invocationArgStrs.push_back("-Xclang");
771-
invocationArgStrs.push_back(CASOpts.CASOpts.PluginPath);
772-
for (auto Opt : CASOpts.CASOpts.PluginOptions) {
773-
invocationArgStrs.push_back("-Xclang");
774-
invocationArgStrs.push_back("-fcas-plugin-option");
775-
invocationArgStrs.push_back("-Xclang");
776-
invocationArgStrs.push_back(
777-
(llvm::Twine(Opt.first) + "=" + Opt.second).str());
778-
}
779-
}
780-
}
781758
}
782759

783760
static void
@@ -1170,8 +1147,21 @@ std::optional<std::vector<std::string>> ClangImporter::getClangCC1Arguments(
11701147
// to reduce the number of argument passing on the command-line and swift
11711148
// compiler can be more efficient to compute swift cache key without having
11721149
// the knowledge about clang command-line options.
1173-
if (ctx.CASOpts.EnableCaching)
1150+
if (ctx.CASOpts.EnableCaching) {
11741151
CI->getCASOpts() = ctx.CASOpts.CASOpts;
1152+
// When clangImporter is used to compile (generate .pcm or .pch), need to
1153+
// inherit the include tree from swift args (last one wins) and clear the
1154+
// input file.
1155+
if ((CI->getFrontendOpts().ProgramAction ==
1156+
clang::frontend::ActionKind::GenerateModule ||
1157+
CI->getFrontendOpts().ProgramAction ==
1158+
clang::frontend::ActionKind::GeneratePCH) &&
1159+
ctx.ClangImporterOpts.HasClangIncludeTreeRoot) {
1160+
CI->getFrontendOpts().CASIncludeTreeID =
1161+
ctx.CASOpts.ClangIncludeTrees.back();
1162+
CI->getFrontendOpts().Inputs.clear();
1163+
}
1164+
}
11751165

11761166
// If clang target is ignored, using swift target.
11771167
if (ignoreClangTarget)

lib/ClangImporter/ClangModuleDependencyScanner.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/ClangImporter/ClangImporter.h"
2121
#include "swift/Basic/Assertions.h"
2222
#include "clang/Basic/Diagnostic.h"
23+
#include "clang/CAS/CASOptions.h"
2324
#include "clang/Frontend/CompilerInvocation.h"
2425
#include "clang/Frontend/FrontendOptions.h"
2526
#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
@@ -242,6 +243,10 @@ ModuleDependencyVector ClangImporter::bridgeClangModuleDependencies(
242243
depsInvocation.getFrontendOpts().PathPrefixMappings.clear();
243244
depsInvocation.getFrontendOpts().OutputFile.clear();
244245

246+
// Reset CASOptions since that should be coming from swift.
247+
depsInvocation.getCASOpts() = clang::CASOptions();
248+
depsInvocation.getFrontendOpts().CASIncludeTreeID.clear();
249+
245250
// FIXME: workaround for rdar://105684525: find the -ivfsoverlay option
246251
// from clang scanner and pass to swift.
247252
for (auto overlay : depsInvocation.getHeaderSearchOpts().VFSOverlayFiles) {

test/CAS/no-cas-path-dependency.swift

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file %s %t
3+
4+
// RUN: %target-swift-frontend -scan-dependencies -module-name Test -module-cache-path %t/clang-module-cache -O \
5+
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -parse-stdlib \
6+
// RUN: %t/main.swift -o %t/deps.json -swift-version 5 -cache-compile-job -cas-path %t/cas -I %t/include
7+
8+
// RUN: %{python} %S/Inputs/BuildCommandExtractor.py %t/deps.json clang:A > %t/A.cmd
9+
// RUN: %swift_frontend_plain @%t/A.cmd -Rcache-compile-job 2>&1 | %FileCheck %s --check-prefix=CACHE-MISS
10+
// RUN: %{python} %S/Inputs/BuildCommandExtractor.py %t/deps.json B > %t/B.cmd
11+
// RUN: %swift_frontend_plain @%t/B.cmd -Rcache-compile-job 2>&1 | %FileCheck %s --check-prefix=CACHE-MISS
12+
13+
// RUN: %{python} %S/Inputs/GenerateExplicitModuleMap.py %t/deps.json > %t/map.json
14+
// RUN: llvm-cas --cas %t/cas --make-blob --data %t/map.json > %t/map.casid
15+
// RUN: %{python} %S/Inputs/BuildCommandExtractor.py %t/deps.json Test > %t/MyApp.cmd
16+
17+
// RUN: %target-swift-frontend \
18+
// RUN: -c -cache-compile-job -cas-path %t/cas \
19+
// RUN: -swift-version 5 -disable-implicit-swift-modules \
20+
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -parse-stdlib \
21+
// RUN: -module-name Test -explicit-swift-module-map-file @%t/map.casid \
22+
// RUN: %t/main.swift @%t/MyApp.cmd -Rcache-compile-job 2>&1 | %FileCheck %s --check-prefix=CACHE-MISS
23+
24+
// CACHE-MISS: remark: cache miss for input
25+
// CACHE-HIT: remark: replay output file
26+
// CACHE-HIT-NOT: remark: cache miss for input
27+
28+
// RUN: mv %t/cas %t/cas-2
29+
30+
// RUN: %target-swift-frontend -scan-dependencies -module-name Test -module-cache-path %t/clang-module-cache -O \
31+
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -parse-stdlib \
32+
// RUN: %t/main.swift -o %t/deps-2.json -swift-version 5 -cache-compile-job -cas-path %t/cas-2 -I %t/include
33+
34+
// RUN: %{python} %S/Inputs/BuildCommandExtractor.py %t/deps-2.json clang:A > %t/A-2.cmd
35+
// RUN: %swift_frontend_plain @%t/A-2.cmd -Rcache-compile-job 2>&1 | %FileCheck %s --check-prefix=CACHE-HIT
36+
// RUN: %{python} %S/Inputs/BuildCommandExtractor.py %t/deps-2.json B > %t/B-2.cmd
37+
// RUN: %swift_frontend_plain @%t/B-2.cmd -Rcache-compile-job 2>&1 | %FileCheck %s --check-prefix=CACHE-HIT
38+
39+
// RUN: %{python} %S/Inputs/GenerateExplicitModuleMap.py %t/deps-2.json > %t/map-2.json
40+
// RUN: llvm-cas --cas %t/cas-2 --make-blob --data %t/map-2.json > %t/map-2.casid
41+
// RUN: %{python} %S/Inputs/BuildCommandExtractor.py %t/deps-2.json Test > %t/MyApp-2.cmd
42+
43+
// RUN: %target-swift-frontend \
44+
// RUN: -c -cache-compile-job -cas-path %t/cas-2 \
45+
// RUN: -swift-version 5 -disable-implicit-swift-modules \
46+
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -parse-stdlib \
47+
// RUN: -module-name Test -explicit-swift-module-map-file @%t/map-2.casid \
48+
// RUN: %t/main.swift @%t/MyApp-2.cmd -Rcache-compile-job 2>&1 | %FileCheck %s --check-prefix=CACHE-HIT
49+
50+
//--- main.swift
51+
import A
52+
import B
53+
54+
//--- include/A.h
55+
void a(void);
56+
57+
//--- include/module.modulemap
58+
module A {
59+
header "A.h"
60+
export *
61+
}
62+
63+
//--- include/B.swiftinterface
64+
// swift-interface-format-version: 1.0
65+
// swift-module-flags: -module-name B -O -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -parse-stdlib -user-module-version 1.0
66+
public func c() { }

0 commit comments

Comments
 (0)