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 SIMD support #27731

Open
alexcrichton opened this Issue Aug 12, 2015 · 24 comments

Comments

Projects
None yet
@alexcrichton
Copy link
Member

alexcrichton commented Aug 12, 2015

This is a tracking issue for the unstable core_simd feature in the standard library. SIMD support is quite a thorny topic, but it's largely covered in rust-lang/rfcs#1199, being implemented in #27169, and @huonw will be creating an external crate for full-fledged SIMD support.

cc @huonw

@pnkfelix

This comment has been minimized.

Copy link
Member

pnkfelix commented Oct 6, 2015

Note that #26403 may well be a blocker issue for 100% safe + composable SIMD

@aturon

This comment has been minimized.

Copy link
Member

aturon commented Nov 5, 2015

This issue now tracks the simd, simd_ffi, and repr_simd features.

@huonw

This comment has been minimized.

Copy link
Member

huonw commented Dec 11, 2015

Status update:

In the compiler:

  • there is support for a lot of the intel x86/x86-64 intrinsics, but none of the AMD-specific ones
  • it also has pretty much all the ARM & AArch64 NEON ones
  • other targets have essentially no support
  • some of the fanciest intrinsics are missing, especially (IIRC) some of the ARM pointer ones
  • the cfg(target_feature = "...") detection is subpar, e.g. it doesn't detect features when an explicit -C target-cpu="..." is set, it doesn't handle disabling features like -C target-feature="-sse2", nor does it handle (AFAICT) for custom target specs

In https://github.com/huonw/simd:

  • the cross-platform API is mostly there, for x86, ARM and AArch64
  • a small number of platform-intrinsics are exposed, but not a lot
  • the autogenerator (in etc) should be upgraded to handle emitting the actual wrappers as well as the raw extern blocks that it can currently do
  • this probably makes most sense after rewriting it into Rust, as a proper cargo binary checking in Cargo.lock and everything, using e.g. libs from crates.io (this is fine, as the binary is not part of the normal build process, it is only run when someone decides to)

I'm intending to work on the simd crate first, starting with the rewrite of the autogenerator, but I've currently got a thesis to work on.

@bstrie

This comment has been minimized.

Copy link
Contributor

bstrie commented Mar 29, 2016

@huonw How's the thesis going? :) Any progress on this issue, to relay to those interested in SIMD stabilization?

@alexcrichton

This comment has been minimized.

Copy link
Member Author

alexcrichton commented Jun 17, 2016

