Skip to content

Commit 141bea8

Browse files
authored
[DirectX] Add stub PSV0 section (llvm#96712)
Direct3D requires a PSV0 section to be present in the DXContainer in order to be able to load and use the shader. This change adds a minimal stub PSV0, with some hard-coded values, that are just enough to unblock loading into Direct3D. Contributes to #90129
1 parent 868fae1 commit 141bea8

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

llvm/include/llvm/MC/DXContainerPSVInfo.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ struct PSVSignatureElement {
4747
// modifiable format, and can be used to serialize the data back into valid PSV
4848
// RuntimeInfo.
4949
struct PSVRuntimeInfo {
50-
PSVRuntimeInfo() : DXConStrTabBuilder(StringTableBuilder::DXContainer) {}
50+
PSVRuntimeInfo() : DXConStrTabBuilder(StringTableBuilder::DXContainer) {
51+
memset((void *)&BaseData, 0, sizeof(dxbc::PSV::v3::RuntimeInfo));
52+
}
5153
bool IsFinalized = false;
5254
dxbc::PSV::v3::RuntimeInfo BaseData;
5355
SmallVector<dxbc::PSV::v2::ResourceBindInfo> Resources;

llvm/lib/Target/DirectX/DXContainerGlobals.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class DXContainerGlobals : public llvm::ModulePass {
3737
GlobalVariable *buildSignature(Module &M, Signature &Sig, StringRef Name,
3838
StringRef SectionName);
3939
void addSignature(Module &M, SmallVector<GlobalValue *> &Globals);
40+
void addPipelineStateValidationInfo(Module &M,
41+
SmallVector<GlobalValue *> &Globals);
4042

4143
public:
4244
static char ID; // Pass identification, replacement for typeid
@@ -63,6 +65,7 @@ bool DXContainerGlobals::runOnModule(Module &M) {
6365
Globals.push_back(getFeatureFlags(M));
6466
Globals.push_back(computeShaderHash(M));
6567
addSignature(M, Globals);
68+
addPipelineStateValidationInfo(M, Globals);
6669
appendToCompilerUsed(M, Globals);
6770
return true;
6871
}
@@ -133,6 +136,34 @@ void DXContainerGlobals::addSignature(Module &M,
133136
Globals.emplace_back(buildSignature(M, OutputSig, "dx.osg1", "OSG1"));
134137
}
135138

139+
void DXContainerGlobals::addPipelineStateValidationInfo(
140+
Module &M, SmallVector<GlobalValue *> &Globals) {
141+
SmallString<256> Data;
142+
raw_svector_ostream OS(Data);
143+
PSVRuntimeInfo PSV;
144+
Triple TT(M.getTargetTriple());
145+
PSV.BaseData.MinimumWaveLaneCount = 0;
146+
PSV.BaseData.MaximumWaveLaneCount = std::numeric_limits<uint32_t>::max();
147+
PSV.BaseData.ShaderStage =
148+
static_cast<uint8_t>(TT.getEnvironment() - Triple::Pixel);
149+
150+
// Hardcoded values here to unblock loading the shader into D3D.
151+
//
152+
// TODO: Lots more stuff to do here!
153+
//
154+
// See issue https://github.com/llvm/llvm-project/issues/96674.
155+
PSV.BaseData.NumThreadsX = 1;
156+
PSV.BaseData.NumThreadsY = 1;
157+
PSV.BaseData.NumThreadsZ = 1;
158+
PSV.EntryName = "main";
159+
160+
PSV.finalize(TT.getEnvironment());
161+
PSV.write(OS);
162+
Constant *Constant =
163+
ConstantDataArray::getString(M.getContext(), Data, /*AddNull*/ false);
164+
Globals.emplace_back(buildContainerGlobal(M, Constant, "dx.psv0", "PSV0"));
165+
}
166+
136167
char DXContainerGlobals::ID = 0;
137168
INITIALIZE_PASS_BEGIN(DXContainerGlobals, "dxil-globals",
138169
"DXContainer Global Emitter", false, true)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
; RUN: opt %s -dxil-embed -dxil-globals -S -o - | FileCheck %s
2+
; RUN: llc %s --filetype=obj -o - | obj2yaml | FileCheck %s --check-prefix=DXC
3+
target triple = "dxil-unknown-shadermodel6.0-compute"
4+
5+
; CHECK: @dx.psv0 = private constant [76 x i8] c"{{.*}}", section "PSV0", align 4
6+
7+
define void @main() #0 {
8+
entry:
9+
ret void
10+
}
11+
12+
attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }
13+
14+
!dx.valver = !{!0}
15+
16+
!0 = !{i32 1, i32 7}
17+
18+
; DXC: - Name: PSV0
19+
; DXC: Size: 76
20+
; DXC: PSVInfo:
21+
; DXC: Version: 3
22+
; DXC: ShaderStage: 5
23+
; DXC: MinimumWaveLaneCount: 0
24+
; DXC: MaximumWaveLaneCount: 4294967295
25+
; DXC: UsesViewID: 0
26+
; DXC: SigInputVectors: 0
27+
; DXC: SigOutputVectors: [ 0, 0, 0, 0 ]
28+
; DXC: NumThreadsX: 1
29+
; DXC: NumThreadsY: 1
30+
; DXC: NumThreadsZ: 1
31+
; DXC: EntryName: main
32+
; DXC: ResourceStride: 24
33+
; DXC: Resources: []
34+
; DXC: SigInputElements: []
35+
; DXC: SigOutputElements: []
36+
; DXC: SigPatchOrPrimElements: []
37+
; DXC: InputOutputMap:
38+
; DXC: - [ ]
39+
; DXC: - [ ]
40+
; DXC: - [ ]
41+
; DXC: - [ ]

0 commit comments

Comments
 (0)