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 LIBSPIRV_VALIDATE_H_ 16 #define LIBSPIRV_VALIDATE_H_ 17 18 #include <functional> 19 #include <utility> 20 #include <vector> 21 22 #include "instruction.h" 23 #include "message.h" 24 #include "spirv-tools/libspirv.h" 25 #include "table.h" 26 27 namespace libspirv { 28 29 class ValidationState_t; 30 class BasicBlock; 31 32 /// A function that returns a vector of BasicBlocks given a BasicBlock. Used to 33 /// get the successor and predecessor nodes of a CFG block 34 using get_blocks_func = 35 std::function<const std::vector<BasicBlock*>*(const BasicBlock*)>; 36 37 /// @brief Performs the Control Flow Graph checks 38 /// 39 /// @param[in] _ the validation state of the module 40 /// 41 /// @return SPV_SUCCESS if no errors are found. SPV_ERROR_INVALID_CFG otherwise 42 spv_result_t PerformCfgChecks(ValidationState_t& _); 43 44 /// @brief Updates the use vectors of all instructions that can be referenced 45 /// 46 /// This function will update the vector which define where an instruction was 47 /// referenced in the binary. 48 /// 49 /// @param[in] _ the validation state of the module 50 /// 51 /// @return SPV_SUCCESS if no errors are found. 52 spv_result_t UpdateIdUse(ValidationState_t& _); 53 54 /// @brief This function checks all ID definitions dominate their use in the 55 /// CFG. 56 /// 57 /// This function will iterate over all ID definitions that are defined in the 58 /// functions of a module and make sure that the definitions appear in a 59 /// block that dominates their use. 60 /// 61 /// @param[in] _ the validation state of the module 62 /// 63 /// @return SPV_SUCCESS if no errors are found. SPV_ERROR_INVALID_ID otherwise 64 spv_result_t CheckIdDefinitionDominateUse(const ValidationState_t& _); 65 66 /// @brief Updates the immediate dominator for each of the block edges 67 /// 68 /// Updates the immediate dominator of the blocks for each of the edges 69 /// provided by the @p dom_edges parameter 70 /// 71 /// @param[in,out] dom_edges The edges of the dominator tree 72 /// @param[in] set_func This function will be called to updated the Immediate 73 /// dominator 74 void UpdateImmediateDominators( 75 const std::vector<std::pair<BasicBlock*, BasicBlock*>>& dom_edges, 76 std::function<void(BasicBlock*, BasicBlock*)> set_func); 77 78 /// @brief Prints all of the dominators of a BasicBlock 79 /// 80 /// @param[in] block The dominators of this block will be printed 81 void printDominatorList(BasicBlock& block); 82 83 /// Performs logical layout validation as described in section 2.4 of the SPIR-V 84 /// spec. 85 spv_result_t ModuleLayoutPass(ValidationState_t& _, 86 const spv_parsed_instruction_t* inst); 87 88 /// Performs Control Flow Graph validation of a module 89 spv_result_t CfgPass(ValidationState_t& _, 90 const spv_parsed_instruction_t* inst); 91 92 /// Performs Id and SSA validation of a module 93 spv_result_t IdPass(ValidationState_t& _, const spv_parsed_instruction_t* inst); 94 95 /// Performs validation of the Data Rules subsection of 2.16.1 Universal 96 /// Validation Rules. 97 /// TODO(ehsann): add more comments here as more validation code is added. 98 spv_result_t DataRulesPass(ValidationState_t& _, 99 const spv_parsed_instruction_t* inst); 100 101 /// Performs instruction validation. 102 spv_result_t InstructionPass(ValidationState_t& _, 103 const spv_parsed_instruction_t* inst); 104 105 /// Performs decoration validation. 106 spv_result_t ValidateDecorations(ValidationState_t& _); 107 108 /// Validates that type declarations are unique, unless multiple declarations 109 /// of the same data type are allowed by the specification. 110 /// (see section 2.8 Types and Variables) 111 spv_result_t TypeUniquePass(ValidationState_t& _, 112 const spv_parsed_instruction_t* inst); 113 114 // Validates that capability declarations use operands allowed in the current 115 // context. 116 spv_result_t CapabilityPass(ValidationState_t& _, 117 const spv_parsed_instruction_t* inst); 118 119 } // namespace libspirv 120 121 /// @brief Validate the ID usage of the instruction stream 122 /// 123 /// @param[in] pInsts stream of instructions 124 /// @param[in] instCount number of instructions 125 /// @param[in] opcodeTable table of specified Opcodes 126 /// @param[in] operandTable table of specified operands 127 /// @param[in] usedefs use-def info from module parsing 128 /// @param[in,out] position current position in the stream 129 /// 130 /// @return result code 131 spv_result_t spvValidateInstructionIDs(const spv_instruction_t* pInsts, 132 const uint64_t instCount, 133 const spv_opcode_table opcodeTable, 134 const spv_operand_table operandTable, 135 const spv_ext_inst_table extInstTable, 136 const libspirv::ValidationState_t& state, 137 spv_position position); 138 139 /// @brief Validate the ID's within a SPIR-V binary 140 /// 141 /// @param[in] pInstructions array of instructions 142 /// @param[in] count number of elements in instruction array 143 /// @param[in] bound the binary header 144 /// @param[in] opcodeTable table of specified Opcodes 145 /// @param[in] operandTable table of specified operands 146 /// @param[in,out] position current word in the binary 147 /// @param[in] consumer message consumer callback 148 /// 149 /// @return result code 150 spv_result_t spvValidateIDs(const spv_instruction_t* pInstructions, 151 const uint64_t count, const uint32_t bound, 152 const spv_opcode_table opcodeTable, 153 const spv_operand_table operandTable, 154 const spv_ext_inst_table extInstTable, 155 spv_position position, 156 const spvtools::MessageConsumer& consumer); 157 158 namespace spvtools { 159 // Performs validation for the SPIRV-V module binary. 160 // The main difference between this API and spvValidateBinary is that the 161 // "Validation State" is not destroyed upon function return; it lives on and is 162 // pointed to by the vstate unique_ptr. 163 spv_result_t ValidateBinaryAndKeepValidationState( 164 const spv_const_context context, spv_const_validator_options options, 165 const uint32_t* words, const size_t num_words, spv_diagnostic* pDiagnostic, 166 std::unique_ptr<libspirv::ValidationState_t>* vstate); 167 168 // Performs validation for a single instruction and updates given validation 169 // state. 170 spv_result_t ValidateInstructionAndUpdateValidationState( 171 libspirv::ValidationState_t* vstate, const spv_parsed_instruction_t* inst); 172 173 } // namespace spvtools 174 175 #endif // LIBSPIRV_VALIDATE_H_ 176