validate_misc.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Copyright (c) 2018 Google LLC.
  2. // Copyright (c) 2019 NVIDIA Corporation
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #include "source/val/validate.h"
  16. #include "source/opcode.h"
  17. #include "source/spirv_target_env.h"
  18. #include "source/val/instruction.h"
  19. #include "source/val/validate_scopes.h"
  20. #include "source/val/validation_state.h"
  21. namespace spvtools {
  22. namespace val {
  23. namespace {
  24. spv_result_t ValidateUndef(ValidationState_t& _, const Instruction* inst) {
  25. if (_.IsVoidType(inst->type_id())) {
  26. return _.diag(SPV_ERROR_INVALID_ID, inst)
  27. << "Cannot create undefined values with void type";
  28. }
  29. if (_.HasCapability(spv::Capability::Shader) &&
  30. _.ContainsLimitedUseIntOrFloatType(inst->type_id()) &&
  31. !_.IsPointerType(inst->type_id())) {
  32. return _.diag(SPV_ERROR_INVALID_ID, inst)
  33. << "Cannot create undefined values with 8- or 16-bit types";
  34. }
  35. return SPV_SUCCESS;
  36. }
  37. spv_result_t ValidateShaderClock(ValidationState_t& _,
  38. const Instruction* inst) {
  39. const uint32_t scope = inst->GetOperandAs<uint32_t>(2);
  40. if (auto error = ValidateScope(_, inst, scope)) {
  41. return error;
  42. }
  43. bool is_int32 = false, is_const_int32 = false;
  44. uint32_t value = 0;
  45. std::tie(is_int32, is_const_int32, value) = _.EvalInt32IfConst(scope);
  46. if (is_const_int32) {
  47. spv::Scope scope_val{value};
  48. if (spvIsVulkanEnv(_.context()->target_env)) {
  49. if (scope_val != spv::Scope::Subgroup &&
  50. scope_val != spv::Scope::Device) {
  51. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  52. << _.VkErrorID(4652) << "Scope must be Subgroup or Device";
  53. }
  54. } else if (spvIsOpenCLEnv(_.context()->target_env)) {
  55. if (scope_val != spv::Scope::Workgroup &&
  56. scope_val != spv::Scope::Subgroup &&
  57. scope_val != spv::Scope::Device) {
  58. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  59. << "Scope must be Subgroup, Workgroup, or Device";
  60. }
  61. }
  62. }
  63. // Result Type must be a 64 - bit unsigned integer type or
  64. // a vector of two - components of 32 -
  65. // bit unsigned integer type
  66. const uint32_t result_type = inst->type_id();
  67. if (!_.IsUnsigned64BitHandle(result_type)) {
  68. return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Expected Value to be a "
  69. "vector of two components"
  70. " of unsigned integer"
  71. " or 64bit unsigned integer";
  72. }
  73. return SPV_SUCCESS;
  74. }
  75. spv_result_t ValidateAssumeTrue(ValidationState_t& _, const Instruction* inst) {
  76. const auto operand_type_id = _.GetOperandTypeId(inst, 0);
  77. if (!operand_type_id || !_.IsBoolScalarType(operand_type_id)) {
  78. return _.diag(SPV_ERROR_INVALID_ID, inst)
  79. << "Value operand of OpAssumeTrueKHR must be a boolean scalar";
  80. }
  81. return SPV_SUCCESS;
  82. }
  83. spv_result_t ValidateExpect(ValidationState_t& _, const Instruction* inst) {
  84. const auto result_type = inst->type_id();
  85. if (!_.IsBoolScalarOrVectorType(result_type) &&
  86. !_.IsIntScalarOrVectorType(result_type)) {
  87. return _.diag(SPV_ERROR_INVALID_ID, inst)
  88. << "Result of OpExpectKHR must be a scalar or vector of integer "
  89. "type or boolean type";
  90. }
  91. if (_.GetOperandTypeId(inst, 2) != result_type) {
  92. return _.diag(SPV_ERROR_INVALID_ID, inst)
  93. << "Type of Value operand of OpExpectKHR does not match the result "
  94. "type ";
  95. }
  96. if (_.GetOperandTypeId(inst, 3) != result_type) {
  97. return _.diag(SPV_ERROR_INVALID_ID, inst)
  98. << "Type of ExpectedValue operand of OpExpectKHR does not match the "
  99. "result type ";
  100. }
  101. return SPV_SUCCESS;
  102. }
  103. } // namespace
  104. spv_result_t MiscPass(ValidationState_t& _, const Instruction* inst) {
  105. switch (inst->opcode()) {
  106. case spv::Op::OpUndef:
  107. if (auto error = ValidateUndef(_, inst)) return error;
  108. break;
  109. default:
  110. break;
  111. }
  112. switch (inst->opcode()) {
  113. case spv::Op::OpBeginInvocationInterlockEXT:
  114. case spv::Op::OpEndInvocationInterlockEXT:
  115. _.function(inst->function()->id())
  116. ->RegisterExecutionModelLimitation(
  117. spv::ExecutionModel::Fragment,
  118. "OpBeginInvocationInterlockEXT/OpEndInvocationInterlockEXT "
  119. "require Fragment execution model");
  120. _.function(inst->function()->id())
  121. ->RegisterLimitation([](const ValidationState_t& state,
  122. const Function* entry_point,
  123. std::string* message) {
  124. const auto* execution_modes =
  125. state.GetExecutionModes(entry_point->id());
  126. auto find_interlock = [](const spv::ExecutionMode& mode) {
  127. switch (mode) {
  128. case spv::ExecutionMode::PixelInterlockOrderedEXT:
  129. case spv::ExecutionMode::PixelInterlockUnorderedEXT:
  130. case spv::ExecutionMode::SampleInterlockOrderedEXT:
  131. case spv::ExecutionMode::SampleInterlockUnorderedEXT:
  132. case spv::ExecutionMode::ShadingRateInterlockOrderedEXT:
  133. case spv::ExecutionMode::ShadingRateInterlockUnorderedEXT:
  134. return true;
  135. default:
  136. return false;
  137. }
  138. };
  139. bool found = false;
  140. if (execution_modes) {
  141. auto i = std::find_if(execution_modes->begin(),
  142. execution_modes->end(), find_interlock);
  143. found = (i != execution_modes->end());
  144. }
  145. if (!found) {
  146. *message =
  147. "OpBeginInvocationInterlockEXT/OpEndInvocationInterlockEXT "
  148. "require a fragment shader interlock execution mode.";
  149. return false;
  150. }
  151. return true;
  152. });
  153. break;
  154. case spv::Op::OpDemoteToHelperInvocationEXT:
  155. _.function(inst->function()->id())
  156. ->RegisterExecutionModelLimitation(
  157. spv::ExecutionModel::Fragment,
  158. "OpDemoteToHelperInvocationEXT requires Fragment execution "
  159. "model");
  160. break;
  161. case spv::Op::OpIsHelperInvocationEXT: {
  162. const uint32_t result_type = inst->type_id();
  163. _.function(inst->function()->id())
  164. ->RegisterExecutionModelLimitation(
  165. spv::ExecutionModel::Fragment,
  166. "OpIsHelperInvocationEXT requires Fragment execution model");
  167. if (!_.IsBoolScalarType(result_type))
  168. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  169. << "Expected bool scalar type as Result Type: "
  170. << spvOpcodeString(inst->opcode());
  171. break;
  172. }
  173. case spv::Op::OpReadClockKHR:
  174. if (auto error = ValidateShaderClock(_, inst)) {
  175. return error;
  176. }
  177. break;
  178. case spv::Op::OpAssumeTrueKHR:
  179. if (auto error = ValidateAssumeTrue(_, inst)) {
  180. return error;
  181. }
  182. break;
  183. case spv::Op::OpExpectKHR:
  184. if (auto error = ValidateExpect(_, inst)) {
  185. return error;
  186. }
  187. break;
  188. default:
  189. break;
  190. }
  191. return SPV_SUCCESS;
  192. }
  193. } // namespace val
  194. } // namespace spvtools