Skip to content

Commit 032dfe4

Browse files
committed
Auto merge of rust-lang#89167 - workingjubilee:use-simd, r=MarkSimulacrum
pub use core::simd; A portable abstraction over SIMD has been a major pursuit in recent years for several programming languages. In Rust, `std::arch` offers explicit SIMD acceleration via compiler intrinsics, but it does so at the cost of having to individually maintain each and every single such API, and is almost completely `unsafe` to use. `core::simd` offers safe abstractions that are resolved to the appropriate SIMD instructions by LLVM during compilation, including scalar instructions if that is all that is available. `core::simd` is enabled by the `#![portable_simd]` nightly feature tracked in rust-lang#86656 and is introduced here by pulling in the https://github.com/rust-lang/portable-simd repository as a subtree. We built the repository out-of-tree to allow faster compilation and a stochastic test suite backed by the proptest crate to verify that different targets, features, and optimizations produce the same result, so that using this library does not introduce any surprises. As these tests are technically non-deterministic, and thus can introduce overly interesting Heisenbugs if included in the rustc CI, they are visible in the commit history of the subtree but do nothing here. Some tests **are** introduced via the documentation, but these use deterministic asserts. There are multiple unsolved problems with the library at the current moment, including a want for better documentation, technical issues with LLVM scalarizing and lowering to libm, room for improvement for the APIs, and so far I have not added the necessary plumbing for allowing the more experimental or libm-dependent APIs to be used. However, I thought it would be prudent to open this for review in its current condition, as it is both usable and it is likely I am going to learn something else needs to be fixed when bors tries this out. The major types are - `core::simd::Simd<T, N>` - `core::simd::Mask<T, N>` There is also the `LaneCount` struct, which, together with the SimdElement and SupportedLaneCount traits, limit the implementation's maximum support to vectors we know will actually compile and provide supporting logic for bitmasks. I'm hoping to simplify at least some of these out of the way as the compiler and library evolve.
2 parents e90c5fb + 7c3d72d commit 032dfe4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+7631
-0
lines changed

library/core/src/lib.rs

+21
Original file line numberDiff line numberDiff line change
@@ -392,4 +392,25 @@ pub mod arch {
392392
}
393393
}
394394

395+
// Pull in the `core_simd` crate directly into libcore. The contents of
396+
// `core_simd` are in a different repository: rust-lang/portable-simd.
397+
//
398+
// `core_simd` depends on libcore, but the contents of this module are
399+
// set up in such a way that directly pulling it here works such that the
400+
// crate uses this crate as its libcore.
401+
#[path = "../../portable-simd/crates/core_simd/src/mod.rs"]
402+
#[allow(missing_debug_implementations, dead_code, unsafe_op_in_unsafe_fn, unused_unsafe)]
403+
#[allow(rustdoc::bare_urls)]
404+
#[unstable(feature = "portable_simd", issue = "86656")]
405+
#[cfg(not(bootstrap))]
406+
mod core_simd;
407+
408+
#[doc = include_str!("../../portable-simd/crates/core_simd/src/core_simd_docs.md")]
409+
#[unstable(feature = "portable_simd", issue = "86656")]
410+
#[cfg(not(bootstrap))]
411+
pub mod simd {
412+
#[unstable(feature = "portable_simd", issue = "86656")]
413+
pub use crate::core_simd::simd::*;
414+
}
415+
395416
include!("primitive_docs.rs");

library/core/tests/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#![feature(never_type)]
6161
#![feature(unwrap_infallible)]
6262
#![feature(result_into_ok_or_err)]
63+
#![cfg_attr(not(bootstrap), feature(portable_simd))]
6364
#![feature(ptr_metadata)]
6465
#![feature(once_cell)]
6566
#![feature(unsized_tuple_coercion)]
@@ -105,6 +106,8 @@ mod pattern;
105106
mod pin;
106107
mod ptr;
107108
mod result;
109+
#[cfg(not(bootstrap))]
110+
mod simd;
108111
mod slice;
109112
mod str;
110113
mod str_lossy;

library/core/tests/simd.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use core::simd::f32x4;
2+
3+
#[test]
4+
fn testing() {
5+
let x = f32x4::from_array([1.0, 1.0, 1.0, 1.0]);
6+
let y = -x;
7+
8+
let h = x * 0.5;
9+
10+
let r = y.abs();
11+
assert_eq!(x, r);
12+
assert_eq!(h, f32x4::splat(0.5));
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
name: Blank Issue
3+
about: Create a blank issue.
4+
---
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
name: Bug Report
3+
about: Create a bug report for Rust.
4+
labels: C-bug
5+
---
6+
<!--
7+
Thank you for filing a bug report! 🐛 Please provide a short summary of the bug,
8+
along with any information you feel relevant to replicating the bug.
9+
-->
10+
11+
I tried this code:
12+
13+
```rust
14+
<code>
15+
```
16+
17+
I expected to see this happen: *explanation*
18+
19+
Instead, this happened: *explanation*
20+
21+
### Meta
22+
23+
`rustc --version --verbose`:
24+
```
25+
<version>
26+
```
27+
28+
29+
`crate version in Cargo.toml`:
30+
```toml
31+
[dependencies]
32+
stdsimd =
33+
```
34+
<!-- If this specifies the repo at HEAD, please include the latest commit. -->
35+
36+
37+
<!--
38+
If a backtrace is available, please include a backtrace in the code block by
39+
setting `RUST_BACKTRACE=1` in your environment. e.g.
40+
`RUST_BACKTRACE=1 cargo build`.
41+
-->
42+
<details><summary>Backtrace</summary>
43+
<p>
44+
45+
```
46+
<backtrace>
47+
```
48+
49+
</p>
50+
</details>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This only controls whether a tiny, hard-to-find "open a blank issue" link appears at the end of
2+
# the template list.
3+
blank_issues_enabled: true
4+
contact_links:
5+
- name: Intrinsic Support
6+
url: https://github.com/rust-lang/stdarch/issues
7+
about: Please direct issues about Rust's support for vendor intrinsics to core::arch
8+
- name: Internal Compiler Error
9+
url: https://github.com/rust-lang/rust/issues
10+
about: Please report ICEs to the rustc repository
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
name: Feature Request
3+
about: Request an addition to the core::simd API
4+
labels: C-feature-request
5+
---
6+
<!--
7+
Hello!
8+
9+
We are very interested in any feature requests you may have.
10+
11+
However, please be aware that core::simd exists to address concerns with creating a portable SIMD API for Rust.
12+
Requests for extensions to compiler features, such as `target_feature`, binary versioning for SIMD APIs, or
13+
improving specific compilation issues in general should be discussed at https://internals.rust-lang.org/
14+
-->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Hello, welcome to `std::simd`!
2+
3+
It seems this pull request template checklist was created while a lot of vector math ops were being implemented, and only really applies to ops. Feel free to delete everything here if it's not applicable, or ask for help if you're not sure what it means!
4+
5+
For a given vector math operation on TxN, please add tests for interactions with:
6+
- [ ] `T::MAX`
7+
- [ ] `T::MIN`
8+
- [ ] -1
9+
- [ ] 1
10+
- [ ] 0
11+
12+
13+
For a given vector math operation on TxN where T is a float, please add tests for test interactions with:
14+
- [ ] a really large number, larger than the mantissa
15+
- [ ] a really small "subnormal" number
16+
- [ ] NaN
17+
- [ ] Infinity
18+
- [ ] Negative Infinity

0 commit comments

Comments
 (0)