Skip to content

Commit e20c1b9

Browse files
Merge pull request #71066 from cachemeifyoucan/eng/PR-121291342
2 parents 701549f + 5323983 commit e20c1b9

File tree

4 files changed

+82
-19
lines changed

4 files changed

+82
-19
lines changed

test/CAS/Inputs/PrintResponseFile.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Usage: PrintResponseFile.py [SwiftCommandLine]
4+
5+
import sys
6+
7+
# Grab swift-frontend arguments. argv[0] is python script, argv[1] is
8+
# swift-frontend path, the remaining args are what needs to be used.
9+
cmd = sys.argv[2:]
10+
11+
# Print quoted command-line as response file.
12+
for c in cmd:
13+
print('"{}"'.format(c))
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %{python} %S/Inputs/PrintResponseFile.py %target-swift-frontend -cache-compile-job -Rcache-compile-job %s \
3+
// RUN: -emit-module -emit-module-path %t/Test.swiftmodule -c -emit-dependencies -module-name Test -o %t/test.o -cas-path %t/cas \
4+
// RUN: -allow-unstable-cache-key-for-testing > %t/cmd.resp
5+
6+
// RUN: %swift-scan-test -action compute_cache_key -cas-path %t/cas -input %s -- %swift_frontend_plain @%t/cmd.resp > %t/key.casid
7+
8+
// RUN: %swift-scan-test -action compute_cache_key_from_index -cas-path %t/cas -input 0 -- %swift_frontend_plain @%t/cmd.resp > %t/key1.casid
9+
10+
// RUN: diff %t/key.casid %t/key1.casid
11+
12+
// RUN: not %swift-scan-test -action cache_query -id @%t/key.casid -cas-path %t/cas 2>&1 | %FileCheck %s --check-prefix=CHECK-QUERY-NOT-FOUND
13+
14+
// RUN: %swift_frontend_plain @%t/cmd.resp
15+
16+
// RUN: %swift-scan-test -action cache_query -id @%t/key.casid -cas-path %t/cas | %FileCheck %s --check-prefix=CHECK-QUERY
17+
18+
// RUN: %{python} %S/Inputs/PrintResponseFile.py %target-swift-frontend -cache-compile-job -Rcache-compile-job %s \
19+
// RUN: -emit-module -emit-module-path %t/Test2.swiftmodule -c -emit-dependencies -module-name Test -o %t/test.o -cas-path %t/cas \
20+
// RUN: -allow-unstable-cache-key-for-testing > %t/cmd2.resp
21+
// RUN: %swift-scan-test -action replay_result -cas-path %t/cas -id @%t/key.casid -- %swift_frontend_plain @%t/cmd2.resp
22+
23+
// RUN: diff %t/Test.swiftmodule %t/Test2.swiftmodule
24+
// RUN: diff %t/test.o %t/test.o
25+
26+
// CHECK-QUERY-NOT-FOUND: cached output not found
27+
// CHECK-QUERY: Cached Compilation for key "llvmcas://{{.*}}" has 4 outputs:
28+
// CHECK-QUERY-NEXT: object: llvmcas://
29+
// CHECK-QUERY-NEXT: dependencies: llvmcas://
30+
// CHECK-QUERY-NEXT: swiftmodule: llvmcas://
31+
// CHECK-QUERY-NEXT: cached-diagnostics: llvmcas://
32+
33+
func testFunc() {}
34+

