Skip to content

Commit 6a58253

Browse files
Compute bridging header pch cache key on demand
Reduce mutable state in Driver
1 parent 321373c commit 6a58253

File tree

6 files changed

+43
-18
lines changed

6 files changed

+43
-18
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,6 @@ public struct Driver {
204204
/// The working directory for the driver, if there is one.
205205
let workingDirectory: AbsolutePath?
206206

207-
/// CacheKey for bridging header
208-
var bridgingHeaderCacheKey: String? = nil
209-
210207
/// The set of input files
211208
@_spi(Testing) public let inputFiles: [TypedVirtualPath]
212209

Sources/SwiftDriver/Jobs/CompileJob.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ extension Driver {
225225
mutating func compileJob(primaryInputs: [TypedVirtualPath],
226226
outputType: FileType?,
227227
addJobOutputs: ([TypedVirtualPath]) -> Void,
228+
pchCompileJob: Job?,
228229
emitModuleTrace: Bool)
229230
throws -> Job {
230231
var commandLine: [Job.ArgTemplate] = swiftCompilerPrefixArgs.map { Job.ArgTemplate.flag($0) }
@@ -386,6 +387,7 @@ extension Driver {
386387
let pchInput = TypedVirtualPath(file: pchPath, type: .pch)
387388
inputs.append(pchInput)
388389
}
390+
try addBridgingHeaderPCHCacheKeyArguments(commandLine: &commandLine, pchCompileJob: pchCompileJob)
389391

390392
let displayInputs : [TypedVirtualPath]
391393
if case .singleCompile = compilerMode {

Sources/SwiftDriver/Jobs/EmitModuleJob.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ extension Driver {
7474
}
7575

7676
/// Form a job that emits a single module
77-
@_spi(Testing) public mutating func emitModuleJob() throws -> Job {
77+
@_spi(Testing) public mutating func emitModuleJob(pchCompileJob: Job?) throws -> Job {
7878
let moduleOutputPath = moduleOutputInfo.output!.outputPath
7979
var commandLine: [Job.ArgTemplate] = swiftCompilerPrefixArgs.map { Job.ArgTemplate.flag($0) }
8080
var inputs: [TypedVirtualPath] = []
@@ -93,6 +93,7 @@ extension Driver {
9393
if let pchPath = bridgingPrecompiledHeader {
9494
inputs.append(TypedVirtualPath(file: pchPath, type: .pch))
9595
}
96+
try addBridgingHeaderPCHCacheKeyArguments(commandLine: &commandLine, pchCompileJob: pchCompileJob)
9697

9798
try addCommonFrontendOptions(commandLine: &commandLine, inputs: &inputs, kind: .emitModule)
9899
// FIXME: Add MSVC runtime library flags

Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,6 @@ extension Driver {
380380
} else {
381381
commandLine.appendPath(VirtualPath.lookup(importedObjCHeader))
382382
}
383-
if kind == .compile || kind == .emitModule, let bridgingHeaderKey = bridgingHeaderCacheKey {
384-
commandLine.appendFlag("-bridging-header-pch-key")
385-
commandLine.appendFlag(bridgingHeaderKey)
386-
}
387383
}
388384

389385
// Repl Jobs shouldn't include -module-name.
@@ -422,6 +418,20 @@ extension Driver {
422418
}
423419
}
424420

421+
func addBridgingHeaderPCHCacheKeyArguments(commandLine: inout [Job.ArgTemplate],
422+
pchCompileJob: Job?) throws {
423+
guard let pchJob = pchCompileJob, enableCaching else { return }
424+
425+
// The pch input file (the bridging header) is added as last inputs to the job.
426+
guard let inputFile = pchJob.inputs.last else { assertionFailure("no input files from pch job"); return }
427+
assert(inputFile.type == .objcHeader, "Expect objc header input type")
428+
let bridgingHeaderCacheKey = try interModuleDependencyOracle.computeCacheKeyForOutput(kind: .pch,
429+
commandLine: pchJob.commandLine,
430+
input: inputFile.fileHandle)
431+
commandLine.appendFlag("-bridging-header-pch-key")
432+
commandLine.appendFlag(bridgingHeaderCacheKey)
433+
}
434+
425435
mutating func addFrontendSupplementaryOutputArguments(commandLine: inout [Job.ArgTemplate],
426436
primaryInputs: [TypedVirtualPath],
427437
inputsGeneratingCodeCount: Int,

Sources/SwiftDriver/Jobs/GeneratePCHJob.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,6 @@ extension Driver {
7070
inputs.append(input)
7171
commandLine.appendPath(input.file)
7272

73-
// Compute the cache key after we have the full command-line
74-
if enableCaching {
75-
bridgingHeaderCacheKey = try interModuleDependencyOracle.computeCacheKeyForOutput(kind: .pch, commandLine: commandLine, input: input.fileHandle)
76-
}
77-
7873
return Job(
7974
moduleName: moduleOutputInfo.name,
8075
kind: .generatePCH,

Sources/SwiftDriver/Jobs/Planning.swift

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ extension Driver {
180180
try addPrecompileBridgingHeaderJob(addJob: addJobBeforeCompiles)
181181
let linkerInputs = try addJobsFeedingLinker(
182182
addJobBeforeCompiles: addJobBeforeCompiles,
183+
jobsBeforeCompiles: jobsBeforeCompiles,
183184
addCompileJobGroup: addCompileJobGroup,
184185
addJobAfterCompiles: addJobAfterCompiles)
185186
try addAPIDigesterJobs(addJob: addJobAfterCompiles)
@@ -223,9 +224,9 @@ extension Driver {
223224
)
224225
}
225226

226-
private mutating func addEmitModuleJob(addJobBeforeCompiles: (Job) -> Void) throws -> Job? {
227+
private mutating func addEmitModuleJob(addJobBeforeCompiles: (Job) -> Void, pchCompileJob: Job?) throws -> Job? {
227228
if emitModuleSeparately {
228-
let emitJob = try emitModuleJob()
229+
let emitJob = try emitModuleJob(pchCompileJob: pchCompileJob)
229230
addJobBeforeCompiles(emitJob)
230231
return emitJob
231232
}
@@ -234,6 +235,7 @@ extension Driver {
234235

235236
private mutating func addJobsFeedingLinker(
236237
addJobBeforeCompiles: (Job) -> Void,
238+
jobsBeforeCompiles: [Job],
237239
addCompileJobGroup: (CompileJobGroup) -> Void,
238240
addJobAfterCompiles: (Job) -> Void
239241
) throws -> [TypedVirtualPath] {
@@ -280,15 +282,20 @@ extension Driver {
280282
try addVerifyJobs(emitModuleJob: emitModuleJob, addJob: addJobAfterCompiles)
281283
}
282284

285+
// Try to see if we scheduled a pch compile job. If so, pass it to the comile jobs.
286+
let jobCreatingPch: Job? = jobsBeforeCompiles.first(where: {$0.kind == .generatePCH})
287+
283288
// Whole-module
284289
if let compileJob = try addSingleCompileJobs(addJob: addJobBeforeCompiles,
285290
addJobOutputs: addJobOutputs,
291+
pchCompileJob: jobCreatingPch,
286292
emitModuleTrace: loadedModuleTracePath != nil) {
287293
try addPostModuleFilesJobs(compileJob)
288294
}
289295

290296
// Emit-module-separately
291-
if let emitModuleJob = try addEmitModuleJob(addJobBeforeCompiles: addJobBeforeCompiles) {
297+
if let emitModuleJob = try addEmitModuleJob(addJobBeforeCompiles: addJobBeforeCompiles,
298+
pchCompileJob: jobCreatingPch) {
292299
try addPostModuleFilesJobs(emitModuleJob)
293300

294301
try addWrapJobOrMergeOutputs(
@@ -302,7 +309,8 @@ extension Driver {
302309
addCompileJobGroup: addCompileJobGroup,
303310
addModuleInput: addModuleInput,
304311
addLinkerInput: addLinkerInput,
305-
addJobOutputs: addJobOutputs)
312+
addJobOutputs: addJobOutputs,
313+
pchCompileJob: jobCreatingPch)
306314

307315
try addAutolinkExtractJob(linkerInputs: linkerInputs,
308316
addLinkerInput: addLinkerInput,
@@ -329,6 +337,7 @@ extension Driver {
329337
private mutating func addSingleCompileJobs(
330338
addJob: (Job) -> Void,
331339
addJobOutputs: ([TypedVirtualPath]) -> Void,
340+
pchCompileJob: Job?,
332341
emitModuleTrace: Bool
333342
) throws -> Job? {
334343
guard case .singleCompile = compilerMode,
@@ -340,6 +349,7 @@ extension Driver {
340349
let compile = try compileJob(primaryInputs: [],
341350
outputType: .llvmBitcode,
342351
addJobOutputs: addJobOutputs,
352+
pchCompileJob: pchCompileJob,
343353
emitModuleTrace: emitModuleTrace)
344354
addJob(compile)
345355
let backendJobs = try compile.outputs.compactMap { output in
@@ -355,6 +365,7 @@ extension Driver {
355365
let compile = try compileJob(primaryInputs: [],
356366
outputType: compilerOutputType,
357367
addJobOutputs: addJobOutputs,
368+
pchCompileJob: pchCompileJob,
358369
emitModuleTrace: emitModuleTrace)
359370
addJob(compile)
360371
return compile
@@ -365,7 +376,8 @@ extension Driver {
365376
addCompileJobGroup: (CompileJobGroup) -> Void,
366377
addModuleInput: (TypedVirtualPath) -> Void,
367378
addLinkerInput: (TypedVirtualPath) -> Void,
368-
addJobOutputs: ([TypedVirtualPath]) -> Void)
379+
addJobOutputs: ([TypedVirtualPath]) -> Void,
380+
pchCompileJob: Job?)
369381
throws {
370382
let loadedModuleTraceInputIndex = inputFiles.firstIndex(where: {
371383
$0.type.isPartOfSwiftCompilation && loadedModuleTracePath != nil
@@ -378,6 +390,7 @@ extension Driver {
378390
addModuleInput: addModuleInput,
379391
addLinkerInput: addLinkerInput,
380392
addJobOutputs: addJobOutputs,
393+
pchCompileJob: pchCompileJob,
381394
emitModuleTrace: index == loadedModuleTraceInputIndex)
382395
}
383396
}
@@ -388,6 +401,7 @@ extension Driver {
388401
addModuleInput: (TypedVirtualPath) -> Void,
389402
addLinkerInput: (TypedVirtualPath) -> Void,
390403
addJobOutputs: ([TypedVirtualPath]) -> Void,
404+
pchCompileJob: Job?,
391405
emitModuleTrace: Bool
392406
) throws
393407
{
@@ -403,6 +417,7 @@ extension Driver {
403417
try createAndAddCompileJobGroup(primaryInput: input,
404418
emitModuleTrace: emitModuleTrace,
405419
canSkipIfOnlyModule: canSkipIfOnlyModule,
420+
pchCompileJob: pchCompileJob,
406421
addCompileJobGroup: addCompileJobGroup,
407422
addJobOutputs: addJobOutputs)
408423

@@ -436,6 +451,7 @@ extension Driver {
436451
primaryInput: TypedVirtualPath,
437452
emitModuleTrace: Bool,
438453
canSkipIfOnlyModule: Bool,
454+
pchCompileJob: Job?,
439455
addCompileJobGroup: (CompileJobGroup) -> Void,
440456
addJobOutputs: ([TypedVirtualPath]) -> Void
441457
) throws {
@@ -444,6 +460,7 @@ extension Driver {
444460
let compile = try compileJob(primaryInputs: [primaryInput],
445461
outputType: .llvmBitcode,
446462
addJobOutputs: addJobOutputs,
463+
pchCompileJob: pchCompileJob,
447464
emitModuleTrace: emitModuleTrace)
448465
let backendJobs = try compile.outputs.compactMap { output in
449466
output.type == .llvmBitcode
@@ -462,6 +479,7 @@ extension Driver {
462479
let compile = try compileJob(primaryInputs: [primaryInput],
463480
outputType: compilerOutputType,
464481
addJobOutputs: addJobOutputs,
482+
pchCompileJob: pchCompileJob,
465483
emitModuleTrace: emitModuleTrace)
466484
addCompileJobGroup(CompileJobGroup(compileJob: compile, backendJob: nil))
467485
}
@@ -821,6 +839,7 @@ extension Driver {
821839
compileJobs.filter { $0.outputs.contains {$0.type == .moduleTrace} }
822840
.flatMap {$0.primaryInputs}
823841
)
842+
let jobCreatingPch: Job? = jobs.first(where: {$0.kind == .generatePCH})
824843

825844
let batchedCompileJobs = try inputsInOrder.compactMap { anInput -> Job? in
826845
let idx = partitions.assignment[anInput]!
@@ -851,6 +870,7 @@ extension Driver {
851870
return try compileJob(primaryInputs: primaryInputs,
852871
outputType: outputType,
853872
addJobOutputs: {_ in },
873+
pchCompileJob: jobCreatingPch,
854874
emitModuleTrace: constituentsEmittedModuleTrace)
855875
}
856876
return batchedCompileJobs + noncompileJobs

0 commit comments

Comments
 (0)