From 4a74ceb21ffaed46a57c6c079820207b19587a84 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 15 Nov 2026 17:17:03 +0100 Subject: [PATCH 1/8] tweak notes for optimize(speed/size) --- src/attributes/codegen.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/attributes/codegen.md b/src/attributes/codegen.md index 6e8e624d17..cd1ad6d8bc 100644 --- a/src/attributes/codegen.md +++ b/src/attributes/codegen.md @@ -285,9 +285,6 @@ The *`optimize` [attribute]* suggests an optimization goal for the attributed fu > pub fn example3() {} > ``` -> [!NOTE] -> For `rustc`, `-C opt-level=0` (the default) disables LLVM optimizations for the entire crate, and `optimize(size)` and `optimize(speed)` do not enable them for the attributed function. `rustc` still applies its own optimizations, other than inlining, to such a function. - r[attributes.codegen.optimize.syntax] The syntax for the `optimize` attribute is: @@ -368,6 +365,9 @@ The `optimize` attribute supports these modes: > > 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) and `-C opt-level=1` disable most of the LLVM optimization pipeline for the entire crate, and `optimize(size)` and `optimize(speed)` do not enable them for the attributed function. These two modes therefore do not have much of an effect at low opt-levels. The point of these modes is that at `-C opt-level=2` or higher, some functions can be marked as preferring a small size over speed, and at `-C opt-level=s` or `-C opt-level=z`, some functions can be marked as preferred speed over a small size. + r[attributes.codegen.optimize.inline] `#[optimize(none)]` may not be used with `#[inline]` or `#[inline(always)]` on the same function. It may be used with `#[inline(never)]`. (See [attributes.codegen.inline.optimize].) From 7de3a4dcc6d3440fec601f5fc8de834f25aaa47c Mon Sep 17 00:00:00 2001 From: Travis Cross Date: Sun, 15 Nov 2026 16:30:51 +0000 Subject: [PATCH 2/8] Revise wording on tweaked `optimize` note Let's make some editorial fixes and revisions to the tweaked note about the `optimize` attribute. --- src/attributes/codegen.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/attributes/codegen.md b/src/attributes/codegen.md index cd1ad6d8bc..2a56727560 100644 --- a/src/attributes/codegen.md +++ b/src/attributes/codegen.md @@ -366,7 +366,7 @@ The `optimize` attribute supports these modes: > 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) and `-C opt-level=1` disable most of the LLVM optimization pipeline for the entire crate, and `optimize(size)` and `optimize(speed)` do not enable them for the attributed function. These two modes therefore do not have much of an effect at low opt-levels. The point of these modes is that at `-C opt-level=2` or higher, some functions can be marked as preferring a small size over speed, and at `-C opt-level=s` or `-C opt-level=z`, some functions can be marked as preferred speed over a small size. +> For `rustc`, `-C opt-level=0` (the default) and `-C opt-level=1` disable most of the LLVM optimization pipeline for the entire crate, and `optimize(size)` and `optimize(speed)` do not enable it for the attributed function. These two modes therefore do not have much of an effect at low optimization levels. 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 at `-C opt-level=2` and higher, and some functions can be marked as preferring more speed over a smaller size, which will have effect at `-C opt-level=s` and `-C opt-level=z`. r[attributes.codegen.optimize.inline] `#[optimize(none)]` may not be used with `#[inline]` or `#[inline(always)]` on the same function. It may be used with `#[inline(never)]`. (See [attributes.codegen.inline.optimize].) From d01c67b5f891c8cbb2a750d42da441fb9c5b5649 Mon Sep 17 00:00:00 2001 From: Travis Cross Date: Sun, 15 Nov 2026 21:55:48 +0000 Subject: [PATCH 3/8] Fix the `optimize` note WRT `-C opt-level=1` 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. --- src/attributes/codegen.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/attributes/codegen.md b/src/attributes/codegen.md index 2a56727560..4dd718b3c6 100644 --- a/src/attributes/codegen.md +++ b/src/attributes/codegen.md @@ -366,7 +366,7 @@ The `optimize` attribute supports these modes: > 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) and `-C opt-level=1` disable most of the LLVM optimization pipeline for the entire crate, and `optimize(size)` and `optimize(speed)` do not enable it for the attributed function. These two modes therefore do not have much of an effect at low optimization levels. 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 at `-C opt-level=2` and higher, and some functions can be marked as preferring more speed over a smaller size, which will have effect at `-C opt-level=s` and `-C opt-level=z`. +> For `rustc`, `-C opt-level=0` (the default) disables most of the LLVM optimization pipeline for the entire crate, and `optimize(size)` and `optimize(speed)` do not enable it for the attributed function. At that optimization level, therefore, these two modes do not have much of an 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 at `-C opt-level=2` and higher, and some functions can be marked as preferring more speed over a smaller size, which will have effect at `-C opt-level=s` and `-C opt-level=z`. r[attributes.codegen.optimize.inline] `#[optimize(none)]` may not be used with `#[inline]` or `#[inline(always)]` on the same function. It may be used with `#[inline(never)]`. (See [attributes.codegen.inline.optimize].) From e58b374c6756605094b8532518ba397ac0273e97 Mon Sep 17 00:00:00 2001 From: Travis Cross Date: Sun, 15 Nov 2026 21:55:49 +0000 Subject: [PATCH 4/8] Say what LLVM still does at `-C opt-level=0` 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. --- src/attributes/codegen.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/attributes/codegen.md b/src/attributes/codegen.md index 4dd718b3c6..5d4a56be75 100644 --- a/src/attributes/codegen.md +++ b/src/attributes/codegen.md @@ -366,7 +366,7 @@ The `optimize` attribute supports these modes: > 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 most of the LLVM optimization pipeline for the entire crate, and `optimize(size)` and `optimize(speed)` do not enable it for the attributed function. At that optimization level, therefore, these two modes do not have much of an 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 at `-C opt-level=2` and higher, and some functions can be marked as preferring more speed over a smaller size, which will have effect at `-C opt-level=s` and `-C opt-level=z`. +> 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 do not have much of an 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 at `-C opt-level=2` and higher, and some functions can be marked as preferring more speed over a smaller size, which will have effect at `-C opt-level=s` and `-C opt-level=z`. r[attributes.codegen.optimize.inline] `#[optimize(none)]` may not be used with `#[inline]` or `#[inline(always)]` on the same function. It may be used with `#[inline(never)]`. (See [attributes.codegen.inline.optimize].) From 649ef3ed2bc81931f8d6058206669b100611a073 Mon Sep 17 00:00:00 2001 From: Travis Cross Date: Sun, 15 Nov 2026 22:04:53 +0000 Subject: [PATCH 5/8] Scope the `optimize` note to optimization effect 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. --- src/attributes/codegen.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/attributes/codegen.md b/src/attributes/codegen.md index 5d4a56be75..dccef0a3b9 100644 --- a/src/attributes/codegen.md +++ b/src/attributes/codegen.md @@ -366,7 +366,7 @@ The `optimize` attribute supports these modes: > 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 do not have much of an 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 at `-C opt-level=2` and higher, and some functions can be marked as preferring more speed over a smaller size, which will have effect at `-C opt-level=s` and `-C opt-level=z`. +> 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 `-C opt-level=2` and higher, 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`. r[attributes.codegen.optimize.inline] `#[optimize(none)]` may not be used with `#[inline]` or `#[inline(always)]` on the same function. It may be used with `#[inline(never)]`. (See [attributes.codegen.inline.optimize].) From 83029114c0596eb054721bde334647184c1fe56a Mon Sep 17 00:00:00 2001 From: Travis Cross Date: Sun, 15 Nov 2026 21:55:49 +0000 Subject: [PATCH 6/8] Fix where the note says `optimize(size)` has effect 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. --- src/attributes/codegen.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/attributes/codegen.md b/src/attributes/codegen.md index dccef0a3b9..91e6535ca7 100644 --- a/src/attributes/codegen.md +++ b/src/attributes/codegen.md @@ -366,7 +366,7 @@ The `optimize` attribute supports these modes: > 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 `-C opt-level=2` and higher, 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`. +> 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`. r[attributes.codegen.optimize.inline] `#[optimize(none)]` may not be used with `#[inline]` or `#[inline(always)]` on the same function. It may be used with `#[inline(never)]`. (See [attributes.codegen.inline.optimize].) From 869613e03d74a464374d60f867e50f82b856407e Mon Sep 17 00:00:00 2001 From: Travis Cross Date: Sun, 15 Nov 2026 21:55:50 +0000 Subject: [PATCH 7/8] Add note on code generation effect of `optimize` 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. --- src/attributes/codegen.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/attributes/codegen.md b/src/attributes/codegen.md index 91e6535ca7..e85e671593 100644 --- a/src/attributes/codegen.md +++ b/src/attributes/codegen.md @@ -368,6 +368,9 @@ The `optimize` attribute supports these modes: > [!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`. +> [!NOTE] +> For `rustc`, `optimize(size)` marks the function for LLVM as one whose code is to be as small as possible. Disabling the LLVM optimization pipeline does not disable the LLVM code generator, which consults this mark when choosing among ways to translate an operation to machine instructions. On some targets, therefore, `optimize(size)` has effect on code generation even at `-C opt-level=0`. For example, when targeting AArch64, a `match` with a small number of closely spaced arms may be compiled to a jump table rather than to a chain of comparisons, and when targeting RISC-V, a division by a constant may be compiled to a division rather than to a multiplication and shifts. Conversely, `optimize(speed)` leaves the function without the marks that `-C opt-level=s` and `-C opt-level=z` otherwise place on every function, so it has effect on code generation at those optimization levels. + r[attributes.codegen.optimize.inline] `#[optimize(none)]` may not be used with `#[inline]` or `#[inline(always)]` on the same function. It may be used with `#[inline(never)]`. (See [attributes.codegen.inline.optimize].) From 72764742d377ede08efa7104a48f20cdf21c0077 Mon Sep 17 00:00:00 2001 From: Travis Cross Date: Mon, 16 Nov 2026 19:25:12 +0000 Subject: [PATCH 8/8] Simplify notes on `optimize(size/speed)` 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. --- src/attributes/codegen.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/attributes/codegen.md b/src/attributes/codegen.md index e85e671593..7793daf070 100644 --- a/src/attributes/codegen.md +++ b/src/attributes/codegen.md @@ -366,10 +366,9 @@ The `optimize` attribute supports these modes: > 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`. - -> [!NOTE] -> For `rustc`, `optimize(size)` marks the function for LLVM as one whose code is to be as small as possible. Disabling the LLVM optimization pipeline does not disable the LLVM code generator, which consults this mark when choosing among ways to translate an operation to machine instructions. On some targets, therefore, `optimize(size)` has effect on code generation even at `-C opt-level=0`. For example, when targeting AArch64, a `match` with a small number of closely spaced arms may be compiled to a jump table rather than to a chain of comparisons, and when targeting RISC-V, a division by a constant may be compiled to a division rather than to a multiplication and shifts. Conversely, `optimize(speed)` leaves the function without the marks that `-C opt-level=s` and `-C opt-level=z` otherwise place on every function, so it has effect on code generation at those optimization levels. +> For `rustc`, `#[optimize(size)]` and `#[optimize(speed)]` don't have much effect with `-C opt-level=0` (the default). This is because `-C opt-level=0` disables most of the LLVM optimization pipeline and these attributes do not enable it for attributed functions. +> +> Applying `#[optimize(size)]` to a function suggests you would prefer a smaller size over more speed for that function; applying `#[optimize(speed)]` suggests you would prefer more speed over a smaller size. Under optimization, `rustc` tries to give effect to these preferences. r[attributes.codegen.optimize.inline] `#[optimize(none)]` may not be used with `#[inline]` or `#[inline(always)]` on the same function. It may be used with `#[inline(never)]`. (See [attributes.codegen.inline.optimize].)