validate_function.cpp 14 KB

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