Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ The following is an index of all built-in attributes.
- [`cold`] --- Hint that a function is unlikely to be called.
- [`naked`] --- Prevent the compiler from emitting a function prologue and epilogue.
- [`no_builtins`] --- Disables use of certain built-in functions.
- [`optimize`] --- Controls the level of optimization to be applied to the function.
- [`target_feature`] --- Configure platform-specific code generation.
- [`track_caller`] --- Pass the parent call location to `std::panic::Location::caller()`.
- [`instruction_set`] --- Specify the instruction set used to generate a function's code.
Expand Down Expand Up @@ -328,6 +329,7 @@ The following is an index of all built-in attributes.
[`no_mangle`]: abi.md#the-no_mangle-attribute
[`no_std`]: names/preludes.md#the-no_std-attribute
[`non_exhaustive`]: attributes/type_system.md#the-non_exhaustive-attribute
[`optimize`]: attributes/codegen.md#the-optimize-attribute
[`panic_handler`]: panic.md#the-panic_handler-attribute
[`path`]: items/modules.md#the-path-attribute
[`proc_macro_attribute`]: procedural-macros.md#the-proc_macro_attribute-attribute
Expand Down
45 changes: 45 additions & 0 deletions src/attributes/codegen.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,51 @@ Only the first use of the `no_builtins` attribute has effect.
> [!NOTE]
> `rustc` lints against any use following the first.

<!-- template:attributes -->
r[attributes.codegen.optimize]
## The `optimize` attribute

r[attributes.codegen.optimize.intro]
The *`optimize` [attribute]* suggests whether a function, inherent method, trait method, or closure should be optimized for size or speed.

> [!EXAMPLE]
> ```rust
> #[optimize(size)]
> pub fn example1() {}
>
> #[optimize(speed)]
> pub fn example2() {}
>
> #[optimize(none)]
> pub fn example3() {}
> ```

r[attributes.codegen.optimize.syntax]
The syntax for the `optimize` attribute is:

```grammar,attributes
@root OptimizeAttribute ->
`optimize` `(` `size` `)`
| `optimize` `(` `speed` `)`
| `optimize` `(` `none` `)`
```

r[attributes.codegen.optimize.allowed-positions]
The `optimize` attribute may only be applied to [closures], [async blocks], [free functions], [associated functions] in an [inherent impl] or [trait impl], and associated functions in a [trait definition].

r[attributes.codegen.optimize.duplicates]
Applying multiple `optimize` attributes to the same function is an error.

r[attributes.codegen.optimize.modes]
The `optimize` attribute supports these modes:

- `#[optimize(size)]` suggests prioritizing minimizing code size, potentially at the cost of execution performance.
- `#[optimize(speed)]` suggests prioritizing execution performance, potentially at the cost of code size.
- `#[optimize(none)]` suggests not optimizing the function, with similar effects to applying -Copt-level=0 locally.

> [!NOTE]
> In every form the attribute is a hint. The compiler may ignore it.

r[attributes.codegen.target_feature]
## The `target_feature` attribute

Expand Down
2 changes: 2 additions & 0 deletions src/items/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,7 @@ The attributes that have meaning on a function are:
- [`link_section`]
- [`must_use`]
- [`no_mangle`]
- [`optimize`]
- [Lint check attributes]
- [Procedural macro attributes]
- [Testing attributes]
Expand Down Expand Up @@ -764,6 +765,7 @@ fn foo_oof(#[some_inert_attribute] arg: u8) {
[`export_name`]: ../abi.md#the-export_name-attribute
[`link_section`]: ../abi.md#the-link_section-attribute
[`no_mangle`]: ../abi.md#the-no_mangle-attribute
[`optimize`]: ../attributes/codegen.md#the-optimize-attribute
[built-in attributes]: ../attributes.md#built-in-attributes-index
[trait item]: traits.md
[method]: associated-items.md#methods
Expand Down
Loading