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?
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 :-) |
What's important here, as best I can tell, is the coercion to a function pointer. Due to that, we're calling the This seems a QoI matter. @veluca93, do you want to look into whether |
#157802 ought to fix the problem (I think, not 100% sure that the approach is right). If we decide that we are OK with |
|
@traviscross has proposed to merge this. The next step is review by the rest of the tagged team members:
Concerns:
Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns. |
optimize attribute
Yes, this PR will stabilize the following: #![feature(optimize_attribute)]
fn main() {
f(
#[optimize(size)] || { },
);
}
fn f(_: impl Fn()) {}Note the absence of |
…mejrs,wesleywiser Ensure that optimize attributes on closures are inherited by the shim. Tracking issue: rust-lang#54882 Stabilization PR: rust-lang#157273
|
I think it's weird for this attribute not to apply to nested functions, and I finally put my finger on why. Optimization is a knob you usually set at the level of an entire compilation unit, and in fact, even an entire project. This attribute is great as a way of making that knob more fine-grained. But that implies it should work on modules. In other words, it would be surprising if there was a discontinuity here:
If we accept that In contrast, there is no equivalent of |
…mejrs,wesleywiser Ensure that optimize attributes on closures are inherited by the shim. Tracking issue: rust-lang#54882 Stabilization PR: rust-lang#157273
…mejrs,wesleywiser Ensure that optimize attributes on closures are inherited by the shim. Tracking issue: rust-lang#54882 Stabilization PR: rust-lang#157273
…mejrs,wesleywiser Ensure that optimize attributes on closures are inherited by the shim. Tracking issue: rust-lang#54882 Stabilization PR: rust-lang#157273
The intended semantics would be that:
Is that correct? If so, I can make a PR for that once #157802 is merged. This naturally extends to allowing the optimize attribute on modules, but we can leave that to a future extension IMO. |
I've wanted
Would you feel the same way if we had attributes to control loop unrolling or vectorization (i.e., some of the underlying things controlled by an While I understand the UX intuition that you mention (i.e., that some knobs feel more like compilation-unit ones), when adding attributes to control those, it seems better to me, as a language matter, for our codegen attributes to apply to items in a consistent way rather than having them differ based on those intuitions (which may vary between people). If we want to support automatic application to nested items, I'd suggest we do that (later) as an orthogonal axis in a syntactically distinct way. As one comparable, that's what GCC does: // Compile compilation unit with `-O2`.
static int helper(int x) { return x * 7 + 3; }
__attribute__((optimize("O0")))
int with_attribute(int x) {
int attr_nested(int y) { return helper(y) + 1; } // Still `-O2`.
return attr_nested(x);
}
#pragma GCC push_options
#pragma GCC optimize ("O0")
int in_region(int x) {
int region_nested(int y) { return helper(y) + 2; } // This is `-O0`.
return region_nested(x);
}
#pragma GCC pop_options |
|
@rfcbot reviewed |
|
This seems fine to me, but one thing did surprise me:
Looking at the code I see that |
I don't personally know, perhaps @nagisa would? (as - I believe - the person to implement that part) |
|
There isn't a way to tell LLVM that you want to optimize a single function for speed; or at least there wasn't. My memory is that I had planned to implement this as setting EDIT: though now with the improvements on LLVM side that Nikita mentioned maybe we can do tell LLVM that we want speed pretty much unconditionally and then mark all the functions not otherwise annotated with an |
Whatever ends up happening, a comment on that empty match arm would be helpful. |
|
We shouldn't stabilize @rfcbot concern optimize-speed-does-nothing |
Agreed. @nikic, is there a way to get My two cents: it'd be nice to stabilize |
|
tl;dr |
|
See rust/compiler/rustc_codegen_ssa/src/base.rs Lines 1101 to 1118 in 8e15021 |
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.