validate_decorations.cpp 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  1. // Copyright (c) 2017 Google 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. #include <algorithm>
  15. #include <cassert>
  16. #include <string>
  17. #include <tuple>
  18. #include <unordered_map>
  19. #include <unordered_set>
  20. #include <utility>
  21. #include <vector>
  22. #include "source/diagnostic.h"
  23. #include "source/opcode.h"
  24. #include "source/spirv_constant.h"
  25. #include "source/spirv_target_env.h"
  26. #include "source/spirv_validator_options.h"
  27. #include "source/val/validate_scopes.h"
  28. #include "source/val/validation_state.h"
  29. namespace spvtools {
  30. namespace val {
  31. namespace {
  32. // Distinguish between row and column major matrix layouts.
  33. enum MatrixLayout { kRowMajor, kColumnMajor };
  34. // A functor for hashing a pair of integers.
  35. struct PairHash {
  36. std::size_t operator()(const std::pair<uint32_t, uint32_t> pair) const {
  37. const uint32_t a = pair.first;
  38. const uint32_t b = pair.second;
  39. const uint32_t rotated_b = (b >> 2) | ((b & 3) << 30);
  40. return a ^ rotated_b;
  41. }
  42. };
  43. // A functor for hashing decoration types.
  44. struct SpvDecorationHash {
  45. std::size_t operator()(SpvDecoration dec) const {
  46. return static_cast<std::size_t>(dec);
  47. }
  48. };
  49. // Struct member layout attributes that are inherited through arrays.
  50. struct LayoutConstraints {
  51. explicit LayoutConstraints(
  52. MatrixLayout the_majorness = MatrixLayout::kColumnMajor,
  53. uint32_t stride = 0)
  54. : majorness(the_majorness), matrix_stride(stride) {}
  55. MatrixLayout majorness;
  56. uint32_t matrix_stride;
  57. };
  58. // A type for mapping (struct id, member id) to layout constraints.
  59. using MemberConstraints = std::unordered_map<std::pair<uint32_t, uint32_t>,
  60. LayoutConstraints, PairHash>;
  61. // Returns the array stride of the given array type.
  62. uint32_t GetArrayStride(uint32_t array_id, ValidationState_t& vstate) {
  63. for (auto& decoration : vstate.id_decorations(array_id)) {
  64. if (SpvDecorationArrayStride == decoration.dec_type()) {
  65. return decoration.params()[0];
  66. }
  67. }
  68. return 0;
  69. }
  70. // Returns true if the given variable has a BuiltIn decoration.
  71. bool isBuiltInVar(uint32_t var_id, ValidationState_t& vstate) {
  72. const auto& decorations = vstate.id_decorations(var_id);
  73. return std::any_of(
  74. decorations.begin(), decorations.end(),
  75. [](const Decoration& d) { return SpvDecorationBuiltIn == d.dec_type(); });
  76. }
  77. // Returns true if the given structure type has any members with BuiltIn
  78. // decoration.
  79. bool isBuiltInStruct(uint32_t struct_id, ValidationState_t& vstate) {
  80. const auto& decorations = vstate.id_decorations(struct_id);
  81. return std::any_of(
  82. decorations.begin(), decorations.end(), [](const Decoration& d) {
  83. return SpvDecorationBuiltIn == d.dec_type() &&
  84. Decoration::kInvalidMember != d.struct_member_index();
  85. });
  86. }
  87. // Returns true if the given ID has the Import LinkageAttributes decoration.
  88. bool hasImportLinkageAttribute(uint32_t id, ValidationState_t& vstate) {
  89. const auto& decorations = vstate.id_decorations(id);
  90. return std::any_of(decorations.begin(), decorations.end(),
  91. [](const Decoration& d) {
  92. return SpvDecorationLinkageAttributes == d.dec_type() &&
  93. d.params().size() >= 2u &&
  94. d.params().back() == SpvLinkageTypeImport;
  95. });
  96. }
  97. // Returns a vector of all members of a structure.
  98. std::vector<uint32_t> getStructMembers(uint32_t struct_id,
  99. ValidationState_t& vstate) {
  100. const auto inst = vstate.FindDef(struct_id);
  101. return std::vector<uint32_t>(inst->words().begin() + 2, inst->words().end());
  102. }
  103. // Returns a vector of all members of a structure that have specific type.
  104. std::vector<uint32_t> getStructMembers(uint32_t struct_id, SpvOp type,
  105. ValidationState_t& vstate) {
  106. std::vector<uint32_t> members;
  107. for (auto id : getStructMembers(struct_id, vstate)) {
  108. if (type == vstate.FindDef(id)->opcode()) {
  109. members.push_back(id);
  110. }
  111. }
  112. return members;
  113. }
  114. // Returns whether the given structure is missing Offset decoration for any
  115. // member. Handles also nested structures.
  116. bool isMissingOffsetInStruct(uint32_t struct_id, ValidationState_t& vstate) {
  117. std::vector<bool> hasOffset(getStructMembers(struct_id, vstate).size(),
  118. false);
  119. // Check offsets of member decorations
  120. for (auto& decoration : vstate.id_decorations(struct_id)) {
  121. if (SpvDecorationOffset == decoration.dec_type() &&
  122. Decoration::kInvalidMember != decoration.struct_member_index()) {
  123. hasOffset[decoration.struct_member_index()] = true;
  124. }
  125. }
  126. // Check also nested structures
  127. bool nestedStructsMissingOffset = false;
  128. for (auto id : getStructMembers(struct_id, SpvOpTypeStruct, vstate)) {
  129. if (isMissingOffsetInStruct(id, vstate)) {
  130. nestedStructsMissingOffset = true;
  131. break;
  132. }
  133. }
  134. return nestedStructsMissingOffset ||
  135. !std::all_of(hasOffset.begin(), hasOffset.end(),
  136. [](const bool b) { return b; });
  137. }
  138. // Rounds x up to the next alignment. Assumes alignment is a power of two.
  139. uint32_t align(uint32_t x, uint32_t alignment) {
  140. return (x + alignment - 1) & ~(alignment - 1);
  141. }
  142. // Returns base alignment of struct member. If |roundUp| is true, also
  143. // ensure that structs and arrays are aligned at least to a multiple of 16
  144. // bytes.
  145. uint32_t getBaseAlignment(uint32_t member_id, bool roundUp,
  146. const LayoutConstraints& inherited,
  147. MemberConstraints& constraints,
  148. ValidationState_t& vstate) {
  149. const auto inst = vstate.FindDef(member_id);
  150. const auto& words = inst->words();
  151. // Minimal alignment is byte-aligned.
  152. uint32_t baseAlignment = 1;
  153. switch (inst->opcode()) {
  154. case SpvOpTypeInt:
  155. case SpvOpTypeFloat:
  156. baseAlignment = words[2] / 8;
  157. break;
  158. case SpvOpTypeVector: {
  159. const auto componentId = words[2];
  160. const auto numComponents = words[3];
  161. const auto componentAlignment = getBaseAlignment(
  162. componentId, roundUp, inherited, constraints, vstate);
  163. baseAlignment =
  164. componentAlignment * (numComponents == 3 ? 4 : numComponents);
  165. break;
  166. }
  167. case SpvOpTypeMatrix: {
  168. const auto column_type = words[2];
  169. if (inherited.majorness == kColumnMajor) {
  170. baseAlignment = getBaseAlignment(column_type, roundUp, inherited,
  171. constraints, vstate);
  172. } else {
  173. // A row-major matrix of C columns has a base alignment equal to the
  174. // base alignment of a vector of C matrix components.
  175. const auto num_columns = words[3];
  176. const auto component_inst = vstate.FindDef(column_type);
  177. const auto component_id = component_inst->words()[2];
  178. const auto componentAlignment = getBaseAlignment(
  179. component_id, roundUp, inherited, constraints, vstate);
  180. baseAlignment =
  181. componentAlignment * (num_columns == 3 ? 4 : num_columns);
  182. }
  183. } break;
  184. case SpvOpTypeArray:
  185. case SpvOpTypeRuntimeArray:
  186. baseAlignment =
  187. getBaseAlignment(words[2], roundUp, inherited, constraints, vstate);
  188. if (roundUp) baseAlignment = align(baseAlignment, 16u);
  189. break;
  190. case SpvOpTypeStruct: {
  191. const auto members = getStructMembers(member_id, vstate);
  192. for (uint32_t memberIdx = 0, numMembers = uint32_t(members.size());
  193. memberIdx < numMembers; ++memberIdx) {
  194. const auto id = members[memberIdx];
  195. const auto& constraint =
  196. constraints[std::make_pair(member_id, memberIdx)];
  197. baseAlignment = std::max(
  198. baseAlignment,
  199. getBaseAlignment(id, roundUp, constraint, constraints, vstate));
  200. }
  201. if (roundUp) baseAlignment = align(baseAlignment, 16u);
  202. break;
  203. }
  204. case SpvOpTypePointer:
  205. baseAlignment = vstate.pointer_size_and_alignment();
  206. break;
  207. default:
  208. assert(0);
  209. break;
  210. }
  211. return baseAlignment;
  212. }
  213. // Returns scalar alignment of a type.
  214. uint32_t getScalarAlignment(uint32_t type_id, ValidationState_t& vstate) {
  215. const auto inst = vstate.FindDef(type_id);
  216. const auto& words = inst->words();
  217. switch (inst->opcode()) {
  218. case SpvOpTypeInt:
  219. case SpvOpTypeFloat:
  220. return words[2] / 8;
  221. case SpvOpTypeVector:
  222. case SpvOpTypeMatrix:
  223. case SpvOpTypeArray:
  224. case SpvOpTypeRuntimeArray: {
  225. const auto compositeMemberTypeId = words[2];
  226. return getScalarAlignment(compositeMemberTypeId, vstate);
  227. }
  228. case SpvOpTypeStruct: {
  229. const auto members = getStructMembers(type_id, vstate);
  230. uint32_t max_member_alignment = 1;
  231. for (uint32_t memberIdx = 0, numMembers = uint32_t(members.size());
  232. memberIdx < numMembers; ++memberIdx) {
  233. const auto id = members[memberIdx];
  234. uint32_t member_alignment = getScalarAlignment(id, vstate);
  235. if (member_alignment > max_member_alignment) {
  236. max_member_alignment = member_alignment;
  237. }
  238. }
  239. return max_member_alignment;
  240. } break;
  241. case SpvOpTypePointer:
  242. return vstate.pointer_size_and_alignment();
  243. default:
  244. assert(0);
  245. break;
  246. }
  247. return 1;
  248. }
  249. // Returns size of a struct member. Doesn't include padding at the end of struct
  250. // or array. Assumes that in the struct case, all members have offsets.
  251. uint32_t getSize(uint32_t member_id, const LayoutConstraints& inherited,
  252. MemberConstraints& constraints, ValidationState_t& vstate) {
  253. const auto inst = vstate.FindDef(member_id);
  254. const auto& words = inst->words();
  255. switch (inst->opcode()) {
  256. case SpvOpTypeInt:
  257. case SpvOpTypeFloat:
  258. return words[2] / 8;
  259. case SpvOpTypeVector: {
  260. const auto componentId = words[2];
  261. const auto numComponents = words[3];
  262. const auto componentSize =
  263. getSize(componentId, inherited, constraints, vstate);
  264. const auto size = componentSize * numComponents;
  265. return size;
  266. }
  267. case SpvOpTypeArray: {
  268. const auto sizeInst = vstate.FindDef(words[3]);
  269. if (spvOpcodeIsSpecConstant(sizeInst->opcode())) return 0;
  270. assert(SpvOpConstant == sizeInst->opcode());
  271. const uint32_t num_elem = sizeInst->words()[3];
  272. const uint32_t elem_type = words[2];
  273. const uint32_t elem_size =
  274. getSize(elem_type, inherited, constraints, vstate);
  275. // Account for gaps due to alignments in the first N-1 elements,
  276. // then add the size of the last element.
  277. const auto size =
  278. (num_elem - 1) * GetArrayStride(member_id, vstate) + elem_size;
  279. return size;
  280. }
  281. case SpvOpTypeRuntimeArray:
  282. return 0;
  283. case SpvOpTypeMatrix: {
  284. const auto num_columns = words[3];
  285. if (inherited.majorness == kColumnMajor) {
  286. return num_columns * inherited.matrix_stride;
  287. } else {
  288. // Row major case.
  289. const auto column_type = words[2];
  290. const auto component_inst = vstate.FindDef(column_type);
  291. const auto num_rows = component_inst->words()[3];
  292. const auto scalar_elem_type = component_inst->words()[2];
  293. const uint32_t scalar_elem_size =
  294. getSize(scalar_elem_type, inherited, constraints, vstate);
  295. return (num_rows - 1) * inherited.matrix_stride +
  296. num_columns * scalar_elem_size;
  297. }
  298. }
  299. case SpvOpTypeStruct: {
  300. const auto& members = getStructMembers(member_id, vstate);
  301. if (members.empty()) return 0;
  302. const auto lastIdx = uint32_t(members.size() - 1);
  303. const auto& lastMember = members.back();
  304. uint32_t offset = 0xffffffff;
  305. // Find the offset of the last element and add the size.
  306. for (auto& decoration : vstate.id_decorations(member_id)) {
  307. if (SpvDecorationOffset == decoration.dec_type() &&
  308. decoration.struct_member_index() == (int)lastIdx) {
  309. offset = decoration.params()[0];
  310. }
  311. }
  312. // This check depends on the fact that all members have offsets. This
  313. // has been checked earlier in the flow.
  314. assert(offset != 0xffffffff);
  315. const auto& constraint = constraints[std::make_pair(lastMember, lastIdx)];
  316. return offset + getSize(lastMember, constraint, constraints, vstate);
  317. }
  318. case SpvOpTypePointer:
  319. return vstate.pointer_size_and_alignment();
  320. default:
  321. assert(0);
  322. return 0;
  323. }
  324. }
  325. // A member is defined to improperly straddle if either of the following are
  326. // true:
  327. // - It is a vector with total size less than or equal to 16 bytes, and has
  328. // Offset decorations placing its first byte at F and its last byte at L, where
  329. // floor(F / 16) != floor(L / 16).
  330. // - It is a vector with total size greater than 16 bytes and has its Offset
  331. // decorations placing its first byte at a non-integer multiple of 16.
  332. bool hasImproperStraddle(uint32_t id, uint32_t offset,
  333. const LayoutConstraints& inherited,
  334. MemberConstraints& constraints,
  335. ValidationState_t& vstate) {
  336. const auto size = getSize(id, inherited, constraints, vstate);
  337. const auto F = offset;
  338. const auto L = offset + size - 1;
  339. if (size <= 16) {
  340. if ((F >> 4) != (L >> 4)) return true;
  341. } else {
  342. if (F % 16 != 0) return true;
  343. }
  344. return false;
  345. }
  346. // Returns true if |offset| satsifies an alignment to |alignment|. In the case
  347. // of |alignment| of zero, the |offset| must also be zero.
  348. bool IsAlignedTo(uint32_t offset, uint32_t alignment) {
  349. if (alignment == 0) return offset == 0;
  350. return 0 == (offset % alignment);
  351. }
  352. // Returns SPV_SUCCESS if the given struct satisfies standard layout rules for
  353. // Block or BufferBlocks in Vulkan. Otherwise emits a diagnostic and returns
  354. // something other than SPV_SUCCESS. Matrices inherit the specified column
  355. // or row major-ness.
  356. spv_result_t checkLayout(uint32_t struct_id, const char* storage_class_str,
  357. const char* decoration_str, bool blockRules,
  358. uint32_t incoming_offset,
  359. MemberConstraints& constraints,
  360. ValidationState_t& vstate) {
  361. if (vstate.options()->skip_block_layout) return SPV_SUCCESS;
  362. // blockRules are the same as bufferBlock rules if the uniform buffer
  363. // standard layout extension is being used.
  364. if (vstate.options()->uniform_buffer_standard_layout) blockRules = false;
  365. // Relaxed layout and scalar layout can both be in effect at the same time.
  366. // For example, relaxed layout is implied by Vulkan 1.1. But scalar layout
  367. // is more permissive than relaxed layout.
  368. const bool relaxed_block_layout = vstate.IsRelaxedBlockLayout();
  369. const bool scalar_block_layout = vstate.options()->scalar_block_layout;
  370. auto fail = [&vstate, struct_id, storage_class_str, decoration_str,
  371. blockRules, relaxed_block_layout,
  372. scalar_block_layout](uint32_t member_idx) -> DiagnosticStream {
  373. DiagnosticStream ds =
  374. std::move(vstate.diag(SPV_ERROR_INVALID_ID, vstate.FindDef(struct_id))
  375. << "Structure id " << struct_id << " decorated as "
  376. << decoration_str << " for variable in " << storage_class_str
  377. << " storage class must follow "
  378. << (scalar_block_layout
  379. ? "scalar "
  380. : (relaxed_block_layout ? "relaxed " : "standard "))
  381. << (blockRules ? "uniform buffer" : "storage buffer")
  382. << " layout rules: member " << member_idx << " ");
  383. return ds;
  384. };
  385. const auto& members = getStructMembers(struct_id, vstate);
  386. // To check for member overlaps, we want to traverse the members in
  387. // offset order.
  388. struct MemberOffsetPair {
  389. uint32_t member;
  390. uint32_t offset;
  391. };
  392. std::vector<MemberOffsetPair> member_offsets;
  393. member_offsets.reserve(members.size());
  394. for (uint32_t memberIdx = 0, numMembers = uint32_t(members.size());
  395. memberIdx < numMembers; memberIdx++) {
  396. uint32_t offset = 0xffffffff;
  397. for (auto& decoration : vstate.id_decorations(struct_id)) {
  398. if (decoration.struct_member_index() == (int)memberIdx) {
  399. switch (decoration.dec_type()) {
  400. case SpvDecorationOffset:
  401. offset = decoration.params()[0];
  402. break;
  403. default:
  404. break;
  405. }
  406. }
  407. }
  408. member_offsets.push_back(
  409. MemberOffsetPair{memberIdx, incoming_offset + offset});
  410. }
  411. std::stable_sort(
  412. member_offsets.begin(), member_offsets.end(),
  413. [](const MemberOffsetPair& lhs, const MemberOffsetPair& rhs) {
  414. return lhs.offset < rhs.offset;
  415. });
  416. // Now scan from lowest offest to highest offset.
  417. uint32_t nextValidOffset = 0;
  418. for (size_t ordered_member_idx = 0;
  419. ordered_member_idx < member_offsets.size(); ordered_member_idx++) {
  420. const auto& member_offset = member_offsets[ordered_member_idx];
  421. const auto memberIdx = member_offset.member;
  422. const auto offset = member_offset.offset;
  423. auto id = members[member_offset.member];
  424. const LayoutConstraints& constraint =
  425. constraints[std::make_pair(struct_id, uint32_t(memberIdx))];
  426. // Scalar layout takes precedence because it's more permissive, and implying
  427. // an alignment that divides evenly into the alignment that would otherwise
  428. // be used.
  429. const auto alignment =
  430. scalar_block_layout
  431. ? getScalarAlignment(id, vstate)
  432. : getBaseAlignment(id, blockRules, constraint, constraints, vstate);
  433. const auto inst = vstate.FindDef(id);
  434. const auto opcode = inst->opcode();
  435. const auto size = getSize(id, constraint, constraints, vstate);
  436. // Check offset.
  437. if (offset == 0xffffffff)
  438. return fail(memberIdx) << "is missing an Offset decoration";
  439. if (!scalar_block_layout && relaxed_block_layout &&
  440. opcode == SpvOpTypeVector) {
  441. // In relaxed block layout, the vector offset must be aligned to the
  442. // vector's scalar element type.
  443. const auto componentId = inst->words()[2];
  444. const auto scalar_alignment = getScalarAlignment(componentId, vstate);
  445. if (!IsAlignedTo(offset, scalar_alignment)) {
  446. return fail(memberIdx)
  447. << "at offset " << offset
  448. << " is not aligned to scalar element size " << scalar_alignment;
  449. }
  450. } else {
  451. // Without relaxed block layout, the offset must be divisible by the
  452. // alignment requirement.
  453. if (!IsAlignedTo(offset, alignment)) {
  454. return fail(memberIdx)
  455. << "at offset " << offset << " is not aligned to " << alignment;
  456. }
  457. }
  458. if (offset < nextValidOffset)
  459. return fail(memberIdx) << "at offset " << offset
  460. << " overlaps previous member ending at offset "
  461. << nextValidOffset - 1;
  462. if (!scalar_block_layout && relaxed_block_layout) {
  463. // Check improper straddle of vectors.
  464. if (SpvOpTypeVector == opcode &&
  465. hasImproperStraddle(id, offset, constraint, constraints, vstate))
  466. return fail(memberIdx)
  467. << "is an improperly straddling vector at offset " << offset;
  468. }
  469. // Check struct members recursively.
  470. spv_result_t recursive_status = SPV_SUCCESS;
  471. if (SpvOpTypeStruct == opcode &&
  472. SPV_SUCCESS != (recursive_status = checkLayout(
  473. id, storage_class_str, decoration_str, blockRules,
  474. offset, constraints, vstate)))
  475. return recursive_status;
  476. // Check matrix stride.
  477. if (SpvOpTypeMatrix == opcode) {
  478. for (auto& decoration : vstate.id_decorations(id)) {
  479. if (SpvDecorationMatrixStride == decoration.dec_type() &&
  480. !IsAlignedTo(decoration.params()[0], alignment))
  481. return fail(memberIdx)
  482. << "is a matrix with stride " << decoration.params()[0]
  483. << " not satisfying alignment to " << alignment;
  484. }
  485. }
  486. // Check arrays and runtime arrays recursively.
  487. auto array_inst = inst;
  488. auto array_alignment = alignment;
  489. while (array_inst->opcode() == SpvOpTypeArray ||
  490. array_inst->opcode() == SpvOpTypeRuntimeArray) {
  491. const auto typeId = array_inst->word(2);
  492. const auto element_inst = vstate.FindDef(typeId);
  493. // Check array stride.
  494. auto array_stride = 0;
  495. for (auto& decoration : vstate.id_decorations(array_inst->id())) {
  496. if (SpvDecorationArrayStride == decoration.dec_type()) {
  497. array_stride = decoration.params()[0];
  498. if (!IsAlignedTo(array_stride, array_alignment))
  499. return fail(memberIdx)
  500. << "contains an array with stride " << decoration.params()[0]
  501. << " not satisfying alignment to " << alignment;
  502. }
  503. }
  504. bool is_int32 = false;
  505. bool is_const = false;
  506. uint32_t num_elements = 0;
  507. if (array_inst->opcode() == SpvOpTypeArray) {
  508. std::tie(is_int32, is_const, num_elements) =
  509. vstate.EvalInt32IfConst(array_inst->word(3));
  510. }
  511. num_elements = std::max(1u, num_elements);
  512. // Check each element recursively if it is a struct. There is a
  513. // limitation to this check if the array size is a spec constant or is a
  514. // runtime array then we will only check a single element. This means
  515. // some improper straddles might be missed.
  516. for (uint32_t i = 0; i < num_elements; ++i) {
  517. uint32_t next_offset = i * array_stride + offset;
  518. if (SpvOpTypeStruct == element_inst->opcode() &&
  519. SPV_SUCCESS != (recursive_status = checkLayout(
  520. typeId, storage_class_str, decoration_str,
  521. blockRules, next_offset, constraints, vstate)))
  522. return recursive_status;
  523. // If offsets accumulate up to a 16-byte multiple stop checking since
  524. // it will just repeat.
  525. if (i > 0 && (next_offset % 16 == 0)) break;
  526. }
  527. // Proceed to the element in case it is an array.
  528. array_inst = element_inst;
  529. array_alignment = scalar_block_layout
  530. ? getScalarAlignment(array_inst->id(), vstate)
  531. : getBaseAlignment(array_inst->id(), blockRules,
  532. constraint, constraints, vstate);
  533. }
  534. nextValidOffset = offset + size;
  535. if (!scalar_block_layout && blockRules &&
  536. (SpvOpTypeArray == opcode || SpvOpTypeStruct == opcode)) {
  537. // Uniform block rules don't permit anything in the padding of a struct
  538. // or array.
  539. nextValidOffset = align(nextValidOffset, alignment);
  540. }
  541. }
  542. return SPV_SUCCESS;
  543. }
  544. // Returns true if variable or structure id has given decoration. Handles also
  545. // nested structures.
  546. bool hasDecoration(uint32_t id, SpvDecoration decoration,
  547. ValidationState_t& vstate) {
  548. for (auto& dec : vstate.id_decorations(id)) {
  549. if (decoration == dec.dec_type()) return true;
  550. }
  551. if (SpvOpTypeStruct != vstate.FindDef(id)->opcode()) {
  552. return false;
  553. }
  554. for (auto member_id : getStructMembers(id, SpvOpTypeStruct, vstate)) {
  555. if (hasDecoration(member_id, decoration, vstate)) {
  556. return true;
  557. }
  558. }
  559. return false;
  560. }
  561. // Returns true if all ids of given type have a specified decoration.
  562. bool checkForRequiredDecoration(uint32_t struct_id, SpvDecoration decoration,
  563. SpvOp type, ValidationState_t& vstate) {
  564. const auto& members = getStructMembers(struct_id, vstate);
  565. for (size_t memberIdx = 0; memberIdx < members.size(); memberIdx++) {
  566. const auto id = members[memberIdx];
  567. if (type != vstate.FindDef(id)->opcode()) continue;
  568. bool found = false;
  569. for (auto& dec : vstate.id_decorations(id)) {
  570. if (decoration == dec.dec_type()) found = true;
  571. }
  572. for (auto& dec : vstate.id_decorations(struct_id)) {
  573. if (decoration == dec.dec_type() &&
  574. (int)memberIdx == dec.struct_member_index()) {
  575. found = true;
  576. }
  577. }
  578. if (!found) {
  579. return false;
  580. }
  581. }
  582. for (auto id : getStructMembers(struct_id, SpvOpTypeStruct, vstate)) {
  583. if (!checkForRequiredDecoration(id, decoration, type, vstate)) {
  584. return false;
  585. }
  586. }
  587. return true;
  588. }
  589. spv_result_t CheckLinkageAttrOfFunctions(ValidationState_t& vstate) {
  590. for (const auto& function : vstate.functions()) {
  591. if (function.block_count() == 0u) {
  592. // A function declaration (an OpFunction with no basic blocks), must have
  593. // a Linkage Attributes Decoration with the Import Linkage Type.
  594. if (!hasImportLinkageAttribute(function.id(), vstate)) {
  595. return vstate.diag(SPV_ERROR_INVALID_BINARY,
  596. vstate.FindDef(function.id()))
  597. << "Function declaration (id " << function.id()
  598. << ") must have a LinkageAttributes decoration with the Import "
  599. "Linkage type.";
  600. }
  601. } else {
  602. if (hasImportLinkageAttribute(function.id(), vstate)) {
  603. return vstate.diag(SPV_ERROR_INVALID_BINARY,
  604. vstate.FindDef(function.id()))
  605. << "Function definition (id " << function.id()
  606. << ") may not be decorated with Import Linkage type.";
  607. }
  608. }
  609. }
  610. return SPV_SUCCESS;
  611. }
  612. // Checks whether an imported variable is initialized by this module.
  613. spv_result_t CheckImportedVariableInitialization(ValidationState_t& vstate) {
  614. // According the SPIR-V Spec 2.16.1, it is illegal to initialize an imported
  615. // variable. This means that a module-scope OpVariable with initialization
  616. // value cannot be marked with the Import Linkage Type (import type id = 1).
  617. for (auto global_var_id : vstate.global_vars()) {
  618. // Initializer <id> is an optional argument for OpVariable. If initializer
  619. // <id> is present, the instruction will have 5 words.
  620. auto variable_instr = vstate.FindDef(global_var_id);
  621. if (variable_instr->words().size() == 5u &&
  622. hasImportLinkageAttribute(global_var_id, vstate)) {
  623. return vstate.diag(SPV_ERROR_INVALID_ID, variable_instr)
  624. << "A module-scope OpVariable with initialization value "
  625. "cannot be marked with the Import Linkage Type.";
  626. }
  627. }
  628. return SPV_SUCCESS;
  629. }
  630. // Checks whether a builtin variable is valid.
  631. spv_result_t CheckBuiltInVariable(uint32_t var_id, ValidationState_t& vstate) {
  632. const auto& decorations = vstate.id_decorations(var_id);
  633. for (const auto& d : decorations) {
  634. if (spvIsVulkanEnv(vstate.context()->target_env)) {
  635. if (d.dec_type() == SpvDecorationLocation ||
  636. d.dec_type() == SpvDecorationComponent) {
  637. return vstate.diag(SPV_ERROR_INVALID_ID, vstate.FindDef(var_id))
  638. << "A BuiltIn variable (id " << var_id
  639. << ") cannot have any Location or Component decorations";
  640. }
  641. }
  642. }
  643. return SPV_SUCCESS;
  644. }
  645. // Checks whether proper decorations have been appied to the entry points.
  646. spv_result_t CheckDecorationsOfEntryPoints(ValidationState_t& vstate) {
  647. for (uint32_t entry_point : vstate.entry_points()) {
  648. const auto& descs = vstate.entry_point_descriptions(entry_point);
  649. int num_builtin_inputs = 0;
  650. int num_builtin_outputs = 0;
  651. for (const auto& desc : descs) {
  652. std::unordered_set<Instruction*> seen_vars;
  653. for (auto interface : desc.interfaces) {
  654. Instruction* var_instr = vstate.FindDef(interface);
  655. if (!var_instr || SpvOpVariable != var_instr->opcode()) {
  656. return vstate.diag(SPV_ERROR_INVALID_ID, var_instr)
  657. << "Interfaces passed to OpEntryPoint must be of type "
  658. "OpTypeVariable. Found Op"
  659. << spvOpcodeString(var_instr->opcode()) << ".";
  660. }
  661. const SpvStorageClass storage_class =
  662. var_instr->GetOperandAs<SpvStorageClass>(2);
  663. if (vstate.version() >= SPV_SPIRV_VERSION_WORD(1, 4)) {
  664. // Starting in 1.4, OpEntryPoint must list all global variables
  665. // it statically uses and those interfaces must be unique.
  666. if (storage_class == SpvStorageClassFunction) {
  667. return vstate.diag(SPV_ERROR_INVALID_ID, var_instr)
  668. << "OpEntryPoint interfaces should only list global "
  669. "variables";
  670. }
  671. if (!seen_vars.insert(var_instr).second) {
  672. return vstate.diag(SPV_ERROR_INVALID_ID, var_instr)
  673. << "Non-unique OpEntryPoint interface "
  674. << vstate.getIdName(interface) << " is disallowed";
  675. }
  676. } else {
  677. if (storage_class != SpvStorageClassInput &&
  678. storage_class != SpvStorageClassOutput) {
  679. return vstate.diag(SPV_ERROR_INVALID_ID, var_instr)
  680. << "OpEntryPoint interfaces must be OpVariables with "
  681. "Storage Class of Input(1) or Output(3). Found Storage "
  682. "Class "
  683. << storage_class << " for Entry Point id " << entry_point
  684. << ".";
  685. }
  686. }
  687. const uint32_t ptr_id = var_instr->word(1);
  688. Instruction* ptr_instr = vstate.FindDef(ptr_id);
  689. // It is guaranteed (by validator ID checks) that ptr_instr is
  690. // OpTypePointer. Word 3 of this instruction is the type being pointed
  691. // to.
  692. const uint32_t type_id = ptr_instr->word(3);
  693. Instruction* type_instr = vstate.FindDef(type_id);
  694. if (type_instr && SpvOpTypeStruct == type_instr->opcode() &&
  695. isBuiltInStruct(type_id, vstate)) {
  696. if (storage_class == SpvStorageClassInput) ++num_builtin_inputs;
  697. if (storage_class == SpvStorageClassOutput) ++num_builtin_outputs;
  698. if (num_builtin_inputs > 1 || num_builtin_outputs > 1) break;
  699. if (auto error = CheckBuiltInVariable(interface, vstate))
  700. return error;
  701. } else if (isBuiltInVar(interface, vstate)) {
  702. if (auto error = CheckBuiltInVariable(interface, vstate))
  703. return error;
  704. }
  705. }
  706. if (num_builtin_inputs > 1 || num_builtin_outputs > 1) {
  707. return vstate.diag(SPV_ERROR_INVALID_BINARY,
  708. vstate.FindDef(entry_point))
  709. << "There must be at most one object per Storage Class that can "
  710. "contain a structure type containing members decorated with "
  711. "BuiltIn, consumed per entry-point. Entry Point id "
  712. << entry_point << " does not meet this requirement.";
  713. }
  714. // The LinkageAttributes Decoration cannot be applied to functions
  715. // targeted by an OpEntryPoint instruction
  716. for (auto& decoration : vstate.id_decorations(entry_point)) {
  717. if (SpvDecorationLinkageAttributes == decoration.dec_type()) {
  718. const char* linkage_name =
  719. reinterpret_cast<const char*>(&decoration.params()[0]);
  720. return vstate.diag(SPV_ERROR_INVALID_BINARY,
  721. vstate.FindDef(entry_point))
  722. << "The LinkageAttributes Decoration (Linkage name: "
  723. << linkage_name << ") cannot be applied to function id "
  724. << entry_point
  725. << " because it is targeted by an OpEntryPoint instruction.";
  726. }
  727. }
  728. }
  729. }
  730. return SPV_SUCCESS;
  731. }
  732. // Load |constraints| with all the member constraints for structs contained
  733. // within the given array type.
  734. void ComputeMemberConstraintsForArray(MemberConstraints* constraints,
  735. uint32_t array_id,
  736. const LayoutConstraints& inherited,
  737. ValidationState_t& vstate);
  738. // Load |constraints| with all the member constraints for the given struct,
  739. // and all its contained structs.
  740. void ComputeMemberConstraintsForStruct(MemberConstraints* constraints,
  741. uint32_t struct_id,
  742. const LayoutConstraints& inherited,
  743. ValidationState_t& vstate) {
  744. assert(constraints);
  745. const auto& members = getStructMembers(struct_id, vstate);
  746. for (uint32_t memberIdx = 0, numMembers = uint32_t(members.size());
  747. memberIdx < numMembers; memberIdx++) {
  748. LayoutConstraints& constraint =
  749. (*constraints)[std::make_pair(struct_id, memberIdx)];
  750. constraint = inherited;
  751. for (auto& decoration : vstate.id_decorations(struct_id)) {
  752. if (decoration.struct_member_index() == (int)memberIdx) {
  753. switch (decoration.dec_type()) {
  754. case SpvDecorationRowMajor:
  755. constraint.majorness = kRowMajor;
  756. break;
  757. case SpvDecorationColMajor:
  758. constraint.majorness = kColumnMajor;
  759. break;
  760. case SpvDecorationMatrixStride:
  761. constraint.matrix_stride = decoration.params()[0];
  762. break;
  763. default:
  764. break;
  765. }
  766. }
  767. }
  768. // Now recurse
  769. auto member_type_id = members[memberIdx];
  770. const auto member_type_inst = vstate.FindDef(member_type_id);
  771. const auto opcode = member_type_inst->opcode();
  772. switch (opcode) {
  773. case SpvOpTypeArray:
  774. case SpvOpTypeRuntimeArray:
  775. ComputeMemberConstraintsForArray(constraints, member_type_id, inherited,
  776. vstate);
  777. break;
  778. case SpvOpTypeStruct:
  779. ComputeMemberConstraintsForStruct(constraints, member_type_id,
  780. inherited, vstate);
  781. break;
  782. default:
  783. break;
  784. }
  785. }
  786. }
  787. void ComputeMemberConstraintsForArray(MemberConstraints* constraints,
  788. uint32_t array_id,
  789. const LayoutConstraints& inherited,
  790. ValidationState_t& vstate) {
  791. assert(constraints);
  792. auto elem_type_id = vstate.FindDef(array_id)->words()[2];
  793. const auto elem_type_inst = vstate.FindDef(elem_type_id);
  794. const auto opcode = elem_type_inst->opcode();
  795. switch (opcode) {
  796. case SpvOpTypeArray:
  797. case SpvOpTypeRuntimeArray:
  798. ComputeMemberConstraintsForArray(constraints, elem_type_id, inherited,
  799. vstate);
  800. break;
  801. case SpvOpTypeStruct:
  802. ComputeMemberConstraintsForStruct(constraints, elem_type_id, inherited,
  803. vstate);
  804. break;
  805. default:
  806. break;
  807. }
  808. }
  809. spv_result_t CheckDecorationsOfBuffers(ValidationState_t& vstate) {
  810. // Set of entry points that are known to use a push constant.
  811. std::unordered_set<uint32_t> uses_push_constant;
  812. for (const auto& inst : vstate.ordered_instructions()) {
  813. const auto& words = inst.words();
  814. if (SpvOpVariable == inst.opcode()) {
  815. const auto var_id = inst.id();
  816. // For storage class / decoration combinations, see Vulkan 14.5.4 "Offset
  817. // and Stride Assignment".
  818. const auto storageClass = words[3];
  819. const bool uniform = storageClass == SpvStorageClassUniform;
  820. const bool uniform_constant =
  821. storageClass == SpvStorageClassUniformConstant;
  822. const bool push_constant = storageClass == SpvStorageClassPushConstant;
  823. const bool storage_buffer = storageClass == SpvStorageClassStorageBuffer;
  824. if (spvIsVulkanEnv(vstate.context()->target_env)) {
  825. // Vulkan 14.5.1: There must be no more than one PushConstant block
  826. // per entry point.
  827. if (push_constant) {
  828. auto entry_points = vstate.EntryPointReferences(var_id);
  829. for (auto ep_id : entry_points) {
  830. const bool already_used = !uses_push_constant.insert(ep_id).second;
  831. if (already_used) {
  832. return vstate.diag(SPV_ERROR_INVALID_ID, vstate.FindDef(var_id))
  833. << "Entry point id '" << ep_id
  834. << "' uses more than one PushConstant interface.\n"
  835. << "From Vulkan spec, section 14.5.1:\n"
  836. << "There must be no more than one push constant block "
  837. << "statically used per shader entry point.";
  838. }
  839. }
  840. }
  841. // Vulkan 14.5.2: Check DescriptorSet and Binding decoration for
  842. // UniformConstant which cannot be a struct.
  843. if (uniform_constant) {
  844. auto entry_points = vstate.EntryPointReferences(var_id);
  845. if (!entry_points.empty() &&
  846. !hasDecoration(var_id, SpvDecorationDescriptorSet, vstate)) {
  847. return vstate.diag(SPV_ERROR_INVALID_ID, vstate.FindDef(var_id))
  848. << "UniformConstant id '" << var_id
  849. << "' is missing DescriptorSet decoration.\n"
  850. << "From Vulkan spec, section 14.5.2:\n"
  851. << "These variables must have DescriptorSet and Binding "
  852. "decorations specified";
  853. }
  854. if (!entry_points.empty() &&
  855. !hasDecoration(var_id, SpvDecorationBinding, vstate)) {
  856. return vstate.diag(SPV_ERROR_INVALID_ID, vstate.FindDef(var_id))
  857. << "UniformConstant id '" << var_id
  858. << "' is missing Binding decoration.\n"
  859. << "From Vulkan spec, section 14.5.2:\n"
  860. << "These variables must have DescriptorSet and Binding "
  861. "decorations specified";
  862. }
  863. }
  864. }
  865. if (spvIsOpenGLEnv(vstate.context()->target_env)) {
  866. bool has_block = hasDecoration(var_id, SpvDecorationBlock, vstate);
  867. bool has_buffer_block =
  868. hasDecoration(var_id, SpvDecorationBufferBlock, vstate);
  869. if ((uniform && (has_block || has_buffer_block)) ||
  870. (storage_buffer && has_block)) {
  871. auto entry_points = vstate.EntryPointReferences(var_id);
  872. if (!entry_points.empty() &&
  873. !hasDecoration(var_id, SpvDecorationBinding, vstate)) {
  874. return vstate.diag(SPV_ERROR_INVALID_ID, vstate.FindDef(var_id))
  875. << (uniform ? "Uniform" : "Storage Buffer") << " id '"
  876. << var_id << "' is missing Binding decoration.\n"
  877. << "From ARB_gl_spirv extension:\n"
  878. << "Uniform and shader storage block variables must "
  879. << "also be decorated with a *Binding*.";
  880. }
  881. }
  882. }
  883. const bool phys_storage_buffer =
  884. storageClass == SpvStorageClassPhysicalStorageBufferEXT;
  885. if (uniform || push_constant || storage_buffer || phys_storage_buffer) {
  886. const auto ptrInst = vstate.FindDef(words[1]);
  887. assert(SpvOpTypePointer == ptrInst->opcode());
  888. auto id = ptrInst->words()[3];
  889. auto id_inst = vstate.FindDef(id);
  890. // Jump through one level of arraying.
  891. if (id_inst->opcode() == SpvOpTypeArray ||
  892. id_inst->opcode() == SpvOpTypeRuntimeArray) {
  893. id = id_inst->GetOperandAs<uint32_t>(1u);
  894. id_inst = vstate.FindDef(id);
  895. }
  896. if (SpvOpTypeStruct != id_inst->opcode()) continue;
  897. MemberConstraints constraints;
  898. ComputeMemberConstraintsForStruct(&constraints, id, LayoutConstraints(),
  899. vstate);
  900. // Prepare for messages
  901. const char* sc_str =
  902. uniform ? "Uniform"
  903. : (push_constant ? "PushConstant" : "StorageBuffer");
  904. if (spvIsVulkanEnv(vstate.context()->target_env)) {
  905. if (storage_buffer &&
  906. hasDecoration(id, SpvDecorationBufferBlock, vstate)) {
  907. return vstate.diag(SPV_ERROR_INVALID_ID, vstate.FindDef(var_id))
  908. << "Storage buffer id '" << var_id
  909. << " In Vulkan, BufferBlock is disallowed on variables in "
  910. "the StorageBuffer storage class";
  911. }
  912. // Vulkan 14.5.1: Check Block decoration for PushConstant variables.
  913. if (push_constant && !hasDecoration(id, SpvDecorationBlock, vstate)) {
  914. return vstate.diag(SPV_ERROR_INVALID_ID, vstate.FindDef(id))
  915. << "PushConstant id '" << id
  916. << "' is missing Block decoration.\n"
  917. << "From Vulkan spec, section 14.5.1:\n"
  918. << "Such variables must be identified with a Block "
  919. "decoration";
  920. }
  921. // Vulkan 14.5.2: Check DescriptorSet and Binding decoration for
  922. // Uniform and StorageBuffer variables.
  923. if (uniform || storage_buffer) {
  924. auto entry_points = vstate.EntryPointReferences(var_id);
  925. if (!entry_points.empty() &&
  926. !hasDecoration(var_id, SpvDecorationDescriptorSet, vstate)) {
  927. return vstate.diag(SPV_ERROR_INVALID_ID, vstate.FindDef(var_id))
  928. << sc_str << " id '" << var_id
  929. << "' is missing DescriptorSet decoration.\n"
  930. << "From Vulkan spec, section 14.5.2:\n"
  931. << "These variables must have DescriptorSet and Binding "
  932. "decorations specified";
  933. }
  934. if (!entry_points.empty() &&
  935. !hasDecoration(var_id, SpvDecorationBinding, vstate)) {
  936. return vstate.diag(SPV_ERROR_INVALID_ID, vstate.FindDef(var_id))
  937. << sc_str << " id '" << var_id
  938. << "' is missing Binding decoration.\n"
  939. << "From Vulkan spec, section 14.5.2:\n"
  940. << "These variables must have DescriptorSet and Binding "
  941. "decorations specified";
  942. }
  943. }
  944. }
  945. for (const auto& dec : vstate.id_decorations(id)) {
  946. const bool blockDeco = SpvDecorationBlock == dec.dec_type();
  947. const bool bufferDeco = SpvDecorationBufferBlock == dec.dec_type();
  948. const bool blockRules = uniform && blockDeco;
  949. const bool bufferRules =
  950. (uniform && bufferDeco) || (push_constant && blockDeco) ||
  951. ((storage_buffer || phys_storage_buffer) && blockDeco);
  952. if (uniform && blockDeco) {
  953. vstate.RegisterPointerToUniformBlock(ptrInst->id());
  954. vstate.RegisterStructForUniformBlock(id);
  955. }
  956. if ((uniform && bufferDeco) ||
  957. ((storage_buffer || phys_storage_buffer) && blockDeco)) {
  958. vstate.RegisterPointerToStorageBuffer(ptrInst->id());
  959. vstate.RegisterStructForStorageBuffer(id);
  960. }
  961. if (blockRules || bufferRules) {
  962. const char* deco_str = blockDeco ? "Block" : "BufferBlock";
  963. spv_result_t recursive_status = SPV_SUCCESS;
  964. if (isMissingOffsetInStruct(id, vstate)) {
  965. return vstate.diag(SPV_ERROR_INVALID_ID, vstate.FindDef(id))
  966. << "Structure id " << id << " decorated as " << deco_str
  967. << " must be explicitly laid out with Offset "
  968. "decorations.";
  969. } else if (hasDecoration(id, SpvDecorationGLSLShared, vstate)) {
  970. return vstate.diag(SPV_ERROR_INVALID_ID, vstate.FindDef(id))
  971. << "Structure id " << id << " decorated as " << deco_str
  972. << " must not use GLSLShared decoration.";
  973. } else if (hasDecoration(id, SpvDecorationGLSLPacked, vstate)) {
  974. return vstate.diag(SPV_ERROR_INVALID_ID, vstate.FindDef(id))
  975. << "Structure id " << id << " decorated as " << deco_str
  976. << " must not use GLSLPacked decoration.";
  977. } else if (!checkForRequiredDecoration(id, SpvDecorationArrayStride,
  978. SpvOpTypeArray, vstate)) {
  979. return vstate.diag(SPV_ERROR_INVALID_ID, vstate.FindDef(id))
  980. << "Structure id " << id << " decorated as " << deco_str
  981. << " must be explicitly laid out with ArrayStride "
  982. "decorations.";
  983. } else if (!checkForRequiredDecoration(id,
  984. SpvDecorationMatrixStride,
  985. SpvOpTypeMatrix, vstate)) {
  986. return vstate.diag(SPV_ERROR_INVALID_ID, vstate.FindDef(id))
  987. << "Structure id " << id << " decorated as " << deco_str
  988. << " must be explicitly laid out with MatrixStride "
  989. "decorations.";
  990. } else if (blockRules &&
  991. (SPV_SUCCESS != (recursive_status = checkLayout(
  992. id, sc_str, deco_str, true, 0,
  993. constraints, vstate)))) {
  994. return recursive_status;
  995. } else if (bufferRules &&
  996. (SPV_SUCCESS != (recursive_status = checkLayout(
  997. id, sc_str, deco_str, false, 0,
  998. constraints, vstate)))) {
  999. return recursive_status;
  1000. }
  1001. }
  1002. }
  1003. }
  1004. }
  1005. }
  1006. return SPV_SUCCESS;
  1007. }
  1008. // Returns true if |decoration| cannot be applied to the same id more than once.
  1009. bool AtMostOncePerId(SpvDecoration decoration) {
  1010. return decoration == SpvDecorationArrayStride;
  1011. }
  1012. // Returns true if |decoration| cannot be applied to the same member more than
  1013. // once.
  1014. bool AtMostOncePerMember(SpvDecoration decoration) {
  1015. switch (decoration) {
  1016. case SpvDecorationOffset:
  1017. case SpvDecorationMatrixStride:
  1018. case SpvDecorationRowMajor:
  1019. case SpvDecorationColMajor:
  1020. return true;
  1021. default:
  1022. return false;
  1023. }
  1024. }
  1025. // Returns the string name for |decoration|.
  1026. const char* GetDecorationName(SpvDecoration decoration) {
  1027. switch (decoration) {
  1028. case SpvDecorationAliased:
  1029. return "Aliased";
  1030. case SpvDecorationRestrict:
  1031. return "Restrict";
  1032. case SpvDecorationArrayStride:
  1033. return "ArrayStride";
  1034. case SpvDecorationOffset:
  1035. return "Offset";
  1036. case SpvDecorationMatrixStride:
  1037. return "MatrixStride";
  1038. case SpvDecorationRowMajor:
  1039. return "RowMajor";
  1040. case SpvDecorationColMajor:
  1041. return "ColMajor";
  1042. case SpvDecorationBlock:
  1043. return "Block";
  1044. case SpvDecorationBufferBlock:
  1045. return "BufferBlock";
  1046. default:
  1047. return "";
  1048. }
  1049. }
  1050. spv_result_t CheckDecorationsCompatibility(ValidationState_t& vstate) {
  1051. using PerIDKey = std::tuple<SpvDecoration, uint32_t>;
  1052. using PerMemberKey = std::tuple<SpvDecoration, uint32_t, uint32_t>;
  1053. // An Array of pairs where the decorations in the pair cannot both be applied
  1054. // to the same id.
  1055. static const SpvDecoration mutually_exclusive_per_id[][2] = {
  1056. {SpvDecorationBlock, SpvDecorationBufferBlock},
  1057. {SpvDecorationRestrict, SpvDecorationAliased}};
  1058. static const auto num_mutually_exclusive_per_id_pairs =
  1059. sizeof(mutually_exclusive_per_id) / (2 * sizeof(SpvDecoration));
  1060. // An Array of pairs where the decorations in the pair cannot both be applied
  1061. // to the same member.
  1062. static const SpvDecoration mutually_exclusive_per_member[][2] = {
  1063. {SpvDecorationRowMajor, SpvDecorationColMajor}};
  1064. static const auto num_mutually_exclusive_per_mem_pairs =
  1065. sizeof(mutually_exclusive_per_member) / (2 * sizeof(SpvDecoration));
  1066. std::set<PerIDKey> seen_per_id;
  1067. std::set<PerMemberKey> seen_per_member;
  1068. for (const auto& inst : vstate.ordered_instructions()) {
  1069. const auto& words = inst.words();
  1070. if (SpvOpDecorate == inst.opcode()) {
  1071. const auto id = words[1];
  1072. const auto dec_type = static_cast<SpvDecoration>(words[2]);
  1073. const auto k = PerIDKey(dec_type, id);
  1074. const auto already_used = !seen_per_id.insert(k).second;
  1075. if (already_used && AtMostOncePerId(dec_type)) {
  1076. return vstate.diag(SPV_ERROR_INVALID_ID, vstate.FindDef(id))
  1077. << "ID '" << id << "' decorated with "
  1078. << GetDecorationName(dec_type)
  1079. << " multiple times is not allowed.";
  1080. }
  1081. // Verify certain mutually exclusive decorations are not both applied on
  1082. // an ID.
  1083. for (uint32_t pair_idx = 0;
  1084. pair_idx < num_mutually_exclusive_per_id_pairs; ++pair_idx) {
  1085. SpvDecoration excl_dec_type = SpvDecorationMax;
  1086. if (mutually_exclusive_per_id[pair_idx][0] == dec_type) {
  1087. excl_dec_type = mutually_exclusive_per_id[pair_idx][1];
  1088. } else if (mutually_exclusive_per_id[pair_idx][1] == dec_type) {
  1089. excl_dec_type = mutually_exclusive_per_id[pair_idx][0];
  1090. } else {
  1091. continue;
  1092. }
  1093. const auto excl_k = PerIDKey(excl_dec_type, id);
  1094. if (seen_per_id.find(excl_k) != seen_per_id.end()) {
  1095. return vstate.diag(SPV_ERROR_INVALID_ID, vstate.FindDef(id))
  1096. << "ID '" << id << "' decorated with both "
  1097. << GetDecorationName(dec_type) << " and "
  1098. << GetDecorationName(excl_dec_type) << " is not allowed.";
  1099. }
  1100. }
  1101. } else if (SpvOpMemberDecorate == inst.opcode()) {
  1102. const auto id = words[1];
  1103. const auto member_id = words[2];
  1104. const auto dec_type = static_cast<SpvDecoration>(words[3]);
  1105. const auto k = PerMemberKey(dec_type, id, member_id);
  1106. const auto already_used = !seen_per_member.insert(k).second;
  1107. if (already_used && AtMostOncePerMember(dec_type)) {
  1108. return vstate.diag(SPV_ERROR_INVALID_ID, vstate.FindDef(id))
  1109. << "ID '" << id << "', member '" << member_id
  1110. << "' decorated with " << GetDecorationName(dec_type)
  1111. << " multiple times is not allowed.";
  1112. }
  1113. // Verify certain mutually exclusive decorations are not both applied on
  1114. // a (ID, member) tuple.
  1115. for (uint32_t pair_idx = 0;
  1116. pair_idx < num_mutually_exclusive_per_mem_pairs; ++pair_idx) {
  1117. SpvDecoration excl_dec_type = SpvDecorationMax;
  1118. if (mutually_exclusive_per_member[pair_idx][0] == dec_type) {
  1119. excl_dec_type = mutually_exclusive_per_member[pair_idx][1];
  1120. } else if (mutually_exclusive_per_member[pair_idx][1] == dec_type) {
  1121. excl_dec_type = mutually_exclusive_per_member[pair_idx][0];
  1122. } else {
  1123. continue;
  1124. }
  1125. const auto excl_k = PerMemberKey(excl_dec_type, id, member_id);
  1126. if (seen_per_member.find(excl_k) != seen_per_member.end()) {
  1127. return vstate.diag(SPV_ERROR_INVALID_ID, vstate.FindDef(id))
  1128. << "ID '" << id << "', member '" << member_id
  1129. << "' decorated with both " << GetDecorationName(dec_type)
  1130. << " and " << GetDecorationName(excl_dec_type)
  1131. << " is not allowed.";
  1132. }
  1133. }
  1134. }
  1135. }
  1136. return SPV_SUCCESS;
  1137. }
  1138. spv_result_t CheckVulkanMemoryModelDeprecatedDecorations(
  1139. ValidationState_t& vstate) {
  1140. if (vstate.memory_model() != SpvMemoryModelVulkanKHR) return SPV_SUCCESS;
  1141. std::string msg;
  1142. std::ostringstream str(msg);
  1143. for (const auto& def : vstate.all_definitions()) {
  1144. const auto inst = def.second;
  1145. const auto id = inst->id();
  1146. for (const auto& dec : vstate.id_decorations(id)) {
  1147. const auto member = dec.struct_member_index();
  1148. if (dec.dec_type() == SpvDecorationCoherent ||
  1149. dec.dec_type() == SpvDecorationVolatile) {
  1150. str << (dec.dec_type() == SpvDecorationCoherent ? "Coherent"
  1151. : "Volatile");
  1152. str << " decoration targeting " << vstate.getIdName(id);
  1153. if (member != Decoration::kInvalidMember) {
  1154. str << " (member index " << member << ")";
  1155. }
  1156. str << " is banned when using the Vulkan memory model.";
  1157. return vstate.diag(SPV_ERROR_INVALID_ID, inst) << str.str();
  1158. }
  1159. }
  1160. }
  1161. return SPV_SUCCESS;
  1162. }
  1163. // Returns SPV_SUCCESS if validation rules are satisfied for FPRoundingMode
  1164. // decorations. Otherwise emits a diagnostic and returns something other than
  1165. // SPV_SUCCESS.
  1166. spv_result_t CheckFPRoundingModeForShaders(ValidationState_t& vstate,
  1167. const Instruction& inst) {
  1168. // Validates width-only conversion instruction for floating-point object
  1169. // i.e., OpFConvert
  1170. if (inst.opcode() != SpvOpFConvert) {
  1171. return vstate.diag(SPV_ERROR_INVALID_ID, &inst)
  1172. << "FPRoundingMode decoration can be applied only to a "
  1173. "width-only conversion instruction for floating-point "
  1174. "object.";
  1175. }
  1176. // Validates Object operand of an OpStore
  1177. for (const auto& use : inst.uses()) {
  1178. const auto store = use.first;
  1179. if (store->opcode() == SpvOpFConvert) continue;
  1180. if (spvOpcodeIsDebug(store->opcode())) continue;
  1181. if (spvOpcodeIsDecoration(store->opcode())) continue;
  1182. if (store->opcode() != SpvOpStore) {
  1183. return vstate.diag(SPV_ERROR_INVALID_ID, &inst)
  1184. << "FPRoundingMode decoration can be applied only to the "
  1185. "Object operand of an OpStore.";
  1186. }
  1187. if (use.second != 2) {
  1188. return vstate.diag(SPV_ERROR_INVALID_ID, &inst)
  1189. << "FPRoundingMode decoration can be applied only to the "
  1190. "Object operand of an OpStore.";
  1191. }
  1192. const auto ptr_inst = vstate.FindDef(store->GetOperandAs<uint32_t>(0));
  1193. const auto ptr_type = vstate.FindDef(ptr_inst->GetOperandAs<uint32_t>(0));
  1194. const auto half_float_id = ptr_type->GetOperandAs<uint32_t>(2);
  1195. if (!vstate.IsFloatScalarOrVectorType(half_float_id) ||
  1196. vstate.GetBitWidth(half_float_id) != 16) {
  1197. return vstate.diag(SPV_ERROR_INVALID_ID, &inst)
  1198. << "FPRoundingMode decoration can be applied only to the "
  1199. "Object operand of an OpStore storing through a pointer "
  1200. "to "
  1201. "a 16-bit floating-point scalar or vector object.";
  1202. }
  1203. // Validates storage class of the pointer to the OpStore
  1204. const auto storage = ptr_type->GetOperandAs<uint32_t>(1);
  1205. if (storage != SpvStorageClassStorageBuffer &&
  1206. storage != SpvStorageClassUniform &&
  1207. storage != SpvStorageClassPushConstant &&
  1208. storage != SpvStorageClassInput && storage != SpvStorageClassOutput &&
  1209. storage != SpvStorageClassPhysicalStorageBufferEXT) {
  1210. return vstate.diag(SPV_ERROR_INVALID_ID, &inst)
  1211. << "FPRoundingMode decoration can be applied only to the "
  1212. "Object operand of an OpStore in the StorageBuffer, "
  1213. "PhysicalStorageBufferEXT, Uniform, PushConstant, Input, or "
  1214. "Output Storage Classes.";
  1215. }
  1216. }
  1217. return SPV_SUCCESS;
  1218. }
  1219. // Returns SPV_SUCCESS if validation rules are satisfied for the NonWritable
  1220. // decoration. Otherwise emits a diagnostic and returns something other than
  1221. // SPV_SUCCESS. The |inst| parameter is the object being decorated. This must
  1222. // be called after TypePass and AnnotateCheckDecorationsOfBuffers are called.
  1223. spv_result_t CheckNonWritableDecoration(ValidationState_t& vstate,
  1224. const Instruction& inst,
  1225. const Decoration& decoration) {
  1226. assert(inst.id() && "Parser ensures the target of the decoration has an ID");
  1227. if (decoration.struct_member_index() == Decoration::kInvalidMember) {
  1228. // The target must be a memory object declaration.
  1229. // First, it must be a variable or function parameter.
  1230. const auto opcode = inst.opcode();
  1231. const auto type_id = inst.type_id();
  1232. if (opcode != SpvOpVariable && opcode != SpvOpFunctionParameter) {
  1233. return vstate.diag(SPV_ERROR_INVALID_ID, &inst)
  1234. << "Target of NonWritable decoration must be a memory object "
  1235. "declaration (a variable or a function parameter)";
  1236. }
  1237. const auto var_storage_class = opcode == SpvOpVariable
  1238. ? inst.GetOperandAs<SpvStorageClass>(2)
  1239. : SpvStorageClassMax;
  1240. if ((var_storage_class == SpvStorageClassFunction ||
  1241. var_storage_class == SpvStorageClassPrivate) &&
  1242. vstate.features().nonwritable_var_in_function_or_private) {
  1243. // New permitted feature in SPIR-V 1.4.
  1244. } else if (
  1245. // It may point to a UBO, SSBO, or storage image.
  1246. vstate.IsPointerToUniformBlock(type_id) ||
  1247. vstate.IsPointerToStorageBuffer(type_id) ||
  1248. vstate.IsPointerToStorageImage(type_id)) {
  1249. } else {
  1250. return vstate.diag(SPV_ERROR_INVALID_ID, &inst)
  1251. << "Target of NonWritable decoration is invalid: must point to a "
  1252. "storage image, uniform block, "
  1253. << (vstate.features().nonwritable_var_in_function_or_private
  1254. ? "storage buffer, or variable in Private or Function "
  1255. "storage class"
  1256. : "or storage buffer");
  1257. }
  1258. }
  1259. return SPV_SUCCESS;
  1260. }
  1261. // Returns SPV_SUCCESS if validation rules are satisfied for Uniform or
  1262. // UniformId decorations. Otherwise emits a diagnostic and returns something
  1263. // other than SPV_SUCCESS. Assumes each decoration on a group has been
  1264. // propagated down to the group members. The |inst| parameter is the object
  1265. // being decorated.
  1266. spv_result_t CheckUniformDecoration(ValidationState_t& vstate,
  1267. const Instruction& inst,
  1268. const Decoration& decoration) {
  1269. const char* const dec_name =
  1270. decoration.dec_type() == SpvDecorationUniform ? "Uniform" : "UniformId";
  1271. // Uniform or UniformId must decorate an "object"
  1272. // - has a result ID
  1273. // - is an instantiation of a non-void type. So it has a type ID, and that
  1274. // type is not void.
  1275. // We already know the result ID is non-zero.
  1276. if (inst.type_id() == 0) {
  1277. return vstate.diag(SPV_ERROR_INVALID_ID, &inst)
  1278. << dec_name << " decoration applied to a non-object";
  1279. }
  1280. if (Instruction* type_inst = vstate.FindDef(inst.type_id())) {
  1281. if (type_inst->opcode() == SpvOpTypeVoid) {
  1282. return vstate.diag(SPV_ERROR_INVALID_ID, &inst)
  1283. << dec_name << " decoration applied to a value with void type";
  1284. }
  1285. } else {
  1286. // We might never get here because this would have been rejected earlier in
  1287. // the flow.
  1288. return vstate.diag(SPV_ERROR_INVALID_ID, &inst)
  1289. << dec_name << " decoration applied to an object with invalid type";
  1290. }
  1291. // Use of Uniform with OpDecorate is checked elsewhere.
  1292. // Use of UniformId with OpDecorateId is checked elsewhere.
  1293. if (decoration.dec_type() == SpvDecorationUniformId) {
  1294. assert(decoration.params().size() == 1 &&
  1295. "Grammar ensures UniformId has one parameter");
  1296. // The scope id is an execution scope.
  1297. if (auto error =
  1298. ValidateExecutionScope(vstate, &inst, decoration.params()[0]))
  1299. return error;
  1300. }
  1301. return SPV_SUCCESS;
  1302. }
  1303. // Returns SPV_SUCCESS if validation rules are satisfied for NoSignedWrap or
  1304. // NoUnsignedWrap decorations. Otherwise emits a diagnostic and returns
  1305. // something other than SPV_SUCCESS. Assumes each decoration on a group has been
  1306. // propagated down to the group members.
  1307. spv_result_t CheckIntegerWrapDecoration(ValidationState_t& vstate,
  1308. const Instruction& inst,
  1309. const Decoration& decoration) {
  1310. switch (inst.opcode()) {
  1311. case SpvOpIAdd:
  1312. case SpvOpISub:
  1313. case SpvOpIMul:
  1314. case SpvOpShiftLeftLogical:
  1315. case SpvOpSNegate:
  1316. return SPV_SUCCESS;
  1317. case SpvOpExtInst:
  1318. // TODO(dneto): Only certain extended instructions allow these
  1319. // decorations. For now allow anything.
  1320. return SPV_SUCCESS;
  1321. default:
  1322. break;
  1323. }
  1324. return vstate.diag(SPV_ERROR_INVALID_ID, &inst)
  1325. << (decoration.dec_type() == SpvDecorationNoSignedWrap
  1326. ? "NoSignedWrap"
  1327. : "NoUnsignedWrap")
  1328. << " decoration may not be applied to "
  1329. << spvOpcodeString(inst.opcode());
  1330. }
  1331. // Returns SPV_SUCCESS if validation rules are satisfied for the Component
  1332. // decoration. Otherwise emits a diagnostic and returns something other than
  1333. // SPV_SUCCESS.
  1334. spv_result_t CheckComponentDecoration(ValidationState_t& vstate,
  1335. const Instruction& inst,
  1336. const Decoration& decoration) {
  1337. assert(inst.id() && "Parser ensures the target of the decoration has an ID");
  1338. uint32_t type_id;
  1339. if (decoration.struct_member_index() == Decoration::kInvalidMember) {
  1340. // The target must be a memory object declaration.
  1341. const auto opcode = inst.opcode();
  1342. if (opcode != SpvOpVariable && opcode != SpvOpFunctionParameter) {
  1343. return vstate.diag(SPV_ERROR_INVALID_ID, &inst)
  1344. << "Target of Component decoration must be a memory object "
  1345. "declaration (a variable or a function parameter)";
  1346. }
  1347. // Only valid for the Input and Output Storage Classes.
  1348. const auto storage_class = opcode == SpvOpVariable
  1349. ? inst.GetOperandAs<SpvStorageClass>(2)
  1350. : SpvStorageClassMax;
  1351. if (storage_class != SpvStorageClassInput &&
  1352. storage_class != SpvStorageClassOutput &&
  1353. storage_class != SpvStorageClassMax) {
  1354. return vstate.diag(SPV_ERROR_INVALID_ID, &inst)
  1355. << "Target of Component decoration is invalid: must point to a "
  1356. "Storage Class of Input(1) or Output(3). Found Storage "
  1357. "Class "
  1358. << storage_class;
  1359. }
  1360. type_id = inst.type_id();
  1361. if (vstate.IsPointerType(type_id)) {
  1362. const auto pointer = vstate.FindDef(type_id);
  1363. type_id = pointer->GetOperandAs<uint32_t>(2);
  1364. }
  1365. } else {
  1366. if (inst.opcode() != SpvOpTypeStruct) {
  1367. return vstate.diag(SPV_ERROR_INVALID_DATA, &inst)
  1368. << "Attempted to get underlying data type via member index for "
  1369. "non-struct type.";
  1370. }
  1371. type_id = inst.word(decoration.struct_member_index() + 2);
  1372. }
  1373. if (spvIsVulkanEnv(vstate.context()->target_env)) {
  1374. // Strip the array, if present.
  1375. if (vstate.GetIdOpcode(type_id) == SpvOpTypeArray) {
  1376. type_id = vstate.FindDef(type_id)->word(2u);
  1377. }
  1378. if (!vstate.IsIntScalarOrVectorType(type_id) &&
  1379. !vstate.IsFloatScalarOrVectorType(type_id)) {
  1380. return vstate.diag(SPV_ERROR_INVALID_ID, &inst)
  1381. << "Component decoration specified for type "
  1382. << vstate.getIdName(type_id) << " that is not a scalar or vector";
  1383. }
  1384. // For 16-, and 32-bit types, it is invalid if this sequence of components
  1385. // gets larger than 3.
  1386. const auto bit_width = vstate.GetBitWidth(type_id);
  1387. if (bit_width == 16 || bit_width == 32) {
  1388. assert(decoration.params().size() == 1 &&
  1389. "Grammar ensures Component has one parameter");
  1390. const auto component = decoration.params()[0];
  1391. const auto last_component = component + vstate.GetDimension(type_id) - 1;
  1392. if (last_component > 3) {
  1393. return vstate.diag(SPV_ERROR_INVALID_ID, &inst)
  1394. << "Sequence of components starting with " << component
  1395. << " and ending with " << last_component
  1396. << " gets larger than 3";
  1397. }
  1398. }
  1399. }
  1400. return SPV_SUCCESS;
  1401. }
  1402. #define PASS_OR_BAIL_AT_LINE(X, LINE) \
  1403. { \
  1404. spv_result_t e##LINE = (X); \
  1405. if (e##LINE != SPV_SUCCESS) return e##LINE; \
  1406. }
  1407. #define PASS_OR_BAIL(X) PASS_OR_BAIL_AT_LINE(X, __LINE__)
  1408. // Check rules for decorations where we start from the decoration rather
  1409. // than the decorated object. Assumes each decoration on a group have been
  1410. // propagated down to the group members.
  1411. spv_result_t CheckDecorationsFromDecoration(ValidationState_t& vstate) {
  1412. // Some rules are only checked for shaders.
  1413. const bool is_shader = vstate.HasCapability(SpvCapabilityShader);
  1414. for (const auto& kv : vstate.id_decorations()) {
  1415. const uint32_t id = kv.first;
  1416. const auto& decorations = kv.second;
  1417. if (decorations.empty()) continue;
  1418. const Instruction* inst = vstate.FindDef(id);
  1419. assert(inst);
  1420. // We assume the decorations applied to a decoration group have already
  1421. // been propagated down to the group members.
  1422. if (inst->opcode() == SpvOpDecorationGroup) continue;
  1423. for (const auto& decoration : decorations) {
  1424. switch (decoration.dec_type()) {
  1425. case SpvDecorationComponent:
  1426. PASS_OR_BAIL(CheckComponentDecoration(vstate, *inst, decoration));
  1427. break;
  1428. case SpvDecorationFPRoundingMode:
  1429. if (is_shader)
  1430. PASS_OR_BAIL(CheckFPRoundingModeForShaders(vstate, *inst));
  1431. break;
  1432. case SpvDecorationNonWritable:
  1433. PASS_OR_BAIL(CheckNonWritableDecoration(vstate, *inst, decoration));
  1434. break;
  1435. case SpvDecorationUniform:
  1436. case SpvDecorationUniformId:
  1437. PASS_OR_BAIL(CheckUniformDecoration(vstate, *inst, decoration));
  1438. break;
  1439. case SpvDecorationNoSignedWrap:
  1440. case SpvDecorationNoUnsignedWrap:
  1441. PASS_OR_BAIL(CheckIntegerWrapDecoration(vstate, *inst, decoration));
  1442. break;
  1443. default:
  1444. break;
  1445. }
  1446. }
  1447. }
  1448. return SPV_SUCCESS;
  1449. }
  1450. } // namespace
  1451. spv_result_t ValidateDecorations(ValidationState_t& vstate) {
  1452. if (auto error = CheckImportedVariableInitialization(vstate)) return error;
  1453. if (auto error = CheckDecorationsOfEntryPoints(vstate)) return error;
  1454. if (auto error = CheckDecorationsOfBuffers(vstate)) return error;
  1455. if (auto error = CheckDecorationsCompatibility(vstate)) return error;
  1456. if (auto error = CheckLinkageAttrOfFunctions(vstate)) return error;
  1457. if (auto error = CheckVulkanMemoryModelDeprecatedDecorations(vstate))
  1458. return error;
  1459. if (auto error = CheckDecorationsFromDecoration(vstate)) return error;
  1460. return SPV_SUCCESS;
  1461. }
  1462. } // namespace val
  1463. } // namespace spvtools