Skip to content

Commit

Permalink
Auto merge of rust-lang#89167 - workingjubilee:use-simd, r=MarkSimula…
Browse files Browse the repository at this point in the history
…crum

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.
  • Loading branch information
bors committed Nov 13, 2021
2 parents e90c5fb + 7c3d72d commit 032dfe4
Show file tree
Hide file tree
Showing 89 changed files with 7,631 additions and 0 deletions.
21 changes: 21 additions & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,25 @@ pub mod arch {
}
}

// Pull in the `core_simd` crate directly into libcore. The contents of
// `core_simd` are in a different repository: rust-lang/portable-simd.
//
// `core_simd` depends on libcore, but the contents of this module are
// set up in such a way that directly pulling it here works such that the
// crate uses this crate as its libcore.
#[path = "../../portable-simd/crates/core_simd/src/mod.rs"]
#[allow(missing_debug_implementations, dead_code, unsafe_op_in_unsafe_fn, unused_unsafe)]
#[allow(rustdoc::bare_urls)]
#[unstable(feature = "portable_simd", issue = "86656")]
#[cfg(not(bootstrap))]
mod core_simd;

#[doc = include_str!("../../portable-simd/crates/core_simd/src/core_simd_docs.md")]
#[unstable(feature = "portable_simd", issue = "86656")]
#[cfg(not(bootstrap))]
pub mod simd {
#[unstable(feature = "portable_simd", issue = "86656")]
pub use crate::core_simd::simd::*;
}

include!("primitive_docs.rs");
3 changes: 3 additions & 0 deletions library/core/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#![feature(never_type)]
#![feature(unwrap_infallible)]
#![feature(result_into_ok_or_err)]
#![cfg_attr(not(bootstrap), feature(portable_simd))]
#![feature(ptr_metadata)]
#![feature(once_cell)]
#![feature(unsized_tuple_coercion)]
Expand Down Expand Up @@ -105,6 +106,8 @@ mod pattern;
mod pin;
mod ptr;
mod result;
#[cfg(not(bootstrap))]
mod simd;
mod slice;
mod str;
mod str_lossy;
Expand Down
13 changes: 13 additions & 0 deletions library/core/tests/simd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use core::simd::f32x4;

#[test]
fn testing() {
let x = f32x4::from_array([1.0, 1.0, 1.0, 1.0]);
let y = -x;

let h = x * 0.5;

let r = y.abs();
assert_eq!(x, r);
assert_eq!(h, f32x4::splat(0.5));
}
4 changes: 4 additions & 0 deletions library/portable-simd/.github/ISSUE_TEMPLATE/blank_issue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
name: Blank Issue
about: Create a blank issue.
---
50 changes: 50 additions & 0 deletions library/portable-simd/.github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
name: Bug Report
about: Create a bug report for Rust.
labels: C-bug
---
<!--
Thank you for filing a bug report! 🐛 Please provide a short summary of the bug,
along with any information you feel relevant to replicating the bug.
-->

I tried this code:

```rust
<code>
```

I expected to see this happen: *explanation*

Instead, this happened: *explanation*

### Meta

`rustc --version --verbose`:
```
<version>
```


`crate version in Cargo.toml`:
```toml
[dependencies]
stdsimd =
```
<!-- If this specifies the repo at HEAD, please include the latest commit. -->


<!--
If a backtrace is available, please include a backtrace in the code block by
setting `RUST_BACKTRACE=1` in your environment. e.g.
`RUST_BACKTRACE=1 cargo build`.
-->
<details><summary>Backtrace</summary>
<p>

```
<backtrace>
```

</p>
</details>
10 changes: 10 additions & 0 deletions library/portable-simd/.github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This only controls whether a tiny, hard-to-find "open a blank issue" link appears at the end of
# the template list.
blank_issues_enabled: true
contact_links:
- name: Intrinsic Support
url: https://github.com/rust-lang/stdarch/issues
about: Please direct issues about Rust's support for vendor intrinsics to core::arch
- name: Internal Compiler Error
url: https://github.com/rust-lang/rust/issues
about: Please report ICEs to the rustc repository
14 changes: 14 additions & 0 deletions library/portable-simd/.github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
name: Feature Request
about: Request an addition to the core::simd API
labels: C-feature-request
---
<!--
Hello!
We are very interested in any feature requests you may have.
However, please be aware that core::simd exists to address concerns with creating a portable SIMD API for Rust.
Requests for extensions to compiler features, such as `target_feature`, binary versioning for SIMD APIs, or
improving specific compilation issues in general should be discussed at https://internals.rust-lang.org/
-->
18 changes: 18 additions & 0 deletions library/portable-simd/.github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Hello, welcome to `std::simd`!

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!

For a given vector math operation on TxN, please add tests for interactions with:
- [ ] `T::MAX`
- [ ] `T::MIN`
- [ ] -1
- [ ] 1
- [ ] 0


For a given vector math operation on TxN where T is a float, please add tests for test interactions with:
- [ ] a really large number, larger than the mantissa
- [ ] a really small "subnormal" number
- [ ] NaN
- [ ] Infinity
- [ ] Negative Infinity

0 comments on commit 032dfe4

Please sign in to comment.