tools/libSwiftScan/SwiftCaching.cpp

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "swift/Basic/SourceManager.h"
2222
#include "swift/DependencyScan/DependencyScanImpl.h"
2323
#include "swift/DependencyScan/StringUtils.h"
24+
#include "swift/Driver/FrontendUtil.h"
2425
#include "swift/Frontend/CachedDiagnostics.h"
2526
#include "swift/Frontend/CachingUtils.h"
2627
#include "swift/Frontend/CompileJobCacheKey.h"
@@ -108,7 +109,7 @@ struct SwiftCachedOutputHandle {
108109
struct SwiftScanReplayInstance {
109110
swift::CompilerInvocation Invocation;
110111
llvm::BumpPtrAllocator StringAlloc;
111-
std::vector<const char *> Args;
112+
llvm::SmallVector<const char *> Args;
112113
};
113114

114115
struct SwiftCachedReplayResult {
@@ -196,6 +197,25 @@ swiftscan_string_ref_t swiftscan_cas_store(swiftscan_cas_t cas, uint8_t *data,
196197
CAS.getID(*Result).toString().c_str());
197198
}
198199

200+
/// Expand the invocation if there is repsonseFile into Args that are passed in
201+
/// the parameter. Return swift-frontend arguments in an ArrayRef, which has the
202+
/// first "-frontend" option dropped if needed.
203+
static llvm::ArrayRef<const char *>
204+
expandSwiftInvocation(int argc, const char **argv, llvm::StringSaver &Saver,
205+
llvm::SmallVectorImpl<const char *> &ArgsStorage) {
206+
ArgsStorage.reserve(argc);
207+
for (int i = 0; i < argc; ++i)
208+
ArgsStorage.push_back(argv[i]);
209+
swift::driver::ExpandResponseFilesWithRetry(Saver, ArgsStorage);
210+
211+
// Drop the `-frontend` option if it is passed.
212+
llvm::ArrayRef<const char*> FrontendArgs(ArgsStorage);
213+
if (!FrontendArgs.empty() &&
214+
llvm::StringRef(FrontendArgs.front()) == "-frontend")
215+
FrontendArgs = FrontendArgs.drop_front();
216+
return FrontendArgs;
217+
}
218+
199219
static llvm::Expected<std::string>
200220
computeCacheKey(llvm::cas::ObjectStore &CAS, llvm::ArrayRef<const char *> Args,
201221
llvm::StringRef InputPath) {
@@ -215,10 +235,6 @@ computeCacheKey(llvm::cas::ObjectStore &CAS, llvm::ArrayRef<const char *> Args,
215235
std::string MainExecutablePath = llvm::sys::fs::getMainExecutable(
216236
"swift-frontend", (void *)swiftscan_cache_replay_compilation);
217237

218-
// Drop the `-frontend` option if it is passed.
219-
if (llvm::StringRef(Args.front()) == "-frontend")
220-
Args = Args.drop_front();
221-
222238
if (Invocation.parseArgs(Args, Diags, &configurationFileBuffers,
223239
workingDirectory, MainExecutablePath))
224240
return llvm::createStringError(llvm::inconvertibleErrorCode(),
@@ -270,11 +286,12 @@ computeCacheKeyFromIndex(llvm::cas::ObjectStore &CAS,
270286
swiftscan_string_ref_t
271287
swiftscan_cache_compute_key(swiftscan_cas_t cas, int argc, const char **argv,
272288
const char *input, swiftscan_string_ref_t *error) {
273-
std::vector<const char *> Compilation;
274-
for (int i = 0; i < argc; ++i)
275-
Compilation.push_back(argv[i]);
289+
llvm::SmallVector<const char *> ArgsStorage;
290+
llvm::BumpPtrAllocator Alloc;
291+
llvm::StringSaver Saver(Alloc);
292+
auto Args = expandSwiftInvocation(argc, argv, Saver, ArgsStorage);
276293

277-
auto ID = computeCacheKey(unwrap(cas)->getCAS(), Compilation, input);
294+
auto ID = computeCacheKey(unwrap(cas)->getCAS(), Args, input);
278295
if (!ID) {
279296
*error =
280297
swift::c_string_utils::create_clone(toString(ID.takeError()).c_str());
@@ -289,12 +306,13 @@ swiftscan_cache_compute_key_from_input_index(swiftscan_cas_t cas, int argc,
289306
const char **argv,
290307
unsigned input_index,
291308
swiftscan_string_ref_t *error) {
292-
std::vector<const char *> Compilation;
293-
for (int i = 0; i < argc; ++i)
294-
Compilation.push_back(argv[i]);
309+
llvm::SmallVector<const char *> ArgsStorage;
310+
llvm::BumpPtrAllocator Alloc;
311+
llvm::StringSaver Saver(Alloc);
312+
auto Args = expandSwiftInvocation(argc, argv, Saver, ArgsStorage);
295313

296314
auto ID =
297-
computeCacheKeyFromIndex(unwrap(cas)->getCAS(), Compilation, input_index);
315+
computeCacheKeyFromIndex(unwrap(cas)->getCAS(), Args, input_index);
298316
if (!ID) {
299317
*error =
300318
swift::c_string_utils::create_clone(toString(ID.takeError()).c_str());
@@ -747,11 +765,9 @@ swiftscan_cache_replay_instance_t
747765
swiftscan_cache_replay_instance_create(int argc, const char **argv,
748766
swiftscan_string_ref_t *error) {
749767
auto *Instance = new SwiftScanReplayInstance();
768+
llvm::SmallVector<const char *> Compilation;
750769
llvm::StringSaver Saver(Instance->StringAlloc);
751-
for (int i = 0; i < argc; ++i) {
752-
auto Str = Saver.save(argv[i]);
753-
Instance->Args.push_back(Str.data());
754-
}
770+
auto Args = expandSwiftInvocation(argc, argv, Saver, Instance->Args);
755771

756772
// Capture the diagnostics when creating invocation.
757773
std::string err_msg;
@@ -764,7 +780,7 @@ swiftscan_cache_replay_instance_create(int argc, const char **argv,
764780
std::string MainExecutablePath = llvm::sys::fs::getMainExecutable(
765781
"swift-frontend", (void *)swiftscan_cache_replay_compilation);
766782

767-
if (Instance->Invocation.parseArgs(Instance->Args, DE, nullptr, {},
783+
if (Instance->Invocation.parseArgs(Args, DE, nullptr, {},
768784
MainExecutablePath)) {
769785
delete Instance;
770786
*error = swift::c_string_utils::create_clone(err_msg.c_str());

tools/swift-scan-test/swift-scan-test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ static int action_cache_query(swiftscan_cas_t cas, const char *key) {
133133
return printError(err_msg);
134134

135135
if (!comp) {
136-
llvm::errs() << "cached output not found for \"" << key << "\n";
136+
llvm::errs() << "cached output not found for \"" << key << "\"\n";
137137
return EXIT_FAILURE;
138138
}
139139

0 commit comments

Comments
 (0)