validate_function.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. return SPV_SUCCESS;
  137. }
  138. spv_result_t ValidateFunctionCall(ValidationState_t& _,
  139. const Instruction* inst) {
  140. const auto function_id = inst->GetOperandAs<uint32_t>(2);
  141. const auto function = _.FindDef(function_id);
  142. if (!function || spv::Op::OpFunction != function->opcode()) {
  143. return _.diag(SPV_ERROR_INVALID_ID, inst)
  144. << "OpFunctionCall Function <id> " << _.getIdName(function_id)
  145. << " is not a function.";
  146. }
  147. auto return_type = _.FindDef(function->type_id());
  148. if (!return_type || return_type->id() != inst->type_id()) {
  149. return _.diag(SPV_ERROR_INVALID_ID, inst)
  150. << "OpFunctionCall Result Type <id> " << _.getIdName(inst->type_id())
  151. << "s type does not match Function <id> "
  152. << _.getIdName(return_type->id()) << "s return type.";
  153. }
  154. const auto function_type_id = function->GetOperandAs<uint32_t>(3);
  155. const auto function_type = _.FindDef(function_type_id);
  156. if (!function_type || function_type->opcode() != spv::Op::OpTypeFunction) {
  157. return _.diag(SPV_ERROR_INVALID_ID, inst)
  158. << "Missing function type definition.";
  159. }
  160. const auto function_call_arg_count = inst->words().size() - 4;
  161. const auto function_param_count = function_type->words().size() - 3;
  162. if (function_param_count != function_call_arg_count) {
  163. return _.diag(SPV_ERROR_INVALID_ID, inst)
  164. << "OpFunctionCall Function <id>'s parameter count does not match "
  165. "the argument count.";
  166. }
  167. for (size_t argument_index = 3, param_index = 2;
  168. argument_index < inst->operands().size();
  169. argument_index++, param_index++) {
  170. const auto argument_id = inst->GetOperandAs<uint32_t>(argument_index);
  171. const auto argument = _.FindDef(argument_id);
  172. if (!argument) {
  173. return _.diag(SPV_ERROR_INVALID_ID, inst)
  174. << "Missing argument " << argument_index - 3 << " definition.";
  175. }
  176. const auto argument_type = _.FindDef(argument->type_id());
  177. if (!argument_type) {
  178. return _.diag(SPV_ERROR_INVALID_ID, inst)
  179. << "Missing argument " << argument_index - 3
  180. << " type definition.";
  181. }
  182. const auto parameter_type_id =
  183. function_type->GetOperandAs<uint32_t>(param_index);
  184. const auto parameter_type = _.FindDef(parameter_type_id);
  185. if (!parameter_type || argument_type->id() != parameter_type->id()) {
  186. if (!parameter_type || !_.options()->before_hlsl_legalization ||
  187. !DoPointeesLogicallyMatch(argument_type, parameter_type, _)) {
  188. return _.diag(SPV_ERROR_INVALID_ID, inst)
  189. << "OpFunctionCall Argument <id> " << _.getIdName(argument_id)
  190. << "s type does not match Function <id> "
  191. << _.getIdName(parameter_type_id) << "s parameter type.";
  192. }
  193. }
  194. if (_.addressing_model() == spv::AddressingModel::Logical) {
  195. if ((parameter_type->opcode() == spv::Op::OpTypePointer ||
  196. parameter_type->opcode() == spv::Op::OpTypeUntypedPointerKHR) &&
  197. !_.options()->relax_logical_pointer) {
  198. spv::StorageClass sc =
  199. parameter_type->GetOperandAs<spv::StorageClass>(1u);
  200. // Validate which storage classes can be pointer operands.
  201. switch (sc) {
  202. case spv::StorageClass::UniformConstant:
  203. case spv::StorageClass::Function:
  204. case spv::StorageClass::Private:
  205. case spv::StorageClass::Workgroup:
  206. case spv::StorageClass::AtomicCounter:
  207. // These are always allowed.
  208. break;
  209. case spv::StorageClass::StorageBuffer:
  210. if (!_.features().variable_pointers) {
  211. return _.diag(SPV_ERROR_INVALID_ID, inst)
  212. << "StorageBuffer pointer operand "
  213. << _.getIdName(argument_id)
  214. << " requires a variable pointers capability";
  215. }
  216. break;
  217. default:
  218. return _.diag(SPV_ERROR_INVALID_ID, inst)
  219. << "Invalid storage class for pointer operand "
  220. << _.getIdName(argument_id);
  221. }
  222. // Validate memory object declaration requirements.
  223. if (argument->opcode() != spv::Op::OpVariable &&
  224. argument->opcode() != spv::Op::OpUntypedVariableKHR &&
  225. argument->opcode() != spv::Op::OpFunctionParameter) {
  226. const bool ssbo_vptr =
  227. _.HasCapability(spv::Capability::VariablePointersStorageBuffer) &&
  228. sc == spv::StorageClass::StorageBuffer;
  229. const bool wg_vptr =
  230. _.HasCapability(spv::Capability::VariablePointers) &&
  231. sc == spv::StorageClass::Workgroup;
  232. const bool uc_ptr = sc == spv::StorageClass::UniformConstant;
  233. if (!_.options()->before_hlsl_legalization && !ssbo_vptr &&
  234. !wg_vptr && !uc_ptr) {
  235. return _.diag(SPV_ERROR_INVALID_ID, inst)
  236. << "Pointer operand " << _.getIdName(argument_id)
  237. << " must be a memory object declaration";
  238. }
  239. }
  240. }
  241. }
  242. }
  243. return SPV_SUCCESS;
  244. }
  245. spv_result_t ValidateCooperativeMatrixPerElementOp(ValidationState_t& _,
  246. const Instruction* inst) {
  247. const auto function_id = inst->GetOperandAs<uint32_t>(3);
  248. const auto function = _.FindDef(function_id);
  249. if (!function || spv::Op::OpFunction != function->opcode()) {
  250. return _.diag(SPV_ERROR_INVALID_ID, inst)
  251. << "OpCooperativeMatrixPerElementOpNV Function <id> "
  252. << _.getIdName(function_id) << " is not a function.";
  253. }
  254. const auto matrix_id = inst->GetOperandAs<uint32_t>(2);
  255. const auto matrix = _.FindDef(matrix_id);
  256. const auto matrix_type_id = matrix->type_id();
  257. if (!_.IsCooperativeMatrixKHRType(matrix_type_id)) {
  258. return _.diag(SPV_ERROR_INVALID_ID, inst)
  259. << "OpCooperativeMatrixPerElementOpNV Matrix <id> "
  260. << _.getIdName(matrix_id) << " is not a cooperative matrix.";
  261. }
  262. const auto result_type_id = inst->GetOperandAs<uint32_t>(0);
  263. if (matrix_type_id != result_type_id) {
  264. return _.diag(SPV_ERROR_INVALID_ID, inst)
  265. << "OpCooperativeMatrixPerElementOpNV Result Type <id> "
  266. << _.getIdName(result_type_id) << " must match matrix type <id> "
  267. << _.getIdName(matrix_type_id) << ".";
  268. }
  269. const auto matrix_comp_type_id =
  270. _.FindDef(matrix_type_id)->GetOperandAs<uint32_t>(1);
  271. const auto function_type_id = function->GetOperandAs<uint32_t>(3);
  272. const auto function_type = _.FindDef(function_type_id);
  273. auto return_type_id = function_type->GetOperandAs<uint32_t>(1);
  274. if (return_type_id != matrix_comp_type_id) {
  275. return _.diag(SPV_ERROR_INVALID_ID, inst)
  276. << "OpCooperativeMatrixPerElementOpNV function return type <id> "
  277. << _.getIdName(return_type_id)
  278. << " must match matrix component type <id> "
  279. << _.getIdName(matrix_comp_type_id) << ".";
  280. }
  281. if (function_type->operands().size() < 5) {
  282. return _.diag(SPV_ERROR_INVALID_ID, inst)
  283. << "OpCooperativeMatrixPerElementOpNV function type <id> "
  284. << _.getIdName(function_type_id)
  285. << " must have a least three parameters.";
  286. }
  287. const auto param0_id = function_type->GetOperandAs<uint32_t>(2);
  288. const auto param1_id = function_type->GetOperandAs<uint32_t>(3);
  289. const auto param2_id = function_type->GetOperandAs<uint32_t>(4);
  290. if (!_.IsIntScalarType(param0_id) || _.GetBitWidth(param0_id) != 32) {
  291. return _.diag(SPV_ERROR_INVALID_ID, inst)
  292. << "OpCooperativeMatrixPerElementOpNV function type first parameter "
  293. "type <id> "
  294. << _.getIdName(param0_id) << " must be a 32-bit integer.";
  295. }
  296. if (!_.IsIntScalarType(param1_id) || _.GetBitWidth(param1_id) != 32) {
  297. return _.diag(SPV_ERROR_INVALID_ID, inst)
  298. << "OpCooperativeMatrixPerElementOpNV function type second "
  299. "parameter type <id> "
  300. << _.getIdName(param1_id) << " must be a 32-bit integer.";
  301. }
  302. if (param2_id != matrix_comp_type_id) {
  303. return _.diag(SPV_ERROR_INVALID_ID, inst)
  304. << "OpCooperativeMatrixPerElementOpNV function type third parameter "
  305. "type <id> "
  306. << _.getIdName(param2_id) << " must match matrix component type.";
  307. }
  308. return SPV_SUCCESS;
  309. }
  310. } // namespace
  311. spv_result_t FunctionPass(ValidationState_t& _, const Instruction* inst) {
  312. switch (inst->opcode()) {
  313. case spv::Op::OpFunction:
  314. if (auto error = ValidateFunction(_, inst)) return error;
  315. break;
  316. case spv::Op::OpFunctionParameter:
  317. if (auto error = ValidateFunctionParameter(_, inst)) return error;
  318. break;
  319. case spv::Op::OpFunctionCall:
  320. if (auto error = ValidateFunctionCall(_, inst)) return error;
  321. break;
  322. case spv::Op::OpCooperativeMatrixPerElementOpNV:
  323. if (auto error = ValidateCooperativeMatrixPerElementOp(_, inst))
  324. return error;
  325. break;
  326. default:
  327. break;
  328. }
  329. return SPV_SUCCESS;
  330. }
  331. } // namespace val
  332. } // namespace spvtools