validate_memory.cpp 135 KB

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