[clang] [HLSL] Add ByteAddressBuffer, RWByteAddressBuffer and RasterizerOrderedByteAddressBuffer definitions to HLSLExternalSemaSource #113477 (PR #116699)

via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 28 09:57:39 PST 2024


https://github.com/joaosaffran updated https://github.com/llvm/llvm-project/pull/116699

>From 84c1c4dc168b4d20c9f0f0131548080d32f3f02b Mon Sep 17 00:00:00 2001
From: Joao Saffran <jderezende at microsoft.com>
Date: Sat, 9 Nov 2024 01:34:16 +0000
Subject: [PATCH 01/18] adding definition

---
 clang/lib/Sema/HLSLExternalSemaSource.cpp     | 10 +++++
 .../test/AST/HLSL/ByteAddressBuffer-AST.hlsl  | 41 +++++++++++++++++++
 2 files changed, 51 insertions(+)
 create mode 100644 clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl

diff --git a/clang/lib/Sema/HLSLExternalSemaSource.cpp b/clang/lib/Sema/HLSLExternalSemaSource.cpp
index 951375cc84ba82..4c428087ec55d2 100644
--- a/clang/lib/Sema/HLSLExternalSemaSource.cpp
+++ b/clang/lib/Sema/HLSLExternalSemaSource.cpp
@@ -939,6 +939,16 @@ void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
         .addDecrementCounterMethod()
         .completeDefinition();
   });
+
+  Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "ByteAddressBuffer")
+             .Record;
+  onCompletion(Decl, [this](CXXRecordDecl *Decl) {
+    setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, ResourceKind::RawBuffer,
+                    /*IsROV=*/true,
+                    /*RawBuffer=*/true)
+        .addArraySubscriptOperators()
+        .completeDefinition();
+  });
 }
 
 void HLSLExternalSemaSource::onCompletion(CXXRecordDecl *Record,
diff --git a/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl b/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl
new file mode 100644
index 00000000000000..013974369d033a
--- /dev/null
+++ b/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl
@@ -0,0 +1,41 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl -ast-dump -DEMPTY %s | FileCheck -check-prefix=EMPTY %s
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl -ast-dump %s | FileCheck %s
+
+
+// EMPTY: CXXRecordDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> implicit <undeserialized declarations> class ByteAddressBuffer
+// EMPTY-NEXT: FinalAttr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> Implicit final
+
+// There should be no more occurrences of ByteAddressBuffer
+// EMPTY-NOT: {{[^[:alnum:]]}}ByteAddressBuffer
+
+#ifndef EMPTY
+
+ByteAddressBuffer Buffer;
+
+#endif
+
+// CHECK: CXXRecordDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> implicit referenced <undeserialized declarations> class ByteAddressBuffer definition
+
+
+// CHECK: FinalAttr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> Implicit final
+// CHECK-NEXT: FieldDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> implicit h '__hlsl_resource_t
+// CHECK-SAME{LITERAL}: [[hlsl::resource_class(UAV)]]
+// CHECK-SAME{LITERAL}: [[hlsl::raw_buffer]]
+// CHECK-SAME{LITERAL}: [[hlsl::contained_type(char8_t)]]
+// CHECK-NEXT: HLSLResourceAttr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> Implicit RawBuffer
+
+// CHECK: CXXMethodDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> operator[] 'char8_t &const (unsigned int) const'
+// CHECK-NEXT: ParmVarDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> Idx 'unsigned int'
+// CHECK-NEXT: CompoundStmt 0x{{[0-9A-Fa-f]+}} <<invalid sloc>>
+// CHECK-NEXT: ReturnStmt 0x{{[0-9A-Fa-f]+}} <<invalid sloc>>
+// CHECK-NEXT: MemberExpr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> 'char8_t' lvalue .e 0x{{[0-9A-Fa-f]+}}
+// CHECK-NEXT: CXXThisExpr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> 'const hlsl::ByteAddressBuffer' lvalue implicit this
+// CHECK-NEXT: AlwaysInlineAttr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> Implicit always_inline
+
+// CHECK-NEXT: CXXMethodDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> operator[] 'char8_t &(unsigned int)'
+// CHECK-NEXT: ParmVarDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> Idx 'unsigned int'
+// CHECK-NEXT: CompoundStmt 0x{{[0-9A-Fa-f]+}} <<invalid sloc>>
+// CHECK-NEXT: ReturnStmt 0x{{[0-9A-Fa-f]+}} <<invalid sloc>>
+// CHECK-NEXT: MemberExpr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> 'char8_t' lvalue .e 0x{{[0-9A-Fa-f]+}}
+// CHECK-NEXT: CXXThisExpr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> 'hlsl::ByteAddressBuffer' lvalue implicit this
+// CHECK-NEXT: AlwaysInlineAttr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> Implicit always_inline

>From a08103a27066729b4224a949c11c3fcc70e2ea3e Mon Sep 17 00:00:00 2001
From: Joao Saffran <jderezende at microsoft.com>
Date: Mon, 18 Nov 2024 21:11:47 +0000
Subject: [PATCH 02/18] adding tests

---
 clang/test/AST/HLSL/ByteAddressBuffer-AST.ll        | 13 +++++++++++++
 .../CodeGenHLSL/builtins/ByteAddressBuffer.hlsl     |  8 ++++++++
 2 files changed, 21 insertions(+)
 create mode 100644 clang/test/AST/HLSL/ByteAddressBuffer-AST.ll
 create mode 100644 clang/test/CodeGenHLSL/builtins/ByteAddressBuffer.hlsl

diff --git a/clang/test/AST/HLSL/ByteAddressBuffer-AST.ll b/clang/test/AST/HLSL/ByteAddressBuffer-AST.ll
new file mode 100644
index 00000000000000..7df06100d69fe5
--- /dev/null
+++ b/clang/test/AST/HLSL/ByteAddressBuffer-AST.ll
@@ -0,0 +1,13 @@
+; ModuleID = '/workspace/llvm-project/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl'
+source_filename = "/workspace/llvm-project/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl"
+target datalayout = "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-f32:32-f64:64-n8:16:32:64"
+target triple = "dxilv1.0-pc-shadermodel6.0-library"
+
+!llvm.module.flags = !{!0, !1}
+!dx.valver = !{!2}
+!llvm.ident = !{!3}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 4, !"dx.disable_optimizations", i32 1}
+!2 = !{i32 1, i32 8}
+!3 = !{!"clang version 20.0.0git (https://github.com/joaosaffran/llvm-project.git c3b3c87db9b6a47bea9ed69575d4a9a728d1b154)"}
diff --git a/clang/test/CodeGenHLSL/builtins/ByteAddressBuffer.hlsl b/clang/test/CodeGenHLSL/builtins/ByteAddressBuffer.hlsl
new file mode 100644
index 00000000000000..348f3d29d66aef
--- /dev/null
+++ b/clang/test/CodeGenHLSL/builtins/ByteAddressBuffer.hlsl
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl -emit-llvm %s -o - | FileCheck %s
+
+// CHECK: class.hlsl::ByteAddressBuffer" = type <{ target("dx.RawBuffer", i8, 1, 1)
+// CHECK: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @_GLOBAL__sub_I_ByteAddressBuffer.hlsl, ptr null }]
+
+ByteAddressBuffer Buffer;
+
+//CHECK: define internal void @_GLOBAL__sub_I_ByteAddressBuffer.hlsl()

