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

Xiang Li via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 30 08:56:11 PDT 2024


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

>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 1/6] [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:      []

>From bca032c305b19459c08345b256e656af8a922bcc Mon Sep 17 00:00:00 2001
From: Xiang Li <python3kgae at outlook.com>
Date: Mon, 29 Apr 2024 16:25:06 -0400
Subject: [PATCH 2/6] Use llvm::mcdxbc::Signature.

---
 .../lib/Target/DirectX/DXContainerGlobals.cpp | 51 ++++++++-----------
 .../DirectX/ContainerData/EmptySignature.ll   |  5 +-
 2 files changed, 24 insertions(+), 32 deletions(-)

diff --git a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
index 4af4c506b630f4..f0a92dcbb9349c 100644
--- a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
+++ b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
@@ -18,12 +18,14 @@
 #include "llvm/CodeGen/Passes.h"
 #include "llvm/IR/Constants.h"
 #include "llvm/InitializePasses.h"
+#include "llvm/MC/DXContainerPSVInfo.h"
 #include "llvm/Pass.h"
 #include "llvm/Support/MD5.h"
 #include "llvm/Transforms/Utils/ModuleUtils.h"
 
 using namespace llvm;
 using namespace llvm::dxil;
+using namespace llvm::mcdxbc;
 
 namespace {
 class DXContainerGlobals : public llvm::ModulePass {
@@ -32,8 +34,8 @@ class DXContainerGlobals : public llvm::ModulePass {
                                        StringRef Name, StringRef SectionName);
   GlobalVariable *getFeatureFlags(Module &M);
   GlobalVariable *computeShaderHash(Module &M);
-  GlobalVariable *buildInputSingature(Module &M, Constant *Content);
-  GlobalVariable *buildOutputSingature(Module &M, Constant *Content);
+  GlobalVariable *buildSingature(Module &M, Signature &Sig, StringRef Name,
+                                 StringRef SectionName);
   void addSingature(Module &M, SmallVector<GlobalValue *> &Globals, Triple &TT);
 
 public:
@@ -119,39 +121,28 @@ GlobalVariable *DXContainerGlobals::buildContainerGlobal(
   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");
+GlobalVariable *DXContainerGlobals::buildSingature(Module &M, Signature &Sig,
+                                                   StringRef Name,
+                                                   StringRef SectionName) {
+  std::string Data;
+  raw_string_ostream OS(Data);
+  Sig.write(OS);
+  OS.flush();
+  Constant *Constant =
+      ConstantDataArray::getString(M.getContext(), Data, /*AddNull*/ false);
+  return buildContainerGlobal(M, Constant, Name, SectionName);
 }
 
 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));
+  Signature InputSig;
+  Signature OutputSig;
+  // FIXME: support graphics shader.
+  //  see issue https://github.com/llvm/llvm-project/issues/90504.
+
+  Globals.emplace_back(buildSingature(M, InputSig, "dx.isg1", "ISG1"));
+  Globals.emplace_back(buildSingature(M, OutputSig, "dx.osg1", "OSG1"));
 }
 
 char DXContainerGlobals::ID = 0;
diff --git a/llvm/test/CodeGen/DirectX/ContainerData/EmptySignature.ll b/llvm/test/CodeGen/DirectX/ContainerData/EmptySignature.ll
index bae50606a6b2ac..796f7942a80b12 100644
--- a/llvm/test/CodeGen/DirectX/ContainerData/EmptySignature.ll
+++ b/llvm/test/CodeGen/DirectX/ContainerData/EmptySignature.ll
@@ -2,8 +2,9 @@
 ; 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
+; CHECK: @dx.isg1 = private constant [8 x i8] c"\00\00\00\00\08\00\00\00", section "ISG1", align 4
+; CHECK: @dx.osg1 = private constant [8 x i8] c"\00\00\00\00\08\00\00\00", section "OSG1", align 4
+
 define void @main() #0 {
 entry:
   ret void

>From de055b134afb661cd4d841d93ce5caa5a9b06c2a Mon Sep 17 00:00:00 2001
From: Xiang Li <python3kgae at outlook.com>
Date: Mon, 29 Apr 2024 16:37:15 -0400
Subject: [PATCH 3/6] Change to raw_svector_ostream.

---
 llvm/lib/Target/DirectX/DXContainerGlobals.cpp | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
index f0a92dcbb9349c..14e8633653a434 100644
--- a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
+++ b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
@@ -124,10 +124,9 @@ GlobalVariable *DXContainerGlobals::buildContainerGlobal(
 GlobalVariable *DXContainerGlobals::buildSingature(Module &M, Signature &Sig,
                                                    StringRef Name,
                                                    StringRef SectionName) {
-  std::string Data;
-  raw_string_ostream OS(Data);
+  SmallString<256> Data;
+  raw_svector_ostream OS(Data);
   Sig.write(OS);
-  OS.flush();
   Constant *Constant =
       ConstantDataArray::getString(M.getContext(), Data, /*AddNull*/ false);
   return buildContainerGlobal(M, Constant, Name, SectionName);

>From 4a4f3924c34736fe83a2f130fef702bc6d89d54a Mon Sep 17 00:00:00 2001
From: Xiang Li <python3kgae at outlook.com>
Date: Mon, 29 Apr 2024 17:29:56 -0400
Subject: [PATCH 4/6] Remove Triple which is for hull shader.

---
 llvm/lib/Target/DirectX/DXContainerGlobals.cpp | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
index 14e8633653a434..ea2826798594ab 100644
--- a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
+++ b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
@@ -36,7 +36,7 @@ class DXContainerGlobals : public llvm::ModulePass {
   GlobalVariable *computeShaderHash(Module &M);
   GlobalVariable *buildSingature(Module &M, Signature &Sig, StringRef Name,
                                  StringRef SectionName);
-  void addSingature(Module &M, SmallVector<GlobalValue *> &Globals, Triple &TT);
+  void addSingature(Module &M, SmallVector<GlobalValue *> &Globals);
 
 public:
   static char ID; // Pass identification, replacement for typeid
@@ -62,8 +62,7 @@ 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);
+  addSingature(M, Globals);
   appendToCompilerUsed(M, Globals);
   return true;
 }
@@ -133,8 +132,7 @@ GlobalVariable *DXContainerGlobals::buildSingature(Module &M, Signature &Sig,
 }
 
 void DXContainerGlobals::addSingature(Module &M,
-                                      SmallVector<GlobalValue *> &Globals,
-                                      Triple &TT) {
+                                      SmallVector<GlobalValue *> &Globals) {
   Signature InputSig;
   Signature OutputSig;
   // FIXME: support graphics shader.

>From 25c25413849a7425e3c1db9779db947418f19226 Mon Sep 17 00:00:00 2001
From: Xiang Li <python3kgae at outlook.com>
Date: Mon, 29 Apr 2024 17:34:52 -0400
Subject: [PATCH 5/6] Code cleanup.

---
 .../lib/Target/DirectX/DXContainerGlobals.cpp | 19 +++++--------------
 1 file changed, 5 insertions(+), 14 deletions(-)

diff --git a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
index ea2826798594ab..b4a3d7ae549101 100644
--- a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
+++ b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
@@ -75,12 +75,7 @@ GlobalVariable *DXContainerGlobals::getFeatureFlags(Module &M) {
 
   Constant *FeatureFlagsConstant =
       ConstantInt::get(M.getContext(), APInt(64, FeatureFlags));
-  auto *GV = new llvm::GlobalVariable(M, FeatureFlagsConstant->getType(), true,
-                                      GlobalValue::PrivateLinkage,
-                                      FeatureFlagsConstant, "dx.sfi0");
-  GV->setSection("SFI0");
-  GV->setAlignment(Align(4));
-  return GV;
+  return buildContainerGlobal(M, FeatureFlagsConstant, "dx.sfi0", "SFI0");
 }
 
 GlobalVariable *DXContainerGlobals::computeShaderHash(Module &M) {
@@ -103,12 +98,7 @@ GlobalVariable *DXContainerGlobals::computeShaderHash(Module &M) {
 
   Constant *ModuleConstant =
       ConstantDataArray::get(M.getContext(), arrayRefFromStringRef(Data));
-  auto *GV = new llvm::GlobalVariable(M, ModuleConstant->getType(), true,
-                                      GlobalValue::PrivateLinkage,
-                                      ModuleConstant, "dx.hash");
-  GV->setSection("HASH");
-  GV->setAlignment(Align(4));
-  return GV;
+  return buildContainerGlobal(M, ModuleConstant, "dx.hash", "HASH");
 }
 
 GlobalVariable *DXContainerGlobals::buildContainerGlobal(
@@ -133,12 +123,13 @@ GlobalVariable *DXContainerGlobals::buildSingature(Module &M, Signature &Sig,
 
 void DXContainerGlobals::addSingature(Module &M,
                                       SmallVector<GlobalValue *> &Globals) {
-  Signature InputSig;
-  Signature OutputSig;
   // FIXME: support graphics shader.
   //  see issue https://github.com/llvm/llvm-project/issues/90504.
 
+  Signature InputSig;
   Globals.emplace_back(buildSingature(M, InputSig, "dx.isg1", "ISG1"));
+
+  Signature OutputSig;
   Globals.emplace_back(buildSingature(M, OutputSig, "dx.osg1", "OSG1"));
 }
 

>From b2169103d52a09a0c98cf613b9cd8d23388ac254 Mon Sep 17 00:00:00 2001
From: Xiang Li <python3kgae at outlook.com>
Date: Tue, 30 Apr 2024 11:55:54 -0400
Subject: [PATCH 6/6] Fix typo.

---
 llvm/lib/Target/DirectX/DXContainerGlobals.cpp | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
index b4a3d7ae549101..67e04c212a6921 100644
--- a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
+++ b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
@@ -34,9 +34,9 @@ class DXContainerGlobals : public llvm::ModulePass {
                                        StringRef Name, StringRef SectionName);
   GlobalVariable *getFeatureFlags(Module &M);
   GlobalVariable *computeShaderHash(Module &M);
-  GlobalVariable *buildSingature(Module &M, Signature &Sig, StringRef Name,
+  GlobalVariable *buildSignature(Module &M, Signature &Sig, StringRef Name,
                                  StringRef SectionName);
-  void addSingature(Module &M, SmallVector<GlobalValue *> &Globals);
+  void addSignature(Module &M, SmallVector<GlobalValue *> &Globals);
 
 public:
   static char ID; // Pass identification, replacement for typeid
@@ -62,7 +62,7 @@ bool DXContainerGlobals::runOnModule(Module &M) {
   llvm::SmallVector<GlobalValue *> Globals;
   Globals.push_back(getFeatureFlags(M));
   Globals.push_back(computeShaderHash(M));
-  addSingature(M, Globals);
+  addSignature(M, Globals);
   appendToCompilerUsed(M, Globals);
   return true;
 }
@@ -110,7 +110,7 @@ GlobalVariable *DXContainerGlobals::buildContainerGlobal(
   return GV;
 }
 
-GlobalVariable *DXContainerGlobals::buildSingature(Module &M, Signature &Sig,
+GlobalVariable *DXContainerGlobals::buildSignature(Module &M, Signature &Sig,
                                                    StringRef Name,
                                                    StringRef SectionName) {
   SmallString<256> Data;
@@ -121,16 +121,16 @@ GlobalVariable *DXContainerGlobals::buildSingature(Module &M, Signature &Sig,
   return buildContainerGlobal(M, Constant, Name, SectionName);
 }
 
-void DXContainerGlobals::addSingature(Module &M,
+void DXContainerGlobals::addSignature(Module &M,
                                       SmallVector<GlobalValue *> &Globals) {
   // FIXME: support graphics shader.
   //  see issue https://github.com/llvm/llvm-project/issues/90504.
 
   Signature InputSig;
-  Globals.emplace_back(buildSingature(M, InputSig, "dx.isg1", "ISG1"));
+  Globals.emplace_back(buildSignature(M, InputSig, "dx.isg1", "ISG1"));
 
   Signature OutputSig;
-  Globals.emplace_back(buildSingature(M, OutputSig, "dx.osg1", "OSG1"));
+  Globals.emplace_back(buildSignature(M, OutputSig, "dx.osg1", "OSG1"));
 }
 
 char DXContainerGlobals::ID = 0;



More information about the llvm-commits mailing list