mirror of
https://github.com/allankoechke/rust.git
synced 2026-11-30 11:05:36 +00:00
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: https://github.com/rust-lang/reference/pull/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: https://github.com/rust-lang/reference/pull/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: https://github.com/rust-lang/rust/issues/54882#issuecomment-4208014721 ### Key points - `#[optimize(none)]` implies `#[inline(never)]` to be effective (https://github.com/rust-lang/rust/issues/136329) - As with all optimization-controlling flags, the effects on speed and binary size can be hard to predict: https://github.com/rust-lang/rust/issues/54882#issuecomment-2789971186 - Applying the attribute to incompatible items emits an error: https://github.com/rust-lang/rust/pull/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:c0bb140a37/compiler/rustc_codegen_llvm/src/attributes.rs (L416)- Catching usage on incompatible items: https://github.com/rust-lang/rust/pull/128458 - Implementing `optimize(none)`: https://github.com/rust-lang/rust/pull/128657 https://github.com/rust-lang/rust/pull/136358 (includes disabling MIR opts) ### Coverage - Basic tests:c0bb140a37/tests/ui/attributes/optimize.rs- Testing the effect on compilation:c0bb140a37/tests/codegen-llvm/optimize-attr-1.rs### Tool changes Trivial changes to rust-analyzer to support the new attribute as an inert attribute: https://github.com/rust-lang/rust-analyzer/pull/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](https://github.com/rust-lang/rust/pull/157273#issuecomment-4623050478)~~ resolved in [this comment](https://github.com/rust-lang/rust/pull/157273#issuecomment-4677336542) > - ~~[meaning-of-optimize-size](https://github.com/rust-lang/rust/pull/157273#issuecomment-4623062899)~~ resolved in [this comment](https://github.com/rust-lang/rust/pull/157273#issuecomment-4677336542) > - ~~[explicitly-decide-on-effect-on-nested-functions](https://github.com/rust-lang/rust/pull/157273#issuecomment-4630018199)~~ resolved in [this comment](https://github.com/rust-lang/rust/pull/157273#issuecomment-4677336542) > > *Managed by `@rustbot`—see [help](https://forge.rust-lang.org/triagebot/concern.html) for details.* <!-- TRIAGEBOT_CONCERN-ISSUE_END --> <!-- TRIAGEBOT_END -->