validate_memory.cpp 120 KB

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