Debian Codegraph @

gcc/testsuite/gcc.target/aarch64/sve/pr122827.c

raw
1 /* PR target/122827.
2
3 With -msve-vector-bits=128 and SVE-only autovec, the inlined
4 madd_v3 calls are vectorised into SVE partial-mode FMLAs that share
5 a single vec_duplicate of the scalar FAC.
6 Before the fix, the register allocator placed the VNx2SF
7 broadcast in V14 across the call to clobber() because IRA thought
8 V14 was preserved (only the low 64 bits, D14, are callee-saved under
9 AAPCS64). The SVE FMLA reads the full Z register, so the upper
10 lanes of the broadcast are corrupted by the call and the result of
11 the FMLA is wrong. */
12
13 /* { dg-do run { target aarch64_sve128_hw } } */
14 /* { dg-options "-Ofast -msve-vector-bits=128 --param=aarch64-autovec-preference=sve-only" } */
15 /* { dg-require-effective-target aarch64_little_endian } */
16
17 struct R
18 {
19 float combined[3];
20 float spec[3];
21 float diff[3];
22 float diffshad[3];
23 float shad[3];
24 };
25
26 struct I
27 {
28 float fac;
29 int passflag;
30 };
31
32 /* AAPCS64 only requires callees to preserve the low 64 bits of V8-V15;
33 the upper lanes are caller-clobbered. The buggy register allocator
34 in this test places the broadcast in V14 specifically, so clobbering
35 V14s upper bits here is sufficient to expose the miscompile. The
36 asm clobber list lets GCC save/restore D14 around the write, so we
37 honour the ABI contract for the low half while leaving the upper
38 half trashed, what any normal callee is permitted to do. */
39 __attribute__((noipa)) static void
40 clobber (struct I *si, struct R *r)
41 {
42 asm volatile ("movi v14.16b, #0xff" : : : "v14");
43 }
44
45 static inline __attribute__((always_inline)) void
46 madd_v3 (float r[3], const float a[3], float f)
47 {
48 r[0] += a[0] * f;
49 r[1] += a[1] * f;
50 r[2] += a[2] * f;
51 }
52
53 __attribute__((noipa)) void
54 test (struct I *si, struct R *r)
55 {
56 struct R t = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 },
57 { 10, 11, 12 }, { 13, 14, 15 } };
58
59 clobber (si, r);
60 if (si->fac != 0.0f)
61 {
62 float fac = si->fac;
63 clobber (si, &t);
64 madd_v3 (r->combined, t.combined, fac);
65 if (si->passflag & 1)
66 madd_v3 (r->spec, t.spec, fac);
67 if (si->passflag & 2)
68 {
69 madd_v3 (r->diff, t.diff, fac);
70 madd_v3 (r->diffshad, t.diffshad, fac);
71 }
72 if (si->passflag & 4)
73 madd_v3 (r->shad, t.shad, fac);
74 }
75 }
76
77 int
78 main (void)
79 {
80 struct I si = { 2.0f, 7 };
81 struct R r = { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 },
82 { 0, 0, 0 }, { 0, 0, 0 } };
83 test (&si, &r);
84 if (r.combined[0] != 2.f || r.combined[1] != 4.f || r.combined[2] != 6.f)
85 __builtin_abort ();
86 if (r.spec[0] != 8.f || r.spec[1] != 10.f || r.spec[2] != 12.f)
87 __builtin_abort ();
88 if (r.diff[0] != 14.f || r.diff[1] != 16.f || r.diff[2] != 18.f)
89 __builtin_abort ();
90 if (r.diffshad[0] != 20.f || r.diffshad[1] != 22.f || r.diffshad[2] != 24.f)
91 __builtin_abort ();
92 if (r.shad[0] != 26.f || r.shad[1] != 28.f || r.shad[2] != 30.f)
93 __builtin_abort ();
94 return 0;
95 }
96