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 <jbati...@microsoft.com>
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,7 +984,7 @@ void 
HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
   });
 
   Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RWStructuredBuffer")
-             .addSimpleTemplateParams({"element_type"})
+             .addSimpleTemplateParams({"element_type"}, RawBufferConcept)
              .finalizeForwardDeclaration();
   onCompletion(Decl, [this](CXXRecordDecl *Decl) {
     setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, 
ResourceKind::RawBuffer,
@@ -960,7 +997,7 @@ void 
HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
 
   Decl =
       BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "AppendStructuredBuffer")
-          .addSimpleTemplateParams({"element_type"})
+          .addSimpleTemplateParams({"element_type"}, RawBufferConcept)
           .finalizeForwardDeclaration();
   onCompletion(Decl, [this](CXXRecordDecl *Decl) {
     setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, 
ResourceKind::RawBuffer,
@@ -971,7 +1008,7 @@ void 
HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
 
   Decl =
       BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, 
"ConsumeStructuredBuffer")
-          .addSimpleTemplateParams({"element_type"})
+          .addSimpleTemplateParams({"element_type"}, RawBufferConcept)
           .finalizeForwardDeclaration();
   onCompletion(Decl, [this](CXXRecordDecl *Decl) {
     setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, 
ResourceKind::RawBuffer,
@@ -982,7 +1019,7 @@ void 
HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
 
   Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace,
                                 "RasterizerOrderedStructuredBuffer")
-             .addSimpleTemplateParams({"element_type"})
+             .addSimpleTemplateParams({"element_type"}, RawBufferConcept)
              .finalizeForwardDeclaration();
   onCompletion(Decl, [this](CXXRecordDecl *Decl) {
     setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, 
ResourceKind::RawBuffer,
diff --git a/clang/test/AST/HLSL/StructuredBuffers-AST.hlsl 
b/clang/test/AST/HLSL/StructuredBuffers-AST.hlsl
index 6cb4379ef5f556..3e71333da08fd8 100644
--- a/clang/test/AST/HLSL/StructuredBuffers-AST.hlsl
+++ b/clang/test/AST/HLSL/StructuredBuffers-AST.hlsl
@@ -50,6 +50,14 @@
 
 // EMPTY: ClassTemplateDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> 
implicit [[RESOURCE]]
 // EMPTY-NEXT: TemplateTypeParmDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> 
<invalid sloc> typename depth 0 index 0 element_type
+// EMPTY-NEXT: ConceptSpecializationExpr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> 
'bool' Concept 0x{{[0-9A-Fa-f]+}} '__is_raw_resource_element_compatible'
+// EMPTY-NEXT: ImplicitConceptSpecializationDecl 0x{{[0-9A-Fa-f]+}} <<invalid 
sloc>> <invalid sloc>
+// EMPTY-NEXT: TemplateArgument type 'type-parameter-0-0'
+// EMPTY-NEXT: TemplateTypeParmType 0x{{[0-9A-Fa-f]+}} 'type-parameter-0-0' 
dependent depth 0 index 0
+// EMPTY-NEXT: TemplateTypeParm 0x{{[0-9A-Fa-f]+}} ''
+// EMPTY-NEXT: TemplateArgument type 'element_type':'type-parameter-0-0'
+// EMPTY-NEXT: TemplateTypeParmType 0x{{[0-9A-Fa-f]+}} 'element_type' 
dependent depth 0 index 0
+// EMPTY-NEXT: TemplateTypeParm 0x{{[0-9A-Fa-f]+}} 'element_type'
 // EMPTY-NEXT: CXXRecordDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid 
sloc> implicit <undeserialized declarations> class [[RESOURCE]]
 // EMPTY-NEXT: FinalAttr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> Implicit final
 
@@ -64,6 +72,14 @@ RESOURCE<float> Buffer;
 
 // CHECK: ClassTemplateDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> 
implicit [[RESOURCE]]
 // CHECK-NEXT: TemplateTypeParmDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> 
<invalid sloc> typename depth 0 index 0 element_type
+// CHECK-NEXT: ConceptSpecializationExpr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> 
'bool' Concept 0x{{[0-9A-Fa-f]+}} '__is_raw_resource_element_compatible'
+// CHECK-NEXT: ImplicitConceptSpecializationDecl 0x{{[0-9A-Fa-f]+}} <<invalid 
sloc>> <invalid sloc>
+// CHECK-NEXT: TemplateArgument type 'type-parameter-0-0'
+// CHECK-NEXT: TemplateTypeParmType 0x{{[0-9A-Fa-f]+}} 'type-parameter-0-0' 
dependent depth 0 index 0
+// CHECK-NEXT: TemplateTypeParm 0x{{[0-9A-Fa-f]+}} ''
+// CHECK-NEXT: TemplateArgument type 'element_type':'type-parameter-0-0'
+// CHECK-NEXT: TemplateTypeParmType 0x{{[0-9A-Fa-f]+}} 'element_type' 
dependent depth 0 index 0
+// CHECK-NEXT: TemplateTypeParm 0x{{[0-9A-Fa-f]+}} 'element_type'
 // CHECK-NEXT: CXXRecordDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid 
sloc> implicit class [[RESOURCE]] definition
 
 // CHECK: FinalAttr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> Implicit final
diff --git 
a/clang/test/AST/HLSL/is_raw_resource_element_compatible_concept.hlsl 
b/clang/test/AST/HLSL/is_raw_resource_element_compatible_concept.hlsl
new file mode 100644
index 00000000000000..9c0b151739090a
--- /dev/null
+++ b/clang/test/AST/HLSL/is_raw_resource_element_compatible_concept.hlsl
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl -ast-dump 
-ast-dump-filter=__is_raw_resource_element_compatible %s | FileCheck %s
+
+// CHECK: ConceptDecl 0x{{[0-9a-f]+}} <<invalid sloc>> <invalid sloc> 
__is_raw_resource_element_compatible
+// CHECK: |-TemplateTypeParmDecl 0x{{[0-9a-f]+}} <<invalid sloc>> <invalid 
sloc> referenced typename depth 0 index 0 element_type
+// 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'
+
+StructuredBuffer<float> Buffer;

>From 7d119520e88de39118736604303ac55aae8e1a68 Mon Sep 17 00:00:00 2001
From: Joshua Batista <jbati...@microsoft.com>
Date: Thu, 12 Dec 2024 14:48:47 -0800
Subject: [PATCH 2/7] add condition that sizeof >= 1

---
 clang/lib/AST/Type.cpp                        |  2 +-
 clang/lib/Sema/HLSLExternalSemaSource.cpp     | 21 ++++++++++++++++++-
 .../SemaHLSL/BuiltIns/StructuredBuffers.hlsl  | 16 ++++++++++++--
 3 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 976361d07b68bf..caa0ac858a1bea 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -5118,7 +5118,7 @@ bool Type::isHLSLIntangibleType() const {
 
   CXXRecordDecl *RD = RT->getAsCXXRecordDecl();
   assert(RD != nullptr &&
-         "all HLSL struct and classes should be CXXRecordDecl");
+         "all HLSL structs and classes should be CXXRecordDecl");
   assert(RD->isCompleteDefinition() && "expecting complete type");
   return RD->isHLSLIntangible();
 }
diff --git a/clang/lib/Sema/HLSLExternalSemaSource.cpp 
b/clang/lib/Sema/HLSLExternalSemaSource.cpp
index e109e8b9abbfa7..ec7e246b89a222 100644
--- a/clang/lib/Sema/HLSLExternalSemaSource.cpp
+++ b/clang/lib/Sema/HLSLExternalSemaSource.cpp
@@ -891,7 +891,26 @@ static Expr *constructRawBufferConstraintExpr(Sema &S, 
SourceLocation NameLoc,
       Context, IsIntangibleExpr, UO_Not, BoolTy, VK_LValue, OK_Ordinary,
       NameLoc, false, FPOptionsOverride());
 
-  return NotIntangibleExpr;
+  // 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,
diff --git a/clang/test/SemaHLSL/BuiltIns/StructuredBuffers.hlsl 
b/clang/test/SemaHLSL/BuiltIns/StructuredBuffers.hlsl
index edfc1b48037c9f..5158d20887cc41 100644
--- a/clang/test/SemaHLSL/BuiltIns/StructuredBuffers.hlsl
+++ b/clang/test/SemaHLSL/BuiltIns/StructuredBuffers.hlsl
@@ -5,13 +5,25 @@ typedef vector<float, 3> float3;
 StructuredBuffer<float3> Buffer;
 
 // expected-error@+2 {{class template 'StructuredBuffer' requires template 
arguments}}
-// expected-note@*:* {{template declaration from hidden source: template 
<typename element_type> class StructuredBuffer {}}}
+// expected-note@*:* {{template declaration from hidden source: template 
<typename element_type> requires 
__is_raw_resource_element_compatible<element_type> class StructuredBuffer {}}}
 StructuredBuffer BufferErr1;
 
 // expected-error@+2 {{too few template arguments for class template 
