File: SwiftDriverJobSchedulingTaskAction.swift

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (352 lines) | stat: -rw-r--r-- 21,654 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SWBUtil
import SWBLibc
public import SWBCore
public import enum SWBLLBuild.BuildValueKind
import Foundation
import SWBProtocol

open class SwiftDriverJobSchedulingTaskAction: TaskAction {
    public override class var toolIdentifier: String {
        assertionFailure("Subclass responsibility")
        return ""
    }


     private enum State {
         /// The action is in it's initial state, and has not yet performed any work
         case initial
         /// The action is waiting for execution inputs to be produced. Execution inputs include everything that would be considered an input to a standalone invocation of the driver (source files, output file map, gate tasks, etc.).
         case waitingForExecutionInputs(openExecutionInputIDs: Set<UInt>, jobTaskIDBase: UInt)
         /// The action has requested a planning task for a target, and is waiting for the resulting planned build.
         case planning(jobTaskIDBase: UInt)
         /// A planned build is available, and the action is requesting and running tasks for individual jobs.
         case requestingDriverJobs(primaryJobsTaskIDs: Set<UInt>, secondaryJobTaskIDs: Set<UInt>, discoveredJobTaskIDs: Set<UInt>, jobTaskIDBase: UInt)

         /// A dependency of the action failed.
         case failedDependencies
         /// The action failed internally
         case executionError(any Error)

         mutating func reset() {
             self = .initial
         }
     }

    private var state = State.initial

    public override func taskSetup(_ task: any ExecutableTask, executionDelegate: any TaskExecutionDelegate, dynamicExecutionDelegate: any DynamicTaskExecutionDelegate) {
        state.reset()

        // Request execution inputs and move to the `waitingForExecutionInputs` state
        if let executionInputs = task.executionInputs {
            let openExecutionInputIDs = Set(executionInputs.indices.map(UInt.init))
            // `jobTaskIDBase` represents the first ID corresponding to a planned driver job. 0..<executionInputs.count are execution input IDs, executionInputs.count is the dynamic planning ID, and (executionInputs.count + 1)... are driver job IDs.
            let jobTaskIDBase = UInt(executionInputs.count) + 1
            state = .waitingForExecutionInputs(openExecutionInputIDs: openExecutionInputIDs, jobTaskIDBase: jobTaskIDBase)
            for (index, input) in executionInputs.enumerated() {
                dynamicExecutionDelegate.requestInputNode(node: input, nodeID: UInt(index))
            }
        }
    }

