/src/spirv-tools/source/val/validate.cpp
Line | Count | Source |
1 | | // Copyright (c) 2015-2016 The Khronos Group Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include "source/val/validate.h" |
16 | | |
17 | | #include <functional> |
18 | | #include <iterator> |
19 | | #include <memory> |
20 | | #include <string> |
21 | | #include <unordered_set> |
22 | | #include <vector> |
23 | | |
24 | | #include "source/binary.h" |
25 | | #include "source/diagnostic.h" |
26 | | #include "source/extensions.h" |
27 | | #include "source/opcode.h" |
28 | | #include "source/spirv_constant.h" |
29 | | #include "source/spirv_endian.h" |
30 | | #include "source/spirv_target_env.h" |
31 | | #include "source/table2.h" |
32 | | #include "source/val/construct.h" |
33 | | #include "source/val/instruction.h" |
34 | | #include "source/val/validation_state.h" |
35 | | #include "spirv-tools/libspirv.h" |
36 | | |
37 | | namespace { |
38 | | // TODO(issue 1950): The validator only returns a single message anyway, so no |
39 | | // point in generating more than 1 warning. |
40 | | static uint32_t kDefaultMaxNumOfWarnings = 1; |
41 | | } // namespace |
42 | | |
43 | | namespace spvtools { |
44 | | namespace val { |
45 | | namespace { |
46 | | |
47 | | // Parses OpExtension instruction and registers extension. |
48 | | void RegisterExtension(ValidationState_t& _, |
49 | 19.8k | const spv_parsed_instruction_t* inst) { |
50 | 19.8k | const std::string extension_str = spvtools::GetExtensionString(inst); |
51 | 19.8k | Extension extension; |
52 | 19.8k | if (!GetExtensionFromString(extension_str.c_str(), &extension)) { |
53 | | // The error will be logged in the ProcessInstruction pass. |
54 | 17.8k | return; |
55 | 17.8k | } |
56 | | |
57 | 2.02k | _.RegisterExtension(extension); |
58 | 2.02k | } |
59 | | |
60 | | // Parses the beginning of the module searching for OpExtension instructions. |
61 | | // Registers extensions if recognized. Returns SPV_REQUESTED_TERMINATION |
62 | | // once an instruction which is not spv::Op::OpCapability and |
63 | | // spv::Op::OpExtension is encountered. According to the SPIR-V spec extensions |
64 | | // are declared after capabilities and before everything else. |
65 | | spv_result_t ProcessExtensions(void* user_data, |
66 | 136k | const spv_parsed_instruction_t* inst) { |
67 | 136k | const spv::Op opcode = static_cast<spv::Op>(inst->opcode); |
68 | 136k | if (opcode == spv::Op::OpCapability || |
69 | 72.8k | opcode == spv::Op::OpConditionalCapabilityINTEL) |
70 | 63.4k | return SPV_SUCCESS; |
71 | | |
72 | 72.8k | if (opcode == spv::Op::OpExtension || |
73 | 53.2k | opcode == spv::Op::OpConditionalExtensionINTEL) { |
74 | 19.8k | ValidationState_t& _ = *(reinterpret_cast<ValidationState_t*>(user_data)); |
75 | 19.8k | RegisterExtension(_, inst); |
76 | 19.8k | return SPV_SUCCESS; |
77 | 19.8k | } |
78 | | |
79 | | // OpExtension block is finished, requesting termination. |
80 | 52.9k | return SPV_REQUESTED_TERMINATION; |
81 | 72.8k | } |
82 | | |
83 | | spv_result_t ProcessInstruction(void* user_data, |
84 | 15.6M | const spv_parsed_instruction_t* inst) { |
85 | 15.6M | ValidationState_t& _ = *(reinterpret_cast<ValidationState_t*>(user_data)); |
86 | | |
87 | 15.6M | auto* instruction = _.AddOrderedInstruction(inst); |
88 | 15.6M | _.RegisterDebugInstruction(instruction); |
89 | | |
90 | 15.6M | return SPV_SUCCESS; |
91 | 15.6M | } |
92 | | |
93 | 41.6k | spv_result_t ValidateForwardDecls(ValidationState_t& _) { |
94 | 41.6k | if (_.unresolved_forward_id_count() == 0) return SPV_SUCCESS; |
95 | | |
96 | 2.84k | std::stringstream ss; |
97 | 2.84k | std::vector<uint32_t> ids = _.UnresolvedForwardIds(); |
98 | | |
99 | 2.84k | std::transform( |
100 | 2.84k | std::begin(ids), std::end(ids), |
101 | 2.84k | std::ostream_iterator<std::string>(ss, " "), |
102 | 2.84k | bind(&ValidationState_t::getIdName, std::ref(_), std::placeholders::_1)); |
103 | | |
104 | 2.84k | auto id_str = ss.str(); |
105 | 2.84k | return _.diag(SPV_ERROR_INVALID_ID, nullptr) |
106 | 2.84k | << "The following forward referenced IDs have not been defined:\n" |
107 | 2.84k | << id_str.substr(0, id_str.size() - 1); |
108 | 41.6k | } |
109 | | |
110 | | // Entry point validation. Based on 2.16.1 (Universal Validation Rules) of the |
111 | | // SPIRV spec: |
112 | | // * There is at least one OpEntryPoint instruction, unless the Linkage |
113 | | // capability is being used. |
114 | | // * No function can be targeted by both an OpEntryPoint instruction and an |
115 | | // OpFunctionCall instruction. |
116 | | // |
117 | | // Additionally enforces that entry points for Vulkan should not have recursion. |
118 | 31.4k | spv_result_t ValidateEntryPoints(ValidationState_t& _) { |
119 | 31.4k | _.ComputeFunctionToEntryPointMapping(); |
120 | 31.4k | _.ComputeRecursiveEntryPoints(); |
121 | | |
122 | 31.4k | if (_.entry_points().empty() && !_.HasCapability(spv::Capability::Linkage) && |
123 | 3.25k | !_.HasCapability(spv::Capability::GraphARM)) { |
124 | 3.25k | return _.diag(SPV_ERROR_INVALID_BINARY, nullptr) |
125 | 3.25k | << "No OpEntryPoint instruction was found. This is only allowed if " |
126 | 3.25k | "the Linkage or GraphARM capability is being used."; |
127 | 3.25k | } |
128 | | |
129 | 28.1k | for (const auto& entry_point : _.entry_points()) { |
130 | 21.7k | if (_.IsFunctionCallTarget(entry_point)) { |
131 | 56 | return _.diag(SPV_ERROR_INVALID_BINARY, _.FindDef(entry_point)) |
132 | 56 | << "A function (" << entry_point |
133 | 56 | << ") may not be targeted by both an OpEntryPoint instruction and " |
134 | 56 | "an OpFunctionCall instruction."; |
135 | 56 | } |
136 | | |
137 | | // For Vulkan, the static function-call graph for an entry point |
138 | | // must not contain cycles. |
139 | 21.7k | if (spvIsVulkanEnv(_.context()->target_env)) { |
140 | 0 | if (_.recursive_entry_points().find(entry_point) != |
141 | 0 | _.recursive_entry_points().end()) { |
142 | 0 | return _.diag(SPV_ERROR_INVALID_BINARY, _.FindDef(entry_point)) |
143 | 0 | << _.VkErrorID(4634) |
144 | 0 | << "Entry points may not have a call graph with cycles."; |
145 | 0 | } |
146 | 0 | } |
147 | 21.7k | } |
148 | | |
149 | 28.1k | if (auto error = ValidateFloatControls2(_)) { |
150 | 0 | return error; |
151 | 0 | } |
152 | 28.1k | if (auto error = ValidateDuplicateExecutionModes(_)) { |
153 | 338 | return error; |
154 | 338 | } |
155 | | |
156 | 27.7k | return SPV_SUCCESS; |
157 | 28.1k | } |
158 | | |
159 | 27.7k | spv_result_t ValidateGraphEntryPoints(ValidationState_t& _) { |
160 | 27.7k | if (_.graph_entry_points().empty() && |
161 | 27.7k | _.HasCapability(spv::Capability::GraphARM)) { |
162 | 0 | return _.diag(SPV_ERROR_INVALID_BINARY, nullptr) |
163 | 0 | << "No OpGraphEntryPointARM instruction was found but the GraphARM " |
164 | 0 | "capability is declared."; |
165 | 0 | } |
166 | 27.7k | return SPV_SUCCESS; |
167 | 27.7k | } |
168 | | |
169 | | spv_result_t ValidateBinaryUsingContextAndValidationState( |
170 | | const spv_context_t& context, const uint32_t* words, const size_t num_words, |
171 | 57.4k | spv_diagnostic* pDiagnostic, ValidationState_t* vstate) { |
172 | 57.4k | auto binary = std::unique_ptr<spv_const_binary_t>( |
173 | 57.4k | new spv_const_binary_t{words, num_words}); |
174 | | |
175 | 57.4k | spv_endianness_t endian; |
176 | 57.4k | spv_position_t position = {}; |
177 | 57.4k | if (spvBinaryEndianness(binary.get(), &endian)) { |
178 | 89 | return DiagnosticStream(position, context.consumer, "", |
179 | 89 | SPV_ERROR_INVALID_BINARY) |
180 | 89 | << "Invalid SPIR-V magic number."; |
181 | 89 | } |
182 | | |
183 | 57.4k | spv_header_t header; |
184 | 57.4k | if (spvBinaryHeaderGet(binary.get(), endian, &header)) { |
185 | 72 | return DiagnosticStream(position, context.consumer, "", |
186 | 72 | SPV_ERROR_INVALID_BINARY) |
187 | 72 | << "Invalid SPIR-V header."; |
188 | 72 | } |
189 | | |
190 | 57.3k | if (header.version > spvVersionForTargetEnv(context.target_env)) { |
191 | 1.01k | return DiagnosticStream(position, context.consumer, "", |
192 | 1.01k | SPV_ERROR_WRONG_VERSION) |
193 | 1.01k | << "Invalid SPIR-V binary version " |
194 | 1.01k | << SPV_SPIRV_VERSION_MAJOR_PART(header.version) << "." |
195 | 1.01k | << SPV_SPIRV_VERSION_MINOR_PART(header.version) |
196 | 1.01k | << " for target environment " |
197 | 1.01k | << spvTargetEnvDescription(context.target_env) << "."; |
198 | 1.01k | } |
199 | | |
200 | 56.3k | if (header.bound > vstate->options()->universal_limits_.max_id_bound) { |
201 | 1.31k | return DiagnosticStream(position, context.consumer, "", |
202 | 1.31k | SPV_ERROR_INVALID_BINARY) |
203 | 1.31k | << "Invalid SPIR-V. The id bound is larger than the max id bound " |
204 | 1.31k | << vstate->options()->universal_limits_.max_id_bound << "."; |
205 | 1.31k | } |
206 | | |
207 | | // Look for OpExtension instructions and register extensions. |
208 | | // This parse should not produce any error messages. Hijack the context and |
209 | | // replace the message consumer so that we do not pollute any state in input |
210 | | // consumer. |
211 | 55.0k | spv_context_t hijacked_context = context; |
212 | 55.0k | hijacked_context.consumer = [](spv_message_level_t, const char*, |
213 | 55.0k | const spv_position_t&, const char*) {}; |
214 | 55.0k | spvBinaryParse(&hijacked_context, vstate, words, num_words, |
215 | 55.0k | /* parsed_header = */ nullptr, ProcessExtensions, |
216 | 55.0k | /* diagnostic = */ nullptr); |
217 | | |
218 | | // Parse the module and perform inline validation checks. These checks do |
219 | | // not require the knowledge of the whole module. |
220 | 55.0k | if (auto error = spvBinaryParse(&context, vstate, words, num_words, |
221 | 55.0k | /*parsed_header =*/nullptr, |
222 | 55.0k | ProcessInstruction, pDiagnostic)) { |
223 | 1.39k | return error; |
224 | 1.39k | } |
225 | | |
226 | 53.6k | bool has_mask_task_nv = false; |
227 | 53.6k | bool has_mask_task_ext = false; |
228 | 53.6k | std::vector<Instruction*> visited_entry_points; |
229 | 53.6k | std::unordered_set<std::string> graph_entry_point_names; |
230 | 15.3M | for (auto& instruction : vstate->ordered_instructions()) { |
231 | 15.3M | { |
232 | | // In order to do this work outside of Process Instruction we need to be |
233 | | // able to, briefly, de-const the instruction. |
234 | 15.3M | Instruction* inst = const_cast<Instruction*>(&instruction); |
235 | | |
236 | 15.3M | if ((inst->opcode() == spv::Op::OpEntryPoint) || |
237 | 15.3M | (inst->opcode() == spv::Op::OpConditionalEntryPointINTEL)) { |
238 | 31.0k | const int i_model = inst->opcode() == spv::Op::OpEntryPoint ? 0 : 1; |
239 | 31.0k | const int i_point = inst->opcode() == spv::Op::OpEntryPoint ? 1 : 2; |
240 | 31.0k | const int i_name = inst->opcode() == spv::Op::OpEntryPoint ? 2 : 3; |
241 | 31.0k | const int min_num_operands = |
242 | 31.0k | inst->opcode() == spv::Op::OpEntryPoint ? 3 : 4; |
243 | | |
244 | 31.0k | const auto entry_point = inst->GetOperandAs<uint32_t>(i_point); |
245 | 31.0k | const auto execution_model = |
246 | 31.0k | inst->GetOperandAs<spv::ExecutionModel>(i_model); |
247 | 31.0k | const std::string desc_name = inst->GetOperandAs<std::string>(i_name); |
248 | | |
249 | 31.0k | ValidationState_t::EntryPointDescription desc; |
250 | 31.0k | desc.name = desc_name; |
251 | | |
252 | 31.0k | std::vector<uint32_t> interfaces; |
253 | 1.56M | for (size_t j = min_num_operands; j < inst->operands().size(); ++j) |
254 | 1.53M | desc.interfaces.push_back(inst->word(inst->operand(j).offset)); |
255 | | |
256 | 31.0k | vstate->RegisterEntryPoint(entry_point, execution_model, |
257 | 31.0k | std::move(desc)); |
258 | | |
259 | 31.0k | if (inst->opcode() == spv::Op::OpEntryPoint) { |
260 | | // conditional entry points are allowed to share the same name and |
261 | | // exec mode |
262 | 31.0k | if (visited_entry_points.size() > 0) { |
263 | 22.7k | for (const Instruction* check_inst : visited_entry_points) { |
264 | 22.7k | const auto check_execution_model = |
265 | 22.7k | check_inst->GetOperandAs<spv::ExecutionModel>(i_model); |
266 | 22.7k | const std::string check_name = |
267 | 22.7k | check_inst->GetOperandAs<std::string>(i_name); |
268 | | |
269 | 22.7k | if (desc_name == check_name && |
270 | 512 | execution_model == check_execution_model) { |
271 | 107 | return vstate->diag(SPV_ERROR_INVALID_DATA, inst) |
272 | 107 | << "2 Entry points cannot share the same name and " |
273 | 107 | "ExecutionMode."; |
274 | 107 | } |
275 | 22.7k | } |
276 | 4.89k | } |
277 | 30.9k | visited_entry_points.push_back(inst); |
278 | 30.9k | } |
279 | | |
280 | 30.9k | has_mask_task_nv |= (execution_model == spv::ExecutionModel::TaskNV || |
281 | 30.9k | execution_model == spv::ExecutionModel::MeshNV); |
282 | 30.9k | has_mask_task_ext |= (execution_model == spv::ExecutionModel::TaskEXT || |
283 | 30.9k | execution_model == spv::ExecutionModel::MeshEXT); |
284 | 30.9k | } |
285 | 15.3M | if (inst->opcode() == spv::Op::OpGraphEntryPointARM) { |
286 | 33 | const std::string name = inst->GetOperandAs<std::string>(1); |
287 | 33 | if (!graph_entry_point_names.insert(name).second) { |
288 | 0 | return vstate->diag(SPV_ERROR_INVALID_DATA, inst) |
289 | 0 | << "Graph entry points cannot share the same name."; |
290 | 0 | } |
291 | 33 | const auto graph = inst->GetOperandAs<uint32_t>(0); |
292 | 33 | vstate->RegisterGraphEntryPoint(graph); |
293 | 33 | } |
294 | 15.3M | if (inst->opcode() == spv::Op::OpFunctionCall) { |
295 | 37.2k | if (!vstate->in_function_body()) { |
296 | 1 | return vstate->diag(SPV_ERROR_INVALID_LAYOUT, &instruction) |
297 | 1 | << "A FunctionCall must happen within a function body."; |
298 | 1 | } |
299 | | |
300 | 37.2k | const auto called_id = inst->GetOperandAs<uint32_t>(2); |
301 | 37.2k | vstate->AddFunctionCallTarget(called_id); |
302 | 37.2k | } |
303 | | |
304 | 15.3M | if (vstate->in_function_body()) { |
305 | 3.10M | inst->set_function(&(vstate->current_function())); |
306 | 3.10M | inst->set_block(vstate->current_function().current_block()); |
307 | | |
308 | 3.10M | if (vstate->in_block() && spvOpcodeIsBlockTerminator(inst->opcode())) { |
309 | 509k | vstate->current_function().current_block()->set_terminator(inst); |
310 | 509k | } |
311 | 3.10M | } |
312 | | |
313 | 15.3M | if (auto error = IdPass(*vstate, inst)) return error; |
314 | 15.3M | } |
315 | | |
316 | 15.3M | if (auto error = CapabilityPass(*vstate, &instruction)) return error; |
317 | 15.3M | if (auto error = ModuleLayoutPass(*vstate, &instruction)) return error; |
318 | 15.3M | if (auto error = CfgPass(*vstate, &instruction)) return error; |
319 | 15.3M | if (auto error = InstructionPass(*vstate, &instruction)) return error; |
320 | | |
321 | | // Now that all of the checks are done, update the state. |
322 | 15.3M | { |
323 | 15.3M | Instruction* inst = const_cast<Instruction*>(&instruction); |
324 | 15.3M | vstate->RegisterInstruction(inst); |
325 | 15.3M | if (inst->opcode() == spv::Op::OpTypeForwardPointer) { |
326 | 705 | vstate->RegisterForwardPointer(inst->GetOperandAs<uint32_t>(0)); |
327 | 705 | } |
328 | 15.3M | } |
329 | 15.3M | } |
330 | | |
331 | 44.2k | if (!vstate->has_memory_model_specified()) |
332 | 736 | return vstate->diag(SPV_ERROR_INVALID_LAYOUT, nullptr) |
333 | 736 | << "Missing required OpMemoryModel instruction."; |
334 | | |
335 | 43.5k | if (vstate->in_function_body()) |
336 | 1.87k | return vstate->diag(SPV_ERROR_INVALID_LAYOUT, nullptr) |
337 | 1.87k | << "Missing OpFunctionEnd at end of module."; |
338 | | |
339 | 41.6k | if (vstate->graph_definition_region() != kGraphDefinitionOutside) |
340 | 0 | return vstate->diag(SPV_ERROR_INVALID_LAYOUT, nullptr) |
341 | 0 | << "Missing OpGraphEndARM at end of module."; |
342 | | |
343 | 41.6k | if (vstate->HasCapability(spv::Capability::BindlessTextureNV) && |
344 | 0 | !vstate->has_samplerimage_variable_address_mode_specified()) |
345 | 0 | return vstate->diag(SPV_ERROR_INVALID_LAYOUT, nullptr) |
346 | 0 | << "Missing required OpSamplerImageAddressingModeNV instruction."; |
347 | | |
348 | 41.6k | if (has_mask_task_ext && has_mask_task_nv) |
349 | 0 | return vstate->diag(SPV_ERROR_INVALID_LAYOUT, nullptr) |
350 | 0 | << vstate->VkErrorID(7102) |
351 | 0 | << "Module can't mix MeshEXT/TaskEXT with MeshNV/TaskNV Execution " |
352 | 0 | "Model."; |
353 | | |
354 | | // Catch undefined forward references before performing further checks. |
355 | 41.6k | if (auto error = ValidateForwardDecls(*vstate)) return error; |
356 | | |
357 | | // Calculate reachability after all the blocks are parsed, but early that it |
358 | | // can be relied on in subsequent passes. |
359 | 38.7k | ReachabilityPass(*vstate); |
360 | | |
361 | | // ID usage needs be handled in its own iteration of the instructions, |
362 | | // between the two others. It depends on the first loop to have been |
363 | | // finished, so that all instructions have been registered. And the following |
364 | | // loop depends on all of the usage data being populated. Thus it cannot live |
365 | | // in either of those iterations. |
366 | | // It should also live after the forward declaration check, since it will |
367 | | // have problems with missing forward declarations, but give less useful error |
368 | | // messages. |
369 | 14.8M | for (size_t i = 0; i < vstate->ordered_instructions().size(); ++i) { |
370 | 14.7M | auto& instruction = vstate->ordered_instructions()[i]; |
371 | 14.7M | if (auto error = UpdateIdUse(*vstate, &instruction)) return error; |
372 | 14.7M | } |
373 | | |
374 | | // Validate individual opcodes. |
375 | 14.6M | for (size_t i = 0; i < vstate->ordered_instructions().size(); ++i) { |
376 | 14.6M | auto& instruction = vstate->ordered_instructions()[i]; |
377 | | |
378 | | // Keep these passes in the order they appear in the SPIR-V specification |
379 | | // sections to maintain test consistency. |
380 | 14.6M | if (auto error = MiscPass(*vstate, &instruction)) return error; |
381 | 14.6M | if (auto error = DebugPass(*vstate, &instruction)) return error; |
382 | 14.6M | if (auto error = AnnotationPass(*vstate, &instruction)) return error; |
383 | 14.6M | if (auto error = ExtensionPass(*vstate, &instruction)) return error; |
384 | 14.6M | if (auto error = ModeSettingPass(*vstate, &instruction)) return error; |
385 | 14.6M | if (auto error = TypePass(*vstate, &instruction)) return error; |
386 | 14.6M | if (auto error = ConstantPass(*vstate, &instruction)) return error; |
387 | 14.6M | if (auto error = MemoryPass(*vstate, &instruction)) return error; |
388 | 14.6M | if (auto error = FunctionPass(*vstate, &instruction)) return error; |
389 | 14.6M | if (auto error = ImagePass(*vstate, &instruction)) return error; |
390 | 14.6M | if (auto error = ConversionPass(*vstate, &instruction)) return error; |
391 | 14.6M | if (auto error = CompositesPass(*vstate, &instruction)) return error; |
392 | 14.6M | if (auto error = ArithmeticsPass(*vstate, &instruction)) return error; |
393 | 14.6M | if (auto error = BitwisePass(*vstate, &instruction)) return error; |
394 | 14.6M | if (auto error = LogicalsPass(*vstate, &instruction)) return error; |
395 | 14.6M | if (auto error = ControlFlowPass(*vstate, &instruction)) return error; |
396 | 14.6M | if (auto error = DerivativesPass(*vstate, &instruction)) return error; |
397 | 14.6M | if (auto error = AtomicsPass(*vstate, &instruction)) return error; |
398 | 14.6M | if (auto error = PrimitivesPass(*vstate, &instruction)) return error; |
399 | 14.6M | if (auto error = BarriersPass(*vstate, &instruction)) return error; |
400 | 14.6M | if (auto error = DotProductPass(*vstate, &instruction)) return error; |
401 | 14.6M | if (auto error = GroupPass(*vstate, &instruction)) return error; |
402 | | // Device-Side Enqueue |
403 | 14.6M | if (auto error = PipePass(*vstate, &instruction)) return error; |
404 | 14.6M | if (auto error = NonUniformPass(*vstate, &instruction)) return error; |
405 | | |
406 | 14.6M | if (auto error = LiteralsPass(*vstate, &instruction)) return error; |
407 | 14.6M | if (auto error = RayQueryPass(*vstate, &instruction)) return error; |
408 | 14.6M | if (auto error = RayTracingPass(*vstate, &instruction)) return error; |
409 | 14.6M | if (auto error = RayReorderNVPass(*vstate, &instruction)) return error; |
410 | 14.6M | if (auto error = RayReorderEXTPass(*vstate, &instruction)) return error; |
411 | 14.6M | if (auto error = MeshShadingPass(*vstate, &instruction)) return error; |
412 | 14.6M | if (auto error = TensorLayoutPass(*vstate, &instruction)) return error; |
413 | 14.6M | if (auto error = TensorPass(*vstate, &instruction)) return error; |
414 | 14.6M | if (auto error = GraphPass(*vstate, &instruction)) return error; |
415 | 14.6M | if (auto error = InvalidTypePass(*vstate, &instruction)) return error; |
416 | 14.6M | } |
417 | | |
418 | | // Validate the preconditions involving adjacent instructions. e.g. |
419 | | // spv::Op::OpPhi must only be preceded by spv::Op::OpLabel, spv::Op::OpPhi, |
420 | | // or spv::Op::OpLine. |
421 | 31.4k | if (auto error = ValidateAdjacency(*vstate)) return error; |
422 | | |
423 | 31.4k | if (auto error = ValidateEntryPoints(*vstate)) return error; |
424 | 27.7k | if (auto error = ValidateGraphEntryPoints(*vstate)) return error; |
425 | | // CFG checks are performed after the binary has been parsed |
426 | | // and the CFGPass has collected information about the control flow |
427 | 27.7k | if (auto error = PerformCfgChecks(*vstate)) return error; |
428 | 26.2k | if (auto error = CheckIdDefinitionDominateUse(*vstate)) return error; |
429 | 26.0k | if (auto error = ValidateDecorations(*vstate)) return error; |
430 | 24.3k | if (auto error = ValidateInterfaces(*vstate)) return error; |
431 | | // TODO(dsinclair): Restructure ValidateBuiltins so we can move into the |
432 | | // for() above as it loops over all ordered_instructions internally. |
433 | 24.1k | if (auto error = ValidateBuiltIns(*vstate)) return error; |
434 | | // These checks must be performed after individual opcode checks because |
435 | | // those checks register the limitation checked here. |
436 | 13.5M | for (const auto& inst : vstate->ordered_instructions()) { |
437 | 13.5M | if (auto error = ValidateExecutionLimitations(*vstate, &inst)) return error; |
438 | 13.5M | if (auto error = ValidateSmallTypeUses(*vstate, &inst)) return error; |
439 | 13.5M | if (auto error = ValidateQCOMImageProcessingTextureUsages(*vstate, &inst)) |
440 | 0 | return error; |
441 | 13.5M | } |
442 | 24.0k | if (auto error = ValidateLogicalPointers(*vstate)) return error; |
443 | | |
444 | 23.9k | return SPV_SUCCESS; |
445 | 24.0k | } |
446 | | |
447 | | } // namespace |
448 | | |
449 | | spv_result_t ValidateBinaryAndKeepValidationState( |
450 | | const spv_const_context context, spv_const_validator_options options, |
451 | | const uint32_t* words, const size_t num_words, spv_diagnostic* pDiagnostic, |
452 | 0 | std::unique_ptr<ValidationState_t>* vstate) { |
453 | 0 | spv_context_t hijack_context = *context; |
454 | 0 | if (pDiagnostic) { |
455 | 0 | *pDiagnostic = nullptr; |
456 | 0 | UseDiagnosticAsMessageConsumer(&hijack_context, pDiagnostic); |
457 | 0 | } |
458 | |
|
459 | 0 | vstate->reset(new ValidationState_t(&hijack_context, options, words, |
460 | 0 | num_words, kDefaultMaxNumOfWarnings)); |
461 | |
|
462 | 0 | return ValidateBinaryUsingContextAndValidationState( |
463 | 0 | hijack_context, words, num_words, pDiagnostic, vstate->get()); |
464 | 0 | } |
465 | | |
466 | | } // namespace val |
467 | | } // namespace spvtools |
468 | | |
469 | | spv_result_t spvValidate(const spv_const_context context, |
470 | | const spv_const_binary binary, |
471 | 0 | spv_diagnostic* pDiagnostic) { |
472 | 0 | return spvValidateBinary(context, binary->code, binary->wordCount, |
473 | 0 | pDiagnostic); |
474 | 0 | } |
475 | | |
476 | | spv_result_t spvValidateBinary(const spv_const_context context, |
477 | | const uint32_t* words, const size_t num_words, |
478 | 20.0k | spv_diagnostic* pDiagnostic) { |
479 | 20.0k | spv_context_t hijack_context = *context; |
480 | 20.0k | if (pDiagnostic) { |
481 | 0 | *pDiagnostic = nullptr; |
482 | 0 | spvtools::UseDiagnosticAsMessageConsumer(&hijack_context, pDiagnostic); |
483 | 0 | } |
484 | | |
485 | | // This interface is used for default command line options. |
486 | 20.0k | spv_validator_options default_options = spvValidatorOptionsCreate(); |
487 | | |
488 | | // Create the ValidationState using the context and default options. |
489 | 20.0k | spvtools::val::ValidationState_t vstate(&hijack_context, default_options, |
490 | 20.0k | words, num_words, |
491 | 20.0k | kDefaultMaxNumOfWarnings); |
492 | | |
493 | 20.0k | spv_result_t result = |
494 | 20.0k | spvtools::val::ValidateBinaryUsingContextAndValidationState( |
495 | 20.0k | hijack_context, words, num_words, pDiagnostic, &vstate); |
496 | | |
497 | 20.0k | spvValidatorOptionsDestroy(default_options); |
498 | 20.0k | return result; |
499 | 20.0k | } |
500 | | |
501 | | spv_result_t spvValidateWithOptions(const spv_const_context context, |
502 | | spv_const_validator_options options, |
503 | | const spv_const_binary binary, |
504 | 37.4k | spv_diagnostic* pDiagnostic) { |
505 | 37.4k | spv_context_t hijack_context = *context; |
506 | 37.4k | if (pDiagnostic) { |
507 | 37.4k | *pDiagnostic = nullptr; |
508 | 37.4k | spvtools::UseDiagnosticAsMessageConsumer(&hijack_context, pDiagnostic); |
509 | 37.4k | } |
510 | | |
511 | | // Create the ValidationState using the context. |
512 | 37.4k | spvtools::val::ValidationState_t vstate(&hijack_context, options, |
513 | 37.4k | binary->code, binary->wordCount, |
514 | 37.4k | kDefaultMaxNumOfWarnings); |
515 | | |
516 | 37.4k | return spvtools::val::ValidateBinaryUsingContextAndValidationState( |
517 | 37.4k | hijack_context, binary->code, binary->wordCount, pDiagnostic, &vstate); |
518 | 37.4k | } |