validate_interfaces.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <algorithm>
  15. #include <vector>
  16. #include "source/diagnostic.h"
  17. #include "source/spirv_constant.h"
  18. #include "source/spirv_target_env.h"
  19. #include "source/val/function.h"
  20. #include "source/val/instruction.h"
  21. #include "source/val/validate.h"
  22. #include "source/val/validation_state.h"
  23. namespace spvtools {
  24. namespace val {
  25. namespace {
  26. // Returns true if \c inst is an input or output variable.
  27. bool is_interface_variable(const Instruction* inst, bool is_spv_1_4) {
  28. if (is_spv_1_4) {
  29. // Starting in SPIR-V 1.4, all global variables are interface variables.
  30. return inst->opcode() == SpvOpVariable &&
  31. inst->word(3u) != SpvStorageClassFunction;
  32. } else {
  33. return inst->opcode() == SpvOpVariable &&
  34. (inst->word(3u) == SpvStorageClassInput ||
  35. inst->word(3u) == SpvStorageClassOutput);
  36. }
  37. }
  38. // Checks that \c var is listed as an interface in all the entry points that use
  39. // it.
  40. spv_result_t check_interface_variable(ValidationState_t& _,
  41. const Instruction* var) {
  42. std::vector<const Function*> functions;
  43. std::vector<const Instruction*> uses;
  44. for (auto use : var->uses()) {
  45. uses.push_back(use.first);
  46. }
  47. for (uint32_t i = 0; i < uses.size(); ++i) {
  48. const auto user = uses[i];
  49. if (const Function* func = user->function()) {
  50. functions.push_back(func);
  51. } else {
  52. // In the rare case that the variable is used by another instruction in
  53. // the global scope, continue searching for an instruction used in a
  54. // function.
  55. for (auto use : user->uses()) {
  56. uses.push_back(use.first);
  57. }
  58. }
  59. }
  60. std::sort(functions.begin(), functions.end(),
  61. [](const Function* lhs, const Function* rhs) {
  62. return lhs->id() < rhs->id();
  63. });
  64. functions.erase(std::unique(functions.begin(), functions.end()),
  65. functions.end());
  66. std::vector<uint32_t> entry_points;
  67. for (const auto func : functions) {
  68. for (auto id : _.FunctionEntryPoints(func->id())) {
  69. entry_points.push_back(id);
  70. }
  71. }
  72. std::sort(entry_points.begin(), entry_points.end());
  73. entry_points.erase(std::unique(entry_points.begin(), entry_points.end()),
  74. entry_points.end());
  75. for (auto id : entry_points) {
  76. for (const auto& desc : _.entry_point_descriptions(id)) {
  77. bool found = false;
  78. for (auto interface : desc.interfaces) {
  79. if (var->id() == interface) {
  80. found = true;
  81. break;
  82. }
  83. }
  84. if (!found) {
  85. return _.diag(SPV_ERROR_INVALID_ID, var)
  86. << "Interface variable id <" << var->id()
  87. << "> is used by entry point '" << desc.name << "' id <" << id
  88. << ">, but is not listed as an interface";
  89. }
  90. }
  91. }
  92. return SPV_SUCCESS;
  93. }
  94. } // namespace
  95. spv_result_t ValidateInterfaces(ValidationState_t& _) {
  96. bool is_spv_1_4 = _.version() >= SPV_SPIRV_VERSION_WORD(1, 4);
  97. for (auto& inst : _.ordered_instructions()) {
  98. if (is_interface_variable(&inst, is_spv_1_4)) {
  99. if (auto error = check_interface_variable(_, &inst)) {
  100. return error;
  101. }
  102. }
  103. }
  104. return SPV_SUCCESS;
  105. }
  106. } // namespace val
  107. } // namespace spvtools