Skip to content

Commit d745a77

Browse files
committed
[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 llvm#88778
1 parent 91f251c commit d745a77

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

llvm/lib/Target/DirectX/DXContainerGlobals.cpp

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@ using namespace llvm::dxil;
2828
namespace {
2929
class DXContainerGlobals : public llvm::ModulePass {
3030

31+
GlobalVariable *buildContainerGlobal(Module &M, Constant *Content,
32+
StringRef Name, StringRef SectionName);
3133
GlobalVariable *getFeatureFlags(Module &M);
3234
GlobalVariable *computeShaderHash(Module &M);
35+
GlobalVariable *buildInputSingature(Module &M, Constant *Content);
36+
GlobalVariable *buildOutputSingature(Module &M, Constant *Content);
37+
void addSingature(Module &M, SmallVector<GlobalValue *> &Globals, Triple &TT);
3338

3439
public:
3540
static char ID; // Pass identification, replacement for typeid
@@ -55,7 +60,8 @@ bool DXContainerGlobals::runOnModule(Module &M) {
5560
llvm::SmallVector<GlobalValue *> Globals;
5661
Globals.push_back(getFeatureFlags(M));
5762
Globals.push_back(computeShaderHash(M));
58-
63+
Triple TT(M.getTargetTriple());
64+
addSingature(M, Globals, TT);
5965
appendToCompilerUsed(M, Globals);
6066
return true;
6167
}
@@ -104,6 +110,50 @@ GlobalVariable *DXContainerGlobals::computeShaderHash(Module &M) {
104110
return GV;
105111
}
106112

113+
GlobalVariable *DXContainerGlobals::buildContainerGlobal(
114+
Module &M, Constant *Content, StringRef Name, StringRef SectionName) {
115+
auto *GV = new llvm::GlobalVariable(
116+
M, Content->getType(), true, GlobalValue::PrivateLinkage, Content, Name);
117+
GV->setSection(SectionName);
118+
GV->setAlignment(Align(4));
119+
return GV;
120+
}
121+
122+
GlobalVariable *DXContainerGlobals::buildInputSingature(Module &M,
123+
Constant *Content) {
124+
return buildContainerGlobal(M, Content, "dx.isg1", "ISG1");
125+
}
126+
GlobalVariable *DXContainerGlobals::buildOutputSingature(Module &M,
127+
Constant *Content) {
128+
return buildContainerGlobal(M, Content, "dx.osg1", "OSG1");
129+
}
130+
131+
void DXContainerGlobals::addSingature(Module &M,
132+
SmallVector<GlobalValue *> &Globals,
133+
Triple &TT) {
134+
dxbc::ProgramSignatureHeader Sig;
135+
Sig.ParamCount = 0;
136+
Sig.FirstParamOffset = sizeof(dxbc::ProgramSignatureHeader);
137+
Type *Int32Ty = Type::getInt32Ty(M.getContext());
138+
Constant *InputSig = nullptr;
139+
Constant *OutputSig = nullptr;
140+
switch (TT.getEnvironment()) {
141+
case Triple::EnvironmentType::Compute:
142+
InputSig = ConstantStruct::get(
143+
StructType::get(Int32Ty, Int32Ty),
144+
{ConstantInt::get(M.getContext(), APInt(32, Sig.ParamCount)),
145+
ConstantInt::get(M.getContext(), APInt(32, Sig.FirstParamOffset))});
146+
OutputSig = InputSig;
147+
break;
148+
// FIXME: support graphics shader.
149+
// see issue https://github.com/llvm/llvm-project/issues/90504.
150+
default:
151+
return;
152+
}
153+
Globals.emplace_back(buildInputSingature(M, InputSig));
154+
Globals.emplace_back(buildOutputSingature(M, OutputSig));
155+
}
156+
107157
char DXContainerGlobals::ID = 0;
108158
INITIALIZE_PASS_BEGIN(DXContainerGlobals, "dxil-globals",
109159
"DXContainer Global Emitter", false, true)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
; RUN: opt %s -dxil-embed -dxil-globals -S -o - | FileCheck %s
2+
; RUN: llc %s --filetype=obj -o - | obj2yaml | FileCheck %s --check-prefix=DXC
3+
target triple = "dxil-unknown-shadermodel6.0-compute"
4+
5+
; CHECK: @dx.isg1 = private constant { i32, i32 } { i32 0, i32 8 }, section "ISG1", align 4
6+
; CHECK: @dx.osg1 = private constant { i32, i32 } { i32 0, i32 8 }, section "OSG1", align 4
7+
define void @main() #0 {
8+
entry:
9+
ret void
10+
}
11+
12+
attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }
13+
14+
!dx.valver = !{!0}
15+
16+
!0 = !{i32 1, i32 7}
17+
18+
; DXC: - Name: ISG1
19+
; DXC-NEXT: Size: 8
20+
; DXC-NEXT: Signature:
21+
; DXC-NEXT: Parameters: []
22+
; DXC: - Name: OSG1
23+
; DXC-NEXT: Size: 8
24+
; DXC-NEXT: Signature:
25+
; DXC-NEXT: Parameters: []

0 commit comments

Comments
 (0)