validate_memory.cpp 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658
  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. << _.VkErrorID(6925)
  914. << "In the Vulkan environment, cannot store to Uniform Blocks";
  915. }
  916. }
  917. }
  918. }
  919. const auto object_index = 1;
  920. const auto object_id = inst->GetOperandAs<uint32_t>(object_index);
  921. const auto object = _.FindDef(object_id);
  922. if (!object || !object->type_id()) {
  923. return _.diag(SPV_ERROR_INVALID_ID, inst)
  924. << "OpStore Object <id> '" << _.getIdName(object_id)
  925. << "' is not an object.";
  926. }
  927. const auto object_type = _.FindDef(object->type_id());
  928. if (!object_type || SpvOpTypeVoid == object_type->opcode()) {
  929. return _.diag(SPV_ERROR_INVALID_ID, inst)
  930. << "OpStore Object <id> '" << _.getIdName(object_id)
  931. << "'s type is void.";
  932. }
  933. if (type->id() != object_type->id()) {
  934. if (!_.options()->relax_struct_store || type->opcode() != SpvOpTypeStruct ||
  935. object_type->opcode() != SpvOpTypeStruct) {
  936. return _.diag(SPV_ERROR_INVALID_ID, inst)
  937. << "OpStore Pointer <id> '" << _.getIdName(pointer_id)
  938. << "'s type does not match Object <id> '"
  939. << _.getIdName(object->id()) << "'s type.";
  940. }
  941. // TODO: Check for layout compatible matricies and arrays as well.
  942. if (!AreLayoutCompatibleStructs(_, type, object_type)) {
  943. return _.diag(SPV_ERROR_INVALID_ID, inst)
  944. << "OpStore Pointer <id> '" << _.getIdName(pointer_id)
  945. << "'s layout does not match Object <id> '"
  946. << _.getIdName(object->id()) << "'s layout.";
  947. }
  948. }
  949. if (auto error = CheckMemoryAccess(_, inst, 2)) return error;
  950. if (_.HasCapability(SpvCapabilityShader) &&
  951. _.ContainsLimitedUseIntOrFloatType(inst->type_id()) &&
  952. object_type->opcode() != SpvOpTypePointer) {
  953. if (object_type->opcode() != SpvOpTypeInt &&
  954. object_type->opcode() != SpvOpTypeFloat &&
  955. object_type->opcode() != SpvOpTypeVector &&
  956. object_type->opcode() != SpvOpTypeMatrix) {
  957. return _.diag(SPV_ERROR_INVALID_ID, inst)
  958. << "8- or 16-bit stores must be a scalar, vector or matrix type";
  959. }
  960. }
  961. return SPV_SUCCESS;
  962. }
  963. spv_result_t ValidateCopyMemoryMemoryAccess(ValidationState_t& _,
  964. const Instruction* inst) {
  965. assert(inst->opcode() == SpvOpCopyMemory ||
  966. inst->opcode() == SpvOpCopyMemorySized);
  967. const uint32_t first_access_index = inst->opcode() == SpvOpCopyMemory ? 2 : 3;
  968. if (inst->operands().size() > first_access_index) {
  969. if (auto error = CheckMemoryAccess(_, inst, first_access_index))
  970. return error;
  971. const auto first_access = inst->GetOperandAs<uint32_t>(first_access_index);
  972. const uint32_t second_access_index =
  973. first_access_index + MemoryAccessNumWords(first_access);
  974. if (inst->operands().size() > second_access_index) {
  975. if (_.features().copy_memory_permits_two_memory_accesses) {
  976. if (auto error = CheckMemoryAccess(_, inst, second_access_index))
  977. return error;
  978. // In the two-access form in SPIR-V 1.4 and later:
  979. // - the first is the target (write) access and it can't have
  980. // make-visible.
  981. // - the second is the source (read) access and it can't have
  982. // make-available.
  983. if (first_access & SpvMemoryAccessMakePointerVisibleKHRMask) {
  984. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  985. << "Target memory access must not include "
  986. "MakePointerVisibleKHR";
  987. }
  988. const auto second_access =
  989. inst->GetOperandAs<uint32_t>(second_access_index);
  990. if (second_access & SpvMemoryAccessMakePointerAvailableKHRMask) {
  991. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  992. << "Source memory access must not include "
  993. "MakePointerAvailableKHR";
  994. }
  995. } else {
  996. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  997. << spvOpcodeString(static_cast<SpvOp>(inst->opcode()))
  998. << " with two memory access operands requires SPIR-V 1.4 or "
  999. "later";
  1000. }
  1001. }
  1002. }
  1003. return SPV_SUCCESS;
  1004. }
  1005. spv_result_t ValidateCopyMemory(ValidationState_t& _, const Instruction* inst) {
  1006. const auto target_index = 0;
  1007. const auto target_id = inst->GetOperandAs<uint32_t>(target_index);
  1008. const auto target = _.FindDef(target_id);
  1009. if (!target) {
  1010. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1011. << "Target operand <id> '" << _.getIdName(target_id)
  1012. << "' is not defined.";
  1013. }
  1014. const auto source_index = 1;
  1015. const auto source_id = inst->GetOperandAs<uint32_t>(source_index);
  1016. const auto source = _.FindDef(source_id);
  1017. if (!source) {
  1018. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1019. << "Source operand <id> '" << _.getIdName(source_id)
  1020. << "' is not defined.";
  1021. }
  1022. const auto target_pointer_type = _.FindDef(target->type_id());
  1023. if (!target_pointer_type ||
  1024. target_pointer_type->opcode() != SpvOpTypePointer) {
  1025. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1026. << "Target operand <id> '" << _.getIdName(target_id)
  1027. << "' is not a pointer.";
  1028. }
  1029. const auto source_pointer_type = _.FindDef(source->type_id());
  1030. if (!source_pointer_type ||
  1031. source_pointer_type->opcode() != SpvOpTypePointer) {
  1032. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1033. << "Source operand <id> '" << _.getIdName(source_id)
  1034. << "' is not a pointer.";
  1035. }
  1036. if (inst->opcode() == SpvOpCopyMemory) {
  1037. const auto target_type =
  1038. _.FindDef(target_pointer_type->GetOperandAs<uint32_t>(2));
  1039. if (!target_type || target_type->opcode() == SpvOpTypeVoid) {
  1040. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1041. << "Target operand <id> '" << _.getIdName(target_id)
  1042. << "' cannot be a void pointer.";
  1043. }
  1044. const auto source_type =
  1045. _.FindDef(source_pointer_type->GetOperandAs<uint32_t>(2));
  1046. if (!source_type || source_type->opcode() == SpvOpTypeVoid) {
  1047. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1048. << "Source operand <id> '" << _.getIdName(source_id)
  1049. << "' cannot be a void pointer.";
  1050. }
  1051. if (target_type->id() != source_type->id()) {
  1052. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1053. << "Target <id> '" << _.getIdName(source_id)
  1054. << "'s type does not match Source <id> '"
  1055. << _.getIdName(source_type->id()) << "'s type.";
  1056. }
  1057. } else {
  1058. const auto size_id = inst->GetOperandAs<uint32_t>(2);
  1059. const auto size = _.FindDef(size_id);
  1060. if (!size) {
  1061. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1062. << "Size operand <id> '" << _.getIdName(size_id)
  1063. << "' is not defined.";
  1064. }
  1065. const auto size_type = _.FindDef(size->type_id());
  1066. if (!_.IsIntScalarType(size_type->id())) {
  1067. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1068. << "Size operand <id> '" << _.getIdName(size_id)
  1069. << "' must be a scalar integer type.";
  1070. }
  1071. bool is_zero = true;
  1072. switch (size->opcode()) {
  1073. case SpvOpConstantNull:
  1074. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1075. << "Size operand <id> '" << _.getIdName(size_id)
  1076. << "' cannot be a constant zero.";
  1077. case SpvOpConstant:
  1078. if (size_type->word(3) == 1 &&
  1079. size->word(size->words().size() - 1) & 0x80000000) {
  1080. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1081. << "Size operand <id> '" << _.getIdName(size_id)
  1082. << "' cannot have the sign bit set to 1.";
  1083. }
  1084. for (size_t i = 3; is_zero && i < size->words().size(); ++i) {
  1085. is_zero &= (size->word(i) == 0);
  1086. }
  1087. if (is_zero) {
  1088. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1089. << "Size operand <id> '" << _.getIdName(size_id)
  1090. << "' cannot be a constant zero.";
  1091. }
  1092. break;
  1093. default:
  1094. // Cannot infer any other opcodes.
  1095. break;
  1096. }
  1097. }
  1098. if (auto error = ValidateCopyMemoryMemoryAccess(_, inst)) return error;
  1099. // Get past the pointers to avoid checking a pointer copy.
  1100. auto sub_type = _.FindDef(target_pointer_type->GetOperandAs<uint32_t>(2));
  1101. while (sub_type->opcode() == SpvOpTypePointer) {
  1102. sub_type = _.FindDef(sub_type->GetOperandAs<uint32_t>(2));
  1103. }
  1104. if (_.HasCapability(SpvCapabilityShader) &&
  1105. _.ContainsLimitedUseIntOrFloatType(sub_type->id())) {
  1106. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1107. << "Cannot copy memory of objects containing 8- or 16-bit types";
  1108. }
  1109. return SPV_SUCCESS;
  1110. }
  1111. spv_result_t ValidateAccessChain(ValidationState_t& _,
  1112. const Instruction* inst) {
  1113. std::string instr_name =
  1114. "Op" + std::string(spvOpcodeString(static_cast<SpvOp>(inst->opcode())));
  1115. // The result type must be OpTypePointer.
  1116. auto result_type = _.FindDef(inst->type_id());
  1117. if (SpvOpTypePointer != result_type->opcode()) {
  1118. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1119. << "The Result Type of " << instr_name << " <id> '"
  1120. << _.getIdName(inst->id()) << "' must be OpTypePointer. Found Op"
  1121. << spvOpcodeString(static_cast<SpvOp>(result_type->opcode())) << ".";
  1122. }
  1123. // Result type is a pointer. Find out what it's pointing to.
  1124. // This will be used to make sure the indexing results in the same type.
  1125. // OpTypePointer word 3 is the type being pointed to.
  1126. const auto result_type_pointee = _.FindDef(result_type->word(3));
  1127. // Base must be a pointer, pointing to the base of a composite object.
  1128. const auto base_index = 2;
  1129. const auto base_id = inst->GetOperandAs<uint32_t>(base_index);
  1130. const auto base = _.FindDef(base_id);
  1131. const auto base_type = _.FindDef(base->type_id());
  1132. if (!base_type || SpvOpTypePointer != base_type->opcode()) {
  1133. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1134. << "The Base <id> '" << _.getIdName(base_id) << "' in " << instr_name
  1135. << " instruction must be a pointer.";
  1136. }
  1137. // The result pointer storage class and base pointer storage class must match.
  1138. // Word 2 of OpTypePointer is the Storage Class.
  1139. auto result_type_storage_class = result_type->word(2);
  1140. auto base_type_storage_class = base_type->word(2);
  1141. if (result_type_storage_class != base_type_storage_class) {
  1142. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1143. << "The result pointer storage class and base "
  1144. "pointer storage class in "
  1145. << instr_name << " do not match.";
  1146. }
  1147. // The type pointed to by OpTypePointer (word 3) must be a composite type.
  1148. auto type_pointee = _.FindDef(base_type->word(3));
  1149. // Check Universal Limit (SPIR-V Spec. Section 2.17).
  1150. // The number of indexes passed to OpAccessChain may not exceed 255
  1151. // The instruction includes 4 words + N words (for N indexes)
  1152. size_t num_indexes = inst->words().size() - 4;
  1153. if (inst->opcode() == SpvOpPtrAccessChain ||
  1154. inst->opcode() == SpvOpInBoundsPtrAccessChain) {
  1155. // In pointer access chains, the element operand is required, but not
  1156. // counted as an index.
  1157. --num_indexes;
  1158. }
  1159. const size_t num_indexes_limit =
  1160. _.options()->universal_limits_.max_access_chain_indexes;
  1161. if (num_indexes > num_indexes_limit) {
  1162. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1163. << "The number of indexes in " << instr_name << " may not exceed "
  1164. << num_indexes_limit << ". Found " << num_indexes << " indexes.";
  1165. }
  1166. // Indexes walk the type hierarchy to the desired depth, potentially down to
  1167. // scalar granularity. The first index in Indexes will select the top-level
  1168. // member/element/component/element of the base composite. All composite
  1169. // constituents use zero-based numbering, as described by their OpType...
  1170. // instruction. The second index will apply similarly to that result, and so
  1171. // on. Once any non-composite type is reached, there must be no remaining
  1172. // (unused) indexes.
  1173. auto starting_index = 4;
  1174. if (inst->opcode() == SpvOpPtrAccessChain ||
  1175. inst->opcode() == SpvOpInBoundsPtrAccessChain) {
  1176. ++starting_index;
  1177. }
  1178. for (size_t i = starting_index; i < inst->words().size(); ++i) {
  1179. const uint32_t cur_word = inst->words()[i];
  1180. // Earlier ID checks ensure that cur_word definition exists.
  1181. auto cur_word_instr = _.FindDef(cur_word);
  1182. // The index must be a scalar integer type (See OpAccessChain in the Spec.)
  1183. auto index_type = _.FindDef(cur_word_instr->type_id());
  1184. if (!index_type || SpvOpTypeInt != index_type->opcode()) {
  1185. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1186. << "Indexes passed to " << instr_name
  1187. << " must be of type integer.";
  1188. }
  1189. switch (type_pointee->opcode()) {
  1190. case SpvOpTypeMatrix:
  1191. case SpvOpTypeVector:
  1192. case SpvOpTypeCooperativeMatrixNV:
  1193. case SpvOpTypeArray:
  1194. case SpvOpTypeRuntimeArray: {
  1195. // In OpTypeMatrix, OpTypeVector, SpvOpTypeCooperativeMatrixNV,
  1196. // OpTypeArray, and OpTypeRuntimeArray, word 2 is the Element Type.
  1197. type_pointee = _.FindDef(type_pointee->word(2));
  1198. break;
  1199. }
  1200. case SpvOpTypeStruct: {
  1201. // In case of structures, there is an additional constraint on the
  1202. // index: the index must be an OpConstant.
  1203. if (SpvOpConstant != cur_word_instr->opcode()) {
  1204. return _.diag(SPV_ERROR_INVALID_ID, cur_word_instr)
  1205. << "The <id> passed to " << instr_name
  1206. << " to index into a "
  1207. "structure must be an OpConstant.";
  1208. }
  1209. // Get the index value from the OpConstant (word 3 of OpConstant).
  1210. // OpConstant could be a signed integer. But it's okay to treat it as
  1211. // unsigned because a negative constant int would never be seen as
  1212. // correct as a struct offset, since structs can't have more than 2
  1213. // billion members.
  1214. const uint32_t cur_index = cur_word_instr->word(3);
  1215. // The index points to the struct member we want, therefore, the index
  1216. // should be less than the number of struct members.
  1217. const uint32_t num_struct_members =
  1218. static_cast<uint32_t>(type_pointee->words().size() - 2);
  1219. if (cur_index >= num_struct_members) {
  1220. return _.diag(SPV_ERROR_INVALID_ID, cur_word_instr)
  1221. << "Index is out of bounds: " << instr_name
  1222. << " can not find index " << cur_index
  1223. << " into the structure <id> '"
  1224. << _.getIdName(type_pointee->id()) << "'. This structure has "
  1225. << num_struct_members << " members. Largest valid index is "
  1226. << num_struct_members - 1 << ".";
  1227. }
  1228. // Struct members IDs start at word 2 of OpTypeStruct.
  1229. auto structMemberId = type_pointee->word(cur_index + 2);
  1230. type_pointee = _.FindDef(structMemberId);
  1231. break;
  1232. }
  1233. default: {
  1234. // Give an error. reached non-composite type while indexes still remain.
  1235. return _.diag(SPV_ERROR_INVALID_ID, cur_word_instr)
  1236. << instr_name
  1237. << " reached non-composite type while indexes "
  1238. "still remain to be traversed.";
  1239. }
  1240. }
  1241. }
  1242. // At this point, we have fully walked down from the base using the indeces.
  1243. // The type being pointed to should be the same as the result type.
  1244. if (type_pointee->id() != result_type_pointee->id()) {
  1245. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1246. << instr_name << " result type (Op"
  1247. << spvOpcodeString(static_cast<SpvOp>(result_type_pointee->opcode()))
  1248. << ") does not match the type that results from indexing into the "
  1249. "base "
  1250. "<id> (Op"
  1251. << spvOpcodeString(static_cast<SpvOp>(type_pointee->opcode()))
  1252. << ").";
  1253. }
  1254. return SPV_SUCCESS;
  1255. }
  1256. spv_result_t ValidatePtrAccessChain(ValidationState_t& _,
  1257. const Instruction* inst) {
  1258. if (_.addressing_model() == SpvAddressingModelLogical) {
  1259. if (!_.features().variable_pointers) {
  1260. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1261. << "Generating variable pointers requires capability "
  1262. << "VariablePointers or VariablePointersStorageBuffer";
  1263. }
  1264. }
  1265. return ValidateAccessChain(_, inst);
  1266. }
  1267. spv_result_t ValidateArrayLength(ValidationState_t& state,
  1268. const Instruction* inst) {
  1269. std::string instr_name =
  1270. "Op" + std::string(spvOpcodeString(static_cast<SpvOp>(inst->opcode())));
  1271. // Result type must be a 32-bit unsigned int.
  1272. auto result_type = state.FindDef(inst->type_id());
  1273. if (result_type->opcode() != SpvOpTypeInt ||
  1274. result_type->GetOperandAs<uint32_t>(1) != 32 ||
  1275. result_type->GetOperandAs<uint32_t>(2) != 0) {
  1276. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1277. << "The Result Type of " << instr_name << " <id> '"
  1278. << state.getIdName(inst->id())
  1279. << "' must be OpTypeInt with width 32 and signedness 0.";
  1280. }
  1281. // The structure that is passed in must be an pointer to a structure, whose
  1282. // last element is a runtime array.
  1283. auto pointer = state.FindDef(inst->GetOperandAs<uint32_t>(2));
  1284. auto pointer_type = state.FindDef(pointer->type_id());
  1285. if (pointer_type->opcode() != SpvOpTypePointer) {
  1286. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1287. << "The Struture's type in " << instr_name << " <id> '"
  1288. << state.getIdName(inst->id())
  1289. << "' must be a pointer to an OpTypeStruct.";
  1290. }
  1291. auto structure_type = state.FindDef(pointer_type->GetOperandAs<uint32_t>(2));
  1292. if (structure_type->opcode() != SpvOpTypeStruct) {
  1293. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1294. << "The Struture's type in " << instr_name << " <id> '"
  1295. << state.getIdName(inst->id())
  1296. << "' must be a pointer to an OpTypeStruct.";
  1297. }
  1298. auto num_of_members = structure_type->operands().size() - 1;
  1299. auto last_member =
  1300. state.FindDef(structure_type->GetOperandAs<uint32_t>(num_of_members));
  1301. if (last_member->opcode() != SpvOpTypeRuntimeArray) {
  1302. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1303. << "The Struture's last member in " << instr_name << " <id> '"
  1304. << state.getIdName(inst->id()) << "' must be an OpTypeRuntimeArray.";
  1305. }
  1306. // The array member must the index of the last element (the run time
  1307. // array).
  1308. if (inst->GetOperandAs<uint32_t>(3) != num_of_members - 1) {
  1309. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1310. << "The array member in " << instr_name << " <id> '"
  1311. << state.getIdName(inst->id())
  1312. << "' must be an the last member of the struct.";
  1313. }
  1314. return SPV_SUCCESS;
  1315. }
  1316. spv_result_t ValidateCooperativeMatrixLengthNV(ValidationState_t& state,
  1317. const Instruction* inst) {
  1318. std::string instr_name =
  1319. "Op" + std::string(spvOpcodeString(static_cast<SpvOp>(inst->opcode())));
  1320. // Result type must be a 32-bit unsigned int.
  1321. auto result_type = state.FindDef(inst->type_id());
  1322. if (result_type->opcode() != SpvOpTypeInt ||
  1323. result_type->GetOperandAs<uint32_t>(1) != 32 ||
  1324. result_type->GetOperandAs<uint32_t>(2) != 0) {
  1325. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1326. << "The Result Type of " << instr_name << " <id> '"
  1327. << state.getIdName(inst->id())
  1328. << "' must be OpTypeInt with width 32 and signedness 0.";
  1329. }
  1330. auto type_id = inst->GetOperandAs<uint32_t>(2);
  1331. auto type = state.FindDef(type_id);
  1332. if (type->opcode() != SpvOpTypeCooperativeMatrixNV) {
  1333. return state.diag(SPV_ERROR_INVALID_ID, inst)
  1334. << "The type in " << instr_name << " <id> '"
  1335. << state.getIdName(type_id)
  1336. << "' must be OpTypeCooperativeMatrixNV.";
  1337. }
  1338. return SPV_SUCCESS;
  1339. }
  1340. spv_result_t ValidateCooperativeMatrixLoadStoreNV(ValidationState_t& _,
  1341. const Instruction* inst) {
  1342. uint32_t type_id;
  1343. const char* opname;
  1344. if (inst->opcode() == SpvOpCooperativeMatrixLoadNV) {
  1345. type_id = inst->type_id();
  1346. opname = "SpvOpCooperativeMatrixLoadNV";
  1347. } else {
  1348. // get Object operand's type
  1349. type_id = _.FindDef(inst->GetOperandAs<uint32_t>(1))->type_id();
  1350. opname = "SpvOpCooperativeMatrixStoreNV";
  1351. }
  1352. auto matrix_type = _.FindDef(type_id);
  1353. if (matrix_type->opcode() != SpvOpTypeCooperativeMatrixNV) {
  1354. if (inst->opcode() == SpvOpCooperativeMatrixLoadNV) {
  1355. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1356. << "SpvOpCooperativeMatrixLoadNV Result Type <id> '"
  1357. << _.getIdName(type_id) << "' is not a cooperative matrix type.";
  1358. } else {
  1359. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1360. << "SpvOpCooperativeMatrixStoreNV Object type <id> '"
  1361. << _.getIdName(type_id) << "' is not a cooperative matrix type.";
  1362. }
  1363. }
  1364. const auto pointer_index =
  1365. (inst->opcode() == SpvOpCooperativeMatrixLoadNV) ? 2u : 0u;
  1366. const auto pointer_id = inst->GetOperandAs<uint32_t>(pointer_index);
  1367. const auto pointer = _.FindDef(pointer_id);
  1368. if (!pointer ||
  1369. ((_.addressing_model() == SpvAddressingModelLogical) &&
  1370. ((!_.features().variable_pointers &&
  1371. !spvOpcodeReturnsLogicalPointer(pointer->opcode())) ||
  1372. (_.features().variable_pointers &&
  1373. !spvOpcodeReturnsLogicalVariablePointer(pointer->opcode()))))) {
  1374. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1375. << opname << " Pointer <id> '" << _.getIdName(pointer_id)
  1376. << "' is not a logical pointer.";
  1377. }
  1378. const auto pointer_type_id = pointer->type_id();
  1379. const auto pointer_type = _.FindDef(pointer_type_id);
  1380. if (!pointer_type || pointer_type->opcode() != SpvOpTypePointer) {
  1381. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1382. << opname << " type for pointer <id> '" << _.getIdName(pointer_id)
  1383. << "' is not a pointer type.";
  1384. }
  1385. const auto storage_class_index = 1u;
  1386. const auto storage_class =
  1387. pointer_type->GetOperandAs<uint32_t>(storage_class_index);
  1388. if (storage_class != SpvStorageClassWorkgroup &&
  1389. storage_class != SpvStorageClassStorageBuffer &&
  1390. storage_class != SpvStorageClassPhysicalStorageBuffer) {
  1391. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1392. << opname << " storage class for pointer type <id> '"
  1393. << _.getIdName(pointer_type_id)
  1394. << "' is not Workgroup or StorageBuffer.";
  1395. }
  1396. const auto pointee_id = pointer_type->GetOperandAs<uint32_t>(2);
  1397. const auto pointee_type = _.FindDef(pointee_id);
  1398. if (!pointee_type || !(_.IsIntScalarOrVectorType(pointee_id) ||
  1399. _.IsFloatScalarOrVectorType(pointee_id))) {
  1400. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1401. << opname << " Pointer <id> '" << _.getIdName(pointer->id())
  1402. << "'s Type must be a scalar or vector type.";
  1403. }
  1404. const auto stride_index =
  1405. (inst->opcode() == SpvOpCooperativeMatrixLoadNV) ? 3u : 2u;
  1406. const auto stride_id = inst->GetOperandAs<uint32_t>(stride_index);
  1407. const auto stride = _.FindDef(stride_id);
  1408. if (!stride || !_.IsIntScalarType(stride->type_id())) {
  1409. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1410. << "Stride operand <id> '" << _.getIdName(stride_id)
  1411. << "' must be a scalar integer type.";
  1412. }
  1413. const auto colmajor_index =
  1414. (inst->opcode() == SpvOpCooperativeMatrixLoadNV) ? 4u : 3u;
  1415. const auto colmajor_id = inst->GetOperandAs<uint32_t>(colmajor_index);
  1416. const auto colmajor = _.FindDef(colmajor_id);
  1417. if (!colmajor || !_.IsBoolScalarType(colmajor->type_id()) ||
  1418. !(spvOpcodeIsConstant(colmajor->opcode()) ||
  1419. spvOpcodeIsSpecConstant(colmajor->opcode()))) {
  1420. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1421. << "Column Major operand <id> '" << _.getIdName(colmajor_id)
  1422. << "' must be a boolean constant instruction.";
  1423. }
  1424. const auto memory_access_index =
  1425. (inst->opcode() == SpvOpCooperativeMatrixLoadNV) ? 5u : 4u;
  1426. if (inst->operands().size() > memory_access_index) {
  1427. if (auto error = CheckMemoryAccess(_, inst, memory_access_index))
  1428. return error;
  1429. }
  1430. return SPV_SUCCESS;
  1431. }
  1432. spv_result_t ValidatePtrComparison(ValidationState_t& _,
  1433. const Instruction* inst) {
  1434. if (_.addressing_model() == SpvAddressingModelLogical &&
  1435. !_.features().variable_pointers) {
  1436. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1437. << "Instruction cannot for logical addressing model be used without "
  1438. "a variable pointers capability";
  1439. }
  1440. const auto result_type = _.FindDef(inst->type_id());
  1441. if (inst->opcode() == SpvOpPtrDiff) {
  1442. if (!result_type || result_type->opcode() != SpvOpTypeInt) {
  1443. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1444. << "Result Type must be an integer scalar";
  1445. }
  1446. } else {
  1447. if (!result_type || result_type->opcode() != SpvOpTypeBool) {
  1448. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1449. << "Result Type must be OpTypeBool";
  1450. }
  1451. }
  1452. const auto op1 = _.FindDef(inst->GetOperandAs<uint32_t>(2u));
  1453. const auto op2 = _.FindDef(inst->GetOperandAs<uint32_t>(3u));
  1454. if (!op1 || !op2 || op1->type_id() != op2->type_id()) {
  1455. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1456. << "The types of Operand 1 and Operand 2 must match";
  1457. }
  1458. const auto op1_type = _.FindDef(op1->type_id());
  1459. if (!op1_type || op1_type->opcode() != SpvOpTypePointer) {
  1460. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1461. << "Operand type must be a pointer";
  1462. }
  1463. SpvStorageClass sc = op1_type->GetOperandAs<SpvStorageClass>(1u);
  1464. if (_.addressing_model() == SpvAddressingModelLogical) {
  1465. if (sc != SpvStorageClassWorkgroup && sc != SpvStorageClassStorageBuffer) {
  1466. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1467. << "Invalid pointer storage class";
  1468. }
  1469. if (sc == SpvStorageClassWorkgroup &&
  1470. !_.HasCapability(SpvCapabilityVariablePointers)) {
  1471. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1472. << "Workgroup storage class pointer requires VariablePointers "
  1473. "capability to be specified";
  1474. }
  1475. } else if (sc == SpvStorageClassPhysicalStorageBuffer) {
  1476. return _.diag(SPV_ERROR_INVALID_ID, inst)
  1477. << "Cannot use a pointer in the PhysicalStorageBuffer storage class";
  1478. }
  1479. return SPV_SUCCESS;
  1480. }
  1481. } // namespace
  1482. spv_result_t MemoryPass(ValidationState_t& _, const Instruction* inst) {
  1483. switch (inst->opcode()) {
  1484. case SpvOpVariable:
  1485. if (auto error = ValidateVariable(_, inst)) return error;
  1486. break;
  1487. case SpvOpLoad:
  1488. if (auto error = ValidateLoad(_, inst)) return error;
  1489. break;
  1490. case SpvOpStore:
  1491. if (auto error = ValidateStore(_, inst)) return error;
  1492. break;
  1493. case SpvOpCopyMemory:
  1494. case SpvOpCopyMemorySized:
  1495. if (auto error = ValidateCopyMemory(_, inst)) return error;
  1496. break;
  1497. case SpvOpPtrAccessChain:
  1498. if (auto error = ValidatePtrAccessChain(_, inst)) return error;
  1499. break;
  1500. case SpvOpAccessChain:
  1501. case SpvOpInBoundsAccessChain:
  1502. case SpvOpInBoundsPtrAccessChain:
  1503. if (auto error = ValidateAccessChain(_, inst)) return error;
  1504. break;
  1505. case SpvOpArrayLength:
  1506. if (auto error = ValidateArrayLength(_, inst)) return error;
  1507. break;
  1508. case SpvOpCooperativeMatrixLoadNV:
  1509. case SpvOpCooperativeMatrixStoreNV:
  1510. if (auto error = ValidateCooperativeMatrixLoadStoreNV(_, inst))
  1511. return error;
  1512. break;
  1513. case SpvOpCooperativeMatrixLengthNV:
  1514. if (auto error = ValidateCooperativeMatrixLengthNV(_, inst)) return error;
  1515. break;
  1516. case SpvOpPtrEqual:
  1517. case SpvOpPtrNotEqual:
  1518. case SpvOpPtrDiff:
  1519. if (auto error = ValidatePtrComparison(_, inst)) return error;
  1520. break;
  1521. case SpvOpImageTexelPointer:
  1522. case SpvOpGenericPtrMemSemantics:
  1523. default:
  1524. break;
  1525. }
  1526. return SPV_SUCCESS;
  1527. }
  1528. } // namespace val
  1529. } // namespace spvtools