validate_instruction.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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 <cassert>
  16. #include <sstream>
  17. #include <string>
  18. #include <vector>
  19. #include "source/enum_set.h"
  20. #include "source/enum_string_mapping.h"
  21. #include "source/extensions.h"
  22. #include "source/opcode.h"
  23. #include "source/operand.h"
  24. #include "source/spirv_constant.h"
  25. #include "source/spirv_target_env.h"
  26. #include "source/spirv_validator_options.h"
  27. #include "source/util/string_utils.h"
  28. #include "source/val/validate.h"
  29. #include "source/val/validation_state.h"
  30. namespace spvtools {
  31. namespace val {
  32. namespace {
  33. std::string ToString(const CapabilitySet& capabilities,
  34. const AssemblyGrammar& grammar) {
  35. std::stringstream ss;
  36. for (auto capability : capabilities) {
  37. spv_operand_desc desc;
  38. if (SPV_SUCCESS == grammar.lookupOperand(SPV_OPERAND_TYPE_CAPABILITY,
  39. uint32_t(capability), &desc))
  40. ss << desc->name << " ";
  41. else
  42. ss << uint32_t(capability) << " ";
  43. }
  44. return ss.str();
  45. }
  46. // Returns capabilities that enable an opcode. An empty result is interpreted
  47. // as no prohibition of use of the opcode. If the result is non-empty, then
  48. // the opcode may only be used if at least one of the capabilities is specified
  49. // by the module.
  50. CapabilitySet EnablingCapabilitiesForOp(const ValidationState_t& state,
  51. spv::Op opcode) {
  52. // Exceptions for SPV_AMD_shader_ballot
  53. switch (opcode) {
  54. // Normally these would require Group capability
  55. case spv::Op::OpGroupIAddNonUniformAMD:
  56. case spv::Op::OpGroupFAddNonUniformAMD:
  57. case spv::Op::OpGroupFMinNonUniformAMD:
  58. case spv::Op::OpGroupUMinNonUniformAMD:
  59. case spv::Op::OpGroupSMinNonUniformAMD:
  60. case spv::Op::OpGroupFMaxNonUniformAMD:
  61. case spv::Op::OpGroupUMaxNonUniformAMD:
  62. case spv::Op::OpGroupSMaxNonUniformAMD:
  63. if (state.HasExtension(kSPV_AMD_shader_ballot)) return CapabilitySet();
  64. break;
  65. default:
  66. break;
  67. }
  68. // Look it up in the grammar
  69. spv_opcode_desc opcode_desc = {};
  70. if (SPV_SUCCESS == state.grammar().lookupOpcode(opcode, &opcode_desc)) {
  71. return state.grammar().filterCapsAgainstTargetEnv(
  72. opcode_desc->capabilities, opcode_desc->numCapabilities);
  73. }
  74. return CapabilitySet();
  75. }
  76. // Returns SPV_SUCCESS if, for the given operand, the target environment
  77. // satsifies minimum version requirements, or if the module declares an
  78. // enabling extension for the operand. Otherwise emit a diagnostic and
  79. // return an error code.
  80. spv_result_t OperandVersionExtensionCheck(
  81. ValidationState_t& _, const Instruction* inst, size_t which_operand,
  82. const spv_operand_desc_t& operand_desc, uint32_t word) {
  83. const uint32_t module_version = _.version();
  84. const uint32_t operand_min_version = operand_desc.minVersion;
  85. const uint32_t operand_last_version = operand_desc.lastVersion;
  86. const bool reserved = operand_min_version == 0xffffffffu;
  87. const bool version_satisfied = !reserved &&
  88. (operand_min_version <= module_version) &&
  89. (module_version <= operand_last_version);
  90. if (version_satisfied) {
  91. return SPV_SUCCESS;
  92. }
  93. if (operand_last_version < module_version) {
  94. return _.diag(SPV_ERROR_WRONG_VERSION, inst)
  95. << spvtools::utils::CardinalToOrdinal(which_operand)
  96. << " operand of " << spvOpcodeString(inst->opcode()) << ": operand "
  97. << operand_desc.name << "(" << word << ") requires SPIR-V version "
  98. << SPV_SPIRV_VERSION_MAJOR_PART(operand_last_version) << "."
  99. << SPV_SPIRV_VERSION_MINOR_PART(operand_last_version)
  100. << " or earlier";
  101. }
  102. if (!reserved && operand_desc.numExtensions == 0) {
  103. return _.diag(SPV_ERROR_WRONG_VERSION, inst)
  104. << spvtools::utils::CardinalToOrdinal(which_operand)
  105. << " operand of " << spvOpcodeString(inst->opcode()) << ": operand "
  106. << operand_desc.name << "(" << word << ") requires SPIR-V version "
  107. << SPV_SPIRV_VERSION_MAJOR_PART(operand_min_version) << "."
  108. << SPV_SPIRV_VERSION_MINOR_PART(operand_min_version) << " or later";
  109. } else {
  110. ExtensionSet required_extensions(operand_desc.numExtensions,
  111. operand_desc.extensions);
  112. if (!_.HasAnyOfExtensions(required_extensions)) {
  113. return _.diag(SPV_ERROR_MISSING_EXTENSION, inst)
  114. << spvtools::utils::CardinalToOrdinal(which_operand)
  115. << " operand of " << spvOpcodeString(inst->opcode())
  116. << ": operand " << operand_desc.name << "(" << word
  117. << ") requires one of these extensions: "
  118. << ExtensionSetToString(required_extensions);
  119. }
  120. }
  121. return SPV_SUCCESS;
  122. }
  123. // Returns SPV_SUCCESS if the given operand is enabled by capabilities declared
  124. // in the module. Otherwise issues an error message and returns
  125. // SPV_ERROR_INVALID_CAPABILITY.
  126. spv_result_t CheckRequiredCapabilities(ValidationState_t& state,
  127. const Instruction* inst,
  128. size_t which_operand,
  129. const spv_parsed_operand_t& operand,
  130. uint32_t word) {
  131. // Mere mention of PointSize, ClipDistance, or CullDistance in a Builtin
  132. // decoration does not require the associated capability. The use of such
  133. // a variable value should trigger the capability requirement, but that's
  134. // not implemented yet. This rule is independent of target environment.
  135. // See https://github.com/KhronosGroup/SPIRV-Tools/issues/365
  136. if (operand.type == SPV_OPERAND_TYPE_BUILT_IN) {
  137. switch (spv::BuiltIn(word)) {
  138. case spv::BuiltIn::PointSize:
  139. case spv::BuiltIn::ClipDistance:
  140. case spv::BuiltIn::CullDistance:
  141. return SPV_SUCCESS;
  142. default:
  143. break;
  144. }
  145. } else if (operand.type == SPV_OPERAND_TYPE_FP_ROUNDING_MODE) {
  146. // Allow all FP rounding modes if requested
  147. if (state.features().free_fp_rounding_mode) {
  148. return SPV_SUCCESS;
  149. }
  150. } else if (operand.type == SPV_OPERAND_TYPE_GROUP_OPERATION &&
  151. state.features().group_ops_reduce_and_scans &&
  152. (word <= uint32_t(spv::GroupOperation::ExclusiveScan))) {
  153. // Allow certain group operations if requested.
  154. return SPV_SUCCESS;
  155. }
  156. CapabilitySet enabling_capabilities;
  157. spv_operand_desc operand_desc = nullptr;
  158. const auto lookup_result =
  159. state.grammar().lookupOperand(operand.type, word, &operand_desc);
  160. if (lookup_result == SPV_SUCCESS) {
  161. // Allow FPRoundingMode decoration if requested.
  162. if (operand.type == SPV_OPERAND_TYPE_DECORATION &&
  163. spv::Decoration(operand_desc->value) ==
  164. spv::Decoration::FPRoundingMode) {
  165. if (state.features().free_fp_rounding_mode) return SPV_SUCCESS;
  166. // Vulkan API requires more capabilities on rounding mode.
  167. if (spvIsVulkanEnv(state.context()->target_env)) {
  168. enabling_capabilities.insert(
  169. spv::Capability::StorageUniformBufferBlock16);
  170. enabling_capabilities.insert(spv::Capability::StorageUniform16);
  171. enabling_capabilities.insert(spv::Capability::StoragePushConstant16);
  172. enabling_capabilities.insert(spv::Capability::StorageInputOutput16);
  173. }
  174. } else {
  175. enabling_capabilities = state.grammar().filterCapsAgainstTargetEnv(
  176. operand_desc->capabilities, operand_desc->numCapabilities);
  177. }
  178. // When encountering an OpCapability instruction, the instruction pass
  179. // registers a capability with the module *before* checking capabilities.
  180. // So in the case of an OpCapability instruction, don't bother checking
  181. // enablement by another capability.
  182. if (inst->opcode() != spv::Op::OpCapability) {
  183. const bool enabled_by_cap =
  184. state.HasAnyOfCapabilities(enabling_capabilities);
  185. if (!enabling_capabilities.empty() && !enabled_by_cap) {
  186. return state.diag(SPV_ERROR_INVALID_CAPABILITY, inst)
  187. << "Operand " << which_operand << " of "
  188. << spvOpcodeString(inst->opcode())
  189. << " requires one of these capabilities: "
  190. << ToString(enabling_capabilities, state.grammar());
  191. }
  192. }
  193. return OperandVersionExtensionCheck(state, inst, which_operand,
  194. *operand_desc, word);
  195. }
  196. return SPV_SUCCESS;
  197. }
  198. // Returns SPV_ERROR_INVALID_BINARY and emits a diagnostic if the instruction
  199. // is explicitly reserved in the SPIR-V core spec. Otherwise return
  200. // SPV_SUCCESS.
  201. spv_result_t ReservedCheck(ValidationState_t& _, const Instruction* inst) {
  202. const spv::Op opcode = inst->opcode();
  203. switch (opcode) {
  204. // These instructions are enabled by a capability, but should never
  205. // be used anyway.
  206. case spv::Op::OpImageSparseSampleProjImplicitLod:
  207. case spv::Op::OpImageSparseSampleProjExplicitLod:
  208. case spv::Op::OpImageSparseSampleProjDrefImplicitLod:
  209. case spv::Op::OpImageSparseSampleProjDrefExplicitLod: {
  210. spv_opcode_desc inst_desc;
  211. _.grammar().lookupOpcode(opcode, &inst_desc);
  212. return _.diag(SPV_ERROR_INVALID_BINARY, inst)
  213. << "Invalid Opcode name 'Op" << inst_desc->name << "'";
  214. }
  215. default:
  216. break;
  217. }
  218. return SPV_SUCCESS;
  219. }
  220. // Returns SPV_ERROR_INVALID_CAPABILITY and emits a diagnostic if the
  221. // instruction is invalid because the required capability isn't declared
  222. // in the module.
  223. spv_result_t CapabilityCheck(ValidationState_t& _, const Instruction* inst) {
  224. const spv::Op opcode = inst->opcode();
  225. CapabilitySet opcode_caps = EnablingCapabilitiesForOp(_, opcode);
  226. if (!_.HasAnyOfCapabilities(opcode_caps)) {
  227. return _.diag(SPV_ERROR_INVALID_CAPABILITY, inst)
  228. << "Opcode " << spvOpcodeString(opcode)
  229. << " requires one of these capabilities: "
  230. << ToString(opcode_caps, _.grammar());
  231. }
  232. for (size_t i = 0; i < inst->operands().size(); ++i) {
  233. const auto& operand = inst->operand(i);
  234. const auto word = inst->word(operand.offset);
  235. if (spvOperandIsConcreteMask(operand.type)) {
  236. // Check for required capabilities for each bit position of the mask.
  237. for (uint32_t mask_bit = 0x80000000; mask_bit; mask_bit >>= 1) {
  238. if (word & mask_bit) {
  239. spv_result_t status =
  240. CheckRequiredCapabilities(_, inst, i + 1, operand, mask_bit);
  241. if (status != SPV_SUCCESS) return status;
  242. }
  243. }
  244. } else if (spvIsIdType(operand.type)) {
  245. // TODO(dneto): Check the value referenced by this Id, if we can compute
  246. // it. For now, just punt, to fix issue 248:
  247. // https://github.com/KhronosGroup/SPIRV-Tools/issues/248
  248. } else {
  249. // Check the operand word as a whole.
  250. spv_result_t status =
  251. CheckRequiredCapabilities(_, inst, i + 1, operand, word);
  252. if (status != SPV_SUCCESS) return status;
  253. }
  254. }
  255. return SPV_SUCCESS;
  256. }
  257. // Checks that the instruction can be used in this target environment's base
  258. // version. Assumes that CapabilityCheck has checked direct capability
  259. // dependencies for the opcode.
  260. spv_result_t VersionCheck(ValidationState_t& _, const Instruction* inst) {
  261. const auto opcode = inst->opcode();
  262. spv_opcode_desc inst_desc;
  263. const spv_result_t r = _.grammar().lookupOpcode(opcode, &inst_desc);
  264. assert(r == SPV_SUCCESS);
  265. (void)r;
  266. const auto min_version = inst_desc->minVersion;
  267. const auto last_version = inst_desc->lastVersion;
  268. const auto module_version = _.version();
  269. if (last_version < module_version) {
  270. return _.diag(SPV_ERROR_WRONG_VERSION, inst)
  271. << spvOpcodeString(opcode) << " requires SPIR-V version "
  272. << SPV_SPIRV_VERSION_MAJOR_PART(last_version) << "."
  273. << SPV_SPIRV_VERSION_MINOR_PART(last_version) << " or earlier";
  274. }
  275. // OpTerminateInvocation is special because it is enabled by Shader
  276. // capability, but also requires an extension and/or version check.
  277. const bool capability_check_is_sufficient =
  278. inst->opcode() != spv::Op::OpTerminateInvocation;
  279. if (capability_check_is_sufficient && (inst_desc->numCapabilities > 0u)) {
  280. // We already checked that the direct capability dependency has been
  281. // satisfied. We don't need to check any further.
  282. return SPV_SUCCESS;
  283. }
  284. ExtensionSet exts(inst_desc->numExtensions, inst_desc->extensions);
  285. if (exts.empty()) {
  286. // If no extensions can enable this instruction, then emit error
  287. // messages only concerning core SPIR-V versions if errors happen.
  288. if (min_version == ~0u) {
  289. return _.diag(SPV_ERROR_WRONG_VERSION, inst)
  290. << spvOpcodeString(opcode) << " is reserved for future use.";
  291. }
  292. if (module_version < min_version) {
  293. return _.diag(SPV_ERROR_WRONG_VERSION, inst)
  294. << spvOpcodeString(opcode) << " requires SPIR-V version "
  295. << SPV_SPIRV_VERSION_MAJOR_PART(min_version) << "."
  296. << SPV_SPIRV_VERSION_MINOR_PART(min_version) << " at minimum.";
  297. }
  298. } else if (!_.HasAnyOfExtensions(exts)) {
  299. // Otherwise, we only error out when no enabling extensions are
  300. // registered.
  301. if (min_version == ~0u) {
  302. return _.diag(SPV_ERROR_MISSING_EXTENSION, inst)
  303. << spvOpcodeString(opcode)
  304. << " requires one of the following extensions: "
  305. << ExtensionSetToString(exts);
  306. }
  307. if (module_version < min_version) {
  308. return _.diag(SPV_ERROR_WRONG_VERSION, inst)
  309. << spvOpcodeString(opcode) << " requires SPIR-V version "
  310. << SPV_SPIRV_VERSION_MAJOR_PART(min_version) << "."
  311. << SPV_SPIRV_VERSION_MINOR_PART(min_version)
  312. << " at minimum or one of the following extensions: "
  313. << ExtensionSetToString(exts);
  314. }
  315. }
  316. return SPV_SUCCESS;
  317. }
  318. // Checks that the Resuld <id> is within the valid bound.
  319. spv_result_t LimitCheckIdBound(ValidationState_t& _, const Instruction* inst) {
  320. if (inst->id() >= _.getIdBound()) {
  321. return _.diag(SPV_ERROR_INVALID_BINARY, inst)
  322. << "Result <id> '" << inst->id()
  323. << "' must be less than the ID bound '" << _.getIdBound() << "'.";
  324. }
  325. return SPV_SUCCESS;
  326. }
  327. // Checks that the number of OpTypeStruct members is within the limit.
  328. spv_result_t LimitCheckStruct(ValidationState_t& _, const Instruction* inst) {
  329. if (spv::Op::OpTypeStruct != inst->opcode()) {
  330. return SPV_SUCCESS;
  331. }
  332. // Number of members is the number of operands of the instruction minus 1.
  333. // One operand is the result ID.
  334. const uint16_t limit =
  335. static_cast<uint16_t>(_.options()->universal_limits_.max_struct_members);
  336. if (inst->operands().size() - 1 > limit) {
  337. return _.diag(SPV_ERROR_INVALID_BINARY, inst)
  338. << "Number of OpTypeStruct members (" << inst->operands().size() - 1
  339. << ") has exceeded the limit (" << limit << ").";
  340. }
  341. // Section 2.17 of SPIRV Spec specifies that the "Structure Nesting Depth"
  342. // must be less than or equal to 255.
  343. // This is interpreted as structures including other structures as
  344. // members. The code does not follow pointers or look into arrays to see
  345. // if we reach a structure downstream. The nesting depth of a struct is
  346. // 1+(largest depth of any member). Scalars are at depth 0.
  347. uint32_t max_member_depth = 0;
  348. // Struct members start at word 2 of OpTypeStruct instruction.
  349. for (size_t word_i = 2; word_i < inst->words().size(); ++word_i) {
  350. auto member = inst->word(word_i);
  351. auto memberTypeInstr = _.FindDef(member);
  352. if (memberTypeInstr && spv::Op::OpTypeStruct == memberTypeInstr->opcode()) {
  353. max_member_depth = std::max(
  354. max_member_depth, _.struct_nesting_depth(memberTypeInstr->id()));
  355. }
  356. }
  357. const uint32_t depth_limit = _.options()->universal_limits_.max_struct_depth;
  358. const uint32_t cur_depth = 1 + max_member_depth;
  359. _.set_struct_nesting_depth(inst->id(), cur_depth);
  360. if (cur_depth > depth_limit) {
  361. return _.diag(SPV_ERROR_INVALID_BINARY, inst)
  362. << "Structure Nesting Depth may not be larger than " << depth_limit
  363. << ". Found " << cur_depth << ".";
  364. }
  365. return SPV_SUCCESS;
  366. }
  367. // Checks that the number of (literal, label) pairs in OpSwitch is within
  368. // the limit.
  369. spv_result_t LimitCheckSwitch(ValidationState_t& _, const Instruction* inst) {
  370. if (spv::Op::OpSwitch == inst->opcode()) {
  371. // The instruction syntax is as follows:
  372. // OpSwitch <selector ID> <Default ID> literal label literal label ...
  373. // literal,label pairs come after the first 2 operands.
  374. // It is guaranteed at this point that num_operands is an even number.
  375. size_t num_pairs = (inst->operands().size() - 2) / 2;
  376. const unsigned int num_pairs_limit =
  377. _.options()->universal_limits_.max_switch_branches;
  378. if (num_pairs > num_pairs_limit) {
  379. return _.diag(SPV_ERROR_INVALID_BINARY, inst)
  380. << "Number of (literal, label) pairs in OpSwitch (" << num_pairs
  381. << ") exceeds the limit (" << num_pairs_limit << ").";
  382. }
  383. }
  384. return SPV_SUCCESS;
  385. }
  386. // Ensure the number of variables of the given class does not exceed the
  387. // limit.
  388. spv_result_t LimitCheckNumVars(ValidationState_t& _, const uint32_t var_id,
  389. const spv::StorageClass storage_class) {
  390. if (spv::StorageClass::Function == storage_class) {
  391. _.registerLocalVariable(var_id);
  392. const uint32_t num_local_vars_limit =
  393. _.options()->universal_limits_.max_local_variables;
  394. if (_.num_local_vars() > num_local_vars_limit) {
  395. return _.diag(SPV_ERROR_INVALID_BINARY, nullptr)
  396. << "Number of local variables ('Function' Storage Class) "
  397. "exceeded the valid limit ("
  398. << num_local_vars_limit << ").";
  399. }
  400. } else {
  401. _.registerGlobalVariable(var_id);
  402. const uint32_t num_global_vars_limit =
  403. _.options()->universal_limits_.max_global_variables;
  404. if (_.num_global_vars() > num_global_vars_limit) {
  405. return _.diag(SPV_ERROR_INVALID_BINARY, nullptr)
  406. << "Number of Global Variables (Storage Class other than "
  407. "'Function') exceeded the valid limit ("
  408. << num_global_vars_limit << ").";
  409. }
  410. }
  411. return SPV_SUCCESS;
  412. }
  413. // Parses OpExtension instruction and logs warnings if unsuccessful.
  414. spv_result_t CheckIfKnownExtension(ValidationState_t& _,
  415. const Instruction* inst) {
  416. const std::string extension_str = GetExtensionString(&(inst->c_inst()));
  417. Extension extension;
  418. if (!GetExtensionFromString(extension_str.c_str(), &extension)) {
  419. return _.diag(SPV_WARNING, inst)
  420. << "Found unrecognized extension " << extension_str;
  421. }
  422. return SPV_SUCCESS;
  423. }
  424. } // namespace
  425. spv_result_t InstructionPass(ValidationState_t& _, const Instruction* inst) {
  426. const spv::Op opcode = inst->opcode();
  427. if (opcode == spv::Op::OpExtension) {
  428. CheckIfKnownExtension(_, inst);
  429. } else if (opcode == spv::Op::OpCapability) {
  430. _.RegisterCapability(inst->GetOperandAs<spv::Capability>(0));
  431. } else if (opcode == spv::Op::OpMemoryModel) {
  432. if (_.has_memory_model_specified()) {
  433. return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
  434. << "OpMemoryModel should only be provided once.";
  435. }
  436. _.set_addressing_model(inst->GetOperandAs<spv::AddressingModel>(0));
  437. _.set_memory_model(inst->GetOperandAs<spv::MemoryModel>(1));
  438. } else if (opcode == spv::Op::OpExecutionMode ||
  439. opcode == spv::Op::OpExecutionModeId) {
  440. const uint32_t entry_point = inst->word(1);
  441. _.RegisterExecutionModeForEntryPoint(entry_point,
  442. spv::ExecutionMode(inst->word(2)));
  443. if (inst->GetOperandAs<spv::ExecutionMode>(1) ==
  444. spv::ExecutionMode::LocalSize ||
  445. inst->GetOperandAs<spv::ExecutionMode>(1) ==
  446. spv::ExecutionMode::LocalSizeId) {
  447. _.RegisterEntryPointLocalSize(entry_point, inst);
  448. }
  449. if (inst->GetOperandAs<spv::ExecutionMode>(1) ==
  450. spv::ExecutionMode::OutputPrimitivesEXT) {
  451. _.RegisterEntryPointOutputPrimitivesEXT(entry_point, inst);
  452. }
  453. } else if (opcode == spv::Op::OpVariable) {
  454. const auto storage_class = inst->GetOperandAs<spv::StorageClass>(2);
  455. if (auto error = LimitCheckNumVars(_, inst->id(), storage_class)) {
  456. return error;
  457. }
  458. } else if (opcode == spv::Op::OpSamplerImageAddressingModeNV) {
  459. if (!_.HasCapability(spv::Capability::BindlessTextureNV)) {
  460. return _.diag(SPV_ERROR_MISSING_EXTENSION, inst)
  461. << "OpSamplerImageAddressingModeNV supported only with extension "
  462. "SPV_NV_bindless_texture";
  463. }
  464. uint32_t bitwidth = inst->GetOperandAs<uint32_t>(0);
  465. if (_.samplerimage_variable_address_mode() != 0) {
  466. return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
  467. << "OpSamplerImageAddressingModeNV should only be provided once";
  468. }
  469. if (bitwidth != 32 && bitwidth != 64) {
  470. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  471. << "OpSamplerImageAddressingModeNV bitwidth should be 64 or 32";
  472. }
  473. _.set_samplerimage_variable_address_mode(bitwidth);
  474. }
  475. if (auto error = ReservedCheck(_, inst)) return error;
  476. if (auto error = CapabilityCheck(_, inst)) return error;
  477. if (auto error = LimitCheckIdBound(_, inst)) return error;
  478. if (auto error = LimitCheckStruct(_, inst)) return error;
  479. if (auto error = LimitCheckSwitch(_, inst)) return error;
  480. if (auto error = VersionCheck(_, inst)) return error;
  481. // All instruction checks have passed.
  482. return SPV_SUCCESS;
  483. }
  484. } // namespace val
  485. } // namespace spvtools