-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[HLSL] Add concepts for Structured buffers #119643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
cbcdcd3
7d11952
9cf81d8
140b7e6
6fb774a
48ea30c
b46c4c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -868,8 +868,54 @@ static Expr *constructTypedBufferConstraintExpr(Sema &S, SourceLocation NameLoc, | |
| return TypedResExpr; | ||
| } | ||
|
|
||
| static ConceptDecl *constructTypedBufferConceptDecl(Sema &S, | ||
| NamespaceDecl *NSD) { | ||
| static Expr *constructStructuredBufferConstraintExpr(Sema &S, | ||
| SourceLocation NameLoc, | ||
| TemplateTypeParmDecl *T) { | ||
| ASTContext &Context = S.getASTContext(); | ||
|
|
||
| // Obtain the QualType for 'bool' | ||
| QualType BoolTy = Context.BoolTy; | ||
|
|
||
| // Create a QualType that points to this TemplateTypeParmDecl | ||
| QualType TType = Context.getTypeDeclType(T); | ||
|
|
||
| // Create a TypeSourceInfo for the template type parameter 'T' | ||
| TypeSourceInfo *TTypeSourceInfo = | ||
| Context.getTrivialTypeSourceInfo(TType, NameLoc); | ||
|
|
||
| TypeTraitExpr *IsIntangibleExpr = | ||
| TypeTraitExpr::Create(Context, BoolTy, NameLoc, UTT_IsIntangibleType, | ||
| {TTypeSourceInfo}, NameLoc, true); | ||
|
|
||
| // negate IsIntangibleExpr | ||
| UnaryOperator *NotIntangibleExpr = UnaryOperator::Create( | ||
| Context, IsIntangibleExpr, UO_Not, BoolTy, VK_LValue, OK_Ordinary, | ||
| NameLoc, false, FPOptionsOverride()); | ||
|
|
||
| // element types also may not be of 0 size | ||
| UnaryExprOrTypeTraitExpr *SizeOfExpr = new (Context) UnaryExprOrTypeTraitExpr( | ||
| UETT_SizeOf, TTypeSourceInfo, BoolTy, NameLoc, NameLoc); | ||
|
|
||
| // Create a BinaryOperator that checks if the size of the type is not equal to | ||
| // 1 Empty structs have a size of 1 in HLSL, so we need to check for that | ||
| IntegerLiteral *rhs = IntegerLiteral::Create( | ||
| Context, llvm::APInt(Context.getTypeSize(Context.getSizeType()), 1, true), | ||
| Context.getSizeType(), NameLoc); | ||
|
|
||
| BinaryOperator *SizeGEQOneExpr = | ||
| BinaryOperator::Create(Context, SizeOfExpr, rhs, BO_GE, BoolTy, VK_LValue, | ||
| OK_Ordinary, NameLoc, FPOptionsOverride()); | ||
|
|
||
| // Combine the two constraints | ||
| BinaryOperator *CombinedExpr = BinaryOperator::Create( | ||
| Context, NotIntangibleExpr, SizeGEQOneExpr, BO_LAnd, BoolTy, VK_LValue, | ||
| OK_Ordinary, NameLoc, FPOptionsOverride()); | ||
|
|
||
| return CombinedExpr; | ||
| } | ||
|
|
||
| static ConceptDecl *constructTypedBufferConceptDecl(Sema &S, NamespaceDecl *NSD, | ||
| bool isTypedBuffer) { | ||
|
||
| ASTContext &Context = S.getASTContext(); | ||
| DeclContext *DC = NSD->getDeclContext(); | ||
| SourceLocation DeclLoc = SourceLocation(); | ||
|
|
@@ -890,9 +936,18 @@ static ConceptDecl *constructTypedBufferConceptDecl(Sema &S, | |
| TemplateParameterList *ConceptParams = TemplateParameterList::Create( | ||
| Context, DeclLoc, DeclLoc, {T}, DeclLoc, nullptr); | ||
|
|
||
| DeclarationName DeclName = DeclarationName( | ||
| &Context.Idents.get("__is_typed_resource_element_compatible")); | ||
| Expr *ConstraintExpr = constructTypedBufferConstraintExpr(S, DeclLoc, T); | ||
| DeclarationName DeclName; | ||
| Expr *ConstraintExpr = nullptr; | ||
|
|
||
| if (isTypedBuffer) { | ||
| DeclName = DeclarationName( | ||
| &Context.Idents.get("__is_typed_resource_element_compatible")); | ||
| ConstraintExpr = constructTypedBufferConstraintExpr(S, DeclLoc, T); | ||
| } else { | ||
| DeclName = DeclarationName( | ||
| &Context.Idents.get("__is_structured_resource_element_compatible")); | ||
| ConstraintExpr = constructStructuredBufferConstraintExpr(S, DeclLoc, T); | ||
| } | ||
|
|
||
| // Create a ConceptDecl | ||
| ConceptDecl *CD = | ||
|
|
@@ -910,8 +965,10 @@ static ConceptDecl *constructTypedBufferConceptDecl(Sema &S, | |
|
|
||
| void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() { | ||
| CXXRecordDecl *Decl; | ||
| ConceptDecl *TypedBufferConcept = | ||
| constructTypedBufferConceptDecl(*SemaPtr, HLSLNamespace); | ||
| ConceptDecl *TypedBufferConcept = constructTypedBufferConceptDecl( | ||
| *SemaPtr, HLSLNamespace, /*isTypedBuffer*/ true); | ||
| ConceptDecl *StructuredBufferConcept = constructTypedBufferConceptDecl( | ||
| *SemaPtr, HLSLNamespace, /*isTypedBuffer*/ false); | ||
| Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RWBuffer") | ||
| .addSimpleTemplateParams({"element_type"}, TypedBufferConcept) | ||
| .finalizeForwardDeclaration(); | ||
|
|
@@ -926,7 +983,7 @@ void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() { | |
|
|
||
| Decl = | ||
| BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RasterizerOrderedBuffer") | ||
| .addSimpleTemplateParams({"element_type"}) | ||
| .addSimpleTemplateParams({"element_type"}, StructuredBufferConcept) | ||
| .finalizeForwardDeclaration(); | ||
| onCompletion(Decl, [this](CXXRecordDecl *Decl) { | ||
| setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, | ||
|
|
@@ -937,7 +994,7 @@ void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() { | |
| }); | ||
|
|
||
| Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "StructuredBuffer") | ||
| .addSimpleTemplateParams({"element_type"}) | ||
| .addSimpleTemplateParams({"element_type"}, StructuredBufferConcept) | ||
| .finalizeForwardDeclaration(); | ||
| onCompletion(Decl, [this](CXXRecordDecl *Decl) { | ||
| setupBufferType(Decl, *SemaPtr, ResourceClass::SRV, ResourceKind::RawBuffer, | ||
|
|
@@ -947,7 +1004,7 @@ void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() { | |
| }); | ||
|
|
||
| Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RWStructuredBuffer") | ||
| .addSimpleTemplateParams({"element_type"}) | ||
| .addSimpleTemplateParams({"element_type"}, StructuredBufferConcept) | ||
| .finalizeForwardDeclaration(); | ||
| onCompletion(Decl, [this](CXXRecordDecl *Decl) { | ||
| setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, ResourceKind::RawBuffer, | ||
|
|
@@ -960,7 +1017,7 @@ void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() { | |
|
|
||
| Decl = | ||
| BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "AppendStructuredBuffer") | ||
| .addSimpleTemplateParams({"element_type"}) | ||
| .addSimpleTemplateParams({"element_type"}, StructuredBufferConcept) | ||
| .finalizeForwardDeclaration(); | ||
| onCompletion(Decl, [this](CXXRecordDecl *Decl) { | ||
| setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, ResourceKind::RawBuffer, | ||
|
|
@@ -971,7 +1028,7 @@ void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() { | |
|
|
||
| Decl = | ||
| BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "ConsumeStructuredBuffer") | ||
| .addSimpleTemplateParams({"element_type"}) | ||
| .addSimpleTemplateParams({"element_type"}, StructuredBufferConcept) | ||
| .finalizeForwardDeclaration(); | ||
| onCompletion(Decl, [this](CXXRecordDecl *Decl) { | ||
| setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, ResourceKind::RawBuffer, | ||
|
|
@@ -982,7 +1039,7 @@ void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() { | |
|
|
||
| Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, | ||
| "RasterizerOrderedStructuredBuffer") | ||
| .addSimpleTemplateParams({"element_type"}) | ||
| .addSimpleTemplateParams({"element_type"}, StructuredBufferConcept) | ||
| .finalizeForwardDeclaration(); | ||
| onCompletion(Decl, [this](CXXRecordDecl *Decl) { | ||
| setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, ResourceKind::RawBuffer, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl -ast-dump -ast-dump-filter=__is_structured_resource_element_compatible %s | FileCheck %s | ||
|
|
||
| // CHECK: ConceptDecl 0x{{[0-9a-f]+}} <<invalid sloc>> <invalid sloc> __is_structured_resource_element_compatible | ||
| // CHECK: |-TemplateTypeParmDecl 0x{{[0-9a-f]+}} <<invalid sloc>> <invalid sloc> referenced typename depth 0 index 0 element_type | ||
| // CHECK: `-BinaryOperator 0x{{[0-9a-f]+}} <<invalid sloc>> 'bool' lvalue '&&' | ||
| // CHECK: |-UnaryOperator 0x{{[0-9a-f]+}} <<invalid sloc>> 'bool' lvalue prefix '~' cannot overflow | ||
|
||
| // CHECK: | `-TypeTraitExpr 0x{{[0-9a-f]+}} <<invalid sloc>> 'bool' __builtin_hlsl_is_intangible | ||
| // CHECK: | `-TemplateTypeParmType 0x{{[0-9a-f]+}} 'element_type' dependent depth 0 index 0 | ||
| // CHECK: | `-TemplateTypeParm 0x{{[0-9a-f]+}} 'element_type' | ||
| // CHECK: `-BinaryOperator 0x{{[0-9a-f]+}} <<invalid sloc>> 'bool' lvalue '>=' | ||
| // CHECK: |-UnaryExprOrTypeTraitExpr 0x{{[0-9a-f]+}} <<invalid sloc>> 'bool' sizeof 'element_type' | ||
| // CHECK: `-IntegerLiteral 0x{{[0-9a-f]+}} <<invalid sloc>> 'unsigned long' 1 | ||
|
|
||
|
|
||
| StructuredBuffer<float> Buffer; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to have a comment that spells out this expression in C++ so that it's easy to understand what exactly we're constructing here.