validate.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. /// A function that returns a vector of BasicBlocks given a BasicBlock. Used to
  29. /// get the successor and predecessor nodes of a CFG block
  30. using get_blocks_func =
  31. std::function<const std::vector<BasicBlock*>*(const BasicBlock*)>;
  32. /// @brief Performs the Control Flow Graph checks
  33. ///
  34. /// @param[in] _ the validation state of the module
  35. ///
  36. /// @return SPV_SUCCESS if no errors are found. SPV_ERROR_INVALID_CFG otherwise
  37. spv_result_t PerformCfgChecks(ValidationState_t& _);
  38. /// @brief Updates the use vectors of all instructions that can be referenced
  39. ///
  40. /// This function will update the vector which define where an instruction was
  41. /// referenced in the binary.
  42. ///
  43. /// @param[in] _ the validation state of the module
  44. ///
  45. /// @return SPV_SUCCESS if no errors are found.
  46. spv_result_t UpdateIdUse(ValidationState_t& _, const Instruction* inst);
  47. /// @brief This function checks all ID definitions dominate their use in the
  48. /// CFG.
  49. ///
  50. /// This function will iterate over all ID definitions that are defined in the
  51. /// functions of a module and make sure that the definitions appear in a
  52. /// block that dominates their use.
  53. ///
  54. /// @param[in] _ the validation state of the module
  55. ///
  56. /// @return SPV_SUCCESS if no errors are found. SPV_ERROR_INVALID_ID otherwise
  57. spv_result_t CheckIdDefinitionDominateUse(ValidationState_t& _);
  58. /// @brief This function checks for preconditions involving the adjacent
  59. /// instructions.
  60. ///
  61. /// This function will iterate over all instructions and check for any required
  62. /// predecessor and/or successor instructions. e.g. SpvOpPhi must only be
  63. /// preceeded by SpvOpLabel, SpvOpPhi, or SpvOpLine.
  64. ///
  65. /// @param[in] _ the validation state of the module
  66. ///
  67. /// @return SPV_SUCCESS if no errors are found. SPV_ERROR_INVALID_DATA otherwise
  68. spv_result_t ValidateAdjacency(ValidationState_t& _);
  69. /// @brief Validates static uses of input and output variables
  70. ///
  71. /// Checks that any entry point that uses a input or output variable lists that
  72. /// variable in its interface.
  73. ///
  74. /// @param[in] _ the validation state of the module
  75. ///
  76. /// @return SPV_SUCCESS if no errors are found.
  77. spv_result_t ValidateInterfaces(ValidationState_t& _);
  78. /// @brief Validates memory instructions
  79. ///
  80. /// @param[in] _ the validation state of the module
  81. /// @return SPV_SUCCESS if no errors are found.
  82. spv_result_t MemoryPass(ValidationState_t& _, const Instruction* inst);
  83. /// @brief Updates the immediate dominator for each of the block edges
  84. ///
  85. /// Updates the immediate dominator of the blocks for each of the edges
  86. /// provided by the @p dom_edges parameter
  87. ///
  88. /// @param[in,out] dom_edges The edges of the dominator tree
  89. /// @param[in] set_func This function will be called to updated the Immediate
  90. /// dominator
  91. void UpdateImmediateDominators(
  92. const std::vector<std::pair<BasicBlock*, BasicBlock*>>& dom_edges,
  93. std::function<void(BasicBlock*, BasicBlock*)> set_func);
  94. /// @brief Prints all of the dominators of a BasicBlock
  95. ///
  96. /// @param[in] block The dominators of this block will be printed
  97. void printDominatorList(BasicBlock& block);
  98. /// Performs logical layout validation as described in section 2.4 of the SPIR-V
  99. /// spec.
  100. spv_result_t ModuleLayoutPass(ValidationState_t& _, const Instruction* inst);
  101. /// Performs Control Flow Graph validation and construction.
  102. spv_result_t CfgPass(ValidationState_t& _, const Instruction* inst);
  103. /// Validates Control Flow Graph instructions.
  104. spv_result_t ControlFlowPass(ValidationState_t& _, const Instruction* inst);
  105. /// Performs Id and SSA validation of a module
  106. spv_result_t IdPass(ValidationState_t& _, Instruction* inst);
  107. /// Performs instruction validation.
  108. spv_result_t InstructionPass(ValidationState_t& _, const Instruction* inst);
  109. /// Performs decoration validation. Assumes each decoration on a group
  110. /// has been propagated down to the group members.
  111. spv_result_t ValidateDecorations(ValidationState_t& _);
  112. /// Performs validation of built-in variables.
  113. spv_result_t ValidateBuiltIns(ValidationState_t& _);
  114. /// Validates type instructions.
  115. spv_result_t TypePass(ValidationState_t& _, const Instruction* inst);
  116. /// Validates constant instructions.
  117. spv_result_t ConstantPass(ValidationState_t& _, const Instruction* inst);
  118. /// Validates correctness of arithmetic instructions.
  119. spv_result_t ArithmeticsPass(ValidationState_t& _, const Instruction* inst);
  120. /// Validates correctness of composite instructions.
  121. spv_result_t CompositesPass(ValidationState_t& _, const Instruction* inst);
  122. /// Validates correctness of conversion instructions.
  123. spv_result_t ConversionPass(ValidationState_t& _, const Instruction* inst);
  124. /// Validates correctness of derivative instructions.
  125. spv_result_t DerivativesPass(ValidationState_t& _, const Instruction* inst);
  126. /// Validates correctness of logical instructions.
  127. spv_result_t LogicalsPass(ValidationState_t& _, const Instruction* inst);
  128. /// Validates correctness of bitwise instructions.
  129. spv_result_t BitwisePass(ValidationState_t& _, const Instruction* inst);
  130. /// Validates correctness of image instructions.
  131. spv_result_t ImagePass(ValidationState_t& _, const Instruction* inst);
  132. /// Validates correctness of atomic instructions.
  133. spv_result_t AtomicsPass(ValidationState_t& _, const Instruction* inst);
  134. /// Validates correctness of barrier instructions.
  135. spv_result_t BarriersPass(ValidationState_t& _, const Instruction* inst);
  136. /// Validates correctness of literal numbers.
  137. spv_result_t LiteralsPass(ValidationState_t& _, const Instruction* inst);
  138. /// Validates correctness of extension instructions.
  139. spv_result_t ExtensionPass(ValidationState_t& _, const Instruction* inst);
  140. /// Validates correctness of annotation instructions.
  141. spv_result_t AnnotationPass(ValidationState_t& _, const Instruction* inst);
  142. /// Validates correctness of non-uniform group instructions.
  143. spv_result_t NonUniformPass(ValidationState_t& _, const Instruction* inst);
  144. /// Validates correctness of debug instructions.
  145. spv_result_t DebugPass(ValidationState_t& _, const Instruction* inst);
  146. // Validates that capability declarations use operands allowed in the current
  147. // context.
  148. spv_result_t CapabilityPass(ValidationState_t& _, const Instruction* inst);
  149. /// Validates correctness of primitive instructions.
  150. spv_result_t PrimitivesPass(ValidationState_t& _, const Instruction* inst);
  151. /// Validates correctness of mode setting instructions.
  152. spv_result_t ModeSettingPass(ValidationState_t& _, const Instruction* inst);
  153. /// Validates correctness of function instructions.
  154. spv_result_t FunctionPass(ValidationState_t& _, const Instruction* inst);
  155. /// Validates correctness of miscellaneous instructions.
  156. spv_result_t MiscPass(ValidationState_t& _, const Instruction* inst);
  157. /// Calculates the reachability of basic blocks.
  158. void ReachabilityPass(ValidationState_t& _);
  159. /// Validates execution limitations.
  160. ///
  161. /// Verifies execution models are allowed for all functionality they contain.
  162. spv_result_t ValidateExecutionLimitations(ValidationState_t& _,
  163. const Instruction* inst);
  164. /// Validates restricted uses of 8- and 16-bit types.
  165. ///
  166. /// Validates shaders that uses 8- or 16-bit storage capabilities, but not full
  167. /// capabilities only have appropriate uses of those types.
  168. spv_result_t ValidateSmallTypeUses(ValidationState_t& _,
  169. const Instruction* inst);
  170. /// @brief Validate the ID's within a SPIR-V binary
  171. ///
  172. /// @param[in] pInstructions array of instructions
  173. /// @param[in] count number of elements in instruction array
  174. /// @param[in] bound the binary header
  175. /// @param[in,out] position current word in the binary
  176. /// @param[in] consumer message consumer callback
  177. ///
  178. /// @return result code
  179. spv_result_t spvValidateIDs(const spv_instruction_t* pInstructions,
  180. const uint64_t count, const uint32_t bound,
  181. spv_position position,
  182. const MessageConsumer& consumer);
  183. // Performs validation for the SPIRV-V module binary.
  184. // The main difference between this API and spvValidateBinary is that the
  185. // "Validation State" is not destroyed upon function return; it lives on and is
  186. // pointed to by the vstate unique_ptr.
  187. spv_result_t ValidateBinaryAndKeepValidationState(
  188. const spv_const_context context, spv_const_validator_options options,
  189. const uint32_t* words, const size_t num_words, spv_diagnostic* pDiagnostic,
  190. std::unique_ptr<ValidationState_t>* vstate);
  191. } // namespace val
  192. } // namespace spvtools
  193. #endif // SOURCE_VAL_VALIDATE_H_