https://github.com/bob80905 updated
https://github.com/llvm/llvm-project/pull/119643
>From cbcdcd37ec82e7bbb5fdd1cf83b093778d4fcf9f Mon Sep 17 00:00:00 2001
From: Joshua Batista
Date: Wed, 11 Dec 2024 17:02:02 -0800
Subject: [PATCH 1/7] add concepts for raw buffers
---
clang/lib/Sema/HLSLExternalSemaSource.cpp | 63 +++
.../test/AST/HLSL/StructuredBuffers-AST.hlsl | 16 +
...w_resource_element_compatible_concept.hlsl | 10 +++
3 files changed, 76 insertions(+), 13 deletions(-)
create mode 100644
clang/test/AST/HLSL/is_raw_resource_element_compatible_concept.hlsl
diff --git a/clang/lib/Sema/HLSLExternalSemaSource.cpp
b/clang/lib/Sema/HLSLExternalSemaSource.cpp
index 79fc2751b73812..e109e8b9abbfa7 100644
--- a/clang/lib/Sema/HLSLExternalSemaSource.cpp
+++ b/clang/lib/Sema/HLSLExternalSemaSource.cpp
@@ -868,8 +868,34 @@ static Expr *constructTypedBufferConstraintExpr(Sema &S,
SourceLocation NameLoc,
return TypedResExpr;
}
-static ConceptDecl *constructTypedBufferConceptDecl(Sema &S,
-NamespaceDecl *NSD) {
+static Expr *constructRawBufferConstraintExpr(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());
+
+ return NotIntangibleExpr;
+}
+
+static ConceptDecl *constructTypedBufferConceptDecl(Sema &S, NamespaceDecl
*NSD,
+bool isTypedBuffer) {
ASTContext &Context = S.getASTContext();
DeclContext *DC = NSD->getDeclContext();
SourceLocation DeclLoc = SourceLocation();
@@ -890,9 +916,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_raw_resource_element_compatible"));
+ConstraintExpr = constructRawBufferConstraintExpr(S, DeclLoc, T);
+ }
// Create a ConceptDecl
ConceptDecl *CD =
@@ -910,8 +945,10 @@ static ConceptDecl *constructTypedBufferConceptDecl(Sema
&S,
void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
CXXRecordDecl *Decl;
- ConceptDecl *TypedBufferConcept =
- constructTypedBufferConceptDecl(*SemaPtr, HLSLNamespace);
+ ConceptDecl *TypedBufferConcept = constructTypedBufferConceptDecl(
+ *SemaPtr, HLSLNamespace, /*isTypedBuffer*/ true);
+ ConceptDecl *RawBufferConcept = constructTypedBufferConceptDecl(
+ *SemaPtr, HLSLNamespace, /*isTypedBuffer*/ false);
Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RWBuffer")
.addSimpleTemplateParams({"element_type"}, TypedBufferConcept)
.finalizeForwardDeclaration();
@@ -926,7 +963,7 @@ void
HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
Decl =
BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace,
"RasterizerOrderedBuffer")
- .addSimpleTemplateParams({"element_type"})
+ .addSimpleTemplateParams({"element_type"}, RawBufferConcept)
.finalizeForwardDeclaration();
onCompletion(Decl, [this](CXXRecordDecl *Decl) {
setupBufferType(Decl, *SemaPtr, ResourceClass::UAV,
@@ -937,7 +974,7 @@ void
HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
});
Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "StructuredBuffer")
- .addSimpleTemplateParams({"element_type"})
+ .addSimpleTemplateParams({"element_type"}, RawBufferConcept)
.finalizeForwardDeclaration();
onCompletion(Decl, [this](CXXRecordDecl *Decl) {
setupBufferType(Decl, *SemaPtr, ResourceClass::SRV,
ResourceKind::RawBuffer,
@@ -947,