>From c3cb594aff7824707614f60e14b7367c3908fcd5 Mon Sep 17 00:00:00 2001
From: Joao Saffran <jderezende at microsoft.com>
Date: Mon, 18 Nov 2024 22:11:28 +0000
Subject: [PATCH 03/18] removing useless file

---
 clang/test/AST/HLSL/ByteAddressBuffer-AST.ll  | 13 -------------
 .../ByteAddressBuffer-contructor.hlsl         | 19 +++++++++++++++++++
 .../builtins/ByteAddressBuffer.hlsl           |  8 --------
 3 files changed, 19 insertions(+), 21 deletions(-)
 delete mode 100644 clang/test/AST/HLSL/ByteAddressBuffer-AST.ll
 create mode 100644 clang/test/CodeGenHLSL/builtins/ByteAddressBuffer-contructor.hlsl
 delete mode 100644 clang/test/CodeGenHLSL/builtins/ByteAddressBuffer.hlsl

diff --git a/clang/test/AST/HLSL/ByteAddressBuffer-AST.ll b/clang/test/AST/HLSL/ByteAddressBuffer-AST.ll
deleted file mode 100644
index 7df06100d69fe5..00000000000000
--- a/clang/test/AST/HLSL/ByteAddressBuffer-AST.ll
+++ /dev/null
@@ -1,13 +0,0 @@
-; ModuleID = '/workspace/llvm-project/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl'
-source_filename = "/workspace/llvm-project/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl"
-target datalayout = "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-f32:32-f64:64-n8:16:32:64"
-target triple = "dxilv1.0-pc-shadermodel6.0-library"
-
-!llvm.module.flags = !{!0, !1}
-!dx.valver = !{!2}
-!llvm.ident = !{!3}
-
-!0 = !{i32 1, !"wchar_size", i32 4}
-!1 = !{i32 4, !"dx.disable_optimizations", i32 1}
-!2 = !{i32 1, i32 8}
-!3 = !{!"clang version 20.0.0git (https://github.com/joaosaffran/llvm-project.git c3b3c87db9b6a47bea9ed69575d4a9a728d1b154)"}
diff --git a/clang/test/CodeGenHLSL/builtins/ByteAddressBuffer-contructor.hlsl b/clang/test/CodeGenHLSL/builtins/ByteAddressBuffer-contructor.hlsl
new file mode 100644
index 00000000000000..230701a5a8139a
--- /dev/null
+++ b/clang/test/CodeGenHLSL/builtins/ByteAddressBuffer-contructor.hlsl
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s --check-prefixes=CHECK,CHECK-DXIL
+// RUN-DISABLED: %clang_cc1 -triple spirv-vulkan-library -x hlsl -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s --check-prefixes=CHECK,CHECK-SPIRV
+
+// NOTE: SPIRV codegen for resource types is not yet implemented
+
+ByteAddressBuffer Buffer: register(u0, space0);
+
+// CHECK: class.hlsl::ByteAddressBuffer" = type <{ target("dx.RawBuffer", i8, 1, 1), i8, [3 x i8] }>
+
+// CHECK: @Buffer = global %"class.hlsl::ByteAddressBuffer" zeroinitializer, align 4
+
+// CHECK: define internal void @_GLOBAL__sub_I_ByteAddressBuffer_contructor.hlsl()
+// CHECK: entry:
+// CHECK: call void @_init_resource_bindings()
+
+// CHECK: define internal void @_init_resource_bindings() {
+// CHECK-NEXT: entry:
+// CHECK-DXIL-NEXT: %Buffer_h = call target("dx.RawBuffer", i8, 1, 1) @llvm.dx.handle.fromBinding.tdx.RawBuffer_i8_1_1t(i32 0, i32 0, i32 1, i32 0, i1 false)
+// CHECK-DXIL-NEXT: store target("dx.RawBuffer", i8, 1, 1) %Buffer_h, ptr @Buffer, align 4
diff --git a/clang/test/CodeGenHLSL/builtins/ByteAddressBuffer.hlsl b/clang/test/CodeGenHLSL/builtins/ByteAddressBuffer.hlsl
deleted file mode 100644
index 348f3d29d66aef..00000000000000
--- a/clang/test/CodeGenHLSL/builtins/ByteAddressBuffer.hlsl
+++ /dev/null
@@ -1,8 +0,0 @@
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl -emit-llvm %s -o - | FileCheck %s
-
-// CHECK: class.hlsl::ByteAddressBuffer" = type <{ target("dx.RawBuffer", i8, 1, 1)
-// CHECK: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @_GLOBAL__sub_I_ByteAddressBuffer.hlsl, ptr null }]
-
-ByteAddressBuffer Buffer;
-
-//CHECK: define internal void @_GLOBAL__sub_I_ByteAddressBuffer.hlsl()

>From 8e2c6028c969db98a8e441a8017a37d2ad02feee Mon Sep 17 00:00:00 2001
From: Joao Saffran <jderezende at microsoft.com>
Date: Tue, 19 Nov 2024 17:02:59 +0000
Subject: [PATCH 04/18] fixing tests

---
 clang/lib/AST/DeclBase.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp
