Skip to content

Conversation

chiragramani
Copy link

@chiragramani chiragramani commented Sep 17, 2025

This PR introduces two new instrumentation flags and plumbs them through to IRGen:

  1. -ir-profile-generate - enable IR-level instrumentation.

  2. -cs-profile-generate - enable context-sensitive IR-level instrumentation.

Context: https://forums.swift.org/t/ir-level-pgo-instrumentation-in-swift/82123

Open Questions (Reviewer Input Requested)

  1. Naming:
    Is -ir-profile-generate a good flag name?
    -cs-profile-generate pairs nicely with Clang’s -fcs-profile-generate. However, -ir-profile-generate feels closer to Clang's -fprofile-generate but -profile-generate already exists in Swift as a frontend level and not IR level. Should it stay as is, or is there a clearer alternative?

  2. PR separation strategy:
    I also updated the auto-generated Options.swift file in this PR, I am aware its not supposed to manually updated and I have a Swift-side PR open here: Add IRPGO and CSIRPGO options to Swift swift#84335
    .
    Should I first land a separate PR in Swift adding only the new options, and then follow up with this PR for the Driver + IRGen functionality? My current assumption is that splitting them would help unblock this PR. Is that correct?

@chiragramani chiragramani changed the title Add ir and cs instrumentation options Adds IR level PGO Instrumentation options Sep 17, 2025
Copy link

@ellishg ellishg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you plan to add support for -ir-profile-use and -cs-profile-use?

Comment on lines -261 to -263
if parsedOptions.hasArgument(.profileGenerate) {
commandLine.appendFlag("-fprofile-generate")
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like a bug to me. As I understand, Swift's -profile-generate flag does front-end instrumentation, and is different from Clang's -fprofile-generate.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this behavior is existing in swift-driver today, and I’m a bit wary of changing it here because there may be tests and downstream flows that rely on it. This PR is primarily adding the new pieces, that said, if you feel it’s worth addressing this legacy behavior in the same PR, please let me know and I can look into it.

public static let printTargetInfo: Option = Option("-print-target-info", .flag, attributes: [.frontend], metaVar: "<triple>", helpText: "Print target information for the given target <triple>, such as x86_64-apple-macos10.9")
public static let printZeroStats: Option = Option("-print-zero-stats", .flag, attributes: [.helpHidden, .frontend], helpText: "Prints all stats even if they are zero")
public static let profileCoverageMapping: Option = Option("-profile-coverage-mapping", .flag, attributes: [.frontend, .noInteractive], helpText: "Generate coverage data for use with profiled execution counts")
public static let profileGenerate: Option = Option("-profile-generate", .flag, attributes: [.frontend, .noInteractive], helpText: "Generate instrumented code to collect execution counts")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we change this help text to make it more clear this is front-end instrumentation?

public static let printZeroStats: Option = Option("-print-zero-stats", .flag, attributes: [.helpHidden, .frontend], helpText: "Prints all stats even if they are zero")
public static let profileCoverageMapping: Option = Option("-profile-coverage-mapping", .flag, attributes: [.frontend, .noInteractive], helpText: "Generate coverage data for use with profiled execution counts")
public static let profileGenerate: Option = Option("-profile-generate", .flag, attributes: [.frontend, .noInteractive], helpText: "Generate instrumented code to collect execution counts")
public static let irProfileGenerate: Option = Option("-ir-profile-generate", .flag, attributes: [.frontend, .noInteractive], helpText: "Generate instrumented code to collect execution counts into default.profraw (overridden by LLVM_PROFILE_FILE env var)")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add irProfileGenerateEq like we do for the CS variant?

@artemcm
Copy link
Contributor

artemcm commented Sep 17, 2025

Just a friendly heads-up that new changes need to be added with this process:
https://github.com/swiftlang/swift-driver?tab=readme-ov-file#rebuilding-optionsswift

@chiragramani
Copy link
Author

Do you plan to add support for -ir-profile-use and -cs-profile-use?

Yes, I’ve added -ir-profile-use in the Swift repo, which, in line with Clang, configures PGOOptions correctly. This PR isn’t fully up to date yet, but once the Swift PR lands, I’ll make sure to update this one as well.

@chiragramani
Copy link
Author

Just a friendly heads-up that new changes need to be added with this process: https://github.com/swiftlang/swift-driver?tab=readme-ov-file#rebuilding-optionsswift

Thank you for the heads-up! I’ll take a look and make sure to follow that process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants