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?
Add a smoke test for the optimize attribute. Tracking issue: rust-lang#54882 Stabilization PR: rust-lang#157273
…nline, r=JonathanBrouwer Forbid optimize(none) with inline(always) or inline. Tracking issue: rust-lang#54882 Stabilization PR: rust-lang#157273
Add a smoke test for the optimize attribute. Tracking issue: rust-lang#54882 Stabilization PR: rust-lang#157273
…nline, r=JonathanBrouwer Forbid optimize(none) with inline(always) or inline. Tracking issue: rust-lang#54882 Stabilization PR: rust-lang#157273
Add a smoke test for the optimize attribute. Tracking issue: rust-lang#54882 Stabilization PR: rust-lang#157273
…nline, r=JonathanBrouwer Forbid optimize(none) with inline(always) or inline. Tracking issue: rust-lang#54882 Stabilization PR: rust-lang#157273
Add a smoke test for the optimize attribute. Tracking issue: rust-lang#54882 Stabilization PR: rust-lang#157273
This comment has been minimized.
This comment has been minimized.
In my view, it should work in the same way as |
This commit stabilizes the `#[optimize]` attribute, which allows for more fine-grained control of optimization levels.
c87a3e7 to
3aa06ad
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. |
|
On the capabilities and scope of the attribute (@nagisa): as you said, I don't believe we can ever give realistic guarantees on what effects those attributes will have - beyond the fact that they do not change the semantics of the program, and that they might affect compiler optimizations -- similarly to the On the meaning of optimize(size) (@nikic): I believe using LLVM's On behaviour of nested functions: I agree with @traviscross that it probably should be the same as |
…JonathanBrouwer Forbid optimize(none) with inline(always) or inline. Tracking issue: rust-lang/rust#54882 Stabilization PR: rust-lang/rust#157273
Add a smoke test for the optimize attribute. Tracking issue: rust-lang/rust#54882 Stabilization PR: rust-lang/rust#157273
|
Leaving aside whether we apply the optimize attribute recursively to nested items, it'd be helpful to know whether it applies recursively to closures. If those are treated the same way, that seems likely to be surprising; I think people would expect the attribute to apply recursively to closures. If the easiest way to do that is to also apply recursively to items, then I think that's what we should do, as closures seem likely to be the common case where it's desired and where it'd be otherwise inconvenient to do. (Also, can we confirm whether we can write the attribute in a fashion that applies to closures?) |
|
Random bikeshed-color suggestion (for future extension, not derailing what's currently proposed for stabilization):
|
As far as I can tell, it is not (and neither is |
At least from a lang perspective I'd be happy adding things like |
my 2c: I prefer |
|
For my own part, on the meaning question, I'm happy for Regarding the degree to which this is a lang question, I think what we intend for an attribute to mean (e.g., "optimize for size at all costs"), and consequently, what we'd write in the Reference about it, is a lang question. How to best implement that, in terms of which flags to apply, is a compiler one. E.g., our docs say that On the other two questions: One, I still expect this attribute to affect only the item to which it is applied, in the same way as If we were to later want to apply this to modules and to functions in a way that it would affect inner items, I'd expect this to be syntactically distinct and to work in the same way with Two, on the question of whether we're happy with the capability and scope of this attribute, e.g., that no guarantees can be made about |
|
In the lang meeting today, we discussed the three questions raised as concerns: On the capabilities and scope question, we were all agreed that we're happy to accept the limitations. On the question of meaning, we were happy with On the question of whether this applies recursively to inner items, my sense of the meeting was that we were happy for this to land in its current form where it does not apply recursively, but I note @joshtriplett's earlier comment in #157273 (comment), so I'll let him speak to whether he considers application to inner closures something we should discuss and resolve before proceeding. @jieyouxu: Does that help with the concerns you raised? Once you've resolved those, I'm planning to propose dual FCP merge between lang and compiler. |
|
FWIW, specifically for closures, I think we should at least make an optimize attribute applied to a closure do something if the closure is not inlined, regardless of whether closures inherit the optimize attribute of the function they live in. Since often closures are MIR-inlined (I believe!), perhaps having the closure inherit the attribute is the least surprising behaviour - otherwise whether the closure inherits the attribute or not depends on hard-to-control optimizations (of course whether the attribute does something at all also depends on optimizations, but we should at least try to not make the problem worse) |
Yes, unless we still have that closure semantics question to make a deliberate decision on. I was mostly just forwarding important comments as concerns so they're not lost. @rustbot resolve are-we-happy-wiht-capability-and-scope-of-this-attribute |
As best I can tell, @veluca93: Are we looking at the same thing? |
I think we are looking at similar things, at least - I tried (size) instead of (none) and its behaviour seems a bit less predictable, see https://rust.godbolt.org/z/jGjc51PKn :-) |
View all comments
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.