Skip to content

Commit 5ea08fe

Browse files
[Caching] Don't produce cache key for initial compile job in batch mode
During batch mode, the initially computed compilation tasks will be batched later. There is no need to compute cache keys for those Jobs since they will not be executed.
1 parent 7ba9d48 commit 5ea08fe

File tree

3 files changed

+62
-13
lines changed

3 files changed

+62
-13
lines changed

Sources/SwiftDriver/Jobs/CompileJob.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ extension Driver {
231231
outputType: FileType?,
232232
addJobOutputs: ([TypedVirtualPath]) -> Void,
233233
pchCompileJob: Job?,
234-
emitModuleTrace: Bool)
234+
emitModuleTrace: Bool,
235+
produceCacheKey: Bool)
235236
throws -> Job {
236237
var commandLine: [Job.ArgTemplate] = swiftCompilerPrefixArgs.map { Job.ArgTemplate.flag($0) }
237238
var inputs: [TypedVirtualPath] = []

Sources/SwiftDriver/Jobs/Planning.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,8 @@ extension Driver {
356356
outputType: compilerOutputType,
357357
addJobOutputs: addJobOutputs,
358358
pchCompileJob: pchCompileJob,
359-
emitModuleTrace: emitModuleTrace)
359+
emitModuleTrace: emitModuleTrace,
360+
produceCacheKey: true)
360361
addJob(compile)
361362
return compile
362363
}
@@ -447,11 +448,14 @@ extension Driver {
447448
// We can skip the compile jobs if all we want is a module when it's
448449
// built separately.
449450
if parsedOptions.hasArgument(.driverExplicitModuleBuild), canSkipIfOnlyModule { return }
451+
// If we are in the batch mode, the constructed jobs here will be batched
452+
// later. There is no need to produce cache key for the job.
450453
let compile = try compileJob(primaryInputs: [primaryInput],
451454
outputType: compilerOutputType,
452455
addJobOutputs: addJobOutputs,
453456
pchCompileJob: pchCompileJob,
454-
emitModuleTrace: emitModuleTrace)
457+
emitModuleTrace: emitModuleTrace,
458+
produceCacheKey: !compilerMode.isBatchCompile)
455459
addCompileJob(compile)
456460
}
457461

@@ -873,7 +877,8 @@ extension Driver {
873877
outputType: compilerOutputType,
874878
addJobOutputs: {_ in },
875879
pchCompileJob: jobCreatingPch,
876-
emitModuleTrace: constituentsEmittedModuleTrace)
880+
emitModuleTrace: constituentsEmittedModuleTrace,
881+
produceCacheKey: true)
877882
}
878883
return batchedCompileJobs + noncompileJobs
879884
}

Tests/SwiftDriverTests/CachingBuildTests.swift

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -986,17 +986,60 @@ final class CachingBuildTests: XCTestCase {
986986
XCTFail("Cached compilation doesn't have a CAS")
987987
}
988988
try checkCASForResults(jobs: jobs, cas: cas, fs: driver.fileSystem)
989+
}
990+
}
989991

990-
// try replan the job and make sure some key command-line options are generated.
991-
let rebuildJobs = try driver.planBuild()
992-
for job in rebuildJobs {
993-
if job.kind == .compile || job.kind == .emitModule {
994-
XCTAssertTrue(job.commandLine.contains(.flag(String("-disable-implicit-swift-modules"))))
995-
XCTAssertTrue(job.commandLine.contains(.flag(String("-cache-compile-job"))))
996-
XCTAssertTrue(job.commandLine.contains(.flag(String("-cas-path"))))
997-
XCTAssertTrue(job.commandLine.contains(.flag(String("-bridging-header-pch-key"))))
998-
}
992+
func testCacheBatchBuildPlan() throws {
993+
try withTemporaryDirectory { path in
994+
try localFileSystem.changeCurrentWorkingDirectory(to: path)
995+
let moduleCachePath = path.appending(component: "ModuleCache")
996+
let casPath = path.appending(component: "cas")
997+
try localFileSystem.createDirectory(moduleCachePath)
998+
let main = path.appending(component: "testCachingBuild.swift")
999+
let mainFileContent = "import C;import E;import G;"
1000+
try localFileSystem.writeFileContents(main) {
1001+
$0.send(mainFileContent)
9991002
}
1003+
let ofm = path.appending(component: "ofm.json")
1004+
let inputPathsAndContents: [(AbsolutePath, String)] = [(main, mainFileContent)]
1005+
OutputFileMapCreator.write(
1006+
module: "Test", inputPaths: inputPathsAndContents.map {$0.0},
1007+
derivedData: path, to: ofm, excludeMainEntry: false)
1008+
1009+
let cHeadersPath: AbsolutePath =
1010+
try testInputsPath.appending(component: "ExplicitModuleBuilds")
1011+
.appending(component: "CHeaders")
1012+
let swiftModuleInterfacesPath: AbsolutePath =
1013+
try testInputsPath.appending(component: "ExplicitModuleBuilds")
1014+
.appending(component: "Swift")
1015+
let sdkArgumentsForTesting = (try? Driver.sdkArgumentsForTesting()) ?? []
1016+
let bridgingHeaderpath: AbsolutePath =
1017+
cHeadersPath.appending(component: "Bridging.h")
1018+
var driver = try Driver(args: ["swiftc",
1019+
"-I", cHeadersPath.nativePathString(escaped: true),
1020+
"-I", swiftModuleInterfacesPath.nativePathString(escaped: true),
1021+
"-explicit-module-build", "-Rcache-compile-job", "-incremental",
1022+
"-module-cache-path", moduleCachePath.nativePathString(escaped: true),
1023+
"-cache-compile-job", "-cas-path", casPath.nativePathString(escaped: true),
1024+
"-import-objc-header", bridgingHeaderpath.nativePathString(escaped: true),
1025+
"-output-file-map", ofm.nativePathString(escaped: true),
1026+
"-working-directory", path.nativePathString(escaped: true),
1027+
main.nativePathString(escaped: true)] + sdkArgumentsForTesting,
1028+
interModuleDependencyOracle: dependencyOracle)
1029+
let jobs = try driver.planBuild()
1030+
try driver.run(jobs: jobs)
1031+
XCTAssertFalse(driver.diagnosticEngine.hasErrors)
1032+
1033+
let scanLibPath = try XCTUnwrap(driver.getSwiftScanLibPath())
1034+
try dependencyOracle.verifyOrCreateScannerInstance(swiftScanLibPath: scanLibPath)
1035+
1036+
let cas = try dependencyOracle.getOrCreateCAS(pluginPath: nil, onDiskPath: casPath, pluginOptions: [])
1037+
if let driverCAS = driver.cas {
1038+
XCTAssertEqual(cas, driverCAS, "CAS should only be created once")
1039+
} else {
1040+
XCTFail("Cached compilation doesn't have a CAS")
1041+
}
1042+
try checkCASForResults(jobs: jobs, cas: cas, fs: driver.fileSystem)
10001043
}
10011044
}
10021045

0 commit comments

Comments
 (0)