index fb701f76231bcd..e38a0b9dbc1c6e 100644
--- a/clang/lib/AST/DeclBase.cpp
+++ b/clang/lib/AST/DeclBase.cpp
@@ -1464,6 +1464,8 @@ DeclContext *DeclContext::getPrimaryContext() {
         return Def;
     return this;
 
+  case Decl::CXXRecord:
+    return this;
   case Decl::ObjCCategory:
     return this;
 

>From 88a7c0a7df217539678a4630691d500d884ad58b Mon Sep 17 00:00:00 2001
From: Joao Saffran <jderezende at microsoft.com>
Date: Tue, 19 Nov 2024 17:32:40 +0000
Subject: [PATCH 05/18] Revert "fixing tests"

This reverts commit c4a448e91124d3cbf8e53e5b7bf1f3d2c432ebc6.
---
 clang/lib/AST/DeclBase.cpp | 2 --
 1 file changed, 2 deletions(-)

diff --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp
index e38a0b9dbc1c6e..fb701f76231bcd 100644
--- a/clang/lib/AST/DeclBase.cpp
+++ b/clang/lib/AST/DeclBase.cpp
@@ -1464,8 +1464,6 @@ DeclContext *DeclContext::getPrimaryContext() {
         return Def;
     return this;
 
-  case Decl::CXXRecord:
-    return this;
   case Decl::ObjCCategory:
     return this;
 

>From e1f62b56d53699971b4d5993375fae7c09aa0ab6 Mon Sep 17 00:00:00 2001
From: Joao Saffran <jderezende at microsoft.com>
Date: Tue, 19 Nov 2024 18:35:50 +0000
Subject: [PATCH 06/18] adding new CXXRecord decl primary context

---
 clang/lib/AST/DeclBase.cpp | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp
index fb701f76231bcd..928da907b37aba 100644
--- a/clang/lib/AST/DeclBase.cpp
+++ b/clang/lib/AST/DeclBase.cpp
@@ -1471,6 +1471,11 @@ DeclContext *DeclContext::getPrimaryContext() {
   case Decl::ObjCCategoryImpl:
     return this;
 
+  case Decl::CXXRecord:
+    if (auto *OPD = dyn_cast<CXXRecordDecl>(this))
+      if (auto *Def = OPD->getDefinition())
+        return Def;
+    return this;
   default:
     if (getDeclKind() >= Decl::firstTag && getDeclKind() <= Decl::lastTag) {
       // If this is a tag type that has a definition or is currently

>From d846f341c9e17bc5e33a60738a28e82db48ef84c Mon Sep 17 00:00:00 2001
From: Joao Saffran <jderezende at microsoft.com>
Date: Thu, 21 Nov 2024 19:58:11 +0000
Subject: [PATCH 07/18] addressing pr comments

---
 clang/lib/Sema/HLSLExternalSemaSource.cpp      |  7 +++----
 clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl | 18 +-----------------
 ...sl => ByteAddressBuffers-constructors.hlsl} |  2 +-
 3 files changed, 5 insertions(+), 22 deletions(-)
 rename clang/test/CodeGenHLSL/builtins/{ByteAddressBuffer-contructor.hlsl => ByteAddressBuffers-constructors.hlsl} (95%)

diff --git a/clang/lib/Sema/HLSLExternalSemaSource.cpp b/clang/lib/Sema/HLSLExternalSemaSource.cpp
index 4c428087ec55d2..3d3c016573f713 100644
--- a/clang/lib/Sema/HLSLExternalSemaSource.cpp
+++ b/clang/lib/Sema/HLSLExternalSemaSource.cpp
@@ -943,11 +943,10 @@ void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
   Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "ByteAddressBuffer")
              .Record;
   onCompletion(Decl, [this](CXXRecordDecl *Decl) {
-    setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, ResourceKind::RawBuffer,
-                    /*IsROV=*/true,
+    setupBufferType(Decl, *SemaPtr, ResourceClass::SRV, ResourceKind::RawBuffer,
+                    /*IsROV=*/false,
                     /*RawBuffer=*/true)
-        .addArraySubscriptOperators()
-        .completeDefinition();
+    .completeDefinition();
   });
 }
 
diff --git a/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl b/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl
index 013974369d033a..97a822d2a7b10b 100644
--- a/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl
+++ b/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl
@@ -19,23 +19,7 @@ ByteAddressBuffer Buffer;
 
 // CHECK: FinalAttr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> Implicit final
 // CHECK-NEXT: FieldDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> implicit h '__hlsl_resource_t
-// CHECK-SAME{LITERAL}: [[hlsl::resource_class(UAV)]]
+// CHECK-SAME{LITERAL}: [[hlsl::resource_class(SRV)]]
 // CHECK-SAME{LITERAL}: [[hlsl::raw_buffer]]
 // CHECK-SAME{LITERAL}: [[hlsl::contained_type(char8_t)]]
 // CHECK-NEXT: HLSLResourceAttr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> Implicit RawBuffer
-
-// CHECK: CXXMethodDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> operator[] 'char8_t &const (unsigned int) const'
-// CHECK-NEXT: ParmVarDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> Idx 'unsigned int'
-// CHECK-NEXT: CompoundStmt 0x{{[0-9A-Fa-f]+}} <<invalid sloc>>
-// CHECK-NEXT: ReturnStmt 0x{{[0-9A-Fa-f]+}} <<invalid sloc>>
-// CHECK-NEXT: MemberExpr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> 'char8_t' lvalue .e 0x{{[0-9A-Fa-f]+}}
-// CHECK-NEXT: CXXThisExpr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> 'const hlsl::ByteAddressBuffer' lvalue implicit this
-// CHECK-NEXT: AlwaysInlineAttr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> Implicit always_inline
-
-// CHECK-NEXT: CXXMethodDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> operator[] 'char8_t &(unsigned int)'
-// CHECK-NEXT: ParmVarDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> Idx 'unsigned int'
-// CHECK-NEXT: CompoundStmt 0x{{[0-9A-Fa-f]+}} <<invalid sloc>>
-// CHECK-NEXT: ReturnStmt 0x{{[0-9A-Fa-f]+}} <<invalid sloc>>
-// CHECK-NEXT: MemberExpr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> 'char8_t' lvalue .e 0x{{[0-9A-Fa-f]+}}
-// CHECK-NEXT: CXXThisExpr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> 'hlsl::ByteAddressBuffer' lvalue implicit this
-// CHECK-NEXT: AlwaysInlineAttr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> Implicit always_inline
diff --git a/clang/test/CodeGenHLSL/builtins/ByteAddressBuffer-contructor.hlsl b/clang/test/CodeGenHLSL/builtins/ByteAddressBuffers-constructors.hlsl
similarity index 95%
rename from clang/test/CodeGenHLSL/builtins/ByteAddressBuffer-contructor.hlsl
rename to clang/test/CodeGenHLSL/builtins/ByteAddressBuffers-constructors.hlsl
index 230701a5a8139a..c4e3290643781f 100644
--- a/clang/test/CodeGenHLSL/builtins/ByteAddressBuffer-contructor.hlsl
+++ b/clang/test/CodeGenHLSL/builtins/ByteAddressBuffers-constructors.hlsl
@@ -3,7 +3,7 @@
 
 // NOTE: SPIRV codegen for resource types is not yet implemented
 
-ByteAddressBuffer Buffer: register(u0, space0);
+ByteAddressBuffer Buffer: register(t0);
 
 // CHECK: class.hlsl::ByteAddressBuffer" = type <{ target("dx.RawBuffer", i8, 1, 1), i8, [3 x i8] }>
 

>From c78977632436d6ba71871c02f4e6cf48d13dbf36 Mon Sep 17 00:00:00 2001
From: Joao Saffran <jderezende at microsoft.com>
Date: Thu, 21 Nov 2024 20:08:46 +0000
Subject: [PATCH 08/18] fixing format

---
 clang/lib/Sema/HLSLExternalSemaSource.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Sema/HLSLExternalSemaSource.cpp b/clang/lib/Sema/HLSLExternalSemaSource.cpp
index 3d3c016573f713..727daaa76424e8 100644
--- a/clang/lib/Sema/HLSLExternalSemaSource.cpp
+++ b/clang/lib/Sema/HLSLExternalSemaSource.cpp
@@ -946,7 +946,7 @@ void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
     setupBufferType(Decl, *SemaPtr, ResourceClass::SRV, ResourceKind::RawBuffer,
                     /*IsROV=*/false,
                     /*RawBuffer=*/true)
-    .completeDefinition();
+        .completeDefinition();
   });
 }
 

>From 7d8eef8980c9b331757967dfa7f6425b9639b16c Mon Sep 17 00:00:00 2001
From: Joao Saffran <jderezende at microsoft.com>
Date: Thu, 21 Nov 2024 22:02:49 +0000
Subject: [PATCH 09/18] starting to make the test more generic

---
 .../test/AST/HLSL/ByteAddressBuffer-AST.hlsl  | 23 +++++++++----------
 1 file changed, 11 insertions(+), 12 deletions(-)

diff --git a/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl b/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl
index 97a822d2a7b10b..9436da576e6084 100644
--- a/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl
+++ b/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl
@@ -1,12 +1,12 @@
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl -ast-dump -DEMPTY %s | FileCheck -check-prefix=EMPTY %s
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl -ast-dump %s | FileCheck %s
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl -ast-dump -DEMPTY %s | FileCheck %s -DRESOURCE=ByteAddressBuffer -check-prefix=EMPTY 
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl -ast-dump %s | FileCheck %s -DRESOURCE=ByteAddressBuffer -check-prefixes=CHECK-ATTR
 
 
-// EMPTY: CXXRecordDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> implicit <undeserialized declarations> class ByteAddressBuffer
+// EMPTY: 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
 
