Skip to content

Commit 54107ec

Browse files
Document the target_feature_11 feature
1 parent 0a2fe66 commit 54107ec

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

src/attributes/codegen.md

+32-15
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@ features. It uses the [_MetaListNameValueStr_] syntax with a single key of
5353
`enable` whose value is a string of comma-separated feature names to enable.
5454

5555
```rust
56-
# #[cfg(target_feature = "avx2")]
5756
#[target_feature(enable = "avx2")]
58-
unsafe fn foo_avx2() {}
57+
fn foo_avx2() {}
5958
```
6059

6160
Each [target architecture] has a set of features that may be enabled. It is an
@@ -66,20 +65,42 @@ It is [undefined behavior] to call a function that is compiled with a feature
6665
that is not supported on the current platform the code is running on, *except*
6766
if the platform explicitly documents this to be safe.
6867

69-
Functions marked with `target_feature` are not inlined into a context that
70-
does not support the given features. The `#[inline(always)]` attribute may not
71-
be used with a `target_feature` attribute.
68+
For this reason, a function marked with `target_feature` is unsafe, except in
69+
a context that supports the given features. For example:
70+
71+
```rust
72+
# #[target_feature(enable = "avx2")]
73+
# fn foo_avx2() {}
74+
75+
fn bar() {
76+
// Calling `foo_avx2` here is unsafe, as we must ensure
77+
// that AVX is available first.
78+
unsafe {
79+
foo_avx2();
80+
}
81+
}
82+
83+
#[target_feature(enable = "avx2")]
84+
fn bar_avx2() {
85+
// Calling `foo_avx2` here is safe.
86+
foo_avx2();
87+
|| foo_avx2();
88+
}
89+
```
90+
91+
Like unsafe functions, functions marked with `target_feature` cannot be
92+
assigned to a safe function pointer and do not implement `FnOnce`.
93+
94+
Functions marked with `target_feature` are not inlined into a context unless
95+
it supports the given features. The `#[inline(always)]` attribute may not
96+
be used with `target_feature`.
7297

7398
### Available features
7499

75100
The following is a list of the available feature names.
76101

77102
#### `x86` or `x86_64`
78103

79-
Executing code with unsupported features is undefined behavior on this platform.
80-
Hence this platform requires that `#[target_feature]` is only applied to [`unsafe`
81-
functions][unsafe function].
82-
83104
Feature | Implicitly Enables | Description
84105
------------|--------------------|-------------------
85106
`aes` | `sse2` | [AES] — Advanced Encryption Standard
@@ -135,9 +156,6 @@ Feature | Implicitly Enables | Description
135156

136157
#### `aarch64`
137158

138-
This platform requires that `#[target_feature]` is only applied to [`unsafe`
139-
functions][unsafe function].
140-
141159
Further documentation on these features can be found in the [ARM Architecture
142160
Reference Manual], or elsewhere on [developer.arm.com].
143161

@@ -200,9 +218,8 @@ Feature | Implicitly Enables | Feature Name
200218

201219
#### `wasm32` or `wasm64`
202220

203-
`#[target_feature]` may be used with both safe and
204-
[`unsafe` functions][unsafe function] on Wasm platforms. It is impossible to
205-
cause undefined behavior via the `#[target_feature]` attribute because
221+
`#[target_feature]` may be called from a safe context on Wasm platforms. It is
222+
impossible to cause undefined behavior via the `#[target_feature]` attribute because
206223
attempting to use instructions unsupported by the Wasm engine will fail at load
207224
time without the risk of being interpreted in a way different from what the
208225
compiler expected.

0 commit comments

Comments
 (0)