validate_memory.cpp 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753
  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<uint32_t> allowed) {
  38. if (std::find(allowed.begin(), allowed.end(), type->opcode()) !=
  39. allowed.end()) {
  40. return true;
  41. }
  42. if (type->opcode() == SpvOpTypeArray ||
  43. type->opcode() == SpvOpTypeRuntimeArray) {
  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() != SpvOpTypeStruct) {
  55. return false;
  56. }
  57. if (type2->opcode() != SpvOpTypeStruct) {
  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() == SpvOpTypeStruct &&
  69. "type1 must be an OpTypeStruct instruction.");
  70. assert(type2->opcode() == SpvOpTypeStruct &&
  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() == SpvOpTypeStruct &&
  94. "type1 must be an OpTypeStruct instruction.");
  95. assert(type2->opcode() == SpvOpTypeStruct &&
  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 SpvDecorationOffset: {
  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() != SpvDecorationOffset) 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() == SpvDecorationBuiltIn) 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 SpvOpTypeBool:
  159. return true;
  160. case SpvOpTypeVector:
  161. case SpvOpTypeMatrix:
  162. case SpvOpTypeArray:
  163. case SpvOpTypeRuntimeArray:
  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 SpvOpTypeStruct:
  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 SpvOpTypeCooperativeMatrixNV:
  188. return true;
  189. case SpvOpTypeArray:
  190. case SpvOpTypeRuntimeArray:
  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 SpvOpTypeStruct:
  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<SpvStorageClass, SpvStorageClass> GetStorageClass(
  210. ValidationState_t& _, const Instruction* inst) {
  211. SpvStorageClass dst_sc = SpvStorageClassMax;
  212. SpvStorageClass src_sc = SpvStorageClassMax;
  213. switch (inst->opcode()) {
  214. case SpvOpCooperativeMatrixLoadNV:
  215. case SpvOpLoad: {
  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<SpvStorageClass>(1);
  219. break;
  220. }
  221. case SpvOpCooperativeMatrixStoreNV:
  222. case SpvOpStore: {
  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<SpvStorageClass>(1);
  226. break;
  227. }
  228. case SpvOpCopyMemory:
  229. case SpvOpCopyMemorySized: {
  230. auto dst = _.FindDef(inst->GetOperandAs<uint32_t>(0));
  231. auto dst_type = _.FindDef(dst->type_id());
  232. dst_sc = dst_type->GetOperandAs<SpvStorageClass>(1);
  233. auto src = _.FindDef(inst->GetOperandAs<uint32_t>(1));
  234. auto src_type = _.FindDef(src->type_id());
  235. src_sc = src_type->GetOperandAs<SpvStorageClass>(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 & SpvMemoryAccessAlignedMask) ++result;
  248. if (mask & SpvMemoryAccessMakePointerAvailableKHRMask) ++result;
  249. if (mask & SpvMemoryAccessMakePointerVisibleKHRMask) ++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 & SpvMemoryAccessMakePointerAvailableKHRMask);
  260. uint32_t this_bit = uint32_t(SpvMemoryAccessMakePointerAvailableKHRMask);
  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 & SpvMemoryAccessMakePointerVisibleKHRMask);
  271. uint32_t this_bit = uint32_t(SpvMemoryAccessMakePointerVisibleKHRMask);
  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() == SpvOpTypeRuntimeArray) return true;
  282. }
  283. return false;
  284. }
  285. spv_result_t CheckMemoryAccess(ValidationState_t& _, const Instruction* inst,
  286. uint32_t index) {
  287. SpvStorageClass 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 == SpvStorageClassPhysicalStorageBuffer ||
  292. dst_sc == SpvStorageClassPhysicalStorageBuffer) {
  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 & SpvMemoryAccessMakePointerAvailableKHRMask) {
  301. if (inst->opcode() == SpvOpLoad ||
  302. inst->opcode() == SpvOpCooperativeMatrixLoadNV) {
  303. return _.diag(SPV_ERROR_INVALID_ID, inst)
  304. << "MakePointerAvailableKHR cannot be used with OpLoad.";
  305. }
  306. if (!(mask & SpvMemoryAccessNonPrivatePointerKHRMask)) {
  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 & SpvMemoryAccessMakePointerVisibleKHRMask) {
  317. if (inst->opcode() == SpvOpStore ||
  318. inst->opcode() == SpvOpCooperativeMatrixStoreNV) {
  319. return _.diag(SPV_ERROR_INVALID_ID, inst)
  320. << "MakePointerVisibleKHR cannot be used with OpStore.";
  321. }
  322. if (!(mask & SpvMemoryAccessNonPrivatePointerKHRMask)) {
  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 & SpvMemoryAccessNonPrivatePointerKHRMask) {
  332. if (dst_sc != SpvStorageClassUniform &&
  333. dst_sc != SpvStorageClassWorkgroup &&
  334. dst_sc != SpvStorageClassCrossWorkgroup &&
  335. dst_sc != SpvStorageClassGeneric && dst_sc != SpvStorageClassImage &&
  336. dst_sc != SpvStorageClassStorageBuffer &&
  337. dst_sc != SpvStorageClassPhysicalStorageBuffer) {
  338. return _.diag(SPV_ERROR_INVALID_ID, inst)
  339. << "NonPrivatePointerKHR requires a pointer in Uniform, "
  340. << "Workgroup, CrossWorkgroup, Generic, Image or StorageBuffer "
  341. << "storage classes.";
  342. }
  343. if (src_sc != SpvStorageClassMax && src_sc != SpvStorageClassUniform &&
  344. src_sc != SpvStorageClassWorkgroup &&
  345. src_sc != SpvStorageClassCrossWorkgroup &&
  346. src_sc != SpvStorageClassGeneric && src_sc != SpvStorageClassImage &&
  347. src_sc != SpvStorageClassStorageBuffer &&
  348. src_sc != SpvStorageClassPhysicalStorageBuffer) {
  349. return _.diag(SPV_ERROR_INVALID_ID, inst)
  350. << "NonPrivatePointerKHR requires a pointer in Uniform, "
  351. << "Workgroup, CrossWorkgroup, Generic, Image or StorageBuffer "
  352. << "storage classes.";
  353. }
  354. }
  355. if (!(mask & SpvMemoryAccessAlignedMask)) {
  356. if (src_sc == SpvStorageClassPhysicalStorageBuffer ||
  357. dst_sc == SpvStorageClassPhysicalStorageBuffer) {
  358. return _.diag(SPV_ERROR_INVALID_ID, inst)
  359. << _.VkErrorID(4708)
  360. << "Memory accesses with PhysicalStorageBuffer must use Aligned.";
  361. }
  362. }
  363. return SPV_SUCCESS;
  364. }
  365. spv_result_t ValidateVariable(ValidationState_t& _, const Instruction* inst) {
  366. auto result_type = _.FindDef(inst->type_id());
  367. if (!result_type || result_type->opcode() != SpvOpTypePointer) {
  368. return _.diag(SPV_ERROR_INVALID_ID, inst)
  369. << "OpVariable Result Type <id> " << _.getIdName(inst->type_id())
  370. << " is not a pointer type.";
  371. }
  372. const auto type_index = 2;
  373. const auto value_id = result_type->GetOperandAs<uint32_t>(type_index);
  374. auto value_type = _.FindDef(value_id);
  375. const auto initializer_index = 3;
  376. const auto storage_class_index = 2;
  377. if (initializer_index < inst->operands().size()) {
  378. const auto initializer_id = inst->GetOperandAs<uint32_t>(initializer_index);
  379. const auto initializer = _.FindDef(initializer_id);
  380. const auto is_module_scope_var =
  381. initializer && (initializer->opcode() == SpvOpVariable) &&
  382. (initializer->GetOperandAs<SpvStorageClass>(storage_class_index) !=
  383. SpvStorageClassFunction);
  384. const auto is_constant =
  385. initializer && spvOpcodeIsConstant(initializer->opcode());
  386. if (!initializer || !(is_constant || is_module_scope_var)) {
  387. return _.diag(SPV_ERROR_INVALID_ID, inst)
  388. << "OpVariable Initializer <id> " << _.getIdName(initializer_id)
  389. << " is not a constant or module-scope variable.";
  390. }
  391. if (initializer->type_id() != value_id) {
  392. return _.diag(SPV_ERROR_INVALID_ID, inst)
  393. << "Initializer type must match the type pointed to by the Result "
  394. "Type";
  395. }
  396. }
  397. auto storage_class = inst->GetOperandAs<SpvStorageClass>(storage_class_index);
  398. if (storage_class != SpvStorageClassWorkgroup &&
  399. storage_class != SpvStorageClassCrossWorkgroup &&
  400. storage_class != SpvStorageClassPrivate &&
  401. storage_class != SpvStorageClassFunction &&
  402. storage_class != SpvStorageClassRayPayloadKHR &&
  403. storage_class != SpvStorageClassIncomingRayPayloadKHR &&
  404. storage_class != SpvStorageClassHitAttributeKHR &&
  405. storage_class != SpvStorageClassCallableDataKHR &&
  406. storage_class != SpvStorageClassIncomingCallableDataKHR &&
  407. storage_class != SpvStorageClassTaskPayloadWorkgroupEXT) {
  408. bool storage_input_or_output = storage_class == SpvStorageClassInput ||
  409. storage_class == SpvStorageClassOutput;
  410. bool builtin = false;
  411. if (storage_input_or_output) {
  412. for (const Decoration& decoration : _.id_decorations(inst->id())) {
  413. if (decoration.dec_type() == SpvDecorationBuiltIn) {
  414. builtin = true;
  415. break;
  416. }
  417. }
  418. }
  419. if (!builtin &&
  420. ContainsInvalidBool(_, value_type, storage_input_or_output)) {
  421. if (storage_input_or_output) {
  422. return _.diag(SPV_ERROR_INVALID_ID, inst)
  423. << _.VkErrorID(7290)
  424. << "If OpTypeBool is stored in conjunction with OpVariable "
  425. "using Input or Output Storage Classes it requires a BuiltIn "
  426. "decoration";
  427. } else {
  428. return _.diag(SPV_ERROR_INVALID_ID, inst)
  429. << "If OpTypeBool is stored in conjunction with OpVariable, it "
  430. "can only be used with non-externally visible shader Storage "
  431. "Classes: Workgroup, CrossWorkgroup, Private, Function, "
  432. "Input, Output, RayPayloadKHR, IncomingRayPayloadKHR, "
  433. "HitAttributeKHR, CallableDataKHR, or "
  434. "IncomingCallableDataKHR";
  435. }
  436. }
  437. }
  438. if (!_.IsValidStorageClass(storage_class)) {
  439. return _.diag(SPV_ERROR_INVALID_BINARY, inst)
  440. << _.VkErrorID(4643)
  441. << "Invalid storage class for target environment";
  442. }
  443. if (storage_class == SpvStorageClassGeneric) {
  444. return _.diag(SPV_ERROR_INVALID_BINARY, inst)
  445. << "OpVariable storage class cannot be Generic";
  446. }
  447. if (inst->function() && storage_class != SpvStorageClassFunction) {
  448. return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
  449. << "Variables must have a function[7] storage class inside"
  450. " of a function";
  451. }
  452. if (!inst->function() && storage_class == SpvStorageClassFunction) {
  453. return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
  454. << "Variables can not have a function[7] storage class "
  455. "outside of a function";
  456. }
  457. // SPIR-V 3.32.8: Check that pointer type and variable type have the same
  458. // storage class.
  459. const auto result_storage_class_index = 1;
  460. const auto result_storage_class =
  461. result_type->GetOperandAs<uint32_t>(result_storage_class_index);
  462. if (storage_class != result_storage_class) {
  463. return _.diag(SPV_ERROR_INVALID_ID, inst)
  464. << "From SPIR-V spec, section 3.32.8 on OpVariable:\n"
  465. << "Its Storage Class operand must be the same as the Storage Class "
  466. << "operand of the result type.";
  467. }
  468. // Variable pointer related restrictions.
  469. const auto pointee = _.FindDef(result_type->word(3));
  470. if (_.addressing_model() == SpvAddressingModelLogical &&
  471. !_.options()->relax_logical_pointer) {
  472. // VariablePointersStorageBuffer is implied by VariablePointers.
  473. if (pointee->opcode() == SpvOpTypePointer) {
  474. if (!_.HasCapability(SpvCapabilityVariablePointersStorageBuffer)) {
  475. return _.diag(SPV_ERROR_INVALID_ID, inst)
  476. << "In Logical addressing, variables may not allocate a pointer "
  477. << "type";
  478. } else if (storage_class != SpvStorageClassFunction &&
  479. storage_class != SpvStorageClassPrivate) {
  480. return _.diag(SPV_ERROR_INVALID_ID, inst)
  481. << "In Logical addressing with variable pointers, variables "
  482. << "that allocate pointers must be in Function or Private "
  483. << "storage classes";
  484. }
  485. }
  486. }
  487. if (spvIsVulkanEnv(_.context()->target_env)) {
  488. // Vulkan Push Constant Interface section: Check type of PushConstant
  489. // variables.
  490. if (storage_class == SpvStorageClassPushConstant) {
  491. if (pointee->opcode() != SpvOpTypeStruct) {
  492. return _.diag(SPV_ERROR_INVALID_ID, inst)
  493. << _.VkErrorID(6808) << "PushConstant OpVariable <id> "
  494. << _.getIdName(inst->id()) << " has illegal type.\n"
  495. << "From Vulkan spec, Push Constant Interface section:\n"
  496. << "Such variables must be typed as OpTypeStruct";
  497. }
  498. }
  499. // Vulkan Descriptor Set Interface: Check type of UniformConstant and
  500. // Uniform variables.
  501. if (storage_class == SpvStorageClassUniformConstant) {
  502. if (!IsAllowedTypeOrArrayOfSame(
  503. _, pointee,
  504. {SpvOpTypeImage, SpvOpTypeSampler, SpvOpTypeSampledImage,
  505. SpvOpTypeAccelerationStructureKHR})) {
  506. return _.diag(SPV_ERROR_INVALID_ID, inst)
  507. << _.VkErrorID(4655) << "UniformConstant OpVariable <id> "
  508. << _.getIdName(inst->id()) << " has illegal type.\n"
  509. << "Variables identified with the UniformConstant storage class "
  510. << "are used only as handles to refer to opaque resources. Such "
  511. << "variables must be typed as OpTypeImage, OpTypeSampler, "
  512. << "OpTypeSampledImage, OpTypeAccelerationStructureKHR, "
  513. << "or an array of one of these types.";
  514. }
  515. }
  516. if (storage_class == SpvStorageClassUniform) {
  517. if (!IsAllowedTypeOrArrayOfSame(_, pointee, {SpvOpTypeStruct})) {
  518. return _.diag(SPV_ERROR_INVALID_ID, inst)
  519. << _.VkErrorID(6807) << "Uniform OpVariable <id> "
  520. << _.getIdName(inst->id()) << " has illegal type.\n"
  521. << "From Vulkan spec:\n"
  522. << "Variables identified with the Uniform storage class are "
  523. << "used to access transparent buffer backed resources. Such "
  524. << "variables must be typed as OpTypeStruct, or an array of "
  525. << "this type";
  526. }
  527. }
  528. if (storage_class == SpvStorageClassStorageBuffer) {
  529. if (!IsAllowedTypeOrArrayOfSame(_, pointee, {SpvOpTypeStruct})) {
  530. return _.diag(SPV_ERROR_INVALID_ID, inst)
  531. << _.VkErrorID(6807) << "StorageBuffer OpVariable <id> "
  532. << _.getIdName(inst->id()) << " has illegal type.\n"
  533. << "From Vulkan spec:\n"
  534. << "Variables identified with the StorageBuffer storage class "
  535. "are used to access transparent buffer backed resources. "
  536. "Such variables must be typed as OpTypeStruct, or an array "
  537. "of this type";
  538. }
  539. }
  540. // Check for invalid use of Invariant
  541. if (storage_class != SpvStorageClassInput &&
  542. storage_class != SpvStorageClassOutput) {
  543. if (_.HasDecoration(inst->id(), SpvDecorationInvariant)) {
  544. return _.diag(SPV_ERROR_INVALID_ID, inst)
  545. << _.VkErrorID(4677)
  546. << "Variable decorated with Invariant must only be identified "
  547. "with the Input or Output storage class in Vulkan "
  548. "environment.";
  549. }
  550. // Need to check if only the members in a struct are decorated
  551. if (value_type && value_type->opcode() == SpvOpTypeStruct) {
  552. if (_.HasDecoration(value_id, SpvDecorationInvariant)) {
  553. return _.diag(SPV_ERROR_INVALID_ID, inst)
  554. << _.VkErrorID(4677)
  555. << "Variable struct member decorated with Invariant must only "
  556. "be identified with the Input or Output storage class in "
  557. "Vulkan environment.";
  558. }
  559. }
  560. }
  561. // Initializers in Vulkan are only allowed in some storage clases
  562. if (inst->operands().size() > 3) {
  563. if (storage_class == SpvStorageClassWorkgroup) {
  564. auto init_id = inst->GetOperandAs<uint32_t>(3);
  565. auto init = _.FindDef(init_id);
  566. if (init->opcode() != SpvOpConstantNull) {
  567. return _.diag(SPV_ERROR_INVALID_ID, inst)
  568. << _.VkErrorID(4734) << "OpVariable, <id> "
  569. << _.getIdName(inst->id())
  570. << ", initializers are limited to OpConstantNull in "
  571. "Workgroup "
  572. "storage class";
  573. }
  574. } else if (storage_class != SpvStorageClassOutput &&
  575. storage_class != SpvStorageClassPrivate &&
  576. storage_class != SpvStorageClassFunction) {
  577. return _.diag(SPV_ERROR_INVALID_ID, inst)
  578. << _.VkErrorID(4651) << "OpVariable, <id> "
  579. << _.getIdName(inst->id())
  580. << ", has a disallowed initializer & storage class "
  581. << "combination.\n"
  582. << "From " << spvLogStringForEnv(_.context()->target_env)
  583. << " spec:\n"
  584. << "Variable declarations that include initializers must have "
  585. << "one of the following storage classes: Output, Private, "
  586. << "Function or Workgroup";
  587. }
  588. }
  589. }
  590. if (inst->operands().size() > 3) {
  591. if (storage_class == SpvStorageClassTaskPayloadWorkgroupEXT) {
  592. return _.diag(SPV_ERROR_INVALID_ID, inst)
  593. << "OpVariable, <id> " << _.getIdName(inst->id())
  594. << ", initializer are not allowed for TaskPayloadWorkgroupEXT";
  595. }
  596. if (storage_class == SpvStorageClassInput) {
  597. return _.diag(SPV_ERROR_INVALID_ID, inst)
  598. << "OpVariable, <id> " << _.getIdName(inst->id())
  599. << ", initializer are not allowed for Input";
  600. }
  601. }
  602. if (storage_class == SpvStorageClassPhysicalStorageBuffer) {
  603. return _.diag(SPV_ERROR_INVALID_ID, inst)
  604. << "PhysicalStorageBuffer must not be used with OpVariable.";
  605. }
  606. auto pointee_base = pointee;
  607. while (pointee_base->opcode() == SpvOpTypeArray) {
  608. pointee_base = _.FindDef(pointee_base->GetOperandAs<uint32_t>(1u));
  609. }
  610. if (pointee_base->opcode() == SpvOpTypePointer) {
  611. if (pointee_base->GetOperandAs<uint32_t>(1u) ==
  612. SpvStorageClassPhysicalStorageBuffer) {
  613. // check for AliasedPointer/RestrictPointer
  614. bool foundAliased =
  615. _.HasDecoration(inst->id(), SpvDecorationAliasedPointer);
  616. bool foundRestrict =
  617. _.HasDecoration(inst->id(), SpvDecorationRestrictPointer);
  618. if (!foundAliased && !foundRestrict) {
  619. return _.diag(SPV_ERROR_INVALID_ID, inst)
  620. << "OpVariable " << inst->id()
  621. << ": expected AliasedPointer or RestrictPointer for "
  622. << "PhysicalStorageBuffer pointer.";
  623. }
  624. if (foundAliased && foundRestrict) {
  625. return _.diag(SPV_ERROR_INVALID_ID, inst)
  626. << "OpVariable " << inst->id()
  627. << ": can't specify both AliasedPointer and "
  628. << "RestrictPointer for PhysicalStorageBuffer pointer.";
  629. }
  630. }
  631. }
  632. // Vulkan specific validation rules for OpTypeRuntimeArray
  633. if (spvIsVulkanEnv(_.context()->target_env)) {
  634. // OpTypeRuntimeArray should only ever be in a container like OpTypeStruct,
  635. // so should never appear as a bare variable.
  636. // Unless the module has the RuntimeDescriptorArrayEXT capability.
  637. if (value_type && value_type->opcode() == SpvOpTypeRuntimeArray) {
  638. if (!_.HasCapability(SpvCapabilityRuntimeDescriptorArrayEXT)) {
  639. return _.diag(SPV_ERROR_INVALID_ID, inst)
  640. << _.VkErrorID(4680) << "OpVariable, <id> "
  641. << _.getIdName(inst->id())
  642. << ", is attempting to create memory for an illegal type, "
  643. << "OpTypeRuntimeArray.\nFor Vulkan OpTypeRuntimeArray can only "
  644. << "appear as the final member of an OpTypeStruct, thus cannot "
  645. << "be instantiated via OpVariable";
  646. } else {
  647. // A bare variable OpTypeRuntimeArray is allowed in this context, but
  648. // still need to check the storage class.
  649. if (storage_class != SpvStorageClassStorageBuffer &&
  650. storage_class != SpvStorageClassUniform &&
  651. storage_class != SpvStorageClassUniformConstant) {
  652. return _.diag(SPV_ERROR_INVALID_ID, inst)
  653. << _.VkErrorID(4680)
  654. << "For Vulkan with RuntimeDescriptorArrayEXT, a variable "
  655. << "containing OpTypeRuntimeArray must have storage class of "
  656. << "StorageBuffer, Uniform, or UniformConstant.";
  657. }
  658. }
  659. }
  660. // If an OpStruct has an OpTypeRuntimeArray somewhere within it, then it
  661. // must either have the storage class StorageBuffer and be decorated
  662. // with Block, or it must be in the Uniform storage class and be decorated
  663. // as BufferBlock.
  664. if (value_type && value_type->opcode() == SpvOpTypeStruct) {
  665. if (DoesStructContainRTA(_, value_type)) {
  666. if (storage_class == SpvStorageClassStorageBuffer ||
  667. storage_class == SpvStorageClassPhysicalStorageBuffer) {
  668. if (!_.HasDecoration(value_id, SpvDecorationBlock)) {
  669. return _.diag(SPV_ERROR_INVALID_ID, inst)
  670. << _.VkErrorID(4680)
  671. << "For Vulkan, an OpTypeStruct variable containing an "
  672. << "OpTypeRuntimeArray must be decorated with Block if it "
  673. << "has storage class StorageBuffer or "
  674. "PhysicalStorageBuffer.";
  675. }
  676. } else if (storage_class == SpvStorageClassUniform) {
  677. if (!_.HasDecoration(value_id, SpvDecorationBufferBlock)) {
  678. return _.diag(SPV_ERROR_INVALID_ID, inst)
  679. << _.VkErrorID(4680)
  680. << "For Vulkan, an OpTypeStruct variable containing an "
  681. << "OpTypeRuntimeArray must be decorated with BufferBlock "
  682. << "if it has storage class Uniform.";
  683. }
  684. } else {
  685. return _.diag(SPV_ERROR_INVALID_ID, inst)
  686. << _.VkErrorID(4680)
  687. << "For Vulkan, OpTypeStruct variables containing "
  688. << "OpTypeRuntimeArray must have storage class of "
  689. << "StorageBuffer, PhysicalStorageBuffer, or Uniform.";
  690. }
  691. }
  692. }
  693. }
  694. // Cooperative matrix types can only be allocated in Function or Private
  695. if ((storage_class != SpvStorageClassFunction &&
  696. storage_class != SpvStorageClassPrivate) &&
  697. ContainsCooperativeMatrix(_, pointee)) {
  698. return _.diag(SPV_ERROR_INVALID_ID, inst)
  699. << "Cooperative matrix types (or types containing them) can only be "
  700. "allocated "
  701. << "in Function or Private storage classes or as function "
  702. "parameters";
  703. }
  704. if (_.HasCapability(SpvCapabilityShader)) {
  705. // Don't allow variables containing 16-bit elements without the appropriate
  706. // capabilities.
  707. if ((!_.HasCapability(SpvCapabilityInt16) &&
  708. _.ContainsSizedIntOrFloatType(value_id, SpvOpTypeInt, 16)) ||
  709. (!_.HasCapability(SpvCapabilityFloat16) &&
  710. _.ContainsSizedIntOrFloatType(value_id, SpvOpTypeFloat, 16))) {
  711. auto underlying_type = value_type;
  712. while (underlying_type->opcode() == SpvOpTypePointer) {
  713. storage_class = underlying_type->GetOperandAs<SpvStorageClass>(1u);
  714. underlying_type =
  715. _.FindDef(underlying_type->GetOperandAs<uint32_t>(2u));
  716. }
  717. bool storage_class_ok = true;
  718. std::string sc_name = _.grammar().lookupOperandName(
  719. SPV_OPERAND_TYPE_STORAGE_CLASS, storage_class);
  720. switch (storage_class) {
  721. case SpvStorageClassStorageBuffer:
  722. case SpvStorageClassPhysicalStorageBuffer:
  723. if (!_.HasCapability(SpvCapabilityStorageBuffer16BitAccess)) {
  724. storage_class_ok = false;
  725. }
  726. break;
  727. case SpvStorageClassUniform:
  728. if (!_.HasCapability(
  729. SpvCapabilityUniformAndStorageBuffer16BitAccess)) {
  730. if (underlying_type->opcode() == SpvOpTypeArray ||
  731. underlying_type->opcode() == SpvOpTypeRuntimeArray) {
  732. underlying_type =
  733. _.FindDef(underlying_type->GetOperandAs<uint32_t>(1u));
  734. }
  735. if (!_.HasCapability(SpvCapabilityStorageBuffer16BitAccess) ||
  736. !_.HasDecoration(underlying_type->id(),
  737. SpvDecorationBufferBlock)) {
  738. storage_class_ok = false;
  739. }
  740. }
  741. break;
  742. case SpvStorageClassPushConstant:
  743. if (!_.HasCapability(SpvCapabilityStoragePushConstant16)) {
  744. storage_class_ok = false;
  745. }
  746. break;
  747. case SpvStorageClassInput:
  748. case SpvStorageClassOutput:
  749. if (!_.HasCapability(SpvCapabilityStorageInputOutput16)) {
  750. storage_class_ok = false;
  751. }
  752. break;
  753. case SpvStorageClassWorkgroup:
  754. if (!_.HasCapability(SpvCapabilityWorkgroupMemoryExplicitLayout16BitAccessKHR)) {
  755. storage_class_ok = false;
  756. }
  757. break;
  758. default:
  759. return _.diag(SPV_ERROR_INVALID_ID, inst)
  760. << "Cannot allocate a variable containing a 16-bit type in "
  761. << sc_name << " storage class";
  762. }
  763. if (!storage_class_ok) {
  764. return _.diag(SPV_ERROR_INVALID_ID, inst)
  765. << "Allocating a variable containing a 16-bit element in "
  766. << sc_name << " storage class requires an additional capability";
  767. }
  768. }
  769. // Don't allow variables containing 8-bit elements without the appropriate
  770. // capabilities.
  771. if (!_.HasCapability(SpvCapabilityInt8) &&
  772. _.ContainsSizedIntOrFloatType(value_id, SpvOpTypeInt, 8)) {
  773. auto underlying_type = value_type;
  774. while (underlying_type->opcode() == SpvOpTypePointer) {
  775. storage_class = underlying_type->GetOperandAs<SpvStorageClass>(1u);
  776. underlying_type =
  777. _.FindDef(underlying_type->GetOperandAs<uint32_t>(2u));
  778. }
  779. bool storage_class_ok = true;
  780. std::string sc_name = _.grammar().lookupOperandName(
  781. SPV_OPERAND_TYPE_STORAGE_CLASS, storage_class);
  782. switch (storage_class) {
  783. case SpvStorageClassStorageBuffer:
  784. case SpvStorageClassPhysicalStorageBuffer:
  785. if (!_.HasCapability(SpvCapabilityStorageBuffer8BitAccess)) {
  786. storage_class_ok = false;
  787. }
  788. break;
  789. case SpvStorageClassUniform:
  790. if (!_.HasCapability(
  791. SpvCapabilityUniformAndStorageBuffer8BitAccess)) {
  792. if (underlying_type->opcode() == SpvOpTypeArray ||
  793. underlying_type->opcode() == SpvOpTypeRuntimeArray) {
  794. underlying_type =
  795. _.FindDef(underlying_type->GetOperandAs<uint32_t>(1u));
  796. }
  797. if (!_.HasCapability(SpvCapabilityStorageBuffer8BitAccess) ||
  798. !_.HasDecoration(underlying_type->id(),
  799. SpvDecorationBufferBlock)) {
  800. storage_class_ok = false;
  801. }
  802. }
  803. break;
  804. case SpvStorageClassPushConstant:
  805. if (!_.HasCapability(SpvCapabilityStoragePushConstant8)) {
  806. storage_class_ok = false;
  807. }
  808. break;
  809. case SpvStorageClassWorkgroup:
  810. if (!_.HasCapability(SpvCapabilityWorkgroupMemoryExplicitLayout8BitAccessKHR)) {
  811. storage_class_ok = false;
  812. }
  813. break;
  814. default:
  815. return _.diag(SPV_ERROR_INVALID_ID, inst)
  816. << "Cannot allocate a variable containing a 8-bit type in "
  817. << sc_name << " storage class";
  818. }
  819. if (!storage_class_ok) {
  820. return _.diag(SPV_ERROR_INVALID_ID, inst)
  821. << "Allocating a variable containing a 8-bit element in "
  822. << sc_name << " storage class requires an additional capability";
  823. }
  824. }
  825. }
  826. return SPV_SUCCESS;
  827. }
  828. spv_result_t ValidateLoad(ValidationState_t& _, const Instruction* inst) {
  829. const auto result_type = _.FindDef(inst->type_id());
  830. if (!result_type) {
  831. return _.diag(SPV_ERROR_INVALID_ID, inst)
  832. << "OpLoad Result Type <id> " << _.getIdName(inst->type_id())
  833. << " is not defined.";
  834. }
  835. const auto pointer_index = 2;
  836. const auto pointer_id = inst->GetOperandAs<uint32_t>(pointer_index);
  837. const auto pointer = _.FindDef(pointer_id);
  838. if (!pointer ||
  839. ((_.addressing_model() == SpvAddressingModelLogical) &&
  840. ((!_.features().variable_pointers &&
  841. !spvOpcodeReturnsLogicalPointer(pointer->opcode())) ||
  842. (_.features().variable_pointers &&
  843. !spvOpcodeReturnsLogicalVariablePointer(pointer->opcode()))))) {
  844. return _.diag(SPV_ERROR_INVALID_ID, inst)
  845. << "OpLoad Pointer <id> " << _.getIdName(pointer_id)
  846. << " is not a logical pointer.";
  847. }
  848. const auto pointer_type = _.FindDef(pointer->type_id());
  849. if (!pointer_type || pointer_type->opcode() != SpvOpTypePointer) {
  850. return _.diag(SPV_ERROR_INVALID_ID, inst)
  851. << "OpLoad type for pointer <id> " << _.getIdName(pointer_id)
  852. << " is not a pointer type.";
  853. }
  854. uint32_t pointee_data_type;
  855. uint32_t storage_class;
  856. if (!_.GetPointerTypeInfo(pointer_type->id(), &pointee_data_type,
  857. &storage_class) ||
  858. result_type->id() != pointee_data_type) {
  859. return _.diag(SPV_ERROR_INVALID_ID, inst)
  860. << "OpLoad Result Type <id> " << _.getIdName(inst->type_id())
  861. << " does not match Pointer <id> " << _.getIdName(pointer->id())
  862. << "s type.";
  863. }
  864. if (!_.options()->before_hlsl_legalization &&
  865. _.ContainsRuntimeArray(inst->type_id())) {
  866. return _.diag(SPV_ERROR_INVALID_ID, inst)
  867. << "Cannot load a runtime-sized array";
  868. }
  869. if (auto error = CheckMemoryAccess(_, inst, 3)) return error;
  870. if (_.HasCapability(SpvCapabilityShader) &&
  871. _.ContainsLimitedUseIntOrFloatType(inst->type_id()) &&
  872. result_type->opcode() != SpvOpTypePointer) {
  873. if (result_type->opcode() != SpvOpTypeInt &&
  874. result_type->opcode() != SpvOpTypeFloat &&
  875. result_type->opcode() != SpvOpTypeVector &&
  876. result_type->opcode() != SpvOpTypeMatrix) {
  877. return _.diag(SPV_ERROR_INVALID_ID, inst)
  878. << "8- or 16-bit loads must be a scalar, vector or matrix type";
  879. }
  880. }
  881. return SPV_SUCCESS;
  882. }
  883. spv_result_t ValidateStore(ValidationState_t& _, const Instruction* inst) {
  884. const auto pointer_index = 0;
  885. const auto pointer_id = inst->GetOperandAs<uint32_t>(pointer_index);
  886. const auto pointer = _.FindDef(pointer_id);
  887. if (!pointer ||
  888. (_.addressing_model() == SpvAddressingModelLogical &&
  889. ((!_.features().variable_pointers &&
  890. !spvOpcodeReturnsLogicalPointer(pointer->opcode())) ||
  891. (_.features().variable_pointers &&
  892. !spvOpcodeReturnsLogicalVariablePointer(pointer->opcode()))))) {
  893. return _.diag(SPV_ERROR_INVALID_ID, inst)
  894. << "OpStore Pointer <id> " << _.getIdName(pointer_id)
  895. << " is not a logical pointer.";
  896. }
  897. const auto pointer_type = _.FindDef(pointer->type_id());
  898. if (!pointer_type || pointer_type->opcode() != SpvOpTypePointer) {
  899. return _.diag(SPV_ERROR_INVALID_ID, inst)
  900. << "OpStore type for pointer <id> " << _.getIdName(pointer_id)
  901. << " is not a pointer type.";
  902. }
  903. const auto type_id = pointer_type->GetOperandAs<uint32_t>(2);
  904. const auto type = _.FindDef(type_id);
  905. if (!type || SpvOpTypeVoid == type->opcode()) {
  906. return _.diag(SPV_ERROR_INVALID_ID, inst)
  907. << "OpStore Pointer <id> " << _.getIdName(pointer_id)
  908. << "s type is void.";
  909. }
  910. // validate storage class
  911. {
  912. uint32_t data_type;
  913. uint32_t storage_class;
  914. if (!_.GetPointerTypeInfo(pointer_type->id(), &data_type, &storage_class)) {
  915. return _.diag(SPV_ERROR_INVALID_ID, inst)
  916. << "OpStore Pointer <id> " << _.getIdName(pointer_id)
  917. << " is not pointer type";
  918. }
  919. if (storage_class == SpvStorageClassUniformConstant ||
  920. storage_class == SpvStorageClassInput ||
  921. storage_class == SpvStorageClassPushConstant) {
  922. return _.diag(SPV_ERROR_INVALID_ID, inst)
  923. << "OpStore Pointer <id> " << _.getIdName(pointer_id)
  924. << " storage class is read-only";
  925. } else if (storage_class == SpvStorageClassShaderRecordBufferKHR) {
  926. return _.diag(SPV_ERROR_INVALID_ID, inst)
  927. << "ShaderRecordBufferKHR Storage Class variables are read only";
  928. } else if (storage_class == SpvStorageClassHitAttributeKHR) {
  929. std::string errorVUID = _.VkErrorID(4703);
  930. _.function(inst->function()->id())
  931. ->RegisterExecutionModelLimitation(
  932. [errorVUID](SpvExecutionModel model, std::string* message) {
  933. if (model == SpvExecutionModelAnyHitKHR ||
  934. model == SpvExecutionModelClosestHitKHR) {
  935. if (message) {
  936. *message =
  937. errorVUID +
  938. "HitAttributeKHR Storage Class variables are read only "
  939. "with AnyHitKHR and ClosestHitKHR";
  940. }
  941. return false;
  942. }
  943. return true;
  944. });
  945. }
  946. if (spvIsVulkanEnv(_.context()->target_env) &&
  947. storage_class == SpvStorageClassUniform) {
  948. auto base_ptr = _.TracePointer(pointer);
  949. if (base_ptr->opcode() == SpvOpVariable) {
  950. // If it's not a variable a different check should catch the problem.
  951. auto base_type = _.FindDef(base_ptr->GetOperandAs<uint32_t>(0));
  952. // Get the pointed-to type.
  953. base_type = _.FindDef(base_type->GetOperandAs<uint32_t>(2u));
  954. if (base_type->opcode() == SpvOpTypeArray ||
  955. base_type->opcode() == SpvOpTypeRuntimeArray) {
  956. base_type = _.FindDef(base_type->GetOperandAs<uint32_t>(1u));
  957. }
  958. if (_.HasDecoration(base_type->id(), SpvDecorationBlock)) {
  959. return _.diag(SPV_ERROR_INVALID_ID, inst)
  960. << _.VkErrorID(6925)
  961. << "In the Vulkan environment, cannot store to Uniform Blocks";
  962. }
  963. }
  964. }
  965. }
  966. const auto object_index = 1;
  967. const auto object_id = inst->GetOperandAs<uint32_t>(object_index);
  968. const auto object = _.FindDef(object_id);
  969. if (!object || !object->type_id()) {
  970. return _.diag(SPV_ERROR_INVALID_ID, inst)
  971. << "OpStore Object <id> " << _.getIdName(object_id)
  972. << " is not an object.";
  973. }
  974. const auto object_type = _.FindDef(object->type_id());
  975. if (!object_type || SpvOpTypeVoid == object_type->opcode()) {
  976. return _.diag(SPV_ERROR_INVALID_ID, inst)
  977. << "OpStore Object <id> " << _.getIdName(object_id)
  978. << "s type is void.";
  979. }
  980. if (type->id() != object_type->id()) {
  981. if (!_.options()->relax_struct_store || type->opcode() != SpvOpTypeStruct ||
  982. object_type->opcode() != SpvOpTypeStruct) {
  983. return _.diag(SPV_ERROR_INVALID_ID, inst)
  984. << "OpStore Pointer <id> " << _.getIdName(pointer_id)
  985. << "s type does not match Object <id> "
  986. << _.getIdName(object->id()) << "s type.";
  987. }
  988. // TODO: Check for layout compatible matricies and arrays as well.
  989. if (!AreLayoutCompatibleStructs(_, type, object_type)) {
  990. return _.diag(SPV_ERROR_INVALID_ID, inst)
  991. << "OpStore Pointer <id> " << _.getIdName(pointer_id)
  992. << "s layout does not match Object <id> "
  993. << _.getIdName(object->id()) << "s layout.";
  994. }
  995. }
  996. if (auto error = CheckMemoryAccess(_, inst, 2)) return error;
  997. if (_.HasCapability(SpvCapabilityShader) &&
  998. _.ContainsLimitedUseIntOrFloatType(inst->type_id()) &&
  999. object_type->opcode() != SpvOpTypePointer) {
  1000. if (object_type->opcode() != SpvOpTypeInt &&
  1001. object_type->opcode() != SpvOpTypeFloat &&
  1002. object_type->opcode() != SpvOpTypeVector &&
  1003. object_type->opcode() != SpvOpTypeMatrix) {
  1004. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1005. << "8- or 16-bit stores must be a scalar, vector or matrix type";
  1006. }
  1007. }
  1008. return SPV_SUCCESS;
  1009. }
  1010. spv_result_t ValidateCopyMemoryMemoryAccess(ValidationState_t& _,
  1011. const Instruction* inst) {
  1012. assert(inst->opcode() == SpvOpCopyMemory ||
  1013. inst->opcode() == SpvOpCopyMemorySized);
  1014. const uint32_t first_access_index = inst->opcode() == SpvOpCopyMemory ? 2 : 3;
  1015. if (inst->operands().size() > first_access_index) {
  1016. if (auto error = CheckMemoryAccess(_, inst, first_access_index))
  1017. return error;
  1018. const auto first_access = inst->GetOperandAs<uint32_t>(first_access_index);
  1019. const uint32_t second_access_index =
  1020. first_access_index + MemoryAccessNumWords(first_access);
  1021. if (inst->operands().size() > second_access_index) {
  1022. if (_.features().copy_memory_permits_two_memory_accesses) {
  1023. if (auto error = CheckMemoryAccess(_, inst, second_access_index))
  1024. return error;
  1025. // In the two-access form in SPIR-V 1.4 and later:
  1026. // - the first is the target (write) access and it can't have
  1027. // make-visible.
  1028. // - the second is the source (read) access and it can't have
  1029. // make-available.
  1030. if (first_access & SpvMemoryAccessMakePointerVisibleKHRMask) {
  1031. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1032. << "Target memory access must not include "
  1033. "MakePointerVisibleKHR";
  1034. }
  1035. const auto second_access =
  1036. inst->GetOperandAs<uint32_t>(second_access_index);
  1037. if (second_access & SpvMemoryAccessMakePointerAvailableKHRMask) {
  1038. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1039. << "Source memory access must not include "
  1040. "MakePointerAvailableKHR";
  1041. }
  1042. } else {
  1043. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1044. << spvOpcodeString(static_cast<SpvOp>(inst->opcode()))
  1045. << " with two memory access operands requires SPIR-V 1.4 or "
  1046. "later";
  1047. }
  1048. }
  1049. }
  1050. return SPV_SUCCESS;
  1051. }
  1052. spv_result_t ValidateCopyMemory(ValidationState_t& _, const Instruction* inst) {
  1053. const auto target_index = 0;
  1054. const auto target_id = inst->GetOperandAs<uint32_t>(target_index);
  1055. const auto target = _.FindDef(target_id);
  1056. if (!target) {
  1057. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1058. << "Target operand <id> " << _.getIdName(target_id)
  1059. << " is not defined.";
  1060. }
  1061. const auto source_index = 1;
  1062. const auto source_id = inst->GetOperandAs<uint32_t>(source_index);
  1063. const auto source = _.FindDef(source_id);
  1064. if (!source) {
  1065. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1066. << "Source operand <id> " << _.getIdName(source_id)
  1067. << " is not defined.";
  1068. }
  1069. const auto target_pointer_type = _.FindDef(target->type_id());
  1070. if (!target_pointer_type ||
  1071. target_pointer_type->opcode() != SpvOpTypePointer) {
  1072. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1073. << "Target operand <id> " << _.getIdName(target_id)
  1074. << " is not a pointer.";
  1075. }
  1076. const auto source_pointer_type = _.FindDef(source->type_id());
  1077. if (!source_pointer_type ||
  1078. source_pointer_type->opcode() != SpvOpTypePointer) {
  1079. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1080. << "Source operand <id> " << _.getIdName(source_id)
  1081. << " is not a pointer.";
  1082. }
  1083. if (inst->opcode() == SpvOpCopyMemory) {
  1084. const auto target_type =
  1085. _.FindDef(target_pointer_type->GetOperandAs<uint32_t>(2));
  1086. if (!target_type || target_type->opcode() == SpvOpTypeVoid) {
  1087. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1088. << "Target operand <id> " << _.getIdName(target_id)
  1089. << " cannot be a void pointer.";
  1090. }
  1091. const auto source_type =
  1092. _.FindDef(source_pointer_type->GetOperandAs<uint32_t>(2));
  1093. if (!source_type || source_type->opcode() == SpvOpTypeVoid) {
  1094. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1095. << "Source operand <id> " << _.getIdName(source_id)
  1096. << " cannot be a void pointer.";
  1097. }
  1098. if (target_type->id() != source_type->id()) {
  1099. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1100. << "Target <id> " << _.getIdName(source_id)
  1101. << "s type does not match Source <id> "
  1102. << _.getIdName(source_type->id()) << "s type.";
  1103. }
  1104. } else {
  1105. const auto size_id = inst->GetOperandAs<uint32_t>(2);
  1106. const auto size = _.FindDef(size_id);
  1107. if (!size) {
  1108. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1109. << "Size operand <id> " << _.getIdName(size_id)
  1110. << " is not defined.";
  1111. }
  1112. const auto size_type = _.FindDef(size->type_id());
  1113. if (!_.IsIntScalarType(size_type->id())) {
  1114. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1115. << "Size operand <id> " << _.getIdName(size_id)
  1116. << " must be a scalar integer type.";
  1117. }
  1118. bool is_zero = true;
  1119. switch (size->opcode()) {
  1120. case SpvOpConstantNull:
  1121. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1122. << "Size operand <id> " << _.getIdName(size_id)
  1123. << " cannot be a constant zero.";
  1124. case SpvOpConstant:
  1125. if (size_type->word(3) == 1 &&
  1126. size->word(size->words().size() - 1) & 0x80000000) {
  1127. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1128. << "Size operand <id> " << _.getIdName(size_id)
  1129. << " cannot have the sign bit set to 1.";
  1130. }
  1131. for (size_t i = 3; is_zero && i < size->words().size(); ++i) {
  1132. is_zero &= (size->word(i) == 0);
  1133. }
  1134. if (is_zero) {
  1135. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1136. << "Size operand <id> " << _.getIdName(size_id)
  1137. << " cannot be a constant zero.";
  1138. }
  1139. break;
  1140. default:
  1141. // Cannot infer any other opcodes.
  1142. break;
  1143. }
  1144. }
  1145. if (auto error = ValidateCopyMemoryMemoryAccess(_, inst)) return error;
  1146. // Get past the pointers to avoid checking a pointer copy.
  1147. auto sub_type = _.FindDef(target_pointer_type->GetOperandAs<uint32_t>(2));
  1148. while (sub_type->opcode() == SpvOpTypePointer) {
  1149. sub_type = _.FindDef(sub_type->GetOperandAs<uint32_t>(2));
  1150. }
  1151. if (_.HasCapability(SpvCapabilityShader) &&
  1152. _.ContainsLimitedUseIntOrFloatType(sub_type->id())) {
  1153. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1154. << "Cannot copy memory of objects containing 8- or 16-bit types";
  1155. }
  1156. return SPV_SUCCESS;
  1157. }
  1158. spv_result_t ValidateAccessChain(ValidationState_t& _,
  1159. const Instruction* inst) {
  1160. std::string instr_name =
  1161. "Op" + std::string(spvOpcodeString(static_cast<SpvOp>(inst->opcode())));
  1162. // The result type must be OpTypePointer.
  1163. auto result_type = _.FindDef(inst->type_id());
  1164. if (SpvOpTypePointer != result_type->opcode()) {
  1165. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1166. << "The Result Type of " << instr_name << " <id> "
  1167. << _.getIdName(inst->id()) << " must be OpTypePointer. Found Op"
  1168. << spvOpcodeString(static_cast<SpvOp>(result_type->opcode())) << ".";
  1169. }
  1170. // Result type is a pointer. Find out what it's pointing to.
  1171. // This will be used to make sure the indexing results in the same type.
  1172. // OpTypePointer word 3 is the type being pointed to.
  1173. const auto result_type_pointee = _.FindDef(result_type->word(3));
  1174. // Base must be a pointer, pointing to the base of a composite object.
  1175. const auto base_index = 2;
  1176. const auto base_id = inst->GetOperandAs<uint32_t>(base_index);
  1177. const auto base = _.FindDef(base_id);
  1178. const auto base_type = _.FindDef(base->type_id());
  1179. if (!base_type || SpvOpTypePointer != base_type->opcode()) {
  1180. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1181. << "The Base <id> " << _.getIdName(base_id) << " in " << instr_name
  1182. << " instruction must be a pointer.";
  1183. }
  1184. // The result pointer storage class and base pointer storage class must match.
  1185. // Word 2 of OpTypePointer is the Storage Class.
  1186. auto result_type_storage_class = result_type->word(2);
  1187. auto base_type_storage_class = base_type->word(2);
  1188. if (result_type_storage_class != base_type_storage_class) {
  1189. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1190. << "The result pointer storage class and base "
  1191. "pointer storage class in "
  1192. << instr_name << " do not match.";
  1193. }
  1194. // The type pointed to by OpTypePointer (word 3) must be a composite type.
  1195. auto type_pointee = _.FindDef(base_type->word(3));
  1196. // Check Universal Limit (SPIR-V Spec. Section 2.17).
  1197. // The number of indexes passed to OpAccessChain may not exceed 255
  1198. // The instruction includes 4 words + N words (for N indexes)
  1199. size_t num_indexes = inst->words().size() - 4;
  1200. if (inst->opcode() == SpvOpPtrAccessChain ||
  1201. inst->opcode() == SpvOpInBoundsPtrAccessChain) {
  1202. // In pointer access chains, the element operand is required, but not
  1203. // counted as an index.
  1204. --num_indexes;
  1205. }
  1206. const size_t num_indexes_limit =
  1207. _.options()->universal_limits_.max_access_chain_indexes;
  1208. if (num_indexes > num_indexes_limit) {
  1209. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1210. << "The number of indexes in " << instr_name << " may not exceed "
  1211. << num_indexes_limit << ". Found " << num_indexes << " indexes.";
  1212. }
  1213. // Indexes walk the type hierarchy to the desired depth, potentially down to
  1214. // scalar granularity. The first index in Indexes will select the top-level
  1215. // member/element/component/element of the base composite. All composite
  1216. // constituents use zero-based numbering, as described by their OpType...
  1217. // instruction. The second index will apply similarly to that result, and so
  1218. // on. Once any non-composite type is reached, there must be no remaining
  1219. // (unused) indexes.
  1220. auto starting_index = 4;
  1221. if (inst->opcode() == SpvOpPtrAccessChain ||
  1222. inst->opcode() == SpvOpInBoundsPtrAccessChain) {
  1223. ++starting_index;
  1224. }
  1225. for (size_t i = starting_index; i < inst->words().size(); ++i) {
  1226. const uint32_t cur_word = inst->words()[i];
  1227. // Earlier ID checks ensure that cur_word definition exists.
  1228. auto cur_word_instr = _.FindDef(cur_word);
  1229. // The index must be a scalar integer type (See OpAccessChain in the Spec.)
  1230. auto index_type = _.FindDef(cur_word_instr->type_id());
  1231. if (!index_type || SpvOpTypeInt != index_type->opcode()) {
  1232. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1233. << "Indexes passed to " << instr_name
  1234. << " must be of type integer.";
  1235. }
  1236. switch (type_pointee->opcode()) {
  1237. case SpvOpTypeMatrix:
  1238. case SpvOpTypeVector:
  1239. case SpvOpTypeCooperativeMatrixNV:
  1240. case SpvOpTypeArray:
  1241. case SpvOpTypeRuntimeArray: {
  1242. // In OpTypeMatrix, OpTypeVector, SpvOpTypeCooperativeMatrixNV,
  1243. // OpTypeArray, and OpTypeRuntimeArray, word 2 is the Element Type.
  1244. type_pointee = _.FindDef(type_pointee->word(2));
  1245. break;
  1246. }
  1247. case SpvOpTypeStruct: {
  1248. // In case of structures, there is an additional constraint on the
  1249. // index: the index must be an OpConstant.
  1250. if (SpvOpConstant != cur_word_instr->opcode()) {
  1251. return _.diag(SPV_ERROR_INVALID_ID, cur_word_instr)
  1252. << "The <id> passed to " << instr_name
  1253. << " to index into a "
  1254. "structure must be an OpConstant.";
  1255. }
  1256. // Get the index value from the OpConstant (word 3 of OpConstant).
  1257. // OpConstant could be a signed integer. But it's okay to treat it as
  1258. // unsigned because a negative constant int would never be seen as
  1259. // correct as a struct offset, since structs can't have more than 2
  1260. // billion members.
  1261. const uint32_t cur_index = cur_word_instr->word(3);
  1262. // The index points to the struct member we want, therefore, the index
  1263. // should be less than the number of struct members.
  1264. const uint32_t num_struct_members =
  1265. static_cast<uint32_t>(type_pointee->words().size() - 2);
  1266. if (cur_index >= num_struct_members) {
  1267. return _.diag(SPV_ERROR_INVALID_ID, cur_word_instr)
  1268. << "Index is out of bounds: " << instr_name
  1269. << " can not find index " << cur_index
  1270. << " into the structure <id> "
  1271. << _.getIdName(type_pointee->id()) << ". This structure has "
  1272. << num_struct_members << " members. Largest valid index is "
  1273. << num_struct_members - 1 << ".";
  1274. }
  1275. // Struct members IDs start at word 2 of OpTypeStruct.
  1276. auto structMemberId = type_pointee->word(cur_index + 2);
  1277. type_pointee = _.FindDef(structMemberId);
  1278. break;
  1279. }
  1280. default: {
  1281. // Give an error. reached non-composite type while indexes still remain.
  1282. return _.diag(SPV_ERROR_INVALID_ID, cur_word_instr)
  1283. << instr_name
  1284. << " reached non-composite type while indexes "
  1285. "still remain to be traversed.";
  1286. }
  1287. }
  1288. }
  1289. // At this point, we have fully walked down from the base using the indeces.
  1290. // The type being pointed to should be the same as the result type.
  1291. if (type_pointee->id() != result_type_pointee->id()) {
  1292. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1293. << instr_name << " result type (Op"
  1294. << spvOpcodeString(static_cast<SpvOp>(result_type_pointee->opcode()))
  1295. << ") does not match the type that results from indexing into the "
  1296. "base "
  1297. "<id> (Op"
  1298. << spvOpcodeString(static_cast<SpvOp>(type_pointee->opcode()))
  1299. << ").";
  1300. }
  1301. return SPV_SUCCESS;
  1302. }
  1303. spv_result_t ValidatePtrAccessChain(ValidationState_t& _,
  1304. const Instruction* inst) {
  1305. if (_.addressing_model() == SpvAddressingModelLogical) {
  1306. if (!_.features().variable_pointers) {
  1307. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1308. << "Generating variable pointers requires capability "
  1309. << "VariablePointers or VariablePointersStorageBuffer";
  1310. }
  1311. }
  1312. // Need to call first, will make sure Base is a valid ID
  1313. if (auto error = ValidateAccessChain(_, inst)) return error;
  1314. const auto base_id = inst->GetOperandAs<uint32_t>(2);
  1315. const auto base = _.FindDef(base_id);
  1316. const auto base_type = _.FindDef(base->type_id());
  1317. const auto base_type_storage_class = base_type->word(2);
  1318. if (_.HasCapability(SpvCapabilityShader) &&
  1319. (base_type_storage_class == SpvStorageClassUniform ||
  1320. base_type_storage_class == SpvStorageClassStorageBuffer ||
  1321. base_type_storage_class == SpvStorageClassPhysicalStorageBuffer ||
  1322. base_type_storage_class == SpvStorageClassPushConstant ||
  1323. (_.HasCapability(SpvCapabilityWorkgroupMemoryExplicitLayoutKHR) &&
  1324. base_type_storage_class == SpvStorageClassWorkgroup)) &&
  1325. !_.HasDecoration(base_type->id(), SpvDecorationArrayStride)) {
  1326. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1327. << "OpPtrAccessChain must have a Base whose type is decorated "
  1328. "with ArrayStride";
  1329. }
  1330. if (spvIsVulkanEnv(_.context()->target_env)) {
  1331. if (base_type_storage_class == SpvStorageClassWorkgroup) {
  1332. if (!_.HasCapability(SpvCapabilityVariablePointers)) {
  1333. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1334. << _.VkErrorID(7651)
  1335. << "OpPtrAccessChain Base operand pointing to Workgroup "
  1336. "storage class must use VariablePointers capability";
  1337. }
  1338. } else if (base_type_storage_class == SpvStorageClassStorageBuffer) {
  1339. if (!_.features().variable_pointers) {
  1340. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1341. << _.VkErrorID(7652)
  1342. << "OpPtrAccessChain Base operand pointing to StorageBuffer "
  1343. "storage class must use VariablePointers or "
  1344. "VariablePointersStorageBuffer capability";
  1345. }
  1346. } else if (base_type_storage_class !=
  1347. SpvStorageClassPhysicalStorageBuffer) {
  1348. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1349. << _.VkErrorID(7650)
  1350. << "OpPtrAccessChain Base operand must point to Workgroup, "
  1351. "StorageBuffer, or PhysicalStorageBuffer storage class";
  1352. }
  1353. }
  1354. return SPV_SUCCESS;
  1355. }
  1356. spv_result_t ValidateArrayLength(ValidationState_t& state,
  1357. const Instruction* inst) {
  1358. std::string instr_name =
  1359. "Op" + std::string(spvOpcodeString(static_cast<SpvOp>(inst->opcode())));
  1360. // Result type must be a 32-bit unsigned int.
  1361. auto result_type = state.FindDef(inst->type_id());
  1362. if (result_type->opcode() != SpvOpTypeInt ||
  1363. result_type->GetOperandAs<uint32_t>(1) != 32 ||
  1364. result_type->GetOperandAs<uint32_t>(2) != 0) {
  1365. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1366. << "The Result Type of " << instr_name << " <id> "
  1367. << state.getIdName(inst->id())
  1368. << " must be OpTypeInt with width 32 and signedness 0.";
  1369. }
  1370. // The structure that is passed in must be an pointer to a structure, whose
  1371. // last element is a runtime array.
  1372. auto pointer = state.FindDef(inst->GetOperandAs<uint32_t>(2));
  1373. auto pointer_type = state.FindDef(pointer->type_id());
  1374. if (pointer_type->opcode() != SpvOpTypePointer) {
  1375. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1376. << "The Structure's type in " << instr_name << " <id> "
  1377. << state.getIdName(inst->id())
  1378. << " must be a pointer to an OpTypeStruct.";
  1379. }
  1380. auto structure_type = state.FindDef(pointer_type->GetOperandAs<uint32_t>(2));
  1381. if (structure_type->opcode() != SpvOpTypeStruct) {
  1382. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1383. << "The Structure's type in " << instr_name << " <id> "
  1384. << state.getIdName(inst->id())
  1385. << " must be a pointer to an OpTypeStruct.";
  1386. }
  1387. auto num_of_members = structure_type->operands().size() - 1;
  1388. auto last_member =
  1389. state.FindDef(structure_type->GetOperandAs<uint32_t>(num_of_members));
  1390. if (last_member->opcode() != SpvOpTypeRuntimeArray) {
  1391. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1392. << "The Structure's last member in " << instr_name << " <id> "
  1393. << state.getIdName(inst->id()) << " must be an OpTypeRuntimeArray.";
  1394. }
  1395. // The array member must the index of the last element (the run time
  1396. // array).
  1397. if (inst->GetOperandAs<uint32_t>(3) != num_of_members - 1) {
  1398. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1399. << "The array member in " << instr_name << " <id> "
  1400. << state.getIdName(inst->id())
  1401. << " must be an the last member of the struct.";
  1402. }
  1403. return SPV_SUCCESS;
  1404. }
  1405. spv_result_t ValidateCooperativeMatrixLengthNV(ValidationState_t& state,
  1406. const Instruction* inst) {
  1407. std::string instr_name =
  1408. "Op" + std::string(spvOpcodeString(static_cast<SpvOp>(inst->opcode())));
  1409. // Result type must be a 32-bit unsigned int.
  1410. auto result_type = state.FindDef(inst->type_id());
  1411. if (result_type->opcode() != SpvOpTypeInt ||
  1412. result_type->GetOperandAs<uint32_t>(1) != 32 ||
  1413. result_type->GetOperandAs<uint32_t>(2) != 0) {
  1414. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1415. << "The Result Type of " << instr_name << " <id> "
  1416. << state.getIdName(inst->id())
  1417. << " must be OpTypeInt with width 32 and signedness 0.";
  1418. }
  1419. auto type_id = inst->GetOperandAs<uint32_t>(2);
  1420. auto type = state.FindDef(type_id);
  1421. if (type->opcode() != SpvOpTypeCooperativeMatrixNV) {
  1422. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1423. << "The type in " << instr_name << " <id> "
  1424. << state.getIdName(type_id) << " must be OpTypeCooperativeMatrixNV.";
  1425. }
  1426. return SPV_SUCCESS;
  1427. }
  1428. spv_result_t ValidateCooperativeMatrixLoadStoreNV(ValidationState_t& _,
  1429. const Instruction* inst) {
  1430. uint32_t type_id;
  1431. const char* opname;
  1432. if (inst->opcode() == SpvOpCooperativeMatrixLoadNV) {
  1433. type_id = inst->type_id();
  1434. opname = "SpvOpCooperativeMatrixLoadNV";
  1435. } else {
  1436. // get Object operand's type
  1437. type_id = _.FindDef(inst->GetOperandAs<uint32_t>(1))->type_id();
  1438. opname = "SpvOpCooperativeMatrixStoreNV";
  1439. }
  1440. auto matrix_type = _.FindDef(type_id);
  1441. if (matrix_type->opcode() != SpvOpTypeCooperativeMatrixNV) {
  1442. if (inst->opcode() == SpvOpCooperativeMatrixLoadNV) {
  1443. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1444. << "SpvOpCooperativeMatrixLoadNV Result Type <id> "
  1445. << _.getIdName(type_id) << " is not a cooperative matrix type.";
  1446. } else {
  1447. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1448. << "SpvOpCooperativeMatrixStoreNV Object type <id> "
  1449. << _.getIdName(type_id) << " is not a cooperative matrix type.";
  1450. }
  1451. }
  1452. const auto pointer_index =
  1453. (inst->opcode() == SpvOpCooperativeMatrixLoadNV) ? 2u : 0u;
  1454. const auto pointer_id = inst->GetOperandAs<uint32_t>(pointer_index);
  1455. const auto pointer = _.FindDef(pointer_id);
  1456. if (!pointer ||
  1457. ((_.addressing_model() == SpvAddressingModelLogical) &&
  1458. ((!_.features().variable_pointers &&
  1459. !spvOpcodeReturnsLogicalPointer(pointer->opcode())) ||
  1460. (_.features().variable_pointers &&
  1461. !spvOpcodeReturnsLogicalVariablePointer(pointer->opcode()))))) {
  1462. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1463. << opname << " Pointer <id> " << _.getIdName(pointer_id)
  1464. << " is not a logical pointer.";
  1465. }
  1466. const auto pointer_type_id = pointer->type_id();
  1467. const auto pointer_type = _.FindDef(pointer_type_id);
  1468. if (!pointer_type || pointer_type->opcode() != SpvOpTypePointer) {
  1469. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1470. << opname << " type for pointer <id> " << _.getIdName(pointer_id)
  1471. << " is not a pointer type.";
  1472. }
  1473. const auto storage_class_index = 1u;
  1474. const auto storage_class =
  1475. pointer_type->GetOperandAs<uint32_t>(storage_class_index);
  1476. if (storage_class != SpvStorageClassWorkgroup &&
  1477. storage_class != SpvStorageClassStorageBuffer &&
  1478. storage_class != SpvStorageClassPhysicalStorageBuffer) {
  1479. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1480. << opname << " storage class for pointer type <id> "
  1481. << _.getIdName(pointer_type_id)
  1482. << " is not Workgroup or StorageBuffer.";
  1483. }
  1484. const auto pointee_id = pointer_type->GetOperandAs<uint32_t>(2);
  1485. const auto pointee_type = _.FindDef(pointee_id);
  1486. if (!pointee_type || !(_.IsIntScalarOrVectorType(pointee_id) ||
  1487. _.IsFloatScalarOrVectorType(pointee_id))) {
  1488. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1489. << opname << " Pointer <id> " << _.getIdName(pointer->id())
  1490. << "s Type must be a scalar or vector type.";
  1491. }
  1492. const auto stride_index =
  1493. (inst->opcode() == SpvOpCooperativeMatrixLoadNV) ? 3u : 2u;
  1494. const auto stride_id = inst->GetOperandAs<uint32_t>(stride_index);
  1495. const auto stride = _.FindDef(stride_id);
  1496. if (!stride || !_.IsIntScalarType(stride->type_id())) {
  1497. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1498. << "Stride operand <id> " << _.getIdName(stride_id)
  1499. << " must be a scalar integer type.";
  1500. }
  1501. const auto colmajor_index =
  1502. (inst->opcode() == SpvOpCooperativeMatrixLoadNV) ? 4u : 3u;
  1503. const auto colmajor_id = inst->GetOperandAs<uint32_t>(colmajor_index);
  1504. const auto colmajor = _.FindDef(colmajor_id);
  1505. if (!colmajor || !_.IsBoolScalarType(colmajor->type_id()) ||
  1506. !(spvOpcodeIsConstant(colmajor->opcode()) ||
  1507. spvOpcodeIsSpecConstant(colmajor->opcode()))) {
  1508. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1509. << "Column Major operand <id> " << _.getIdName(colmajor_id)
  1510. << " must be a boolean constant instruction.";
  1511. }
  1512. const auto memory_access_index =
  1513. (inst->opcode() == SpvOpCooperativeMatrixLoadNV) ? 5u : 4u;
  1514. if (inst->operands().size() > memory_access_index) {
  1515. if (auto error = CheckMemoryAccess(_, inst, memory_access_index))
  1516. return error;
  1517. }
  1518. return SPV_SUCCESS;
  1519. }
  1520. spv_result_t ValidatePtrComparison(ValidationState_t& _,
  1521. const Instruction* inst) {
  1522. if (_.addressing_model() == SpvAddressingModelLogical &&
  1523. !_.features().variable_pointers) {
  1524. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1525. << "Instruction cannot for logical addressing model be used without "
  1526. "a variable pointers capability";
  1527. }
  1528. const auto result_type = _.FindDef(inst->type_id());
  1529. if (inst->opcode() == SpvOpPtrDiff) {
  1530. if (!result_type || result_type->opcode() != SpvOpTypeInt) {
  1531. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1532. << "Result Type must be an integer scalar";
  1533. }
  1534. } else {
  1535. if (!result_type || result_type->opcode() != SpvOpTypeBool) {
  1536. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1537. << "Result Type must be OpTypeBool";
  1538. }
  1539. }
  1540. const auto op1 = _.FindDef(inst->GetOperandAs<uint32_t>(2u));
  1541. const auto op2 = _.FindDef(inst->GetOperandAs<uint32_t>(3u));
  1542. if (!op1 || !op2 || op1->type_id() != op2->type_id()) {
  1543. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1544. << "The types of Operand 1 and Operand 2 must match";
  1545. }
  1546. const auto op1_type = _.FindDef(op1->type_id());
  1547. if (!op1_type || op1_type->opcode() != SpvOpTypePointer) {
  1548. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1549. << "Operand type must be a pointer";
  1550. }
  1551. SpvStorageClass sc = op1_type->GetOperandAs<SpvStorageClass>(1u);
  1552. if (_.addressing_model() == SpvAddressingModelLogical) {
  1553. if (sc != SpvStorageClassWorkgroup && sc != SpvStorageClassStorageBuffer) {
  1554. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1555. << "Invalid pointer storage class";
  1556. }
  1557. if (sc == SpvStorageClassWorkgroup &&
  1558. !_.HasCapability(SpvCapabilityVariablePointers)) {
  1559. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1560. << "Workgroup storage class pointer requires VariablePointers "
  1561. "capability to be specified";
  1562. }
  1563. } else if (sc == SpvStorageClassPhysicalStorageBuffer) {
  1564. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1565. << "Cannot use a pointer in the PhysicalStorageBuffer storage class";
  1566. }
  1567. return SPV_SUCCESS;
  1568. }
  1569. } // namespace
  1570. spv_result_t MemoryPass(ValidationState_t& _, const Instruction* inst) {
  1571. switch (inst->opcode()) {
  1572. case SpvOpVariable:
  1573. if (auto error = ValidateVariable(_, inst)) return error;
  1574. break;
  1575. case SpvOpLoad:
  1576. if (auto error = ValidateLoad(_, inst)) return error;
  1577. break;
  1578. case SpvOpStore:
  1579. if (auto error = ValidateStore(_, inst)) return error;
  1580. break;
  1581. case SpvOpCopyMemory:
  1582. case SpvOpCopyMemorySized:
  1583. if (auto error = ValidateCopyMemory(_, inst)) return error;
  1584. break;
  1585. case SpvOpPtrAccessChain:
  1586. if (auto error = ValidatePtrAccessChain(_, inst)) return error;
  1587. break;
  1588. case SpvOpAccessChain:
  1589. case SpvOpInBoundsAccessChain:
  1590. case SpvOpInBoundsPtrAccessChain:
  1591. if (auto error = ValidateAccessChain(_, inst)) return error;
  1592. break;
  1593. case SpvOpArrayLength:
  1594. if (auto error = ValidateArrayLength(_, inst)) return error;
  1595. break;
  1596. case SpvOpCooperativeMatrixLoadNV:
  1597. case SpvOpCooperativeMatrixStoreNV:
  1598. if (auto error = ValidateCooperativeMatrixLoadStoreNV(_, inst))
  1599. return error;
  1600. break;
  1601. case SpvOpCooperativeMatrixLengthNV:
  1602. if (auto error = ValidateCooperativeMatrixLengthNV(_, inst)) return error;
  1603. break;
  1604. case SpvOpPtrEqual:
  1605. case SpvOpPtrNotEqual:
  1606. case SpvOpPtrDiff:
  1607. if (auto error = ValidatePtrComparison(_, inst)) return error;
  1608. break;
  1609. case SpvOpImageTexelPointer:
  1610. case SpvOpGenericPtrMemSemantics:
  1611. default:
  1612. break;
  1613. }
  1614. return SPV_SUCCESS;
  1615. }
  1616. } // namespace val
  1617. } // namespace spvtools