diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index a7c514e809aa9..e8869e04390f3 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -4582,7 +4582,7 @@ defm ptrauth_block_descriptor_pointers : OptInCC1FFlag<"ptrauth-block-descriptor def fenable_matrix : Flag<["-"], "fenable-matrix">, Group, Visibility<[ClangOption, CC1Option]>, HelpText<"Enable matrix data type and related builtin functions">, - MarshallingInfoFlag>; + MarshallingInfoFlag, hlsl.KeyPath>; defm raw_string_literals : BoolFOption<"raw-string-literals", LangOpts<"RawStringLiterals">, Default, diff --git a/clang/include/clang/Sema/HLSLExternalSemaSource.h b/clang/include/clang/Sema/HLSLExternalSemaSource.h index d93fb8c8eef6b..049fc7b8fe3f2 100644 --- a/clang/include/clang/Sema/HLSLExternalSemaSource.h +++ b/clang/include/clang/Sema/HLSLExternalSemaSource.h @@ -44,6 +44,7 @@ class HLSLExternalSemaSource : public ExternalSemaSource { private: void defineTrivialHLSLTypes(); void defineHLSLVectorAlias(); + void defineHLSLMatrixAlias(); void defineHLSLTypesWithForwardDeclarations(); void onCompletion(CXXRecordDecl *Record, CompletionFunction Fn); }; diff --git a/clang/lib/AST/TypePrinter.cpp b/clang/lib/AST/TypePrinter.cpp index cd59678d67f2f..f3448af5f8f50 100644 --- a/clang/lib/AST/TypePrinter.cpp +++ b/clang/lib/AST/TypePrinter.cpp @@ -846,16 +846,45 @@ void TypePrinter::printExtVectorAfter(const ExtVectorType *T, raw_ostream &OS) { } } -void TypePrinter::printConstantMatrixBefore(const ConstantMatrixType *T, - raw_ostream &OS) { - printBefore(T->getElementType(), OS); - OS << " __attribute__((matrix_type("; +static void printDims(const ConstantMatrixType *T, raw_ostream &OS) { OS << T->getNumRows() << ", " << T->getNumColumns(); +} + +static void printHLSLMatrixBefore(TypePrinter &TP, const ConstantMatrixType *T, + raw_ostream &OS) { + OS << "matrix<"; + TP.printBefore(T->getElementType(), OS); +} + +static void printHLSLMatrixAfter(const ConstantMatrixType *T, raw_ostream &OS) { + OS << ", "; + printDims(T, OS); + OS << ">"; +} + +static void printClangMatrixBefore(TypePrinter &TP, const ConstantMatrixType *T, + raw_ostream &OS) { + TP.printBefore(T->getElementType(), OS); + OS << " __attribute__((matrix_type("; + printDims(T, OS); OS << ")))"; } +void TypePrinter::printConstantMatrixBefore(const ConstantMatrixType *T, + raw_ostream &OS) { + if (Policy.UseHLSLTypes) { + printHLSLMatrixBefore(*this, T, OS); + return; + } + printClangMatrixBefore(*this, T, OS); +} + void TypePrinter::printConstantMatrixAfter(const ConstantMatrixType *T, raw_ostream &OS) { + if (Policy.UseHLSLTypes) { + printHLSLMatrixAfter(T, OS); + return; + } printAfter(T->getElementType(), OS); } diff --git a/clang/lib/Headers/hlsl/hlsl_basic_types.h b/clang/lib/Headers/hlsl/hlsl_basic_types.h index eff94e0d7f950..fc1e265067714 100644 --- a/clang/lib/Headers/hlsl/hlsl_basic_types.h +++ b/clang/lib/Headers/hlsl/hlsl_basic_types.h @@ -115,6 +115,239 @@ typedef vector float64_t2; typedef vector float64_t3; typedef vector float64_t4; +#ifdef __HLSL_ENABLE_16_BIT +typedef matrix int16_t1x1; +typedef matrix int16_t1x2; +typedef matrix int16_t1x3; +typedef matrix int16_t1x4; +typedef matrix int16_t2x1; +typedef matrix int16_t2x2; +typedef matrix int16_t2x3; +typedef matrix int16_t2x4; +typedef matrix int16_t3x1; +typedef matrix int16_t3x2; +typedef matrix int16_t3x3; +typedef matrix int16_t3x4; +typedef matrix int16_t4x1; +typedef matrix int16_t4x2; +typedef matrix int16_t4x3; +typedef matrix int16_t4x4; +typedef matrix uint16_t1x1; +typedef matrix uint16_t1x2; +typedef matrix uint16_t1x3; +typedef matrix uint16_t1x4; +typedef matrix uint16_t2x1; +typedef matrix uint16_t2x2; +typedef matrix uint16_t2x3; +typedef matrix uint16_t2x4; +typedef matrix uint16_t3x1; +typedef matrix uint16_t3x2; +typedef matrix uint16_t3x3; +typedef matrix uint16_t3x4; +typedef matrix uint16_t4x1; +typedef matrix uint16_t4x2; +typedef matrix uint16_t4x3; +typedef matrix uint16_t4x4; +#endif + +typedef matrix int1x1; +typedef matrix int1x2; +typedef matrix int1x3; +typedef matrix int1x4; +typedef matrix int2x1; +typedef matrix int2x2; +typedef matrix int2x3; +typedef matrix int2x4; +typedef matrix int3x1; +typedef matrix int3x2; +typedef matrix int3x3; +typedef matrix int3x4; +typedef matrix int4x1; +typedef matrix int4x2; +typedef matrix int4x3; +typedef matrix int4x4; +typedef matrix uint1x1; +typedef matrix uint1x2; +typedef matrix uint1x3; +typedef matrix uint1x4; +typedef matrix uint2x1; +typedef matrix uint2x2; +typedef matrix uint2x3; +typedef matrix uint2x4; +typedef matrix uint3x1; +typedef matrix uint3x2; +typedef matrix uint3x3; +typedef matrix uint3x4; +typedef matrix uint4x1; +typedef matrix uint4x2; +typedef matrix uint4x3; +typedef matrix uint4x4; +typedef matrix int32_t1x1; +typedef matrix int32_t1x2; +typedef matrix int32_t1x3; +typedef matrix int32_t1x4; +typedef matrix int32_t2x1; +typedef matrix int32_t2x2; +typedef matrix int32_t2x3; +typedef matrix int32_t2x4; +typedef matrix int32_t3x1; +typedef matrix int32_t3x2; +typedef matrix int32_t3x3; +typedef matrix int32_t3x4; +typedef matrix int32_t4x1; +typedef matrix int32_t4x2; +typedef matrix int32_t4x3; +typedef matrix int32_t4x4; +typedef matrix uint32_t1x1; +typedef matrix uint32_t1x2; +typedef matrix uint32_t1x3; +typedef matrix uint32_t1x4; +typedef matrix uint32_t2x1; +typedef matrix uint32_t2x2; +typedef matrix uint32_t2x3; +typedef matrix uint32_t2x4; +typedef matrix uint32_t3x1; +typedef matrix uint32_t3x2; +typedef matrix uint32_t3x3; +typedef matrix uint32_t3x4; +typedef matrix uint32_t4x1; +typedef matrix uint32_t4x2; +typedef matrix uint32_t4x3; +typedef matrix uint32_t4x4; +typedef matrix int64_t1x1; +typedef matrix int64_t1x2; +typedef matrix int64_t1x3; +typedef matrix int64_t1x4; +typedef matrix int64_t2x1; +typedef matrix int64_t2x2; +typedef matrix int64_t2x3; +typedef matrix int64_t2x4; +typedef matrix int64_t3x1; +typedef matrix int64_t3x2; +typedef matrix int64_t3x3; +typedef matrix int64_t3x4; +typedef matrix int64_t4x1; +typedef matrix int64_t4x2; +typedef matrix int64_t4x3; +typedef matrix int64_t4x4; +typedef matrix uint64_t1x1; +typedef matrix uint64_t1x2; +typedef matrix uint64_t1x3; +typedef matrix uint64_t1x4; +typedef matrix uint64_t2x1; +typedef matrix uint64_t2x2; +typedef matrix uint64_t2x3; +typedef matrix uint64_t2x4; +typedef matrix uint64_t3x1; +typedef matrix uint64_t3x2; +typedef matrix uint64_t3x3; +typedef matrix uint64_t3x4; +typedef matrix uint64_t4x1; +typedef matrix uint64_t4x2; +typedef matrix uint64_t4x3; +typedef matrix uint64_t4x4; + +typedef matrix half1x1; +typedef matrix half1x2; +typedef matrix half1x3; +typedef matrix half1x4; +typedef matrix half2x1; +typedef matrix half2x2; +typedef matrix half2x3; +typedef matrix half2x4; +typedef matrix half3x1; +typedef matrix half3x2; +typedef matrix half3x3; +typedef matrix half3x4; +typedef matrix half4x1; +typedef matrix half4x2; +typedef matrix half4x3; +typedef matrix half4x4; +typedef matrix float1x1; +typedef matrix float1x2; +typedef matrix float1x3; +typedef matrix float1x4; +typedef matrix float2x1; +typedef matrix float2x2; +typedef matrix float2x3; +typedef matrix float2x4; +typedef matrix float3x1; +typedef matrix float3x2; +typedef matrix float3x3; +typedef matrix float3x4; +typedef matrix float4x1; +typedef matrix float4x2; +typedef matrix float4x3; +typedef matrix float4x4; +typedef matrix double1x1; +typedef matrix double1x2; +typedef matrix double1x3; +typedef matrix double1x4; +typedef matrix double2x1; +typedef matrix double2x2; +typedef matrix double2x3; +typedef matrix double2x4; +typedef matrix double3x1; +typedef matrix double3x2; +typedef matrix double3x3; +typedef matrix double3x4; +typedef matrix double4x1; +typedef matrix double4x2; +typedef matrix double4x3; +typedef matrix double4x4; + +#ifdef __HLSL_ENABLE_16_BIT +typedef matrix float16_t1x1; +typedef matrix float16_t1x2; +typedef matrix float16_t1x3; +typedef matrix float16_t1x4; +typedef matrix float16_t2x1; +typedef matrix float16_t2x2; +typedef matrix float16_t2x3; +typedef matrix float16_t2x4; +typedef matrix float16_t3x1; +typedef matrix float16_t3x2; +typedef matrix float16_t3x3; +typedef matrix float16_t3x4; +typedef matrix float16_t4x1; +typedef matrix float16_t4x2; +typedef matrix float16_t4x3; +typedef matrix float16_t4x4; +#endif + +typedef matrix float32_t1x1; +typedef matrix float32_t1x2; +typedef matrix float32_t1x3; +typedef matrix float32_t1x4; +typedef matrix float32_t2x1; +typedef matrix float32_t2x2; +typedef matrix float32_t2x3; +typedef matrix float32_t2x4; +typedef matrix float32_t3x1; +typedef matrix float32_t3x2; +typedef matrix float32_t3x3; +typedef matrix float32_t3x4; +typedef matrix float32_t4x1; +typedef matrix float32_t4x2; +typedef matrix float32_t4x3; +typedef matrix float32_t4x4; +typedef matrix float64_t1x1; +typedef matrix float64_t1x2; +typedef matrix float64_t1x3; +typedef matrix float64_t1x4; +typedef matrix float64_t2x1; +typedef matrix float64_t2x2; +typedef matrix float64_t2x3; +typedef matrix float64_t2x4; +typedef matrix float64_t3x1; +typedef matrix float64_t3x2; +typedef matrix float64_t3x3; +typedef matrix float64_t3x4; +typedef matrix float64_t4x1; +typedef matrix float64_t4x2; +typedef matrix float64_t4x3; +typedef matrix float64_t4x4; + } // namespace hlsl #endif //_HLSL_HLSL_BASIC_TYPES_H_ diff --git a/clang/lib/Sema/HLSLExternalSemaSource.cpp b/clang/lib/Sema/HLSLExternalSemaSource.cpp index 3386d8da281e9..ebf86b756acc8 100644 --- a/clang/lib/Sema/HLSLExternalSemaSource.cpp +++ b/clang/lib/Sema/HLSLExternalSemaSource.cpp @@ -121,8 +121,110 @@ void HLSLExternalSemaSource::defineHLSLVectorAlias() { HLSLNamespace->addDecl(Template); } +void HLSLExternalSemaSource::defineHLSLMatrixAlias() { + ASTContext &AST = SemaPtr->getASTContext(); + llvm::SmallVector TemplateParams; + + auto *TypeParam = TemplateTypeParmDecl::Create( + AST, HLSLNamespace, SourceLocation(), SourceLocation(), 0, 0, + &AST.Idents.get("element", tok::TokenKind::identifier), false, false); + TypeParam->setDefaultArgument( + AST, SemaPtr->getTrivialTemplateArgumentLoc( + TemplateArgument(AST.FloatTy), QualType(), SourceLocation())); + + TemplateParams.emplace_back(TypeParam); + + // these should be 64 bit to be consistent with other clang matrices. + auto *RowsParam = NonTypeTemplateParmDecl::Create( + AST, HLSLNamespace, SourceLocation(), SourceLocation(), 0, 1, + &AST.Idents.get("rows_count", tok::TokenKind::identifier), AST.IntTy, + false, AST.getTrivialTypeSourceInfo(AST.IntTy)); + llvm::APInt RVal(AST.getIntWidth(AST.IntTy), 4); + TemplateArgument RDefault(AST, llvm::APSInt(std::move(RVal)), AST.IntTy, + /*IsDefaulted=*/true); + RowsParam->setDefaultArgument( + AST, SemaPtr->getTrivialTemplateArgumentLoc(RDefault, AST.IntTy, + SourceLocation(), RowsParam)); + TemplateParams.emplace_back(RowsParam); + + auto *ColsParam = NonTypeTemplateParmDecl::Create( + AST, HLSLNamespace, SourceLocation(), SourceLocation(), 0, 2, + &AST.Idents.get("cols_count", tok::TokenKind::identifier), AST.IntTy, + false, AST.getTrivialTypeSourceInfo(AST.IntTy)); + llvm::APInt CVal(AST.getIntWidth(AST.IntTy), 4); + TemplateArgument CDefault(AST, llvm::APSInt(std::move(CVal)), AST.IntTy, + /*IsDefaulted=*/true); + ColsParam->setDefaultArgument( + AST, SemaPtr->getTrivialTemplateArgumentLoc(CDefault, AST.IntTy, + SourceLocation(), ColsParam)); + TemplateParams.emplace_back(ColsParam); + + const unsigned MaxMatDim = 4; + auto *MaxRow = IntegerLiteral::Create( + AST, llvm::APInt(AST.getIntWidth(AST.IntTy), MaxMatDim), AST.IntTy, + SourceLocation()); + auto *MaxCol = IntegerLiteral::Create( + AST, llvm::APInt(AST.getIntWidth(AST.IntTy), MaxMatDim), AST.IntTy, + SourceLocation()); + + auto *RowsRef = DeclRefExpr::Create( + AST, NestedNameSpecifierLoc(), SourceLocation(), RowsParam, + /*RefersToEnclosingVariableOrCapture*/ false, + DeclarationNameInfo(RowsParam->getDeclName(), SourceLocation()), + AST.IntTy, VK_LValue); + auto *ColsRef = DeclRefExpr::Create( + AST, NestedNameSpecifierLoc(), SourceLocation(), ColsParam, + /*RefersToEnclosingVariableOrCapture*/ false, + DeclarationNameInfo(ColsParam->getDeclName(), SourceLocation()), + AST.IntTy, VK_LValue); + + auto *RowsLE = BinaryOperator::Create(AST, RowsRef, MaxRow, BO_LE, AST.BoolTy, + VK_PRValue, OK_Ordinary, + SourceLocation(), FPOptionsOverride()); + auto *ColsLE = BinaryOperator::Create(AST, ColsRef, MaxCol, BO_LE, AST.BoolTy, + VK_PRValue, OK_Ordinary, + SourceLocation(), FPOptionsOverride()); + + auto *RequiresExpr = BinaryOperator::Create( + AST, RowsLE, ColsLE, BO_LAnd, AST.BoolTy, VK_PRValue, OK_Ordinary, + SourceLocation(), FPOptionsOverride()); + + auto *ParamList = TemplateParameterList::Create( + AST, SourceLocation(), SourceLocation(), TemplateParams, SourceLocation(), + RequiresExpr); + + IdentifierInfo &II = AST.Idents.get("matrix", tok::TokenKind::identifier); + + QualType AliasType = AST.getDependentSizedMatrixType( + AST.getTemplateTypeParmType(0, 0, false, TypeParam), + DeclRefExpr::Create( + AST, NestedNameSpecifierLoc(), SourceLocation(), RowsParam, false, + DeclarationNameInfo(RowsParam->getDeclName(), SourceLocation()), + AST.IntTy, VK_LValue), + DeclRefExpr::Create( + AST, NestedNameSpecifierLoc(), SourceLocation(), ColsParam, false, + DeclarationNameInfo(ColsParam->getDeclName(), SourceLocation()), + AST.IntTy, VK_LValue), + SourceLocation()); + + auto *Record = TypeAliasDecl::Create(AST, HLSLNamespace, SourceLocation(), + SourceLocation(), &II, + AST.getTrivialTypeSourceInfo(AliasType)); + Record->setImplicit(true); + + auto *Template = + TypeAliasTemplateDecl::Create(AST, HLSLNamespace, SourceLocation(), + Record->getIdentifier(), ParamList, Record); + + Record->setDescribedAliasTemplate(Template); + Template->setImplicit(true); + Template->setLexicalDeclContext(Record->getDeclContext()); + HLSLNamespace->addDecl(Template); +} + void HLSLExternalSemaSource::defineTrivialHLSLTypes() { defineHLSLVectorAlias(); + defineHLSLMatrixAlias(); } /// Set up common members and attributes for buffer types diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp index 0af38472b0fec..38f81a24945c5 100644 --- a/clang/lib/Sema/SemaHLSL.cpp +++ b/clang/lib/Sema/SemaHLSL.cpp @@ -3285,7 +3285,6 @@ static void BuildFlattenedTypeList(QualType BaseTy, while (!WorkList.empty()) { QualType T = WorkList.pop_back_val(); T = T.getCanonicalType().getUnqualifiedType(); - assert(!isa(T) && "Matrix types not yet supported in HLSL"); if (const auto *AT = dyn_cast(T)) { llvm::SmallVector ElementFields; // Generally I've avoided recursion in this algorithm, but arrays of diff --git a/clang/test/AST/HLSL/matrix-alias.hlsl b/clang/test/AST/HLSL/matrix-alias.hlsl new file mode 100644 index 0000000000000..2758b6f0d202f --- /dev/null +++ b/clang/test/AST/HLSL/matrix-alias.hlsl @@ -0,0 +1,49 @@ +// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -ast-dump -o - %s | FileCheck %s + +// Test that matrix aliases are set up properly for HLSL + +// CHECK: NamespaceDecl 0x{{[0-9a-fA-F]+}} <> implicit hlsl +// CHECK-NEXT: TypeAliasTemplateDecl 0x{{[0-9a-fA-F]+}} <> implicit vector +// CHECK-NEXT: TemplateTypeParmDecl 0x{{[0-9a-fA-F]+}} <> class depth 0 index 0 element +// CHECK-NEXT: TemplateArgument type 'float' +// CHECK-NEXT: BuiltinType 0x{{[0-9a-fA-F]+}} 'float' +// CHECK-NEXT: NonTypeTemplateParmDecl 0x{{[0-9a-fA-F]+}} <> 'int' depth 0 index 1 element_count +// CHECK-NEXT: TemplateArgument expr +// CHECK-NEXT: IntegerLiteral 0x{{[0-9a-fA-F]+}} <> 'int' 4 +// CHECK-NEXT: TypeAliasDecl 0x{{[0-9a-fA-F]+}} <> implicit vector 'vector' +// CHECK-NEXT: DependentSizedExtVectorType 0x{{[0-9a-fA-F]+}} 'vector' dependent +// CHECK-NEXT: TemplateTypeParmType 0x{{[0-9a-fA-F]+}} 'element' dependent depth 0 index 0 +// CHECK-NEXT: TemplateTypeParm 0x{{[0-9a-fA-F]+}} 'element' +// CHECK-NEXT: DeclRefExpr 0x{{[0-9a-fA-F]+}} <> 'int' lvalue +// CHECK-SAME: NonTypeTemplateParm 0x{{[0-9a-fA-F]+}} 'element_count' 'int' + +// Make sure we got a using directive at the end. +// CHECK: UsingDirectiveDecl 0x{{[0-9a-fA-F]+}} <> Namespace 0x{{[0-9a-fA-F]+}} 'hlsl' + +[numthreads(1,1,1)] +int entry() { + // Verify that the alias is generated inside the hlsl namespace. + hlsl::matrix Mat2x2f; + + // CHECK: DeclStmt 0x{{[0-9a-fA-F]+}} + // CHECK-NEXT: VarDecl 0x{{[0-9a-fA-F]+}} col:29 Mat2x2f 'hlsl::matrix' + + // Verify that you don't need to specify the namespace. + matrix Mat2x2i; + + // CHECK: DeclStmt 0x{{[0-9a-fA-F]+}} + // CHECK-NEXT: VarDecl 0x{{[0-9a-fA-F]+}} col:21 Mat2x2i 'matrix' + + // Build a bigger matrix. + matrix Mat4x4d; + + // CHECK: DeclStmt 0x{{[0-9a-fA-F]+}} + // CHECK-NEXT: VarDecl 0x{{[0-9a-fA-F]+}} col:24 Mat4x4d 'matrix' + + // Verify that the implicit arguments generate the correct type. + matrix<> ImpMat4x4; + + // CHECK: DeclStmt 0x{{[0-9a-fA-F]+}} + // CHECK-NEXT: VarDecl 0x{{[0-9a-fA-F]+}} col:12 ImpMat4x4 'matrix<>':'matrix' + return 1; +} diff --git a/clang/test/CodeGenHLSL/builtins/transpose-builtin.hlsl b/clang/test/CodeGenHLSL/builtins/transpose-builtin.hlsl new file mode 100644 index 0000000000000..86aa7cd6985dd --- /dev/null +++ b/clang/test/CodeGenHLSL/builtins/transpose-builtin.hlsl @@ -0,0 +1,30 @@ +// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.3-library %s -fnative-half-type -emit-llvm -disable-llvm-passes -o - | FileCheck %s + +// NOTE: This test is only to confirm we can do codgen with the matrix alias. + +// CHECK-LABEL: define {{.*}}transpose_half_2x2 +void transpose_half_2x2(half2x2 a) { + // CHECK: [[A:%.*]] = load <4 x half>, ptr {{.*}}, align 2 + // CHECK-NEXT: [[TRANS:%.*]] = call {{.*}}<4 x half> @llvm.matrix.transpose.v4f16(<4 x half> [[A]], i32 2, i32 2) + // CHECK-NEXT: store <4 x half> [[TRANS]], ptr %a_t, align 2 + + half2x2 a_t = __builtin_matrix_transpose(a); +} + +// CHECK-LABEL: define {{.*}}transpose_float_3x2 +void transpose_float_3x2(float3x2 a) { + // CHECK: [[A:%.*]] = load <6 x float>, ptr {{.*}}, align 4 + // CHECK-NEXT: [[TRANS:%.*]] = call {{.*}}<6 x float> @llvm.matrix.transpose.v6f32(<6 x float> [[A]], i32 3, i32 2) + // CHECK-NEXT: store <6 x float> [[TRANS]], ptr %a_t, align 4 + + float2x3 a_t = __builtin_matrix_transpose(a); +} + +// CHECK-LABEL: define {{.*}}transpose_int_4x3 +void transpose_int_4x3(int4x3 a) { + // CHECK: [[A:%.*]] = load <12 x i32>, ptr {{.*}}, align 4 + // CHECK-NEXT: [[TRANS:%.*]] = call <12 x i32> @llvm.matrix.transpose.v12i32(<12 x i32> [[A]], i32 4, i32 3) + // CHECK-NEXT: store <12 x i32> [[TRANS]], ptr %a_t, align 4 + + int3x4 a_t = __builtin_matrix_transpose(a); +} diff --git a/clang/test/SemaHLSL/BuiltIns/matrix-basic_types-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/matrix-basic_types-errors.hlsl new file mode 100644 index 0000000000000..6f6b01bac829e --- /dev/null +++ b/clang/test/SemaHLSL/BuiltIns/matrix-basic_types-errors.hlsl @@ -0,0 +1,12 @@ +// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -emit-llvm-only -disable-llvm-passes -verify + +uint64_t5x5 mat; +// expected-error@-1 {{unknown type name 'uint64_t5x5'}} + +// Note: this one only fails because -fnative-half-type is not set +uint16_t4x4 mat2; +// expected-error@-1 {{unknown type name 'uint16_t4x4'}} + +matrix 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}} diff --git a/clang/test/SemaHLSL/BuiltIns/matrix-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/matrix-errors.hlsl new file mode 100644 index 0000000000000..03751878bbb98 --- /dev/null +++ b/clang/test/SemaHLSL/BuiltIns/matrix-errors.hlsl @@ -0,0 +1,29 @@ +// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -fsyntax-only -verify %s + +// Some bad declarations +hlsl::matrix ShouldWorkSomeday; // expected-error{{use of alias template 'hlsl::matrix' requires template arguments}} +// expected-note@*:* {{template declaration from hidden source: template requires rows_count <= 4 && cols_count <= 4 using matrix = element __attribute__((matrix_type(rows_count, cols_count)))}} + +hlsl::matrix<1,1,1> BadMat; // expected-error{{template argument for template type parameter must be a type}} +// expected-note@*:* {{template parameter from hidden source: class element = float}} + +hlsl::matrix AnotherBadMat; // expected-error{{template argument for non-type template parameter must be an expression}} +// expected-note@*:* {{template parameter from hidden source: int rows_count = 4}} + +hlsl::matrix YABV; // expected-error{{too many template arguments for alias template 'matrix'}} +// expected-note@*:* {{template declaration from hidden source: template requires rows_count <= 4 && cols_count <= 4 using matrix = element __attribute__((matrix_type(rows_count, cols_count)))}} + +// This code is rejected by clang because clang puts the HLSL built-in types +// into the HLSL namespace. +namespace hlsl { + struct matrix {}; // expected-error {{redefinition of 'matrix'}} +} + +// This code is rejected by dxc because dxc puts the HLSL built-in types +// into the global space, but clang will allow it even though it will shadow the +// matrix template. +struct matrix {}; // expected-note {{candidate found by name lookup is 'matrix'}} + +matrix matInt2x2; // expected-error {{reference to 'matrix' is ambiguous}} + +// expected-note@*:* {{candidate found by name lookup is 'hlsl::matrix'}}