[clang] [Matrix][Clang][HLSL] Move MaxMatrixDimension to a LangOpt (PR #163307)
Farzon Lotfi via cfe-commits
cfe-commits at lists.llvm.org
Wed Oct 15 08:16:18 PDT 2025
https://github.com/farzonl updated https://github.com/llvm/llvm-project/pull/163307
>From 30a88ad2539c83473da70b7dd083ae4974eb3a57 Mon Sep 17 00:00:00 2001
From: Farzon Lotfi <farzonlotfi at microsoft.com>
Date: Mon, 13 Oct 2025 21:50:45 -0400
Subject: [PATCH 1/3] [NFC][Matrix][Clang][HLSL] Move MaxMatrixDimension to a
LangOpt
fixes #160190
This change just makes MaxMatrixDimension configurable by language mode.
It was previously introduced in https://github.com/llvm/llvm-project/commit/94b43118e2203fed8ca0377ae762c08189aa6f3d
when there was not a need to make dimensions configurable.
Current testing to this effect exists in:
- clang/test/Sema/matrix-type-builtins.c
- clang/test/SemaCXX/matrix-type-builtins.cpp
- clang/test/SemaHLSL/BuiltIns/matrix-basic_types-errors.hlsl
I considered adding a driver flag to `clang/include/clang/Driver/Options.td`
but HLSL matrix max dim is always 4 so we don't need this configurable
beyond that size for our use case.
---
clang/include/clang/AST/TypeBase.h | 12 ------------
clang/include/clang/Basic/LangOptions.def | 1 +
clang/lib/AST/ASTContext.cpp | 4 ++--
clang/lib/Basic/LangOptions.cpp | 8 ++++++--
clang/lib/Sema/HLSLExternalSemaSource.cpp | 3 ++-
clang/lib/Sema/SemaChecking.cpp | 4 ++--
clang/lib/Sema/SemaType.cpp | 4 ++--
7 files changed, 15 insertions(+), 21 deletions(-)
diff --git a/clang/include/clang/AST/TypeBase.h b/clang/include/clang/AST/TypeBase.h
index 625cc77dc1f08..5c51ec52daed3 100644
--- a/clang/include/clang/AST/TypeBase.h
+++ b/clang/include/clang/AST/TypeBase.h
@@ -4378,8 +4378,6 @@ class ConstantMatrixType final : public MatrixType {
unsigned NumRows;
unsigned NumColumns;
- static constexpr unsigned MaxElementsPerDimension = (1 << 20) - 1;
-
ConstantMatrixType(QualType MatrixElementType, unsigned NRows,
unsigned NColumns, QualType CanonElementType);
@@ -4398,16 +4396,6 @@ class ConstantMatrixType final : public MatrixType {
return getNumRows() * getNumColumns();
}
- /// Returns true if \p NumElements is a valid matrix dimension.
- static constexpr bool isDimensionValid(size_t NumElements) {
- return NumElements > 0 && NumElements <= MaxElementsPerDimension;
- }
-
- /// Returns the maximum number of elements per dimension.
- static constexpr unsigned getMaxElementsPerDimension() {
- return MaxElementsPerDimension;
- }
-
void Profile(llvm::FoldingSetNodeID &ID) {
Profile(ID, getElementType(), getNumRows(), getNumColumns(),
getTypeClass());
diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def
index 9e850089ad87f..690439c1258c1 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -432,6 +432,7 @@ ENUM_LANGOPT(RegisterStaticDestructors, RegisterStaticDestructorsKind, 2,
LANGOPT(RegCall4, 1, 0, NotCompatible, "Set __regcall4 as a default calling convention to respect __regcall ABI v.4")
LANGOPT(MatrixTypes, 1, 0, NotCompatible, "Enable or disable the builtin matrix type")
+VALUE_LANGOPT(MaxMatrixDimension, 32, (1 << 20) - 1, NotCompatible, "maximum allowed matrix dimension")
LANGOPT(CXXAssumptions, 1, 1, NotCompatible, "Enable or disable codegen and compile-time checks for C++23's [[assume]] attribute")
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index a8b41ba18fa01..fa363bc6fea7c 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -4713,8 +4713,8 @@ QualType ASTContext::getConstantMatrixType(QualType ElementTy, unsigned NumRows,
assert(MatrixType::isValidElementType(ElementTy) &&
"need a valid element type");
- assert(ConstantMatrixType::isDimensionValid(NumRows) &&
- ConstantMatrixType::isDimensionValid(NumColumns) &&
+ assert(NumRows > 0 && NumRows <= LangOpts.MaxMatrixDimension &&
+ NumColumns > 0 && NumColumns <= LangOpts.MaxMatrixDimension &&
"need valid matrix dimensions");
void *InsertPos = nullptr;
if (ConstantMatrixType *MTP = MatrixTypes.FindNodeOrInsertPos(ID, InsertPos))
diff --git a/clang/lib/Basic/LangOptions.cpp b/clang/lib/Basic/LangOptions.cpp
index f034514466d3f..4f5d20d946de8 100644
--- a/clang/lib/Basic/LangOptions.cpp
+++ b/clang/lib/Basic/LangOptions.cpp
@@ -131,8 +131,12 @@ void LangOptions::setLangDefaults(LangOptions &Opts, Language Lang,
Opts.NamedLoops = Std.isC2y();
Opts.HLSL = Lang == Language::HLSL;
- if (Opts.HLSL && Opts.IncludeDefaultHeader)
- Includes.push_back("hlsl.h");
+ if (Opts.HLSL) {
+ if (Opts.IncludeDefaultHeader)
+ Includes.push_back("hlsl.h");
+ // Set maximum matrix dimension to 4 for HLSL
+ Opts.MaxMatrixDimension = 4;
+ }
// Set OpenCL Version.
Opts.OpenCL = Std.isOpenCL();
diff --git a/clang/lib/Sema/HLSLExternalSemaSource.cpp b/clang/lib/Sema/HLSLExternalSemaSource.cpp
index e118dda4780e2..81c836fe60452 100644
--- a/clang/lib/Sema/HLSLExternalSemaSource.cpp
+++ b/clang/lib/Sema/HLSLExternalSemaSource.cpp
@@ -159,7 +159,8 @@ void HLSLExternalSemaSource::defineHLSLMatrixAlias() {
SourceLocation(), ColsParam));
TemplateParams.emplace_back(ColsParam);
- const unsigned MaxMatDim = 4;
+ const unsigned MaxMatDim = SemaPtr->getLangOpts().MaxMatrixDimension;
+ ;
auto *MaxRow = IntegerLiteral::Create(
AST, llvm::APInt(AST.getIntWidth(AST.IntTy), MaxMatDim), AST.IntTy,
SourceLocation());
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 063db05665af1..de37ee8ba5783 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -16240,9 +16240,9 @@ getAndVerifyMatrixDimension(Expr *Expr, StringRef Name, Sema &S) {
return {};
}
uint64_t Dim = Value->getZExtValue();
- if (!ConstantMatrixType::isDimensionValid(Dim)) {
+ if (Dim == 0 || Dim > S.Context.getLangOpts().MaxMatrixDimension) {
S.Diag(Expr->getBeginLoc(), diag::err_builtin_matrix_invalid_dimension)
- << Name << ConstantMatrixType::getMaxElementsPerDimension();
+ << Name << S.Context.getLangOpts().MaxMatrixDimension;
return {};
}
return Dim;
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index a9e7c34de94f4..811295c566a66 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -2517,12 +2517,12 @@ QualType Sema::BuildMatrixType(QualType ElementTy, Expr *NumRows, Expr *NumCols,
Diag(AttrLoc, diag::err_attribute_zero_size) << "matrix" << ColRange;
return QualType();
}
- if (!ConstantMatrixType::isDimensionValid(MatrixRows)) {
+ if (MatrixRows > Context.getLangOpts().MaxMatrixDimension) {
Diag(AttrLoc, diag::err_attribute_size_too_large)
<< RowRange << "matrix row";
return QualType();
}
- if (!ConstantMatrixType::isDimensionValid(MatrixColumns)) {
+ if (MatrixColumns > Context.getLangOpts().MaxMatrixDimension) {
Diag(AttrLoc, diag::err_attribute_size_too_large)
<< ColRange << "matrix column";
return QualType();
>From 95801e7a9a84c5ba51e4e7495c8fa2ed904622c8 Mon Sep 17 00:00:00 2001
From: Farzon Lotfi <farzonlotfi at microsoft.com>
Date: Tue, 14 Oct 2025 12:23:41 -0400
Subject: [PATCH 2/3] add unit test
---
.../Frontend/CompilerInvocationTest.cpp | 20 +++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git a/clang/unittests/Frontend/CompilerInvocationTest.cpp b/clang/unittests/Frontend/CompilerInvocationTest.cpp
index 75390aa42d00e..1332422688fe6 100644
--- a/clang/unittests/Frontend/CompilerInvocationTest.cpp
+++ b/clang/unittests/Frontend/CompilerInvocationTest.cpp
@@ -732,6 +732,26 @@ TEST_F(CommandLineTest, ConditionalParsingIfTrueFlagPresent) {
ASSERT_THAT(GeneratedArgs, Contains(StrEq("-sycl-std=2017")));
}
+TEST_F(CommandLineTest, ConditionalParsingIfHLSLFlagPresent) {
+ const char *Args[] = {"-xhlsl"};
+
+ CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
+
+ ASSERT_EQ(Invocation.getLangOpts().MaxMatrixDimension, 4u);
+
+ Invocation.generateCC1CommandLine(GeneratedArgs, *this);
+}
+
+TEST_F(CommandLineTest, ConditionalParsingIfHLSLFlagNotPresent) {
+ const char *Args[] = {""};
+
+ CompilerInvocation::CreateFromArgs(Invocation, Args, *Diags);
+
+ ASSERT_EQ(Invocation.getLangOpts().MaxMatrixDimension, 1048575u);
+
+ Invocation.generateCC1CommandLine(GeneratedArgs, *this);
+}
+
// Wide integer option.
TEST_F(CommandLineTest, WideIntegerHighValue) {
>From f6c3cf249ac97b7ac52a86cf23c56b44dc3c2cbe Mon Sep 17 00:00:00 2001
From: Farzon Lotfi <farzonlotfi at microsoft.com>
Date: Wed, 15 Oct 2025 11:12:37 -0400
Subject: [PATCH 3/3] update tests to confirm matrix_type can not be larger
than 4 per dim
---
clang/test/SemaHLSL/BuiltIns/matrix-basic_types-errors.hlsl | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/clang/test/SemaHLSL/BuiltIns/matrix-basic_types-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/matrix-basic_types-errors.hlsl
index 6f6b01bac829e..63d7acfcd647f 100644
--- a/clang/test/SemaHLSL/BuiltIns/matrix-basic_types-errors.hlsl
+++ b/clang/test/SemaHLSL/BuiltIns/matrix-basic_types-errors.hlsl
@@ -10,3 +10,9 @@ uint16_t4x4 mat2;
matrix<int, 5, 5> mat3;
// expected-error at -1 {{constraints not satisfied for alias template 'matrix' [with element = int, rows_count = 5, cols_count = 5]}}
// expected-note@* {{because '5 <= 4' (5 <= 4) evaluated to false}}
+
+using float8x4 = __attribute__((matrix_type(8,4))) float;
+// expected-error at -1 {{matrix row size too large}}
+
+using float4x8 = __attribute__((matrix_type(4,8))) float;
+// expected-error at -1 {{matrix column size too large}}
More information about the cfe-commits
mailing list