validate_memory.cpp 128 KB

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