1 // RUN: %clang_cc1 %s -fenable-matrix -pedantic -std=c++11 -verify -triple=x86_64-apple-darwin9
2 
3 template <typename EltTy, unsigned Rows, unsigned Columns>
4 struct MyMatrix {
5   using matrix_t = EltTy __attribute__((matrix_type(Rows, Columns)));
6 
7   matrix_t value;
8 };
9 
10 template <typename EltTy0, unsigned R0, unsigned C0, typename EltTy1, unsigned R1, unsigned C1>
transpose(MyMatrix<EltTy0,R0,C0> & A)11 typename MyMatrix<EltTy1, R1, C1>::matrix_t transpose(MyMatrix<EltTy0, R0, C0> &A) {
12   char *v1 = __builtin_matrix_transpose(A.value);
13   // expected-error@-1 {{cannot initialize a variable of type 'char *' with an rvalue of type 'unsigned int __attribute__((matrix_type(3, 2)))'}}
14   // expected-error@-2 {{cannot initialize a variable of type 'char *' with an rvalue of type 'unsigned int __attribute__((matrix_type(3, 3)))'}}
15   // expected-error@-3 {{cannot initialize a variable of type 'char *' with an rvalue of type 'unsigned int __attribute__((matrix_type(3, 3)))'}}
16 
17   __builtin_matrix_transpose(A);
18   // expected-error@-1 {{1st argument must be a matrix}}
19   // expected-error@-2 {{1st argument must be a matrix}}
20   // expected-error@-3 {{1st argument must be a matrix}}
21 
22   return __builtin_matrix_transpose(A.value);
23   // expected-error@-1 {{cannot initialize return object of type 'typename MyMatrix<unsigned int, 2U, 3U>::matrix_t' (aka 'unsigned int __attribute__((matrix_type(2, 3)))') with an rvalue of type 'unsigned int __attribute__((matrix_type(3, 2)))'}}
24   // expected-error@-2 {{cannot initialize return object of type 'typename MyMatrix<unsigned int, 2U, 3U>::matrix_t' (aka 'unsigned int __attribute__((matrix_type(2, 3)))') with an rvalue of type 'unsigned int __attribute__((matrix_type(3, 3)))'}}
25   // expected-error@-3 {{cannot initialize return object of type 'typename MyMatrix<float, 3U, 3U>::matrix_t' (aka 'float __attribute__((matrix_type(3, 3)))') with an rvalue of type 'unsigned int __attribute__((matrix_type(3, 3)))'}}
26 }
27 
test_transpose_template(unsigned * Ptr1,float * Ptr2)28 void test_transpose_template(unsigned *Ptr1, float *Ptr2) {
29   MyMatrix<unsigned, 2, 3> Mat1;
30   MyMatrix<unsigned, 3, 3> Mat2;
31   Mat1.value = *((decltype(Mat1)::matrix_t *)Ptr1);
32   Mat1.value = transpose<unsigned, 2, 3, unsigned, 2, 3>(Mat1);
33   // expected-note@-1 {{in instantiation of function template specialization 'transpose<unsigned int, 2U, 3U, unsigned int, 2U, 3U>' requested here}}
34 
35   Mat1.value = transpose<unsigned, 3, 3, unsigned, 2, 3>(Mat2);
36   // expected-note@-1 {{in instantiation of function template specialization 'transpose<unsigned int, 3U, 3U, unsigned int, 2U, 3U>' requested here}}
37 
38   MyMatrix<float, 3, 3> Mat3;
39   Mat3.value = transpose<unsigned, 3, 3, float, 3, 3>(Mat2);
40   // expected-note@-1 {{in instantiation of function template specialization 'transpose<unsigned int, 3U, 3U, float, 3U, 3U>' requested here}}
41 }
42 
43 template <typename EltTy0, unsigned R0, unsigned C0, typename EltTy1, unsigned R1, unsigned C1>
column_major_load(MyMatrix<EltTy0,R0,C0> & A,EltTy0 * Ptr)44 typename MyMatrix<EltTy1, R1, C1>::matrix_t column_major_load(MyMatrix<EltTy0, R0, C0> &A, EltTy0 *Ptr) {
45   char *v1 = __builtin_matrix_column_major_load(Ptr, 9, 4, 10);
46   // expected-error@-1 {{cannot initialize a variable of type 'char *' with an rvalue of type 'unsigned int __attribute__((matrix_type(9, 4)))'}}
47   // expected-error@-2 {{cannot initialize a variable of type 'char *' with an rvalue of type 'unsigned int __attribute__((matrix_type(9, 4)))'}}
48   // expected-error@-3 {{cannot initialize a variable of type 'char *' with an rvalue of type 'float __attribute__((matrix_type(9, 4)))'}}
49 
50   return __builtin_matrix_column_major_load(Ptr, R0, C0, R0);
51   // expected-error@-1 {{cannot initialize return object of type 'typename MyMatrix<unsigned int, 5U, 5U>::matrix_t' (aka 'unsigned int __attribute__((matrix_type(5, 5)))') with an rvalue of type 'unsigned int __attribute__((matrix_type(2, 3)))'}}
52   // expected-error@-2 {{cannot initialize return object of type 'typename MyMatrix<unsigned int, 2U, 3U>::matrix_t' (aka 'unsigned int __attribute__((matrix_type(2, 3)))') with an rvalue of type 'float __attribute__((matrix_type(2, 3)))'}}
53 }
54 
test_column_major_loads_template(unsigned * Ptr1,float * Ptr2)55 void test_column_major_loads_template(unsigned *Ptr1, float *Ptr2) {
56   MyMatrix<unsigned, 2, 3> Mat1;
57   Mat1.value = column_major_load<unsigned, 2, 3, unsigned, 2, 3>(Mat1, Ptr1);
58   // expected-note@-1 {{in instantiation of function template specialization 'column_major_load<unsigned int, 2U, 3U, unsigned int, 2U, 3U>' requested here}}
59   column_major_load<unsigned, 2, 3, unsigned, 5, 5>(Mat1, Ptr1);
60   // expected-note@-1 {{in instantiation of function template specialization 'column_major_load<unsigned int, 2U, 3U, unsigned int, 5U, 5U>' requested here}}
61 
62   MyMatrix<float, 2, 3> Mat2;
63   Mat1.value = column_major_load<float, 2, 3, unsigned, 2, 3>(Mat2, Ptr2);
64   // expected-note@-1 {{in instantiation of function template specialization 'column_major_load<float, 2U, 3U, unsigned int, 2U, 3U>' requested here}}
65 }
66 
constexpr1()67 constexpr int constexpr1() { return 1; }
constexpr_neg1()68 constexpr int constexpr_neg1() { return -1; }
69 
test_column_major_load_constexpr(unsigned * Ptr)70 void test_column_major_load_constexpr(unsigned *Ptr) {
71   (void)__builtin_matrix_column_major_load(Ptr, 2, 2, constexpr1());
72   // expected-error@-1 {{stride must be greater or equal to the number of rows}}
73   (void)__builtin_matrix_column_major_load(Ptr, constexpr_neg1(), 2, 4);
74   // expected-error@-1 {{row dimension is outside the allowed range [1, 1048575]}}
75   (void)__builtin_matrix_column_major_load(Ptr, 2, constexpr_neg1(), 4);
76   // expected-error@-1 {{column dimension is outside the allowed range [1, 1048575]}}
77 }
78 
79 struct IntWrapper {
operator intIntWrapper80   operator int() {
81     return 1;
82   }
83 };
84 
test_column_major_load_wrapper(unsigned * Ptr,IntWrapper & W)85 void test_column_major_load_wrapper(unsigned *Ptr, IntWrapper &W) {
86   (void)__builtin_matrix_column_major_load(Ptr, W, 2, 2);
87   // expected-error@-1 {{row argument must be a constant unsigned integer expression}}
88   (void)__builtin_matrix_column_major_load(Ptr, 2, W, 2);
89   // expected-error@-1 {{column argument must be a constant unsigned integer expression}}
90 }
91 
92 template <typename T, unsigned R, unsigned C, unsigned S>
test_column_major_load_temp(T Ptr)93 void test_column_major_load_temp(T Ptr) {
94   (void)__builtin_matrix_column_major_load(Ptr, R, C, S);
95 }
96 
call_column_major_load_temp(unsigned * Ptr,unsigned X)97 void call_column_major_load_temp(unsigned *Ptr, unsigned X) {
98   (void)__builtin_matrix_column_major_load(Ptr, X, X, X);
99   // expected-error@-1 {{row argument must be a constant unsigned integer expression}}
100   // expected-error@-2 {{column argument must be a constant unsigned integer expression}}
101   (void)__builtin_matrix_column_major_load(X, 2, 2, 2);
102   // expected-error@-1 {{1st argument must be a pointer to a valid matrix element type}}
103 }
104 
105 template <typename EltTy0, unsigned R0, unsigned C0, typename PtrTy>
column_major_store(MyMatrix<EltTy0,R0,C0> & A,PtrTy Ptr,unsigned Stride)106 void column_major_store(MyMatrix<EltTy0, R0, C0> &A, PtrTy Ptr, unsigned Stride) {
107   __builtin_matrix_column_major_store(A.value, Ptr, Stride);
108   // expected-error@-1 {{the pointee of the 2nd argument must match the element type of the 1st argument ('float' != 'unsigned int')}}
109 }
110 
111 template <typename MTy, typename PtrTy, unsigned Stride>
column_major_store(MTy & A,PtrTy Ptr)112 void column_major_store(MTy &A, PtrTy Ptr) {
113   __builtin_matrix_column_major_store(A.value, Ptr, Stride);
114   // expected-error@-1 {{stride must be greater or equal to the number of rows}}
115 }
116 
test_column_major_stores_template(MyMatrix<unsigned,2,3> & M1,unsigned * Ptr1,MyMatrix<float,3,4> & M2,float * Ptr2)117 void test_column_major_stores_template(MyMatrix<unsigned, 2, 3> &M1, unsigned *Ptr1, MyMatrix<float, 3, 4> &M2, float *Ptr2) {
118   column_major_store(M1, Ptr2, 10);
119   // expected-note@-1 {{in instantiation of function template specialization 'column_major_store<unsigned int, 2U, 3U, float *>' requested here}}
120 
121   column_major_store<decltype(M2), float *, 1>(M2, Ptr2);
122   // expected-note@-1 {{in instantiation of function template specialization 'column_major_store<MyMatrix<float, 3, 4> &, float *, 1U>' requested here}}
123 }
124 
125 template <typename EltTy0, unsigned R0, unsigned C0, typename EltTy1>
column_major_store(MyMatrix<EltTy0,R0,C0> & A,EltTy1 * Ptr)126 void column_major_store(MyMatrix<EltTy0, R0, C0> &A, EltTy1 *Ptr) {
127   __builtin_matrix_column_major_store(A.value, Ptr, 1);
128   // expected-error@-1 3 {{stride must be greater or equal to the number of rows}}
129   // expected-error@-2 {{the pointee of the 2nd argument must match the element type of the 1st argument ('float' != 'unsigned int')}}
130   // expected-error@-3 {{the pointee of the 2nd argument must match the element type of the 1st argument ('unsigned int' != 'float')}}
131 
132   char *s;
133   return __builtin_matrix_column_major_store(A.value, s, 20);
134   // expected-error@-1 {{the pointee of the 2nd argument must match the element type of the 1st argument ('char' != 'unsigned int')}}
135   // expected-error@-2 {{the pointee of the 2nd argument must match the element type of the 1st argument ('char' != 'unsigned int')}}
136   // expected-error@-3 {{he pointee of the 2nd argument must match the element type of the 1st argument ('char' != 'float')}}
137 }
138 
test_column_major_store_template(unsigned * Ptr1,float * Ptr2)139 void test_column_major_store_template(unsigned *Ptr1, float *Ptr2) {
140   MyMatrix<unsigned, 2, 3> Mat1;
141   column_major_store<unsigned, 2, 3, unsigned>(Mat1, Ptr1);
142   // expected-note@-1 {{in instantiation of function template specialization 'column_major_store<unsigned int, 2U, 3U, unsigned int>'}}
143   column_major_store<unsigned, 2, 3, float>(Mat1, Ptr2);
144   // expected-note@-1 {{in instantiation of function template specialization 'column_major_store<unsigned int, 2U, 3U, float>'}}
145 
146   MyMatrix<float, 2, 3> Mat2;
147   column_major_store<float, 2, 3, unsigned>(Mat2, Ptr1);
148   // expected-note@-1 {{in instantiation of function template specialization 'column_major_store<float, 2U, 3U, unsigned int>'}}
149 }
150 
test_column_major_store_constexpr(unsigned * Ptr,MyMatrix<unsigned,3,3> & M)151 void test_column_major_store_constexpr(unsigned *Ptr, MyMatrix<unsigned, 3, 3> &M) {
152   __builtin_matrix_column_major_store(M.value, Ptr, constexpr1());
153   // expected-error@-1 {{stride must be greater or equal to the number of rows}}
154   __builtin_matrix_column_major_store(constexpr1(), Ptr, 1);
155   // expected-error@-1 {{1st argument must be a matrix}}
156   __builtin_matrix_column_major_store(M.value, constexpr1(), 1);
157   // expected-error@-1 {{2nd argument must be a pointer to a valid matrix element type}}
158   // expected-error@-2 {{stride must be greater or equal to the number of rows}}
159 }
160 
test_column_major_store_wrapper(unsigned * Ptr,MyMatrix<unsigned,3,3> & M,IntWrapper & W)161 void test_column_major_store_wrapper(unsigned *Ptr, MyMatrix<unsigned, 3, 3> &M, IntWrapper &W) {
162   __builtin_matrix_column_major_store(M.value, Ptr, W);
163 
164   __builtin_matrix_column_major_store(W, Ptr, W);
165   // expected-error@-1 {{1st argument must be a matrix}}
166 }
167