Skip to content

[DirectX] Gather resource names in DXIL resource analysis #140633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 28, 2025

Conversation

hekota
Copy link
Member

@hekota hekota commented May 19, 2025

Gather resource names from llvm.dx.resource.handlefrombinding calls during DXIL resource analysis and adds them to DXILResourceMap.

Part 3/4 of #105059

Depends on #139991

@llvmbot
Copy link
Member

llvmbot commented May 19, 2025

@llvm/pr-subscribers-backend-directx

@llvm/pr-subscribers-llvm-analysis

Author: Helena Kotas (hekota)

Changes

Gather resource names from llvm.dx.resource.handlefrombinding calls during DXIL resource analysis and adds them to DXILResourceMap.

Part 3/4 of llvm/llvm-project#105059

Depends on #139991


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

4 Files Affected:

  • (modified) llvm/include/llvm/Analysis/DXILResource.h (+11-6)
  • (modified) llvm/lib/Analysis/DXILResource.cpp (+31-4)
  • (modified) llvm/test/Analysis/DXILResource/buffer-frombinding.ll (+29-10)
  • (modified) llvm/unittests/Analysis/DXILResourceTest.cpp (+30-43)
diff --git a/llvm/include/llvm/Analysis/DXILResource.h b/llvm/include/llvm/Analysis/DXILResource.h
index b6efd82bb308e..a274e2294561e 100644
--- a/llvm/include/llvm/Analysis/DXILResource.h
+++ b/llvm/include/llvm/Analysis/DXILResource.h
@@ -16,9 +16,7 @@
 #include "llvm/IR/GlobalVariable.h"
 #include "llvm/IR/PassManager.h"
 #include "llvm/Pass.h"
-#include "llvm/Support/Alignment.h"
 #include "llvm/Support/DXILABI.h"
