rust/tests/ui/asm/x86_64
Jacob Pratt 575405161f
Rollup merge of #134090 - veluca93:stable-tf11, r=oli-obk
Stabilize target_feature_11

# Stabilization report

This is an updated version of https://github.com/rust-lang/rust/pull/116114, which is itself a redo of https://github.com/rust-lang/rust/pull/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 https://github.com/rust-lang/rust/pull/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/`](f6cb952dc1/tests/ui/target-feature).

## Edge cases
### Closures
 * [target-feature 1.1: should closures inherit target-feature annotations? #73631](https://github.com/rust-lang/rust/issues/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. 2e29bdf908/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](https://github.com/rust-lang/rust/pull/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](https://github.com/rust-lang/rust/issues/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](https://github.com/rust-lang/rust/issues/108645)
* [`#[target_feature]` is allowed on default implementations #108646](https://github.com/rust-lang/rust/issues/108646)
* [#[target_feature] is allowed on #[panic_handler] with target_feature 1.1 #109411](https://github.com/rust-lang/rust/issues/109411)
* [Prevent using `#[target_feature]` on lang item functions #115910](https://github.com/rust-lang/rust/pull/115910)

## Documentation
 * Reference: [Document the `target_feature_11` feature reference#1181](https://github.com/rust-lang/reference/pull/1181)
---

cc tracking issue https://github.com/rust-lang/rust/issues/69098
cc ```@workingjubilee```
cc ```@RalfJung```
r? ```@rust-lang/lang```
2025-02-12 20:09:56 -05:00
..
bad-clobber-abi.rs [AUTO-GENERATED] Migrate ui tests from `//` to `//@` directives 2024-02-16 20:02:50 +00:00
bad-clobber-abi.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
bad-options.rs improve error message when `global_asm!` uses `asm!` options 2024-07-25 22:33:52 +02:00
bad-options.stderr improve error message when `global_asm!` uses `asm!` options 2024-07-25 22:33:52 +02:00
bad-reg.rs stabilize `asm_const` 2024-08-13 23:18:31 +02:00
bad-reg.stderr show supported register classes 2025-01-29 12:15:12 +01:00
const.rs stabilize `asm_const` 2024-08-13 23:18:31 +02:00
duplicate-options.fixed [AUTO-GENERATED] Migrate ui tests from `//` to `//@` directives 2024-02-16 20:02:50 +00:00
duplicate-options.rs [AUTO-GENERATED] Migrate ui tests from `//` to `//@` directives 2024-02-16 20:02:50 +00:00
duplicate-options.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
evex512-implicit-feature.rs [AUTO-GENERATED] Migrate ui tests from `//` to `//@` directives 2024-02-16 20:02:50 +00:00
goto-block-safe.rs Make asm label blocks safe context 2024-10-11 11:54:30 +01:00
goto-block-safe.stderr Make asm label blocks safe context 2024-10-11 11:54:30 +01:00
goto.rs Make asm_goto_with_outputs a separate feature gate 2024-11-24 15:24:01 +00:00
goto.stderr Support use of asm goto with outputs and `options(noreturn)` 2024-11-24 14:18:10 +00:00
interpolated-idents.rs [AUTO-GENERATED] Migrate ui tests from `//` to `//@` directives 2024-02-16 20:02:50 +00:00
interpolated-idents.stderr Fix duplicate labels emitted in `render_multispan_macro_backtrace()` 2023-10-19 08:03:40 +05:30
issue-82869.rs [AUTO-GENERATED] Migrate ui tests from `//` to `//@` directives 2024-02-16 20:02:50 +00:00
issue-82869.stderr show supported register classes 2025-01-29 12:15:12 +01:00
issue-89875.rs Stabilize target_feature_11 2025-01-27 23:44:47 +01:00
issue-96797.rs [AUTO-GENERATED] Migrate ui tests from `//` to `//@` directives 2024-02-16 20:02:50 +00:00
may_unwind.rs Remove c_unwind from tests and fix tests 2024-06-19 13:54:55 +01:00
multiple-clobber-abi.rs [AUTO-GENERATED] Migrate ui tests from `//` to `//@` directives 2024-02-16 20:02:50 +00:00
srcloc.new.stderr Pass end position of span through inline ASM cookie 2024-11-26 13:00:08 +00:00
srcloc.old.stderr Pass end position of span through inline ASM cookie 2024-11-26 13:00:08 +00:00
srcloc.rs Pass end position of span through inline ASM cookie 2024-11-26 13:00:08 +00:00
sym.rs [AUTO-GENERATED] Migrate ui tests from `//` to `//@` directives 2024-02-16 20:02:50 +00:00
target-feature-attr.rs Add inline comments why we're forcing the target cpu 2024-05-01 16:54:20 -07:00
target-feature-attr.stderr Add inline comments why we're forcing the target cpu 2024-05-01 16:54:20 -07:00
type-check-2.rs Ban non-array SIMD 2024-09-09 19:39:43 -07:00
type-check-2.stderr Ban non-array SIMD 2024-09-09 19:39:43 -07:00
type-check-3.rs stabilize `asm_const` 2024-08-13 23:18:31 +02:00
type-check-3.stderr stabilize `asm_const` 2024-08-13 23:18:31 +02:00
type-check-5.rs Ban non-array SIMD 2024-09-09 19:39:43 -07:00
type-check-5.stderr Ban non-array SIMD 2024-09-09 19:39:43 -07:00
x86_64_parse_error.rs stabilize `asm_const` 2024-08-13 23:18:31 +02:00
x86_64_parse_error.stderr Show diff suggestion format on verbose replacement 2025-02-10 20:21:39 +00:00