validate_memory.cpp 127 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097
  1. // Copyright (c) 2018 Google LLC.
  2. // Modifications Copyright (C) 2020-2024 Advanced Micro Devices, Inc. All
  3. // rights 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. std::pair<Instruction*, Instruction*> GetPointerTypes(ValidationState_t& _,
  182. const Instruction* inst) {
  183. Instruction* dst_pointer_type = nullptr;
  184. Instruction* src_pointer_type = nullptr;
  185. switch (inst->opcode()) {
  186. case spv::Op::OpCooperativeMatrixLoadNV:
  187. case spv::Op::OpCooperativeMatrixLoadTensorNV:
  188. case spv::Op::OpCooperativeMatrixLoadKHR:
  189. case spv::Op::OpCooperativeVectorLoadNV:
  190. case spv::Op::OpLoad: {
  191. auto load_pointer = _.FindDef(inst->GetOperandAs<uint32_t>(2));
  192. dst_pointer_type = _.FindDef(load_pointer->type_id());
  193. break;
  194. }
  195. case spv::Op::OpCooperativeMatrixStoreNV:
  196. case spv::Op::OpCooperativeMatrixStoreTensorNV:
  197. case spv::Op::OpCooperativeMatrixStoreKHR:
  198. case spv::Op::OpCooperativeVectorStoreNV:
  199. case spv::Op::OpStore: {
  200. auto store_pointer = _.FindDef(inst->GetOperandAs<uint32_t>(0));
  201. dst_pointer_type = _.FindDef(store_pointer->type_id());
  202. break;
  203. }
  204. // Spec: "Matching Storage Class is not required"
  205. case spv::Op::OpCopyMemory:
  206. case spv::Op::OpCopyMemorySized: {
  207. auto dst_pointer = _.FindDef(inst->GetOperandAs<uint32_t>(0));
  208. dst_pointer_type = _.FindDef(dst_pointer->type_id());
  209. auto src_pointer = _.FindDef(inst->GetOperandAs<uint32_t>(1));
  210. src_pointer_type = _.FindDef(src_pointer->type_id());
  211. break;
  212. }
  213. default:
  214. break;
  215. }
  216. return std::make_pair(dst_pointer_type, src_pointer_type);
  217. }
  218. // Returns the number of instruction words taken up by a memory access
  219. // argument and its implied operands.
  220. int MemoryAccessNumWords(uint32_t mask) {
  221. int result = 1; // Count the mask
  222. if (mask & uint32_t(spv::MemoryAccessMask::Aligned)) ++result;
  223. if (mask & uint32_t(spv::MemoryAccessMask::MakePointerAvailableKHR)) ++result;
  224. if (mask & uint32_t(spv::MemoryAccessMask::MakePointerVisibleKHR)) ++result;
  225. return result;
  226. }
  227. // Returns the scope ID operand for MakeAvailable memory access with mask
  228. // at the given operand index.
  229. // This function is only called for OpLoad, OpStore, OpCopyMemory and
  230. // OpCopyMemorySized, OpCooperativeMatrixLoadNV,
  231. // OpCooperativeMatrixStoreNV, OpCooperativeVectorLoadNV,
  232. // OpCooperativeVectorStoreNV.
  233. uint32_t GetMakeAvailableScope(const Instruction* inst, uint32_t mask,
  234. uint32_t mask_index) {
  235. assert(mask & uint32_t(spv::MemoryAccessMask::MakePointerAvailableKHR));
  236. uint32_t this_bit = uint32_t(spv::MemoryAccessMask::MakePointerAvailableKHR);
  237. uint32_t index =
  238. mask_index - 1 + MemoryAccessNumWords(mask & (this_bit | (this_bit - 1)));
  239. return inst->GetOperandAs<uint32_t>(index);
  240. }
  241. // This function is only called for OpLoad, OpStore, OpCopyMemory,
  242. // OpCopyMemorySized, OpCooperativeMatrixLoadNV,
  243. // OpCooperativeMatrixStoreNV, OpCooperativeVectorLoadNV,
  244. // OpCooperativeVectorStoreNV.
  245. uint32_t GetMakeVisibleScope(const Instruction* inst, uint32_t mask,
  246. uint32_t mask_index) {
  247. assert(mask & uint32_t(spv::MemoryAccessMask::MakePointerVisibleKHR));
  248. uint32_t this_bit = uint32_t(spv::MemoryAccessMask::MakePointerVisibleKHR);
  249. uint32_t index =
  250. mask_index - 1 + MemoryAccessNumWords(mask & (this_bit | (this_bit - 1)));
  251. return inst->GetOperandAs<uint32_t>(index);
  252. }
  253. bool DoesStructContainRTA(const ValidationState_t& _, const Instruction* inst) {
  254. for (size_t member_index = 1; member_index < inst->operands().size();
  255. ++member_index) {
  256. const auto member_id = inst->GetOperandAs<uint32_t>(member_index);
  257. const auto member_type = _.FindDef(member_id);
  258. if (member_type->opcode() == spv::Op::OpTypeRuntimeArray) return true;
  259. }
  260. return false;
  261. }
  262. spv_result_t CheckMemoryAccess(ValidationState_t& _, const Instruction* inst,
  263. uint32_t index) {
  264. Instruction* dst_pointer_type = nullptr;
  265. Instruction* src_pointer_type = nullptr; // only used for OpCopyMemory
  266. std::tie(dst_pointer_type, src_pointer_type) = GetPointerTypes(_, inst);
  267. const spv::StorageClass dst_sc =
  268. dst_pointer_type ? dst_pointer_type->GetOperandAs<spv::StorageClass>(1)
  269. : spv::StorageClass::Max;
  270. const spv::StorageClass src_sc =
  271. src_pointer_type ? src_pointer_type->GetOperandAs<spv::StorageClass>(1)
  272. : spv::StorageClass::Max;
  273. if (inst->operands().size() <= index) {
  274. // Cases where lack of some operand is invalid
  275. if (src_sc == spv::StorageClass::PhysicalStorageBuffer ||
  276. dst_sc == spv::StorageClass::PhysicalStorageBuffer) {
  277. return _.diag(SPV_ERROR_INVALID_ID, inst)
  278. << _.VkErrorID(4708)
  279. << "Memory accesses with PhysicalStorageBuffer must use Aligned.";
  280. }
  281. return SPV_SUCCESS;
  282. }
  283. const uint32_t mask = inst->GetOperandAs<uint32_t>(index);
  284. if (mask & uint32_t(spv::MemoryAccessMask::MakePointerAvailableKHR)) {
  285. if (inst->opcode() == spv::Op::OpLoad ||
  286. inst->opcode() == spv::Op::OpCooperativeMatrixLoadNV ||
  287. inst->opcode() == spv::Op::OpCooperativeMatrixLoadTensorNV ||
  288. inst->opcode() == spv::Op::OpCooperativeMatrixLoadKHR ||
  289. inst->opcode() == spv::Op::OpCooperativeVectorLoadNV) {
  290. return _.diag(SPV_ERROR_INVALID_ID, inst)
  291. << "MakePointerAvailableKHR cannot be used with OpLoad.";
  292. }
  293. if (!(mask & uint32_t(spv::MemoryAccessMask::NonPrivatePointerKHR))) {
  294. return _.diag(SPV_ERROR_INVALID_ID, inst)
  295. << "NonPrivatePointerKHR must be specified if "
  296. "MakePointerAvailableKHR is specified.";
  297. }
  298. // Check the associated scope for MakeAvailableKHR.
  299. const auto available_scope = GetMakeAvailableScope(inst, mask, index);
  300. if (auto error = ValidateMemoryScope(_, inst, available_scope))
  301. return error;
  302. }
  303. if (mask & uint32_t(spv::MemoryAccessMask::MakePointerVisibleKHR)) {
  304. if (inst->opcode() == spv::Op::OpStore ||
  305. inst->opcode() == spv::Op::OpCooperativeMatrixStoreNV ||
  306. inst->opcode() == spv::Op::OpCooperativeMatrixStoreKHR ||
  307. inst->opcode() == spv::Op::OpCooperativeMatrixStoreTensorNV ||
  308. inst->opcode() == spv::Op::OpCooperativeVectorStoreNV) {
  309. return _.diag(SPV_ERROR_INVALID_ID, inst)
  310. << "MakePointerVisibleKHR cannot be used with OpStore.";
  311. }
  312. if (!(mask & uint32_t(spv::MemoryAccessMask::NonPrivatePointerKHR))) {
  313. return _.diag(SPV_ERROR_INVALID_ID, inst)
  314. << "NonPrivatePointerKHR must be specified if "
  315. << "MakePointerVisibleKHR is specified.";
  316. }
  317. // Check the associated scope for MakeVisibleKHR.
  318. const auto visible_scope = GetMakeVisibleScope(inst, mask, index);
  319. if (auto error = ValidateMemoryScope(_, inst, visible_scope)) return error;
  320. }
  321. if (mask & uint32_t(spv::MemoryAccessMask::NonPrivatePointerKHR)) {
  322. if (dst_sc != spv::StorageClass::Uniform &&
  323. dst_sc != spv::StorageClass::Workgroup &&
  324. dst_sc != spv::StorageClass::CrossWorkgroup &&
  325. dst_sc != spv::StorageClass::Generic &&
  326. dst_sc != spv::StorageClass::Image &&
  327. dst_sc != spv::StorageClass::StorageBuffer &&
  328. dst_sc != spv::StorageClass::PhysicalStorageBuffer) {
  329. return _.diag(SPV_ERROR_INVALID_ID, inst)
  330. << "NonPrivatePointerKHR requires a pointer in Uniform, "
  331. << "Workgroup, CrossWorkgroup, Generic, Image or StorageBuffer "
  332. << "storage classes.";
  333. }
  334. if (src_sc != spv::StorageClass::Max &&
  335. src_sc != spv::StorageClass::Uniform &&
  336. src_sc != spv::StorageClass::Workgroup &&
  337. src_sc != spv::StorageClass::CrossWorkgroup &&
  338. src_sc != spv::StorageClass::Generic &&
  339. src_sc != spv::StorageClass::Image &&
  340. src_sc != spv::StorageClass::StorageBuffer &&
  341. src_sc != spv::StorageClass::PhysicalStorageBuffer) {
  342. return _.diag(SPV_ERROR_INVALID_ID, inst)
  343. << "NonPrivatePointerKHR requires a pointer in Uniform, "
  344. << "Workgroup, CrossWorkgroup, Generic, Image or StorageBuffer "
  345. << "storage classes.";
  346. }
  347. }
  348. if (!(mask & uint32_t(spv::MemoryAccessMask::Aligned))) {
  349. if (src_sc == spv::StorageClass::PhysicalStorageBuffer ||
  350. dst_sc == spv::StorageClass::PhysicalStorageBuffer) {
  351. return _.diag(SPV_ERROR_INVALID_ID, inst)
  352. << _.VkErrorID(4708)
  353. << "Memory accesses with PhysicalStorageBuffer must use Aligned.";
  354. }
  355. } else {
  356. // even if there are other masks, the Aligned operand will be next
  357. const uint32_t aligned_value = inst->GetOperandAs<uint32_t>(index + 1);
  358. const bool is_power_of_two =
  359. aligned_value && !(aligned_value & (aligned_value - 1));
  360. if (!is_power_of_two) {
  361. return _.diag(SPV_ERROR_INVALID_ID, inst)
  362. << "Memory accesses Aligned operand value " << aligned_value
  363. << " is not a power of two.";
  364. }
  365. uint32_t largest_scalar = 0;
  366. if (dst_sc == spv::StorageClass::PhysicalStorageBuffer) {
  367. largest_scalar =
  368. _.GetLargestScalarType(dst_pointer_type->GetOperandAs<uint32_t>(2));
  369. }
  370. if (src_sc == spv::StorageClass::PhysicalStorageBuffer) {
  371. largest_scalar = std::max(
  372. largest_scalar,
  373. _.GetLargestScalarType(src_pointer_type->GetOperandAs<uint32_t>(2)));
  374. }
  375. if (aligned_value < largest_scalar) {
  376. return _.diag(SPV_ERROR_INVALID_ID, inst)
  377. << _.VkErrorID(6314) << "Memory accesses Aligned operand value "
  378. << aligned_value << " is too small, the largest scalar type is "
  379. << largest_scalar << " bytes.";
  380. }
  381. }
  382. return SPV_SUCCESS;
  383. }
  384. spv_result_t ValidateVariable(ValidationState_t& _, const Instruction* inst) {
  385. const bool untyped_pointer = inst->opcode() == spv::Op::OpUntypedVariableKHR;
  386. auto result_type = _.FindDef(inst->type_id());
  387. if (untyped_pointer) {
  388. if (!result_type ||
  389. result_type->opcode() != spv::Op::OpTypeUntypedPointerKHR)
  390. return _.diag(SPV_ERROR_INVALID_ID, inst)
  391. << "Result type must be an untyped pointer";
  392. } else {
  393. if (!result_type || result_type->opcode() != spv::Op::OpTypePointer) {
  394. return _.diag(SPV_ERROR_INVALID_ID, inst)
  395. << "OpVariable Result Type <id> " << _.getIdName(inst->type_id())
  396. << " is not a pointer type.";
  397. }
  398. }
  399. const auto storage_class_index = 2u;
  400. auto storage_class =
  401. inst->GetOperandAs<spv::StorageClass>(storage_class_index);
  402. uint32_t value_id = 0;
  403. if (untyped_pointer) {
  404. const auto has_data_type = 3u < inst->operands().size();
  405. if (has_data_type) {
  406. value_id = inst->GetOperandAs<uint32_t>(3u);
  407. auto data_type = _.FindDef(value_id);
  408. if (!data_type || !spvOpcodeGeneratesType(data_type->opcode())) {
  409. return _.diag(SPV_ERROR_INVALID_ID, inst)
  410. << "Data type must be a type instruction";
  411. }
  412. } else {
  413. if (storage_class == spv::StorageClass::Function ||
  414. storage_class == spv::StorageClass::Private ||
  415. storage_class == spv::StorageClass::Workgroup) {
  416. return _.diag(SPV_ERROR_INVALID_ID, inst)
  417. << "Data type must be specified for Function, Private, and "
  418. "Workgroup storage classes";
  419. }
  420. if (spvIsVulkanEnv(_.context()->target_env)) {
  421. return _.diag(SPV_ERROR_INVALID_ID, inst)
  422. << _.VkErrorID(11167)
  423. << "Vulkan requires that data type be specified";
  424. }
  425. }
  426. }
  427. // For OpVariable the data type comes from pointee type of the result type,
  428. // while for OpUntypedVariableKHR the data type comes from the operand.
  429. if (!untyped_pointer) {
  430. value_id = result_type->GetOperandAs<uint32_t>(2);
  431. }
  432. auto value_type = value_id == 0 ? nullptr : _.FindDef(value_id);
  433. const auto initializer_index = untyped_pointer ? 4u : 3u;
  434. if (initializer_index < inst->operands().size()) {
  435. const auto initializer_id = inst->GetOperandAs<uint32_t>(initializer_index);
  436. const auto initializer = _.FindDef(initializer_id);
  437. const auto is_module_scope_var =
  438. initializer &&
  439. (initializer->opcode() == spv::Op::OpVariable ||
  440. initializer->opcode() == spv::Op::OpUntypedVariableKHR) &&
  441. (initializer->GetOperandAs<spv::StorageClass>(storage_class_index) !=
  442. spv::StorageClass::Function);
  443. const auto is_constant =
  444. initializer && spvOpcodeIsConstant(initializer->opcode());
  445. if (!initializer || !(is_constant || is_module_scope_var)) {
  446. return _.diag(SPV_ERROR_INVALID_ID, inst)
  447. << "Variable Initializer <id> " << _.getIdName(initializer_id)
  448. << " is not a constant or module-scope variable.";
  449. }
  450. if (initializer->type_id() != value_id) {
  451. return _.diag(SPV_ERROR_INVALID_ID, inst)
  452. << "Initializer type must match the data type";
  453. }
  454. }
  455. if (storage_class != spv::StorageClass::Workgroup &&
  456. storage_class != spv::StorageClass::CrossWorkgroup &&
  457. storage_class != spv::StorageClass::Private &&
  458. storage_class != spv::StorageClass::Function &&
  459. storage_class != spv::StorageClass::UniformConstant &&
  460. storage_class != spv::StorageClass::RayPayloadKHR &&
  461. storage_class != spv::StorageClass::IncomingRayPayloadKHR &&
  462. storage_class != spv::StorageClass::HitAttributeKHR &&
  463. storage_class != spv::StorageClass::CallableDataKHR &&
  464. storage_class != spv::StorageClass::IncomingCallableDataKHR &&
  465. storage_class != spv::StorageClass::TaskPayloadWorkgroupEXT &&
  466. storage_class != spv::StorageClass::HitObjectAttributeNV &&
  467. storage_class != spv::StorageClass::NodePayloadAMDX) {
  468. bool storage_input_or_output = storage_class == spv::StorageClass::Input ||
  469. storage_class == spv::StorageClass::Output;
  470. bool builtin = false;
  471. if (storage_input_or_output) {
  472. for (const Decoration& decoration : _.id_decorations(inst->id())) {
  473. if (decoration.dec_type() == spv::Decoration::BuiltIn) {
  474. builtin = true;
  475. break;
  476. }
  477. }
  478. }
  479. if (!builtin && value_type &&
  480. ContainsInvalidBool(_, value_type, storage_input_or_output)) {
  481. if (storage_input_or_output) {
  482. return _.diag(SPV_ERROR_INVALID_ID, inst)
  483. << _.VkErrorID(7290)
  484. << "If OpTypeBool is stored in conjunction with OpVariable "
  485. "using Input or Output Storage Classes it requires a BuiltIn "
  486. "decoration";
  487. } else {
  488. return _.diag(SPV_ERROR_INVALID_ID, inst)
  489. << "If OpTypeBool is stored in conjunction with OpVariable, it "
  490. "can only be used with non-externally visible shader Storage "
  491. "Classes: Workgroup, CrossWorkgroup, Private, Function, "
  492. "Input, Output, RayPayloadKHR, IncomingRayPayloadKHR, "
  493. "HitAttributeKHR, CallableDataKHR, "
  494. "IncomingCallableDataKHR, NodePayloadAMDX, or "
  495. "UniformConstant";
  496. }
  497. }
  498. }
  499. if (!_.IsValidStorageClass(storage_class)) {
  500. return _.diag(SPV_ERROR_INVALID_BINARY, inst)
  501. << _.VkErrorID(4643)
  502. << "Invalid storage class for target environment";
  503. }
  504. if (storage_class == spv::StorageClass::Generic) {
  505. return _.diag(SPV_ERROR_INVALID_BINARY, inst)
  506. << "Variable storage class cannot be Generic";
  507. }
  508. if (inst->function() && storage_class != spv::StorageClass::Function) {
  509. return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
  510. << "Variables must have a function[7] storage class inside"
  511. " of a function";
  512. }
  513. if (!inst->function() && storage_class == spv::StorageClass::Function) {
  514. return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
  515. << "Variables can not have a function[7] storage class "
  516. "outside of a function";
  517. }
  518. // SPIR-V 3.32.8: Check that pointer type and variable type have the same
  519. // storage class.
  520. const auto result_storage_class_index = 1;
  521. const auto result_storage_class =
  522. result_type->GetOperandAs<spv::StorageClass>(result_storage_class_index);
  523. if (storage_class != result_storage_class) {
  524. return _.diag(SPV_ERROR_INVALID_ID, inst)
  525. << "Storage class must match result type storage class";
  526. }
  527. // Variable pointer related restrictions.
  528. const auto pointee = untyped_pointer
  529. ? value_id == 0 ? nullptr : _.FindDef(value_id)
  530. : _.FindDef(result_type->word(3));
  531. if (_.addressing_model() == spv::AddressingModel::Logical &&
  532. !_.options()->relax_logical_pointer) {
  533. // VariablePointersStorageBuffer is implied by VariablePointers.
  534. if (pointee && pointee->opcode() == spv::Op::OpTypePointer) {
  535. if (!_.HasCapability(spv::Capability::VariablePointersStorageBuffer)) {
  536. return _.diag(SPV_ERROR_INVALID_ID, inst)
  537. << "In Logical addressing, variables may not allocate a pointer "
  538. << "type";
  539. } else if (storage_class != spv::StorageClass::Function &&
  540. storage_class != spv::StorageClass::Private) {
  541. return _.diag(SPV_ERROR_INVALID_ID, inst)
  542. << "In Logical addressing with variable pointers, variables "
  543. << "that allocate pointers must be in Function or Private "
  544. << "storage classes";
  545. }
  546. }
  547. }
  548. if (spvIsVulkanEnv(_.context()->target_env)) {
  549. // Vulkan Push Constant Interface section: Check type of PushConstant
  550. // variables.
  551. if (storage_class == spv::StorageClass::PushConstant) {
  552. if (pointee && pointee->opcode() != spv::Op::OpTypeStruct) {
  553. return _.diag(SPV_ERROR_INVALID_ID, inst)
  554. << _.VkErrorID(6808) << "PushConstant OpVariable <id> "
  555. << _.getIdName(inst->id()) << " has illegal type.\n"
  556. << "From Vulkan spec, Push Constant Interface section:\n"
  557. << "Such variables must be typed as OpTypeStruct";
  558. }
  559. }
  560. // Vulkan Descriptor Set Interface: Check type of UniformConstant and
  561. // Uniform variables.
  562. if (storage_class == spv::StorageClass::UniformConstant) {
  563. if (pointee && !IsAllowedTypeOrArrayOfSame(
  564. _, pointee,
  565. {spv::Op::OpTypeImage, spv::Op::OpTypeSampler,
  566. spv::Op::OpTypeSampledImage, spv::Op::OpTypeTensorARM,
  567. spv::Op::OpTypeAccelerationStructureKHR})) {
  568. return _.diag(SPV_ERROR_INVALID_ID, inst)
  569. << _.VkErrorID(4655) << "UniformConstant OpVariable <id> "
  570. << _.getIdName(inst->id()) << " has illegal type.\n"
  571. << "Variables identified with the UniformConstant storage class "
  572. << "are used only as handles to refer to opaque resources. Such "
  573. << "variables must be typed as OpTypeImage, OpTypeSampler, "
  574. << "OpTypeSampledImage, OpTypeAccelerationStructureKHR, "
  575. << "or an array of one of these types.";
  576. }
  577. }
  578. if (storage_class == spv::StorageClass::Uniform) {
  579. if (pointee &&
  580. !IsAllowedTypeOrArrayOfSame(_, pointee, {spv::Op::OpTypeStruct})) {
  581. return _.diag(SPV_ERROR_INVALID_ID, inst)
  582. << _.VkErrorID(6807) << "Uniform OpVariable <id> "
  583. << _.getIdName(inst->id()) << " has illegal type.\n"
  584. << "From Vulkan spec:\n"
  585. << "Variables identified with the Uniform storage class are "
  586. << "used to access transparent buffer backed resources. Such "
  587. << "variables must be typed as OpTypeStruct, or an array of "
  588. << "this type";
  589. }
  590. }
  591. if (storage_class == spv::StorageClass::StorageBuffer) {
  592. if (pointee &&
  593. !IsAllowedTypeOrArrayOfSame(_, pointee, {spv::Op::OpTypeStruct})) {
  594. return _.diag(SPV_ERROR_INVALID_ID, inst)
  595. << _.VkErrorID(6807) << "StorageBuffer OpVariable <id> "
  596. << _.getIdName(inst->id()) << " has illegal type.\n"
  597. << "From Vulkan spec:\n"
  598. << "Variables identified with the StorageBuffer storage class "
  599. "are used to access transparent buffer backed resources. "
  600. "Such variables must be typed as OpTypeStruct, or an array "
  601. "of this type";
  602. }
  603. }
  604. // Check for invalid use of Invariant
  605. if (storage_class != spv::StorageClass::Input &&
  606. storage_class != spv::StorageClass::Output) {
  607. if (_.HasDecoration(inst->id(), spv::Decoration::Invariant)) {
  608. return _.diag(SPV_ERROR_INVALID_ID, inst)
  609. << _.VkErrorID(4677)
  610. << "Variable decorated with Invariant must only be identified "
  611. "with the Input or Output storage class in Vulkan "
  612. "environment.";
  613. }
  614. // Need to check if only the members in a struct are decorated
  615. if (value_type && value_type->opcode() == spv::Op::OpTypeStruct) {
  616. if (_.HasDecoration(value_id, spv::Decoration::Invariant)) {
  617. return _.diag(SPV_ERROR_INVALID_ID, inst)
  618. << _.VkErrorID(4677)
  619. << "Variable struct member decorated with Invariant must only "
  620. "be identified with the Input or Output storage class in "
  621. "Vulkan environment.";
  622. }
  623. }
  624. }
  625. }
  626. // Vulkan Appendix A: Check that if contains initializer, then
  627. // storage class is Output, Private, or Function.
  628. if (inst->operands().size() > initializer_index &&
  629. storage_class != spv::StorageClass::Output &&
  630. storage_class != spv::StorageClass::Private &&
  631. storage_class != spv::StorageClass::Function) {
  632. if (spvIsVulkanEnv(_.context()->target_env)) {
  633. if (storage_class == spv::StorageClass::Workgroup) {
  634. auto init_id = inst->GetOperandAs<uint32_t>(initializer_index);
  635. auto init = _.FindDef(init_id);
  636. if (init->opcode() != spv::Op::OpConstantNull) {
  637. return _.diag(SPV_ERROR_INVALID_ID, inst)
  638. << _.VkErrorID(4734) << "OpVariable, <id> "
  639. << _.getIdName(inst->id())
  640. << ", initializers are limited to OpConstantNull in "
  641. "Workgroup "
  642. "storage class";
  643. }
  644. } else if (storage_class != spv::StorageClass::Output &&
  645. storage_class != spv::StorageClass::Private &&
  646. storage_class != spv::StorageClass::Function) {
  647. return _.diag(SPV_ERROR_INVALID_ID, inst)
  648. << _.VkErrorID(4651) << "OpVariable, <id> "
  649. << _.getIdName(inst->id())
  650. << ", has a disallowed initializer & storage class "
  651. << "combination.\n"
  652. << "From " << spvLogStringForEnv(_.context()->target_env)
  653. << " spec:\n"
  654. << "Variable declarations that include initializers must have "
  655. << "one of the following storage classes: Output, Private, "
  656. << "Function or Workgroup";
  657. }
  658. }
  659. }
  660. if (initializer_index < inst->operands().size()) {
  661. if (storage_class == spv::StorageClass::TaskPayloadWorkgroupEXT) {
  662. return _.diag(SPV_ERROR_INVALID_ID, inst)
  663. << "OpVariable, <id> " << _.getIdName(inst->id())
  664. << ", initializer are not allowed for TaskPayloadWorkgroupEXT";
  665. }
  666. if (storage_class == spv::StorageClass::Input) {
  667. return _.diag(SPV_ERROR_INVALID_ID, inst)
  668. << "OpVariable, <id> " << _.getIdName(inst->id())
  669. << ", initializer are not allowed for Input";
  670. }
  671. if (storage_class == spv::StorageClass::HitObjectAttributeNV) {
  672. return _.diag(SPV_ERROR_INVALID_ID, inst)
  673. << "OpVariable, <id> " << _.getIdName(inst->id())
  674. << ", initializer are not allowed for HitObjectAttributeNV";
  675. }
  676. }
  677. if (storage_class == spv::StorageClass::PhysicalStorageBuffer) {
  678. return _.diag(SPV_ERROR_INVALID_ID, inst)
  679. << "PhysicalStorageBuffer must not be used with OpVariable.";
  680. }
  681. // Vulkan specific validation rules for OpTypeRuntimeArray
  682. if (spvIsVulkanEnv(_.context()->target_env)) {
  683. // OpTypeRuntimeArray should only ever be in a container like OpTypeStruct,
  684. // so should never appear as a bare variable.
  685. // Unless the module has the RuntimeDescriptorArrayEXT capability.
  686. if (value_type && value_type->opcode() == spv::Op::OpTypeRuntimeArray) {
  687. if (!_.HasCapability(spv::Capability::RuntimeDescriptorArrayEXT)) {
  688. return _.diag(SPV_ERROR_INVALID_ID, inst)
  689. << _.VkErrorID(4680) << "OpVariable, <id> "
  690. << _.getIdName(inst->id())
  691. << ", is attempting to create memory for an illegal type, "
  692. << "OpTypeRuntimeArray.\nFor Vulkan OpTypeRuntimeArray can only "
  693. << "appear as the final member of an OpTypeStruct, thus cannot "
  694. << "be instantiated via OpVariable";
  695. } else {
  696. // A bare variable OpTypeRuntimeArray is allowed in this context, but
  697. // still need to check the storage class.
  698. if (storage_class != spv::StorageClass::StorageBuffer &&
  699. storage_class != spv::StorageClass::Uniform &&
  700. storage_class != spv::StorageClass::UniformConstant) {
  701. return _.diag(SPV_ERROR_INVALID_ID, inst)
  702. << _.VkErrorID(4680)
  703. << "For Vulkan with RuntimeDescriptorArrayEXT, a variable "
  704. << "containing OpTypeRuntimeArray must have storage class of "
  705. << "StorageBuffer, Uniform, or UniformConstant.";
  706. }
  707. }
  708. }
  709. // If an OpStruct has an OpTypeRuntimeArray somewhere within it, then it
  710. // must either have the storage class StorageBuffer and be decorated
  711. // with Block, or it must be in the Uniform storage class and be decorated
  712. // as BufferBlock.
  713. if (value_type && value_type->opcode() == spv::Op::OpTypeStruct) {
  714. if (DoesStructContainRTA(_, value_type)) {
  715. if (storage_class == spv::StorageClass::StorageBuffer ||
  716. storage_class == spv::StorageClass::PhysicalStorageBuffer) {
  717. if (!_.HasDecoration(value_id, spv::Decoration::Block)) {
  718. return _.diag(SPV_ERROR_INVALID_ID, inst)
  719. << _.VkErrorID(4680)
  720. << "For Vulkan, an OpTypeStruct variable containing an "
  721. << "OpTypeRuntimeArray must be decorated with Block if it "
  722. << "has storage class StorageBuffer or "
  723. "PhysicalStorageBuffer.";
  724. }
  725. } else if (storage_class == spv::StorageClass::Uniform) {
  726. if (!_.HasDecoration(value_id, spv::Decoration::BufferBlock)) {
  727. return _.diag(SPV_ERROR_INVALID_ID, inst)
  728. << _.VkErrorID(4680)
  729. << "For Vulkan, an OpTypeStruct variable containing an "
  730. << "OpTypeRuntimeArray must be decorated with BufferBlock "
  731. << "if it has storage class Uniform.";
  732. }
  733. } else {
  734. return _.diag(SPV_ERROR_INVALID_ID, inst)
  735. << _.VkErrorID(4680)
  736. << "For Vulkan, OpTypeStruct variables containing "
  737. << "OpTypeRuntimeArray must have storage class of "
  738. << "StorageBuffer, PhysicalStorageBuffer, or Uniform.";
  739. }
  740. }
  741. }
  742. }
  743. // Cooperative matrix types can only be allocated in Function or Private
  744. if ((storage_class != spv::StorageClass::Function &&
  745. storage_class != spv::StorageClass::Private) &&
  746. pointee &&
  747. _.ContainsType(pointee->id(), [](const Instruction* type_inst) {
  748. auto opcode = type_inst->opcode();
  749. return opcode == spv::Op::OpTypeCooperativeMatrixNV ||
  750. opcode == spv::Op::OpTypeCooperativeMatrixKHR;
  751. })) {
  752. return _.diag(SPV_ERROR_INVALID_ID, inst)
  753. << "Cooperative matrix types (or types containing them) can only be "
  754. "allocated "
  755. << "in Function or Private storage classes or as function "
  756. "parameters";
  757. }
  758. if ((storage_class != spv::StorageClass::Function &&
  759. storage_class != spv::StorageClass::Private) &&
  760. pointee &&
  761. _.ContainsType(pointee->id(), [](const Instruction* type_inst) {
  762. auto opcode = type_inst->opcode();
  763. return opcode == spv::Op::OpTypeCooperativeVectorNV;
  764. })) {
  765. return _.diag(SPV_ERROR_INVALID_ID, inst)
  766. << "Cooperative vector types (or types containing them) can only be "
  767. "allocated "
  768. << "in Function or Private storage classes or as function "
  769. "parameters";
  770. }
  771. if (_.HasCapability(spv::Capability::Shader)) {
  772. // Don't allow variables containing 16-bit elements without the appropriate
  773. // capabilities.
  774. if ((!_.HasCapability(spv::Capability::Int16) &&
  775. _.ContainsSizedIntOrFloatType(value_id, spv::Op::OpTypeInt, 16)) ||
  776. (!_.HasCapability(spv::Capability::Float16) &&
  777. _.ContainsSizedIntOrFloatType(value_id, spv::Op::OpTypeFloat, 16))) {
  778. auto underlying_type = value_type;
  779. while (underlying_type &&
  780. underlying_type->opcode() == spv::Op::OpTypePointer) {
  781. storage_class = underlying_type->GetOperandAs<spv::StorageClass>(1u);
  782. underlying_type =
  783. _.FindDef(underlying_type->GetOperandAs<uint32_t>(2u));
  784. }
  785. bool storage_class_ok = true;
  786. std::string sc_name = _.grammar().lookupOperandName(
  787. SPV_OPERAND_TYPE_STORAGE_CLASS, uint32_t(storage_class));
  788. switch (storage_class) {
  789. case spv::StorageClass::StorageBuffer:
  790. case spv::StorageClass::PhysicalStorageBuffer:
  791. if (!_.HasCapability(spv::Capability::StorageBuffer16BitAccess)) {
  792. storage_class_ok = false;
  793. }
  794. break;
  795. case spv::StorageClass::Uniform:
  796. if (underlying_type &&
  797. !_.HasCapability(
  798. spv::Capability::UniformAndStorageBuffer16BitAccess)) {
  799. if (underlying_type->opcode() == spv::Op::OpTypeArray ||
  800. underlying_type->opcode() == spv::Op::OpTypeRuntimeArray) {
  801. underlying_type =
  802. _.FindDef(underlying_type->GetOperandAs<uint32_t>(1u));
  803. }
  804. if (!_.HasCapability(spv::Capability::StorageBuffer16BitAccess) ||
  805. !_.HasDecoration(underlying_type->id(),
  806. spv::Decoration::BufferBlock)) {
  807. storage_class_ok = false;
  808. }
  809. }
  810. break;
  811. case spv::StorageClass::PushConstant:
  812. if (!_.HasCapability(spv::Capability::StoragePushConstant16)) {
  813. storage_class_ok = false;
  814. }
  815. break;
  816. case spv::StorageClass::Input:
  817. case spv::StorageClass::Output:
  818. if (!_.HasCapability(spv::Capability::StorageInputOutput16)) {
  819. storage_class_ok = false;
  820. }
  821. break;
  822. case spv::StorageClass::Workgroup:
  823. if (!_.HasCapability(
  824. spv::Capability::
  825. WorkgroupMemoryExplicitLayout16BitAccessKHR)) {
  826. storage_class_ok = false;
  827. }
  828. break;
  829. default:
  830. return _.diag(SPV_ERROR_INVALID_ID, inst)
  831. << "Cannot allocate a variable containing a 16-bit type in "
  832. << sc_name << " storage class";
  833. }
  834. if (!storage_class_ok) {
  835. return _.diag(SPV_ERROR_INVALID_ID, inst)
  836. << "Allocating a variable containing a 16-bit element in "
  837. << sc_name << " storage class requires an additional capability";
  838. }
  839. }
  840. // Don't allow variables containing 8-bit elements without the appropriate
  841. // capabilities.
  842. if (!_.HasCapability(spv::Capability::Int8) &&
  843. _.ContainsSizedIntOrFloatType(value_id, spv::Op::OpTypeInt, 8)) {
  844. auto underlying_type = value_type;
  845. while (underlying_type &&
  846. underlying_type->opcode() == spv::Op::OpTypePointer) {
  847. storage_class = underlying_type->GetOperandAs<spv::StorageClass>(1u);
  848. underlying_type =
  849. _.FindDef(underlying_type->GetOperandAs<uint32_t>(2u));
  850. }
  851. bool storage_class_ok = true;
  852. std::string sc_name = _.grammar().lookupOperandName(
  853. SPV_OPERAND_TYPE_STORAGE_CLASS, uint32_t(storage_class));
  854. switch (storage_class) {
  855. case spv::StorageClass::StorageBuffer:
  856. case spv::StorageClass::PhysicalStorageBuffer:
  857. if (!_.HasCapability(spv::Capability::StorageBuffer8BitAccess)) {
  858. storage_class_ok = false;
  859. }
  860. break;
  861. case spv::StorageClass::Uniform:
  862. if (underlying_type &&
  863. !_.HasCapability(
  864. spv::Capability::UniformAndStorageBuffer8BitAccess)) {
  865. if (underlying_type->opcode() == spv::Op::OpTypeArray ||
  866. underlying_type->opcode() == spv::Op::OpTypeRuntimeArray) {
  867. underlying_type =
  868. _.FindDef(underlying_type->GetOperandAs<uint32_t>(1u));
  869. }
  870. if (!_.HasCapability(spv::Capability::StorageBuffer8BitAccess) ||
  871. !_.HasDecoration(underlying_type->id(),
  872. spv::Decoration::BufferBlock)) {
  873. storage_class_ok = false;
  874. }
  875. }
  876. break;
  877. case spv::StorageClass::PushConstant:
  878. if (!_.HasCapability(spv::Capability::StoragePushConstant8)) {
  879. storage_class_ok = false;
  880. }
  881. break;
  882. case spv::StorageClass::Workgroup:
  883. if (!_.HasCapability(
  884. spv::Capability::
  885. WorkgroupMemoryExplicitLayout8BitAccessKHR)) {
  886. storage_class_ok = false;
  887. }
  888. break;
  889. default:
  890. return _.diag(SPV_ERROR_INVALID_ID, inst)
  891. << "Cannot allocate a variable containing a 8-bit type in "
  892. << sc_name << " storage class";
  893. }
  894. if (!storage_class_ok) {
  895. return _.diag(SPV_ERROR_INVALID_ID, inst)
  896. << "Allocating a variable containing a 8-bit element in "
  897. << sc_name << " storage class requires an additional capability";
  898. }
  899. }
  900. }
  901. if (_.HasCapability(spv::Capability::TileShadingQCOM) &&
  902. storage_class == spv::StorageClass::TileAttachmentQCOM) {
  903. if (result_type->opcode() == spv::Op::OpTypePointer) {
  904. const auto pointee_type =
  905. _.FindDef(result_type->GetOperandAs<uint32_t>(2));
  906. if (pointee_type && pointee_type->opcode() == spv::Op::OpTypeImage) {
  907. spv::Dim dim = static_cast<spv::Dim>(pointee_type->word(3));
  908. if (dim != spv::Dim::Dim2D) {
  909. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  910. << "Any OpTypeImage variable in the TileAttachmentQCOM "
  911. "Storage Class must "
  912. "have 2D as its dimension";
  913. }
  914. unsigned sampled = pointee_type->word(7);
  915. if (sampled != 1 && sampled != 2) {
  916. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  917. << "Any OpyTpeImage variable in the TileAttachmentQCOM "
  918. "Storage Class must "
  919. "have 1 or 2 as Image 'Sampled' parameter";
  920. }
  921. for (const auto& pair_o : inst->uses()) {
  922. const auto* use_inst_o = pair_o.first;
  923. if (use_inst_o->opcode() == spv::Op::OpLoad) {
  924. for (const auto& pair_i : use_inst_o->uses()) {
  925. const auto* use_inst_i = pair_i.first;
  926. switch (use_inst_i->opcode()) {
  927. case spv::Op::OpImageQueryFormat:
  928. case spv::Op::OpImageQueryOrder:
  929. case spv::Op::OpImageQuerySizeLod:
  930. case spv::Op::OpImageQuerySize:
  931. case spv::Op::OpImageQueryLod:
  932. case spv::Op::OpImageQueryLevels:
  933. case spv::Op::OpImageQuerySamples:
  934. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  935. << "Any variable in the TileAttachmentQCOM Storage "
  936. "Class must "
  937. "not be consumed by an OpImageQuery* instruction";
  938. default:
  939. break;
  940. }
  941. }
  942. }
  943. }
  944. }
  945. }
  946. if (!(_.HasDecoration(inst->id(), spv::Decoration::DescriptorSet) &&
  947. _.HasDecoration(inst->id(), spv::Decoration::Binding))) {
  948. return _.diag(SPV_ERROR_INVALID_ID, inst)
  949. << "Any variable in the TileAttachmentQCOM Storage Class must "
  950. "be decorated with DescriptorSet and Binding";
  951. }
  952. if (_.HasDecoration(inst->id(), spv::Decoration::Component)) {
  953. return _.diag(SPV_ERROR_INVALID_ID, inst)
  954. << "Any variable in the TileAttachmentQCOM Storage Class must "
  955. "not be decorated with Component decoration";
  956. }
  957. }
  958. return SPV_SUCCESS;
  959. }
  960. spv_result_t ValidateLoad(ValidationState_t& _, const Instruction* inst) {
  961. const auto result_type = _.FindDef(inst->type_id());
  962. if (!result_type) {
  963. return _.diag(SPV_ERROR_INVALID_ID, inst)
  964. << "OpLoad Result Type <id> " << _.getIdName(inst->type_id())
  965. << " is not defined.";
  966. }
  967. const auto pointer_index = 2;
  968. const auto pointer_id = inst->GetOperandAs<uint32_t>(pointer_index);
  969. const auto pointer = _.FindDef(pointer_id);
  970. if (!pointer ||
  971. ((_.addressing_model() == spv::AddressingModel::Logical) &&
  972. ((!_.features().variable_pointers &&
  973. !spvOpcodeReturnsLogicalPointer(pointer->opcode())) ||
  974. (_.features().variable_pointers &&
  975. !spvOpcodeReturnsLogicalVariablePointer(pointer->opcode()))))) {
  976. return _.diag(SPV_ERROR_INVALID_ID, inst)
  977. << "OpLoad Pointer <id> " << _.getIdName(pointer_id)
  978. << " is not a logical pointer.";
  979. }
  980. const auto pointer_type = _.FindDef(pointer->type_id());
  981. if (!pointer_type ||
  982. (pointer_type->opcode() != spv::Op::OpTypePointer &&
  983. pointer_type->opcode() != spv::Op::OpTypeUntypedPointerKHR)) {
  984. return _.diag(SPV_ERROR_INVALID_ID, inst)
  985. << "OpLoad type for pointer <id> " << _.getIdName(pointer_id)
  986. << " is not a pointer type.";
  987. }
  988. if (pointer_type->opcode() == spv::Op::OpTypePointer) {
  989. const auto pointee_type =
  990. _.FindDef(pointer_type->GetOperandAs<uint32_t>(2));
  991. if (!pointee_type || result_type->id() != pointee_type->id()) {
  992. return _.diag(SPV_ERROR_INVALID_ID, inst)
  993. << "OpLoad Result Type <id> " << _.getIdName(inst->type_id())
  994. << " does not match Pointer <id> " << _.getIdName(pointer->id())
  995. << "s type.";
  996. }
  997. }
  998. if (!_.options()->before_hlsl_legalization &&
  999. _.ContainsRuntimeArray(inst->type_id())) {
  1000. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1001. << "Cannot load a runtime-sized array";
  1002. }
  1003. if (auto error = CheckMemoryAccess(_, inst, 3)) return error;
  1004. if (_.HasCapability(spv::Capability::Shader) &&
  1005. _.ContainsLimitedUseIntOrFloatType(inst->type_id()) &&
  1006. result_type->opcode() != spv::Op::OpTypePointer) {
  1007. if (result_type->opcode() != spv::Op::OpTypeInt &&
  1008. result_type->opcode() != spv::Op::OpTypeFloat &&
  1009. result_type->opcode() != spv::Op::OpTypeVector &&
  1010. result_type->opcode() != spv::Op::OpTypeMatrix) {
  1011. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1012. << "8- or 16-bit loads must be a scalar, vector or matrix type";
  1013. }
  1014. }
  1015. _.RegisterQCOMImageProcessingTextureConsumer(pointer_id, inst, nullptr);
  1016. return SPV_SUCCESS;
  1017. }
  1018. spv_result_t ValidateStore(ValidationState_t& _, const Instruction* inst) {
  1019. const auto pointer_index = 0;
  1020. const auto pointer_id = inst->GetOperandAs<uint32_t>(pointer_index);
  1021. const auto pointer = _.FindDef(pointer_id);
  1022. if (!pointer ||
  1023. (_.addressing_model() == spv::AddressingModel::Logical &&
  1024. ((!_.features().variable_pointers &&
  1025. !spvOpcodeReturnsLogicalPointer(pointer->opcode())) ||
  1026. (_.features().variable_pointers &&
  1027. !spvOpcodeReturnsLogicalVariablePointer(pointer->opcode()))))) {
  1028. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1029. << "OpStore Pointer <id> " << _.getIdName(pointer_id)
  1030. << " is not a logical pointer.";
  1031. }
  1032. const auto pointer_type = _.FindDef(pointer->type_id());
  1033. if (!pointer_type ||
  1034. (pointer_type->opcode() != spv::Op::OpTypePointer &&
  1035. pointer_type->opcode() != spv::Op::OpTypeUntypedPointerKHR)) {
  1036. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1037. << "OpStore type for pointer <id> " << _.getIdName(pointer_id)
  1038. << " is not a pointer type.";
  1039. }
  1040. Instruction* type = nullptr;
  1041. if (pointer_type->opcode() == spv::Op::OpTypePointer) {
  1042. const auto type_id = pointer_type->GetOperandAs<uint32_t>(2);
  1043. type = _.FindDef(type_id);
  1044. if (!type || spv::Op::OpTypeVoid == type->opcode()) {
  1045. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1046. << "OpStore Pointer <id> " << _.getIdName(pointer_id)
  1047. << "s type is void.";
  1048. }
  1049. }
  1050. // validate storage class
  1051. {
  1052. uint32_t data_type;
  1053. spv::StorageClass storage_class;
  1054. if (!_.GetPointerTypeInfo(pointer_type->id(), &data_type, &storage_class)) {
  1055. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1056. << "OpStore Pointer <id> " << _.getIdName(pointer_id)
  1057. << " is not pointer type";
  1058. }
  1059. if (storage_class == spv::StorageClass::UniformConstant ||
  1060. storage_class == spv::StorageClass::Input ||
  1061. storage_class == spv::StorageClass::PushConstant) {
  1062. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1063. << "OpStore Pointer <id> " << _.getIdName(pointer_id)
  1064. << " storage class is read-only";
  1065. } else if (storage_class == spv::StorageClass::ShaderRecordBufferKHR) {
  1066. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1067. << "ShaderRecordBufferKHR Storage Class variables are read only";
  1068. } else if (storage_class == spv::StorageClass::HitAttributeKHR) {
  1069. std::string errorVUID = _.VkErrorID(4703);
  1070. _.function(inst->function()->id())
  1071. ->RegisterExecutionModelLimitation(
  1072. [errorVUID](spv::ExecutionModel model, std::string* message) {
  1073. if (model == spv::ExecutionModel::AnyHitKHR ||
  1074. model == spv::ExecutionModel::ClosestHitKHR) {
  1075. if (message) {
  1076. *message =
  1077. errorVUID +
  1078. "HitAttributeKHR Storage Class variables are read only "
  1079. "with AnyHitKHR and ClosestHitKHR";
  1080. }
  1081. return false;
  1082. }
  1083. return true;
  1084. });
  1085. }
  1086. if (spvIsVulkanEnv(_.context()->target_env) &&
  1087. storage_class == spv::StorageClass::Uniform) {
  1088. auto base_ptr = _.TracePointer(pointer);
  1089. if (base_ptr->opcode() == spv::Op::OpVariable) {
  1090. // If it's not a variable a different check should catch the problem.
  1091. auto base_type = _.FindDef(base_ptr->GetOperandAs<uint32_t>(0));
  1092. // Get the pointed-to type.
  1093. base_type = _.FindDef(base_type->GetOperandAs<uint32_t>(2u));
  1094. if (base_type->opcode() == spv::Op::OpTypeArray ||
  1095. base_type->opcode() == spv::Op::OpTypeRuntimeArray) {
  1096. base_type = _.FindDef(base_type->GetOperandAs<uint32_t>(1u));
  1097. }
  1098. if (_.HasDecoration(base_type->id(), spv::Decoration::Block)) {
  1099. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1100. << _.VkErrorID(6925)
  1101. << "In the Vulkan environment, cannot store to Uniform Blocks";
  1102. }
  1103. }
  1104. }
  1105. }
  1106. const auto object_index = 1;
  1107. const auto object_id = inst->GetOperandAs<uint32_t>(object_index);
  1108. const auto object = _.FindDef(object_id);
  1109. if (!object || !object->type_id()) {
  1110. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1111. << "OpStore Object <id> " << _.getIdName(object_id)
  1112. << " is not an object.";
  1113. }
  1114. const auto object_type = _.FindDef(object->type_id());
  1115. if (!object_type || spv::Op::OpTypeVoid == object_type->opcode()) {
  1116. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1117. << "OpStore Object <id> " << _.getIdName(object_id)
  1118. << "s type is void.";
  1119. }
  1120. if (type && (type->id() != object_type->id())) {
  1121. if (!_.options()->relax_struct_store ||
  1122. type->opcode() != spv::Op::OpTypeStruct ||
  1123. object_type->opcode() != spv::Op::OpTypeStruct) {
  1124. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1125. << "OpStore Pointer <id> " << _.getIdName(pointer_id)
  1126. << "s type does not match Object <id> "
  1127. << _.getIdName(object->id()) << "s type.";
  1128. }
  1129. // TODO: Check for layout compatible matricies and arrays as well.
  1130. if (!AreLayoutCompatibleStructs(_, type, object_type)) {
  1131. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1132. << "OpStore Pointer <id> " << _.getIdName(pointer_id)
  1133. << "s layout does not match Object <id> "
  1134. << _.getIdName(object->id()) << "s layout.";
  1135. }
  1136. }
  1137. if (auto error = CheckMemoryAccess(_, inst, 2)) return error;
  1138. if (_.HasCapability(spv::Capability::Shader) &&
  1139. _.ContainsLimitedUseIntOrFloatType(inst->type_id()) &&
  1140. object_type->opcode() != spv::Op::OpTypePointer) {
  1141. if (object_type->opcode() != spv::Op::OpTypeInt &&
  1142. object_type->opcode() != spv::Op::OpTypeFloat &&
  1143. object_type->opcode() != spv::Op::OpTypeVector &&
  1144. object_type->opcode() != spv::Op::OpTypeMatrix) {
  1145. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1146. << "8- or 16-bit stores must be a scalar, vector or matrix type";
  1147. }
  1148. }
  1149. if (spvIsVulkanEnv(_.context()->target_env) &&
  1150. !_.options()->before_hlsl_legalization) {
  1151. const auto isForbiddenType = [](const Instruction* type_inst) {
  1152. auto opcode = type_inst->opcode();
  1153. return opcode == spv::Op::OpTypeImage ||
  1154. opcode == spv::Op::OpTypeSampler ||
  1155. opcode == spv::Op::OpTypeSampledImage ||
  1156. opcode == spv::Op::OpTypeAccelerationStructureKHR;
  1157. };
  1158. if (_.ContainsType(object_type->id(), isForbiddenType)) {
  1159. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1160. << _.VkErrorID(6924)
  1161. << "Cannot store to OpTypeImage, OpTypeSampler, "
  1162. "OpTypeSampledImage, or OpTypeAccelerationStructureKHR objects";
  1163. }
  1164. }
  1165. return SPV_SUCCESS;
  1166. }
  1167. spv_result_t ValidateCopyMemoryMemoryAccess(ValidationState_t& _,
  1168. const Instruction* inst) {
  1169. assert(inst->opcode() == spv::Op::OpCopyMemory ||
  1170. inst->opcode() == spv::Op::OpCopyMemorySized);
  1171. const uint32_t first_access_index =
  1172. inst->opcode() == spv::Op::OpCopyMemory ? 2 : 3;
  1173. if (inst->operands().size() > first_access_index) {
  1174. if (auto error = CheckMemoryAccess(_, inst, first_access_index))
  1175. return error;
  1176. const auto first_access = inst->GetOperandAs<uint32_t>(first_access_index);
  1177. const uint32_t second_access_index =
  1178. first_access_index + MemoryAccessNumWords(first_access);
  1179. if (inst->operands().size() > second_access_index) {
  1180. if (_.features().copy_memory_permits_two_memory_accesses) {
  1181. if (auto error = CheckMemoryAccess(_, inst, second_access_index))
  1182. return error;
  1183. // In the two-access form in SPIR-V 1.4 and later:
  1184. // - the first is the target (write) access and it can't have
  1185. // make-visible.
  1186. // - the second is the source (read) access and it can't have
  1187. // make-available.
  1188. if (first_access &
  1189. uint32_t(spv::MemoryAccessMask::MakePointerVisibleKHR)) {
  1190. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1191. << "Target memory access must not include "
  1192. "MakePointerVisibleKHR";
  1193. }
  1194. const auto second_access =
  1195. inst->GetOperandAs<uint32_t>(second_access_index);
  1196. if (second_access &
  1197. uint32_t(spv::MemoryAccessMask::MakePointerAvailableKHR)) {
  1198. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1199. << "Source memory access must not include "
  1200. "MakePointerAvailableKHR";
  1201. }
  1202. } else {
  1203. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1204. << spvOpcodeString(static_cast<spv::Op>(inst->opcode()))
  1205. << " with two memory access operands requires SPIR-V 1.4 or "
  1206. "later";
  1207. }
  1208. }
  1209. }
  1210. return SPV_SUCCESS;
  1211. }
  1212. spv_result_t ValidateCopyMemory(ValidationState_t& _, const Instruction* inst) {
  1213. const auto target_index = 0;
  1214. const auto target_id = inst->GetOperandAs<uint32_t>(target_index);
  1215. const auto target = _.FindDef(target_id);
  1216. if (!target) {
  1217. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1218. << "Target operand <id> " << _.getIdName(target_id)
  1219. << " is not defined.";
  1220. }
  1221. const auto source_index = 1;
  1222. const auto source_id = inst->GetOperandAs<uint32_t>(source_index);
  1223. const auto source = _.FindDef(source_id);
  1224. if (!source) {
  1225. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1226. << "Source operand <id> " << _.getIdName(source_id)
  1227. << " is not defined.";
  1228. }
  1229. const auto target_pointer_type = _.FindDef(target->type_id());
  1230. if (!target_pointer_type ||
  1231. (target_pointer_type->opcode() != spv::Op::OpTypePointer &&
  1232. target_pointer_type->opcode() != spv::Op::OpTypeUntypedPointerKHR)) {
  1233. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1234. << "Target operand <id> " << _.getIdName(target_id)
  1235. << " is not a pointer.";
  1236. }
  1237. const auto source_pointer_type = _.FindDef(source->type_id());
  1238. if (!source_pointer_type ||
  1239. (source_pointer_type->opcode() != spv::Op::OpTypePointer &&
  1240. source_pointer_type->opcode() != spv::Op::OpTypeUntypedPointerKHR)) {
  1241. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1242. << "Source operand <id> " << _.getIdName(source_id)
  1243. << " is not a pointer.";
  1244. }
  1245. if (inst->opcode() == spv::Op::OpCopyMemory) {
  1246. const bool target_typed =
  1247. target_pointer_type->opcode() == spv::Op::OpTypePointer;
  1248. const bool source_typed =
  1249. source_pointer_type->opcode() == spv::Op::OpTypePointer;
  1250. Instruction* target_type = nullptr;
  1251. Instruction* source_type = nullptr;
  1252. if (target_typed) {
  1253. target_type = _.FindDef(target_pointer_type->GetOperandAs<uint32_t>(2));
  1254. if (!target_type || target_type->opcode() == spv::Op::OpTypeVoid) {
  1255. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1256. << "Target operand <id> " << _.getIdName(target_id)
  1257. << " cannot be a void pointer.";
  1258. }
  1259. }
  1260. if (source_typed) {
  1261. source_type = _.FindDef(source_pointer_type->GetOperandAs<uint32_t>(2));
  1262. if (!source_type || source_type->opcode() == spv::Op::OpTypeVoid) {
  1263. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1264. << "Source operand <id> " << _.getIdName(source_id)
  1265. << " cannot be a void pointer.";
  1266. }
  1267. }
  1268. if (target_type && source_type && target_type->id() != source_type->id()) {
  1269. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1270. << "Target <id> " << _.getIdName(source_id)
  1271. << "s type does not match Source <id> "
  1272. << _.getIdName(source_type->id()) << "s type.";
  1273. }
  1274. if (!target_type && !source_type) {
  1275. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1276. << "One of Source or Target must be a typed pointer";
  1277. }
  1278. if (auto error = CheckMemoryAccess(_, inst, 2)) return error;
  1279. } else {
  1280. const auto size_id = inst->GetOperandAs<uint32_t>(2);
  1281. const auto size = _.FindDef(size_id);
  1282. if (!size) {
  1283. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1284. << "Size operand <id> " << _.getIdName(size_id)
  1285. << " is not defined.";
  1286. }
  1287. const auto size_type = _.FindDef(size->type_id());
  1288. if (!_.IsIntScalarType(size_type->id())) {
  1289. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1290. << "Size operand <id> " << _.getIdName(size_id)
  1291. << " must be a scalar integer type.";
  1292. }
  1293. bool is_zero = true;
  1294. switch (size->opcode()) {
  1295. case spv::Op::OpConstantNull:
  1296. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1297. << "Size operand <id> " << _.getIdName(size_id)
  1298. << " cannot be a constant zero.";
  1299. case spv::Op::OpConstant:
  1300. if (size_type->word(3) == 1 &&
  1301. size->word(size->words().size() - 1) & 0x80000000) {
  1302. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1303. << "Size operand <id> " << _.getIdName(size_id)
  1304. << " cannot have the sign bit set to 1.";
  1305. }
  1306. for (size_t i = 3; is_zero && i < size->words().size(); ++i) {
  1307. is_zero &= (size->word(i) == 0);
  1308. }
  1309. if (is_zero) {
  1310. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1311. << "Size operand <id> " << _.getIdName(size_id)
  1312. << " cannot be a constant zero.";
  1313. }
  1314. break;
  1315. default:
  1316. // Cannot infer any other opcodes.
  1317. break;
  1318. }
  1319. if (_.HasCapability(spv::Capability::Shader)) {
  1320. bool is_int = false;
  1321. bool is_const = false;
  1322. uint32_t value = 0;
  1323. std::tie(is_int, is_const, value) = _.EvalInt32IfConst(size_id);
  1324. if (is_const) {
  1325. if (value % 4 != 0) {
  1326. const auto source_sc =
  1327. source_pointer_type->GetOperandAs<spv::StorageClass>(1);
  1328. const auto target_sc =
  1329. target_pointer_type->GetOperandAs<spv::StorageClass>(1);
  1330. const bool int8 = _.HasCapability(spv::Capability::Int8);
  1331. const bool ubo_int8 = _.HasCapability(
  1332. spv::Capability::UniformAndStorageBuffer8BitAccess);
  1333. const bool ssbo_int8 =
  1334. _.HasCapability(spv::Capability::StorageBuffer8BitAccess) ||
  1335. ubo_int8;
  1336. const bool pc_int8 =
  1337. _.HasCapability(spv::Capability::StoragePushConstant8);
  1338. const bool wg_int8 = _.HasCapability(
  1339. spv::Capability::WorkgroupMemoryExplicitLayout8BitAccessKHR);
  1340. const bool int16 = _.HasCapability(spv::Capability::Int16) || int8;
  1341. const bool ubo_int16 =
  1342. _.HasCapability(
  1343. spv::Capability::UniformAndStorageBuffer16BitAccess) ||
  1344. ubo_int8;
  1345. const bool ssbo_int16 =
  1346. _.HasCapability(spv::Capability::StorageBuffer16BitAccess) ||
  1347. ubo_int16 || ssbo_int8;
  1348. const bool pc_int16 =
  1349. _.HasCapability(spv::Capability::StoragePushConstant16) ||
  1350. pc_int8;
  1351. const bool io_int16 =
  1352. _.HasCapability(spv::Capability::StorageInputOutput16);
  1353. const bool wg_int16 = _.HasCapability(
  1354. spv::Capability::WorkgroupMemoryExplicitLayout16BitAccessKHR);
  1355. bool source_int16_match = false;
  1356. bool target_int16_match = false;
  1357. bool source_int8_match = false;
  1358. bool target_int8_match = false;
  1359. switch (source_sc) {
  1360. case spv::StorageClass::StorageBuffer:
  1361. source_int16_match = ssbo_int16;
  1362. source_int8_match = ssbo_int8;
  1363. break;
  1364. case spv::StorageClass::Uniform:
  1365. source_int16_match = ubo_int16;
  1366. source_int8_match = ubo_int8;
  1367. break;
  1368. case spv::StorageClass::PushConstant:
  1369. source_int16_match = pc_int16;
  1370. source_int8_match = pc_int8;
  1371. break;
  1372. case spv::StorageClass::Input:
  1373. case spv::StorageClass::Output:
  1374. source_int16_match = io_int16;
  1375. break;
  1376. case spv::StorageClass::Workgroup:
  1377. source_int16_match = wg_int16;
  1378. source_int8_match = wg_int8;
  1379. break;
  1380. default:
  1381. break;
  1382. }
  1383. switch (target_sc) {
  1384. case spv::StorageClass::StorageBuffer:
  1385. target_int16_match = ssbo_int16;
  1386. target_int8_match = ssbo_int8;
  1387. break;
  1388. case spv::StorageClass::Uniform:
  1389. target_int16_match = ubo_int16;
  1390. target_int8_match = ubo_int8;
  1391. break;
  1392. case spv::StorageClass::PushConstant:
  1393. target_int16_match = pc_int16;
  1394. target_int8_match = pc_int8;
  1395. break;
  1396. // Input is read-only so it cannot be the target pointer.
  1397. case spv::StorageClass::Output:
  1398. target_int16_match = io_int16;
  1399. break;
  1400. case spv::StorageClass::Workgroup:
  1401. target_int16_match = wg_int16;
  1402. target_int8_match = wg_int8;
  1403. break;
  1404. default:
  1405. break;
  1406. }
  1407. if (!int8 && !int16 && !(source_int16_match && target_int16_match)) {
  1408. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1409. << "Size must be a multiple of 4";
  1410. }
  1411. if (value % 2 != 0) {
  1412. if (!int8 && !(source_int8_match && target_int8_match)) {
  1413. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1414. << "Size must be a multiple of 2";
  1415. }
  1416. }
  1417. }
  1418. }
  1419. }
  1420. if (auto error = CheckMemoryAccess(_, inst, 3)) return error;
  1421. }
  1422. if (auto error = ValidateCopyMemoryMemoryAccess(_, inst)) return error;
  1423. // Get past the pointers to avoid checking a pointer copy.
  1424. if (target_pointer_type->opcode() == spv::Op::OpTypePointer) {
  1425. auto sub_type = _.FindDef(target_pointer_type->GetOperandAs<uint32_t>(2));
  1426. while (sub_type->opcode() == spv::Op::OpTypePointer) {
  1427. sub_type = _.FindDef(sub_type->GetOperandAs<uint32_t>(2));
  1428. }
  1429. if (_.HasCapability(spv::Capability::Shader) &&
  1430. _.ContainsLimitedUseIntOrFloatType(sub_type->id())) {
  1431. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1432. << "Cannot copy memory of objects containing 8- or 16-bit types";
  1433. }
  1434. }
  1435. return SPV_SUCCESS;
  1436. }
  1437. spv_result_t ValidateAccessChain(ValidationState_t& _,
  1438. const Instruction* inst) {
  1439. std::string instr_name =
  1440. "Op" + std::string(spvOpcodeString(static_cast<spv::Op>(inst->opcode())));
  1441. const bool untyped_pointer = spvOpcodeGeneratesUntypedPointer(inst->opcode());
  1442. // The result type must be OpTypePointer for regular access chains and an
  1443. // OpTypeUntypedPointerKHR for untyped access chains.
  1444. auto result_type = _.FindDef(inst->type_id());
  1445. if (untyped_pointer) {
  1446. if (!result_type ||
  1447. spv::Op::OpTypeUntypedPointerKHR != result_type->opcode()) {
  1448. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1449. << "The Result Type of " << instr_name << " <id> "
  1450. << _.getIdName(inst->id())
  1451. << " must be OpTypeUntypedPointerKHR. Found Op"
  1452. << spvOpcodeString(static_cast<spv::Op>(result_type->opcode()))
  1453. << ".";
  1454. }
  1455. } else {
  1456. if (!result_type || spv::Op::OpTypePointer != result_type->opcode()) {
  1457. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1458. << "The Result Type of " << instr_name << " <id> "
  1459. << _.getIdName(inst->id()) << " must be OpTypePointer. Found Op"
  1460. << spvOpcodeString(static_cast<spv::Op>(result_type->opcode()))
  1461. << ".";
  1462. }
  1463. }
  1464. if (untyped_pointer) {
  1465. // Base type must be a non-pointer type.
  1466. const auto base_type = _.FindDef(inst->GetOperandAs<uint32_t>(2));
  1467. if (!base_type || !spvOpcodeGeneratesType(base_type->opcode()) ||
  1468. base_type->opcode() == spv::Op::OpTypePointer ||
  1469. base_type->opcode() == spv::Op::OpTypeUntypedPointerKHR) {
  1470. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1471. << "Base type must be a non-pointer type";
  1472. }
  1473. const auto ContainsBlock = [&_](const Instruction* type_inst) {
  1474. if (type_inst->opcode() == spv::Op::OpTypeStruct) {
  1475. if (_.HasDecoration(type_inst->id(), spv::Decoration::Block) ||
  1476. _.HasDecoration(type_inst->id(), spv::Decoration::BufferBlock)) {
  1477. return true;
  1478. }
  1479. }
  1480. return false;
  1481. };
  1482. // Block (and BufferBlock) arrays cannot be reinterpreted via untyped access
  1483. // chains.
  1484. const bool base_type_block_array =
  1485. base_type->opcode() == spv::Op::OpTypeArray &&
  1486. _.ContainsType(base_type->id(), ContainsBlock,
  1487. /* traverse_all_types = */ false);
  1488. const auto base_index = untyped_pointer ? 3 : 2;
  1489. const auto base_id = inst->GetOperandAs<uint32_t>(base_index);
  1490. auto base = _.FindDef(base_id);
  1491. // Strictly speaking this misses trivial access chains and function
  1492. // parameter chasing, but that would be a significant complication in the
  1493. // traversal.
  1494. while (base->opcode() == spv::Op::OpCopyObject) {
  1495. base = _.FindDef(base->GetOperandAs<uint32_t>(2));
  1496. }
  1497. const Instruction* base_data_type = nullptr;
  1498. if (base->opcode() == spv::Op::OpVariable) {
  1499. const auto ptr_type = _.FindDef(base->type_id());
  1500. base_data_type = _.FindDef(ptr_type->GetOperandAs<uint32_t>(2));
  1501. } else if (base->opcode() == spv::Op::OpUntypedVariableKHR) {
  1502. if (base->operands().size() > 3) {
  1503. base_data_type = _.FindDef(base->GetOperandAs<uint32_t>(3));
  1504. }
  1505. }
  1506. if (base_data_type) {
  1507. const bool base_block_array =
  1508. base_data_type->opcode() == spv::Op::OpTypeArray &&
  1509. _.ContainsType(base_data_type->id(), ContainsBlock,
  1510. /* traverse_all_types = */ false);
  1511. if (base_type_block_array != base_block_array) {
  1512. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1513. << "Both Base Type and Base must be Block or BufferBlock arrays "
  1514. "or neither can be";
  1515. } else if (base_type_block_array && base_block_array &&
  1516. base_type->id() != base_data_type->id()) {
  1517. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1518. << "If Base or Base Type is a Block or BufferBlock array, the "
  1519. "other must also be the same array";
  1520. }
  1521. }
  1522. }
  1523. // Base must be a pointer, pointing to the base of a composite object.
  1524. const auto base_index = untyped_pointer ? 3 : 2;
  1525. const auto base_id = inst->GetOperandAs<uint32_t>(base_index);
  1526. const auto base = _.FindDef(base_id);
  1527. const auto base_type = _.FindDef(base->type_id());
  1528. if (!base_type || !(spv::Op::OpTypePointer == base_type->opcode() ||
  1529. (untyped_pointer && spv::Op::OpTypeUntypedPointerKHR ==
  1530. base_type->opcode()))) {
  1531. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1532. << "The Base <id> " << _.getIdName(base_id) << " in " << instr_name
  1533. << " instruction must be a pointer.";
  1534. }
  1535. // The result pointer storage class and base pointer storage class must match.
  1536. // Word 2 of OpTypePointer is the Storage Class.
  1537. auto result_type_storage_class = result_type->word(2);
  1538. auto base_type_storage_class = base_type->word(2);
  1539. if (result_type_storage_class != base_type_storage_class) {
  1540. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1541. << "The result pointer storage class and base "
  1542. "pointer storage class in "
  1543. << instr_name << " do not match.";
  1544. }
  1545. // The type pointed to by OpTypePointer (word 3) must be a composite type.
  1546. auto type_pointee = untyped_pointer
  1547. ? _.FindDef(inst->GetOperandAs<uint32_t>(2))
  1548. : _.FindDef(base_type->word(3));
  1549. // Check Universal Limit (SPIR-V Spec. Section 2.17).
  1550. // The number of indexes passed to OpAccessChain may not exceed 255
  1551. // The instruction includes 4 words + N words (for N indexes)
  1552. size_t num_indexes = inst->words().size() - 4;
  1553. if (inst->opcode() == spv::Op::OpPtrAccessChain ||
  1554. inst->opcode() == spv::Op::OpInBoundsPtrAccessChain ||
  1555. inst->opcode() == spv::Op::OpUntypedPtrAccessChainKHR ||
  1556. inst->opcode() == spv::Op::OpUntypedInBoundsPtrAccessChainKHR) {
  1557. // In pointer access chains, the element operand is required, but not
  1558. // counted as an index.
  1559. --num_indexes;
  1560. }
  1561. const size_t num_indexes_limit =
  1562. _.options()->universal_limits_.max_access_chain_indexes;
  1563. if (num_indexes > num_indexes_limit) {
  1564. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1565. << "The number of indexes in " << instr_name << " may not exceed "
  1566. << num_indexes_limit << ". Found " << num_indexes << " indexes.";
  1567. }
  1568. // Indexes walk the type hierarchy to the desired depth, potentially down to
  1569. // scalar granularity. The first index in Indexes will select the top-level
  1570. // member/element/component/element of the base composite. All composite
  1571. // constituents use zero-based numbering, as described by their OpType...
  1572. // instruction. The second index will apply similarly to that result, and so
  1573. // on. Once any non-composite type is reached, there must be no remaining
  1574. // (unused) indexes.
  1575. auto starting_index = untyped_pointer ? 5 : 4;
  1576. if (inst->opcode() == spv::Op::OpPtrAccessChain ||
  1577. inst->opcode() == spv::Op::OpInBoundsPtrAccessChain ||
  1578. inst->opcode() == spv::Op::OpUntypedPtrAccessChainKHR ||
  1579. inst->opcode() == spv::Op::OpUntypedInBoundsPtrAccessChainKHR) {
  1580. ++starting_index;
  1581. }
  1582. for (size_t i = starting_index; i < inst->words().size(); ++i) {
  1583. const uint32_t cur_word = inst->words()[i];
  1584. // Earlier ID checks ensure that cur_word definition exists.
  1585. auto cur_word_instr = _.FindDef(cur_word);
  1586. // The index must be a scalar integer type (See OpAccessChain in the Spec.)
  1587. auto index_type = _.FindDef(cur_word_instr->type_id());
  1588. if (!index_type || spv::Op::OpTypeInt != index_type->opcode()) {
  1589. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1590. << "Indexes passed to " << instr_name
  1591. << " must be of type integer.";
  1592. }
  1593. switch (type_pointee->opcode()) {
  1594. case spv::Op::OpTypeMatrix:
  1595. case spv::Op::OpTypeVector:
  1596. case spv::Op::OpTypeCooperativeVectorNV:
  1597. case spv::Op::OpTypeCooperativeMatrixNV:
  1598. case spv::Op::OpTypeCooperativeMatrixKHR:
  1599. case spv::Op::OpTypeArray:
  1600. case spv::Op::OpTypeRuntimeArray:
  1601. case spv::Op::OpTypeNodePayloadArrayAMDX: {
  1602. // In OpTypeMatrix, OpTypeVector, spv::Op::OpTypeCooperativeMatrixNV,
  1603. // OpTypeCooperativeVectorNV, OpTypeArray, and OpTypeRuntimeArray, word
  1604. // 2 is the Element Type.
  1605. type_pointee = _.FindDef(type_pointee->word(2));
  1606. break;
  1607. }
  1608. case spv::Op::OpTypeStruct: {
  1609. // In case of structures, there is an additional constraint on the
  1610. // index: the index must be an OpConstant.
  1611. int64_t cur_index;
  1612. if (!_.EvalConstantValInt64(cur_word, &cur_index)) {
  1613. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1614. << "The <id> passed to " << instr_name << " to index "
  1615. << _.getIdName(cur_word)
  1616. << " into a "
  1617. "structure must be an OpConstant.";
  1618. }
  1619. // The index points to the struct member we want, therefore, the index
  1620. // should be less than the number of struct members.
  1621. const int64_t num_struct_members =
  1622. static_cast<int64_t>(type_pointee->words().size() - 2);
  1623. if (cur_index >= num_struct_members || cur_index < 0) {
  1624. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1625. << "Index " << _.getIdName(cur_word)
  1626. << " is out of bounds: " << instr_name << " cannot find index "
  1627. << cur_index << " into the structure <id> "
  1628. << _.getIdName(type_pointee->id()) << ". This structure has "
  1629. << num_struct_members << " members. Largest valid index is "
  1630. << num_struct_members - 1 << ".";
  1631. }
  1632. // Struct members IDs start at word 2 of OpTypeStruct.
  1633. const size_t word_index = static_cast<size_t>(cur_index) + 2;
  1634. auto structMemberId = type_pointee->word(word_index);
  1635. type_pointee = _.FindDef(structMemberId);
  1636. break;
  1637. }
  1638. default: {
  1639. // Give an error. reached non-composite type while indexes still remain.
  1640. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1641. << instr_name
  1642. << " reached non-composite type while indexes "
  1643. "still remain to be traversed.";
  1644. }
  1645. }
  1646. }
  1647. if (!untyped_pointer) {
  1648. // Result type is a pointer. Find out what it's pointing to.
  1649. // This will be used to make sure the indexing results in the same type.
  1650. // OpTypePointer word 3 is the type being pointed to.
  1651. const auto result_type_pointee = _.FindDef(result_type->word(3));
  1652. // At this point, we have fully walked down from the base using the indeces.
  1653. // The type being pointed to should be the same as the result type.
  1654. if (type_pointee->id() != result_type_pointee->id()) {
  1655. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1656. << instr_name << " result type (Op"
  1657. << spvOpcodeString(
  1658. static_cast<spv::Op>(result_type_pointee->opcode()))
  1659. << ") does not match the type that results from indexing into the "
  1660. "base "
  1661. "<id> (Op"
  1662. << spvOpcodeString(static_cast<spv::Op>(type_pointee->opcode()))
  1663. << ").";
  1664. }
  1665. }
  1666. return SPV_SUCCESS;
  1667. }
  1668. spv_result_t ValidateRawAccessChain(ValidationState_t& _,
  1669. const Instruction* inst) {
  1670. std::string instr_name = "Op" + std::string(spvOpcodeString(inst->opcode()));
  1671. // The result type must be OpTypePointer.
  1672. const auto result_type = _.FindDef(inst->type_id());
  1673. if (spv::Op::OpTypePointer != result_type->opcode()) {
  1674. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1675. << "The Result Type of " << instr_name << " <id> "
  1676. << _.getIdName(inst->id()) << " must be OpTypePointer. Found Op"
  1677. << spvOpcodeString(result_type->opcode()) << '.';
  1678. }
  1679. // The pointed storage class must be valid.
  1680. const auto storage_class = result_type->GetOperandAs<spv::StorageClass>(1);
  1681. if (storage_class != spv::StorageClass::StorageBuffer &&
  1682. storage_class != spv::StorageClass::PhysicalStorageBuffer &&
  1683. storage_class != spv::StorageClass::Uniform) {
  1684. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1685. << "The Result Type of " << instr_name << " <id> "
  1686. << _.getIdName(inst->id())
  1687. << " must point to a storage class of "
  1688. "StorageBuffer, PhysicalStorageBuffer, or Uniform.";
  1689. }
  1690. // The pointed type must not be one in the list below.
  1691. const auto result_type_pointee =
  1692. _.FindDef(result_type->GetOperandAs<uint32_t>(2));
  1693. if (result_type_pointee->opcode() == spv::Op::OpTypeArray ||
  1694. result_type_pointee->opcode() == spv::Op::OpTypeMatrix ||
  1695. result_type_pointee->opcode() == spv::Op::OpTypeStruct) {
  1696. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1697. << "The Result Type of " << instr_name << " <id> "
  1698. << _.getIdName(inst->id())
  1699. << " must not point to "
  1700. "OpTypeArray, OpTypeMatrix, or OpTypeStruct.";
  1701. }
  1702. // Validate Stride is a OpConstant.
  1703. const auto stride = _.FindDef(inst->GetOperandAs<uint32_t>(3));
  1704. if (stride->opcode() != spv::Op::OpConstant) {
  1705. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1706. << "The Stride of " << instr_name << " <id> "
  1707. << _.getIdName(inst->id()) << " must be OpConstant. Found Op"
  1708. << spvOpcodeString(stride->opcode()) << '.';
  1709. }
  1710. // Stride type must be OpTypeInt
  1711. const auto stride_type = _.FindDef(stride->type_id());
  1712. if (stride_type->opcode() != spv::Op::OpTypeInt) {
  1713. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1714. << "The type of Stride of " << instr_name << " <id> "
  1715. << _.getIdName(inst->id()) << " must be OpTypeInt. Found Op"
  1716. << spvOpcodeString(stride_type->opcode()) << '.';
  1717. }
  1718. // Index and Offset type must be OpTypeInt with a width of 32
  1719. const auto ValidateType = [&](const char* name,
  1720. int operandIndex) -> spv_result_t {
  1721. const auto value = _.FindDef(inst->GetOperandAs<uint32_t>(operandIndex));
  1722. const auto value_type = _.FindDef(value->type_id());
  1723. if (value_type->opcode() != spv::Op::OpTypeInt) {
  1724. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1725. << "The type of " << name << " of " << instr_name << " <id> "
  1726. << _.getIdName(inst->id()) << " must be OpTypeInt. Found Op"
  1727. << spvOpcodeString(value_type->opcode()) << '.';
  1728. }
  1729. const auto width = value_type->GetOperandAs<uint32_t>(1);
  1730. if (width != 32) {
  1731. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1732. << "The integer width of " << name << " of " << instr_name
  1733. << " <id> " << _.getIdName(inst->id()) << " must be 32. Found "
  1734. << width << '.';
  1735. }
  1736. return SPV_SUCCESS;
  1737. };
  1738. spv_result_t result;
  1739. result = ValidateType("Index", 4);
  1740. if (result != SPV_SUCCESS) {
  1741. return result;
  1742. }
  1743. result = ValidateType("Offset", 5);
  1744. if (result != SPV_SUCCESS) {
  1745. return result;
  1746. }
  1747. uint32_t access_operands = 0;
  1748. if (inst->operands().size() >= 7) {
  1749. access_operands = inst->GetOperandAs<uint32_t>(6);
  1750. }
  1751. if (access_operands &
  1752. uint32_t(spv::RawAccessChainOperandsMask::RobustnessPerElementNV)) {
  1753. uint64_t stride_value = 0;
  1754. if (_.EvalConstantValUint64(stride->id(), &stride_value) &&
  1755. stride_value == 0) {
  1756. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1757. << "Stride must not be zero when per-element robustness is used.";
  1758. }
  1759. }
  1760. if (access_operands &
  1761. uint32_t(spv::RawAccessChainOperandsMask::RobustnessPerComponentNV) ||
  1762. access_operands &
  1763. uint32_t(spv::RawAccessChainOperandsMask::RobustnessPerElementNV)) {
  1764. if (storage_class == spv::StorageClass::PhysicalStorageBuffer) {
  1765. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1766. << "Storage class cannot be PhysicalStorageBuffer when "
  1767. "raw access chain robustness is used.";
  1768. }
  1769. }
  1770. if (access_operands &
  1771. uint32_t(spv::RawAccessChainOperandsMask::RobustnessPerComponentNV) &&
  1772. access_operands &
  1773. uint32_t(spv::RawAccessChainOperandsMask::RobustnessPerElementNV)) {
  1774. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1775. << "Per-component robustness and per-element robustness are "
  1776. "mutually exclusive.";
  1777. }
  1778. return SPV_SUCCESS;
  1779. }
  1780. spv_result_t ValidatePtrAccessChain(ValidationState_t& _,
  1781. const Instruction* inst) {
  1782. if (_.addressing_model() == spv::AddressingModel::Logical &&
  1783. inst->opcode() == spv::Op::OpPtrAccessChain) {
  1784. if (!_.features().variable_pointers) {
  1785. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1786. << "Generating variable pointers requires capability "
  1787. << "VariablePointers or VariablePointersStorageBuffer";
  1788. }
  1789. }
  1790. // Need to call first, will make sure Base is a valid ID
  1791. if (auto error = ValidateAccessChain(_, inst)) return error;
  1792. const bool untyped_pointer = spvOpcodeGeneratesUntypedPointer(inst->opcode());
  1793. const auto base_idx = untyped_pointer ? 3 : 2;
  1794. const auto base = _.FindDef(inst->GetOperandAs<uint32_t>(base_idx));
  1795. const auto base_type = _.FindDef(base->type_id());
  1796. const auto base_type_storage_class =
  1797. base_type->GetOperandAs<spv::StorageClass>(1);
  1798. const auto element_idx = untyped_pointer ? 4 : 3;
  1799. const auto element = _.FindDef(inst->GetOperandAs<uint32_t>(element_idx));
  1800. const auto element_type = _.FindDef(element->type_id());
  1801. if (!element_type || element_type->opcode() != spv::Op::OpTypeInt) {
  1802. return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Element must be an integer";
  1803. }
  1804. uint64_t element_val = 0;
  1805. if (_.EvalConstantValUint64(element->id(), &element_val)) {
  1806. if (element_val != 0) {
  1807. const auto interp_type =
  1808. untyped_pointer ? _.FindDef(inst->GetOperandAs<uint32_t>(2))
  1809. : _.FindDef(base_type->GetOperandAs<uint32_t>(2));
  1810. if (interp_type->opcode() == spv::Op::OpTypeStruct &&
  1811. (_.HasDecoration(interp_type->id(), spv::Decoration::Block) ||
  1812. _.HasDecoration(interp_type->id(), spv::Decoration::BufferBlock))) {
  1813. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1814. << "Element must be 0 if the interpretation type is a Block- or "
  1815. "BufferBlock-decorated structure";
  1816. }
  1817. }
  1818. }
  1819. if (_.HasCapability(spv::Capability::Shader) &&
  1820. (base_type_storage_class == spv::StorageClass::Uniform ||
  1821. base_type_storage_class == spv::StorageClass::StorageBuffer ||
  1822. base_type_storage_class == spv::StorageClass::PhysicalStorageBuffer ||
  1823. base_type_storage_class == spv::StorageClass::PushConstant ||
  1824. (_.HasCapability(spv::Capability::WorkgroupMemoryExplicitLayoutKHR) &&
  1825. base_type_storage_class == spv::StorageClass::Workgroup)) &&
  1826. !_.HasDecoration(base_type->id(), spv::Decoration::ArrayStride)) {
  1827. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1828. << "OpPtrAccessChain must have a Base whose type is decorated "
  1829. "with ArrayStride";
  1830. }
  1831. if (spvIsVulkanEnv(_.context()->target_env)) {
  1832. const auto untyped_cap =
  1833. untyped_pointer && _.HasCapability(spv::Capability::UntypedPointersKHR);
  1834. if (base_type_storage_class == spv::StorageClass::Workgroup) {
  1835. if (!_.HasCapability(spv::Capability::VariablePointers) && !untyped_cap) {
  1836. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1837. << _.VkErrorID(7651)
  1838. << "OpPtrAccessChain Base operand pointing to Workgroup "
  1839. "storage class must use VariablePointers capability";
  1840. }
  1841. } else if (base_type_storage_class == spv::StorageClass::StorageBuffer) {
  1842. if (!_.features().variable_pointers && !untyped_cap) {
  1843. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1844. << _.VkErrorID(7652)
  1845. << "OpPtrAccessChain Base operand pointing to StorageBuffer "
  1846. "storage class must use VariablePointers or "
  1847. "VariablePointersStorageBuffer capability";
  1848. }
  1849. } else if (base_type_storage_class !=
  1850. spv::StorageClass::PhysicalStorageBuffer &&
  1851. !untyped_cap) {
  1852. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1853. << _.VkErrorID(7650)
  1854. << "OpPtrAccessChain Base operand must point to Workgroup, "
  1855. "StorageBuffer, or PhysicalStorageBuffer storage class";
  1856. }
  1857. }
  1858. return SPV_SUCCESS;
  1859. }
  1860. spv_result_t ValidateArrayLength(ValidationState_t& state,
  1861. const Instruction* inst) {
  1862. std::string instr_name =
  1863. "Op" + std::string(spvOpcodeString(static_cast<spv::Op>(inst->opcode())));
  1864. // Result type must be a 32-bit unsigned int.
  1865. auto result_type = state.FindDef(inst->type_id());
  1866. if (result_type->opcode() != spv::Op::OpTypeInt ||
  1867. result_type->GetOperandAs<uint32_t>(1) != 32 ||
  1868. result_type->GetOperandAs<uint32_t>(2) != 0) {
  1869. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1870. << "The Result Type of " << instr_name << " <id> "
  1871. << state.getIdName(inst->id())
  1872. << " must be OpTypeInt with width 32 and signedness 0.";
  1873. }
  1874. const bool untyped = inst->opcode() == spv::Op::OpUntypedArrayLengthKHR;
  1875. auto pointer_ty_id = state.GetOperandTypeId(inst, (untyped ? 3 : 2));
  1876. auto pointer_ty = state.FindDef(pointer_ty_id);
  1877. if (untyped) {
  1878. if (pointer_ty->opcode() != spv::Op::OpTypeUntypedPointerKHR) {
  1879. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1880. << "Pointer must be an untyped pointer";
  1881. }
  1882. } else if (pointer_ty->opcode() != spv::Op::OpTypePointer) {
  1883. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1884. << "The Structure's type in " << instr_name << " <id> "
  1885. << state.getIdName(inst->id())
  1886. << " must be a pointer to an OpTypeStruct.";
  1887. }
  1888. Instruction* structure_type = nullptr;
  1889. if (untyped) {
  1890. structure_type = state.FindDef(inst->GetOperandAs<uint32_t>(2));
  1891. } else {
  1892. structure_type = state.FindDef(pointer_ty->GetOperandAs<uint32_t>(2));
  1893. }
  1894. if (structure_type->opcode() != spv::Op::OpTypeStruct) {
  1895. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1896. << "The Structure's type in " << instr_name << " <id> "
  1897. << state.getIdName(inst->id())
  1898. << " must be a pointer to an OpTypeStruct.";
  1899. }
  1900. auto num_of_members = structure_type->operands().size() - 1;
  1901. auto last_member =
  1902. state.FindDef(structure_type->GetOperandAs<uint32_t>(num_of_members));
  1903. if (last_member->opcode() != spv::Op::OpTypeRuntimeArray) {
  1904. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1905. << "The Structure's last member in " << instr_name << " <id> "
  1906. << state.getIdName(inst->id()) << " must be an OpTypeRuntimeArray.";
  1907. }
  1908. // The array member must the index of the last element (the run time
  1909. // array).
  1910. const auto index = untyped ? 4 : 3;
  1911. if (inst->GetOperandAs<uint32_t>(index) != num_of_members - 1) {
  1912. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1913. << "The array member in " << instr_name << " <id> "
  1914. << state.getIdName(inst->id())
  1915. << " must be the last member of the struct.";
  1916. }
  1917. return SPV_SUCCESS;
  1918. }
  1919. spv_result_t ValidateCooperativeMatrixLengthNV(ValidationState_t& state,
  1920. const Instruction* inst) {
  1921. std::string instr_name =
  1922. "Op" + std::string(spvOpcodeString(static_cast<spv::Op>(inst->opcode())));
  1923. // Result type must be a 32-bit unsigned int.
  1924. auto result_type = state.FindDef(inst->type_id());
  1925. if (result_type->opcode() != spv::Op::OpTypeInt ||
  1926. result_type->GetOperandAs<uint32_t>(1) != 32 ||
  1927. result_type->GetOperandAs<uint32_t>(2) != 0) {
  1928. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1929. << "The Result Type of " << instr_name << " <id> "
  1930. << state.getIdName(inst->id())
  1931. << " must be OpTypeInt with width 32 and signedness 0.";
  1932. }
  1933. bool isKhr = inst->opcode() == spv::Op::OpCooperativeMatrixLengthKHR;
  1934. auto type_id = inst->GetOperandAs<uint32_t>(2);
  1935. auto type = state.FindDef(type_id);
  1936. if (isKhr && type->opcode() != spv::Op::OpTypeCooperativeMatrixKHR) {
  1937. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1938. << "The type in " << instr_name << " <id> "
  1939. << state.getIdName(type_id)
  1940. << " must be OpTypeCooperativeMatrixKHR.";
  1941. } else if (!isKhr && type->opcode() != spv::Op::OpTypeCooperativeMatrixNV) {
  1942. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1943. << "The type in " << instr_name << " <id> "
  1944. << state.getIdName(type_id) << " must be OpTypeCooperativeMatrixNV.";
  1945. }
  1946. return SPV_SUCCESS;
  1947. }
  1948. spv_result_t ValidateCooperativeMatrixLoadStoreNV(ValidationState_t& _,
  1949. const Instruction* inst) {
  1950. uint32_t type_id;
  1951. const char* opname;
  1952. if (inst->opcode() == spv::Op::OpCooperativeMatrixLoadNV) {
  1953. type_id = inst->type_id();
  1954. opname = "spv::Op::OpCooperativeMatrixLoadNV";
  1955. } else {
  1956. // get Object operand's type
  1957. type_id = _.FindDef(inst->GetOperandAs<uint32_t>(1))->type_id();
  1958. opname = "spv::Op::OpCooperativeMatrixStoreNV";
  1959. }
  1960. auto matrix_type = _.FindDef(type_id);
  1961. if (matrix_type->opcode() != spv::Op::OpTypeCooperativeMatrixNV) {
  1962. if (inst->opcode() == spv::Op::OpCooperativeMatrixLoadNV) {
  1963. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1964. << "spv::Op::OpCooperativeMatrixLoadNV Result Type <id> "
  1965. << _.getIdName(type_id) << " is not a cooperative matrix type.";
  1966. } else {
  1967. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1968. << "spv::Op::OpCooperativeMatrixStoreNV Object type <id> "
  1969. << _.getIdName(type_id) << " is not a cooperative matrix type.";
  1970. }
  1971. }
  1972. const auto pointer_index =
  1973. (inst->opcode() == spv::Op::OpCooperativeMatrixLoadNV) ? 2u : 0u;
  1974. const auto pointer_id = inst->GetOperandAs<uint32_t>(pointer_index);
  1975. const auto pointer = _.FindDef(pointer_id);
  1976. if (!pointer ||
  1977. ((_.addressing_model() == spv::AddressingModel::Logical) &&
  1978. ((!_.features().variable_pointers &&
  1979. !spvOpcodeReturnsLogicalPointer(pointer->opcode())) ||
  1980. (_.features().variable_pointers &&
  1981. !spvOpcodeReturnsLogicalVariablePointer(pointer->opcode()))))) {
  1982. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1983. << opname << " Pointer <id> " << _.getIdName(pointer_id)
  1984. << " is not a logical pointer.";
  1985. }
  1986. const auto pointer_type_id = pointer->type_id();
  1987. const auto pointer_type = _.FindDef(pointer_type_id);
  1988. if (!pointer_type || pointer_type->opcode() != spv::Op::OpTypePointer) {
  1989. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1990. << opname << " type for pointer <id> " << _.getIdName(pointer_id)
  1991. << " is not a pointer type.";
  1992. }
  1993. const auto storage_class_index = 1u;
  1994. const auto storage_class =
  1995. pointer_type->GetOperandAs<spv::StorageClass>(storage_class_index);
  1996. if (storage_class != spv::StorageClass::Workgroup &&
  1997. storage_class != spv::StorageClass::StorageBuffer &&
  1998. storage_class != spv::StorageClass::PhysicalStorageBuffer) {
  1999. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2000. << opname << " storage class for pointer type <id> "
  2001. << _.getIdName(pointer_type_id)
  2002. << " is not Workgroup or StorageBuffer.";
  2003. }
  2004. const auto pointee_id = pointer_type->GetOperandAs<uint32_t>(2);
  2005. const auto pointee_type = _.FindDef(pointee_id);
  2006. if (!pointee_type || !(_.IsIntScalarOrVectorType(pointee_id) ||
  2007. _.IsFloatScalarOrVectorType(pointee_id))) {
  2008. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2009. << opname << " Pointer <id> " << _.getIdName(pointer->id())
  2010. << "s Type must be a scalar or vector type.";
  2011. }
  2012. const auto stride_index =
  2013. (inst->opcode() == spv::Op::OpCooperativeMatrixLoadNV) ? 3u : 2u;
  2014. const auto stride_id = inst->GetOperandAs<uint32_t>(stride_index);
  2015. const auto stride = _.FindDef(stride_id);
  2016. if (!stride || !_.IsIntScalarType(stride->type_id())) {
  2017. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2018. << "Stride operand <id> " << _.getIdName(stride_id)
  2019. << " must be a scalar integer type.";
  2020. }
  2021. const auto colmajor_index =
  2022. (inst->opcode() == spv::Op::OpCooperativeMatrixLoadNV) ? 4u : 3u;
  2023. const auto colmajor_id = inst->GetOperandAs<uint32_t>(colmajor_index);
  2024. const auto colmajor = _.FindDef(colmajor_id);
  2025. if (!colmajor || !_.IsBoolScalarType(colmajor->type_id()) ||
  2026. !(spvOpcodeIsConstant(colmajor->opcode()) ||
  2027. spvOpcodeIsSpecConstant(colmajor->opcode()))) {
  2028. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2029. << "Column Major operand <id> " << _.getIdName(colmajor_id)
  2030. << " must be a boolean constant instruction.";
  2031. }
  2032. const auto memory_access_index =
  2033. (inst->opcode() == spv::Op::OpCooperativeMatrixLoadNV) ? 5u : 4u;
  2034. if (inst->operands().size() > memory_access_index) {
  2035. if (auto error = CheckMemoryAccess(_, inst, memory_access_index))
  2036. return error;
  2037. }
  2038. return SPV_SUCCESS;
  2039. }
  2040. spv_result_t ValidateCooperativeMatrixLoadStoreKHR(ValidationState_t& _,
  2041. const Instruction* inst) {
  2042. uint32_t type_id;
  2043. const char* opname;
  2044. if (inst->opcode() == spv::Op::OpCooperativeMatrixLoadKHR) {
  2045. type_id = inst->type_id();
  2046. opname = "spv::Op::OpCooperativeMatrixLoadKHR";
  2047. } else {
  2048. // get Object operand's type
  2049. type_id = _.FindDef(inst->GetOperandAs<uint32_t>(1))->type_id();
  2050. opname = "spv::Op::OpCooperativeMatrixStoreKHR";
  2051. }
  2052. auto matrix_type = _.FindDef(type_id);
  2053. if (matrix_type->opcode() != spv::Op::OpTypeCooperativeMatrixKHR) {
  2054. if (inst->opcode() == spv::Op::OpCooperativeMatrixLoadKHR) {
  2055. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2056. << "spv::Op::OpCooperativeMatrixLoadKHR Result Type <id> "
  2057. << _.getIdName(type_id) << " is not a cooperative matrix type.";
  2058. } else {
  2059. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2060. << "spv::Op::OpCooperativeMatrixStoreKHR Object type <id> "
  2061. << _.getIdName(type_id) << " is not a cooperative matrix type.";
  2062. }
  2063. }
  2064. const auto pointer_index =
  2065. (inst->opcode() == spv::Op::OpCooperativeMatrixLoadKHR) ? 2u : 0u;
  2066. const auto pointer_id = inst->GetOperandAs<uint32_t>(pointer_index);
  2067. const auto pointer = _.FindDef(pointer_id);
  2068. if (!pointer ||
  2069. ((_.addressing_model() == spv::AddressingModel::Logical) &&
  2070. ((!_.features().variable_pointers &&
  2071. !spvOpcodeReturnsLogicalPointer(pointer->opcode())) ||
  2072. (_.features().variable_pointers &&
  2073. !spvOpcodeReturnsLogicalVariablePointer(pointer->opcode()))))) {
  2074. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2075. << opname << " Pointer <id> " << _.getIdName(pointer_id)
  2076. << " is not a logical pointer.";
  2077. }
  2078. const auto pointer_type_id = pointer->type_id();
  2079. const auto pointer_type = _.FindDef(pointer_type_id);
  2080. if (!pointer_type ||
  2081. !(pointer_type->opcode() == spv::Op::OpTypePointer ||
  2082. pointer_type->opcode() == spv::Op::OpTypeUntypedPointerKHR)) {
  2083. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2084. << opname << " type for pointer <id> " << _.getIdName(pointer_id)
  2085. << " is not a pointer type.";
  2086. }
  2087. const bool untyped =
  2088. pointer_type->opcode() == spv::Op::OpTypeUntypedPointerKHR;
  2089. const auto storage_class_index = 1u;
  2090. const auto storage_class =
  2091. pointer_type->GetOperandAs<spv::StorageClass>(storage_class_index);
  2092. if (spvIsVulkanEnv(_.context()->target_env)) {
  2093. if (storage_class != spv::StorageClass::Workgroup &&
  2094. storage_class != spv::StorageClass::StorageBuffer &&
  2095. storage_class != spv::StorageClass::PhysicalStorageBuffer) {
  2096. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2097. << _.VkErrorID(8973) << opname
  2098. << " storage class for pointer type <id> "
  2099. << _.getIdName(pointer_type_id)
  2100. << " is not Workgroup, StorageBuffer, or PhysicalStorageBuffer.";
  2101. }
  2102. }
  2103. if (!untyped) {
  2104. const auto pointee_id = pointer_type->GetOperandAs<uint32_t>(2);
  2105. const auto pointee_type = _.FindDef(pointee_id);
  2106. if (!pointee_type || !(_.IsIntScalarOrVectorType(pointee_id) ||
  2107. _.IsFloatScalarOrVectorType(pointee_id))) {
  2108. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2109. << opname << " Pointer <id> " << _.getIdName(pointer->id())
  2110. << "s Type must be a scalar or vector type.";
  2111. }
  2112. }
  2113. const auto layout_index =
  2114. (inst->opcode() == spv::Op::OpCooperativeMatrixLoadKHR) ? 3u : 2u;
  2115. const auto layout_id = inst->GetOperandAs<uint32_t>(layout_index);
  2116. const auto layout_inst = _.FindDef(layout_id);
  2117. if (!layout_inst || !_.IsIntScalarType(layout_inst->type_id()) ||
  2118. !spvOpcodeIsConstant(layout_inst->opcode())) {
  2119. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2120. << "MemoryLayout operand <id> " << _.getIdName(layout_id)
  2121. << " must be a 32-bit integer constant instruction.";
  2122. }
  2123. bool stride_required = false;
  2124. uint64_t layout;
  2125. if (_.EvalConstantValUint64(layout_id, &layout)) {
  2126. stride_required =
  2127. (layout == (uint64_t)spv::CooperativeMatrixLayout::RowMajorKHR) ||
  2128. (layout == (uint64_t)spv::CooperativeMatrixLayout::ColumnMajorKHR);
  2129. }
  2130. const auto stride_index =
  2131. (inst->opcode() == spv::Op::OpCooperativeMatrixLoadKHR) ? 4u : 3u;
  2132. if (inst->operands().size() > stride_index) {
  2133. const auto stride_id = inst->GetOperandAs<uint32_t>(stride_index);
  2134. const auto stride = _.FindDef(stride_id);
  2135. if (!stride || !_.IsIntScalarType(stride->type_id())) {
  2136. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2137. << "Stride operand <id> " << _.getIdName(stride_id)
  2138. << " must be a scalar integer type.";
  2139. }
  2140. } else if (stride_required) {
  2141. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2142. << "MemoryLayout " << layout << " requires a Stride.";
  2143. }
  2144. const auto memory_access_index =
  2145. (inst->opcode() == spv::Op::OpCooperativeMatrixLoadKHR) ? 5u : 4u;
  2146. if (inst->operands().size() > memory_access_index) {
  2147. if (auto error = CheckMemoryAccess(_, inst, memory_access_index))
  2148. return error;
  2149. }
  2150. return SPV_SUCCESS;
  2151. }
  2152. // Returns the number of instruction words taken up by a tensor addressing
  2153. // operands argument and its implied operands.
  2154. int TensorAddressingOperandsNumWords(spv::TensorAddressingOperandsMask mask) {
  2155. int result = 1; // Count the mask
  2156. if ((mask & spv::TensorAddressingOperandsMask::TensorView) !=
  2157. spv::TensorAddressingOperandsMask::MaskNone)
  2158. ++result;
  2159. if ((mask & spv::TensorAddressingOperandsMask::DecodeFunc) !=
  2160. spv::TensorAddressingOperandsMask::MaskNone)
  2161. ++result;
  2162. return result;
  2163. }
  2164. spv_result_t ValidateCooperativeMatrixLoadStoreTensorNV(
  2165. ValidationState_t& _, const Instruction* inst) {
  2166. uint32_t type_id;
  2167. const char* opname;
  2168. if (inst->opcode() == spv::Op::OpCooperativeMatrixLoadTensorNV) {
  2169. type_id = inst->type_id();
  2170. opname = "spv::Op::OpCooperativeMatrixLoadTensorNV";
  2171. } else {
  2172. // get Object operand's type
  2173. type_id = _.FindDef(inst->GetOperandAs<uint32_t>(1))->type_id();
  2174. opname = "spv::Op::OpCooperativeMatrixStoreTensorNV";
  2175. }
  2176. auto matrix_type = _.FindDef(type_id);
  2177. if (matrix_type->opcode() != spv::Op::OpTypeCooperativeMatrixKHR) {
  2178. if (inst->opcode() == spv::Op::OpCooperativeMatrixLoadTensorNV) {
  2179. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2180. << "spv::Op::OpCooperativeMatrixLoadTensorNV Result Type <id> "
  2181. << _.getIdName(type_id) << " is not a cooperative matrix type.";
  2182. } else {
  2183. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2184. << "spv::Op::OpCooperativeMatrixStoreTensorNV Object type <id> "
  2185. << _.getIdName(type_id) << " is not a cooperative matrix type.";
  2186. }
  2187. }
  2188. const auto pointer_index =
  2189. (inst->opcode() == spv::Op::OpCooperativeMatrixLoadTensorNV) ? 2u : 0u;
  2190. const auto pointer_id = inst->GetOperandAs<uint32_t>(pointer_index);
  2191. const auto pointer = _.FindDef(pointer_id);
  2192. if (!pointer ||
  2193. ((_.addressing_model() == spv::AddressingModel::Logical) &&
  2194. ((!_.features().variable_pointers &&
  2195. !spvOpcodeReturnsLogicalPointer(pointer->opcode())) ||
  2196. (_.features().variable_pointers &&
  2197. !spvOpcodeReturnsLogicalVariablePointer(pointer->opcode()))))) {
  2198. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2199. << opname << " Pointer <id> " << _.getIdName(pointer_id)
  2200. << " is not a logical pointer.";
  2201. }
  2202. const auto pointer_type_id = pointer->type_id();
  2203. const auto pointer_type = _.FindDef(pointer_type_id);
  2204. if (!pointer_type || pointer_type->opcode() != spv::Op::OpTypePointer) {
  2205. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2206. << opname << " type for pointer <id> " << _.getIdName(pointer_id)
  2207. << " is not a pointer type.";
  2208. }
  2209. const auto storage_class_index = 1u;
  2210. const auto storage_class =
  2211. pointer_type->GetOperandAs<spv::StorageClass>(storage_class_index);
  2212. if (storage_class != spv::StorageClass::Workgroup &&
  2213. storage_class != spv::StorageClass::StorageBuffer &&
  2214. storage_class != spv::StorageClass::PhysicalStorageBuffer) {
  2215. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2216. << _.VkErrorID(8973) << opname
  2217. << " storage class for pointer type <id> "
  2218. << _.getIdName(pointer_type_id)
  2219. << " is not Workgroup, StorageBuffer, or PhysicalStorageBuffer.";
  2220. }
  2221. if (inst->opcode() == spv::Op::OpCooperativeMatrixLoadTensorNV) {
  2222. const auto object_index = 3;
  2223. const auto object_id = inst->GetOperandAs<uint32_t>(object_index);
  2224. const auto object = _.FindDef(object_id);
  2225. if (!object || object->type_id() != type_id) {
  2226. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2227. << opname << " Object <id> " << _.getIdName(object_id)
  2228. << " type does not match Result Type.";
  2229. }
  2230. }
  2231. const auto tensor_layout_index =
  2232. (inst->opcode() == spv::Op::OpCooperativeMatrixLoadTensorNV) ? 4u : 2u;
  2233. const auto tensor_layout_id =
  2234. inst->GetOperandAs<uint32_t>(tensor_layout_index);
  2235. const auto tensor_layout = _.FindDef(tensor_layout_id);
  2236. if (!tensor_layout || _.FindDef(tensor_layout->type_id())->opcode() !=
  2237. spv::Op::OpTypeTensorLayoutNV) {
  2238. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2239. << opname << " TensorLayout <id> " << _.getIdName(tensor_layout_id)
  2240. << " does not have a tensor layout type.";
  2241. }
  2242. const auto memory_access_index =
  2243. (inst->opcode() == spv::Op::OpCooperativeMatrixLoadTensorNV) ? 5u : 3u;
  2244. if (inst->operands().size() > memory_access_index) {
  2245. if (auto error = CheckMemoryAccess(_, inst, memory_access_index))
  2246. return error;
  2247. }
  2248. const auto memory_access_mask =
  2249. inst->GetOperandAs<uint32_t>(memory_access_index);
  2250. const auto tensor_operands_index =
  2251. memory_access_index + MemoryAccessNumWords(memory_access_mask);
  2252. const auto tensor_operands =
  2253. inst->GetOperandAs<spv::TensorAddressingOperandsMask>(
  2254. tensor_operands_index);
  2255. if (inst->operands().size() <
  2256. tensor_operands_index +
  2257. TensorAddressingOperandsNumWords(tensor_operands)) {
  2258. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2259. << opname << " not enough tensor addressing operands.";
  2260. }
  2261. uint32_t tensor_operand_index = tensor_operands_index + 1;
  2262. if ((tensor_operands & spv::TensorAddressingOperandsMask::TensorView) !=
  2263. spv::TensorAddressingOperandsMask::MaskNone) {
  2264. const auto tensor_view_id =
  2265. inst->GetOperandAs<uint32_t>(tensor_operand_index);
  2266. const auto tensor_view = _.FindDef(tensor_view_id);
  2267. if (!tensor_view || _.FindDef(tensor_view->type_id())->opcode() !=
  2268. spv::Op::OpTypeTensorViewNV) {
  2269. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2270. << opname << " TensorView <id> " << _.getIdName(tensor_view_id)
  2271. << " does not have a tensor view type.";
  2272. }
  2273. tensor_operand_index++;
  2274. }
  2275. if ((tensor_operands & spv::TensorAddressingOperandsMask::DecodeFunc) !=
  2276. spv::TensorAddressingOperandsMask::MaskNone) {
  2277. if (inst->opcode() == spv::Op::OpCooperativeMatrixStoreTensorNV) {
  2278. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2279. << "OpCooperativeMatrixStoreTensorNV does not support DecodeFunc.";
  2280. }
  2281. const auto decode_func_id =
  2282. inst->GetOperandAs<uint32_t>(tensor_operand_index);
  2283. const auto decode_func = _.FindDef(decode_func_id);
  2284. if (!decode_func || decode_func->opcode() != spv::Op::OpFunction) {
  2285. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2286. << opname << " DecodeFunc <id> " << _.getIdName(decode_func_id)
  2287. << " is not a function.";
  2288. }
  2289. const auto component_type_index = 1;
  2290. const auto component_type_id =
  2291. matrix_type->GetOperandAs<uint32_t>(component_type_index);
  2292. const auto function_type =
  2293. _.FindDef(decode_func->GetOperandAs<uint32_t>(3));
  2294. if (function_type->GetOperandAs<uint32_t>(1) != component_type_id) {
  2295. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2296. << opname << " DecodeFunc <id> " << _.getIdName(decode_func_id)
  2297. << " return type must match matrix component type.";
  2298. }
  2299. const auto decode_ptr_type_id = function_type->GetOperandAs<uint32_t>(2);
  2300. const auto decode_ptr_type = _.FindDef(decode_ptr_type_id);
  2301. auto decode_storage_class =
  2302. decode_ptr_type->GetOperandAs<spv::StorageClass>(storage_class_index);
  2303. if (decode_storage_class != spv::StorageClass::PhysicalStorageBuffer) {
  2304. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2305. << opname << " DecodeFunc <id> " << _.getIdName(decode_func_id)
  2306. << " first parameter must be pointer to PhysicalStorageBuffer.";
  2307. }
  2308. const auto tensor_layout_type = _.FindDef(tensor_layout->type_id());
  2309. for (uint32_t param = 3; param < 5; ++param) {
  2310. const auto param_type_id = function_type->GetOperandAs<uint32_t>(param);
  2311. const auto param_type = _.FindDef(param_type_id);
  2312. if (param_type->opcode() != spv::Op::OpTypeArray) {
  2313. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2314. << opname << " DecodeFunc <id> " << _.getIdName(decode_func_id)
  2315. << " second/third parameter must be array of 32-bit integer "
  2316. "with "
  2317. << " dimension equal to the tensor dimension.";
  2318. }
  2319. const auto length_index = 2u;
  2320. uint64_t array_length;
  2321. if (_.EvalConstantValUint64(
  2322. param_type->GetOperandAs<uint32_t>(length_index),
  2323. &array_length)) {
  2324. const auto tensor_layout_dim_id =
  2325. tensor_layout_type->GetOperandAs<uint32_t>(1);
  2326. uint64_t dim_value;
  2327. if (_.EvalConstantValUint64(tensor_layout_dim_id, &dim_value)) {
  2328. if (array_length != dim_value) {
  2329. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2330. << opname << " DecodeFunc <id> "
  2331. << _.getIdName(decode_func_id)
  2332. << " second/third parameter must be array of 32-bit integer "
  2333. "with "
  2334. << " dimension equal to the tensor dimension.";
  2335. }
  2336. }
  2337. }
  2338. }
  2339. tensor_operand_index++;
  2340. }
  2341. return SPV_SUCCESS;
  2342. }
  2343. spv_result_t ValidateInt32Operand(ValidationState_t& _, const Instruction* inst,
  2344. uint32_t operand_index,
  2345. const char* opcode_name,
  2346. const char* operand_name) {
  2347. const auto type_id =
  2348. _.FindDef(inst->GetOperandAs<uint32_t>(operand_index))->type_id();
  2349. if (!_.IsIntScalarType(type_id) || _.GetBitWidth(type_id) != 32) {
  2350. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2351. << opcode_name << " " << operand_name << " type <id> "
  2352. << _.getIdName(type_id) << " is not a 32 bit integer.";
  2353. }
  2354. return SPV_SUCCESS;
  2355. }
  2356. spv_result_t ValidateCooperativeVectorPointer(ValidationState_t& _,
  2357. const Instruction* inst,
  2358. const char* opname,
  2359. uint32_t pointer_index) {
  2360. const auto pointer_id = inst->GetOperandAs<uint32_t>(pointer_index);
  2361. const auto pointer = _.FindDef(pointer_id);
  2362. if (!pointer ||
  2363. ((_.addressing_model() == spv::AddressingModel::Logical) &&
  2364. ((!_.features().variable_pointers &&
  2365. !spvOpcodeReturnsLogicalPointer(pointer->opcode())) ||
  2366. (_.features().variable_pointers &&
  2367. !spvOpcodeReturnsLogicalVariablePointer(pointer->opcode()))))) {
  2368. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2369. << opname << " Pointer <id> " << _.getIdName(pointer_id)
  2370. << " is not a logical pointer.";
  2371. }
  2372. const auto pointer_type_id = pointer->type_id();
  2373. const auto pointer_type = _.FindDef(pointer_type_id);
  2374. if (!pointer_type || pointer_type->opcode() != spv::Op::OpTypePointer) {
  2375. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2376. << opname << " type for pointer <id> " << _.getIdName(pointer_id)
  2377. << " is not a pointer type.";
  2378. }
  2379. const auto storage_class_index = 1u;
  2380. const auto storage_class =
  2381. pointer_type->GetOperandAs<spv::StorageClass>(storage_class_index);
  2382. if (storage_class != spv::StorageClass::Workgroup &&
  2383. storage_class != spv::StorageClass::StorageBuffer &&
  2384. storage_class != spv::StorageClass::PhysicalStorageBuffer) {
  2385. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2386. << opname << " storage class for pointer type <id> "
  2387. << _.getIdName(pointer_type_id)
  2388. << " is not Workgroup or StorageBuffer.";
  2389. }
  2390. const auto pointee_id = pointer_type->GetOperandAs<uint32_t>(2);
  2391. const auto pointee_type = _.FindDef(pointee_id);
  2392. if (!pointee_type ||
  2393. (pointee_type->opcode() != spv::Op::OpTypeArray &&
  2394. pointee_type->opcode() != spv::Op::OpTypeRuntimeArray)) {
  2395. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2396. << opname << " Pointer <id> " << _.getIdName(pointer->id())
  2397. << "s Type must be an array type.";
  2398. }
  2399. const auto array_elem_type_id = pointee_type->GetOperandAs<uint32_t>(1);
  2400. auto array_elem_type = _.FindDef(array_elem_type_id);
  2401. if (!array_elem_type || !(_.IsIntScalarOrVectorType(array_elem_type_id) ||
  2402. _.IsFloatScalarOrVectorType(array_elem_type_id))) {
  2403. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2404. << opname << " Pointer <id> " << _.getIdName(pointer->id())
  2405. << "s Type must be an array of scalar or vector type.";
  2406. }
  2407. return SPV_SUCCESS;
  2408. }
  2409. spv_result_t ValidateCooperativeVectorLoadStoreNV(ValidationState_t& _,
  2410. const Instruction* inst) {
  2411. uint32_t type_id;
  2412. const char* opname;
  2413. if (inst->opcode() == spv::Op::OpCooperativeVectorLoadNV) {
  2414. type_id = inst->type_id();
  2415. opname = "spv::Op::OpCooperativeVectorLoadNV";
  2416. } else {
  2417. // get Object operand's type
  2418. type_id = _.FindDef(inst->GetOperandAs<uint32_t>(2))->type_id();
  2419. opname = "spv::Op::OpCooperativeVectorStoreNV";
  2420. }
  2421. auto vector_type = _.FindDef(type_id);
  2422. if (vector_type->opcode() != spv::Op::OpTypeCooperativeVectorNV) {
  2423. if (inst->opcode() == spv::Op::OpCooperativeVectorLoadNV) {
  2424. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2425. << "spv::Op::OpCooperativeVectorLoadNV Result Type <id> "
  2426. << _.getIdName(type_id) << " is not a cooperative vector type.";
  2427. } else {
  2428. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2429. << "spv::Op::OpCooperativeVectorStoreNV Object type <id> "
  2430. << _.getIdName(type_id) << " is not a cooperative vector type.";
  2431. }
  2432. }
  2433. const auto pointer_index =
  2434. (inst->opcode() == spv::Op::OpCooperativeVectorLoadNV) ? 2u : 0u;
  2435. if (auto error =
  2436. ValidateCooperativeVectorPointer(_, inst, opname, pointer_index)) {
  2437. return error;
  2438. }
  2439. const auto memory_access_index =
  2440. (inst->opcode() == spv::Op::OpCooperativeVectorLoadNV) ? 4u : 3u;
  2441. if (inst->operands().size() > memory_access_index) {
  2442. if (auto error = CheckMemoryAccess(_, inst, memory_access_index))
  2443. return error;
  2444. }
  2445. return SPV_SUCCESS;
  2446. }
  2447. spv_result_t ValidateCooperativeVectorOuterProductNV(ValidationState_t& _,
  2448. const Instruction* inst) {
  2449. const auto pointer_index = 0u;
  2450. const auto opcode_name =
  2451. "spv::Op::OpCooperativeVectorOuterProductAccumulateNV";
  2452. if (auto error = ValidateCooperativeVectorPointer(_, inst, opcode_name,
  2453. pointer_index)) {
  2454. return error;
  2455. }
  2456. auto type_id = _.FindDef(inst->GetOperandAs<uint32_t>(2))->type_id();
  2457. auto a_type = _.FindDef(type_id);
  2458. if (a_type->opcode() != spv::Op::OpTypeCooperativeVectorNV) {
  2459. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2460. << opcode_name << " A type <id> " << _.getIdName(type_id)
  2461. << " is not a cooperative vector type.";
  2462. }
  2463. type_id = _.FindDef(inst->GetOperandAs<uint32_t>(3))->type_id();
  2464. auto b_type = _.FindDef(type_id);
  2465. if (b_type->opcode() != spv::Op::OpTypeCooperativeVectorNV) {
  2466. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2467. << opcode_name << " B type <id> " << _.getIdName(type_id)
  2468. << " is not a cooperative vector type.";
  2469. }
  2470. const auto a_component_type_id = a_type->GetOperandAs<uint32_t>(1);
  2471. const auto b_component_type_id = b_type->GetOperandAs<uint32_t>(1);
  2472. if (a_component_type_id != b_component_type_id) {
  2473. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2474. << opcode_name << " A and B component types "
  2475. << _.getIdName(a_component_type_id) << " and "
  2476. << _.getIdName(b_component_type_id) << " do not match.";
  2477. }
  2478. if (auto error = ValidateInt32Operand(_, inst, 1, opcode_name, "Offset")) {
  2479. return error;
  2480. }
  2481. if (auto error =
  2482. ValidateInt32Operand(_, inst, 4, opcode_name, "MemoryLayout")) {
  2483. return error;
  2484. }
  2485. if (auto error = ValidateInt32Operand(_, inst, 5, opcode_name,
  2486. "MatrixInterpretation")) {
  2487. return error;
  2488. }
  2489. if (inst->operands().size() > 6) {
  2490. if (auto error =
  2491. ValidateInt32Operand(_, inst, 6, opcode_name, "MatrixStride")) {
  2492. return error;
  2493. }
  2494. }
  2495. return SPV_SUCCESS;
  2496. }
  2497. spv_result_t ValidateCooperativeVectorReduceSumNV(ValidationState_t& _,
  2498. const Instruction* inst) {
  2499. const auto opcode_name = "spv::Op::OpCooperativeVectorReduceSumAccumulateNV";
  2500. const auto pointer_index = 0u;
  2501. if (auto error = ValidateCooperativeVectorPointer(_, inst, opcode_name,
  2502. pointer_index)) {
  2503. return error;
  2504. }
  2505. auto type_id = _.FindDef(inst->GetOperandAs<uint32_t>(2))->type_id();
  2506. auto v_type = _.FindDef(type_id);
  2507. if (v_type->opcode() != spv::Op::OpTypeCooperativeVectorNV) {
  2508. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2509. << opcode_name << " V type <id> " << _.getIdName(type_id)
  2510. << " is not a cooperative vector type.";
  2511. }
  2512. if (auto error = ValidateInt32Operand(_, inst, 1, opcode_name, "Offset")) {
  2513. return error;
  2514. }
  2515. return SPV_SUCCESS;
  2516. }
  2517. bool InterpretationIsPacked(spv::ComponentType interp) {
  2518. switch (interp) {
  2519. case spv::ComponentType::SignedInt8PackedNV:
  2520. case spv::ComponentType::UnsignedInt8PackedNV:
  2521. return true;
  2522. default:
  2523. return false;
  2524. }
  2525. }
  2526. using std::get;
  2527. spv_result_t ValidateCooperativeVectorMatrixMulNV(ValidationState_t& _,
  2528. const Instruction* inst) {
  2529. const bool has_bias =
  2530. inst->opcode() == spv::Op::OpCooperativeVectorMatrixMulAddNV;
  2531. const auto opcode_name = has_bias
  2532. ? "spv::Op::OpCooperativeVectorMatrixMulAddNV"
  2533. : "spv::Op::OpCooperativeVectorMatrixMulNV";
  2534. const auto bias_offset = has_bias ? 3 : 0;
  2535. const auto result_type_index = 0u;
  2536. const auto input_index = 2u;
  2537. const auto input_interpretation_index = 3u;
  2538. const auto matrix_index = 4u;
  2539. const auto matrix_interpretation_index = 6u;
  2540. const auto bias_index = 7u;
  2541. const auto bias_interpretation_index = 9u;
  2542. const auto m_index = 7u + bias_offset;
  2543. const auto k_index = 8u + bias_offset;
  2544. const auto memory_layout_index = 9u + bias_offset;
  2545. const auto transpose_index = 10u + bias_offset;
  2546. const auto result_type_id = inst->GetOperandAs<uint32_t>(result_type_index);
  2547. const auto input_id = inst->GetOperandAs<uint32_t>(input_index);
  2548. const auto input_interpretation_id =
  2549. inst->GetOperandAs<uint32_t>(input_interpretation_index);
  2550. const auto matrix_interpretation_id =
  2551. inst->GetOperandAs<uint32_t>(matrix_interpretation_index);
  2552. const auto bias_interpretation_id =
  2553. inst->GetOperandAs<uint32_t>(bias_interpretation_index);
  2554. const auto m_id = inst->GetOperandAs<uint32_t>(m_index);
  2555. const auto k_id = inst->GetOperandAs<uint32_t>(k_index);
  2556. const auto memory_layout_id =
  2557. inst->GetOperandAs<uint32_t>(memory_layout_index);
  2558. const auto transpose_id = inst->GetOperandAs<uint32_t>(transpose_index);
  2559. if (auto error = ValidateCooperativeVectorPointer(_, inst, opcode_name,
  2560. matrix_index)) {
  2561. return error;
  2562. }
  2563. if (inst->opcode() == spv::Op::OpCooperativeVectorMatrixMulAddNV) {
  2564. if (auto error = ValidateCooperativeVectorPointer(_, inst, opcode_name,
  2565. bias_index)) {
  2566. return error;
  2567. }
  2568. }
  2569. const auto result_type = _.FindDef(result_type_id);
  2570. if (result_type->opcode() != spv::Op::OpTypeCooperativeVectorNV) {
  2571. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2572. << opcode_name << " result type <id> " << _.getIdName(result_type_id)
  2573. << " is not a cooperative vector type.";
  2574. }
  2575. const auto result_component_type_id = result_type->GetOperandAs<uint32_t>(1u);
  2576. if (!(_.IsIntScalarType(result_component_type_id) &&
  2577. _.GetBitWidth(result_component_type_id) == 32) &&
  2578. !(_.IsFloatScalarType(result_component_type_id) &&
  2579. (_.GetBitWidth(result_component_type_id) == 32 ||
  2580. _.GetBitWidth(result_component_type_id) == 16))) {
  2581. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2582. << opcode_name << " result component type <id> "
  2583. << _.getIdName(result_component_type_id)
  2584. << " is not a 32 bit int or 16/32 bit float.";
  2585. }
  2586. const auto m_eval = _.EvalInt32IfConst(m_id);
  2587. const auto rc_eval =
  2588. _.EvalInt32IfConst(result_type->GetOperandAs<uint32_t>(2u));
  2589. if (get<1>(m_eval) && get<1>(rc_eval) && get<2>(m_eval) != get<2>(rc_eval)) {
  2590. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2591. << opcode_name << " result type number of components "
  2592. << get<2>(rc_eval) << " does not match M " << get<2>(m_eval);
  2593. }
  2594. const auto k_eval = _.EvalInt32IfConst(k_id);
  2595. const auto input = _.FindDef(input_id);
  2596. const auto input_type = _.FindDef(input->type_id());
  2597. const auto input_num_components_id = input_type->GetOperandAs<uint32_t>(2u);
  2598. auto input_interp_eval = _.EvalInt32IfConst(input_interpretation_id);
  2599. if (get<1>(input_interp_eval) &&
  2600. !InterpretationIsPacked(spv::ComponentType{get<2>(input_interp_eval)})) {
  2601. const auto inc_eval = _.EvalInt32IfConst(input_num_components_id);
  2602. if (get<1>(inc_eval) && get<1>(k_eval) &&
  2603. get<2>(inc_eval) != get<2>(k_eval)) {
  2604. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2605. << opcode_name << " input number of components "
  2606. << get<2>(inc_eval) << " does not match K " << get<2>(k_eval);
  2607. }
  2608. }
  2609. if (!_.IsBoolScalarType(_.FindDef(transpose_id)->type_id())) {
  2610. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2611. << opcode_name << " Transpose <id> " << _.getIdName(transpose_id)
  2612. << " is not a scalar boolean.";
  2613. }
  2614. const auto check_constant = [&](uint32_t id,
  2615. const char* operand_name) -> spv_result_t {
  2616. if (!spvOpcodeIsConstant(_.GetIdOpcode(id))) {
  2617. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2618. << opcode_name << " " << operand_name << " <id> "
  2619. << _.getIdName(id) << " is not a constant instruction.";
  2620. }
  2621. return SPV_SUCCESS;
  2622. };
  2623. if (auto error =
  2624. check_constant(input_interpretation_id, "InputInterpretation")) {
  2625. return error;
  2626. }
  2627. if (auto error =
  2628. check_constant(matrix_interpretation_id, "MatrixInterpretation")) {
  2629. return error;
  2630. }
  2631. if (has_bias) {
  2632. if (auto error =
  2633. check_constant(bias_interpretation_id, "BiasInterpretation")) {
  2634. return error;
  2635. }
  2636. }
  2637. if (auto error = check_constant(m_id, "M")) {
  2638. return error;
  2639. }
  2640. if (auto error = check_constant(k_id, "K")) {
  2641. return error;
  2642. }
  2643. if (auto error = check_constant(memory_layout_id, "MemoryLayout")) {
  2644. return error;
  2645. }
  2646. if (auto error = check_constant(transpose_id, "Transpose")) {
  2647. return error;
  2648. }
  2649. if (auto error = ValidateInt32Operand(_, inst, input_interpretation_index,
  2650. opcode_name, "InputInterpretation")) {
  2651. return error;
  2652. }
  2653. if (auto error = ValidateInt32Operand(_, inst, matrix_interpretation_index,
  2654. opcode_name, "MatrixInterpretation")) {
  2655. return error;
  2656. }
  2657. if (has_bias) {
  2658. if (auto error = ValidateInt32Operand(_, inst, bias_interpretation_index,
  2659. opcode_name, "BiasInterpretation")) {
  2660. return error;
  2661. }
  2662. }
  2663. if (auto error = ValidateInt32Operand(_, inst, m_index, opcode_name, "M")) {
  2664. return error;
  2665. }
  2666. if (auto error = ValidateInt32Operand(_, inst, k_index, opcode_name, "K")) {
  2667. return error;
  2668. }
  2669. if (auto error = ValidateInt32Operand(_, inst, memory_layout_index,
  2670. opcode_name, "MemoryLayout")) {
  2671. return error;
  2672. }
  2673. return SPV_SUCCESS;
  2674. }
  2675. spv_result_t ValidatePtrComparison(ValidationState_t& _,
  2676. const Instruction* inst) {
  2677. if (_.addressing_model() == spv::AddressingModel::Logical &&
  2678. !_.features().variable_pointers) {
  2679. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2680. << "Instruction cannot for logical addressing model be used without "
  2681. "a variable pointers capability";
  2682. }
  2683. const auto result_type = _.FindDef(inst->type_id());
  2684. if (inst->opcode() == spv::Op::OpPtrDiff) {
  2685. if (!result_type || result_type->opcode() != spv::Op::OpTypeInt) {
  2686. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2687. << "Result Type must be an integer scalar";
  2688. }
  2689. } else {
  2690. if (!result_type || result_type->opcode() != spv::Op::OpTypeBool) {
  2691. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2692. << "Result Type must be OpTypeBool";
  2693. }
  2694. }
  2695. const auto op1 = _.FindDef(inst->GetOperandAs<uint32_t>(2u));
  2696. const auto op2 = _.FindDef(inst->GetOperandAs<uint32_t>(3u));
  2697. const auto op1_type = _.FindDef(op1->type_id());
  2698. const auto op2_type = _.FindDef(op2->type_id());
  2699. if (!op1_type || (op1_type->opcode() != spv::Op::OpTypePointer &&
  2700. op1_type->opcode() != spv::Op::OpTypeUntypedPointerKHR)) {
  2701. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2702. << "Operand type must be a pointer";
  2703. }
  2704. if (!op2_type || (op2_type->opcode() != spv::Op::OpTypePointer &&
  2705. op2_type->opcode() != spv::Op::OpTypeUntypedPointerKHR)) {
  2706. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2707. << "Operand type must be a pointer";
  2708. }
  2709. if (inst->opcode() == spv::Op::OpPtrDiff) {
  2710. if (op1->type_id() != op2->type_id()) {
  2711. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2712. << "The types of Operand 1 and Operand 2 must match";
  2713. }
  2714. } else {
  2715. const auto either_untyped =
  2716. op1_type->opcode() == spv::Op::OpTypeUntypedPointerKHR ||
  2717. op2_type->opcode() == spv::Op::OpTypeUntypedPointerKHR;
  2718. if (either_untyped) {
  2719. const auto sc1 = op1_type->GetOperandAs<spv::StorageClass>(1);
  2720. const auto sc2 = op2_type->GetOperandAs<spv::StorageClass>(1);
  2721. if (sc1 != sc2) {
  2722. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2723. << "Pointer storage classes must match";
  2724. }
  2725. } else if (op1->type_id() != op2->type_id()) {
  2726. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2727. << "The types of Operand 1 and Operand 2 must match";
  2728. }
  2729. }
  2730. spv::StorageClass sc = op1_type->GetOperandAs<spv::StorageClass>(1u);
  2731. if (_.addressing_model() == spv::AddressingModel::Logical) {
  2732. if (sc != spv::StorageClass::Workgroup &&
  2733. sc != spv::StorageClass::StorageBuffer) {
  2734. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2735. << "Invalid pointer storage class";
  2736. }
  2737. if (sc == spv::StorageClass::Workgroup &&
  2738. !_.HasCapability(spv::Capability::VariablePointers)) {
  2739. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2740. << "Workgroup storage class pointer requires VariablePointers "
  2741. "capability to be specified";
  2742. }
  2743. } else if (sc == spv::StorageClass::PhysicalStorageBuffer) {
  2744. return _.diag(SPV_ERROR_INVALID_ID, inst)
  2745. << "Cannot use a pointer in the PhysicalStorageBuffer storage class";
  2746. }
  2747. return SPV_SUCCESS;
  2748. }
  2749. } // namespace
  2750. spv_result_t MemoryPass(ValidationState_t& _, const Instruction* inst) {
  2751. switch (inst->opcode()) {
  2752. case spv::Op::OpVariable:
  2753. case spv::Op::OpUntypedVariableKHR:
  2754. if (auto error = ValidateVariable(_, inst)) return error;
  2755. break;
  2756. case spv::Op::OpLoad:
  2757. if (auto error = ValidateLoad(_, inst)) return error;
  2758. break;
  2759. case spv::Op::OpStore:
  2760. if (auto error = ValidateStore(_, inst)) return error;
  2761. break;
  2762. case spv::Op::OpCopyMemory:
  2763. case spv::Op::OpCopyMemorySized:
  2764. if (auto error = ValidateCopyMemory(_, inst)) return error;
  2765. break;
  2766. case spv::Op::OpPtrAccessChain:
  2767. case spv::Op::OpUntypedPtrAccessChainKHR:
  2768. case spv::Op::OpUntypedInBoundsPtrAccessChainKHR:
  2769. if (auto error = ValidatePtrAccessChain(_, inst)) return error;
  2770. break;
  2771. case spv::Op::OpAccessChain:
  2772. case spv::Op::OpInBoundsAccessChain:
  2773. case spv::Op::OpInBoundsPtrAccessChain:
  2774. case spv::Op::OpUntypedAccessChainKHR:
  2775. case spv::Op::OpUntypedInBoundsAccessChainKHR:
  2776. if (auto error = ValidateAccessChain(_, inst)) return error;
  2777. break;
  2778. case spv::Op::OpRawAccessChainNV:
  2779. if (auto error = ValidateRawAccessChain(_, inst)) return error;
  2780. break;
  2781. case spv::Op::OpArrayLength:
  2782. case spv::Op::OpUntypedArrayLengthKHR:
  2783. if (auto error = ValidateArrayLength(_, inst)) return error;
  2784. break;
  2785. case spv::Op::OpCooperativeMatrixLoadNV:
  2786. case spv::Op::OpCooperativeMatrixStoreNV:
  2787. if (auto error = ValidateCooperativeMatrixLoadStoreNV(_, inst))
  2788. return error;
  2789. break;
  2790. case spv::Op::OpCooperativeMatrixLengthKHR:
  2791. case spv::Op::OpCooperativeMatrixLengthNV:
  2792. if (auto error = ValidateCooperativeMatrixLengthNV(_, inst)) return error;
  2793. break;
  2794. case spv::Op::OpCooperativeMatrixLoadKHR:
  2795. case spv::Op::OpCooperativeMatrixStoreKHR:
  2796. if (auto error = ValidateCooperativeMatrixLoadStoreKHR(_, inst))
  2797. return error;
  2798. break;
  2799. case spv::Op::OpCooperativeMatrixLoadTensorNV:
  2800. case spv::Op::OpCooperativeMatrixStoreTensorNV:
  2801. if (auto error = ValidateCooperativeMatrixLoadStoreTensorNV(_, inst))
  2802. return error;
  2803. break;
  2804. case spv::Op::OpCooperativeVectorLoadNV:
  2805. case spv::Op::OpCooperativeVectorStoreNV:
  2806. if (auto error = ValidateCooperativeVectorLoadStoreNV(_, inst))
  2807. return error;
  2808. break;
  2809. case spv::Op::OpCooperativeVectorOuterProductAccumulateNV:
  2810. if (auto error = ValidateCooperativeVectorOuterProductNV(_, inst))
  2811. return error;
  2812. break;
  2813. case spv::Op::OpCooperativeVectorReduceSumAccumulateNV:
  2814. if (auto error = ValidateCooperativeVectorReduceSumNV(_, inst))
  2815. return error;
  2816. break;
  2817. case spv::Op::OpCooperativeVectorMatrixMulNV:
  2818. case spv::Op::OpCooperativeVectorMatrixMulAddNV:
  2819. if (auto error = ValidateCooperativeVectorMatrixMulNV(_, inst))
  2820. return error;
  2821. break;
  2822. case spv::Op::OpPtrEqual:
  2823. case spv::Op::OpPtrNotEqual:
  2824. case spv::Op::OpPtrDiff:
  2825. if (auto error = ValidatePtrComparison(_, inst)) return error;
  2826. break;
  2827. case spv::Op::OpImageTexelPointer:
  2828. case spv::Op::OpGenericPtrMemSemantics:
  2829. default:
  2830. break;
  2831. }
  2832. return SPV_SUCCESS;
  2833. }
  2834. } // namespace val
  2835. } // namespace spvtools