validate_function.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 "source/opcode.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 ValidateFunction(ValidationState_t& _, const Instruction* inst) {
  23. const auto function_type_id = inst->GetOperandAs<uint32_t>(3);
  24. const auto function_type = _.FindDef(function_type_id);
  25. if (!function_type || SpvOpTypeFunction != function_type->opcode()) {
  26. return _.diag(SPV_ERROR_INVALID_ID, inst)
  27. << "OpFunction Function Type <id> '" << _.getIdName(function_type_id)
  28. << "' is not a function type.";
  29. }
  30. const auto return_id = function_type->GetOperandAs<uint32_t>(1);
  31. if (return_id != inst->type_id()) {
  32. return _.diag(SPV_ERROR_INVALID_ID, inst)
  33. << "OpFunction Result Type <id> '" << _.getIdName(inst->type_id())
  34. << "' does not match the Function Type's return type <id> '"
  35. << _.getIdName(return_id) << "'.";
  36. }
  37. const std::vector<SpvOp> acceptable = {
  38. SpvOpDecorate,
  39. SpvOpEnqueueKernel,
  40. SpvOpEntryPoint,
  41. SpvOpExecutionMode,
  42. SpvOpExecutionModeId,
  43. SpvOpFunctionCall,
  44. SpvOpGetKernelNDrangeSubGroupCount,
  45. SpvOpGetKernelNDrangeMaxSubGroupSize,
  46. SpvOpGetKernelWorkGroupSize,
  47. SpvOpGetKernelPreferredWorkGroupSizeMultiple,
  48. SpvOpGetKernelLocalSizeForSubgroupCount,
  49. SpvOpGetKernelMaxNumSubgroups,
  50. SpvOpName};
  51. for (auto& pair : inst->uses()) {
  52. const auto* use = pair.first;
  53. if (std::find(acceptable.begin(), acceptable.end(), use->opcode()) ==
  54. acceptable.end()) {
  55. return _.diag(SPV_ERROR_INVALID_ID, use)
  56. << "Invalid use of function result id " << _.getIdName(inst->id())
  57. << ".";
  58. }
  59. }
  60. return SPV_SUCCESS;
  61. }
  62. spv_result_t ValidateFunctionParameter(ValidationState_t& _,
  63. const Instruction* inst) {
  64. // NOTE: Find OpFunction & ensure OpFunctionParameter is not out of place.
  65. size_t param_index = 0;
  66. size_t inst_num = inst->LineNum() - 1;
  67. if (inst_num == 0) {
  68. return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
  69. << "Function parameter cannot be the first instruction.";
  70. }
  71. auto func_inst = &_.ordered_instructions()[inst_num];
  72. while (--inst_num) {
  73. func_inst = &_.ordered_instructions()[inst_num];
  74. if (func_inst->opcode() == SpvOpFunction) {
  75. break;
  76. } else if (func_inst->opcode() == SpvOpFunctionParameter) {
  77. ++param_index;
  78. }
  79. }
  80. if (func_inst->opcode() != SpvOpFunction) {
  81. return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
  82. << "Function parameter must be preceded by a function.";
  83. }
  84. const auto function_type_id = func_inst->GetOperandAs<uint32_t>(3);
  85. const auto function_type = _.FindDef(function_type_id);
  86. if (!function_type) {
  87. return _.diag(SPV_ERROR_INVALID_ID, func_inst)
  88. << "Missing function type definition.";
  89. }
  90. if (param_index >= function_type->words().size() - 3) {
  91. return _.diag(SPV_ERROR_INVALID_ID, inst)
  92. << "Too many OpFunctionParameters for " << func_inst->id()
  93. << ": expected " << function_type->words().size() - 3
  94. << " based on the function's type";
  95. }
  96. const auto param_type =
  97. _.FindDef(function_type->GetOperandAs<uint32_t>(param_index + 2));
  98. if (!param_type || inst->type_id() != param_type->id()) {
  99. return _.diag(SPV_ERROR_INVALID_ID, inst)
  100. << "OpFunctionParameter Result Type <id> '"
  101. << _.getIdName(inst->type_id())
  102. << "' does not match the OpTypeFunction parameter "
  103. "type of the same index.";
  104. }
  105. // Validate that PhysicalStorageBufferEXT have one of Restrict, Aliased,
  106. // RestrictPointerEXT, or AliasedPointerEXT.
  107. auto param_nonarray_type_id = param_type->id();
  108. while (_.GetIdOpcode(param_nonarray_type_id) == SpvOpTypeArray) {
  109. param_nonarray_type_id =
  110. _.FindDef(param_nonarray_type_id)->GetOperandAs<uint32_t>(1u);
  111. }
  112. if (_.GetIdOpcode(param_nonarray_type_id) == SpvOpTypePointer) {
  113. auto param_nonarray_type = _.FindDef(param_nonarray_type_id);
  114. if (param_nonarray_type->GetOperandAs<uint32_t>(1u) ==
  115. SpvStorageClassPhysicalStorageBufferEXT) {
  116. // check for Aliased or Restrict
  117. const auto& decorations = _.id_decorations(inst->id());
  118. bool foundAliased = std::any_of(
  119. decorations.begin(), decorations.end(), [](const Decoration& d) {
  120. return SpvDecorationAliased == d.dec_type();
  121. });
  122. bool foundRestrict = std::any_of(
  123. decorations.begin(), decorations.end(), [](const Decoration& d) {
  124. return SpvDecorationRestrict == d.dec_type();
  125. });
  126. if (!foundAliased && !foundRestrict) {
  127. return _.diag(SPV_ERROR_INVALID_ID, inst)
  128. << "OpFunctionParameter " << inst->id()
  129. << ": expected Aliased or Restrict for PhysicalStorageBufferEXT "
  130. "pointer.";
  131. }
  132. if (foundAliased && foundRestrict) {
  133. return _.diag(SPV_ERROR_INVALID_ID, inst)
  134. << "OpFunctionParameter " << inst->id()
  135. << ": can't specify both Aliased and Restrict for "
  136. "PhysicalStorageBufferEXT pointer.";
  137. }
  138. } else {
  139. const auto pointee_type_id =
  140. param_nonarray_type->GetOperandAs<uint32_t>(2);
  141. const auto pointee_type = _.FindDef(pointee_type_id);
  142. if (SpvOpTypePointer == pointee_type->opcode() &&
  143. pointee_type->GetOperandAs<uint32_t>(1u) ==
  144. SpvStorageClassPhysicalStorageBufferEXT) {
  145. // check for AliasedPointerEXT/RestrictPointerEXT
  146. const auto& decorations = _.id_decorations(inst->id());
  147. bool foundAliased = std::any_of(
  148. decorations.begin(), decorations.end(), [](const Decoration& d) {
  149. return SpvDecorationAliasedPointerEXT == d.dec_type();
  150. });
  151. bool foundRestrict = std::any_of(
  152. decorations.begin(), decorations.end(), [](const Decoration& d) {
  153. return SpvDecorationRestrictPointerEXT == d.dec_type();
  154. });
  155. if (!foundAliased && !foundRestrict) {
  156. return _.diag(SPV_ERROR_INVALID_ID, inst)
  157. << "OpFunctionParameter " << inst->id()
  158. << ": expected AliasedPointerEXT or RestrictPointerEXT for "
  159. "PhysicalStorageBufferEXT pointer.";
  160. }
  161. if (foundAliased && foundRestrict) {
  162. return _.diag(SPV_ERROR_INVALID_ID, inst)
  163. << "OpFunctionParameter " << inst->id()
  164. << ": can't specify both AliasedPointerEXT and "
  165. "RestrictPointerEXT for PhysicalStorageBufferEXT pointer.";
  166. }
  167. }
  168. }
  169. }
  170. return SPV_SUCCESS;
  171. }
  172. spv_result_t ValidateFunctionCall(ValidationState_t& _,
  173. const Instruction* inst) {
  174. const auto function_id = inst->GetOperandAs<uint32_t>(2);
  175. const auto function = _.FindDef(function_id);
  176. if (!function || SpvOpFunction != function->opcode()) {
  177. return _.diag(SPV_ERROR_INVALID_ID, inst)
  178. << "OpFunctionCall Function <id> '" << _.getIdName(function_id)
  179. << "' is not a function.";
  180. }
  181. auto return_type = _.FindDef(function->type_id());
  182. if (!return_type || return_type->id() != inst->type_id()) {
  183. return _.diag(SPV_ERROR_INVALID_ID, inst)
  184. << "OpFunctionCall Result Type <id> '"
  185. << _.getIdName(inst->type_id())
  186. << "'s type does not match Function <id> '"
  187. << _.getIdName(return_type->id()) << "'s return type.";
  188. }
  189. const auto function_type_id = function->GetOperandAs<uint32_t>(3);
  190. const auto function_type = _.FindDef(function_type_id);
  191. if (!function_type || function_type->opcode() != SpvOpTypeFunction) {
  192. return _.diag(SPV_ERROR_INVALID_ID, inst)
  193. << "Missing function type definition.";
  194. }
  195. const auto function_call_arg_count = inst->words().size() - 4;
  196. const auto function_param_count = function_type->words().size() - 3;
  197. if (function_param_count != function_call_arg_count) {
  198. return _.diag(SPV_ERROR_INVALID_ID, inst)
  199. << "OpFunctionCall Function <id>'s parameter count does not match "
  200. "the argument count.";
  201. }
  202. for (size_t argument_index = 3, param_index = 2;
  203. argument_index < inst->operands().size();
  204. argument_index++, param_index++) {
  205. const auto argument_id = inst->GetOperandAs<uint32_t>(argument_index);
  206. const auto argument = _.FindDef(argument_id);
  207. if (!argument) {
  208. return _.diag(SPV_ERROR_INVALID_ID, inst)
  209. << "Missing argument " << argument_index - 3 << " definition.";
  210. }
  211. const auto argument_type = _.FindDef(argument->type_id());
  212. if (!argument_type) {
  213. return _.diag(SPV_ERROR_INVALID_ID, inst)
  214. << "Missing argument " << argument_index - 3
  215. << " type definition.";
  216. }
  217. const auto parameter_type_id =
  218. function_type->GetOperandAs<uint32_t>(param_index);
  219. const auto parameter_type = _.FindDef(parameter_type_id);
  220. if (!parameter_type || argument_type->id() != parameter_type->id()) {
  221. return _.diag(SPV_ERROR_INVALID_ID, inst)
  222. << "OpFunctionCall Argument <id> '" << _.getIdName(argument_id)
  223. << "'s type does not match Function <id> '"
  224. << _.getIdName(parameter_type_id) << "'s parameter type.";
  225. }
  226. if (_.addressing_model() == SpvAddressingModelLogical) {
  227. if (parameter_type->opcode() == SpvOpTypePointer) {
  228. SpvStorageClass sc = parameter_type->GetOperandAs<SpvStorageClass>(1u);
  229. // Validate which storage classes can be pointer operands.
  230. switch (sc) {
  231. case SpvStorageClassUniformConstant:
  232. case SpvStorageClassFunction:
  233. case SpvStorageClassPrivate:
  234. case SpvStorageClassWorkgroup:
  235. case SpvStorageClassAtomicCounter:
  236. // These are always allowed.
  237. break;
  238. case SpvStorageClassStorageBuffer:
  239. if (!_.features().variable_pointers_storage_buffer) {
  240. return _.diag(SPV_ERROR_INVALID_ID, inst)
  241. << "StorageBuffer pointer operand "
  242. << _.getIdName(argument_id)
  243. << " requires a variable pointers capability";
  244. }
  245. break;
  246. default:
  247. return _.diag(SPV_ERROR_INVALID_ID, inst)
  248. << "Invalid storage class for pointer operand "
  249. << _.getIdName(argument_id);
  250. }
  251. // Validate memory object declaration requirements.
  252. if (argument->opcode() != SpvOpVariable &&
  253. argument->opcode() != SpvOpFunctionParameter) {
  254. const bool ssbo_vptr =
  255. _.features().variable_pointers_storage_buffer &&
  256. sc == SpvStorageClassStorageBuffer;
  257. const bool wg_vptr =
  258. _.features().variable_pointers && sc == SpvStorageClassWorkgroup;
  259. if (!ssbo_vptr && !wg_vptr) {
  260. return _.diag(SPV_ERROR_INVALID_ID, inst)
  261. << "Pointer operand " << _.getIdName(argument_id)
  262. << " must be a memory object declaration";
  263. }
  264. }
  265. }
  266. }
  267. }
  268. return SPV_SUCCESS;
  269. }
  270. } // namespace
  271. spv_result_t FunctionPass(ValidationState_t& _, const Instruction* inst) {
  272. switch (inst->opcode()) {
  273. case SpvOpFunction:
  274. if (auto error = ValidateFunction(_, inst)) return error;
  275. break;
  276. case SpvOpFunctionParameter:
  277. if (auto error = ValidateFunctionParameter(_, inst)) return error;
  278. break;
  279. case SpvOpFunctionCall:
  280. if (auto error = ValidateFunctionCall(_, inst)) return error;
  281. break;
  282. default:
  283. break;
  284. }
  285. return SPV_SUCCESS;
  286. }
  287. } // namespace val
  288. } // namespace spvtools