Skip to content

Commit 9b59d01

Browse files
author
joaosaffran
committed
adding support for root constants in metadata generation
1 parent 82a31fa commit 9b59d01

File tree

5 files changed

+182
-5
lines changed

5 files changed

+182
-5
lines changed

llvm/lib/Target/DirectX/DXILRootSignature.cpp

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ static bool reportError(LLVMContext *Ctx, Twine Message,
4040
return true;
4141
}
4242

43+
static bool reportValueError(LLVMContext *Ctx, Twine ParamName, uint32_t Value,
44+
DiagnosticSeverity Severity = DS_Error) {
45+
Ctx->diagnose(DiagnosticInfoGeneric(
46+
"Invalid value for " + ParamName + ": " + Twine(Value), Severity));
47+
return true;
48+
}
49+
4350
static bool parseRootFlags(LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD,
4451
MDNode *RootFlagNode) {
4552

@@ -52,6 +59,45 @@ static bool parseRootFlags(LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD,
5259
return false;
5360
}
5461

62+
static bool extractMdValue(uint32_t &Value, MDNode *Node, unsigned int OpId) {
63+
64+
auto *CI = mdconst::extract<ConstantInt>(Node->getOperand(OpId));
65+
if (CI == nullptr)
66+
return true;
67+
68+
Value = CI->getZExtValue();
69+
return false;
70+
}
71+
72+
static bool parseRootConstants(LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD,
73+
MDNode *RootFlagNode) {
74+
75+
if (RootFlagNode->getNumOperands() != 5)
76+
return reportError(Ctx, "Invalid format for RootConstants Element");
77+
78+
mcdxbc::RootParameter NewParameter;
79+
NewParameter.Header.ParameterType = dxbc::RootParameterType::Constants32Bit;
80+
81+
uint32_t SV;
82+
if (extractMdValue(SV, RootFlagNode, 1))
83+
return reportError(Ctx, "Invalid value for ShaderVisibility");
84+
85+
NewParameter.Header.ShaderVisibility = (dxbc::ShaderVisibility)SV;
86+
87+
if (extractMdValue(NewParameter.Constants.ShaderRegister, RootFlagNode, 2))
88+
return reportError(Ctx, "Invalid value for ShaderRegister");
89+
90+
if (extractMdValue(NewParameter.Constants.RegisterSpace, RootFlagNode, 3))
91+
return reportError(Ctx, "Invalid value for RegisterSpace");
92+
93+
if (extractMdValue(NewParameter.Constants.Num32BitValues, RootFlagNode, 4))
94+
return reportError(Ctx, "Invalid value for Num32BitValues");
95+
96+
RSD.Parameters.push_back(NewParameter);
97+
98+
return false;
99+
}
100+
55101
static bool parseRootSignatureElement(LLVMContext *Ctx,
56102
mcdxbc::RootSignatureDesc &RSD,
57103
MDNode *Element) {
@@ -62,12 +108,16 @@ static bool parseRootSignatureElement(LLVMContext *Ctx,
62108
RootSignatureElementKind ElementKind =
63109
StringSwitch<RootSignatureElementKind>(ElementText->getString())
64110
.Case("RootFlags", RootSignatureElementKind::RootFlags)
111+
.Case("RootConstants", RootSignatureElementKind::RootConstants)
65112
.Default(RootSignatureElementKind::Error);
66113

67114
switch (ElementKind) {
68115

69116
case RootSignatureElementKind::RootFlags:
70117
return parseRootFlags(Ctx, RSD, Element);
118+
case RootSignatureElementKind::RootConstants:
119+
return parseRootConstants(Ctx, RSD, Element);
120+
break;
71121
case RootSignatureElementKind::Error:
72122
return reportError(Ctx, "Invalid Root Signature Element: " +
73123
ElementText->getString());
@@ -94,10 +144,56 @@ static bool parse(LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD,
94144

95145
static bool verifyRootFlag(uint32_t Flags) { return (Flags & ~0xfff) == 0; }
96146

147+
static bool verifyShaderVisibility(dxbc::ShaderVisibility Flags) {
148+
switch (Flags) {
149+
150+
case dxbc::ShaderVisibility::All:
151+
case dxbc::ShaderVisibility::Vertex:
152+
case dxbc::ShaderVisibility::Hull:
153+
case dxbc::ShaderVisibility::Domain:
154+
case dxbc::ShaderVisibility::Geometry:
155+
case dxbc::ShaderVisibility::Pixel:
156+
case dxbc::ShaderVisibility::Amplification:
157+
case dxbc::ShaderVisibility::Mesh:
158+
return true;
159+
}
160+
161+
return false;
162+
}
163+
164+
static bool verifyParameterType(dxbc::RootParameterType Flags) {
165+
switch (Flags) {
166+
case dxbc::RootParameterType::Constants32Bit:
167+
return true;
168+
}
169+
170+
return false;
171+
}
172+
173+
static bool verifyVersion(uint32_t Version) {
174+
return (Version == 1 || Version == 2);
175+
}
176+
97177
static bool validate(LLVMContext *Ctx, const mcdxbc::RootSignatureDesc &RSD) {
178+
179+
if (!verifyVersion(RSD.Header.Version)) {
180+
return reportValueError(Ctx, "Version", RSD.Header.Version);
181+
}
182+
98183
if (!verifyRootFlag(RSD.Header.Flags)) {
99-
return reportError(Ctx, "Invalid Root Signature flag value");
184+
return reportValueError(Ctx, "RootFlags", RSD.Header.Flags);
185+
}
186+
187+
for (const auto &P : RSD.Parameters) {
188+
if (!verifyShaderVisibility(P.Header.ShaderVisibility))
189+
return reportValueError(Ctx, "ShaderVisibility",
190+
(uint32_t)P.Header.ShaderVisibility);
191+
192+
if (!verifyParameterType(P.Header.ParameterType))
193+
return reportValueError(Ctx, "ParameterType",
194+
(uint32_t)P.Header.ParameterType);
100195
}
196+
101197
return false;
102198
}
103199

@@ -211,6 +307,28 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M,
211307
OS << indent(Space) << "NumStaticSamplers: " << 0 << ":\n";
212308
OS << indent(Space) << "StaticSamplersOffset: "
213309
<< sizeof(RS.Header) + RS.Parameters.size_in_bytes() << ":\n";
310+
311+
Space++;
312+
for (auto const &P : RS.Parameters) {
313+
OS << indent(Space) << "Parameter Type: " << &P.Header.ParameterType
314+
<< ":\n";
315+
OS << indent(Space) << "Shader Visibility: " << &P.Header.ShaderVisibility
316+
<< ":\n";
317+
OS << indent(Space) << "Parameter Offset: " << &P.Header.ParameterOffset
318+
<< ":\n";
319+
switch (P.Header.ParameterType) {
320+
case dxbc::RootParameterType::Constants32Bit:
321+
OS << indent(Space) << "Register Space: " << &P.Constants.RegisterSpace
322+
<< ":\n";
323+
OS << indent(Space)
324+
<< "Shader Register: " << &P.Constants.ShaderRegister << ":\n";
325+
OS << indent(Space)
326+
<< "Num 32 Bit Values: " << &P.Constants.Num32BitValues << ":\n";
327+
break;
328+
}
329+
}
330+
Space--;
331+
214332
Space--;
215333
// end root signature header
216334
}

llvm/lib/Target/DirectX/DXILRootSignature.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
namespace llvm {
2525
namespace dxil {
2626

27-
enum class RootSignatureElementKind { Error = 0, RootFlags = 1 };
27+
enum class RootSignatureElementKind {
28+
Error = 0,
29+
RootFlags = 1,
30+
RootConstants = 2
31+
};
2832
class RootSignatureAnalysis : public AnalysisInfoMixin<RootSignatureAnalysis> {
2933
friend AnalysisInfoMixin<RootSignatureAnalysis>;
3034
static AnalysisKey Key;

llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Flags-Validation-Error.ll

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
; RUN: not opt -passes='print<dxil-root-signature>' %s -S -o - 2>&1 | FileCheck %s
22

3-
; CHECK: error: Invalid Root Signature flag value
3+
; CHECK: error: Invalid value for ShaderVisibility: 255
44
; CHECK-NOT: Root Signature Definitions
55

66
target triple = "dxil-unknown-shadermodel6.0-compute"
@@ -16,5 +16,6 @@ attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }
1616

1717
!dx.rootsignatures = !{!2} ; list of function/root signature pairs
1818
!2 = !{ ptr @main, !3 } ; function, root signature
19-
!3 = !{ !4 } ; list of root signature elements
20-
!4 = !{ !"RootFlags", i32 2147487744 } ; 1 = allow_input_assembler_input_layout
19+
!3 = !{ !4, !5 } ; list of root signature elements
20+
!4 = !{ !"RootFlags", i32 1 } ; 1 = allow_input_assembler_input_layout
21+
!5 = !{ !"RootConstants", i32 255, i32 1, i32 2, i32 3 }
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 = !{ !4, !5 } ; list of root signature elements
18+
!4 = !{ !"RootFlags", i32 1 } ; 1 = allow_input_assembler_input_layout
19+
!5 = !{ !"RootConstants", i32 0, i32 1, i32 2, i32 3 }
20+
21+
; DXC: - Name: RTS0
22+
; DXC-NEXT: Size: 48
23+
; DXC-NEXT: RootSignature:
24+
; DXC-NEXT: Version: 2
25+
; DXC-NEXT: NumStaticSamplers: 0
26+
; DXC-NEXT: StaticSamplersOffset: 0
27+
; DXC-NEXT: Parameters:
28+
; DXC-NEXT: - ParameterType: Constants32Bit
29+
; DXC-NEXT: ShaderVisibility: All
30+
; DXC-NEXT: Constants:
31+
; DXC-NEXT: Num32BitValues: 3
32+
; DXC-NEXT: RegisterSpace: 2
33+
; DXC-NEXT: ShaderRegister: 1
34+
; DXC-NEXT: AllowInputAssemblerInputLayout: true
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
; RUN: not opt -passes='print<dxil-root-signature>' %s -S -o - 2>&1 | FileCheck %s
2+
3+
; CHECK: error: Invalid value for RootFlags: 2147487744
4+
; CHECK-NOT: Root Signature Definitions
5+
6+
target triple = "dxil-unknown-shadermodel6.0-compute"
7+
8+
9+
define void @main() #0 {
10+
entry:
11+
ret void
12+
}
13+
14+
attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }
15+
16+
17+
!dx.rootsignatures = !{!2} ; list of function/root signature pairs
18+
!2 = !{ ptr @main, !3 } ; function, root signature
19+
!3 = !{ !4 } ; list of root signature elements
20+
!4 = !{ !"RootFlags", i32 2147487744 } ; 1 = allow_input_assembler_input_layout

0 commit comments

Comments
 (0)