[llvm] [clang] [DirectX] Move ROV info into HLSL metadata. NFC (PR #74896)

Justin Bogner via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 8 15:41:23 PST 2023


https://github.com/bogner created https://github.com/llvm/llvm-project/pull/74896

None

>From 133bdc09eed41545794a369d3fa4d2afb720815e Mon Sep 17 00:00:00 2001
From: Justin Bogner <mail at justinbogner.com>
Date: Fri, 8 Dec 2023 15:41:14 -0800
Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?=
 =?UTF-8?q?l=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.5
---
 clang/lib/CodeGen/CGHLSLRuntime.cpp           |  9 ++++++---
 clang/lib/CodeGen/CGHLSLRuntime.h             |  1 +
 .../builtins/RWBuffer-annotations.hlsl        | 12 +++++------
 clang/test/CodeGenHLSL/cbuf.hlsl              |  4 ++--
 .../include/llvm/Frontend/HLSL/HLSLResource.h |  5 +++--
 llvm/lib/Frontend/HLSL/HLSLResource.cpp       | 14 +++++++++----
 llvm/lib/Target/DirectX/DXILResource.cpp      |  4 +---
 llvm/test/CodeGen/DirectX/UAVMetadata.ll      | 20 +++++++++----------
 llvm/test/CodeGen/DirectX/cbuf.ll             |  2 +-
 .../CodeGen/DirectX/legacy_cb_layout_0.ll     |  2 +-
 .../CodeGen/DirectX/legacy_cb_layout_1.ll     |  2 +-
 .../CodeGen/DirectX/legacy_cb_layout_2.ll     |  4 ++--
 .../CodeGen/DirectX/legacy_cb_layout_3.ll     |  2 +-
 13 files changed, 45 insertions(+), 36 deletions(-)

diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index c239bc17ef267..3e8a40e7540be 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -184,7 +184,8 @@ void CGHLSLRuntime::finishCodeGen() {
                                       : llvm::hlsl::ResourceKind::TBuffer;
     std::string TyName =
         Buf.Name.str() + (Buf.IsCBuffer ? ".cb." : ".tb.") + "ty";
-    addBufferResourceAnnotation(GV, TyName, RC, RK, Buf.Binding);
+    addBufferResourceAnnotation(GV, TyName, RC, RK, /*IsROV=*/false,
+                                Buf.Binding);
   }
 }
 
@@ -196,6 +197,7 @@ void CGHLSLRuntime::addBufferResourceAnnotation(llvm::GlobalVariable *GV,
                                                 llvm::StringRef TyName,
                                                 llvm::hlsl::ResourceClass RC,
                                                 llvm::hlsl::ResourceKind RK,
+                                                bool IsROV,
                                                 BufferResBinding &Binding) {
   llvm::Module &M = CGM.getModule();
 
@@ -219,7 +221,7 @@ void CGHLSLRuntime::addBufferResourceAnnotation(llvm::GlobalVariable *GV,
          "ResourceMD must have been set by the switch above.");
 
   llvm::hlsl::FrontendResource Res(
-      GV, TyName, RK, Binding.Reg.value_or(UINT_MAX), Binding.Space);
+      GV, TyName, RK, IsROV, Binding.Reg.value_or(UINT_MAX), Binding.Space);
   ResourceMD->addOperand(Res.getMetadata());
 }
 
