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

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

https://github.com/hekota approved this pull request.

LGTM! Please add [HLSL] to the title.

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


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

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


@@ -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]+}} <>  
__is_structured_resource_element_compatible
+// CHECK: |-TemplateTypeParmDecl 0x{{[0-9a-f]+}} <>  referenced typename depth 0 index 0 element_type
+// CHECK: `-BinaryOperator 0x{{[0-9a-f]+}} <> 'bool' lvalue '&&'
+// CHECK:   |-UnaryOperator 0x{{[0-9a-f]+}} <> 'bool' lvalue 
prefix '~' cannot overflow

bob80905 wrote:

Nice catch!

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


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

2024-12-19 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/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,

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

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


@@ -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]+}} <>  
__is_structured_resource_element_compatible
+// CHECK: |-TemplateTypeParmDecl 0x{{[0-9a-f]+}} <>  referenced typename depth 0 index 0 element_type
+// CHECK: `-BinaryOperator 0x{{[0-9a-f]+}} <> 'bool' lvalue '&&'
+// CHECK:   |-UnaryOperator 0x{{[0-9a-f]+}} <> 'bool' lvalue 
prefix '~' cannot overflow

hekota wrote:

You'll need to use `UO_LNot` when creating the unary expr.

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


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

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


@@ -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]+}} <>  
__is_structured_resource_element_compatible
+// CHECK: |-TemplateTypeParmDecl 0x{{[0-9a-f]+}} <>  referenced typename depth 0 index 0 element_type
+// CHECK: `-BinaryOperator 0x{{[0-9a-f]+}} <> 'bool' lvalue '&&'
+// CHECK:   |-UnaryOperator 0x{{[0-9a-f]+}} <> 'bool' lvalue 
prefix '~' cannot overflow

hekota wrote:

`~` is bitwise NOT, it should be `!` logical NOT.

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


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

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

https://github.com/bogner approved this pull request.


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


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

2024-12-19 Thread Justin Bogner 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) {

bogner wrote:

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.

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


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

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

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


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

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

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