-// There should be no more occurrences of ByteAddressBuffer
-// EMPTY-NOT: {{[^[:alnum:]]}}ByteAddressBuffer
+// There should be no more occurrences of [[RESOURCE]]
+// EMPTY-NOT: {{[^[:alnum:]]}}[[RESOURCE]]
 
 #ifndef EMPTY
 
@@ -14,12 +14,11 @@ ByteAddressBuffer Buffer;
 
 #endif
 
-// CHECK: CXXRecordDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> implicit referenced <undeserialized declarations> class ByteAddressBuffer definition
+// CHECK: CXXRecordDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> implicit referenced <undeserialized declarations> class [[RESOURCE]] definition
 
 
-// CHECK: FinalAttr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> Implicit final
-// CHECK-NEXT: FieldDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> implicit h '__hlsl_resource_t
-// CHECK-SAME{LITERAL}: [[hlsl::resource_class(SRV)]]
-// CHECK-SAME{LITERAL}: [[hlsl::raw_buffer]]
-// CHECK-SAME{LITERAL}: [[hlsl::contained_type(char8_t)]]
-// CHECK-NEXT: HLSLResourceAttr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> Implicit RawBuffer
+// CHECK-ATTR: FieldDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> implicit h '__hlsl_resource_t
+// CHECK-ATTR-SAME{LITERAL}: [[hlsl::resource_class(SRV)]]
+// CHECK-ATTR-SAME{LITERAL}: [[hlsl::raw_buffer]]
+// CHECK-ATTR-SAME{LITERAL}: [[hlsl::contained_type(char8_t)]]
+// CHECK-ATTR-NEXT: HLSLResourceAttr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> Implicit RawBuffer

>From 75ba09459b3bf9c4c7bba4e71823dd6a62e9d087 Mon Sep 17 00:00:00 2001
From: Joao Saffran <jderezende at microsoft.com>
Date: Thu, 21 Nov 2024 23:07:59 +0000
Subject: [PATCH 10/18] fixing test

---
 .../test/AST/HLSL/ByteAddressBuffer-AST.hlsl  | 23 ++++++++++---------
 .../ByteAddressBuffers-constructors.hlsl      |  8 +++----
 2 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl b/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl
index 9436da576e6084..97a822d2a7b10b 100644
--- a/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl
+++ b/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl
@@ -1,12 +1,12 @@
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl -ast-dump -DEMPTY %s | FileCheck %s -DRESOURCE=ByteAddressBuffer -check-prefix=EMPTY 
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl -ast-dump %s | FileCheck %s -DRESOURCE=ByteAddressBuffer -check-prefixes=CHECK-ATTR
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl -ast-dump -DEMPTY %s | FileCheck -check-prefix=EMPTY %s
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl -ast-dump %s | FileCheck %s
 
 
-// EMPTY: CXXRecordDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> implicit <undeserialized declarations> class [[RESOURCE]]
+// EMPTY: CXXRecordDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> implicit <undeserialized declarations> class ByteAddressBuffer
 // EMPTY-NEXT: FinalAttr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> Implicit final
 
-// There should be no more occurrences of [[RESOURCE]]
-// EMPTY-NOT: {{[^[:alnum:]]}}[[RESOURCE]]
+// There should be no more occurrences of ByteAddressBuffer
+// EMPTY-NOT: {{[^[:alnum:]]}}ByteAddressBuffer
 
 #ifndef EMPTY
 
@@ -14,11 +14,12 @@ ByteAddressBuffer Buffer;
 
 #endif
 
-// CHECK: CXXRecordDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> implicit referenced <undeserialized declarations> class [[RESOURCE]] definition
+// CHECK: CXXRecordDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> implicit referenced <undeserialized declarations> class ByteAddressBuffer definition
 
 
-// CHECK-ATTR: FieldDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> implicit h '__hlsl_resource_t
-// CHECK-ATTR-SAME{LITERAL}: [[hlsl::resource_class(SRV)]]
-// CHECK-ATTR-SAME{LITERAL}: [[hlsl::raw_buffer]]
-// CHECK-ATTR-SAME{LITERAL}: [[hlsl::contained_type(char8_t)]]
-// CHECK-ATTR-NEXT: HLSLResourceAttr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> Implicit RawBuffer
+// CHECK: FinalAttr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> Implicit final
+// CHECK-NEXT: FieldDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> implicit h '__hlsl_resource_t
+// CHECK-SAME{LITERAL}: [[hlsl::resource_class(SRV)]]
+// CHECK-SAME{LITERAL}: [[hlsl::raw_buffer]]
+// CHECK-SAME{LITERAL}: [[hlsl::contained_type(char8_t)]]
+// CHECK-NEXT: HLSLResourceAttr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> Implicit RawBuffer
diff --git a/clang/test/CodeGenHLSL/builtins/ByteAddressBuffers-constructors.hlsl b/clang/test/CodeGenHLSL/builtins/ByteAddressBuffers-constructors.hlsl
index c4e3290643781f..546040b662cf87 100644
--- a/clang/test/CodeGenHLSL/builtins/ByteAddressBuffers-constructors.hlsl
+++ b/clang/test/CodeGenHLSL/builtins/ByteAddressBuffers-constructors.hlsl
@@ -5,15 +5,15 @@
 
 ByteAddressBuffer Buffer: register(t0);
 
-// CHECK: class.hlsl::ByteAddressBuffer" = type <{ target("dx.RawBuffer", i8, 1, 1), i8, [3 x i8] }>
+// CHECK: "class.hlsl::ByteAddressBuffer" = type { target("dx.RawBuffer", i8, 0, 0) }
 
 // CHECK: @Buffer = global %"class.hlsl::ByteAddressBuffer" zeroinitializer, align 4
 
-// CHECK: define internal void @_GLOBAL__sub_I_ByteAddressBuffer_contructor.hlsl()
+// CHECK: define internal void @_GLOBAL__sub_I_ByteAddressBuffers_constructors.hlsl()
 // CHECK: entry:
 // CHECK: call void @_init_resource_bindings()
 
 // CHECK: define internal void @_init_resource_bindings() {
 // CHECK-NEXT: entry:
-// CHECK-DXIL-NEXT: %Buffer_h = call target("dx.RawBuffer", i8, 1, 1) @llvm.dx.handle.fromBinding.tdx.RawBuffer_i8_1_1t(i32 0, i32 0, i32 1, i32 0, i1 false)
-// CHECK-DXIL-NEXT: store target("dx.RawBuffer", i8, 1, 1) %Buffer_h, ptr @Buffer, align 4
+// CHECK-DXIL-NEXT: %Buffer_h = call target("dx.RawBuffer", i8, 0, 0) @llvm.dx.handle.fromBinding.tdx.RawBuffer_i8_0_0t(i32 0, i32 0, i32 1, i32 0, i1 false)
+// CHECK-DXIL-NEXT: store target("dx.RawBuffer", i8, 0, 0) %Buffer_h, ptr @Buffer, align 4

>From 63651b283b42a6c74927cfa2de58bc3403f5d0bc Mon Sep 17 00:00:00 2001
From: Joao Saffran <jderezende at microsoft.com>
Date: Sat, 23 Nov 2024 01:51:04 +0000
Subject: [PATCH 11/18] fixing record decl