@@ -236,10 +238,11 @@ void CGHLSLRuntime::annotateHLSLResource(const VarDecl *D, GlobalVariable *GV) {
 
   llvm::hlsl::ResourceClass RC = Attr->getResourceClass();
   llvm::hlsl::ResourceKind RK = Attr->getResourceKind();
+  bool IsROV = Attr->getIsROV();
 
   QualType QT(Ty, 0);
   BufferResBinding Binding(D->getAttr<HLSLResourceBindingAttr>());
-  addBufferResourceAnnotation(GV, QT.getAsString(), RC, RK, Binding);
+  addBufferResourceAnnotation(GV, QT.getAsString(), RC, RK, IsROV, Binding);
 }
 
 CGHLSLRuntime::BufferResBinding::BufferResBinding(
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.h b/clang/lib/CodeGen/CGHLSLRuntime.h
index 67413fbd4a78e..0979a0c5eb9ad 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.h
+++ b/clang/lib/CodeGen/CGHLSLRuntime.h
@@ -93,6 +93,7 @@ class CGHLSLRuntime {
                                    llvm::StringRef TyName,
                                    llvm::hlsl::ResourceClass RC,
                                    llvm::hlsl::ResourceKind RK,
+                                   bool IsROV,
                                    BufferResBinding &Binding);
   void addConstant(VarDecl *D, Buffer &CB);
   void addBufferDecls(const DeclContext *DC, Buffer &CB);
diff --git a/clang/test/CodeGenHLSL/builtins/RWBuffer-annotations.hlsl b/clang/test/CodeGenHLSL/builtins/RWBuffer-annotations.hlsl
index 77091f8390a15..a70e224b81e4b 100644
--- a/clang/test/CodeGenHLSL/builtins/RWBuffer-annotations.hlsl
+++ b/clang/test/CodeGenHLSL/builtins/RWBuffer-annotations.hlsl
@@ -16,9 +16,9 @@ void main() {
 }
 
 // CHECK: !hlsl.uavs = !{![[Single:[0-9]+]], ![[Array:[0-9]+]], ![[SingleAllocated:[0-9]+]], ![[ArrayAllocated:[0-9]+]], ![[SingleSpace:[0-9]+]], ![[ArraySpace:[0-9]+]]}
-// CHECK-DAG: ![[Single]] = !{ptr @"?Buffer1@@3V?$RWBuffer at M@hlsl@@A", !"RWBuffer<float>", i32 10, i32 -1, i32 0}
-// CHECK-DAG: ![[Array]] = !{ptr @"?BufferArray@@3PAV?$RWBuffer at T?$__vector at M$03 at __clang@@@hlsl@@A", !"RWBuffer<vector<float, 4> >", i32 10, i32 -1, i32 0}
-// CHECK-DAG: ![[SingleAllocated]] = !{ptr @"?Buffer2@@3V?$RWBuffer at M@hlsl@@A", !"RWBuffer<float>", i32 10, i32 3, i32 0}
-// CHECK-DAG: ![[ArrayAllocated]] = !{ptr @"?BufferArray2@@3PAV?$RWBuffer at T?$__vector at M$03 at __clang@@@hlsl@@A", !"RWBuffer<vector<float, 4> >", i32 10, i32 4, i32 0}
-// CHECK-DAG: ![[SingleSpace]] = !{ptr @"?Buffer3@@3V?$RWBuffer at M@hlsl@@A", !"RWBuffer<float>", i32 10, i32 3, i32 1}
-// CHECK-DAG: ![[ArraySpace]] = !{ptr @"?BufferArray3@@3PAV?$RWBuffer at T?$__vector at M$03 at __clang@@@hlsl@@A", !"RWBuffer<vector<float, 4> >", i32 10, i32 4, i32 1}
+// CHECK-DAG: ![[Single]] = !{ptr @"?Buffer1@@3V?$RWBuffer at M@hlsl@@A", !"RWBuffer<float>", i32 10, i1 false, i32 -1, i32 0}
+// CHECK-DAG: ![[Array]] = !{ptr @"?BufferArray@@3PAV?$RWBuffer at T?$__vector at M$03 at __clang@@@hlsl@@A", !"RWBuffer<vector<float, 4> >", i32 10, i1 false, i32 -1, i32 0}
+// CHECK-DAG: ![[SingleAllocated]] = !{ptr @"?Buffer2@@3V?$RWBuffer at M@hlsl@@A", !"RWBuffer<float>", i32 10, i1 false, i32 3, i32 0}
+// CHECK-DAG: ![[ArrayAllocated]] = !{ptr @"?BufferArray2@@3PAV?$RWBuffer at T?$__vector at M$03 at __clang@@@hlsl@@A", !"RWBuffer<vector<float, 4> >", i32 10, i1 false, i32 4, i32 0}
+// CHECK-DAG: ![[SingleSpace]] = !{ptr @"?Buffer3@@3V?$RWBuffer at M@hlsl@@A", !"RWBuffer<float>", i32 10, i1 false, i32 3, i32 1}
+// CHECK-DAG: ![[ArraySpace]] = !{ptr @"?BufferArray3@@3PAV?$RWBuffer at T?$__vector at M$03 at __clang@@@hlsl@@A", !"RWBuffer<vector<float, 4> >", i32 10, i1 false, i32 4, i32 1}
diff --git a/clang/test/CodeGenHLSL/cbuf.hlsl b/clang/test/CodeGenHLSL/cbuf.hlsl
index 92c883943d03e..5dee1feb902aa 100644
--- a/clang/test/CodeGenHLSL/cbuf.hlsl
+++ b/clang/test/CodeGenHLSL/cbuf.hlsl
@@ -24,5 +24,5 @@ float foo() {
 
 // CHECK: !hlsl.cbufs = !{![[CBMD:[0-9]+]]}
 // CHECK: !hlsl.srvs = !{![[TBMD:[0-9]+]]}
-// CHECK: ![[CBMD]] = !{ptr @[[CB]], !"A.cb.ty", i32 13, i32 0, i32 2}
-// CHECK: ![[TBMD]] = !{ptr @[[TB]], !"A.tb.ty", i32 15, i32 2, i32 1}
+// CHECK: ![[CBMD]] = !{ptr @[[CB]], !"A.cb.ty", i32 13, i1 false, i32 0, i32 2}
+// CHECK: ![[TBMD]] = !{ptr @[[TB]], !"A.tb.ty", i32 15, i1 false, i32 2, i32 1}
diff --git a/llvm/include/llvm/Frontend/HLSL/HLSLResource.h b/llvm/include/llvm/Frontend/HLSL/HLSLResource.h
index 997ddccd687a7..eedecaea4e58d 100644
--- a/llvm/include/llvm/Frontend/HLSL/HLSLResource.h
+++ b/llvm/include/llvm/Frontend/HLSL/HLSLResource.h
@@ -59,15 +59,16 @@ class FrontendResource {
 
 public:
   FrontendResource(MDNode *E) : Entry(E) {
-    assert(Entry->getNumOperands() == 5 && "Unexpected metadata shape");
+    assert(Entry->getNumOperands() == 6 && "Unexpected metadata shape");
   }
 
   FrontendResource(GlobalVariable *GV, StringRef TypeStr, ResourceKind RK,
-                   uint32_t ResIndex, uint32_t Space);
+                   bool IsROV, uint32_t ResIndex, uint32_t Space);
 
   GlobalVariable *getGlobalVariable();
   StringRef getSourceType();
   ResourceKind getResourceKind();
+  bool getIsROV();
   uint32_t getResourceIndex();
   uint32_t getSpace();
   MDNode *getMetadata() { return Entry; }
diff --git a/llvm/lib/Frontend/HLSL/HLSLResource.cpp b/llvm/lib/Frontend/HLSL/HLSLResource.cpp
index a3a7d0b8696cf..709fe3212623e 100644
--- a/llvm/lib/Frontend/HLSL/HLSLResource.cpp
+++ b/llvm/lib/Frontend/HLSL/HLSLResource.cpp
@@ -33,25 +33,31 @@ ResourceKind FrontendResource::getResourceKind() {
           cast<ConstantAsMetadata>(Entry->getOperand(2))->getValue())
           ->getLimitedValue());
 }
