validate_barriers.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright (c) 2018 Google LLC.
  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. // Validates correctness of barrier SPIR-V instructions.
  15. #include "source/val/validate.h"
  16. #include <string>
  17. #include "source/diagnostic.h"
  18. #include "source/opcode.h"
  19. #include "source/spirv_constant.h"
  20. #include "source/spirv_target_env.h"
  21. #include "source/util/bitutils.h"
  22. #include "source/val/instruction.h"
  23. #include "source/val/validate_memory_semantics.h"
  24. #include "source/val/validate_scopes.h"
  25. #include "source/val/validation_state.h"
  26. namespace spvtools {
  27. namespace val {
  28. // Validates correctness of barrier instructions.
  29. spv_result_t BarriersPass(ValidationState_t& _, const Instruction* inst) {
  30. const SpvOp opcode = inst->opcode();
  31. const uint32_t result_type = inst->type_id();
  32. switch (opcode) {
  33. case SpvOpControlBarrier: {
  34. if (spvVersionForTargetEnv(_.context()->target_env) <
  35. SPV_SPIRV_VERSION_WORD(1, 3)) {
  36. _.function(inst->function()->id())
  37. ->RegisterExecutionModelLimitation(
  38. [](SpvExecutionModel model, std::string* message) {
  39. if (model != SpvExecutionModelTessellationControl &&
  40. model != SpvExecutionModelGLCompute &&
  41. model != SpvExecutionModelKernel &&
  42. model != SpvExecutionModelTaskNV &&
  43. model != SpvExecutionModelMeshNV) {
  44. if (message) {
  45. *message =
  46. "OpControlBarrier requires one of the following "
  47. "Execution "
  48. "Models: TessellationControl, GLCompute or Kernel";
  49. }
  50. return false;
  51. }
  52. return true;
  53. });
  54. }
  55. const uint32_t execution_scope = inst->word(1);
  56. const uint32_t memory_scope = inst->word(2);
  57. if (auto error = ValidateExecutionScope(_, inst, execution_scope)) {
  58. return error;
  59. }
  60. if (auto error = ValidateMemoryScope(_, inst, memory_scope)) {
  61. return error;
  62. }
  63. if (auto error = ValidateMemorySemantics(_, inst, 2)) {
  64. return error;
  65. }
  66. break;
  67. }
  68. case SpvOpMemoryBarrier: {
  69. const uint32_t memory_scope = inst->word(1);
  70. if (auto error = ValidateMemoryScope(_, inst, memory_scope)) {
  71. return error;
  72. }
  73. if (auto error = ValidateMemorySemantics(_, inst, 1)) {
  74. return error;
  75. }
  76. break;
  77. }
  78. case SpvOpNamedBarrierInitialize: {
  79. if (_.GetIdOpcode(result_type) != SpvOpTypeNamedBarrier) {
  80. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  81. << spvOpcodeString(opcode)
  82. << ": expected Result Type to be OpTypeNamedBarrier";
  83. }
  84. const uint32_t subgroup_count_type = _.GetOperandTypeId(inst, 2);
  85. if (!_.IsIntScalarType(subgroup_count_type) ||
  86. _.GetBitWidth(subgroup_count_type) != 32) {
  87. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  88. << spvOpcodeString(opcode)
  89. << ": expected Subgroup Count to be a 32-bit int";
  90. }
  91. break;
  92. }
  93. case SpvOpMemoryNamedBarrier: {
  94. const uint32_t named_barrier_type = _.GetOperandTypeId(inst, 0);
  95. if (_.GetIdOpcode(named_barrier_type) != SpvOpTypeNamedBarrier) {
  96. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  97. << spvOpcodeString(opcode)
  98. << ": expected Named Barrier to be of type OpTypeNamedBarrier";
  99. }
  100. const uint32_t memory_scope = inst->word(2);
  101. if (auto error = ValidateMemoryScope(_, inst, memory_scope)) {
  102. return error;
  103. }
  104. if (auto error = ValidateMemorySemantics(_, inst, 2)) {
  105. return error;
  106. }
  107. break;
  108. }
  109. default:
  110. break;
  111. }
  112. return SPV_SUCCESS;
  113. }
  114. } // namespace val
  115. } // namespace spvtools