Repository navigation
Conversation
6b8de48 to
0765805
Compare
This commit stabilizes the `#[optimize]` attribute, which allows for more fine-grained control of optimization levels. Tracking issue: rust-lang#54882 Reference PRs: rust-lang/reference#2278 cc @rust-lang/lang @rust-lang/lang-advisors @rust-lang/t-compiler > Describe each behavior being stabilized and give a short example of code that will now be accepted. ```rust // Instructs the optimization pipeline to prioritize smaller code size over speed. pub fn binary_size_sensitive() { // ... } // Instructs the optimization pipeline to prioritize execution speed over code size. pub fn performance_critical() { // ... } // Disables optimizations entirely for this item. pub fn debug_only() { // ... } ``` We might want to allow specifying a per-function optimization level (i.e. `#[optimize(level = 3)]`). To my understanding, backend support for this is not quite there yet (and this - or other - extensions have consequently not been implemented yet). Applying the attribute on a module level is also not implemented yet. - Description of the new attribute: rust-lang/reference#2278 - https://rust-lang.github.io/rfcs/2412-optimize-attr.html - Should we also implement optimize(always)? optimize(level=x)? Not yet. - Should there be any way to specify what global optimization for speed level is used in conjunction with the optimization for speed option (e.g. -Copt-level=s3 could be equivalent to -Copt-level=3 and #[optimize(size)] on the crate item). Not yet. The `optimize(none)` variant was added. The variants above were discussed here: rust-lang#54882 (comment) - `#[optimize(none)]` implies `#[inline(never)]` to be effective (rust-lang#136329) - As with all optimization-controlling flags, the effects on speed and binary size can be hard to predict: rust-lang#54882 (comment) - Applying the attribute to incompatible items emits an error: rust-lang#128458 The standard library itself uses this attribute in a few places - Wiring it up to the llvm codegen backend: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/compiler/rustc_codegen_llvm/src/attributes.rs#L416 - Catching usage on incompatible items: rust-lang#128458 - Basic tests: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/tests/ui/attributes/optimize.rs - Testing the effect on compilation: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/tests/codegen-llvm/optimize-attr-1.rs Trivial changes to rust-analyzer to support the new attribute as an inert attribute (part of this PR). As far as the AM is concerned, this attribute doesn't exist. Most of the work was not done by me, I'm just writing the stabilization report :-) @nagisa did the initial implementation, @clubby789 implemented optimize(none) and fixed the attribute being allowed in too many places.
0765805 to
59b5d8d
Compare
There was a problem hiding this comment.
There are a few other things that need updating:
- Update the list of allowed attributes on functions in
items/functions.md. - Add to the list of builtin attributes.
a58a16d to
e9f9d99
Compare
e9f9d99 to
b499fc3
Compare
65bdfe5 to
a6519b4
Compare
…eywiser Stabilize `optimize` attribute This commit stabilizes the `#[optimize]` attribute, which allows for more fine-grained control of optimization levels. # Stabilization report ## Summary Tracking issue: rust-lang#54882 Reference PRs: rust-lang/reference#2278 cc @rust-lang/lang @rust-lang/lang-advisors @rust-lang/t-compiler ### What is stabilized ```rust // Instructs the optimization pipeline to prioritize smaller code size over speed. #[optimize(size)] pub fn binary_size_sensitive() { // ... } // Instructs the optimization pipeline to prioritize execution speed over code size. #[optimize(speed)] pub fn performance_critical() { // ... } // Disables optimizations entirely for this item. #[optimize(none)] pub fn debug_only() { // ... } ``` ### What isn't stabilized We might want to allow specifying a per-function optimization level (i.e. `#[optimize(level = 3)]`). To my understanding, backend support for this is not quite there yet (and this - or other - extensions have consequently not been implemented yet). Applying the attribute on a module level is also not implemented yet. ## Design ### Reference - Description of the new attribute: rust-lang/reference#2278 ### RFC history - https://rust-lang.github.io/rfcs/2412-optimize-attr.html ### Answers to unresolved question - Should we also implement optimize(always)? optimize(level=x)? Not yet. - Should there be any way to specify what global optimization for speed level is used in conjunction with the optimization for speed option (e.g. -Copt-level=s3 could be equivalent to -Copt-level=3 and #[optimize(size)] on the crate item). Not yet. ### Post-RFC changes The `optimize(none)` variant was added. The variants above were discussed here: rust-lang#54882 (comment) ### Key points - `#[optimize(none)]` implies `#[inline(never)]` to be effective (rust-lang#136329) - As with all optimization-controlling flags, the effects on speed and binary size can be hard to predict: rust-lang#54882 (comment) - Applying the attribute to incompatible items emits an error: rust-lang#128458 ### Nightly uses The standard library itself uses this attribute in a few places ## Implementation ### Major part - Wiring it up to the llvm codegen backend: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/compiler/rustc_codegen_llvm/src/attributes.rs#L416 - Catching usage on incompatible items: rust-lang#128458 - Implementing `optimize(none)`: rust-lang#128657 rust-lang#136358 (includes disabling MIR opts) ### Coverage - Basic tests: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/tests/ui/attributes/optimize.rs - Testing the effect on compilation: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/tests/codegen-llvm/optimize-attr-1.rs ### Tool changes Trivial changes to rust-analyzer to support the new attribute as an inert attribute: rust-lang/rust-analyzer#22511 ## Type system, opsem ### Breaks the AM? As far as the AM is concerned, this attribute doesn't exist. ## Acknowledgments Most of the work was not done by me, I'm just writing the stabilization report :-) @nagisa did the initial implementation, @clubby789 implemented optimize(none) and fixed the attribute being allowed in too many places. <!-- TRIAGEBOT_START --> <!-- TRIAGEBOT_CONCERN-ISSUE_START --> > [!NOTE] > # Concerns (0 active) > > - ~~[are-we-happy-wiht-capability-and-scope-of-this-attribute](rust-lang#157273 (comment) resolved in [this comment](rust-lang#157273 (comment)) > - ~~[meaning-of-optimize-size](rust-lang#157273 (comment) resolved in [this comment](rust-lang#157273 (comment)) > - ~~[explicitly-decide-on-effect-on-nested-functions](rust-lang#157273 (comment) resolved in [this comment](rust-lang#157273 (comment)) > > *Managed by `@rustbot`—see [help](https://forge.rust-lang.org/triagebot/concern.html) for details.* <!-- TRIAGEBOT_CONCERN-ISSUE_END --> <!-- TRIAGEBOT_END -->
…eywiser Stabilize `optimize` attribute This commit stabilizes the `#[optimize]` attribute, which allows for more fine-grained control of optimization levels. # Stabilization report ## Summary Tracking issue: rust-lang#54882 Reference PRs: rust-lang/reference#2278 cc @rust-lang/lang @rust-lang/lang-advisors @rust-lang/t-compiler ### What is stabilized ```rust // Instructs the optimization pipeline to prioritize smaller code size over speed. #[optimize(size)] pub fn binary_size_sensitive() { // ... } // Instructs the optimization pipeline to prioritize execution speed over code size. #[optimize(speed)] pub fn performance_critical() { // ... } // Disables optimizations entirely for this item. #[optimize(none)] pub fn debug_only() { // ... } ``` ### What isn't stabilized We might want to allow specifying a per-function optimization level (i.e. `#[optimize(level = 3)]`). To my understanding, backend support for this is not quite there yet (and this - or other - extensions have consequently not been implemented yet). Applying the attribute on a module level is also not implemented yet. ## Design ### Reference - Description of the new attribute: rust-lang/reference#2278 ### RFC history - https://rust-lang.github.io/rfcs/2412-optimize-attr.html ### Answers to unresolved question - Should we also implement optimize(always)? optimize(level=x)? Not yet. - Should there be any way to specify what global optimization for speed level is used in conjunction with the optimization for speed option (e.g. -Copt-level=s3 could be equivalent to -Copt-level=3 and #[optimize(size)] on the crate item). Not yet. ### Post-RFC changes The `optimize(none)` variant was added. The variants above were discussed here: rust-lang#54882 (comment) ### Key points - `#[optimize(none)]` implies `#[inline(never)]` to be effective (rust-lang#136329) - As with all optimization-controlling flags, the effects on speed and binary size can be hard to predict: rust-lang#54882 (comment) - Applying the attribute to incompatible items emits an error: rust-lang#128458 ### Nightly uses The standard library itself uses this attribute in a few places ## Implementation ### Major part - Wiring it up to the llvm codegen backend: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/compiler/rustc_codegen_llvm/src/attributes.rs#L416 - Catching usage on incompatible items: rust-lang#128458 - Implementing `optimize(none)`: rust-lang#128657 rust-lang#136358 (includes disabling MIR opts) ### Coverage - Basic tests: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/tests/ui/attributes/optimize.rs - Testing the effect on compilation: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/tests/codegen-llvm/optimize-attr-1.rs ### Tool changes Trivial changes to rust-analyzer to support the new attribute as an inert attribute: rust-lang/rust-analyzer#22511 ## Type system, opsem ### Breaks the AM? As far as the AM is concerned, this attribute doesn't exist. ## Acknowledgments Most of the work was not done by me, I'm just writing the stabilization report :-) @nagisa did the initial implementation, @clubby789 implemented optimize(none) and fixed the attribute being allowed in too many places. <!-- TRIAGEBOT_START --> <!-- TRIAGEBOT_CONCERN-ISSUE_START --> > [!NOTE] > # Concerns (0 active) > > - ~~[are-we-happy-wiht-capability-and-scope-of-this-attribute](rust-lang#157273 (comment) resolved in [this comment](rust-lang#157273 (comment)) > - ~~[meaning-of-optimize-size](rust-lang#157273 (comment) resolved in [this comment](rust-lang#157273 (comment)) > - ~~[explicitly-decide-on-effect-on-nested-functions](rust-lang#157273 (comment) resolved in [this comment](rust-lang#157273 (comment)) > > *Managed by `@rustbot`—see [help](https://forge.rust-lang.org/triagebot/concern.html) for details.* <!-- TRIAGEBOT_CONCERN-ISSUE_END --> <!-- TRIAGEBOT_END -->
…eywiser Stabilize `optimize` attribute This commit stabilizes the `#[optimize]` attribute, which allows for more fine-grained control of optimization levels. # Stabilization report ## Summary Tracking issue: rust-lang#54882 Reference PRs: rust-lang/reference#2278 cc @rust-lang/lang @rust-lang/lang-advisors @rust-lang/t-compiler ### What is stabilized ```rust // Instructs the optimization pipeline to prioritize smaller code size over speed. #[optimize(size)] pub fn binary_size_sensitive() { // ... } // Instructs the optimization pipeline to prioritize execution speed over code size. #[optimize(speed)] pub fn performance_critical() { // ... } // Disables optimizations entirely for this item. #[optimize(none)] pub fn debug_only() { // ... } ``` ### What isn't stabilized We might want to allow specifying a per-function optimization level (i.e. `#[optimize(level = 3)]`). To my understanding, backend support for this is not quite there yet (and this - or other - extensions have consequently not been implemented yet). Applying the attribute on a module level is also not implemented yet. ## Design ### Reference - Description of the new attribute: rust-lang/reference#2278 ### RFC history - https://rust-lang.github.io/rfcs/2412-optimize-attr.html ### Answers to unresolved question - Should we also implement optimize(always)? optimize(level=x)? Not yet. - Should there be any way to specify what global optimization for speed level is used in conjunction with the optimization for speed option (e.g. -Copt-level=s3 could be equivalent to -Copt-level=3 and #[optimize(size)] on the crate item). Not yet. ### Post-RFC changes The `optimize(none)` variant was added. The variants above were discussed here: rust-lang#54882 (comment) ### Key points - `#[optimize(none)]` implies `#[inline(never)]` to be effective (rust-lang#136329) - As with all optimization-controlling flags, the effects on speed and binary size can be hard to predict: rust-lang#54882 (comment) - Applying the attribute to incompatible items emits an error: rust-lang#128458 ### Nightly uses The standard library itself uses this attribute in a few places ## Implementation ### Major part - Wiring it up to the llvm codegen backend: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/compiler/rustc_codegen_llvm/src/attributes.rs#L416 - Catching usage on incompatible items: rust-lang#128458 - Implementing `optimize(none)`: rust-lang#128657 rust-lang#136358 (includes disabling MIR opts) ### Coverage - Basic tests: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/tests/ui/attributes/optimize.rs - Testing the effect on compilation: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/tests/codegen-llvm/optimize-attr-1.rs ### Tool changes Trivial changes to rust-analyzer to support the new attribute as an inert attribute: rust-lang/rust-analyzer#22511 ## Type system, opsem ### Breaks the AM? As far as the AM is concerned, this attribute doesn't exist. ## Acknowledgments Most of the work was not done by me, I'm just writing the stabilization report :-) @nagisa did the initial implementation, @clubby789 implemented optimize(none) and fixed the attribute being allowed in too many places. <!-- TRIAGEBOT_START --> <!-- TRIAGEBOT_CONCERN-ISSUE_START --> > [!NOTE] > # Concerns (0 active) > > - ~~[are-we-happy-wiht-capability-and-scope-of-this-attribute](rust-lang#157273 (comment) resolved in [this comment](rust-lang#157273 (comment)) > - ~~[meaning-of-optimize-size](rust-lang#157273 (comment) resolved in [this comment](rust-lang#157273 (comment)) > - ~~[explicitly-decide-on-effect-on-nested-functions](rust-lang#157273 (comment) resolved in [this comment](rust-lang#157273 (comment)) > > *Managed by `@rustbot`—see [help](https://forge.rust-lang.org/triagebot/concern.html) for details.* <!-- TRIAGEBOT_CONCERN-ISSUE_END --> <!-- TRIAGEBOT_END -->
Rollup merge of #157273 - veluca93:optimize-attribte, r=wesleywiser Stabilize `optimize` attribute This commit stabilizes the `#[optimize]` attribute, which allows for more fine-grained control of optimization levels. # Stabilization report ## Summary Tracking issue: #54882 Reference PRs: rust-lang/reference#2278 cc @rust-lang/lang @rust-lang/lang-advisors @rust-lang/t-compiler ### What is stabilized ```rust // Instructs the optimization pipeline to prioritize smaller code size over speed. #[optimize(size)] pub fn binary_size_sensitive() { // ... } // Instructs the optimization pipeline to prioritize execution speed over code size. #[optimize(speed)] pub fn performance_critical() { // ... } // Disables optimizations entirely for this item. #[optimize(none)] pub fn debug_only() { // ... } ``` ### What isn't stabilized We might want to allow specifying a per-function optimization level (i.e. `#[optimize(level = 3)]`). To my understanding, backend support for this is not quite there yet (and this - or other - extensions have consequently not been implemented yet). Applying the attribute on a module level is also not implemented yet. ## Design ### Reference - Description of the new attribute: rust-lang/reference#2278 ### RFC history - https://rust-lang.github.io/rfcs/2412-optimize-attr.html ### Answers to unresolved question - Should we also implement optimize(always)? optimize(level=x)? Not yet. - Should there be any way to specify what global optimization for speed level is used in conjunction with the optimization for speed option (e.g. -Copt-level=s3 could be equivalent to -Copt-level=3 and #[optimize(size)] on the crate item). Not yet. ### Post-RFC changes The `optimize(none)` variant was added. The variants above were discussed here: #54882 (comment) ### Key points - `#[optimize(none)]` implies `#[inline(never)]` to be effective (#136329) - As with all optimization-controlling flags, the effects on speed and binary size can be hard to predict: #54882 (comment) - Applying the attribute to incompatible items emits an error: #128458 ### Nightly uses The standard library itself uses this attribute in a few places ## Implementation ### Major part - Wiring it up to the llvm codegen backend: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/compiler/rustc_codegen_llvm/src/attributes.rs#L416 - Catching usage on incompatible items: #128458 - Implementing `optimize(none)`: #128657 #136358 (includes disabling MIR opts) ### Coverage - Basic tests: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/tests/ui/attributes/optimize.rs - Testing the effect on compilation: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/tests/codegen-llvm/optimize-attr-1.rs ### Tool changes Trivial changes to rust-analyzer to support the new attribute as an inert attribute: rust-lang/rust-analyzer#22511 ## Type system, opsem ### Breaks the AM? As far as the AM is concerned, this attribute doesn't exist. ## Acknowledgments Most of the work was not done by me, I'm just writing the stabilization report :-) @nagisa did the initial implementation, @clubby789 implemented optimize(none) and fixed the attribute being allowed in too many places. <!-- TRIAGEBOT_START --> <!-- TRIAGEBOT_CONCERN-ISSUE_START --> > [!NOTE] > # Concerns (0 active) > > - ~~[are-we-happy-wiht-capability-and-scope-of-this-attribute](#157273 (comment) resolved in [this comment](#157273 (comment)) > - ~~[meaning-of-optimize-size](#157273 (comment) resolved in [this comment](#157273 (comment)) > - ~~[explicitly-decide-on-effect-on-nested-functions](#157273 (comment) resolved in [this comment](#157273 (comment)) > > *Managed by `@rustbot`—see [help](https://forge.rust-lang.org/triagebot/concern.html) for details.* <!-- TRIAGEBOT_CONCERN-ISSUE_END --> <!-- TRIAGEBOT_END -->
a6519b4 to
dc63d89
Compare
|
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. |
dc63d89 to
0686253
Compare
Stabilize `optimize` attribute This commit stabilizes the `#[optimize]` attribute, which allows for more fine-grained control of optimization levels. # Stabilization report ## Summary Tracking issue: rust-lang/rust#54882 Reference PRs: rust-lang/reference#2278 cc @rust-lang/lang @rust-lang/lang-advisors @rust-lang/t-compiler ### What is stabilized ```rust // Instructs the optimization pipeline to prioritize smaller code size over speed. #[optimize(size)] pub fn binary_size_sensitive() { // ... } // Instructs the optimization pipeline to prioritize execution speed over code size. #[optimize(speed)] pub fn performance_critical() { // ... } // Disables optimizations entirely for this item. #[optimize(none)] pub fn debug_only() { // ... } ``` ### What isn't stabilized We might want to allow specifying a per-function optimization level (i.e. `#[optimize(level = 3)]`). To my understanding, backend support for this is not quite there yet (and this - or other - extensions have consequently not been implemented yet). Applying the attribute on a module level is also not implemented yet. ## Design ### Reference - Description of the new attribute: rust-lang/reference#2278 ### RFC history - https://rust-lang.github.io/rfcs/2412-optimize-attr.html ### Answers to unresolved question - Should we also implement optimize(always)? optimize(level=x)? Not yet. - Should there be any way to specify what global optimization for speed level is used in conjunction with the optimization for speed option (e.g. -Copt-level=s3 could be equivalent to -Copt-level=3 and #[optimize(size)] on the crate item). Not yet. ### Post-RFC changes The `optimize(none)` variant was added. The variants above were discussed here: rust-lang/rust#54882 (comment) ### Key points - `#[optimize(none)]` implies `#[inline(never)]` to be effective (rust-lang/rust#136329) - As with all optimization-controlling flags, the effects on speed and binary size can be hard to predict: rust-lang/rust#54882 (comment) - Applying the attribute to incompatible items emits an error: rust-lang/rust#128458 ### Nightly uses The standard library itself uses this attribute in a few places ## Implementation ### Major part - Wiring it up to the llvm codegen backend: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/compiler/rustc_codegen_llvm/src/attributes.rs#L416 - Catching usage on incompatible items: rust-lang/rust#128458 - Implementing `optimize(none)`: rust-lang/rust#128657 rust-lang/rust#136358 (includes disabling MIR opts) ### Coverage - Basic tests: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/tests/ui/attributes/optimize.rs - Testing the effect on compilation: https://github.com/rust-lang/rust/blob/c0bb140a37c81cf59a0b40c21c9413059644e294/tests/codegen-llvm/optimize-attr-1.rs ### Tool changes Trivial changes to rust-analyzer to support the new attribute as an inert attribute: rust-lang/rust-analyzer#22511 ## Type system, opsem ### Breaks the AM? As far as the AM is concerned, this attribute doesn't exist. ## Acknowledgments Most of the work was not done by me, I'm just writing the stabilization report :-) @nagisa did the initial implementation, @clubby789 implemented optimize(none) and fixed the attribute being allowed in too many places. <!-- TRIAGEBOT_START --> <!-- TRIAGEBOT_CONCERN-ISSUE_START --> > [!NOTE] > # Concerns (0 active) > > - ~~[are-we-happy-wiht-capability-and-scope-of-this-attribute](rust-lang/rust#157273 (comment) resolved in [this comment](rust-lang/rust#157273 (comment)) > - ~~[meaning-of-optimize-size](rust-lang/rust#157273 (comment) resolved in [this comment](rust-lang/rust#157273 (comment)) > - ~~[explicitly-decide-on-effect-on-nested-functions](rust-lang/rust#157273 (comment) resolved in [this comment](rust-lang/rust#157273 (comment)) > > *Managed by `@rustbot`—see [help](https://forge.rust-lang.org/triagebot/concern.html) for details.* <!-- TRIAGEBOT_CONCERN-ISSUE_END --> <!-- TRIAGEBOT_END -->
|
ping @ehuss :-) |
|
Or I guess @tgross35 :-) |
|
Mentioning on Zulip but usually Lang handles reference reviews :) |
|
Thanks for your patience here. This needs some revisions, which I've been working on. We spent much of the lang-docs call on Tuesday on this. They're close, and I hope to have these up soon. |
#[optimize] attribute
An earlier update of this branch lost some changes that had been made in response to the review by Eric Huss. Let's restore those and also adjust the wording of the duplicates rule to follow the template.
The earlier wording, that `optimize(none)` "suggests not optimizing the function", could be read to suggest that it's asking specifically for optimizations *not* to happen rather than *not* asking for optimizations to happen. This would arguably be a tier below `-Copt-level=0`. In the lang call on 2026-07-29, the meeting consensus was that with `optimize(none)` we're not asking for optimizations to happen rather than asking for optimizations to not happen.[^1] Let's adopt this framing here. [^1]: rust-lang/rust#157273 (comment)
We need to say explicitly how the inheritance works for this attribute; we need to define the behavior for `async` blocks as well as for closures; and we need to address `const` blocks. Let's do those things.
As with `cold` and `inline`, putting the `optimize` attribute on an associated function within a trait definition only applies the effect of that attribute to the body within the trait definition, not to the bodies of function definitions within implementations of the trait. Let's say that.
It's an error to write `optimize(none)` and `inline` or `inline(always)` on the same function, though it's OK for the effect of `optimize(none)` to be inherited on a closure that is marked with `inline` or `inline(always)`. Let's make these things clear.
The `optimize` attribute suggests an optimization goal (or lack thereof); it doesn't "control" the optimization level. Let's be more clear, and let's use the Reference identifiers for the link reference definitions.
The `optimize` and `naked` attributes can't be used on the same function. Let's document that.
Describing how the `optimize` attribute has no effect when `-Copt-level=0` is set is more appropriate for a nonnormative admonition rather than in the main body of the Reference. From a language perspective, the attribute always has no effect (even if it does, as a language matter, have a certain meaning and intention).
Let's tighten the scope of the alternation within the grammar for the `optimize` attribute.
Let's add examples for `optimize` demonstrating the allowed positions and the behavior when the attribute is repeated on an item.
152d695 to
13dcb18
Compare
|
|
||
| - `#[optimize(size)]` suggests optimizing for smaller code, even at the cost of execution speed. | ||
| - `#[optimize(speed)]` suggests optimizing for faster execution, even at the cost of code size. | ||
| - `#[optimize(none)]` suggests compiling the function as though no optimization had been requested for it, whatever was requested for the crate as a whole. |
There was a problem hiding this comment.
| - `#[optimize(none)]` suggests compiling the function as though no optimization had been requested for it, whatever was requested for the crate as a whole. | |
| - `#[optimize(none)]` suggests compiling the function as though no optimization had been requested for it, disregarding what was requested for the crate as a whole. |
| ``` | ||
|
|
||
| r[attributes.codegen.optimize.closures] | ||
| Closures and [async blocks] without an `optimize` attribute of their own inherit the attribute in effect on the innermost enclosing function, closure, or async block, which may itself be one inherited rather than one written directly. The body of an [async function] inherits the attribute in effect on the function, and the body of an [async closure] inherits the attribute in effect on the closure. Items and [const blocks] within a function do not inherit the attribute in effect on the function, nor do the closures and async blocks within them. |
There was a problem hiding this comment.
Did someone check this against the implementation?
There was a problem hiding this comment.
There's an UI test about it applying to closures (and explicit code I wrote for it). Async block I guess are a side-effect of that code; I don't remember if I added a test for nested items but them not inheriting the attribute was IIRC intentional; I imagine const blocks are a consequence of that (I assume @traviscross checked the behaviour, although possibly for const blocks it would be somewhat surprising)
| > ``` | ||
|
|
||
| > [!NOTE] | ||
| > For `rustc`, the attribute has no effect when optimizations are disabled (`-C opt-level=0`). |
There was a problem hiding this comment.
Did someone check this against the implementation?
There was a problem hiding this comment.
I checked this on Godbolt when you initially raised the point in the stabilization PR, but I did not check the code directly.
There was a problem hiding this comment.
Does this mean when -Copt-level is not passed at all, or specifically when you pass -Copt-level=0? I thought some of the purpose of this attribute was to be able to enable optimizations regardless of opt level.
Stabilization: rust-lang/rust#157273