| 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 |
#ifndef SOURCE_VAL_VALIDATION_STATE_H_ |
| 16 |
#define SOURCE_VAL_VALIDATION_STATE_H_ |
| 17 |
|
| 18 |
#include <algorithm> |
| 19 |
#include <map> |
| 20 |
#include <set> |
| 21 |
#include <string> |
| 22 |
#include <tuple> |
| 23 |
#include <unordered_map> |
| 24 |
#include <unordered_set> |
| 25 |
#include <vector> |
| 26 |
|
| 27 |
#include "source/assembly_grammar.h" |
| 28 |
#include "source/diagnostic.h" |
| 29 |
#include "source/disassemble.h" |
| 30 |
#include "source/enum_set.h" |
| 31 |
#include "source/latest_version_spirv_header.h" |
| 32 |
#include "source/name_mapper.h" |
| 33 |
#include "source/spirv_definition.h" |
| 34 |
#include "source/spirv_validator_options.h" |
| 35 |
#include "source/val/decoration.h" |
| 36 |
#include "source/val/function.h" |
| 37 |
#include "source/val/instruction.h" |
| 38 |
#include "spirv-tools/libspirv.h" |
| 39 |
|
| 40 |
namespace spvtools { |
| 41 |
namespace val { |
| 42 |
|
| 43 |
/// This enum represents the sections of a SPIRV module. See section 2.4 |
| 44 |
/// of the SPIRV spec for additional details of the order. The enumerant values |
| 45 |
/// are in the same order as the vector returned by GetModuleOrder |
| 46 |
enum ModuleLayoutSection { |
| 47 |
kLayoutCapabilities, /// < Section 2.4 #1 |
| 48 |
kLayoutExtensions, /// < Section 2.4 #2 |
| 49 |
kLayoutExtInstImport, /// < Section 2.4 #3 |
| 50 |
kLayoutMemoryModel, /// < Section 2.4 #4 |
| 51 |
kLayoutEntryPoint, /// < Section 2.4 #5 |
| 52 |
kLayoutExecutionMode, /// < Section 2.4 #6 |
| 53 |
kLayoutDebug1, /// < Section 2.4 #7 > 1 |
| 54 |
kLayoutDebug2, /// < Section 2.4 #7 > 2 |
| 55 |
kLayoutDebug3, /// < Section 2.4 #7 > 3 |
| 56 |
kLayoutAnnotations, /// < Section 2.4 #8 |
| 57 |
kLayoutTypes, /// < Section 2.4 #9 |
| 58 |
kLayoutFunctionDeclarations, /// < Section 2.4 #10 |
| 59 |
kLayoutFunctionDefinitions /// < Section 2.4 #11 |
| 60 |
}; |
| 61 |
|
| 62 |
/// This class manages the state of the SPIR-V validation as it is being parsed. |
| 63 |
class ValidationState_t { |
| 64 |
public: |
| 65 |
// Features that can optionally be turned on by a capability or environment. |
| 66 |
struct Feature { |
| 67 |
bool declare_int16_type = false; // Allow OpTypeInt with 16 bit width? |
| 68 |
bool declare_float16_type = false; // Allow OpTypeFloat with 16 bit width? |
| 69 |
bool free_fp_rounding_mode = false; // Allow the FPRoundingMode decoration |
| 70 |
// and its vaules to be used without |
| 71 |
// requiring any capability |
| 72 |
|
| 73 |
// Allow functionalities enabled by VariablePointers capability. |
| 74 |
bool variable_pointers = false; |
| 75 |
// Allow functionalities enabled by VariablePointersStorageBuffer |
| 76 |
// capability. |
| 77 |
bool variable_pointers_storage_buffer = false; |
| 78 |
|
| 79 |
// Permit group oerations Reduce, InclusiveScan, ExclusiveScan |
| 80 |
bool group_ops_reduce_and_scans = false; |
| 81 |
|
| 82 |
// Allow OpTypeInt with 8 bit width? |
| 83 |
bool declare_int8_type = false; |
| 84 |
|
| 85 |
// Target environment uses relaxed block layout. |
| 86 |
// This is true for Vulkan 1.1 or later. |
| 87 |
bool env_relaxed_block_layout = false; |
| 88 |
|
| 89 |
// Allow an OpTypeInt with 8 bit width to be used in more than just int |
| 90 |
// conversion opcodes |
| 91 |
bool use_int8_type = false; |
| 92 |
|
| 93 |
// Use scalar block layout. See VK_EXT_scalar_block_layout: |
| 94 |
// Defines scalar alignment: |
| 95 |
// - scalar alignment equals the scalar size in bytes |
| 96 |
// - array alignment is same as its element alignment |
| 97 |
// - array alignment is max alignment of any of its members |
| 98 |
// - vector alignment is same as component alignment |
| 99 |
// - matrix alignment is same as component alignment |
| 100 |
// For struct in Uniform, StorageBuffer, PushConstant: |
| 101 |
// - Offset of a member is multiple of scalar alignment of that member |
| 102 |
// - ArrayStride and MatrixStride are multiples of scalar alignment |
| 103 |
// Members need not be listed in offset order |
| 104 |
bool scalar_block_layout = false; |
| 105 |
|
| 106 |
// SPIR-V 1.4 allows us to select between any two composite values |
| 107 |
// of the same type. |
| 108 |
bool select_between_composites = false; |
| 109 |
|
| 110 |
// SPIR-V 1.4 allows two memory access operands for OpCopyMemory and |
| 111 |
// OpCopyMemorySized. |
| 112 |
bool copy_memory_permits_two_memory_accesses = false; |
| 113 |
|
| 114 |
// SPIR-V 1.4 allows UConvert as a spec constant op in any environment. |
| 115 |
// The Kernel capability already enables it, separately from this flag. |
| 116 |
bool uconvert_spec_constant_op = false; |
| 117 |
|
| 118 |
// SPIR-V 1.4 allows Function and Private variables to be NonWritable |
| 119 |
bool nonwritable_var_in_function_or_private = false; |
| 120 |
}; |
| 121 |
|
| 122 |
ValidationState_t(const spv_const_context context, |
| 123 |
const spv_const_validator_options opt, |
| 124 |
const uint32_t* words, const size_t num_words, |
| 125 |
const uint32_t max_warnings); |
| 126 |
|
| 127 |
/// Returns the context |
| 128 |
spv_const_context context() const { return context_; } |
| 129 |
|
| 130 |
/// Returns the command line options |
| 131 |
spv_const_validator_options options() const { return options_; } |
| 132 |
|
| 133 |
/// Sets the ID of the generator for this module. |
| 134 |
void setGenerator(uint32_t gen) { generator_ = gen; } |
| 135 |
|
| 136 |
/// Returns the ID of the generator for this module. |
| 137 |
uint32_t generator() const { return generator_; } |
| 138 |
|
| 139 |
/// Sets the SPIR-V version of this module. |
| 140 |
void setVersion(uint32_t ver) { version_ = ver; } |
| 141 |
|
| 142 |
/// Gets the SPIR-V version of this module. |
| 143 |
uint32_t version() const { return version_; } |
| 144 |
|
| 145 |
/// Forward declares the id in the module |
| 146 |
spv_result_t ForwardDeclareId(uint32_t id); |
| 147 |
|
| 148 |
/// Removes a forward declared ID if it has been defined |
| 149 |
spv_result_t RemoveIfForwardDeclared(uint32_t id); |
| 150 |
|
| 151 |
/// Registers an ID as a forward pointer |
| 152 |
spv_result_t RegisterForwardPointer(uint32_t id); |
| 153 |
|
| 154 |
/// Returns whether or not an ID is a forward pointer |
| 155 |
bool IsForwardPointer(uint32_t id) const; |
| 156 |
|
| 157 |
/// Assigns a name to an ID |
| 158 |
void AssignNameToId(uint32_t id, std::string name); |
| 159 |
|
| 160 |
/// Returns a string representation of the ID in the format <id>[Name] where |
| 161 |
/// the <id> is the numeric valid of the id and the Name is a name assigned by |
| 162 |
/// the OpName instruction |
| 163 |
std::string getIdName(uint32_t id) const; |
| 164 |
|
| 165 |
/// Accessor function for ID bound. |
| 166 |
uint32_t getIdBound() const; |
| 167 |
|
| 168 |
/// Mutator function for ID bound. |
| 169 |
void setIdBound(uint32_t bound); |
| 170 |
|
| 171 |
/// Returns the number of ID which have been forward referenced but not |
| 172 |
/// defined |
| 173 |
size_t unresolved_forward_id_count() const; |
| 174 |
|
| 175 |
/// Returns a vector of unresolved forward ids. |
| 176 |
std::vector<uint32_t> UnresolvedForwardIds() const; |
| 177 |
|
| 178 |
/// Returns true if the id has been defined |
| 179 |
bool IsDefinedId(uint32_t id) const; |
| 180 |
|
| 181 |
/// Increments the total number of instructions in the file. |
| 182 |
void increment_total_instructions() { total_instructions_++; } |
| 183 |
|
| 184 |
/// Increments the total number of functions in the file. |
| 185 |
void increment_total_functions() { total_functions_++; } |
| 186 |
|
| 187 |
/// Allocates internal storage. Note, calling this will invalidate any |
| 188 |
/// pointers to |ordered_instructions_| or |module_functions_| and, hence, |
| 189 |
/// should only be called at the beginning of validation. |
| 190 |
void preallocateStorage(); |
| 191 |
|
| 192 |
/// Returns the current layout section which is being processed |
| 193 |
ModuleLayoutSection current_layout_section() const; |
| 194 |
|
| 195 |
/// Increments the module_layout_order_section_ |
| 196 |
void ProgressToNextLayoutSectionOrder(); |
| 197 |
|
| 198 |
/// Determines if the op instruction is in a previous layout section |
| 199 |
bool IsOpcodeInPreviousLayoutSection(SpvOp op); |
| 200 |
|
| 201 |
/// Determines if the op instruction is part of the current section |
| 202 |
bool IsOpcodeInCurrentLayoutSection(SpvOp op); |
| 203 |
|
| 204 |
DiagnosticStream diag(spv_result_t error_code, const Instruction* inst); |
| 205 |
|
| 206 |
/// Returns the function states |
| 207 |
std::vector<Function>& functions(); |
| 208 |
|
| 209 |
/// Returns the function states |
| 210 |
Function& current_function(); |
| 211 |
const Function& current_function() const; |
| 212 |
|
| 213 |
/// Returns function state with the given id, or nullptr if no such function. |
| 214 |
const Function* function(uint32_t id) const; |
| 215 |
Function* function(uint32_t id); |
| 216 |
|
| 217 |
/// Returns true if the called after a function instruction but before the |
| 218 |
/// function end instruction |
| 219 |
bool in_function_body() const; |
| 220 |
|
| 221 |
/// Returns true if called after a label instruction but before a branch |
| 222 |
/// instruction |
| 223 |
bool in_block() const; |
| 224 |
|
| 225 |
struct EntryPointDescription { |
| 226 |
std::string name; |
| 227 |
std::vector<uint32_t> interfaces; |
| 228 |
}; |
| 229 |
|
| 230 |
/// Registers |id| as an entry point with |execution_model| and |interfaces|. |
| 231 |
void RegisterEntryPoint(const uint32_t id, SpvExecutionModel execution_model, |
| 232 |
EntryPointDescription&& desc) { |
| 233 |
entry_points_.push_back(id); |
| 234 |
entry_point_to_execution_models_[id].insert(execution_model); |
| 235 |
entry_point_descriptions_[id].emplace_back(desc); |
| 236 |
} |
| 237 |
|
| 238 |
/// Returns a list of entry point function ids |
| 239 |
const std::vector<uint32_t>& entry_points() const { return entry_points_; } |
| 240 |
|
| 241 |
/// Returns the set of entry points that root call graphs that contain |
| 242 |
/// recursion. |
| 243 |
const std::set<uint32_t>& recursive_entry_points() const { |
| 244 |
return recursive_entry_points_; |
| 245 |
} |
| 246 |
|
| 247 |
/// Registers execution mode for the given entry point. |
| 248 |
void RegisterExecutionModeForEntryPoint(uint32_t entry_point, |
| 249 |
SpvExecutionMode execution_mode) { |
| 250 |
entry_point_to_execution_modes_[entry_point].insert(execution_mode); |
| 251 |
} |
| 252 |
|
| 253 |
/// Returns the interface descriptions of a given entry point. |
| 254 |
const std::vector<EntryPointDescription>& entry_point_descriptions( |
| 255 |
uint32_t entry_point) { |
| 256 |
return entry_point_descriptions_.at(entry_point); |
| 257 |
} |
| 258 |
|
| 259 |
/// Returns Execution Models for the given Entry Point. |
| 260 |
/// Returns nullptr if none found (would trigger assertion). |
| 261 |
const std::set<SpvExecutionModel>* GetExecutionModels( |
| 262 |
uint32_t entry_point) const { |
| 263 |
const auto it = entry_point_to_execution_models_.find(entry_point); |
| 264 |
if (it == entry_point_to_execution_models_.end()) { |
| 265 |
assert(0); |
| 266 |
return nullptr; |
| 267 |
} |
| 268 |
return &it->second; |
| 269 |
} |
| 270 |
|
| 271 |
/// Returns Execution Modes for the given Entry Point. |
| 272 |
/// Returns nullptr if none found. |
| 273 |
const std::set<SpvExecutionMode>* GetExecutionModes( |
| 274 |
uint32_t entry_point) const { |
| 275 |
const auto it = entry_point_to_execution_modes_.find(entry_point); |
| 276 |
if (it == entry_point_to_execution_modes_.end()) { |
| 277 |
return nullptr; |
| 278 |
} |
| 279 |
return &it->second; |
| 280 |
} |
| 281 |
|
| 282 |
/// Traverses call tree and computes function_to_entry_points_. |
| 283 |
/// Note: called after fully parsing the binary. |
| 284 |
void ComputeFunctionToEntryPointMapping(); |
| 285 |
|
| 286 |
/// Traverse call tree and computes recursive_entry_points_. |
| 287 |
/// Note: called after fully parsing the binary and calling |
| 288 |
/// ComputeFunctionToEntryPointMapping. |
| 289 |
void ComputeRecursiveEntryPoints(); |
| 290 |
|
| 291 |
/// Returns all the entry points that can call |func|. |
| 292 |
const std::vector<uint32_t>& FunctionEntryPoints(uint32_t func) const; |
| 293 |
|
| 294 |
/// Returns all the entry points that statically use |id|. |
| 295 |
/// |
| 296 |
/// Note: requires ComputeFunctionToEntryPointMapping to have been called. |
| 297 |
std::set<uint32_t> EntryPointReferences(uint32_t id) const; |
| 298 |
|
| 299 |
/// Inserts an <id> to the set of functions that are target of OpFunctionCall. |
| 300 |
void AddFunctionCallTarget(const uint32_t id) { |
| 301 |
function_call_targets_.insert(id); |
| 302 |
current_function().AddFunctionCallTarget(id); |
| 303 |
} |
| 304 |
|
| 305 |
/// Returns whether or not a function<id> is the target of OpFunctionCall. |
| 306 |
bool IsFunctionCallTarget(const uint32_t id) { |
| 307 |
return (function_call_targets_.find(id) != function_call_targets_.end()); |
| 308 |
} |
| 309 |
|
| 310 |
bool IsFunctionCallDefined(const uint32_t id) { |
| 311 |
return (id_to_function_.find(id) != id_to_function_.end()); |
| 312 |
} |
| 313 |
/// Registers the capability and its dependent capabilities |
| 314 |
void RegisterCapability(SpvCapability cap); |
| 315 |
|
| 316 |
/// Registers the extension. |
| 317 |
void RegisterExtension(Extension ext); |
| 318 |
|
| 319 |
/// Registers the function in the module. Subsequent instructions will be |
| 320 |
/// called against this function |
| 321 |
spv_result_t RegisterFunction(uint32_t id, uint32_t ret_type_id, |
| 322 |
SpvFunctionControlMask function_control, |
| 323 |
uint32_t function_type_id); |
| 324 |
|
| 325 |
/// Register a function end instruction |
| 326 |
spv_result_t RegisterFunctionEnd(); |
| 327 |
|
| 328 |
/// Returns true if the capability is enabled in the module. |
| 329 |
bool HasCapability(SpvCapability cap) const { |
| 330 |
return module_capabilities_.Contains(cap); |
| 331 |
} |
| 332 |
|
| 333 |
/// Returns a reference to the set of capabilities in the module. |
| 334 |
/// This is provided for debuggability. |
| 335 |
const CapabilitySet& module_capabilities() const { |
| 336 |
return module_capabilities_; |
| 337 |
} |
| 338 |
|
| 339 |
/// Returns true if the extension is enabled in the module. |
| 340 |
bool HasExtension(Extension ext) const { |
| 341 |
return module_extensions_.Contains(ext); |
| 342 |
} |
| 343 |
|
| 344 |
/// Returns true if any of the capabilities is enabled, or if |capabilities| |
| 345 |
/// is an empty set. |
| 346 |
bool HasAnyOfCapabilities(const CapabilitySet& capabilities) const; |
| 347 |
|
| 348 |
/// Returns true if any of the extensions is enabled, or if |extensions| |
| 349 |
/// is an empty set. |
| 350 |
bool HasAnyOfExtensions(const ExtensionSet& extensions) const; |
| 351 |
|
| 352 |
/// Sets the addressing model of this module (logical/physical). |
| 353 |
void set_addressing_model(SpvAddressingModel am); |
| 354 |
|
| 355 |
/// Returns true if the OpMemoryModel was found. |
| 356 |
bool has_memory_model_specified() const { |
| 357 |
return addressing_model_ != SpvAddressingModelMax && |
| 358 |
memory_model_ != SpvMemoryModelMax; |
| 359 |
} |
| 360 |
|
| 361 |
/// Returns the addressing model of this module, or Logical if uninitialized. |
| 362 |
SpvAddressingModel addressing_model() const; |
| 363 |
|
| 364 |
/// Returns the addressing model of this module, or Logical if uninitialized. |
| 365 |
uint32_t pointer_size_and_alignment() const { |
| 366 |
return pointer_size_and_alignment_; |
| 367 |
} |
| 368 |
|
| 369 |
/// Sets the memory model of this module. |
| 370 |
void set_memory_model(SpvMemoryModel mm); |
| 371 |
|
| 372 |
/// Returns the memory model of this module, or Simple if uninitialized. |
| 373 |
SpvMemoryModel memory_model() const; |
| 374 |
|
| 375 |
const AssemblyGrammar& grammar() const { return grammar_; } |
| 376 |
|
| 377 |
/// Inserts the instruction into the list of ordered instructions in the file. |
| 378 |
Instruction* AddOrderedInstruction(const spv_parsed_instruction_t* inst); |
| 379 |
|
| 380 |
/// Registers the instruction. This will add the instruction to the list of |
| 381 |
/// definitions and register sampled image consumers. |
| 382 |
void RegisterInstruction(Instruction* inst); |
| 383 |
|
| 384 |
/// Registers the debug instruction information. |
| 385 |
void RegisterDebugInstruction(const Instruction* inst); |
| 386 |
|
| 387 |
/// Registers the decoration for the given <id> |
| 388 |
void RegisterDecorationForId(uint32_t id, const Decoration& dec) { |
| 389 |
auto& dec_list = id_decorations_[id]; |
| 390 |
auto lb = std::find(dec_list.begin(), dec_list.end(), dec); |
| 391 |
if (lb == dec_list.end()) { |
| 392 |
dec_list.push_back(dec); |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
/// Registers the list of decorations for the given <id> |
| 397 |
template <class InputIt> |
| 398 |
void RegisterDecorationsForId(uint32_t id, InputIt begin, InputIt end) { |
| 399 |
std::vector<Decoration>& cur_decs = id_decorations_[id]; |
| 400 |
cur_decs.insert(cur_decs.end(), begin, end); |
| 401 |
} |
| 402 |
|
| 403 |
/// Registers the list of decorations for the given member of the given |
| 404 |
/// structure. |
| 405 |
template <class InputIt> |
| 406 |
void RegisterDecorationsForStructMember(uint32_t struct_id, |
| 407 |
uint32_t member_index, InputIt begin, |
| 408 |
InputIt end) { |
| 409 |
RegisterDecorationsForId(struct_id, begin, end); |
| 410 |
for (auto& decoration : id_decorations_[struct_id]) { |
| 411 |
decoration.set_struct_member_index(member_index); |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
/// Returns all the decorations for the given <id>. If no decorations exist |
| 416 |
/// for the <id>, it registers an empty vector for it in the map and |
| 417 |
/// returns the empty vector. |
| 418 |
std::vector<Decoration>& id_decorations(uint32_t id) { |
| 419 |
return id_decorations_[id]; |
| 420 |
} |
| 421 |
|
| 422 |
// Returns const pointer to the internal decoration container. |
| 423 |
const std::map<uint32_t, std::vector<Decoration>>& id_decorations() const { |
| 424 |
return id_decorations_; |
| 425 |
} |
| 426 |
|
| 427 |
/// Returns true if the given id <id> has the given decoration <dec>, |
| 428 |
/// otherwise returns false. |
| 429 |
bool HasDecoration(uint32_t id, SpvDecoration dec) { |
| 430 |
const auto& decorations = id_decorations_.find(id); |
| 431 |
if (decorations == id_decorations_.end()) return false; |
| 432 |
|
| 433 |
return std::any_of( |
| 434 |
decorations->second.begin(), decorations->second.end(), |
| 435 |
[dec](const Decoration& d) { return dec == d.dec_type(); }); |
| 436 |
} |
| 437 |
|
| 438 |
/// Finds id's def, if it exists. If found, returns the definition otherwise |
| 439 |
/// nullptr |
| 440 |
const Instruction* FindDef(uint32_t id) const; |
| 441 |
|
| 442 |
/// Finds id's def, if it exists. If found, returns the definition otherwise |
| 443 |
/// nullptr |
| 444 |
Instruction* FindDef(uint32_t id); |
| 445 |
|
| 446 |
/// Returns the instructions in the order they appear in the binary |
| 447 |
const std::vector<Instruction>& ordered_instructions() const { |
| 448 |
return ordered_instructions_; |
| 449 |
} |
| 450 |
|
| 451 |
/// Returns a map of instructions mapped by their result id |
| 452 |
const std::unordered_map<uint32_t, Instruction*>& all_definitions() const { |
| 453 |
return all_definitions_; |
| 454 |
} |
| 455 |
|
| 456 |
/// Returns a vector containing the instructions that consume the given |
| 457 |
/// SampledImage id. |
| 458 |
std::vector<Instruction*> getSampledImageConsumers(uint32_t id) const; |
| 459 |
|
| 460 |
/// Records cons_id as a consumer of sampled_image_id. |
| 461 |
void RegisterSampledImageConsumer(uint32_t sampled_image_id, |
| 462 |
Instruction* consumer); |
| 463 |
|
| 464 |
/// Returns the set of Global Variables. |
| 465 |
std::unordered_set<uint32_t>& global_vars() { return global_vars_; } |
| 466 |
|
| 467 |
/// Returns the set of Local Variables. |
| 468 |
std::unordered_set<uint32_t>& local_vars() { return local_vars_; } |
| 469 |
|
| 470 |
/// Returns the number of Global Variables. |
| 471 |
size_t num_global_vars() { return global_vars_.size(); } |
| 472 |
|
| 473 |
/// Returns the number of Local Variables. |
| 474 |
size_t num_local_vars() { return local_vars_.size(); } |
| 475 |
|
| 476 |
/// Inserts a new <id> to the set of Global Variables. |
| 477 |
void registerGlobalVariable(const uint32_t id) { global_vars_.insert(id); } |
| 478 |
|
| 479 |
/// Inserts a new <id> to the set of Local Variables. |
| 480 |
void registerLocalVariable(const uint32_t id) { local_vars_.insert(id); } |
| 481 |
|
| 482 |
// Returns true if using relaxed block layout, equivalent to |
| 483 |
// VK_KHR_relaxed_block_layout. |
| 484 |
bool IsRelaxedBlockLayout() const { |
| 485 |
return features_.env_relaxed_block_layout || options()->relax_block_layout; |
| 486 |
} |
| 487 |
|
| 488 |
/// Sets the struct nesting depth for a given struct ID |
| 489 |
void set_struct_nesting_depth(uint32_t id, uint32_t depth) { |
| 490 |
struct_nesting_depth_[id] = depth; |
| 491 |
} |
| 492 |
|
| 493 |
/// Returns the nesting depth of a given structure ID |
| 494 |
uint32_t struct_nesting_depth(uint32_t id) { |
| 495 |
return struct_nesting_depth_[id]; |
| 496 |
} |
| 497 |
|
| 498 |
/// Records the has a nested block/bufferblock decorated struct for a given |
| 499 |
/// struct ID |
| 500 |
void SetHasNestedBlockOrBufferBlockStruct(uint32_t id, bool has) { |
| 501 |
struct_has_nested_blockorbufferblock_struct_[id] = has; |
| 502 |
} |
| 503 |
|
| 504 |
/// For a given struct ID returns true if it has a nested block/bufferblock |
| 505 |
/// decorated struct |
| 506 |
bool GetHasNestedBlockOrBufferBlockStruct(uint32_t id) { |
| 507 |
return struct_has_nested_blockorbufferblock_struct_[id]; |
| 508 |
} |
| 509 |
|
| 510 |
/// Records that the structure type has a member decorated with a built-in. |
| 511 |
void RegisterStructTypeWithBuiltInMember(uint32_t id) { |
| 512 |
builtin_structs_.insert(id); |
| 513 |
} |
| 514 |
|
| 515 |
/// Returns true if the struct type with the given Id has a BuiltIn member. |
| 516 |
bool IsStructTypeWithBuiltInMember(uint32_t id) const { |
| 517 |
return (builtin_structs_.find(id) != builtin_structs_.end()); |
| 518 |
} |
| 519 |
|
| 520 |
// Returns the state of optional features. |
| 521 |
const Feature& features() const { return features_; } |
| 522 |
|
| 523 |
/// Adds the instruction data to unique_type_declarations_. |
| 524 |
/// Returns false if an identical type declaration already exists. |
| 525 |
bool RegisterUniqueTypeDeclaration(const Instruction* inst); |
| 526 |
|
| 527 |
// Returns type_id of the scalar component of |id|. |
| 528 |
// |id| can be either |
| 529 |
// - scalar, vector or matrix type |
| 530 |
// - object of either scalar, vector or matrix type |
| 531 |
uint32_t GetComponentType(uint32_t id) const; |
| 532 |
|
| 533 |
// Returns |
| 534 |
// - 1 for scalar types or objects |
| 535 |
// - vector size for vector types or objects |
| 536 |
// - num columns for matrix types or objects |
| 537 |
// Should not be called with any other arguments (will return zero and invoke |
| 538 |
// assertion). |
| 539 |
uint32_t GetDimension(uint32_t id) const; |
| 540 |
|
| 541 |
// Returns bit width of scalar or component. |
| 542 |
// |id| can be |
| 543 |
// - scalar, vector or matrix type |
| 544 |
// - object of either scalar, vector or matrix type |
| 545 |
// Will invoke assertion and return 0 if |id| is none of the above. |
| 546 |
uint32_t GetBitWidth(uint32_t id) const; |
| 547 |
|
| 548 |
// Provides detailed information on matrix type. |
| 549 |
// Returns false iff |id| is not matrix type. |
| 550 |
bool GetMatrixTypeInfo(uint32_t id, uint32_t* num_rows, uint32_t* num_cols, |
| 551 |
uint32_t* column_type, uint32_t* component_type) const; |
| 552 |
|
| 553 |
// Collects struct member types into |member_types|. |
| 554 |
// Returns false iff not struct type or has no members. |
| 555 |
// Deletes prior contents of |member_types|. |
| 556 |
bool GetStructMemberTypes(uint32_t struct_type_id, |
| 557 |
std::vector<uint32_t>* member_types) const; |
| 558 |
|
| 559 |
// Returns true iff |id| is a type corresponding to the name of the function. |
| 560 |
// Only works for types not for objects. |
| 561 |
bool IsVoidType(uint32_t id) const; |
| 562 |
bool IsFloatScalarType(uint32_t id) const; |
| 563 |
bool IsFloatVectorType(uint32_t id) const; |
| 564 |
bool IsFloatScalarOrVectorType(uint32_t id) const; |
| 565 |
bool IsFloatMatrixType(uint32_t id) const; |
| 566 |
bool IsIntScalarType(uint32_t id) const; |
| 567 |
bool IsIntVectorType(uint32_t id) const; |
| 568 |
bool IsIntScalarOrVectorType(uint32_t id) const; |
| 569 |
bool IsUnsignedIntScalarType(uint32_t id) const; |
| 570 |
bool IsUnsignedIntVectorType(uint32_t id) const; |
| 571 |
bool IsSignedIntScalarType(uint32_t id) const; |
| 572 |
bool IsSignedIntVectorType(uint32_t id) const; |
| 573 |
bool IsBoolScalarType(uint32_t id) const; |
| 574 |
bool IsBoolVectorType(uint32_t id) const; |
| 575 |
bool IsBoolScalarOrVectorType(uint32_t id) const; |
| 576 |
bool IsPointerType(uint32_t id) const; |
| 577 |
bool IsCooperativeMatrixType(uint32_t id) const; |
| 578 |
bool IsFloatCooperativeMatrixType(uint32_t id) const; |
| 579 |
bool IsIntCooperativeMatrixType(uint32_t id) const; |
| 580 |
bool IsUnsignedIntCooperativeMatrixType(uint32_t id) const; |
| 581 |
|
| 582 |
// Returns true if |id| is a type id that contains |type| (or integer or |
| 583 |
// floating point type) of |width| bits. |
| 584 |
bool ContainsSizedIntOrFloatType(uint32_t id, SpvOp type, |
| 585 |
uint32_t width) const; |
| 586 |
// Returns true if |id| is a type id that contains a 8- or 16-bit int or |
| 587 |
// 16-bit float that is not generally enabled for use. |
| 588 |
bool ContainsLimitedUseIntOrFloatType(uint32_t id) const; |
| 589 |
|
| 590 |
// Gets value from OpConstant and OpSpecConstant as uint64. |
| 591 |
// Returns false on failure (no instruction, wrong instruction, not int). |
| 592 |
bool GetConstantValUint64(uint32_t id, uint64_t* val) const; |
| 593 |
|
| 594 |
// Returns type_id if id has type or zero otherwise. |
| 595 |
uint32_t GetTypeId(uint32_t id) const; |
| 596 |
|
| 597 |
// Returns opcode of the instruction which issued the id or OpNop if the |
| 598 |
// instruction is not registered. |
| 599 |
SpvOp GetIdOpcode(uint32_t id) const; |
| 600 |
|
| 601 |
// Returns type_id for given id operand if it has a type or zero otherwise. |
| 602 |
// |operand_index| is expected to be pointing towards an operand which is an |
| 603 |
// id. |
| 604 |
uint32_t GetOperandTypeId(const Instruction* inst, |
| 605 |
size_t operand_index) const; |
| 606 |
|
| 607 |
// Provides information on pointer type. Returns false iff not pointer type. |
| 608 |
bool GetPointerTypeInfo(uint32_t id, uint32_t* data_type, |
| 609 |
uint32_t* storage_class) const; |
| 610 |
|
| 611 |
// Is the ID the type of a pointer to a uniform block: Block-decorated struct |
| 612 |
// in uniform storage class? The result is only valid after internal method |
| 613 |
// CheckDecorationsOfBuffers has been called. |
| 614 |
bool IsPointerToUniformBlock(uint32_t type_id) const { |
| 615 |
return pointer_to_uniform_block_.find(type_id) != |
| 616 |
pointer_to_uniform_block_.cend(); |
| 617 |
} |
| 618 |
// Save the ID of a pointer to uniform block. |
| 619 |
void RegisterPointerToUniformBlock(uint32_t type_id) { |
| 620 |
pointer_to_uniform_block_.insert(type_id); |
| 621 |
} |
| 622 |
// Is the ID the type of a struct used as a uniform block? |
| 623 |
// The result is only valid after internal method CheckDecorationsOfBuffers |
| 624 |
// has been called. |
| 625 |
bool IsStructForUniformBlock(uint32_t type_id) const { |
| 626 |
return struct_for_uniform_block_.find(type_id) != |
| 627 |
struct_for_uniform_block_.cend(); |
| 628 |
} |
| 629 |
// Save the ID of a struct of a uniform block. |
| 630 |
void RegisterStructForUniformBlock(uint32_t type_id) { |
| 631 |
struct_for_uniform_block_.insert(type_id); |
| 632 |
} |
| 633 |
// Is the ID the type of a pointer to a storage buffer: BufferBlock-decorated |
| 634 |
// struct in uniform storage class, or Block-decorated struct in StorageBuffer |
| 635 |
// storage class? The result is only valid after internal method |
| 636 |
// CheckDecorationsOfBuffers has been called. |
| 637 |
bool IsPointerToStorageBuffer(uint32_t type_id) const { |
| 638 |
return pointer_to_storage_buffer_.find(type_id) != |
| 639 |
pointer_to_storage_buffer_.cend(); |
| 640 |
} |
| 641 |
// Save the ID of a pointer to a storage buffer. |
| 642 |
void RegisterPointerToStorageBuffer(uint32_t type_id) { |
| 643 |
pointer_to_storage_buffer_.insert(type_id); |
| 644 |
} |
| 645 |
// Is the ID the type of a struct for storage buffer? |
| 646 |
// The result is only valid after internal method CheckDecorationsOfBuffers |
| 647 |
// has been called. |
| 648 |
bool IsStructForStorageBuffer(uint32_t type_id) const { |
| 649 |
return struct_for_storage_buffer_.find(type_id) != |
| 650 |
struct_for_storage_buffer_.cend(); |
| 651 |
} |
| 652 |
// Save the ID of a struct of a storage buffer. |
| 653 |
void RegisterStructForStorageBuffer(uint32_t type_id) { |
| 654 |
struct_for_storage_buffer_.insert(type_id); |
| 655 |
} |
| 656 |
|
| 657 |
// Is the ID the type of a pointer to a storage image? That is, the pointee |
| 658 |
// type is an image type which is known to not use a sampler. |
| 659 |
bool IsPointerToStorageImage(uint32_t type_id) const { |
| 660 |
return pointer_to_storage_image_.find(type_id) != |
| 661 |
pointer_to_storage_image_.cend(); |
| 662 |
} |
| 663 |
// Save the ID of a pointer to a storage image. |
| 664 |
void RegisterPointerToStorageImage(uint32_t type_id) { |
| 665 |
pointer_to_storage_image_.insert(type_id); |
| 666 |
} |
| 667 |
|
| 668 |
// Tries to evaluate a 32-bit signed or unsigned scalar integer constant. |
| 669 |
// Returns tuple <is_int32, is_const_int32, value>. |
| 670 |
// OpSpecConstant* return |is_const_int32| as false since their values cannot |
| 671 |
// be relied upon during validation. |
| 672 |
std::tuple<bool, bool, uint32_t> EvalInt32IfConst(uint32_t id) const; |
| 673 |
|
| 674 |
// Returns the disassembly string for the given instruction. |
| 675 |
std::string Disassemble(const Instruction& inst) const; |
| 676 |
|
| 677 |
// Returns the disassembly string for the given instruction. |
| 678 |
std::string Disassemble(const uint32_t* words, uint16_t num_words) const; |
| 679 |
|
| 680 |
// Returns whether type m1 and type m2 are cooperative matrices with |
| 681 |
// the same "shape" (matching scope, rows, cols). If any are specialization |
| 682 |
// constants, we assume they can match because we can't prove they don't. |
| 683 |
spv_result_t CooperativeMatrixShapesMatch(const Instruction* inst, |
| 684 |
uint32_t m1, uint32_t m2); |
| 685 |
|
| 686 |
// Returns true if |lhs| and |rhs| logically match and, if the decorations of |
| 687 |
// |rhs| are a subset of |lhs|. |
| 688 |
// |
| 689 |
// 1. Must both be either OpTypeArray or OpTypeStruct |
| 690 |
// 2. If OpTypeArray, then |
| 691 |
// * Length must be the same |
| 692 |
// * Element type must match or logically match |
| 693 |
// 3. If OpTypeStruct, then |
| 694 |
// * Both have same number of elements |
| 695 |
// * Element N for both structs must match or logically match |
| 696 |
// |
| 697 |
// If |check_decorations| is false, then the decorations are not checked. |
| 698 |
bool LogicallyMatch(const Instruction* lhs, const Instruction* rhs, |
| 699 |
bool check_decorations); |
| 700 |
|
| 701 |
// Traces |inst| to find a single base pointer. Returns the base pointer. |
| 702 |
// Will trace through the following instructions: |
| 703 |
// * OpAccessChain |
| 704 |
// * OpInBoundsAccessChain |
| 705 |
// * OpPtrAccessChain |
| 706 |
// * OpInBoundsPtrAccessChain |
| 707 |
// * OpCopyObject |
| 708 |
const Instruction* TracePointer(const Instruction* inst) const; |
| 709 |
|
| 710 |
// Validates the storage class for the target environment. |
| 711 |
bool IsValidStorageClass(SpvStorageClass storage_class) const; |
| 712 |
|
| 713 |
// Takes a Vulkan Valid Usage ID (VUID) as |id| and optional |reference| and |
| 714 |
// will return a non-empty string only if ID is known and targeting Vulkan. |
| 715 |
// VUIDs are found in the Vulkan-Docs repo in the form "[[VUID-ref-ref-id]]" |
| 716 |
// where "id" is always an 5 char long number (with zeros padding) and matches |
| 717 |
// to |id|. |reference| is used if there is a "common validity" and the VUID |
| 718 |
// shares the same |id| value. |
| 719 |
// |
| 720 |
// More details about Vulkan validation can be found in Vulkan Guide: |
| 721 |
// https://github.com/KhronosGroup/Vulkan-Guide/blob/master/chapters/validation_overview.md |
| 722 |
std::string VkErrorID(uint32_t id, const char* reference = nullptr) const; |
| 723 |
|
| 724 |
// Testing method to allow setting the current layout section. |
| 725 |
void SetCurrentLayoutSectionForTesting(ModuleLayoutSection section) { |
| 726 |
current_layout_section_ = section; |
| 727 |
} |
| 728 |
|
| 729 |
private: |
| 730 |
ValidationState_t(const ValidationState_t&); |
| 731 |
|
| 732 |
const spv_const_context context_; |
| 733 |
|
| 734 |
/// Stores the Validator command line options. Must be a valid options object. |
| 735 |
const spv_const_validator_options options_; |
| 736 |
|
| 737 |
/// The SPIR-V binary module we're validating. |
| 738 |
const uint32_t* words_; |
| 739 |
const size_t num_words_; |
| 740 |
|
| 741 |
/// The generator of the SPIR-V. |
| 742 |
uint32_t generator_ = 0; |
| 743 |
|
| 744 |
/// The version of the SPIR-V. |
| 745 |
uint32_t version_ = 0; |
| 746 |
|
| 747 |
/// The total number of instructions in the binary. |
| 748 |
size_t total_instructions_ = 0; |
| 749 |
/// The total number of functions in the binary. |
| 750 |
size_t total_functions_ = 0; |
| 751 |
|
| 752 |
/// IDs which have been forward declared but have not been defined |
| 753 |
std::unordered_set<uint32_t> unresolved_forward_ids_; |
| 754 |
|
| 755 |
/// IDs that have been declared as forward pointers. |
| 756 |
std::unordered_set<uint32_t> forward_pointer_ids_; |
| 757 |
|
| 758 |
/// Stores a vector of instructions that use the result of a given |
| 759 |
/// OpSampledImage instruction. |
| 760 |
std::unordered_map<uint32_t, std::vector<Instruction*>> |
| 761 |
sampled_image_consumers_; |
| 762 |
|
| 763 |
/// A map of operand IDs and their names defined by the OpName instruction |
| 764 |
std::unordered_map<uint32_t, std::string> operand_names_; |
| 765 |
|
| 766 |
/// The section of the code being processed |
| 767 |
ModuleLayoutSection current_layout_section_; |
| 768 |
|
| 769 |
/// A list of functions in the module. |
| 770 |
/// Pointers to objects in this container are guaranteed to be stable and |
| 771 |
/// valid until the end of lifetime of the validation state. |
| 772 |
std::vector<Function> module_functions_; |
| 773 |
|
| 774 |
/// Capabilities declared in the module |
| 775 |
CapabilitySet module_capabilities_; |
| 776 |
|
| 777 |
/// Extensions declared in the module |
| 778 |
ExtensionSet module_extensions_; |
| 779 |
|
| 780 |
/// List of all instructions in the order they appear in the binary |
| 781 |
std::vector<Instruction> ordered_instructions_; |
| 782 |
|
| 783 |
/// Instructions that can be referenced by Ids |
| 784 |
std::unordered_map<uint32_t, Instruction*> all_definitions_; |
| 785 |
|
| 786 |
/// IDs that are entry points, ie, arguments to OpEntryPoint. |
| 787 |
std::vector<uint32_t> entry_points_; |
| 788 |
|
| 789 |
/// Maps an entry point id to its desciptions. |
| 790 |
std::unordered_map<uint32_t, std::vector<EntryPointDescription>> |
| 791 |
entry_point_descriptions_; |
| 792 |
|
| 793 |
/// IDs that are entry points, ie, arguments to OpEntryPoint, and root a call |
| 794 |
/// graph that recurses. |
| 795 |
std::set<uint32_t> recursive_entry_points_; |
| 796 |
|
| 797 |
/// Functions IDs that are target of OpFunctionCall. |
| 798 |
std::unordered_set<uint32_t> function_call_targets_; |
| 799 |
|
| 800 |
/// ID Bound from the Header |
| 801 |
uint32_t id_bound_; |
| 802 |
|
| 803 |
/// Set of Global Variable IDs (Storage Class other than 'Function') |
| 804 |
std::unordered_set<uint32_t> global_vars_; |
| 805 |
|
| 806 |
/// Set of Local Variable IDs ('Function' Storage Class) |
| 807 |
std::unordered_set<uint32_t> local_vars_; |
| 808 |
|
| 809 |
/// Set of struct types that have members with a BuiltIn decoration. |
| 810 |
std::unordered_set<uint32_t> builtin_structs_; |
| 811 |
|
| 812 |
/// Structure Nesting Depth |
| 813 |
std::unordered_map<uint32_t, uint32_t> struct_nesting_depth_; |
| 814 |
|
| 815 |
/// Structure has nested blockorbufferblock struct |
| 816 |
std::unordered_map<uint32_t, bool> |
| 817 |
struct_has_nested_blockorbufferblock_struct_; |
| 818 |
|
| 819 |
/// Stores the list of decorations for a given <id> |
| 820 |
std::map<uint32_t, std::vector<Decoration>> id_decorations_; |
| 821 |
|
| 822 |
/// Stores type declarations which need to be unique (i.e. non-aggregates), |
| 823 |
/// in the form [opcode, operand words], result_id is not stored. |
| 824 |
/// Using ordered set to avoid the need for a vector hash function. |
| 825 |
/// The size of this container is expected not to exceed double-digits. |
| 826 |
std::set<std::vector<uint32_t>> unique_type_declarations_; |
| 827 |
|
| 828 |
AssemblyGrammar grammar_; |
| 829 |
|
| 830 |
SpvAddressingModel addressing_model_; |
| 831 |
SpvMemoryModel memory_model_; |
| 832 |
// pointer size derived from addressing model. Assumes all storage classes |
| 833 |
// have the same pointer size (for physical pointer types). |
| 834 |
uint32_t pointer_size_and_alignment_; |
| 835 |
|
| 836 |
/// NOTE: See correspoding getter functions |
| 837 |
bool in_function_; |
| 838 |
|
| 839 |
/// The state of optional features. These are determined by capabilities |
| 840 |
/// declared by the module and the environment. |
| 841 |
Feature features_; |
| 842 |
|
| 843 |
/// Maps function ids to function stat objects. |
| 844 |
std::unordered_map<uint32_t, Function*> id_to_function_; |
| 845 |
|
| 846 |
/// Mapping entry point -> execution models. It is presumed that the same |
| 847 |
/// function could theoretically be used as 'main' by multiple OpEntryPoint |
| 848 |
/// instructions. |
| 849 |
std::unordered_map<uint32_t, std::set<SpvExecutionModel>> |
| 850 |
entry_point_to_execution_models_; |
| 851 |
|
| 852 |
/// Mapping entry point -> execution modes. |
| 853 |
std::unordered_map<uint32_t, std::set<SpvExecutionMode>> |
| 854 |
entry_point_to_execution_modes_; |
| 855 |
|
| 856 |
/// Mapping function -> array of entry points inside this |
| 857 |
/// module which can (indirectly) call the function. |
| 858 |
std::unordered_map<uint32_t, std::vector<uint32_t>> function_to_entry_points_; |
| 859 |
const std::vector<uint32_t> empty_ids_; |
| 860 |
|
| 861 |
// The IDs of types of pointers to Block-decorated structs in Uniform storage |
| 862 |
// class. This is populated at the start of ValidateDecorations. |
| 863 |
std::unordered_set<uint32_t> pointer_to_uniform_block_; |
| 864 |
// The IDs of struct types for uniform blocks. |
| 865 |
// This is populated at the start of ValidateDecorations. |
| 866 |
std::unordered_set<uint32_t> struct_for_uniform_block_; |
| 867 |
// The IDs of types of pointers to BufferBlock-decorated structs in Uniform |
| 868 |
// storage class, or Block-decorated structs in StorageBuffer storage class. |
| 869 |
// This is populated at the start of ValidateDecorations. |
| 870 |
std::unordered_set<uint32_t> pointer_to_storage_buffer_; |
| 871 |
// The IDs of struct types for storage buffers. |
| 872 |
// This is populated at the start of ValidateDecorations. |
| 873 |
std::unordered_set<uint32_t> struct_for_storage_buffer_; |
| 874 |
// The IDs of types of pointers to storage images. This is populated in the |
| 875 |
// TypePass. |
| 876 |
std::unordered_set<uint32_t> pointer_to_storage_image_; |
| 877 |
|
| 878 |
/// Maps ids to friendly names. |
| 879 |
std::unique_ptr<spvtools::FriendlyNameMapper> friendly_mapper_; |
| 880 |
spvtools::NameMapper name_mapper_; |
| 881 |
|
| 882 |
/// Variables used to reduce the number of diagnostic messages. |
| 883 |
uint32_t num_of_warnings_; |
| 884 |
uint32_t max_num_of_warnings_; |
| 885 |
}; |
| 886 |
|
| 887 |
} // namespace val |
| 888 |
} // namespace spvtools |
| 889 |
|
| 890 |
#endif // SOURCE_VAL_VALIDATION_STATE_H_ |