validate_annotation.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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 SpvDecorationPerVertexNV:
  137. return "PerVertexNV";
  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 = 0) -> 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() << "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() << "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() << "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 (inst->GetOperandAs<SpvBuiltIn>(2) == SpvBuiltInWorkgroupSize) {
  263. if (!spvOpcodeIsConstant(target->opcode())) {
  264. return fail() << "must be a constant for WorkgroupSize";
  265. }
  266. } else if (target->opcode() != SpvOpVariable) {
  267. return fail() << "must be a variable";
  268. }
  269. break;
  270. case SpvDecorationNoPerspective:
  271. case SpvDecorationFlat:
  272. case SpvDecorationPatch:
  273. case SpvDecorationCentroid:
  274. case SpvDecorationSample:
  275. case SpvDecorationRestrict:
  276. case SpvDecorationAliased:
  277. case SpvDecorationVolatile:
  278. case SpvDecorationCoherent:
  279. case SpvDecorationNonWritable:
  280. case SpvDecorationNonReadable:
  281. case SpvDecorationXfbBuffer:
  282. case SpvDecorationXfbStride:
  283. case SpvDecorationComponent:
  284. case SpvDecorationStream:
  285. case SpvDecorationRestrictPointer:
  286. case SpvDecorationAliasedPointer:
  287. if (target->opcode() != SpvOpVariable &&
  288. target->opcode() != SpvOpFunctionParameter) {
  289. return fail() << "must be a memory object declaration";
  290. }
  291. if (_.GetIdOpcode(target->type_id()) != SpvOpTypePointer) {
  292. return fail() << "must be a pointer type";
  293. }
  294. break;
  295. case SpvDecorationInvariant:
  296. case SpvDecorationConstant:
  297. case SpvDecorationLocation:
  298. case SpvDecorationIndex:
  299. case SpvDecorationBinding:
  300. case SpvDecorationDescriptorSet:
  301. case SpvDecorationInputAttachmentIndex:
  302. if (target->opcode() != SpvOpVariable) {
  303. return fail() << "must be a variable";
  304. }
  305. break;
  306. default:
  307. break;
  308. }
  309. if (spvIsVulkanEnv(_.context()->target_env)) {
  310. // The following were all checked as pointer types above.
  311. SpvStorageClass sc = SpvStorageClassUniform;
  312. const auto type = _.FindDef(target->type_id());
  313. if (type && type->operands().size() > 2) {
  314. sc = type->GetOperandAs<SpvStorageClass>(1);
  315. }
  316. switch (dec) {
  317. case SpvDecorationLocation:
  318. case SpvDecorationComponent:
  319. // Location is used for input, output and ray tracing stages.
  320. if (sc == SpvStorageClassStorageBuffer ||
  321. sc == SpvStorageClassUniform ||
  322. sc == SpvStorageClassUniformConstant ||
  323. sc == SpvStorageClassWorkgroup || sc == SpvStorageClassPrivate ||
  324. sc == SpvStorageClassFunction) {
  325. return _.diag(SPV_ERROR_INVALID_ID, target)
  326. << LogStringForDecoration(dec)
  327. << " decoration must not be applied to this storage class";
  328. }
  329. break;
  330. case SpvDecorationIndex:
  331. if (sc != SpvStorageClassOutput) {
  332. return fail() << "must be in the Output storage class";
  333. }
  334. break;
  335. case SpvDecorationBinding:
  336. case SpvDecorationDescriptorSet:
  337. if (sc != SpvStorageClassStorageBuffer &&
  338. sc != SpvStorageClassUniform &&
  339. sc != SpvStorageClassUniformConstant) {
  340. return fail() << "must be in the StorageBuffer, Uniform, or "
  341. "UniformConstant storage class";
  342. }
  343. break;
  344. case SpvDecorationInputAttachmentIndex:
  345. if (sc != SpvStorageClassUniformConstant) {
  346. return fail() << "must be in the UniformConstant storage class";
  347. }
  348. break;
  349. case SpvDecorationFlat:
  350. case SpvDecorationNoPerspective:
  351. case SpvDecorationCentroid:
  352. case SpvDecorationSample:
  353. if (sc != SpvStorageClassInput && sc != SpvStorageClassOutput) {
  354. return fail(4670) << "storage class must be Input or Output";
  355. }
  356. break;
  357. default:
  358. break;
  359. }
  360. }
  361. return SPV_SUCCESS;
  362. }
  363. spv_result_t ValidateDecorate(ValidationState_t& _, const Instruction* inst) {
  364. const auto decoration = inst->GetOperandAs<SpvDecoration>(1);
  365. const auto target_id = inst->GetOperandAs<uint32_t>(0);
  366. const auto target = _.FindDef(target_id);
  367. if (!target) {
  368. return _.diag(SPV_ERROR_INVALID_ID, inst) << "target is not defined";
  369. }
  370. if (spvIsVulkanEnv(_.context()->target_env)) {
  371. if ((decoration == SpvDecorationGLSLShared) ||
  372. (decoration == SpvDecorationGLSLPacked)) {
  373. return _.diag(SPV_ERROR_INVALID_ID, inst)
  374. << _.VkErrorID(4669) << "OpDecorate decoration '"
  375. << LogStringForDecoration(decoration)
  376. << "' is not valid for the Vulkan execution environment.";
  377. }
  378. }
  379. if (DecorationTakesIdParameters(decoration)) {
  380. return _.diag(SPV_ERROR_INVALID_ID, inst)
  381. << "Decorations taking ID parameters may not be used with "
  382. "OpDecorateId";
  383. }
  384. if (target->opcode() != SpvOpDecorationGroup) {
  385. if (IsMemberDecorationOnly(decoration)) {
  386. return _.diag(SPV_ERROR_INVALID_ID, inst)
  387. << LogStringForDecoration(decoration)
  388. << " can only be applied to structure members";
  389. }
  390. if (auto error = ValidateDecorationTarget(_, decoration, inst, target)) {
  391. return error;
  392. }
  393. }
  394. // TODO: Add validations for all decorations.
  395. return SPV_SUCCESS;
  396. }
  397. spv_result_t ValidateDecorateId(ValidationState_t& _, const Instruction* inst) {
  398. const auto decoration = inst->GetOperandAs<SpvDecoration>(1);
  399. if (!DecorationTakesIdParameters(decoration)) {
  400. return _.diag(SPV_ERROR_INVALID_ID, inst)
  401. << "Decorations that don't take ID parameters may not be used with "
  402. "OpDecorateId";
  403. }
  404. // No member decorations take id parameters, so we don't bother checking if
  405. // we are using a member only decoration here.
  406. // TODO: Add validations for these decorations.
  407. // UniformId is covered elsewhere.
  408. return SPV_SUCCESS;
  409. }
  410. spv_result_t ValidateMemberDecorate(ValidationState_t& _,
  411. const Instruction* inst) {
  412. const auto struct_type_id = inst->GetOperandAs<uint32_t>(0);
  413. const auto struct_type = _.FindDef(struct_type_id);
  414. if (!struct_type || SpvOpTypeStruct != struct_type->opcode()) {
  415. return _.diag(SPV_ERROR_INVALID_ID, inst)
  416. << "OpMemberDecorate Structure type <id> '"
  417. << _.getIdName(struct_type_id) << "' is not a struct type.";
  418. }
  419. const auto member = inst->GetOperandAs<uint32_t>(1);
  420. const auto member_count =
  421. static_cast<uint32_t>(struct_type->words().size() - 2);
  422. if (member_count <= member) {
  423. return _.diag(SPV_ERROR_INVALID_ID, inst)
  424. << "Index " << member
  425. << " provided in OpMemberDecorate for struct <id> "
  426. << _.getIdName(struct_type_id)
  427. << " is out of bounds. The structure has " << member_count
  428. << " members. Largest valid index is " << member_count - 1 << ".";
  429. }
  430. const auto decoration = inst->GetOperandAs<SpvDecoration>(2);
  431. if (IsNotMemberDecoration(decoration)) {
  432. return _.diag(SPV_ERROR_INVALID_ID, inst)
  433. << LogStringForDecoration(decoration)
  434. << " cannot be applied to structure members";
  435. }
  436. return SPV_SUCCESS;
  437. }
  438. spv_result_t ValidateDecorationGroup(ValidationState_t& _,
  439. const Instruction* inst) {
  440. const auto decoration_group_id = inst->GetOperandAs<uint32_t>(0);
  441. const auto decoration_group = _.FindDef(decoration_group_id);
  442. for (auto pair : decoration_group->uses()) {
  443. auto use = pair.first;
  444. if (use->opcode() != SpvOpDecorate && use->opcode() != SpvOpGroupDecorate &&
  445. use->opcode() != SpvOpGroupMemberDecorate &&
  446. use->opcode() != SpvOpName && use->opcode() != SpvOpDecorateId &&
  447. !use->IsNonSemantic()) {
  448. return _.diag(SPV_ERROR_INVALID_ID, inst)
  449. << "Result id of OpDecorationGroup can only "
  450. << "be targeted by OpName, OpGroupDecorate, "
  451. << "OpDecorate, OpDecorateId, and OpGroupMemberDecorate";
  452. }
  453. }
  454. return SPV_SUCCESS;
  455. }
  456. spv_result_t ValidateGroupDecorate(ValidationState_t& _,
  457. const Instruction* inst) {
  458. const auto decoration_group_id = inst->GetOperandAs<uint32_t>(0);
  459. auto decoration_group = _.FindDef(decoration_group_id);
  460. if (!decoration_group || SpvOpDecorationGroup != decoration_group->opcode()) {
  461. return _.diag(SPV_ERROR_INVALID_ID, inst)
  462. << "OpGroupDecorate Decoration group <id> '"
  463. << _.getIdName(decoration_group_id)
  464. << "' is not a decoration group.";
  465. }
  466. for (unsigned i = 1; i < inst->operands().size(); ++i) {
  467. auto target_id = inst->GetOperandAs<uint32_t>(i);
  468. auto target = _.FindDef(target_id);
  469. if (!target || target->opcode() == SpvOpDecorationGroup) {
  470. return _.diag(SPV_ERROR_INVALID_ID, inst)
  471. << "OpGroupDecorate may not target OpDecorationGroup <id> '"
  472. << _.getIdName(target_id) << "'";
  473. }
  474. }
  475. return SPV_SUCCESS;
  476. }
  477. spv_result_t ValidateGroupMemberDecorate(ValidationState_t& _,
  478. const Instruction* inst) {
  479. const auto decoration_group_id = inst->GetOperandAs<uint32_t>(0);
  480. const auto decoration_group = _.FindDef(decoration_group_id);
  481. if (!decoration_group || SpvOpDecorationGroup != decoration_group->opcode()) {
  482. return _.diag(SPV_ERROR_INVALID_ID, inst)
  483. << "OpGroupMemberDecorate Decoration group <id> '"
  484. << _.getIdName(decoration_group_id)
  485. << "' is not a decoration group.";
  486. }
  487. // Grammar checks ensures that the number of arguments to this instruction
  488. // is an odd number: 1 decoration group + (id,literal) pairs.
  489. for (size_t i = 1; i + 1 < inst->operands().size(); i += 2) {
  490. const uint32_t struct_id = inst->GetOperandAs<uint32_t>(i);
  491. const uint32_t index = inst->GetOperandAs<uint32_t>(i + 1);
  492. auto struct_instr = _.FindDef(struct_id);
  493. if (!struct_instr || SpvOpTypeStruct != struct_instr->opcode()) {
  494. return _.diag(SPV_ERROR_INVALID_ID, inst)
  495. << "OpGroupMemberDecorate Structure type <id> '"
  496. << _.getIdName(struct_id) << "' is not a struct type.";
  497. }
  498. const uint32_t num_struct_members =
  499. static_cast<uint32_t>(struct_instr->words().size() - 2);
  500. if (index >= num_struct_members) {
  501. return _.diag(SPV_ERROR_INVALID_ID, inst)
  502. << "Index " << index
  503. << " provided in OpGroupMemberDecorate for struct <id> "
  504. << _.getIdName(struct_id)
  505. << " is out of bounds. The structure has " << num_struct_members
  506. << " members. Largest valid index is " << num_struct_members - 1
  507. << ".";
  508. }
  509. }
  510. return SPV_SUCCESS;
  511. }
  512. // Registers necessary decoration(s) for the appropriate IDs based on the
  513. // instruction.
  514. spv_result_t RegisterDecorations(ValidationState_t& _,
  515. const Instruction* inst) {
  516. switch (inst->opcode()) {
  517. case SpvOpDecorate:
  518. case SpvOpDecorateId: {
  519. const uint32_t target_id = inst->word(1);
  520. const SpvDecoration dec_type = static_cast<SpvDecoration>(inst->word(2));
  521. std::vector<uint32_t> dec_params;
  522. if (inst->words().size() > 3) {
  523. dec_params.insert(dec_params.end(), inst->words().begin() + 3,
  524. inst->words().end());
  525. }
  526. _.RegisterDecorationForId(target_id, Decoration(dec_type, dec_params));
  527. break;
  528. }
  529. case SpvOpMemberDecorate: {
  530. const uint32_t struct_id = inst->word(1);
  531. const uint32_t index = inst->word(2);
  532. const SpvDecoration dec_type = static_cast<SpvDecoration>(inst->word(3));
  533. std::vector<uint32_t> dec_params;
  534. if (inst->words().size() > 4) {
  535. dec_params.insert(dec_params.end(), inst->words().begin() + 4,
  536. inst->words().end());
  537. }
  538. _.RegisterDecorationForId(struct_id,
  539. Decoration(dec_type, dec_params, index));
  540. break;
  541. }
  542. case SpvOpDecorationGroup: {
  543. // We don't need to do anything right now. Assigning decorations to groups
  544. // will be taken care of via OpGroupDecorate.
  545. break;
  546. }
  547. case SpvOpGroupDecorate: {
  548. // Word 1 is the group <id>. All subsequent words are target <id>s that
  549. // are going to be decorated with the decorations.
  550. const uint32_t decoration_group_id = inst->word(1);
  551. std::vector<Decoration>& group_decorations =
  552. _.id_decorations(decoration_group_id);
  553. for (size_t i = 2; i < inst->words().size(); ++i) {
  554. const uint32_t target_id = inst->word(i);
  555. _.RegisterDecorationsForId(target_id, group_decorations.begin(),
  556. group_decorations.end());
  557. }
  558. break;
  559. }
  560. case SpvOpGroupMemberDecorate: {
  561. // Word 1 is the Decoration Group <id> followed by (struct<id>,literal)
  562. // pairs. All decorations of the group should be applied to all the struct
  563. // members that are specified in the instructions.
  564. const uint32_t decoration_group_id = inst->word(1);
  565. std::vector<Decoration>& group_decorations =
  566. _.id_decorations(decoration_group_id);
  567. // Grammar checks ensures that the number of arguments to this instruction
  568. // is an odd number: 1 decoration group + (id,literal) pairs.
  569. for (size_t i = 2; i + 1 < inst->words().size(); i = i + 2) {
  570. const uint32_t struct_id = inst->word(i);
  571. const uint32_t index = inst->word(i + 1);
  572. // ID validation phase ensures this is in fact a struct instruction and
  573. // that the index is not out of bound.
  574. _.RegisterDecorationsForStructMember(struct_id, index,
  575. group_decorations.begin(),
  576. group_decorations.end());
  577. }
  578. break;
  579. }
  580. default:
  581. break;
  582. }
  583. return SPV_SUCCESS;
  584. }
  585. } // namespace
  586. spv_result_t AnnotationPass(ValidationState_t& _, const Instruction* inst) {
  587. switch (inst->opcode()) {
  588. case SpvOpDecorate:
  589. if (auto error = ValidateDecorate(_, inst)) return error;
  590. break;
  591. case SpvOpDecorateId:
  592. if (auto error = ValidateDecorateId(_, inst)) return error;
  593. break;
  594. // TODO(dneto): SpvOpDecorateStringGOOGLE
  595. // See https://github.com/KhronosGroup/SPIRV-Tools/issues/2253
  596. case SpvOpMemberDecorate:
  597. if (auto error = ValidateMemberDecorate(_, inst)) return error;
  598. break;
  599. case SpvOpDecorationGroup:
  600. if (auto error = ValidateDecorationGroup(_, inst)) return error;
  601. break;
  602. case SpvOpGroupDecorate:
  603. if (auto error = ValidateGroupDecorate(_, inst)) return error;
  604. break;
  605. case SpvOpGroupMemberDecorate:
  606. if (auto error = ValidateGroupMemberDecorate(_, inst)) return error;
  607. break;
  608. default:
  609. break;
  610. }
  611. // In order to validate decoration rules, we need to know all the decorations
  612. // that are applied to any given <id>.
  613. RegisterDecorations(_, inst);
  614. return SPV_SUCCESS;
  615. }
  616. } // namespace val
  617. } // namespace spvtools