Skip to content

Commit 5754051

Browse files
authored
Rollup merge of #134090 - veluca93:stable-tf11, r=oli-obk
Stabilize target_feature_11 # Stabilization report This is an updated version of #116114, which is itself a redo of #99767. Most of this commit and report were copied from those PRs. Thanks ```@LeSeulArtichaut``` and ```@calebzulawski!``` ## Summary Allows for safe functions to be marked with `#[target_feature]` attributes. Functions marked with `#[target_feature]` are generally considered as unsafe functions: they are unsafe to call, cannot *generally* be assigned to safe function pointers, and don't implement the `Fn*` traits. However, calling them from other `#[target_feature]` functions with a superset of features is safe. ```rust // Demonstration function #[target_feature(enable = "avx2")] fn avx2() {} fn foo() { // Calling `avx2` here is unsafe, as we must ensure // that AVX is available first. unsafe { avx2(); } } #[target_feature(enable = "avx2")] fn bar() { // Calling `avx2` here is safe. avx2(); } ``` Moreover, once #135504 is merged, they can be converted to safe function pointers in a context in which calling them is safe: ```rust // Demonstration function #[target_feature(enable = "avx2")] fn avx2() {} fn foo() -> fn() { // Converting `avx2` to fn() is a compilation error here. avx2 } #[target_feature(enable = "avx2")] fn bar() -> fn() { // `avx2` coerces to fn() here avx2 } ``` See the section "Closures" below for justification of this behaviour. ## Test cases Tests for this feature can be found in [`tests/ui/target_feature/`](https://github.com/rust-lang/rust/tree/f6cb952dc115fd1311b02b694933e31d8dc8b002/tests/ui/target-feature). ## Edge cases ### Closures * [target-feature 1.1: should closures inherit target-feature annotations? #73631](#73631) Closures defined inside functions marked with #[target_feature] inherit the target features of their parent function. They can still be assigned to safe function pointers and implement the appropriate `Fn*` traits. ```rust #[target_feature(enable = "avx2")] fn qux() { let my_closure = || avx2(); // this call to `avx2` is safe let f: fn() = my_closure; } ``` This means that in order to call a function with #[target_feature], you must guarantee that the target-feature is available while the function, any closures defined inside it, as well as any safe function pointers obtained from target-feature functions inside it, execute. This is usually ensured because target features are assumed to never disappear, and: - on any unsafe call to a `#[target_feature]` function, presence of the target feature is guaranteed by the programmer through the safety requirements of the unsafe call. - on any safe call, this is guaranteed recursively by the caller. If you work in an environment where target features can be disabled, it is your responsibility to ensure that no code inside a target feature function (including inside a closure) runs after this (until the feature is enabled again). **Note:** this has an effect on existing code, as nowadays closures do not inherit features from the enclosing function, and thus this strengthens a safety requirement. It was originally proposed in #73631 to solve this by adding a new type of UB: “taking a target feature away from your process after having run code that uses that target feature is UB” . This was motivated by userspace code already assuming in a few places that CPU features never disappear from a program during execution (see i.e. https://github.com/rust-lang/stdarch/blob/2e29bdf90832931ea499755bb4ad7a6b0809295a/crates/std_detect/src/detect/arch/x86.rs); however, concerns were raised in the context of the Linux kernel; thus, we propose to relax that requirement to "causing the set of usable features to be reduced is unsafe; when doing so, the programmer is required to ensure that no closures or safe fn pointers that use removed features are still in scope". * [Fix #[inline(always)] on closures with target feature 1.1 #111836](#111836) Closures accept `#[inline(always)]`, even within functions marked with `#[target_feature]`. Since these attributes conflict, `#[inline(always)]` wins out to maintain compatibility. ### ABI concerns * [The extern "C" ABI of SIMD vector types depends on target features #116558](#116558) The ABI of some types can change when compiling a function with different target features. This could have introduced unsoundness with target_feature_11, but recent fixes (#133102, #132173) either make those situations invalid or make the ABI no longer dependent on features. Thus, those issues should no longer occur. ### Special functions The `#[target_feature]` attribute is forbidden from a variety of special functions, such as main, current and future lang items (e.g. `#[start]`, `#[panic_handler]`), safe default trait implementations and safe trait methods. This was not disallowed at the time of the first stabilization PR for target_features_11, and resulted in the following issues/PRs: * [`#[target_feature]` is allowed on `main` #108645](#108645) * [`#[target_feature]` is allowed on default implementations #108646](#108646) * [#[target_feature] is allowed on #[panic_handler] with target_feature 1.1 #109411](#109411) * [Prevent using `#[target_feature]` on lang item functions #115910](#115910) ## Documentation * Reference: [Document the `target_feature_11` feature reference#1181](rust-lang/reference#1181) --- cc tracking issue #69098 cc ```@workingjubilee``` cc ```@RalfJung``` r? ```@rust-lang/lang```
2 parents ef148cd + 44b2e6c commit 5754051