'StructuredBuffer'}}
-// expected-note@*:* {{template declaration from hidden source: template 
<typename element_type> class StructuredBuffer {}}}
+// expected-note@*:* {{template declaration from hidden source: template 
<typename element_type> requires 
__is_raw_resource_element_compatible<element_type> class StructuredBuffer {}}}
 StructuredBuffer<> BufferErr2;
 
+// test elements of 0 size
+// expected-error@+3{{constraints not satisfied for class template 
'StructuredBuffer' [with element_type = int[0]]}}
+// expected-note@*:*{{because 'int[0]' does not satisfy 
'__is_raw_resource_element_compatible'}}
+// expected-note@*:*{{because 'sizeof(int[0]) >= 1UL' (0 >= 1) evaluated to 
false}}
+StructuredBuffer<int[0]> BufferErr3;
+
+// In C++, empty structs do have a size of 1. So should HLSL.
+// The concept will accept empty structs as element types, despite it being 
unintuitive.
+struct Empty {};
+StructuredBuffer<Empty> BufferErr4;
+
+
 [numthreads(1,1,1)]
 void main() {
   (void)Buffer.__handle; // expected-error {{'__handle' is a private member of 
'hlsl::StructuredBuffer<vector<float, 3>>'}}

>From 9cf81d80f37140ad2a84def85f5433c12343d406 Mon Sep 17 00:00:00 2001
From: Joshua Batista <jbati...@microsoft.com>
Date: Thu, 12 Dec 2024 15:54:12 -0800
Subject: [PATCH 3/7] update concept AST

