<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div dir="auto" class=""><div dir="ltr" class=""><div dir="ltr" class=""><span class="">Thanks for the feedback! I’ve responded inline.<br class=""></span></div><div dir="ltr" class=""><br class=""><blockquote type="cite" class="">On 16 Jan 2020, at 23:06, Richard Smith <<a href="mailto:richard@metafoo.co.uk" class="">richard@metafoo.co.uk</a>> wrote:<br class=""><br class=""></blockquote></div><blockquote type="cite" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class="">On Fri, 20 Dec 2019 at 10:32, Florian Hahn via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" class="">cfe-dev@lists.llvm.org</a>> wrote:<br class=""></div><div class="gmail_quote"><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote"><div style="overflow-wrap: break-word;" class=""><div class=""><span class=""><span style="color:rgb(51,51,51);background-color:rgb(255,255,255)" class="">Hello,</span></span></div><span class=""><span style="color:rgb(51,51,51);background-color:rgb(255,255,255)" class=""><div class=""><span class=""><span style="color:rgb(51,51,51);background-color:rgb(255,255,255)" class=""><br class=""></span></span></div>This is a Clang-focused follow up to the original proposal on llvm-dev (</span><a href="http://lists.llvm.org/pipermail/llvm-dev/2019-October/136240.html" style="background-color:rgb(255,255,255)" target="_blank" class="">http://lists.llvm.org/pipermail/llvm-dev/2019-October/136240.html</a><span style="color:rgb(51,51,51);background-color:rgb(255,255,255)" class="">). On the LLVM side, we recently landed the first commit adding matrix intrinsics as proposed.</span><br style="color:rgb(51,51,51);background-color:rgb(255,255,255)" class=""><br style="color:rgb(51,51,51);background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">On the Clang side, we would like to propose adding support for matrix math operations to Clang. This includes adding a new matrix type (similar to ext_vector_type) and a set of builtins to operate on values of the matrix type.</span><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">Our main motivation for the matrix support in Clang is to give users a way to</span><br style="background-color:rgb(255,255,255)" class=""></span><div style="margin:12px 0px;background-color:rgb(255,255,255)" class=""><ul id="gmail-m_7720800767577388321GXO9CAfukUr" style="line-height:20px;padding:0px 0px 0px 40px" class=""><li id="gmail-m_7720800767577388321GXO9CAroPnP" value="1" style="margin-bottom:0.4em" class=""><span class=""><span class="">Guarantee generation of high-quality code for matrix operations. For isolated operations, we can guarantee vector code generation suitable for the target. For trees of operations, the proposed value type helps with eliminating temporary loads & stores.</span> <br class=""></span></li><li id="gmail-m_7720800767577388321GXO9CALHQyJ" style="margin-bottom:0.4em" class=""><span class=""><span class="">Make use of specialized matrix ISA extensions, like the new matrix instructions in ARM v8.6 or various proprietary matrix accelerators, in their C/C++ code.</span> <br class=""></span></li><li id="gmail-m_7720800767577388321GXO9CAsv87p" style="margin-bottom:0.4em" class=""><span class=""><span class="">Move optimizations from matrix wrapper libraries into the compiler. </span><span class="">We use it internally to simplify an Eigen-style matrix library, by relying on LLVM for generating tiled & fused loops for matrix operations.</span> <br class=""></span></li></ul></div><span class=""><span style="background-color:rgb(255,255,255)" class="">The rest of this RFC is structured as follows: First we propose a draft specification for the matrix type and accompanying builtins. Next we show an example of how matrix operations will be lowered by Clang, followed by a discussion of the contributing criteria for new extensions.  We wrap up the RFC by discussing possible extensions to the matrix type.</span></span><br style="color:rgb(51,51,51);font-size:16px;background-color:rgb(255,255,255)" class=""><h2 id="gmail-m_7720800767577388321GXO9CAMHcpM" style="margin:1.25em 0px 0.75em;line-height:22px;color:rgb(51,51,51);background-color:rgb(255,255,255)" class=""><font size="3" class="">Draft Specification</font></h2><h3 id="gmail-m_7720800767577388321GXO9CAQnvex" style="margin:1.25em 0px 0.75em;line-height:16px;background-color:rgb(255,255,255)" class=""><font size="3" class="">Matrix TYPE Attribute</font></h3><span class=""><span style="background-color:rgb(255,255,255)" class="">The </span><i style="background-color:rgb(255,255,255)" class="">attribute-token</i><span style="background-color:rgb(255,255,255)" class=""> </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">matrix_type</code><span style="background-color:rgb(255,255,255)" class=""> is used to declare a matrix type. It shall appear at most once in each </span><i style="background-color:rgb(255,255,255)" class="">attribute-list</i><span style="background-color:rgb(255,255,255)" class="">. The attribute shall only appertain to a </span><i style="background-color:rgb(255,255,255)" class="">typedef-name</i><span style="background-color:rgb(255,255,255)" class=""> of a typedef of a non-volatile type that is a </span><i style="background-color:rgb(255,255,255)" class="">signed integer type</i><span style="background-color:rgb(255,255,255)" class="">, an </span><i style="background-color:rgb(255,255,255)" class="">unsigned integer type</i><span style="background-color:rgb(255,255,255)" class="">, or a </span><i style="background-color:rgb(255,255,255)" class="">floating-point type</i><span style="background-color:rgb(255,255,255)" class="">. An </span><i style="background-color:rgb(255,255,255)" class="">attribute-argument-clause</i><span style="background-color:rgb(255,255,255)" class=""> must be present and it shall have the form:</span><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">(<i class="">constant-expression</i>, <i class="">constant-expression</i>)</code><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">Both </span><i style="background-color:rgb(255,255,255)" class="">constant-expression</i><span style="background-color:rgb(255,255,255)" class="">s shall be a positive non-zero integral constant expressions. The maximum of the product of the constants is implementation defined. If that implementation defined limit is exceeded, the program is ill-formed.</span><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">An </span><i style="background-color:rgb(255,255,255)" class="">attribute</i><span style="background-color:rgb(255,255,255)" class=""> of the form </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">matrix_type(</code><i style="background-color:rgb(255,255,255)" class=""><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">R</code></i><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">, </code><i style="background-color:rgb(255,255,255)" class=""><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">C</code></i><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">)</code><span style="background-color:rgb(255,255,255)" class=""> forms a matrix type with an element type of the cv-qualified type the attribute appertains to and </span><i style="background-color:rgb(255,255,255)" class="">R</i><span style="background-color:rgb(255,255,255)" class=""> rows and </span><i style="background-color:rgb(255,255,255)" class="">C</i><span style="background-color:rgb(255,255,255)" class=""> columns.</span><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">If a declaration of a </span><i style="background-color:rgb(255,255,255)" class="">typedef-name</i><span style="background-color:rgb(255,255,255)" class=""> has a </span><i style="background-color:rgb(255,255,255)" class="">matrix_type</i><span style="background-color:rgb(255,255,255)" class=""> attribute, then all declaration of that </span><i style="background-color:rgb(255,255,255)" class="">typedef-name</i><span style="background-color:rgb(255,255,255)" class=""> shall have a </span><i style="background-color:rgb(255,255,255)" class="">matrix_type</i><span style="background-color:rgb(255,255,255)" class=""> attribute with the same element type, number of rows, and number of columns.</span></span><div class=""><span style="font-size:14px" class=""><br style="background-color:rgb(255,255,255)" class=""></span><h3 id="gmail-m_7720800767577388321GXO9CArLQor" style="margin:1.25em 0px 0.75em;line-height:16px;background-color:rgb(255,255,255)" class=""><font size="3" class="">Matrix Type</font></h3><span class=""><span style="background-color:rgb(255,255,255)" class="">A matrix type has an underlying </span><i style="background-color:rgb(255,255,255)" class="">element type</i><span style="background-color:rgb(255,255,255)" class="">, a constant number of rows, and a constant number of columns. Matrix types with the same element type, rows, and columns are the same type. A value of a matrix type contains </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">rows * columns</code><span style="background-color:rgb(255,255,255)" class=""> values of the </span><i style="background-color:rgb(255,255,255)" class="">element type</i><span style="background-color:rgb(255,255,255)" class=""> laid out in column-major order without padding in a way compatible with an array of at least that many elements of the underlying </span><i style="background-color:rgb(255,255,255)" class="">element type</i><span style="background-color:rgb(255,255,255)" class="">.</span><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">A matrix type is a </span><i style="background-color:rgb(255,255,255)" class="">scalar type </i><span style="background-color:rgb(255,255,255)" class="">with the same alignment as its underlying element type, but objects of matrix type are not usable in constant expressions.</span><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">TODO: Allow reinterpret_cast from pointer to element type. Make aliasing work.</span><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">Future Work: Initialization syntax.</span><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">Future Work: Access syntax. </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">m[col][row]</code><span style="background-color:rgb(255,255,255)" class="">.</span><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">Future Work: Conversions between matrix types with const qualified and unqualified element types.</span><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">Future Work: Conversions between matrix types with different element types.</span></span></div><div class=""><span style="font-size:14px" class=""><br style="background-color:rgb(255,255,255)" class=""></span><h3 id="gmail-m_7720800767577388321GXO9CABJGdF" style="margin:1.25em 0px 0.75em;line-height:16px;background-color:rgb(255,255,255)" class=""><font size="3" class="">Matrix Type builtin Operations</font></h3><span class=""><span style="background-color:rgb(255,255,255)" class="">Each matrix type supports a collection of builtin expressions that look like function calls but do not form an overload set. Here they are described as function declarations with rules for how to construct the argument list types and return type and the library description elements from [library.description.structure.specifications]/3 in the C++ standard. </span><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">Definitions:</span><br style="background-color:rgb(255,255,255)" class=""></span><div style="margin:12px 0px;background-color:rgb(255,255,255)" class=""><ul id="gmail-m_7720800767577388321GXO9CA079ga" style="line-height:20px;padding:0px 0px 0px 40px" class=""><li id="gmail-m_7720800767577388321GXO9CAXp3Qu" value="1" style="margin-bottom:0.4em" class=""><span class=""><i class="">M, M1, M2, M3</i> - Matrix types <br class=""></span></li><li id="gmail-m_7720800767577388321GXO9CANvXAk" style="margin-bottom:0.4em" class=""><span class=""><i class="">T</i> - Element type <br class=""></span></li><li id="gmail-m_7720800767577388321GXO9CApPDi5" style="margin-bottom:0.4em" class=""><span class="">row, col - Row and column arguments respectively. </span></li></ul></div><span class=""><span style="background-color:rgb(255,255,255)" class="">All operations on matrix types match the behavior of the underlying element type with respect to signed overflows.</span></span><div class=""><font size="3" class=""><b style="background-color:rgb(255,255,255)" class=""></b></font></div></div></div></blockquote><div class=""><br class=""></div><div class="">Do you anticipate providing builtin operators for matrices? If not, then the utility of a dedicated type and `matrix_type` attribute seems greatly diminished: the builtin matrix operators could instead -- in principle -- operate on a suitable vector type (either as a flat vector, matching the LLVM IR model, or as a vector of vectors, to support two-dimensional indexing). I think your proposal should express why those would be inferior choices (eg, do matrix types have different calling conventions, alignment requirements, ... on some target? Do you intend to provide matrix x matrix multiplication and matrix x vector multiplication via the * operator in the future?). Adding *only* builtin functions and no new matrix types would be a substantial simplification in the proposal.</div></div></div></div></blockquote><div dir="ltr" class=""><br class=""></div>I think it would make sense to provide builtin operators instead of the proposed builtins for math operations. Same for element insertion/extraction. However I am not sure how to provide the strided matrix load/store as operators. Would it be OK to just have builtins for those? The reason we went for builtins initially was that we thought that might make the proposal a bit more lightweight, but it sounds like builtin operators would be preferred with the type.<br class=""><br class="">I do not think ext_vector_type would be suitable for our proposal, as it matches LLVM’s vector alignment and the matrix type should match the alignment of the underlying data type, to allow easy interaction with existing matrix libraries. </div><div dir="ltr" class=""><br class=""></div><div dir="ltr" class="">A vector of vectors should work in principle, as long as we could fix both dimensions on a type level. Not having the dimensions guaranteed by the type would have a negative impact on the user-experience I think, as we would, for example, loose the ability to type-check if the dimensions match the operators and users would have to provide the dimensions for certain operations. Also, it would make supporting 3+ dimensions a bit more tricky.<br class=""><blockquote type="cite" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div class="gmail_quote"><div class=""><br class=""></div><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote"><div style="overflow-wrap: break-word;" class=""><div class=""><div class=""><font size="3" class=""><b style="background-color:rgb(255,255,255)" class="">Element Operations</b><br style="background-color:rgb(255,255,255)" class=""></font><br style="font-size:16px;background-color:rgb(255,255,255)" class=""><span class=""><span style="background-color:rgb(255,255,255)" class=""><span style="font-size:14px" class="">P</span>reconditions: </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">row</code><span style="background-color:rgb(255,255,255)" class=""> and </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">col</code><span style="background-color:rgb(255,255,255)" class=""> are in the ranges [0, rows in </span><i style="background-color:rgb(255,255,255)" class="">M</i><span style="background-color:rgb(255,255,255)" class="">) and [0, columns in </span><i style="background-color:rgb(255,255,255)" class="">M</i><span style="background-color:rgb(255,255,255)" class="">) respectively.</span><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class=""><i class="">M</i> __builtin_matrix_insert(<i class="">M</i> matrix, int row, int col, <i class="">T</i> elt)</code><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">Remarks: The return type and the type </span><i style="background-color:rgb(255,255,255)" class="">T</i><span style="background-color:rgb(255,255,255)" class=""> are inferred from the cv-unqualified type of the </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">matrix</code><span style="background-color:rgb(255,255,255)" class=""> argument and its cv-unqualified </span><i style="background-color:rgb(255,255,255)" class="">element type</i><span style="background-color:rgb(255,255,255)" class=""> respectively.</span><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">Returns: a copy of </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">matrix</code><span style="background-color:rgb(255,255,255)" class=""> with the element at the specified row and column set to </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">elt</code><span style="background-color:rgb(255,255,255)" class="">.</span><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class=""><i class="">T</i> __builtin_matrix_extract(<i class="">M</i> matrix, int row, int col)</code><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">The return type is inferred from the cv-unqualified type of the </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">matrix</code><span style="background-color:rgb(255,255,255)" class=""> argument’s </span><i style="background-color:rgb(255,255,255)" class="">element type</i><span style="background-color:rgb(255,255,255)" class="">.</span><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">Returns: a copy of the element at the specified row and column.</span></span><div class=""><span class=""><br style="background-color:rgb(255,255,255)" class=""></span><br style="font-size:16px;background-color:rgb(255,255,255)" class=""><font size="3" class=""><b style="background-color:rgb(255,255,255)" class="">Simple Binary Operations</b><br style="background-color:rgb(255,255,255)" class=""></font><br style="font-size:16px;background-color:rgb(255,255,255)" class=""><span class=""><span style="background-color:rgb(255,255,255)" class="">For the following binary operations </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">matrix1</code><span style="background-color:rgb(255,255,255)" class=""> and </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">matrix2</code><span style="background-color:rgb(255,255,255)" class=""> shall be matrix values of the same cv-unqualified type, and the return type is the cv-unqualified version of that type. </span><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class=""><i class="">M</i> __builtin_matrix_add(<i class="">M</i> matrix1, <i class="">M</i> matrix2)</code><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">Returns: A matrix </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">Res</code><span style="background-color:rgb(255,255,255)" class=""> equivalent to the code below, where </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">col</code><span style="background-color:rgb(255,255,255)" class=""> refers to the number of columns of M, </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">row</code><span style="background-color:rgb(255,255,255)" class=""> to the number of rows of M and </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">EltTy</code><span style="background-color:rgb(255,255,255)" class=""> to the element type of M.</span><br style="background-color:rgb(255,255,255)" class=""></span><pre id="gmail-m_7720800767577388321GXO9CAZZ7rw" style="background-color:rgb(242,242,242);line-height:20px;padding:5px 5px 5px 30px;white-space:nowrap;margin-top:1em;margin-bottom:1em" class=""><font face="Helvetica" class=""><i class="">M</i> Res;<br class="">for (int C = 0; C < col; ++C) {<br class="">  for (int R = 0; R < row; ++R) {<br class="">    EltTy Elt = __builtin_matrix_extract(matrix1, R, C) + <br class="">                     __builtin_matrix_extract(matrix2, R, C)<br class="">    Res = __builtin_matrix_insert(Res, R, C, Elt);<br class="">  }<br class="">}</font></pre><span class=""><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class=""><i class="">M</i> __builtin_matrix_sub(<i class="">M</i> matrix1, <i class="">M</i> matrix2)</code><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">Returns: A matrix </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">Res</code><span style="background-color:rgb(255,255,255)" class=""> equivalent to the code below, where </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">col</code><span style="background-color:rgb(255,255,255)" class=""> refers to the number of columns of M, </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">row</code><span style="background-color:rgb(255,255,255)" class=""> to the number of rows of M and </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">EltTy</code><span style="background-color:rgb(255,255,255)" class=""> to the element type of M.</span><br style="background-color:rgb(255,255,255)" class=""></span><pre id="gmail-m_7720800767577388321GXO9CApF8IV" style="background-color:rgb(242,242,242);line-height:20px;padding:5px 5px 5px 30px;white-space:nowrap;margin-top:1em;margin-bottom:1em" class=""><font face="Helvetica" class=""><i class="">M</i> Res;<br class="">for (int C = 0; C < col; ++C) {<br class="">  for (int R = 0; R < row; ++R) {<br class="">    EltTy Elt = __builtin_matrix_extract(matrix1, R, C) - <br class="">                     __builtin_matrix_extract(matrix2, R, C)<br class="">    Res = __builtin_matrix_insert(Res, R, C, Elt);<br class="">  }<br class="">}</font></pre><div class=""><br class=""></div><div class=""><br class=""></div><b style="font-size:16px;background-color:rgb(255,255,255)" class="">Other Operations</b><br style="font-size:16px;background-color:rgb(255,255,255)" class=""><br style="font-size:16px;background-color:rgb(255,255,255)" class=""><span class=""><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class=""><i class="">M3</i> __builtin_matrix_multiply(<i class="">M1</i> matrix1, <i class="">M2</i> matrix2)</code><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">Mandates: </span><i style="background-color:rgb(255,255,255)" class="">M1</i><span style="background-color:rgb(255,255,255)" class=""> and </span><i style="background-color:rgb(255,255,255)" class="">M2</i><span style="background-color:rgb(255,255,255)" class=""> shall be matrix types with the same cv-unqualified element type and </span><i style="background-color:rgb(255,255,255)" class="">M1</i><span style="background-color:rgb(255,255,255)" class="">’s number of columns matching </span><i style="background-color:rgb(255,255,255)" class="">M2</i><span style="background-color:rgb(255,255,255)" class="">’s number of row.</span><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">Remarks: The return type is a cv-unqualified matrix type with the same element type as </span><i style="background-color:rgb(255,255,255)" class="">M1</i><span style="background-color:rgb(255,255,255)" class=""> and </span><i style="background-color:rgb(255,255,255)" class="">M2</i><span style="background-color:rgb(255,255,255)" class=""> if both </span><i style="background-color:rgb(255,255,255)" class="">M1</i><span style="background-color:rgb(255,255,255)" class=""> and </span><i style="background-color:rgb(255,255,255)" class="">M2</i><span style="background-color:rgb(255,255,255)" class="">’s element type is const, or the cv-unqualified element type otherwise, and with the same number of rows as </span><i style="background-color:rgb(255,255,255)" class="">M1</i><span style="background-color:rgb(255,255,255)" class=""> and the same number of columns as </span><i style="background-color:rgb(255,255,255)" class="">M2</i><span style="background-color:rgb(255,255,255)" class="">.</span><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">Returns: A matrix </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">Res</code><span style="background-color:rgb(255,255,255)" class=""> equivalent to the code below, where </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">col</code><span style="background-color:rgb(255,255,255)" class=""> refers to the number of columns of M, </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">row</code><span style="background-color:rgb(255,255,255)" class=""> to the number of rows of M, </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">EltTy</code><span style="background-color:rgb(255,255,255)" class=""> to the element type of M and </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">inner</code><span style="background-color:rgb(255,255,255)" class=""> refers to the number of columns of M1.</span><br style="background-color:rgb(255,255,255)" class=""></span><pre id="gmail-m_7720800767577388321GXO9CAL8VSH" style="background-color:rgb(242,242,242);line-height:20px;padding:5px 5px 5px 30px;white-space:nowrap;margin-top:1em;margin-bottom:1em" class=""><font face="Helvetica" class=""><i class="">M</i> Res;<br class="">for (int C = 0; C < col; ++C) {<br class="">  for (int R = 0; R < row; ++R) {<br class="">    EltTy Elt = 0;<br class="">    for (int K = 0; K < inner; ++K) {<br class="">      Elt += __builtin_matrix_extract(matrix1, R, K) * <br class="">                 __builtin_matrix_extract(matrix2, K, C)<br class="">  }<br class="">  Res = __builtin_matrix_insert(Res, R, C, Elt);<br class="">}</font></pre><span class=""><span style="background-color:rgb(255,255,255)" class="">Remark: With respect to rounding errors, the operation preserves the behavior of the separate multiply and add operations by default. We propose to provide a Clang option to override this behavior and allow contraction of those operations (e.g. -ffp-contract=matrix).</span></span></div></div></div></div></blockquote><div class=""><br class=""></div><div class="">The above seem like they would be better if provided as operators rather than as builtin functions. We don't provide builtins for these kinds of operations for vector types, because we expect all code to use the operator syntax instead.</div></div></div></div></blockquote><div dir="ltr" class=""><br class=""></div>Agreed<font face="-webkit-standard" size="3" class="">.</font></div><div dir="ltr" class=""><font face="-webkit-standard" size="3" class=""><br class=""></font><blockquote type="cite" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div class="gmail_quote"><div class=""><br class=""></div><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote"><div style="overflow-wrap: break-word;" class=""><div class=""><div class=""><div class=""><span class=""><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class=""><i class="">M2</i> __builtin_matrix_transpose(<i class="">M1</i> matrix)</code><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">Remarks: The return type is a cv-unqualified matrix type that has the same element type as </span><i style="background-color:rgb(255,255,255)" class="">M1</i><span style="background-color:rgb(255,255,255)" class=""> and has the the same number of rows as </span><i style="background-color:rgb(255,255,255)" class="">M1</i><span style="background-color:rgb(255,255,255)" class=""> has columns and the same number of columns as </span><i style="background-color:rgb(255,255,255)" class="">M1</i><span style="background-color:rgb(255,255,255)" class=""> has rows.</span><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">Returns: A matrix </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">Res</code><span style="background-color:rgb(255,255,255)" class=""> equivalent to the code below, where </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">col</code><span style="background-color:rgb(255,255,255)" class=""> refers to the number of columns of M, and </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">row</code><span style="background-color:rgb(255,255,255)" class=""> to the number of rows of M.</span><br style="background-color:rgb(255,255,255)" class=""></span><pre id="gmail-m_7720800767577388321GXO9CAkn8rZ" style="background-color:rgb(242,242,242);line-height:20px;padding:5px 5px 5px 30px;white-space:nowrap;margin-top:1em;margin-bottom:1em" class=""><font face="Helvetica" class=""><i class="">M</i> Res;<br class="">for (int C = 0; C < col; ++C) {<br class="">  for (int R = 0; R < row; ++R) {<br class="">    EltTy Elt = __builtin_matrix_extract(matrix, R, C);<br class="">    Res = __builtin_matrix_insert(Res, C, R, Elt);<br class="">  }<br class="">}</font></pre></div></div></div></div></blockquote><div class="">Maybe it's a bit cute, but have you considered using an operator such as prefix ~ for this, or perhaps a posfix .T? (This is in some sense a swizzle, and we use member-access-like syntax for those already.)<br class=""></div></div></div></div></blockquote><div dir="ltr" class=""><br class=""></div>Something like .t()/T() would probably be quite convenient for the users.</div><div dir="ltr" class=""><br class=""><blockquote type="cite" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div class="gmail_quote"><div class=""><br class=""></div><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote"><div style="overflow-wrap: break-word;" class=""><div class=""><div class=""><div class=""><span class=""><i style="background-color:rgb(255,255,255)" class=""><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">M</code></i><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class=""> __builtin_matrix_column_load(</code><i style="background-color:rgb(255,255,255)" class=""><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">T</code></i><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class=""> *ptr, int row, int col, int stride)</code><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">Mandates: </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">row</code><span style="background-color:rgb(255,255,255)" class=""> and </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">col</code><span style="background-color:rgb(255,255,255)" class=""> shall be integral constants greater than 0. </span><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">Preconditions: </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">stride >= </code><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">row</code><span style="background-color:rgb(255,255,255)" class="">.</span><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">Remarks: The return type is a cv-unqualified matrix type with an element type of the cv-unqualified version of </span><i style="background-color:rgb(255,255,255)" class="">T</i><span style="background-color:rgb(255,255,255)" class=""> and a number of rows and columns equal to </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">row</code><span style="background-color:rgb(255,255,255)" class=""> and </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">col</code><span style="background-color:rgb(255,255,255)" class=""> respectively.</span><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">Returns: A matrix </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">Res</code><span style="background-color:rgb(255,255,255)" class=""> equivalent to:</span><br style="background-color:rgb(255,255,255)" class=""></span><pre id="gmail-m_7720800767577388321GXO9CAYgF6K" style="background-color:rgb(242,242,242);line-height:20px;padding:5px 5px 5px 30px;white-space:nowrap;margin-top:1em;margin-bottom:1em" class=""><font face="Helvetica" class=""><i class="">M</i> Res;<br class="">for (int C = 0; C < col; ++C) {<br class="">  for (int R = 0; R < row; ++K)<br class="">    Res = __builtin_matrix_insert(Res, R, C, ptr[R]);<br class="">  ptr += stride<br class="">}</font></pre><span class=""><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><font face="Helvetica" class=""><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">void __builtin_matrix_column_store(<i class="">M</i> matrix, <i class="">T</i> *ptr, int stride)</code><br style="background-color:rgb(255,255,255)" class=""></font><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">Preconditions: </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">stride</code><span style="background-color:rgb(255,255,255)" class=""> is greater than or equal to the number of rows in </span><i style="background-color:rgb(255,255,255)" class="">M</i><span style="background-color:rgb(255,255,255)" class="">.</span><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">Effects: Equivalent to:</span><br style="background-color:rgb(255,255,255)" class=""></span><pre id="gmail-m_7720800767577388321GXO9CA0f2i5" style="background-color:rgb(242,242,242);line-height:20px;padding:5px 5px 5px 30px;white-space:nowrap;margin-top:1em;margin-bottom:1em" class=""><font face="Helvetica" class="">for (int C = 0; C < <i class="">columns </i><i class="">in</i><i class=""> M</i>; ++C) {<br class="">  for (int R = 0; R < <i class="">rows </i><i class="">in M</i>; ++K)<br class="">    ptr[R] = __builtin_matrix_extract(matrix, R, C);<br class="">  ptr += stride<br class="">}</font></pre><span class=""><span style="background-color:rgb(255,255,255)" class="">Remarks: The type </span><i style="background-color:rgb(255,255,255)" class="">T</i><span style="background-color:rgb(255,255,255)" class=""> is the const-unqualified version of the </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">matrix</code><span style="background-color:rgb(255,255,255)" class=""> argument’s </span><i style="background-color:rgb(255,255,255)" class="">element type</i><span style="background-color:rgb(255,255,255)" class="">.</span></span></div></div></div></div></blockquote><div class=""><br class=""></div><div class="">Presumably these would be unnecessary if we permitted casting between an M* and a T* and treating the M* as a suitably-sized array of T? (Again, we don't have anything like this for vector types, for which we do guarantee that you can cast a vector* to a T* and access the vector elements directly.)</div></div></div></div></blockquote><div dir="ltr" class=""><br class=""></div>Yes, they are not strictly necessary, but I think they are very convenient for users and help guaranteeing vector code generation for those loads/stores, rather than relying on vectorization of load/store loops (I think it would be good to not encourage people to much to use loops with matrix values). Having the loads/stores expressed on the whole matrix likely also helps with alias analysis, although we haven’t explored that direction so far.</div><div dir="ltr" class=""><font face="-webkit-standard" size="3" class=""><br class=""></font><blockquote type="cite" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div class="gmail_quote"><div class=""><br class=""></div><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote"><div style="overflow-wrap: break-word;" class=""><div class=""><div class=""><div class=""><span class=""><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class=""><i class="">M</i> __builtin_matrix_scalar_multiply(<i class="">M</i> matrix, <i class="">T</i> scalar)</code><br style="background-color:rgb(255,255,255)" class=""><br style="background-color:rgb(255,255,255)" class=""><span style="background-color:rgb(255,255,255)" class="">Returns: A matrix </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">Res</code><span style="background-color:rgb(255,255,255)" class=""> equivalent to the code below, where </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">col</code><span style="background-color:rgb(255,255,255)" class=""> refers to the number of columns of M, and </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">row</code><span style="background-color:rgb(255,255,255)" class=""> to the number of rows of M.</span><br style="background-color:rgb(255,255,255)" class=""></span><pre id="gmail-m_7720800767577388321GXO9CArYmVv" style="background-color:rgb(242,242,242);line-height:20px;padding:5px 5px 5px 30px;white-space:nowrap;margin-top:1em;margin-bottom:1em" class=""><font face="Helvetica" class=""><i class="">M</i> Res;<br class="">for (int C = 0; C < col; ++C) {<br class="">  for (int R = 0; R < row; ++R) {<br class="">    EltTy Elt = __builtin_matrix_extract(matrix, R, C) * scalar;<br class="">    Res = __builtin_matrix_insert(Res, R, C, Elt);<br class="">  }<br class="">}</font></pre><span class=""><span style="background-color:rgb(255,255,255)" class="">Remarks: The return type and the type </span><i style="background-color:rgb(255,255,255)" class="">T</i><span style="background-color:rgb(255,255,255)" class=""> are the cv-unqualified type of the </span><code style="background-color:rgb(242,242,242);padding:1px;border-radius:3px" class="">matrix</code><span style="background-color:rgb(255,255,255)" class=""> argument and its cv-unqualified </span><i style="background-color:rgb(255,255,255)" class="">element type</i><span style="background-color:rgb(255,255,255)" class=""> respectively.</span></span></div></div></div></div></blockquote><div class=""><br class=""></div><div class="">(As with the above operators, using the * operator for this seems more appropriate to me.)</div><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote"><div style="overflow-wrap: break-word;" class=""><div class=""><div class=""><div class=""><h2 id="gmail-m_7720800767577388321GXO9CAuR9KH" style="margin:1.25em 0px 0.75em;line-height:22px;background-color:rgb(255,255,255)" class=""><font size="3" class="">Example </font></h2><span class=""><span style="background-color:rgb(255,255,255)" class="">This code performs a matrix-multiply of two 4x4 matrices followed by an matrix addition:</span><br style="background-color:rgb(255,255,255)" class=""></span><pre id="gmail-m_7720800767577388321GXO9CAX0xX9" style="background-color:rgb(242,242,242);line-height:20px;padding:5px 5px 5px 30px;white-space:nowrap;margin-top:1em;margin-bottom:1em" class=""><font face="Helvetica" class="">typedef float m4x4_t __attribute__((matrix_type(4, 4)));<br class="">void f(m4x4_t *a, m4x4_t *b, m4x4_t *c, m4x4_t *r) {<br class="">  *r = __builtin_matrix_add(__builtin_matrix_multiply(*a, *b), *c);<br class="">}</font></pre><span class=""><span style="background-color:rgb(255,255,255)" class="">This will get lowered by Clang to the LLVM IR below. In our current implementation, we use LLVM’s array type as storage type for the matrix data. Before accessing the data, we cast the array to a vector type. This allows us to use the element width as alignment, without running into issues with LLVM’s large default alignment for vector types, which is problematic in structs.</span><br style="background-color:rgb(255,255,255)" class=""></span><pre id="gmail-m_7720800767577388321GXO9CAhuj3Y" style="background-color:rgb(242,242,242);line-height:20px;padding:5px 5px 5px 30px;white-space:nowrap;margin-top:1em;margin-bottom:1em" class=""><font face="Helvetica" class="">define void @f([16 x float]* %a, [16 x float]* %b, [16 x float]* %c, [16 x float]* %r) #0 {<br class="">entry:<br class="">  %a.addr = alloca [16 x float]*, align 8<br class="">  %b.addr = alloca [16 x float]*, align 8<br class="">  %c.addr = alloca [16 x float]*, align 8<br class="">  %r.addr = alloca [16 x float]*, align 8<br class="">  store [16 x float]* %a, [16 x float]** %a.addr, align 8<br class="">  store [16 x float]* %b, [16 x float]** %b.addr, align 8<br class="">  store [16 x float]* %c, [16 x float]** %c.addr, align 8<br class="">  store [16 x float]* %r, [16 x float]** %r.addr, align 8<br class="">  %0 = load [16 x float]*, [16 x float]** %a.addr, align 8<br class="">  %1 = bitcast [16 x float]* %0 to <16 x float>*<br class="">  %2 = load <16 x float>, <16 x float>* %1, align 4<br class="">  %3 = load [16 x float]*, [16 x float]** %b.addr, align 8<br class="">  %4 = bitcast [16 x float]* %3 to <16 x float>*<br class="">  %5 = load <16 x float>, <16 x float>* %4, align 4<br class="">  %6 = call <16 x float> @llvm.matrix.multiply.v16f32.v16f32.v16f32(<16 x float> %2, <16 x float> %5, i32 4, i32 4, i32 4)<br class="">  %7 = load [16 x float]*, [16 x float]** %c.addr, align 8<br class="">  %8 = bitcast [16 x float]* %7 to <16 x float>*<br class="">  %9 = load <16 x float>, <16 x float>* %8, align 4<br class="">  %10 = fadd <16 x float> %6, %9<br class="">  %11 = load [16 x float]*, [16 x float]** %r.addr, align 8<br class="">  %12 = bitcast [16 x float]* %11 to <16 x float>*<br class="">  store <16 x float> %10, <16 x float>* %12, align 4<br class="">  ret void<br class="">}<br class="">declare <16 x float> @llvm.matrix.multiply.v16f32.v16f32.v16f32(<16 x float>, <16 x float>, i32 immarg, i32 immarg, i32 immarg)</font></pre><br style="font-size:16px;background-color:rgb(255,255,255)" class=""><h2 id="gmail-m_7720800767577388321GXO9CAahOFU" style="margin:1.25em 0px 0.75em;line-height:22px;background-color:rgb(255,255,255)" class=""><font size="3" class="">Contributing Criteria</font></h2><div class=""><font class=""><i class="">Evidence of a significant user community: This is based on a number of factors, including an existing user community, the perceived likelihood that users would adopt such a feature if it were available, and any secondary effects that come from, e.g., a library adopting the feature and providing benefits to its users.<br class=""></i>Currently this is part of one of our compiler toolchains and used on a few large internal codebases. The matrix type can be used by matrix libraries like Eigen, to offload some of the optimization responsibility from the library to the compiler.</font></div></div></div></div></div></blockquote><div class=""><br class=""></div><div class="">Have the Eigen developers indicated they would consider using this if it were available to them?</div><div class="">Have you reached out to the GCC developers to see if they would also be likely to support this extension?</div><div class="">We should be aiming to build critical mass behind this feature so that it gets adopted; it would be a waste of resources if a different technology ends up being adopted in this space and we're left maintaining a system that no-one outside Apple uses.</div></div></div></div></blockquote><div dir="ltr" class=""><br class=""></div>We hoped to get some initial feedback before reaching out, to make sure the proposal is in reasonably good shape. I plan to reach out to them early next week.</div><div dir="ltr" class=""><br class=""><blockquote type="cite" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div class="gmail_quote"><div class=""><br class=""></div><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote"><div style="overflow-wrap: break-word;" class=""><div class=""><div class=""><div class=""><div class=""><font class="">It would also be suitable target for implementing a standard matrix library. It also provides functionality similar to various libraries for matrix math on small matrixes, like <a href="https://developer.apple.com/documentation/accelerate/working_with_matrices" target="_blank" class="">https://developer.apple.com/documentation/accelerate/working_with_matrices</a>, with more flexibility (supports any combination of input dimensions).<br class=""><br class=""><i class="">A specific need to reside within the Clang tree: There are some extensions that would be better expressed as a separate tool, and should remain as separate tools even if they end up being hosted as part of the LLVM umbrella project.<br class=""></i>We want to expose this feature at the C/C++ level. For that, it needs to be part of Clang.<br class=""><br class=""><i class="">A specification: The specification must be sufficient to understand the design of the feature as well as interpret the meaning of specific examples. The specification should be detailed enough that another compiler vendor could implement the feature.</i><br class="">We currently have the design above and will work on a more comprehensive spec.<br class=""></font></div></div></div></div></div></blockquote><div class=""><br class=""></div><div class="">Do you anticipate the various psABIs being updated to specify the calling convention for matrix parameters and return values? If not, you'll need to include that in your specification too.</div></div></div></div></blockquote><div dir="ltr" class=""><br class=""></div>We don’t have any plans to update ABIs at the moment. Matrix value would be passed in memory. I thought we spelled that out in the proposal, but couldn’t find it while re-reading.</div><div dir="ltr" class=""><br class=""><blockquote type="cite" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div class="gmail_quote"><div class="">Similarly, you will need to specify a mangling to use for these types in both the Itanium and MS C++ ABIs.</div></div></div></div></blockquote><div dir="ltr" class=""><br class=""></div>Yes we will have to update those. In our initial implementation we used Dm{NumRows}_{NumColumns} for Itanium.<br class=""><blockquote type="cite" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div class="gmail_quote"><div class=""><br class=""></div><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote"><div style="overflow-wrap: break-word;" class=""><div class=""><div class=""><div class=""><div class=""><font class=""><i class="">Representation within the appropriate governing organization: For extensions to a language governed by a standards committee (C, C++, OpenCL), the extension itself must have an active proposal and proponent within that committee and have a reasonable chance of acceptance. Clang should drive the standard, not diverge from it. This criterion does not apply to all extensions, since some extensions fall outside of the realm of the standards bodies.<br class=""></i>We think this extension would fall outside of the realm of the standards bodies. It is an implementation detail used to implement matrix math libraries and such, much like the vector extensions are an implementation detail for SIMD libraries.<br class=""><br class=""><i class="">A long-term support plan: increasingly large or complex extensions to Clang need matching commitments to supporting them over time, including improving their implementation and specification as Clang evolves. The capacity of the contributor to make that commitment is as important as the commitment itself.<br class=""></i>We are using this internally and adding this feature to Clang upstream means we intend to support it as part of our ongoing Clang work.<br class=""><br class=""><i class="">A high-quality implementation: The implementation must fit well into Clang's architecture, follow LLVM's coding conventions, and meet Clang's quality standards, including diagnostics and complete AST representations. This is particularly important for language extensions, because users will learn how those extensions work through the behavior of the compiler.<br class=""></i>Will we provide a series of patches to implement the extension soon and look forward to any feedback to make sure the patches meet the quality requirement.<br class=""><br class=""><i class="">A test suite: Extensive testing is crucial to ensure that the language extension is not broken by ongoing maintenance in Clang. The test suite should be complete enough that another compiler vendor could conceivably validate their implementation of the feature against it</i><br class="">We will provide this as part of Clang’s unit tests and test-suite.<br class=""></font></div><div style="margin:12px 0px;color:rgb(51,51,51);font-size:16px;background-color:rgb(255,255,255)" class=""><ul id="gmail-m_7720800767577388321GXO9CA9gVip" style="line-height:20px;padding:0px 0px 0px 40px;list-style-type:none" class=""></ul></div><h2 id="gmail-m_7720800767577388321GXO9CAsn5Fz" style="margin:1.25em 0px 0.75em;line-height:22px;color:rgb(51,51,51);background-color:rgb(255,255,255)" class=""><font size="3" class="">Extensions</font></h2><span class=""><span style="background-color:rgb(255,255,255)" class="">Initially we want to focus on 2D matrixes without padding in column-major layout as a concrete use case. This is similar to the defaults for the Matrix type in Eigen, for example. But our proposed type can be extended naturally to</span><br style="background-color:rgb(255,255,255)" class=""></span><div style="margin:12px 0px;background-color:rgb(255,255,255)" class=""><ul id="gmail-m_7720800767577388321GXO9CAqgpTh" style="line-height:20px;padding:0px 0px 0px 40px" class=""><li id="gmail-m_7720800767577388321GXO9CASh87e" value="1" style="margin-bottom:0.4em" class=""><span class=""><span class="">Support N (known constant) dimensions by turning matrix_type attribute into a variadic attribute.</span> <br class=""></span></li></ul></div></div></div></div></div></blockquote><div class="">Hmm. "matrix" wouldn't really be the right name for the generalized attribute. Presumably matrix_type(N) would mean the same thing as ext_vector_type(N)? Are there realistic use cases for this? (I expect it's not worth planning for this eventuality until we actually have such a use case.)</div><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote"><div style="overflow-wrap: break-word;" class=""><div class=""><div class=""><div class=""><div style="margin:12px 0px;background-color:rgb(255,255,255)" class=""><ul id="gmail-m_7720800767577388321GXO9CAqgpTh" style="line-height:20px;padding:0px 0px 0px 40px" class=""><li id="gmail-m_7720800767577388321GXO9CASh87e" value="1" style="margin-bottom:0.4em" class=""><span class=""></span></li><li id="gmail-m_7720800767577388321GXO9CAEVc38" style="margin-bottom:0.4em" class=""><span class=""><span class="">Support column/row-wise padding, by adding a column_padding clause to the attribute.<br class="">Dealing with the padding could be exclusively handled on the frontend side, by emitting additional shufflevector instructions to extract the data. If there is a desire to exploit the padding more on the LLVM side, we can add a set of intrinsics for that.</span> <br class=""></span></li><li id="gmail-m_7720800767577388321GXO9CA8mj8h" style="margin-bottom:0.4em" class=""><span class=""><span class="">Support row & column major layouts, by adding a layout clause to the attribute.<br class="">Again, this naively could be handled while lowering to LLVM IR in Clang using shufflevector to produce flattened vectors with the required layout. For better optimisations, the LLVM intrinsics relying on shape/layout information can be extended to take the layout as additional argument. Through propagating the layout information similar to the dimensions, we should be able to optimise the points where we need to transform the layout of the underlying matrixes.</span> <br class=""></span></li></ul></div><span class=""><span style="background-color:rgb(255,255,255)" class="">In all cases, we require known integer constants as dimensions and we do not plan to support dynamic dimensions for now, as the main optimization potential comes from the fact that we know the dimensions. Supporting dynamic dimensions should be fairly straight forward, but means we lose the ability to type check matrix expressions at compile time and we also have to rely on dynamic dimension during code generation.</span><br style="background-color:rgb(255,255,255)" class=""></span></div></div></div></div><br class="">
</blockquote></div></div>
</div></blockquote><br class=""></div><div dir="ltr" class="">If I understand your question correctly, if N = 1, matrix_type(N) would be the same thing as ext_vector_type(N). </div><div dir="ltr" class=""><br class=""></div><div dir="ltr" class="">There might be interesting use cases for 3+ dimensions, but as you said, I think it would be best to plan for that once we have a concrete use case. The name itself might need extra generalization, but I think most of the proposal can be extended quite easily.</div></div></body></html>