34 files changed

+77
-165
lines changed

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,9 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
272272
if safe_target_features {
273273
if tcx.sess.target.is_like_wasm || tcx.sess.opts.actually_rustdoc {
274274
// The `#[target_feature]` attribute is allowed on
275-
// WebAssembly targets on all functions, including safe
276-
// ones. Other targets require that `#[target_feature]` is
277-
// only applied to unsafe functions (pending the
278-
// `target_feature_11` feature) because on most targets
275+
// WebAssembly targets on all functions. Prior to stabilizing
276+
// the `target_feature_11` feature, `#[target_feature]` was
277+
// only permitted on unsafe functions because on most targets
279278
// execution of instructions that are not supported is
280279
// considered undefined behavior. For WebAssembly which is a
281280
// 100% safe target at execution time it's not possible to
@@ -289,17 +288,10 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
289288
// if a target is documenting some wasm-specific code then
290289
// it's not spuriously denied.
291290
//
292-
// This exception needs to be kept in sync with allowing
293-
// `#[target_feature]` on `main` and `start`.
294-
} else if !tcx.features().target_feature_11() {
295-
feature_err(
296-
&tcx.sess,
297-
sym::target_feature_11,
298-
attr.span,
299-
"`#[target_feature(..)]` can only be applied to `unsafe` functions",
300-
)
301-
.with_span_label(tcx.def_span(did), "not an `unsafe` function")
302-
.emit();
291+
// Now that `#[target_feature]` is permitted on safe functions,
292+
// this exception must still exist for allowing the attribute on
293+
// `main`, `start`, and other functions that are not usually
294+
// allowed.
303295
} else {
304296
check_target_feature_trait_unsafe(tcx, did, attr.span);
305297
}
@@ -628,10 +620,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
628620
// its parent function, which effectively inherits the features anyway. Boxing this closure
629621
// would result in this closure being compiled without the inherited target features, but this
630622
// is probably a poor usage of `#[inline(always)]` and easily avoided by not using the attribute.
631-
if tcx.features().target_feature_11()
632-
&& tcx.is_closure_like(did.to_def_id())
633-
&& !codegen_fn_attrs.inline.always()
634-
{
623+
if tcx.is_closure_like(did.to_def_id()) && codegen_fn_attrs.inline != InlineAttr::Always {
635624
let owner_id = tcx.parent(did.to_def_id());
636625
if tcx.def_kind(owner_id).has_codegen_attrs() {
637626
codegen_fn_attrs

compiler/rustc_feature/src/accepted.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,8 @@ declare_features! (
386386
(accepted, struct_variant, "1.0.0", None),
387387
/// Allows `#[target_feature(...)]`.
388388
(accepted, target_feature, "1.27.0", None),
389+
/// Allows the use of `#[target_feature]` on safe functions.
390+
(accepted, target_feature_11, "CURRENT_RUSTC_VERSION", Some(69098)),
389391
/// Allows `fn main()` with return types which implements `Termination` (RFC 1937).
390392
(accepted, termination_trait, "1.26.0", Some(43301)),
391393
/// Allows `#[test]` functions where the return type implements `Termination` (RFC 1937).

compiler/rustc_feature/src/unstable.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,8 +633,6 @@ declare_features! (
633633
(unstable, strict_provenance_lints, "1.61.0", Some(130351)),
634634
/// Allows string patterns to dereference values to match them.
635635
(unstable, string_deref_patterns, "1.67.0", Some(87121)),
636-
/// Allows the use of `#[target_feature]` on safe functions.
637-
(unstable, target_feature_11, "1.45.0", Some(69098)),
638636
/// Allows using `#[thread_local]` on `static` items.
639637
(unstable, thread_local, "1.0.0", Some(29594)),
640638
/// Allows defining `trait X = A + B;` alias items.

library/core/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@
192192
#![feature(staged_api)]
193193
#![feature(stmt_expr_attributes)]
194194
#![feature(strict_provenance_lints)]
195-
#![feature(target_feature_11)]
196195
#![feature(trait_alias)]
197196
#![feature(transparent_unions)]
198197
#![feature(try_blocks)]

tests/assembly/closure-inherit-target-feature.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// make sure the feature is not enabled at compile-time
55
//@ compile-flags: -C opt-level=3 -C target-feature=-sse4.1 -C llvm-args=-x86-asm-syntax=intel
66

7-
#![feature(target_feature_11)]
87
#![crate_type = "rlib"]
98

109
use std::arch::x86_64::{__m128, _mm_blend_ps};

tests/codegen/target-feature-inline-closure.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//@ compile-flags: -Copt-level=3 -Ctarget-cpu=x86-64
44

55
#![crate_type = "lib"]
6-
#![feature(target_feature_11)]
76

87
#[cfg(target_arch = "x86_64")]
98
use std::arch::x86_64::*;

tests/mir-opt/inline/inline_compatibility.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#![crate_type = "lib"]
66
#![feature(no_sanitize)]
7-
#![feature(target_feature_11)]
87
#![feature(c_variadic)]
98

109
#[inline]

tests/ui/asm/x86_64/issue-89875.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
//@ needs-asm-support
33
//@ only-x86_64
44

5-
#![feature(target_feature_11)]
6-
75
use std::arch::asm;
86

97
#[target_feature(enable = "avx")]

tests/ui/async-await/async-closures/fn-exception-target-features.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
//@ edition: 2021
22
//@ only-x86_64
33

4-
#![feature(target_feature_11)]
5-
// `target_feature_11` just to test safe functions w/ target features.
6-
7-
use std::pin::Pin;
84
use std::future::Future;
5+
use std::pin::Pin;
96

107
#[target_feature(enable = "sse2")]
11-
fn target_feature() -> Pin<Box<dyn Future<Output = ()> + 'static>> { todo!() }
8+
fn target_feature() -> Pin<Box<dyn Future<Output = ()> + 'static>> {
9+
todo!()
10+
}
1211

1312
fn test(f: impl AsyncFn()) {}
1413

tests/ui/async-await/async-closures/fn-exception-target-features.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0277]: the trait bound `#[target_features] fn() -> Pin<Box<(dyn Future<Output = ()> + 'static)>> {target_feature}: AsyncFn()` is not satisfied
2-
--> $DIR/fn-exception-target-features.rs:16:10
2+
--> $DIR/fn-exception-target-features.rs:15:10
33
|
44
LL | test(target_feature);
55
| ---- ^^^^^^^^^^^^^^ unsatisfied trait bound
@@ -8,7 +8,7 @@ LL | test(target_feature);
88
|
99
= help: the trait `AsyncFn()` is not implemented for fn item `#[target_features] fn() -> Pin<Box<(dyn Future<Output = ()> + 'static)>> {target_feature}`
1010
note: required by a bound in `test`
11-
--> $DIR/fn-exception-target-features.rs:13:17
11+
--> $DIR/fn-exception-target-features.rs:12:17
1212
|
1313
LL | fn test(f: impl AsyncFn()) {}
1414
| ^^^^^^^^^ required by this bound in `test`

0 commit comments

Comments
 (0)