[llvm] [DirectX backend] generate ISG1, OSG1 part for compute shader (PR #90508)

Xiang Li via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 29 11:25:36 PDT 2024


https://github.com/python3kgae created https://github.com/llvm/llvm-project/pull/90508

Empty ISG1 and OSG1 parts are generated for compute shader since there's no signature for compute shader.

Fixes #88778

>From d745a774b60f883593e11d28fe30334f88cd7858 Mon Sep 17 00:00:00 2001
From: Xiang Li <python3kgae at outlook.com>
Date: Mon, 29 Apr 2024 14:21:22 -0400
Subject: [PATCH] [DirectX backend] generate ISG1, OSG1 part for compute shader

Empty ISG1 and OSG1 parts are generated for compute shader since there's no signature for compute shader.

Fixes #88778
---
 .../lib/Target/DirectX/DXContainerGlobals.cpp | 52 ++++++++++++++++++-
 .../DirectX/ContainerData/EmptySignature.ll   | 25 +++++++++
 2 files changed, 76 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/CodeGen/DirectX/ContainerData/EmptySignature.ll

diff --git a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
index 65cf1dfdb4031b..4af4c506b630f4 100644
--- a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
+++ b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
@@ -28,8 +28,13 @@ using namespace llvm::dxil;
 namespace {
 class DXContainerGlobals : public llvm::ModulePass {
 
+  GlobalVariable *buildContainerGlobal(Module &M, Constant *Content,
+                                       StringRef Name, StringRef SectionName);
   GlobalVariable *getFeatureFlags(Module &M);
   GlobalVariable *computeShaderHash(Module &M);
+  GlobalVariable *buildInputSingature(Module &M, Constant *Content);
+  GlobalVariable *buildOutputSingature(Module &M, Constant *Content);
+  void addSingature(Module &M, SmallVector<GlobalValue *> &Globals, Triple &TT);
 
 public:
   static char ID; // Pass identification, replacement for typeid
@@ -55,7 +60,8 @@ bool DXContainerGlobals::runOnModule(Module &M) {
   llvm::SmallVector<GlobalValue *> Globals;
   Globals.push_back(getFeatureFlags(M));
   Globals.push_back(computeShaderHash(M));
-
+  Triple TT(M.getTargetTriple());
+  addSingature(M, Globals, TT);
   appendToCompilerUsed(M, Globals);
   return true;
 }
@@ -104,6 +110,50 @@ GlobalVariable *DXContainerGlobals::computeShaderHash(Module &M) {
   return GV;
 }
 
+GlobalVariable *DXContainerGlobals::buildContainerGlobal(
+    Module &M, Constant *Content, StringRef Name, StringRef SectionName) {
+  auto *GV = new llvm::GlobalVariable(
+      M, Content->getType(), true, GlobalValue::PrivateLinkage, Content, Name);
+  GV->setSection(SectionName);
+  GV->setAlignment(Align(4));
+  return GV;
+}
+
+GlobalVariable *DXContainerGlobals::buildInputSingature(Module &M,
+                                                        Constant *Content) {
+  return buildContainerGlobal(M, Content, "dx.isg1", "ISG1");
+}
+GlobalVariable *DXContainerGlobals::buildOutputSingature(Module &M,
+                                                         Constant *Content) {
+  return buildContainerGlobal(M, Content, "dx.osg1", "OSG1");
+}
+
+void DXContainerGlobals::addSingature(Module &M,
+                                      SmallVector<GlobalValue *> &Globals,
+                                      Triple &TT) {
+  dxbc::ProgramSignatureHeader Sig;
+  Sig.ParamCount = 0;
+  Sig.FirstParamOffset = sizeof(dxbc::ProgramSignatureHeader);
+  Type *Int32Ty = Type::getInt32Ty(M.getContext());
+  Constant *InputSig = nullptr;
+  Constant *OutputSig = nullptr;
+  switch (TT.getEnvironment()) {
+  case Triple::EnvironmentType::Compute:
+    InputSig = ConstantStruct::get(
+        StructType::get(Int32Ty, Int32Ty),
+        {ConstantInt::get(M.getContext(), APInt(32, Sig.ParamCount)),
+         ConstantInt::get(M.getContext(), APInt(32, Sig.FirstParamOffset))});
+    OutputSig = InputSig;
+    break;
+    // FIXME: support graphics shader.
+    //  see issue https://github.com/llvm/llvm-project/issues/90504.
+  default:
+    return;
+  }
+  Globals.emplace_back(buildInputSingature(M, InputSig));
+  Globals.emplace_back(buildOutputSingature(M, OutputSig));
+}
+
 char DXContainerGlobals::ID = 0;
 INITIALIZE_PASS_BEGIN(DXContainerGlobals, "dxil-globals",
                       "DXContainer Global Emitter", false, true)
diff --git a/llvm/test/CodeGen/DirectX/ContainerData/EmptySignature.ll b/llvm/test/CodeGen/DirectX/ContainerData/EmptySignature.ll
new file mode 100644
index 00000000000000..bae50606a6b2ac
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/ContainerData/EmptySignature.ll
@@ -0,0 +1,25 @@
+; 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.isg1 = private constant { i32, i32 } { i32 0, i32 8 }, section "ISG1", align 4
+; CHECK: @dx.osg1 = private constant { i32, i32 } { i32 0, i32 8 }, section "OSG1", 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:            ISG1
+; DXC-NEXT:   Size:            8
+; DXC-NEXT:   Signature:
+; DXC-NEXT:     Parameters:      []
+; DXC: - Name:            OSG1
+; DXC-NEXT:   Size:            8
+; DXC-NEXT:   Signature:
+; DXC-NEXT:     Parameters:      []



More information about the llvm-commits mailing list