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