Skip to content

tweak notes for optimize(speed/size) - #2415

Open
RalfJung wants to merge 8 commits into
rust-lang:masterfrom
RalfJung:optimize
Open

RalfJung wants to merge 8 commits into
rust-lang:masterfrom
RalfJung:optimize

Conversation

@RalfJung

@RalfJung RalfJung commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

I found it odd that the comment for optimize(size/speed) is so far away from the one for optimize(none), so I moved it down and also updated it based on the discussion on Zulip.

Also see rust-lang/rust#166002 which adjusts rustc to make mir-opt and LLVM behavior more consistent.

@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Nov 15, 2026
Let's make some editorial fixes and revisions to the tweaked note
about the `optimize` attribute.
The note says that `optimize(size)` and `optimize(speed)` don't do
much at `-C opt-level=0` or at `-C opt-level=1`.

That's true for `-C opt-level=0`.  But at `-C opt-level=1`, the
compiler does meaningful optimization, and `optimize(size)`
meaningfully affects the results.

Let's trim the claim to `-C opt-level=0` only.
We say that `-C opt-level=0` disables most of the LLVM optimization
pipeline.  Let's be more precise and say exactly what it doesn't
disable.
The `optimize` attribute affects both optimization and code
generation.  What we're saying in this note is most accurate if scoped
to speak only about the effect on optimization, so let's scope it in
that way.
In the note, we say that marking a function with `optimize(size)`
affects optimization at `-C opt-level=2` and higher.  But that's
incomplete.  It actually affects optimization at all levels other than
`-C opt-level=0` and `-C opt-level=z`.  Let's say that.
There's more to the story of the `optimize` attribute than just the
effect on optimization.  It also affects code generation.  Let's
discuss this in a new note.
@traviscross

Copy link
Copy Markdown
Contributor

Thanks @RalfJung. I've pushed some revisions. (The situation is always a bit more complicated.) Please have a look.

Comment thread src/attributes/codegen.md Outdated
> Settings that base their defaults on the optimization level, such as debug assertions and overflow checks, ignore this attribute and use the crate's optimization level.

> [!NOTE]
> For `rustc`, `-C opt-level=0` (the default) disables the LLVM optimization pipeline for the entire crate, except for the inlining of `inline(always)` functions, and `optimize(size)` and `optimize(speed)` do not enable it for the attributed function. At that optimization level, therefore, these two modes have no optimization effect. The point of these two modes is that some functions can be marked as preferring a smaller size over more speed, which will have effect on optimization at every optimization level except `-C opt-level=0` and `-C opt-level=z`, and some functions can be marked as preferring more speed over a smaller size, which will have effect on optimization at `-C opt-level=s` and `-C opt-level=z`.

@RalfJung RalfJung Nov 16, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

which will have effect on optimization at every optimization level except -C opt-level=0 and -C opt-level=z

Is that right? According to what @nikic said, -C opt-level=z should respect optimize(speed).

I find this paragraph very hard to read, and it overlaps a lot with the next one as well. Talking about both paragraphs, I don't understand why you want to go into so much detail, I find that distracting and it makes it harder to understand the high-level picture: generally, these flags are meant for optimized builds, where in a build that's generally optimized for one dimension, one can pick a few functions to be optimized for the other dimension. Minor effects may occur even in unoptimized builds, but this can not be used to make a largely unoptimized build where some functions are optimized.

The main thing I want to achieve with this PR is to explain to people why adding optimize(speed) in an opt-level=0 build doesn't do much. With your changes I don't think it achieves that goal as people will get lost in (mostly irrelevant) details and are likely to miss this high-level point.

View changes since the review

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I agree with Ralf that this seems like way too much detail that is mostly irrelevant, unactionable, and distracting for users. It's also the sort of details that is likely to become outdated over time, and invites even further unnecessarily precise correction (e.g., opt-level significantly affects the shape of the CodeGen pipeline and in particular the choice of ISel implementation, so while the specific effects listed as examples in the second note may be true, it's misleading to present CodeGen as categorically different from IR optimizations in this respect).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks. I've revised. Better?

Regarding the deleted language:

[...some functions can be marked as preferring a smaller size over more speed,] which will have effect on optimization at every optimization level except -C opt-level=0 and -C opt-level=z

Is that right? According to what @nikic said, -C opt-level=z should respect optimize(speed).

Yes, that's right. That part of the sentence was referring to optimize(size).

Regarding why to go into some detail, one of the main audiences of the Reference is the lang team, in support of our work on language design, and more broadly, the Project. As the details of some situation become more confusing, I tend to lean toward seeing more value in writing down those details precisely in the Reference so we can better reason about them clearly, now and in the future.

But I also sympathize with the tensions you mention, so I've simplified that discussion and focused it on the points that you highlighted.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks, that's much better yes. :) I like it.

The more detailed notes on the behavior of `optimize(size)` and
`optimize(speed)` might obscure the high-level point.  Let's
consolidate and rewrite the notes to say the high-level thing
precisely.

This branch has not been deployed

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

Labels

S-waiting-on-review Status: The marked PR is awaiting review from a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants