validate_misc.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/validation_state.h"
  20. namespace spvtools {
  21. namespace val {
  22. namespace {
  23. spv_result_t ValidateUndef(ValidationState_t& _, const Instruction* inst) {
  24. if (_.HasCapability(SpvCapabilityShader) &&
  25. _.ContainsLimitedUseIntOrFloatType(inst->type_id()) &&
  26. !_.IsPointerType(inst->type_id())) {
  27. return _.diag(SPV_ERROR_INVALID_ID, inst)
  28. << "Cannot create undefined values with 8- or 16-bit types";
  29. }
  30. return SPV_SUCCESS;
  31. }
  32. } // namespace
  33. spv_result_t MiscPass(ValidationState_t& _, const Instruction* inst) {
  34. switch (inst->opcode()) {
  35. case SpvOpUndef:
  36. if (auto error = ValidateUndef(_, inst)) return error;
  37. break;
  38. default:
  39. break;
  40. }
  41. switch (inst->opcode()) {
  42. case SpvOpBeginInvocationInterlockEXT:
  43. case SpvOpEndInvocationInterlockEXT:
  44. _.function(inst->function()->id())
  45. ->RegisterExecutionModelLimitation(
  46. SpvExecutionModelFragment,
  47. "OpBeginInvocationInterlockEXT/OpEndInvocationInterlockEXT "
  48. "require Fragment execution model");
  49. _.function(inst->function()->id())
  50. ->RegisterLimitation([](const ValidationState_t& state,
  51. const Function* entry_point,
  52. std::string* message) {
  53. const auto* execution_modes =
  54. state.GetExecutionModes(entry_point->id());
  55. auto find_interlock = [](const SpvExecutionMode& mode) {
  56. switch (mode) {
  57. case SpvExecutionModePixelInterlockOrderedEXT:
  58. case SpvExecutionModePixelInterlockUnorderedEXT:
  59. case SpvExecutionModeSampleInterlockOrderedEXT:
  60. case SpvExecutionModeSampleInterlockUnorderedEXT:
  61. case SpvExecutionModeShadingRateInterlockOrderedEXT:
  62. case SpvExecutionModeShadingRateInterlockUnorderedEXT:
  63. return true;
  64. default:
  65. return false;
  66. }
  67. };
  68. bool found = false;
  69. if (execution_modes) {
  70. auto i = std::find_if(execution_modes->begin(),
  71. execution_modes->end(), find_interlock);
  72. found = (i != execution_modes->end());
  73. }
  74. if (!found) {
  75. *message =
  76. "OpBeginInvocationInterlockEXT/OpEndInvocationInterlockEXT "
  77. "require a fragment shader interlock execution mode.";
  78. return false;
  79. }
  80. return true;
  81. });
  82. break;
  83. case SpvOpDemoteToHelperInvocationEXT:
  84. _.function(inst->function()->id())
  85. ->RegisterExecutionModelLimitation(
  86. SpvExecutionModelFragment,
  87. "OpDemoteToHelperInvocationEXT requires Fragment execution "
  88. "model");
  89. break;
  90. case SpvOpIsHelperInvocationEXT: {
  91. const uint32_t result_type = inst->type_id();
  92. _.function(inst->function()->id())
  93. ->RegisterExecutionModelLimitation(
  94. SpvExecutionModelFragment,
  95. "OpIsHelperInvocationEXT requires Fragment execution model");
  96. if (!_.IsBoolScalarType(result_type))
  97. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  98. << "Expected bool scalar type as Result Type: "
  99. << spvOpcodeString(inst->opcode());
  100. break;
  101. }
  102. default:
  103. break;
  104. }
  105. return SPV_SUCCESS;
  106. }
  107. } // namespace val
  108. } // namespace spvtools