Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions clang/include/clang/AST/TypeBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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());
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/Basic/LangOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
4 changes: 2 additions & 2 deletions clang/lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4712,8 +4712,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))
Expand Down
8 changes: 6 additions & 2 deletions clang/lib/Basic/LangOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,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();
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Sema/HLSLExternalSemaSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Sema/SemaChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16239,9 +16239,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;
Expand Down
10 changes: 8 additions & 2 deletions clang/lib/Sema/SemaType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2517,12 +2517,18 @@ 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 &&
MatrixColumns > Context.getLangOpts().MaxMatrixDimension) {
Diag(AttrLoc, diag::err_attribute_size_too_large)
<< RowRange << ColRange << "matrix row and column";
return QualType();
}
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();
Expand Down
1 change: 1 addition & 0 deletions clang/test/SemaCXX/matrix-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ void matrix_var_dimensions(int Rows, unsigned Columns, char C) {
using matrix7_t = int __attribute__((matrix_type(1, 0))); // expected-error{{zero matrix size}}
using matrix7_t = int __attribute__((matrix_type(char, 0))); // expected-error{{expected '(' for function-style cast or type construction}}
using matrix8_t = int __attribute__((matrix_type(1048576, 1))); // expected-error{{matrix row size too large}}
using matrix8_t = int __attribute__((matrix_type(1048576, 1048576))); // expected-error{{matrix row and column size too large}}
}

struct S1 {};
Expand Down
23 changes: 23 additions & 0 deletions clang/test/SemaHLSL/BuiltIns/matrix-basic_types-errors.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,26 @@ uint16_t4x4 mat2;
matrix<int, 5, 5> mat3;
// expected-error@-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@-1 {{matrix row size too large}}

using float4x8 = __attribute__((matrix_type(4,8))) float;
// expected-error@-1 {{matrix column size too large}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few other cases to consider:

using float8x8 = __attribute__((matrix_type(8,8))) float; // both dimensions too large

// these are likely tested in C/C++, but might be worth verifying or just adding them:
using floatNeg1x4 = __attribute__((matrix_type(-1,4))) float; // negative rows
using float4xNeg1 = __attribute__((matrix_type(4,-1))) float; // negative columns
using floatNeg1xNeg1 = __attribute__((matrix_type(-1,-1))) float; // both dimensions negative!

using float0x4 = __attribute__((matrix_type(0,4))) float; // zero rows?
using float4x0 = __attribute__((matrix_type(4,0))) float; // zero columns?
using float0x0 = __attribute__((matrix_type(0,0))) float; // both dimensions zero

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current implementation does not do multiple errors so if both row/col are larger than 4 then we will just error matrix row size too large. The negative cases will just be treated as an unsinged integer so 4294967295 and will also be a row dimensions too large case. All of these will error.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can change the BuildMatrixType implementation to allow for both row and col errors.


using float8x8 = __attribute__((matrix_type(8,8))) float;
// expected-error@-1 {{matrix row and column size too large}}

using floatNeg1x4 = __attribute__((matrix_type(-1,4))) float;
// expected-error@-1 {{matrix row size too large}}
using float4xNeg1 = __attribute__((matrix_type(4,-1))) float;
// expected-error@-1 {{matrix column size too large}}
using floatNeg1xNeg1 = __attribute__((matrix_type(-1,-1))) float;
// expected-error@-1 {{matrix row and column size too large}}

using float0x4 = __attribute__((matrix_type(0,4))) float;
// expected-error@-1 {{zero matrix size}}
using float4x0 = __attribute__((matrix_type(4,0))) float;
// expected-error@-1 {{zero matrix size}}
using float0x0 = __attribute__((matrix_type(0,0))) float;
// expected-error@-1 {{zero matrix size}}
20 changes: 20 additions & 0 deletions clang/unittests/Frontend/CompilerInvocationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down