Skip to content

Commit

Permalink
[DirectX] Add stub PSV0 section (llvm#96712)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
damyanp authored and AlexisPerry committed Jun 27, 2024
1 parent 01ac534 commit ef20d5b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
4 changes: 3 additions & 1 deletion llvm/include/llvm/MC/DXContainerPSVInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ struct PSVSignatureElement {
// modifiable format, and can be used to serialize the data back into valid PSV
// RuntimeInfo.
struct PSVRuntimeInfo {
PSVRuntimeInfo() : DXConStrTabBuilder(StringTableBuilder::DXContainer) {}
PSVRuntimeInfo() : DXConStrTabBuilder(StringTableBuilder::DXContainer) {
memset((void *)&BaseData, 0, sizeof(dxbc::PSV::v3::RuntimeInfo));
}
bool IsFinalized = false;
dxbc::PSV::v3::RuntimeInfo BaseData;
SmallVector<dxbc::PSV::v2::ResourceBindInfo> Resources;
Expand Down
31 changes: 31 additions & 0 deletions llvm/lib/Target/DirectX/DXContainerGlobals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class DXContainerGlobals : public llvm::ModulePass {
GlobalVariable *buildSignature(Module &M, Signature &Sig, StringRef Name,
StringRef SectionName);
void addSignature(Module &M, SmallVector<GlobalValue *> &Globals);
void addPipelineStateValidationInfo(Module &M,
SmallVector<GlobalValue *> &Globals);

public:
static char ID; // Pass identification, replacement for typeid
Expand All @@ -63,6 +65,7 @@ bool DXContainerGlobals::runOnModule(Module &M) {
Globals.push_back(getFeatureFlags(M));
Globals.push_back(computeShaderHash(M));
addSignature(M, Globals);
addPipelineStateValidationInfo(M, Globals);
appendToCompilerUsed(M, Globals);
return true;
}
Expand Down Expand Up @@ -133,6 +136,34 @@ void DXContainerGlobals::addSignature(Module &M,
Globals.emplace_back(buildSignature(M, OutputSig, "dx.osg1", "OSG1"));
}

void DXContainerGlobals::addPipelineStateValidationInfo(
Module &M, SmallVector<GlobalValue *> &Globals) {
SmallString<256> Data;
raw_svector_ostream OS(Data);
PSVRuntimeInfo PSV;
Triple TT(M.getTargetTriple());
PSV.BaseData.MinimumWaveLaneCount = 0;
PSV.BaseData.MaximumWaveLaneCount = std::numeric_limits<uint32_t>::max();
PSV.BaseData.ShaderStage =
static_cast<uint8_t>(TT.getEnvironment() - Triple::Pixel);

// Hardcoded values here to unblock loading the shader into D3D.
//
// TODO: Lots more stuff to do here!
//
// See issue https://github.com/llvm/llvm-project/issues/96674.
PSV.BaseData.NumThreadsX = 1;
PSV.BaseData.NumThreadsY = 1;
PSV.BaseData.NumThreadsZ = 1;
PSV.EntryName = "main";

PSV.finalize(TT.getEnvironment());
PSV.write(OS);
Constant *Constant =
ConstantDataArray::getString(M.getContext(), Data, /*AddNull*/ false);
Globals.emplace_back(buildContainerGlobal(M, Constant, "dx.psv0", "PSV0"));
}

char DXContainerGlobals::ID = 0;
INITIALIZE_PASS_BEGIN(DXContainerGlobals, "dxil-globals",
"DXContainer Global Emitter", false, true)
Expand Down
41 changes: 41 additions & 0 deletions llvm/test/CodeGen/DirectX/ContainerData/PipelineStateValidation.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
; RUN: opt %s -dxil-embed -dxil-globals -S -o - | FileCheck %s
; RUN: llc %s --filetype=obj -o - | obj2yaml | FileCheck %s --check-prefix=DXC
target triple = "dxil-unknown-shadermodel6.0-compute"

; CHECK: @dx.psv0 = private constant [76 x i8] c"{{.*}}", section "PSV0", align 4

define void @main() #0 {
entry:
ret void
}

attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }

!dx.valver = !{!0}

!0 = !{i32 1, i32 7}

; DXC: - Name: PSV0
; DXC: Size: 76
; DXC: PSVInfo:
; DXC: Version: 3
; DXC: ShaderStage: 5
; DXC: MinimumWaveLaneCount: 0
; DXC: MaximumWaveLaneCount: 4294967295
; DXC: UsesViewID: 0
; DXC: SigInputVectors: 0
; DXC: SigOutputVectors: [ 0, 0, 0, 0 ]
; DXC: NumThreadsX: 1
; DXC: NumThreadsY: 1
; DXC: NumThreadsZ: 1
; DXC: EntryName: main
; DXC: ResourceStride: 24
; DXC: Resources: []
; DXC: SigInputElements: []
; DXC: SigOutputElements: []
; DXC: SigPatchOrPrimElements: []
; DXC: InputOutputMap:
; DXC: - [ ]
; DXC: - [ ]
; DXC: - [ ]
; DXC: - [ ]

0 comments on commit ef20d5b

Please sign in to comment.