Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DirectX backend] generate ISG1, OSG1 part for compute shader #90508

Merged
merged 6 commits into from
May 1, 2024

Conversation

python3kgae
Copy link
Contributor

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

Fixes #88778

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

Fixes llvm#88778
@llvmbot
Copy link
Collaborator

llvmbot commented Apr 29, 2024

@llvm/pr-subscribers-backend-directx

Author: Xiang Li (python3kgae)

Changes

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

Fixes #88778


Full diff: https://github.com/llvm/llvm-project/pull/90508.diff

2 Files Affected:

  • (modified) llvm/lib/Target/DirectX/DXContainerGlobals.cpp (+51-1)
  • (added) llvm/test/CodeGen/DirectX/ContainerData/EmptySignature.ll (+25)
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:      []


void DXContainerGlobals::addSingature(Module &M,
SmallVector<GlobalValue *> &Globals,
Triple &TT) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TT is unused (and also reachable from the module)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

Comment on lines 138 to 144
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"));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: It's clearer to read what is going on here if we group creating the signature with emitting it.

Suggested change
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"));
// 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"));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

Comment on lines 37 to 39
GlobalVariable *buildSingature(Module &M, Signature &Sig, StringRef Name,
StringRef SectionName);
void addSingature(Module &M, SmallVector<GlobalValue *> &Globals);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
GlobalVariable *buildSingature(Module &M, Signature &Sig, StringRef Name,
StringRef SectionName);
void addSingature(Module &M, SmallVector<GlobalValue *> &Globals);
GlobalVariable *buildSignature(Module &M, Signature &Sig, StringRef Name,
StringRef SectionName);
void addSignature(Module &M, SmallVector<GlobalValue *> &Globals);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Copy link
Contributor

@damyanp damyanp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be good to get the "singature" typos fixed.

@python3kgae python3kgae merged commit a764f49 into llvm:main May 1, 2024
4 checks passed
@python3kgae python3kgae deleted the empty_io_part branch May 1, 2024 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

[DirectX backend] generate ISG1, OSG1 parts for DXContainer
4 participants