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_VALIDATE_H_ 16 #define SOURCE_VAL_VALIDATE_H_ 17 18 #include <functional> 19 #include <memory> 20 #include <utility> 21 #include <vector> 22 23 #include "source/instruction.h" 24 #include "source/table.h" 25 #include "spirv-tools/libspirv.h" 26 27 namespace spvtools { 28 namespace val { 29 30 class ValidationState_t; 31 class BasicBlock; 32 class Instruction; 33 34 /// @brief Performs the Control Flow Graph checks 35 /// 36 /// @param[in] _ the validation state of the module 37 /// 38 /// @return SPV_SUCCESS if no errors are found. SPV_ERROR_INVALID_CFG otherwise 39 spv_result_t PerformCfgChecks(ValidationState_t& _); 40 41 /// @brief Updates the use vectors of all instructions that can be referenced 42 /// 43 /// This function will update the vector which define where an instruction was 44 /// referenced in the binary. 45 /// 46 /// @param[in] _ the validation state of the module 47 /// 48 /// @return SPV_SUCCESS if no errors are found. 49 spv_result_t UpdateIdUse(ValidationState_t& _, const Instruction* inst); 50 51 /// @brief This function checks all ID definitions dominate their use in the 52 /// CFG. 53 /// 54 /// This function will iterate over all ID definitions that are defined in the 55 /// functions of a module and make sure that the definitions appear in a 56 /// block that dominates their use. 57 /// 58 /// @param[in] _ the validation state of the module 59 /// 60 /// @return SPV_SUCCESS if no errors are found. SPV_ERROR_INVALID_ID otherwise 61 spv_result_t CheckIdDefinitionDominateUse(ValidationState_t& _); 62 63 /// @brief This function checks for preconditions involving the adjacent 64 /// instructions. 65 /// 66 /// This function will iterate over all instructions and check for any required 67 /// predecessor and/or successor instructions. e.g. spv::Op::OpPhi must only be 68 /// preceded by spv::Op::OpLabel, spv::Op::OpPhi, or spv::Op::OpLine. 69 /// 70 /// @param[in] _ the validation state of the module 71 /// 72 /// @return SPV_SUCCESS if no errors are found. SPV_ERROR_INVALID_DATA otherwise 73 spv_result_t ValidateAdjacency(ValidationState_t& _); 74 75 /// @brief Validates static uses of input and output variables 76 /// 77 /// Checks that any entry point that uses a input or output variable lists that 78 /// variable in its interface. 79 /// 80 /// @param[in] _ the validation state of the module 81 /// 82 /// @return SPV_SUCCESS if no errors are found. 83 spv_result_t ValidateInterfaces(ValidationState_t& _); 84 85 /// @brief Validates memory instructions 86 /// 87 /// @param[in] _ the validation state of the module 88 /// @return SPV_SUCCESS if no errors are found. 89 spv_result_t MemoryPass(ValidationState_t& _, const Instruction* inst); 90 91 /// @brief Updates the immediate dominator for each of the block edges 92 /// 93 /// Updates the immediate dominator of the blocks for each of the edges 94 /// provided by the @p dom_edges parameter 95 /// 96 /// @param[in,out] dom_edges The edges of the dominator tree 97 /// @param[in] set_func This function will be called to updated the Immediate 98 /// dominator 99 void UpdateImmediateDominators( 100 const std::vector<std::pair<BasicBlock*, BasicBlock*>>& dom_edges, 101 std::function<void(BasicBlock*, BasicBlock*)> set_func); 102 103 /// @brief Prints all of the dominators of a BasicBlock 104 /// 105 /// @param[in] block The dominators of this block will be printed 106 void printDominatorList(BasicBlock& block); 107 108 /// Performs logical layout validation as described in section 2.4 of the SPIR-V 109 /// spec. 110 spv_result_t ModuleLayoutPass(ValidationState_t& _, const Instruction* inst); 111 112 /// Performs Control Flow Graph validation and construction. 113 spv_result_t CfgPass(ValidationState_t& _, const Instruction* inst); 114 115 /// Validates Control Flow Graph instructions. 116 spv_result_t ControlFlowPass(ValidationState_t& _, const Instruction* inst); 117 118 /// Performs Id and SSA validation of a module 119 spv_result_t IdPass(ValidationState_t& _, Instruction* inst); 120 121 /// Performs instruction validation. 122 spv_result_t InstructionPass(ValidationState_t& _, const Instruction* inst); 123 124 /// Performs decoration validation. Assumes each decoration on a group 125 /// has been propagated down to the group members. 126 spv_result_t ValidateDecorations(ValidationState_t& _); 127 128 /// Performs validation of built-in variables. 129 spv_result_t ValidateBuiltIns(ValidationState_t& _); 130 131 /// Validates type instructions. 132 spv_result_t TypePass(ValidationState_t& _, const Instruction* inst); 133 134 /// Validates constant instructions. 135 spv_result_t ConstantPass(ValidationState_t& _, const Instruction* inst); 136 137 /// Validates correctness of arithmetic instructions. 138 spv_result_t ArithmeticsPass(ValidationState_t& _, const Instruction* inst); 139 140 /// Validates correctness of composite instructions. 141 spv_result_t CompositesPass(ValidationState_t& _, const Instruction* inst); 142 143 /// Validates correctness of conversion instructions. 144 spv_result_t ConversionPass(ValidationState_t& _, const Instruction* inst); 145 146 /// Validates correctness of derivative instructions. 147 spv_result_t DerivativesPass(ValidationState_t& _, const Instruction* inst); 148 149 /// Validates correctness of logical instructions. 150 spv_result_t LogicalsPass(ValidationState_t& _, const Instruction* inst); 151 152 /// Validates correctness of bitwise instructions. 153 spv_result_t BitwisePass(ValidationState_t& _, const Instruction* inst); 154 155 /// Validates correctness of image instructions. 156 spv_result_t ImagePass(ValidationState_t& _, const Instruction* inst); 157 158 /// Validates correctness of atomic instructions. 159 spv_result_t AtomicsPass(ValidationState_t& _, const Instruction* inst); 160 161 /// Validates correctness of barrier instructions. 162 spv_result_t BarriersPass(ValidationState_t& _, const Instruction* inst); 163 164 /// Validates correctness of literal numbers. 165 spv_result_t LiteralsPass(ValidationState_t& _, const Instruction* inst); 166 167 /// Validates correctness of extension instructions. 168 spv_result_t ExtensionPass(ValidationState_t& _, const Instruction* inst); 169 170 /// Validates correctness of annotation instructions. 171 spv_result_t AnnotationPass(ValidationState_t& _, const Instruction* inst); 172 173 /// Validates correctness of non-uniform group instructions. 174 spv_result_t NonUniformPass(ValidationState_t& _, const Instruction* inst); 175 176 /// Validates correctness of debug instructions. 177 spv_result_t DebugPass(ValidationState_t& _, const Instruction* inst); 178 179 // Validates that capability declarations use operands allowed in the current 180 // context. 181 spv_result_t CapabilityPass(ValidationState_t& _, const Instruction* inst); 182 183 /// Validates correctness of primitive instructions. 184 spv_result_t PrimitivesPass(ValidationState_t& _, const Instruction* inst); 185 186 /// Validates correctness of mode setting instructions. 187 spv_result_t ModeSettingPass(ValidationState_t& _, const Instruction* inst); 188 189 /// Validates correctness of function instructions. 190 spv_result_t FunctionPass(ValidationState_t& _, const Instruction* inst); 191 192 /// Validates correctness of miscellaneous instructions. 193 spv_result_t MiscPass(ValidationState_t& _, const Instruction* inst); 194 195 /// Validates correctness of ray query instructions. 196 spv_result_t RayQueryPass(ValidationState_t& _, const Instruction* inst); 197 198 /// Validates correctness of ray tracing instructions. 199 spv_result_t RayTracingPass(ValidationState_t& _, const Instruction* inst); 200 201 /// Validates correctness of shader execution reorder instructions. 202 spv_result_t RayReorderNVPass(ValidationState_t& _, const Instruction* inst); 203 204 /// Validates correctness of mesh shading instructions. 205 spv_result_t MeshShadingPass(ValidationState_t& _, const Instruction* inst); 206 207 /// Calculates the reachability of basic blocks. 208 void ReachabilityPass(ValidationState_t& _); 209 210 /// Validates execution limitations. 211 /// 212 /// Verifies execution models are allowed for all functionality they contain. 213 spv_result_t ValidateExecutionLimitations(ValidationState_t& _, 214 const Instruction* inst); 215 216 /// Validates restricted uses of 8- and 16-bit types. 217 /// 218 /// Validates shaders that uses 8- or 16-bit storage capabilities, but not full 219 /// capabilities only have appropriate uses of those types. 220 spv_result_t ValidateSmallTypeUses(ValidationState_t& _, 221 const Instruction* inst); 222 223 /// Validates restricted uses of QCOM decorated textures 224 /// 225 /// The textures that are decorated with some of QCOM image processing 226 /// decorations must be used in the specified QCOM image processing built-in 227 /// functions and not used in any other image functions. 228 spv_result_t ValidateQCOMImageProcessingTextureUsages(ValidationState_t& _, 229 const Instruction* inst); 230 231 /// @brief Validate the ID's within a SPIR-V binary 232 /// 233 /// @param[in] pInstructions array of instructions 234 /// @param[in] count number of elements in instruction array 235 /// @param[in] bound the binary header 236 /// @param[in,out] position current word in the binary 237 /// @param[in] consumer message consumer callback 238 /// 239 /// @return result code 240 spv_result_t spvValidateIDs(const spv_instruction_t* pInstructions, 241 const uint64_t count, const uint32_t bound, 242 spv_position position, 243 const MessageConsumer& consumer); 244 245 // Performs validation for the SPIRV-V module binary. 246 // The main difference between this API and spvValidateBinary is that the 247 // "Validation State" is not destroyed upon function return; it lives on and is 248 // pointed to by the vstate unique_ptr. 249 spv_result_t ValidateBinaryAndKeepValidationState( 250 const spv_const_context context, spv_const_validator_options options, 251 const uint32_t* words, const size_t num_words, spv_diagnostic* pDiagnostic, 252 std::unique_ptr<ValidationState_t>* vstate); 253 254 } // namespace val 255 } // namespace spvtools 256 257 #endif // SOURCE_VAL_VALIDATE_H_ 258