[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 13:25:21 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/2] [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/2] 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
More information about the llvm-commits
mailing list