Skip to content

Commit 219312c

Browse files
authored
[DirectX] Gather resource names in DXIL resource analysis (#140633)
Gather resource names from `llvm.dx.resource.handlefrombinding` calls during DXIL resource analysis and add them to `DXILResourceMap`. Part 3/4 of #105059
1 parent a8d8af3 commit 219312c

File tree

4 files changed

+100
-63
lines changed

4 files changed

+100
-63
lines changed

llvm/include/llvm/Analysis/DXILResource.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "llvm/Support/Alignment.h"
2020
#include "llvm/Support/Compiler.h"
2121
#include "llvm/Support/DXILABI.h"
22-
#include <climits>
2322
#include <cstdint>
2423

2524
namespace llvm {
@@ -33,6 +32,10 @@ class DXILResourceTypeMap;
3332

3433
namespace dxil {
3534

35+
// Returns the resource name from dx_resource_handlefrombinding or
36+
// dx_resource_handlefromimplicitbinding call
37+
StringRef getResourceNameFromBindingCall(CallInst *CI);
38+
3639
/// The dx.RawBuffer target extension type
3740
///
3841
/// `target("dx.RawBuffer", Type, IsWriteable, IsROV)`
@@ -360,17 +363,18 @@ class ResourceInfo {
360363
private:
361364
ResourceBinding Binding;
362365
TargetExtType *HandleTy;
366+
StringRef Name;
363367
GlobalVariable *Symbol = nullptr;
364368

365369
public:
366370
bool GloballyCoherent = false;
367371
ResourceCounterDirection CounterDirection = ResourceCounterDirection::Unknown;
368372

369373
ResourceInfo(uint32_t RecordID, uint32_t Space, uint32_t LowerBound,
370-
uint32_t Size, TargetExtType *HandleTy,
374+
uint32_t Size, TargetExtType *HandleTy, StringRef Name = "",
371375
GlobalVariable *Symbol = nullptr)
372376
: Binding{RecordID, Space, LowerBound, Size}, HandleTy(HandleTy),
373-
Symbol(Symbol) {}
377+
Name(Name), Symbol(Symbol) {}
374378

375379
void setBindingID(unsigned ID) { Binding.RecordID = ID; }
376380

@@ -380,11 +384,10 @@ class ResourceInfo {
380384

381385
const ResourceBinding &getBinding() const { return Binding; }
382386
TargetExtType *getHandleTy() const { return HandleTy; }
383-
StringRef getName() const { return Symbol ? Symbol->getName() : ""; }
387+
const StringRef getName() const { return Name; }
384388

385389
bool hasSymbol() const { return Symbol; }
386-
LLVM_ABI GlobalVariable *createSymbol(Module &M, StructType *Ty,
387-
StringRef Name = "");
390+
LLVM_ABI GlobalVariable *createSymbol(Module &M, StructType *Ty);
388391
LLVM_ABI MDTuple *getAsMetadata(Module &M, dxil::ResourceTypeInfo &RTI) const;
389392

390393
LLVM_ABI std::pair<uint32_t, uint32_t>

llvm/lib/Analysis/DXILResource.cpp

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "llvm/IR/Module.h"
2222
#include "llvm/InitializePasses.h"
2323
#include "llvm/Support/FormatVariadic.h"
24-
#include <climits>
2524
#include <cstdint>
2625
#include <optional>
2726

@@ -531,8 +530,7 @@ void ResourceTypeInfo::print(raw_ostream &OS, const DataLayout &DL) const {
531530
}
532531
}
533532

534-
GlobalVariable *ResourceInfo::createSymbol(Module &M, StructType *Ty,
535-
StringRef Name) {
533+
GlobalVariable *ResourceInfo::createSymbol(Module &M, StructType *Ty) {
536534
assert(!Symbol && "Symbol has already been created");
537535
Symbol = new GlobalVariable(M, Ty, /*isConstant=*/true,
538536
GlobalValue::ExternalLinkage,
@@ -659,6 +657,9 @@ ResourceInfo::getAnnotateProps(Module &M, dxil::ResourceTypeInfo &RTI) const {
659657

660658
void ResourceInfo::print(raw_ostream &OS, dxil::ResourceTypeInfo &RTI,
661659
const DataLayout &DL) const {
660+
if (!Name.empty())
661+
OS << " Name: " << Name << "\n";
662+
662663
if (Symbol) {
663664
OS << " Symbol: ";
664665
Symbol->printAsOperand(OS);
@@ -706,6 +707,30 @@ static bool isUpdateCounterIntrinsic(Function &F) {
706707
return F.getIntrinsicID() == Intrinsic::dx_resource_updatecounter;
707708
}
708709

710+
StringRef dxil::getResourceNameFromBindingCall(CallInst *CI) {
711+
Value *Op = nullptr;
712+
switch (CI->getCalledFunction()->getIntrinsicID()) {
713+
default:
714+
llvm_unreachable("unexpected handle creation intrinsic");
715+
case Intrinsic::dx_resource_handlefrombinding:
716+
case Intrinsic::dx_resource_handlefromimplicitbinding:
717+
Op = CI->getArgOperand(5);
718+
break;
719+
}
720+
721+
auto *GV = dyn_cast<llvm::GlobalVariable>(Op);
722+
if (!GV)
723+
return "";
724+
725+
auto *CA = dyn_cast<ConstantDataArray>(GV->getInitializer());
726+
assert(CA && CA->isString() && "expected constant string");
727+
StringRef Name = CA->getAsString();
728+
// strip trailing 0
729+
if (Name.ends_with('\0'))
730+
Name = Name.drop_back(1);
731+
return Name;
732+
}
733+
709734
void DXILResourceMap::populateResourceInfos(Module &M,
710735
DXILResourceTypeMap &DRTM) {
711736
SmallVector<std::tuple<CallInst *, ResourceInfo, ResourceTypeInfo>> CIToInfos;
@@ -731,8 +756,11 @@ void DXILResourceMap::populateResourceInfos(Module &M,
731756
cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
732757
uint32_t Size =
733758
cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
759+
StringRef Name = getResourceNameFromBindingCall(CI);
760+
734761
ResourceInfo RI =
735-
ResourceInfo{/*RecordID=*/0, Space, LowerBound, Size, HandleTy};
762+
ResourceInfo{/*RecordID=*/0, Space, LowerBound,
763+
Size, HandleTy, Name};
736764

737765
CIToInfos.emplace_back(CI, RI, RTI);
738766
}

llvm/test/Analysis/DXILResource/buffer-frombinding.ll

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,22 @@
22

33
@G = external constant <4 x float>, align 4
44

5+
@Zero.str = private unnamed_addr constant [5 x i8] c"Zero\00", align 1
6+
@One.str = private unnamed_addr constant [4 x i8] c"One\00", align 1
7+
@Two.str = private unnamed_addr constant [4 x i8] c"Two\00", align 1
8+
@Three.str = private unnamed_addr constant [6 x i8] c"Three\00", align 1
9+
@Four.str = private unnamed_addr constant [5 x i8] c"Four\00", align 1
10+
@Array.str = private unnamed_addr constant [6 x i8] c"Array\00", align 1
11+
@Five.str = private unnamed_addr constant [5 x i8] c"Five\00", align 1
12+
@CB.str = private unnamed_addr constant [3 x i8] c"CB\00", align 1
13+
@Constants.str = private unnamed_addr constant [10 x i8] c"Constants\00", align 1
14+
515
define void @test_typedbuffer() {
616
; ByteAddressBuffer Buf : register(t8, space1)
717
%srv0 = call target("dx.RawBuffer", void, 0, 0)
8-
@llvm.dx.resource.handlefrombinding(i32 1, i32 8, i32 1, i32 0, i1 false, ptr null)
18+
@llvm.dx.resource.handlefrombinding(i32 1, i32 8, i32 1, i32 0, i1 false, ptr @Zero.str)
919
; CHECK: Resource [[SRV0:[0-9]+]]:
20+
; CHECK: Name: Zero
1021
; CHECK: Binding:
1122
; CHECK: Record ID: 0
1223
; CHECK: Space: 1
@@ -18,8 +29,9 @@ define void @test_typedbuffer() {
1829
; struct S { float4 a; uint4 b; };
1930
; StructuredBuffer<S> Buf : register(t2, space4)
2031
%srv1 = call target("dx.RawBuffer", {<4 x float>, <4 x i32>}, 0, 0)
21-
@llvm.dx.resource.handlefrombinding(i32 4, i32 2, i32 1, i32 0, i1 false, ptr null)
32+
@llvm.dx.resource.handlefrombinding(i32 4, i32 2, i32 1, i32 0, i1 false, ptr @One.str)
2233
; CHECK: Resource [[SRV1:[0-9]+]]:
34+
; CHECK: Name: One
2335
; CHECK: Binding:
2436
; CHECK: Record ID: 1
2537
; CHECK: Space: 4
@@ -32,8 +44,9 @@ define void @test_typedbuffer() {
3244

3345
; Buffer<uint4> Buf[24] : register(t3, space5)
3446
%srv2 = call target("dx.TypedBuffer", <4 x i32>, 0, 0, 0)
35-
@llvm.dx.resource.handlefrombinding(i32 5, i32 3, i32 24, i32 0, i1 false, ptr null)
47+
@llvm.dx.resource.handlefrombinding(i32 5, i32 3, i32 24, i32 0, i1 false, ptr @Two.str)
3648
; CHECK: Resource [[SRV2:[0-9]+]]:
49+
; CHECK: Name: Two
3750
; CHECK: Binding:
3851
; CHECK: Record ID: 2
3952
; CHECK: Space: 5
@@ -46,8 +59,9 @@ define void @test_typedbuffer() {
4659

4760
; RWBuffer<int> Buf : register(u7, space2)
4861
%uav0 = call target("dx.TypedBuffer", i32, 1, 0, 1)
49-
@llvm.dx.resource.handlefrombinding(i32 2, i32 7, i32 1, i32 0, i1 false, ptr null)
62+
@llvm.dx.resource.handlefrombinding(i32 2, i32 7, i32 1, i32 0, i1 false, ptr @Three.str)
5063
; CHECK: Resource [[UAV0:[0-9]+]]:
64+
; CHECK: Name: Three
5165
; CHECK: Binding:
5266
; CHECK: Record ID: 0
5367
; CHECK: Space: 2
@@ -63,9 +77,10 @@ define void @test_typedbuffer() {
6377

6478
; RWBuffer<float4> Buf : register(u5, space3)
6579
%uav1 = call target("dx.TypedBuffer", <4 x float>, 1, 0, 0)
66-
@llvm.dx.resource.handlefrombinding(i32 3, i32 5, i32 1, i32 0, i1 false, ptr null)
80+
@llvm.dx.resource.handlefrombinding(i32 3, i32 5, i32 1, i32 0, i1 false, ptr @Four.str)
6781
call i32 @llvm.dx.resource.updatecounter(target("dx.TypedBuffer", <4 x float>, 1, 0, 0) %uav1, i8 -1)
6882
; CHECK: Resource [[UAV1:[0-9]+]]:
83+
; CHECK: Name: Four
6984
; CHECK: Binding:
7085
; CHECK: Record ID: 1
7186
; CHECK: Space: 3
@@ -82,12 +97,13 @@ define void @test_typedbuffer() {
8297
; RWBuffer<float4> BufferArray[10] : register(u0, space4)
8398
; RWBuffer<float4> Buf = BufferArray[0]
8499
%uav2_1 = call target("dx.TypedBuffer", <4 x float>, 1, 0, 0)
85-
@llvm.dx.resource.handlefrombinding(i32 4, i32 0, i32 10, i32 0, i1 false, ptr null)
100+
@llvm.dx.resource.handlefrombinding(i32 4, i32 0, i32 10, i32 0, i1 false, ptr @Array.str)
86101
; RWBuffer<float4> Buf = BufferArray[5]
87102
%uav2_2 = call target("dx.TypedBuffer", <4 x float>, 1, 0, 0)
88-
@llvm.dx.resource.handlefrombinding(i32 4, i32 0, i32 10, i32 5, i1 false, ptr null)
103+
@llvm.dx.resource.handlefrombinding(i32 4, i32 0, i32 10, i32 5, i1 false, ptr @Array.str)
89104
call i32 @llvm.dx.resource.updatecounter(target("dx.TypedBuffer", <4 x float>, 1, 0, 0) %uav2_2, i8 1)
90105
; CHECK: Resource [[UAV2:[0-9]+]]:
106+
; CHECK: Name: Array
91107
; CHECK: Binding:
92108
; CHECK: Record ID: 2
93109
; CHECK: Space: 4
@@ -103,10 +119,11 @@ define void @test_typedbuffer() {
103119

104120
; RWBuffer<float4> Buf : register(u0, space5)
105121
%uav3 = call target("dx.TypedBuffer", <4 x float>, 1, 0, 0)
106-
@llvm.dx.resource.handlefrombinding(i32 5, i32 0, i32 1, i32 0, i1 false, ptr null)
122+
@llvm.dx.resource.handlefrombinding(i32 5, i32 0, i32 1, i32 0, i1 false, ptr @Five.str)
107123
call i32 @llvm.dx.resource.updatecounter(target("dx.TypedBuffer", <4 x float>, 1, 0, 0) %uav3, i8 -1)
108124
call i32 @llvm.dx.resource.updatecounter(target("dx.TypedBuffer", <4 x float>, 1, 0, 0) %uav3, i8 1)
109125
; CHECK: Resource [[UAV3:[0-9]+]]:
126+
; CHECK: Name: Five
110127
; CHECK: Binding:
111128
; CHECK: Record ID: 3
112129
; CHECK: Space: 5
@@ -121,8 +138,9 @@ define void @test_typedbuffer() {
121138
; CHECK: Element Count: 4
122139

123140
%cb0 = call target("dx.CBuffer", {float})
124-
@llvm.dx.resource.handlefrombinding(i32 1, i32 0, i32 1, i32 0, i1 false, ptr null)
141+
@llvm.dx.resource.handlefrombinding(i32 1, i32 0, i32 1, i32 0, i1 false, ptr @CB.str)
125142
; CHECK: Resource [[CB0:[0-9]+]]:
143+
; CHECK: Name: CB
126144
; CHECK: Binding:
127145
; CHECK: Record ID: 0
128146
; CHECK: Space: 1
@@ -133,8 +151,9 @@ define void @test_typedbuffer() {
133151
; CHECK: CBuffer size: 4
134152

135153
%cb1 = call target("dx.CBuffer", target("dx.Layout", {float}, 4, 0))
136-
@llvm.dx.resource.handlefrombinding(i32 1, i32 8, i32 1, i32 0, i1 false, ptr null)
154+
@llvm.dx.resource.handlefrombinding(i32 1, i32 8, i32 1, i32 0, i1 false, ptr @Constants.str)
137155
; CHECK: Resource [[CB1:[0-9]+]]:
156+
; CHECK: Name: Constants
138157
; CHECK: Binding:
139158
; CHECK: Record ID: 1
140159
; CHECK: Space: 1

0 commit comments

Comments
 (0)