validate_memory.cpp 74 KB

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