---
 .../is_raw_resource_element_compatible_concept.hlsl | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git 
a/clang/test/AST/HLSL/is_raw_resource_element_compatible_concept.hlsl 
b/clang/test/AST/HLSL/is_raw_resource_element_compatible_concept.hlsl
index 9c0b151739090a..c8db64a3c50d03 100644
--- a/clang/test/AST/HLSL/is_raw_resource_element_compatible_concept.hlsl
+++ b/clang/test/AST/HLSL/is_raw_resource_element_compatible_concept.hlsl
@@ -2,9 +2,14 @@
 
 // CHECK: ConceptDecl 0x{{[0-9a-f]+}} <<invalid sloc>> <invalid sloc> 
__is_raw_resource_element_compatible
 // CHECK: |-TemplateTypeParmDecl 0x{{[0-9a-f]+}} <<invalid sloc>> <invalid 
sloc> referenced typename depth 0 index 0 element_type
-// 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:   |-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;

>From 140b7e6c60b61b0d1090386e295ba9e11ba0d6ba Mon Sep 17 00:00:00 2001
From: Joshua Batista <jbati...@microsoft.com>
Date: Thu, 12 Dec 2024 17:13:07 -0800
Subject: [PATCH 4/7] rephrase rawbuffer to structured buffer

---
 clang/lib/Sema/HLSLExternalSemaSource.cpp     | 23 ++++++++++---------
 .../test/AST/HLSL/StructuredBuffers-AST.hlsl  |  4 ++--
 ..._resource_element_compatible_concept.hlsl} |  4 ++--
 .../SemaHLSL/BuiltIns/StructuredBuffers.hlsl  |  6 ++---
 4 files changed, 19 insertions(+), 18 deletions(-)
 rename clang/test/AST/HLSL/{is_raw_resource_element_compatible_concept.hlsl => 
is_structured_resource_element_compatible_concept.hlsl} (86%)

