validate_ext_inst.cpp 74 KB

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