Skip to content

Stabilize #[optimize] attribute#2278

Draft
veluca93 wants to merge 1 commit into
rust-lang:masterfrom
veluca93:optimize-attribute
Draft

Stabilize #[optimize] attribute#2278
veluca93 wants to merge 1 commit into
rust-lang:masterfrom
veluca93:optimize-attribute

Conversation

@veluca93
Copy link
Copy Markdown
Contributor

@veluca93 veluca93 commented Jun 1, 2026

No description provided.

@ehuss ehuss added the S-waiting-on-stabilization Waiting for a stabilization PR to be merged in the main Rust repository label Jun 1, 2026
@veluca93 veluca93 force-pushed the optimize-attribute branch from 6b8de48 to 0765805 Compare June 1, 2026 19:54
veluca93 added a commit to veluca93/rust that referenced this pull request Jun 1, 2026
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.
@veluca93 veluca93 force-pushed the optimize-attribute branch from 0765805 to 59b5d8d Compare June 2, 2026 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-stabilization Waiting for a stabilization PR to be merged in the main Rust repository

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants