validate_misc.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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(SpvCapabilityShader) &&
  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. if (spvIsWebGPUEnv(_.context()->target_env)) {
  36. return _.diag(SPV_ERROR_INVALID_BINARY, inst) << "OpUndef is disallowed";
  37. }
  38. return SPV_SUCCESS;
  39. }
  40. spv_result_t ValidateShaderClock(ValidationState_t& _,
  41. const Instruction* inst) {
  42. const uint32_t scope = inst->GetOperandAs<uint32_t>(2);
  43. if (auto error = ValidateScope(_, inst, scope)) {
  44. return error;
  45. }
  46. bool is_int32 = false, is_const_int32 = false;
  47. uint32_t value = 0;
  48. std::tie(is_int32, is_const_int32, value) = _.EvalInt32IfConst(scope);
  49. if (is_const_int32 && value != SpvScopeSubgroup && value != SpvScopeDevice) {
  50. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  51. << "Scope must be Subgroup or Device";
  52. }
  53. // Result Type must be a 64 - bit unsigned integer type or
  54. // a vector of two - components of 32 -
  55. // bit unsigned integer type
  56. const uint32_t result_type = inst->type_id();
  57. if (!(_.IsUnsignedIntScalarType(result_type) &&
  58. _.GetBitWidth(result_type) == 64) &&
  59. !(_.IsUnsignedIntVectorType(result_type) &&
  60. _.GetDimension(result_type) == 2 && _.GetBitWidth(result_type) == 32)) {
  61. return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Expected Value to be a "
  62. "vector of two components"
  63. " of unsigned integer"
  64. " or 64bit unsigned integer";
  65. }
  66. return SPV_SUCCESS;
  67. }
  68. } // namespace
  69. spv_result_t MiscPass(ValidationState_t& _, const Instruction* inst) {
  70. switch (inst->opcode()) {
  71. case SpvOpUndef:
  72. if (auto error = ValidateUndef(_, inst)) return error;
  73. break;
  74. default:
  75. break;
  76. }
  77. switch (inst->opcode()) {
  78. case SpvOpBeginInvocationInterlockEXT:
  79. case SpvOpEndInvocationInterlockEXT:
  80. _.function(inst->function()->id())
  81. ->RegisterExecutionModelLimitation(
  82. SpvExecutionModelFragment,
  83. "OpBeginInvocationInterlockEXT/OpEndInvocationInterlockEXT "
  84. "require Fragment execution model");
  85. _.function(inst->function()->id())
  86. ->RegisterLimitation([](const ValidationState_t& state,
  87. const Function* entry_point,
  88. std::string* message) {
  89. const auto* execution_modes =
  90. state.GetExecutionModes(entry_point->id());
  91. auto find_interlock = [](const SpvExecutionMode& mode) {
  92. switch (mode) {
  93. case SpvExecutionModePixelInterlockOrderedEXT:
  94. case SpvExecutionModePixelInterlockUnorderedEXT:
  95. case SpvExecutionModeSampleInterlockOrderedEXT:
  96. case SpvExecutionModeSampleInterlockUnorderedEXT:
  97. case SpvExecutionModeShadingRateInterlockOrderedEXT:
  98. case SpvExecutionModeShadingRateInterlockUnorderedEXT:
  99. return true;
  100. default:
  101. return false;
  102. }
  103. };
  104. bool found = false;
  105. if (execution_modes) {
  106. auto i = std::find_if(execution_modes->begin(),
  107. execution_modes->end(), find_interlock);
  108. found = (i != execution_modes->end());
  109. }
  110. if (!found) {
  111. *message =
  112. "OpBeginInvocationInterlockEXT/OpEndInvocationInterlockEXT "
  113. "require a fragment shader interlock execution mode.";
  114. return false;
  115. }
  116. return true;
  117. });
  118. break;
  119. case SpvOpDemoteToHelperInvocationEXT:
  120. _.function(inst->function()->id())
  121. ->RegisterExecutionModelLimitation(
  122. SpvExecutionModelFragment,
  123. "OpDemoteToHelperInvocationEXT requires Fragment execution "
  124. "model");
  125. break;
  126. case SpvOpIsHelperInvocationEXT: {
  127. const uint32_t result_type = inst->type_id();
  128. _.function(inst->function()->id())
  129. ->RegisterExecutionModelLimitation(
  130. SpvExecutionModelFragment,
  131. "OpIsHelperInvocationEXT requires Fragment execution model");
  132. if (!_.IsBoolScalarType(result_type))
  133. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  134. << "Expected bool scalar type as Result Type: "
  135. << spvOpcodeString(inst->opcode());
  136. break;
  137. }
  138. case SpvOpReadClockKHR:
  139. if (auto error = ValidateShaderClock(_, inst)) {
  140. return error;
  141. }
  142. break;
  143. default:
  144. break;
  145. }
  146. return SPV_SUCCESS;
  147. }
  148. } // namespace val
  149. } // namespace spvtools