Skip to content

Commit 158ea9b

Browse files
committed
add size constraint to hlsl matrix using language option as default.
1 parent 9e18ebb commit 158ea9b

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

clang/include/clang/Basic/LangOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ ENUM_LANGOPT(HLSLVersion, HLSLLangStd, 16, HLSL_Unset, NotCompatible, "HLSL Vers
243243
LANGOPT(HLSLStrictAvailability, 1, 0, NotCompatible,
244244
"Strict availability diagnostic mode for HLSL built-in functions.")
245245
LANGOPT(HLSLSpvUseUnknownImageFormat, 1, 0, NotCompatible, "For storage images and texel buffers, sets the default format to 'Unknown' when not specified via the `vk::image_format` attribute. If this option is not used, the format is inferred from the resource's data type.")
246+
VALUE_LANGOPT(MaxMatrixDimension, 32, (1 << 20) - 1, NotCompatible, "maximum allowed matrix dimension")
246247

247248
LANGOPT(CUDAIsDevice , 1, 0, NotCompatible, "compiling for CUDA device")
248249
LANGOPT(CUDAAllowVariadicFunctions, 1, 0, NotCompatible, "allowing variadic functions in CUDA device code")

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: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,39 @@ void HLSLExternalSemaSource::defineHLSLMatrixAlias() {
159159
SourceLocation(), ColsParam));
160160
TemplateParams.emplace_back(ColsParam);
161161

162-
auto *ParamList =
163-
TemplateParameterList::Create(AST, SourceLocation(), SourceLocation(),
164-
TemplateParams, SourceLocation(), nullptr);
162+
const unsigned MaxMatDim = SemaPtr->getLangOpts().MaxMatrixDimension;
163+
auto *MaxRow = IntegerLiteral::Create(
164+
AST, llvm::APInt(AST.getIntWidth(AST.IntTy), MaxMatDim), AST.IntTy,
165+
SourceLocation());
166+
auto *MaxCol = IntegerLiteral::Create(
167+
AST, llvm::APInt(AST.getIntWidth(AST.IntTy), MaxMatDim), AST.IntTy,
168+
SourceLocation());
169+
170+
auto *RowsRef = DeclRefExpr::Create(
171+
AST, NestedNameSpecifierLoc(), SourceLocation(), RowsParam,
172+
/*RefersToEnclosingVariableOrCapture*/ false,
173+
DeclarationNameInfo(RowsParam->getDeclName(), SourceLocation()),
174+
AST.IntTy, VK_LValue);
175+
auto *ColsRef = DeclRefExpr::Create(
176+
AST, NestedNameSpecifierLoc(), SourceLocation(), ColsParam,
177+
/*RefersToEnclosingVariableOrCapture*/ false,
178+
DeclarationNameInfo(ColsParam->getDeclName(), SourceLocation()),
179+
AST.IntTy, VK_LValue);
180+
181+
auto *RowsLE = BinaryOperator::Create(AST, RowsRef, MaxRow, BO_LE, AST.BoolTy,
182+
VK_PRValue, OK_Ordinary,
183+
SourceLocation(), FPOptionsOverride());
184+
auto *ColsLE = BinaryOperator::Create(AST, ColsRef, MaxCol, BO_LE, AST.BoolTy,
185+
VK_PRValue, OK_Ordinary,
186+
SourceLocation(), FPOptionsOverride());
187+
188+
auto *RequiresExpr = BinaryOperator::Create(
189+
AST, RowsLE, ColsLE, BO_LAnd, AST.BoolTy, VK_PRValue, OK_Ordinary,
190+
SourceLocation(), FPOptionsOverride());
191+
192+
auto *ParamList = TemplateParameterList::Create(
193+
AST, SourceLocation(), SourceLocation(), TemplateParams, SourceLocation(),
194+
RequiresExpr);
165195

166196
IdentifierInfo &II = AST.Idents.get("matrix", tok::TokenKind::identifier);
167197

clang/test/SemaHLSL/BuiltIns/matrix-basic_types-errors.hlsl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,9 @@ uint64_t5x5 mat;
44
// expected-error@-1 {{unknown type name 'uint64_t5x5'}}
55

66
// Note: this one only fails because -fnative-half-type is not set
7-
uint16_t4x4 mat;
7+
uint16_t4x4 mat2;
88
// expected-error@-1 {{unknown type name 'uint16_t4x4'}}
9+
10+
matrix<int, 5, 5> mat3;
11+
// expected-error@-1 {{constraints not satisfied for alias template 'matrix' [with element = int, rows_count = 5, cols_count = 5]}}
12+
// expected-note@* {{because '5 <= 4' (5 <= 4) evaluated to false}}

clang/test/SemaHLSL/BuiltIns/matrix-errors.hlsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// Some bad declarations
44
hlsl::matrix ShouldWorkSomeday; // expected-error{{use of alias template 'hlsl::matrix' requires template arguments}}
5-
// expected-note@*:* {{template declaration from hidden source: template <class element = float, int rows_count = 4, int cols_count = 4> using matrix = element __attribute__((matrix_type(rows_count, cols_count)))}}
5+
// expected-note@*:* {{template declaration from hidden source: template <class element = float, int rows_count = 4, int cols_count = 4> requires rows_count <= 4 && cols_count <= 4 using matrix = element __attribute__((matrix_type(rows_count, cols_count)))}}
66

77
hlsl::matrix<1,1,1> BadMat; // expected-error{{template argument for template type parameter must be a type}}
88
// expected-note@*:* {{template parameter from hidden source: class element = float}}
@@ -11,7 +11,7 @@ hlsl::matrix<int, float,4> AnotherBadMat; // expected-error{{template argument f
1111
// expected-note@*:* {{template parameter from hidden source: int rows_count = 4}}
1212

1313
hlsl::matrix<int, 2, 3, 2> YABV; // expected-error{{too many template arguments for alias template 'matrix'}}
14-
// expected-note@*:* {{template declaration from hidden source: template <class element = float, int rows_count = 4, int cols_count = 4> using matrix = element __attribute__((matrix_type(rows_count, cols_count)))}}
14+
// expected-note@*:* {{template declaration from hidden source: template <class element = float, int rows_count = 4, int cols_count = 4> requires rows_count <= 4 && cols_count <= 4 using matrix = element __attribute__((matrix_type(rows_count, cols_count)))}}
1515

1616
// This code is rejected by clang because clang puts the HLSL built-in types
1717
// into the HLSL namespace.

0 commit comments

Comments
 (0)