---
 clang/lib/AST/DeclBase.cpp                     | 5 -----
 clang/lib/Sema/HLSLExternalSemaSource.cpp      | 1 +
 clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl | 2 +-
 3 files changed, 2 insertions(+), 6 deletions(-)

diff --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp
index 928da907b37aba..fb701f76231bcd 100644
--- a/clang/lib/AST/DeclBase.cpp
+++ b/clang/lib/AST/DeclBase.cpp
@@ -1471,11 +1471,6 @@ DeclContext *DeclContext::getPrimaryContext() {
   case Decl::ObjCCategoryImpl:
     return this;
 
-  case Decl::CXXRecord:
-    if (auto *OPD = dyn_cast<CXXRecordDecl>(this))
-      if (auto *Def = OPD->getDefinition())
-        return Def;
-    return this;
   default:
     if (getDeclKind() >= Decl::firstTag && getDeclKind() <= Decl::lastTag) {
       // If this is a tag type that has a definition or is currently
diff --git a/clang/lib/Sema/HLSLExternalSemaSource.cpp b/clang/lib/Sema/HLSLExternalSemaSource.cpp
index 727daaa76424e8..acb612a2494ea8 100644
--- a/clang/lib/Sema/HLSLExternalSemaSource.cpp
+++ b/clang/lib/Sema/HLSLExternalSemaSource.cpp
@@ -77,6 +77,7 @@ struct BuiltinTypeDeclBuilder {
     Record = CXXRecordDecl::Create(AST, TagDecl::TagKind::Class, HLSLNamespace,
                                    SourceLocation(), SourceLocation(), &II,
                                    PrevDecl, true);
+    Record->startDefinition();
     Record->setImplicit(true);
     Record->setLexicalDeclContext(HLSLNamespace);
     Record->setHasExternalLexicalStorage();
diff --git a/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl b/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl
index 97a822d2a7b10b..e51f9677891fd1 100644
--- a/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl
+++ b/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl
@@ -18,7 +18,7 @@ ByteAddressBuffer Buffer;
 
 
 // CHECK: FinalAttr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> Implicit final
-// CHECK-NEXT: FieldDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> implicit h '__hlsl_resource_t
+// CHECK-NEXT: FieldDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> implicit __handle '__hlsl_resource_t
 // CHECK-SAME{LITERAL}: [[hlsl::resource_class(SRV)]]
 // CHECK-SAME{LITERAL}: [[hlsl::raw_buffer]]
 // CHECK-SAME{LITERAL}: [[hlsl::contained_type(char8_t)]]

>From b002fd2c8274c0a3531b4b7b00f3d7473686b6bc Mon Sep 17 00:00:00 2001
From: Joao Saffran <jderezende at microsoft.com>
Date: Sat, 23 Nov 2024 08:22:05 +0000
Subject: [PATCH 12/18] changing fix

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

diff --git a/clang/lib/Sema/HLSLExternalSemaSource.cpp b/clang/lib/Sema/HLSLExternalSemaSource.cpp
index acb612a2494ea8..78bf948f693abe 100644
--- a/clang/lib/Sema/HLSLExternalSemaSource.cpp
+++ b/clang/lib/Sema/HLSLExternalSemaSource.cpp
@@ -77,7 +77,6 @@ struct BuiltinTypeDeclBuilder {
     Record = CXXRecordDecl::Create(AST, TagDecl::TagKind::Class, HLSLNamespace,
                                    SourceLocation(), SourceLocation(), &II,
                                    PrevDecl, true);
-    Record->startDefinition();
     Record->setImplicit(true);
     Record->setLexicalDeclContext(HLSLNamespace);
     Record->setHasExternalLexicalStorage();
@@ -88,6 +87,9 @@ struct BuiltinTypeDeclBuilder {
   }
 
   ~BuiltinTypeDeclBuilder() {
+    if (!Record->getTypeForDecl())
+      Record->getASTContext().getTypeDeclType(Record,
+                                              Record->getPreviousDecl());
     if (HLSLNamespace && !Template && Record->getDeclContext() == HLSLNamespace)
       HLSLNamespace->addDecl(Record);
   }

>From 9ee46f72dee080b1b0ecdfefe0684cf8738b846e Mon Sep 17 00:00:00 2001
From: Joao Saffran <jderezende at microsoft.com>
Date: Sat, 23 Nov 2024 19:25:34 +0000
Subject: [PATCH 13/18] appling suggested fix from llvm-beanz

---
 clang/lib/Sema/HLSLExternalSemaSource.cpp | 55 ++++++++++++++---------
 1 file changed, 33 insertions(+), 22 deletions(-)

diff --git a/clang/lib/Sema/HLSLExternalSemaSource.cpp b/clang/lib/Sema/HLSLExternalSemaSource.cpp
index 78bf948f693abe..c03e8211f9ea16 100644
--- a/clang/lib/Sema/HLSLExternalSemaSource.cpp
+++ b/clang/lib/Sema/HLSLExternalSemaSource.cpp
@@ -35,16 +35,17 @@ namespace {
 
 struct TemplateParameterListBuilder;
 
-struct BuiltinTypeDeclBuilder {
-  Sema &SemaRef;
+class BuiltinTypeDeclBuilder {
   CXXRecordDecl *Record = nullptr;
   ClassTemplateDecl *Template = nullptr;
   ClassTemplateDecl *PrevTemplate = nullptr;
   NamespaceDecl *HLSLNamespace = nullptr;
   llvm::StringMap<FieldDecl *> Fields;
 
-  BuiltinTypeDeclBuilder(Sema &SemaRef, CXXRecordDecl *R)
-      : SemaRef(SemaRef), Record(R) {
+public:
+  friend struct TemplateParameterListBuilder;
+
+  BuiltinTypeDeclBuilder(CXXRecordDecl *R) : Record(R) {
     Record->startDefinition();
     Template = Record->getDescribedClassTemplate();
   }
@@ -87,13 +88,22 @@ struct BuiltinTypeDeclBuilder {
   }
 
   ~BuiltinTypeDeclBuilder() {
-    if (!Record->getTypeForDecl())
-      Record->getASTContext().getTypeDeclType(Record,
-                                              Record->getPreviousDecl());
     if (HLSLNamespace && !Template && Record->getDeclContext() == HLSLNamespace)
       HLSLNamespace->addDecl(Record);
   }
 
+  CXXRecordDecl *finalizeForwardDeclaration() {
+    // Force the QualType to be generated for the record declaration. In most
+    // cases this will happen naturally when something uses the type the
+    // QualType gets lazily created. Unfortunately, with our injected types if a
+    // type isn't used in a translation unit the QualType may not get
+    // automatically generated before a PCH is generated. To resolve this we
+    // just force that the QualType is generated after we create a forward
+    // declaration.
+    (void)Record->getASTContext().getRecordType(Record);
+    return Record;
+  }
+
   BuiltinTypeDeclBuilder &
   addMemberVariable(StringRef Name, QualType Type, llvm::ArrayRef<Attr *> Attrs,
                     AccessSpecifier Access = AccessSpecifier::AS_private) {
@@ -865,8 +875,9 @@ void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
   ConceptDecl *TypedBufferConcept =
       constructTypedBufferConceptDecl(*SemaPtr, HLSLNamespace);
   Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RWBuffer")
-             .addSimpleTemplateParams({"element_type"}, TypedBufferConcept)
-             .Record;
+             .addSimpleTemplateParams(*SemaPtr, {"element_type"},
+                                      TypedBufferConcept)
+             .finalizeForwardDeclaration();
 
   onCompletion(Decl, [this](CXXRecordDecl *Decl) {
     setupBufferType(Decl, *SemaPtr, ResourceClass::UAV,
@@ -878,8 +889,8 @@ void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
 
   Decl =
       BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RasterizerOrderedBuffer")
-          .addSimpleTemplateParams({"element_type"})
-          .Record;
+          .addSimpleTemplateParams(*SemaPtr, {"element_type"})
+          .finalizeForwardDeclaration();
   onCompletion(Decl, [this](CXXRecordDecl *Decl) {
     setupBufferType(Decl, *SemaPtr, ResourceClass::UAV,
                     ResourceKind::TypedBuffer, /*IsROV=*/true,
@@ -889,8 +900,8 @@ void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
   });
 
   Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "StructuredBuffer")
-             .addSimpleTemplateParams({"element_type"})
-             .Record;
+             .addSimpleTemplateParams(*SemaPtr, {"element_type"})
+             .finalizeForwardDeclaration();
   onCompletion(Decl, [this](CXXRecordDecl *Decl) {
     setupBufferType(Decl, *SemaPtr, ResourceClass::SRV, ResourceKind::RawBuffer,
                     /*IsROV=*/false, /*RawBuffer=*/true)
@@ -899,8 +910,8 @@ void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
   });
 
   Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RWStructuredBuffer")
-             .addSimpleTemplateParams({"element_type"})
-             .Record;
+             .addSimpleTemplateParams(*SemaPtr, {"element_type"})
+             .finalizeForwardDeclaration();
   onCompletion(Decl, [this](CXXRecordDecl *Decl) {
     setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, ResourceKind::RawBuffer,
                     /*IsROV=*/false, /*RawBuffer=*/true)
@@ -912,8 +923,8 @@ void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
 
   Decl =
       BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "AppendStructuredBuffer")
-          .addSimpleTemplateParams({"element_type"})
-          .Record;
+          .addSimpleTemplateParams(*SemaPtr, {"element_type"})
+          .finalizeForwardDeclaration();
   onCompletion(Decl, [this](CXXRecordDecl *Decl) {
     setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, ResourceKind::RawBuffer,
                     /*IsROV=*/false, /*RawBuffer=*/true)
@@ -922,8 +933,8 @@ void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
 
   Decl =
       BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "ConsumeStructuredBuffer")
-          .addSimpleTemplateParams({"element_type"})
-          .Record;
+          .addSimpleTemplateParams(*SemaPtr, {"element_type"})
+          .finalizeForwardDeclaration();
   onCompletion(Decl, [this](CXXRecordDecl *Decl) {
     setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, ResourceKind::RawBuffer,
                     /*IsROV=*/false, /*RawBuffer=*/true)
@@ -932,8 +943,8 @@ void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
 
   Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace,
                                 "RasterizerOrderedStructuredBuffer")
-             .addSimpleTemplateParams({"element_type"})
-             .Record;
+             .addSimpleTemplateParams(*SemaPtr, {"element_type"})
+             .finalizeForwardDeclaration();
   onCompletion(Decl, [this](CXXRecordDecl *Decl) {
     setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, ResourceKind::RawBuffer,
                     /*IsROV=*/true, /*RawBuffer=*/true)
@@ -944,7 +955,7 @@ void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
   });
 
   Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "ByteAddressBuffer")
-             .Record;
+             .finalizeForwardDeclaration();
   onCompletion(Decl, [this](CXXRecordDecl *Decl) {
     setupBufferType(Decl, *SemaPtr, ResourceClass::SRV, ResourceKind::RawBuffer,
                     /*IsROV=*/false,

>From 2c15c3a515d3a9470fd75c3934290b4640579100 Mon Sep 17 00:00:00 2001
From: Joao Saffran <jderezende at microsoft.com>
Date: Tue, 26 Nov 2024 21:23:51 +0000
Subject: [PATCH 14/18] changing test pattern

---
 .../test/AST/HLSL/ByteAddressBuffer-AST.hlsl  | 24 ++++++++++++-------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl b/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl
index e51f9677891fd1..1bf5492df611af 100644
--- a/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl
+++ b/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl
@@ -1,8 +1,12 @@
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl -ast-dump -DEMPTY %s | FileCheck -check-prefix=EMPTY %s
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl -ast-dump %s | FileCheck %s
-
-
-// EMPTY: CXXRecordDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> implicit <undeserialized declarations> class ByteAddressBuffer
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl -ast-dump -DEMPTY \
+// RUN:  -DRESOURCE=ByteAddressBuffer %s | FileCheck -DRESOURCE=ByteAddressBuffer \
+// RUN:  -check-prefix=EMPTY %s
+//
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl -ast-dump \
+// RUN:   -DRESOURCE=ByteAddressBuffer %s | FileCheck -DRESOURCE=ByteAddressBuffer \
+// RUN:   -check-prefixes=CHECK,CHECK-SRV,CHECK-NOSUBSCRIPT %s
+
+// EMPTY: 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
 
 // There should be no more occurrences of ByteAddressBuffer
@@ -10,16 +14,20 @@
 
 #ifndef EMPTY
 
-ByteAddressBuffer Buffer;
+RESOURCE Buffer;
 
 #endif
 
-// CHECK: CXXRecordDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> implicit referenced <undeserialized declarations> class ByteAddressBuffer definition
+// CHECK: CXXRecordDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> implicit referenced <undeserialized declarations> class [[RESOURCE]] definition
 
 
 // CHECK: FinalAttr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> Implicit final
 // CHECK-NEXT: FieldDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> implicit __handle '__hlsl_resource_t
-// CHECK-SAME{LITERAL}: [[hlsl::resource_class(SRV)]]
+// CHECK-SRV-SAME{LITERAL}: [[hlsl::resource_class(SRV)]]
+// CHECK-UAV-SAME{LITERAL}: [[hlsl::resource_class(UAV)]]
 // CHECK-SAME{LITERAL}: [[hlsl::raw_buffer]]
 // CHECK-SAME{LITERAL}: [[hlsl::contained_type(char8_t)]]
 // CHECK-NEXT: HLSLResourceAttr 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> Implicit RawBuffer
+
+// CHECK-NOSUBSCRIPT-NOT: CXXMethodDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> operator[] 'const element_type &(unsigned int) const'
+// CHECK-NOSUBSCRIPT-NOT: CXXMethodDecl 0x{{[0-9A-Fa-f]+}} <<invalid sloc>> <invalid sloc> operator[] 'element_type &(unsigned int)'

>From 40b288d4f953a833674885fe888abecc9e0e37cd Mon Sep 17 00:00:00 2001
From: Joao Saffran <jderezende at microsoft.com>
Date: Tue, 26 Nov 2024 22:41:31 +0000
Subject: [PATCH 15/18] adding other buffers definition

---
 clang/lib/Sema/HLSLExternalSemaSource.cpp      | 16 ++++++++++++++++
 clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl | 16 ++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/clang/lib/Sema/HLSLExternalSemaSource.cpp b/clang/lib/Sema/HLSLExternalSemaSource.cpp
index c03e8211f9ea16..e67284f9af319c 100644
--- a/clang/lib/Sema/HLSLExternalSemaSource.cpp
+++ b/clang/lib/Sema/HLSLExternalSemaSource.cpp
@@ -962,6 +962,22 @@ void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
                     /*RawBuffer=*/true)
         .completeDefinition();
   });
+  Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RWByteAddressBuffer")
+             .finalizeForwardDeclaration();
+  onCompletion(Decl, [this](CXXRecordDecl *Decl) {
+    setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, ResourceKind::RawBuffer,
+                    /*IsROV=*/false,
+                    /*RawBuffer=*/true)
+        .completeDefinition();
+  });
+  Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RasterizerOrderedByteAddressBuffer")
+             .finalizeForwardDeclaration();
+  onCompletion(Decl, [this](CXXRecordDecl *Decl) {
+    setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, ResourceKind::RawBuffer,
+                    /*IsROV=*/true,
+                    /*RawBuffer=*/true)
+        .completeDefinition();
+  });
 }
 
 void HLSLExternalSemaSource::onCompletion(CXXRecordDecl *Record,
diff --git a/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl b/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl
index 1bf5492df611af..adc03bcdd49ebc 100644
--- a/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl
+++ b/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl
@@ -5,6 +5,22 @@
 // RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl -ast-dump \
 // RUN:   -DRESOURCE=ByteAddressBuffer %s | FileCheck -DRESOURCE=ByteAddressBuffer \
 // RUN:   -check-prefixes=CHECK,CHECK-SRV,CHECK-NOSUBSCRIPT %s
+//
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl -ast-dump -DEMPTY \
+// RUN:  -DRESOURCE=RWByteAddressBuffer %s | FileCheck -DRESOURCE=RWByteAddressBuffer \
+// RUN:  -check-prefix=EMPTY %s
+//
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl -ast-dump \
+// RUN:   -DRESOURCE=RWByteAddressBuffer %s | FileCheck -DRESOURCE=RWByteAddressBuffer \
+// RUN:   -check-prefixes=CHECK,CHECK-UAV,CHECK-NOSUBSCRIPT %s
+//
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl -ast-dump -DEMPTY \
+// RUN:  -DRESOURCE=RasterizerOrderedByteAddressBuffer %s | FileCheck -DRESOURCE=RasterizerOrderedByteAddressBuffer \
+// RUN:  -check-prefix=EMPTY %s
+//
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl -ast-dump \
+// RUN:   -DRESOURCE=RasterizerOrderedByteAddressBuffer %s | FileCheck -DRESOURCE=RasterizerOrderedByteAddressBuffer \
+// RUN:   -check-prefixes=CHECK,CHECK-UAV,CHECK-NOSUBSCRIPT %s
 
 // EMPTY: 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

>From 485fa68893353f5eb3e2c653e4e05a6ef7f90a5f Mon Sep 17 00:00:00 2001
From: Joao Saffran <jderezende at microsoft.com>
Date: Wed, 27 Nov 2024 23:56:30 +0000
Subject: [PATCH 16/18] renaming test file

---
 .../{ByteAddressBuffer-AST.hlsl => ByteAddressBuffers-AST.hlsl}   | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename clang/test/AST/HLSL/{ByteAddressBuffer-AST.hlsl => ByteAddressBuffers-AST.hlsl} (100%)

diff --git a/clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl b/clang/test/AST/HLSL/ByteAddressBuffers-AST.hlsl
similarity index 100%
rename from clang/test/AST/HLSL/ByteAddressBuffer-AST.hlsl
rename to clang/test/AST/HLSL/ByteAddressBuffers-AST.hlsl

>From 684e0e0a25b05c5cbbf0dffae0ae64d40cb82cfc Mon Sep 17 00:00:00 2001
From: Joao Saffran <jderezende at microsoft.com>
Date: Thu, 28 Nov 2024 00:13:47 +0000
Subject: [PATCH 17/18] adding missing tests

---
 .../test/AST/HLSL/ByteAddressBuffers-AST.hlsl  |  4 ++--
 .../ByteAddressBuffers-constructors.hlsl       | 18 ++++++++++++++----
 2 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/clang/test/AST/HLSL/ByteAddressBuffers-AST.hlsl b/clang/test/AST/HLSL/ByteAddressBuffers-AST.hlsl
index adc03bcdd49ebc..909cce17e344c1 100644
--- a/clang/test/AST/HLSL/ByteAddressBuffers-AST.hlsl
+++ b/clang/test/AST/HLSL/ByteAddressBuffers-AST.hlsl
@@ -25,8 +25,8 @@
 // EMPTY: 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
 
-// There should be no more occurrences of ByteAddressBuffer
-// EMPTY-NOT: {{[^[:alnum:]]}}ByteAddressBuffer
+// There should be no more occurrences of RESOURCE
+// EMPTY-NOT: {{[^[:alnum:]]}}[[RESOURCE]]
 
 #ifndef EMPTY
 
diff --git a/clang/test/CodeGenHLSL/builtins/ByteAddressBuffers-constructors.hlsl b/clang/test/CodeGenHLSL/builtins/ByteAddressBuffers-constructors.hlsl
index 546040b662cf87..45e135427ba9c3 100644
--- a/clang/test/CodeGenHLSL/builtins/ByteAddressBuffers-constructors.hlsl
+++ b/clang/test/CodeGenHLSL/builtins/ByteAddressBuffers-constructors.hlsl
@@ -3,11 +3,17 @@
 
 // NOTE: SPIRV codegen for resource types is not yet implemented
 
-ByteAddressBuffer Buffer: register(t0);
+ByteAddressBuffer Buffer0: register(t0);
+RWByteAddressBuffer Buffer1: register(u1, space2);
+RasterizerOrderedByteAddressBuffer Buffer2: register(u3, space4);
 
 // CHECK: "class.hlsl::ByteAddressBuffer" = type { target("dx.RawBuffer", i8, 0, 0) }
+// CHECK: "class.hlsl::RWByteAddressBuffer" = type { target("dx.RawBuffer", i8, 1, 0) }
+// CHECK: "class.hlsl::RasterizerOrderedByteAddressBuffer" = type { target("dx.RawBuffer", i8, 1, 1) }
 
-// CHECK: @Buffer = global %"class.hlsl::ByteAddressBuffer" zeroinitializer, align 4
+// CHECK: @Buffer0 = global %"class.hlsl::ByteAddressBuffer" zeroinitializer, align 4
+// CHECK: @Buffer1 = global %"class.hlsl::RWByteAddressBuffer" zeroinitializer, align 4
+// CHECK: @Buffer2 = global %"class.hlsl::RasterizerOrderedByteAddressBuffer" zeroinitializer, align 4
 
 // CHECK: define internal void @_GLOBAL__sub_I_ByteAddressBuffers_constructors.hlsl()
 // CHECK: entry:
@@ -15,5 +21,9 @@ ByteAddressBuffer Buffer: register(t0);
 
 // CHECK: define internal void @_init_resource_bindings() {
 // CHECK-NEXT: entry:
-// CHECK-DXIL-NEXT: %Buffer_h = call target("dx.RawBuffer", i8, 0, 0) @llvm.dx.handle.fromBinding.tdx.RawBuffer_i8_0_0t(i32 0, i32 0, i32 1, i32 0, i1 false)
-// CHECK-DXIL-NEXT: store target("dx.RawBuffer", i8, 0, 0) %Buffer_h, ptr @Buffer, align 4
+// CHECK-DXIL-NEXT: %Buffer0_h = call target("dx.RawBuffer", i8, 0, 0) @llvm.dx.handle.fromBinding.tdx.RawBuffer_i8_0_0t(i32 0, i32 0, i32 1, i32 0, i1 false)
+// CHECK-DXIL-NEXT: store target("dx.RawBuffer", i8, 0, 0) %Buffer0_h, ptr @Buffer0, align 4
+// CHECK-DXIL-NEXT: %Buffer1_h = call target("dx.RawBuffer", i8, 1, 0) @llvm.dx.handle.fromBinding.tdx.RawBuffer_i8_1_0t(i32 2, i32 1, i32 1, i32 0, i1 false)
+// CHECK-DXIL-NEXT: store target("dx.RawBuffer", i8, 1, 0) %Buffer1_h, ptr @Buffer1, align 4
+// CHECK-DXIL-NEXT: %Buffer2_h = call target("dx.RawBuffer", i8, 1, 1) @llvm.dx.handle.fromBinding.tdx.RawBuffer_i8_1_1t(i32 4, i32 3, i32 1, i32 0, i1 false)
+// CHECK-DXIL-NEXT: store target("dx.RawBuffer", i8, 1, 1) %Buffer2_h, ptr @Buffer2, align 4

>From cca110b62f1eb98587fbe706a7b0860faa86b1d8 Mon Sep 17 00:00:00 2001
From: Joao Saffran <jderezende at microsoft.com>
Date: Thu, 28 Nov 2024 17:55:25 +0000
Subject: [PATCH 18/18] fixing merge issues

---
 clang/lib/Sema/HLSLExternalSemaSource.cpp | 31 ++++++++++++-----------
 1 file changed, 16 insertions(+), 15 deletions(-)

diff --git a/clang/lib/Sema/HLSLExternalSemaSource.cpp b/clang/lib/Sema/HLSLExternalSemaSource.cpp
index e67284f9af319c..5bbb39c23e0511 100644
--- a/clang/lib/Sema/HLSLExternalSemaSource.cpp
+++ b/clang/lib/Sema/HLSLExternalSemaSource.cpp
@@ -36,23 +36,25 @@ namespace {
 struct TemplateParameterListBuilder;
 
 class BuiltinTypeDeclBuilder {
-  CXXRecordDecl *Record = nullptr;
   ClassTemplateDecl *Template = nullptr;
   ClassTemplateDecl *PrevTemplate = nullptr;
   NamespaceDecl *HLSLNamespace = nullptr;
   llvm::StringMap<FieldDecl *> Fields;
 
 public:
+  Sema &SemaRef;
+  CXXRecordDecl *Record = nullptr;
   friend struct TemplateParameterListBuilder;
 
-  BuiltinTypeDeclBuilder(CXXRecordDecl *R) : Record(R) {
+  BuiltinTypeDeclBuilder(Sema &SemaRef, CXXRecordDecl *R)
+      : SemaRef(SemaRef), Record(R) {
     Record->startDefinition();
     Template = Record->getDescribedClassTemplate();
   }
 
   BuiltinTypeDeclBuilder(Sema &SemaRef, NamespaceDecl *Namespace,
                          StringRef Name)
-      : SemaRef(SemaRef), HLSLNamespace(Namespace) {
+      : HLSLNamespace(Namespace), SemaRef(SemaRef) {
     ASTContext &AST = SemaRef.getASTContext();
     IdentifierInfo &II = AST.Idents.get(Name, tok::TokenKind::identifier);
 
@@ -553,10 +555,10 @@ struct BuiltinTypeMethodBuilder {
 
     // create method decl
     auto *TSInfo = AST.getTrivialTypeSourceInfo(MethodTy, SourceLocation());
-    Method =
-        CXXMethodDecl::Create(AST, DeclBuilder.Record, SourceLocation(),
-                              NameInfo, MethodTy, TSInfo, SC_None, false, false,
-                              ConstexprSpecKind::Unspecified, SourceLocation());
+    Method = CXXMethodDecl::Create(
+        AST, DeclBuilder.finalizeForwardDeclaration(), SourceLocation(),
+        NameInfo, MethodTy, TSInfo, SC_None, false, false,
+        ConstexprSpecKind::Unspecified, SourceLocation());
 
     // create params & set them to the function prototype
     SmallVector<ParmVarDecl *> ParmDecls;
@@ -875,8 +877,7 @@ void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
   ConceptDecl *TypedBufferConcept =
       constructTypedBufferConceptDecl(*SemaPtr, HLSLNamespace);
   Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RWBuffer")
-             .addSimpleTemplateParams(*SemaPtr, {"element_type"},
-                                      TypedBufferConcept)
+             .addSimpleTemplateParams({"element_type"}, TypedBufferConcept)
              .finalizeForwardDeclaration();
 
   onCompletion(Decl, [this](CXXRecordDecl *Decl) {
@@ -889,7 +890,7 @@ void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
 
   Decl =
       BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RasterizerOrderedBuffer")
-          .addSimpleTemplateParams(*SemaPtr, {"element_type"})
+          .addSimpleTemplateParams({"element_type"})
           .finalizeForwardDeclaration();
   onCompletion(Decl, [this](CXXRecordDecl *Decl) {
     setupBufferType(Decl, *SemaPtr, ResourceClass::UAV,
@@ -900,7 +901,7 @@ void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
   });
 
   Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "StructuredBuffer")
-             .addSimpleTemplateParams(*SemaPtr, {"element_type"})
+             .addSimpleTemplateParams({"element_type"})
              .finalizeForwardDeclaration();
   onCompletion(Decl, [this](CXXRecordDecl *Decl) {
     setupBufferType(Decl, *SemaPtr, ResourceClass::SRV, ResourceKind::RawBuffer,
@@ -910,7 +911,7 @@ void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
   });
 
   Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "RWStructuredBuffer")
-             .addSimpleTemplateParams(*SemaPtr, {"element_type"})
+             .addSimpleTemplateParams({"element_type"})
              .finalizeForwardDeclaration();
   onCompletion(Decl, [this](CXXRecordDecl *Decl) {
     setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, ResourceKind::RawBuffer,
@@ -923,7 +924,7 @@ void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
 
   Decl =
       BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "AppendStructuredBuffer")
-          .addSimpleTemplateParams(*SemaPtr, {"element_type"})
+          .addSimpleTemplateParams({"element_type"})
           .finalizeForwardDeclaration();
   onCompletion(Decl, [this](CXXRecordDecl *Decl) {
     setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, ResourceKind::RawBuffer,
@@ -933,7 +934,7 @@ void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
 
   Decl =
       BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "ConsumeStructuredBuffer")
-          .addSimpleTemplateParams(*SemaPtr, {"element_type"})
+          .addSimpleTemplateParams({"element_type"})
           .finalizeForwardDeclaration();
   onCompletion(Decl, [this](CXXRecordDecl *Decl) {
     setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, ResourceKind::RawBuffer,
@@ -943,7 +944,7 @@ void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
 
   Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace,
                                 "RasterizerOrderedStructuredBuffer")
-             .addSimpleTemplateParams(*SemaPtr, {"element_type"})
+             .addSimpleTemplateParams({"element_type"})
              .finalizeForwardDeclaration();
   onCompletion(Decl, [this](CXXRecordDecl *Decl) {
     setupBufferType(Decl, *SemaPtr, ResourceClass::UAV, ResourceKind::RawBuffer,



More information about the cfe-commits mailing list