validate_annotation.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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) -> 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. << 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(0) << "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. default:
  362. break;
  363. }
  364. }
  365. return SPV_SUCCESS;
  366. }
  367. spv_result_t ValidateDecorate(ValidationState_t& _, const Instruction* inst) {
  368. const auto decoration = inst->GetOperandAs<SpvDecoration>(1);
  369. const auto target_id = inst->GetOperandAs<uint32_t>(0);
  370. const auto target = _.FindDef(target_id);
  371. if (!target) {
  372. return _.diag(SPV_ERROR_INVALID_ID, inst) << "target is not defined";
  373. }
  374. if (spvIsVulkanEnv(_.context()->target_env)) {
  375. if ((decoration == SpvDecorationGLSLShared) ||
  376. (decoration == SpvDecorationGLSLPacked)) {
  377. return _.diag(SPV_ERROR_INVALID_ID, inst)
  378. << _.VkErrorID(4669) << "OpDecorate decoration '"
  379. << LogStringForDecoration(decoration)
  380. << "' is not valid for the Vulkan execution environment.";
  381. }
  382. }
  383. if (DecorationTakesIdParameters(decoration)) {
  384. return _.diag(SPV_ERROR_INVALID_ID, inst)
  385. << "Decorations taking ID parameters may not be used with "
  386. "OpDecorateId";
  387. }
  388. if (target->opcode() != SpvOpDecorationGroup) {
  389. if (IsMemberDecorationOnly(decoration)) {
  390. return _.diag(SPV_ERROR_INVALID_ID, inst)
  391. << LogStringForDecoration(decoration)
  392. << " can only be applied to structure members";
  393. }
  394. if (auto error = ValidateDecorationTarget(_, decoration, inst, target)) {
  395. return error;
  396. }
  397. }
  398. // TODO: Add validations for all decorations.
  399. return SPV_SUCCESS;
  400. }
  401. spv_result_t ValidateDecorateId(ValidationState_t& _, const Instruction* inst) {
  402. const auto decoration = inst->GetOperandAs<SpvDecoration>(1);
  403. if (!DecorationTakesIdParameters(decoration)) {
  404. return _.diag(SPV_ERROR_INVALID_ID, inst)
  405. << "Decorations that don't take ID parameters may not be used with "
  406. "OpDecorateId";
  407. }
  408. // No member decorations take id parameters, so we don't bother checking if
  409. // we are using a member only decoration here.
  410. // TODO: Add validations for these decorations.
  411. // UniformId is covered elsewhere.
  412. return SPV_SUCCESS;
  413. }
  414. spv_result_t ValidateMemberDecorate(ValidationState_t& _,
  415. const Instruction* inst) {
  416. const auto struct_type_id = inst->GetOperandAs<uint32_t>(0);
  417. const auto struct_type = _.FindDef(struct_type_id);
  418. if (!struct_type || SpvOpTypeStruct != struct_type->opcode()) {
  419. return _.diag(SPV_ERROR_INVALID_ID, inst)
  420. << "OpMemberDecorate Structure type <id> '"
  421. << _.getIdName(struct_type_id) << "' is not a struct type.";
  422. }
  423. const auto member = inst->GetOperandAs<uint32_t>(1);
  424. const auto member_count =
  425. static_cast<uint32_t>(struct_type->words().size() - 2);
  426. if (member_count <= member) {
  427. return _.diag(SPV_ERROR_INVALID_ID, inst)
  428. << "Index " << member
  429. << " provided in OpMemberDecorate for struct <id> "
  430. << _.getIdName(struct_type_id)
  431. << " is out of bounds. The structure has " << member_count
  432. << " members. Largest valid index is " << member_count - 1 << ".";
  433. }
  434. const auto decoration = inst->GetOperandAs<SpvDecoration>(2);
  435. if (IsNotMemberDecoration(decoration)) {
  436. return _.diag(SPV_ERROR_INVALID_ID, inst)
  437. << LogStringForDecoration(decoration)
  438. << " cannot be applied to structure members";
  439. }
  440. return SPV_SUCCESS;
  441. }
  442. spv_result_t ValidateDecorationGroup(ValidationState_t& _,
  443. const Instruction* inst) {
  444. const auto decoration_group_id = inst->GetOperandAs<uint32_t>(0);
  445. const auto decoration_group = _.FindDef(decoration_group_id);
  446. for (auto pair : decoration_group->uses()) {
  447. auto use = pair.first;
  448. if (use->opcode() != SpvOpDecorate && use->opcode() != SpvOpGroupDecorate &&
  449. use->opcode() != SpvOpGroupMemberDecorate &&
  450. use->opcode() != SpvOpName && use->opcode() != SpvOpDecorateId &&
  451. !use->IsNonSemantic()) {
  452. return _.diag(SPV_ERROR_INVALID_ID, inst)
  453. << "Result id of OpDecorationGroup can only "
  454. << "be targeted by OpName, OpGroupDecorate, "
  455. << "OpDecorate, OpDecorateId, and OpGroupMemberDecorate";
  456. }
  457. }
  458. return SPV_SUCCESS;
  459. }
  460. spv_result_t ValidateGroupDecorate(ValidationState_t& _,
  461. const Instruction* inst) {
  462. const auto decoration_group_id = inst->GetOperandAs<uint32_t>(0);
  463. auto decoration_group = _.FindDef(decoration_group_id);
  464. if (!decoration_group || SpvOpDecorationGroup != decoration_group->opcode()) {
  465. return _.diag(SPV_ERROR_INVALID_ID, inst)
  466. << "OpGroupDecorate Decoration group <id> '"
  467. << _.getIdName(decoration_group_id)
  468. << "' is not a decoration group.";
  469. }
  470. for (unsigned i = 1; i < inst->operands().size(); ++i) {
  471. auto target_id = inst->GetOperandAs<uint32_t>(i);
  472. auto target = _.FindDef(target_id);
  473. if (!target || target->opcode() == SpvOpDecorationGroup) {
  474. return _.diag(SPV_ERROR_INVALID_ID, inst)
  475. << "OpGroupDecorate may not target OpDecorationGroup <id> '"
  476. << _.getIdName(target_id) << "'";
  477. }
  478. }
  479. return SPV_SUCCESS;
  480. }
  481. spv_result_t ValidateGroupMemberDecorate(ValidationState_t& _,
  482. const Instruction* inst) {
  483. const auto decoration_group_id = inst->GetOperandAs<uint32_t>(0);
  484. const auto decoration_group = _.FindDef(decoration_group_id);
  485. if (!decoration_group || SpvOpDecorationGroup != decoration_group->opcode()) {
  486. return _.diag(SPV_ERROR_INVALID_ID, inst)
  487. << "OpGroupMemberDecorate Decoration group <id> '"
  488. << _.getIdName(decoration_group_id)
  489. << "' is not a decoration group.";
  490. }
  491. // Grammar checks ensures that the number of arguments to this instruction
  492. // is an odd number: 1 decoration group + (id,literal) pairs.
  493. for (size_t i = 1; i + 1 < inst->operands().size(); i += 2) {
  494. const uint32_t struct_id = inst->GetOperandAs<uint32_t>(i);
  495. const uint32_t index = inst->GetOperandAs<uint32_t>(i + 1);
  496. auto struct_instr = _.FindDef(struct_id);
  497. if (!struct_instr || SpvOpTypeStruct != struct_instr->opcode()) {
  498. return _.diag(SPV_ERROR_INVALID_ID, inst)
  499. << "OpGroupMemberDecorate Structure type <id> '"
  500. << _.getIdName(struct_id) << "' is not a struct type.";
  501. }
  502. const uint32_t num_struct_members =
  503. static_cast<uint32_t>(struct_instr->words().size() - 2);
  504. if (index >= num_struct_members) {
  505. return _.diag(SPV_ERROR_INVALID_ID, inst)
  506. << "Index " << index
  507. << " provided in OpGroupMemberDecorate for struct <id> "
  508. << _.getIdName(struct_id)
  509. << " is out of bounds. The structure has " << num_struct_members
  510. << " members. Largest valid index is " << num_struct_members - 1
  511. << ".";
  512. }
  513. }
  514. return SPV_SUCCESS;
  515. }
  516. // Registers necessary decoration(s) for the appropriate IDs based on the
  517. // instruction.
  518. spv_result_t RegisterDecorations(ValidationState_t& _,
  519. const Instruction* inst) {
  520. switch (inst->opcode()) {
  521. case SpvOpDecorate:
  522. case SpvOpDecorateId: {
  523. const uint32_t target_id = inst->word(1);
  524. const SpvDecoration dec_type = static_cast<SpvDecoration>(inst->word(2));
  525. std::vector<uint32_t> dec_params;
  526. if (inst->words().size() > 3) {
  527. dec_params.insert(dec_params.end(), inst->words().begin() + 3,
  528. inst->words().end());
  529. }
  530. _.RegisterDecorationForId(target_id, Decoration(dec_type, dec_params));
  531. break;
  532. }
  533. case SpvOpMemberDecorate: {
  534. const uint32_t struct_id = inst->word(1);
  535. const uint32_t index = inst->word(2);
  536. const SpvDecoration dec_type = static_cast<SpvDecoration>(inst->word(3));
  537. std::vector<uint32_t> dec_params;
  538. if (inst->words().size() > 4) {
  539. dec_params.insert(dec_params.end(), inst->words().begin() + 4,
  540. inst->words().end());
  541. }
  542. _.RegisterDecorationForId(struct_id,
  543. Decoration(dec_type, dec_params, index));
  544. break;
  545. }
  546. case SpvOpDecorationGroup: {
  547. // We don't need to do anything right now. Assigning decorations to groups
  548. // will be taken care of via OpGroupDecorate.
  549. break;
  550. }
  551. case SpvOpGroupDecorate: {
  552. // Word 1 is the group <id>. All subsequent words are target <id>s that
  553. // are going to be decorated with the decorations.
  554. const uint32_t decoration_group_id = inst->word(1);
  555. std::vector<Decoration>& group_decorations =
  556. _.id_decorations(decoration_group_id);
  557. for (size_t i = 2; i < inst->words().size(); ++i) {
  558. const uint32_t target_id = inst->word(i);
  559. _.RegisterDecorationsForId(target_id, group_decorations.begin(),
  560. group_decorations.end());
  561. }
  562. break;
  563. }
  564. case SpvOpGroupMemberDecorate: {
  565. // Word 1 is the Decoration Group <id> followed by (struct<id>,literal)
  566. // pairs. All decorations of the group should be applied to all the struct
  567. // members that are specified in the instructions.
  568. const uint32_t decoration_group_id = inst->word(1);
  569. std::vector<Decoration>& group_decorations =
  570. _.id_decorations(decoration_group_id);
  571. // Grammar checks ensures that the number of arguments to this instruction
  572. // is an odd number: 1 decoration group + (id,literal) pairs.
  573. for (size_t i = 2; i + 1 < inst->words().size(); i = i + 2) {
  574. const uint32_t struct_id = inst->word(i);
  575. const uint32_t index = inst->word(i + 1);
  576. // ID validation phase ensures this is in fact a struct instruction and
  577. // that the index is not out of bound.
  578. _.RegisterDecorationsForStructMember(struct_id, index,
  579. group_decorations.begin(),
  580. group_decorations.end());
  581. }
  582. break;
  583. }
  584. default:
  585. break;
  586. }
  587. return SPV_SUCCESS;
  588. }
  589. } // namespace
  590. spv_result_t AnnotationPass(ValidationState_t& _, const Instruction* inst) {
  591. switch (inst->opcode()) {
  592. case SpvOpDecorate:
  593. if (auto error = ValidateDecorate(_, inst)) return error;
  594. break;
  595. case SpvOpDecorateId:
  596. if (auto error = ValidateDecorateId(_, inst)) return error;
  597. break;
  598. // TODO(dneto): SpvOpDecorateStringGOOGLE
  599. // See https://github.com/KhronosGroup/SPIRV-Tools/issues/2253
  600. case SpvOpMemberDecorate:
  601. if (auto error = ValidateMemberDecorate(_, inst)) return error;
  602. break;
  603. case SpvOpDecorationGroup:
  604. if (auto error = ValidateDecorationGroup(_, inst)) return error;
  605. break;
  606. case SpvOpGroupDecorate:
  607. if (auto error = ValidateGroupDecorate(_, inst)) return error;
  608. break;
  609. case SpvOpGroupMemberDecorate:
  610. if (auto error = ValidateGroupMemberDecorate(_, inst)) return error;
  611. break;
  612. default:
  613. break;
  614. }
  615. // In order to validate decoration rules, we need to know all the decorations
  616. // that are applied to any given <id>.
  617. RegisterDecorations(_, inst);
  618. return SPV_SUCCESS;
  619. }
  620. } // namespace val
  621. } // namespace spvtools