Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / Sema / vector-gcc-compat.c
blob7764b3bf686add2d218b2d44b22b4eb88d0f5828
1 // RUN: %clang_cc1 %s -verify -fsyntax-only -Weverything -Wno-unused-but-set-variable -triple x86_64-apple-darwin10
3 // Test the compatibility of clang's vector extensions with gcc's vector
4 // extensions for C. Notably &&, ||, ?: and ! are not available.
5 typedef long long v2i64 __attribute__((vector_size(16)));
6 typedef int v2i32 __attribute__((vector_size(8)));
7 typedef short v2i16 __attribute__((vector_size(4)));
8 typedef char v2i8 __attribute__((vector_size(2)));
10 typedef unsigned long long v2u64 __attribute__((vector_size(16)));
11 typedef unsigned int v2u32 __attribute__((vector_size(8)));
12 typedef unsigned short v2u16 __attribute__((vector_size(4)));
13 typedef unsigned char v2u8 __attribute__((vector_size(2)));
15 typedef float v4f32 __attribute__((vector_size(16)));
16 typedef double v2f64 __attribute__((vector_size(16)));
17 typedef double v4f64 __attribute__((vector_size(32)));
18 typedef int v4i32 __attribute((vector_size(16)));
20 // Verify that we can use the [[]] spelling of the attribute.
21 // We intentionally use the same type alias name to check that both versions
22 // define the same type.
23 typedef long long v2i64 [[gnu::vector_size(16)]]; // expected-warning{{[[]] attributes are a C23 extension}}
24 typedef int v2i32 [[gnu::vector_size(8)]]; // expected-warning{{[[]] attributes are a C23 extension}}
26 // Check various positions where the [[]] spelling can or cannot be used.
27 [[gnu::vector_size(16)]] typedef long long v2i64; // expected-warning{{[[]] attributes are a C23 extension}}
28 typedef long long [[gnu::vector_size(16)]] v2i64_ignored;
29 // expected-warning@-1{{'vector_size' attribute ignored}}
30 // expected-warning@-2{{[[]] attributes are a C23 extension}}
31 // FIXME: Contrary to the error message that we emit, GCC does actually allow
32 // the attribute in the following position. Somewhat surprisingly, the attribute
33 // is applied not to the pointer but to the base type, i.e. this declaration has
34 // the same effect in GCC as the other declarations for `v2i64`.
35 typedef long long *[[gnu::vector_size(16)]] v2i64_doesnt_work;
36 // expected-error@-1{{invalid vector element type 'long long *'}}
37 // expected-warning@-2{{GCC does not allow the 'vector_size' attribute to be written on a type}}
38 // expected-warning@-3{{[[]] attributes are a C23 extension}}
40 // Verify that we can use the attribute outside of a typedef.
41 static int v2i32_var [[gnu::vector_size(8)]]; // expected-warning{{[[]] attributes are a C23 extension}}
43 void arithmeticTest(void);
44 void logicTest(void);
45 void comparisonTest(void);
46 void floatTestSignedType(char a, short b, int c, long long d);
47 void floatTestUnsignedType(unsigned char a, unsigned short b, unsigned int c,
48 unsigned long long d);
49 void floatTestConstant(void);
50 void intTestType(char a, short b, int c, long long d);
51 void intTestTypeUnsigned(unsigned char a, unsigned short b, unsigned int c,
52 unsigned long long d);
53 void uintTestType(char a, short b, int c, long long d);
54 void uintTestTypeUnsigned(unsigned char a, unsigned short b, unsigned int c,
55 unsigned long long d);
56 void uintTestConstant(v2u64 v2u64_a, v2u32 v2u32_a, v2u16 v2u16_a, v2u8 v2u8_a);
57 void intTestConstant(v2i64 v2i64_a, v2i32 v2i32_a, v2i16 v2i16_a, v2i8 v2i8_a);
59 void arithmeticTest(void) {
60 v2i64 v2i64_a = (v2i64){0, 1};
61 v2i64 v2i64_r;
63 v2i64_r = v2i64_a + 1;
64 v2i64_r = v2i64_a - 1;
65 v2i64_r = v2i64_a * 1;
66 v2i64_r = v2i64_a / 1;
67 v2i64_r = v2i64_a % 1;
69 v2i64_r = 1 + v2i64_a;
70 v2i64_r = 1 - v2i64_a;
71 v2i64_r = 1 * v2i64_a;
72 v2i64_r = 1 / v2i64_a;
73 v2i64_r = 1 % v2i64_a;
75 v2i64_a += 1;
76 v2i64_a -= 1;
77 v2i64_a *= 1;
78 v2i64_a /= 1;
79 v2i64_a %= 1;
82 void comparisonTest(void) {
83 v2i64 v2i64_a = (v2i64){0, 1};
84 v2i64 v2i64_r;
86 v2i64_r = v2i64_a == 1;
87 v2i64_r = v2i64_a != 1;
88 v2i64_r = v2i64_a < 1;
89 v2i64_r = v2i64_a > 1;
90 v2i64_r = v2i64_a <= 1;
91 v2i64_r = v2i64_a >= 1;
93 v2i64_r = 1 == v2i64_a;
94 v2i64_r = 1 != v2i64_a;
95 v2i64_r = 1 < v2i64_a;
96 v2i64_r = 1 > v2i64_a;
97 v2i64_r = 1 <= v2i64_a;
98 v2i64_r = 1 >= v2i64_a;
101 void logicTest(void) {
102 v2i64 v2i64_a = (v2i64){0, 1};
103 v2i64 v2i64_b = (v2i64){2, 1};
104 v2i64 v2i64_c = (v2i64){3, 1};
105 v2i64 v2i64_r;
107 v2i64_r = !v2i64_a; // expected-error {{invalid argument type 'v2i64' (vector of 2 'long long' values) to unary expression}}
108 v2i64_r = ~v2i64_a;
110 v2i64_r = v2i64_a ? v2i64_b : v2i64_c; // expected-error {{used type 'v2i64' (vector of 2 'long long' values) where arithmetic or pointer type is required}}
112 v2i64_r = v2i64_a & 1;
113 v2i64_r = v2i64_a | 1;
114 v2i64_r = v2i64_a ^ 1;
116 v2i64_r = 1 & v2i64_a;
117 v2i64_r = 1 | v2i64_a;
118 v2i64_r = 1 ^ v2i64_a;
120 v2i64_a &= 1;
121 v2i64_a |= 1;
122 v2i64_a ^= 1;
124 v2i64_r = v2i64_a && 1; // expected-error {{logical expression with vector type 'v2i64' (vector of 2 'long long' values) and non-vector type 'int' is only supported in C++}}
125 v2i64_r = v2i64_a || 1; // expected-error {{logical expression with vector type 'v2i64' (vector of 2 'long long' values) and non-vector type 'int' is only supported in C++}}
127 v2i64_r = v2i64_a && v2i64_a; // expected-error {{logical expression with vector types 'v2i64' (vector of 2 'long long' values) and 'v2i64' is only supported in C++}}
128 v2i64_r = v2i64_a || v2i64_a; // expected-error {{logical expression with vector types 'v2i64' (vector of 2 'long long' values) and 'v2i64' is only supported in C++}}
130 v2i64_r = v2i64_a << 1;
131 v2i64_r = v2i64_a >> 1;
133 v2i64_r = 1 << v2i64_a;
134 v2i64_r = 1 >> v2i64_a;
136 v2i64_a <<= 1;
137 v2i64_a >>= 1;
140 // For operations with floating point types, we check that integer constants
141 // can be respresented, or failing that checking based on the integer types.
142 void floatTestConstant(void) {
143 // Test that constants added to floats must be expressible as floating point
144 // numbers.
145 v4f32 v4f32_a = {0.4f, 0.4f, 0.4f, 0.4f};
146 v4f32_a = v4f32_a + 1;
147 v4f32_a = v4f32_a + 0xFFFFFF;
148 v4f32_a = v4f32_a + (-1567563LL);
149 v4f32_a = v4f32_a + (16777208);
150 v4f32_a = v4f32_a + (16777219); // expected-error {{cannot convert between scalar type 'int' and vector type 'v4f32' (vector of 4 'float' values) as implicit conversion would cause truncation}}
153 void floatTestConstantComparison(void);
154 void doubleTestConstantComparison(void);
156 void floatTestConstantComparison(void) {
157 v4f32 v4f32_a = {0.4f, 0.4f, 0.4f, 0.4f};
158 v4i32 v4i32_r;
159 v4i32_r = v4f32_a > 0.4f;
160 v4i32_r = v4f32_a >= 0.4f;
161 v4i32_r = v4f32_a < 0.4f;
162 v4i32_r = v4f32_a <= 0.4f;
163 v4i32_r = v4f32_a == 0.4f; // expected-warning {{comparing floating point with == or != is unsafe}}
164 v4i32_r = v4f32_a != 0.4f; // expected-warning {{comparing floating point with == or != is unsafe}}
167 void doubleTestConstantComparison(void) {
168 v2f64 v2f64_a = {0.4, 0.4};
169 v2i64 v2i64_r;
170 v2i64_r = v2f64_a > 0.4;
171 v2i64_r = v2f64_a >= 0.4;
172 v2i64_r = v2f64_a < 0.4;
173 v2i64_r = v2f64_a <= 0.4;
174 v2i64_r = v2f64_a == 0.4; // expected-warning {{comparing floating point with == or != is unsafe}}
175 v2i64_r = v2f64_a != 0.4; // expected-warning {{comparing floating point with == or != is unsafe}}
178 void floatTestUnsignedType(unsigned char a, unsigned short b, unsigned int c,
179 unsigned long long d) {
180 v4f32 v4f32_a = {0.4f, 0.4f, 0.4f, 0.4f};
181 v4f64 v4f64_b = {0.4, 0.4, 0.4, 0.4};
183 v4f32_a = v4f32_a + a;
184 v4f32_a = v4f32_a + b;
185 v4f32_a = v4f32_a + c; // expected-error {{cannot convert between scalar type 'unsigned int' and vector type 'v4f32' (vector of 4 'float' values) as implicit conversion would cause truncation}}
186 v4f32_a = v4f32_a + d; // expected-error {{cannot convert between scalar type 'unsigned long long' and vector type 'v4f32' (vector of 4 'float' values) as implicit conversion would cause truncation}}
188 v4f64_b = v4f64_b + a;
189 v4f64_b = v4f64_b + b;
190 v4f64_b = v4f64_b + c;
191 v4f64_b = v4f64_b + d; // expected-error {{cannot convert between scalar type 'unsigned long long' and vector type 'v4f64' (vector of 4 'double' values) as implicit conversion would cause truncation}}
194 void floatTestSignedType(char a, short b, int c, long long d) {
195 v4f32 v4f32_a = {0.4f, 0.4f, 0.4f, 0.4f};
196 v4f64 v4f64_b = {0.4, 0.4, 0.4, 0.4};
198 v4f32_a = v4f32_a + a;
199 v4f32_a = v4f32_a + b;
200 v4f32_a = v4f32_a + c; // expected-error {{cannot convert between scalar type 'int' and vector type 'v4f32' (vector of 4 'float' values) as implicit conversion would cause truncation}}
201 v4f32_a = v4f32_a + d; // expected-error {{cannot convert between scalar type 'long long' and vector type 'v4f32' (vector of 4 'float' values) as implicit conversion would cause truncation}}
203 v4f64_b = v4f64_b + a;
204 v4f64_b = v4f64_b + b;
205 v4f64_b = v4f64_b + c;
206 v4f64_b = v4f64_b + d; // expected-error {{cannot convert between scalar type 'long long' and vector type 'v4f64' (vector of 4 'double' values) as implicit conversion would cause truncation}}
209 void intTestType(char a, short b, int c, long long d) {
210 v2i64 v2i64_a = {1, 2};
211 v2i32 v2i32_a = {1, 2};
212 v2i16 v2i16_a = {1, 2};
213 v2i8 v2i8_a = {1, 2};
215 v2i64_a = v2i64_a + d;
216 v2i64_a = v2i64_a + c;
217 v2i64_a = v2i64_a + b;
218 v2i64_a = v2i64_a + a;
220 v2i32_a = v2i32_a + d; // expected-warning {{implicit conversion loses integer precision: 'long long' to 'v2i32' (vector of 2 'int' values)}}
221 v2i32_a = v2i32_a + c;
222 v2i32_a = v2i32_a + b;
223 v2i32_a = v2i32_a + a;
225 v2i16_a = v2i16_a + d; // expected-error {{cannot convert between scalar type 'long long' and vector type 'v2i16' (vector of 2 'short' values) as implicit conversion would cause truncation}}
226 v2i16_a = v2i16_a + c; // expected-warning {{implicit conversion loses integer precision: 'int' to 'v2i16' (vector of 2 'short' values)}}
227 v2i16_a = v2i16_a + b;
228 v2i16_a = v2i16_a + a;
230 v2i8_a = v2i8_a + d; // expected-error {{cannot convert between scalar type 'long long' and vector type 'v2i8' (vector of 2 'char' values) as implicit conversion would cause truncation}}
231 v2i8_a = v2i8_a + c; // expected-error {{cannot convert between scalar type 'int' and vector type 'v2i8' (vector of 2 'char' values) as implicit conversion would cause truncation}}
232 v2i8_a = v2i8_a + b; // expected-warning {{implicit conversion loses integer precision: 'short' to 'v2i8' (vector of 2 'char' values)}}
233 v2i8_a = v2i8_a + a;
236 void intTestTypeUnsigned(unsigned char a, unsigned short b, unsigned int c,
237 unsigned long long d) {
238 v2i64 v2i64_a = {1, 2};
239 v2i32 v2i32_a = {1, 2};
240 v2i16 v2i16_a = {1, 2};
241 v2i8 v2i8_a = {1, 2};
243 v2i64_a = v2i64_a + d; // expected-error {{cannot convert between scalar type 'unsigned long long' and vector type 'v2i64' (vector of 2 'long long' values) as implicit conversion would cause truncation}}
245 v2i64_a = v2i64_a + c;
246 v2i64_a = v2i64_a + b;
247 v2i64_a = v2i64_a + a;
249 v2i32_a = v2i32_a + d; // expected-warning {{implicit conversion loses integer precision: 'unsigned long long' to 'v2i32' (vector of 2 'int' values)}}
250 v2i32_a = v2i32_a + c; // expected-error {{cannot convert between scalar type 'unsigned int' and vector type 'v2i32' (vector of 2 'int' values) as implicit conversion would cause truncation}}
251 v2i32_a = v2i32_a + b;
252 v2i32_a = v2i32_a + a;
254 v2i16_a = v2i16_a + d; // expected-error {{cannot convert between scalar type 'unsigned long long' and vector type 'v2i16' (vector of 2 'short' values) as implicit conversion would cause truncation}}
255 v2i16_a = v2i16_a + c; // expected-warning {{implicit conversion loses integer precision: 'unsigned int' to 'v2i16' (vector of 2 'short' values)}}
256 v2i16_a = v2i16_a + b; // expected-error {{cannot convert between scalar type 'unsigned short' and vector type 'v2i16' (vector of 2 'short' values) as implicit conversion would cause truncation}}
257 v2i16_a = v2i16_a + a;
259 v2i8_a = v2i8_a + d; // expected-error {{cannot convert between scalar type 'unsigned long long' and vector type 'v2i8' (vector of 2 'char' values) as implicit conversion would cause truncation}}
260 v2i8_a = v2i8_a + c; // expected-error {{cannot convert between scalar type 'unsigned int' and vector type 'v2i8' (vector of 2 'char' values) as implicit conversion would cause truncation}}
261 v2i8_a = v2i8_a + b; // expected-warning {{implicit conversion loses integer precision: 'unsigned short' to 'v2i8' (vector of 2 'char' values)}}
262 v2i8_a = v2i8_a + a; // expected-error {{cannot convert between scalar type 'unsigned char' and vector type 'v2i8' (vector of 2 'char' values) as implicit conversion would cause truncation}}
265 void uintTestType(char a, short b, int c, long long d) {
266 v2u64 v2u64_a = {1, 2};
267 v2u32 v2u32_a = {1, 2};
268 v2u16 v2u16_a = {1, 2};
269 v2u8 v2u8_a = {1, 2};
271 v2u64_a = v2u64_a + d; // expected-warning {{implicit conversion changes signedness: 'long long' to 'v2u64' (vector of 2 'unsigned long long' values)}}
272 v2u64_a = v2u64_a + c; // expected-warning {{implicit conversion changes signedness: 'int' to 'v2u64' (vector of 2 'unsigned long long' values)}}
273 v2u64_a = v2u64_a + b; // expected-warning {{implicit conversion changes signedness: 'short' to 'v2u64' (vector of 2 'unsigned long long' values)}}
274 v2u64_a = v2u64_a + a; // expected-warning {{implicit conversion changes signedness: 'char' to 'v2u64' (vector of 2 'unsigned long long' values)}}
276 v2u32_a = v2u32_a + d; // expected-warning {{implicit conversion loses integer precision: 'long long' to 'v2u32' (vector of 2 'unsigned int' values)}}
277 v2u32_a = v2u32_a + c; // expected-warning {{implicit conversion changes signedness: 'int' to 'v2u32' (vector of 2 'unsigned int' values)}}
278 v2u32_a = v2u32_a + b; // expected-warning {{implicit conversion changes signedness: 'short' to 'v2u32' (vector of 2 'unsigned int' values)}}
279 v2u32_a = v2u32_a + a; // expected-warning {{implicit conversion changes signedness: 'char' to 'v2u32' (vector of 2 'unsigned int' values)}}
281 v2u16_a = v2u16_a + d; // expected-error {{cannot convert between scalar type 'long long' and vector type 'v2u16' (vector of 2 'unsigned short' values) as implicit conversion would cause truncation}}
282 v2u16_a = v2u16_a + c; // expected-warning {{implicit conversion loses integer precision: 'int' to 'v2u16' (vector of 2 'unsigned short' values)}}
283 v2u16_a = v2u16_a + b; // expected-warning {{implicit conversion changes signedness: 'short' to 'v2u16' (vector of 2 'unsigned short' values)}}
284 v2u16_a = v2u16_a + a; // expected-warning {{implicit conversion changes signedness: 'char' to 'v2u16' (vector of 2 'unsigned short' values)}}
286 v2u8_a = v2u8_a + d; // expected-error {{cannot convert between scalar type 'long long' and vector type 'v2u8' (vector of 2 'unsigned char' values) as implicit conversion would cause truncation}}
287 v2u8_a = v2u8_a + c; // expected-error {{cannot convert between scalar type 'int' and vector type 'v2u8' (vector of 2 'unsigned char' values) as implicit conversion would cause truncation}}
288 v2u8_a = v2u8_a + b; // expected-warning {{implicit conversion loses integer precision: 'short' to 'v2u8' (vector of 2 'unsigned char' values)}}
289 v2u8_a = v2u8_a + a; // expected-warning {{implicit conversion changes signedness: 'char' to 'v2u8' (vector of 2 'unsigned char' values)}}
292 void uintTestTypeUnsigned(unsigned char a, unsigned short b, unsigned int c,
293 unsigned long long d) {
294 v2u64 v2u64_a = {1, 2};
295 v2u32 v2u32_a = {1, 2};
296 v2u16 v2u16_a = {1, 2};
297 v2u8 v2u8_a = {1, 2};
299 v2u64_a = v2u64_a + d;
300 v2u64_a = v2u64_a + c;
301 v2u64_a = v2u64_a + b;
302 v2u64_a = v2u64_a + a;
304 v2u32_a = v2u32_a + d; // expected-warning {{implicit conversion loses integer precision: 'unsigned long long' to 'v2u32' (vector of 2 'unsigned int' values)}}
305 v2u32_a = v2u32_a + c;
306 v2u32_a = v2u32_a + b;
307 v2u32_a = v2u32_a + a;
309 v2u16_a = v2u16_a + d; // expected-error {{cannot convert between scalar type 'unsigned long long' and vector type 'v2u16' (vector of 2 'unsigned short' values) as implicit conversion would cause truncation}}
310 v2u16_a = v2u16_a + c; // expected-warning {{implicit conversion loses integer precision: 'unsigned int' to 'v2u16' (vector of 2 'unsigned short' values)}}
311 v2u16_a = v2u16_a + b;
312 v2u16_a = v2u16_a + a;
314 v2u8_a = v2u8_a + d; // expected-error {{cannot convert between scalar type 'unsigned long long' and vector type 'v2u8' (vector of 2 'unsigned char' values) as implicit conversion would cause truncation}}
315 v2u8_a = v2u8_a + c; // expected-error {{cannot convert between scalar type 'unsigned int' and vector type 'v2u8' (vector of 2 'unsigned char' values) as implicit conversion would cause truncation}}
316 v2u8_a = v2u8_a + b; // expected-warning {{implicit conversion loses integer precision: 'unsigned short' to 'v2u8' (vector of 2 'unsigned char' values)}}
317 v2u8_a = v2u8_a + a;
320 void uintTestConstant(v2u64 v2u64_a, v2u32 v2u32_a, v2u16 v2u16_a,
321 v2u8 v2u8_a) {
322 v2u64_a = v2u64_a + 0xFFFFFFFFFFFFFFFF;
323 v2u32_a = v2u32_a + 0xFFFFFFFF;
324 v2u16_a = v2u16_a + 0xFFFF;
325 v2u8_a = v2u8_a + 0xFF;
327 v2u32_a = v2u32_a + 0x1FFFFFFFF; // expected-warning {{implicit conversion from 'long' to 'v2u32' (vector of 2 'unsigned int' values) changes value from 8589934591 to 4294967295}}
328 v2u16_a = v2u16_a + 0x1FFFF; // expected-warning {{implicit conversion from 'int' to 'v2u16' (vector of 2 'unsigned short' values) changes value from 131071 to 65535}}
329 v2u8_a = v2u8_a + 0x1FF; // expected-error {{cannot convert between scalar type 'int' and vector type 'v2u8' (vector of 2 'unsigned char' values) as implicit conversion would cause truncation}}
332 void intTestConstant(v2i64 v2i64_a, v2i32 v2i32_a, v2i16 v2i16_a, v2i8 v2i8_a) {
333 // Legal upper bounds.
334 v2i64_a = v2i64_a + (long long)0x7FFFFFFFFFFFFFFF;
335 v2i32_a = v2i32_a + (int)0x7FFFFFFF;
336 v2i16_a = v2i16_a + (short)0x7FFF;
337 v2i8_a = v2i8_a + (char)0x7F;
339 // Legal lower bounds.
340 v2i64_a = v2i64_a + (-9223372036854775807);
341 v2i32_a = v2i32_a + (-2147483648);
342 v2i16_a = v2i16_a + (-32768);
343 v2i8_a = v2i8_a + (-128);
345 // One increment/decrement more than the type can hold
346 v2i32_a = v2i32_a + 2147483648; // expected-warning {{implicit conversion from 'long' to 'v2i32' (vector of 2 'int' values) changes value from 2147483648 to -2147483648}}
347 v2i16_a = v2i16_a + 32768; // expected-warning {{implicit conversion from 'int' to 'v2i16' (vector of 2 'short' values) changes value from 32768 to -32768}}
348 v2i8_a = v2i8_a + 128; // expected-warning {{implicit conversion from 'int' to 'v2i8' (vector of 2 'char' values) changes value from 128 to -128}}
350 v2i32_a = v2i32_a + (-2147483649); // expected-warning {{implicit conversion from 'long' to 'v2i32' (vector of 2 'int' values) changes value from -2147483649 to 2147483647}}
351 v2i16_a = v2i16_a + (-32769); // expected-warning {{implicit conversion from 'int' to 'v2i16' (vector of 2 'short' values) changes value from -32769 to 32767}}
352 v2i8_a = v2i8_a + (-129); // expected-error {{cannot convert between scalar type 'int' and vector type 'v2i8' (vector of 2 'char' values) as implicit conversion would cause truncation}}