@@ -53,9 +53,8 @@ features. It uses the [_MetaListNameValueStr_] syntax with a single key of
53
53
` enable ` whose value is a string of comma-separated feature names to enable.
54
54
55
55
``` rust
56
- # #[cfg(target_feature = " avx2" )]
57
56
#[target_feature(enable = " avx2" )]
58
- unsafe fn foo_avx2 () {}
57
+ fn foo_avx2 () {}
59
58
```
60
59
61
60
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
66
65
that is not supported on the current platform the code is running on, * except*
67
66
if the platform explicitly documents this to be safe.
68
67
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 ` .
72
97
73
98
### Available features
74
99
75
100
The following is a list of the available feature names.
76
101
77
102
#### ` x86 ` or ` x86_64 `
78
103
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
-
83
104
Feature | Implicitly Enables | Description
84
105
------------|--------------------|-------------------
85
106
` aes ` | ` sse2 ` | [ AES] — Advanced Encryption Standard
@@ -135,9 +156,6 @@ Feature | Implicitly Enables | Description
135
156
136
157
#### ` aarch64 `
137
158
138
- This platform requires that ` #[target_feature] ` is only applied to [ ` unsafe `
139
- functions] [ unsafe function ] .
140
-
141
159
Further documentation on these features can be found in the [ ARM Architecture
142
160
Reference Manual] , or elsewhere on [ developer.arm.com] .
143
161
@@ -200,9 +218,8 @@ Feature | Implicitly Enables | Feature Name
200
218
201
219
#### ` wasm32 ` or ` wasm64 `
202
220
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
206
223
attempting to use instructions unsupported by the Wasm engine will fail at load
207
224
time without the risk of being interpreted in a way different from what the
208
225
compiler expected.
0 commit comments