validate_memory.cpp 67 KB

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