Stabilize optimize attribute - #157273
Conversation
|
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 |
|
r? @JohnTitor rustbot has assigned @JohnTitor. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
deb33c2 to
1de7123
Compare
This comment has been minimized.
This comment has been minimized.
1de7123 to
56db805
Compare
|
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 |
This comment has been minimized.
This comment has been minimized.
|
Marking as S-blocked given the current status. |
56db805 to
680ec3c
Compare
|
These commits modify Please ensure that if you've changed the output:
|
|
With my 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. |
There was a problem hiding this comment.
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?
|
Inheritance of this attribute was also a part of the original rfc that introduced the attribute: https://github.com/rust-lang/rfcs/blob/master/text/2412-optimize-attr.md#reference-level-explanation (4th paragraph.) |
(It was left as an unresolved question.) |
This comment has been minimized.
This comment has been minimized.
|
@rfcbot concern optimize-none-not-being-none-muddles-motivation Ralf pointed out in #128657 (comment) that the PR implementing If it's not "none" (and I guess also different from opt-level=0 or that change wouldn't have been needed?) but "well, sorta none but also optimized enough that operations on ZSTs don't make it to codegen (and maybe more things)", now I'm puzzled what the point of it is. Did people actually want |
FWIW I am significantly more interested in (And among those, I moreover care more about |
Reading back the comments on the tracking issue, it seems that people do indeed want |
3aa06ad to
3a17308
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This commit stabilizes the `#[optimize]` attribute, which allows for more fine-grained control of optimization levels.
3a17308 to
03bd1ac
Compare
|
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. |
…leywiser,scottmcm Closures inherit #[optimize] from the enclosing function by default. Tracking issue: rust-lang/rust#54882 Stabilization PR: rust-lang/rust#157273
|
@rustbot label I-lang-nominated See #157273 (comment). Nominating for team guidance on whether to ship this as |
Just for current context, that issue was actually unrelated to |
Yeah so that's the question I was about to bring up here. :) Should FWIW the same question comes up for |
|
☔ The latest upstream changes (presumably #160102) made this pull request unmergeable. Please resolve the merge conflicts by rebasing. |
|
We talked about my fuzzy concern (that I didn't want to leave open for long) in the lang triage meeting today. There was general agreement that there's no intent here to create a "less than @rfcbot concern make-optimize-none-be-c-opt-level-0 TC is going to post a separate comment with a more lang-like phrasing of how to describe this. My one above is admittedly too compiler-implementation-terminology. @rfcbot resolve optimize-none-not-being-none-muddles-motivation |
|
From a lang perspective, I think about the intent we write down in the Reference for these attributes. In that respect, I interpret If you compile a program with That is, In the meeting, there was general agreement about this interpretation. |
|
That sounds like a kind of unnecessarily complicated way of saying "behaves like (Note that there are other values for |
|
Maybe we should just spell this as |
|
FWIW, the current wording in the reference PR stays vague on purpose (something like "suggests the compiler to apply as few optimizations as possible"). Given that the semantics of |
|
"As few as possible" sounds stronger than what we say about |
|
After chatting with TC I now understand why I was so confused by his wording -- there's actually a difference in what we think we should say about I think we both would have it actually do the same thing in the compiler implementation, this is a docs-only difference. Not sure if it has to block stabilization since the docs can change without the implementation changing. ;) But personally I would find it odd to treat |
I came in with the intuition that
There are benefits to that, but currently we have consensus on the lang team for a more semantic interpretation. The benefit of the semantic approach is it's less tied to a compiler implementation and it doesn't imply a one-dimensional goal, like a number does. In the future the compiler could accept
It looks like we have consensus to ship |
Ok. How is that different from what's happening now, and what needs to change compiler-side for that to be the case? I am not familiar enough with llvm/mir optimizations to be able to tell. |
That sounds like a direction to me. We already have You have to pick some area of the tradeoff space, you cannot refuse to make a choice. In that sense
For MIR passes, I have no idea what the attribute does on the LLVM side or how we even forward it to LLVM. Also, I just realized an additional wrinkle: IMO, |
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
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
Not yet.
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 (#[optimize(none)] should imply #[inline(never)] #136329)#[optimize]is applied to an incompatible item #128458Nightly uses
The standard library itself uses this attribute in a few places
Implementation
Major part
rust/compiler/rustc_codegen_llvm/src/attributes.rs
Line 416 in c0bb140
#[optimize]is applied to an incompatible item #128458optimize(none): Add#[optimize(none)]#128657#[optimize(none)]implies#[inline(never)]#136358 (includes disabling MIR opts)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.
Note
Concerns (0 active)
are-we-happy-wiht-capability-and-scope-of-this-attributeresolved in this commentmeaning-of-optimize-sizeresolved in this commentexplicitly-decide-on-effect-on-nested-functionsresolved in this commentManaged by
@rustbot—see help for details.