Skip to content

Stabilize optimize attribute - #157273

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
veluca93:optimize-attribte
Oct 8, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
veluca93:optimize-attribte

Conversation

@veluca93

@veluca93 veluca93 commented Jun 1, 2026 •

Copy link
Copy Markdown
Contributor

View all comments
FCP proposal and checkboxes

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

// 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

RFC history

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

Nightly uses

The standard library itself uses this attribute in a few places

Implementation

Major part

Coverage

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.

@rustbot

rustbot commented Jun 1, 2026

Copy link
Copy Markdown
Collaborator

rust-analyzer is developed in its own repository. If possible, consider making this change to rust-lang/rust-analyzer instead.

cc @rust-lang/rust-analyzer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-rust-analyzer Relevant to the rust-analyzer team, which will review and decide on the PR/issue. labels Jun 1, 2026
@rustbot

rustbot commented Jun 1, 2026

Copy link
Copy Markdown
Collaborator

r? @JohnTitor

rustbot has assigned @JohnTitor.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 17 candidates

@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@veluca93
veluca93 force-pushed the optimize-attribte branch from deb33c2 to 1de7123 Compare June 1, 2026 20:28
@rustbot

This comment has been minimized.

@veluca93
veluca93 force-pushed the optimize-attribte branch from 1de7123 to 56db805 Compare June 1, 2026 20:30
@veluca93 veluca93 changed the title stabilize optimize attribute (size, speed, and none) stabilize optimize attribute Jun 1, 2026
@tgross35

tgross35 commented Jun 1, 2026

Copy link
Copy Markdown
Member

Given the interactions here, I expect this will need a lang+compiler FCP

@rustbot label +I-lang-nominated +I-compiler-nominated

See some recent discussion about this feature on Zulip #t-lang > status of #[optimize] attribute

@rustbot rustbot added I-compiler-nominated Nominated for discussion during a compiler team meeting. I-lang-nominated Nominated for discussion during a lang team meeting. labels Jun 1, 2026
@tgross35 tgross35 added needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. T-lang Relevant to the language team and removed T-rust-analyzer Relevant to the rust-analyzer team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 1, 2026
@rust-log-analyzer

This comment has been minimized.

@JohnTitor JohnTitor added S-blocked Status: Blocked on something else such as an RFC or other implementation work. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 1, 2026
@JohnTitor

Copy link
Copy Markdown
Member

Marking as S-blocked given the current status.

@veluca93
veluca93 force-pushed the optimize-attribte branch from 56db805 to 680ec3c Compare June 1, 2026 22:03
@rustbot

rustbot commented Jun 1, 2026

Copy link
Copy Markdown
Collaborator

These commits modify tests/rustdoc-json.
rustdoc-json is a public (but unstable) interface.

Please ensure that if you've changed the output:

  • It's intentional.
  • The FORMAT_VERSION in src/librustdoc-json-types is bumped if necessary.

cc @aDotInTheVoid, @obi1kenobi

@rustbot rustbot added the A-rustdoc-json Area: Rustdoc JSON backend label Jun 1, 2026
@obi1kenobi

Copy link
Copy Markdown
Member

With my cargo-semver-checks hat on: do we expect this attribute to have SemVer implications?

For example, can applying or removing this attribute on an item influence the contexts in which that item may be used without compile errors or other changes that would be considered public-API-breaking?

@veluca93

veluca93 commented Jun 1, 2026

Copy link
Copy Markdown
Contributor Author

With my cargo-semver-checks hat on: do we expect this attribute to have SemVer implications?

For example, can applying or removing this attribute on an item influence the contexts in which that item may be used without compile errors or other changes that would be considered public-API-breaking?

I don't believe this is possible - the attribute should only influence how the compiler optimizes functions, and compiler optimizations should not have any visible effects.

Comment thread tests/ui/feature-gates/feature-gate-optimize_attribute.stderr Outdated

@jieyouxu jieyouxu Jun 2, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Discussion: hm, I wonder if this attribute deserves an entry in the rustc book (a bit like lint levels), because while sure there's the Reference PR, but this is more like a compiler knob?

View changes since the review

@theemathas

Copy link
Copy Markdown
Contributor

See also #138272 for another example of optimize(none) functions not being treated as opaque for optimizations.

@nikic

nikic commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

