Skip to content

feat: add Profile-Guided Optimization (PGO) support#3085

Merged
messense merged 4 commits intoPyO3:mainfrom
messense:feature/pgo-support
Mar 14, 2026
Merged

feat: add Profile-Guided Optimization (PGO) support#3085
messense merged 4 commits intoPyO3:mainfrom
messense:feature/pgo-support

Conversation

@messense
Copy link
Member

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

Closes #1840

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.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 --pgo CLI flag (build + publish) and plumb it into BuildContext.
  • Add [tool.maturin] pgo-command config 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).
@messense messense force-pushed the feature/pgo-support branch from 2b9633f to 2a0caac Compare March 14, 2026 02:30
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.
@messense messense marked this pull request as ready for review March 14, 2026 03:20
@messense messense merged commit e2ee46d into PyO3:main Mar 14, 2026
44 of 45 checks passed
@messense messense deleted the feature/pgo-support branch March 14, 2026 03:20
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.

Supporting PGO builds from maturin

2 participants