validate_debug.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #include "source/val/validate.h"
  15. #include "source/opcode.h"
  16. #include "source/spirv_target_env.h"
  17. #include "source/val/instruction.h"
  18. #include "source/val/validation_state.h"
  19. namespace spvtools {
  20. namespace val {
  21. namespace {
  22. spv_result_t ValidateMemberName(ValidationState_t& _, const Instruction* inst) {
  23. const auto type_id = inst->GetOperandAs<uint32_t>(0);
  24. const auto type = _.FindDef(type_id);
  25. if (!type || SpvOpTypeStruct != type->opcode()) {
  26. return _.diag(SPV_ERROR_INVALID_ID, inst)
  27. << "OpMemberName Type <id> '" << _.getIdName(type_id)
  28. << "' is not a struct type.";
  29. }
  30. const auto member_id = inst->GetOperandAs<uint32_t>(1);
  31. const auto member_count = (uint32_t)(type->words().size() - 2);
  32. if (member_count <= member_id) {
  33. return _.diag(SPV_ERROR_INVALID_ID, inst)
  34. << "OpMemberName Member <id> '" << _.getIdName(member_id)
  35. << "' index is larger than Type <id> '" << _.getIdName(type->id())
  36. << "'s member count.";
  37. }
  38. return SPV_SUCCESS;
  39. }
  40. spv_result_t ValidateLine(ValidationState_t& _, const Instruction* inst) {
  41. const auto file_id = inst->GetOperandAs<uint32_t>(0);
  42. const auto file = _.FindDef(file_id);
  43. if (!file || SpvOpString != file->opcode()) {
  44. return _.diag(SPV_ERROR_INVALID_ID, inst)
  45. << "OpLine Target <id> '" << _.getIdName(file_id)
  46. << "' is not an OpString.";
  47. }
  48. return SPV_SUCCESS;
  49. }
  50. } // namespace
  51. spv_result_t DebugPass(ValidationState_t& _, const Instruction* inst) {
  52. if (spvIsWebGPUEnv(_.context()->target_env) &&
  53. spvOpcodeIsDebug(inst->opcode())) {
  54. return _.diag(SPV_ERROR_INVALID_BINARY, inst)
  55. << "Debugging instructions are not allowed in the WebGPU execution "
  56. << "environment.";
  57. }
  58. switch (inst->opcode()) {
  59. case SpvOpMemberName:
  60. if (auto error = ValidateMemberName(_, inst)) return error;
  61. break;
  62. case SpvOpLine:
  63. if (auto error = ValidateLine(_, inst)) return error;
  64. break;
  65. default:
  66. break;
  67. }
  68. return SPV_SUCCESS;
  69. }
  70. } // namespace val
  71. } // namespace spvtools