    public override func taskDependencyReady(_ task: any ExecutableTask, _ dependencyID: UInt, _ buildValueKind: BuildValueKind?, dynamicExecutionDelegate: any DynamicTaskExecutionDelegate, executionDelegate: any TaskExecutionDelegate) {
        guard let buildValueKind else {
            state = .failedDependencies
            return
        }
        if buildValueKind.isFailed {
            if case .prepareForIndexing = executionDelegate.buildCommand, case .waitingForExecutionInputs = state {
                // Ignore the failed dependency.
            } else {
                state = .failedDependencies
                return
            }
        }

        guard let payload = task.payload as? SwiftTaskPayload, let driverPayload = payload.driverPayload else {
            state = .executionError(StubError.error("Invalid payload for Swift explicit module support"))
            return
        }

        let graph = dynamicExecutionDelegate.operationContext.swiftModuleDependencyGraph

        switch state {
        case .initial:
            state = .executionError(StubError.error("taskDependencyReady unexpectedly called in initial state"))
        case .waitingForExecutionInputs(openExecutionInputIDs: var openExecutionInputIDs, jobTaskIDBase: let jobTaskIDBase):
            if openExecutionInputIDs.contains(dependencyID) {
                openExecutionInputIDs.remove(dependencyID)
                state = .waitingForExecutionInputs(openExecutionInputIDs: openExecutionInputIDs, jobTaskIDBase: jobTaskIDBase)
            }
            // If all execution inputs are now available, request the driver planning task and move to the `planning` state.
            if openExecutionInputIDs.isEmpty {
                dynamicExecutionDelegate.requestDynamicTask(toolIdentifier: SwiftDriverTaskAction.toolIdentifier,
                                                            taskKey: .swiftDriverPlanning(.init(swiftPayload: payload)),
                                                            taskID: jobTaskIDBase - 1,
                                                            singleUse: true,
                                                            workingDirectory: task.workingDirectory,
                                                            environment: task.environment,
                                                            forTarget: task.forTarget,
                                                            priority: .unblocksDownstreamTasks,
                                                            showEnvironment: task.showEnvironment,
                                                            reason: .wasScheduledBySwiftDriver)
                state = .planning(jobTaskIDBase: jobTaskIDBase)
            }
            return
        case .planning(jobTaskIDBase: let jobTaskIDBase):
            if buildValueKind == .skippedCommand {
                // If planning was skipped, for example because it didn't start in a prepare-for-indexing build, move into the failed dependencies state so that we cancel.
                state = .failedDependencies
                return
            }
            // If the planning task is complete, begin requesting dynamic tasks for jobs and move to the `requestingDriverJobs` state.
            guard dependencyID == jobTaskIDBase - 1 else { return }
            do {
                let plannedBuild = try graph.queryPlannedBuild(for: driverPayload.uniqueID)
                let primaryJobs = primaryJobs(for: plannedBuild, driverPayload: driverPayload)
                let untrackedPrimaryJobs = untrackedPrimaryJobs(for: plannedBuild, driverPayload: driverPayload)
                if !primaryJobs.isEmpty {
                    var primaryJobsTaskIDs: Set<UInt> = []
                    scheduleJobs(dynamicExecutionDelegate, task, driverPayload: driverPayload, plannedBuild: plannedBuild, primaryJobs, cacheTaskID: { primaryJobsTaskIDs.insert($0) }, jobTaskIDBase: jobTaskIDBase)
                    scheduleJobs(dynamicExecutionDelegate, task, driverPayload: driverPayload, plannedBuild: plannedBuild, untrackedPrimaryJobs, jobTaskIDBase: jobTaskIDBase)
                    state = .requestingDriverJobs(primaryJobsTaskIDs: primaryJobsTaskIDs, secondaryJobTaskIDs: [], discoveredJobTaskIDs: [], jobTaskIDBase: jobTaskIDBase)
                } else {
                    var secondaryJobTaskIDs: Set<UInt> = []
                    scheduleJobs(dynamicExecutionDelegate, task, driverPayload: driverPayload, plannedBuild: plannedBuild, secondaryJobs(for: plannedBuild, driverPayload: driverPayload), cacheTaskID: { secondaryJobTaskIDs.insert($0) }, jobTaskIDBase: jobTaskIDBase)
                    state = .requestingDriverJobs(primaryJobsTaskIDs: [], secondaryJobTaskIDs: secondaryJobTaskIDs, discoveredJobTaskIDs: [], jobTaskIDBase: jobTaskIDBase)
                }
            } catch {
                state = .executionError(error)
            }
        case .requestingDriverJobs(primaryJobsTaskIDs: var primaryJobsTaskIDs, secondaryJobTaskIDs: var secondaryJobTaskIDs, discoveredJobTaskIDs: var discoveredJobTaskIDs, jobTaskIDBase: let jobTaskIDBase):
            do {
                func scheduleDiscoveredJobs(for dependencyID: UInt) throws {
                    let plannedBuild = try graph.queryPlannedBuild(for: driverPayload.uniqueID)

                    guard let jobFinished = plannedBuild.plannedTargetJob(for: .targetJob(Int(dependencyID - jobTaskIDBase))) else {
                        return
                    }

                    let signatureCtx = InsecureHashContext()
                    signatureCtx.add(string: task.identifier.rawValue)
                    signatureCtx.add(string: "swiftdriverjobdiscoveryactivity")
                    signatureCtx.add(number: dependencyID)

                    try dynamicExecutionDelegate.withActivity(ruleInfo: "SwiftDriverJobDiscovery \(driverPayload.variant) \(driverPayload.architecture) \(jobFinished.description)", executionDescription: "Discovering Swift tasks after '\(jobFinished.description)'", signature: signatureCtx.signature, target: task.forTarget, parentActivity: nil) { _ in
                        let discovered = try plannedBuild.getDiscoveredJobsAfterFinishing(job: jobFinished)
                        scheduleJobs(dynamicExecutionDelegate, task, driverPayload: driverPayload, plannedBuild: plannedBuild, discovered, cacheTaskID: { discoveredJobTaskIDs.insert($0) }, jobTaskIDBase: jobTaskIDBase)
                        return .succeeded
                    }
                }

                if primaryJobsTaskIDs.contains(dependencyID) {
                    primaryJobsTaskIDs.remove(dependencyID)

                    // Explicit module tasks have no secondary or discovered, etc. jobs.
                    if Self.toolIdentifier != "swift-driver-explicit-modules" {
                        try scheduleDiscoveredJobs(for: dependencyID)

                        if primaryJobsTaskIDs.isEmpty {
                            // Primary tasks done, schedule secondary tasks
                            let plannedBuild = try graph.queryPlannedBuild(for: driverPayload.uniqueID)
                            scheduleJobs(dynamicExecutionDelegate, task, driverPayload: driverPayload, plannedBuild: plannedBuild, secondaryJobs(for: plannedBuild, driverPayload: driverPayload), cacheTaskID: { secondaryJobTaskIDs.insert($0) }, jobTaskIDBase: jobTaskIDBase)
                        }
                    }
                } else if secondaryJobTaskIDs.contains(dependencyID) {
                    secondaryJobTaskIDs.remove(dependencyID)
                    try scheduleDiscoveredJobs(for: dependencyID)
                } else if discoveredJobTaskIDs.contains(dependencyID) {
                    discoveredJobTaskIDs.remove(dependencyID)
                    try scheduleDiscoveredJobs(for: dependencyID)
                } else {
                    // Ignore any other reported inputs
                }
                state = .requestingDriverJobs(primaryJobsTaskIDs: primaryJobsTaskIDs, secondaryJobTaskIDs: secondaryJobTaskIDs, discoveredJobTaskIDs: discoveredJobTaskIDs, jobTaskIDBase: jobTaskIDBase)
            } catch {
                state = .executionError(error)
            }
        case .failedDependencies:
            break
        case .executionError(_):
            break
        }
    }