-#include <climits>
 #include <cstdint>
 
 namespace llvm {
@@ -32,6 +30,10 @@ class DXILResourceTypeMap;
 
 namespace dxil {
 
+// Returns the resource name from dx_resource_handlefrombinding or
+// dx_resource_handlefromimplicitbinding call
+StringRef getResourceNameFromBindingCall(CallInst *CI);
+
 /// The dx.RawBuffer target extension type
 ///
 /// `target("dx.RawBuffer", Type, IsWriteable, IsROV)`
@@ -358,6 +360,7 @@ class ResourceInfo {
 private:
   ResourceBinding Binding;
   TargetExtType *HandleTy;
+  StringRef Name;
   GlobalVariable *Symbol = nullptr;
 
 public:
@@ -365,10 +368,10 @@ class ResourceInfo {
   ResourceCounterDirection CounterDirection = ResourceCounterDirection::Unknown;
 
   ResourceInfo(uint32_t RecordID, uint32_t Space, uint32_t LowerBound,
-               uint32_t Size, TargetExtType *HandleTy,
+               uint32_t Size, TargetExtType *HandleTy, StringRef Name = "",
                GlobalVariable *Symbol = nullptr)
       : Binding{RecordID, Space, LowerBound, Size}, HandleTy(HandleTy),
-        Symbol(Symbol) {}
+        Name(Name), Symbol(Symbol) {}
 
   void setBindingID(unsigned ID) { Binding.RecordID = ID; }
 
@@ -378,10 +381,12 @@ class ResourceInfo {
 
   const ResourceBinding &getBinding() const { return Binding; }
   TargetExtType *getHandleTy() const { return HandleTy; }
-  StringRef getName() const { return Symbol ? Symbol->getName() : ""; }
+  const StringRef getName() const {
+    return Name.empty() ? (Symbol ? Symbol->getName() : "") : Name;
+  }
 
   bool hasSymbol() const { return Symbol; }
-  GlobalVariable *createSymbol(Module &M, StructType *Ty, StringRef Name = "");
+  GlobalVariable *createSymbol(Module &M, StructType *Ty);
   MDTuple *getAsMetadata(Module &M, dxil::ResourceTypeInfo &RTI) const;
 
   std::pair<uint32_t, uint32_t>
diff --git a/llvm/lib/Analysis/DXILResource.cpp b/llvm/lib/Analysis/DXILResource.cpp
index 36b3901246285..8cc9316dfb667 100644
--- a/llvm/lib/Analysis/DXILResource.cpp
+++ b/llvm/lib/Analysis/DXILResource.cpp
@@ -21,7 +21,6 @@
 #include "llvm/IR/Module.h"
 #include "llvm/InitializePasses.h"
 #include "llvm/Support/FormatVariadic.h"
-#include <climits>
 #include <cstdint>
 #include <optional>
 
@@ -531,8 +530,7 @@ void ResourceTypeInfo::print(raw_ostream &OS, const DataLayout &DL) const {
   }
 }
 
-GlobalVariable *ResourceInfo::createSymbol(Module &M, StructType *Ty,
-                                           StringRef Name) {
+GlobalVariable *ResourceInfo::createSymbol(Module &M, StructType *Ty) {
   assert(!Symbol && "Symbol has already been created");
   Symbol = new GlobalVariable(M, Ty, /*isConstant=*/true,
                               GlobalValue::ExternalLinkage,
@@ -659,6 +657,9 @@ ResourceInfo::getAnnotateProps(Module &M, dxil::ResourceTypeInfo &RTI) const {
 
 void ResourceInfo::print(raw_ostream &OS, dxil::ResourceTypeInfo &RTI,
                          const DataLayout &DL) const {
+  if (!Name.empty())
+    OS << "  Name: " << Name << "\n";
+
   if (Symbol) {
     OS << "  Symbol: ";
     Symbol->printAsOperand(OS);
@@ -706,6 +707,29 @@ static bool isUpdateCounterIntrinsic(Function &F) {
   return F.getIntrinsicID() == Intrinsic::dx_resource_updatecounter;
 }
 
+StringRef dxil::getResourceNameFromBindingCall(CallInst *CI) {
+  Value *Op = nullptr;
+  switch (CI->getCalledFunction()->getIntrinsicID()) {
+  default:
+    return "";
+  case Intrinsic::dx_resource_handlefrombinding:
+  case Intrinsic::dx_resource_handlefromimplicitbinding:
+    Op = CI->getArgOperand(5);
+    break;
+  }
+  StringRef Name;
+  if (auto *GV = dyn_cast<llvm::GlobalVariable>(Op)) {
+    auto *CA = dyn_cast<ConstantDataArray>(GV->getInitializer());
+    if (CA && CA->isString()) {
+      Name = CA->getAsString();
+      // strip trailing 0
+      if (Name.ends_with('\0'))
+        Name = Name.drop_back(1);
+    }
+  }
+  return Name;
+}
+
 void DXILResourceMap::populateResourceInfos(Module &M,
                                             DXILResourceTypeMap &DRTM) {
   SmallVector<std::tuple<CallInst *, ResourceInfo, ResourceTypeInfo>> CIToInfos;
@@ -731,8 +755,11 @@ void DXILResourceMap::populateResourceInfos(Module &M,
               cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
           uint32_t Size =
               cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
+          StringRef Name = getResourceNameFromBindingCall(CI);
+
           ResourceInfo RI =
-              ResourceInfo{/*RecordID=*/0, Space, LowerBound, Size, HandleTy};
+              ResourceInfo{/*RecordID=*/0, Space,    LowerBound,
+                           Size,           HandleTy, Name};
 
           CIToInfos.emplace_back(CI, RI, RTI);
         }
diff --git a/llvm/test/Analysis/DXILResource/buffer-frombinding.ll b/llvm/test/Analysis/DXILResource/buffer-frombinding.ll
index efacff71ee127..6c8476617e693 100644
--- a/llvm/test/Analysis/DXILResource/buffer-frombinding.ll
+++ b/llvm/test/Analysis/DXILResource/buffer-frombinding.ll
@@ -2,11 +2,22 @@
 
 @G = external constant <4 x float>, align 4
 
+@Zero.str = private unnamed_addr constant [5 x i8] c"Zero\00", align 1
+@One.str = private unnamed_addr constant [4 x i8] c"One\00", align 1
+@Two.str = private unnamed_addr constant [4 x i8] c"Two\00", align 1
+@Three.str = private unnamed_addr constant [6 x i8] c"Three\00", align 1
+@Four.str = private unnamed_addr constant [5 x i8] c"Four\00", align 1
+@Array.str = private unnamed_addr constant [6 x i8] c"Array\00", align 1
+@Five.str = private unnamed_addr constant [5 x i8] c"Five\00", align 1
+@CB.str = private unnamed_addr constant [3 x i8] c"CB\00", align 1
+@Constants.str = private unnamed_addr constant [10 x i8] c"Constants\00", align 1
+
 define void @test_typedbuffer() {
   ; ByteAddressBuffer Buf : register(t8, space1)
   %srv0 = call target("dx.RawBuffer", void, 0, 0)
-      @llvm.dx.resource.handlefrombinding(i32 1, i32 8, i32 1, i32 0, i1 false, ptr null)
+      @llvm.dx.resource.handlefrombinding(i32 1, i32 8, i32 1, i32 0, i1 false, ptr @Zero.str)
   ; CHECK: Resource [[SRV0:[0-9]+]]:
+  ; CHECK:   Name: Zero
   ; CHECK:   Binding:
   ; CHECK:     Record ID: 0
   ; CHECK:     Space: 1
@@ -18,8 +29,9 @@ define void @test_typedbuffer() {
   ; struct S { float4 a; uint4 b; };
   ; StructuredBuffer<S> Buf : register(t2, space4)
   %srv1 = call target("dx.RawBuffer", {<4 x float>, <4 x i32>}, 0, 0)
-      @llvm.dx.resource.handlefrombinding(i32 4, i32 2, i32 1, i32 0, i1 false, ptr null)
+      @llvm.dx.resource.handlefrombinding(i32 4, i32 2, i32 1, i32 0, i1 false, ptr @One.str)
   ; CHECK: Resource [[SRV1:[0-9]+]]:
+  ; CHECK:   Name: One
   ; CHECK:   Binding:
   ; CHECK:     Record ID: 1
   ; CHECK:     Space: 4
@@ -32,8 +44,9 @@ define void @test_typedbuffer() {
 
   ; Buffer<uint4> Buf[24] : register(t3, space5)
   %srv2 = call target("dx.TypedBuffer", <4 x i32>, 0, 0, 0)
-      @llvm.dx.resource.handlefrombinding(i32 5, i32 3, i32 24, i32 0, i1 false, ptr null)
+      @llvm.dx.resource.handlefrombinding(i32 5, i32 3, i32 24, i32 0, i1 false, ptr @Two.str)
   ; CHECK: Resource [[SRV2:[0-9]+]]:
+  ; CHECK:   Name: Two
   ; CHECK:   Binding:
   ; CHECK:     Record ID: 2
   ; CHECK:     Space: 5
@@ -46,8 +59,9 @@ define void @test_typedbuffer() {
 
   ; RWBuffer<int> Buf : register(u7, space2)
   %uav0 = call target("dx.TypedBuffer", i32, 1, 0, 1)
-      @llvm.dx.resource.handlefrombinding(i32 2, i32 7, i32 1, i32 0, i1 false, ptr null)
+      @llvm.dx.resource.handlefrombinding(i32 2, i32 7, i32 1, i32 0, i1 false, ptr @Three.str)
   ; CHECK: Resource [[UAV0:[0-9]+]]:
+  ; CHECK:   Name: Three
   ; CHECK:   Binding:
   ; CHECK:     Record ID: 0
   ; CHECK:     Space: 2
@@ -63,9 +77,10 @@ define void @test_typedbuffer() {
 
   ; RWBuffer<float4> Buf : register(u5, space3)
   %uav1 = call target("dx.TypedBuffer", <4 x float>, 1, 0, 0)
-      @llvm.dx.resource.handlefrombinding(i32 3, i32 5, i32 1, i32 0, i1 false, ptr null)
+      @llvm.dx.resource.handlefrombinding(i32 3, i32 5, i32 1, i32 0, i1 false, ptr @Four.str)
   call i32 @llvm.dx.resource.updatecounter(target("dx.TypedBuffer", <4 x float>, 1, 0, 0) %uav1, i8 -1)
   ; CHECK: Resource [[UAV1:[0-9]+]]:
+  ; CHECK:   Name: Four
   ; CHECK:   Binding:
   ; CHECK:     Record ID: 1
   ; CHECK:     Space: 3
@@ -82,12 +97,13 @@ define void @test_typedbuffer() {
   ; RWBuffer<float4> BufferArray[10] : register(u0, space4)
   ; RWBuffer<float4> Buf = BufferArray[0]
   %uav2_1 = call target("dx.TypedBuffer", <4 x float>, 1, 0, 0)
-        @llvm.dx.resource.handlefrombinding(i32 4, i32 0, i32 10, i32 0, i1 false, ptr null)
+        @llvm.dx.resource.handlefrombinding(i32 4, i32 0, i32 10, i32 0, i1 false, ptr @Array.str)
   ; RWBuffer<float4> Buf = BufferArray[5]
   %uav2_2 = call target("dx.TypedBuffer", <4 x float>, 1, 0, 0)
-        @llvm.dx.resource.handlefrombinding(i32 4, i32 0, i32 10, i32 5, i1 false, ptr null)
+        @llvm.dx.resource.handlefrombinding(i32 4, i32 0, i32 10, i32 5, i1 false, ptr @Array.str)
   call i32 @llvm.dx.resource.updatecounter(target("dx.TypedBuffer", <4 x float>, 1, 0, 0) %uav2_2, i8 1)
   ; CHECK: Resource [[UAV2:[0-9]+]]:
+  ; CHECK:   Name: Array
   ; CHECK:   Binding:
   ; CHECK:     Record ID: 2
   ; CHECK:     Space: 4
@@ -103,10 +119,11 @@ define void @test_typedbuffer() {
 
   ; RWBuffer<float4> Buf : register(u0, space5)
   %uav3 = call target("dx.TypedBuffer", <4 x float>, 1, 0, 0)
-      @llvm.dx.resource.handlefrombinding(i32 5, i32 0, i32 1, i32 0, i1 false, ptr null)
+      @llvm.dx.resource.handlefrombinding(i32 5, i32 0, i32 1, i32 0, i1 false, ptr @Five.str)
   call i32 @llvm.dx.resource.updatecounter(target("dx.TypedBuffer", <4 x float>, 1, 0, 0) %uav3, i8 -1)
   call i32 @llvm.dx.resource.updatecounter(target("dx.TypedBuffer", <4 x float>, 1, 0, 0) %uav3, i8 1)
   ; CHECK: Resource [[UAV3:[0-9]+]]:
+  ; CHECK:   Name: Five
   ; CHECK:   Binding:
   ; CHECK:     Record ID: 3
   ; CHECK:     Space: 5
@@ -121,8 +138,9 @@ define void @test_typedbuffer() {
   ; CHECK:   Element Count: 4
 
   %cb0 = call target("dx.CBuffer", {float})
-     @llvm.dx.resource.handlefrombinding(i32 1, i32 0, i32 1, i32 0, i1 false, ptr null)
+     @llvm.dx.resource.handlefrombinding(i32 1, i32 0, i32 1, i32 0, i1 false, ptr @CB.str)
   ; CHECK: Resource [[CB0:[0-9]+]]:
+  ; CHECK:   Name: CB
   ; CHECK:   Binding:
   ; CHECK:     Record ID: 0
   ; CHECK:     Space: 1
@@ -133,8 +151,9 @@ define void @test_typedbuffer() {
   ; CHECK:   CBuffer size: 4
 
   %cb1 = call target("dx.CBuffer", target("dx.Layout", {float}, 4, 0))
-     @llvm.dx.resource.handlefrombinding(i32 1, i32 8, i32 1, i32 0, i1 false, ptr null)
+     @llvm.dx.resource.handlefrombinding(i32 1, i32 8, i32 1, i32 0, i1 false, ptr @Constants.str)
   ; CHECK: Resource [[CB1:[0-9]+]]:
+  ; CHECK:   Name: Constants
   ; CHECK:   Binding:
   ; CHECK:     Record ID: 1
   ; CHECK:     Space: 1
diff --git a/llvm/unittests/Analysis/DXILResourceTest.cpp b/llvm/unittests/Analysis/DXILResourceTest.cpp
index 669f849bbaa97..f6d6ddf9cbb2d 100644
--- a/llvm/unittests/Analysis/DXILResourceTest.cpp
+++ b/llvm/unittests/Analysis/DXILResourceTest.cpp
@@ -101,9 +101,8 @@ TEST(DXILResource, AnnotationsAndMetadata) {
 
     ResourceInfo RI(
         /*RecordID=*/0, /*Space=*/0, /*LowerBound=*/0, /*Size=*/1,
-        RTI.getHandleTy());
-    GlobalVariable *GV =
-        RI.createSymbol(M, RTI.createElementStruct(), "Buffer");
+        RTI.getHandleTy(), "Buffer");
+    GlobalVariable *GV = RI.createSymbol(M, RTI.createElementStruct());
     EXPECT_PROPS_EQ(RI.getAnnotateProps(M, RTI), 0x0000000bU, 0U);
     EXPECT_MDEQ(RI.getAsMetadata(M, RTI),
                 TestMD.get(0, GV, "Buffer", 0, 0, 1, 11, 0, nullptr));
@@ -119,9 +118,8 @@ TEST(DXILResource, AnnotationsAndMetadata) {
 
     ResourceInfo RI(
         /*RecordID=*/1, /*Space=*/2, /*LowerBound=*/3, /*Size=*/1,
-        RTI.getHandleTy());
-    GlobalVariable *GV =
-        RI.createSymbol(M, RTI.createElementStruct(), "BufferOut");
+        RTI.getHandleTy(), "BufferOut");
+    GlobalVariable *GV = RI.createSymbol(M, RTI.createElementStruct());
     EXPECT_PROPS_EQ(RI.getAnnotateProps(M, RTI), 0x0000100bU, 0U);
     EXPECT_MDEQ(RI.getAsMetadata(M, RTI),
                 TestMD.get(1, GV, "BufferOut", 2, 3, 1, 11, false, false, false,
@@ -146,9 +144,8 @@ TEST(DXILResource, AnnotationsAndMetadata) {
 
     ResourceInfo RI(
         /*RecordID=*/0, /*Space=*/0, /*LowerBound=*/0, /*Size=*/1,
-        RTI.getHandleTy());
-    GlobalVariable *GV =
-        RI.createSymbol(M, RTI.createElementStruct(), "Buffer0");
+        RTI.getHandleTy(), "Buffer0");
+    GlobalVariable *GV = RI.createSymbol(M, RTI.createElementStruct());
     EXPECT_PROPS_EQ(RI.getAnnotateProps(M, RTI), 0x0000030cU, 0x00000010U);
     EXPECT_MDEQ(RI.getAsMetadata(M, RTI), TestMD.get(0, GV, "Buffer0", 0, 0, 1,
                                                      12, 0, TestMD.get(1, 16)));
@@ -166,9 +163,8 @@ TEST(DXILResource, AnnotationsAndMetadata) {
 
     ResourceInfo RI(
         /*RecordID=*/1, /*Space=*/0, /*LowerBound=*/1, /*Size=*/1,
-        RTI.getHandleTy());
-    GlobalVariable *GV =
-        RI.createSymbol(M, RTI.createElementStruct(), "Buffer1");
+        RTI.getHandleTy(), "Buffer1");
+    GlobalVariable *GV = RI.createSymbol(M, RTI.createElementStruct());
     EXPECT_PROPS_EQ(RI.getAnnotateProps(M, RTI), 0x0000000cU, 0x0000000cU);
     EXPECT_MDEQ(RI.getAsMetadata(M, RTI), TestMD.get(1, GV, "Buffer1", 0, 1, 1,
                                                      12, 0, TestMD.get(1, 12)));
@@ -188,9 +184,8 @@ TEST(DXILResource, AnnotationsAndMetadata) {
 
     ResourceInfo RI(
         /*RecordID=*/2, /*Space=*/0, /*LowerBound=*/2, /*Size=*/1,
-        RTI.getHandleTy());
-    GlobalVariable *GV =
-        RI.createSymbol(M, RTI.createElementStruct(), "ColorMapTexture");
+        RTI.getHandleTy(), "ColorMapTexture");
+    GlobalVariable *GV = RI.createSymbol(M, RTI.createElementStruct());
     EXPECT_PROPS_EQ(RI.getAnnotateProps(M, RTI), 0x00000002U, 0x00000409U);
     EXPECT_MDEQ(
         RI.getAsMetadata(M, RTI),
@@ -213,9 +208,8 @@ TEST(DXILResource, AnnotationsAndMetadata) {
 
     ResourceInfo RI(
         /*RecordID=*/0, /*Space=*/0, /*LowerBound=*/0, /*Size=*/1,
-        RTI.getHandleTy());
-    GlobalVariable *GV =
-        RI.createSymbol(M, RTI.createElementStruct(), "DepthBuffer");
+        RTI.getHandleTy(), "DepthBuffer");
+    GlobalVariable *GV = RI.createSymbol(M, RTI.createElementStruct());
     EXPECT_PROPS_EQ(RI.getAnnotateProps(M, RTI), 0x00000003U, 0x00080109U);
     EXPECT_MDEQ(
         RI.getAsMetadata(M, RTI),
@@ -235,9 +229,8 @@ TEST(DXILResource, AnnotationsAndMetadata) {
 
     ResourceInfo RI(
         /*RecordID=*/0, /*Space=*/0, /*LowerBound=*/0, /*Size=*/1,
-        RTI.getHandleTy());
-    GlobalVariable *GV =
-        RI.createSymbol(M, RTI.createElementStruct(), "feedbackMinMip");
+        RTI.getHandleTy(), "feedbackMinMip");
+    GlobalVariable *GV = RI.createSymbol(M, RTI.createElementStruct());
     EXPECT_PROPS_EQ(RI.getAnnotateProps(M, RTI), 0x00001011U, 0U);
     EXPECT_MDEQ(RI.getAsMetadata(M, RTI),
                 TestMD.get(0, GV, "feedbackMinMip", 0, 0, 1, 17, false, false,
@@ -257,9 +250,8 @@ TEST(DXILResource, AnnotationsAndMetadata) {
 
     ResourceInfo RI(
         /*RecordID=*/0, /*Space=*/0, /*LowerBound=*/0, /*Size=*/1,
-        RTI.getHandleTy());
-    GlobalVariable *GV =
-        RI.createSymbol(M, RTI.createElementStruct(), "feedbackMipRegion");
+        RTI.getHandleTy(), "feedbackMipRegion");
+    GlobalVariable *GV = RI.createSymbol(M, RTI.createElementStruct());
     EXPECT_PROPS_EQ(RI.getAnnotateProps(M, RTI), 0x00001012U, 0x00000001U);
     EXPECT_MDEQ(RI.getAsMetadata(M, RTI),
                 TestMD.get(0, GV, "feedbackMipRegion", 0, 0, 1, 18, false,
@@ -280,10 +272,9 @@ TEST(DXILResource, AnnotationsAndMetadata) {
 
     ResourceInfo RI(
         /*RecordID=*/0, /*Space=*/2, /*LowerBound=*/0, /*Size=*/1,
-        RTI.getHandleTy());
+        RTI.getHandleTy(), "OutputTexture");
     RI.GloballyCoherent = true;
-    GlobalVariable *GV =
-        RI.createSymbol(M, RTI.createElementStruct(), "OutputTexture");
+    GlobalVariable *GV = RI.createSymbol(M, RTI.createElementStruct());
     EXPECT_PROPS_EQ(RI.getAnnotateProps(M, RTI), 0x00005002U, 0x00000204U);
     EXPECT_MDEQ(RI.getAsMetadata(M, RTI),
                 TestMD.get(0, GV, "OutputTexture", 2, 0, 1, 2, true, false,
@@ -308,8 +299,8 @@ TEST(DXILResource, AnnotationsAndMetadata) {
 
     ResourceInfo RI(
         /*RecordID=*/0, /*Space=*/0, /*LowerBound=*/0, /*Size=*/1,
-        RTI.getHandleTy());
-    GlobalVariable *GV = RI.createSymbol(M, RTI.createElementStruct(), "ROB");
+        RTI.getHandleTy(), "ROB");
+    GlobalVariable *GV = RI.createSymbol(M, RTI.createElementStruct());
     EXPECT_PROPS_EQ(RI.getAnnotateProps(M, RTI), 0x0000300aU, 0x00000409U);
     EXPECT_MDEQ(RI.getAsMetadata(M, RTI),
                 TestMD.get(0, GV, "ROB", 0, 0, 1, 10, false, false, true,
@@ -334,10 +325,9 @@ TEST(DXILResource, AnnotationsAndMetadata) {
 
     ResourceInfo RI(
         /*RecordID=*/0, /*Space=*/0, /*LowerBound=*/2, /*Size=*/1,
-        RTI.getHandleTy());
+        RTI.getHandleTy(), "g_OutputBuffer");
     RI.CounterDirection = ResourceCounterDirection::Increment;
-    GlobalVariable *GV =
-        RI.createSymbol(M, RTI.createElementStruct(), "g_OutputBuffer");
+    GlobalVariable *GV = RI.createSymbol(M, RTI.createElementStruct());
     EXPECT_PROPS_EQ(RI.getAnnotateProps(M, RTI), 0x0000920cU, 0x00000014U);
     EXPECT_MDEQ(RI.getAsMetadata(M, RTI),
                 TestMD.get(0, GV, "g_OutputBuffer", 0, 2, 1, 12, false, true,
@@ -364,9 +354,8 @@ TEST(DXILResource, AnnotationsAndMetadata) {
 
     ResourceInfo RI(
         /*RecordID=*/0, /*Space=*/0, /*LowerBound=*/0, /*Size=*/1,
-        RTI.getHandleTy());
-    GlobalVariable *GV =
-        RI.createSymbol(M, RTI.createElementStruct(), "g_rw_t2dmsa");
+        RTI.getHandleTy(), "g_rw_t2dmsa");
+    GlobalVariable *GV = RI.createSymbol(M, RTI.createElementStruct());
     EXPECT_PROPS_EQ(RI.getAnnotateProps(M, RTI), 0x00001008U, 0x00080105U);
     EXPECT_MDEQ(RI.getAsMetadata(M, RTI),
                 TestMD.get(0, GV, "g_rw_t2dmsa", 0, 0, 1, 8, false, false,
@@ -388,8 +377,8 @@ TEST(DXILResource, AnnotationsAndMetadata) {
 
     ResourceInfo RI(
         /*RecordID=*/0, /*Space=*/0, /*LowerBound=*/0, /*Size=*/1,
-        RTI.getHandleTy());
-    GlobalVariable *GV = RI.createSymbol(M, RTI.createElementStruct(), "");
+        RTI.getHandleTy(), "");
+    GlobalVariable *GV = RI.createSymbol(M, RTI.createElementStruct());
     EXPECT_PROPS_EQ(RI.getAnnotateProps(M, RTI), 0x0000000dU, 0x00000020U);
     EXPECT_MDEQ(RI.getAsMetadata(M, RTI),
                 TestMD.get(0, GV, "", 0, 0, 1, 32, nullptr));
@@ -406,9 +395,8 @@ TEST(DXILResource, AnnotationsAndMetadata) {
 
     ResourceInfo RI(
         /*RecordID=*/0, /*Space=*/0, /*LowerBound=*/0, /*Size=*/1,
-        RTI.getHandleTy());
-    GlobalVariable *GV =
-        RI.createSymbol(M, RTI.createElementStruct(), "ColorMapSampler");
+        RTI.getHandleTy(), "ColorMapSampler");
+    GlobalVariable *GV = RI.createSymbol(M, RTI.createElementStruct());
     EXPECT_PROPS_EQ(RI.getAnnotateProps(M, RTI), 0x0000000eU, 0U);
     EXPECT_MDEQ(RI.getAsMetadata(M, RTI),
                 TestMD.get(0, GV, "ColorMapSampler", 0, 0, 1, 0, nullptr));
@@ -424,9 +412,8 @@ TEST(DXILResource, AnnotationsAndMetadata) {
 
     ResourceInfo RI(
         /*RecordID=*/0, /*Space=*/0, /*LowerBound=*/0, /*Size=*/1,
-        RTI.getHandleTy());
-    GlobalVariable *GV =
-        RI.createSymbol(M, RTI.createElementStruct(), "CmpSampler");
+        RTI.getHandleTy(), "CmpSampler");
+    GlobalVariable *GV = RI.createSymbol(M, RTI.createElementStruct());
     EXPECT_PROPS_EQ(RI.getAnnotateProps(M, RTI), 0x0000800eU, 0U);
     EXPECT_MDEQ(RI.getAsMetadata(M, RTI),
                 TestMD.get(0, GV, "CmpSampler", 0, 0, 1, 1, nullptr));

@@ -358,17 +360,18 @@ class ResourceInfo {
private:
ResourceBinding Binding;
TargetExtType *HandleTy;
StringRef Name;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The best reason I can see to include this here and not as part of the ResourceBinding member is to make it more readily accessible to getName, but I don't see that as being called at all? The other use of the name is in #140982 where it could be simplified as being part of the Binding compare instead of another parameter to std::tie. It works either way though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that it is called for pretty print reasons, even so, the name could be retrieved from the binding member.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name is also used for generating DXIL metadata related to resources (#140635) and for diagnostics. The ResourceBinding struct contains information related to the binding, I don't think the name should be included in the struct.

@hekota hekota changed the base branch from users/hekota/pr139991-resource-names-llvm-intr to main May 28, 2025 06:05
@hekota hekota merged commit 219312c into llvm:main May 28, 2025
11 checks passed
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Jun 3, 2025
Gather resource names from `llvm.dx.resource.handlefrombinding` calls during DXIL resource analysis and add them to `DXILResourceMap`.

Part 3/4 of llvm#105059
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[DirectX] Capture the "name of the resource variable" in DXIL resource analysis
5 participants