Debian Codegraph @

clang/docs/MatrixTypes.rst

raw
1 ==================
2 Matrix Types
3 ==================
4
5 .. contents::
6 :local:
7
8 .. _matrixtypes:
9
10 Clang provides a C/C++ language extension that allows users to directly express
11 fixed-size 2-dimensional matrices as language values and perform arithmetic on
12 them.
13
14 This feature is currently experimental, and both its design and its
15 implementation are in flux.
16
17 Draft Specification
18 ===================
19
20 Matrix Type
21 -----------
22
23 A matrix type is a scalar type with an underlying *element type*, a constant
24 number of *rows*, and a constant number of *columns*. Matrix types with the same
25 element type, rows, and columns are the same type. A value of a matrix type
26 includes storage for ``rows * columns`` values of the *element type*. The
27 internal layout, overall size and alignment are implementation-defined.
28
29 The maximum of the product of the number of rows and columns is
30 implementation-defined. If that implementation-defined limit is exceeded, the
31 program is ill-formed.
32
33 Currently, the element type of a matrix is only permitted to be one of the
34 following types:
35
36 * an integer type (as in C23 6.2.5p22), but excluding enumerated types, ``bool``,
37 and ``_BitInt`` types whose width is not a power of 2;
38 * the standard floating types ``float`` or ``double``;
39 * a half-precision floating point type, if one is supported on the target.
40
41 Other types may be supported in the future.
42
43 Matrix Type Attribute
44 ---------------------
45
46 Matrix types can be declared by adding the ``matrix_type`` attribute to the
47 declaration of a *typedef* (or a C++ alias declaration). The underlying type
48 of the *typedef* must be a valid matrix element type. The
49 attribute takes two arguments, both of which must be integer constant
50 expressions that evaluate to a value greater than zero. The first specifies the
51 number of rows, and the second specifies the number of columns. The underlying
52 type of the *typedef* becomes a matrix type with the given dimensions and an
53 element type of the former underlying type.
54
55 If a declaration of a *typedef-name* has a ``matrix_type`` attribute, then all
56 declarations of that *typedef-name* shall have a matrix_type attribute with the
57 same element type, number of rows, and number of columns.
58
59 Standard Conversions
60 --------------------
61
62 The standard conversions are extended as follows. Note that these conversions
63 are intentionally not listed as satisfying the constraints for assignment,
64 which is to say, they are only permitted as explicit casts, not as implicit
65 conversions.
66
67 A value of matrix type can be converted to another matrix type if the number of
68 rows and columns are the same and the value's elements can be converted to the
69 element type of the result type. The result is a matrix where each element is
70 the converted corresponding element.
71
72 A value of any real type (as in C23 6.2.5p14) can be converted to a matrix type
73 if it can be converted to the element type of the matrix. The result is a
74 matrix where all elements are the converted original value.
75
76 If the number of rows or columns differ between the original and resulting
77 type, the program is ill-formed.
78
79
80 Arithmetic Conversions
81 ----------------------
82
83 The usual arithmetic conversions are extended as follows.
84
85 Insert at the start:
86
87 * If both operands are of matrix type, no arithmetic conversion is performed.
88 * If one operand is of matrix type and the other operand is of a real type,
89 convert the real type operand to the matrix type
90 according to the standard conversion rules.
91
92 Matrix Type Element Access Operator
93 -----------------------------------
94
95 An expression of the form ``E1 [E2] [E3]``, where ``E1`` has matrix type ``cv
96 M``, is a matrix element access expression. Let ``T`` be the element type
97 of ``M``, and let ``R`` and ``C`` be the number of rows and columns in ``M``
98 respectively. The index expressions shall have integral or unscoped
99 enumeration type and shall not be uses of the comma operator unless
100 parenthesized. The first index expression shall evaluate to a
101 non-negative value less than ``R``, and the second index expression shall
102 evaluate to a non-negative value less than ``C``, or else the expression has
103 undefined behavior. If ``E1`` is a prvalue, the result is a prvalue with type
104 ``T`` and is the value of the element at the given row and column in the matrix.
105 Otherwise, the result is a glvalue with type ``cv T`` and with the same value
106 category as ``E1`` which refers to the element at the given row and column in
107 the matrix.
108
109 A single subscript expression into a matrix is legal in HLSL and denotes a
110 vector for the specified row lane, but is ill-formed in C and C++.
111
112 **Note**: We considered providing an expression of the form
113 ``postfix-expression [expression]`` to access columns of a matrix. We think
114 that such an expression would be problematic once both column and row major
115 matrixes are supported: depending on the memory layout, either accessing columns
116 or rows can be done efficiently, but not both. Instead, we propose to provide
117 builtins to extract rows and columns from a matrix. This makes the operations
118 more explicit.
119
120 Matrix Type Binary Operators
121 ----------------------------
122
123 Given two matrixes, the ``+`` and ``-`` operators perform element-wise addition
124 and subtraction, while the ``*`` operator performs matrix multiplication.
125 ``+``, ``-``, ``*``, and ``/`` can also be used with a matrix and a scalar
126 value, applying the operation to each element of the matrix.
127
128 Earlier versions of this extension did not support division by a scalar.
129 You can test for the availability of this feature with
130 ``__has_extension(matrix_types_scalar_division)``.
131
132 For the expression ``M1 BIN_OP M2`` where
133
134 * ``BIN_OP`` is one of ``+`` or ``-``, one of ``M1`` and ``M2`` is of matrix
135 type, and the other is of matrix type or real type; or
136 * ``BIN_OP`` is ``*``, one of ``M1`` and ``M2`` is of matrix type, and the
137 other is of a real type; or
138 * ``BIN_OP`` is ``/``, ``M1`` is of matrix type, and ``M2`` is of a real type:
139
140 * The usual arithmetic conversions are applied to ``M1`` and ``M2``. [ Note: if ``M1`` or
141 ``M2`` are of a real type, they are broadcast to matrices here. — end note ]
142 * ``M1`` and ``M2`` shall be of the same matrix type.
143 * The result is equivalent to Res in the following where col is the number of
144 columns and row is the number of rows in the matrix type:
145
146 .. code-block:: c++
147
148 decltype(M1) Res;
149 for (int C = 0; C < col; ++C)
150 for (int R = 0; R < row; ++R)
151 Res[R][C] = M1[R][C] BIN_OP M2[R][C];
152
153 Given the expression ``M1 * M2`` where ``M1`` and ``M2`` are of matrix type:
154
155 * The usual arithmetic conversions are applied to ``M1`` and ``M2``.
156 * The type of ``M1`` shall have the same number of columns as the type of ``M2`` has
157 rows. The element types of ``M1`` and ``M2`` shall be the same type.
158 * The resulting type, ``MTy``, is a matrix type with the common element type,
159 the number of rows of ``M1`` and the number of columns of ``M2``.
160 * The result is equivalent to ``Res`` in the following where ``EltTy`` is the
161 element type of ``MTy``, ``col`` is the number of columns, ``row`` is the
162 number of rows in ``MTy`` and ``inner`` is the number of columns of ``M1``:
163
164 .. code-block:: c++
165
166 MTy Res;
167 for (int C = 0; C < col; ++C) {
168 for (int R = 0; R < row; ++R) {
169 EltTy Elt = 0;
170 for (int K = 0; K < inner; ++K) {
171 Elt += M1[R][K] * M2[K][C];
172 }
173 Res[R][C] = Elt;
174 }
175
176 All operations on matrix types match the behavior of the element type with
177 respect to signed overflows.
178
179 With respect to floating-point contraction, rounding and environment rules,
180 operations on matrix types match the behavior of the elementwise operations
181 in the corresponding expansions provided above.
182
183 Operations on floating-point matrices have the same rounding and floating-point
184 environment behavior as ordinary floating-point operations in the expression's
185 context. For the purposes of floating-point contraction, all calculations done
186 as part of a matrix operation are considered intermediate operations, and their
187 results need not be rounded to the format of the element type until the final
188 result in the containing expression. This is subject to the normal restrictions
189 on contraction, such as ``#pragma STDC FP_CONTRACT``.
190
191 For the ``+=``, ``-=`` and ``*=`` operators the semantics match their expanded
192 variants.
193
194 Matrix Type Builtin Operations
195 ------------------------------
196
197 Each matrix type supports a collection of builtin expressions that look like
198 function calls but do not form an overload set. Here they are described as
199 function declarations with rules for how to construct the argument list types
200 and return type and the library description elements from
201 [library.description.structure.specifications]/3 in the C++ standard.
202
203 Definitions:
204
205 * *M*, *M1*, *M2*, *M3* - Matrix types
206 * *T* - Element type
207 * *row*, *col* - Row and column arguments respectively.
208
209
210 ``M2 __builtin_matrix_transpose(M1 matrix)``
211
212 **Remarks**: The return type is a cv-unqualified matrix type that has the same
213 element type as ``M1`` and has the same number of rows as ``M1`` has columns and
214 the same number of columns as ``M1`` has rows.
215
216 **Returns**: A matrix ``Res`` equivalent to the code below, where ``col`` refers to the
217 number of columns of ``M``, and ``row`` to the number of rows of ``M``.
218
219 **Effects**: Equivalent to:
220
221 .. code-block:: c++
222
223 M Res;
224 for (int C = 0; C < col; ++C)
225 for (int R = 0; R < row; ++R)
226 Res[C][R] = matrix[R][C];
227
228
229 ``M __builtin_matrix_column_major_load(T *ptr, size_t row, size_t col, size_t columnStride)``
230
231 **Mandates**: ``row`` and ``col`` shall be integral constants greater than 0.
232
233 **Preconditions**: ``columnStride`` is greater than or equal to ``row``.
234
235 **Remarks**: The return type is a cv-unqualified matrix type with an element
236 type of the cv-unqualified version of ``T`` and a number of rows and columns equal
237 to ``row`` and ``col`` respectively. The parameter ``columnStride`` is optional
238 and if omitted ``row`` is used as ``columnStride``.
239
240 **Returns**: A matrix ``Res`` equivalent to:
241
242 .. code-block:: c++
243
244 M Res;
245 for (size_t C = 0; C < col; ++C) {
246 for (size_t R = 0; R < row; ++K)
247 Res[R][C] = ptr[R];
248 ptr += columnStride
249 }
250
251
252 ``void __builtin_matrix_column_major_store(M matrix, T *ptr, size_t columnStride)``
253
254 **Preconditions**: ``columnStride`` is greater than or equal to the number of rows in ``M``.
255
256 **Remarks**: The type ``T`` is the const-unqualified version of the matrix
257 argument’s element type. The parameter ``columnStride`` is optional and if
258 omitted, the number of rows of ``M`` is used as ``columnStride``.
259
260 **Effects**: Equivalent to:
261
262 .. code-block:: c++
263
264 for (size_t C = 0; C < columns in M; ++C) {
265 for (size_t R = 0; R < rows in M; ++K)
266 ptr[R] = matrix[R][C];
267 ptr += columnStride
268 }
269
270
271 TODOs
272 -----
273
274 TODO: Does it make sense to allow M::element_type, M::rows, and M::columns
275 where M is a matrix type? We don’t support this anywhere else, but it’s
276 convenient. The alternative is using template deduction to extract this
277 information. Also add spelling for C.
278
279 Future Work: Initialization syntax.
280
281
282 Decisions for the Implementation in Clang
283 =========================================
284
285 This section details decisions taken for the implementation in Clang and is not
286 part of the draft specification.
287
288 The elements of a value of a matrix type are laid out in column-major order
289 without padding.
290
291 To change memory layout to row major use the `-fmatrix-memory-layout` flag.
292 This flag supports two flag argument values either `column-major` or
293 `row-major` used like so `-fmatrix-memory-layout=column-major`.`
294
295 We propose to provide a Clang option to override this behavior and allow
296 contraction of those operations (e.g. *-ffp-contract=matrix*).
297
298 TODO: Specify how matrix values are passed to functions.