Skip to content

[Caching] Adopt new APIs from libSwiftScan for key computation #1503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Sources/CSwiftScan/include/swiftscan_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ typedef struct {
swiftscan_string_ref_t (*swiftscan_cache_compute_key)(
swiftscan_cas_t cas, int argc, const char **argv, const char *input,
swiftscan_string_ref_t *error);
swiftscan_string_ref_t (*swiftscan_cache_compute_key_from_input_index)(
swiftscan_cas_t cas, int argc, const char **argv, unsigned input_index,
swiftscan_string_ref_t *error);

//=== Scanner Caching Query/Replay Operations -----------------------------===//
swiftscan_cached_compilation_t (*swiftscan_cache_query)(
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftDriver/Driver/Driver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public struct Driver {

/// Is swift caching enabled.
lazy var isCachingEnabled: Bool = {
return enableCaching && isFeatureSupported(.cache_compile_job)
return enableCaching && isFeatureSupported(.compilation_caching)
}()

/// Scanner prefix mapping.
Expand Down Expand Up @@ -427,7 +427,7 @@ public struct Driver {
@_spi(Testing)
public enum KnownCompilerFeature: String {
case emit_abi_descriptor = "emit-abi-descriptor"
case cache_compile_job = "cache-compile-job"
case compilation_caching = "compilation-caching"
}

lazy var sdkPath: VirtualPath? = {
Expand Down
6 changes: 5 additions & 1 deletion Sources/SwiftDriver/Jobs/CompileJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,11 @@ extension Driver {
displayInputs = primaryInputs
}
// Only swift input files are contributing to the cache keys.
let cacheContributingInputs = displayInputs.filter() { $0.type == .swift }
let cacheContributingInputs = inputs.enumerated().reduce(into: [(TypedVirtualPath, Int)]()) { result, input in
if input.element.type == .swift, displayInputs.contains(input.element) {
result.append((input.element, input.offset))
}
}
let cacheKeys = try computeOutputCacheKeyForJob(commandLine: commandLine, inputs: cacheContributingInputs)

return Job(
Expand Down
10 changes: 4 additions & 6 deletions Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ extension Driver {

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

return try inputs.reduce(into: [:]) { keys, input in
let remappedPath = remapPath(input.file)
keys[input] = try cas.computeCacheKey(commandLine: arguments, input: remappedPath.name)
keys[input.0] = try cas.computeCacheKey(commandLine: arguments, index: input.1)
}
}

public mutating func computeOutputCacheKey(commandLine: [Job.ArgTemplate],
input: TypedVirtualPath) throws -> String? {
index: Int) throws -> String? {
// No caching setup, return empty dictionary.
guard let cas = self.cas else {
return nil
}
// Resolve command-line first.
let resolver = try ArgsResolver(fileSystem: fileSystem)
let arguments: [String] = try resolver.resolveArgumentList(for: commandLine)
let remappedPath = remapPath(input.file)
return try cas.computeCacheKey(commandLine: arguments, input: remappedPath.name)
return try cas.computeCacheKey(commandLine: arguments, index: index)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftDriver/Jobs/GeneratePCHJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ extension Driver {
inputs.append(input)
try addPathArgument(input.file, to: &commandLine)

let cacheKeys = try computeOutputCacheKeyForJob(commandLine: commandLine, inputs: [input])
let cacheKeys = try computeOutputCacheKeyForJob(commandLine: commandLine, inputs: [(input, 0)])

return Job(
moduleName: moduleOutputInfo.name,
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftDriver/Jobs/GeneratePCMJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ extension Driver {
commandLine: &commandLine, inputs: &inputs, kind: .generatePCM, bridgingHeaderHandling: .ignored)

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

return Job(
moduleName: moduleOutputInfo.name,
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftDriver/Jobs/VerifyModuleInterfaceJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension Driver {

// Assume swiftinterface file is always the supplementary output for first input file.
let key = try computeOutputCacheKey(commandLine: emitModuleJob.commandLine,
input: emitModuleJob.inputs[0])
index: 0)
return key
}

Expand Down Expand Up @@ -61,7 +61,7 @@ extension Driver {
commandLine.appendFlag(.downgradeTypecheckInterfaceError)
}

let cacheKeys = try computeOutputCacheKeyForJob(commandLine: commandLine, inputs: [interfaceInput])
let cacheKeys = try computeOutputCacheKeyForJob(commandLine: commandLine, inputs: [(interfaceInput, 0)])
return Job(
moduleName: moduleOutputInfo.name,
kind: .verifyModuleInterface,
Expand Down
2 changes: 2 additions & 0 deletions Sources/SwiftDriver/SwiftScan/SwiftScan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ internal extension swiftscan_diagnostic_severity_t {
api.swiftscan_cas_create_from_options != nil &&
api.swiftscan_cas_dispose != nil &&
api.swiftscan_cache_compute_key != nil &&
api.swiftscan_cache_compute_key_from_input_index != nil &&
api.swiftscan_cas_store != nil &&
api.swiftscan_swift_textual_detail_get_module_cache_key != nil &&
api.swiftscan_swift_binary_detail_get_module_cache_key != nil &&
Expand Down Expand Up @@ -507,6 +508,7 @@ private extension swiftscan_functions_t {
self.swiftscan_cas_create_from_options = try loadOptional("swiftscan_cas_create_from_options")
self.swiftscan_cas_dispose = try loadOptional("swiftscan_cas_dispose")
self.swiftscan_cache_compute_key = try loadOptional("swiftscan_cache_compute_key")
self.swiftscan_cache_compute_key_from_input_index = try loadOptional("swiftscan_cache_compute_key_from_input_index")
self.swiftscan_cas_store = try loadOptional("swiftscan_cas_store")

self.swiftscan_cache_query = try loadOptional("swiftscan_cache_query")
Expand Down
14 changes: 14 additions & 0 deletions Sources/SwiftDriver/SwiftScan/SwiftScanCAS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ public final class SwiftScanCAS {
return try scanner.toSwiftString(casid)
}

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

public func computeCacheKey(commandLine: [String], index: Int) throws -> String {
let casid = try scanner.handleCASError { err_msg in
withArrayOfCStrings(commandLine) { commandArray in
scanner.api.swiftscan_cache_compute_key_from_input_index(cas,
Int32(commandLine.count),
commandArray,
UInt32(index),
&err_msg)
}
}
return try scanner.toSwiftString(casid)
}

public func createReplayInstance(commandLine: [String]) throws -> CacheReplayInstance {
let instance = try scanner.handleCASError { err_msg in
withArrayOfCStrings(commandLine) { commandArray in
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftDriverTests/CachingBuildTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ final class CachingBuildTests: XCTestCase {
#if os(Windows)
throw XCTSkip("caching not supported on windows")
#else
guard driver.isFeatureSupported(.cache_compile_job) else {
guard driver.isFeatureSupported(.compilation_caching) else {
throw XCTSkip("caching not supported")
}
#endif
Expand Down