Skip to content

Commit dff01db

Browse files
authored
feat: support --pgo for maturin develop (#3270)
1 parent f7c38a0 commit dff01db

4 files changed

Lines changed: 32 additions & 0 deletions

File tree

‎guide/src/local_development.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ Options:
2020
2121
[possible values: pyo3, pyo3-ffi, cffi, uniffi, bin]
2222
23+
--pgo
24+
Build with Profile-Guided Optimization (PGO).
25+
26+
Requires `pgo-command` to be set in `[tool.maturin]` in pyproject.toml. This performs a three-phase build: instrumented build, profile training, and optimized rebuild.
27+
28+
Implies `--release` unless `--profile` is specified.
29+
2330
--strip
2431
Strip the library for minimum file size
2532

‎src/develop/mod.rs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ pub struct DevelopOptions {
3939
/// Pass --release to cargo
4040
#[arg(short = 'r', long, help_heading = heading::COMPILATION_OPTIONS, conflicts_with = "profile")]
4141
pub release: bool,
42+
/// Build with Profile-Guided Optimization (PGO).
43+
///
44+
/// Requires `pgo-command` to be set in `[tool.maturin]` in pyproject.toml.
45+
/// This performs a three-phase build: instrumented build, profile training,
46+
/// and optimized rebuild.
47+
///
48+
/// Implies `--release` unless `--profile` is specified.
49+
#[arg(long)]
50+
pub pgo: bool,
4251
/// Strip the library for minimum file size
4352
#[arg(long)]
4453
pub strip: bool,
@@ -324,6 +333,7 @@ pub fn develop(develop_options: DevelopOptions, venv_dir: &Path) -> Result<()> {
324333
let DevelopOptions {
325334
bindings,
326335
release,
336+
pgo,
327337
strip,
328338
extras,
329339
group,
@@ -340,6 +350,11 @@ pub fn develop(develop_options: DevelopOptions, venv_dir: &Path) -> Result<()> {
340350
if release {
341351
cargo_options.profile = Some("release".to_string());
342352
}
353+
// PGO-optimizing a debug build makes no sense, so `--pgo` implies the
354+
// release profile unless another profile was requested explicitly
355+
if pgo && cargo_options.profile.is_none() {
356+
cargo_options.profile = Some("release".to_string());
357+
}
343358

344359
let mut target_triple = cargo_options.target.clone();
345360
let target = Target::from_target_triple(cargo_options.target.as_ref())?;
@@ -385,6 +400,7 @@ pub fn develop(develop_options: DevelopOptions, venv_dir: &Path) -> Result<()> {
385400
.into_build_context()
386401
.strip(if strip { Some(true) } else { None })
387402
.editable(true)
403+
.pgo(pgo)
388404
.build()?;
389405

390406
// Ensure that version information is present, https://github.com/PyO3/maturin/issues/2416

‎tests/cmd/develop.stdout‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ Options:
1212

1313
[possible values: pyo3, pyo3-ffi, cffi, uniffi, bin]
1414

15+
--pgo
16+
Build with Profile-Guided Optimization (PGO).
17+
18+
Requires `pgo-command` to be set in `[tool.maturin]` in pyproject.toml. This performs a
19+
three-phase build: instrumented build, profile training, and optimized rebuild.
20+
21+
Implies `--release` unless `--profile` is specified.
22+
1523
--strip
1624
Strip the library for minimum file size
1725

‎tests/common/develop.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ pub fn test_develop(case: &DevelopCase<'_>) -> Result<()> {
105105
let develop_options = DevelopOptions {
106106
bindings: case.bindings.map(|binding| binding.to_owned()),
107107
release: false,
108+
pgo: false,
108109
strip: false,
109110
extras: Vec::new(),
110111
group: Vec::new(),

0 commit comments

Comments
 (0)