validate.h 11 KB

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