feat: add Profile-Guided Optimization (PGO) support#3085
Merged
Conversation
Add `--pgo` CLI flag and `pgo-command` config in `[tool.maturin]` to enable PGO builds via a three-phase pipeline: 1. Build instrumented wheel with `-Cprofile-generate` 2. Install in temp venv and run the user's pgo-command to collect `.profraw` files 3. Merge profiles with `llvm-profdata` and rebuild with `-Cprofile-use` For non-abi3 PyO3 builds, each interpreter gets its own instrument-train-optimize cycle via `build_single_pyo3_wheel()`, since different Python versions produce different compiled code through PyO3's build-time configuration. For abi3/cffi/uniffi/bin builds, a single PGO pass suffices. Key components: - `PgoContext` in `src/pgo.rs`: manages temp dirs, `llvm-profdata` resolution, venv creation, dependency installation, and profile merging - `PgoPhase` enum injected into `BuildContext.pgo_phase`, read in `compile.rs` to add rustc flags - `clone_for_pgo()` helper to reduce context-setup boilerplate - `build_single_pyo3_wheel()` extracted from `build_pyo3_wheels()` to share the compile-audit-write pipeline with the PGO path
Use `uv venv` and `uv pip install` when uv is available, falling back to `python -m venv` and `python -m pip` otherwise. Detection reuses the existing `find_uv_python`/`find_uv_bin` helpers from the develop module. Extracted a `pip_install()` helper on `PgoContext` to avoid duplicating the uv-vs-pip dispatch across install calls.
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a first-class Profile-Guided Optimization (PGO) build mode to maturin, driven by a pgo-command configured in pyproject.toml, and wires it into the wheel build pipeline.
Changes:
- Introduce
--pgoCLI flag (build + publish) and plumb it intoBuildContext. - Add
[tool.maturin] pgo-commandconfig and implement a three-phase PGO build (instrument → train → optimize). - Inject rustc PGO flags during compilation and refactor PyO3 wheel building to support per-interpreter PGO cycles.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/pyproject_toml.rs | Adds pgo-command to [tool.maturin] and exposes it via an accessor. |
| src/pgo.rs | New module implementing PGO orchestration (venv creation, workload execution, profile merge). |
| src/main.rs | Adds --pgo flag to build/publish commands and passes it into the build context builder. |
| src/lib.rs | Registers the new pgo module. |
| src/compile.rs | Injects -Cprofile-generate / -Cprofile-use rustflags based on the active PGO phase. |
| src/build_context/wheels.rs | Factors out a reusable “build a single PyO3 wheel” helper for PGO per-interpreter cycles. |
| src/build_context/mod.rs | Routes wheel building through PGO pipeline when enabled; implements single-pass + per-interpreter flows. |
| src/build_context/builder.rs | Adds builder flag for PGO and enforces presence of pgo-command when enabled. |
You can also share your feedback on Copilot code review. Take the survey.
messense
added a commit
to messense/maturin
that referenced
this pull request
Mar 14, 2026
1. Set `current_dir` to the project root when running the instrumentation command, so relative paths (e.g. `pytest tests/`) resolve correctly even when maturin is invoked from elsewhere. 2. Propagate `read_dir` entry errors in `merge_profiles()` instead of silently ignoring them, avoiding a misleading 'no .profraw files' error when files exist but can't be read. 3. In single-pass PGO, restrict Phase 1 to a single interpreter so we don't wastefully compile multiple instrumented wheels when only the first is used for training. 4. Reject empty/whitespace-only `pgo-command` at builder validation time rather than failing at runtime in `run_instrumentation`. 5. Add unit tests for `pgo-command` TOML deserialization (present and absent cases).
1. Set `current_dir` to the project root when running the instrumentation command, so relative paths (e.g. `pytest tests/`) resolve correctly even when maturin is invoked from elsewhere. 2. Propagate `read_dir` entry errors in `merge_profiles()` instead of silently ignoring them, avoiding a misleading 'no .profraw files' error when files exist but can't be read. 3. In single-pass PGO, restrict Phase 1 to a single interpreter so we don't wastefully compile multiple instrumented wheels when only the first is used for training. 4. Reject empty/whitespace-only `pgo-command` at builder validation time rather than failing at runtime in `run_instrumentation`. 5. Add unit tests for `pgo-command` TOML deserialization (present and absent cases).
2b9633f to
2a0caac
Compare
PyPy emits debug output to stdout when importing pyo3 modules, causing
check_installed to fail because the entire stdout didn't match 'SUCCESS'.
Check only the last line instead, which is where print('SUCCESS') lands.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add
--pgoCLI flag andpgo-commandconfig in[tool.maturin]to enable PGO builds via a three-phase pipeline:-Cprofile-generate.profrawfilesllvm-profdataand rebuild with-Cprofile-useFor non-abi3 PyO3 builds, each interpreter gets its own instrument-train-optimize cycle via
build_single_pyo3_wheel(), since different Python versions produce different compiled code through PyO3's build-time configuration. For abi3/cffi/uniffi/bin builds, a single PGO pass suffices.Key components:
PgoContextinsrc/pgo.rs: manages temp dirs,llvm-profdataresolution, venv creation, dependency installation, and profile mergingPgoPhaseenum injected intoBuildContext.pgo_phase, read incompile.rsto add rustc flagsclone_for_pgo()helper to reduce context-setup boilerplatebuild_single_pyo3_wheel()extracted frombuild_pyo3_wheels()to share the compile-audit-write pipeline with the PGO pathCloses #1840