Skip to content

Tracking issue for SIMD support #27731

Closed

Description

@alexcrichton

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

Activity

  1. added
    T-libs-api[DEPRECATED; DO NOT USE]
    B-unstableBlocker: Implemented in the nightly compiler and unstable.
    on Aug 12, 2015
  2. pnkfelix commented on Oct 6, 2015

    @pnkfelix
    Contributor

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

  3. aturon commented on Nov 5, 2015

    @aturon
    Contributor

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

  4. huonw commented on Dec 11, 2015

    @huonw
    Contributor

    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.

  5. bstrie commented on Mar 29, 2016

    @bstrie
    Contributor

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

  6. alexcrichton commented on Jun 17, 2016

    @alexcrichton
    MemberAuthor

    @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!

  7. eddyb commented on Jun 17, 2016

    @eddyb
    Contributor

    @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.

  8. alexcrichton commented on Jun 17, 2016

    @alexcrichton
    MemberAuthor

    @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).

  9. BurntSushi commented on Jun 17, 2016

    @BurntSushi
    Member

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

  10. alexcrichton commented on Jun 17, 2016

    @alexcrichton
    MemberAuthor

    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.

  11. nikomatsakis commented on Jun 17, 2016

    @nikomatsakis
    Contributor

    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.)
  12. nikomatsakis commented on Jun 17, 2016

    @nikomatsakis
    Contributor

    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.

  13. ruuda commented on Jun 17, 2016

    @ruuda
    Contributor

    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).

  14. eddyb commented on Jun 17, 2016

    @eddyb
    Contributor

    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.

  15. 60 remaining items

  16. Amanieu commented on Aug 4, 2023

    @Amanieu
    Member

    Zbb doesn't need to be exposed as intrinsics, all of the functionality is already available as methods on plain integer types.

    Crypto intrinsics are fine to add though, but please open a new tracking issue for them when you send a PR.

  17. added a commit that references this issue on Jan 7, 2024
  18. added a commit that references this issue on Jan 9, 2024
  19. added a commit that references this issue on Jan 9, 2024
  20. added a commit that references this issue on Jan 18, 2024
  21. added a commit that references this issue on Feb 5, 2024
  22. Lokathor commented on Feb 5, 2024

    @Lokathor
    Contributor

    @workingjubilee bors has run rampant with power, this issue isn't done.

  23. CryZe commented on Feb 5, 2024

    @CryZe
    Contributor

    The commit explicitly says that this issue got split into various smaller issues. The question is rather where are they?

  24. added a commit that references this issue on Feb 6, 2024
  25. ChrisDenton commented on Feb 8, 2024

    @ChrisDenton
    Member

    The commit explicitly says that this issue got split into various smaller issues. The question is rather where are they?

    Presumably: https://github.com/rust-lang/rust/issues?q=is%3Aissue+is%3Aopen++label%3AA-simd+label%3AC-tracking-issue

  26. zachs18 commented on Feb 8, 2024

    @zachs18
  27. Amanieu commented on Feb 11, 2024

    @Amanieu
    Member

    Here is the full set of new tracking issues for what stdsimd was previous tracking:

  28. added a commit that references this issue on Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-SIMDArea: SIMD (Single Instruction Multiple Data)A-target-featureArea: Enabling/disabling target features like AVX, Neon, etc.B-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCI-libs-radarLibs issues that are tracked on the team's radar.T-libs-api[DEPRECATED; DO NOT USE]

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions