xref: /llvm-project/clang/test/Sema/matrix-type-operators.c (revision 67e22983115451ef5512ad2813dd337762c52da3)
1 // RUN: %clang_cc1 %s -fenable-matrix -pedantic -verify -triple=x86_64-apple-darwin9
2 
3 typedef float sx5x10_t __attribute__((matrix_type(5, 10)));
4 typedef float sx10x5_t __attribute__((matrix_type(10, 5)));
5 typedef float sx10x10_t __attribute__((matrix_type(10, 10)));
6 
add(sx10x10_t a,sx5x10_t b,sx10x5_t c)7 void add(sx10x10_t a, sx5x10_t b, sx10x5_t c) {
8   a = b + c;
9   // expected-error@-1 {{invalid operands to binary expression ('sx5x10_t' (aka 'float __attribute__((matrix_type(5, 10)))') and 'sx10x5_t' (aka 'float __attribute__((matrix_type(10, 5)))'))}}
10 
11   b += c;
12   // expected-error@-1 {{invalid operands to binary expression ('sx5x10_t' (aka 'float __attribute__((matrix_type(5, 10)))') and 'sx10x5_t' (aka 'float __attribute__((matrix_type(10, 5)))'))}}
13 
14   a = b + b; // expected-error {{assigning to 'sx10x10_t' (aka 'float __attribute__((matrix_type(10, 10)))') from incompatible type 'sx5x10_t' (aka 'float __attribute__((matrix_type(5, 10)))')}}
15 
16   a = 10 + b;
17   // expected-error@-1 {{assigning to 'sx10x10_t' (aka 'float __attribute__((matrix_type(10, 10)))') from incompatible type 'sx5x10_t' (aka 'float __attribute__((matrix_type(5, 10)))')}}
18 
19   a = b + &c;
20   // expected-error@-1 {{invalid operands to binary expression ('sx5x10_t' (aka 'float __attribute__((matrix_type(5, 10)))') and 'sx10x5_t *' (aka 'float  __attribute__((matrix_type(10, 5)))*'))}}
21   // expected-error@-2 {{casting 'sx10x5_t *' (aka 'float  __attribute__((matrix_type(10, 5)))*') to incompatible type 'float'}}
22 
23   b += &c;
24   // expected-error@-1 {{invalid operands to binary expression ('sx5x10_t' (aka 'float __attribute__((matrix_type(5, 10)))') and 'sx10x5_t *' (aka 'float  __attribute__((matrix_type(10, 5)))*'))}}
25   // expected-error@-2 {{casting 'sx10x5_t *' (aka 'float  __attribute__((matrix_type(10, 5)))*') to incompatible type 'float'}}
26 }
27 
sub(sx10x10_t a,sx5x10_t b,sx10x5_t c)28 void sub(sx10x10_t a, sx5x10_t b, sx10x5_t c) {
29   a = b - c;
30   // expected-error@-1 {{invalid operands to binary expression ('sx5x10_t' (aka 'float __attribute__((matrix_type(5, 10)))') and 'sx10x5_t' (aka 'float __attribute__((matrix_type(10, 5)))'))}}
31 
32   b -= c;
33   // expected-error@-1 {{invalid operands to binary expression ('sx5x10_t' (aka 'float __attribute__((matrix_type(5, 10)))') and 'sx10x5_t' (aka 'float __attribute__((matrix_type(10, 5)))'))}}
34 
35   a = b - b; // expected-error {{assigning to 'sx10x10_t' (aka 'float __attribute__((matrix_type(10, 10)))') from incompatible type 'sx5x10_t' (aka 'float __attribute__((matrix_type(5, 10)))')}}
36 
37   a = 10 - b;
38   // expected-error@-1 {{assigning to 'sx10x10_t' (aka 'float __attribute__((matrix_type(10, 10)))') from incompatible type 'sx5x10_t' (aka 'float __attribute__((matrix_type(5, 10)))')}}
39 
40   a = b - &c;
41   // expected-error@-1 {{invalid operands to binary expression ('sx5x10_t' (aka 'float __attribute__((matrix_type(5, 10)))') and 'sx10x5_t *' (aka 'float  __attribute__((matrix_type(10, 5)))*'))}}
42   // expected-error@-2 {{casting 'sx10x5_t *' (aka 'float  __attribute__((matrix_type(10, 5)))*') to incompatible type 'float'}}
43 
44   b -= &c;
45   // expected-error@-1 {{invalid operands to binary expression ('sx5x10_t' (aka 'float __attribute__((matrix_type(5, 10)))') and 'sx10x5_t *' (aka 'float  __attribute__((matrix_type(10, 5)))*'))}}
46   // expected-error@-2 {{casting 'sx10x5_t *' (aka 'float  __attribute__((matrix_type(10, 5)))*') to incompatible type 'float'}}
47 }
48 
49 typedef int ix10x5_t __attribute__((matrix_type(10, 5)));
50 typedef int ix10x10_t __attribute__((matrix_type(10, 10)));
51 
matrix_matrix_multiply(sx10x10_t a,sx5x10_t b,ix10x5_t c,ix10x10_t d,float sf,char * p)52 void matrix_matrix_multiply(sx10x10_t a, sx5x10_t b, ix10x5_t c, ix10x10_t d, float sf, char *p) {
53   // Check dimension mismatches.
54   a = a * b;
55   // expected-error@-1 {{invalid operands to binary expression ('sx10x10_t' (aka 'float __attribute__((matrix_type(10, 10)))') and 'sx5x10_t' (aka 'float __attribute__((matrix_type(5, 10)))'))}}
56   a *= b;
57   // expected-error@-1 {{invalid operands to binary expression ('sx10x10_t' (aka 'float __attribute__((matrix_type(10, 10)))') and 'sx5x10_t' (aka 'float __attribute__((matrix_type(5, 10)))'))}}
58   b = a * a;
59   // expected-error@-1 {{assigning to 'sx5x10_t' (aka 'float __attribute__((matrix_type(5, 10)))') from incompatible type 'sx10x10_t' (aka 'float __attribute__((matrix_type(10, 10)))')}}
60 
61   // Check element type mismatches.
62   a = b * c;
63   // expected-error@-1 {{invalid operands to binary expression ('sx5x10_t' (aka 'float __attribute__((matrix_type(5, 10)))') and 'ix10x5_t' (aka 'int __attribute__((matrix_type(10, 5)))'))}}
64   b *= c;
65   // expected-error@-1 {{invalid operands to binary expression ('sx5x10_t' (aka 'float __attribute__((matrix_type(5, 10)))') and 'ix10x5_t' (aka 'int __attribute__((matrix_type(10, 5)))'))}}
66   d = a * a;
67   // expected-error@-1 {{assigning to 'ix10x10_t' (aka 'int __attribute__((matrix_type(10, 10)))') from incompatible type 'sx10x10_t' (aka 'float __attribute__((matrix_type(10, 10)))')}}
68 
69   p = a * a;
70   // expected-error@-1 {{assigning to 'char *' from incompatible type 'sx10x10_t' (aka 'float __attribute__((matrix_type(10, 10)))')}}
71 }
72 
mat_scalar_multiply(sx10x10_t a,sx5x10_t b,float sf,char * p)73 void mat_scalar_multiply(sx10x10_t a, sx5x10_t b, float sf, char *p) {
74   // Shape of multiplication result does not match the type of b.
75   b = a * sf;
76   // expected-error@-1 {{assigning to 'sx5x10_t' (aka 'float __attribute__((matrix_type(5, 10)))') from incompatible type 'sx10x10_t' (aka 'float __attribute__((matrix_type(10, 10)))')}}
77   b = sf * a;
78   // expected-error@-1 {{assigning to 'sx5x10_t' (aka 'float __attribute__((matrix_type(5, 10)))') from incompatible type 'sx10x10_t' (aka 'float __attribute__((matrix_type(10, 10)))')}}
79 
80   a = a * p;
81   // expected-error@-1 {{casting 'char *' to incompatible type 'float'}}
82   // expected-error@-2 {{invalid operands to binary expression ('sx10x10_t' (aka 'float __attribute__((matrix_type(10, 10)))') and 'char *')}}
83   a *= p;
84   // expected-error@-1 {{casting 'char *' to incompatible type 'float'}}
85   // expected-error@-2 {{invalid operands to binary expression ('sx10x10_t' (aka 'float __attribute__((matrix_type(10, 10)))') and 'char *')}}
86   a = p * a;
87   // expected-error@-1 {{casting 'char *' to incompatible type 'float'}}
88   // expected-error@-2 {{invalid operands to binary expression ('char *' and 'sx10x10_t' (aka 'float __attribute__((matrix_type(10, 10)))'))}}
89   p *= a;
90   // expected-error@-1 {{casting 'char *' to incompatible type 'float'}}
91   // expected-error@-2 {{invalid operands to binary expression ('char *' and 'sx10x10_t' (aka 'float __attribute__((matrix_type(10, 10)))'))}}
92 
93   sf = a * sf;
94   // expected-error@-1 {{assigning to 'float' from incompatible type 'sx10x10_t' (aka 'float __attribute__((matrix_type(10, 10)))')}}
95 }
96 
mat_scalar_divide(sx10x10_t a,sx5x10_t b,float sf,char * p)97 void mat_scalar_divide(sx10x10_t a, sx5x10_t b, float sf, char *p) {
98   // Shape of multiplication result does not match the type of b.
99   b = a / sf;
100   // expected-error@-1 {{assigning to 'sx5x10_t' (aka 'float __attribute__((matrix_type(5, 10)))') from incompatible type 'sx10x10_t' (aka 'float __attribute__((matrix_type(10, 10)))')}}
101   b = sf / a;
102   // expected-error@-1 {{invalid operands to binary expression ('float' and 'sx10x10_t' (aka 'float __attribute__((matrix_type(10, 10)))'))}}
103 
104   a = a / p;
105   // expected-error@-1 {{invalid operands to binary expression ('sx10x10_t' (aka 'float __attribute__((matrix_type(10, 10)))') and 'char *')}}
106   a = p / a;
107   // expected-error@-1 {{invalid operands to binary expression ('char *' and 'sx10x10_t' (aka 'float __attribute__((matrix_type(10, 10)))'))}}
108 
109   sf = a / sf;
110   // expected-error@-1 {{assigning to 'float' from incompatible type 'sx10x10_t' (aka 'float __attribute__((matrix_type(10, 10)))')}}
111 }
112 
matrix_matrix_divide(sx10x10_t a,sx5x10_t b,ix10x5_t c,ix10x10_t d,float sf,char * p)113 void matrix_matrix_divide(sx10x10_t a, sx5x10_t b, ix10x5_t c, ix10x10_t d, float sf, char *p) {
114   // Matrix by matrix division is not supported.
115   a = a / a;
116   // expected-error@-1 {{invalid operands to binary expression ('sx10x10_t' (aka 'float __attribute__((matrix_type(10, 10)))') and 'sx10x10_t')}}
117 
118   b = a / a;
119   // expected-error@-1 {{invalid operands to binary expression ('sx10x10_t' (aka 'float __attribute__((matrix_type(10, 10)))') and 'sx10x10_t')}}
120 
121   // Check element type mismatches.
122   a = b / c;
123   // expected-error@-1 {{invalid operands to binary expression ('sx5x10_t' (aka 'float __attribute__((matrix_type(5, 10)))') and 'ix10x5_t' (aka 'int __attribute__((matrix_type(10, 5)))'))}}
124   d = a / a;
125   // expected-error@-1 {{invalid operands to binary expression ('sx10x10_t' (aka 'float __attribute__((matrix_type(10, 10)))') and 'sx10x10_t')}}
126 
127   p = a / a;
128   // expected-error@-1 {{invalid operands to binary expression ('sx10x10_t' (aka 'float __attribute__((matrix_type(10, 10)))') and 'sx10x10_t')}}
129 }
130 
131 sx5x10_t get_matrix(void);
132 
insert(sx5x10_t a,float f)133 void insert(sx5x10_t a, float f) {
134   // Non integer indexes.
135   a[3][f] = 0;
136   // expected-error@-1 {{matrix column index is not an integer}}
137   a[f][9] = 0;
138   // expected-error@-1 {{matrix row index is not an integer}}
139   a[f][f] = 0;
140   // expected-error@-1 {{matrix row index is not an integer}}
141   // expected-error@-2 {{matrix column index is not an integer}}
142   a[0][f] = 0;
143   // expected-error@-1 {{matrix column index is not an integer}}
144 
145   a[f][f] = 0;
146   // expected-error@-1 {{matrix row index is not an integer}}
147   // expected-error@-2 {{matrix column index is not an integer}}
148 
149   // Invalid element type.
150   a[3][4] = &f;
151   // expected-error@-1 {{assigning to 'float' from incompatible type 'float *'; remove &}}
152 
153   // Indexes outside allowed dimensions.
154   a[-1][3] = 10.0;
155   // expected-error@-1 {{matrix row index is outside the allowed range [0, 5)}}
156   a[3][-1] = 10.0;
157   // expected-error@-1 {{matrix column index is outside the allowed range [0, 10)}}
158   a[3][-1u] = 10.0;
159   // expected-error@-1 {{matrix column index is outside the allowed range [0, 10)}}
160   a[-1u][3] = 10.0;
161   // expected-error@-1 {{matrix row index is outside the allowed range [0, 5)}}
162   a[5][2] = 10.0;
163   // expected-error@-1 {{matrix row index is outside the allowed range [0, 5)}}
164   a[4][10] = 10.0;
165   // expected-error@-1 {{matrix column index is outside the allowed range [0, 10)}}
166   a[5][0] = f;
167   // expected-error@-1 {{matrix row index is outside the allowed range [0, 5)}}
168   (a[1])[1] = f;
169   // expected-error@-1 {{matrix row and column subscripts cannot be separated by any expression}}
170 
171   a[3] = 5.0;
172   // expected-error@-1 {{single subscript expressions are not allowed for matrix values}}
173 
174   (a[3]) = 5.0;
175   // expected-error@-1 {{single subscript expressions are not allowed for matrix values}}
176 
177   get_matrix()[0][0] = f;
178   // expected-error@-1 {{expression is not assignable}}
179   get_matrix()[5][1] = f;
180   // expected-error@-1 {{matrix row index is outside the allowed range [0, 5)}}
181   get_matrix()[3] = 5.0;
182   // expected-error@-1 {{single subscript expressions are not allowed for matrix values}}
183 
184   (get_matrix()[5])[10.0] = f;
185   // expected-error@-1 {{matrix row and column subscripts cannot be separated by any expression}}
186   (get_matrix()[3]) = 5.0;
187   // expected-error@-1 {{single subscript expressions are not allowed for matrix values}}
188 
189   a([0])[0] = f;
190   // expected-error@-1 {{expected expression}}
191   a[0]([0]) = f;
192   // expected-error@-1 {{expected expression}}
193 }
194 
extract(sx5x10_t a,float f)195 void extract(sx5x10_t a, float f) {
196   // Non integer indexes.
197   float v1 = a[3][f];
198   // expected-error@-1 {{matrix column index is not an integer}}
199   float v2 = a[f][9];
200   // expected-error@-1 {{matrix row index is not an integer}}
201   float v3 = a[f][f];
202   // expected-error@-1 {{matrix row index is not an integer}}
203   // expected-error@-2 {{matrix column index is not an integer}}
204 
205   // Invalid element type.
206   char *v4 = a[3][4];
207   // expected-error@-1 {{initializing 'char *' with an expression of incompatible type 'float'}}
208 
209   // Indexes outside allowed dimensions.
210   float v5 = a[-1][3];
211   // expected-error@-1 {{matrix row index is outside the allowed range [0, 5)}}
212   float v6 = a[3][-1];
213   // expected-error@-1 {{matrix column index is outside the allowed range [0, 10)}}
214   float v8 = a[-1u][3];
215   // expected-error@-1 {{matrix row index is outside the allowed range [0, 5)}}
216   float v9 = a[5][2];
217   // expected-error@-1 {{matrix row index is outside the allowed range [0, 5)}}
218   float v10 = a[4][10];
219   // expected-error@-1 {{matrix column index is outside the allowed range [0, 10)}}
220   float v11 = a[5][9];
221   // expected-error@-1 {{matrix row index is outside the allowed range [0, 5)}}
222 
223   float v12 = a[3];
224   // expected-error@-1 {{single subscript expressions are not allowed for matrix values}}
225 }
226 
address_of_element(sx5x10_t * a)227 float *address_of_element(sx5x10_t *a) {
228   return &(*a)[0][1];
229   // expected-error@-1 {{address of matrix element requested}}
230 }
231