@BurntSushi, @nikomatsakis, and I talked about this recently at the work week, and our thoughts are:

  • The original strategy of stabilizing the small pieces (intrinsics + #[repr(simd)]) in the compiler is probably the best way forward here. More experimentation can happen in the simd crate, but those are at least the bare bones for moving forward.

  • Specifically with intrinsics, we probably want to stabilize defining them in a different fashion, specifically:

    #[simd_intrinsic = "name"] 
    pub fn foo(a: A, b: B) -> C {
        foo(a, b)
    }

    That is, we'd probably stabilize a new #[simd_intrinsic] (ish) attribute whose name would closely follow the standard naming conventions (e.g. those in C compilers). The function would look like normal Rust and look like it recurses into itself but the compiler would understand that direct calls to the function are actually implemented inline, so this isn't actually infinite recursion.

  • The "structural typing" of the intrinsics is relatively ok. That is, the compiler can verify any definition is correct, even though each definition may be slightly different (e.g. any 32-bit x 4 value could show up perhaps). The bad part, however, is that not all intrinsics can be verified. For example the simd_lt intrinsic can have any number of SIMD types instantiated, but a pairing like String and Vec<u8> would be nonsensical. This may be difficult to solve in a "pure" fashion but may be worth stomaching to stabilize the SIMD intrinsics (which in general should never be called in favor of the simd crate itself)

  • Specifically with #[repr(simd)], we may want to remove support for tagging a generic structure. This does not appear to be used in the simd crate today and may not be necessary at all, in which case it's probably just complications that we don't want to have to think about today.

  • One possible route with the intrinsics would be a thought by @arielb1 where any illegal instantiation of type parameters just causes the intrinsic to codegen as a trap, which means that any instantiation is possible and some would just fail at runtime (which would need to be avoided anyway).

All of this was discussed hopefully with an eye to start the process of stabilization soon-ish, and then we can all get SIMD on stable Rust!

cc @eddyb, you likely have many opinions as well!

@eddyb

This comment has been minimized.

Copy link
Member

eddyb commented Jun 17, 2016

@alexcrichton Ahh, I ignored the multiple-definition option in my recent comment.
I think it's a great solution for integer and floating-point intrinsics, but I didn't consider stabilization of any intrinsic to be possible, hence why I tried to only think of options where libcore hosts all intrinsics.

I am still wary about stabilizing intrinsics, but #[simd_intrinsic] seems focused in scope, so I can see how that works. Although, would it be restricted to things that are definitely about SIMD?
There are various platform intrinsics that don't do anything with vectors, such as prefetch.

Other than that, this seems like a good move forward, without the complexities I was worried about.

@alexcrichton

This comment has been minimized.

Copy link
Member Author

alexcrichton commented Jun 17, 2016

@eddyb hm yeah I'm not sure if #[simd_intrinsic] is the best name, certainly up for debate! I would figure that all intrinsics would be defined through a similar mechanism, but the SIMD intrinsics were namespaced somehow so they're the only ones that we stabilize. I wouldn't want to stabilize, for example, intrinsics like prefetch (for now).

@BurntSushi

This comment has been minimized.

Copy link
Member

BurntSushi commented Jun 17, 2016

There are other useful intrinsics like crc32 that are explicitly part of SSE 4.2 but aren't necessarily SIMD.

@alexcrichton

This comment has been minimized.

Copy link
Member Author

alexcrichton commented Jun 17, 2016

Oh interesting! I'd be ok punting on those for now in favor of just dealing with the SIMD pieces, but we can relatively easily reevaluate to do something different though.

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Jun 17, 2016

So I had a really interesting conversation with @sunfishcode on the topic of SIMD, and in particular the design of SIMD in WASM. The high-level summary was two points:

  1. The current breakdown (platform-specific intrinsics, a portable layer atop) is a good one.
  2. Since we modeled the SIMD crate on the JS SIMD designs, which are now being incorporated into WASM, it will align well with the WASM design. Code that can be expressed in terms of the simd crate will thus also be a good candidate for compiling to WASM.

Some other interesting points that he raised:

  1. SIMD has many audiences with diverse needs, and you can't necessarily accommodate them all very well with just one API:
  • codec authors want the raw intrinsics because they use them in clever and unexpected ways;
  • HPC people want higher-level abstractions but don't need access to every trick in the book;
  • high-performance demands also require raw intrinsics, because they don't mind investing the time to reshape the algorithm for each platform.
  1. One way to support these diverse needs, which has been considered for WASM, is to offer the "union" of features across platforms, but offer a way to query which features are "fast" (the idea is that the "slow" features will be emulated). In Rust I would expect we may want similar things, though perhaps the "slow" paths would just trap? (It's probably a bug if you actually wind up executing one of them.)
@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Jun 17, 2016

On the topic of intrinsics, I feel overall pretty good about some kind of attribute that can be applied to a fn to indicate that the compiler should compile it via pure instructions. Such functions would have to have appropriate argument/return types (roughly like today). If the argument/return types are not generic, this seems very harmless to me, as we can check it purely at the definition site (as @alexcrichton noted).

However, I feel mildly less good about the generic versions, since these cannot be checked until trans time, which means we have to face two annoying choices:

  • issue traps or compilation errors when an intrinsic is used incorrectly;
  • introduce more machinery into the compiler like special SIMD traits.

However, it does seem that there is a third way out: we could remove all support for generic intrinsics, and instead have people define their own traits that map to these operations. For example, today the simd crate does something roughly like this:

#[simd_intrinsic(...)]
fn simd_eq<T,U>(t: T, u: T) -> U;

unsafe trait Simd {
    type EqType;
}

fn generic_eq<T:Simd>(t: T, u: T) -> T::EqType {
    simd_eq(t, t)
}

unsafe impl Simd for u32x4 { ... } // etc

It seems like we could instead do:

trait Simd { // no longer an unsafe trait
    type EqType;

    // we now include a method for the various simd operations we might want to do:
    fn eq(x: &Self, y: &Self) -> Self::EqType;
    ...
}

#[simd_intrinsic]
fn eq_u32x4(x: u32x4, y: u32x4) -> boolx4 {...}

impl Simd for u32x4 {
    #[inline(always)]
    fn eq(x: &Self, y: &Self) -> Self::EqType {
         eq_u32x4(x, y)
    }
}

I'm probably getting some of the details wrong (have to consult the crate for the precise names involved) but hopefully you get the idea. Basically, the compiler only supports monotype intrinsics, and the wrapper crate adds (using normal trait methods) any generic dispatch needed.

@ruuda

This comment has been minimized.

Copy link
Contributor

ruuda commented Jun 17, 2016

The function would look like normal Rust and look like it recurses into itself but the compiler would understand that direct calls to the function are actually implemented inline, so this isn't actually infinite recursion.

Is there a good reason for making the function recurse into itself? It seems like unnecessary repetition to me. Would a macro like intrinsic!(), similar to unreachable!(), be possible?

  • codec authors want the raw intrinsics because they use them in clever and unexpected ways;
  • HPC people want higher-level abstractions but don't need access to every trick in the book;
  • high-performance demands also require raw intrinsics, because they don't mind investing the time to reshape the algorithm for each platform.

I agree. This is one of the papercuts of the current state: most of the platform-specific intrinsics are there with their usual names, except for a few basic arithmetic operations, which are simd_add and such. I think it would be better to expose all of the raw platform intrinsics and build a higher-level cross-platform simd_add on top of that with #[cfg(target_feature)]. A crate like simd could build on top of that by providing fallback (e.g. two SSE adds if AVX is not available). It wouldn’t be generic, but does it need to be? I can’t think of a #[repr(simd)] type that is not just an n-tuple of the scalar type. And for the low-level intrinsics the types have little meaning anyway (e.g. _mm256_cmp_ps returns a vector of floats, but actually they are bitmasks).

@eddyb

This comment has been minimized.

Copy link
Member

eddyb commented Jun 17, 2016

Is there a good reason for making the function recurse into itself?

Maybe it's contrived, but casting the function to a function pointer would naturally give you a pointer to a function which contains the intrinsic operation.

except for a few basic arithmetic operations, which are simd_add and such

There's a very good reason for keeping those that way: they're basic LLVM operations (i.e. simd_add is just the integer/float add you get for + but with vector arguments) and LLVM can optimize them, unlike arbitrary intrinsics, which are function calls and get lowered in target codegen.

@camlorn

This comment has been minimized.

Copy link
Contributor

camlorn commented Oct 3, 2016

Can anyone provide an overview of the status of this? I was talking with someone whose GitHub name I don't know on IRC, and there was some indication that no one is handling further development of this feature. I have enough experience with X86 SIMD that I could probably help.

I like @nikomatsakis approach, except that sometimes you need to be able to treat f32x4 as i32x4 or similar on at least X86. This is because some of the shuffles aren't implemented for f32. If the compiler provides intrinsics for all possible vector types for this case, then it should be fine.

One other possibility that comes to mind now that we're close to it is to finish type-level integers, then make generic intrinsics with declarations like this:

fn simd_mul<T>(v1: T, v2: T) -> T
where std::mem::size_of<T>(): platform_simd_size, std::mem::align_of<T>(): platform_simd_align {
//magic code
}

This of course depends on how close we are to having type-level integers, but it should be checkable well before trans in any sane implementation of type-level integers I can think of. Just a thought.

@eddyb

This comment has been minimized.

Copy link
Member

eddyb commented Oct 3, 2016

This is because some of the shuffles aren't implemented for f32.

LLVM shuffles don't care what the element types are, and neither do the Rust intrinsics exposing them.

@camlorn

This comment has been minimized.

Copy link
Contributor

camlorn commented Oct 3, 2016

@eddyb
People were talking about exposing the platform intrinsics explicitly, which was my point here.

If you drop the cross-platform shuffles in favor of putting it all in a crate and also drop the weird semi-generic nature of the original RFC, this does indeed become a problem.

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Oct 4, 2016

@camlorn afaik, nobody is carrying this forward, but I would very much like to see progress! I still basically stand by my previous comment, though I think @eddyb suggested (perhaps on IRC) the idea of applying the special attribute directly to the method in the impl, and that seems even better (perhaps just making it a lang item -- it would mean though that this lang item can be applied multiple times).

I have no objection to exposing the platform intrinsics explicitly, but it also doesn't seem like a required ingredient. It'd be great to make progress on the wrapper library, and adding in platform-specific names feels orthogonal to me. (Right? This is a bit out of cache.)

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Oct 4, 2016

I'm not exactly sure what's the best next step. Perhaps a new RFC is warranted, just to lay out the plan clearly? At minimum some kind of canonical write-up feels appropriate. Hopefully the changes vis-a-vis today are relatively minimal.

@camlorn

This comment has been minimized.

Copy link
Contributor

camlorn commented Oct 4, 2016

@nikomatsakis
I like the idea of cross platform intrinsics a great deal, and tbh I need to read the whole thread before I'm at full understanding.

It seems to me that you could provide only the platform specific intrinsics, get the optimizer doing a good job with eliminating temporary moves, get type-level integers, and then add a #[inline(force)] that libs can use to make the code efficient.

As I understand it, we almost have type-level integers. And @pcwalton is working on the needed optimizer stuff.

But that said, I have no problem with the original RFC. I started at the bottom of this thread and read up, however, and it seems to me that people are no longer convinced that this is a good way. Perhaps this impression changes once I read the whole thing.

@eddyb

This comment has been minimized.

Copy link
Member

eddyb commented Nov 14, 2016

@BurntSushi I knew I saw something somewhere! See #27731 (comment) above.

@jonathandturner

This comment has been minimized.

Copy link
Contributor

jonathandturner commented Jun 5, 2017

Hate to just jump in out of the blue, but since there hasn't been an update in a while, is there any news on getting simd support in?

@aturon

This comment has been minimized.

Copy link
Member

aturon commented Jun 7, 2017

@jonathandturner No big updates, but @BurntSushi continues to plug away at it. If he follows his typical pattern, one morning he'll show up, open a massive, beautiful PR, and we'll be totally set :-)

@gnzlbg

This comment has been minimized.

Copy link
Contributor

gnzlbg commented Oct 12, 2018

FYI this issue is a bit of a dumping ground for SIMD features that we don't know what to do with yet.

  • simd_ffi, which is being discussed here: #53346
  • repr(simd), which we currently never plan to stabilize, but instead expose it through a std library API like the one proposed in rust-lang/rfcs#2366
  • in stdsimd it tracks the core::arch APIs for platforms that are not available on stable yet (ARM, NVPTX, WASM, etc.)

I don't think it is worth it to clean up this issue. As parts of the above get proposed for stabilization they will get their own tracking issues. The only thing that might be worth doing is splitting repr(simd) into its own feature and making a clear statement that it is not planned for stabilization.

@alexcrichton

This comment has been minimized.

Copy link
Member Author

alexcrichton commented Oct 12, 2018

@gnzlbg want to file some follow-up tracking issues and re-point unstable features to the focused tracking issues? Agreed that this tracking issue isn't really serving much purpose nowadays!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.