Skip to content

LLVM ERROR with avx2 instructions #253

Closed

Description

@Cocalus

when running "cargo test" on the following code with "rustc 1.24.0-nightly (1abeb436d 2017-12-27)" I get
LLVM ERROR: Do not know how to split the result of this operator!

extern crate stdsimd;

use stdsimd::vendor::*;
use stdsimd::simd::u32x8;

//Find minimum val within 3 registers and splat it to a new register
pub fn splat_min(v1: u32x8, v2: u32x8, v3: u32x8) -> u32x8 {
    unsafe {
        //across registers
        let mut min = _mm256_min_epu32(_mm256_min_epu32(v1, v2), v3);

        //within 128 bit halfs
        let mut tmp = _mm256_shuffle_epi32(min.into(), 0x4E).into();
        min = _mm256_min_epu32(min, tmp);
        tmp = _mm256_shuffle_epi32(min.into(), 0xB1).into();
        min = _mm256_min_epu32(min, tmp);


        //across 128 bit halfs
        tmp = _mm256_permute2f128_si256(min.into(), min.into(), 1).into();
        _mm256_min_epu32(min, tmp)
    }
}


#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test() {
        let v1 = u32x8::new(0,1,2,3,4,5,6,7);
        let v2 = u32x8::new(8,9,10,11,12,13,14,15);
        let v3 = u32x8::new(16,17,18,19,20,21,22,23);

        assert_eq!(splat_min(v1,v2,v3), u32x8::splat(0));
    }
}

Activity

  1. AdamNiederer commented on Dec 28, 2017

    @AdamNiederer
    Contributor

    I'm having a similar issue with non-avx2 instructions - I think the issue is caused by calling an intrinsic in an assert! macro, but I've only came across the issue in my doctests.

    See here for another example of this issue.

  2. Cocalus commented on Dec 28, 2017

    @Cocalus
    Author

    I still get the error even if I remove the assert

    #[cfg(test)]
    mod tests {
        use super::*;
        #[test]
        fn test() {
            let v1 = u32x8::new(0,1,2,3,4,5,6,7);
            let v2 = u32x8::new(8,9,10,11,12,13,14,15);
            let v3 = u32x8::new(16,17,18,19,20,21,22,23);
    
            splat_min(v1,v2,v3);
        }
    }
  3. Cocalus commented on Dec 28, 2017

    @Cocalus
    Author

    Here's a smaller example which I copied from the stdsimd test for _mm256_permute2f128_si256

    If get the error with
    cargo run

    but not with
    cargo run --release

    adding
    #[target_feature = "+avx"]
    just before main, also fixes it

    Which mean's it's probably not going to compile to the right instruction anyway. But it would nice if it returned a more useful error if it shouldn't compile.

    extern crate stdsimd;
    
    use stdsimd::vendor::_mm256_permute2f128_si256;
    use stdsimd::simd::i32x8;
    
    fn main() {
        unsafe {
            let a = i32x8::new(1, 2, 3, 4, 1, 2, 3, 4);
            let b = i32x8::new(5, 6, 7, 8, 5, 6, 7, 8);
            let r = _mm256_permute2f128_si256(a, b, 0x20);
            let e = i32x8::new(1, 2, 3, 4, 5, 6, 7, 8);
            assert_eq!(r, e);
        }
    }
  4. gnzlbg commented on Dec 29, 2017

    @gnzlbg
    Contributor

    @Cocalus last example is compiling main with SSE, but unconditionally calling an AVX2 function in it so unless it is compiled with -C target-feature=+avx2 this example has undefined behavior. In --release LLVM probably just deduces that fn main() can only run in AVX2 targets (it's UB otherwise) and just compiles it with AVX2 (which will work on AVX2 hosts, and crash otherwise).

    In debug mode, main is compiled with SSE, and because i32x8 has a different ABI in SSE than in AVX2 this should trigger: rust-lang/rust#44367 , why it triggers the LLVM error instead, I don't know (sometimes this works and crashes, sometimes it doesn't compile; once the rust issue is fixed the ABIs will be automatically converted).

    However, I don't understand why using #[target_feature = "+avx"] fn main() { ... } fixes this issue in debug builds. It should not: i32x8 also has a different ABI in AVX than in AVX2, which should again trigger: rust-lang/rust#44367

    @alexcrichton ?

  5. Cocalus commented on Dec 29, 2017

    @Cocalus
    Author

    I was wondering why all the vendor functions were marked unsafe, I guess the requirement on the programmer is that they are only used a section of code where the relevant target features are enabled. It seems like it should be possible to statically check that _mm256_permute2f128_si256 is only called in a an avx enabled section of code, then a lot of unsafe blocks could be avoided. I'm might be missing other reasons the vendor functions are tagged as unsafe.

    In my original code I had the splat_min as an inner function and I expected the target_feature to apply to everything within the function, including inner functions and closures. Which is apparently not the case. I haven't figured out the incantation to get closures to work. Though this could also be related to repr(simd) issue.

    Using cargo run on this only produces the right answer for good. Notable there is no unsafe here.

    #![feature(target_feature)]
    
    extern crate stdsimd;
    
    use stdsimd::vendor::*;
    use stdsimd::simd::i32x8;
    
    #[target_feature = "+avx,+avx2"]
    fn main() {
        let x = i32x8::splat(0);
        let y = i32x8::splat(1);
    
        let bad = |a,b| {a+b};
    
        fn alsobad(a: i32x8, b: i32x8) -> i32x8{
            a+b
        }
    
        #[target_feature = "+avx,+avx2"]
        fn good(a: i32x8, b: i32x8) -> i32x8{
            a+b
        }
    
        #[target_feature = "+avx,+avx2"]
        let stillbad = |a,b| {a+b};
    
    
        let alsostillbad = |a,b| {
            #[target_feature = "+avx,+avx2"]
            {a+b}
        };
    
    
        println!("{:?}", bad(x,y));
        println!("{:?}", alsobad(x,y));
        println!("{:?}", good(x,y));
        println!("{:?}", stillbad(x,y));
        println!("{:?}", alsostillbad(x,y));
    }
  6. alexcrichton commented on Dec 29, 2017

    @alexcrichton
    Member

    In general these are probably either LLVM bugs or otherwise consequences of rust-lang/rust#44367 where we're mismatching enabled target features per function.

  7. gnzlbg commented on Dec 29, 2017

    @gnzlbg
    Contributor

    It seems like it should be possible to statically check that _mm256_permute2f128_si256 is only called in a an avx enabled section of code, then a lot of unsafe blocks could be avoided.

    @Cocalus There is an RFC for that: rust-lang/rfcs#2212

  8. alexcrichton commented on Jan 29, 2018

    @alexcrichton
    Member

    I've opened #306 to close out this issue as my suspected cause, rust-lang/rust#44367 , has since been fixed and it unfortunately didn't fix this issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions