[RFC] Matrix / Vector type interOps

Hi All:
This is my first post here. My friend told me that I can share my ideas here.
In Clang 14.0, matrix type is added and is still under development. Recently, I’ve tried matrix type and found that current implementation is column-major. Therefore, I have some ideas for developers:

  • row/column vector type derived from matrix type definition.
    For example, as a 8x8 float matrix type is defined

typedef float float8x8 attribute((matrix_type(8, 8)));

since we have the number of row and the number of columns, it would be great to derived vector types from the matrix definition. And we could define row and column vectors like this way:

float8x8.row_t vrow, vrow2; // the two variables are float vectors with the length of a row.
float8x8.col_t vcol, vcol2; // the two variables are float vectors with the length of a column.

  • set a row or a column of a matrix
    Similar to vector or matrix type, we use brackets to set or get a value of a vector or matrix.
    For matrix, we could tried to have some notation to set a row or a column by providing vectors.

float8x8 mat;
… // load data into mat, vrow, vcol …
mat.rows[3] = vrow; // set a row of the matrix
mat.cols[4] = vcol; // set a col of the matrix
vrow2 = mat.rows[0]; // get a row of the matrix
vcol2 = mat.cols[6]; // get a column of the matrix

  • multiplications - (vec x matrix) or (matrix x vec)
    In some transformation applications, the notation is very useful - for example - Direct3D or OpenGL/Vulkan.

vrow2 = vcol*mat;
vcol2 = mat*vrow;

regards
champ

Sorry, I didn’t express it clearly. I mean that clang 14.0 only support column-major load/store built-in functions.

Hi,

That’s great, thanks for sharing!

Yes, the current implementation defaults to column major, but I think we tried to make sure the definition allows for easy extension to row-major. The lowering pass in LLVM already supports row-major.

That’s a good point, IIRC this was considered during the original proposal. There’s a TODO which should match this suggestion: Matrix Types — Clang 15.0.0git documentation

IIRC there were some concerns about how this would interact with row-major matrixes, there’s a note at the end of the section about access operators (Matrix Types — Clang 15.0.0git documentation)

If vec is defined as matrix type with 1 column/row, this should already work I think. What doesn’t work at the moment I think is multiplying other vector types (like ext_vector_type with matrix types).

Cheers,
Florian