Add opt-in full-value target-app instrumentation PGO to `ios_ui_test`
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
- Domain
- build-system, mobile-dev, performance, testing-qa
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_hostand its relevant dependencies, enabling Swift-profile-generateand 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
XCTestObservationfallback 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_FILEand 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 plusCoverage.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_testwith 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_pgothe 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
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from bazelbuild/rules_apple
-
`ios_xctestrun_runner` skips the simulator cleanup action on early exits and signalsPossibly taken @katre claimed this 2 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 67/100
bazelbuild/rules_apple#3163 ·
Maintainers usually reply within 1 day
-
register_binary_linking_action links with the GCC coverage map format under coveragePossibly taken @TrapsterDK claimed this 6 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 30/100
bazelbuild/rules_apple#3160 ·
Maintainers usually reply within 1 day
-
Registered toolchains load //apple/internal and its dependencies in every buildMay be free again A pull request for this issue was closed without being merged. Open
Difficulty 3/5 1-2 days Newbie friendliness 30/100
bazelbuild/rules_apple#3158 ·
Maintainers usually reply within 1 day
-
Simulator cleanup is skipped when the post-action failsPossibly taken @vakhidbetrakhmadov claimed this 14 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 58/100
bazelbuild/rules_apple#3147 ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
bazelbuild/rules_apple#3139 ·
Maintainers usually reply within 1 day
All issues in bazelbuild/rules_apple
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
bazelbuild/apple_support#707 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
out-of-date
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
CachyOS/CachyOS-PKGBUILDS#2232 ·
Maintainers usually reply within 1 day
-
blast/large refactor risk/low spend-dem
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
chriskelly/MusicWall#87 · 2 comments ·
Maintainers usually reply within 1 day
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 77/100