validate_memory.cpp 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903
  1. // Copyright (c) 2018 Google LLC.
  2. // Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights
  3. // reserved.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. #include <algorithm>
  17. #include <string>
  18. #include <vector>
  19. #include "source/opcode.h"
  20. #include "source/spirv_target_env.h"
  21. #include "source/val/instruction.h"
  22. #include "source/val/validate.h"
  23. #include "source/val/validate_scopes.h"
  24. #include "source/val/validation_state.h"
  25. namespace spvtools {
  26. namespace val {
  27. namespace {
  28. bool AreLayoutCompatibleStructs(ValidationState_t&, const Instruction*,
  29. const Instruction*);
  30. bool HaveLayoutCompatibleMembers(ValidationState_t&, const Instruction*,
  31. const Instruction*);
  32. bool HaveSameLayoutDecorations(ValidationState_t&, const Instruction*,
  33. const Instruction*);
  34. bool HasConflictingMemberOffsets(const std::set<Decoration>&,
  35. const std::set<Decoration>&);
  36. bool IsAllowedTypeOrArrayOfSame(ValidationState_t& _, const Instruction* type,
  37. std::initializer_list<spv::Op> allowed) {
  38. if (std::find(allowed.begin(), allowed.end(), type->opcode()) !=
  39. allowed.end()) {
  40. return true;
  41. }
  42. if (type->opcode() == spv::Op::OpTypeArray ||
  43. type->opcode() == spv::Op::OpTypeRuntimeArray) {
  44. auto elem_type = _.FindDef(type->word(2));
  45. return std::find(allowed.begin(), allowed.end(), elem_type->opcode()) !=
  46. allowed.end();
  47. }
  48. return false;
  49. }
  50. // Returns true if the two instructions represent structs that, as far as the
  51. // validator can tell, have the exact same data layout.
  52. bool AreLayoutCompatibleStructs(ValidationState_t& _, const Instruction* type1,
  53. const Instruction* type2) {
  54. if (type1->opcode() != spv::Op::OpTypeStruct) {
  55. return false;
  56. }
  57. if (type2->opcode() != spv::Op::OpTypeStruct) {
  58. return false;
  59. }
  60. if (!HaveLayoutCompatibleMembers(_, type1, type2)) return false;
  61. return HaveSameLayoutDecorations(_, type1, type2);
  62. }
  63. // Returns true if the operands to the OpTypeStruct instruction defining the
  64. // types are the same or are layout compatible types. |type1| and |type2| must
  65. // be OpTypeStruct instructions.
  66. bool HaveLayoutCompatibleMembers(ValidationState_t& _, const Instruction* type1,
  67. const Instruction* type2) {
  68. assert(type1->opcode() == spv::Op::OpTypeStruct &&
  69. "type1 must be an OpTypeStruct instruction.");
  70. assert(type2->opcode() == spv::Op::OpTypeStruct &&
  71. "type2 must be an OpTypeStruct instruction.");
  72. const auto& type1_operands = type1->operands();
  73. const auto& type2_operands = type2->operands();
  74. if (type1_operands.size() != type2_operands.size()) {
  75. return false;
  76. }
  77. for (size_t operand = 2; operand < type1_operands.size(); ++operand) {
  78. if (type1->word(operand) != type2->word(operand)) {
  79. auto def1 = _.FindDef(type1->word(operand));
  80. auto def2 = _.FindDef(type2->word(operand));
  81. if (!AreLayoutCompatibleStructs(_, def1, def2)) {
  82. return false;
  83. }
  84. }
  85. }
  86. return true;
  87. }
  88. // Returns true if all decorations that affect the data layout of the struct
  89. // (like Offset), are the same for the two types. |type1| and |type2| must be
  90. // OpTypeStruct instructions.
  91. bool HaveSameLayoutDecorations(ValidationState_t& _, const Instruction* type1,
  92. const Instruction* type2) {
  93. assert(type1->opcode() == spv::Op::OpTypeStruct &&
  94. "type1 must be an OpTypeStruct instruction.");
  95. assert(type2->opcode() == spv::Op::OpTypeStruct &&
  96. "type2 must be an OpTypeStruct instruction.");
  97. const std::set<Decoration>& type1_decorations = _.id_decorations(type1->id());
  98. const std::set<Decoration>& type2_decorations = _.id_decorations(type2->id());
  99. // TODO: Will have to add other check for arrays an matricies if we want to
  100. // handle them.
  101. if (HasConflictingMemberOffsets(type1_decorations, type2_decorations)) {
  102. return false;
  103. }
  104. return true;
  105. }
  106. bool HasConflictingMemberOffsets(
  107. const std::set<Decoration>& type1_decorations,
  108. const std::set<Decoration>& type2_decorations) {
  109. {
  110. // We are interested in conflicting decoration. If a decoration is in one
  111. // list but not the other, then we will assume the code is correct. We are
  112. // looking for things we know to be wrong.
  113. //
  114. // We do not have to traverse type2_decoration because, after traversing
  115. // type1_decorations, anything new will not be found in
  116. // type1_decoration. Therefore, it cannot lead to a conflict.
  117. for (const Decoration& decoration : type1_decorations) {
  118. switch (decoration.dec_type()) {
  119. case spv::Decoration::Offset: {
  120. // Since these affect the layout of the struct, they must be present
  121. // in both structs.
  122. auto compare = [&decoration](const Decoration& rhs) {
  123. if (rhs.dec_type() != spv::Decoration::Offset) return false;
  124. return decoration.struct_member_index() ==
  125. rhs.struct_member_index();
  126. };
  127. auto i = std::find_if(type2_decorations.begin(),
  128. type2_decorations.end(), compare);
  129. if (i != type2_decorations.end() &&
  130. decoration.params().front() != i->params().front()) {
  131. return true;
  132. }
  133. } break;
  134. default:
  135. // This decoration does not affect the layout of the structure, so
  136. // just moving on.
  137. break;
  138. }
  139. }
  140. }
  141. return false;
  142. }
  143. // If |skip_builtin| is true, returns true if |storage| contains bool within
  144. // it and no storage that contains the bool is builtin.
  145. // If |skip_builtin| is false, returns true if |storage| contains bool within
  146. // it.
  147. bool ContainsInvalidBool(ValidationState_t& _, const Instruction* storage,
  148. bool skip_builtin) {
  149. if (skip_builtin) {
  150. for (const Decoration& decoration : _.id_decorations(storage->id())) {
  151. if (decoration.dec_type() == spv::Decoration::BuiltIn) return false;
  152. }
  153. }
  154. const size_t elem_type_index = 1;
  155. uint32_t elem_type_id;
  156. Instruction* elem_type;
  157. switch (storage->opcode()) {
  158. case spv::Op::OpTypeBool:
  159. return true;
  160. case spv::Op::OpTypeVector:
  161. case spv::Op::OpTypeMatrix:
  162. case spv::Op::OpTypeArray:
  163. case spv::Op::OpTypeRuntimeArray:
  164. elem_type_id = storage->GetOperandAs<uint32_t>(elem_type_index);
  165. elem_type = _.FindDef(elem_type_id);
  166. return ContainsInvalidBool(_, elem_type, skip_builtin);
  167. case spv::Op::OpTypeStruct:
  168. for (size_t member_type_index = 1;
  169. member_type_index < storage->operands().size();
  170. ++member_type_index) {
  171. auto member_type_id =
  172. storage->GetOperandAs<uint32_t>(member_type_index);
  173. auto member_type = _.FindDef(member_type_id);
  174. if (ContainsInvalidBool(_, member_type, skip_builtin)) return true;
  175. }
  176. default:
  177. break;
  178. }
  179. return false;
  180. }
  181. bool ContainsCooperativeMatrix(ValidationState_t& _,
  182. const Instruction* storage) {
  183. const size_t elem_type_index = 1;
  184. uint32_t elem_type_id;
  185. Instruction* elem_type;
  186. switch (storage->opcode()) {
  187. case spv::Op::OpTypeCooperativeMatrixNV:
  188. case spv::Op::OpTypeCooperativeMatrixKHR:
  189. return true;
  190. case spv::Op::OpTypeArray:
  191. case spv::Op::OpTypeRuntimeArray:
  192. elem_type_id = storage->GetOperandAs<uint32_t>(elem_type_index);
  193. elem_type = _.FindDef(elem_type_id);
  194. return ContainsCooperativeMatrix(_, elem_type);
  195. case spv::Op::OpTypeStruct:
  196. for (size_t member_type_index = 1;
  197. member_type_index < storage->operands().size();
  198. ++member_type_index) {
  199. auto member_type_id =
  200. storage->GetOperandAs<uint32_t>(member_type_index);
  201. auto member_type = _.FindDef(member_type_id);
  202. if (ContainsCooperativeMatrix(_, member_type)) return true;
  203. }
  204. break;
  205. default:
  206. break;
  207. }
  208. return false;
  209. }
  210. std::pair<spv::StorageClass, spv::StorageClass> GetStorageClass(
  211. ValidationState_t& _, const Instruction* inst) {
  212. spv::StorageClass dst_sc = spv::StorageClass::Max;
  213. spv::StorageClass src_sc = spv::StorageClass::Max;
  214. switch (inst->opcode()) {
  215. case spv::Op::OpCooperativeMatrixLoadNV:
  216. case spv::Op::OpCooperativeMatrixLoadKHR:
  217. case spv::Op::OpLoad: {
  218. auto load_pointer = _.FindDef(inst->GetOperandAs<uint32_t>(2));
  219. auto load_pointer_type = _.FindDef(load_pointer->type_id());
  220. dst_sc = load_pointer_type->GetOperandAs<spv::StorageClass>(1);
  221. break;
  222. }
  223. case spv::Op::OpCooperativeMatrixStoreNV:
  224. case spv::Op::OpCooperativeMatrixStoreKHR:
  225. case spv::Op::OpStore: {
  226. auto store_pointer = _.FindDef(inst->GetOperandAs<uint32_t>(0));
  227. auto store_pointer_type = _.FindDef(store_pointer->type_id());
  228. dst_sc = store_pointer_type->GetOperandAs<spv::StorageClass>(1);
  229. break;
  230. }
  231. case spv::Op::OpCopyMemory:
  232. case spv::Op::OpCopyMemorySized: {
  233. auto dst = _.FindDef(inst->GetOperandAs<uint32_t>(0));
  234. auto dst_type = _.FindDef(dst->type_id());
  235. dst_sc = dst_type->GetOperandAs<spv::StorageClass>(1);
  236. auto src = _.FindDef(inst->GetOperandAs<uint32_t>(1));
  237. auto src_type = _.FindDef(src->type_id());
  238. src_sc = src_type->GetOperandAs<spv::StorageClass>(1);
  239. break;
  240. }
  241. default:
  242. break;
  243. }
  244. return std::make_pair(dst_sc, src_sc);
  245. }
  246. // Returns the number of instruction words taken up by a memory access
  247. // argument and its implied operands.
  248. int MemoryAccessNumWords(uint32_t mask) {
  249. int result = 1; // Count the mask
  250. if (mask & uint32_t(spv::MemoryAccessMask::Aligned)) ++result;
  251. if (mask & uint32_t(spv::MemoryAccessMask::MakePointerAvailableKHR)) ++result;
  252. if (mask & uint32_t(spv::MemoryAccessMask::MakePointerVisibleKHR)) ++result;
  253. return result;
  254. }
  255. // Returns the scope ID operand for MakeAvailable memory access with mask
  256. // at the given operand index.
  257. // This function is only called for OpLoad, OpStore, OpCopyMemory and
  258. // OpCopyMemorySized, OpCooperativeMatrixLoadNV, and
  259. // OpCooperativeMatrixStoreNV.
  260. uint32_t GetMakeAvailableScope(const Instruction* inst, uint32_t mask,
  261. uint32_t mask_index) {
  262. assert(mask & uint32_t(spv::MemoryAccessMask::MakePointerAvailableKHR));
  263. uint32_t this_bit = uint32_t(spv::MemoryAccessMask::MakePointerAvailableKHR);
  264. uint32_t index =
  265. mask_index - 1 + MemoryAccessNumWords(mask & (this_bit | (this_bit - 1)));
  266. return inst->GetOperandAs<uint32_t>(index);
  267. }
  268. // This function is only called for OpLoad, OpStore, OpCopyMemory,
  269. // OpCopyMemorySized, OpCooperativeMatrixLoadNV, and
  270. // OpCooperativeMatrixStoreNV.
  271. uint32_t GetMakeVisibleScope(const Instruction* inst, uint32_t mask,
  272. uint32_t mask_index) {
  273. assert(mask & uint32_t(spv::MemoryAccessMask::MakePointerVisibleKHR));
  274. uint32_t this_bit = uint32_t(spv::MemoryAccessMask::MakePointerVisibleKHR);
  275. uint32_t index =
  276. mask_index - 1 + MemoryAccessNumWords(mask & (this_bit | (this_bit - 1)));
  277. return inst->GetOperandAs<uint32_t>(index);
  278. }
  279. bool DoesStructContainRTA(const ValidationState_t& _, const Instruction* inst) {
  280. for (size_t member_index = 1; member_index < inst->operands().size();
  281. ++member_index) {
  282. const auto member_id = inst->GetOperandAs<uint32_t>(member_index);
  283. const auto member_type = _.FindDef(member_id);
  284. if (member_type->opcode() == spv::Op::OpTypeRuntimeArray) return true;
  285. }
  286. return false;
  287. }
  288. spv_result_t CheckMemoryAccess(ValidationState_t& _, const Instruction* inst,
  289. uint32_t index) {
  290. spv::StorageClass dst_sc, src_sc;
  291. std::tie(dst_sc, src_sc) = GetStorageClass(_, inst);
  292. if (inst->operands().size() <= index) {
  293. // Cases where lack of some operand is invalid
  294. if (src_sc == spv::StorageClass::PhysicalStorageBuffer ||
  295. dst_sc == spv::StorageClass::PhysicalStorageBuffer) {
  296. return _.diag(SPV_ERROR_INVALID_ID, inst)
  297. << _.VkErrorID(4708)
  298. << "Memory accesses with PhysicalStorageBuffer must use Aligned.";
  299. }
  300. return SPV_SUCCESS;
  301. }
  302. const uint32_t mask = inst->GetOperandAs<uint32_t>(index);
  303. if (mask & uint32_t(spv::MemoryAccessMask::MakePointerAvailableKHR)) {
  304. if (inst->opcode() == spv::Op::OpLoad ||
  305. inst->opcode() == spv::Op::OpCooperativeMatrixLoadNV ||
  306. inst->opcode() == spv::Op::OpCooperativeMatrixLoadKHR) {
  307. return _.diag(SPV_ERROR_INVALID_ID, inst)
  308. << "MakePointerAvailableKHR cannot be used with OpLoad.";
  309. }
  310. if (!(mask & uint32_t(spv::MemoryAccessMask::NonPrivatePointerKHR))) {
  311. return _.diag(SPV_ERROR_INVALID_ID, inst)
  312. << "NonPrivatePointerKHR must be specified if "
  313. "MakePointerAvailableKHR is specified.";
  314. }
  315. // Check the associated scope for MakeAvailableKHR.
  316. const auto available_scope = GetMakeAvailableScope(inst, mask, index);
  317. if (auto error = ValidateMemoryScope(_, inst, available_scope))
  318. return error;
  319. }
  320. if (mask & uint32_t(spv::MemoryAccessMask::MakePointerVisibleKHR)) {
  321. if (inst->opcode() == spv::Op::OpStore ||
  322. inst->opcode() == spv::Op::OpCooperativeMatrixStoreNV) {
  323. return _.diag(SPV_ERROR_INVALID_ID, inst)
  324. << "MakePointerVisibleKHR cannot be used with OpStore.";
  325. }
  326. if (!(mask & uint32_t(spv::MemoryAccessMask::NonPrivatePointerKHR))) {
  327. return _.diag(SPV_ERROR_INVALID_ID, inst)
  328. << "NonPrivatePointerKHR must be specified if "
  329. << "MakePointerVisibleKHR is specified.";
  330. }
  331. // Check the associated scope for MakeVisibleKHR.
  332. const auto visible_scope = GetMakeVisibleScope(inst, mask, index);
  333. if (auto error = ValidateMemoryScope(_, inst, visible_scope)) return error;
  334. }
  335. if (mask & uint32_t(spv::MemoryAccessMask::NonPrivatePointerKHR)) {
  336. if (dst_sc != spv::StorageClass::Uniform &&
  337. dst_sc != spv::StorageClass::Workgroup &&
  338. dst_sc != spv::StorageClass::CrossWorkgroup &&
  339. dst_sc != spv::StorageClass::Generic &&
  340. dst_sc != spv::StorageClass::Image &&
  341. dst_sc != spv::StorageClass::StorageBuffer &&
  342. dst_sc != spv::StorageClass::PhysicalStorageBuffer) {
  343. return _.diag(SPV_ERROR_INVALID_ID, inst)
  344. << "NonPrivatePointerKHR requires a pointer in Uniform, "
  345. << "Workgroup, CrossWorkgroup, Generic, Image or StorageBuffer "
  346. << "storage classes.";
  347. }
  348. if (src_sc != spv::StorageClass::Max &&
  349. src_sc != spv::StorageClass::Uniform &&
  350. src_sc != spv::StorageClass::Workgroup &&
  351. src_sc != spv::StorageClass::CrossWorkgroup &&
  352. src_sc != spv::StorageClass::Generic &&
  353. src_sc != spv::StorageClass::Image &&
  354. src_sc != spv::StorageClass::StorageBuffer &&
  355. src_sc != spv::StorageClass::PhysicalStorageBuffer) {
  356. return _.diag(SPV_ERROR_INVALID_ID, inst)
  357. << "NonPrivatePointerKHR requires a pointer in Uniform, "
  358. << "Workgroup, CrossWorkgroup, Generic, Image or StorageBuffer "
  359. << "storage classes.";
  360. }
  361. }
  362. if (!(mask & uint32_t(spv::MemoryAccessMask::Aligned))) {
  363. if (src_sc == spv::StorageClass::PhysicalStorageBuffer ||
  364. dst_sc == spv::StorageClass::PhysicalStorageBuffer) {
  365. return _.diag(SPV_ERROR_INVALID_ID, inst)
  366. << _.VkErrorID(4708)
  367. << "Memory accesses with PhysicalStorageBuffer must use Aligned.";
  368. }
  369. }
  370. return SPV_SUCCESS;
  371. }
  372. spv_result_t ValidateVariable(ValidationState_t& _, const Instruction* inst) {
  373. auto result_type = _.FindDef(inst->type_id());
  374. if (!result_type || result_type->opcode() != spv::Op::OpTypePointer) {
  375. return _.diag(SPV_ERROR_INVALID_ID, inst)
  376. << "OpVariable Result Type <id> " << _.getIdName(inst->type_id())
  377. << " is not a pointer type.";
  378. }
  379. const auto type_index = 2;
  380. const auto value_id = result_type->GetOperandAs<uint32_t>(type_index);
  381. auto value_type = _.FindDef(value_id);
  382. const auto initializer_index = 3;
  383. const auto storage_class_index = 2;
  384. if (initializer_index < inst->operands().size()) {
  385. const auto initializer_id = inst->GetOperandAs<uint32_t>(initializer_index);
  386. const auto initializer = _.FindDef(initializer_id);
  387. const auto is_module_scope_var =
  388. initializer && (initializer->opcode() == spv::Op::OpVariable) &&
  389. (initializer->GetOperandAs<spv::StorageClass>(storage_class_index) !=
  390. spv::StorageClass::Function);
  391. const auto is_constant =
  392. initializer && spvOpcodeIsConstant(initializer->opcode());
  393. if (!initializer || !(is_constant || is_module_scope_var)) {
  394. return _.diag(SPV_ERROR_INVALID_ID, inst)
  395. << "OpVariable Initializer <id> " << _.getIdName(initializer_id)
  396. << " is not a constant or module-scope variable.";
  397. }
  398. if (initializer->type_id() != value_id) {
  399. return _.diag(SPV_ERROR_INVALID_ID, inst)
  400. << "Initializer type must match the type pointed to by the Result "
  401. "Type";
  402. }
  403. }
  404. auto storage_class =
  405. inst->GetOperandAs<spv::StorageClass>(storage_class_index);
  406. if (storage_class != spv::StorageClass::Workgroup &&
  407. storage_class != spv::StorageClass::CrossWorkgroup &&
  408. storage_class != spv::StorageClass::Private &&
  409. storage_class != spv::StorageClass::Function &&
  410. storage_class != spv::StorageClass::UniformConstant &&
  411. storage_class != spv::StorageClass::RayPayloadKHR &&
  412. storage_class != spv::StorageClass::IncomingRayPayloadKHR &&
  413. storage_class != spv::StorageClass::HitAttributeKHR &&
  414. storage_class != spv::StorageClass::CallableDataKHR &&
  415. storage_class != spv::StorageClass::IncomingCallableDataKHR &&
  416. storage_class != spv::StorageClass::TaskPayloadWorkgroupEXT &&
  417. storage_class != spv::StorageClass::HitObjectAttributeNV) {
  418. bool storage_input_or_output = storage_class == spv::StorageClass::Input ||
  419. storage_class == spv::StorageClass::Output;
  420. bool builtin = false;
  421. if (storage_input_or_output) {
  422. for (const Decoration& decoration : _.id_decorations(inst->id())) {
  423. if (decoration.dec_type() == spv::Decoration::BuiltIn) {
  424. builtin = true;
  425. break;
  426. }
  427. }
  428. }
  429. if (!builtin &&
  430. ContainsInvalidBool(_, value_type, storage_input_or_output)) {
  431. if (storage_input_or_output) {
  432. return _.diag(SPV_ERROR_INVALID_ID, inst)
  433. << _.VkErrorID(7290)
  434. << "If OpTypeBool is stored in conjunction with OpVariable "
  435. "using Input or Output Storage Classes it requires a BuiltIn "
  436. "decoration";
  437. } else {
  438. return _.diag(SPV_ERROR_INVALID_ID, inst)
  439. << "If OpTypeBool is stored in conjunction with OpVariable, it "
  440. "can only be used with non-externally visible shader Storage "
  441. "Classes: Workgroup, CrossWorkgroup, Private, Function, "
  442. "Input, Output, RayPayloadKHR, IncomingRayPayloadKHR, "
  443. "HitAttributeKHR, CallableDataKHR, "
  444. "IncomingCallableDataKHR, or UniformConstant";
  445. }
  446. }
  447. }
  448. if (!_.IsValidStorageClass(storage_class)) {
  449. return _.diag(SPV_ERROR_INVALID_BINARY, inst)
  450. << _.VkErrorID(4643)
  451. << "Invalid storage class for target environment";
  452. }
  453. if (storage_class == spv::StorageClass::Generic) {
  454. return _.diag(SPV_ERROR_INVALID_BINARY, inst)
  455. << "OpVariable storage class cannot be Generic";
  456. }
  457. if (inst->function() && storage_class != spv::StorageClass::Function) {
  458. return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
  459. << "Variables must have a function[7] storage class inside"
  460. " of a function";
  461. }
  462. if (!inst->function() && storage_class == spv::StorageClass::Function) {
  463. return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
  464. << "Variables can not have a function[7] storage class "
  465. "outside of a function";
  466. }
  467. // SPIR-V 3.32.8: Check that pointer type and variable type have the same
  468. // storage class.
  469. const auto result_storage_class_index = 1;
  470. const auto result_storage_class =
  471. result_type->GetOperandAs<spv::StorageClass>(result_storage_class_index);
  472. if (storage_class != result_storage_class) {
  473. return _.diag(SPV_ERROR_INVALID_ID, inst)
  474. << "From SPIR-V spec, section 3.32.8 on OpVariable:\n"
  475. << "Its Storage Class operand must be the same as the Storage Class "
  476. << "operand of the result type.";
  477. }
  478. // Variable pointer related restrictions.
  479. const auto pointee = _.FindDef(result_type->word(3));
  480. if (_.addressing_model() == spv::AddressingModel::Logical &&
  481. !_.options()->relax_logical_pointer) {
  482. // VariablePointersStorageBuffer is implied by VariablePointers.
  483. if (pointee->opcode() == spv::Op::OpTypePointer) {
  484. if (!_.HasCapability(spv::Capability::VariablePointersStorageBuffer)) {
  485. return _.diag(SPV_ERROR_INVALID_ID, inst)
  486. << "In Logical addressing, variables may not allocate a pointer "
  487. << "type";
  488. } else if (storage_class != spv::StorageClass::Function &&
  489. storage_class != spv::StorageClass::Private) {
  490. return _.diag(SPV_ERROR_INVALID_ID, inst)
  491. << "In Logical addressing with variable pointers, variables "
  492. << "that allocate pointers must be in Function or Private "
  493. << "storage classes";
  494. }
  495. }
  496. }
  497. if (spvIsVulkanEnv(_.context()->target_env)) {
  498. // Vulkan Push Constant Interface section: Check type of PushConstant
  499. // variables.
  500. if (storage_class == spv::StorageClass::PushConstant) {
  501. if (pointee->opcode() != spv::Op::OpTypeStruct) {
  502. return _.diag(SPV_ERROR_INVALID_ID, inst)
  503. << _.VkErrorID(6808) << "PushConstant OpVariable <id> "
  504. << _.getIdName(inst->id()) << " has illegal type.\n"
  505. << "From Vulkan spec, Push Constant Interface section:\n"
  506. << "Such variables must be typed as OpTypeStruct";
  507. }
  508. }
  509. // Vulkan Descriptor Set Interface: Check type of UniformConstant and
  510. // Uniform variables.
  511. if (storage_class == spv::StorageClass::UniformConstant) {
  512. if (!IsAllowedTypeOrArrayOfSame(
  513. _, pointee,
  514. {spv::Op::OpTypeImage, spv::Op::OpTypeSampler,
  515. spv::Op::OpTypeSampledImage,
  516. spv::Op::OpTypeAccelerationStructureKHR})) {
  517. return _.diag(SPV_ERROR_INVALID_ID, inst)
  518. << _.VkErrorID(4655) << "UniformConstant OpVariable <id> "
  519. << _.getIdName(inst->id()) << " has illegal type.\n"
  520. << "Variables identified with the UniformConstant storage class "
  521. << "are used only as handles to refer to opaque resources. Such "
  522. << "variables must be typed as OpTypeImage, OpTypeSampler, "
  523. << "OpTypeSampledImage, OpTypeAccelerationStructureKHR, "
  524. << "or an array of one of these types.";
  525. }
  526. }
  527. if (storage_class == spv::StorageClass::Uniform) {
  528. if (!IsAllowedTypeOrArrayOfSame(_, pointee, {spv::Op::OpTypeStruct})) {
  529. return _.diag(SPV_ERROR_INVALID_ID, inst)
  530. << _.VkErrorID(6807) << "Uniform OpVariable <id> "
  531. << _.getIdName(inst->id()) << " has illegal type.\n"
  532. << "From Vulkan spec:\n"
  533. << "Variables identified with the Uniform storage class are "
  534. << "used to access transparent buffer backed resources. Such "
  535. << "variables must be typed as OpTypeStruct, or an array of "
  536. << "this type";
  537. }
  538. }
  539. if (storage_class == spv::StorageClass::StorageBuffer) {
  540. if (!IsAllowedTypeOrArrayOfSame(_, pointee, {spv::Op::OpTypeStruct})) {
  541. return _.diag(SPV_ERROR_INVALID_ID, inst)
  542. << _.VkErrorID(6807) << "StorageBuffer OpVariable <id> "
  543. << _.getIdName(inst->id()) << " has illegal type.\n"
  544. << "From Vulkan spec:\n"
  545. << "Variables identified with the StorageBuffer storage class "
  546. "are used to access transparent buffer backed resources. "
  547. "Such variables must be typed as OpTypeStruct, or an array "
  548. "of this type";
  549. }
  550. }
  551. // Check for invalid use of Invariant
  552. if (storage_class != spv::StorageClass::Input &&
  553. storage_class != spv::StorageClass::Output) {
  554. if (_.HasDecoration(inst->id(), spv::Decoration::Invariant)) {
  555. return _.diag(SPV_ERROR_INVALID_ID, inst)
  556. << _.VkErrorID(4677)
  557. << "Variable decorated with Invariant must only be identified "
  558. "with the Input or Output storage class in Vulkan "
  559. "environment.";
  560. }
  561. // Need to check if only the members in a struct are decorated
  562. if (value_type && value_type->opcode() == spv::Op::OpTypeStruct) {
  563. if (_.HasDecoration(value_id, spv::Decoration::Invariant)) {
  564. return _.diag(SPV_ERROR_INVALID_ID, inst)
  565. << _.VkErrorID(4677)
  566. << "Variable struct member decorated with Invariant must only "
  567. "be identified with the Input or Output storage class in "
  568. "Vulkan environment.";
  569. }
  570. }
  571. }
  572. // Initializers in Vulkan are only allowed in some storage clases
  573. if (inst->operands().size() > 3) {
  574. if (storage_class == spv::StorageClass::Workgroup) {
  575. auto init_id = inst->GetOperandAs<uint32_t>(3);
  576. auto init = _.FindDef(init_id);
  577. if (init->opcode() != spv::Op::OpConstantNull) {
  578. return _.diag(SPV_ERROR_INVALID_ID, inst)
  579. << _.VkErrorID(4734) << "OpVariable, <id> "
  580. << _.getIdName(inst->id())
  581. << ", initializers are limited to OpConstantNull in "
  582. "Workgroup "
  583. "storage class";
  584. }
  585. } else if (storage_class != spv::StorageClass::Output &&
  586. storage_class != spv::StorageClass::Private &&
  587. storage_class != spv::StorageClass::Function) {
  588. return _.diag(SPV_ERROR_INVALID_ID, inst)
  589. << _.VkErrorID(4651) << "OpVariable, <id> "
  590. << _.getIdName(inst->id())
  591. << ", has a disallowed initializer & storage class "
  592. << "combination.\n"
  593. << "From " << spvLogStringForEnv(_.context()->target_env)
  594. << " spec:\n"
  595. << "Variable declarations that include initializers must have "
  596. << "one of the following storage classes: Output, Private, "
  597. << "Function or Workgroup";
  598. }
  599. }
  600. }
  601. if (inst->operands().size() > 3) {
  602. if (storage_class == spv::StorageClass::TaskPayloadWorkgroupEXT) {
  603. return _.diag(SPV_ERROR_INVALID_ID, inst)
  604. << "OpVariable, <id> " << _.getIdName(inst->id())
  605. << ", initializer are not allowed for TaskPayloadWorkgroupEXT";
  606. }
  607. if (storage_class == spv::StorageClass::Input) {
  608. return _.diag(SPV_ERROR_INVALID_ID, inst)
  609. << "OpVariable, <id> " << _.getIdName(inst->id())
  610. << ", initializer are not allowed for Input";
  611. }
  612. if (storage_class == spv::StorageClass::HitObjectAttributeNV) {
  613. return _.diag(SPV_ERROR_INVALID_ID, inst)
  614. << "OpVariable, <id> " << _.getIdName(inst->id())
  615. << ", initializer are not allowed for HitObjectAttributeNV";
  616. }
  617. }
  618. if (storage_class == spv::StorageClass::PhysicalStorageBuffer) {
  619. return _.diag(SPV_ERROR_INVALID_ID, inst)
  620. << "PhysicalStorageBuffer must not be used with OpVariable.";
  621. }
  622. auto pointee_base = pointee;
  623. while (pointee_base->opcode() == spv::Op::OpTypeArray) {
  624. pointee_base = _.FindDef(pointee_base->GetOperandAs<uint32_t>(1u));
  625. }
  626. if (pointee_base->opcode() == spv::Op::OpTypePointer) {
  627. if (pointee_base->GetOperandAs<spv::StorageClass>(1u) ==
  628. spv::StorageClass::PhysicalStorageBuffer) {
  629. // check for AliasedPointer/RestrictPointer
  630. bool foundAliased =
  631. _.HasDecoration(inst->id(), spv::Decoration::AliasedPointer);
  632. bool foundRestrict =
  633. _.HasDecoration(inst->id(), spv::Decoration::RestrictPointer);
  634. if (!foundAliased && !foundRestrict) {
  635. return _.diag(SPV_ERROR_INVALID_ID, inst)
  636. << "OpVariable " << inst->id()
  637. << ": expected AliasedPointer or RestrictPointer for "
  638. << "PhysicalStorageBuffer pointer.";
  639. }
  640. if (foundAliased && foundRestrict) {
  641. return _.diag(SPV_ERROR_INVALID_ID, inst)
  642. << "OpVariable " << inst->id()
  643. << ": can't specify both AliasedPointer and "
  644. << "RestrictPointer for PhysicalStorageBuffer pointer.";
  645. }
  646. }
  647. }
  648. // Vulkan specific validation rules for OpTypeRuntimeArray
  649. if (spvIsVulkanEnv(_.context()->target_env)) {
  650. // OpTypeRuntimeArray should only ever be in a container like OpTypeStruct,
  651. // so should never appear as a bare variable.
  652. // Unless the module has the RuntimeDescriptorArrayEXT capability.
  653. if (value_type && value_type->opcode() == spv::Op::OpTypeRuntimeArray) {
  654. if (!_.HasCapability(spv::Capability::RuntimeDescriptorArrayEXT)) {
  655. return _.diag(SPV_ERROR_INVALID_ID, inst)
  656. << _.VkErrorID(4680) << "OpVariable, <id> "
  657. << _.getIdName(inst->id())
  658. << ", is attempting to create memory for an illegal type, "
  659. << "OpTypeRuntimeArray.\nFor Vulkan OpTypeRuntimeArray can only "
  660. << "appear as the final member of an OpTypeStruct, thus cannot "
  661. << "be instantiated via OpVariable";
  662. } else {
  663. // A bare variable OpTypeRuntimeArray is allowed in this context, but
  664. // still need to check the storage class.
  665. if (storage_class != spv::StorageClass::StorageBuffer &&
  666. storage_class != spv::StorageClass::Uniform &&
  667. storage_class != spv::StorageClass::UniformConstant) {
  668. return _.diag(SPV_ERROR_INVALID_ID, inst)
  669. << _.VkErrorID(4680)
  670. << "For Vulkan with RuntimeDescriptorArrayEXT, a variable "
  671. << "containing OpTypeRuntimeArray must have storage class of "
  672. << "StorageBuffer, Uniform, or UniformConstant.";
  673. }
  674. }
  675. }
  676. // If an OpStruct has an OpTypeRuntimeArray somewhere within it, then it
  677. // must either have the storage class StorageBuffer and be decorated
  678. // with Block, or it must be in the Uniform storage class and be decorated
  679. // as BufferBlock.
  680. if (value_type && value_type->opcode() == spv::Op::OpTypeStruct) {
  681. if (DoesStructContainRTA(_, value_type)) {
  682. if (storage_class == spv::StorageClass::StorageBuffer ||
  683. storage_class == spv::StorageClass::PhysicalStorageBuffer) {
  684. if (!_.HasDecoration(value_id, spv::Decoration::Block)) {
  685. return _.diag(SPV_ERROR_INVALID_ID, inst)
  686. << _.VkErrorID(4680)
  687. << "For Vulkan, an OpTypeStruct variable containing an "
  688. << "OpTypeRuntimeArray must be decorated with Block if it "
  689. << "has storage class StorageBuffer or "
  690. "PhysicalStorageBuffer.";
  691. }
  692. } else if (storage_class == spv::StorageClass::Uniform) {
  693. if (!_.HasDecoration(value_id, spv::Decoration::BufferBlock)) {
  694. return _.diag(SPV_ERROR_INVALID_ID, inst)
  695. << _.VkErrorID(4680)
  696. << "For Vulkan, an OpTypeStruct variable containing an "
  697. << "OpTypeRuntimeArray must be decorated with BufferBlock "
  698. << "if it has storage class Uniform.";
  699. }
  700. } else {
  701. return _.diag(SPV_ERROR_INVALID_ID, inst)
  702. << _.VkErrorID(4680)
  703. << "For Vulkan, OpTypeStruct variables containing "
  704. << "OpTypeRuntimeArray must have storage class of "
  705. << "StorageBuffer, PhysicalStorageBuffer, or Uniform.";
  706. }
  707. }
  708. }
  709. }
  710. // Cooperative matrix types can only be allocated in Function or Private
  711. if ((storage_class != spv::StorageClass::Function &&
  712. storage_class != spv::StorageClass::Private) &&
  713. ContainsCooperativeMatrix(_, pointee)) {
  714. return _.diag(SPV_ERROR_INVALID_ID, inst)
  715. << "Cooperative matrix types (or types containing them) can only be "
  716. "allocated "
  717. << "in Function or Private storage classes or as function "
  718. "parameters";
  719. }
  720. if (_.HasCapability(spv::Capability::Shader)) {
  721. // Don't allow variables containing 16-bit elements without the appropriate
  722. // capabilities.
  723. if ((!_.HasCapability(spv::Capability::Int16) &&
  724. _.ContainsSizedIntOrFloatType(value_id, spv::Op::OpTypeInt, 16)) ||
  725. (!_.HasCapability(spv::Capability::Float16) &&
  726. _.ContainsSizedIntOrFloatType(value_id, spv::Op::OpTypeFloat, 16))) {
  727. auto underlying_type = value_type;
  728. while (underlying_type->opcode() == spv::Op::OpTypePointer) {
  729. storage_class = underlying_type->GetOperandAs<spv::StorageClass>(1u);
  730. underlying_type =
  731. _.FindDef(underlying_type->GetOperandAs<uint32_t>(2u));
  732. }
  733. bool storage_class_ok = true;
  734. std::string sc_name = _.grammar().lookupOperandName(
  735. SPV_OPERAND_TYPE_STORAGE_CLASS, uint32_t(storage_class));
  736. switch (storage_class) {
  737. case spv::StorageClass::StorageBuffer:
  738. case spv::StorageClass::PhysicalStorageBuffer:
  739. if (!_.HasCapability(spv::Capability::StorageBuffer16BitAccess)) {
  740. storage_class_ok = false;
  741. }
  742. break;
  743. case spv::StorageClass::Uniform:
  744. if (!_.HasCapability(
  745. spv::Capability::UniformAndStorageBuffer16BitAccess)) {
  746. if (underlying_type->opcode() == spv::Op::OpTypeArray ||
  747. underlying_type->opcode() == spv::Op::OpTypeRuntimeArray) {
  748. underlying_type =
  749. _.FindDef(underlying_type->GetOperandAs<uint32_t>(1u));
  750. }
  751. if (!_.HasCapability(spv::Capability::StorageBuffer16BitAccess) ||
  752. !_.HasDecoration(underlying_type->id(),
  753. spv::Decoration::BufferBlock)) {
  754. storage_class_ok = false;
  755. }
  756. }
  757. break;
  758. case spv::StorageClass::PushConstant:
  759. if (!_.HasCapability(spv::Capability::StoragePushConstant16)) {
  760. storage_class_ok = false;
  761. }
  762. break;
  763. case spv::StorageClass::Input:
  764. case spv::StorageClass::Output:
  765. if (!_.HasCapability(spv::Capability::StorageInputOutput16)) {
  766. storage_class_ok = false;
  767. }
  768. break;
  769. case spv::StorageClass::Workgroup:
  770. if (!_.HasCapability(
  771. spv::Capability::
  772. WorkgroupMemoryExplicitLayout16BitAccessKHR)) {
  773. storage_class_ok = false;
  774. }
  775. break;
  776. default:
  777. return _.diag(SPV_ERROR_INVALID_ID, inst)
  778. << "Cannot allocate a variable containing a 16-bit type in "
  779. << sc_name << " storage class";
  780. }
  781. if (!storage_class_ok) {
  782. return _.diag(SPV_ERROR_INVALID_ID, inst)
  783. << "Allocating a variable containing a 16-bit element in "
  784. << sc_name << " storage class requires an additional capability";
  785. }
  786. }
  787. // Don't allow variables containing 8-bit elements without the appropriate
  788. // capabilities.
  789. if (!_.HasCapability(spv::Capability::Int8) &&
  790. _.ContainsSizedIntOrFloatType(value_id, spv::Op::OpTypeInt, 8)) {
  791. auto underlying_type = value_type;
  792. while (underlying_type->opcode() == spv::Op::OpTypePointer) {
  793. storage_class = underlying_type->GetOperandAs<spv::StorageClass>(1u);
  794. underlying_type =
  795. _.FindDef(underlying_type->GetOperandAs<uint32_t>(2u));
  796. }
  797. bool storage_class_ok = true;
  798. std::string sc_name = _.grammar().lookupOperandName(
  799. SPV_OPERAND_TYPE_STORAGE_CLASS, uint32_t(storage_class));
  800. switch (storage_class) {
  801. case spv::StorageClass::StorageBuffer:
  802. case spv::StorageClass::PhysicalStorageBuffer:
  803. if (!_.HasCapability(spv::Capability::StorageBuffer8BitAccess)) {
  804. storage_class_ok = false;
  805. }
  806. break;
  807. case spv::StorageClass::Uniform:
  808. if (!_.HasCapability(
  809. spv::Capability::UniformAndStorageBuffer8BitAccess)) {
  810. if (underlying_type->opcode() == spv::Op::OpTypeArray ||
  811. underlying_type->opcode() == spv::Op::OpTypeRuntimeArray) {
  812. underlying_type =
  813. _.FindDef(underlying_type->GetOperandAs<uint32_t>(1u));
  814. }
  815. if (!_.HasCapability(spv::Capability::StorageBuffer8BitAccess) ||
  816. !_.HasDecoration(underlying_type->id(),
  817. spv::Decoration::BufferBlock)) {
  818. storage_class_ok = false;
  819. }
  820. }
  821. break;
  822. case spv::StorageClass::PushConstant:
  823. if (!_.HasCapability(spv::Capability::StoragePushConstant8)) {
  824. storage_class_ok = false;
  825. }
  826. break;
  827. case spv::StorageClass::Workgroup:
  828. if (!_.HasCapability(
  829. spv::Capability::
  830. WorkgroupMemoryExplicitLayout8BitAccessKHR)) {
  831. storage_class_ok = false;
  832. }
  833. break;
  834. default:
  835. return _.diag(SPV_ERROR_INVALID_ID, inst)
  836. << "Cannot allocate a variable containing a 8-bit type in "
  837. << sc_name << " storage class";
  838. }
  839. if (!storage_class_ok) {
  840. return _.diag(SPV_ERROR_INVALID_ID, inst)
  841. << "Allocating a variable containing a 8-bit element in "
  842. << sc_name << " storage class requires an additional capability";
  843. }
  844. }
  845. }
  846. return SPV_SUCCESS;
  847. }
  848. spv_result_t ValidateLoad(ValidationState_t& _, const Instruction* inst) {
  849. const auto result_type = _.FindDef(inst->type_id());
  850. if (!result_type) {
  851. return _.diag(SPV_ERROR_INVALID_ID, inst)
  852. << "OpLoad Result Type <id> " << _.getIdName(inst->type_id())
  853. << " is not defined.";
  854. }
  855. const auto pointer_index = 2;
  856. const auto pointer_id = inst->GetOperandAs<uint32_t>(pointer_index);
  857. const auto pointer = _.FindDef(pointer_id);
  858. if (!pointer ||
  859. ((_.addressing_model() == spv::AddressingModel::Logical) &&
  860. ((!_.features().variable_pointers &&
  861. !spvOpcodeReturnsLogicalPointer(pointer->opcode())) ||
  862. (_.features().variable_pointers &&
  863. !spvOpcodeReturnsLogicalVariablePointer(pointer->opcode()))))) {
  864. return _.diag(SPV_ERROR_INVALID_ID, inst)
  865. << "OpLoad Pointer <id> " << _.getIdName(pointer_id)
  866. << " is not a logical pointer.";
  867. }
  868. const auto pointer_type = _.FindDef(pointer->type_id());
  869. if (!pointer_type || pointer_type->opcode() != spv::Op::OpTypePointer) {
  870. return _.diag(SPV_ERROR_INVALID_ID, inst)
  871. << "OpLoad type for pointer <id> " << _.getIdName(pointer_id)
  872. << " is not a pointer type.";
  873. }
  874. uint32_t pointee_data_type;
  875. spv::StorageClass storage_class;
  876. if (!_.GetPointerTypeInfo(pointer_type->id(), &pointee_data_type,
  877. &storage_class) ||
  878. result_type->id() != pointee_data_type) {
  879. return _.diag(SPV_ERROR_INVALID_ID, inst)
  880. << "OpLoad Result Type <id> " << _.getIdName(inst->type_id())
  881. << " does not match Pointer <id> " << _.getIdName(pointer->id())
  882. << "s type.";
  883. }
  884. if (!_.options()->before_hlsl_legalization &&
  885. _.ContainsRuntimeArray(inst->type_id())) {
  886. return _.diag(SPV_ERROR_INVALID_ID, inst)
  887. << "Cannot load a runtime-sized array";
  888. }
  889. if (auto error = CheckMemoryAccess(_, inst, 3)) return error;
  890. if (_.HasCapability(spv::Capability::Shader) &&
  891. _.ContainsLimitedUseIntOrFloatType(inst->type_id()) &&
  892. result_type->opcode() != spv::Op::OpTypePointer) {
  893. if (result_type->opcode() != spv::Op::OpTypeInt &&
  894. result_type->opcode() != spv::Op::OpTypeFloat &&
  895. result_type->opcode() != spv::Op::OpTypeVector &&
  896. result_type->opcode() != spv::Op::OpTypeMatrix) {
  897. return _.diag(SPV_ERROR_INVALID_ID, inst)
  898. << "8- or 16-bit loads must be a scalar, vector or matrix type";
  899. }
  900. }
  901. _.RegisterQCOMImageProcessingTextureConsumer(pointer_id, inst, nullptr);
  902. return SPV_SUCCESS;
  903. }
  904. spv_result_t ValidateStore(ValidationState_t& _, const Instruction* inst) {
  905. const auto pointer_index = 0;
  906. const auto pointer_id = inst->GetOperandAs<uint32_t>(pointer_index);
  907. const auto pointer = _.FindDef(pointer_id);
  908. if (!pointer ||
  909. (_.addressing_model() == spv::AddressingModel::Logical &&
  910. ((!_.features().variable_pointers &&
  911. !spvOpcodeReturnsLogicalPointer(pointer->opcode())) ||
  912. (_.features().variable_pointers &&
  913. !spvOpcodeReturnsLogicalVariablePointer(pointer->opcode()))))) {
  914. return _.diag(SPV_ERROR_INVALID_ID, inst)
  915. << "OpStore Pointer <id> " << _.getIdName(pointer_id)
  916. << " is not a logical pointer.";
  917. }
  918. const auto pointer_type = _.FindDef(pointer->type_id());
  919. if (!pointer_type || pointer_type->opcode() != spv::Op::OpTypePointer) {
  920. return _.diag(SPV_ERROR_INVALID_ID, inst)
  921. << "OpStore type for pointer <id> " << _.getIdName(pointer_id)
  922. << " is not a pointer type.";
  923. }
  924. const auto type_id = pointer_type->GetOperandAs<uint32_t>(2);
  925. const auto type = _.FindDef(type_id);
  926. if (!type || spv::Op::OpTypeVoid == type->opcode()) {
  927. return _.diag(SPV_ERROR_INVALID_ID, inst)
  928. << "OpStore Pointer <id> " << _.getIdName(pointer_id)
  929. << "s type is void.";
  930. }
  931. // validate storage class
  932. {
  933. uint32_t data_type;
  934. spv::StorageClass storage_class;
  935. if (!_.GetPointerTypeInfo(pointer_type->id(), &data_type, &storage_class)) {
  936. return _.diag(SPV_ERROR_INVALID_ID, inst)
  937. << "OpStore Pointer <id> " << _.getIdName(pointer_id)
  938. << " is not pointer type";
  939. }
  940. if (storage_class == spv::StorageClass::UniformConstant ||
  941. storage_class == spv::StorageClass::Input ||
  942. storage_class == spv::StorageClass::PushConstant) {
  943. return _.diag(SPV_ERROR_INVALID_ID, inst)
  944. << "OpStore Pointer <id> " << _.getIdName(pointer_id)
  945. << " storage class is read-only";
  946. } else if (storage_class == spv::StorageClass::ShaderRecordBufferKHR) {
  947. return _.diag(SPV_ERROR_INVALID_ID, inst)
  948. << "ShaderRecordBufferKHR Storage Class variables are read only";
  949. } else if (storage_class == spv::StorageClass::HitAttributeKHR) {
  950. std::string errorVUID = _.VkErrorID(4703);
  951. _.function(inst->function()->id())
  952. ->RegisterExecutionModelLimitation(
  953. [errorVUID](spv::ExecutionModel model, std::string* message) {
  954. if (model == spv::ExecutionModel::AnyHitKHR ||
  955. model == spv::ExecutionModel::ClosestHitKHR) {
  956. if (message) {
  957. *message =
  958. errorVUID +
  959. "HitAttributeKHR Storage Class variables are read only "
  960. "with AnyHitKHR and ClosestHitKHR";
  961. }
  962. return false;
  963. }
  964. return true;
  965. });
  966. }
  967. if (spvIsVulkanEnv(_.context()->target_env) &&
  968. storage_class == spv::StorageClass::Uniform) {
  969. auto base_ptr = _.TracePointer(pointer);
  970. if (base_ptr->opcode() == spv::Op::OpVariable) {
  971. // If it's not a variable a different check should catch the problem.
  972. auto base_type = _.FindDef(base_ptr->GetOperandAs<uint32_t>(0));
  973. // Get the pointed-to type.
  974. base_type = _.FindDef(base_type->GetOperandAs<uint32_t>(2u));
  975. if (base_type->opcode() == spv::Op::OpTypeArray ||
  976. base_type->opcode() == spv::Op::OpTypeRuntimeArray) {
  977. base_type = _.FindDef(base_type->GetOperandAs<uint32_t>(1u));
  978. }
  979. if (_.HasDecoration(base_type->id(), spv::Decoration::Block)) {
  980. return _.diag(SPV_ERROR_INVALID_ID, inst)
  981. << _.VkErrorID(6925)
  982. << "In the Vulkan environment, cannot store to Uniform Blocks";
  983. }
  984. }
  985. }
  986. }
  987. const auto object_index = 1;
  988. const auto object_id = inst->GetOperandAs<uint32_t>(object_index);
  989. const auto object = _.FindDef(object_id);
  990. if (!object || !object->type_id()) {
  991. return _.diag(SPV_ERROR_INVALID_ID, inst)
  992. << "OpStore Object <id> " << _.getIdName(object_id)
  993. << " is not an object.";
  994. }
  995. const auto object_type = _.FindDef(object->type_id());
  996. if (!object_type || spv::Op::OpTypeVoid == object_type->opcode()) {
  997. return _.diag(SPV_ERROR_INVALID_ID, inst)
  998. << "OpStore Object <id> " << _.getIdName(object_id)
  999. << "s type is void.";
  1000. }
  1001. if (type->id() != object_type->id()) {
  1002. if (!_.options()->relax_struct_store ||
  1003. type->opcode() != spv::Op::OpTypeStruct ||
  1004. object_type->opcode() != spv::Op::OpTypeStruct) {
  1005. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1006. << "OpStore Pointer <id> " << _.getIdName(pointer_id)
  1007. << "s type does not match Object <id> "
  1008. << _.getIdName(object->id()) << "s type.";
  1009. }
  1010. // TODO: Check for layout compatible matricies and arrays as well.
  1011. if (!AreLayoutCompatibleStructs(_, type, object_type)) {
  1012. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1013. << "OpStore Pointer <id> " << _.getIdName(pointer_id)
  1014. << "s layout does not match Object <id> "
  1015. << _.getIdName(object->id()) << "s layout.";
  1016. }
  1017. }
  1018. if (auto error = CheckMemoryAccess(_, inst, 2)) return error;
  1019. if (_.HasCapability(spv::Capability::Shader) &&
  1020. _.ContainsLimitedUseIntOrFloatType(inst->type_id()) &&
  1021. object_type->opcode() != spv::Op::OpTypePointer) {
  1022. if (object_type->opcode() != spv::Op::OpTypeInt &&
  1023. object_type->opcode() != spv::Op::OpTypeFloat &&
  1024. object_type->opcode() != spv::Op::OpTypeVector &&
  1025. object_type->opcode() != spv::Op::OpTypeMatrix) {
  1026. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1027. << "8- or 16-bit stores must be a scalar, vector or matrix type";
  1028. }
  1029. }
  1030. return SPV_SUCCESS;
  1031. }
  1032. spv_result_t ValidateCopyMemoryMemoryAccess(ValidationState_t& _,
  1033. const Instruction* inst) {
  1034. assert(inst->opcode() == spv::Op::OpCopyMemory ||
  1035. inst->opcode() == spv::Op::OpCopyMemorySized);
  1036. const uint32_t first_access_index =
  1037. inst->opcode() == spv::Op::OpCopyMemory ? 2 : 3;
  1038. if (inst->operands().size() > first_access_index) {
  1039. if (auto error = CheckMemoryAccess(_, inst, first_access_index))
  1040. return error;
  1041. const auto first_access = inst->GetOperandAs<uint32_t>(first_access_index);
  1042. const uint32_t second_access_index =
  1043. first_access_index + MemoryAccessNumWords(first_access);
  1044. if (inst->operands().size() > second_access_index) {
  1045. if (_.features().copy_memory_permits_two_memory_accesses) {
  1046. if (auto error = CheckMemoryAccess(_, inst, second_access_index))
  1047. return error;
  1048. // In the two-access form in SPIR-V 1.4 and later:
  1049. // - the first is the target (write) access and it can't have
  1050. // make-visible.
  1051. // - the second is the source (read) access and it can't have
  1052. // make-available.
  1053. if (first_access &
  1054. uint32_t(spv::MemoryAccessMask::MakePointerVisibleKHR)) {
  1055. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1056. << "Target memory access must not include "
  1057. "MakePointerVisibleKHR";
  1058. }
  1059. const auto second_access =
  1060. inst->GetOperandAs<uint32_t>(second_access_index);
  1061. if (second_access &
  1062. uint32_t(spv::MemoryAccessMask::MakePointerAvailableKHR)) {
  1063. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1064. << "Source memory access must not include "
  1065. "MakePointerAvailableKHR";
  1066. }
  1067. } else {
  1068. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1069. << spvOpcodeString(static_cast<spv::Op>(inst->opcode()))
  1070. << " with two memory access operands requires SPIR-V 1.4 or "
  1071. "later";
  1072. }
  1073. }
  1074. }
  1075. return SPV_SUCCESS;
  1076. }
  1077. spv_result_t ValidateCopyMemory(ValidationState_t& _, const Instruction* inst) {
  1078. const auto target_index = 0;
  1079. const auto target_id = inst->GetOperandAs<uint32_t>(target_index);
  1080. const auto target = _.FindDef(target_id);
  1081. if (!target) {
  1082. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1083. << "Target operand <id> " << _.getIdName(target_id)
  1084. << " is not defined.";
  1085. }
  1086. const auto source_index = 1;
  1087. const auto source_id = inst->GetOperandAs<uint32_t>(source_index);
  1088. const auto source = _.FindDef(source_id);
  1089. if (!source) {
  1090. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1091. << "Source operand <id> " << _.getIdName(source_id)
  1092. << " is not defined.";
  1093. }
  1094. const auto target_pointer_type = _.FindDef(target->type_id());
  1095. if (!target_pointer_type ||
  1096. target_pointer_type->opcode() != spv::Op::OpTypePointer) {
  1097. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1098. << "Target operand <id> " << _.getIdName(target_id)
  1099. << " is not a pointer.";
  1100. }
  1101. const auto source_pointer_type = _.FindDef(source->type_id());
  1102. if (!source_pointer_type ||
  1103. source_pointer_type->opcode() != spv::Op::OpTypePointer) {
  1104. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1105. << "Source operand <id> " << _.getIdName(source_id)
  1106. << " is not a pointer.";
  1107. }
  1108. if (inst->opcode() == spv::Op::OpCopyMemory) {
  1109. const auto target_type =
  1110. _.FindDef(target_pointer_type->GetOperandAs<uint32_t>(2));
  1111. if (!target_type || target_type->opcode() == spv::Op::OpTypeVoid) {
  1112. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1113. << "Target operand <id> " << _.getIdName(target_id)
  1114. << " cannot be a void pointer.";
  1115. }
  1116. const auto source_type =
  1117. _.FindDef(source_pointer_type->GetOperandAs<uint32_t>(2));
  1118. if (!source_type || source_type->opcode() == spv::Op::OpTypeVoid) {
  1119. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1120. << "Source operand <id> " << _.getIdName(source_id)
  1121. << " cannot be a void pointer.";
  1122. }
  1123. if (target_type->id() != source_type->id()) {
  1124. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1125. << "Target <id> " << _.getIdName(source_id)
  1126. << "s type does not match Source <id> "
  1127. << _.getIdName(source_type->id()) << "s type.";
  1128. }
  1129. } else {
  1130. const auto size_id = inst->GetOperandAs<uint32_t>(2);
  1131. const auto size = _.FindDef(size_id);
  1132. if (!size) {
  1133. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1134. << "Size operand <id> " << _.getIdName(size_id)
  1135. << " is not defined.";
  1136. }
  1137. const auto size_type = _.FindDef(size->type_id());
  1138. if (!_.IsIntScalarType(size_type->id())) {
  1139. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1140. << "Size operand <id> " << _.getIdName(size_id)
  1141. << " must be a scalar integer type.";
  1142. }
  1143. bool is_zero = true;
  1144. switch (size->opcode()) {
  1145. case spv::Op::OpConstantNull:
  1146. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1147. << "Size operand <id> " << _.getIdName(size_id)
  1148. << " cannot be a constant zero.";
  1149. case spv::Op::OpConstant:
  1150. if (size_type->word(3) == 1 &&
  1151. size->word(size->words().size() - 1) & 0x80000000) {
  1152. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1153. << "Size operand <id> " << _.getIdName(size_id)
  1154. << " cannot have the sign bit set to 1.";
  1155. }
  1156. for (size_t i = 3; is_zero && i < size->words().size(); ++i) {
  1157. is_zero &= (size->word(i) == 0);
  1158. }
  1159. if (is_zero) {
  1160. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1161. << "Size operand <id> " << _.getIdName(size_id)
  1162. << " cannot be a constant zero.";
  1163. }
  1164. break;
  1165. default:
  1166. // Cannot infer any other opcodes.
  1167. break;
  1168. }
  1169. }
  1170. if (auto error = ValidateCopyMemoryMemoryAccess(_, inst)) return error;
  1171. // Get past the pointers to avoid checking a pointer copy.
  1172. auto sub_type = _.FindDef(target_pointer_type->GetOperandAs<uint32_t>(2));
  1173. while (sub_type->opcode() == spv::Op::OpTypePointer) {
  1174. sub_type = _.FindDef(sub_type->GetOperandAs<uint32_t>(2));
  1175. }
  1176. if (_.HasCapability(spv::Capability::Shader) &&
  1177. _.ContainsLimitedUseIntOrFloatType(sub_type->id())) {
  1178. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1179. << "Cannot copy memory of objects containing 8- or 16-bit types";
  1180. }
  1181. return SPV_SUCCESS;
  1182. }
  1183. spv_result_t ValidateAccessChain(ValidationState_t& _,
  1184. const Instruction* inst) {
  1185. std::string instr_name =
  1186. "Op" + std::string(spvOpcodeString(static_cast<spv::Op>(inst->opcode())));
  1187. // The result type must be OpTypePointer.
  1188. auto result_type = _.FindDef(inst->type_id());
  1189. if (spv::Op::OpTypePointer != result_type->opcode()) {
  1190. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1191. << "The Result Type of " << instr_name << " <id> "
  1192. << _.getIdName(inst->id()) << " must be OpTypePointer. Found Op"
  1193. << spvOpcodeString(static_cast<spv::Op>(result_type->opcode()))
  1194. << ".";
  1195. }
  1196. // Result type is a pointer. Find out what it's pointing to.
  1197. // This will be used to make sure the indexing results in the same type.
  1198. // OpTypePointer word 3 is the type being pointed to.
  1199. const auto result_type_pointee = _.FindDef(result_type->word(3));
  1200. // Base must be a pointer, pointing to the base of a composite object.
  1201. const auto base_index = 2;
  1202. const auto base_id = inst->GetOperandAs<uint32_t>(base_index);
  1203. const auto base = _.FindDef(base_id);
  1204. const auto base_type = _.FindDef(base->type_id());
  1205. if (!base_type || spv::Op::OpTypePointer != base_type->opcode()) {
  1206. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1207. << "The Base <id> " << _.getIdName(base_id) << " in " << instr_name
  1208. << " instruction must be a pointer.";
  1209. }
  1210. // The result pointer storage class and base pointer storage class must match.
  1211. // Word 2 of OpTypePointer is the Storage Class.
  1212. auto result_type_storage_class = result_type->word(2);
  1213. auto base_type_storage_class = base_type->word(2);
  1214. if (result_type_storage_class != base_type_storage_class) {
  1215. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1216. << "The result pointer storage class and base "
  1217. "pointer storage class in "
  1218. << instr_name << " do not match.";
  1219. }
  1220. // The type pointed to by OpTypePointer (word 3) must be a composite type.
  1221. auto type_pointee = _.FindDef(base_type->word(3));
  1222. // Check Universal Limit (SPIR-V Spec. Section 2.17).
  1223. // The number of indexes passed to OpAccessChain may not exceed 255
  1224. // The instruction includes 4 words + N words (for N indexes)
  1225. size_t num_indexes = inst->words().size() - 4;
  1226. if (inst->opcode() == spv::Op::OpPtrAccessChain ||
  1227. inst->opcode() == spv::Op::OpInBoundsPtrAccessChain) {
  1228. // In pointer access chains, the element operand is required, but not
  1229. // counted as an index.
  1230. --num_indexes;
  1231. }
  1232. const size_t num_indexes_limit =
  1233. _.options()->universal_limits_.max_access_chain_indexes;
  1234. if (num_indexes > num_indexes_limit) {
  1235. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1236. << "The number of indexes in " << instr_name << " may not exceed "
  1237. << num_indexes_limit << ". Found " << num_indexes << " indexes.";
  1238. }
  1239. // Indexes walk the type hierarchy to the desired depth, potentially down to
  1240. // scalar granularity. The first index in Indexes will select the top-level
  1241. // member/element/component/element of the base composite. All composite
  1242. // constituents use zero-based numbering, as described by their OpType...
  1243. // instruction. The second index will apply similarly to that result, and so
  1244. // on. Once any non-composite type is reached, there must be no remaining
  1245. // (unused) indexes.
  1246. auto starting_index = 4;
  1247. if (inst->opcode() == spv::Op::OpPtrAccessChain ||
  1248. inst->opcode() == spv::Op::OpInBoundsPtrAccessChain) {
  1249. ++starting_index;
  1250. }
  1251. for (size_t i = starting_index; i < inst->words().size(); ++i) {
  1252. const uint32_t cur_word = inst->words()[i];
  1253. // Earlier ID checks ensure that cur_word definition exists.
  1254. auto cur_word_instr = _.FindDef(cur_word);
  1255. // The index must be a scalar integer type (See OpAccessChain in the Spec.)
  1256. auto index_type = _.FindDef(cur_word_instr->type_id());
  1257. if (!index_type || spv::Op::OpTypeInt != index_type->opcode()) {
  1258. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1259. << "Indexes passed to " << instr_name
  1260. << " must be of type integer.";
  1261. }
  1262. switch (type_pointee->opcode()) {
  1263. case spv::Op::OpTypeMatrix:
  1264. case spv::Op::OpTypeVector:
  1265. case spv::Op::OpTypeCooperativeMatrixNV:
  1266. case spv::Op::OpTypeCooperativeMatrixKHR:
  1267. case spv::Op::OpTypeArray:
  1268. case spv::Op::OpTypeRuntimeArray: {
  1269. // In OpTypeMatrix, OpTypeVector, spv::Op::OpTypeCooperativeMatrixNV,
  1270. // OpTypeArray, and OpTypeRuntimeArray, word 2 is the Element Type.
  1271. type_pointee = _.FindDef(type_pointee->word(2));
  1272. break;
  1273. }
  1274. case spv::Op::OpTypeStruct: {
  1275. // In case of structures, there is an additional constraint on the
  1276. // index: the index must be an OpConstant.
  1277. if (spv::Op::OpConstant != cur_word_instr->opcode()) {
  1278. return _.diag(SPV_ERROR_INVALID_ID, cur_word_instr)
  1279. << "The <id> passed to " << instr_name
  1280. << " to index into a "
  1281. "structure must be an OpConstant.";
  1282. }
  1283. // Get the index value from the OpConstant (word 3 of OpConstant).
  1284. // OpConstant could be a signed integer. But it's okay to treat it as
  1285. // unsigned because a negative constant int would never be seen as
  1286. // correct as a struct offset, since structs can't have more than 2
  1287. // billion members.
  1288. const uint32_t cur_index = cur_word_instr->word(3);
  1289. // The index points to the struct member we want, therefore, the index
  1290. // should be less than the number of struct members.
  1291. const uint32_t num_struct_members =
  1292. static_cast<uint32_t>(type_pointee->words().size() - 2);
  1293. if (cur_index >= num_struct_members) {
  1294. return _.diag(SPV_ERROR_INVALID_ID, cur_word_instr)
  1295. << "Index is out of bounds: " << instr_name
  1296. << " can not find index " << cur_index
  1297. << " into the structure <id> "
  1298. << _.getIdName(type_pointee->id()) << ". This structure has "
  1299. << num_struct_members << " members. Largest valid index is "
  1300. << num_struct_members - 1 << ".";
  1301. }
  1302. // Struct members IDs start at word 2 of OpTypeStruct.
  1303. auto structMemberId = type_pointee->word(cur_index + 2);
  1304. type_pointee = _.FindDef(structMemberId);
  1305. break;
  1306. }
  1307. default: {
  1308. // Give an error. reached non-composite type while indexes still remain.
  1309. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1310. << instr_name
  1311. << " reached non-composite type while indexes "
  1312. "still remain to be traversed.";
  1313. }
  1314. }
  1315. }
  1316. // At this point, we have fully walked down from the base using the indeces.
  1317. // The type being pointed to should be the same as the result type.
  1318. if (type_pointee->id() != result_type_pointee->id()) {
  1319. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1320. << instr_name << " result type (Op"
  1321. << spvOpcodeString(
  1322. static_cast<spv::Op>(result_type_pointee->opcode()))
  1323. << ") does not match the type that results from indexing into the "
  1324. "base "
  1325. "<id> (Op"
  1326. << spvOpcodeString(static_cast<spv::Op>(type_pointee->opcode()))
  1327. << ").";
  1328. }
  1329. return SPV_SUCCESS;
  1330. }
  1331. spv_result_t ValidatePtrAccessChain(ValidationState_t& _,
  1332. const Instruction* inst) {
  1333. if (_.addressing_model() == spv::AddressingModel::Logical) {
  1334. if (!_.features().variable_pointers) {
  1335. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1336. << "Generating variable pointers requires capability "
  1337. << "VariablePointers or VariablePointersStorageBuffer";
  1338. }
  1339. }
  1340. // Need to call first, will make sure Base is a valid ID
  1341. if (auto error = ValidateAccessChain(_, inst)) return error;
  1342. const auto base_id = inst->GetOperandAs<uint32_t>(2);
  1343. const auto base = _.FindDef(base_id);
  1344. const auto base_type = _.FindDef(base->type_id());
  1345. const auto base_type_storage_class =
  1346. base_type->GetOperandAs<spv::StorageClass>(1);
  1347. if (_.HasCapability(spv::Capability::Shader) &&
  1348. (base_type_storage_class == spv::StorageClass::Uniform ||
  1349. base_type_storage_class == spv::StorageClass::StorageBuffer ||
  1350. base_type_storage_class == spv::StorageClass::PhysicalStorageBuffer ||
  1351. base_type_storage_class == spv::StorageClass::PushConstant ||
  1352. (_.HasCapability(spv::Capability::WorkgroupMemoryExplicitLayoutKHR) &&
  1353. base_type_storage_class == spv::StorageClass::Workgroup)) &&
  1354. !_.HasDecoration(base_type->id(), spv::Decoration::ArrayStride)) {
  1355. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1356. << "OpPtrAccessChain must have a Base whose type is decorated "
  1357. "with ArrayStride";
  1358. }
  1359. if (spvIsVulkanEnv(_.context()->target_env)) {
  1360. if (base_type_storage_class == spv::StorageClass::Workgroup) {
  1361. if (!_.HasCapability(spv::Capability::VariablePointers)) {
  1362. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1363. << _.VkErrorID(7651)
  1364. << "OpPtrAccessChain Base operand pointing to Workgroup "
  1365. "storage class must use VariablePointers capability";
  1366. }
  1367. } else if (base_type_storage_class == spv::StorageClass::StorageBuffer) {
  1368. if (!_.features().variable_pointers) {
  1369. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1370. << _.VkErrorID(7652)
  1371. << "OpPtrAccessChain Base operand pointing to StorageBuffer "
  1372. "storage class must use VariablePointers or "
  1373. "VariablePointersStorageBuffer capability";
  1374. }
  1375. } else if (base_type_storage_class !=
  1376. spv::StorageClass::PhysicalStorageBuffer) {
  1377. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1378. << _.VkErrorID(7650)
  1379. << "OpPtrAccessChain Base operand must point to Workgroup, "
  1380. "StorageBuffer, or PhysicalStorageBuffer storage class";
  1381. }
  1382. }
  1383. return SPV_SUCCESS;
  1384. }
  1385. spv_result_t ValidateArrayLength(ValidationState_t& state,
  1386. const Instruction* inst) {
  1387. std::string instr_name =
  1388. "Op" + std::string(spvOpcodeString(static_cast<spv::Op>(inst->opcode())));
  1389. // Result type must be a 32-bit unsigned int.
  1390. auto result_type = state.FindDef(inst->type_id());
  1391. if (result_type->opcode() != spv::Op::OpTypeInt ||
  1392. result_type->GetOperandAs<uint32_t>(1) != 32 ||
  1393. result_type->GetOperandAs<uint32_t>(2) != 0) {
  1394. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1395. << "The Result Type of " << instr_name << " <id> "
  1396. << state.getIdName(inst->id())
  1397. << " must be OpTypeInt with width 32 and signedness 0.";
  1398. }
  1399. // The structure that is passed in must be an pointer to a structure, whose
  1400. // last element is a runtime array.
  1401. auto pointer = state.FindDef(inst->GetOperandAs<uint32_t>(2));
  1402. auto pointer_type = state.FindDef(pointer->type_id());
  1403. if (pointer_type->opcode() != spv::Op::OpTypePointer) {
  1404. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1405. << "The Structure's type in " << instr_name << " <id> "
  1406. << state.getIdName(inst->id())
  1407. << " must be a pointer to an OpTypeStruct.";
  1408. }
  1409. auto structure_type = state.FindDef(pointer_type->GetOperandAs<uint32_t>(2));
  1410. if (structure_type->opcode() != spv::Op::OpTypeStruct) {
  1411. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1412. << "The Structure's type in " << instr_name << " <id> "
  1413. << state.getIdName(inst->id())
  1414. << " must be a pointer to an OpTypeStruct.";
  1415. }
  1416. auto num_of_members = structure_type->operands().size() - 1;
  1417. auto last_member =
  1418. state.FindDef(structure_type->GetOperandAs<uint32_t>(num_of_members));
  1419. if (last_member->opcode() != spv::Op::OpTypeRuntimeArray) {
  1420. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1421. << "The Structure's last member in " << instr_name << " <id> "
  1422. << state.getIdName(inst->id()) << " must be an OpTypeRuntimeArray.";
  1423. }
  1424. // The array member must the index of the last element (the run time
  1425. // array).
  1426. if (inst->GetOperandAs<uint32_t>(3) != num_of_members - 1) {
  1427. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1428. << "The array member in " << instr_name << " <id> "
  1429. << state.getIdName(inst->id())
  1430. << " must be an the last member of the struct.";
  1431. }
  1432. return SPV_SUCCESS;
  1433. }
  1434. spv_result_t ValidateCooperativeMatrixLengthNV(ValidationState_t& state,
  1435. const Instruction* inst) {
  1436. std::string instr_name =
  1437. "Op" + std::string(spvOpcodeString(static_cast<spv::Op>(inst->opcode())));
  1438. // Result type must be a 32-bit unsigned int.
  1439. auto result_type = state.FindDef(inst->type_id());
  1440. if (result_type->opcode() != spv::Op::OpTypeInt ||
  1441. result_type->GetOperandAs<uint32_t>(1) != 32 ||
  1442. result_type->GetOperandAs<uint32_t>(2) != 0) {
  1443. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1444. << "The Result Type of " << instr_name << " <id> "
  1445. << state.getIdName(inst->id())
  1446. << " must be OpTypeInt with width 32 and signedness 0.";
  1447. }
  1448. bool isKhr = inst->opcode() == spv::Op::OpCooperativeMatrixLengthKHR;
  1449. auto type_id = inst->GetOperandAs<uint32_t>(2);
  1450. auto type = state.FindDef(type_id);
  1451. if (isKhr && type->opcode() != spv::Op::OpTypeCooperativeMatrixKHR) {
  1452. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1453. << "The type in " << instr_name << " <id> "
  1454. << state.getIdName(type_id)
  1455. << " must be OpTypeCooperativeMatrixKHR.";
  1456. } else if (!isKhr && type->opcode() != spv::Op::OpTypeCooperativeMatrixNV) {
  1457. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1458. << "The type in " << instr_name << " <id> "
  1459. << state.getIdName(type_id) << " must be OpTypeCooperativeMatrixNV.";
  1460. }
  1461. return SPV_SUCCESS;
  1462. }
  1463. spv_result_t ValidateCooperativeMatrixLoadStoreNV(ValidationState_t& _,
  1464. const Instruction* inst) {
  1465. uint32_t type_id;
  1466. const char* opname;
  1467. if (inst->opcode() == spv::Op::OpCooperativeMatrixLoadNV) {
  1468. type_id = inst->type_id();
  1469. opname = "spv::Op::OpCooperativeMatrixLoadNV";
  1470. } else {
  1471. // get Object operand's type
  1472. type_id = _.FindDef(inst->GetOperandAs<uint32_t>(1))->type_id();
  1473. opname = "spv::Op::OpCooperativeMatrixStoreNV";
  1474. }
  1475. auto matrix_type = _.FindDef(type_id);
  1476. if (matrix_type->opcode() != spv::Op::OpTypeCooperativeMatrixNV) {
  1477. if (inst->opcode() == spv::Op::OpCooperativeMatrixLoadNV) {
  1478. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1479. << "spv::Op::OpCooperativeMatrixLoadNV Result Type <id> "
  1480. << _.getIdName(type_id) << " is not a cooperative matrix type.";
  1481. } else {
  1482. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1483. << "spv::Op::OpCooperativeMatrixStoreNV Object type <id> "
  1484. << _.getIdName(type_id) << " is not a cooperative matrix type.";
  1485. }
  1486. }
  1487. const auto pointer_index =
  1488. (inst->opcode() == spv::Op::OpCooperativeMatrixLoadNV) ? 2u : 0u;
  1489. const auto pointer_id = inst->GetOperandAs<uint32_t>(pointer_index);
  1490. const auto pointer = _.FindDef(pointer_id);
  1491. if (!pointer ||
  1492. ((_.addressing_model() == spv::AddressingModel::Logical) &&
  1493. ((!_.features().variable_pointers &&
  1494. !spvOpcodeReturnsLogicalPointer(pointer->opcode())) ||
  1495. (_.features().variable_pointers &&
  1496. !spvOpcodeReturnsLogicalVariablePointer(pointer->opcode()))))) {
  1497. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1498. << opname << " Pointer <id> " << _.getIdName(pointer_id)
  1499. << " is not a logical pointer.";
  1500. }
  1501. const auto pointer_type_id = pointer->type_id();
  1502. const auto pointer_type = _.FindDef(pointer_type_id);
  1503. if (!pointer_type || pointer_type->opcode() != spv::Op::OpTypePointer) {
  1504. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1505. << opname << " type for pointer <id> " << _.getIdName(pointer_id)
  1506. << " is not a pointer type.";
  1507. }
  1508. const auto storage_class_index = 1u;
  1509. const auto storage_class =
  1510. pointer_type->GetOperandAs<spv::StorageClass>(storage_class_index);
  1511. if (storage_class != spv::StorageClass::Workgroup &&
  1512. storage_class != spv::StorageClass::StorageBuffer &&
  1513. storage_class != spv::StorageClass::PhysicalStorageBuffer) {
  1514. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1515. << opname << " storage class for pointer type <id> "
  1516. << _.getIdName(pointer_type_id)
  1517. << " is not Workgroup or StorageBuffer.";
  1518. }
  1519. const auto pointee_id = pointer_type->GetOperandAs<uint32_t>(2);
  1520. const auto pointee_type = _.FindDef(pointee_id);
  1521. if (!pointee_type || !(_.IsIntScalarOrVectorType(pointee_id) ||
  1522. _.IsFloatScalarOrVectorType(pointee_id))) {
  1523. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1524. << opname << " Pointer <id> " << _.getIdName(pointer->id())
  1525. << "s Type must be a scalar or vector type.";
  1526. }
  1527. const auto stride_index =
  1528. (inst->opcode() == spv::Op::OpCooperativeMatrixLoadNV) ? 3u : 2u;
  1529. const auto stride_id = inst->GetOperandAs<uint32_t>(stride_index);
  1530. const auto stride = _.FindDef(stride_id);
  1531. if (!stride || !_.IsIntScalarType(stride->type_id())) {
  1532. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1533. << "Stride operand <id> " << _.getIdName(stride_id)
  1534. << " must be a scalar integer type.";
  1535. }
  1536. const auto colmajor_index =
  1537. (inst->opcode() == spv::Op::OpCooperativeMatrixLoadNV) ? 4u : 3u;
  1538. const auto colmajor_id = inst->GetOperandAs<uint32_t>(colmajor_index);
  1539. const auto colmajor = _.FindDef(colmajor_id);
  1540. if (!colmajor || !_.IsBoolScalarType(colmajor->type_id()) ||
  1541. !(spvOpcodeIsConstant(colmajor->opcode()) ||
  1542. spvOpcodeIsSpecConstant(colmajor->opcode()))) {
  1543. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1544. << "Column Major operand <id> " << _.getIdName(colmajor_id)
  1545. << " must be a boolean constant instruction.";
  1546. }
  1547. const auto memory_access_index =
  1548. (inst->opcode() == spv::Op::OpCooperativeMatrixLoadNV) ? 5u : 4u;
  1549. if (inst->operands().size() > memory_access_index) {
  1550. if (auto error = CheckMemoryAccess(_, inst, memory_access_index))
  1551. return error;
  1552. }
  1553. return SPV_SUCCESS;
  1554. }
  1555. spv_result_t ValidateCooperativeMatrixLoadStoreKHR(ValidationState_t& _,
  1556. const Instruction* inst) {
  1557. uint32_t type_id;
  1558. const char* opname;
  1559. if (inst->opcode() == spv::Op::OpCooperativeMatrixLoadKHR) {
  1560. type_id = inst->type_id();
  1561. opname = "spv::Op::OpCooperativeMatrixLoadKHR";
  1562. } else {
  1563. // get Object operand's type
  1564. type_id = _.FindDef(inst->GetOperandAs<uint32_t>(1))->type_id();
  1565. opname = "spv::Op::OpCooperativeMatrixStoreKHR";
  1566. }
  1567. auto matrix_type = _.FindDef(type_id);
  1568. if (matrix_type->opcode() != spv::Op::OpTypeCooperativeMatrixKHR) {
  1569. if (inst->opcode() == spv::Op::OpCooperativeMatrixLoadKHR) {
  1570. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1571. << "spv::Op::OpCooperativeMatrixLoadKHR Result Type <id> "
  1572. << _.getIdName(type_id) << " is not a cooperative matrix type.";
  1573. } else {
  1574. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1575. << "spv::Op::OpCooperativeMatrixStoreKHR Object type <id> "
  1576. << _.getIdName(type_id) << " is not a cooperative matrix type.";
  1577. }
  1578. }
  1579. const auto pointer_index =
  1580. (inst->opcode() == spv::Op::OpCooperativeMatrixLoadKHR) ? 2u : 0u;
  1581. const auto pointer_id = inst->GetOperandAs<uint32_t>(pointer_index);
  1582. const auto pointer = _.FindDef(pointer_id);
  1583. if (!pointer ||
  1584. ((_.addressing_model() == spv::AddressingModel::Logical) &&
  1585. ((!_.features().variable_pointers &&
  1586. !spvOpcodeReturnsLogicalPointer(pointer->opcode())) ||
  1587. (_.features().variable_pointers &&
  1588. !spvOpcodeReturnsLogicalVariablePointer(pointer->opcode()))))) {
  1589. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1590. << opname << " Pointer <id> " << _.getIdName(pointer_id)
  1591. << " is not a logical pointer.";
  1592. }
  1593. const auto pointer_type_id = pointer->type_id();
  1594. const auto pointer_type = _.FindDef(pointer_type_id);
  1595. if (!pointer_type || pointer_type->opcode() != spv::Op::OpTypePointer) {
  1596. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1597. << opname << " type for pointer <id> " << _.getIdName(pointer_id)
  1598. << " is not a pointer type.";
  1599. }
  1600. const auto storage_class_index = 1u;
  1601. const auto storage_class =
  1602. pointer_type->GetOperandAs<spv::StorageClass>(storage_class_index);
  1603. if (storage_class != spv::StorageClass::Workgroup &&
  1604. storage_class != spv::StorageClass::StorageBuffer &&
  1605. storage_class != spv::StorageClass::PhysicalStorageBuffer) {
  1606. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1607. << _.VkErrorID(8973) << opname
  1608. << " storage class for pointer type <id> "
  1609. << _.getIdName(pointer_type_id)
  1610. << " is not Workgroup, StorageBuffer, or PhysicalStorageBuffer.";
  1611. }
  1612. const auto pointee_id = pointer_type->GetOperandAs<uint32_t>(2);
  1613. const auto pointee_type = _.FindDef(pointee_id);
  1614. if (!pointee_type || !(_.IsIntScalarOrVectorType(pointee_id) ||
  1615. _.IsFloatScalarOrVectorType(pointee_id))) {
  1616. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1617. << opname << " Pointer <id> " << _.getIdName(pointer->id())
  1618. << "s Type must be a scalar or vector type.";
  1619. }
  1620. const auto layout_index =
  1621. (inst->opcode() == spv::Op::OpCooperativeMatrixLoadKHR) ? 3u : 2u;
  1622. const auto colmajor_id = inst->GetOperandAs<uint32_t>(layout_index);
  1623. const auto colmajor = _.FindDef(colmajor_id);
  1624. if (!colmajor || !_.IsIntScalarType(colmajor->type_id()) ||
  1625. !(spvOpcodeIsConstant(colmajor->opcode()) ||
  1626. spvOpcodeIsSpecConstant(colmajor->opcode()))) {
  1627. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1628. << "MemoryLayout operand <id> " << _.getIdName(colmajor_id)
  1629. << " must be a 32-bit integer constant instruction.";
  1630. }
  1631. const auto stride_index =
  1632. (inst->opcode() == spv::Op::OpCooperativeMatrixLoadKHR) ? 4u : 3u;
  1633. if (inst->operands().size() > stride_index) {
  1634. const auto stride_id = inst->GetOperandAs<uint32_t>(stride_index);
  1635. const auto stride = _.FindDef(stride_id);
  1636. if (!stride || !_.IsIntScalarType(stride->type_id())) {
  1637. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1638. << "Stride operand <id> " << _.getIdName(stride_id)
  1639. << " must be a scalar integer type.";
  1640. }
  1641. }
  1642. const auto memory_access_index =
  1643. (inst->opcode() == spv::Op::OpCooperativeMatrixLoadKHR) ? 5u : 4u;
  1644. if (inst->operands().size() > memory_access_index) {
  1645. if (auto error = CheckMemoryAccess(_, inst, memory_access_index))
  1646. return error;
  1647. }
  1648. return SPV_SUCCESS;
  1649. }
  1650. spv_result_t ValidatePtrComparison(ValidationState_t& _,
  1651. const Instruction* inst) {
  1652. if (_.addressing_model() == spv::AddressingModel::Logical &&
  1653. !_.features().variable_pointers) {
  1654. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1655. << "Instruction cannot for logical addressing model be used without "
  1656. "a variable pointers capability";
  1657. }
  1658. const auto result_type = _.FindDef(inst->type_id());
  1659. if (inst->opcode() == spv::Op::OpPtrDiff) {
  1660. if (!result_type || result_type->opcode() != spv::Op::OpTypeInt) {
  1661. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1662. << "Result Type must be an integer scalar";
  1663. }
  1664. } else {
  1665. if (!result_type || result_type->opcode() != spv::Op::OpTypeBool) {
  1666. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1667. << "Result Type must be OpTypeBool";
  1668. }
  1669. }
  1670. const auto op1 = _.FindDef(inst->GetOperandAs<uint32_t>(2u));
  1671. const auto op2 = _.FindDef(inst->GetOperandAs<uint32_t>(3u));
  1672. if (!op1 || !op2 || op1->type_id() != op2->type_id()) {
  1673. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1674. << "The types of Operand 1 and Operand 2 must match";
  1675. }
  1676. const auto op1_type = _.FindDef(op1->type_id());
  1677. if (!op1_type || op1_type->opcode() != spv::Op::OpTypePointer) {
  1678. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1679. << "Operand type must be a pointer";
  1680. }
  1681. spv::StorageClass sc = op1_type->GetOperandAs<spv::StorageClass>(1u);
  1682. if (_.addressing_model() == spv::AddressingModel::Logical) {
  1683. if (sc != spv::StorageClass::Workgroup &&
  1684. sc != spv::StorageClass::StorageBuffer) {
  1685. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1686. << "Invalid pointer storage class";
  1687. }
  1688. if (sc == spv::StorageClass::Workgroup &&
  1689. !_.HasCapability(spv::Capability::VariablePointers)) {
  1690. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1691. << "Workgroup storage class pointer requires VariablePointers "
  1692. "capability to be specified";
  1693. }
  1694. } else if (sc == spv::StorageClass::PhysicalStorageBuffer) {
  1695. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1696. << "Cannot use a pointer in the PhysicalStorageBuffer storage class";
  1697. }
  1698. return SPV_SUCCESS;
  1699. }
  1700. } // namespace
  1701. spv_result_t MemoryPass(ValidationState_t& _, const Instruction* inst) {
  1702. switch (inst->opcode()) {
  1703. case spv::Op::OpVariable:
  1704. if (auto error = ValidateVariable(_, inst)) return error;
  1705. break;
  1706. case spv::Op::OpLoad:
  1707. if (auto error = ValidateLoad(_, inst)) return error;
  1708. break;
  1709. case spv::Op::OpStore:
  1710. if (auto error = ValidateStore(_, inst)) return error;
  1711. break;
  1712. case spv::Op::OpCopyMemory:
  1713. case spv::Op::OpCopyMemorySized:
  1714. if (auto error = ValidateCopyMemory(_, inst)) return error;
  1715. break;
  1716. case spv::Op::OpPtrAccessChain:
  1717. if (auto error = ValidatePtrAccessChain(_, inst)) return error;
  1718. break;
  1719. case spv::Op::OpAccessChain:
  1720. case spv::Op::OpInBoundsAccessChain:
  1721. case spv::Op::OpInBoundsPtrAccessChain:
  1722. if (auto error = ValidateAccessChain(_, inst)) return error;
  1723. break;
  1724. case spv::Op::OpArrayLength:
  1725. if (auto error = ValidateArrayLength(_, inst)) return error;
  1726. break;
  1727. case spv::Op::OpCooperativeMatrixLoadNV:
  1728. case spv::Op::OpCooperativeMatrixStoreNV:
  1729. if (auto error = ValidateCooperativeMatrixLoadStoreNV(_, inst))
  1730. return error;
  1731. break;
  1732. case spv::Op::OpCooperativeMatrixLengthKHR:
  1733. case spv::Op::OpCooperativeMatrixLengthNV:
  1734. if (auto error = ValidateCooperativeMatrixLengthNV(_, inst)) return error;
  1735. break;
  1736. case spv::Op::OpCooperativeMatrixLoadKHR:
  1737. case spv::Op::OpCooperativeMatrixStoreKHR:
  1738. if (auto error = ValidateCooperativeMatrixLoadStoreKHR(_, inst))
  1739. return error;
  1740. break;
  1741. case spv::Op::OpPtrEqual:
  1742. case spv::Op::OpPtrNotEqual:
  1743. case spv::Op::OpPtrDiff:
  1744. if (auto error = ValidatePtrComparison(_, inst)) return error;
  1745. break;
  1746. case spv::Op::OpImageTexelPointer:
  1747. case spv::Op::OpGenericPtrMemSemantics:
  1748. default:
  1749. break;
  1750. }
  1751. return SPV_SUCCESS;
  1752. }
  1753. } // namespace val
  1754. } // namespace spvtools