Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracking issue for target_feature 1.1 RFC #69098

Open
7 of 10 tasks
nikomatsakis opened this issue Feb 12, 2020 · 18 comments
Open
7 of 10 tasks

Tracking issue for target_feature 1.1 RFC #69098

nikomatsakis opened this issue Feb 12, 2020 · 18 comments
Labels
A-target-feature Area: Enabling/disabling target features like AVX, Neon, etc. B-RFC-implemented Blocker: Approved by a merged RFC and implemented. B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. F-target_feature_11 target feature 1.1 RFC S-tracking-ready-to-stabilize Status: This is ready to stabilize; it may need a stabilization report and a PR T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@nikomatsakis
Copy link
Contributor

nikomatsakis commented Feb 12, 2020

This is a tracking issue for the RFC "target_feature 1.1" (rust-lang/rfcs#2396).

Issues: F-target_feature_11 target feature 1.1 RFC

People

Last updated in Mar 2023:

  • Shepherd: gnzlbg @workingjubilee (person who can help answer tricky questions that arise during implementation)
  • Lang team liaison: @joshtriplett (main point of contact from lang team)

Step

Unresolved questions

@nikomatsakis nikomatsakis added B-RFC-approved Blocker: Approved by a merged RFC but not yet implemented. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Feb 12, 2020
@LeSeulArtichaut
Copy link
Contributor

I've been working on this, trying to see if I would be able to implement this with my very little knowledge of the compiler. I think I can propose a PR soon.

@rustbot claim

@hanna-kruppe
Copy link
Contributor

In #69274 (comment), @petrochenkov pointed out a complication not made clear in the RFC (nor realized by anyone during the original discussion, AFAICT): safe trait functions are explicitly excluded because we can't check all call sites, but function items implement the Fn* traits, so safe functions with target features enabled face a similar problem. That is, unless they are special-cased in some way that results in them not implementing those traits.

@petrochenkov
Copy link
Contributor

petrochenkov commented May 2, 2020

It would probably be better to add the unsafe to #[target_feature] functions implicitly during lowering to HIR.
Implicit unsafe is already added to functions in extern blocks in the same way.

That would make the unsafety checker the only place (besides AST lowering) where they would be treated specially. Like, "yes, the function is unsafe, but we know that it's safe to call in this specific context, so the unsafe block can be omitted".

The special coercion checks would no longer be necessary in that case.

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue May 2, 2020
…r=hanna-kruppe

Implement RFC 2396: `#[target_feature]` 1.1

Tracking issue: rust-lang#69098

r? @nikomatsakis
cc @gnzlbg @joshtriplett
bors added a commit to rust-lang-ci/rust that referenced this issue May 2, 2020
…hanna-kruppe

Implement RFC 2396: `#[target_feature]` 1.1

Tracking issue: rust-lang#69098

r? @nikomatsakis
cc @gnzlbg @joshtriplett
@LeSeulArtichaut
Copy link
Contributor

Now that #69274 landed, I think we can check Implement the RFC 🙂

@LeSeulArtichaut
Copy link
Contributor

@rustbot release-assignment

@hanna-kruppe
Copy link
Contributor

Filed #72012 for the unsoundness discussed above (I think this is how T-lang tracking issues are supposed to be used now).

@nikomatsakis nikomatsakis added the F-target_feature_11 target feature 1.1 RFC label May 8, 2020
@nikomatsakis
Copy link
Contributor Author

Opened #73631 to discuss the expected behavior of closures with target-feature.

@LeSeulArtichaut LeSeulArtichaut added B-RFC-implemented Blocker: Approved by a merged RFC and implemented. B-unstable Blocker: Implemented in the nightly compiler and unstable. and removed B-RFC-approved Blocker: Approved by a merged RFC but not yet implemented. labels Oct 22, 2020
@calebzulawski
Copy link
Member

It looks like there haven't been any updates on this in a while--is there anything I can do to bring this closer to stabilization?

@nikomatsakis
Copy link
Contributor Author

@calebzulawski I know it's almost a year later but I think this is probably ready for a stabilization report? (cc @rust-lang/lang)

@LeSeulArtichaut
Copy link
Contributor

Tried to help by opening rust-lang/reference#1181, feedback from people who actually know what they are doing would be greatly appreciated.

While writing the PR, I noticed that #72012 is a breaking change for wasm targets, where target_features could be used on safe functions. It looks like you can still safely call target_feature functions though:

fn call_it(f: impl FnOnce()) {}

#[target_feature(enable = "simd128")]
fn foo_simd128() {}

fn main() {
    foo_simd128(); // OK
    call_it(foo_simd128); // error: the trait `FnOnce<()>` is not implemented
}

@joshtriplett joshtriplett added the S-tracking-ready-to-stabilize Status: This is ready to stabilize; it may need a stabilization report and a PR label Jun 29, 2022
compiler-errors added a commit to compiler-errors/rust that referenced this issue Feb 26, 2023
…re-11, r=estebank

Stabilize `#![feature(target_feature_11)]`

## Stabilization report

### 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 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();
}
```

### Test cases

Tests for this feature can be found in [`src/test/ui/rfcs/rfc-2396-target_feature-11/`](https://github.com/rust-lang/rust/tree/b67ba9ba208ac918228a18321fc3a11a99b1c62b/src/test/ui/rfcs/rfc-2396-target_feature-11/).

### Edge cases

- rust-lang#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 show that the target-feature is available while the function executes *and* for as long as whatever may escape from that function lives.

### Documentation

- Reference: rust-lang/reference#1181

---
cc tracking issue rust-lang#69098
r? `@ghost`
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Feb 27, 2023
…re-11, r=estebank

Stabilize `#![feature(target_feature_11)]`

