validate_function.cpp 13 KB

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