    public override func performTaskAction(_ task: any ExecutableTask, dynamicExecutionDelegate: any DynamicTaskExecutionDelegate, executionDelegate: any TaskExecutionDelegate, clientDelegate: any TaskExecutionClientDelegate, outputDelegate: any TaskOutputDelegate) async -> CommandResult {
        defer {
            state.reset()
        }

        if case .executionError(let error) = state {
            outputDelegate.error(error.localizedDescription)
            return .failed
        }

        if case .failedDependencies = state {
            return .cancelled
        }

        guard let driverPayload = (task.payload as? SwiftTaskPayload)?.driverPayload else {
            outputDelegate.error(StubError.error("Invalid payload for Swift explicit module support"))
            return .failed
        }

        // Report skipped jobs if this is the last task action to run
        let graph = dynamicExecutionDelegate.operationContext.swiftModuleDependencyGraph
        do {
            let plannedBuild = try graph.queryPlannedBuild(for: driverPayload.uniqueID)
            guard case .requestingDriverJobs(primaryJobsTaskIDs: let primaryJobsTaskIDs, secondaryJobTaskIDs: let secondaryJobTaskIDs, discoveredJobTaskIDs: let discoveredJobTaskIDs, jobTaskIDBase: let jobTaskIDBase) = state else {
                throw StubError.error("Finished job execution in unexpected state: \(state)")
            }
            guard primaryJobsTaskIDs.isEmpty, secondaryJobTaskIDs.isEmpty, discoveredJobTaskIDs.isEmpty else {
                let jobs = primaryJobsTaskIDs.union(secondaryJobTaskIDs).union(discoveredJobTaskIDs).compactMap {
                    plannedBuild.plannedTargetJob(for: .targetJob(Int($0 - jobTaskIDBase)))
                }
                throw StubError.error("Internal error with integrated Swift driver. Some planned jobs weren't tracked accordingly: [\(jobs.map(\.debugDescription))]")
            }

            if shouldReportSkippedJobs(driverPayload: driverPayload) {
                try reportSkippedJobs(task, outputDelegate: outputDelegate, driverPayload: driverPayload, plannedBuild: plannedBuild, dynamicExecutionDelegate: dynamicExecutionDelegate)
            }

            let planningDependencies = try graph.queryPlanningDependencies(for: driverPayload.uniqueID)
            if executionDelegate.userPreferences.enableDebugActivityLogs {
                outputDelegate.emitOutput(ByteString(encodingAsUTF8: "Discovered dependency nodes:\n" + planningDependencies.joined(separator: "\n") + "\n"))
            }

            if driverPayload.verifyScannerDependencies {
                if case .makefileIgnoringSubsequentOutputs(let makefilePath) = task.dependencyData {
                    // This is a very rudimentary parser for make-style dependencies as emitted by swiftc.
                    let contents = try executionDelegate.fs.read(makefilePath)
                    let firstLine = contents.asString.split("\n").0
                    let inputs = firstLine.split(":").1.components(separatedBy: " ").map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }.filter { !$0.isEmpty }
                    let makeStyleInputs = Set(inputs)
                    let scannerInputs = Set(planningDependencies)
                    let inputsMissedByScanner = makeStyleInputs.subtracting(scannerInputs)
                    for missedInput in inputsMissedByScanner.sorted() {
                        outputDelegate.emitError("Dependency scanner failed to report input '\(missedInput)' present in '\(makefilePath.str)'")
                    }
                }
            }

            let dependencyFilteringRootPathString = driverPayload.dependencyFilteringRootPath?.str
            for dep in planningDependencies {
                if let dependencyFilteringRootPathString {
                    // We intentionally do a prefix check instead of an ancestor check here, for performance reasons. The filtering path (SDK path) and paths returned by the compiler are guaranteed to be normalized, which makes this safe.
                    if !dep.hasPrefix(dependencyFilteringRootPathString) {
                        dynamicExecutionDelegate.discoveredDependencyNode(ExecutionNode(identifier: dep))
                    }
                } else {
                    dynamicExecutionDelegate.discoveredDependencyNode(ExecutionNode(identifier: dep))
                }
            }
        } catch {
            outputDelegate.error(error)
            return .failed
        }