FYI LLVM 23 also has a noipa attribute. The combination of optnone + noipa should also disable all inter-procedural analysis and optimization. We may want to make optimize(none) to that.

@RalfJung

RalfJung commented Sep 6, 2026

Copy link
Copy Markdown
Member

#160524 resolves the implementation concerns here from my side -- on the MIR-opt side, optimize(none) now properly locally overwrites the opt level. (The one exception is the inliner pass which has some odd logic but cleaning that up is not a blocker for this.)

@rustbot

This comment has been minimized.

@veluca93

veluca93 commented Sep 6, 2026

Copy link
Copy Markdown
Contributor Author

I rebased the stabilization PR and updated the reference PR to include the "equivalent to -Copt-level=0" behaviour for optimize(none).
Is there anything else left to do?

@rust-bors

This comment has been minimized.

@theemathas

Copy link
Copy Markdown
Contributor

@TaKO8Ki, @jdonszelmann, @nikomatsakis, @scottmcm Your checkboxes are remaining at #157273 (comment)

This commit stabilizes the `#[optimize]` attribute, which allows for more fine-grained control
of optimization levels.
@rustbot

rustbot commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main 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.

@theemathas

theemathas commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

@scottmcm @tmandry Could you resolve your concerns in the FCP?

@theemathas

Copy link
Copy Markdown
Contributor

@veluca93 Does this PR have tests on: whether applying the attribute to a function affects closures defined within that function? And does the reference PR specify this?

@veluca93

Copy link
Copy Markdown
Contributor Author

@veluca93 Does this PR have tests on: whether applying the attribute to a function affects closures defined within that function? And does the reference PR specify this?

Well, not this PR, it was merged in a previous one :-) The test is in tests/codegen-llvm/optimize-closures-inheritance.rs.

As for the reference PR: now it does ;)

@tmandry

tmandry commented Sep 15, 2026

Copy link
Copy Markdown
Member

@rfcbot resolve should-apply-to-closures

@veluca93

Copy link
Copy Markdown
Contributor Author

@scottmcm friendly ping :-)

@scottmcm

Copy link
Copy Markdown
Member

@rfcbot resolve make-optimize-none-be-c-opt-level-0

@rust-rfcbot

Copy link
Copy Markdown
Collaborator

🔔 This is now entering its final comment period, as per the review above. 🔔

@RalfJung

RalfJung commented Oct 2, 2026 •

Copy link
Copy Markdown
Member

Is this also covered by wherever the docs will go for #[optimize]?

If globally no optimisation is done, #[optimize] has no effect.

// If globally no optimisation is done, #[optimize] has no effect.

@veluca93

veluca93 commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

Is this also covered by wherever the docs will go for #[optimize]?

If globally no optimisation is done, #[optimize] has no effect.

// If globally no optimisation is done, #[optimize] has no effect.

It was saying the wrong thing, now it should say the right thing :-) (rust-lang/reference#2278)

@rust-rfcbot

Copy link
Copy Markdown
Collaborator

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

@wesleywiser

Copy link
Copy Markdown
Member

Thanks @veluca93 for pushing this over the finish line!

@bors r+ rollup

@rust-bors

rust-bors Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 5e2ccb2 has been approved by wesleywiser

It is now in the queue for this repository.

@rust-timer

Copy link
Copy Markdown
Collaborator

Note

This PR was benchmarked as part of triage of its containing rollup: triage URL.

Finished benchmarking commit (aab12df): comparison URL.

Overall result: ✅ improvements - no action needed

@rustbot label: -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.1% [-0.3%, -0.1%] 4
Improvements ✅
(secondary)
-0.2% [-0.5%, -0.1%] 15
All ❌✅ (primary) -0.1% [-0.3%, -0.1%] 4

Max RSS (memory usage)

This perf run didn't have relevant results for this metric.

Cycles

This perf run didn't have relevant results for this metric.

Binary size

This perf run didn't have relevant results for this metric.

Artifact size: 408.59 MiB -> 408.65 MiB (0.01%)

@JonathanBrouwer

Copy link
Copy Markdown
Member

Probably noise, all PRs in the rollup seem to have similar results

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

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) A-rustdoc-json Area: Rustdoc JSON backend disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. has-reference-pr This language change has an approved Reference PR. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team to-announce Announce this issue on triage meeting

Projects

None yet

Development

Successfully merging this pull request may close these issues.