validate_instruction.cpp 21 KB

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