validate_non_uniform.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 "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_scopes.h"
  23. #include "source/val/validation_state.h"
  24. namespace spvtools {
  25. namespace val {
  26. namespace {
  27. spv_result_t ValidateGroupNonUniformBallotBitCount(ValidationState_t& _,
  28. const Instruction* inst) {
  29. // Scope is already checked by ValidateExecutionScope() above.
  30. const uint32_t result_type = inst->type_id();
  31. if (!_.IsUnsignedIntScalarType(result_type)) {
  32. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  33. << "Expected Result Type to be an unsigned integer type scalar.";
  34. }
  35. const auto value = inst->GetOperandAs<uint32_t>(4);
  36. const auto value_type = _.FindDef(value)->type_id();
  37. if (!_.IsUnsignedIntVectorType(value_type) ||
  38. _.GetDimension(value_type) != 4) {
  39. return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Expected Value to be a "
  40. "vector of four components "
  41. "of integer type scalar";
  42. }
  43. return SPV_SUCCESS;
  44. }
  45. } // namespace
  46. // Validates correctness of non-uniform group instructions.
  47. spv_result_t NonUniformPass(ValidationState_t& _, const Instruction* inst) {
  48. const SpvOp opcode = inst->opcode();
  49. if (spvOpcodeIsNonUniformGroupOperation(opcode)) {
  50. const uint32_t execution_scope = inst->word(3);
  51. if (auto error = ValidateExecutionScope(_, inst, execution_scope)) {
  52. return error;
  53. }
  54. }
  55. switch (opcode) {
  56. case SpvOpGroupNonUniformBallotBitCount:
  57. return ValidateGroupNonUniformBallotBitCount(_, inst);
  58. default:
  59. break;
  60. }
  61. return SPV_SUCCESS;
  62. }
  63. } // namespace val
  64. } // namespace spvtools