Issue 181902
Summary [HLSL][Matrix] Internal representation of matrix initializer lists should hold elements in row-major order
Labels clang:frontend, HLSL
Assignees
Reporter Icohedron
    As noted in https://github.com/llvm/llvm-project/pull/178762#discussion_r2744063087, Matrix `InitListExpr`s hold the elements of the initializer list in column-major order instead of row-major order (as it is in HLSL source code).

https://hlsl.godbolt.org/z/TK4zoKM5r

Currently when you define a matrix in Clang:
```hlsl
float2x2 FM2x2 = {1.5, 2.5, 3.5, 4.5};
```

The following AST is produced in Clang, which holds the elements in column-major order:
```
 `-VarDecl 0x5de77ec314e0 <col:5, col:41> col:14 FM2x2 'float2x2':'matrix<float, 2, 2>' cinit
     `-InitListExpr 0x5de77ec31688 <col:22, col:41> 'float2x2':'matrix<float, 2, 2>'  
 |-FloatingLiteral 0x5de77ec31548 <col:23> 'float' 1.500000e+00 
 |-FloatingLiteral 0x5de77ec31588 <col:33> 'float' 3.500000e+00 
 |-FloatingLiteral 0x5de77ec31568 <col:28> 'float' 2.500000e+00 
 `-FloatingLiteral 0x5de77ec315a8 <col:38> 'float' 4.500000e+00 
```

Whereas DXC's AST holds the elements in row-major order:
```
`-VarDecl <col:5, col:41> col:14 FM2x2 'float2x2':'matrix<float, 2, 2>' cinit
  `-InitListExpr <col:22, col:41> 'float2x2':'matrix<float, 2, 2>'
    |-ImplicitCastExpr <col:23> 'float' <FloatingCast>
    | `-FloatingLiteral <col:23> 'literal float' 1.500000e+00
 |-ImplicitCastExpr <col:28> 'float' <FloatingCast>
    | `-FloatingLiteral <col:28> 'literal float' 2.500000e+00
    |-ImplicitCastExpr <col:33> 'float' <FloatingCast>
    | `-FloatingLiteral <col:33> 'literal float' 3.500000e+00
    `-ImplicitCastExpr <col:38> 'float' <FloatingCast>
 `-FloatingLiteral <col:38> 'literal float' 4.500000e+00
```

In Clang, matrices in APValues used for constant _expression_ evaluation are in row-major order (as per https://github.com/llvm/llvm-project/pull/178762), which makes the column-major ordering of the matrix initializer list awkward to work with initializing a matrix APValue from it.

AC: 
- Change the order of elements in matrix initializer lists to row-major order internally
- Add/modify Clang AST tests to ensure the elements of the matrix initializer lists in the AST are in row-major order
- Update `MatrixExprEvaluator::VisitInitListExpr` in `clang/lib/AST/ExprConstant.cpp` (see https://github.com/llvm/llvm-project/pull/178762#discussion_r2744063087) to ensure that matrix APValues are initialized from matrix `InitListExpr`s correctly after the changes are made to the internal matrix memory layout


_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to