Skip to content

feat(hints): support minimum optimization levels - #17368

Open
0xPoe wants to merge 3 commits into
rust-lang:masterfrom
0xPoe:hint-min-opt-level
Open

feat(hints): support minimum optimization levels#17368
0xPoe wants to merge 3 commits into
rust-lang:masterfrom
0xPoe:hint-min-opt-level

Conversation

@0xPoe

@0xPoe 0xPoe commented Aug 16, 2026

Copy link
Copy Markdown
Member

View all comments

What does this PR try to resolve?

ref #17334

This PR would allow Rust library to provide a simple hint about the minimum opt-level to build them with via the hint.min-opt-level setting in the manifest.

How to test and review this PR?

Check the unit tests and review it commit by commit.

r?@ghost

@0xPoe 0xPoe changed the title feat(hints): support minimum optimization levels WIP: feat(hints): support minimum optimization levels Aug 16, 2026
@0xPoe 0xPoe changed the title WIP: feat(hints): support minimum optimization levels WIP(Not Ready For Review): feat(hints): support minimum optimization levels Aug 16, 2026
@rustbot rustbot added A-build-execution Area: anything dealing with executing the compiler A-cargo-targets Area: selection and definition of targets (lib, bins, examples, tests, benches) A-documenting-cargo-itself Area: Cargo's documentation A-manifest Area: Cargo.toml issues A-profiles Area: profiles A-unstable Area: nightly unstable support labels Aug 18, 2026
@0xPoe
0xPoe force-pushed the hint-min-opt-level branch 7 times, most recently from b532a3f to 2b19c48 Compare August 21, 2026 07:18

@0xPoe 0xPoe left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

🔢 Self-check (PR reviewed by myself and ready for feedback)

  • Code compiles successfully

  • Unit tests added

  • No AI-generated elegant nonsense in PR.

  • Comments added where necessary

  • PR title and description updated

  • Documentation updated

  • PR size is reasonable

r?@joshtriplett

View changes since this review

Comment thread crates/cargo-util-schemas/Cargo.toml
Comment thread crates/cargo-util-schemas/manifest.schema.json
Comment thread src/workspace/profiles.rs
@0xPoe 0xPoe changed the title WIP(Not Ready For Review): feat(hints): support minimum optimization levels feat(hints): support minimum optimization levels Aug 21, 2026
@0xPoe
0xPoe marked this pull request as ready for review August 21, 2026 07:20
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 21, 2026
via `profile`, which takes precedence, and which can only be specified in the
top-level crate being built.

## Package `min-opt-level` hint

@epage epage Aug 21, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you have a subsection that is written as-if it was the end-user documentation?

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Could you give an example of the kind of end-user documentation you expect? What should it include? I thought the current document already covered the basic usage and examples.

Comment on lines +949 to +950
The hint accepts the numeric optimization levels 0, 1, 2, and 3. Other values
produce a warning and are ignored. If the selected [profile](profiles.md) has a

@epage epage Aug 21, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why do they produce a warning instead of error?

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

If we proceed with errors, does that mean that introducing a new optimization level in the future, which is accepted by the new Cargo, would cause an error with older versions of Cargo? Would this be considered a backward compatibility issue? But I guess introducing a new optimization level might be impossible, especially a numeric optimization level?

Comment thread src/compiler/mod.rs Outdated
Comment on lines +1479 to +1499
let pkg_hint_min_opt_level = match parse_min_opt_level_hint(hints.min_opt_level.as_ref()) {
Ok(level) => level,
Err(MinOptLevelHintError::OutOfRange(level)) => {
unit_capped_warn(&format!(
"ignoring unsupported value ({level}) for 'hints.min-opt-level', which only supports integers from 0 to 3"
))?;
None
}
Err(MinOptLevelHintError::WrongType(value_type)) => {
unit_capped_warn(&format!(
"ignoring unsupported value type ({value_type}) for 'hints.min-opt-level', which expects an integer"
))?;
None
}
};
if matches!(pkg_hint_min_opt_level, Some(1..=3)) && !bcx.gctx.cli_unstable().hint_min_opt_level
{
unit_capped_warn(
"ignoring 'hints.min-opt-level', pass `-Zhint-min-opt-level` to enable it",
)?;
}

@epage epage Aug 21, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why is this check placed here?

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I simply followed the mostly-unused placement and tried to group them together. However, after reconsidering, this may not make sense because this case differs from it: this hint does not directly change the cmd here. The per-compilation-unit check also does not apply to the min-opt-level hint. I will try to find the right place to perform this check.

Comment thread src/workspace/profiles.rs
Comment on lines +304 to +309
let mut profile = maker.get_profile(
Some(pkg_id),
is_member,
unit_for.is_for_host(),
min_opt_level,
);

@epage epage Aug 21, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why do we pass it into get_profile, rather than overriding it here?

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Because the hint takes precedence within ProfileMaker::get_profile, applying it afterward would override explicit settings such as:

 [profile.dev.package.foo]                                                                          
 opt-level = 0                                                                                      

Comment thread tests/testsuite/hints.rs
Comment thread tests/testsuite/hints.rs
Comment thread tests/testsuite/hints.rs
@rustbot

This comment has been minimized.

@0xPoe
0xPoe force-pushed the hint-min-opt-level branch 2 times, most recently from 14ab0ce to 415812c Compare August 26, 2026 07:53
@rustbot

This comment has been minimized.

@0xPoe
0xPoe force-pushed the hint-min-opt-level branch from 415812c to aaf6533 Compare August 26, 2026 07:56
@0xPoe
0xPoe marked this pull request as draft August 26, 2026 07:59
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 26, 2026
@0xPoe 0xPoe added the S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author. label Aug 26, 2026
@0xPoe
0xPoe force-pushed the hint-min-opt-level branch 3 times, most recently from 18f313e to ff1b273 Compare August 27, 2026 06:56
@0xPoe
0xPoe marked this pull request as ready for review August 27, 2026 07:09
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 27, 2026
@0xPoe
0xPoe requested a review from epage August 27, 2026 07:29
@rustbot

This comment has been minimized.

0xPoe added 3 commits August 30, 2026 14:22
Record the current ignored-hint behavior across
the RFC precedence, scope, validation, and boundary cases.

Signed-off-by: 0xPoe <poe.liu@pm.me>
RFC 3924 lets a package request a numeric optimization floor without
overriding an application's profile choices. Apply the hint after
profile and built-in host defaults but before explicit package
and build overrides, while leaving the unordered size levels unchanged.

Signed-off-by: 0xPoe <poe.liu@pm.me>
Document the unstable gate, accepted range, profile precedence,
non-recursive
scope, and the narrow cases where packages should request optimization.
Include an explicit debugging override so top-level users retain
control.

Signed-off-by: 0xPoe <poe.liu@pm.me>
@0xPoe
0xPoe force-pushed the hint-min-opt-level branch from ff1b273 to b9ea4de Compare August 30, 2026 12:29
@rustbot

rustbot commented Aug 30, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-build-execution Area: anything dealing with executing the compiler A-cargo-targets Area: selection and definition of targets (lib, bins, examples, tests, benches) A-documenting-cargo-itself Area: Cargo's documentation A-manifest Area: Cargo.toml issues A-profiles Area: profiles A-unstable Area: nightly unstable support S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants