validate_interfaces.cpp 3.4 KB

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