validate_decorations.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright (c) 2017 Google Inc.
  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 "validate.h"
  15. #include <algorithm>
  16. #include <string>
  17. #include "diagnostic.h"
  18. #include "opcode.h"
  19. #include "spirv_target_env.h"
  20. #include "val/validation_state.h"
  21. using libspirv::Decoration;
  22. using libspirv::DiagnosticStream;
  23. using libspirv::Instruction;
  24. using libspirv::ValidationState_t;
  25. namespace {
  26. // Returns whether the given variable has a BuiltIn decoration.
  27. bool isBuiltInVar(uint32_t var_id, ValidationState_t& vstate) {
  28. const auto& decorations = vstate.id_decorations(var_id);
  29. return std::any_of(
  30. decorations.begin(), decorations.end(),
  31. [](const Decoration& d) { return SpvDecorationBuiltIn == d.dec_type(); });
  32. }
  33. // Returns whether the given structure type has any members with BuiltIn
  34. // decoration.
  35. bool isBuiltInStruct(uint32_t struct_id, ValidationState_t& vstate) {
  36. const auto& decorations = vstate.id_decorations(struct_id);
  37. return std::any_of(
  38. decorations.begin(), decorations.end(), [](const Decoration& d) {
  39. return SpvDecorationBuiltIn == d.dec_type() &&
  40. Decoration::kInvalidMember != d.struct_member_index();
  41. });
  42. }
  43. // Returns true if the given ID has the Import LinkageAttributes decoration.
  44. bool hasImportLinkageAttribute(uint32_t id, ValidationState_t& vstate) {
  45. const auto& decorations = vstate.id_decorations(id);
  46. return std::any_of(decorations.begin(), decorations.end(),
  47. [](const Decoration& d) {
  48. return SpvDecorationLinkageAttributes == d.dec_type() &&
  49. d.params().size() >= 2u &&
  50. d.params().back() == SpvLinkageTypeImport;
  51. });
  52. }
  53. spv_result_t CheckLinkageAttrOfFunctions(ValidationState_t& vstate) {
  54. for (const auto& function : vstate.functions()) {
  55. if (function.block_count() == 0u) {
  56. // A function declaration (an OpFunction with no basic blocks), must have
  57. // a Linkage Attributes Decoration with the Import Linkage Type.
  58. if (!hasImportLinkageAttribute(function.id(), vstate)) {
  59. return vstate.diag(SPV_ERROR_INVALID_BINARY)
  60. << "Function declaration (id " << function.id()
  61. << ") must have a LinkageAttributes decoration with the Import "
  62. "Linkage type.";
  63. }
  64. } else {
  65. if (hasImportLinkageAttribute(function.id(), vstate)) {
  66. return vstate.diag(SPV_ERROR_INVALID_BINARY)
  67. << "Function definition (id " << function.id()
  68. << ") may not be decorated with Import Linkage type.";
  69. }
  70. }
  71. }
  72. return SPV_SUCCESS;
  73. }
  74. // Checks whether an imported variable is initialized by this module.
  75. spv_result_t CheckImportedVariableInitialization(ValidationState_t& vstate) {
  76. // According the SPIR-V Spec 2.16.1, it is illegal to initialize an imported
  77. // variable. This means that a module-scope OpVariable with initialization
  78. // value cannot be marked with the Import Linkage Type (import type id = 1).
  79. for (auto global_var_id : vstate.global_vars()) {
  80. // Initializer <id> is an optional argument for OpVariable. If initializer
  81. // <id> is present, the instruction will have 5 words.
  82. auto variable_instr = vstate.FindDef(global_var_id);
  83. if (variable_instr->words().size() == 5u &&
  84. hasImportLinkageAttribute(global_var_id, vstate)) {
  85. return vstate.diag(SPV_ERROR_INVALID_ID)
  86. << "A module-scope OpVariable with initialization value "
  87. "cannot be marked with the Import Linkage Type.";
  88. }
  89. }
  90. return SPV_SUCCESS;
  91. }
  92. // Checks whether a builtin variable is valid.
  93. spv_result_t CheckBuiltInVariable(uint32_t var_id, ValidationState_t& vstate) {
  94. const auto& decorations = vstate.id_decorations(var_id);
  95. for (const auto& d : decorations) {
  96. if (spvIsVulkanEnv(vstate.context()->target_env)) {
  97. if (d.dec_type() == SpvDecorationLocation ||
  98. d.dec_type() == SpvDecorationComponent) {
  99. return vstate.diag(SPV_ERROR_INVALID_ID)
  100. << "A BuiltIn variable (id " << var_id
  101. << ") cannot have any Location or Component decorations";
  102. }
  103. }
  104. }
  105. return SPV_SUCCESS;
  106. }
  107. // Checks whether proper decorations have been appied to the entry points.
  108. spv_result_t CheckDecorationsOfEntryPoints(ValidationState_t& vstate) {
  109. for (uint32_t entry_point : vstate.entry_points()) {
  110. const auto& interfaces = vstate.entry_point_interfaces(entry_point);
  111. int num_builtin_inputs = 0;
  112. int num_builtin_outputs = 0;
  113. for (auto interface : interfaces) {
  114. Instruction* var_instr = vstate.FindDef(interface);
  115. if (SpvOpVariable != var_instr->opcode()) {
  116. return vstate.diag(SPV_ERROR_INVALID_ID)
  117. << "Interfaces passed to OpEntryPoint must be of type "
  118. "OpTypeVariable. Found Op"
  119. << spvOpcodeString(static_cast<SpvOp>(var_instr->opcode()))
  120. << ".";
  121. }
  122. const uint32_t ptr_id = var_instr->word(1);
  123. Instruction* ptr_instr = vstate.FindDef(ptr_id);
  124. // It is guaranteed (by validator ID checks) that ptr_instr is
  125. // OpTypePointer. Word 3 of this instruction is the type being pointed to.
  126. const uint32_t type_id = ptr_instr->word(3);
  127. Instruction* type_instr = vstate.FindDef(type_id);
  128. const auto storage_class =
  129. static_cast<SpvStorageClass>(var_instr->word(3));
  130. if (storage_class != SpvStorageClassInput &&
  131. storage_class != SpvStorageClassOutput) {
  132. return vstate.diag(SPV_ERROR_INVALID_ID)
  133. << "OpEntryPoint interfaces must be OpVariables with "
  134. "Storage Class of Input(1) or Output(3). Found Storage Class "
  135. << storage_class << " for Entry Point id " << entry_point << ".";
  136. }
  137. if (type_instr && SpvOpTypeStruct == type_instr->opcode() &&
  138. isBuiltInStruct(type_id, vstate)) {
  139. if (storage_class == SpvStorageClassInput) ++num_builtin_inputs;
  140. if (storage_class == SpvStorageClassOutput) ++num_builtin_outputs;
  141. if (num_builtin_inputs > 1 || num_builtin_outputs > 1) break;
  142. if (auto error = CheckBuiltInVariable(interface, vstate)) return error;
  143. } else if (isBuiltInVar(interface, vstate)) {
  144. if (auto error = CheckBuiltInVariable(interface, vstate)) return error;
  145. }
  146. }
  147. if (num_builtin_inputs > 1 || num_builtin_outputs > 1) {
  148. return vstate.diag(SPV_ERROR_INVALID_BINARY)
  149. << "There must be at most one object per Storage Class that can "
  150. "contain a structure type containing members decorated with "
  151. "BuiltIn, consumed per entry-point. Entry Point id "
  152. << entry_point << " does not meet this requirement.";
  153. }
  154. // The LinkageAttributes Decoration cannot be applied to functions targeted
  155. // by an OpEntryPoint instruction
  156. for (auto& decoration : vstate.id_decorations(entry_point)) {
  157. if (SpvDecorationLinkageAttributes == decoration.dec_type()) {
  158. const char* linkage_name =
  159. reinterpret_cast<const char*>(&decoration.params()[0]);
  160. return vstate.diag(SPV_ERROR_INVALID_BINARY)
  161. << "The LinkageAttributes Decoration (Linkage name: "
  162. << linkage_name << ") cannot be applied to function id "
  163. << entry_point
  164. << " because it is targeted by an OpEntryPoint instruction.";
  165. }
  166. }
  167. }
  168. return SPV_SUCCESS;
  169. }
  170. } // anonymous namespace
  171. namespace libspirv {
  172. // Validates that decorations have been applied properly.
  173. spv_result_t ValidateDecorations(ValidationState_t& vstate) {
  174. if (auto error = CheckImportedVariableInitialization(vstate)) return error;
  175. if (auto error = CheckDecorationsOfEntryPoints(vstate)) return error;
  176. if (auto error = CheckLinkageAttrOfFunctions(vstate)) return error;
  177. return SPV_SUCCESS;
  178. }
  179. } // namespace libspirv