Add opt-in full-value target-app instrumentation PGO to `ios_ui_test`

Open
#3,085 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
objective-c, swift

Research direction

Start with the ios_ui_test rule and PR #3037’s collection primitive, then trace the proposed XCTestObservation and XCUIApplication.terminate integration. Review the __llvm_profile_dump() flow and session-token behavior; done means the opt-in journey produces a fresh, validated Coverage.profdata with full value data before target-app termination.

Written by the indexing model from the issue text.

Description

Request

Add an opt-in feature that uses an existing ios_ui_test to collect a target-app PGO profile for later optimized Swift and Clang builds:

ios_ui_test(
    name = "JourneyUITests",
    test_host = ":App",
    features = ["apple.target_app_pgo"],
    deps = [":JourneyUITestsLib"],
)

Users should not need to change application source, add support dependencies, configure profile environment variables, or write custom teardown code. The test should publish a validated Coverage.profdata containing execution counters and PGO value records.

Problem

The UI-test runner and target app are separate processes. The useful profile belongs to the target app, so the app must write it before the test terminates it.

PR #3037 provides the runner-side collection and validation. The remaining problem is flushing the profile. Normal-mode LLVM instrumentation writes its data on normal exit or an explicit in-process __llvm_profile_dump() call.

Existing tests often call app.terminate(). In a local Xcode 26.0 simulator reproducer, this terminated the target app by signal. Neither applicationWillTerminate nor atexit ran, and the normal-mode profile stayed empty. Calling __llvm_profile_dump() inside the app before termination produced a valid profile with value data.

LLVM %c continuous mode avoids losing counters on forced termination, but does not serialize value profiles. Counters have a fixed layout that can be memory-mapped; value profiles are runtime-built histograms that need serialization. An explicit dump in continuous mode does not add the missing records.

Those values matter: indirect-call target records can guide indirect-call promotion, and memory-operation size records can guide specialization of operations such as memcpy. This proposal covers instrumentation PGO, not MemProf allocation or lifetime profiling.

Proposed workflow

Use normal-mode profiling and automatically request a dump before terminating the target app:

Build instrumented test host
Run existing UI test
Request a dump inside the target app
Wait for success
Terminate the app
Collect and merge profiles into Coverage.profdata

The proposed implementation would:

  • Apply a configuration transition to test_host and its relevant dependencies, enabling Swift -profile-generate and Clang -fprofile-generate. Ordinary and release builds remain unchanged.
  • Inject a small profile-control endpoint into the target app and a controller into the UI-test bundle. No manually declared support dependencies are needed.
  • Intercept XCUIApplication.terminate() only for the configured test host. Request a dump, wait for acknowledgment, then call the original termination method. A timeout or failed dump fails the test.
  • Use an XCTestObservation fallback to dump when a test finishes with the app still running. Provide explicit checkpoint and dump-then-terminate APIs for advanced tests.
  • Give both processes a unique per-run session token. Activate the endpoint only in the opt-in instrumented configuration, with LLVM_PROFILE_FILE and a valid token. Serialize concurrent requests and avoid writing the same completed dump again.
  • Build on #3037 to clear stale outputs, collect only fresh profiles, merge with llvm-profdata, validate the result, and publish raw profiles plus Coverage.profdata.

A filename such as %t/target-app-pgo/<session>/%p-%m.profraw separates processes and images. %m supports online merging when a complete dump occurs; it is not continuous mode. The runner then merges the separate outputs into the final artifact.

rules_apple owns the test-host configuration, support-library injection, runner environment, and test outputs. LLVM already provides the dump API, so a new compiler-rt protocol should not be required for the first version.

Initial scope

  • Simulator ios_ui_test with one configured target app.
  • Swift and Clang normal-mode IR instrumentation, including counters and selected value records.
  • Multiple launches or app processes merged into one artifact.
  • Physical devices and a generic upstream compiler-rt control protocol can follow later.

Acceptance criteria

  • One documented feature enables the workflow without app source changes, manual support dependencies, or custom teardown.
  • Only the test-host configuration is instrumented; tests without the feature keep their existing behavior and build graph.
  • Existing app.terminate() calls wait for a successful target-app dump. Tests that leave the app running also receive a final dump.
  • Explicit dump and dump-then-terminate APIs are available.
  • Multiple launches or processes produce non-conflicting profiles that merge successfully.
  • An integration test proves the merged profile contains IR counters, an indirect-call target record, and a memory-operation size record.
  • Missing, empty, stale, malformed, timed-out, or unacknowledged profiles fail deterministically.
  • The control endpoint is disabled outside the opt-in profiled test configuration.

Questions for maintainers

  • Is apple.target_app_pgo the right feature name?
  • Should the support endpoint live in rules_apple, apple_support, or another target?
  • Should explicit checkpoint APIs ship in the first version?
  • Should physical-device support be required initially or follow later?
  • Should a generic compiler-rt control protocol be proposed after this integration proves the approach?

Related: PR #3037 provides the collection primitive. Issue #338 covers broader Apple-platform coverage support.

AI assistance

I used AI assistance for the investigation, reproducer, compiler-rt source review, and drafting. I reviewed the reproduced behavior and proposed architecture.

Dominant language
Starlark
Stars
600
Forks
354
Avg merge
2d 7h
Merged PRs (30d)
8

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from bazelbuild/rules_apple

All issues in bazelbuild/rules_apple

Similar issues

More Build System issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.