Context RFC: RFC: Represent HLSL Matrix Layout in the Clang AST
The Problem
#230638 exposed an issue in C\C++ that HLSL recently solved. Clang currently allows the selected memory layout to leak into the representation and interpretation of matrix values. As a result, every operation must determine whether its vector operand is a row-major memory image or a logical matrix value.
That ambiguity is already is a concrete problems in CIR. The discussion in #230600 identified exactly where this becomes painful:
cir.matrix.insertandcir.matrix.extractare pure operations on matrix values.- CIR loads a matrix from memory into a
!cir.matrixvalue before applying these operations. - During LLVM lowering, the operations currently need to know the layout of the memory from which that value originated in order to calculate a flat vector index.
- That forces a memory-layout concern into otherwise representation-independent SSA operations.
- If CIR is serialized and lowered separately, we would need to preserve the frontend option or attach layout provenance to the IR.
Proposed Solution
The HLSL solution C\C++ should adopt
Matrix SSA values are always canonical column-major logical values. Row-major and column-major are properties of storage, not properties of matrix values.
Under this model:
- Whole-matrix loads convert from the selected physical layout into canonical column-major SSA.
- Whole-matrix stores convert canonical SSA back into the selected physical layout.
- Matrix arithmetic, transpose, extract, insert, and function-local SSA all operate on one representation.
- Direct physical element accesses remain layout-aware because they actually index memory.
- Explicit column-major load/store builtins retain their explicitly requested memory semantics.
- LLVM matrix intrinsics are always lowered using their documented column-major interpretation.
This directly resolves the concern raised in #230638. cir.matrix.insert no longer needs a module-level layout setting or knowledge of where its operand originated.
CC: @fhahn @erichkeane @andykaylor @anemet @beanz @Icohedron