[clang] [HLSL] Add matrix constructors using initalizer lists (PR #162743)

Farzon Lotfi via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 9 14:55:27 PDT 2025


================
@@ -4124,6 +4130,32 @@ class InitListTransformer {
       }
       return true;
     }
+    if (auto *MTy = Ty->getAs<ConstantMatrixType>()) {
+      unsigned Rows = MTy->getNumRows();
+      unsigned Cols = MTy->getNumColumns();
+      QualType ElemTy = MTy->getElementType();
+
+      for (unsigned C = 0; C < Cols; ++C) {
+        for (unsigned R = 0; R < Rows; ++R) {
+          // row index literal
+          Expr *RowIdx = IntegerLiteral::Create(
+              Ctx, llvm::APInt(Ctx.getIntWidth(Ctx.IntTy), R), Ctx.IntTy,
+              E->getBeginLoc());
+          // column index literal
+          Expr *ColIdx = IntegerLiteral::Create(
+              Ctx, llvm::APInt(Ctx.getIntWidth(Ctx.IntTy), C), Ctx.IntTy,
+              E->getBeginLoc());
+          ExprResult ElExpr = S.CreateBuiltinMatrixSubscriptExpr(
+              E, RowIdx, ColIdx, E->getEndLoc());
+          if (ElExpr.isInvalid())
+            return false;
+          if (!buildInitializerListImpl(ElExpr.get()))
+            return false;
----------------
farzonl wrote:

this line I copied from the `ConstantArrayType` case. However the vector type case does
```suggestion
          if (!castInitializer(ElExpr.get()))
            return false;
```
I couldn't find a material difference in behavior in my testing.  suggestions on test cases to know which one is the right error handling would be appreicated.

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


More information about the cfe-commits mailing list