Skip to content

Commit f875555

Browse files
author
joaosaffran
committed
adding support for root descriptors
1 parent 8ff4845 commit f875555

File tree

3 files changed

+99
-2
lines changed

3 files changed

+99
-2
lines changed

llvm/lib/Target/DirectX/DXILRootSignature.cpp

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ static std::optional<uint32_t> extractMdIntValue(MDNode *Node,
5555
return std::nullopt;
5656
}
5757

58+
static std::optional<StringRef> extractMdStringValue(MDNode *Node,
59+
unsigned int OpId) {
60+
MDString *NodeText = cast<MDString>(Node->getOperand(OpId));
61+
if (NodeText == nullptr)
62+
return std::nullopt;
63+
return NodeText->getString();
64+
}
65+
5866
static bool parseRootFlags(LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD,
5967
MDNode *RootFlagNode) {
6068

@@ -105,6 +113,56 @@ static bool parseRootConstants(LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD,
105113
return false;
106114
}
107115

116+
static bool parseRootDescriptors(LLVMContext *Ctx,
117+
mcdxbc::RootSignatureDesc &RSD,
118+
MDNode *RootDescriptorNode) {
119+
120+
if (RootDescriptorNode->getNumOperands() != 5)
121+
return reportError(Ctx, "Invalid format for RootConstants Element");
122+
123+
std::optional<StringRef> ElementText =
124+
extractMdStringValue(RootDescriptorNode, 0);
125+
assert(!ElementText->empty());
126+
127+
dxbc::RootParameterHeader Header;
128+
Header.ParameterType =
129+
StringSwitch<uint32_t>(*ElementText)
130+
.Case("RootCBV", llvm::to_underlying(dxbc::RootParameterType::CBV))
131+
.Case("RootSRV", llvm::to_underlying(dxbc::RootParameterType::SRV))
132+
.Case("RootUAV", llvm::to_underlying(dxbc::RootParameterType::UAV));
133+
134+
if (std::optional<uint32_t> Val = extractMdIntValue(RootDescriptorNode, 1))
135+
Header.ShaderVisibility = *Val;
136+
else
137+
return reportError(Ctx, "Invalid value for ShaderVisibility");
138+
139+
dxbc::RTS0::v1::RootDescriptor Descriptor;
140+
if (std::optional<uint32_t> Val = extractMdIntValue(RootDescriptorNode, 2))
141+
Descriptor.ShaderRegister = *Val;
142+
else
143+
return reportError(Ctx, "Invalid value for ShaderRegister");
144+
145+
if (std::optional<uint32_t> Val = extractMdIntValue(RootDescriptorNode, 3))
146+
Descriptor.RegisterSpace = *Val;
147+
else
148+
return reportError(Ctx, "Invalid value for RegisterSpace");
149+
150+
if (RSD.Version == 1) {
151+
RSD.ParametersContainer.addParameter(Header, Descriptor);
152+
return false;
153+
}
154+
assert(RSD.Version > 1);
155+
dxbc::RTS0::v2::RootDescriptor DescriptorV2(Descriptor);
156+
157+
if (std::optional<uint32_t> Val = extractMdIntValue(RootDescriptorNode, 4))
158+
DescriptorV2.Flags = *Val;
159+
else
160+
return reportError(Ctx, "Invalid value for Root Descriptor Flags");
161+
162+
RSD.ParametersContainer.addParameter(Header, DescriptorV2);
163+
return false;
164+
}
165+
108166
static bool parseRootSignatureElement(LLVMContext *Ctx,
109167
mcdxbc::RootSignatureDesc &RSD,
110168
MDNode *Element) {
@@ -116,6 +174,9 @@ static bool parseRootSignatureElement(LLVMContext *Ctx,
116174
StringSwitch<RootSignatureElementKind>(ElementText->getString())
117175
.Case("RootFlags", RootSignatureElementKind::RootFlags)
118176
.Case("RootConstants", RootSignatureElementKind::RootConstants)
177+
.Case("RootCBV", RootSignatureElementKind::RootDescriptors)
178+
.Case("RootSRV", RootSignatureElementKind::RootDescriptors)
179+
.Case("RootUAV", RootSignatureElementKind::RootDescriptors)
119180
.Default(RootSignatureElementKind::Error);
120181

121182
switch (ElementKind) {
@@ -124,7 +185,8 @@ static bool parseRootSignatureElement(LLVMContext *Ctx,
124185
return parseRootFlags(Ctx, RSD, Element);
125186
case RootSignatureElementKind::RootConstants:
126187
return parseRootConstants(Ctx, RSD, Element);
127-
break;
188+
case RootSignatureElementKind::RootDescriptors:
189+
return parseRootDescriptors(Ctx, RSD, Element);
128190
case RootSignatureElementKind::Error:
129191
return reportError(Ctx, "Invalid Root Signature Element: " +
130192
ElementText->getString());

llvm/lib/Target/DirectX/DXILRootSignature.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ namespace dxil {
2727
enum class RootSignatureElementKind {
2828
Error = 0,
2929
RootFlags = 1,
30-
RootConstants = 2
30+
RootConstants = 2,
31+
RootDescriptors = 3
3132
};
3233
class RootSignatureAnalysis : public AnalysisInfoMixin<RootSignatureAnalysis> {
3334
friend AnalysisInfoMixin<RootSignatureAnalysis>;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
4+
target triple = "dxil-unknown-shadermodel6.0-compute"
5+
6+
; CHECK: @dx.rts0 = private constant [48 x i8] c"{{.*}}", section "RTS0", align 4
7+
8+
define void @main() #0 {
9+
entry:
10+
ret void
11+
}
12+
attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }
13+
14+
15+
!dx.rootsignatures = !{!2} ; list of function/root signature pairs
16+
!2 = !{ ptr @main, !3 } ; function, root signature
17+
!3 = !{ !5 } ; list of root signature elements
18+
!5 = !{ !"RootCBV", i32 0, i32 1, i32 2, i32 3 }
19+
20+
; DXC: - Name: RTS0
21+
; DXC-NEXT: Size: 48
22+
; DXC-NEXT: RootSignature:
23+
; DXC-NEXT: Version: 2
24+
; DXC-NEXT: NumRootParameters: 1
25+
; DXC-NEXT: RootParametersOffset: 24
26+
; DXC-NEXT: NumStaticSamplers: 0
27+
; DXC-NEXT: StaticSamplersOffset: 0
28+
; DXC-NEXT: Parameters:
29+
; DXC-NEXT: - ParameterType: 2
30+
; DXC-NEXT: ShaderVisibility: 0
31+
; DXC-NEXT: Descriptor:
32+
; DXC-NEXT: RegisterSpace: 2
33+
; DXC-NEXT: ShaderRegister: 1
34+
; DXC: DATA_VOLATILE: true

0 commit comments

Comments
 (0)