diff --git a/clang/lib/Sema/HLSLExternalSemaSource.cpp 
b/clang/lib/Sema/HLSLExternalSemaSource.cpp
index ec7e246b89a222..61690dbfc58303 100644
--- a/clang/lib/Sema/HLSLExternalSemaSource.cpp
+++ b/clang/lib/Sema/HLSLExternalSemaSource.cpp
@@ -868,8 +868,9 @@ static Expr *constructTypedBufferConstraintExpr(Sema &S, 
SourceLocation NameLoc,
   return TypedResExpr;
 }
 
-static Expr *constructRawBufferConstraintExpr(Sema &S, SourceLocation NameLoc,
-                                              TemplateTypeParmDecl *T) {
+static Expr *constructStructuredBufferConstraintExpr(Sema &S,
+                                                     SourceLocation NameLoc,
+                                                     TemplateTypeParmDecl *T) {
   ASTContext &Context = S.getASTContext();
 
   // Obtain the QualType for 'bool'
@@ -944,8 +945,8 @@ static ConceptDecl *constructTypedBufferConceptDecl(Sema 
&S, NamespaceDecl *NSD,
     ConstraintExpr = constructTypedBufferConstraintExpr(S, DeclLoc, T);
   } else {
     DeclName = DeclarationName(
-        &Context.Idents.get("__is_raw_resource_element_compatible"));
-    ConstraintExpr = constructRawBufferConstraintExpr(S, DeclLoc, T);
+        &Context.Idents.get("__is_structured_resource_element_compatible"));
+    ConstraintExpr = constructStructuredBufferConstraintExpr(S, DeclLoc, T);
   }
 
   // Create a ConceptDecl
@@ -966,7 +967,7 @@ void 
HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
   CXXRecordDecl *Decl;
   ConceptDecl *TypedBufferConcept = constructTypedBufferConceptDecl(
       *SemaPtr, HLSLNamespace, /*isTypedBuffer*/ true);
-  ConceptDecl *RawBufferConcept = constructTypedBufferConceptDecl(
+  ConceptDecl *StructuredBufferConcept = constructTypedBufferConceptDecl(
       *SemaPtr, HLSLNamespace, /*isTypedBuffer*/ false);
   Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RWBuffer")
              .addSimpleTemplateParams({"element_type"}, TypedBufferConcept)
@@ -982,7 +983,7 @@ void 
HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
 
   Decl =
       BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, 
"RasterizerOrderedBuffer")
-          .addSimpleTemplateParams({"element_type"}, RawBufferConcept)
+          .addSimpleTemplateParams({"element_type"}, StructuredBufferConcept)
           .finalizeForwardDeclaration();
   onCompletion(Decl, [this](CXXRecordDecl *Decl) {
     setupBufferType(Decl, *SemaPtr, ResourceClass::UAV,
@@ -993,7 +994,7 @@ void 
HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
   });
 
   Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "StructuredBuffer")
-             .addSimpleTemplateParams({"element_type"}, RawBufferConcept)
+             .addSimpleTemplateParams({"element_type"}, 
StructuredBufferConcept)
              .finalizeForwardDeclaration();
   onCompletion(Decl, [this](CXXRecordDecl *Decl) {
     setupBufferType(Decl, *SemaPtr, ResourceClass::SRV, 
ResourceKind::RawBuffer,
@@ -1003,7 +1004,7 @@ void 
HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
   });
 
   Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RWStructuredBuffer")
-             .addSimpleTemplateParams({"element_type"}, RawBufferConcept)
+             .addSimpleTemplateParams({"element_type"}, 
StructuredBufferConcept)
              .finalizeForwardDeclaration();
   onCompletion(Decl, [this](CXXRecordDecl *Decl) {
     setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, 
ResourceKind::RawBuffer,
@@ -1016,7 +1017,7 @@ void 
HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
 
   Decl =
       BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "AppendStructuredBuffer")
-          .addSimpleTemplateParams({"element_type"}, RawBufferConcept)
+          .addSimpleTemplateParams({"element_type"}, StructuredBufferConcept)
           .finalizeForwardDeclaration();
   onCompletion(Decl, [this](CXXRecordDecl *Decl) {
     setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, 
ResourceKind::RawBuffer,
@@ -1027,7 +1028,7 @@ void 
HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
 
   Decl =
       BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, 
"ConsumeStructuredBuffer")
-          .addSimpleTemplateParams({"element_type"}, RawBufferConcept)
+          .addSimpleTemplateParams({"element_type"}, StructuredBufferConcept)
           .finalizeForwardDeclaration();
   onCompletion(Decl, [this](CXXRecordDecl *Decl) {
     setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, 
ResourceKind::RawBuffer,
@@ -1038,7 +1039,7 @@ void 
HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
 
   Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace,
                                 "RasterizerOrderedStructuredBuffer")
-             .addSimpleTemplateParams({"element_type"}, RawBufferConcept)
+             .addSimpleTemplateParams({"element_type"}, 
StructuredBufferConcept)
              .finalizeForwardDeclaration();
   onCompletion(Decl, [this](CXXRecordDecl *Decl) {
     setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, 
ResourceKind::RawBuffer,
diff --git a/clang/test/AST/HLSL/StructuredBuffers-AST.hlsl 
b/clang/test/AST/HLSL/StructuredBuffers-AST.hlsl
index 3e71333da08fd8..fd4aa58f5891b0 100644
--- a/clang/test/AST/HLSL/StructuredBuffers-AST.hlsl
+++ b/clang/test/AST/HLSL/StructuredBuffers-AST.hlsl
@@ -50,7 +50,7 @@
 
 // EMPTY: ClassTemplateDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> 
implicit [[RESOURCE]]
 // EMPTY-NEXT: TemplateTypeParmDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> 
<invalid sloc> typename depth 0 index 0 element_type
-// EMPTY-NEXT: ConceptSpecializationExpr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> 
'bool' Concept 0x{{[0-9A-Fa-f]+}} '__is_raw_resource_element_compatible'
+// EMPTY-NEXT: ConceptSpecializationExpr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> 
'bool' Concept 0x{{[0-9A-Fa-f]+}} '__is_structured_resource_element_compatible'
 // EMPTY-NEXT: ImplicitConceptSpecializationDecl 0x{{[0-9A-Fa-f]+}} <<invalid 
sloc>> <invalid sloc>
 // EMPTY-NEXT: TemplateArgument type 'type-parameter-0-0'
 // EMPTY-NEXT: TemplateTypeParmType 0x{{[0-9A-Fa-f]+}} 'type-parameter-0-0' 
dependent depth 0 index 0
@@ -72,7 +72,7 @@ RESOURCE<float> Buffer;
 
 // CHECK: ClassTemplateDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> 
implicit [[RESOURCE]]
 // CHECK-NEXT: TemplateTypeParmDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> 
<invalid sloc> typename depth 0 index 0 element_type
-// CHECK-NEXT: ConceptSpecializationExpr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> 
'bool' Concept 0x{{[0-9A-Fa-f]+}} '__is_raw_resource_element_compatible'
+// CHECK-NEXT: ConceptSpecializationExpr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> 
'bool' Concept 0x{{[0-9A-Fa-f]+}} '__is_structured_resource_element_compatible'
 // CHECK-NEXT: ImplicitConceptSpecializationDecl 0x{{[0-9A-Fa-f]+}} <<invalid 
sloc>> <invalid sloc>
 // CHECK-NEXT: TemplateArgument type 'type-parameter-0-0'
 // CHECK-NEXT: TemplateTypeParmType 0x{{[0-9A-Fa-f]+}} 'type-parameter-0-0' 
dependent depth 0 index 0
diff --git 
a/clang/test/AST/HLSL/is_raw_resource_element_compatible_concept.hlsl 
b/clang/test/AST/HLSL/is_structured_resource_element_compatible_concept.hlsl
similarity index 86%
rename from clang/test/AST/HLSL/is_raw_resource_element_compatible_concept.hlsl
rename to 
clang/test/AST/HLSL/is_structured_resource_element_compatible_concept.hlsl
index c8db64a3c50d03..7aa421737fe891 100644
--- a/clang/test/AST/HLSL/is_raw_resource_element_compatible_concept.hlsl
+++ b/clang/test/AST/HLSL/is_structured_resource_element_compatible_concept.hlsl
@@ -1,6 +1,6 @@
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl -ast-dump 
-ast-dump-filter=__is_raw_resource_element_compatible %s | FileCheck %s
+// 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_raw_resource_element_compatible
+// 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
diff --git a/clang/test/SemaHLSL/BuiltIns/StructuredBuffers.hlsl 
b/clang/test/SemaHLSL/BuiltIns/StructuredBuffers.hlsl
index 5158d20887cc41..fb14429025d5a5 100644
--- a/clang/test/SemaHLSL/BuiltIns/StructuredBuffers.hlsl
+++ b/clang/test/SemaHLSL/BuiltIns/StructuredBuffers.hlsl
@@ -5,16 +5,16 @@ typedef vector<float, 3> float3;
 StructuredBuffer<float3> Buffer;
 
 // expected-error@+2 {{class template 'StructuredBuffer' requires template 
arguments}}
-// expected-note@*:* {{template declaration from hidden source: template 
<typename element_type> requires 
__is_raw_resource_element_compatible<element_type> class StructuredBuffer {}}}
+// expected-note@*:* {{template declaration from hidden source: template 
<typename element_type> requires 
__is_structured_resource_element_compatible<element_type> class 
StructuredBuffer {}}}
 StructuredBuffer BufferErr1;
 
 // expected-error@+2 {{too few template arguments for class template 
'StructuredBuffer'}}
-// expected-note@*:* {{template declaration from hidden source: template 
<typename element_type> requires 
__is_raw_resource_element_compatible<element_type> class StructuredBuffer {}}}
+// expected-note@*:* {{template declaration from hidden source: template 
<typename element_type> requires 
__is_structured_resource_element_compatible<element_type> class 
StructuredBuffer {}}}
 StructuredBuffer<> BufferErr2;
 
 // test elements of 0 size
 // expected-error@+3{{constraints not satisfied for class template 
