[llvm] 141bea8 - [DirectX] Add stub PSV0 section (#96712)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 26 11:15:37 PDT 2024


Author: Damyan Pepper
Date: 2024-06-26T11:15:34-07:00
New Revision: 141bea8c3cad62b381aef3c216cf7f78b812f580

URL: https://github.com/llvm/llvm-project/commit/141bea8c3cad62b381aef3c216cf7f78b812f580
DIFF: https://github.com/llvm/llvm-project/commit/141bea8c3cad62b381aef3c216cf7f78b812f580.diff

LOG: [DirectX] Add stub PSV0 section (#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

Added: 
    llvm/test/CodeGen/DirectX/ContainerData/PipelineStateValidation.ll

Modified: 
    llvm/include/llvm/MC/DXContainerPSVInfo.h
    llvm/lib/Target/DirectX/DXContainerGlobals.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/MC/DXContainerPSVInfo.h b/llvm/include/llvm/MC/DXContainerPSVInfo.h
index bad2fe78eb8fb..3a2d2949d0223 100644
--- a/llvm/include/llvm/MC/DXContainerPSVInfo.h
+++ b/llvm/include/llvm/MC/DXContainerPSVInfo.h
@@ -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;

diff  --git a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
index 67e04c212a692..f49ccfe01f72e 100644
--- a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
+++ b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
@@ -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
@@ -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;
 }
@@ -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)

diff  --git a/llvm/test/CodeGen/DirectX/ContainerData/PipelineStateValidation.ll b/llvm/test/CodeGen/DirectX/ContainerData/PipelineStateValidation.ll
new file mode 100644
index 0000000000000..9e1720b0b1cac
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/ContainerData/PipelineStateValidation.ll
@@ -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:         - [  ]
\ No newline at end of file


        


More information about the llvm-commits mailing list