## Stabilization report

### 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 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();
}
```

### Test cases

Tests for this feature can be found in [`src/test/ui/rfcs/rfc-2396-target_feature-11/`](https://github.com/rust-lang/rust/tree/b67ba9ba208ac918228a18321fc3a11a99b1c62b/src/test/ui/rfcs/rfc-2396-target_feature-11/).

### Edge cases

- rust-lang#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 show that the target-feature is available while the function executes *and* for as long as whatever may escape from that function lives.

### Documentation

- Reference: rust-lang/reference#1181

---
cc tracking issue rust-lang#69098
r? ``@ghost``
bors added a commit to rust-lang-ci/rust that referenced this issue Feb 28, 2023
…-11, r=estebank

Stabilize `#![feature(target_feature_11)]`

## Stabilization report

### 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 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();
}
```

### Test cases

Tests for this feature can be found in [`src/test/ui/rfcs/rfc-2396-target_feature-11/`](https://github.com/rust-lang/rust/tree/b67ba9ba208ac918228a18321fc3a11a99b1c62b/src/test/ui/rfcs/rfc-2396-target_feature-11/).

### Edge cases

- rust-lang#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 show that the target-feature is available while the function executes *and* for as long as whatever may escape from that function lives.

### Documentation

- Reference: rust-lang/reference#1181

---
cc tracking issue rust-lang#69098
r? `@ghost`
bors added a commit to rust-lang-ci/rust that referenced this issue Mar 2, 2023
…iler-errors

Revert stabilization of `#![feature(target_feature_11)]`

This reverts rust-lang#99767 due to the presence of bugs rust-lang#108645 and rust-lang#108646.

cc `@joshtriplett`
cc tracking issue rust-lang#69098
r? `@ghost`
@workingjubilee
Copy link
Member

workingjubilee commented Mar 2, 2023

An edge case is introduced by the current implementation where old code is now rejected due to the interaction with inline(always):

I intend to do some work on how target features operate and will look into addressing this regression.

@est31
Copy link
Member

est31 commented Mar 2, 2023

Ideally the diagnostics should also be improved. I wouldn't say it's a hard stabilization blocker but it would be nice to have: #108680

@workingjubilee workingjubilee added the A-target-feature Area: Enabling/disabling target features like AVX, Neon, etc. label Mar 3, 2023
@est31
Copy link
Member

est31 commented Sep 24, 2023

The attempts to address #108680 have stalled, so maybe we shouldn't wait on them. With the recent merging of #115910, can we stabilize the feature?

@calebzulawski
Copy link
Member

I had taken a look at #108680 several months ago and I couldn't figure out a straightforward way to plumb the available features for the diagnostic. I agree that it's probably not worth holding up the entire feature.

I checked the F-target_feature_11 label and don't see any new/remaining issues, so I think it should be fine? I opened a new stabilization PR in #116114.

@est31
Copy link
Member

est31 commented Sep 24, 2023

@calebzulawski the plumbing part has been addressed in #109710 . The reasons why the PR is stalled were not due to the plumbing.

@RalfJung
Copy link
Member

RalfJung commented Oct 9, 2023

The internal compiler name for this is rather confusing: target_feature_11 looks like this is version 11 of target_feature, not version 1.1. If any of those identifiers will remain in the compiler after stabilization, can I ask for this to become target_feature_1_1 or so?

@RalfJung
Copy link
Member

This is currently blocked on #116558: even calling a function with fewer target features can be UB since target features affect the ABI of some types.

rust-lang/lang-team#235 proposes a design meeting on how we could resolve that.

@RalfJung
Copy link
Member

RalfJung commented Oct 1, 2024

#131058 is another potential blocker -- this code is unsound on an aarch64-softfloat target:

#[target_feature(enable = "neon")]
pub fn square(num: f32) -> f32 {
    num * num
}

fn main() {
  if is_aarch64_feature_detected!("neon") {
    // The argument will be passed in general-purpose registers,
    // but the callee expects the argument in a float register.
    println!("{}", unsafe { square(42.0) });
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-target-feature Area: Enabling/disabling target features like AVX, Neon, etc. B-RFC-implemented Blocker: Approved by a merged RFC and implemented. B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. F-target_feature_11 target feature 1.1 RFC S-tracking-ready-to-stabilize Status: This is ready to stabilize; it may need a stabilization report and a PR T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

10 participants