Skip to content

Commit

Permalink
Switch from destructor based time report to defer based time report t…
Browse files Browse the repository at this point in the history
…o silent unsude variable warnings within Profiling probes
  • Loading branch information
adevress committed Nov 16, 2024
1 parent fb463f7 commit 64022db
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 22 deletions.
13 changes: 9 additions & 4 deletions Sources/CodeGen/LLVM/LLVMProgram.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public struct LLVMProgram {
self.target = try target ?? SwiftyLLVM.TargetMachine(for: .host())
for m in ir.modules.keys {
var context = CodeGenerationContext(forCompiling: m, of: ir)
let _convertProbe = profiler?.createTimeMeasurementProbe(MeasurementType.IRConversion)
let probe = profiler?.createAndStartProfilingProbe(MeasurementType.IRConversion)
defer { probe?.stop() }

let transpilation = SwiftyLLVM.Module(transpiling: m, in: &context)
do {
try transpilation.verify()
Expand All @@ -43,7 +45,8 @@ public struct LLVMProgram {

/// Applies the mandatory IR simplification passes on each module in `self`.
public mutating func applyMandatoryPasses() {
let _optimizeProbe = profiler?.createTimeMeasurementProbe(MeasurementType.MandatoryPass)
let probe = profiler?.createAndStartProfilingProbe(MeasurementType.MandatoryPass)
defer { probe?.stop() }
for k in llvmModules.keys {
llvmModules[k]!.runDefaultModulePasses(optimization: .none, for: target)
}
Expand All @@ -53,7 +56,8 @@ public struct LLVMProgram {
///
/// Optimization applied are similar to clang's `-O3`.
public mutating func optimize() {
let _mandatoryProbe = profiler?.createTimeMeasurementProbe(MeasurementType.Optimizations)
let probe = profiler?.createAndStartProfilingProbe(MeasurementType.Optimizations)
defer { probe?.stop() }
for k in llvmModules.keys {
llvmModules[k]!.runDefaultModulePasses(optimization: .aggressive, for: target)
}
Expand All @@ -70,7 +74,8 @@ public struct LLVMProgram {
var result: [URL] = []
for m in llvmModules.values {
let f = directory.appendingPathComponent(m.name).appendingPathExtension("o")
let _writeProbe = profiler?.createTimeMeasurementProbe(MeasurementType.EmitPhase)
let probe = profiler?.createAndStartProfilingProbe(MeasurementType.EmitPhase)
defer { probe?.stop() }
try m.write(type, for: target, to: f.fileSystemPath)
result.append(f)
}
Expand Down
12 changes: 8 additions & 4 deletions Sources/Driver/Driver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,8 @@ public struct Driver: ParsableCommand {
program: TypedProgram, reportingDiagnosticsTo log: inout DiagnosticSet,
profileWith profiler: ProfilingMeasurements?
) throws -> IR.Program {
let _irProbe = profiler?.createTimeMeasurementProbe(MeasurementType.IRLowering)
let probe = profiler?.createAndStartProfilingProbe(MeasurementType.IRLowering)
defer { probe?.stop() }
var loweredModules: [ModuleDecl.ID: IR.Module] = [:]
for d in program.ast.modules {
loweredModules[d] = try lower(d, in: program, reportingDiagnosticsTo: &log)
Expand Down Expand Up @@ -433,7 +434,8 @@ public struct Driver: ParsableCommand {
let xcrun = try findExecutable(invokedAs: "xcrun").fileSystemPath

// linking profiling probe
let _probe = profiler?.createTimeMeasurementProbe(MeasurementType.LinkPhase)
let probe = profiler?.createAndStartProfilingProbe(MeasurementType.LinkPhase)
defer { probe?.stop() }

Check warning on line 439 in Sources/Driver/Driver.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Driver/Driver.swift#L435-L439

Added lines #L435 - L439 were not covered by tests
let sdk =
try runCommandLine(xcrun, ["--sdk", "macosx", "--show-sdk-path"], diagnostics: &diagnostics)
Expand Down Expand Up @@ -467,7 +469,8 @@ public struct Driver: ParsableCommand {
arguments.append(contentsOf: libraries.map({ "-l\($0)" }))

// linking profiling probe
let _probe = profiler?.createTimeMeasurementProbe(MeasurementType.LinkPhase)
let probe = profiler?.createAndStartProfilingProbe(MeasurementType.LinkPhase)
defer { probe?.stop() }

// Note: We use "clang" rather than "ld" so that to deal with the entry point of the program.
// See https://stackoverflow.com/questions/51677440
Expand All @@ -484,7 +487,8 @@ public struct Driver: ParsableCommand {
profileWith profiler: ProfilingMeasurements?
) throws {
// linking profiling probe
let _probe = profiler?.createTimeMeasurementProbe(MeasurementType.LinkPhase)
let probe = profiler?.createAndStartProfilingProbe(MeasurementType.LinkPhase)
defer { probe?.stop() }

Check warning on line 492 in Sources/Driver/Driver.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Driver/Driver.swift#L489-L492

Added lines #L489 - L492 were not covered by tests
try runCommandLine(
findExecutable(invokedAs: "lld-link").fileSystemPath,
Expand Down
3 changes: 2 additions & 1 deletion Sources/FrontEnd/Parse/Lexer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public struct Lexer: IteratorProtocol, Sequence {
/// Advances to the next token and returns it, or returns `nil` if no next token exists.
public mutating func next() -> Token? {
// Start measuring Lexer time
let _probe = profiler?.createTimeMeasurementProbe(MeasurementType.Lexer)
let probe = profiler?.createAndStartProfilingProbe(MeasurementType.Lexer)
defer { probe?.stop() }

// Skip whitespaces and comments.
while true {
Expand Down
3 changes: 2 additions & 1 deletion Sources/FrontEnd/Parse/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public enum Parser {
var members: [AnyDeclID] = []

// start time measurement if needed
let _probe = profiler?.createTimeMeasurementProbe(MeasurementType.Parser)
let probe = profiler?.createAndStartProfilingProbe(MeasurementType.Parser)
defer { probe?.stop() }

while let head = state.peek() {
// Ignore semicolons.
Expand Down
9 changes: 6 additions & 3 deletions Sources/FrontEnd/TypedProgram.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public struct TypedProgram {
tasks.append(t)
}

let _probe = profiler?.createTimeMeasurementProbe(MeasurementType.TypeChecker)
let probe = profiler?.createAndStartProfilingProbe(MeasurementType.TypeChecker)
defer { probe?.stop() }

Check warning on line 86 in Sources/FrontEnd/TypedProgram.swift

View check run for this annotation

Codecov / codecov/patch

Sources/FrontEnd/TypedProgram.swift#L85-L86

Added lines #L85 - L86 were not covered by tests
let queue = OperationQueue()
queue.addOperations(tasks, waitUntilFinished: true)
for t in tasks {
Expand All @@ -95,7 +96,8 @@ public struct TypedProgram {
constructing: $0,
tracingInferenceIf: typeCheckingIsParallel ? nil : shouldTraceInference,
loggingRequirementSystemIf: typeCheckingIsParallel ? nil : shouldLogRequirements)
let _probe = profiler?.createTimeMeasurementProbe(MeasurementType.TypeChecker)
let probe = profiler?.createAndStartProfilingProbe(MeasurementType.TypeChecker)
defer { probe?.stop() }
checker.checkAllDeclarations()

log.formUnion(checker.diagnostics)
Expand Down Expand Up @@ -126,7 +128,8 @@ public struct TypedProgram {
tracingInferenceIf: shouldTraceInference,
loggingRequirementSystemIf: shouldLogRequirements)

let _probe = profiler?.createTimeMeasurementProbe(MeasurementType.TypeChecker)
let probe = profiler?.createAndStartProfilingProbe(MeasurementType.TypeChecker)
defer { probe?.stop() }
checker.checkModule(m)

log.formUnion(checker.diagnostics)
Expand Down
3 changes: 2 additions & 1 deletion Sources/IR/Analysis/Module+Depolymorphize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ extension IR.Program {

/// Generates the non-parametric reslient APIs of the modules in `self`.
public mutating func depolymorphize(profileWith profiler: ProfilingMeasurements? = nil) {
let _probe = profiler?.createTimeMeasurementProbe(MeasurementType.Depolymorphize)
let probe = profiler?.createAndStartProfilingProbe(MeasurementType.Depolymorphize)
defer { probe?.stop() }
for m in modules.keys {
depolymorphize(m)
}
Expand Down
22 changes: 14 additions & 8 deletions Sources/Utils/Profiling.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public enum MeasurementType: Int, CaseIterable {
}

Check warning on line 16 in Sources/Utils/Profiling.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Utils/Profiling.swift#L14-L16

Added lines #L14 - L16 were not covered by tests
}

/// Time measurement probe. Will report time between init() and stop()
/// to the associate ProfilingMeasurements object
public struct TimeMeasurementProbe: ~Copyable {

var beginning: ContinuousClock.Instant
Expand All @@ -28,29 +30,33 @@ public struct TimeMeasurementProbe: ~Copyable {
self.measure_type = measure_type
}

Check warning on line 31 in Sources/Utils/Profiling.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Utils/Profiling.swift#L27-L31

Added lines #L27 - L31 were not covered by tests

deinit {
public func stop() {
let time: Duration = ContinuousClock.Instant.now - beginning
pool.addProfiledDuration(self.measure_type, time)
}

Check warning on line 36 in Sources/Utils/Profiling.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Utils/Profiling.swift#L33-L36

Added lines #L33 - L36 were not covered by tests

}

/// A container for profiling measurement
/// A container for profiling measurements
public class ProfilingMeasurements {

public init() {}

Check warning on line 43 in Sources/Utils/Profiling.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Utils/Profiling.swift#L43

Added line #L43 was not covered by tests

var cumulatedDurations: [Duration] = [Duration](
repeating: Duration.seconds(0), count: MeasurementType.count)

Check warning on line 46 in Sources/Utils/Profiling.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Utils/Profiling.swift#L45-L46

Added lines #L45 - L46 were not covered by tests

public func createTimeMeasurementProbe(_ measure_type: MeasurementType) -> TimeMeasurementProbe {
/// Create a new TimeMeasurementProbe for a given measure type
public func createAndStartProfilingProbe(_ measure_type: MeasurementType) -> TimeMeasurementProbe
{
return TimeMeasurementProbe(measure_type, self)
}

Check warning on line 52 in Sources/Utils/Profiling.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Utils/Profiling.swift#L50-L52

Added lines #L50 - L52 were not covered by tests

public func addProfiledDuration(_ measure_type: MeasurementType, _ duration: Duration) {
/// Internal. Reserved for TimeMeasurementProbe
internal func addProfiledDuration(_ measure_type: MeasurementType, _ duration: Duration) {
self.cumulatedDurations[measure_type.rawValue] += duration
}

Check warning on line 57 in Sources/Utils/Profiling.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Utils/Profiling.swift#L55-L57

Added lines #L55 - L57 were not covered by tests

/// Print the current profiling report
public func printProfilingReport() {
let profilingReport = """
**Compile time profiling summary**
Expand All @@ -71,7 +77,7 @@ public class ProfilingMeasurements {
print(profilingReport)
}

Check warning on line 78 in Sources/Utils/Profiling.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Utils/Profiling.swift#L60-L78

Added lines #L60 - L78 were not covered by tests

public func measurement(_ measure_type: MeasurementType) -> Duration {
public func durationFor(_ measure_type: MeasurementType) -> Duration {
switch measure_type {
case .Parser:
return cumulatedDurations[MeasurementType.Parser.rawValue]
Expand All @@ -82,13 +88,13 @@ public class ProfilingMeasurements {
}

Check warning on line 88 in Sources/Utils/Profiling.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Utils/Profiling.swift#L80-L88

Added lines #L80 - L88 were not covered by tests

public func formattedMeasurement(_ measure_type: MeasurementType) -> String {
let measure = measurement(measure_type)
let measurementDouble =
let measure = durationFor(measure_type)
let measureDouble =
Double(measure.components.seconds) + Double(measure.components.attoseconds) / 1e18
let unitArrays = ["s", "ms", "us", "ns", "ps"]
var factor = 1
for i in 0...unitArrays.count {
let scaledMeasurement = measurementDouble * Double(factor)
let scaledMeasurement = measureDouble * Double(factor)
if scaledMeasurement > 1.0 {
return "\(String(format: "%.03f", scaledMeasurement)) \(unitArrays[i])"
}
Expand Down

0 comments on commit 64022db

Please sign in to comment.