validate_barriers.cpp 4.3 KB

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