[clang] [HLSL][Matrix] Make Matrix InitListExprs and AST row-major order, and respect /Zpr and /Zpc in codegen (PR #182904)

Farzon Lotfi via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 23 11:51:49 PST 2026


================
@@ -2450,6 +2450,23 @@ Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr *E) {
     llvm::Value *Init = llvm::Constant::getNullValue(EltTy);
     V = Builder.CreateInsertElement(V, Init, Idx, "vecinit");
   }
+
+  // Matrix initializer lists are in row-major order but the memory layout for
+  // codegen is determined by the -fmatrix-memory-layout flag (default:
+  // column-major). When the memory layout is column-major, we need to shuffle
+  // the elements from row-major to column-major order.
+  if (const auto *MT = E->getType()->getAs<ConstantMatrixType>()) {
+    if (CGF.getLangOpts().getDefaultMatrixMemoryLayout() !=
+        LangOptions::MatrixMemoryLayout::MatrixRowMajor) {
+      unsigned NumRows = MT->getNumRows();
+      unsigned NumCols = MT->getNumColumns();
+      SmallVector<int, 16> Mask;
+      for (unsigned I = 0, N = NumRows * NumCols; I < N; ++I)
+        Mask.push_back((I % NumRows) * NumCols + (I / NumRows));
----------------
farzonl wrote:

I suppose if you want to do the whole thing in the matrix builder that would be fine. I do think we probably need a  `CreateConstantColumnMajorIndex` helper aswell so this math doesn't get implemented so many times. I'm prretty sure we do this same thing somewhere in the matrix trunc casting code aswell.

https://github.com/llvm/llvm-project/pull/182904


More information about the cfe-commits mailing list