'StructuredBuffer' [with element_type = int[0]]}}
-// expected-note@*:*{{because 'int[0]' does not satisfy 
'__is_raw_resource_element_compatible'}}
+// expected-note@*:*{{because 'int[0]' does not satisfy 
'__is_structured_resource_element_compatible'}}
 // expected-note@*:*{{because 'sizeof(int[0]) >= 1UL' (0 >= 1) evaluated to 
false}}
 StructuredBuffer<int[0]> BufferErr3;
 

>From 6fb774aeb0efce23352961feb7dd1fb9e5036f37 Mon Sep 17 00:00:00 2001
From: Joshua Batista <jbati...@microsoft.com>
Date: Fri, 13 Dec 2024 15:41:08 -0800
Subject: [PATCH 5/7] rename function

---
 clang/lib/Sema/HLSLExternalSemaSource.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Sema/HLSLExternalSemaSource.cpp 
b/clang/lib/Sema/HLSLExternalSemaSource.cpp
index 61690dbfc58303..7febe192bb46e6 100644
--- a/clang/lib/Sema/HLSLExternalSemaSource.cpp
+++ b/clang/lib/Sema/HLSLExternalSemaSource.cpp
@@ -914,8 +914,8 @@ static Expr *constructStructuredBufferConstraintExpr(Sema 
&S,
   return CombinedExpr;
 }
 
