← Back to Pull requests

Only pass -u__llvm_profile_runtime when linking for Linux #2221

Closed
unknown closed this pull request without merging on Oct 06, 2026 03:03 UTC
View on GitHub
unknown unknown commented on Oct 05, 2026 19:06 UTC
Author

Problem

When linking with profiling instrumentation (-profile-generate, -ir-profile-generate, -cs-profile-generate, ...), GenericUnixToolchain always adds -u__llvm_profile_runtime to the clang linker-driver invocation, no matter which OS is being targeted:

https://github.com/swiftlang/swift-driver/blob/2b30a750/Sources/SwiftDriver/Jobs/GenericUnixToolchain%2BLinkerSupport.swift#L308-L317

On FreeBSD, clang then warns on every instrumented link:

clang: warning: argument unused during compilation: '-u __llvm_profile_runtime' [-Wunused-command-line-argument]

This is currently breaking swift-build's FreeBSD CI, where ObjectLibraryBuildOperationTests.dynamicLibraryConsumingObjectLibraryWithCodeCoverage() builds with CLANG_COVERAGE_MAPPING=YES and asserts there are no diagnostics:

✘ Test dynamicLibraryConsumingObjectLibraryWithCodeCoverage() recorded an issue at ObjectLibraryBuildOperationTests.swift:466:43: Issue recorded
↳ found unexpected warning: .../aProject.xcodeproj: Argument unused during compilation: '-u __llvm_profile_runtime' [-Wunused-command-line-argument] (for task: ["Ld", ".../build/Debug-freebsd-x86_64/libDylib.so", "normal"])

(https://github.com/swiftlang/swift-build/actions/runs/37103365827/job/111889818672?pr=1828)

Why the flag is only needed on Linux

The flag copies what clang's Linux toolchain does. It is only needed there because of how LLVM lowers instrumentation:

  • llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp, InstrLowerer::emitRuntimeHook():

  •   // We expect the linker to be invoked with -u<hook_var> flag for Linux
      // in which case there is no need to emit the external variable.
      if (TT.isOSLinux() || TT.isOSAIX())
        return false;
      

    On every other target, each instrumented module gets its own reference to __llvm_profile_runtime, so the linker pulls in the runtime without -u.
  • clang/lib/Driver/ToolChains/Linux.cpp (Linux::addProfileRTLibs) is where clang adds -u__llvm_profile_runtime for Linux. The FreeBSD, OpenBSD, NetBSD, Haiku, etc. toolchains don't add it.

  • Clang's GNU/Linux link step forwards -u to the linker (Gnu.cpp: Args.addAllArgs(CmdArgs, {options::OPT_s, options::OPT_t, options::OPT_u})). The FreeBSD link step forwards only {OPT_T_Group, OPT_s, OPT_t} (FreeBSD.cpp), so -u is never used there and clang reports it as unused. OpenBSD, NetBSD and Haiku don't forward OPT_u either.
  • So on non-Linux generic Unix targets, the flag does nothing except produce the warning.

    Fix

    Only add -u__llvm_profile_runtime when targetTriple.os == .linux. Android triples (*-linux-android) count as Linux, so they keep the flag. This now matches clang's behavior. The profile runtime archive is still linked on every target.

    Testing

  • Added a parameterized profileGenerateLinksRuntimeOnFreeBSD test to MiscDriverTests (x86_64-unknown-freebsd14.3), covering -profile-generate and the -ir-/-cs-profile-generate[=] variants. It checks that the profile runtime archive is still linked and that -u__llvm_profile_runtime is not passed.

  • The existing Linux and Android tests, which still expect -u__llvm_profile_runtime, are unchanged.

  • I checked by hand on macOS with -driver-print-jobs -profile-generate:

  • - x86_64-unknown-freebsd14.3: links clang/lib/freebsd/libclang_rt.profile-x86_64.a, no -u
    - x86_64-unknown-linux-gnu: links clang/lib/linux/libclang_rt.profile-x86_64.a, adds -u__llvm_profile_runtime
    - aarch64-unknown-linux-android: links clang/lib/linux/libclang_rt.profile-aarch64-android.a, adds -u__llvm_profile_runtime

    This test is skipped on Darwin hosts (no swift-autolink-extract), so CI on Linux and Windows will be the first to actually run it.

    Diff: 472 lines (+354 / -118) across 5 files Reviewer: cachemeifyoucan Review duration: 476m
    All Vouch review confidence checks passed
    Residual risk 0.18 = Change Risk (0.46) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.46 · 472 lines diff across 5 files
    0.46
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.368
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #2221"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45.7% (MEDIUM) · Higher = larger blast radius
    45.7%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 18.1% · Safe to merge
    18.1%