-uint32_t FrontendResource::getResourceIndex() {
+bool FrontendResource::getIsROV() {
   return cast<ConstantInt>(
              cast<ConstantAsMetadata>(Entry->getOperand(3))->getValue())
       ->getLimitedValue();
 }
-uint32_t FrontendResource::getSpace() {
+uint32_t FrontendResource::getResourceIndex() {
   return cast<ConstantInt>(
              cast<ConstantAsMetadata>(Entry->getOperand(4))->getValue())
       ->getLimitedValue();
 }
+uint32_t FrontendResource::getSpace() {
+  return cast<ConstantInt>(
+             cast<ConstantAsMetadata>(Entry->getOperand(5))->getValue())
+      ->getLimitedValue();
+}
 
 FrontendResource::FrontendResource(GlobalVariable *GV, StringRef TypeStr,
-                                   ResourceKind RK, uint32_t ResIndex,
-                                   uint32_t Space) {
+                                   ResourceKind RK, bool IsROV,
+                                   uint32_t ResIndex, uint32_t Space) {
   auto &Ctx = GV->getContext();
   IRBuilder<> B(Ctx);
   Entry = MDNode::get(
       Ctx, {ValueAsMetadata::get(GV), MDString::get(Ctx, TypeStr),
             ConstantAsMetadata::get(B.getInt32(static_cast<int>(RK))),
+            ConstantAsMetadata::get(B.getInt1(IsROV)),
             ConstantAsMetadata::get(B.getInt32(ResIndex)),
             ConstantAsMetadata::get(B.getInt32(Space))});
 }
diff --git a/llvm/lib/Target/DirectX/DXILResource.cpp b/llvm/lib/Target/DirectX/DXILResource.cpp
index 041b8fafcd6ad..92306d907e054 100644
--- a/llvm/lib/Target/DirectX/DXILResource.cpp
+++ b/llvm/lib/Target/DirectX/DXILResource.cpp
@@ -234,7 +234,7 @@ void ResourceBase::print(raw_ostream &OS, StringRef IDPrefix,
 
 UAVResource::UAVResource(uint32_t I, FrontendResource R)
     : ResourceBase(I, R), Shape(R.getResourceKind()), GloballyCoherent(false),
-      HasCounter(false), IsROV(false), ExtProps() {
+      HasCounter(false), IsROV(R.getIsROV()), ExtProps() {
   parseSourceType(R.getSourceType());
 }
 
@@ -258,8 +258,6 @@ void UAVResource::print(raw_ostream &OS) const {
 // information we need to remove the source type string from here (See issue:
 // https://github.com/llvm/llvm-project/issues/57991).
 void UAVResource::parseSourceType(StringRef S) {
-  IsROV = S.startswith("RasterizerOrdered");
-
   S = S.substr(S.find("<") + 1);
 
   constexpr size_t PrefixLen = StringRef("vector<").size();
diff --git a/llvm/test/CodeGen/DirectX/UAVMetadata.ll b/llvm/test/CodeGen/DirectX/UAVMetadata.ll
index e86d53cd7afc8..3d95723d6e49f 100644
--- a/llvm/test/CodeGen/DirectX/UAVMetadata.ll
+++ b/llvm/test/CodeGen/DirectX/UAVMetadata.ll
@@ -37,16 +37,16 @@ target triple = "dxil-pc-shadermodel6.0-library"
 
 !hlsl.uavs = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9}
 
-!0 = !{ptr @Zero, !"RWBuffer<half>", i32 10, i32 0, i32 0}
-!1 = !{ptr @One, !"Buffer<vector<float,4>>", i32 10, i32 1, i32 0}
-!2 = !{ptr @Two, !"Buffer<double>", i32 10, i32 2, i32 0}
-!3 = !{ptr @Three, !"Buffer<bool>", i32 10, i32 3, i32 0}
-!4 = !{ptr @Four, !"ByteAddressBuffer<int16_t>", i32 11, i32 5, i32 0}
-!5 = !{ptr @Five, !"StructuredBuffer<uint16_t>", i32 12, i32 6, i32 0}
-!6 = !{ptr @Six, !"RasterizerOrderedBuffer<int32_t>", i32 10, i32 7, i32 0}
-!7 = !{ptr @Seven, !"RasterizerOrderedStructuredBuffer<uint32_t>", i32 12, i32 8, i32 0}
-!8 = !{ptr @Eight, !"RasterizerOrderedByteAddressBuffer<int64_t>", i32 11, i32 9, i32 0}
-!9 = !{ptr @Nine, !"RWBuffer<uint64_t>", i32 10, i32 10, i32 2}
+!0 = !{ptr @Zero, !"RWBuffer<half>", i32 10, i1 false, i32 0, i32 0}
+!1 = !{ptr @One, !"Buffer<vector<float,4>>", i32 10, i1 false, i32 1, i32 0}
+!2 = !{ptr @Two, !"Buffer<double>", i32 10, i1 false, i32 2, i32 0}
+!3 = !{ptr @Three, !"Buffer<bool>", i32 10, i1 false, i32 3, i32 0}
+!4 = !{ptr @Four, !"ByteAddressBuffer<int16_t>", i32 11, i1 false, i32 5, i32 0}
+!5 = !{ptr @Five, !"StructuredBuffer<uint16_t>", i32 12, i1 false, i32 6, i32 0}
+!6 = !{ptr @Six, !"RasterizerOrderedBuffer<int32_t>", i32 10, i1 true, i32 7, i32 0}
+!7 = !{ptr @Seven, !"RasterizerOrderedStructuredBuffer<uint32_t>", i32 12, i1 true, i32 8, i32 0}
+!8 = !{ptr @Eight, !"RasterizerOrderedByteAddressBuffer<int64_t>", i32 11, i1 true, i32 9, i32 0}
+!9 = !{ptr @Nine, !"RWBuffer<uint64_t>", i32 10, i1 false, i32 10, i32 2}
 
 ; CHECK: !dx.resources = !{[[ResList:[!][0-9]+]]}
 
diff --git a/llvm/test/CodeGen/DirectX/cbuf.ll b/llvm/test/CodeGen/DirectX/cbuf.ll
index 6640654f730e9..d07cc1e880b1a 100644
--- a/llvm/test/CodeGen/DirectX/cbuf.ll
+++ b/llvm/test/CodeGen/DirectX/cbuf.ll
@@ -34,4 +34,4 @@ attributes #1 = { nocallback nofree nosync nounwind readnone speculatable willre
 
 !hlsl.cbufs = !{!1}
 
-!1 = !{ptr @A.cb., !"A.cb.ty", i32 13, i32 2, i32 1}
+!1 = !{ptr @A.cb., !"A.cb.ty", i32 13, i1 false, i32 2, i32 1}
diff --git a/llvm/test/CodeGen/DirectX/legacy_cb_layout_0.ll b/llvm/test/CodeGen/DirectX/legacy_cb_layout_0.ll
index 50821467d5dd6..0cfb839746b93 100644
--- a/llvm/test/CodeGen/DirectX/legacy_cb_layout_0.ll
+++ b/llvm/test/CodeGen/DirectX/legacy_cb_layout_0.ll
@@ -11,4 +11,4 @@ target triple = "dxil-unknown-shadermodel6.7-library"
 
 !hlsl.cbufs = !{!1}
 
-!1 = !{ptr @A.cb., !"A.cb.ty", i32 13, i32 2, i32 0}
+!1 = !{ptr @A.cb., !"A.cb.ty", i32 13, i1 false, i32 2, i32 0}
diff --git a/llvm/test/CodeGen/DirectX/legacy_cb_layout_1.ll b/llvm/test/CodeGen/DirectX/legacy_cb_layout_1.ll
index 8a38ccd8ef788..b6d29f8d18d79 100644
--- a/llvm/test/CodeGen/DirectX/legacy_cb_layout_1.ll
+++ b/llvm/test/CodeGen/DirectX/legacy_cb_layout_1.ll
@@ -34,4 +34,4 @@ target triple = "dxil-unknown-shadermodel6.7-library"
 
 !hlsl.cbufs = !{!0}
 
-!0 = !{ptr @B.cb., !"B.cb.ty", i32 13, i32 1, i32 0}
+!0 = !{ptr @B.cb., !"B.cb.ty", i32 13, i1 false, i32 1, i32 0}
diff --git a/llvm/test/CodeGen/DirectX/legacy_cb_layout_2.ll b/llvm/test/CodeGen/DirectX/legacy_cb_layout_2.ll
index b669538846ab1..d023d7906fdc5 100644
--- a/llvm/test/CodeGen/DirectX/legacy_cb_layout_2.ll
+++ b/llvm/test/CodeGen/DirectX/legacy_cb_layout_2.ll
@@ -47,5 +47,5 @@ target triple = "dxil-unknown-shadermodel6.7-library"
 
 !hlsl.cbufs = !{!0, !1}
 
-!0 = !{ptr @B.cb., !"B.cb.ty", i32 13, i32 1, i32 0}
-!1 = !{ptr @B.cb..1, !"B.cb.ty", i32 13, i32 2, i32 0}
+!0 = !{ptr @B.cb., !"B.cb.ty", i32 13, i1 false, i32 1, i32 0}
+!1 = !{ptr @B.cb..1, !"B.cb.ty", i32 13, i1 false, i32 2, i32 0}
diff --git a/llvm/test/CodeGen/DirectX/legacy_cb_layout_3.ll b/llvm/test/CodeGen/DirectX/legacy_cb_layout_3.ll
index afd46d08b5c71..38c2cd18b5ca1 100644
--- a/llvm/test/CodeGen/DirectX/legacy_cb_layout_3.ll
+++ b/llvm/test/CodeGen/DirectX/legacy_cb_layout_3.ll
@@ -78,4 +78,4 @@ target triple = "dxil-unknown-shadermodel6.7-library"
 @D.cb. = external local_unnamed_addr constant { i32, %struct.B, half, %struct.C, double }
 
 !hlsl.cbufs = !{!0}
-!0 = !{ptr @D.cb., !"D.cb.ty", i32 13, i32 1, i32 0}
+!0 = !{ptr @D.cb., !"D.cb.ty", i32 13, i1 false, i32 1, i32 0}



More information about the cfe-commits mailing list