2
0

validate_instruction.cpp 22 KB

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