[clang] [Sema] Reject braced initializer lists for matrix types (PR #246566)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 26 23:14:27 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Cyrus Ding (dingcyrus)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/246566.diff
7 Files Affected:
- (modified) clang/docs/MatrixTypes.md (+4-1)
- (modified) clang/docs/ReleaseNotes.md (+6)
- (modified) clang/lib/Sema/SemaInit.cpp (+16-1)
- (modified) clang/test/AST/ByteCode/matrix.c (+3-1)
- (modified) clang/test/CodeGen/AArch64/abi-classify-return-types.c (+1-1)
- (added) clang/test/Sema/matrix-init.c (+46)
- (added) clang/test/SemaCXX/matrix-init.cpp (+30)
``````````diff
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;
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/246566
More information about the cfe-commits
mailing list