validate_extensions.cpp 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030
  1. // Copyright (c) 2018 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Validates correctness of extension SPIR-V instructions.
  15. #include "source/val/validate.h"
  16. #include <sstream>
  17. #include <string>
  18. #include <vector>
  19. #include "source/diagnostic.h"
  20. #include "source/enum_string_mapping.h"
  21. #include "source/extensions.h"
  22. #include "source/latest_version_glsl_std_450_header.h"
  23. #include "source/latest_version_opencl_std_header.h"
  24. #include "source/opcode.h"
  25. #include "source/spirv_target_env.h"
  26. #include "source/val/instruction.h"
  27. #include "source/val/validation_state.h"
  28. namespace spvtools {
  29. namespace val {
  30. namespace {
  31. uint32_t GetSizeTBitWidth(const ValidationState_t& _) {
  32. if (_.addressing_model() == SpvAddressingModelPhysical32) return 32;
  33. if (_.addressing_model() == SpvAddressingModelPhysical64) return 64;
  34. return 0;
  35. }
  36. } // anonymous namespace
  37. spv_result_t ValidateExtension(ValidationState_t& _, const Instruction* inst) {
  38. if (spvIsWebGPUEnv(_.context()->target_env)) {
  39. std::string extension = GetExtensionString(&(inst->c_inst()));
  40. if (extension != ExtensionToString(kSPV_KHR_vulkan_memory_model)) {
  41. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  42. << "For WebGPU, the only valid parameter to OpExtension is "
  43. << "\"" << ExtensionToString(kSPV_KHR_vulkan_memory_model)
  44. << "\".";
  45. }
  46. }
  47. return SPV_SUCCESS;
  48. }
  49. spv_result_t ValidateExtInstImport(ValidationState_t& _,
  50. const Instruction* inst) {
  51. if (spvIsWebGPUEnv(_.context()->target_env)) {
  52. const auto name_id = 1;
  53. const std::string name(reinterpret_cast<const char*>(
  54. inst->words().data() + inst->operands()[name_id].offset));
  55. if (name != "GLSL.std.450") {
  56. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  57. << "For WebGPU, the only valid parameter to OpExtInstImport is "
  58. "\"GLSL.std.450\".";
  59. }
  60. }
  61. return SPV_SUCCESS;
  62. }
  63. spv_result_t ValidateExtInst(ValidationState_t& _, const Instruction* inst) {
  64. const uint32_t result_type = inst->type_id();
  65. const uint32_t num_operands = static_cast<uint32_t>(inst->operands().size());
  66. const uint32_t ext_inst_set = inst->word(3);
  67. const uint32_t ext_inst_index = inst->word(4);
  68. const spv_ext_inst_type_t ext_inst_type =
  69. spv_ext_inst_type_t(inst->ext_inst_type());
  70. auto ext_inst_name = [&_, ext_inst_set, ext_inst_type, ext_inst_index]() {
  71. spv_ext_inst_desc desc = nullptr;
  72. if (_.grammar().lookupExtInst(ext_inst_type, ext_inst_index, &desc) !=
  73. SPV_SUCCESS ||
  74. !desc) {
  75. return std::string("Unknown ExtInst");
  76. }
  77. auto* import_inst = _.FindDef(ext_inst_set);
  78. assert(import_inst);
  79. std::ostringstream ss;
  80. ss << reinterpret_cast<const char*>(import_inst->words().data() + 2);
  81. ss << " ";
  82. ss << desc->name;
  83. return ss.str();
  84. };
  85. if (ext_inst_type == SPV_EXT_INST_TYPE_GLSL_STD_450) {
  86. const GLSLstd450 ext_inst_key = GLSLstd450(ext_inst_index);
  87. switch (ext_inst_key) {
  88. case GLSLstd450Round:
  89. case GLSLstd450RoundEven:
  90. case GLSLstd450FAbs:
  91. case GLSLstd450Trunc:
  92. case GLSLstd450FSign:
  93. case GLSLstd450Floor:
  94. case GLSLstd450Ceil:
  95. case GLSLstd450Fract:
  96. case GLSLstd450Sqrt:
  97. case GLSLstd450InverseSqrt:
  98. case GLSLstd450FMin:
  99. case GLSLstd450FMax:
  100. case GLSLstd450FClamp:
  101. case GLSLstd450FMix:
  102. case GLSLstd450Step:
  103. case GLSLstd450SmoothStep:
  104. case GLSLstd450Fma:
  105. case GLSLstd450Normalize:
  106. case GLSLstd450FaceForward:
  107. case GLSLstd450Reflect:
  108. case GLSLstd450NMin:
  109. case GLSLstd450NMax:
  110. case GLSLstd450NClamp: {
  111. if (!_.IsFloatScalarOrVectorType(result_type)) {
  112. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  113. << ext_inst_name() << ": "
  114. << "expected Result Type to be a float scalar or vector type";
  115. }
  116. for (uint32_t operand_index = 4; operand_index < num_operands;
  117. ++operand_index) {
  118. const uint32_t operand_type = _.GetOperandTypeId(inst, operand_index);
  119. if (result_type != operand_type) {
  120. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  121. << ext_inst_name() << ": "
  122. << "expected types of all operands to be equal to Result "
  123. "Type";
  124. }
  125. }
  126. break;
  127. }
  128. case GLSLstd450SAbs:
  129. case GLSLstd450SSign:
  130. case GLSLstd450UMin:
  131. case GLSLstd450SMin:
  132. case GLSLstd450UMax:
  133. case GLSLstd450SMax:
  134. case GLSLstd450UClamp:
  135. case GLSLstd450SClamp:
  136. case GLSLstd450FindILsb:
  137. case GLSLstd450FindUMsb:
  138. case GLSLstd450FindSMsb: {
  139. if (!_.IsIntScalarOrVectorType(result_type)) {
  140. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  141. << ext_inst_name() << ": "
  142. << "expected Result Type to be an int scalar or vector type";
  143. }
  144. const uint32_t result_type_bit_width = _.GetBitWidth(result_type);
  145. const uint32_t result_type_dimension = _.GetDimension(result_type);
  146. for (uint32_t operand_index = 4; operand_index < num_operands;
  147. ++operand_index) {
  148. const uint32_t operand_type = _.GetOperandTypeId(inst, operand_index);
  149. if (!_.IsIntScalarOrVectorType(operand_type)) {
  150. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  151. << ext_inst_name() << ": "
  152. << "expected all operands to be int scalars or vectors";
  153. }
  154. if (result_type_dimension != _.GetDimension(operand_type)) {
  155. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  156. << ext_inst_name() << ": "
  157. << "expected all operands to have the same dimension as "
  158. << "Result Type";
  159. }
  160. if (result_type_bit_width != _.GetBitWidth(operand_type)) {
  161. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  162. << ext_inst_name() << ": "
  163. << "expected all operands to have the same bit width as "
  164. << "Result Type";
  165. }
  166. if (ext_inst_key == GLSLstd450FindUMsb ||
  167. ext_inst_key == GLSLstd450FindSMsb) {
  168. if (result_type_bit_width != 32) {
  169. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  170. << ext_inst_name() << ": "
  171. << "this instruction is currently limited to 32-bit width "
  172. << "components";
  173. }
  174. }
  175. }
  176. break;
  177. }
  178. case GLSLstd450Radians:
  179. case GLSLstd450Degrees:
  180. case GLSLstd450Sin:
  181. case GLSLstd450Cos:
  182. case GLSLstd450Tan:
  183. case GLSLstd450Asin:
  184. case GLSLstd450Acos:
  185. case GLSLstd450Atan:
  186. case GLSLstd450Sinh:
  187. case GLSLstd450Cosh:
  188. case GLSLstd450Tanh:
  189. case GLSLstd450Asinh:
  190. case GLSLstd450Acosh:
  191. case GLSLstd450Atanh:
  192. case GLSLstd450Exp:
  193. case GLSLstd450Exp2:
  194. case GLSLstd450Log:
  195. case GLSLstd450Log2:
  196. case GLSLstd450Atan2:
  197. case GLSLstd450Pow: {
  198. if (!_.IsFloatScalarOrVectorType(result_type)) {
  199. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  200. << ext_inst_name() << ": "
  201. << "expected Result Type to be a 16 or 32-bit scalar or "
  202. "vector float type";
  203. }
  204. const uint32_t result_type_bit_width = _.GetBitWidth(result_type);
  205. if (result_type_bit_width != 16 && result_type_bit_width != 32) {
  206. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  207. << ext_inst_name() << ": "
  208. << "expected Result Type to be a 16 or 32-bit scalar or "
  209. "vector float type";
  210. }
  211. for (uint32_t operand_index = 4; operand_index < num_operands;
  212. ++operand_index) {
  213. const uint32_t operand_type = _.GetOperandTypeId(inst, operand_index);
  214. if (result_type != operand_type) {
  215. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  216. << ext_inst_name() << ": "
  217. << "expected types of all operands to be equal to Result "
  218. "Type";
  219. }
  220. }
  221. break;
  222. }
  223. case GLSLstd450Determinant: {
  224. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  225. uint32_t num_rows = 0;
  226. uint32_t num_cols = 0;
  227. uint32_t col_type = 0;
  228. uint32_t component_type = 0;
  229. if (!_.GetMatrixTypeInfo(x_type, &num_rows, &num_cols, &col_type,
  230. &component_type) ||
  231. num_rows != num_cols) {
  232. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  233. << ext_inst_name() << ": "
  234. << "expected operand X to be a square matrix";
  235. }
  236. if (result_type != component_type) {
  237. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  238. << ext_inst_name() << ": "
  239. << "expected operand X component type to be equal to "
  240. << "Result Type";
  241. }
  242. break;
  243. }
  244. case GLSLstd450MatrixInverse: {
  245. uint32_t num_rows = 0;
  246. uint32_t num_cols = 0;
  247. uint32_t col_type = 0;
  248. uint32_t component_type = 0;
  249. if (!_.GetMatrixTypeInfo(result_type, &num_rows, &num_cols, &col_type,
  250. &component_type) ||
  251. num_rows != num_cols) {
  252. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  253. << ext_inst_name() << ": "
  254. << "expected Result Type to be a square matrix";
  255. }
  256. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  257. if (result_type != x_type) {
  258. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  259. << ext_inst_name() << ": "
  260. << "expected operand X type to be equal to Result Type";
  261. }
  262. break;
  263. }
  264. case GLSLstd450Modf: {
  265. if (!_.IsFloatScalarOrVectorType(result_type)) {
  266. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  267. << ext_inst_name() << ": "
  268. << "expected Result Type to be a scalar or vector float type";
  269. }
  270. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  271. const uint32_t i_type = _.GetOperandTypeId(inst, 5);
  272. if (x_type != result_type) {
  273. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  274. << ext_inst_name() << ": "
  275. << "expected operand X type to be equal to Result Type";
  276. }
  277. uint32_t i_storage_class = 0;
  278. uint32_t i_data_type = 0;
  279. if (!_.GetPointerTypeInfo(i_type, &i_data_type, &i_storage_class)) {
  280. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  281. << ext_inst_name() << ": "
  282. << "expected operand I to be a pointer";
  283. }
  284. if (i_data_type != result_type) {
  285. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  286. << ext_inst_name() << ": "
  287. << "expected operand I data type to be equal to Result Type";
  288. }
  289. break;
  290. }
  291. case GLSLstd450ModfStruct: {
  292. std::vector<uint32_t> result_types;
  293. if (!_.GetStructMemberTypes(result_type, &result_types) ||
  294. result_types.size() != 2 ||
  295. !_.IsFloatScalarOrVectorType(result_types[0]) ||
  296. result_types[1] != result_types[0]) {
  297. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  298. << ext_inst_name() << ": "
  299. << "expected Result Type to be a struct with two identical "
  300. << "scalar or vector float type members";
  301. }
  302. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  303. if (x_type != result_types[0]) {
  304. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  305. << ext_inst_name() << ": "
  306. << "expected operand X type to be equal to members of "
  307. << "Result Type struct";
  308. }
  309. break;
  310. }
  311. case GLSLstd450Frexp: {
  312. if (!_.IsFloatScalarOrVectorType(result_type)) {
  313. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  314. << ext_inst_name() << ": "
  315. << "expected Result Type to be a scalar or vector float type";
  316. }
  317. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  318. const uint32_t exp_type = _.GetOperandTypeId(inst, 5);
  319. if (x_type != result_type) {
  320. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  321. << ext_inst_name() << ": "
  322. << "expected operand X type to be equal to Result Type";
  323. }
  324. uint32_t exp_storage_class = 0;
  325. uint32_t exp_data_type = 0;
  326. if (!_.GetPointerTypeInfo(exp_type, &exp_data_type,
  327. &exp_storage_class)) {
  328. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  329. << ext_inst_name() << ": "
  330. << "expected operand Exp to be a pointer";
  331. }
  332. if (!_.IsIntScalarOrVectorType(exp_data_type) ||
  333. (!_.HasExtension(kSPV_AMD_gpu_shader_int16) &&
  334. _.GetBitWidth(exp_data_type) != 32) ||
  335. (_.HasExtension(kSPV_AMD_gpu_shader_int16) &&
  336. _.GetBitWidth(exp_data_type) != 16 &&
  337. _.GetBitWidth(exp_data_type) != 32)) {
  338. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  339. << ext_inst_name() << ": "
  340. << "expected operand Exp data type to be a "
  341. << (_.HasExtension(kSPV_AMD_gpu_shader_int16)
  342. ? "16-bit or 32-bit "
  343. : "32-bit ")
  344. << "int scalar or vector type";
  345. }
  346. if (_.GetDimension(result_type) != _.GetDimension(exp_data_type)) {
  347. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  348. << ext_inst_name() << ": "
  349. << "expected operand Exp data type to have the same component "
  350. << "number as Result Type";
  351. }
  352. break;
  353. }
  354. case GLSLstd450Ldexp: {
  355. if (!_.IsFloatScalarOrVectorType(result_type)) {
  356. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  357. << ext_inst_name() << ": "
  358. << "expected Result Type to be a scalar or vector float type";
  359. }
  360. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  361. const uint32_t exp_type = _.GetOperandTypeId(inst, 5);
  362. if (x_type != result_type) {
  363. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  364. << ext_inst_name() << ": "
  365. << "expected operand X type to be equal to Result Type";
  366. }
  367. if (!_.IsIntScalarOrVectorType(exp_type)) {
  368. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  369. << ext_inst_name() << ": "
  370. << "expected operand Exp to be a 32-bit int scalar "
  371. << "or vector type";
  372. }
  373. if (_.GetDimension(result_type) != _.GetDimension(exp_type)) {
  374. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  375. << ext_inst_name() << ": "
  376. << "expected operand Exp to have the same component "
  377. << "number as Result Type";
  378. }
  379. break;
  380. }
  381. case GLSLstd450FrexpStruct: {
  382. std::vector<uint32_t> result_types;
  383. if (!_.GetStructMemberTypes(result_type, &result_types) ||
  384. result_types.size() != 2 ||
  385. !_.IsFloatScalarOrVectorType(result_types[0]) ||
  386. !_.IsIntScalarOrVectorType(result_types[1]) ||
  387. (!_.HasExtension(kSPV_AMD_gpu_shader_int16) &&
  388. _.GetBitWidth(result_types[1]) != 32) ||
  389. (_.HasExtension(kSPV_AMD_gpu_shader_int16) &&
  390. _.GetBitWidth(result_types[1]) != 16 &&
  391. _.GetBitWidth(result_types[1]) != 32) ||
  392. _.GetDimension(result_types[0]) !=
  393. _.GetDimension(result_types[1])) {
  394. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  395. << ext_inst_name() << ": "
  396. << "expected Result Type to be a struct with two members, "
  397. << "first member a float scalar or vector, second member a "
  398. << (_.HasExtension(kSPV_AMD_gpu_shader_int16)
  399. ? "16-bit or 32-bit "
  400. : "32-bit ")
  401. << "int scalar or vector with the same number of "
  402. << "components as the first member";
  403. }
  404. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  405. if (x_type != result_types[0]) {
  406. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  407. << ext_inst_name() << ": "
  408. << "expected operand X type to be equal to the first member "
  409. << "of Result Type struct";
  410. }
  411. break;
  412. }
  413. case GLSLstd450PackSnorm4x8:
  414. case GLSLstd450PackUnorm4x8: {
  415. if (!_.IsIntScalarType(result_type) ||
  416. _.GetBitWidth(result_type) != 32) {
  417. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  418. << ext_inst_name() << ": "
  419. << "expected Result Type to be 32-bit int scalar type";
  420. }
  421. const uint32_t v_type = _.GetOperandTypeId(inst, 4);
  422. if (!_.IsFloatVectorType(v_type) || _.GetDimension(v_type) != 4 ||
  423. _.GetBitWidth(v_type) != 32) {
  424. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  425. << ext_inst_name() << ": "
  426. << "expected operand V to be a 32-bit float vector of size 4";
  427. }
  428. break;
  429. }
  430. case GLSLstd450PackSnorm2x16:
  431. case GLSLstd450PackUnorm2x16:
  432. case GLSLstd450PackHalf2x16: {
  433. if (!_.IsIntScalarType(result_type) ||
  434. _.GetBitWidth(result_type) != 32) {
  435. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  436. << ext_inst_name() << ": "
  437. << "expected Result Type to be 32-bit int scalar type";
  438. }
  439. const uint32_t v_type = _.GetOperandTypeId(inst, 4);
  440. if (!_.IsFloatVectorType(v_type) || _.GetDimension(v_type) != 2 ||
  441. _.GetBitWidth(v_type) != 32) {
  442. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  443. << ext_inst_name() << ": "
  444. << "expected operand V to be a 32-bit float vector of size 2";
  445. }
  446. break;
  447. }
  448. case GLSLstd450PackDouble2x32: {
  449. if (!_.IsFloatScalarType(result_type) ||
  450. _.GetBitWidth(result_type) != 64) {
  451. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  452. << ext_inst_name() << ": "
  453. << "expected Result Type to be 64-bit float scalar type";
  454. }
  455. const uint32_t v_type = _.GetOperandTypeId(inst, 4);
  456. if (!_.IsIntVectorType(v_type) || _.GetDimension(v_type) != 2 ||
  457. _.GetBitWidth(v_type) != 32) {
  458. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  459. << ext_inst_name() << ": "
  460. << "expected operand V to be a 32-bit int vector of size 2";
  461. }
  462. break;
  463. }
  464. case GLSLstd450UnpackSnorm4x8:
  465. case GLSLstd450UnpackUnorm4x8: {
  466. if (!_.IsFloatVectorType(result_type) ||
  467. _.GetDimension(result_type) != 4 ||
  468. _.GetBitWidth(result_type) != 32) {
  469. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  470. << ext_inst_name() << ": "
  471. << "expected Result Type to be a 32-bit float vector of size "
  472. "4";
  473. }
  474. const uint32_t v_type = _.GetOperandTypeId(inst, 4);
  475. if (!_.IsIntScalarType(v_type) || _.GetBitWidth(v_type) != 32) {
  476. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  477. << ext_inst_name() << ": "
  478. << "expected operand P to be a 32-bit int scalar";
  479. }
  480. break;
  481. }
  482. case GLSLstd450UnpackSnorm2x16:
  483. case GLSLstd450UnpackUnorm2x16:
  484. case GLSLstd450UnpackHalf2x16: {
  485. if (!_.IsFloatVectorType(result_type) ||
  486. _.GetDimension(result_type) != 2 ||
  487. _.GetBitWidth(result_type) != 32) {
  488. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  489. << ext_inst_name() << ": "
  490. << "expected Result Type to be a 32-bit float vector of size "
  491. "2";
  492. }
  493. const uint32_t v_type = _.GetOperandTypeId(inst, 4);
  494. if (!_.IsIntScalarType(v_type) || _.GetBitWidth(v_type) != 32) {
  495. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  496. << ext_inst_name() << ": "
  497. << "expected operand P to be a 32-bit int scalar";
  498. }
  499. break;
  500. }
  501. case GLSLstd450UnpackDouble2x32: {
  502. if (!_.IsIntVectorType(result_type) ||
  503. _.GetDimension(result_type) != 2 ||
  504. _.GetBitWidth(result_type) != 32) {
  505. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  506. << ext_inst_name() << ": "
  507. << "expected Result Type to be a 32-bit int vector of size "
  508. "2";
  509. }
  510. const uint32_t v_type = _.GetOperandTypeId(inst, 4);
  511. if (!_.IsFloatScalarType(v_type) || _.GetBitWidth(v_type) != 64) {
  512. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  513. << ext_inst_name() << ": "
  514. << "expected operand V to be a 64-bit float scalar";
  515. }
  516. break;
  517. }
  518. case GLSLstd450Length: {
  519. if (!_.IsFloatScalarType(result_type)) {
  520. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  521. << ext_inst_name() << ": "
  522. << "expected Result Type to be a float scalar type";
  523. }
  524. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  525. if (!_.IsFloatScalarOrVectorType(x_type)) {
  526. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  527. << ext_inst_name() << ": "
  528. << "expected operand X to be of float scalar or vector type";
  529. }
  530. if (result_type != _.GetComponentType(x_type)) {
  531. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  532. << ext_inst_name() << ": "
  533. << "expected operand X component type to be equal to Result "
  534. "Type";
  535. }
  536. break;
  537. }
  538. case GLSLstd450Distance: {
  539. if (!_.IsFloatScalarType(result_type)) {
  540. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  541. << ext_inst_name() << ": "
  542. << "expected Result Type to be a float scalar type";
  543. }
  544. const uint32_t p0_type = _.GetOperandTypeId(inst, 4);
  545. if (!_.IsFloatScalarOrVectorType(p0_type)) {
  546. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  547. << ext_inst_name() << ": "
  548. << "expected operand P0 to be of float scalar or vector type";
  549. }
  550. if (result_type != _.GetComponentType(p0_type)) {
  551. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  552. << ext_inst_name() << ": "
  553. << "expected operand P0 component type to be equal to "
  554. << "Result Type";
  555. }
  556. const uint32_t p1_type = _.GetOperandTypeId(inst, 5);
  557. if (!_.IsFloatScalarOrVectorType(p1_type)) {
  558. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  559. << ext_inst_name() << ": "
  560. << "expected operand P1 to be of float scalar or vector type";
  561. }
  562. if (result_type != _.GetComponentType(p1_type)) {
  563. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  564. << ext_inst_name() << ": "
  565. << "expected operand P1 component type to be equal to "
  566. << "Result Type";
  567. }
  568. if (_.GetDimension(p0_type) != _.GetDimension(p1_type)) {
  569. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  570. << ext_inst_name() << ": "
  571. << "expected operands P0 and P1 to have the same number of "
  572. << "components";
  573. }
  574. break;
  575. }
  576. case GLSLstd450Cross: {
  577. if (!_.IsFloatVectorType(result_type)) {
  578. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  579. << ext_inst_name() << ": "
  580. << "expected Result Type to be a float vector type";
  581. }
  582. if (_.GetDimension(result_type) != 3) {
  583. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  584. << ext_inst_name() << ": "
  585. << "expected Result Type to have 3 components";
  586. }
  587. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  588. const uint32_t y_type = _.GetOperandTypeId(inst, 5);
  589. if (x_type != result_type) {
  590. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  591. << ext_inst_name() << ": "
  592. << "expected operand X type to be equal to Result Type";
  593. }
  594. if (y_type != result_type) {
  595. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  596. << ext_inst_name() << ": "
  597. << "expected operand Y type to be equal to Result Type";
  598. }
  599. break;
  600. }
  601. case GLSLstd450Refract: {
  602. if (!_.IsFloatScalarOrVectorType(result_type)) {
  603. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  604. << ext_inst_name() << ": "
  605. << "expected Result Type to be a float scalar or vector type";
  606. }
  607. const uint32_t i_type = _.GetOperandTypeId(inst, 4);
  608. const uint32_t n_type = _.GetOperandTypeId(inst, 5);
  609. const uint32_t eta_type = _.GetOperandTypeId(inst, 6);
  610. if (result_type != i_type) {
  611. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  612. << ext_inst_name() << ": "
  613. << "expected operand I to be of type equal to Result Type";
  614. }
  615. if (result_type != n_type) {
  616. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  617. << ext_inst_name() << ": "
  618. << "expected operand N to be of type equal to Result Type";
  619. }
  620. if (!_.IsFloatScalarType(eta_type)) {
  621. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  622. << ext_inst_name() << ": "
  623. << "expected operand Eta to be a float scalar";
  624. }
  625. break;
  626. }
  627. case GLSLstd450InterpolateAtCentroid:
  628. case GLSLstd450InterpolateAtSample:
  629. case GLSLstd450InterpolateAtOffset: {
  630. if (!_.HasCapability(SpvCapabilityInterpolationFunction)) {
  631. return _.diag(SPV_ERROR_INVALID_CAPABILITY, inst)
  632. << ext_inst_name()
  633. << " requires capability InterpolationFunction";
  634. }
  635. if (!_.IsFloatScalarOrVectorType(result_type) ||
  636. _.GetBitWidth(result_type) != 32) {
  637. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  638. << ext_inst_name() << ": "
  639. << "expected Result Type to be a 32-bit float scalar "
  640. << "or vector type";
  641. }
  642. const uint32_t interpolant_type = _.GetOperandTypeId(inst, 4);
  643. uint32_t interpolant_storage_class = 0;
  644. uint32_t interpolant_data_type = 0;
  645. if (!_.GetPointerTypeInfo(interpolant_type, &interpolant_data_type,
  646. &interpolant_storage_class)) {
  647. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  648. << ext_inst_name() << ": "
  649. << "expected Interpolant to be a pointer";
  650. }
  651. if (result_type != interpolant_data_type) {
  652. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  653. << ext_inst_name() << ": "
  654. << "expected Interpolant data type to be equal to Result Type";
  655. }
  656. if (interpolant_storage_class != SpvStorageClassInput) {
  657. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  658. << ext_inst_name() << ": "
  659. << "expected Interpolant storage class to be Input";
  660. }
  661. if (ext_inst_key == GLSLstd450InterpolateAtSample) {
  662. const uint32_t sample_type = _.GetOperandTypeId(inst, 5);
  663. if (!_.IsIntScalarType(sample_type) ||
  664. _.GetBitWidth(sample_type) != 32) {
  665. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  666. << ext_inst_name() << ": "
  667. << "expected Sample to be 32-bit integer";
  668. }
  669. }
  670. if (ext_inst_key == GLSLstd450InterpolateAtOffset) {
  671. const uint32_t offset_type = _.GetOperandTypeId(inst, 5);
  672. if (!_.IsFloatVectorType(offset_type) ||
  673. _.GetDimension(offset_type) != 2 ||
  674. _.GetBitWidth(offset_type) != 32) {
  675. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  676. << ext_inst_name() << ": "
  677. << "expected Offset to be a vector of 2 32-bit floats";
  678. }
  679. }
  680. _.function(inst->function()->id())
  681. ->RegisterExecutionModelLimitation(
  682. SpvExecutionModelFragment,
  683. ext_inst_name() +
  684. std::string(" requires Fragment execution model"));
  685. break;
  686. }
  687. case GLSLstd450IMix: {
  688. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  689. << "Extended instruction GLSLstd450IMix is not supported";
  690. }
  691. case GLSLstd450Bad: {
  692. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  693. << "Encountered extended instruction GLSLstd450Bad";
  694. }
  695. case GLSLstd450Count: {
  696. assert(0);
  697. break;
  698. }
  699. }
  700. } else if (ext_inst_type == SPV_EXT_INST_TYPE_OPENCL_STD) {
  701. const OpenCLLIB::Entrypoints ext_inst_key =
  702. OpenCLLIB::Entrypoints(ext_inst_index);
  703. switch (ext_inst_key) {
  704. case OpenCLLIB::Acos:
  705. case OpenCLLIB::Acosh:
  706. case OpenCLLIB::Acospi:
  707. case OpenCLLIB::Asin:
  708. case OpenCLLIB::Asinh:
  709. case OpenCLLIB::Asinpi:
  710. case OpenCLLIB::Atan:
  711. case OpenCLLIB::Atan2:
  712. case OpenCLLIB::Atanh:
  713. case OpenCLLIB::Atanpi:
  714. case OpenCLLIB::Atan2pi:
  715. case OpenCLLIB::Cbrt:
  716. case OpenCLLIB::Ceil:
  717. case OpenCLLIB::Copysign:
  718. case OpenCLLIB::Cos:
  719. case OpenCLLIB::Cosh:
  720. case OpenCLLIB::Cospi:
  721. case OpenCLLIB::Erfc:
  722. case OpenCLLIB::Erf:
  723. case OpenCLLIB::Exp:
  724. case OpenCLLIB::Exp2:
  725. case OpenCLLIB::Exp10:
  726. case OpenCLLIB::Expm1:
  727. case OpenCLLIB::Fabs:
  728. case OpenCLLIB::Fdim:
  729. case OpenCLLIB::Floor:
  730. case OpenCLLIB::Fma:
  731. case OpenCLLIB::Fmax:
  732. case OpenCLLIB::Fmin:
  733. case OpenCLLIB::Fmod:
  734. case OpenCLLIB::Hypot:
  735. case OpenCLLIB::Lgamma:
  736. case OpenCLLIB::Log:
  737. case OpenCLLIB::Log2:
  738. case OpenCLLIB::Log10:
  739. case OpenCLLIB::Log1p:
  740. case OpenCLLIB::Logb:
  741. case OpenCLLIB::Mad:
  742. case OpenCLLIB::Maxmag:
  743. case OpenCLLIB::Minmag:
  744. case OpenCLLIB::Nextafter:
  745. case OpenCLLIB::Pow:
  746. case OpenCLLIB::Powr:
  747. case OpenCLLIB::Remainder:
  748. case OpenCLLIB::Rint:
  749. case OpenCLLIB::Round:
  750. case OpenCLLIB::Rsqrt:
  751. case OpenCLLIB::Sin:
  752. case OpenCLLIB::Sinh:
  753. case OpenCLLIB::Sinpi:
  754. case OpenCLLIB::Sqrt:
  755. case OpenCLLIB::Tan:
  756. case OpenCLLIB::Tanh:
  757. case OpenCLLIB::Tanpi:
  758. case OpenCLLIB::Tgamma:
  759. case OpenCLLIB::Trunc:
  760. case OpenCLLIB::Half_cos:
  761. case OpenCLLIB::Half_divide:
  762. case OpenCLLIB::Half_exp:
  763. case OpenCLLIB::Half_exp2:
  764. case OpenCLLIB::Half_exp10:
  765. case OpenCLLIB::Half_log:
  766. case OpenCLLIB::Half_log2:
  767. case OpenCLLIB::Half_log10:
  768. case OpenCLLIB::Half_powr:
  769. case OpenCLLIB::Half_recip:
  770. case OpenCLLIB::Half_rsqrt:
  771. case OpenCLLIB::Half_sin:
  772. case OpenCLLIB::Half_sqrt:
  773. case OpenCLLIB::Half_tan:
  774. case OpenCLLIB::Native_cos:
  775. case OpenCLLIB::Native_divide:
  776. case OpenCLLIB::Native_exp:
  777. case OpenCLLIB::Native_exp2:
  778. case OpenCLLIB::Native_exp10:
  779. case OpenCLLIB::Native_log:
  780. case OpenCLLIB::Native_log2:
  781. case OpenCLLIB::Native_log10:
  782. case OpenCLLIB::Native_powr:
  783. case OpenCLLIB::Native_recip:
  784. case OpenCLLIB::Native_rsqrt:
  785. case OpenCLLIB::Native_sin:
  786. case OpenCLLIB::Native_sqrt:
  787. case OpenCLLIB::Native_tan:
  788. case OpenCLLIB::FClamp:
  789. case OpenCLLIB::Degrees:
  790. case OpenCLLIB::FMax_common:
  791. case OpenCLLIB::FMin_common:
  792. case OpenCLLIB::Mix:
  793. case OpenCLLIB::Radians:
  794. case OpenCLLIB::Step:
  795. case OpenCLLIB::Smoothstep:
  796. case OpenCLLIB::Sign: {
  797. if (!_.IsFloatScalarOrVectorType(result_type)) {
  798. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  799. << ext_inst_name() << ": "
  800. << "expected Result Type to be a float scalar or vector type";
  801. }
  802. const uint32_t num_components = _.GetDimension(result_type);
  803. if (num_components > 4 && num_components != 8 && num_components != 16) {
  804. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  805. << ext_inst_name() << ": "
  806. << "expected Result Type to be a scalar or a vector with 2, "
  807. "3, 4, 8 or 16 components";
  808. }
  809. for (uint32_t operand_index = 4; operand_index < num_operands;
  810. ++operand_index) {
  811. const uint32_t operand_type = _.GetOperandTypeId(inst, operand_index);
  812. if (result_type != operand_type) {
  813. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  814. << ext_inst_name() << ": "
  815. << "expected types of all operands to be equal to Result "
  816. "Type";
  817. }
  818. }
  819. break;
  820. }
  821. case OpenCLLIB::Fract:
  822. case OpenCLLIB::Modf:
  823. case OpenCLLIB::Sincos:
  824. case OpenCLLIB::Remquo: {
  825. if (!_.IsFloatScalarOrVectorType(result_type)) {
  826. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  827. << ext_inst_name() << ": "
  828. << "expected Result Type to be a float scalar or vector type";
  829. }
  830. const uint32_t num_components = _.GetDimension(result_type);
  831. if (num_components > 4 && num_components != 8 && num_components != 16) {
  832. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  833. << ext_inst_name() << ": "
  834. << "expected Result Type to be a scalar or a vector with 2, "
  835. "3, 4, 8 or 16 components";
  836. }
  837. uint32_t operand_index = 4;
  838. const uint32_t x_type = _.GetOperandTypeId(inst, operand_index++);
  839. if (result_type != x_type) {
  840. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  841. << ext_inst_name() << ": "
  842. << "expected type of operand X to be equal to Result Type";
  843. }
  844. if (ext_inst_key == OpenCLLIB::Remquo) {
  845. const uint32_t y_type = _.GetOperandTypeId(inst, operand_index++);
  846. if (result_type != y_type) {
  847. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  848. << ext_inst_name() << ": "
  849. << "expected type of operand Y to be equal to Result Type";
  850. }
  851. }
  852. const uint32_t p_type = _.GetOperandTypeId(inst, operand_index++);
  853. uint32_t p_storage_class = 0;
  854. uint32_t p_data_type = 0;
  855. if (!_.GetPointerTypeInfo(p_type, &p_data_type, &p_storage_class)) {
  856. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  857. << ext_inst_name() << ": "
  858. << "expected the last operand to be a pointer";
  859. }
  860. if (p_storage_class != SpvStorageClassGeneric &&
  861. p_storage_class != SpvStorageClassCrossWorkgroup &&
  862. p_storage_class != SpvStorageClassWorkgroup &&
  863. p_storage_class != SpvStorageClassFunction) {
  864. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  865. << ext_inst_name() << ": "
  866. << "expected storage class of the pointer to be Generic, "
  867. "CrossWorkgroup, Workgroup or Function";
  868. }
  869. if (result_type != p_data_type) {
  870. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  871. << ext_inst_name() << ": "
  872. << "expected data type of the pointer to be equal to Result "
  873. "Type";
  874. }
  875. break;
  876. }
  877. case OpenCLLIB::Frexp:
  878. case OpenCLLIB::Lgamma_r: {
  879. if (!_.IsFloatScalarOrVectorType(result_type)) {
  880. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  881. << ext_inst_name() << ": "
  882. << "expected Result Type to be a float scalar or vector type";
  883. }
  884. const uint32_t num_components = _.GetDimension(result_type);
  885. if (num_components > 4 && num_components != 8 && num_components != 16) {
  886. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  887. << ext_inst_name() << ": "
  888. << "expected Result Type to be a scalar or a vector with 2, "
  889. "3, 4, 8 or 16 components";
  890. }
  891. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  892. if (result_type != x_type) {
  893. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  894. << ext_inst_name() << ": "
  895. << "expected type of operand X to be equal to Result Type";
  896. }
  897. const uint32_t p_type = _.GetOperandTypeId(inst, 5);
  898. uint32_t p_storage_class = 0;
  899. uint32_t p_data_type = 0;
  900. if (!_.GetPointerTypeInfo(p_type, &p_data_type, &p_storage_class)) {
  901. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  902. << ext_inst_name() << ": "
  903. << "expected the last operand to be a pointer";
  904. }
  905. if (p_storage_class != SpvStorageClassGeneric &&
  906. p_storage_class != SpvStorageClassCrossWorkgroup &&
  907. p_storage_class != SpvStorageClassWorkgroup &&
  908. p_storage_class != SpvStorageClassFunction) {
  909. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  910. << ext_inst_name() << ": "
  911. << "expected storage class of the pointer to be Generic, "
  912. "CrossWorkgroup, Workgroup or Function";
  913. }
  914. if (!_.IsIntScalarOrVectorType(p_data_type) ||
  915. _.GetBitWidth(p_data_type) != 32) {
  916. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  917. << ext_inst_name() << ": "
  918. << "expected data type of the pointer to be a 32-bit int "
  919. "scalar or vector type";
  920. }
  921. if (_.GetDimension(p_data_type) != num_components) {
  922. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  923. << ext_inst_name() << ": "
  924. << "expected data type of the pointer to have the same number "
  925. "of components as Result Type";
  926. }
  927. break;
  928. }
  929. case OpenCLLIB::Ilogb: {
  930. if (!_.IsIntScalarOrVectorType(result_type) ||
  931. _.GetBitWidth(result_type) != 32) {
  932. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  933. << ext_inst_name() << ": "
  934. << "expected Result Type to be a 32-bit int scalar or vector "
  935. "type";
  936. }
  937. const uint32_t num_components = _.GetDimension(result_type);
  938. if (num_components > 4 && num_components != 8 && num_components != 16) {
  939. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  940. << ext_inst_name() << ": "
  941. << "expected Result Type to be a scalar or a vector with 2, "
  942. "3, 4, 8 or 16 components";
  943. }
  944. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  945. if (!_.IsFloatScalarOrVectorType(x_type)) {
  946. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  947. << ext_inst_name() << ": "
  948. << "expected operand X to be a float scalar or vector";
  949. }
  950. if (_.GetDimension(x_type) != num_components) {
  951. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  952. << ext_inst_name() << ": "
  953. << "expected operand X to have the same number of components "
  954. "as Result Type";
  955. }
  956. break;
  957. }
  958. case OpenCLLIB::Ldexp:
  959. case OpenCLLIB::Pown:
  960. case OpenCLLIB::Rootn: {
  961. if (!_.IsFloatScalarOrVectorType(result_type)) {
  962. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  963. << ext_inst_name() << ": "
  964. << "expected Result Type to be a float scalar or vector type";
  965. }
  966. const uint32_t num_components = _.GetDimension(result_type);
  967. if (num_components > 4 && num_components != 8 && num_components != 16) {
  968. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  969. << ext_inst_name() << ": "
  970. << "expected Result Type to be a scalar or a vector with 2, "
  971. "3, 4, 8 or 16 components";
  972. }
  973. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  974. if (result_type != x_type) {
  975. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  976. << ext_inst_name() << ": "
  977. << "expected type of operand X to be equal to Result Type";
  978. }
  979. const uint32_t exp_type = _.GetOperandTypeId(inst, 5);
  980. if (!_.IsIntScalarOrVectorType(exp_type) ||
  981. _.GetBitWidth(exp_type) != 32) {
  982. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  983. << ext_inst_name() << ": "
  984. << "expected the exponent to be a 32-bit int scalar or vector";
  985. }
  986. if (_.GetDimension(exp_type) != num_components) {
  987. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  988. << ext_inst_name() << ": "
  989. << "expected the exponent to have the same number of "
  990. "components as Result Type";
  991. }
  992. break;
  993. }
  994. case OpenCLLIB::Nan: {
  995. if (!_.IsFloatScalarOrVectorType(result_type)) {
  996. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  997. << ext_inst_name() << ": "
  998. << "expected Result Type to be a float scalar or vector type";
  999. }
  1000. const uint32_t num_components = _.GetDimension(result_type);
  1001. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1002. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1003. << ext_inst_name() << ": "
  1004. << "expected Result Type to be a scalar or a vector with 2, "
  1005. "3, 4, 8 or 16 components";
  1006. }
  1007. const uint32_t nancode_type = _.GetOperandTypeId(inst, 4);
  1008. if (!_.IsIntScalarOrVectorType(nancode_type)) {
  1009. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1010. << ext_inst_name() << ": "
  1011. << "expected Nancode to be an int scalar or vector type";
  1012. }
  1013. if (_.GetDimension(nancode_type) != num_components) {
  1014. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1015. << ext_inst_name() << ": "
  1016. << "expected Nancode to have the same number of components as "
  1017. "Result Type";
  1018. }
  1019. if (_.GetBitWidth(result_type) != _.GetBitWidth(nancode_type)) {
  1020. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1021. << ext_inst_name() << ": "
  1022. << "expected Nancode to have the same bit width as Result "
  1023. "Type";
  1024. }
  1025. break;
  1026. }
  1027. case OpenCLLIB::SAbs:
  1028. case OpenCLLIB::SAbs_diff:
  1029. case OpenCLLIB::SAdd_sat:
  1030. case OpenCLLIB::UAdd_sat:
  1031. case OpenCLLIB::SHadd:
  1032. case OpenCLLIB::UHadd:
  1033. case OpenCLLIB::SRhadd:
  1034. case OpenCLLIB::URhadd:
  1035. case OpenCLLIB::SClamp:
  1036. case OpenCLLIB::UClamp:
  1037. case OpenCLLIB::Clz:
  1038. case OpenCLLIB::Ctz:
  1039. case OpenCLLIB::SMad_hi:
  1040. case OpenCLLIB::UMad_sat:
  1041. case OpenCLLIB::SMad_sat:
  1042. case OpenCLLIB::SMax:
  1043. case OpenCLLIB::UMax:
  1044. case OpenCLLIB::SMin:
  1045. case OpenCLLIB::UMin:
  1046. case OpenCLLIB::SMul_hi:
  1047. case OpenCLLIB::Rotate:
  1048. case OpenCLLIB::SSub_sat:
  1049. case OpenCLLIB::USub_sat:
  1050. case OpenCLLIB::Popcount:
  1051. case OpenCLLIB::UAbs:
  1052. case OpenCLLIB::UAbs_diff:
  1053. case OpenCLLIB::UMul_hi:
  1054. case OpenCLLIB::UMad_hi: {
  1055. if (!_.IsIntScalarOrVectorType(result_type)) {
  1056. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1057. << ext_inst_name() << ": "
  1058. << "expected Result Type to be an int scalar or vector type";
  1059. }
  1060. const uint32_t num_components = _.GetDimension(result_type);
  1061. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1062. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1063. << ext_inst_name() << ": "
  1064. << "expected Result Type to be a scalar or a vector with 2, "
  1065. "3, 4, 8 or 16 components";
  1066. }
  1067. for (uint32_t operand_index = 4; operand_index < num_operands;
  1068. ++operand_index) {
  1069. const uint32_t operand_type = _.GetOperandTypeId(inst, operand_index);
  1070. if (result_type != operand_type) {
  1071. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1072. << ext_inst_name() << ": "
  1073. << "expected types of all operands to be equal to Result "
  1074. "Type";
  1075. }
  1076. }
  1077. break;
  1078. }
  1079. case OpenCLLIB::U_Upsample:
  1080. case OpenCLLIB::S_Upsample: {
  1081. if (!_.IsIntScalarOrVectorType(result_type)) {
  1082. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1083. << ext_inst_name() << ": "
  1084. << "expected Result Type to be an int scalar or vector "
  1085. "type";
  1086. }
  1087. const uint32_t result_num_components = _.GetDimension(result_type);
  1088. if (result_num_components > 4 && result_num_components != 8 &&
  1089. result_num_components != 16) {
  1090. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1091. << ext_inst_name() << ": "
  1092. << "expected Result Type to be a scalar or a vector with 2, "
  1093. "3, 4, 8 or 16 components";
  1094. }
  1095. const uint32_t result_bit_width = _.GetBitWidth(result_type);
  1096. if (result_bit_width != 16 && result_bit_width != 32 &&
  1097. result_bit_width != 64) {
  1098. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1099. << ext_inst_name() << ": "
  1100. << "expected bit width of Result Type components to be 16, 32 "
  1101. "or 64";
  1102. }
  1103. const uint32_t hi_type = _.GetOperandTypeId(inst, 4);
  1104. const uint32_t lo_type = _.GetOperandTypeId(inst, 5);
  1105. if (hi_type != lo_type) {
  1106. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1107. << ext_inst_name() << ": "
  1108. << "expected Hi and Lo operands to have the same type";
  1109. }
  1110. if (result_num_components != _.GetDimension(hi_type)) {
  1111. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1112. << ext_inst_name() << ": "
  1113. << "expected Hi and Lo operands to have the same number of "
  1114. "components as Result Type";
  1115. }
  1116. if (result_bit_width != 2 * _.GetBitWidth(hi_type)) {
  1117. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1118. << ext_inst_name() << ": "
  1119. << "expected bit width of components of Hi and Lo operands to "
  1120. "be half of the bit width of components of Result Type";
  1121. }
  1122. break;
  1123. }
  1124. case OpenCLLIB::SMad24:
  1125. case OpenCLLIB::UMad24:
  1126. case OpenCLLIB::SMul24:
  1127. case OpenCLLIB::UMul24: {
  1128. if (!_.IsIntScalarOrVectorType(result_type) ||
  1129. _.GetBitWidth(result_type) != 32) {
  1130. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1131. << ext_inst_name() << ": "
  1132. << "expected Result Type to be a 32-bit int scalar or vector "
  1133. "type";
  1134. }
  1135. const uint32_t num_components = _.GetDimension(result_type);
  1136. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1137. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1138. << ext_inst_name() << ": "
  1139. << "expected Result Type to be a scalar or a vector with 2, "
  1140. "3, 4, 8 or 16 components";
  1141. }
  1142. for (uint32_t operand_index = 4; operand_index < num_operands;
  1143. ++operand_index) {
  1144. const uint32_t operand_type = _.GetOperandTypeId(inst, operand_index);
  1145. if (result_type != operand_type) {
  1146. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1147. << ext_inst_name() << ": "
  1148. << "expected types of all operands to be equal to Result "
  1149. "Type";
  1150. }
  1151. }
  1152. break;
  1153. }
  1154. case OpenCLLIB::Cross: {
  1155. if (!_.IsFloatVectorType(result_type)) {
  1156. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1157. << ext_inst_name() << ": "
  1158. << "expected Result Type to be a float vector type";
  1159. }
  1160. const uint32_t num_components = _.GetDimension(result_type);
  1161. if (num_components != 3 && num_components != 4) {
  1162. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1163. << ext_inst_name() << ": "
  1164. << "expected Result Type to have 3 or 4 components";
  1165. }
  1166. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  1167. const uint32_t y_type = _.GetOperandTypeId(inst, 5);
  1168. if (x_type != result_type) {
  1169. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1170. << ext_inst_name() << ": "
  1171. << "expected operand X type to be equal to Result Type";
  1172. }
  1173. if (y_type != result_type) {
  1174. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1175. << ext_inst_name() << ": "
  1176. << "expected operand Y type to be equal to Result Type";
  1177. }
  1178. break;
  1179. }
  1180. case OpenCLLIB::Distance:
  1181. case OpenCLLIB::Fast_distance: {
  1182. if (!_.IsFloatScalarType(result_type)) {
  1183. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1184. << ext_inst_name() << ": "
  1185. << "expected Result Type to be a float scalar type";
  1186. }
  1187. const uint32_t p0_type = _.GetOperandTypeId(inst, 4);
  1188. if (!_.IsFloatScalarOrVectorType(p0_type)) {
  1189. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1190. << ext_inst_name() << ": "
  1191. << "expected operand P0 to be of float scalar or vector type";
  1192. }
  1193. const uint32_t num_components = _.GetDimension(p0_type);
  1194. if (num_components > 4) {
  1195. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1196. << ext_inst_name() << ": "
  1197. << "expected operand P0 to have no more than 4 components";
  1198. }
  1199. if (result_type != _.GetComponentType(p0_type)) {
  1200. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1201. << ext_inst_name() << ": "
  1202. << "expected operand P0 component type to be equal to "
  1203. << "Result Type";
  1204. }
  1205. const uint32_t p1_type = _.GetOperandTypeId(inst, 5);
  1206. if (p0_type != p1_type) {
  1207. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1208. << ext_inst_name() << ": "
  1209. << "expected operands P0 and P1 to be of the same type";
  1210. }
  1211. break;
  1212. }
  1213. case OpenCLLIB::Length:
  1214. case OpenCLLIB::Fast_length: {
  1215. if (!_.IsFloatScalarType(result_type)) {
  1216. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1217. << ext_inst_name() << ": "
  1218. << "expected Result Type to be a float scalar type";
  1219. }
  1220. const uint32_t p_type = _.GetOperandTypeId(inst, 4);
  1221. if (!_.IsFloatScalarOrVectorType(p_type)) {
  1222. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1223. << ext_inst_name() << ": "
  1224. << "expected operand P to be a float scalar or vector";
  1225. }
  1226. const uint32_t num_components = _.GetDimension(p_type);
  1227. if (num_components > 4) {
  1228. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1229. << ext_inst_name() << ": "
  1230. << "expected operand P to have no more than 4 components";
  1231. }
  1232. if (result_type != _.GetComponentType(p_type)) {
  1233. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1234. << ext_inst_name() << ": "
  1235. << "expected operand P component type to be equal to Result "
  1236. "Type";
  1237. }
  1238. break;
  1239. }
  1240. case OpenCLLIB::Normalize:
  1241. case OpenCLLIB::Fast_normalize: {
  1242. if (!_.IsFloatScalarOrVectorType(result_type)) {
  1243. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1244. << ext_inst_name() << ": "
  1245. << "expected Result Type to be a float scalar or vector type";
  1246. }
  1247. const uint32_t num_components = _.GetDimension(result_type);
  1248. if (num_components > 4) {
  1249. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1250. << ext_inst_name() << ": "
  1251. << "expected Result Type to have no more than 4 components";
  1252. }
  1253. const uint32_t p_type = _.GetOperandTypeId(inst, 4);
  1254. if (p_type != result_type) {
  1255. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1256. << ext_inst_name() << ": "
  1257. << "expected operand P type to be equal to Result Type";
  1258. }
  1259. break;
  1260. }
  1261. case OpenCLLIB::Bitselect: {
  1262. if (!_.IsFloatScalarOrVectorType(result_type) &&
  1263. !_.IsIntScalarOrVectorType(result_type)) {
  1264. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1265. << ext_inst_name() << ": "
  1266. << "expected Result Type to be an int or float scalar or "
  1267. "vector type";
  1268. }
  1269. const uint32_t num_components = _.GetDimension(result_type);
  1270. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1271. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1272. << ext_inst_name() << ": "
  1273. << "expected Result Type to be a scalar or a vector with 2, "
  1274. "3, 4, 8 or 16 components";
  1275. }
  1276. for (uint32_t operand_index = 4; operand_index < num_operands;
  1277. ++operand_index) {
  1278. const uint32_t operand_type = _.GetOperandTypeId(inst, operand_index);
  1279. if (result_type != operand_type) {
  1280. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1281. << ext_inst_name() << ": "
  1282. << "expected types of all operands to be equal to Result "
  1283. "Type";
  1284. }
  1285. }
  1286. break;
  1287. }
  1288. case OpenCLLIB::Select: {
  1289. if (!_.IsFloatScalarOrVectorType(result_type) &&
  1290. !_.IsIntScalarOrVectorType(result_type)) {
  1291. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1292. << ext_inst_name() << ": "
  1293. << "expected Result Type to be an int or float scalar or "
  1294. "vector type";
  1295. }
  1296. const uint32_t num_components = _.GetDimension(result_type);
  1297. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1298. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1299. << ext_inst_name() << ": "
  1300. << "expected Result Type to be a scalar or a vector with 2, "
  1301. "3, 4, 8 or 16 components";
  1302. }
  1303. const uint32_t a_type = _.GetOperandTypeId(inst, 4);
  1304. const uint32_t b_type = _.GetOperandTypeId(inst, 5);
  1305. const uint32_t c_type = _.GetOperandTypeId(inst, 6);
  1306. if (result_type != a_type) {
  1307. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1308. << ext_inst_name() << ": "
  1309. << "expected operand A type to be equal to Result Type";
  1310. }
  1311. if (result_type != b_type) {
  1312. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1313. << ext_inst_name() << ": "
  1314. << "expected operand B type to be equal to Result Type";
  1315. }
  1316. if (!_.IsIntScalarOrVectorType(c_type)) {
  1317. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1318. << ext_inst_name() << ": "
  1319. << "expected operand C to be an int scalar or vector";
  1320. }
  1321. if (num_components != _.GetDimension(c_type)) {
  1322. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1323. << ext_inst_name() << ": "
  1324. << "expected operand C to have the same number of components "
  1325. "as Result Type";
  1326. }
  1327. if (_.GetBitWidth(result_type) != _.GetBitWidth(c_type)) {
  1328. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1329. << ext_inst_name() << ": "
  1330. << "expected operand C to have the same bit width as Result "
  1331. "Type";
  1332. }
  1333. break;
  1334. }
  1335. case OpenCLLIB::Vloadn: {
  1336. if (!_.IsFloatVectorType(result_type) &&
  1337. !_.IsIntVectorType(result_type)) {
  1338. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1339. << ext_inst_name() << ": "
  1340. << "expected Result Type to be an int or float vector type";
  1341. }
  1342. const uint32_t num_components = _.GetDimension(result_type);
  1343. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1344. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1345. << ext_inst_name() << ": "
  1346. << "expected Result Type to have 2, 3, 4, 8 or 16 components";
  1347. }
  1348. const uint32_t offset_type = _.GetOperandTypeId(inst, 4);
  1349. const uint32_t p_type = _.GetOperandTypeId(inst, 5);
  1350. const uint32_t size_t_bit_width = GetSizeTBitWidth(_);
  1351. if (!size_t_bit_width) {
  1352. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1353. << ext_inst_name()
  1354. << " can only be used with physical addressing models";
  1355. }
  1356. if (!_.IsIntScalarType(offset_type) ||
  1357. _.GetBitWidth(offset_type) != size_t_bit_width) {
  1358. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1359. << ext_inst_name() << ": "
  1360. << "expected operand Offset to be of type size_t ("
  1361. << size_t_bit_width
  1362. << "-bit integer for the addressing model used in the module)";
  1363. }
  1364. uint32_t p_storage_class = 0;
  1365. uint32_t p_data_type = 0;
  1366. if (!_.GetPointerTypeInfo(p_type, &p_data_type, &p_storage_class)) {
  1367. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1368. << ext_inst_name() << ": "
  1369. << "expected operand P to be a pointer";
  1370. }
  1371. if (p_storage_class != SpvStorageClassUniformConstant &&
  1372. p_storage_class != SpvStorageClassGeneric) {
  1373. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1374. << ext_inst_name() << ": "
  1375. << "expected operand P storage class to be UniformConstant or "
  1376. "Generic";
  1377. }
  1378. if (_.GetComponentType(result_type) != p_data_type) {
  1379. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1380. << ext_inst_name() << ": "
  1381. << "expected operand P data type to be equal to component "
  1382. "type of Result Type";
  1383. }
  1384. const uint32_t n_value = inst->word(7);
  1385. if (num_components != n_value) {
  1386. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1387. << ext_inst_name() << ": "
  1388. << "expected literal N to be equal to the number of "
  1389. "components of Result Type";
  1390. }
  1391. break;
  1392. }
  1393. case OpenCLLIB::Vstoren: {
  1394. if (_.GetIdOpcode(result_type) != SpvOpTypeVoid) {
  1395. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1396. << ext_inst_name() << ": expected Result Type to be void";
  1397. }
  1398. const uint32_t data_type = _.GetOperandTypeId(inst, 4);
  1399. const uint32_t offset_type = _.GetOperandTypeId(inst, 5);
  1400. const uint32_t p_type = _.GetOperandTypeId(inst, 6);
  1401. if (!_.IsFloatVectorType(data_type) && !_.IsIntVectorType(data_type)) {
  1402. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1403. << ext_inst_name() << ": "
  1404. << "expected Data to be an int or float vector";
  1405. }
  1406. const uint32_t num_components = _.GetDimension(data_type);
  1407. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1408. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1409. << ext_inst_name() << ": "
  1410. << "expected Data to have 2, 3, 4, 8 or 16 components";
  1411. }
  1412. const uint32_t size_t_bit_width = GetSizeTBitWidth(_);
  1413. if (!size_t_bit_width) {
  1414. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1415. << ext_inst_name()
  1416. << " can only be used with physical addressing models";
  1417. }
  1418. if (!_.IsIntScalarType(offset_type) ||
  1419. _.GetBitWidth(offset_type) != size_t_bit_width) {
  1420. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1421. << ext_inst_name() << ": "
  1422. << "expected operand Offset to be of type size_t ("
  1423. << size_t_bit_width
  1424. << "-bit integer for the addressing model used in the module)";
  1425. }
  1426. uint32_t p_storage_class = 0;
  1427. uint32_t p_data_type = 0;
  1428. if (!_.GetPointerTypeInfo(p_type, &p_data_type, &p_storage_class)) {
  1429. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1430. << ext_inst_name() << ": "
  1431. << "expected operand P to be a pointer";
  1432. }
  1433. if (p_storage_class != SpvStorageClassGeneric) {
  1434. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1435. << ext_inst_name() << ": "
  1436. << "expected operand P storage class to be Generic";
  1437. }
  1438. if (_.GetComponentType(data_type) != p_data_type) {
  1439. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1440. << ext_inst_name() << ": "
  1441. << "expected operand P data type to be equal to the type of "
  1442. "operand Data components";
  1443. }
  1444. break;
  1445. }
  1446. case OpenCLLIB::Vload_half: {
  1447. if (!_.IsFloatScalarType(result_type)) {
  1448. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1449. << ext_inst_name() << ": "
  1450. << "expected Result Type to be a float scalar type";
  1451. }
  1452. const uint32_t offset_type = _.GetOperandTypeId(inst, 4);
  1453. const uint32_t p_type = _.GetOperandTypeId(inst, 5);
  1454. const uint32_t size_t_bit_width = GetSizeTBitWidth(_);
  1455. if (!size_t_bit_width) {
  1456. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1457. << ext_inst_name()
  1458. << " can only be used with physical addressing models";
  1459. }
  1460. if (!_.IsIntScalarType(offset_type) ||
  1461. _.GetBitWidth(offset_type) != size_t_bit_width) {
  1462. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1463. << ext_inst_name() << ": "
  1464. << "expected operand Offset to be of type size_t ("
  1465. << size_t_bit_width
  1466. << "-bit integer for the addressing model used in the module)";
  1467. }
  1468. uint32_t p_storage_class = 0;
  1469. uint32_t p_data_type = 0;
  1470. if (!_.GetPointerTypeInfo(p_type, &p_data_type, &p_storage_class)) {
  1471. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1472. << ext_inst_name() << ": "
  1473. << "expected operand P to be a pointer";
  1474. }
  1475. if (p_storage_class != SpvStorageClassUniformConstant &&
  1476. p_storage_class != SpvStorageClassGeneric &&
  1477. p_storage_class != SpvStorageClassCrossWorkgroup &&
  1478. p_storage_class != SpvStorageClassWorkgroup &&
  1479. p_storage_class != SpvStorageClassFunction) {
  1480. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1481. << ext_inst_name() << ": "
  1482. << "expected operand P storage class to be UniformConstant, "
  1483. "Generic, CrossWorkgroup, Workgroup or Function";
  1484. }
  1485. if (!_.IsFloatScalarType(p_data_type) ||
  1486. _.GetBitWidth(p_data_type) != 16) {
  1487. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1488. << ext_inst_name() << ": "
  1489. << "expected operand P data type to be 16-bit float scalar";
  1490. }
  1491. break;
  1492. }
  1493. case OpenCLLIB::Vload_halfn:
  1494. case OpenCLLIB::Vloada_halfn: {
  1495. if (!_.IsFloatVectorType(result_type)) {
  1496. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1497. << ext_inst_name() << ": "
  1498. << "expected Result Type to be a float vector type";
  1499. }
  1500. const uint32_t num_components = _.GetDimension(result_type);
  1501. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1502. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1503. << ext_inst_name() << ": "
  1504. << "expected Result Type to have 2, 3, 4, 8 or 16 components";
  1505. }
  1506. const uint32_t offset_type = _.GetOperandTypeId(inst, 4);
  1507. const uint32_t p_type = _.GetOperandTypeId(inst, 5);
  1508. const uint32_t size_t_bit_width = GetSizeTBitWidth(_);
  1509. if (!size_t_bit_width) {
  1510. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1511. << ext_inst_name()
  1512. << " can only be used with physical addressing models";
  1513. }
  1514. if (!_.IsIntScalarType(offset_type) ||
  1515. _.GetBitWidth(offset_type) != size_t_bit_width) {
  1516. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1517. << ext_inst_name() << ": "
  1518. << "expected operand Offset to be of type size_t ("
  1519. << size_t_bit_width
  1520. << "-bit integer for the addressing model used in the module)";
  1521. }
  1522. uint32_t p_storage_class = 0;
  1523. uint32_t p_data_type = 0;
  1524. if (!_.GetPointerTypeInfo(p_type, &p_data_type, &p_storage_class)) {
  1525. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1526. << ext_inst_name() << ": "
  1527. << "expected operand P to be a pointer";
  1528. }
  1529. if (p_storage_class != SpvStorageClassUniformConstant &&
  1530. p_storage_class != SpvStorageClassGeneric &&
  1531. p_storage_class != SpvStorageClassCrossWorkgroup &&
  1532. p_storage_class != SpvStorageClassWorkgroup &&
  1533. p_storage_class != SpvStorageClassFunction) {
  1534. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1535. << ext_inst_name() << ": "
  1536. << "expected operand P storage class to be UniformConstant, "
  1537. "Generic, CrossWorkgroup, Workgroup or Function";
  1538. }
  1539. if (!_.IsFloatScalarType(p_data_type) ||
  1540. _.GetBitWidth(p_data_type) != 16) {
  1541. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1542. << ext_inst_name() << ": "
  1543. << "expected operand P data type to be 16-bit float scalar";
  1544. }
  1545. const uint32_t n_value = inst->word(7);
  1546. if (num_components != n_value) {
  1547. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1548. << ext_inst_name() << ": "
  1549. << "expected literal N to be equal to the number of "
  1550. "components of Result Type";
  1551. }
  1552. break;
  1553. }
  1554. case OpenCLLIB::Vstore_half:
  1555. case OpenCLLIB::Vstore_half_r:
  1556. case OpenCLLIB::Vstore_halfn:
  1557. case OpenCLLIB::Vstore_halfn_r:
  1558. case OpenCLLIB::Vstorea_halfn:
  1559. case OpenCLLIB::Vstorea_halfn_r: {
  1560. if (_.GetIdOpcode(result_type) != SpvOpTypeVoid) {
  1561. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1562. << ext_inst_name() << ": expected Result Type to be void";
  1563. }
  1564. const uint32_t data_type = _.GetOperandTypeId(inst, 4);
  1565. const uint32_t offset_type = _.GetOperandTypeId(inst, 5);
  1566. const uint32_t p_type = _.GetOperandTypeId(inst, 6);
  1567. const uint32_t data_type_bit_width = _.GetBitWidth(data_type);
  1568. if (ext_inst_key == OpenCLLIB::Vstore_half ||
  1569. ext_inst_key == OpenCLLIB::Vstore_half_r) {
  1570. if (!_.IsFloatScalarType(data_type) ||
  1571. (data_type_bit_width != 32 && data_type_bit_width != 64)) {
  1572. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1573. << ext_inst_name() << ": "
  1574. << "expected Data to be a 32 or 64-bit float scalar";
  1575. }
  1576. } else {
  1577. if (!_.IsFloatVectorType(data_type) ||
  1578. (data_type_bit_width != 32 && data_type_bit_width != 64)) {
  1579. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1580. << ext_inst_name() << ": "
  1581. << "expected Data to be a 32 or 64-bit float vector";
  1582. }
  1583. const uint32_t num_components = _.GetDimension(data_type);
  1584. if (num_components > 4 && num_components != 8 &&
  1585. num_components != 16) {
  1586. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1587. << ext_inst_name() << ": "
  1588. << "expected Data to have 2, 3, 4, 8 or 16 components";
  1589. }
  1590. }
  1591. const uint32_t size_t_bit_width = GetSizeTBitWidth(_);
  1592. if (!size_t_bit_width) {
  1593. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1594. << ext_inst_name()
  1595. << " can only be used with physical addressing models";
  1596. }
  1597. if (!_.IsIntScalarType(offset_type) ||
  1598. _.GetBitWidth(offset_type) != size_t_bit_width) {
  1599. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1600. << ext_inst_name() << ": "
  1601. << "expected operand Offset to be of type size_t ("
  1602. << size_t_bit_width
  1603. << "-bit integer for the addressing model used in the module)";
  1604. }
  1605. uint32_t p_storage_class = 0;
  1606. uint32_t p_data_type = 0;
  1607. if (!_.GetPointerTypeInfo(p_type, &p_data_type, &p_storage_class)) {
  1608. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1609. << ext_inst_name() << ": "
  1610. << "expected operand P to be a pointer";
  1611. }
  1612. if (p_storage_class != SpvStorageClassGeneric &&
  1613. p_storage_class != SpvStorageClassCrossWorkgroup &&
  1614. p_storage_class != SpvStorageClassWorkgroup &&
  1615. p_storage_class != SpvStorageClassFunction) {
  1616. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1617. << ext_inst_name() << ": "
  1618. << "expected operand P storage class to be Generic, "
  1619. "CrossWorkgroup, Workgroup or Function";
  1620. }
  1621. if (!_.IsFloatScalarType(p_data_type) ||
  1622. _.GetBitWidth(p_data_type) != 16) {
  1623. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1624. << ext_inst_name() << ": "
  1625. << "expected operand P data type to be 16-bit float scalar";
  1626. }
  1627. // Rounding mode enum is checked by assembler.
  1628. break;
  1629. }
  1630. case OpenCLLIB::Shuffle:
  1631. case OpenCLLIB::Shuffle2: {
  1632. if (!_.IsFloatVectorType(result_type) &&
  1633. !_.IsIntVectorType(result_type)) {
  1634. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1635. << ext_inst_name() << ": "
  1636. << "expected Result Type to be an int or float vector type";
  1637. }
  1638. const uint32_t result_num_components = _.GetDimension(result_type);
  1639. if (result_num_components != 2 && result_num_components != 4 &&
  1640. result_num_components != 8 && result_num_components != 16) {
  1641. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1642. << ext_inst_name() << ": "
  1643. << "expected Result Type to have 2, 4, 8 or 16 components";
  1644. }
  1645. uint32_t operand_index = 4;
  1646. const uint32_t x_type = _.GetOperandTypeId(inst, operand_index++);
  1647. if (ext_inst_key == OpenCLLIB::Shuffle2) {
  1648. const uint32_t y_type = _.GetOperandTypeId(inst, operand_index++);
  1649. if (x_type != y_type) {
  1650. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1651. << ext_inst_name() << ": "
  1652. << "expected operands X and Y to be of the same type";
  1653. }
  1654. }
  1655. const uint32_t shuffle_mask_type =
  1656. _.GetOperandTypeId(inst, operand_index++);
  1657. if (!_.IsFloatVectorType(x_type) && !_.IsIntVectorType(x_type)) {
  1658. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1659. << ext_inst_name() << ": "
  1660. << "expected operand X to be an int or float vector";
  1661. }
  1662. const uint32_t x_num_components = _.GetDimension(x_type);
  1663. if (x_num_components != 2 && x_num_components != 4 &&
  1664. x_num_components != 8 && x_num_components != 16) {
  1665. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1666. << ext_inst_name() << ": "
  1667. << "expected operand X to have 2, 4, 8 or 16 components";
  1668. }
  1669. const uint32_t result_component_type = _.GetComponentType(result_type);
  1670. if (result_component_type != _.GetComponentType(x_type)) {
  1671. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1672. << ext_inst_name() << ": "
  1673. << "expected operand X and Result Type to have equal "
  1674. "component types";
  1675. }
  1676. if (!_.IsIntVectorType(shuffle_mask_type)) {
  1677. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1678. << ext_inst_name() << ": "
  1679. << "expected operand Shuffle Mask to be an int vector";
  1680. }
  1681. if (result_num_components != _.GetDimension(shuffle_mask_type)) {
  1682. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1683. << ext_inst_name() << ": "
  1684. << "expected operand Shuffle Mask to have the same number of "
  1685. "components as Result Type";
  1686. }
  1687. if (_.GetBitWidth(result_component_type) !=
  1688. _.GetBitWidth(shuffle_mask_type)) {
  1689. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1690. << ext_inst_name() << ": "
  1691. << "expected operand Shuffle Mask components to have the same "
  1692. "bit width as Result Type components";
  1693. }
  1694. break;
  1695. }
  1696. case OpenCLLIB::Printf: {
  1697. if (!_.IsIntScalarType(result_type) ||
  1698. _.GetBitWidth(result_type) != 32) {
  1699. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1700. << ext_inst_name() << ": "
  1701. << "expected Result Type to be a 32-bit int type";
  1702. }
  1703. const uint32_t format_type = _.GetOperandTypeId(inst, 4);
  1704. uint32_t format_storage_class = 0;
  1705. uint32_t format_data_type = 0;
  1706. if (!_.GetPointerTypeInfo(format_type, &format_data_type,
  1707. &format_storage_class)) {
  1708. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1709. << ext_inst_name() << ": "
  1710. << "expected operand Format to be a pointer";
  1711. }
  1712. if (format_storage_class != SpvStorageClassUniformConstant) {
  1713. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1714. << ext_inst_name() << ": "
  1715. << "expected Format storage class to be UniformConstant";
  1716. }
  1717. if (!_.IsIntScalarType(format_data_type) ||
  1718. _.GetBitWidth(format_data_type) != 8) {
  1719. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1720. << ext_inst_name() << ": "
  1721. << "expected Format data type to be 8-bit int";
  1722. }
  1723. break;
  1724. }
  1725. case OpenCLLIB::Prefetch: {
  1726. if (_.GetIdOpcode(result_type) != SpvOpTypeVoid) {
  1727. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1728. << ext_inst_name() << ": expected Result Type to be void";
  1729. }
  1730. const uint32_t p_type = _.GetOperandTypeId(inst, 4);
  1731. const uint32_t num_elements_type = _.GetOperandTypeId(inst, 5);
  1732. uint32_t p_storage_class = 0;
  1733. uint32_t p_data_type = 0;
  1734. if (!_.GetPointerTypeInfo(p_type, &p_data_type, &p_storage_class)) {
  1735. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1736. << ext_inst_name() << ": "
  1737. << "expected operand Ptr to be a pointer";
  1738. }
  1739. if (p_storage_class != SpvStorageClassCrossWorkgroup) {
  1740. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1741. << ext_inst_name() << ": "
  1742. << "expected operand Ptr storage class to be CrossWorkgroup";
  1743. }
  1744. if (!_.IsFloatScalarOrVectorType(p_data_type) &&
  1745. !_.IsIntScalarOrVectorType(p_data_type)) {
  1746. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1747. << ext_inst_name() << ": "
  1748. << "expected Ptr data type to be int or float scalar or "
  1749. "vector";
  1750. }
  1751. const uint32_t num_components = _.GetDimension(p_data_type);
  1752. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1753. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1754. << ext_inst_name() << ": "
  1755. << "expected Result Type to be a scalar or a vector with 2, "
  1756. "3, 4, 8 or 16 components";
  1757. }
  1758. const uint32_t size_t_bit_width = GetSizeTBitWidth(_);
  1759. if (!size_t_bit_width) {
  1760. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1761. << ext_inst_name()
  1762. << " can only be used with physical addressing models";
  1763. }
  1764. if (!_.IsIntScalarType(num_elements_type) ||
  1765. _.GetBitWidth(num_elements_type) != size_t_bit_width) {
  1766. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1767. << ext_inst_name() << ": "
  1768. << "expected operand Num Elements to be of type size_t ("
  1769. << size_t_bit_width
  1770. << "-bit integer for the addressing model used in the module)";
  1771. }
  1772. break;
  1773. }
  1774. }
  1775. }
  1776. return SPV_SUCCESS;
  1777. }
  1778. spv_result_t ExtensionPass(ValidationState_t& _, const Instruction* inst) {
  1779. const SpvOp opcode = inst->opcode();
  1780. if (opcode == SpvOpExtension) return ValidateExtension(_, inst);
  1781. if (opcode == SpvOpExtInstImport) return ValidateExtInstImport(_, inst);
  1782. if (opcode == SpvOpExtInst) return ValidateExtInst(_, inst);
  1783. return SPV_SUCCESS;
  1784. }
  1785. } // namespace val
  1786. } // namespace spvtools