Auto merge of #94579 - tmiasko:target-features, r=nagisa
Always include global target features in function attributes
This ensures that information about target features configured with
`-C target-feature=...` or detected with `-C target-cpu=native` is
retained for subsequent consumers of LLVM bitcode.
This is crucial for linker plugin LTO, since this information is not
conveyed to the plugin otherwise.
<details><summary>Additional test case demonstrating the issue</summary>
```rust
extern crate core;
#[inline]
#[target_feature(enable = "aes")]
unsafe fn f(a: u128, b: u128) -> u128 {
use core::arch::x86_64::*;
use core::mem::transmute;
transmute(_mm_aesenc_si128(transmute(a), transmute(b)))
}
pub fn g(a: u128, b: u128) -> u128 {
unsafe { f(a, b) }
}
fn main() {
let mut args = std::env::args();
let _ = args.next().unwrap();
let a: u128 = args.next().unwrap().parse().unwrap();
let b: u128 = args.next().unwrap().parse().unwrap();
println!("{}", g(a, b));
}
```
```console
$ rustc --edition=2021 a.rs -Clinker-plugin-lto -Clink-arg=-fuse-ld=lld -Ctarget-feature=+aes -O
...
= note: LLVM ERROR: Cannot select: intrinsic %llvm.x86.aesni.aesenc
```
</details>
r? `@nagisa`
This commit is contained in:
@@ -80,6 +80,7 @@ pub mod obligation_forest;
|
||||
pub mod owning_ref;
|
||||
pub mod sip128;
|
||||
pub mod small_c_str;
|
||||
pub mod small_str;
|
||||
pub mod snapshot_map;
|
||||
pub mod stable_map;
|
||||
pub mod svh;
|
||||
|
||||
@@ -62,6 +62,7 @@ impl SmallCStr {
|
||||
impl Deref for SmallCStr {
|
||||
type Target = ffi::CStr;
|
||||
|
||||
#[inline]
|
||||
fn deref(&self) -> &ffi::CStr {
|
||||
self.as_c_str()
|
||||
}
|
||||
|
||||
68
compiler/rustc_data_structures/src/small_str.rs
Normal file
68
compiler/rustc_data_structures/src/small_str.rs
Normal file
@@ -0,0 +1,68 @@
|
||||
use smallvec::SmallVec;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
/// Like SmallVec but for strings.
|
||||
#[derive(Default)]
|
||||
pub struct SmallStr<const N: usize>(SmallVec<[u8; N]>);
|
||||
|
||||
impl<const N: usize> SmallStr<N> {
|
||||
#[inline]
|
||||
pub fn new() -> Self {
|
||||
SmallStr(SmallVec::default())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn push_str(&mut self, s: &str) {
|
||||
self.0.extend_from_slice(s.as_bytes());
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn empty(&self) -> bool {
|
||||
self.0.is_empty()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn spilled(&self) -> bool {
|
||||
self.0.spilled()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn as_str(&self) -> &str {
|
||||
unsafe { std::str::from_utf8_unchecked(self.0.as_slice()) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> std::ops::Deref for SmallStr<N> {
|
||||
type Target = str;
|
||||
|
||||
#[inline]
|
||||
fn deref(&self) -> &str {
|
||||
self.as_str()
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize, A: AsRef<str>> FromIterator<A> for SmallStr<N> {
|
||||
#[inline]
|
||||
fn from_iter<T>(iter: T) -> Self
|
||||
where
|
||||
T: IntoIterator<Item = A>,
|
||||
{
|
||||
let mut s = SmallStr::default();
|
||||
s.extend(iter);
|
||||
s
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize, A: AsRef<str>> Extend<A> for SmallStr<N> {
|
||||
#[inline]
|
||||
fn extend<T>(&mut self, iter: T)
|
||||
where
|
||||
T: IntoIterator<Item = A>,
|
||||
{
|
||||
for a in iter.into_iter() {
|
||||
self.push_str(a.as_ref());
|
||||
}
|
||||
}
|
||||
}
|
||||
20
compiler/rustc_data_structures/src/small_str/tests.rs
Normal file
20
compiler/rustc_data_structures/src/small_str/tests.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn empty() {
|
||||
let s = SmallStr::<1>::new();
|
||||
assert!(s.empty());
|
||||
assert_eq!("", s.as_str());
|
||||
assert!(!s.spilled());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_iter() {
|
||||
let s = ["aa", "bb", "cc"].iter().collect::<SmallStr<6>>();
|
||||
assert_eq!("aabbcc", s.as_str());
|
||||
assert!(!s.spilled());
|
||||
|
||||
let s = ["aa", "bb", "cc", "dd"].iter().collect::<SmallStr<6>>();
|
||||
assert_eq!("aabbccdd", s.as_str());
|
||||
assert!(s.spilled());
|
||||
}
|
||||
Reference in New Issue
Block a user