Skip to content

Release builds using AVX code produce incorrect output #54583

Description

@vladglv

Code

#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;

fn main() {
    unsafe {
        let f = _mm256_set_pd(2.0, 2.0, 2.0, 2.0);
        let r = _mm256_mul_pd(f, f);

        println!("{:?}", r);
    }
}

Output

The expected output is

__m256d(4.0, 4.0, 4.0, 4.0)

The actual output is

__m256d(4.0, 4.0, 0.0, 0.0)

Notes

  • The code built in debug mode produces the expected result. This issue only occurs in a release build.
  • Using _mm instead of _mm256 results in correct output in both debug and release mode.
  • Code using only _mm or _mm256 yields the same performance (I have another piece of code for that. I can provide it if needed).

Versions

The issue can be reproduced with 1.30.0-nightly (2018-09-24), 1.30.0-beta.7, 1.29.0.

Activity

  1. nicokoch commented on Sep 26, 2018

    @nicokoch
    Contributor

    shouldn‘t be a #[target_feature(enable = "avx2")] in there?

    Edit: It works when you add the target feature. playground link

    I think the compiler should deny the code from the OP though, instead of producing wrong code.

  2. hanna-kruppe commented on Sep 26, 2018

    @hanna-kruppe
    Contributor

    Looks like a duplicate of #50154 at a glance.

  3. vladglv commented on Sep 26, 2018

    @vladglv
    Author

    Yes, I forgot to add #[target_feature(enable = "avx2")]. I agree with @nicokoch. The code should not compile. I think that it should be the case for both debug and release builds.

    Do you think that clippy should provide warnings for this kind of code?

  4. theotherphil commented on Oct 3, 2018

    @theotherphil
    Contributor

    I just ran into this issue and spent quite a while getting very confused. It's very surprising that code using AVX2 intrinsics compiles and then behaves nonsensically without the correct target feature attribute.

  5. added a commit that references this issue on Oct 14, 2018
    5c1be1a
  6. added a commit that references this issue on Oct 19, 2018
    3cc8f73
  7. added a commit that references this issue on Oct 20, 2018
    b860765
  8. andersk commented on Dec 22, 2018

    @andersk
    Contributor

    This test case still produces the incorrect result with -C opt-level=3 (or Cargo’s --release), in both 1.31.1 and 1.33.0-nightly (e40548b 2018-12-21).

    Playground link.

    I guess this is because #55073 was reverted as #55281? This should maybe be reopened then.

  9. nikic commented on Dec 22, 2018

    @nikic
    Contributor

    Yes, the fix was reverted. I don't think it's necessary to reopen this one, as the general issue is already tracked at #50154. This is just one more manifestation of the same problem.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions