validate_barriers.cpp 4.2 KB

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