-static ConceptDecl *constructTypedBufferConceptDecl(Sema &S, NamespaceDecl 
*NSD,
-                                                    bool isTypedBuffer) {
+static ConceptDecl *constructBufferConceptDecl(Sema &S, NamespaceDecl *NSD,
+                                               bool isTypedBuffer) {
   ASTContext &Context = S.getASTContext();
   DeclContext *DC = NSD->getDeclContext();
   SourceLocation DeclLoc = SourceLocation();
@@ -965,9 +965,9 @@ static ConceptDecl *constructTypedBufferConceptDecl(Sema 
&S, NamespaceDecl *NSD,
 
 void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
   CXXRecordDecl *Decl;
-  ConceptDecl *TypedBufferConcept = constructTypedBufferConceptDecl(
+  ConceptDecl *TypedBufferConcept = constructBufferConceptDecl(
       *SemaPtr, HLSLNamespace, /*isTypedBuffer*/ true);
-  ConceptDecl *StructuredBufferConcept = constructTypedBufferConceptDecl(
+  ConceptDecl *StructuredBufferConcept = constructBufferConceptDecl(
       *SemaPtr, HLSLNamespace, /*isTypedBuffer*/ false);
   Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RWBuffer")
              .addSimpleTemplateParams({"element_type"}, TypedBufferConcept)

>From 48ea30cfc0e614ed158a480fa6af7c5cd070fd01 Mon Sep 17 00:00:00 2001
From: Joshua Batista <jbati...@microsoft.com>
Date: Thu, 19 Dec 2024 10:42:28 -0800
Subject: [PATCH 6/7] add helpful comments

---
 clang/lib/Sema/HLSLExternalSemaSource.cpp | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/clang/lib/Sema/HLSLExternalSemaSource.cpp 
b/clang/lib/Sema/HLSLExternalSemaSource.cpp
index 7febe192bb46e6..5d40529f934ac5 100644
--- a/clang/lib/Sema/HLSLExternalSemaSource.cpp
+++ b/clang/lib/Sema/HLSLExternalSemaSource.cpp
@@ -847,6 +847,10 @@ static BuiltinTypeDeclBuilder 
setupBufferType(CXXRecordDecl *Decl, Sema &S,
       .addDefaultHandleConstructor();
 }
 
+// This function is responsible for constructing the constraint expression for
+// this concept:
+// template<typename T> concept is_typed_resource_element_compatible =
+// __is_typed_resource_element_compatible<T>;
 static Expr *constructTypedBufferConstraintExpr(Sema &S, SourceLocation 
NameLoc,
                                                 TemplateTypeParmDecl *T) {
   ASTContext &Context = S.getASTContext();
@@ -868,6 +872,10 @@ static Expr *constructTypedBufferConstraintExpr(Sema &S, 
SourceLocation NameLoc,
   return TypedResExpr;
 }
 
+// This function is responsible for constructing the constraint expression for
+// this concept:
+// template<typename T> concept is_structured_resource_element_compatible =
+// !__is_intangible<T> && sizeof(T) >= 1;
 static Expr *constructStructuredBufferConstraintExpr(Sema &S,
                                                      SourceLocation NameLoc,
                                                      TemplateTypeParmDecl *T) {

>From b46c4c073a6aa3cb6b4c59dd8eb04999dc6010dc Mon Sep 17 00:00:00 2001
From: Joshua Batista <jbati...@microsoft.com>
Date: Thu, 19 Dec 2024 12:48:47 -0800
Subject: [PATCH 7/7] use logical not rather than bitwise

---
 clang/lib/Sema/HLSLExternalSemaSource.cpp                       | 2 +-
 .../HLSL/is_structured_resource_element_compatible_concept.hlsl | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Sema/HLSLExternalSemaSource.cpp 
b/clang/lib/Sema/HLSLExternalSemaSource.cpp
index 5d40529f934ac5..873c2e32e22d0f 100644
--- a/clang/lib/Sema/HLSLExternalSemaSource.cpp
+++ b/clang/lib/Sema/HLSLExternalSemaSource.cpp
@@ -897,7 +897,7 @@ static Expr *constructStructuredBufferConstraintExpr(Sema 
&S,
 
   // negate IsIntangibleExpr
   UnaryOperator *NotIntangibleExpr = UnaryOperator::Create(
-      Context, IsIntangibleExpr, UO_Not, BoolTy, VK_LValue, OK_Ordinary,
+      Context, IsIntangibleExpr, UO_LNot, BoolTy, VK_LValue, OK_Ordinary,
       NameLoc, false, FPOptionsOverride());
 
   // element types also may not be of 0 size
diff --git 
a/clang/test/AST/HLSL/is_structured_resource_element_compatible_concept.hlsl 
b/clang/test/AST/HLSL/is_structured_resource_element_compatible_concept.hlsl
index 7aa421737fe891..2ecd102d524c8a 100644
--- a/clang/test/AST/HLSL/is_structured_resource_element_compatible_concept.hlsl
+++ b/clang/test/AST/HLSL/is_structured_resource_element_compatible_concept.hlsl
@@ -3,7 +3,7 @@
 // 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:   |-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'

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to