Skip to content

Commit a028d48

Browse files
committed
[DirectX] generate resource table for PSV part
Use DXILResourceWrapperPass to build the resource table. Since DXILResourceWrapperPass operates on LLVM intrinsics rather than DXIL operations, add addPreserved for DXILResourceWrapperPass in the passes before DXContainerGlobals Fixes #103275
1 parent 26f6091 commit a028d48

File tree

7 files changed

+159
-0
lines changed

7 files changed

+159
-0
lines changed

llvm/lib/Target/DirectX/DXContainerGlobals.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "llvm/ADT/SmallVector.h"
1616
#include "llvm/ADT/StringExtras.h"
1717
#include "llvm/ADT/StringRef.h"
18+
#include "llvm/Analysis/DXILResource.h"
1819
#include "llvm/BinaryFormat/DXContainer.h"
1920
#include "llvm/CodeGen/Passes.h"
2021
#include "llvm/IR/Constants.h"
@@ -39,6 +40,7 @@ class DXContainerGlobals : public llvm::ModulePass {
3940
GlobalVariable *buildSignature(Module &M, Signature &Sig, StringRef Name,
4041
StringRef SectionName);
4142
void addSignature(Module &M, SmallVector<GlobalValue *> &Globals);
43+
void addResourcesForPSV(Module &M, PSVRuntimeInfo &PSV);
4244
void addPipelineStateValidationInfo(Module &M,
4345
SmallVector<GlobalValue *> &Globals);
4446

@@ -57,6 +59,7 @@ class DXContainerGlobals : public llvm::ModulePass {
5759
void getAnalysisUsage(AnalysisUsage &AU) const override {
5860
AU.setPreservesAll();
5961
AU.addRequired<ShaderFlagsAnalysisWrapper>();
62+
AU.addRequired<DXILResourceWrapperPass>();
6063
}
6164
};
6265

@@ -138,6 +141,56 @@ void DXContainerGlobals::addSignature(Module &M,
138141
Globals.emplace_back(buildSignature(M, OutputSig, "dx.osg1", "OSG1"));
139142
}
140143

144+
void DXContainerGlobals::addResourcesForPSV(Module &M, PSVRuntimeInfo &PSV) {
145+
const DXILResourceMap &ResMap =
146+
getAnalysis<DXILResourceWrapperPass>().getResourceMap();
147+
148+
for (const dxil::ResourceInfo &ResInfo : ResMap) {
149+
const dxil::ResourceInfo::ResourceBinding &Binding = ResInfo.getBinding();
150+
dxbc::PSV::v2::ResourceBindInfo BindInfo;
151+
BindInfo.LowerBound = Binding.LowerBound;
152+
BindInfo.UpperBound = Binding.LowerBound + Binding.Size - 1;
153+
BindInfo.Space = Binding.Space;
154+
155+
dxbc::PSV::ResourceType ResType = dxbc::PSV::ResourceType::Invalid;
156+
bool IsUAV = ResInfo.getResourceClass() == dxil::ResourceClass::UAV;
157+
switch (ResInfo.getResourceKind()) {
158+
case dxil::ResourceKind::Sampler:
159+
ResType = dxbc::PSV::ResourceType::Sampler;
160+
break;
161+
case dxil::ResourceKind::CBuffer:
162+
ResType = dxbc::PSV::ResourceType::CBV;
163+
break;
164+
case dxil::ResourceKind::StructuredBuffer:
165+
ResType = IsUAV ? dxbc::PSV::ResourceType::UAVStructured
166+
: dxbc::PSV::ResourceType::SRVStructured;
167+
if (IsUAV && ResInfo.getUAV().HasCounter)
168+
ResType = dxbc::PSV::ResourceType::UAVStructuredWithCounter;
169+
break;
170+
case dxil::ResourceKind::RTAccelerationStructure:
171+
ResType = dxbc::PSV::ResourceType::SRVRaw;
172+
break;
173+
case dxil::ResourceKind::RawBuffer:
174+
ResType = IsUAV ? dxbc::PSV::ResourceType::UAVRaw
175+
: dxbc::PSV::ResourceType::SRVRaw;
176+
break;
177+
default:
178+
ResType = IsUAV ? dxbc::PSV::ResourceType::UAVTyped
179+
: dxbc::PSV::ResourceType::SRVTyped;
180+
break;
181+
}
182+
BindInfo.Type = ResType;
183+
184+
BindInfo.Kind =
185+
static_cast<dxbc::PSV::ResourceKind>(ResInfo.getResourceKind());
186+
// TODO: Add support for dxbc::PSV::ResourceFlag::UsedByAtomic64, tracking
187+
// with https://github.com/llvm/llvm-project/issues/104392
188+
BindInfo.Flags = 0u;
189+
190+
PSV.Resources.emplace_back(BindInfo);
191+
}
192+
}
193+
141194
void DXContainerGlobals::addPipelineStateValidationInfo(
142195
Module &M, SmallVector<GlobalValue *> &Globals) {
143196
SmallString<256> Data;
@@ -149,6 +202,8 @@ void DXContainerGlobals::addPipelineStateValidationInfo(
149202
PSV.BaseData.ShaderStage =
150203
static_cast<uint8_t>(TT.getEnvironment() - Triple::Pixel);
151204

205+
addResourcesForPSV(M, PSV);
206+
152207
// Hardcoded values here to unblock loading the shader into D3D.
153208
//
154209
// TODO: Lots more stuff to do here!
@@ -170,6 +225,7 @@ char DXContainerGlobals::ID = 0;
170225
INITIALIZE_PASS_BEGIN(DXContainerGlobals, "dxil-globals",
171226
"DXContainer Global Emitter", false, true)
172227
INITIALIZE_PASS_DEPENDENCY(ShaderFlagsAnalysisWrapper)
228+
INITIALIZE_PASS_DEPENDENCY(DXILResourceWrapperPass)
173229
INITIALIZE_PASS_END(DXContainerGlobals, "dxil-globals",
174230
"DXContainer Global Emitter", false, true)
175231

llvm/lib/Target/DirectX/DXILFinalizeLinkage.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "DXILFinalizeLinkage.h"
1010
#include "DirectX.h"
11+
#include "llvm/Analysis/DXILResource.h"
1112
#include "llvm/IR/Function.h"
1213
#include "llvm/IR/GlobalValue.h"
1314
#include "llvm/IR/Metadata.h"
@@ -48,6 +49,11 @@ bool DXILFinalizeLinkageLegacy::runOnModule(Module &M) {
4849
return finalizeLinkage(M);
4950
}
5051

52+
void DXILFinalizeLinkageLegacy::getAnalysisUsage(
53+
AnalysisUsage &AU) const {
54+
AU.addPreserved<DXILResourceWrapperPass>();
55+
}
56+
5157
char DXILFinalizeLinkageLegacy::ID = 0;
5258

5359
INITIALIZE_PASS_BEGIN(DXILFinalizeLinkageLegacy, DEBUG_TYPE,

llvm/lib/Target/DirectX/DXILFinalizeLinkage.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class DXILFinalizeLinkageLegacy : public ModulePass {
3232
DXILFinalizeLinkageLegacy() : ModulePass(ID) {}
3333
bool runOnModule(Module &M) override;
3434

35+
void getAnalysisUsage(AnalysisUsage &AU) const override;
3536
static char ID; // Pass identification.
3637
};
3738
} // namespace llvm

llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "DirectX.h"
1515
#include "llvm/ADT/STLExtras.h"
1616
#include "llvm/ADT/SmallVector.h"
17+
#include "llvm/Analysis/DXILResource.h"
1718
#include "llvm/CodeGen/Passes.h"
1819
#include "llvm/IR/IRBuilder.h"
1920
#include "llvm/IR/Instruction.h"
@@ -440,6 +441,11 @@ bool DXILIntrinsicExpansionLegacy::runOnModule(Module &M) {
440441
return expansionIntrinsics(M);
441442
}
442443

444+
void DXILIntrinsicExpansionLegacy::getAnalysisUsage(
445+
AnalysisUsage &AU) const {
446+
AU.addPreserved<DXILResourceWrapperPass>();
447+
}
448+
443449
char DXILIntrinsicExpansionLegacy::ID = 0;
444450

445451
INITIALIZE_PASS_BEGIN(DXILIntrinsicExpansionLegacy, DEBUG_TYPE,

llvm/lib/Target/DirectX/DXILIntrinsicExpansion.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class DXILIntrinsicExpansionLegacy : public ModulePass {
2626
bool runOnModule(Module &M) override;
2727
DXILIntrinsicExpansionLegacy() : ModulePass(ID) {}
2828

29+
void getAnalysisUsage(AnalysisUsage &AU) const override;
2930
static char ID; // Pass identification.
3031
};
3132
} // namespace llvm

llvm/lib/Target/DirectX/DXILPrepare.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/ADT/STLExtras.h"
2020
#include "llvm/ADT/SmallVector.h"
2121
#include "llvm/ADT/StringSet.h"
22+
#include "llvm/Analysis/DXILResource.h"
2223
#include "llvm/CodeGen/Passes.h"
2324
#include "llvm/IR/AttributeMask.h"
2425
#include "llvm/IR/IRBuilder.h"
@@ -247,6 +248,7 @@ class DXILPrepareModule : public ModulePass {
247248
void getAnalysisUsage(AnalysisUsage &AU) const override {
248249
AU.addPreserved<ShaderFlagsAnalysisWrapper>();
249250
AU.addPreserved<DXILResourceMDWrapper>();
251+
AU.addPreserved<DXILResourceWrapperPass>();
250252
}
251253
static char ID; // Pass identification.
252254
};
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
; RUN: llc %s --filetype=obj -o - | obj2yaml | FileCheck %s --check-prefix=DXC
2+
3+
; Make sure resource table is created correctly.
4+
; DXC: Resources:
5+
target triple = "dxil-unknown-shadermodel6.0-compute"
6+
7+
define void @main() #0 {
8+
9+
; ByteAddressBuffer Buf : register(t8, space1)
10+
; DXC: - Type: SRVRaw
11+
; DXC: Space: 1
12+
; DXC: LowerBound: 8
13+
; DXC: UpperBound: 8
14+
; DXC: Kind: RawBuffer
15+
; DXC: Flags: 0
16+
%srv0 = call target("dx.RawBuffer", i8, 0, 0)
17+
@llvm.dx.handle.fromBinding.tdx.RawBuffer_i8_0_0t(
18+
i32 1, i32 8, i32 1, i32 0, i1 false)
19+
20+
; struct S { float4 a; uint4 b; };
21+
; StructuredBuffer<S> Buf : register(t2, space4)
22+
; DXC: - Type: SRVStructured
23+
; DXC: Space: 4
24+
; DXC: LowerBound: 2
25+
; DXC: UpperBound: 2
26+
; DXC: Kind: StructuredBuffer
27+
; DXC: Flags: 0
28+
%srv1 = call target("dx.RawBuffer", {<4 x float>, <4 x i32>}, 0, 0)
29+
@llvm.dx.handle.fromBinding.tdx.RawBuffer_sl_v4f32v4i32s_0_0t(
30+
i32 4, i32 2, i32 1, i32 0, i1 false)
31+
32+
; Buffer<uint4> Buf[24] : register(t3, space5)
33+
; DXC: - Type: SRVTyped
34+
; DXC: Space: 5
35+
; DXC: LowerBound: 3
36+
; DXC: UpperBound: 26
37+
; DXC: Kind: TypedBuffer
38+
; DXC: Flags: 0
39+
%srv2 = call target("dx.TypedBuffer", <4 x i32>, 0, 0, 0)
40+
@llvm.dx.handle.fromBinding.tdx.TypedBuffer_i32_0_0t(
41+
i32 5, i32 3, i32 24, i32 0, i1 false)
42+
43+
; RWBuffer<int> Buf : register(u7, space2)
44+
; DXC: - Type: UAVTyped
45+
; DXC: Space: 2
46+
; DXC: LowerBound: 7
47+
; DXC: UpperBound: 7
48+
; DXC: Kind: TypedBuffer
49+
; DXC: Flags: 0
50+
%uav0 = call target("dx.TypedBuffer", i32, 1, 0, 1)
51+
@llvm.dx.handle.fromBinding.tdx.TypedBuffer_i32_1_0t(
52+
i32 2, i32 7, i32 1, i32 0, i1 false)
53+
54+
; RWBuffer<float4> Buf : register(u5, space3)
55+
; DXC: - Type: UAVTyped
56+
; DXC: Space: 3
57+
; DXC: LowerBound: 5
58+
; DXC: UpperBound: 5
59+
; DXC: Kind: TypedBuffer
60+
; DXC: Flags: 0
61+
%uav1 = call target("dx.TypedBuffer", <4 x float>, 1, 0, 0)
62+
@llvm.dx.handle.fromBinding.tdx.TypedBuffer_f32_1_0(
63+
i32 3, i32 5, i32 1, i32 0, i1 false)
64+
65+
; RWBuffer<float4> BufferArray[10] : register(u0, space4)
66+
; DXC: - Type: UAVTyped
67+
; DXC: Space: 4
68+
; DXC: LowerBound: 0
69+
; DXC: UpperBound: 9
70+
; DXC: Kind: TypedBuffer
71+
; DXC: Flags: 0
72+
; RWBuffer<float4> Buf = BufferArray[0]
73+
%uav2_1 = call target("dx.TypedBuffer", <4 x float>, 1, 0, 0)
74+
@llvm.dx.handle.fromBinding.tdx.TypedBuffer_f32_1_0(
75+
i32 4, i32 0, i32 10, i32 0, i1 false)
76+
; RWBuffer<float4> Buf = BufferArray[5]
77+
%uav2_2 = call target("dx.TypedBuffer", <4 x float>, 1, 0, 0)
78+
@llvm.dx.handle.fromBinding.tdx.TypedBuffer_f32_1_0(
79+
i32 4, i32 0, i32 10, i32 5, i1 false)
80+
ret void
81+
}
82+
83+
attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }
84+
85+
!dx.valver = !{!0}
86+
87+
!0 = !{i32 1, i32 7}

0 commit comments

Comments
 (0)