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 entry point call tree requirements of 86 /// SPV_KHR_float_controls2 87 /// 88 /// Checks that no entry point using FPFastMathDefault uses: 89 /// * FPFastMathMode Fast 90 /// * NoContraction 91 /// 92 /// @param[in] _ the validation state of the module 93 /// 94 /// @return SPV_SUCCESS if no errors are found. 95 spv_result_t ValidateFloatControls2(ValidationState_t& _); 96 97 /// @brief Validates duplicated execution modes for each entry point. 98 /// 99 /// @param[in] _ the validation state of the module 100 /// 101 /// @return SPV_SUCCESS if no errors are found. 102 spv_result_t ValidateDuplicateExecutionModes(ValidationState_t& _); 103 104 /// @brief Validates memory instructions 105 /// 106 /// @param[in] _ the validation state of the module 107 /// @return SPV_SUCCESS if no errors are found. 108 spv_result_t MemoryPass(ValidationState_t& _, const Instruction* inst); 109 110 /// @brief Updates the immediate dominator for each of the block edges 111 /// 112 /// Updates the immediate dominator of the blocks for each of the edges 113 /// provided by the @p dom_edges parameter 114 /// 115 /// @param[in,out] dom_edges The edges of the dominator tree 116 /// @param[in] set_func This function will be called to updated the Immediate 117 /// dominator 118 void UpdateImmediateDominators( 119 const std::vector<std::pair<BasicBlock*, BasicBlock*>>& dom_edges, 120 std::function<void(BasicBlock*, BasicBlock*)> set_func); 121 122 /// @brief Prints all of the dominators of a BasicBlock 123 /// 124 /// @param[in] block The dominators of this block will be printed 125 void printDominatorList(BasicBlock& block); 126 127 /// Performs logical layout validation as described in section 2.4 of the SPIR-V 128 /// spec. 129 spv_result_t ModuleLayoutPass(ValidationState_t& _, const Instruction* inst); 130 131 /// Performs Control Flow Graph validation and construction. 132 spv_result_t CfgPass(ValidationState_t& _, const Instruction* inst); 133 134 /// Validates Control Flow Graph instructions. 135 spv_result_t ControlFlowPass(ValidationState_t& _, const Instruction* inst); 136 137 /// Performs Id and SSA validation of a module 138 spv_result_t IdPass(ValidationState_t& _, Instruction* inst); 139 140 /// Performs instruction validation. 141 spv_result_t InstructionPass(ValidationState_t& _, const Instruction* inst); 142 143 /// Performs decoration validation. Assumes each decoration on a group 144 /// has been propagated down to the group members. 145 spv_result_t ValidateDecorations(ValidationState_t& _); 146 147 /// Performs validation of built-in variables. 148 spv_result_t ValidateBuiltIns(ValidationState_t& _); 149 150 /// Validates type instructions. 151 spv_result_t TypePass(ValidationState_t& _, const Instruction* inst); 152 153 /// Validates constant instructions. 154 spv_result_t ConstantPass(ValidationState_t& _, const Instruction* inst); 155 156 /// Validates correctness of arithmetic instructions. 157 spv_result_t ArithmeticsPass(ValidationState_t& _, const Instruction* inst); 158 159 /// Validates correctness of composite instructions. 160 spv_result_t CompositesPass(ValidationState_t& _, const Instruction* inst); 161 162 /// Validates correctness of conversion instructions. 163 spv_result_t ConversionPass(ValidationState_t& _, const Instruction* inst); 164 165 /// Validates correctness of derivative instructions. 166 spv_result_t DerivativesPass(ValidationState_t& _, const Instruction* inst); 167 168 /// Validates correctness of logical instructions. 169 spv_result_t LogicalsPass(ValidationState_t& _, const Instruction* inst); 170 171 /// Validates correctness of bitwise instructions. 172 spv_result_t BitwisePass(ValidationState_t& _, const Instruction* inst); 173 174 /// Validates correctness of image instructions. 175 spv_result_t ImagePass(ValidationState_t& _, const Instruction* inst); 176 177 /// Validates correctness of atomic instructions. 178 spv_result_t AtomicsPass(ValidationState_t& _, const Instruction* inst); 179 180 /// Validates correctness of barrier instructions. 181 spv_result_t BarriersPass(ValidationState_t& _, const Instruction* inst); 182 183 /// Validates correctness of literal numbers. 184 spv_result_t LiteralsPass(ValidationState_t& _, const Instruction* inst); 185 186 /// Validates correctness of extension instructions. 187 spv_result_t ExtensionPass(ValidationState_t& _, const Instruction* inst); 188 189 /// Validates correctness of annotation instructions. 190 spv_result_t AnnotationPass(ValidationState_t& _, const Instruction* inst); 191 192 /// Validates correctness of non-uniform group instructions. 193 spv_result_t NonUniformPass(ValidationState_t& _, const Instruction* inst); 194 195 /// Validates correctness of debug instructions. 196 spv_result_t DebugPass(ValidationState_t& _, const Instruction* inst); 197 198 // Validates that capability declarations use operands allowed in the current 199 // context. 200 spv_result_t CapabilityPass(ValidationState_t& _, const Instruction* inst); 201 202 /// Validates correctness of primitive instructions. 203 spv_result_t PrimitivesPass(ValidationState_t& _, const Instruction* inst); 204 205 /// Validates correctness of mode setting instructions. 206 spv_result_t ModeSettingPass(ValidationState_t& _, const Instruction* inst); 207 208 /// Validates correctness of function instructions. 209 spv_result_t FunctionPass(ValidationState_t& _, const Instruction* inst); 210 211 /// Validates correctness of miscellaneous instructions. 212 spv_result_t MiscPass(ValidationState_t& _, const Instruction* inst); 213 214 /// Validates correctness of ray query instructions. 215 spv_result_t RayQueryPass(ValidationState_t& _, const Instruction* inst); 216 217 /// Validates correctness of ray tracing instructions. 218 spv_result_t RayTracingPass(ValidationState_t& _, const Instruction* inst); 219 220 /// Validates correctness of shader execution reorder instructions. 221 spv_result_t RayReorderNVPass(ValidationState_t& _, const Instruction* inst); 222 223 /// Validates correctness of mesh shading instructions. 224 spv_result_t MeshShadingPass(ValidationState_t& _, const Instruction* inst); 225 226 /// Calculates the reachability of basic blocks. 227 void ReachabilityPass(ValidationState_t& _); 228 229 /// Validates execution limitations. 230 /// 231 /// Verifies execution models are allowed for all functionality they contain. 232 spv_result_t ValidateExecutionLimitations(ValidationState_t& _, 233 const Instruction* inst); 234 235 /// Validates restricted uses of 8- and 16-bit types. 236 /// 237 /// Validates shaders that uses 8- or 16-bit storage capabilities, but not full 238 /// capabilities only have appropriate uses of those types. 239 spv_result_t ValidateSmallTypeUses(ValidationState_t& _, 240 const Instruction* inst); 241 242 /// Validates restricted uses of QCOM decorated textures 243 /// 244 /// The textures that are decorated with some of QCOM image processing 245 /// decorations must be used in the specified QCOM image processing built-in 246 /// functions and not used in any other image functions. 247 spv_result_t ValidateQCOMImageProcessingTextureUsages(ValidationState_t& _, 248 const Instruction* inst); 249 250 /// @brief Validate the ID's within a SPIR-V binary 251 /// 252 /// @param[in] pInstructions array of instructions 253 /// @param[in] count number of elements in instruction array 254 /// @param[in] bound the binary header 255 /// @param[in,out] position current word in the binary 256 /// @param[in] consumer message consumer callback 257 /// 258 /// @return result code 259 spv_result_t spvValidateIDs(const spv_instruction_t* pInstructions, 260 const uint64_t count, const uint32_t bound, 261 spv_position position, 262 const MessageConsumer& consumer); 263 264 // Performs validation for the SPIRV-V module binary. 265 // The main difference between this API and spvValidateBinary is that the 266 // "Validation State" is not destroyed upon function return; it lives on and is 267 // pointed to by the vstate unique_ptr. 268 spv_result_t ValidateBinaryAndKeepValidationState( 269 const spv_const_context context, spv_const_validator_options options, 270 const uint32_t* words, const size_t num_words, spv_diagnostic* pDiagnostic, 271 std::unique_ptr<ValidationState_t>* vstate); 272 273 } // namespace val 274 } // namespace spvtools 275 276 #endif // SOURCE_VAL_VALIDATE_H_ 277