validate_instruction.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. // Copyright (c) 2015-2016 The Khronos Group Inc.
  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. // Performs validation on instructions that appear inside of a SPIR-V block.
  15. #include "validate.h"
  16. #include <algorithm>
  17. #include <cassert>
  18. #include <sstream>
  19. #include <string>
  20. #include "binary.h"
  21. #include "diagnostic.h"
  22. #include "enum_set.h"
  23. #include "enum_string_mapping.h"
  24. #include "extensions.h"
  25. #include "opcode.h"
  26. #include "operand.h"
  27. #include "spirv_constant.h"
  28. #include "spirv_definition.h"
  29. #include "spirv_target_env.h"
  30. #include "spirv_validator_options.h"
  31. #include "util/string_utils.h"
  32. #include "val/function.h"
  33. #include "val/validation_state.h"
  34. using libspirv::AssemblyGrammar;
  35. using libspirv::CapabilitySet;
  36. using libspirv::DiagnosticStream;
  37. using libspirv::ExtensionSet;
  38. using libspirv::ValidationState_t;
  39. namespace {
  40. std::string ToString(const CapabilitySet& capabilities,
  41. const AssemblyGrammar& grammar) {
  42. std::stringstream ss;
  43. capabilities.ForEach([&grammar, &ss](SpvCapability cap) {
  44. spv_operand_desc desc;
  45. if (SPV_SUCCESS ==
  46. grammar.lookupOperand(SPV_OPERAND_TYPE_CAPABILITY, cap, &desc))
  47. ss << desc->name << " ";
  48. else
  49. ss << cap << " ";
  50. });
  51. return ss.str();
  52. }
  53. // Reports a missing-capability error to _'s diagnostic stream and returns
  54. // SPV_ERROR_INVALID_CAPABILITY.
  55. spv_result_t CapabilityError(ValidationState_t& _, int which_operand,
  56. SpvOp opcode,
  57. const std::string& required_capabilities) {
  58. return _.diag(SPV_ERROR_INVALID_CAPABILITY)
  59. << "Operand " << which_operand << " of " << spvOpcodeString(opcode)
  60. << " requires one of these capabilities: " << required_capabilities;
  61. }
  62. // Returns capabilities that enable an opcode. An empty result is interpreted
  63. // as no prohibition of use of the opcode. If the result is non-empty, then
  64. // the opcode may only be used if at least one of the capabilities is specified
  65. // by the module.
  66. CapabilitySet EnablingCapabilitiesForOp(const ValidationState_t& state,
  67. SpvOp opcode) {
  68. // Exceptions for SPV_AMD_shader_ballot
  69. switch (opcode) {
  70. // Normally these would require Group capability
  71. case SpvOpGroupIAddNonUniformAMD:
  72. case SpvOpGroupFAddNonUniformAMD:
  73. case SpvOpGroupFMinNonUniformAMD:
  74. case SpvOpGroupUMinNonUniformAMD:
  75. case SpvOpGroupSMinNonUniformAMD:
  76. case SpvOpGroupFMaxNonUniformAMD:
  77. case SpvOpGroupUMaxNonUniformAMD:
  78. case SpvOpGroupSMaxNonUniformAMD:
  79. if (state.HasExtension(libspirv::kSPV_AMD_shader_ballot))
  80. return CapabilitySet();
  81. break;
  82. default:
  83. break;
  84. }
  85. // Look it up in the grammar
  86. spv_opcode_desc opcode_desc = {};
  87. if (SPV_SUCCESS == state.grammar().lookupOpcode(opcode, &opcode_desc)) {
  88. return state.grammar().filterCapsAgainstTargetEnv(
  89. opcode_desc->capabilities, opcode_desc->numCapabilities);
  90. }
  91. return CapabilitySet();
  92. }
  93. // Returns an operand's required capabilities.
  94. CapabilitySet RequiredCapabilities(const ValidationState_t& state,
  95. spv_operand_type_t type, uint32_t operand) {
  96. // Mere mention of PointSize, ClipDistance, or CullDistance in a Builtin
  97. // decoration does not require the associated capability. The use of such
  98. // a variable value should trigger the capability requirement, but that's
  99. // not implemented yet. This rule is independent of target environment.
  100. // See https://github.com/KhronosGroup/SPIRV-Tools/issues/365
  101. if (type == SPV_OPERAND_TYPE_BUILT_IN) {
  102. switch (operand) {
  103. case SpvBuiltInPointSize:
  104. case SpvBuiltInClipDistance:
  105. case SpvBuiltInCullDistance:
  106. return CapabilitySet();
  107. default:
  108. break;
  109. }
  110. } else if (type == SPV_OPERAND_TYPE_FP_ROUNDING_MODE) {
  111. // Allow all FP rounding modes if requested
  112. if (state.features().free_fp_rounding_mode) {
  113. return CapabilitySet();
  114. }
  115. }
  116. spv_operand_desc operand_desc;
  117. const auto ret = state.grammar().lookupOperand(type, operand, &operand_desc);
  118. if (ret == SPV_SUCCESS) {
  119. // Allow FPRoundingMode decoration if requested.
  120. if (type == SPV_OPERAND_TYPE_DECORATION &&
  121. operand_desc->value == SpvDecorationFPRoundingMode) {
  122. if (state.features().free_fp_rounding_mode) return CapabilitySet();
  123. // Vulkan API requires more capabilities on rounding mode.
  124. if (spvIsVulkanEnv(state.context()->target_env)) {
  125. CapabilitySet cap_set;
  126. cap_set.Add(SpvCapabilityStorageUniformBufferBlock16);
  127. cap_set.Add(SpvCapabilityStorageUniform16);
  128. cap_set.Add(SpvCapabilityStoragePushConstant16);
  129. cap_set.Add(SpvCapabilityStorageInputOutput16);
  130. return cap_set;
  131. }
  132. }
  133. // Allow certain group operations if requested.
  134. if (state.features().group_ops_reduce_and_scans &&
  135. type == SPV_OPERAND_TYPE_GROUP_OPERATION &&
  136. (operand <= uint32_t(SpvGroupOperationExclusiveScan))) {
  137. return CapabilitySet();
  138. }
  139. return state.grammar().filterCapsAgainstTargetEnv(
  140. operand_desc->capabilities, operand_desc->numCapabilities);
  141. }
  142. return CapabilitySet();
  143. }
  144. // Returns operand's required extensions.
  145. ExtensionSet RequiredExtensions(const ValidationState_t& state,
  146. spv_operand_type_t type, uint32_t operand) {
  147. spv_operand_desc operand_desc;
  148. if (state.grammar().lookupOperand(type, operand, &operand_desc) ==
  149. SPV_SUCCESS) {
  150. assert(operand_desc);
  151. // If this operand is incorporated into core SPIR-V before or in the current
  152. // target environment, we don't require extensions anymore.
  153. if (spvVersionForTargetEnv(state.grammar().target_env()) >=
  154. operand_desc->minVersion)
  155. return {};
  156. return {operand_desc->numExtensions, operand_desc->extensions};
  157. }
  158. return {};
  159. }
  160. } // namespace
  161. namespace libspirv {
  162. spv_result_t CapabilityCheck(ValidationState_t& _,
  163. const spv_parsed_instruction_t* inst) {
  164. const SpvOp opcode = static_cast<SpvOp>(inst->opcode);
  165. CapabilitySet opcode_caps = EnablingCapabilitiesForOp(_, opcode);
  166. if (!_.HasAnyOfCapabilities(opcode_caps)) {
  167. return _.diag(SPV_ERROR_INVALID_CAPABILITY)
  168. << "Opcode " << spvOpcodeString(opcode)
  169. << " requires one of these capabilities: "
  170. << ToString(opcode_caps, _.grammar());
  171. }
  172. for (int i = 0; i < inst->num_operands; ++i) {
  173. const auto& operand = inst->operands[i];
  174. const auto word = inst->words[operand.offset];
  175. if (spvOperandIsConcreteMask(operand.type)) {
  176. // Check for required capabilities for each bit position of the mask.
  177. for (uint32_t mask_bit = 0x80000000; mask_bit; mask_bit >>= 1) {
  178. if (word & mask_bit) {
  179. const auto caps = RequiredCapabilities(_, operand.type, mask_bit);
  180. if (!_.HasAnyOfCapabilities(caps)) {
  181. return CapabilityError(_, i + 1, opcode,
  182. ToString(caps, _.grammar()));
  183. }
  184. }
  185. }
  186. } else if (spvIsIdType(operand.type)) {
  187. // TODO(dneto): Check the value referenced by this Id, if we can compute
  188. // it. For now, just punt, to fix issue 248:
  189. // https://github.com/KhronosGroup/SPIRV-Tools/issues/248
  190. } else {
  191. // Check the operand word as a whole.
  192. const auto caps = RequiredCapabilities(_, operand.type, word);
  193. if (!_.HasAnyOfCapabilities(caps)) {
  194. return CapabilityError(_, i + 1, opcode, ToString(caps, _.grammar()));
  195. }
  196. }
  197. }
  198. return SPV_SUCCESS;
  199. }
  200. // Checks that all extensions required by the given instruction's operands were
  201. // declared in the module.
  202. spv_result_t ExtensionCheck(ValidationState_t& _,
  203. const spv_parsed_instruction_t* inst) {
  204. const SpvOp opcode = static_cast<SpvOp>(inst->opcode);
  205. for (size_t operand_index = 0; operand_index < inst->num_operands;
  206. ++operand_index) {
  207. const auto& operand = inst->operands[operand_index];
  208. const uint32_t word = inst->words[operand.offset];
  209. const ExtensionSet required_extensions =
  210. RequiredExtensions(_, operand.type, word);
  211. if (!_.HasAnyOfExtensions(required_extensions)) {
  212. return _.diag(SPV_ERROR_MISSING_EXTENSION)
  213. << spvutils::CardinalToOrdinal(operand_index + 1) << " operand of "
  214. << spvOpcodeString(opcode) << ": operand " << word
  215. << " requires one of these extensions: "
  216. << ExtensionSetToString(required_extensions);
  217. }
  218. }
  219. return SPV_SUCCESS;
  220. }
  221. // Checks that the instruction can be used in this target environment.
  222. spv_result_t VersionCheck(ValidationState_t& _,
  223. const spv_parsed_instruction_t* inst) {
  224. const auto opcode = static_cast<SpvOp>(inst->opcode);
  225. spv_opcode_desc inst_desc;
  226. const bool r = _.grammar().lookupOpcode(opcode, &inst_desc);
  227. assert(r == SPV_SUCCESS);
  228. (void)r;
  229. const auto min_version = inst_desc->minVersion;
  230. ExtensionSet exts(inst_desc->numExtensions, inst_desc->extensions);
  231. if (exts.IsEmpty()) {
  232. // If no extensions can enable this instruction, then emit error messages
  233. // only concerning core SPIR-V versions if errors happen.
  234. if (min_version == ~0u) {
  235. return _.diag(SPV_ERROR_WRONG_VERSION)
  236. << spvOpcodeString(opcode) << " is reserved for future use.";
  237. }
  238. if (spvVersionForTargetEnv(_.grammar().target_env()) < min_version) {
  239. return _.diag(SPV_ERROR_WRONG_VERSION)
  240. << spvOpcodeString(opcode) << " requires "
  241. << spvTargetEnvDescription(
  242. static_cast<spv_target_env>(min_version))
  243. << " at minimum.";
  244. }
  245. }
  246. // Otherwise, we only error out when no enabling extensions are registered.
  247. else if (!_.HasAnyOfExtensions(exts)) {
  248. if (min_version == ~0u) {
  249. return _.diag(SPV_ERROR_MISSING_EXTENSION)
  250. << spvOpcodeString(opcode)
  251. << " requires one of the following extensions: "
  252. << ExtensionSetToString(exts);
  253. }
  254. if (static_cast<uint32_t>(_.grammar().target_env()) < min_version) {
  255. return _.diag(SPV_ERROR_WRONG_VERSION)
  256. << spvOpcodeString(opcode) << " requires "
  257. << spvTargetEnvDescription(
  258. static_cast<spv_target_env>(min_version))
  259. << " at minimum or one of the following extensions: "
  260. << ExtensionSetToString(exts);
  261. }
  262. }
  263. return SPV_SUCCESS;
  264. }
  265. // Checks that the Resuld <id> is within the valid bound.
  266. spv_result_t LimitCheckIdBound(ValidationState_t& _,
  267. const spv_parsed_instruction_t* inst) {
  268. if (inst->result_id >= _.getIdBound()) {
  269. return _.diag(SPV_ERROR_INVALID_BINARY)
  270. << "Result <id> '" << inst->result_id
  271. << "' must be less than the ID bound '" << _.getIdBound() << "'.";
  272. }
  273. return SPV_SUCCESS;
  274. }
  275. // Checks that the number of OpTypeStruct members is within the limit.
  276. spv_result_t LimitCheckStruct(ValidationState_t& _,
  277. const spv_parsed_instruction_t* inst) {
  278. if (SpvOpTypeStruct != inst->opcode) {
  279. return SPV_SUCCESS;
  280. }
  281. // Number of members is the number of operands of the instruction minus 1.
  282. // One operand is the result ID.
  283. const uint16_t limit =
  284. static_cast<uint16_t>(_.options()->universal_limits_.max_struct_members);
  285. if (inst->num_operands - 1 > limit) {
  286. return _.diag(SPV_ERROR_INVALID_BINARY)
  287. << "Number of OpTypeStruct members (" << inst->num_operands - 1
  288. << ") has exceeded the limit (" << limit << ").";
  289. }
  290. // Section 2.17 of SPIRV Spec specifies that the "Structure Nesting Depth"
  291. // must be less than or equal to 255.
  292. // This is interpreted as structures including other structures as members.
  293. // The code does not follow pointers or look into arrays to see if we reach a
  294. // structure downstream.
  295. // The nesting depth of a struct is 1+(largest depth of any member).
  296. // Scalars are at depth 0.
  297. uint32_t max_member_depth = 0;
  298. // Struct members start at word 2 of OpTypeStruct instruction.
  299. for (size_t word_i = 2; word_i < inst->num_words; ++word_i) {
  300. auto member = inst->words[word_i];
  301. auto memberTypeInstr = _.FindDef(member);
  302. if (memberTypeInstr && SpvOpTypeStruct == memberTypeInstr->opcode()) {
  303. max_member_depth = std::max(
  304. max_member_depth, _.struct_nesting_depth(memberTypeInstr->id()));
  305. }
  306. }
  307. const uint32_t depth_limit = _.options()->universal_limits_.max_struct_depth;
  308. const uint32_t cur_depth = 1 + max_member_depth;
  309. _.set_struct_nesting_depth(inst->result_id, cur_depth);
  310. if (cur_depth > depth_limit) {
  311. return _.diag(SPV_ERROR_INVALID_BINARY)
  312. << "Structure Nesting Depth may not be larger than " << depth_limit
  313. << ". Found " << cur_depth << ".";
  314. }
  315. return SPV_SUCCESS;
  316. }
  317. // Checks that the number of (literal, label) pairs in OpSwitch is within the
  318. // limit.
  319. spv_result_t LimitCheckSwitch(ValidationState_t& _,
  320. const spv_parsed_instruction_t* inst) {
  321. if (SpvOpSwitch == inst->opcode) {
  322. // The instruction syntax is as follows:
  323. // OpSwitch <selector ID> <Default ID> literal label literal label ...
  324. // literal,label pairs come after the first 2 operands.
  325. // It is guaranteed at this point that num_operands is an even numner.
  326. unsigned int num_pairs = (inst->num_operands - 2) / 2;
  327. const unsigned int num_pairs_limit =
  328. _.options()->universal_limits_.max_switch_branches;
  329. if (num_pairs > num_pairs_limit) {
  330. return _.diag(SPV_ERROR_INVALID_BINARY)
  331. << "Number of (literal, label) pairs in OpSwitch (" << num_pairs
  332. << ") exceeds the limit (" << num_pairs_limit << ").";
  333. }
  334. }
  335. return SPV_SUCCESS;
  336. }
  337. // Ensure the number of variables of the given class does not exceed the limit.
  338. spv_result_t LimitCheckNumVars(ValidationState_t& _, const uint32_t var_id,
  339. const SpvStorageClass storage_class) {
  340. if (SpvStorageClassFunction == storage_class) {
  341. _.registerLocalVariable(var_id);
  342. const uint32_t num_local_vars_limit =
  343. _.options()->universal_limits_.max_local_variables;
  344. if (_.num_local_vars() > num_local_vars_limit) {
  345. return _.diag(SPV_ERROR_INVALID_BINARY)
  346. << "Number of local variables ('Function' Storage Class) "
  347. "exceeded the valid limit ("
  348. << num_local_vars_limit << ").";
  349. }
  350. } else {
  351. _.registerGlobalVariable(var_id);
  352. const uint32_t num_global_vars_limit =
  353. _.options()->universal_limits_.max_global_variables;
  354. if (_.num_global_vars() > num_global_vars_limit) {
  355. return _.diag(SPV_ERROR_INVALID_BINARY)
  356. << "Number of Global Variables (Storage Class other than "
  357. "'Function') exceeded the valid limit ("
  358. << num_global_vars_limit << ").";
  359. }
  360. }
  361. return SPV_SUCCESS;
  362. }
  363. // Registers necessary decoration(s) for the appropriate IDs based on the
  364. // instruction.
  365. spv_result_t RegisterDecorations(ValidationState_t& _,
  366. const spv_parsed_instruction_t* inst) {
  367. switch (inst->opcode) {
  368. case SpvOpDecorate: {
  369. const uint32_t target_id = inst->words[1];
  370. const SpvDecoration dec_type = static_cast<SpvDecoration>(inst->words[2]);
  371. std::vector<uint32_t> dec_params;
  372. if (inst->num_words > 3) {
  373. dec_params.insert(dec_params.end(), inst->words + 3,
  374. inst->words + inst->num_words);
  375. }
  376. _.RegisterDecorationForId(target_id, Decoration(dec_type, dec_params));
  377. break;
  378. }
  379. case SpvOpMemberDecorate: {
  380. const uint32_t struct_id = inst->words[1];
  381. const uint32_t index = inst->words[2];
  382. const SpvDecoration dec_type = static_cast<SpvDecoration>(inst->words[3]);
  383. std::vector<uint32_t> dec_params;
  384. if (inst->num_words > 4) {
  385. dec_params.insert(dec_params.end(), inst->words + 4,
  386. inst->words + inst->num_words);
  387. }
  388. _.RegisterDecorationForId(struct_id,
  389. Decoration(dec_type, dec_params, index));
  390. break;
  391. }
  392. case SpvOpDecorationGroup: {
  393. // We don't need to do anything right now. Assigning decorations to groups
  394. // will be taken care of via OpGroupDecorate.
  395. break;
  396. }
  397. case SpvOpGroupDecorate: {
  398. // Word 1 is the group <id>. All subsequent words are target <id>s that
  399. // are going to be decorated with the decorations.
  400. const uint32_t decoration_group_id = inst->words[1];
  401. std::vector<Decoration>& group_decorations =
  402. _.id_decorations(decoration_group_id);
  403. for (int i = 2; i < inst->num_words; ++i) {
  404. const uint32_t target_id = inst->words[i];
  405. _.RegisterDecorationsForId(target_id, group_decorations.begin(),
  406. group_decorations.end());
  407. }
  408. break;
  409. }
  410. case SpvOpGroupMemberDecorate: {
  411. // Word 1 is the Decoration Group <id> followed by (struct<id>,literal)
  412. // pairs. All decorations of the group should be applied to all the struct
  413. // members that are specified in the instructions.
  414. const uint32_t decoration_group_id = inst->words[1];
  415. std::vector<Decoration>& group_decorations =
  416. _.id_decorations(decoration_group_id);
  417. // Grammar checks ensures that the number of arguments to this instruction
  418. // is an odd number: 1 decoration group + (id,literal) pairs.
  419. for (int i = 2; i + 1 < inst->num_words; i = i + 2) {
  420. const uint32_t struct_id = inst->words[i];
  421. const uint32_t index = inst->words[i + 1];
  422. // ID validation phase ensures this is in fact a struct instruction and
  423. // that the index is not out of bound.
  424. _.RegisterDecorationsForStructMember(struct_id, index,
  425. group_decorations.begin(),
  426. group_decorations.end());
  427. }
  428. break;
  429. }
  430. default:
  431. break;
  432. }
  433. return SPV_SUCCESS;
  434. }
  435. // Parses OpExtension instruction and logs warnings if unsuccessful.
  436. void CheckIfKnownExtension(ValidationState_t& _,
  437. const spv_parsed_instruction_t* inst) {
  438. const std::string extension_str = GetExtensionString(inst);
  439. Extension extension;
  440. if (!GetExtensionFromString(extension_str.c_str(), &extension)) {
  441. _.diag(SPV_SUCCESS) << "Found unrecognized extension " << extension_str;
  442. return;
  443. }
  444. }
  445. spv_result_t InstructionPass(ValidationState_t& _,
  446. const spv_parsed_instruction_t* inst) {
  447. const SpvOp opcode = static_cast<SpvOp>(inst->opcode);
  448. if (opcode == SpvOpExtension) {
  449. CheckIfKnownExtension(_, inst);
  450. } else if (opcode == SpvOpCapability) {
  451. _.RegisterCapability(
  452. static_cast<SpvCapability>(inst->words[inst->operands[0].offset]));
  453. } else if (opcode == SpvOpMemoryModel) {
  454. _.set_addressing_model(
  455. static_cast<SpvAddressingModel>(inst->words[inst->operands[0].offset]));
  456. _.set_memory_model(
  457. static_cast<SpvMemoryModel>(inst->words[inst->operands[1].offset]));
  458. } else if (opcode == SpvOpExecutionMode) {
  459. const uint32_t entry_point = inst->words[1];
  460. _.RegisterExecutionModeForEntryPoint(entry_point,
  461. SpvExecutionMode(inst->words[2]));
  462. } else if (opcode == SpvOpVariable) {
  463. const auto storage_class =
  464. static_cast<SpvStorageClass>(inst->words[inst->operands[2].offset]);
  465. if (auto error = LimitCheckNumVars(_, inst->result_id, storage_class)) {
  466. return error;
  467. }
  468. if (storage_class == SpvStorageClassGeneric)
  469. return _.diag(SPV_ERROR_INVALID_BINARY)
  470. << "OpVariable storage class cannot be Generic";
  471. if (_.current_layout_section() == kLayoutFunctionDefinitions) {
  472. if (storage_class != SpvStorageClassFunction) {
  473. return _.diag(SPV_ERROR_INVALID_LAYOUT)
  474. << "Variables must have a function[7] storage class inside"
  475. " of a function";
  476. }
  477. if (_.current_function().IsFirstBlock(
  478. _.current_function().current_block()->id()) == false) {
  479. return _.diag(SPV_ERROR_INVALID_CFG) << "Variables can only be defined "
  480. "in the first block of a "
  481. "function";
  482. }
  483. } else {
  484. if (storage_class == SpvStorageClassFunction) {
  485. return _.diag(SPV_ERROR_INVALID_LAYOUT)
  486. << "Variables can not have a function[7] storage class "
  487. "outside of a function";
  488. }
  489. }
  490. }
  491. // SPIR-V Spec 2.16.3: Validation Rules for Kernel Capabilities: The
  492. // Signedness in OpTypeInt must always be 0.
  493. if (SpvOpTypeInt == inst->opcode && _.HasCapability(SpvCapabilityKernel) &&
  494. inst->words[inst->operands[2].offset] != 0u) {
  495. return _.diag(SPV_ERROR_INVALID_BINARY) << "The Signedness in OpTypeInt "
  496. "must always be 0 when Kernel "
  497. "capability is used.";
  498. }
  499. // In order to validate decoration rules, we need to know all the decorations
  500. // that are applied to any given <id>.
  501. RegisterDecorations(_, inst);
  502. if (auto error = ExtensionCheck(_, inst)) return error;
  503. if (auto error = CapabilityCheck(_, inst)) return error;
  504. if (auto error = LimitCheckIdBound(_, inst)) return error;
  505. if (auto error = LimitCheckStruct(_, inst)) return error;
  506. if (auto error = LimitCheckSwitch(_, inst)) return error;
  507. if (auto error = VersionCheck(_, inst)) return error;
  508. // All instruction checks have passed.
  509. return SPV_SUCCESS;
  510. }
  511. } // namespace libspirv