Skip to content

Commit 7033657

Browse files
committed
[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 94b4311 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.
1 parent b5d75b2 commit 7033657

File tree

7 files changed

+15
-21
lines changed

7 files changed

+15
-21
lines changed

clang/include/clang/AST/TypeBase.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4378,8 +4378,6 @@ class ConstantMatrixType final : public MatrixType {
43784378
unsigned NumRows;
43794379
unsigned NumColumns;
43804380

4381-
static constexpr unsigned MaxElementsPerDimension = (1 << 20) - 1;
4382-
43834381
ConstantMatrixType(QualType MatrixElementType, unsigned NRows,
43844382
unsigned NColumns, QualType CanonElementType);
43854383

@@ -4398,16 +4396,6 @@ class ConstantMatrixType final : public MatrixType {
43984396
return getNumRows() * getNumColumns();
43994397
}
44004398

4401-
/// Returns true if \p NumElements is a valid matrix dimension.
4402-
static constexpr bool isDimensionValid(size_t NumElements) {
4403-
return NumElements > 0 && NumElements <= MaxElementsPerDimension;
4404-
}
4405-
4406-
/// Returns the maximum number of elements per dimension.
4407-
static constexpr unsigned getMaxElementsPerDimension() {
4408-
return MaxElementsPerDimension;
4409-
}
4410-
44114399
void Profile(llvm::FoldingSetNodeID &ID) {
44124400
Profile(ID, getElementType(), getNumRows(), getNumColumns(),
44134401
getTypeClass());

clang/include/clang/Basic/LangOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ ENUM_LANGOPT(RegisterStaticDestructors, RegisterStaticDestructorsKind, 2,
432432
LANGOPT(RegCall4, 1, 0, NotCompatible, "Set __regcall4 as a default calling convention to respect __regcall ABI v.4")
433433

434434
LANGOPT(MatrixTypes, 1, 0, NotCompatible, "Enable or disable the builtin matrix type")
435+
VALUE_LANGOPT(MaxMatrixDimension, 32, (1 << 20) - 1, NotCompatible, "maximum allowed matrix dimension")
435436

436437
LANGOPT(CXXAssumptions, 1, 1, NotCompatible, "Enable or disable codegen and compile-time checks for C++23's [[assume]] attribute")
437438

clang/lib/AST/ASTContext.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4713,8 +4713,8 @@ QualType ASTContext::getConstantMatrixType(QualType ElementTy, unsigned NumRows,
47134713

47144714
assert(MatrixType::isValidElementType(ElementTy) &&
47154715
"need a valid element type");
4716-
assert(ConstantMatrixType::isDimensionValid(NumRows) &&
4717-
ConstantMatrixType::isDimensionValid(NumColumns) &&
4716+
assert(NumRows > 0 && NumRows <= LangOpts.MaxMatrixDimension &&
4717+
NumColumns > 0 && NumColumns <= LangOpts.MaxMatrixDimension &&
47184718
"need valid matrix dimensions");
47194719
void *InsertPos = nullptr;
47204720
if (ConstantMatrixType *MTP = MatrixTypes.FindNodeOrInsertPos(ID, InsertPos))

clang/lib/Basic/LangOptions.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,12 @@ void LangOptions::setLangDefaults(LangOptions &Opts, Language Lang,
131131
Opts.NamedLoops = Std.isC2y();
132132

133133
Opts.HLSL = Lang == Language::HLSL;
134-
if (Opts.HLSL && Opts.IncludeDefaultHeader)
135-
Includes.push_back("hlsl.h");
134+
if (Opts.HLSL) {
135+
if (Opts.IncludeDefaultHeader)
136+
Includes.push_back("hlsl.h");
137+
// Set maximum matrix dimension to 4 for HLSL
138+
Opts.MaxMatrixDimension = 4;
139+
}
136140

137141
// Set OpenCL Version.
138142
Opts.OpenCL = Std.isOpenCL();

clang/lib/Sema/HLSLExternalSemaSource.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ void HLSLExternalSemaSource::defineHLSLMatrixAlias() {
159159
SourceLocation(), ColsParam));
160160
TemplateParams.emplace_back(ColsParam);
161161

162-
const unsigned MaxMatDim = 4;
162+
const unsigned MaxMatDim = SemaPtr->getLangOpts().MaxMatrixDimension;
163+
;
163164
auto *MaxRow = IntegerLiteral::Create(
164165
AST, llvm::APInt(AST.getIntWidth(AST.IntTy), MaxMatDim), AST.IntTy,
165166
SourceLocation());

clang/lib/Sema/SemaChecking.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16240,9 +16240,9 @@ getAndVerifyMatrixDimension(Expr *Expr, StringRef Name, Sema &S) {
1624016240
return {};
1624116241
}
1624216242
uint64_t Dim = Value->getZExtValue();
16243-
if (!ConstantMatrixType::isDimensionValid(Dim)) {
16243+
if (Dim == 0 || Dim > S.Context.getLangOpts().MaxMatrixDimension) {
1624416244
S.Diag(Expr->getBeginLoc(), diag::err_builtin_matrix_invalid_dimension)
16245-
<< Name << ConstantMatrixType::getMaxElementsPerDimension();
16245+
<< Name << S.Context.getLangOpts().MaxMatrixDimension;
1624616246
return {};
1624716247
}
1624816248
return Dim;

clang/lib/Sema/SemaType.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2517,12 +2517,12 @@ QualType Sema::BuildMatrixType(QualType ElementTy, Expr *NumRows, Expr *NumCols,
25172517
Diag(AttrLoc, diag::err_attribute_zero_size) << "matrix" << ColRange;
25182518
return QualType();
25192519
}
2520-
if (!ConstantMatrixType::isDimensionValid(MatrixRows)) {
2520+
if (MatrixRows > Context.getLangOpts().MaxMatrixDimension) {
25212521
Diag(AttrLoc, diag::err_attribute_size_too_large)
25222522
<< RowRange << "matrix row";
25232523
return QualType();
25242524
}
2525-
if (!ConstantMatrixType::isDimensionValid(MatrixColumns)) {
2525+
if (MatrixColumns > Context.getLangOpts().MaxMatrixDimension) {
25262526
Diag(AttrLoc, diag::err_attribute_size_too_large)
25272527
<< ColRange << "matrix column";
25282528
return QualType();

0 commit comments

Comments
 (0)