-
-
Notifications
You must be signed in to change notification settings - Fork 3k
feat(hints): support minimum optimization levels #17368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -109,6 +109,7 @@ Each new feature described below should explain how to use it. | |
| * `Cargo.toml` extensions | ||
| * [Profile `rustflags` option](#profile-rustflags-option) --- Passed directly to rustc. | ||
| * [Profile `hint-mostly-unused` option](#profile-hint-mostly-unused-option) --- Hint that a dependency is mostly unused, to optimize compilation time. | ||
| * [Package `min-opt-level` hint](#package-min-opt-level-hint) --- Request a numeric optimization floor for one package. | ||
| * [codegen-backend](#codegen-backend) --- Select the codegen backend used by rustc. | ||
| * [per-package-target](#per-package-target) --- Sets the `--target` to use for each individual package. | ||
| * [artifact dependencies](#artifact-dependencies) --- Allow build artifacts to be included into other build artifacts and build them for different targets. | ||
|
|
@@ -923,6 +924,54 @@ This will cause the crate to default to hint-mostly-unused, unless overridden | |
| via `profile`, which takes precedence, and which can only be specified in the | ||
| top-level crate being built. | ||
|
|
||
| ## Package `min-opt-level` hint | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not just looking for how well it is explain but find it helpful to literally have the docs that will go into any guide or reference section written out in the Unstable chapter with asides saying where they will go. I generally ask for this to be written in the RFC but seemed to have overlooked it in this case. For example, will the docs live under https://doc.rust-lang.org/cargo/reference/manifest.html?highlight=hint#the-hints-section ? What will the heading be? How do we want to organize these? |
||
| * Tracking Issue: [#17334](https://github.com/rust-lang/cargo/issues/17334) | ||
| * RFC: [#3924](https://github.com/rust-lang/rfcs/pull/3924) | ||
|
|
||
| The `min-opt-level` hint lets a package request a numeric optimization floor | ||
| for itself: | ||
|
|
||
| ```toml | ||
| # In example-dependency's Cargo.toml | ||
| [hints] | ||
| min-opt-level = 2 | ||
| ``` | ||
|
|
||
| To enable this feature, pass `-Zhint-min-opt-level`. Without the flag, Cargo | ||
| warns and ignores a positive hint. Older versions of Cargo may instead report | ||
| an unused manifest key, but specifying the hint does not change the package's | ||
| MSRV. | ||
|
|
||
| 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 marked this conversation as resolved.
|
||
| lower numeric [`opt-level`](profiles.md#opt-level), Cargo raises it to the | ||
| hinted minimum for that package. A higher numeric level remains unchanged. The | ||
| hint also applies to Cargo's built-in `opt-level = 0` default for build | ||
| dependencies. The size optimization levels `"s"` and `"z"` override a numeric | ||
| hint. | ||
|
|
||
| Explicit [package overrides](profiles.md#overrides) from the top-level package, | ||
| including `[profile.dev.package."*"]`, take precedence over a hint, as do | ||
| explicit build overrides. A lower numeric `opt-level` in the general profile | ||
| cannot reduce the hinted floor. For example: | ||
|
|
||
| ```toml | ||
| # In the top-level package's Cargo.toml | ||
|
|
||
| # Does not affect example-dependency: the hint keeps it at opt-level 2. | ||
| [profile.dev] | ||
| opt-level = 0 | ||
|
|
||
| # Takes precedence over the hint, forcing example-dependency back to no | ||
| # optimization when debugging it. | ||
| [profile.dev.package.example-dependency] | ||
| opt-level = 0 | ||
| ``` | ||
|
|
||
| A hint only affects the package that declares it, not its dependencies. Use | ||
| this hint when optimization either makes a typical full build faster or when | ||
| the package is many times slower without it. | ||
|
|
||
| ## rustdoc-map | ||
| * Tracking Issue: [#8296](https://github.com/rust-lang/cargo/issues/8296) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| use crate::CargoResult; | ||
| use crate::compiler::BuildContext; | ||
| use crate::workspace::profiles::{MinOptLevelHintError, parse_min_opt_level_hint}; | ||
|
|
||
| /// Emits diagnostics for `hints.min-opt-level` once per package selected for compilation. | ||
| #[tracing::instrument(skip_all)] | ||
| pub(crate) fn diagnose(bcx: &BuildContext<'_, '_>) -> CargoResult<()> { | ||
| let gctx = bcx.gctx; | ||
| let mut packages = bcx | ||
| .unit_graph | ||
| .keys() | ||
| .filter(|unit| !unit.skip_non_compile_time_dep && unit.show_warnings(gctx)) | ||
| .map(|unit| unit.pkg.clone()) | ||
| .collect::<Vec<_>>(); | ||
| packages.sort_by_key(|pkg| pkg.package_id()); | ||
| packages.dedup_by_key(|pkg| pkg.package_id()); | ||
|
|
||
| for pkg in packages { | ||
| let warn = |message: &str| { | ||
| gctx.shell() | ||
| .warn(format!("{}@{}: {message}", pkg.name(), pkg.version())) | ||
| }; | ||
| let min_opt_level = match parse_min_opt_level_hint( | ||
| pkg.hints().and_then(|hints| hints.min_opt_level.as_ref()), | ||
| ) { | ||
| Ok(level) => level, | ||
| Err(MinOptLevelHintError::OutOfRange(level)) => { | ||
| 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)) => { | ||
| warn(&format!( | ||
| "ignoring unsupported value type ({value_type}) for 'hints.min-opt-level', which expects an integer" | ||
| ))?; | ||
| None | ||
| } | ||
| }; | ||
|
Comment on lines
+19
to
+39
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we're keeping these long term, let's render them "correctly" using annotate-snippets |
||
|
|
||
| if matches!(min_opt_level, Some(1..=3)) && !gctx.cli_unstable().hint_min_opt_level { | ||
| warn("ignoring 'hints.min-opt-level', pass `-Zhint-min-opt-level` to enable it")?; | ||
| } | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.