        return .succeeded
    }


    open func primaryJobs(for plannedBuild: LibSwiftDriver.PlannedBuild, driverPayload: SwiftDriverPayload) -> ArraySlice<LibSwiftDriver.PlannedBuild.PlannedSwiftDriverJob> {
        assertionFailure("Subclass responsibility")
        return []
    }

    open func untrackedPrimaryJobs(for plannedBuild: LibSwiftDriver.PlannedBuild, driverPayload: SwiftDriverPayload) -> ArraySlice<LibSwiftDriver.PlannedBuild.PlannedSwiftDriverJob> {
        assertionFailure("Subclass responsibility")
        return []
    }

    open func secondaryJobs(for plannedBuild: LibSwiftDriver.PlannedBuild, driverPayload: SwiftDriverPayload) -> ArraySlice<LibSwiftDriver.PlannedBuild.PlannedSwiftDriverJob> {
        return []
    }

    open func shouldReportSkippedJobs(driverPayload: SwiftDriverPayload) -> Bool {
        /// Open to subclasses to decide
        false
    }

    private func reportSkippedJobs(_ task: any ExecutableTask, outputDelegate: any TaskOutputDelegate, driverPayload: SwiftDriverPayload, plannedBuild: LibSwiftDriver.PlannedBuild, dynamicExecutionDelegate: any DynamicTaskExecutionDelegate) throws {
        let spec = SwiftDriverJobDynamicTaskSpec()
        try plannedBuild.reportSkippedJobs { job in
            if job.driverJob.ruleInfoType == "Compile" {
                // When reported as skipped, compile jobs are treated like per-file virtual subtasks which have been 'hoisted' up as top-level tasks.
                guard let target = task.forTarget, let singleInput = job.driverJob.displayInputs.only else {
                    return
                }
                outputDelegate.previouslyBatchedSubtaskUpToDate(signature: SwiftCompilerSpec.computeRuleInfoAndSignatureForPerFileVirtualBatchSubtask(variant: driverPayload.variant, arch: driverPayload.architecture, path: singleInput).1, target: target)
            } else {
                // Other jobs are reported as skipped/up-to-date in the usual way.
                let taskKey = SwiftDriverJobTaskKey(identifier: driverPayload.uniqueID, variant: driverPayload.variant, arch: driverPayload.architecture, driverJobKey: job.key, driverJobSignature: job.signature, isUsingWholeModuleOptimization: driverPayload.isUsingWholeModuleOptimization, compilerLocation: driverPayload.compilerLocation, casOptions: driverPayload.casOptions)
                let dynamicTask = DynamicTask(toolIdentifier: SwiftDriverJobTaskAction.toolIdentifier, taskKey: .swiftDriverJob(taskKey), workingDirectory: task.workingDirectory, environment: task.environment, target: task.forTarget, showEnvironment: task.showEnvironment)
                let subtask = try spec.buildExecutableTask(dynamicTask: dynamicTask, context: dynamicExecutionDelegate.operationContext)
                outputDelegate.subtaskUpToDate(subtask)
            }
        }
    }

    internal func constructDriverJobTaskKey(driverPayload: SwiftDriverPayload,
                                            plannedJob: LibSwiftDriver.PlannedBuild.PlannedSwiftDriverJob) -> DynamicTaskKey {
        let key: DynamicTaskKey
        if plannedJob.driverJob.categorizer.isExplicitDependencyBuild {
            key = .swiftDriverExplicitDependencyJob(SwiftDriverExplicitDependencyJobTaskKey(
                arch: driverPayload.architecture,
                driverJobKey: plannedJob.key,
                driverJobSignature: plannedJob.signature,
                compilerLocation: driverPayload.compilerLocation,
                casOptions: driverPayload.casOptions))
        } else {
            key = .swiftDriverJob(SwiftDriverJobTaskKey(
                identifier: driverPayload.uniqueID,
                variant: driverPayload.variant,
                arch: driverPayload.architecture,
                driverJobKey: plannedJob.key,
                driverJobSignature: plannedJob.signature,
                isUsingWholeModuleOptimization: driverPayload.isUsingWholeModuleOptimization,
                compilerLocation: driverPayload.compilerLocation,
                casOptions: driverPayload.casOptions))
        }
        return key
    }

    private func scheduleJobs<S: Collection>(_ dynamicExecutionDelegate: any DynamicTaskExecutionDelegate, _ task: any ExecutableTask, driverPayload: SwiftDriverPayload, plannedBuild: LibSwiftDriver.PlannedBuild, _ jobs: S, cacheTaskID: ((UInt) -> Void)? = nil, jobTaskIDBase: UInt) where S.Element == LibSwiftDriver.PlannedBuild.PlannedSwiftDriverJob {
        for plannedJob in jobs {
            let isExplicitDependencyBuildJob = plannedJob.driverJob.categorizer.isExplicitDependencyBuild
            let taskID: UInt
            switch plannedJob.key {
                case .targetJob(let index):
                    taskID = UInt(index) + jobTaskIDBase
                case .explicitDependencyJob(let index):
                    taskID = UInt(index) + jobTaskIDBase + UInt(plannedBuild.targetBuildJobCount)
            }
            cacheTaskID?(taskID)
            let taskKey = constructDriverJobTaskKey(driverPayload: driverPayload, plannedJob: plannedJob)
            dynamicExecutionDelegate.requestDynamicTask(
                toolIdentifier: SwiftDriverJobTaskAction.toolIdentifier,
                taskKey: taskKey,
                taskID: taskID,
                singleUse: true,
                workingDirectory: plannedJob.workingDirectory,
                environment: task.environment,
                forTarget: isExplicitDependencyBuildJob ? nil : task.forTarget,
                priority: plannedJob.driverJob.categorizer.priority,
                showEnvironment: task.showEnvironment,
                reason: .wasScheduledBySwiftDriver
            )
        }
    }
}