Skip to content

Commit a584361

Browse files
[Caching] Adopt new APIs from libSwiftScan for key computation
Using the new APIs from libSwiftScan for cache key computation. The new API doesn't require swift-driver and scanner to have the same file system view for the path of the input files, which eliminates one failure point. rdar://119517627
1 parent 0c846ef commit a584361

File tree

10 files changed

+40
-19
lines changed

10 files changed

+40
-19
lines changed

Sources/CSwiftScan/include/swiftscan_header.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ typedef struct {
291291
swiftscan_string_ref_t (*swiftscan_cache_compute_key)(
292292
swiftscan_cas_t cas, int argc, const char **argv, const char *input,
293293
swiftscan_string_ref_t *error);
294+
swiftscan_string_ref_t (*swiftscan_cache_compute_key_from_input_index)(
295+
swiftscan_cas_t cas, int argc, const char **argv, unsigned input_index,
296+
swiftscan_string_ref_t *error);
294297

295298
//=== Scanner Caching Query/Replay Operations -----------------------------===//
296299
swiftscan_cached_compilation_t (*swiftscan_cache_query)(

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ public struct Driver {
279279

280280
/// Is swift caching enabled.
281281
lazy var isCachingEnabled: Bool = {
282-
return enableCaching && isFeatureSupported(.cache_compile_job)
282+
return enableCaching && isFeatureSupported(.compilation_caching)
283283
}()
284284

285285
/// Scanner prefix mapping.
@@ -427,7 +427,7 @@ public struct Driver {
427427
@_spi(Testing)
428428
public enum KnownCompilerFeature: String {
429429
case emit_abi_descriptor = "emit-abi-descriptor"
430-
case cache_compile_job = "cache-compile-job"
430+
case compilation_caching = "compilation-caching"
431431
}
432432

433433
lazy var sdkPath: VirtualPath? = {

Sources/SwiftDriver/Jobs/CompileJob.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,11 @@ extension Driver {
398398
displayInputs = primaryInputs
399399
}
400400
// Only swift input files are contributing to the cache keys.
401-
let cacheContributingInputs = displayInputs.filter() { $0.type == .swift }
401+
let cacheContributingInputs = inputs.enumerated().reduce(into: [(TypedVirtualPath, Int)]()) { result, input in
402+
if input.element.type == .swift, displayInputs.contains(input.element) {
403+
result.append((input.element, input.offset))
404+
}
405+
}
402406
let cacheKeys = try computeOutputCacheKeyForJob(commandLine: commandLine, inputs: cacheContributingInputs)
403407

404408
return Job(

Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ extension Driver {
840840

841841
extension Driver {
842842
public mutating func computeOutputCacheKeyForJob(commandLine: [Job.ArgTemplate],
843-
inputs: [TypedVirtualPath]) throws -> [TypedVirtualPath: String] {
843+
inputs: [(TypedVirtualPath, Int)]) throws -> [TypedVirtualPath: String] {
844844
// No caching setup, return empty dictionary.
845845
guard let cas = self.cas else {
846846
return [:]
@@ -850,21 +850,19 @@ extension Driver {
850850
let arguments: [String] = try resolver.resolveArgumentList(for: commandLine)
851851

852852
return try inputs.reduce(into: [:]) { keys, input in
853-
let remappedPath = remapPath(input.file)
854-
keys[input] = try cas.computeCacheKey(commandLine: arguments, input: remappedPath.name)
853+
keys[input.0] = try cas.computeCacheKey(commandLine: arguments, index: input.1)
855854
}
856855
}
857856

858857
public mutating func computeOutputCacheKey(commandLine: [Job.ArgTemplate],
859-
input: TypedVirtualPath) throws -> String? {
858+
index: Int) throws -> String? {
860859
// No caching setup, return empty dictionary.
861860
guard let cas = self.cas else {
862861
return nil
863862
}
864863
// Resolve command-line first.
865864
let resolver = try ArgsResolver(fileSystem: fileSystem)
866865
let arguments: [String] = try resolver.resolveArgumentList(for: commandLine)
867-
let remappedPath = remapPath(input.file)
868-
return try cas.computeCacheKey(commandLine: arguments, input: remappedPath.name)
866+
return try cas.computeCacheKey(commandLine: arguments, index: index)
869867
}
870868
}

Sources/SwiftDriver/Jobs/GeneratePCHJob.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ extension Driver {
7070
inputs.append(input)
7171
try addPathArgument(input.file, to: &commandLine)
7272

73-
let cacheKeys = try computeOutputCacheKeyForJob(commandLine: commandLine, inputs: [input])
73+
let cacheKeys = try computeOutputCacheKeyForJob(commandLine: commandLine, inputs: [(input, 0)])
7474

7575
return Job(
7676
moduleName: moduleOutputInfo.name,

Sources/SwiftDriver/Jobs/GeneratePCMJob.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ extension Driver {
5151
commandLine: &commandLine, inputs: &inputs, kind: .generatePCM, bridgingHeaderHandling: .ignored)
5252

5353
try commandLine.appendLast(.indexStorePath, from: &parsedOptions)
54-
let cacheKeys = try computeOutputCacheKeyForJob(commandLine: commandLine, inputs: [input])
54+
let cacheKeys = try computeOutputCacheKeyForJob(commandLine: commandLine, inputs: [(input, 0)])
5555

5656
return Job(
5757
moduleName: moduleOutputInfo.name,

Sources/SwiftDriver/Jobs/VerifyModuleInterfaceJob.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ extension Driver {
2020

2121
// Assume swiftinterface file is always the supplementary output for first input file.
2222
let key = try computeOutputCacheKey(commandLine: emitModuleJob.commandLine,
23-
input: emitModuleJob.inputs[0])
23+
index: 0)
2424
return key
2525
}
2626

@@ -61,7 +61,7 @@ extension Driver {
6161
commandLine.appendFlag(.downgradeTypecheckInterfaceError)
6262
}
6363

64-
let cacheKeys = try computeOutputCacheKeyForJob(commandLine: commandLine, inputs: [interfaceInput])
64+
let cacheKeys = try computeOutputCacheKeyForJob(commandLine: commandLine, inputs: [(interfaceInput, 0)])
6565
return Job(
6666
moduleName: moduleOutputInfo.name,
6767
kind: .verifyModuleInterface,

Sources/SwiftDriver/SwiftScan/SwiftScan.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ internal extension swiftscan_diagnostic_severity_t {
287287
api.swiftscan_cas_create_from_options != nil &&
288288
api.swiftscan_cas_dispose != nil &&
289289
api.swiftscan_cache_compute_key != nil &&
290+
api.swiftscan_cache_compute_key_from_input_index != nil &&
290291
api.swiftscan_cas_store != nil &&
291292
api.swiftscan_swift_textual_detail_get_module_cache_key != nil &&
292293
api.swiftscan_swift_binary_detail_get_module_cache_key != nil &&
@@ -507,6 +508,7 @@ private extension swiftscan_functions_t {
507508
self.swiftscan_cas_create_from_options = try loadOptional("swiftscan_cas_create_from_options")
508509
self.swiftscan_cas_dispose = try loadOptional("swiftscan_cas_dispose")
509510
self.swiftscan_cache_compute_key = try loadOptional("swiftscan_cache_compute_key")
511+
self.swiftscan_cache_compute_key_from_input_index = try loadOptional("swiftscan_cache_compute_key_from_input_index")
510512
self.swiftscan_cas_store = try loadOptional("swiftscan_cas_store")
511513

512514
self.swiftscan_cache_query = try loadOptional("swiftscan_cache_query")

Sources/SwiftDriver/SwiftScan/SwiftScanCAS.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ public final class SwiftScanCAS {
198198
return try scanner.toSwiftString(casid)
199199
}
200200

201+
@available(*, deprecated)
201202
public func computeCacheKey(commandLine: [String], input: String) throws -> String {
202203
let casid = try scanner.handleCASError { err_msg in
203204
withArrayOfCStrings(commandLine) { commandArray in
@@ -211,6 +212,19 @@ public final class SwiftScanCAS {
211212
return try scanner.toSwiftString(casid)
212213
}
213214

215+
public func computeCacheKey(commandLine: [String], index: Int) throws -> String {
216+
let casid = try scanner.handleCASError { err_msg in
217+
withArrayOfCStrings(commandLine) { commandArray in
218+
scanner.api.swiftscan_cache_compute_key_from_input_index(cas,
219+
Int32(commandLine.count),
220+
commandArray,
221+
UInt32(index),
222+
&err_msg)
223+
}
224+
}
225+
return try scanner.toSwiftString(casid)
226+
}
227+
214228
public func createReplayInstance(commandLine: [String]) throws -> CacheReplayInstance {
215229
let instance = try scanner.handleCASError { err_msg in
216230
withArrayOfCStrings(commandLine) { commandArray in

Tests/SwiftDriverTests/CachingBuildTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ final class CachingBuildTests: XCTestCase {
234234
"-cache-compile-job", "-cas-path", casPath.nativePathString(escaped: true),
235235
"-import-objc-header", bridgingHeaderpath.nativePathString(escaped: true),
236236
main.nativePathString(escaped: true)] + sdkArgumentsForTesting)
237-
guard driver.isFeatureSupported(.cache_compile_job) else {
237+
guard driver.isFeatureSupported(.compilation_caching) else {
238238
throw XCTSkip("toolchain does not support caching.")
239239
}
240240

@@ -361,7 +361,7 @@ final class CachingBuildTests: XCTestCase {
361361
guard driver.supportExplicitModuleVerifyInterface() else {
362362
throw XCTSkip("-typecheck-module-from-interface doesn't support explicit build.")
363363
}
364-
guard driver.isFeatureSupported(.cache_compile_job) else {
364+
guard driver.isFeatureSupported(.compilation_caching) else {
365365
throw XCTSkip("toolchain does not support caching.")
366366
}
367367

@@ -492,7 +492,7 @@ final class CachingBuildTests: XCTestCase {
492492
"-working-directory", path.nativePathString(escaped: true),
493493
main.nativePathString(escaped: true)] + sdkArgumentsForTesting,
494494
env: ProcessEnv.vars)
495-
guard driver.isFeatureSupported(.cache_compile_job) else {
495+
guard driver.isFeatureSupported(.compilation_caching) else {
496496
throw XCTSkip("toolchain does not support caching.")
497497
}
498498
let jobs = try driver.planBuild()
@@ -554,7 +554,7 @@ final class CachingBuildTests: XCTestCase {
554554
env: ProcessEnv.vars)
555555

556556
// Ensure this tooling supports this functionality
557-
guard fooBuildDriver.isFeatureSupported(.cache_compile_job) else {
557+
guard fooBuildDriver.isFeatureSupported(.compilation_caching) else {
558558
throw XCTSkip("toolchain does not support caching.")
559559
}
560560
let dependencyOracle = InterModuleDependencyOracle()
@@ -623,7 +623,7 @@ final class CachingBuildTests: XCTestCase {
623623
"-disable-clang-target",
624624
main.nativePathString(escaped: true)] + sdkArgumentsForTesting,
625625
env: ProcessEnv.vars)
626-
guard driver.isFeatureSupported(.cache_compile_job) else {
626+
guard driver.isFeatureSupported(.compilation_caching) else {
627627
throw XCTSkip("toolchain does not support caching.")
628628
}
629629
let dependencyOracle = InterModuleDependencyOracle()
@@ -829,7 +829,7 @@ final class CachingBuildTests: XCTestCase {
829829
"-working-directory", path.nativePathString(escaped: true),
830830
main.nativePathString(escaped: true)] + sdkArgumentsForTesting,
831831
env: ProcessEnv.vars)
832-
guard driver.isFeatureSupported(.cache_compile_job) else {
832+
guard driver.isFeatureSupported(.compilation_caching) else {
833833
throw XCTSkip("toolchain does not support caching.")
834834
}
835835
let jobs = try driver.planBuild()

0 commit comments

Comments
 (0)