[clang] Add concepts for raw buffers (PR #119643)

2024-12-13 Thread Joshua Batista via cfe-commits

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/5] 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,

[clang] Add concepts for raw buffers (PR #119643)

2024-12-13 Thread Helena Kotas via cfe-commits


@@ -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) {

hekota wrote:

This function should not have "Typed" in the name since it can create both 
typed and structured buffer concept decls.

https://github.com/llvm/llvm-project/pull/119643
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add concepts for raw buffers (PR #119643)

2024-12-12 Thread Joshua Batista via cfe-commits

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/4] 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,

[clang] Add concepts for raw buffers (PR #119643)

2024-12-12 Thread Justin Bogner via cfe-commits

bogner wrote:

This should really be talking about concepts for structured buffers, not raw 
buffers. At the HLSL language level, there is nothing called a RawBuffer - 
there is ByteAddressBuffer and various kinds of StructuredBuffer, which are 
represented lower in the stack as a "raw" buffer. Given that there are no 
concepts for ByteAddressBuffer (since it isn't a template), these concepts 
apply specifically to structured buffers, so the naming should reflect that.

https://github.com/llvm/llvm-project/pull/119643
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add concepts for raw buffers (PR #119643)

2024-12-12 Thread Joshua Batista via cfe-commits

https://github.com/bob80905 edited 
https://github.com/llvm/llvm-project/pull/119643
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add concepts for raw buffers (PR #119643)

2024-12-12 Thread Joshua Batista via cfe-commits

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/3] 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,

[clang] Add concepts for raw buffers (PR #119643)

2024-12-12 Thread Joshua Batista via cfe-commits

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/2] 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,

[clang] Add concepts for raw buffers (PR #119643)

2024-12-11 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-hlsl

Author: Joshua Batista (bob80905)


Changes

This PR adds concept validation to raw buffers like Structured buffers, in the 
same way that it was done for typed buffers in 
https://github.com/llvm/llvm-project/pull/116413. 
This PR should also be responsible for testing 0 size elements for raw buffers.
Fixes https://github.com/llvm/llvm-project/issues/117406

---
Full diff: https://github.com/llvm/llvm-project/pull/119643.diff


3 Files Affected:

- (modified) clang/lib/Sema/HLSLExternalSemaSource.cpp (+50-13) 
- (modified) clang/test/AST/HLSL/StructuredBuffers-AST.hlsl (+16) 
- (added) clang/test/AST/HLSL/is_raw_resource_element_compatible_concept.hlsl 
(+10) 


``diff
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();
   onComplet

[clang] Add concepts for raw buffers (PR #119643)

2024-12-11 Thread Joshua Batista via cfe-commits

https://github.com/bob80905 created 
https://github.com/llvm/llvm-project/pull/119643

This PR adds concept validation to raw buffers like Structured buffers, in the 
same way that it was done for typed buffers in 
https://github.com/llvm/llvm-project/pull/116413. 
This PR should also be responsible for testing 0 size elements for raw buffers.
Fixes https://github.com/llvm/llvm-project/issues/117406

>From cbcdcd37ec82e7bbb5fdd1cf83b093778d4fcf9f Mon Sep 17 00:00:00 2001
From: Joshua Batista 
Date: Wed, 11 Dec 2024 17:02:02 -0800
Subject: [PATCH] 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")
-