Skip to content

Commit 2df495a

Browse files
Merge pull request #1503 from cachemeifyoucan/eng/PR-119517627
[Caching] Adopt new APIs from libSwiftScan for key computation
2 parents ff67178 + a4a1b96 commit 2df495a

File tree

10 files changed

+35
-14
lines changed

10 files changed

+35
-14
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
@@ -853,7 +853,7 @@ extension Driver {
853853

854854
extension Driver {
855855
public mutating func computeOutputCacheKeyForJob(commandLine: [Job.ArgTemplate],
856-
inputs: [TypedVirtualPath]) throws -> [TypedVirtualPath: String] {
856+
inputs: [(TypedVirtualPath, Int)]) throws -> [TypedVirtualPath: String] {
857857
// No caching setup, return empty dictionary.
858858
guard let cas = self.cas else {
859859
return [:]
@@ -863,22 +863,20 @@ extension Driver {
863863
let arguments: [String] = try resolver.resolveArgumentList(for: commandLine)
864864

865865
return try inputs.reduce(into: [:]) { keys, input in
866-
let remappedPath = remapPath(input.file)
867-
keys[input] = try cas.computeCacheKey(commandLine: arguments, input: remappedPath.name)
866+
keys[input.0] = try cas.computeCacheKey(commandLine: arguments, index: input.1)
868867
}
869868
}
870869

871870
public mutating func computeOutputCacheKey(commandLine: [Job.ArgTemplate],
872-
input: TypedVirtualPath) throws -> String? {
871+
index: Int) throws -> String? {
873872
// No caching setup, return empty dictionary.
874873
guard let cas = self.cas else {
875874
return nil
876875
}
877876
// Resolve command-line first.
878877
let resolver = try ArgsResolver(fileSystem: fileSystem)
879878
let arguments: [String] = try resolver.resolveArgumentList(for: commandLine)
880-
let remappedPath = remapPath(input.file)
881-
return try cas.computeCacheKey(commandLine: arguments, input: remappedPath.name)
879+
return try cas.computeCacheKey(commandLine: arguments, index: index)
882880
}
883881
}
884882

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ final class CachingBuildTests: XCTestCase {
207207
#if os(Windows)
208208
throw XCTSkip("caching not supported on windows")
209209
#else
210-
guard driver.isFeatureSupported(.cache_compile_job) else {
210+
guard driver.isFeatureSupported(.compilation_caching) else {
211211
throw XCTSkip("caching not supported")
212212
}
213213
#endif

0 commit comments

Comments
 (0)