validate_function.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. spv::Op::OpCooperativeMatrixPerElementOpNV,
  79. spv::Op::OpCooperativeMatrixReduceNV,
  80. spv::Op::OpCooperativeMatrixLoadTensorNV};
  81. for (auto& pair : inst->uses()) {
  82. const auto* use = pair.first;
  83. if (std::find(acceptable.begin(), acceptable.end(), use->opcode()) ==
  84. acceptable.end() &&
  85. !use->IsNonSemantic() && !use->IsDebugInfo()) {
  86. return _.diag(SPV_ERROR_INVALID_ID, use)
  87. << "Invalid use of function result id " << _.getIdName(inst->id())
  88. << ".";
  89. }
  90. }
  91. return SPV_SUCCESS;
  92. }
  93. spv_result_t ValidateFunctionParameter(ValidationState_t& _,
  94. const Instruction* inst) {
  95. // NOTE: Find OpFunction & ensure OpFunctionParameter is not out of place.
  96. size_t param_index = 0;
  97. size_t inst_num = inst->LineNum() - 1;
  98. if (inst_num == 0) {
  99. return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
  100. << "Function parameter cannot be the first instruction.";
  101. }
  102. auto func_inst = &_.ordered_instructions()[inst_num];
  103. while (--inst_num) {
  104. func_inst = &_.ordered_instructions()[inst_num];
  105. if (func_inst->opcode() == spv::Op::OpFunction) {
  106. break;
  107. } else if (func_inst->opcode() == spv::Op::OpFunctionParameter) {
  108. ++param_index;
  109. }
  110. }
  111. if (func_inst->opcode() != spv::Op::OpFunction) {
  112. return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
  113. << "Function parameter must be preceded by a function.";
  114. }
  115. const auto function_type_id = func_inst->GetOperandAs<uint32_t>(3);
  116. const auto function_type = _.FindDef(function_type_id);
  117. if (!function_type) {
  118. return _.diag(SPV_ERROR_INVALID_ID, func_inst)
  119. << "Missing function type definition.";
  120. }
  121. if (param_index >= function_type->words().size() - 3) {
  122. return _.diag(SPV_ERROR_INVALID_ID, inst)
  123. << "Too many OpFunctionParameters for " << func_inst->id()
  124. << ": expected " << function_type->words().size() - 3
  125. << " based on the function's type";
  126. }
  127. const auto param_type =
  128. _.FindDef(function_type->GetOperandAs<uint32_t>(param_index + 2));
  129. if (!param_type || inst->type_id() != param_type->id()) {
  130. return _.diag(SPV_ERROR_INVALID_ID, inst)
  131. << "OpFunctionParameter Result Type <id> "
  132. << _.getIdName(inst->type_id())
  133. << " does not match the OpTypeFunction parameter "
  134. "type of the same index.";
  135. }
  136. // Validate that PhysicalStorageBuffer have one of Restrict, Aliased,
  137. // RestrictPointer, or AliasedPointer.
  138. auto param_nonarray_type_id = param_type->id();
  139. while (_.GetIdOpcode(param_nonarray_type_id) == spv::Op::OpTypeArray) {
  140. param_nonarray_type_id =
  141. _.FindDef(param_nonarray_type_id)->GetOperandAs<uint32_t>(1u);
  142. }
  143. if (_.GetIdOpcode(param_nonarray_type_id) == spv::Op::OpTypePointer ||
  144. _.GetIdOpcode(param_nonarray_type_id) ==
  145. spv::Op::OpTypeUntypedPointerKHR) {
  146. auto param_nonarray_type = _.FindDef(param_nonarray_type_id);
  147. if (param_nonarray_type->GetOperandAs<spv::StorageClass>(1u) ==
  148. spv::StorageClass::PhysicalStorageBuffer) {
  149. // check for Aliased or Restrict
  150. const auto& decorations = _.id_decorations(inst->id());
  151. bool foundAliased = std::any_of(
  152. decorations.begin(), decorations.end(), [](const Decoration& d) {
  153. return spv::Decoration::Aliased == d.dec_type();
  154. });
  155. bool foundRestrict = std::any_of(
  156. decorations.begin(), decorations.end(), [](const Decoration& d) {
  157. return spv::Decoration::Restrict == d.dec_type();
  158. });
  159. if (!foundAliased && !foundRestrict) {
  160. return _.diag(SPV_ERROR_INVALID_ID, inst)
  161. << "OpFunctionParameter " << inst->id()
  162. << ": expected Aliased or Restrict for PhysicalStorageBuffer "
  163. "pointer.";
  164. }
  165. if (foundAliased && foundRestrict) {
  166. return _.diag(SPV_ERROR_INVALID_ID, inst)
  167. << "OpFunctionParameter " << inst->id()
  168. << ": can't specify both Aliased and Restrict for "
  169. "PhysicalStorageBuffer pointer.";
  170. }
  171. } else if (param_nonarray_type->opcode() == spv::Op::OpTypePointer) {
  172. const auto pointee_type_id =
  173. param_nonarray_type->GetOperandAs<uint32_t>(2);
  174. const auto pointee_type = _.FindDef(pointee_type_id);
  175. if (spv::Op::OpTypePointer == pointee_type->opcode() &&
  176. pointee_type->GetOperandAs<spv::StorageClass>(1u) ==
  177. spv::StorageClass::PhysicalStorageBuffer) {
  178. // check for AliasedPointer/RestrictPointer
  179. const auto& decorations = _.id_decorations(inst->id());
  180. bool foundAliased = std::any_of(
  181. decorations.begin(), decorations.end(), [](const Decoration& d) {
  182. return spv::Decoration::AliasedPointer == d.dec_type();
  183. });
  184. bool foundRestrict = std::any_of(
  185. decorations.begin(), decorations.end(), [](const Decoration& d) {
  186. return spv::Decoration::RestrictPointer == d.dec_type();
  187. });
  188. if (!foundAliased && !foundRestrict) {
  189. return _.diag(SPV_ERROR_INVALID_ID, inst)
  190. << "OpFunctionParameter " << inst->id()
  191. << ": expected AliasedPointer or RestrictPointer for "
  192. "PhysicalStorageBuffer pointer.";
  193. }
  194. if (foundAliased && foundRestrict) {
  195. return _.diag(SPV_ERROR_INVALID_ID, inst)
  196. << "OpFunctionParameter " << inst->id()
  197. << ": can't specify both AliasedPointer and "
  198. "RestrictPointer for PhysicalStorageBuffer pointer.";
  199. }
  200. }
  201. }
  202. }
  203. return SPV_SUCCESS;
  204. }
  205. spv_result_t ValidateFunctionCall(ValidationState_t& _,
  206. const Instruction* inst) {
  207. const auto function_id = inst->GetOperandAs<uint32_t>(2);
  208. const auto function = _.FindDef(function_id);
  209. if (!function || spv::Op::OpFunction != function->opcode()) {
  210. return _.diag(SPV_ERROR_INVALID_ID, inst)
  211. << "OpFunctionCall Function <id> " << _.getIdName(function_id)
  212. << " is not a function.";
  213. }
  214. auto return_type = _.FindDef(function->type_id());
  215. if (!return_type || return_type->id() != inst->type_id()) {
  216. return _.diag(SPV_ERROR_INVALID_ID, inst)
  217. << "OpFunctionCall Result Type <id> " << _.getIdName(inst->type_id())
  218. << "s type does not match Function <id> "
  219. << _.getIdName(return_type->id()) << "s return type.";
  220. }
  221. const auto function_type_id = function->GetOperandAs<uint32_t>(3);
  222. const auto function_type = _.FindDef(function_type_id);
  223. if (!function_type || function_type->opcode() != spv::Op::OpTypeFunction) {
  224. return _.diag(SPV_ERROR_INVALID_ID, inst)
  225. << "Missing function type definition.";
  226. }
  227. const auto function_call_arg_count = inst->words().size() - 4;
  228. const auto function_param_count = function_type->words().size() - 3;
  229. if (function_param_count != function_call_arg_count) {
  230. return _.diag(SPV_ERROR_INVALID_ID, inst)
  231. << "OpFunctionCall Function <id>'s parameter count does not match "
  232. "the argument count.";
  233. }
  234. for (size_t argument_index = 3, param_index = 2;
  235. argument_index < inst->operands().size();
  236. argument_index++, param_index++) {
  237. const auto argument_id = inst->GetOperandAs<uint32_t>(argument_index);
  238. const auto argument = _.FindDef(argument_id);
  239. if (!argument) {
  240. return _.diag(SPV_ERROR_INVALID_ID, inst)
  241. << "Missing argument " << argument_index - 3 << " definition.";
  242. }
  243. const auto argument_type = _.FindDef(argument->type_id());
  244. if (!argument_type) {
  245. return _.diag(SPV_ERROR_INVALID_ID, inst)
  246. << "Missing argument " << argument_index - 3
  247. << " type definition.";
  248. }
  249. const auto parameter_type_id =
  250. function_type->GetOperandAs<uint32_t>(param_index);
  251. const auto parameter_type = _.FindDef(parameter_type_id);
  252. if (!parameter_type || argument_type->id() != parameter_type->id()) {
  253. if (!parameter_type || !_.options()->before_hlsl_legalization ||
  254. !DoPointeesLogicallyMatch(argument_type, parameter_type, _)) {
  255. return _.diag(SPV_ERROR_INVALID_ID, inst)
  256. << "OpFunctionCall Argument <id> " << _.getIdName(argument_id)
  257. << "s type does not match Function <id> "
  258. << _.getIdName(parameter_type_id) << "s parameter type.";
  259. }
  260. }
  261. if (_.addressing_model() == spv::AddressingModel::Logical) {
  262. if ((parameter_type->opcode() == spv::Op::OpTypePointer ||
  263. parameter_type->opcode() == spv::Op::OpTypeUntypedPointerKHR) &&
  264. !_.options()->relax_logical_pointer) {
  265. spv::StorageClass sc =
  266. parameter_type->GetOperandAs<spv::StorageClass>(1u);
  267. // Validate which storage classes can be pointer operands.
  268. switch (sc) {
  269. case spv::StorageClass::UniformConstant:
  270. case spv::StorageClass::Function:
  271. case spv::StorageClass::Private:
  272. case spv::StorageClass::Workgroup:
  273. case spv::StorageClass::AtomicCounter:
  274. // These are always allowed.
  275. break;
  276. case spv::StorageClass::StorageBuffer:
  277. if (!_.features().variable_pointers) {
  278. return _.diag(SPV_ERROR_INVALID_ID, inst)
  279. << "StorageBuffer pointer operand "
  280. << _.getIdName(argument_id)
  281. << " requires a variable pointers capability";
  282. }
  283. break;
  284. default:
  285. return _.diag(SPV_ERROR_INVALID_ID, inst)
  286. << "Invalid storage class for pointer operand "
  287. << _.getIdName(argument_id);
  288. }
  289. // Validate memory object declaration requirements.
  290. if (argument->opcode() != spv::Op::OpVariable &&
  291. argument->opcode() != spv::Op::OpUntypedVariableKHR &&
  292. argument->opcode() != spv::Op::OpFunctionParameter) {
  293. const bool ssbo_vptr =
  294. _.HasCapability(spv::Capability::VariablePointersStorageBuffer) &&
  295. sc == spv::StorageClass::StorageBuffer;
  296. const bool wg_vptr =
  297. _.HasCapability(spv::Capability::VariablePointers) &&
  298. sc == spv::StorageClass::Workgroup;
  299. const bool uc_ptr = sc == spv::StorageClass::UniformConstant;
  300. if (!ssbo_vptr && !wg_vptr && !uc_ptr) {
  301. return _.diag(SPV_ERROR_INVALID_ID, inst)
  302. << "Pointer operand " << _.getIdName(argument_id)
  303. << " must be a memory object declaration";
  304. }
  305. }
  306. }
  307. }
  308. }
  309. return SPV_SUCCESS;
  310. }
  311. spv_result_t ValidateCooperativeMatrixPerElementOp(ValidationState_t& _,
  312. const Instruction* inst) {
  313. const auto function_id = inst->GetOperandAs<uint32_t>(3);
  314. const auto function = _.FindDef(function_id);
  315. if (!function || spv::Op::OpFunction != function->opcode()) {
  316. return _.diag(SPV_ERROR_INVALID_ID, inst)
  317. << "OpCooperativeMatrixPerElementOpNV Function <id> "
  318. << _.getIdName(function_id) << " is not a function.";
  319. }
  320. const auto matrix_id = inst->GetOperandAs<uint32_t>(2);
  321. const auto matrix = _.FindDef(matrix_id);
  322. const auto matrix_type_id = matrix->type_id();
  323. if (!_.IsCooperativeMatrixKHRType(matrix_type_id)) {
  324. return _.diag(SPV_ERROR_INVALID_ID, inst)
  325. << "OpCooperativeMatrixPerElementOpNV Matrix <id> "
  326. << _.getIdName(matrix_id) << " is not a cooperative matrix.";
  327. }
  328. const auto result_type_id = inst->GetOperandAs<uint32_t>(0);
  329. if (matrix_type_id != result_type_id) {
  330. return _.diag(SPV_ERROR_INVALID_ID, inst)
  331. << "OpCooperativeMatrixPerElementOpNV Result Type <id> "
  332. << _.getIdName(result_type_id) << " must match matrix type <id> "
  333. << _.getIdName(matrix_type_id) << ".";
  334. }
  335. const auto matrix_comp_type_id =
  336. _.FindDef(matrix_type_id)->GetOperandAs<uint32_t>(1);
  337. const auto function_type_id = function->GetOperandAs<uint32_t>(3);
  338. const auto function_type = _.FindDef(function_type_id);
  339. auto return_type_id = function_type->GetOperandAs<uint32_t>(1);
  340. if (return_type_id != matrix_comp_type_id) {
  341. return _.diag(SPV_ERROR_INVALID_ID, inst)
  342. << "OpCooperativeMatrixPerElementOpNV function return type <id> "
  343. << _.getIdName(return_type_id)
  344. << " must match matrix component type <id> "
  345. << _.getIdName(matrix_comp_type_id) << ".";
  346. }
  347. if (function_type->operands().size() < 5) {
  348. return _.diag(SPV_ERROR_INVALID_ID, inst)
  349. << "OpCooperativeMatrixPerElementOpNV function type <id> "
  350. << _.getIdName(function_type_id)
  351. << " must have a least three parameters.";
  352. }
  353. const auto param0_id = function_type->GetOperandAs<uint32_t>(2);
  354. const auto param1_id = function_type->GetOperandAs<uint32_t>(3);
  355. const auto param2_id = function_type->GetOperandAs<uint32_t>(4);
  356. if (!_.IsIntScalarType(param0_id) || _.GetBitWidth(param0_id) != 32) {
  357. return _.diag(SPV_ERROR_INVALID_ID, inst)
  358. << "OpCooperativeMatrixPerElementOpNV function type first parameter "
  359. "type <id> "
  360. << _.getIdName(param0_id) << " must be a 32-bit integer.";
  361. }
  362. if (!_.IsIntScalarType(param1_id) || _.GetBitWidth(param1_id) != 32) {
  363. return _.diag(SPV_ERROR_INVALID_ID, inst)
  364. << "OpCooperativeMatrixPerElementOpNV function type second "
  365. "parameter type <id> "
  366. << _.getIdName(param1_id) << " must be a 32-bit integer.";
  367. }
  368. if (param2_id != matrix_comp_type_id) {
  369. return _.diag(SPV_ERROR_INVALID_ID, inst)
  370. << "OpCooperativeMatrixPerElementOpNV function type third parameter "
  371. "type <id> "
  372. << _.getIdName(param2_id) << " must match matrix component type.";
  373. }
  374. return SPV_SUCCESS;
  375. }
  376. } // namespace
  377. spv_result_t FunctionPass(ValidationState_t& _, const Instruction* inst) {
  378. switch (inst->opcode()) {
  379. case spv::Op::OpFunction:
  380. if (auto error = ValidateFunction(_, inst)) return error;
  381. break;
  382. case spv::Op::OpFunctionParameter:
  383. if (auto error = ValidateFunctionParameter(_, inst)) return error;
  384. break;
  385. case spv::Op::OpFunctionCall:
  386. if (auto error = ValidateFunctionCall(_, inst)) return error;
  387. break;
  388. case spv::Op::OpCooperativeMatrixPerElementOpNV:
  389. if (auto error = ValidateCooperativeMatrixPerElementOp(_, inst))
  390. return error;
  391. break;
  392. default:
  393. break;
  394. }
  395. return SPV_SUCCESS;
  396. }
  397. } // namespace val
  398. } // namespace spvtools