[clang] [Sema] Reject braced initializer lists for matrix types (PR #246566)
Cyrus Ding via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 26 23:13:51 PST 2026
https://github.com/dingcyrus created https://github.com/llvm/llvm-project/pull/246566
Fixes #246536
`CheckMatrixType` returns without consuming any initializer outside of HLSL, so every element of a matrix's braced list falls through to the "excess elements" diagnostic, which is only a warning in C. Clang therefore accepted
typedef int m31 __attribute__((matrix_type(3, 1)));
m31 a = {1, 2, 3};
and zero-initialized `a`. The other forms in #246536 -- compound literal, array of matrices, matrix member of a struct -- silently zero as well, and #232999 reports the struct-member form as a crash while constant-evaluating. Clang 15..21 rejected all of them with `illegal initializer type`.
Reject the list with that same diagnostic, no new wording is introduced. The initializer meant for the matrix is consumed first, so a loop over an enclosing array or record still makes progress. An empty list keeps value-initializing the
matrix, which leaves #232999 as a separate issue.
The `!HLSL` guard is where the bug, so HLSL is untouched. All 98 `clang/test` files mentioning `matrix_type` pass, and `Sema`, `SemaCXX`, `CodeGen`, `CodeGenCXX`, `CodeGenHLSL` and `AST/ByteCode` fail identically with and without this change apart from the three tests added or updated here (the other 28 failures are pre-existing on a X86/AArch64/AMDGPU/NVPTX-only build without lld or runtimes).
`CodeGen/AArch64/abi-classify-return-types.c` is in here because it built its matrix body from a compound literal; it now returns a local, as `matrix-hfa.c` does.
AI assistance was used for code review analysis and local build verification.
>From 2be5ec6c0a41f0c0fc75b9e3d365157a6211a182 Mon Sep 17 00:00:00 2001
From: Cyrus Ding <785101675 at qq.com>
Date: Fri, 27 Nov 2026 15:00:35 +0800
Subject: [PATCH] [Sema] Reject braced initializer lists for matrix types
CheckMatrixType returns without consuming any initializer outside of HLSL,
so every element of a matrix's braced list falls through to the "excess
elements" diagnostic at the end of CheckExplicitInitList, and in C that is
only a warning. Clang therefore accepted
typedef int m31 __attribute__((matrix_type(3, 1)));
m31 a = {1, 2, 3};
and value-initialized a to zero. The other forms reported in the issue --
compound literal, array of matrices, matrix member of a struct -- silently
zero as well, and the struct-member form is what #232999 reports as a crash
while constant-evaluating.
This is a regression from Clang 21: before the matrix dispatch was added in
#162743, a matrix type reached the fallback case in CheckListElementTypes and
was rejected with "illegal initializer type", as Clang 15 through 21 do.
Reject it the same way again, with the same diagnostic, so no new wording is
introduced; the C++ message becomes the common one, where it was already an
error.
The initializer meant for the matrix is consumed before returning, so that a
loop over the elements of an enclosing array or record makes progress even
though nothing was checked. An empty list is still accepted, since it
value-initializes the matrix, which keeps #232999 open as a separate problem
while making its C reproducer unreachable.
HLSL is untouched: the new code sits in the same !HLSL early return that
caused the bug. All 98 clang/test files mentioning matrix_type pass.
Fixes https://github.com/llvm/llvm-project/issues/246536
Assisted-by: Claude Code
Co-Authored-By: Claude Code <noreply at anthropic.com>
---
clang/docs/MatrixTypes.md | 5 +-
clang/docs/ReleaseNotes.md | 6 +++
clang/lib/Sema/SemaInit.cpp | 17 ++++++-
clang/test/AST/ByteCode/matrix.c | 4 +-
.../AArch64/abi-classify-return-types.c | 2 +-
clang/test/Sema/matrix-init.c | 46 +++++++++++++++++++
clang/test/SemaCXX/matrix-init.cpp | 30 ++++++++++++
7 files changed, 106 insertions(+), 4 deletions(-)
create mode 100644 clang/test/Sema/matrix-init.c
create mode 100644 clang/test/SemaCXX/matrix-init.cpp
diff --git a/clang/docs/MatrixTypes.md b/clang/docs/MatrixTypes.md
index 7ae2d7c6f546db..1158a3d583fbfe 100644
--- a/clang/docs/MatrixTypes.md
+++ b/clang/docs/MatrixTypes.md
@@ -257,7 +257,10 @@ where M is a matrix type? We don’t support this anywhere else, but it’s
convenient. The alternative is using template deduction to extract this
information. Also add spelling for C.
-Future Work: Initialization syntax.
+Future Work: Initialization syntax. Until then, a braced list with any elements
+is rejected for a matrix type; only an empty list, which value-initializes the
+matrix, is accepted. HLSL is the exception, as it has its own matrix initializer
+syntax.
## Decisions for the Implementation in Clang
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index cd06024116c80c..97f48a0d6f6b8a 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -48,6 +48,12 @@ The previous behavior can be restored with `-Wno-error=unicode-whitespace`.
Clang will stop accepting non-ascii whitespaces as token separators
in a future version of Clang.
+- A braced initializer list for a matrix type is now rejected in C, rather than
+ producing an `-Wexcess-initializers` warning and silently zero-initializing the
+ matrix. Matrix types have no element initializer syntax outside of HLSL yet, so
+ only an empty list, which value-initializes the matrix, is accepted.
+ (#GH246536)
+
### C++ Specific Potentially Breaking Changes
- The `[[carries_dependency]]` attribute is no longer recognized, in any language
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index ac1e67ed6d5af3..0757c81edc3fa9 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -1890,8 +1890,23 @@ void InitListChecker::CheckMatrixType(const InitializedEntity &Entity,
unsigned &Index,
InitListExpr *StructuredList,
unsigned &StructuredIndex) {
- if (!SemaRef.getLangOpts().HLSL)
+ if (!SemaRef.getLangOpts().HLSL) {
+ // Only HLSL has syntax for the elements of a matrix. Consuming nothing here
+ // would leave every initializer to the "excess elements" diagnostic, which
+ // is only a warning in C, and the matrix would silently be
+ // zero-initialized. Reject the list instead, as Clang did before the matrix
+ // dispatch was added; an empty list still value-initializes the matrix.
+ if (Index < IList->getNumInits()) {
+ // Consume the initializer meant for the matrix, so that a loop over the
+ // elements of an enclosing array or record makes progress.
+ ++Index;
+ hadError = true;
+ if (!VerifyOnly)
+ SemaRef.Diag(IList->getBeginLoc(), diag::err_illegal_initializer_type)
+ << DeclType;
+ }
return;
+ }
const ConstantMatrixType *MT = DeclType->castAs<ConstantMatrixType>();
diff --git a/clang/test/AST/ByteCode/matrix.c b/clang/test/AST/ByteCode/matrix.c
index 1ca513f4a01951..58283aa4782c2c 100644
--- a/clang/test/AST/ByteCode/matrix.c
+++ b/clang/test/AST/ByteCode/matrix.c
@@ -2,4 +2,6 @@
// RUN: %clang_cc1 -fenable-matrix %s -verify
typedef float fx2x2_t __attribute__((matrix_type(2, 2)));
-fx2x2_t ret_matrix() { return (fx2x2_t){1.0f, 2.0f, 3.0f, 4.0f}; } // expected-warning {{excess elements in matrix initializer}}
+// A braced list is rejected before it can reach either interpreter, so the
+// matrix is no longer silently zero-initialized here.
+fx2x2_t ret_matrix() { return (fx2x2_t){1.0f, 2.0f, 3.0f, 4.0f}; } // expected-error {{illegal initializer type}}
diff --git a/clang/test/CodeGen/AArch64/abi-classify-return-types.c b/clang/test/CodeGen/AArch64/abi-classify-return-types.c
index 67d6bb4a1ef072..16dfa472cb7fc1 100644
--- a/clang/test/CodeGen/AArch64/abi-classify-return-types.c
+++ b/clang/test/CodeGen/AArch64/abi-classify-return-types.c
@@ -67,7 +67,7 @@ void* ret_void_ptr() { return &gi; }
// CHECK: define{{.*}} ptr @ret_void_ptr
typedef float fx2x2_t __attribute__((matrix_type(2, 2)));
-fx2x2_t ret_matrix() { return (fx2x2_t){1.0f, 2.0f, 3.0f, 4.0f}; }
+fx2x2_t ret_matrix() { fx2x2_t m; return m; }
// CHECK: define{{.*}} <4 x float> @ret_matrix
_BitInt(7) ret_bitint7(void) { return 0; }
diff --git a/clang/test/Sema/matrix-init.c b/clang/test/Sema/matrix-init.c
new file mode 100644
index 00000000000000..9c5b5faf886381
--- /dev/null
+++ b/clang/test/Sema/matrix-init.c
@@ -0,0 +1,46 @@
+// RUN: %clang_cc1 -fenable-matrix -fsyntax-only -verify=expected %s
+
+// Matrix types have no initializer syntax outside of HLSL yet, so a braced list
+// must be rejected instead of being accepted and silently zero-initializing the
+// matrix. The elements used to fall through to the "excess elements" diagnostic,
+// which is only a warning in C.
+// https://github.com/llvm/llvm-project/issues/246536
+
+typedef int m31 __attribute__((matrix_type(3, 1)));
+typedef float fx2x2 __attribute__((matrix_type(2, 2)));
+
+m31 global = {1, 2, 3}; // expected-error {{illegal initializer type}}
+
+// A compound literal is rejected the same way.
+m31 make_compound_literal(void) { return (m31){1, 2, 3}; } // expected-error {{illegal initializer type}}
+
+// So is a single element: there is no splat form either.
+void one_element(void) {
+ m31 a = {1}; // expected-error {{illegal initializer type}}
+ (void)a;
+}
+
+// Each matrix in an array gets its own diagnostic.
+void array_of_matrices(void) {
+ m31 arr[2] = {{1, 2, 3}, {4, 5, 6}}; // expected-error 2 {{illegal initializer type}}
+ (void)arr;
+}
+
+// The initializer of a matrix member used to be diagnosed as a warning here and
+// then crash the constant evaluator.
+void struct_member(void) {
+ struct S {
+ m31 m;
+ char c;
+ };
+ struct S s = {{7, 8, 9}, 'x'}; // expected-error {{illegal initializer type}}
+ (void)s;
+}
+
+// An empty list is still accepted: it value-initializes the matrix.
+void empty_list(void) {
+ m31 a = {};
+ fx2x2 b = {};
+ (void)a;
+ (void)b;
+}
diff --git a/clang/test/SemaCXX/matrix-init.cpp b/clang/test/SemaCXX/matrix-init.cpp
new file mode 100644
index 00000000000000..59a28d48a36374
--- /dev/null
+++ b/clang/test/SemaCXX/matrix-init.cpp
@@ -0,0 +1,30 @@
+// RUN: %clang_cc1 -fenable-matrix -fsyntax-only -verify=expected %s
+
+// A braced list cannot initialize a matrix: unlike C, where the elements used to
+// be diagnosed as excess initializers (a warning) and the matrix silently
+// zero-initialized, C++ already rejected them. The wording is now the same as
+// for every other type that has no initializer-list form.
+// https://github.com/llvm/llvm-project/issues/246536
+
+typedef int m31 __attribute__((matrix_type(3, 1)));
+
+m31 global = {1, 2, 3}; // expected-error {{illegal initializer type}}
+
+void one_element() {
+ m31 a = {1}; // expected-error {{illegal initializer type}}
+}
+
+void array_of_matrices() {
+ m31 arr[2] = {{1, 2, 3}, {4, 5, 6}}; // expected-error 2 {{illegal initializer type}}
+}
+
+struct S {
+ m31 m;
+ char c;
+};
+S s = {{7, 8, 9}, 'x'}; // expected-error {{illegal initializer type}}
+
+void empty_list() {
+ m31 a = {};
+ (void)a;
+}
More information about the cfe-commits
mailing list