29
29
using namespace llvm ;
30
30
using namespace llvm ::dxil;
31
31
32
- LLVMContext *Ctx;
33
-
34
- static bool reportError (Twine Message, DiagnosticSeverity Severity = DS_Error) {
32
+ static bool reportError (LLVMContext *Ctx, Twine Message,
33
+ DiagnosticSeverity Severity = DS_Error) {
35
34
Ctx->diagnose (DiagnosticInfoGeneric (Message, Severity));
36
35
return true ;
37
36
}
38
37
39
- static bool parseRootFlags (ModuleRootSignature *MRS, MDNode *RootFlagNode) {
38
+ static bool parseRootFlags (LLVMContext *Ctx, ModuleRootSignature *MRS,
39
+ MDNode *RootFlagNode) {
40
40
41
41
if (RootFlagNode->getNumOperands () != 2 )
42
- return reportError (" Invalid format for RootFlag Element" );
42
+ return reportError (Ctx, " Invalid format for RootFlag Element" );
43
43
44
44
auto *Flag = mdconst::extract<ConstantInt>(RootFlagNode->getOperand (1 ));
45
45
MRS->Flags = Flag->getZExtValue ();
46
46
47
47
return false ;
48
48
}
49
49
50
- static bool parseRootSignatureElement (ModuleRootSignature *MRS,
50
+ static bool parseRootSignatureElement (LLVMContext *Ctx,
51
+ ModuleRootSignature *MRS,
51
52
MDNode *Element) {
52
53
MDString *ElementText = cast<MDString>(Element->getOperand (0 ));
53
54
if (ElementText == nullptr )
54
- return reportError (" Invalid format for Root Element" );
55
+ return reportError (Ctx, " Invalid format for Root Element" );
55
56
56
57
RootSignatureElementKind ElementKind =
57
58
StringSwitch<RootSignatureElementKind>(ElementText->getString ())
@@ -68,19 +69,20 @@ static bool parseRootSignatureElement(ModuleRootSignature *MRS,
68
69
switch (ElementKind) {
69
70
70
71
case RootSignatureElementKind::RootFlags:
71
- return parseRootFlags (MRS, Element);
72
+ return parseRootFlags (Ctx, MRS, Element);
72
73
case RootSignatureElementKind::RootConstants:
73
74
case RootSignatureElementKind::RootDescriptor:
74
75
case RootSignatureElementKind::DescriptorTable:
75
76
case RootSignatureElementKind::StaticSampler:
76
77
case RootSignatureElementKind::None:
77
- return reportError (" Invalid Root Element: " + ElementText->getString ());
78
+ return reportError (Ctx,
79
+ " Invalid Root Element: " + ElementText->getString ());
78
80
}
79
81
80
82
return true ;
81
83
}
82
84
83
- static bool parse (ModuleRootSignature *MRS, NamedMDNode *Root,
85
+ static bool parse (LLVMContext *Ctx, ModuleRootSignature *MRS, NamedMDNode *Root,
84
86
const Function *EF) {
85
87
bool HasError = false ;
86
88
@@ -97,55 +99,66 @@ static bool parse(ModuleRootSignature *MRS, NamedMDNode *Root,
97
99
*/
98
100
99
101
for (const MDNode *Node : Root->operands ()) {
100
- if (Node->getNumOperands () != 2 )
101
- return reportError (" Invalid format for Root Signature Definition. Pairs "
102
- " of function, root signature expected." );
102
+ if (Node->getNumOperands () != 2 ) {
103
+ HasError = reportError (
104
+ Ctx, " Invalid format for Root Signature Definition. Pairs "
105
+ " of function, root signature expected." );
106
+ continue ;
107
+ }
103
108
104
109
ValueAsMetadata *VAM =
105
110
llvm::dyn_cast<ValueAsMetadata>(Node->getOperand (0 ).get ());
106
- if (VAM == nullptr )
107
- return reportError (" First element of root signature is not a value" );
111
+ if (VAM == nullptr ) {
112
+ HasError =
113
+ reportError (Ctx, " First element of root signature is not a value" );
114
+ continue ;
115
+ }
108
116
109
117
Function *F = dyn_cast<Function>(VAM->getValue ());
110
- if (F == nullptr )
111
- return reportError (" First element of root signature is not a function" );
118
+ if (F == nullptr ) {
119
+ HasError =
120
+ reportError (Ctx, " First element of root signature is not a function" );
121
+ continue ;
122
+ }
112
123
113
124
if (F != EF)
114
125
continue ;
115
126
116
127
// Get the Root Signature Description from the function signature pair.
117
128
MDNode *RS = dyn_cast<MDNode>(Node->getOperand (1 ).get ());
118
129
119
- if (RS == nullptr )
120
- return reportError (" Missing Root Element List Metadata node." );
130
+ if (RS == nullptr ) {
131
+ reportError (Ctx, " Missing Root Element List Metadata node." );
132
+ continue ;
133
+ }
121
134
122
135
// Loop through the Root Elements of the root signature.
123
136
for (unsigned int Eid = 0 ; Eid < RS->getNumOperands (); Eid++) {
124
137
MDNode *Element = dyn_cast<MDNode>(RS->getOperand (Eid));
125
138
if (Element == nullptr )
126
- return reportError (" Missing Root Element Metadata Node." );
139
+ return reportError (Ctx, " Missing Root Element Metadata Node." );
127
140
128
- HasError = HasError || parseRootSignatureElement (MRS, Element);
141
+ HasError = HasError || parseRootSignatureElement (Ctx, MRS, Element);
129
142
}
130
143
}
131
144
return HasError;
132
145
}
133
146
134
- static bool validate (ModuleRootSignature *MRS) {
147
+ static bool validate (LLVMContext *Ctx, ModuleRootSignature *MRS) {
135
148
if (dxbc::RootSignatureValidations::validateRootFlag (MRS->Flags )) {
136
- return reportError (" Invalid Root Signature flag value" );
149
+ return reportError (Ctx, " Invalid Root Signature flag value" );
137
150
}
138
151
return false ;
139
152
}
140
153
141
154
std::optional<ModuleRootSignature>
142
155
ModuleRootSignature::analyzeModule (Module &M, const Function *F) {
143
156
ModuleRootSignature MRS;
144
- Ctx = &M.getContext ();
157
+ LLVMContext * Ctx = &M.getContext ();
145
158
146
159
NamedMDNode *RootSignatureNode = M.getNamedMetadata (" dx.rootsignatures" );
147
- if (RootSignatureNode == nullptr || parse (&MRS, RootSignatureNode, F) ||
148
- validate (&MRS))
160
+ if (RootSignatureNode == nullptr || parse (Ctx, &MRS, RootSignatureNode, F) ||
161
+ validate (Ctx, &MRS))
149
162
return std::nullopt;
150
163
151
164
return MRS;
@@ -160,7 +173,12 @@ RootSignatureAnalysis::run(Module &M, ModuleAnalysisManager &AM) {
160
173
if (MMI.ShaderProfile == Triple::Library)
161
174
return std::nullopt;
162
175
163
- assert (MMI.EntryPropertyVec .size () == 1 );
176
+ LLVMContext *Ctx = &M.getContext ();
177
+
178
+ if (MMI.EntryPropertyVec .size () != 1 ) {
179
+ reportError (Ctx, " More than one entry function defined." );
180
+ return std::nullopt;
181
+ }
164
182
165
183
const Function *EntryFunction = MMI.EntryPropertyVec [0 ].Entry ;
166
184
return ModuleRootSignature::analyzeModule (M, EntryFunction);
@@ -174,7 +192,12 @@ bool RootSignatureAnalysisWrapper::runOnModule(Module &M) {
174
192
175
193
if (MMI.ShaderProfile == Triple::Library)
176
194
return false ;
177
- assert (MMI.EntryPropertyVec .size () == 1 );
195
+
196
+ LLVMContext *Ctx = &M.getContext ();
197
+ if (MMI.EntryPropertyVec .size () != 1 ) {
198
+ reportError (Ctx, " More than one entry function defined." );
199
+ return false ;
200
+ }
178
201
179
202
const Function *EntryFunction = MMI.EntryPropertyVec [0 ].Entry ;
180
203
MRS = ModuleRootSignature::analyzeModule (M, EntryFunction);
0 commit comments