validate_annotation.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. // Copyright (c) 2018 Google LLC.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "source/opcode.h"
  15. #include "source/spirv_target_env.h"
  16. #include "source/val/instruction.h"
  17. #include "source/val/validate.h"
  18. #include "source/val/validation_state.h"
  19. namespace spvtools {
  20. namespace val {
  21. namespace {
  22. std::string LogStringForDecoration(uint32_t decoration) {
  23. switch (decoration) {
  24. case SpvDecorationRelaxedPrecision:
  25. return "RelaxedPrecision";
  26. case SpvDecorationSpecId:
  27. return "SpecId";
  28. case SpvDecorationBlock:
  29. return "Block";
  30. case SpvDecorationBufferBlock:
  31. return "BufferBlock";
  32. case SpvDecorationRowMajor:
  33. return "RowMajor";
  34. case SpvDecorationColMajor:
  35. return "ColMajor";
  36. case SpvDecorationArrayStride:
  37. return "ArrayStride";
  38. case SpvDecorationMatrixStride:
  39. return "MatrixStride";
  40. case SpvDecorationGLSLShared:
  41. return "GLSLShared";
  42. case SpvDecorationGLSLPacked:
  43. return "GLSLPacked";
  44. case SpvDecorationCPacked:
  45. return "CPacked";
  46. case SpvDecorationBuiltIn:
  47. return "BuiltIn";
  48. case SpvDecorationNoPerspective:
  49. return "NoPerspective";
  50. case SpvDecorationFlat:
  51. return "Flat";
  52. case SpvDecorationPatch:
  53. return "Patch";
  54. case SpvDecorationCentroid:
  55. return "Centroid";
  56. case SpvDecorationSample:
  57. return "Sample";
  58. case SpvDecorationInvariant:
  59. return "Invariant";
  60. case SpvDecorationRestrict:
  61. return "Restrict";
  62. case SpvDecorationAliased:
  63. return "Aliased";
  64. case SpvDecorationVolatile:
  65. return "Volatile";
  66. case SpvDecorationConstant:
  67. return "Constant";
  68. case SpvDecorationCoherent:
  69. return "Coherent";
  70. case SpvDecorationNonWritable:
  71. return "NonWritable";
  72. case SpvDecorationNonReadable:
  73. return "NonReadable";
  74. case SpvDecorationUniform:
  75. return "Uniform";
  76. case SpvDecorationSaturatedConversion:
  77. return "SaturatedConversion";
  78. case SpvDecorationStream:
  79. return "Stream";
  80. case SpvDecorationLocation:
  81. return "Location";
  82. case SpvDecorationComponent:
  83. return "Component";
  84. case SpvDecorationIndex:
  85. return "Index";
  86. case SpvDecorationBinding:
  87. return "Binding";
  88. case SpvDecorationDescriptorSet:
  89. return "DescriptorSet";
  90. case SpvDecorationOffset:
  91. return "Offset";
  92. case SpvDecorationXfbBuffer:
  93. return "XfbBuffer";
  94. case SpvDecorationXfbStride:
  95. return "XfbStride";
  96. case SpvDecorationFuncParamAttr:
  97. return "FuncParamAttr";
  98. case SpvDecorationFPRoundingMode:
  99. return "FPRoundingMode";
  100. case SpvDecorationFPFastMathMode:
  101. return "FPFastMathMode";
  102. case SpvDecorationLinkageAttributes:
  103. return "LinkageAttributes";
  104. case SpvDecorationNoContraction:
  105. return "NoContraction";
  106. case SpvDecorationInputAttachmentIndex:
  107. return "InputAttachmentIndex";
  108. case SpvDecorationAlignment:
  109. return "Alignment";
  110. case SpvDecorationMaxByteOffset:
  111. return "MaxByteOffset";
  112. case SpvDecorationAlignmentId:
  113. return "AlignmentId";
  114. case SpvDecorationMaxByteOffsetId:
  115. return "MaxByteOffsetId";
  116. case SpvDecorationNoSignedWrap:
  117. return "NoSignedWrap";
  118. case SpvDecorationNoUnsignedWrap:
  119. return "NoUnsignedWrap";
  120. case SpvDecorationExplicitInterpAMD:
  121. return "ExplicitInterpAMD";
  122. case SpvDecorationOverrideCoverageNV:
  123. return "OverrideCoverageNV";
  124. case SpvDecorationPassthroughNV:
  125. return "PassthroughNV";
  126. case SpvDecorationViewportRelativeNV:
  127. return "ViewportRelativeNV";
  128. case SpvDecorationSecondaryViewportRelativeNV:
  129. return "SecondaryViewportRelativeNV";
  130. case SpvDecorationPerPrimitiveNV:
  131. return "PerPrimitiveNV";
  132. case SpvDecorationPerViewNV:
  133. return "PerViewNV";
  134. case SpvDecorationPerTaskNV:
  135. return "PerTaskNV";
  136. case SpvDecorationPerVertexKHR:
  137. return "PerVertexKHR";
  138. case SpvDecorationNonUniform:
  139. return "NonUniform";
  140. case SpvDecorationRestrictPointer:
  141. return "RestrictPointer";
  142. case SpvDecorationAliasedPointer:
  143. return "AliasedPointer";
  144. case SpvDecorationCounterBuffer:
  145. return "CounterBuffer";
  146. case SpvDecorationHlslSemanticGOOGLE:
  147. return "HlslSemanticGOOGLE";
  148. default:
  149. break;
  150. }
  151. return "Unknown";
  152. }
  153. // Returns true if the decoration takes ID parameters.
  154. // TODO(dneto): This can be generated from the grammar.
  155. bool DecorationTakesIdParameters(SpvDecoration type) {
  156. switch (type) {
  157. case SpvDecorationUniformId:
  158. case SpvDecorationAlignmentId:
  159. case SpvDecorationMaxByteOffsetId:
  160. case SpvDecorationHlslCounterBufferGOOGLE:
  161. return true;
  162. default:
  163. break;
  164. }
  165. return false;
  166. }
  167. bool IsMemberDecorationOnly(SpvDecoration dec) {
  168. switch (dec) {
  169. case SpvDecorationRowMajor:
  170. case SpvDecorationColMajor:
  171. case SpvDecorationMatrixStride:
  172. // SPIR-V spec bug? Offset is generated on variables when dealing with
  173. // transform feedback.
  174. // case SpvDecorationOffset:
  175. return true;
  176. default:
  177. break;
  178. }
  179. return false;
  180. }
  181. bool IsNotMemberDecoration(SpvDecoration dec) {
  182. switch (dec) {
  183. case SpvDecorationSpecId:
  184. case SpvDecorationBlock:
  185. case SpvDecorationBufferBlock:
  186. case SpvDecorationArrayStride:
  187. case SpvDecorationGLSLShared:
  188. case SpvDecorationGLSLPacked:
  189. case SpvDecorationCPacked:
  190. // TODO: https://github.com/KhronosGroup/glslang/issues/703:
  191. // glslang applies Restrict to structure members.
  192. // case SpvDecorationRestrict:
  193. case SpvDecorationAliased:
  194. case SpvDecorationConstant:
  195. case SpvDecorationUniform:
  196. case SpvDecorationUniformId:
  197. case SpvDecorationSaturatedConversion:
  198. case SpvDecorationIndex:
  199. case SpvDecorationBinding:
  200. case SpvDecorationDescriptorSet:
  201. case SpvDecorationFuncParamAttr:
  202. case SpvDecorationFPRoundingMode:
  203. case SpvDecorationFPFastMathMode:
  204. case SpvDecorationLinkageAttributes:
  205. case SpvDecorationNoContraction:
  206. case SpvDecorationInputAttachmentIndex:
  207. case SpvDecorationAlignment:
  208. case SpvDecorationMaxByteOffset:
  209. case SpvDecorationAlignmentId:
  210. case SpvDecorationMaxByteOffsetId:
  211. case SpvDecorationNoSignedWrap:
  212. case SpvDecorationNoUnsignedWrap:
  213. case SpvDecorationNonUniform:
  214. case SpvDecorationRestrictPointer:
  215. case SpvDecorationAliasedPointer:
  216. case SpvDecorationCounterBuffer:
  217. return true;
  218. default:
  219. break;
  220. }
  221. return false;
  222. }
  223. spv_result_t ValidateDecorationTarget(ValidationState_t& _, SpvDecoration dec,
  224. const Instruction* inst,
  225. const Instruction* target) {
  226. auto fail = [&_, dec, inst, target](uint32_t vuid) -> DiagnosticStream {
  227. DiagnosticStream ds = std::move(
  228. _.diag(SPV_ERROR_INVALID_ID, inst)
  229. << _.VkErrorID(vuid) << LogStringForDecoration(dec)
  230. << " decoration on target <id> '" << _.getIdName(target->id()) << "' ");
  231. return ds;
  232. };
  233. switch (dec) {
  234. case SpvDecorationSpecId:
  235. if (!spvOpcodeIsScalarSpecConstant(target->opcode())) {
  236. return fail(0) << "must be a scalar specialization constant";
  237. }
  238. break;
  239. case SpvDecorationBlock:
  240. case SpvDecorationBufferBlock:
  241. case SpvDecorationGLSLShared:
  242. case SpvDecorationGLSLPacked:
  243. case SpvDecorationCPacked:
  244. if (target->opcode() != SpvOpTypeStruct) {
  245. return fail(0) << "must be a structure type";
  246. }
  247. break;
  248. case SpvDecorationArrayStride:
  249. if (target->opcode() != SpvOpTypeArray &&
  250. target->opcode() != SpvOpTypeRuntimeArray &&
  251. target->opcode() != SpvOpTypePointer) {
  252. return fail(0) << "must be an array or pointer type";
  253. }
  254. break;
  255. case SpvDecorationBuiltIn:
  256. if (target->opcode() != SpvOpVariable &&
  257. !spvOpcodeIsConstant(target->opcode())) {
  258. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  259. << "BuiltIns can only target variables, structure members or "
  260. "constants";
  261. }
  262. if (_.HasCapability(SpvCapabilityShader) &&
  263. inst->GetOperandAs<SpvBuiltIn>(2) == SpvBuiltInWorkgroupSize) {
  264. if (!spvOpcodeIsConstant(target->opcode())) {
  265. return fail(0) << "must be a constant for WorkgroupSize";
  266. }
  267. } else if (target->opcode() != SpvOpVariable) {
  268. return fail(0) << "must be a variable";
  269. }
  270. break;
  271. case SpvDecorationNoPerspective:
  272. case SpvDecorationFlat:
  273. case SpvDecorationPatch:
  274. case SpvDecorationCentroid:
  275. case SpvDecorationSample:
  276. case SpvDecorationRestrict:
  277. case SpvDecorationAliased:
  278. case SpvDecorationVolatile:
  279. case SpvDecorationCoherent:
  280. case SpvDecorationNonWritable:
  281. case SpvDecorationNonReadable:
  282. case SpvDecorationXfbBuffer:
  283. case SpvDecorationXfbStride:
  284. case SpvDecorationComponent:
  285. case SpvDecorationStream:
  286. case SpvDecorationRestrictPointer:
  287. case SpvDecorationAliasedPointer:
  288. if (target->opcode() != SpvOpVariable &&
  289. target->opcode() != SpvOpFunctionParameter) {
  290. return fail(0) << "must be a memory object declaration";
  291. }
  292. if (_.GetIdOpcode(target->type_id()) != SpvOpTypePointer) {
  293. return fail(0) << "must be a pointer type";
  294. }
  295. break;
  296. case SpvDecorationInvariant:
  297. case SpvDecorationConstant:
  298. case SpvDecorationLocation:
  299. case SpvDecorationIndex:
  300. case SpvDecorationBinding:
  301. case SpvDecorationDescriptorSet:
  302. case SpvDecorationInputAttachmentIndex:
  303. if (target->opcode() != SpvOpVariable) {
  304. return fail(0) << "must be a variable";
  305. }
  306. break;
  307. default:
  308. break;
  309. }
  310. if (spvIsVulkanEnv(_.context()->target_env)) {
  311. // The following were all checked as pointer types above.
  312. SpvStorageClass sc = SpvStorageClassUniform;
  313. const auto type = _.FindDef(target->type_id());
  314. if (type && type->operands().size() > 2) {
  315. sc = type->GetOperandAs<SpvStorageClass>(1);
  316. }
  317. switch (dec) {
  318. case SpvDecorationLocation:
  319. case SpvDecorationComponent:
  320. // Location is used for input, output and ray tracing stages.
  321. if (sc != SpvStorageClassInput && sc != SpvStorageClassOutput &&
  322. sc != SpvStorageClassRayPayloadKHR &&
  323. sc != SpvStorageClassIncomingRayPayloadKHR &&
  324. sc != SpvStorageClassHitAttributeKHR &&
  325. sc != SpvStorageClassCallableDataKHR &&
  326. sc != SpvStorageClassIncomingCallableDataKHR &&
  327. sc != SpvStorageClassShaderRecordBufferKHR) {
  328. return _.diag(SPV_ERROR_INVALID_ID, target)
  329. << _.VkErrorID(6672) << LogStringForDecoration(dec)
  330. << " decoration must not be applied to this storage class";
  331. }
  332. break;
  333. case SpvDecorationIndex:
  334. // Langauge from SPIR-V definition of Index
  335. if (sc != SpvStorageClassOutput) {
  336. return fail(0) << "must be in the Output storage class";
  337. }
  338. break;
  339. case SpvDecorationBinding:
  340. case SpvDecorationDescriptorSet:
  341. if (sc != SpvStorageClassStorageBuffer &&
  342. sc != SpvStorageClassUniform &&
  343. sc != SpvStorageClassUniformConstant) {
  344. return fail(6491) << "must be in the StorageBuffer, Uniform, or "
  345. "UniformConstant storage class";
  346. }
  347. break;
  348. case SpvDecorationInputAttachmentIndex:
  349. if (sc != SpvStorageClassUniformConstant) {
  350. return fail(6678) << "must be in the UniformConstant storage class";
  351. }
  352. break;
  353. case SpvDecorationFlat:
  354. case SpvDecorationNoPerspective:
  355. case SpvDecorationCentroid:
  356. case SpvDecorationSample:
  357. if (sc != SpvStorageClassInput && sc != SpvStorageClassOutput) {
  358. return fail(4670) << "storage class must be Input or Output";
  359. }
  360. break;
  361. case SpvDecorationPerVertexKHR:
  362. if (sc != SpvStorageClassInput) {
  363. return fail(6777) << "storage class must be Input";
  364. }
  365. break;
  366. default:
  367. break;
  368. }
  369. }
  370. return SPV_SUCCESS;
  371. }
  372. spv_result_t ValidateDecorate(ValidationState_t& _, const Instruction* inst) {
  373. const auto decoration = inst->GetOperandAs<SpvDecoration>(1);
  374. const auto target_id = inst->GetOperandAs<uint32_t>(0);
  375. const auto target = _.FindDef(target_id);
  376. if (!target) {
  377. return _.diag(SPV_ERROR_INVALID_ID, inst) << "target is not defined";
  378. }
  379. if (spvIsVulkanEnv(_.context()->target_env)) {
  380. if ((decoration == SpvDecorationGLSLShared) ||
  381. (decoration == SpvDecorationGLSLPacked)) {
  382. return _.diag(SPV_ERROR_INVALID_ID, inst)
  383. << _.VkErrorID(4669) << "OpDecorate decoration '"
  384. << LogStringForDecoration(decoration)
  385. << "' is not valid for the Vulkan execution environment.";
  386. }
  387. }
  388. if (DecorationTakesIdParameters(decoration)) {
  389. return _.diag(SPV_ERROR_INVALID_ID, inst)
  390. << "Decorations taking ID parameters may not be used with "
  391. "OpDecorateId";
  392. }
  393. if (target->opcode() != SpvOpDecorationGroup) {
  394. if (IsMemberDecorationOnly(decoration)) {
  395. return _.diag(SPV_ERROR_INVALID_ID, inst)
  396. << LogStringForDecoration(decoration)
  397. << " can only be applied to structure members";
  398. }
  399. if (auto error = ValidateDecorationTarget(_, decoration, inst, target)) {
  400. return error;
  401. }
  402. }
  403. // TODO: Add validations for all decorations.
  404. return SPV_SUCCESS;
  405. }
  406. spv_result_t ValidateDecorateId(ValidationState_t& _, const Instruction* inst) {
  407. const auto decoration = inst->GetOperandAs<SpvDecoration>(1);
  408. if (!DecorationTakesIdParameters(decoration)) {
  409. return _.diag(SPV_ERROR_INVALID_ID, inst)
  410. << "Decorations that don't take ID parameters may not be used with "
  411. "OpDecorateId";
  412. }
  413. // No member decorations take id parameters, so we don't bother checking if
  414. // we are using a member only decoration here.
  415. // TODO: Add validations for these decorations.
  416. // UniformId is covered elsewhere.
  417. return SPV_SUCCESS;
  418. }
  419. spv_result_t ValidateMemberDecorate(ValidationState_t& _,
  420. const Instruction* inst) {
  421. const auto struct_type_id = inst->GetOperandAs<uint32_t>(0);
  422. const auto struct_type = _.FindDef(struct_type_id);
  423. if (!struct_type || SpvOpTypeStruct != struct_type->opcode()) {
  424. return _.diag(SPV_ERROR_INVALID_ID, inst)
  425. << "OpMemberDecorate Structure type <id> '"
  426. << _.getIdName(struct_type_id) << "' is not a struct type.";
  427. }
  428. const auto member = inst->GetOperandAs<uint32_t>(1);
  429. const auto member_count =
  430. static_cast<uint32_t>(struct_type->words().size() - 2);
  431. if (member_count <= member) {
  432. return _.diag(SPV_ERROR_INVALID_ID, inst)
  433. << "Index " << member
  434. << " provided in OpMemberDecorate for struct <id> "
  435. << _.getIdName(struct_type_id)
  436. << " is out of bounds. The structure has " << member_count
  437. << " members. Largest valid index is " << member_count - 1 << ".";
  438. }
  439. const auto decoration = inst->GetOperandAs<SpvDecoration>(2);
  440. if (IsNotMemberDecoration(decoration)) {
  441. return _.diag(SPV_ERROR_INVALID_ID, inst)
  442. << LogStringForDecoration(decoration)
  443. << " cannot be applied to structure members";
  444. }
  445. return SPV_SUCCESS;
  446. }
  447. spv_result_t ValidateDecorationGroup(ValidationState_t& _,
  448. const Instruction* inst) {
  449. const auto decoration_group_id = inst->GetOperandAs<uint32_t>(0);
  450. const auto decoration_group = _.FindDef(decoration_group_id);
  451. for (auto pair : decoration_group->uses()) {
  452. auto use = pair.first;
  453. if (use->opcode() != SpvOpDecorate && use->opcode() != SpvOpGroupDecorate &&
  454. use->opcode() != SpvOpGroupMemberDecorate &&
  455. use->opcode() != SpvOpName && use->opcode() != SpvOpDecorateId &&
  456. !use->IsNonSemantic()) {
  457. return _.diag(SPV_ERROR_INVALID_ID, inst)
  458. << "Result id of OpDecorationGroup can only "
  459. << "be targeted by OpName, OpGroupDecorate, "
  460. << "OpDecorate, OpDecorateId, and OpGroupMemberDecorate";
  461. }
  462. }
  463. return SPV_SUCCESS;
  464. }
  465. spv_result_t ValidateGroupDecorate(ValidationState_t& _,
  466. const Instruction* inst) {
  467. const auto decoration_group_id = inst->GetOperandAs<uint32_t>(0);
  468. auto decoration_group = _.FindDef(decoration_group_id);
  469. if (!decoration_group || SpvOpDecorationGroup != decoration_group->opcode()) {
  470. return _.diag(SPV_ERROR_INVALID_ID, inst)
  471. << "OpGroupDecorate Decoration group <id> '"
  472. << _.getIdName(decoration_group_id)
  473. << "' is not a decoration group.";
  474. }
  475. for (unsigned i = 1; i < inst->operands().size(); ++i) {
  476. auto target_id = inst->GetOperandAs<uint32_t>(i);
  477. auto target = _.FindDef(target_id);
  478. if (!target || target->opcode() == SpvOpDecorationGroup) {
  479. return _.diag(SPV_ERROR_INVALID_ID, inst)
  480. << "OpGroupDecorate may not target OpDecorationGroup <id> '"
  481. << _.getIdName(target_id) << "'";
  482. }
  483. }
  484. return SPV_SUCCESS;
  485. }
  486. spv_result_t ValidateGroupMemberDecorate(ValidationState_t& _,
  487. const Instruction* inst) {
  488. const auto decoration_group_id = inst->GetOperandAs<uint32_t>(0);
  489. const auto decoration_group = _.FindDef(decoration_group_id);
  490. if (!decoration_group || SpvOpDecorationGroup != decoration_group->opcode()) {
  491. return _.diag(SPV_ERROR_INVALID_ID, inst)
  492. << "OpGroupMemberDecorate Decoration group <id> '"
  493. << _.getIdName(decoration_group_id)
  494. << "' is not a decoration group.";
  495. }
  496. // Grammar checks ensures that the number of arguments to this instruction
  497. // is an odd number: 1 decoration group + (id,literal) pairs.
  498. for (size_t i = 1; i + 1 < inst->operands().size(); i += 2) {
  499. const uint32_t struct_id = inst->GetOperandAs<uint32_t>(i);
  500. const uint32_t index = inst->GetOperandAs<uint32_t>(i + 1);
  501. auto struct_instr = _.FindDef(struct_id);
  502. if (!struct_instr || SpvOpTypeStruct != struct_instr->opcode()) {
  503. return _.diag(SPV_ERROR_INVALID_ID, inst)
  504. << "OpGroupMemberDecorate Structure type <id> '"
  505. << _.getIdName(struct_id) << "' is not a struct type.";
  506. }
  507. const uint32_t num_struct_members =
  508. static_cast<uint32_t>(struct_instr->words().size() - 2);
  509. if (index >= num_struct_members) {
  510. return _.diag(SPV_ERROR_INVALID_ID, inst)
  511. << "Index " << index
  512. << " provided in OpGroupMemberDecorate for struct <id> "
  513. << _.getIdName(struct_id)
  514. << " is out of bounds. The structure has " << num_struct_members
  515. << " members. Largest valid index is " << num_struct_members - 1
  516. << ".";
  517. }
  518. }
  519. return SPV_SUCCESS;
  520. }
  521. // Registers necessary decoration(s) for the appropriate IDs based on the
  522. // instruction.
  523. spv_result_t RegisterDecorations(ValidationState_t& _,
  524. const Instruction* inst) {
  525. switch (inst->opcode()) {
  526. case SpvOpDecorate:
  527. case SpvOpDecorateId: {
  528. const uint32_t target_id = inst->word(1);
  529. const SpvDecoration dec_type = static_cast<SpvDecoration>(inst->word(2));
  530. std::vector<uint32_t> dec_params;
  531. if (inst->words().size() > 3) {
  532. dec_params.insert(dec_params.end(), inst->words().begin() + 3,
  533. inst->words().end());
  534. }
  535. _.RegisterDecorationForId(target_id, Decoration(dec_type, dec_params));
  536. break;
  537. }
  538. case SpvOpMemberDecorate: {
  539. const uint32_t struct_id = inst->word(1);
  540. const uint32_t index = inst->word(2);
  541. const SpvDecoration dec_type = static_cast<SpvDecoration>(inst->word(3));
  542. std::vector<uint32_t> dec_params;
  543. if (inst->words().size() > 4) {
  544. dec_params.insert(dec_params.end(), inst->words().begin() + 4,
  545. inst->words().end());
  546. }
  547. _.RegisterDecorationForId(struct_id,
  548. Decoration(dec_type, dec_params, index));
  549. break;
  550. }
  551. case SpvOpDecorationGroup: {
  552. // We don't need to do anything right now. Assigning decorations to groups
  553. // will be taken care of via OpGroupDecorate.
  554. break;
  555. }
  556. case SpvOpGroupDecorate: {
  557. // Word 1 is the group <id>. All subsequent words are target <id>s that
  558. // are going to be decorated with the decorations.
  559. const uint32_t decoration_group_id = inst->word(1);
  560. std::set<Decoration>& group_decorations =
  561. _.id_decorations(decoration_group_id);
  562. for (size_t i = 2; i < inst->words().size(); ++i) {
  563. const uint32_t target_id = inst->word(i);
  564. _.RegisterDecorationsForId(target_id, group_decorations.begin(),
  565. group_decorations.end());
  566. }
  567. break;
  568. }
  569. case SpvOpGroupMemberDecorate: {
  570. // Word 1 is the Decoration Group <id> followed by (struct<id>,literal)
  571. // pairs. All decorations of the group should be applied to all the struct
  572. // members that are specified in the instructions.
  573. const uint32_t decoration_group_id = inst->word(1);
  574. std::set<Decoration>& group_decorations =
  575. _.id_decorations(decoration_group_id);
  576. // Grammar checks ensures that the number of arguments to this instruction
  577. // is an odd number: 1 decoration group + (id,literal) pairs.
  578. for (size_t i = 2; i + 1 < inst->words().size(); i = i + 2) {
  579. const uint32_t struct_id = inst->word(i);
  580. const uint32_t index = inst->word(i + 1);
  581. // ID validation phase ensures this is in fact a struct instruction and
  582. // that the index is not out of bound.
  583. _.RegisterDecorationsForStructMember(struct_id, index,
  584. group_decorations.begin(),
  585. group_decorations.end());
  586. }
  587. break;
  588. }
  589. default:
  590. break;
  591. }
  592. return SPV_SUCCESS;
  593. }
  594. } // namespace
  595. spv_result_t AnnotationPass(ValidationState_t& _, const Instruction* inst) {
  596. switch (inst->opcode()) {
  597. case SpvOpDecorate:
  598. if (auto error = ValidateDecorate(_, inst)) return error;
  599. break;
  600. case SpvOpDecorateId:
  601. if (auto error = ValidateDecorateId(_, inst)) return error;
  602. break;
  603. // TODO(dneto): SpvOpDecorateStringGOOGLE
  604. // See https://github.com/KhronosGroup/SPIRV-Tools/issues/2253
  605. case SpvOpMemberDecorate:
  606. if (auto error = ValidateMemberDecorate(_, inst)) return error;
  607. break;
  608. case SpvOpDecorationGroup:
  609. if (auto error = ValidateDecorationGroup(_, inst)) return error;
  610. break;
  611. case SpvOpGroupDecorate:
  612. if (auto error = ValidateGroupDecorate(_, inst)) return error;
  613. break;
  614. case SpvOpGroupMemberDecorate:
  615. if (auto error = ValidateGroupMemberDecorate(_, inst)) return error;
  616. break;
  617. default:
  618. break;
  619. }
  620. // In order to validate decoration rules, we need to know all the decorations
  621. // that are applied to any given <id>.
  622. RegisterDecorations(_, inst);
  623. return SPV_SUCCESS;
  624. }
  625. } // namespace val
  626. } // namespace spvtools