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

Chris B via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 22 14:08:00 PDT 2025


================
@@ -1877,6 +1887,34 @@ void InitListChecker::CheckReferenceType(const InitializedEntity &Entity,
     AggrDeductionCandidateParamTypes->push_back(DeclType);
 }
 
+void InitListChecker::CheckMatrixType(const InitializedEntity &Entity,
+                                      InitListExpr *IList, QualType DeclType,
+                                      unsigned &Index,
+                                      InitListExpr *StructuredList,
+                                      unsigned &StructuredIndex) {
+  if (!SemaRef.getLangOpts().HLSL)
+    return;
+
+  const ConstantMatrixType *MT = DeclType->castAs<ConstantMatrixType>();
+  QualType ElemTy = MT->getElementType();
+  const unsigned MaxElts = MT->getNumElementsFlattened();
+
+  unsigned NumEltsInit = 0;
+  InitializedEntity ElemEnt =
+      InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity);
+
+  while (NumEltsInit < MaxElts && Index < IList->getNumInits()) {
+    // Not a sublist: just consume directly.
+    ElemEnt.setElementIndex(Index);
+    CheckSubElementType(ElemEnt, IList, ElemTy, Index, StructuredList,
+                        StructuredIndex);
+    ++NumEltsInit;
+  }
+
+  // HLSL requires exactly NumEltsInit to equal Max initializers.
----------------
llvm-beanz wrote:

This comment doesn't really explain why we're not emitting an error. Probably worth adding something like "the error for this case is generated in SemaHLSL, execution should never reach this point if this condition is not true".

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


More information about the cfe-commits mailing list