1
Fork 0
rust/compiler/rustc_passes/src
bors acb4e8b625 Auto merge of #127537 - veluca93:struct_tf, r=BoxyUwU
Implement a first version of RFC 3525: struct target features

This PR is an attempt at implementing https://github.com/rust-lang/rfcs/pull/3525, behind a feature gate `struct_target_features`.

There's obviously a few tasks that ought to be done before this is merged; in no particular order:
- add proper error messages
- add tests
- create a tracking issue for the RFC
- properly serialize/deserialize the new target_features field in `rmeta` (assuming I even understood that correctly :-))

That said, as I am definitely not a `rustc` expert, I'd like to get some early feedback on the overall approach before fixing those things (and perhaps some pointers for `rmeta`...), hence this early PR :-)

Here's an example piece of code that I have been using for testing - with the new code, the calls to intrinsics get correctly inlined:
```rust
#![feature(struct_target_features)]

use std::arch::x86_64::*;

/*
// fails to compile
#[target_feature(enable = "avx")]
struct Invalid(u32);
*/

#[target_feature(enable = "avx")]
struct Avx {}

#[target_feature(enable = "sse")]
struct Sse();

/*
// fails to compile
extern "C" fn bad_fun(_: Avx) {}
*/

/*
// fails to compile
#[inline(always)]
fn inline_fun(_: Avx) {}
*/

trait Simd {
    fn do_something(&self);
}

impl Simd for Avx {
    fn do_something(&self) {
        unsafe {
            println!("{:?}", _mm256_setzero_ps());
        }
    }
}

impl Simd for Sse {
    fn do_something(&self) {
        unsafe {
            println!("{:?}", _mm_setzero_ps());
        }
    }
}

struct WithAvx {
    #[allow(dead_code)]
    avx: Avx,
}

impl Simd for WithAvx {
    fn do_something(&self) {
        unsafe {
            println!("{:?}", _mm256_setzero_ps());
        }
    }
}

#[inline(never)]
fn dosomething<S: Simd>(simd: &S) {
    simd.do_something();
}

fn main() {
    /*
    // fails to compile
    Avx {};
    */

    if is_x86_feature_detected!("avx") {
        let avx = unsafe { Avx {} };
        dosomething(&avx);
        dosomething(&WithAvx { avx });
    }
    if is_x86_feature_detected!("sse") {
        dosomething(&unsafe { Sse {} })
    }
}
```

Tracking:

- https://github.com/rust-lang/rust/issues/129107
2024-08-28 22:54:55 +00:00
..
liveness Reformat use declarations. 2024-07-29 08:26:52 +10:00
abi_test.rs Shrink TyKind::FnPtr. 2024-08-09 14:33:25 +10:00
check_attr.rs Auto merge of #127537 - veluca93:struct_tf, r=BoxyUwU 2024-08-28 22:54:55 +00:00
check_const.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
dead.rs Revert "Rollup merge of #125572 - mu001999-contrib:dead/enhance, r=pnkfelix" 2024-08-03 07:57:31 -04:00
debugger_visualizer.rs Use more slice patterns inside the compiler 2024-08-07 13:37:52 +02:00
diagnostic_items.rs Use bool in favor of Option<()> for diagnostics 2024-08-21 01:31:11 -04:00
entry.rs rustc_passes: remove a redundant #[allow(rustc::untranslatable_diagnostic)] 2024-08-10 14:32:56 +03:00
errors.rs Implement RFC 3525. 2024-08-28 09:54:23 +02:00
hir_id_validator.rs Actually create ranged int types in the type system. 2024-04-08 12:02:19 +00:00
hir_stats.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
lang_items.rs Use cnum for extern crate data 2024-08-17 12:50:18 -04:00
layout_test.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
lib.rs Use tidy to sort crate attributes for all compiler crates. 2024-06-12 15:49:10 +10:00
lib_features.rs Remove Session methods that duplicate DiagCtxt methods. 2023-12-24 08:05:28 +11:00
liveness.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
loops.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
naked_functions.rs Rollup merge of #127853 - folkertdev:naked-function-error-messages, r=bjorn3 2024-07-28 08:57:16 +02:00
reachable.rs Miri function identity hack: account for possible inlining 2024-07-02 21:05:30 +02:00
stability.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
upvars.rs Make body_owned_by return the body directly. 2024-05-29 10:04:08 +00:00
weak_lang_items.rs Revert "Rollup merge of #124976 - petrochenkov:usedcrates, r=oli-obk" 2024-06-06 10:06:28 +00:00