Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a9dff82

Browse files
committedMar 17, 2022
Document the target_feature_11 feature
1 parent 0a2fe66 commit a9dff82

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed
 

‎src/attributes/codegen.md

+29-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,39 @@ 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+
fn bar() {
73+
// Calling `foo_avx2` here is unsafe, as we must ensure
74+
// that AVX is available first.
75+
unsafe {
76+
foo_avx2();
77+
}
78+
}
79+
80+
#[target_feature(enable = "avx2")]
81+
fn bar_avx2() {
82+
// Calling `foo_avx2` here is safe.
83+
foo_avx2();
84+
|| foo_avx2();
85+
}
86+
```
87+
88+
Like unsafe functions, functions marked with `target_feature` cannot be
89+
assigned to a safe function pointer and do not implement `FnOnce`.
90+
91+
Functions marked with `target_feature` are not inlined into a context unless
92+
it supports the given features. The `#[inline(always)]` attribute may not
93+
be used with `target_feature`.
7294

7395
### Available features
7496

7597
The following is a list of the available feature names.
7698

7799
#### `x86` or `x86_64`
78100

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-
83101
Feature | Implicitly Enables | Description
84102
------------|--------------------|-------------------
85103
`aes` | `sse2` | [AES] — Advanced Encryption Standard
@@ -135,9 +153,6 @@ Feature | Implicitly Enables | Description
135153

136154
#### `aarch64`
137155

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

@@ -200,9 +215,8 @@ Feature | Implicitly Enables | Feature Name
200215

201216
#### `wasm32` or `wasm64`
202217

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
218+
`#[target_feature]` may be called from a safe context on Wasm platforms. It is
219+
impossible to cause undefined behavior via the `#[target_feature]` attribute because
206220
attempting to use instructions unsupported by the Wasm engine will fail at load
207221
time without the risk of being interpreted in a way different from what the
208222
compiler expected.

0 commit comments

Comments
 (0)
Please sign in to comment.