File: issue-79865-llvm-miscompile.rs

package info (click to toggle)
rustc-web 1.85.0%2Bdfsg3-1~deb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-proposed-updates
  • size: 1,759,988 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,056; lisp: 29; perl: 29; ruby: 19; sql: 11
file content (38 lines) | stat: -rw-r--r-- 987 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//@ run-pass
//@ only-x86_64
//@ compile-flags: -C opt-level=3

// Regression test for issue #79865.
// The assertion will fail when compiled with Rust 1.56..=1.59
// due to an LLVM miscompilation.

use std::arch::x86_64::*;

fn main() {
    if is_x86_feature_detected!("avx") {
        let res: [f64; 4] = unsafe { std::mem::transmute::<_, _>(first()) };
        assert_eq!(res, [22.0, 44.0, 66.0, 88.0]);
    }
}

#[target_feature(enable = "avx")]
unsafe fn first() -> __m256d {
    second()
}

unsafe fn second() -> __m256d {
    let v0 = _mm256_setr_pd(1.0, 2.0, 3.0, 4.0);
    let v1 = _mm256_setr_pd(10.0, 20.0, 30.0, 40.0);

    // needs to be called twice to hit the miscompilation
    let (add, _) = add_sub(v0, v1);
    let (add, _) = add_sub(add, add);
    add
}

#[inline(never)] // needed to hit the miscompilation
unsafe fn add_sub(v1: __m256d, v0: __m256d) -> (__m256d, __m256d) {
    let add = _mm256_add_pd(v0, v1);
    let sub = _mm256_sub_pd(v0, v1);
    (add, sub)
}