validate_extensions.cpp 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502
  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 <sstream>
  16. #include <string>
  17. #include <vector>
  18. #include "OpenCLDebugInfo100.h"
  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/validate.h"
  28. #include "source/val/validation_state.h"
  29. namespace spvtools {
  30. namespace val {
  31. namespace {
  32. uint32_t GetSizeTBitWidth(const ValidationState_t& _) {
  33. if (_.addressing_model() == SpvAddressingModelPhysical32) return 32;
  34. if (_.addressing_model() == SpvAddressingModelPhysical64) return 64;
  35. return 0;
  36. }
  37. // Check that the operand of a debug info instruction |inst| at |word_index|
  38. // is a result id of an instruction with |expected_opcode|.
  39. spv_result_t ValidateOperandForDebugInfo(
  40. ValidationState_t& _, const std::string& operand_name,
  41. SpvOp expected_opcode, const Instruction* inst, uint32_t word_index,
  42. const std::function<std::string()>& ext_inst_name) {
  43. auto* operand = _.FindDef(inst->word(word_index));
  44. if (operand->opcode() != expected_opcode) {
  45. spv_opcode_desc desc = nullptr;
  46. if (_.grammar().lookupOpcode(expected_opcode, &desc) != SPV_SUCCESS ||
  47. !desc) {
  48. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  49. << ext_inst_name() << ": "
  50. << "expected operand " << operand_name << " is invalid";
  51. }
  52. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  53. << ext_inst_name() << ": "
  54. << "expected operand " << operand_name << " must be a result id of "
  55. << "Op" << desc->name;
  56. }
  57. return SPV_SUCCESS;
  58. }
  59. #define CHECK_OPERAND(NAME, opcode, index) \
  60. do { \
  61. auto result = ValidateOperandForDebugInfo(_, NAME, opcode, inst, index, \
  62. ext_inst_name); \
  63. if (result != SPV_SUCCESS) return result; \
  64. } while (0)
  65. // True if the operand of a debug info instruction |inst| at |word_index|
  66. // satisifies |expectation| that is given as a function. Otherwise,
  67. // returns false.
  68. bool DoesDebugInfoOperandMatchExpectation(
  69. const ValidationState_t& _,
  70. const std::function<bool(OpenCLDebugInfo100Instructions)>& expectation,
  71. const Instruction* inst, uint32_t word_index) {
  72. auto* debug_inst = _.FindDef(inst->word(word_index));
  73. if (debug_inst->opcode() != SpvOpExtInst ||
  74. debug_inst->ext_inst_type() != SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100 ||
  75. !expectation(OpenCLDebugInfo100Instructions(debug_inst->word(4)))) {
  76. return false;
  77. }
  78. return true;
  79. }
  80. // Check that the operand of a debug info instruction |inst| at |word_index|
  81. // is a result id of an debug info instruction whose debug instruction type
  82. // is |expected_debug_inst|.
  83. spv_result_t ValidateDebugInfoOperand(
  84. ValidationState_t& _, const std::string& debug_inst_name,
  85. OpenCLDebugInfo100Instructions expected_debug_inst, const Instruction* inst,
  86. uint32_t word_index, const std::function<std::string()>& ext_inst_name) {
  87. std::function<bool(OpenCLDebugInfo100Instructions)> expectation =
  88. [expected_debug_inst](OpenCLDebugInfo100Instructions dbg_inst) {
  89. return dbg_inst == expected_debug_inst;
  90. };
  91. if (DoesDebugInfoOperandMatchExpectation(_, expectation, inst, word_index))
  92. return SPV_SUCCESS;
  93. spv_ext_inst_desc desc = nullptr;
  94. _.grammar().lookupExtInst(SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100,
  95. expected_debug_inst, &desc);
  96. if (_.grammar().lookupExtInst(SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100,
  97. expected_debug_inst, &desc) != SPV_SUCCESS ||
  98. !desc) {
  99. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  100. << ext_inst_name() << ": "
  101. << "expected operand " << debug_inst_name << " is invalid";
  102. }
  103. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  104. << ext_inst_name() << ": "
  105. << "expected operand " << debug_inst_name << " must be a result id of "
  106. << desc->name;
  107. }
  108. #define CHECK_DEBUG_OPERAND(NAME, debug_opcode, index) \
  109. do { \
  110. auto result = ValidateDebugInfoOperand(_, NAME, debug_opcode, inst, index, \
  111. ext_inst_name); \
  112. if (result != SPV_SUCCESS) return result; \
  113. } while (0)
  114. // Check that the operand of a debug info instruction |inst| at |word_index|
  115. // is a result id of an debug info instruction with DebugTypeBasic.
  116. spv_result_t ValidateOperandBaseType(
  117. ValidationState_t& _, const Instruction* inst, uint32_t word_index,
  118. const std::function<std::string()>& ext_inst_name) {
  119. return ValidateDebugInfoOperand(_, "Base Type",
  120. OpenCLDebugInfo100DebugTypeBasic, inst,
  121. word_index, ext_inst_name);
  122. }
  123. // Check that the operand of a debug info instruction |inst| at |word_index|
  124. // is a result id of a debug lexical scope instruction which is one of
  125. // DebugCompilationUnit, DebugFunction, DebugLexicalBlock, or
  126. // DebugTypeComposite.
  127. spv_result_t ValidateOperandLexicalScope(
  128. ValidationState_t& _, const std::string& debug_inst_name,
  129. const Instruction* inst, uint32_t word_index,
  130. const std::function<std::string()>& ext_inst_name) {
  131. std::function<bool(OpenCLDebugInfo100Instructions)> expectation =
  132. [](OpenCLDebugInfo100Instructions dbg_inst) {
  133. return dbg_inst == OpenCLDebugInfo100DebugCompilationUnit ||
  134. dbg_inst == OpenCLDebugInfo100DebugFunction ||
  135. dbg_inst == OpenCLDebugInfo100DebugLexicalBlock ||
  136. dbg_inst == OpenCLDebugInfo100DebugTypeComposite;
  137. };
  138. if (DoesDebugInfoOperandMatchExpectation(_, expectation, inst, word_index))
  139. return SPV_SUCCESS;
  140. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  141. << ext_inst_name() << ": "
  142. << "expected operand " << debug_inst_name
  143. << " must be a result id of a lexical scope";
  144. }
  145. // Check that the operand of a debug info instruction |inst| at |word_index|
  146. // is a result id of a debug type instruction (See DebugTypeXXX in
  147. // "4.3. Type instructions" section of OpenCL.DebugInfo.100 spec.
  148. spv_result_t ValidateOperandDebugType(
  149. ValidationState_t& _, const std::string& debug_inst_name,
  150. const Instruction* inst, uint32_t word_index,
  151. const std::function<std::string()>& ext_inst_name) {
  152. std::function<bool(OpenCLDebugInfo100Instructions)> expectation =
  153. [](OpenCLDebugInfo100Instructions dbg_inst) {
  154. return OpenCLDebugInfo100DebugTypeBasic <= dbg_inst &&
  155. dbg_inst <= OpenCLDebugInfo100DebugTypePtrToMember;
  156. };
  157. if (DoesDebugInfoOperandMatchExpectation(_, expectation, inst, word_index))
  158. return SPV_SUCCESS;
  159. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  160. << ext_inst_name() << ": "
  161. << "expected operand " << debug_inst_name
  162. << " is not a valid debug type";
  163. }
  164. } // anonymous namespace
  165. spv_result_t ValidateExtension(ValidationState_t& _, const Instruction* inst) {
  166. if (spvIsWebGPUEnv(_.context()->target_env)) {
  167. std::string extension = GetExtensionString(&(inst->c_inst()));
  168. if (extension != ExtensionToString(kSPV_KHR_vulkan_memory_model)) {
  169. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  170. << "For WebGPU, the only valid parameter to OpExtension is "
  171. << "\"" << ExtensionToString(kSPV_KHR_vulkan_memory_model)
  172. << "\".";
  173. }
  174. }
  175. return SPV_SUCCESS;
  176. }
  177. spv_result_t ValidateExtInstImport(ValidationState_t& _,
  178. const Instruction* inst) {
  179. const auto name_id = 1;
  180. if (spvIsWebGPUEnv(_.context()->target_env)) {
  181. const std::string name(reinterpret_cast<const char*>(
  182. inst->words().data() + inst->operands()[name_id].offset));
  183. if (name != "GLSL.std.450") {
  184. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  185. << "For WebGPU, the only valid parameter to OpExtInstImport is "
  186. "\"GLSL.std.450\".";
  187. }
  188. }
  189. if (!_.HasExtension(kSPV_KHR_non_semantic_info)) {
  190. const std::string name(reinterpret_cast<const char*>(
  191. inst->words().data() + inst->operands()[name_id].offset));
  192. if (name.find("NonSemantic.") == 0) {
  193. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  194. << "NonSemantic extended instruction sets cannot be declared "
  195. "without SPV_KHR_non_semantic_info.";
  196. }
  197. }
  198. return SPV_SUCCESS;
  199. }
  200. spv_result_t ValidateExtInst(ValidationState_t& _, const Instruction* inst) {
  201. const uint32_t result_type = inst->type_id();
  202. const uint32_t num_operands = static_cast<uint32_t>(inst->operands().size());
  203. const uint32_t ext_inst_set = inst->word(3);
  204. const uint32_t ext_inst_index = inst->word(4);
  205. const spv_ext_inst_type_t ext_inst_type =
  206. spv_ext_inst_type_t(inst->ext_inst_type());
  207. auto ext_inst_name = [&_, ext_inst_set, ext_inst_type, ext_inst_index]() {
  208. spv_ext_inst_desc desc = nullptr;
  209. if (_.grammar().lookupExtInst(ext_inst_type, ext_inst_index, &desc) !=
  210. SPV_SUCCESS ||
  211. !desc) {
  212. return std::string("Unknown ExtInst");
  213. }
  214. auto* import_inst = _.FindDef(ext_inst_set);
  215. assert(import_inst);
  216. std::ostringstream ss;
  217. ss << reinterpret_cast<const char*>(import_inst->words().data() + 2);
  218. ss << " ";
  219. ss << desc->name;
  220. return ss.str();
  221. };
  222. if (ext_inst_type == SPV_EXT_INST_TYPE_GLSL_STD_450) {
  223. const GLSLstd450 ext_inst_key = GLSLstd450(ext_inst_index);
  224. switch (ext_inst_key) {
  225. case GLSLstd450Round:
  226. case GLSLstd450RoundEven:
  227. case GLSLstd450FAbs:
  228. case GLSLstd450Trunc:
  229. case GLSLstd450FSign:
  230. case GLSLstd450Floor:
  231. case GLSLstd450Ceil:
  232. case GLSLstd450Fract:
  233. case GLSLstd450Sqrt:
  234. case GLSLstd450InverseSqrt:
  235. case GLSLstd450FMin:
  236. case GLSLstd450FMax:
  237. case GLSLstd450FClamp:
  238. case GLSLstd450FMix:
  239. case GLSLstd450Step:
  240. case GLSLstd450SmoothStep:
  241. case GLSLstd450Fma:
  242. case GLSLstd450Normalize:
  243. case GLSLstd450FaceForward:
  244. case GLSLstd450Reflect:
  245. case GLSLstd450NMin:
  246. case GLSLstd450NMax:
  247. case GLSLstd450NClamp: {
  248. if (!_.IsFloatScalarOrVectorType(result_type)) {
  249. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  250. << ext_inst_name() << ": "
  251. << "expected Result Type to be a float scalar or vector type";
  252. }
  253. for (uint32_t operand_index = 4; operand_index < num_operands;
  254. ++operand_index) {
  255. const uint32_t operand_type = _.GetOperandTypeId(inst, operand_index);
  256. if (result_type != operand_type) {
  257. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  258. << ext_inst_name() << ": "
  259. << "expected types of all operands to be equal to Result "
  260. "Type";
  261. }
  262. }
  263. break;
  264. }
  265. case GLSLstd450SAbs:
  266. case GLSLstd450SSign:
  267. case GLSLstd450UMin:
  268. case GLSLstd450SMin:
  269. case GLSLstd450UMax:
  270. case GLSLstd450SMax:
  271. case GLSLstd450UClamp:
  272. case GLSLstd450SClamp:
  273. case GLSLstd450FindILsb:
  274. case GLSLstd450FindUMsb:
  275. case GLSLstd450FindSMsb: {
  276. if (!_.IsIntScalarOrVectorType(result_type)) {
  277. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  278. << ext_inst_name() << ": "
  279. << "expected Result Type to be an int scalar or vector type";
  280. }
  281. const uint32_t result_type_bit_width = _.GetBitWidth(result_type);
  282. const uint32_t result_type_dimension = _.GetDimension(result_type);
  283. for (uint32_t operand_index = 4; operand_index < num_operands;
  284. ++operand_index) {
  285. const uint32_t operand_type = _.GetOperandTypeId(inst, operand_index);
  286. if (!_.IsIntScalarOrVectorType(operand_type)) {
  287. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  288. << ext_inst_name() << ": "
  289. << "expected all operands to be int scalars or vectors";
  290. }
  291. if (result_type_dimension != _.GetDimension(operand_type)) {
  292. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  293. << ext_inst_name() << ": "
  294. << "expected all operands to have the same dimension as "
  295. << "Result Type";
  296. }
  297. if (result_type_bit_width != _.GetBitWidth(operand_type)) {
  298. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  299. << ext_inst_name() << ": "
  300. << "expected all operands to have the same bit width as "
  301. << "Result Type";
  302. }
  303. if (ext_inst_key == GLSLstd450FindUMsb ||
  304. ext_inst_key == GLSLstd450FindSMsb) {
  305. if (result_type_bit_width != 32) {
  306. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  307. << ext_inst_name() << ": "
  308. << "this instruction is currently limited to 32-bit width "
  309. << "components";
  310. }
  311. }
  312. }
  313. break;
  314. }
  315. case GLSLstd450Radians:
  316. case GLSLstd450Degrees:
  317. case GLSLstd450Sin:
  318. case GLSLstd450Cos:
  319. case GLSLstd450Tan:
  320. case GLSLstd450Asin:
  321. case GLSLstd450Acos:
  322. case GLSLstd450Atan:
  323. case GLSLstd450Sinh:
  324. case GLSLstd450Cosh:
  325. case GLSLstd450Tanh:
  326. case GLSLstd450Asinh:
  327. case GLSLstd450Acosh:
  328. case GLSLstd450Atanh:
  329. case GLSLstd450Exp:
  330. case GLSLstd450Exp2:
  331. case GLSLstd450Log:
  332. case GLSLstd450Log2:
  333. case GLSLstd450Atan2:
  334. case GLSLstd450Pow: {
  335. if (!_.IsFloatScalarOrVectorType(result_type)) {
  336. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  337. << ext_inst_name() << ": "
  338. << "expected Result Type to be a 16 or 32-bit scalar or "
  339. "vector float type";
  340. }
  341. const uint32_t result_type_bit_width = _.GetBitWidth(result_type);
  342. if (result_type_bit_width != 16 && result_type_bit_width != 32) {
  343. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  344. << ext_inst_name() << ": "
  345. << "expected Result Type to be a 16 or 32-bit scalar or "
  346. "vector float type";
  347. }
  348. for (uint32_t operand_index = 4; operand_index < num_operands;
  349. ++operand_index) {
  350. const uint32_t operand_type = _.GetOperandTypeId(inst, operand_index);
  351. if (result_type != operand_type) {
  352. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  353. << ext_inst_name() << ": "
  354. << "expected types of all operands to be equal to Result "
  355. "Type";
  356. }
  357. }
  358. break;
  359. }
  360. case GLSLstd450Determinant: {
  361. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  362. uint32_t num_rows = 0;
  363. uint32_t num_cols = 0;
  364. uint32_t col_type = 0;
  365. uint32_t component_type = 0;
  366. if (!_.GetMatrixTypeInfo(x_type, &num_rows, &num_cols, &col_type,
  367. &component_type) ||
  368. num_rows != num_cols) {
  369. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  370. << ext_inst_name() << ": "
  371. << "expected operand X to be a square matrix";
  372. }
  373. if (result_type != component_type) {
  374. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  375. << ext_inst_name() << ": "
  376. << "expected operand X component type to be equal to "
  377. << "Result Type";
  378. }
  379. break;
  380. }
  381. case GLSLstd450MatrixInverse: {
  382. uint32_t num_rows = 0;
  383. uint32_t num_cols = 0;
  384. uint32_t col_type = 0;
  385. uint32_t component_type = 0;
  386. if (!_.GetMatrixTypeInfo(result_type, &num_rows, &num_cols, &col_type,
  387. &component_type) ||
  388. num_rows != num_cols) {
  389. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  390. << ext_inst_name() << ": "
  391. << "expected Result Type to be a square matrix";
  392. }
  393. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  394. if (result_type != x_type) {
  395. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  396. << ext_inst_name() << ": "
  397. << "expected operand X type to be equal to Result Type";
  398. }
  399. break;
  400. }
  401. case GLSLstd450Modf: {
  402. if (!_.IsFloatScalarOrVectorType(result_type)) {
  403. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  404. << ext_inst_name() << ": "
  405. << "expected Result Type to be a scalar or vector float type";
  406. }
  407. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  408. const uint32_t i_type = _.GetOperandTypeId(inst, 5);
  409. if (x_type != result_type) {
  410. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  411. << ext_inst_name() << ": "
  412. << "expected operand X type to be equal to Result Type";
  413. }
  414. uint32_t i_storage_class = 0;
  415. uint32_t i_data_type = 0;
  416. if (!_.GetPointerTypeInfo(i_type, &i_data_type, &i_storage_class)) {
  417. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  418. << ext_inst_name() << ": "
  419. << "expected operand I to be a pointer";
  420. }
  421. if (i_data_type != result_type) {
  422. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  423. << ext_inst_name() << ": "
  424. << "expected operand I data type to be equal to Result Type";
  425. }
  426. break;
  427. }
  428. case GLSLstd450ModfStruct: {
  429. std::vector<uint32_t> result_types;
  430. if (!_.GetStructMemberTypes(result_type, &result_types) ||
  431. result_types.size() != 2 ||
  432. !_.IsFloatScalarOrVectorType(result_types[0]) ||
  433. result_types[1] != result_types[0]) {
  434. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  435. << ext_inst_name() << ": "
  436. << "expected Result Type to be a struct with two identical "
  437. << "scalar or vector float type members";
  438. }
  439. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  440. if (x_type != result_types[0]) {
  441. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  442. << ext_inst_name() << ": "
  443. << "expected operand X type to be equal to members of "
  444. << "Result Type struct";
  445. }
  446. break;
  447. }
  448. case GLSLstd450Frexp: {
  449. if (!_.IsFloatScalarOrVectorType(result_type)) {
  450. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  451. << ext_inst_name() << ": "
  452. << "expected Result Type to be a scalar or vector float type";
  453. }
  454. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  455. const uint32_t exp_type = _.GetOperandTypeId(inst, 5);
  456. if (x_type != result_type) {
  457. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  458. << ext_inst_name() << ": "
  459. << "expected operand X type to be equal to Result Type";
  460. }
  461. uint32_t exp_storage_class = 0;
  462. uint32_t exp_data_type = 0;
  463. if (!_.GetPointerTypeInfo(exp_type, &exp_data_type,
  464. &exp_storage_class)) {
  465. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  466. << ext_inst_name() << ": "
  467. << "expected operand Exp to be a pointer";
  468. }
  469. if (!_.IsIntScalarOrVectorType(exp_data_type) ||
  470. (!_.HasExtension(kSPV_AMD_gpu_shader_int16) &&
  471. _.GetBitWidth(exp_data_type) != 32) ||
  472. (_.HasExtension(kSPV_AMD_gpu_shader_int16) &&
  473. _.GetBitWidth(exp_data_type) != 16 &&
  474. _.GetBitWidth(exp_data_type) != 32)) {
  475. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  476. << ext_inst_name() << ": "
  477. << "expected operand Exp data type to be a "
  478. << (_.HasExtension(kSPV_AMD_gpu_shader_int16)
  479. ? "16-bit or 32-bit "
  480. : "32-bit ")
  481. << "int scalar or vector type";
  482. }
  483. if (_.GetDimension(result_type) != _.GetDimension(exp_data_type)) {
  484. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  485. << ext_inst_name() << ": "
  486. << "expected operand Exp data type to have the same component "
  487. << "number as Result Type";
  488. }
  489. break;
  490. }
  491. case GLSLstd450Ldexp: {
  492. if (!_.IsFloatScalarOrVectorType(result_type)) {
  493. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  494. << ext_inst_name() << ": "
  495. << "expected Result Type to be a scalar or vector float type";
  496. }
  497. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  498. const uint32_t exp_type = _.GetOperandTypeId(inst, 5);
  499. if (x_type != result_type) {
  500. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  501. << ext_inst_name() << ": "
  502. << "expected operand X type to be equal to Result Type";
  503. }
  504. if (!_.IsIntScalarOrVectorType(exp_type)) {
  505. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  506. << ext_inst_name() << ": "
  507. << "expected operand Exp to be a 32-bit int scalar "
  508. << "or vector type";
  509. }
  510. if (_.GetDimension(result_type) != _.GetDimension(exp_type)) {
  511. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  512. << ext_inst_name() << ": "
  513. << "expected operand Exp to have the same component "
  514. << "number as Result Type";
  515. }
  516. break;
  517. }
  518. case GLSLstd450FrexpStruct: {
  519. std::vector<uint32_t> result_types;
  520. if (!_.GetStructMemberTypes(result_type, &result_types) ||
  521. result_types.size() != 2 ||
  522. !_.IsFloatScalarOrVectorType(result_types[0]) ||
  523. !_.IsIntScalarOrVectorType(result_types[1]) ||
  524. (!_.HasExtension(kSPV_AMD_gpu_shader_int16) &&
  525. _.GetBitWidth(result_types[1]) != 32) ||
  526. (_.HasExtension(kSPV_AMD_gpu_shader_int16) &&
  527. _.GetBitWidth(result_types[1]) != 16 &&
  528. _.GetBitWidth(result_types[1]) != 32) ||
  529. _.GetDimension(result_types[0]) !=
  530. _.GetDimension(result_types[1])) {
  531. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  532. << ext_inst_name() << ": "
  533. << "expected Result Type to be a struct with two members, "
  534. << "first member a float scalar or vector, second member a "
  535. << (_.HasExtension(kSPV_AMD_gpu_shader_int16)
  536. ? "16-bit or 32-bit "
  537. : "32-bit ")
  538. << "int scalar or vector with the same number of "
  539. << "components as the first member";
  540. }
  541. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  542. if (x_type != result_types[0]) {
  543. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  544. << ext_inst_name() << ": "
  545. << "expected operand X type to be equal to the first member "
  546. << "of Result Type struct";
  547. }
  548. break;
  549. }
  550. case GLSLstd450PackSnorm4x8:
  551. case GLSLstd450PackUnorm4x8: {
  552. if (!_.IsIntScalarType(result_type) ||
  553. _.GetBitWidth(result_type) != 32) {
  554. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  555. << ext_inst_name() << ": "
  556. << "expected Result Type to be 32-bit int scalar type";
  557. }
  558. const uint32_t v_type = _.GetOperandTypeId(inst, 4);
  559. if (!_.IsFloatVectorType(v_type) || _.GetDimension(v_type) != 4 ||
  560. _.GetBitWidth(v_type) != 32) {
  561. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  562. << ext_inst_name() << ": "
  563. << "expected operand V to be a 32-bit float vector of size 4";
  564. }
  565. break;
  566. }
  567. case GLSLstd450PackSnorm2x16:
  568. case GLSLstd450PackUnorm2x16:
  569. case GLSLstd450PackHalf2x16: {
  570. if (!_.IsIntScalarType(result_type) ||
  571. _.GetBitWidth(result_type) != 32) {
  572. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  573. << ext_inst_name() << ": "
  574. << "expected Result Type to be 32-bit int scalar type";
  575. }
  576. const uint32_t v_type = _.GetOperandTypeId(inst, 4);
  577. if (!_.IsFloatVectorType(v_type) || _.GetDimension(v_type) != 2 ||
  578. _.GetBitWidth(v_type) != 32) {
  579. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  580. << ext_inst_name() << ": "
  581. << "expected operand V to be a 32-bit float vector of size 2";
  582. }
  583. break;
  584. }
  585. case GLSLstd450PackDouble2x32: {
  586. if (!_.IsFloatScalarType(result_type) ||
  587. _.GetBitWidth(result_type) != 64) {
  588. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  589. << ext_inst_name() << ": "
  590. << "expected Result Type to be 64-bit float scalar type";
  591. }
  592. const uint32_t v_type = _.GetOperandTypeId(inst, 4);
  593. if (!_.IsIntVectorType(v_type) || _.GetDimension(v_type) != 2 ||
  594. _.GetBitWidth(v_type) != 32) {
  595. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  596. << ext_inst_name() << ": "
  597. << "expected operand V to be a 32-bit int vector of size 2";
  598. }
  599. break;
  600. }
  601. case GLSLstd450UnpackSnorm4x8:
  602. case GLSLstd450UnpackUnorm4x8: {
  603. if (!_.IsFloatVectorType(result_type) ||
  604. _.GetDimension(result_type) != 4 ||
  605. _.GetBitWidth(result_type) != 32) {
  606. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  607. << ext_inst_name() << ": "
  608. << "expected Result Type to be a 32-bit float vector of size "
  609. "4";
  610. }
  611. const uint32_t v_type = _.GetOperandTypeId(inst, 4);
  612. if (!_.IsIntScalarType(v_type) || _.GetBitWidth(v_type) != 32) {
  613. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  614. << ext_inst_name() << ": "
  615. << "expected operand P to be a 32-bit int scalar";
  616. }
  617. break;
  618. }
  619. case GLSLstd450UnpackSnorm2x16:
  620. case GLSLstd450UnpackUnorm2x16:
  621. case GLSLstd450UnpackHalf2x16: {
  622. if (!_.IsFloatVectorType(result_type) ||
  623. _.GetDimension(result_type) != 2 ||
  624. _.GetBitWidth(result_type) != 32) {
  625. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  626. << ext_inst_name() << ": "
  627. << "expected Result Type to be a 32-bit float vector of size "
  628. "2";
  629. }
  630. const uint32_t v_type = _.GetOperandTypeId(inst, 4);
  631. if (!_.IsIntScalarType(v_type) || _.GetBitWidth(v_type) != 32) {
  632. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  633. << ext_inst_name() << ": "
  634. << "expected operand P to be a 32-bit int scalar";
  635. }
  636. break;
  637. }
  638. case GLSLstd450UnpackDouble2x32: {
  639. if (!_.IsIntVectorType(result_type) ||
  640. _.GetDimension(result_type) != 2 ||
  641. _.GetBitWidth(result_type) != 32) {
  642. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  643. << ext_inst_name() << ": "
  644. << "expected Result Type to be a 32-bit int vector of size "
  645. "2";
  646. }
  647. const uint32_t v_type = _.GetOperandTypeId(inst, 4);
  648. if (!_.IsFloatScalarType(v_type) || _.GetBitWidth(v_type) != 64) {
  649. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  650. << ext_inst_name() << ": "
  651. << "expected operand V to be a 64-bit float scalar";
  652. }
  653. break;
  654. }
  655. case GLSLstd450Length: {
  656. if (!_.IsFloatScalarType(result_type)) {
  657. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  658. << ext_inst_name() << ": "
  659. << "expected Result Type to be a float scalar type";
  660. }
  661. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  662. if (!_.IsFloatScalarOrVectorType(x_type)) {
  663. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  664. << ext_inst_name() << ": "
  665. << "expected operand X to be of float scalar or vector type";
  666. }
  667. if (result_type != _.GetComponentType(x_type)) {
  668. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  669. << ext_inst_name() << ": "
  670. << "expected operand X component type to be equal to Result "
  671. "Type";
  672. }
  673. break;
  674. }
  675. case GLSLstd450Distance: {
  676. if (!_.IsFloatScalarType(result_type)) {
  677. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  678. << ext_inst_name() << ": "
  679. << "expected Result Type to be a float scalar type";
  680. }
  681. const uint32_t p0_type = _.GetOperandTypeId(inst, 4);
  682. if (!_.IsFloatScalarOrVectorType(p0_type)) {
  683. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  684. << ext_inst_name() << ": "
  685. << "expected operand P0 to be of float scalar or vector type";
  686. }
  687. if (result_type != _.GetComponentType(p0_type)) {
  688. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  689. << ext_inst_name() << ": "
  690. << "expected operand P0 component type to be equal to "
  691. << "Result Type";
  692. }
  693. const uint32_t p1_type = _.GetOperandTypeId(inst, 5);
  694. if (!_.IsFloatScalarOrVectorType(p1_type)) {
  695. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  696. << ext_inst_name() << ": "
  697. << "expected operand P1 to be of float scalar or vector type";
  698. }
  699. if (result_type != _.GetComponentType(p1_type)) {
  700. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  701. << ext_inst_name() << ": "
  702. << "expected operand P1 component type to be equal to "
  703. << "Result Type";
  704. }
  705. if (_.GetDimension(p0_type) != _.GetDimension(p1_type)) {
  706. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  707. << ext_inst_name() << ": "
  708. << "expected operands P0 and P1 to have the same number of "
  709. << "components";
  710. }
  711. break;
  712. }
  713. case GLSLstd450Cross: {
  714. if (!_.IsFloatVectorType(result_type)) {
  715. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  716. << ext_inst_name() << ": "
  717. << "expected Result Type to be a float vector type";
  718. }
  719. if (_.GetDimension(result_type) != 3) {
  720. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  721. << ext_inst_name() << ": "
  722. << "expected Result Type to have 3 components";
  723. }
  724. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  725. const uint32_t y_type = _.GetOperandTypeId(inst, 5);
  726. if (x_type != result_type) {
  727. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  728. << ext_inst_name() << ": "
  729. << "expected operand X type to be equal to Result Type";
  730. }
  731. if (y_type != result_type) {
  732. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  733. << ext_inst_name() << ": "
  734. << "expected operand Y type to be equal to Result Type";
  735. }
  736. break;
  737. }
  738. case GLSLstd450Refract: {
  739. if (!_.IsFloatScalarOrVectorType(result_type)) {
  740. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  741. << ext_inst_name() << ": "
  742. << "expected Result Type to be a float scalar or vector type";
  743. }
  744. const uint32_t i_type = _.GetOperandTypeId(inst, 4);
  745. const uint32_t n_type = _.GetOperandTypeId(inst, 5);
  746. const uint32_t eta_type = _.GetOperandTypeId(inst, 6);
  747. if (result_type != i_type) {
  748. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  749. << ext_inst_name() << ": "
  750. << "expected operand I to be of type equal to Result Type";
  751. }
  752. if (result_type != n_type) {
  753. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  754. << ext_inst_name() << ": "
  755. << "expected operand N to be of type equal to Result Type";
  756. }
  757. if (!_.IsFloatScalarType(eta_type)) {
  758. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  759. << ext_inst_name() << ": "
  760. << "expected operand Eta to be a float scalar";
  761. }
  762. break;
  763. }
  764. case GLSLstd450InterpolateAtCentroid:
  765. case GLSLstd450InterpolateAtSample:
  766. case GLSLstd450InterpolateAtOffset: {
  767. if (!_.HasCapability(SpvCapabilityInterpolationFunction)) {
  768. return _.diag(SPV_ERROR_INVALID_CAPABILITY, inst)
  769. << ext_inst_name()
  770. << " requires capability InterpolationFunction";
  771. }
  772. if (!_.IsFloatScalarOrVectorType(result_type) ||
  773. _.GetBitWidth(result_type) != 32) {
  774. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  775. << ext_inst_name() << ": "
  776. << "expected Result Type to be a 32-bit float scalar "
  777. << "or vector type";
  778. }
  779. const uint32_t interpolant_type = _.GetOperandTypeId(inst, 4);
  780. uint32_t interpolant_storage_class = 0;
  781. uint32_t interpolant_data_type = 0;
  782. if (!_.GetPointerTypeInfo(interpolant_type, &interpolant_data_type,
  783. &interpolant_storage_class)) {
  784. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  785. << ext_inst_name() << ": "
  786. << "expected Interpolant to be a pointer";
  787. }
  788. if (result_type != interpolant_data_type) {
  789. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  790. << ext_inst_name() << ": "
  791. << "expected Interpolant data type to be equal to Result Type";
  792. }
  793. if (interpolant_storage_class != SpvStorageClassInput) {
  794. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  795. << ext_inst_name() << ": "
  796. << "expected Interpolant storage class to be Input";
  797. }
  798. if (ext_inst_key == GLSLstd450InterpolateAtSample) {
  799. const uint32_t sample_type = _.GetOperandTypeId(inst, 5);
  800. if (!_.IsIntScalarType(sample_type) ||
  801. _.GetBitWidth(sample_type) != 32) {
  802. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  803. << ext_inst_name() << ": "
  804. << "expected Sample to be 32-bit integer";
  805. }
  806. }
  807. if (ext_inst_key == GLSLstd450InterpolateAtOffset) {
  808. const uint32_t offset_type = _.GetOperandTypeId(inst, 5);
  809. if (!_.IsFloatVectorType(offset_type) ||
  810. _.GetDimension(offset_type) != 2 ||
  811. _.GetBitWidth(offset_type) != 32) {
  812. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  813. << ext_inst_name() << ": "
  814. << "expected Offset to be a vector of 2 32-bit floats";
  815. }
  816. }
  817. _.function(inst->function()->id())
  818. ->RegisterExecutionModelLimitation(
  819. SpvExecutionModelFragment,
  820. ext_inst_name() +
  821. std::string(" requires Fragment execution model"));
  822. break;
  823. }
  824. case GLSLstd450IMix: {
  825. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  826. << "Extended instruction GLSLstd450IMix is not supported";
  827. }
  828. case GLSLstd450Bad: {
  829. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  830. << "Encountered extended instruction GLSLstd450Bad";
  831. }
  832. case GLSLstd450Count: {
  833. assert(0);
  834. break;
  835. }
  836. }
  837. } else if (ext_inst_type == SPV_EXT_INST_TYPE_OPENCL_STD) {
  838. const OpenCLLIB::Entrypoints ext_inst_key =
  839. OpenCLLIB::Entrypoints(ext_inst_index);
  840. switch (ext_inst_key) {
  841. case OpenCLLIB::Acos:
  842. case OpenCLLIB::Acosh:
  843. case OpenCLLIB::Acospi:
  844. case OpenCLLIB::Asin:
  845. case OpenCLLIB::Asinh:
  846. case OpenCLLIB::Asinpi:
  847. case OpenCLLIB::Atan:
  848. case OpenCLLIB::Atan2:
  849. case OpenCLLIB::Atanh:
  850. case OpenCLLIB::Atanpi:
  851. case OpenCLLIB::Atan2pi:
  852. case OpenCLLIB::Cbrt:
  853. case OpenCLLIB::Ceil:
  854. case OpenCLLIB::Copysign:
  855. case OpenCLLIB::Cos:
  856. case OpenCLLIB::Cosh:
  857. case OpenCLLIB::Cospi:
  858. case OpenCLLIB::Erfc:
  859. case OpenCLLIB::Erf:
  860. case OpenCLLIB::Exp:
  861. case OpenCLLIB::Exp2:
  862. case OpenCLLIB::Exp10:
  863. case OpenCLLIB::Expm1:
  864. case OpenCLLIB::Fabs:
  865. case OpenCLLIB::Fdim:
  866. case OpenCLLIB::Floor:
  867. case OpenCLLIB::Fma:
  868. case OpenCLLIB::Fmax:
  869. case OpenCLLIB::Fmin:
  870. case OpenCLLIB::Fmod:
  871. case OpenCLLIB::Hypot:
  872. case OpenCLLIB::Lgamma:
  873. case OpenCLLIB::Log:
  874. case OpenCLLIB::Log2:
  875. case OpenCLLIB::Log10:
  876. case OpenCLLIB::Log1p:
  877. case OpenCLLIB::Logb:
  878. case OpenCLLIB::Mad:
  879. case OpenCLLIB::Maxmag:
  880. case OpenCLLIB::Minmag:
  881. case OpenCLLIB::Nextafter:
  882. case OpenCLLIB::Pow:
  883. case OpenCLLIB::Powr:
  884. case OpenCLLIB::Remainder:
  885. case OpenCLLIB::Rint:
  886. case OpenCLLIB::Round:
  887. case OpenCLLIB::Rsqrt:
  888. case OpenCLLIB::Sin:
  889. case OpenCLLIB::Sinh:
  890. case OpenCLLIB::Sinpi:
  891. case OpenCLLIB::Sqrt:
  892. case OpenCLLIB::Tan:
  893. case OpenCLLIB::Tanh:
  894. case OpenCLLIB::Tanpi:
  895. case OpenCLLIB::Tgamma:
  896. case OpenCLLIB::Trunc:
  897. case OpenCLLIB::Half_cos:
  898. case OpenCLLIB::Half_divide:
  899. case OpenCLLIB::Half_exp:
  900. case OpenCLLIB::Half_exp2:
  901. case OpenCLLIB::Half_exp10:
  902. case OpenCLLIB::Half_log:
  903. case OpenCLLIB::Half_log2:
  904. case OpenCLLIB::Half_log10:
  905. case OpenCLLIB::Half_powr:
  906. case OpenCLLIB::Half_recip:
  907. case OpenCLLIB::Half_rsqrt:
  908. case OpenCLLIB::Half_sin:
  909. case OpenCLLIB::Half_sqrt:
  910. case OpenCLLIB::Half_tan:
  911. case OpenCLLIB::Native_cos:
  912. case OpenCLLIB::Native_divide:
  913. case OpenCLLIB::Native_exp:
  914. case OpenCLLIB::Native_exp2:
  915. case OpenCLLIB::Native_exp10:
  916. case OpenCLLIB::Native_log:
  917. case OpenCLLIB::Native_log2:
  918. case OpenCLLIB::Native_log10:
  919. case OpenCLLIB::Native_powr:
  920. case OpenCLLIB::Native_recip:
  921. case OpenCLLIB::Native_rsqrt:
  922. case OpenCLLIB::Native_sin:
  923. case OpenCLLIB::Native_sqrt:
  924. case OpenCLLIB::Native_tan:
  925. case OpenCLLIB::FClamp:
  926. case OpenCLLIB::Degrees:
  927. case OpenCLLIB::FMax_common:
  928. case OpenCLLIB::FMin_common:
  929. case OpenCLLIB::Mix:
  930. case OpenCLLIB::Radians:
  931. case OpenCLLIB::Step:
  932. case OpenCLLIB::Smoothstep:
  933. case OpenCLLIB::Sign: {
  934. if (!_.IsFloatScalarOrVectorType(result_type)) {
  935. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  936. << ext_inst_name() << ": "
  937. << "expected Result Type to be a float scalar or vector type";
  938. }
  939. const uint32_t num_components = _.GetDimension(result_type);
  940. if (num_components > 4 && num_components != 8 && num_components != 16) {
  941. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  942. << ext_inst_name() << ": "
  943. << "expected Result Type to be a scalar or a vector with 2, "
  944. "3, 4, 8 or 16 components";
  945. }
  946. for (uint32_t operand_index = 4; operand_index < num_operands;
  947. ++operand_index) {
  948. const uint32_t operand_type = _.GetOperandTypeId(inst, operand_index);
  949. if (result_type != operand_type) {
  950. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  951. << ext_inst_name() << ": "
  952. << "expected types of all operands to be equal to Result "
  953. "Type";
  954. }
  955. }
  956. break;
  957. }
  958. case OpenCLLIB::Fract:
  959. case OpenCLLIB::Modf:
  960. case OpenCLLIB::Sincos: {
  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 p_type = _.GetOperandTypeId(inst, 5);
  980. uint32_t p_storage_class = 0;
  981. uint32_t p_data_type = 0;
  982. if (!_.GetPointerTypeInfo(p_type, &p_data_type, &p_storage_class)) {
  983. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  984. << ext_inst_name() << ": "
  985. << "expected the last operand to be a pointer";
  986. }
  987. if (p_storage_class != SpvStorageClassGeneric &&
  988. p_storage_class != SpvStorageClassCrossWorkgroup &&
  989. p_storage_class != SpvStorageClassWorkgroup &&
  990. p_storage_class != SpvStorageClassFunction) {
  991. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  992. << ext_inst_name() << ": "
  993. << "expected storage class of the pointer to be Generic, "
  994. "CrossWorkgroup, Workgroup or Function";
  995. }
  996. if (result_type != p_data_type) {
  997. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  998. << ext_inst_name() << ": "
  999. << "expected data type of the pointer to be equal to Result "
  1000. "Type";
  1001. }
  1002. break;
  1003. }
  1004. case OpenCLLIB::Frexp:
  1005. case OpenCLLIB::Lgamma_r:
  1006. case OpenCLLIB::Remquo: {
  1007. if (!_.IsFloatScalarOrVectorType(result_type)) {
  1008. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1009. << ext_inst_name() << ": "
  1010. << "expected Result Type to be a float scalar or vector type";
  1011. }
  1012. const uint32_t num_components = _.GetDimension(result_type);
  1013. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1014. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1015. << ext_inst_name() << ": "
  1016. << "expected Result Type to be a scalar or a vector with 2, "
  1017. "3, 4, 8 or 16 components";
  1018. }
  1019. uint32_t operand_index = 4;
  1020. const uint32_t x_type = _.GetOperandTypeId(inst, operand_index++);
  1021. if (result_type != x_type) {
  1022. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1023. << ext_inst_name() << ": "
  1024. << "expected type of operand X to be equal to Result Type";
  1025. }
  1026. if (ext_inst_key == OpenCLLIB::Remquo) {
  1027. const uint32_t y_type = _.GetOperandTypeId(inst, operand_index++);
  1028. if (result_type != y_type) {
  1029. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1030. << ext_inst_name() << ": "
  1031. << "expected type of operand Y to be equal to Result Type";
  1032. }
  1033. }
  1034. const uint32_t p_type = _.GetOperandTypeId(inst, operand_index++);
  1035. uint32_t p_storage_class = 0;
  1036. uint32_t p_data_type = 0;
  1037. if (!_.GetPointerTypeInfo(p_type, &p_data_type, &p_storage_class)) {
  1038. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1039. << ext_inst_name() << ": "
  1040. << "expected the last operand to be a pointer";
  1041. }
  1042. if (p_storage_class != SpvStorageClassGeneric &&
  1043. p_storage_class != SpvStorageClassCrossWorkgroup &&
  1044. p_storage_class != SpvStorageClassWorkgroup &&
  1045. p_storage_class != SpvStorageClassFunction) {
  1046. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1047. << ext_inst_name() << ": "
  1048. << "expected storage class of the pointer to be Generic, "
  1049. "CrossWorkgroup, Workgroup or Function";
  1050. }
  1051. if (!_.IsIntScalarOrVectorType(p_data_type) ||
  1052. _.GetBitWidth(p_data_type) != 32) {
  1053. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1054. << ext_inst_name() << ": "
  1055. << "expected data type of the pointer to be a 32-bit int "
  1056. "scalar or vector type";
  1057. }
  1058. if (_.GetDimension(p_data_type) != num_components) {
  1059. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1060. << ext_inst_name() << ": "
  1061. << "expected data type of the pointer to have the same number "
  1062. "of components as Result Type";
  1063. }
  1064. break;
  1065. }
  1066. case OpenCLLIB::Ilogb: {
  1067. if (!_.IsIntScalarOrVectorType(result_type) ||
  1068. _.GetBitWidth(result_type) != 32) {
  1069. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1070. << ext_inst_name() << ": "
  1071. << "expected Result Type to be a 32-bit int scalar or vector "
  1072. "type";
  1073. }
  1074. const uint32_t num_components = _.GetDimension(result_type);
  1075. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1076. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1077. << ext_inst_name() << ": "
  1078. << "expected Result Type to be a scalar or a vector with 2, "
  1079. "3, 4, 8 or 16 components";
  1080. }
  1081. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  1082. if (!_.IsFloatScalarOrVectorType(x_type)) {
  1083. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1084. << ext_inst_name() << ": "
  1085. << "expected operand X to be a float scalar or vector";
  1086. }
  1087. if (_.GetDimension(x_type) != num_components) {
  1088. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1089. << ext_inst_name() << ": "
  1090. << "expected operand X to have the same number of components "
  1091. "as Result Type";
  1092. }
  1093. break;
  1094. }
  1095. case OpenCLLIB::Ldexp:
  1096. case OpenCLLIB::Pown:
  1097. case OpenCLLIB::Rootn: {
  1098. if (!_.IsFloatScalarOrVectorType(result_type)) {
  1099. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1100. << ext_inst_name() << ": "
  1101. << "expected Result Type to be a float scalar or vector type";
  1102. }
  1103. const uint32_t num_components = _.GetDimension(result_type);
  1104. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1105. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1106. << ext_inst_name() << ": "
  1107. << "expected Result Type to be a scalar or a vector with 2, "
  1108. "3, 4, 8 or 16 components";
  1109. }
  1110. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  1111. if (result_type != x_type) {
  1112. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1113. << ext_inst_name() << ": "
  1114. << "expected type of operand X to be equal to Result Type";
  1115. }
  1116. const uint32_t exp_type = _.GetOperandTypeId(inst, 5);
  1117. if (!_.IsIntScalarOrVectorType(exp_type) ||
  1118. _.GetBitWidth(exp_type) != 32) {
  1119. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1120. << ext_inst_name() << ": "
  1121. << "expected the exponent to be a 32-bit int scalar or vector";
  1122. }
  1123. if (_.GetDimension(exp_type) != num_components) {
  1124. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1125. << ext_inst_name() << ": "
  1126. << "expected the exponent to have the same number of "
  1127. "components as Result Type";
  1128. }
  1129. break;
  1130. }
  1131. case OpenCLLIB::Nan: {
  1132. if (!_.IsFloatScalarOrVectorType(result_type)) {
  1133. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1134. << ext_inst_name() << ": "
  1135. << "expected Result Type to be a float scalar or vector type";
  1136. }
  1137. const uint32_t num_components = _.GetDimension(result_type);
  1138. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1139. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1140. << ext_inst_name() << ": "
  1141. << "expected Result Type to be a scalar or a vector with 2, "
  1142. "3, 4, 8 or 16 components";
  1143. }
  1144. const uint32_t nancode_type = _.GetOperandTypeId(inst, 4);
  1145. if (!_.IsIntScalarOrVectorType(nancode_type)) {
  1146. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1147. << ext_inst_name() << ": "
  1148. << "expected Nancode to be an int scalar or vector type";
  1149. }
  1150. if (_.GetDimension(nancode_type) != num_components) {
  1151. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1152. << ext_inst_name() << ": "
  1153. << "expected Nancode to have the same number of components as "
  1154. "Result Type";
  1155. }
  1156. if (_.GetBitWidth(result_type) != _.GetBitWidth(nancode_type)) {
  1157. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1158. << ext_inst_name() << ": "
  1159. << "expected Nancode to have the same bit width as Result "
  1160. "Type";
  1161. }
  1162. break;
  1163. }
  1164. case OpenCLLIB::SAbs:
  1165. case OpenCLLIB::SAbs_diff:
  1166. case OpenCLLIB::SAdd_sat:
  1167. case OpenCLLIB::UAdd_sat:
  1168. case OpenCLLIB::SHadd:
  1169. case OpenCLLIB::UHadd:
  1170. case OpenCLLIB::SRhadd:
  1171. case OpenCLLIB::URhadd:
  1172. case OpenCLLIB::SClamp:
  1173. case OpenCLLIB::UClamp:
  1174. case OpenCLLIB::Clz:
  1175. case OpenCLLIB::Ctz:
  1176. case OpenCLLIB::SMad_hi:
  1177. case OpenCLLIB::UMad_sat:
  1178. case OpenCLLIB::SMad_sat:
  1179. case OpenCLLIB::SMax:
  1180. case OpenCLLIB::UMax:
  1181. case OpenCLLIB::SMin:
  1182. case OpenCLLIB::UMin:
  1183. case OpenCLLIB::SMul_hi:
  1184. case OpenCLLIB::Rotate:
  1185. case OpenCLLIB::SSub_sat:
  1186. case OpenCLLIB::USub_sat:
  1187. case OpenCLLIB::Popcount:
  1188. case OpenCLLIB::UAbs:
  1189. case OpenCLLIB::UAbs_diff:
  1190. case OpenCLLIB::UMul_hi:
  1191. case OpenCLLIB::UMad_hi: {
  1192. if (!_.IsIntScalarOrVectorType(result_type)) {
  1193. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1194. << ext_inst_name() << ": "
  1195. << "expected Result Type to be an int scalar or vector type";
  1196. }
  1197. const uint32_t num_components = _.GetDimension(result_type);
  1198. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1199. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1200. << ext_inst_name() << ": "
  1201. << "expected Result Type to be a scalar or a vector with 2, "
  1202. "3, 4, 8 or 16 components";
  1203. }
  1204. for (uint32_t operand_index = 4; operand_index < num_operands;
  1205. ++operand_index) {
  1206. const uint32_t operand_type = _.GetOperandTypeId(inst, operand_index);
  1207. if (result_type != operand_type) {
  1208. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1209. << ext_inst_name() << ": "
  1210. << "expected types of all operands to be equal to Result "
  1211. "Type";
  1212. }
  1213. }
  1214. break;
  1215. }
  1216. case OpenCLLIB::U_Upsample:
  1217. case OpenCLLIB::S_Upsample: {
  1218. if (!_.IsIntScalarOrVectorType(result_type)) {
  1219. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1220. << ext_inst_name() << ": "
  1221. << "expected Result Type to be an int scalar or vector "
  1222. "type";
  1223. }
  1224. const uint32_t result_num_components = _.GetDimension(result_type);
  1225. if (result_num_components > 4 && result_num_components != 8 &&
  1226. result_num_components != 16) {
  1227. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1228. << ext_inst_name() << ": "
  1229. << "expected Result Type to be a scalar or a vector with 2, "
  1230. "3, 4, 8 or 16 components";
  1231. }
  1232. const uint32_t result_bit_width = _.GetBitWidth(result_type);
  1233. if (result_bit_width != 16 && result_bit_width != 32 &&
  1234. result_bit_width != 64) {
  1235. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1236. << ext_inst_name() << ": "
  1237. << "expected bit width of Result Type components to be 16, 32 "
  1238. "or 64";
  1239. }
  1240. const uint32_t hi_type = _.GetOperandTypeId(inst, 4);
  1241. const uint32_t lo_type = _.GetOperandTypeId(inst, 5);
  1242. if (hi_type != lo_type) {
  1243. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1244. << ext_inst_name() << ": "
  1245. << "expected Hi and Lo operands to have the same type";
  1246. }
  1247. if (result_num_components != _.GetDimension(hi_type)) {
  1248. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1249. << ext_inst_name() << ": "
  1250. << "expected Hi and Lo operands to have the same number of "
  1251. "components as Result Type";
  1252. }
  1253. if (result_bit_width != 2 * _.GetBitWidth(hi_type)) {
  1254. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1255. << ext_inst_name() << ": "
  1256. << "expected bit width of components of Hi and Lo operands to "
  1257. "be half of the bit width of components of Result Type";
  1258. }
  1259. break;
  1260. }
  1261. case OpenCLLIB::SMad24:
  1262. case OpenCLLIB::UMad24:
  1263. case OpenCLLIB::SMul24:
  1264. case OpenCLLIB::UMul24: {
  1265. if (!_.IsIntScalarOrVectorType(result_type) ||
  1266. _.GetBitWidth(result_type) != 32) {
  1267. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1268. << ext_inst_name() << ": "
  1269. << "expected Result Type to be a 32-bit int scalar or vector "
  1270. "type";
  1271. }
  1272. const uint32_t num_components = _.GetDimension(result_type);
  1273. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1274. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1275. << ext_inst_name() << ": "
  1276. << "expected Result Type to be a scalar or a vector with 2, "
  1277. "3, 4, 8 or 16 components";
  1278. }
  1279. for (uint32_t operand_index = 4; operand_index < num_operands;
  1280. ++operand_index) {
  1281. const uint32_t operand_type = _.GetOperandTypeId(inst, operand_index);
  1282. if (result_type != operand_type) {
  1283. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1284. << ext_inst_name() << ": "
  1285. << "expected types of all operands to be equal to Result "
  1286. "Type";
  1287. }
  1288. }
  1289. break;
  1290. }
  1291. case OpenCLLIB::Cross: {
  1292. if (!_.IsFloatVectorType(result_type)) {
  1293. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1294. << ext_inst_name() << ": "
  1295. << "expected Result Type to be a float vector type";
  1296. }
  1297. const uint32_t num_components = _.GetDimension(result_type);
  1298. if (num_components != 3 && num_components != 4) {
  1299. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1300. << ext_inst_name() << ": "
  1301. << "expected Result Type to have 3 or 4 components";
  1302. }
  1303. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  1304. const uint32_t y_type = _.GetOperandTypeId(inst, 5);
  1305. if (x_type != result_type) {
  1306. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1307. << ext_inst_name() << ": "
  1308. << "expected operand X type to be equal to Result Type";
  1309. }
  1310. if (y_type != result_type) {
  1311. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1312. << ext_inst_name() << ": "
  1313. << "expected operand Y type to be equal to Result Type";
  1314. }
  1315. break;
  1316. }
  1317. case OpenCLLIB::Distance:
  1318. case OpenCLLIB::Fast_distance: {
  1319. if (!_.IsFloatScalarType(result_type)) {
  1320. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1321. << ext_inst_name() << ": "
  1322. << "expected Result Type to be a float scalar type";
  1323. }
  1324. const uint32_t p0_type = _.GetOperandTypeId(inst, 4);
  1325. if (!_.IsFloatScalarOrVectorType(p0_type)) {
  1326. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1327. << ext_inst_name() << ": "
  1328. << "expected operand P0 to be of float scalar or vector type";
  1329. }
  1330. const uint32_t num_components = _.GetDimension(p0_type);
  1331. if (num_components > 4) {
  1332. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1333. << ext_inst_name() << ": "
  1334. << "expected operand P0 to have no more than 4 components";
  1335. }
  1336. if (result_type != _.GetComponentType(p0_type)) {
  1337. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1338. << ext_inst_name() << ": "
  1339. << "expected operand P0 component type to be equal to "
  1340. << "Result Type";
  1341. }
  1342. const uint32_t p1_type = _.GetOperandTypeId(inst, 5);
  1343. if (p0_type != p1_type) {
  1344. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1345. << ext_inst_name() << ": "
  1346. << "expected operands P0 and P1 to be of the same type";
  1347. }
  1348. break;
  1349. }
  1350. case OpenCLLIB::Length:
  1351. case OpenCLLIB::Fast_length: {
  1352. if (!_.IsFloatScalarType(result_type)) {
  1353. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1354. << ext_inst_name() << ": "
  1355. << "expected Result Type to be a float scalar type";
  1356. }
  1357. const uint32_t p_type = _.GetOperandTypeId(inst, 4);
  1358. if (!_.IsFloatScalarOrVectorType(p_type)) {
  1359. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1360. << ext_inst_name() << ": "
  1361. << "expected operand P to be a float scalar or vector";
  1362. }
  1363. const uint32_t num_components = _.GetDimension(p_type);
  1364. if (num_components > 4) {
  1365. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1366. << ext_inst_name() << ": "
  1367. << "expected operand P to have no more than 4 components";
  1368. }
  1369. if (result_type != _.GetComponentType(p_type)) {
  1370. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1371. << ext_inst_name() << ": "
  1372. << "expected operand P component type to be equal to Result "
  1373. "Type";
  1374. }
  1375. break;
  1376. }
  1377. case OpenCLLIB::Normalize:
  1378. case OpenCLLIB::Fast_normalize: {
  1379. if (!_.IsFloatScalarOrVectorType(result_type)) {
  1380. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1381. << ext_inst_name() << ": "
  1382. << "expected Result Type to be a float scalar or vector type";
  1383. }
  1384. const uint32_t num_components = _.GetDimension(result_type);
  1385. if (num_components > 4) {
  1386. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1387. << ext_inst_name() << ": "
  1388. << "expected Result Type to have no more than 4 components";
  1389. }
  1390. const uint32_t p_type = _.GetOperandTypeId(inst, 4);
  1391. if (p_type != result_type) {
  1392. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1393. << ext_inst_name() << ": "
  1394. << "expected operand P type to be equal to Result Type";
  1395. }
  1396. break;
  1397. }
  1398. case OpenCLLIB::Bitselect: {
  1399. if (!_.IsFloatScalarOrVectorType(result_type) &&
  1400. !_.IsIntScalarOrVectorType(result_type)) {
  1401. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1402. << ext_inst_name() << ": "
  1403. << "expected Result Type to be an int or float scalar or "
  1404. "vector type";
  1405. }
  1406. const uint32_t num_components = _.GetDimension(result_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 Result Type to be a scalar or a vector with 2, "
  1411. "3, 4, 8 or 16 components";
  1412. }
  1413. for (uint32_t operand_index = 4; operand_index < num_operands;
  1414. ++operand_index) {
  1415. const uint32_t operand_type = _.GetOperandTypeId(inst, operand_index);
  1416. if (result_type != operand_type) {
  1417. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1418. << ext_inst_name() << ": "
  1419. << "expected types of all operands to be equal to Result "
  1420. "Type";
  1421. }
  1422. }
  1423. break;
  1424. }
  1425. case OpenCLLIB::Select: {
  1426. if (!_.IsFloatScalarOrVectorType(result_type) &&
  1427. !_.IsIntScalarOrVectorType(result_type)) {
  1428. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1429. << ext_inst_name() << ": "
  1430. << "expected Result Type to be an int or float scalar or "
  1431. "vector type";
  1432. }
  1433. const uint32_t num_components = _.GetDimension(result_type);
  1434. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1435. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1436. << ext_inst_name() << ": "
  1437. << "expected Result Type to be a scalar or a vector with 2, "
  1438. "3, 4, 8 or 16 components";
  1439. }
  1440. const uint32_t a_type = _.GetOperandTypeId(inst, 4);
  1441. const uint32_t b_type = _.GetOperandTypeId(inst, 5);
  1442. const uint32_t c_type = _.GetOperandTypeId(inst, 6);
  1443. if (result_type != a_type) {
  1444. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1445. << ext_inst_name() << ": "
  1446. << "expected operand A type to be equal to Result Type";
  1447. }
  1448. if (result_type != b_type) {
  1449. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1450. << ext_inst_name() << ": "
  1451. << "expected operand B type to be equal to Result Type";
  1452. }
  1453. if (!_.IsIntScalarOrVectorType(c_type)) {
  1454. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1455. << ext_inst_name() << ": "
  1456. << "expected operand C to be an int scalar or vector";
  1457. }
  1458. if (num_components != _.GetDimension(c_type)) {
  1459. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1460. << ext_inst_name() << ": "
  1461. << "expected operand C to have the same number of components "
  1462. "as Result Type";
  1463. }
  1464. if (_.GetBitWidth(result_type) != _.GetBitWidth(c_type)) {
  1465. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1466. << ext_inst_name() << ": "
  1467. << "expected operand C to have the same bit width as Result "
  1468. "Type";
  1469. }
  1470. break;
  1471. }
  1472. case OpenCLLIB::Vloadn: {
  1473. if (!_.IsFloatVectorType(result_type) &&
  1474. !_.IsIntVectorType(result_type)) {
  1475. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1476. << ext_inst_name() << ": "
  1477. << "expected Result Type to be an int or float vector type";
  1478. }
  1479. const uint32_t num_components = _.GetDimension(result_type);
  1480. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1481. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1482. << ext_inst_name() << ": "
  1483. << "expected Result Type to have 2, 3, 4, 8 or 16 components";
  1484. }
  1485. const uint32_t offset_type = _.GetOperandTypeId(inst, 4);
  1486. const uint32_t p_type = _.GetOperandTypeId(inst, 5);
  1487. const uint32_t size_t_bit_width = GetSizeTBitWidth(_);
  1488. if (!size_t_bit_width) {
  1489. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1490. << ext_inst_name()
  1491. << " can only be used with physical addressing models";
  1492. }
  1493. if (!_.IsIntScalarType(offset_type) ||
  1494. _.GetBitWidth(offset_type) != size_t_bit_width) {
  1495. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1496. << ext_inst_name() << ": "
  1497. << "expected operand Offset to be of type size_t ("
  1498. << size_t_bit_width
  1499. << "-bit integer for the addressing model used in the module)";
  1500. }
  1501. uint32_t p_storage_class = 0;
  1502. uint32_t p_data_type = 0;
  1503. if (!_.GetPointerTypeInfo(p_type, &p_data_type, &p_storage_class)) {
  1504. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1505. << ext_inst_name() << ": "
  1506. << "expected operand P to be a pointer";
  1507. }
  1508. if (p_storage_class != SpvStorageClassUniformConstant &&
  1509. p_storage_class != SpvStorageClassGeneric &&
  1510. p_storage_class != SpvStorageClassCrossWorkgroup &&
  1511. p_storage_class != SpvStorageClassWorkgroup &&
  1512. p_storage_class != SpvStorageClassFunction) {
  1513. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1514. << ext_inst_name() << ": "
  1515. << "expected operand P storage class to be UniformConstant, "
  1516. "Generic, CrossWorkgroup, Workgroup or Function";
  1517. }
  1518. if (_.GetComponentType(result_type) != p_data_type) {
  1519. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1520. << ext_inst_name() << ": "
  1521. << "expected operand P data type to be equal to component "
  1522. "type of Result Type";
  1523. }
  1524. const uint32_t n_value = inst->word(7);
  1525. if (num_components != n_value) {
  1526. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1527. << ext_inst_name() << ": "
  1528. << "expected literal N to be equal to the number of "
  1529. "components of Result Type";
  1530. }
  1531. break;
  1532. }
  1533. case OpenCLLIB::Vstoren: {
  1534. if (_.GetIdOpcode(result_type) != SpvOpTypeVoid) {
  1535. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1536. << ext_inst_name() << ": expected Result Type to be void";
  1537. }
  1538. const uint32_t data_type = _.GetOperandTypeId(inst, 4);
  1539. const uint32_t offset_type = _.GetOperandTypeId(inst, 5);
  1540. const uint32_t p_type = _.GetOperandTypeId(inst, 6);
  1541. if (!_.IsFloatVectorType(data_type) && !_.IsIntVectorType(data_type)) {
  1542. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1543. << ext_inst_name() << ": "
  1544. << "expected Data to be an int or float vector";
  1545. }
  1546. const uint32_t num_components = _.GetDimension(data_type);
  1547. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1548. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1549. << ext_inst_name() << ": "
  1550. << "expected Data to have 2, 3, 4, 8 or 16 components";
  1551. }
  1552. const uint32_t size_t_bit_width = GetSizeTBitWidth(_);
  1553. if (!size_t_bit_width) {
  1554. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1555. << ext_inst_name()
  1556. << " can only be used with physical addressing models";
  1557. }
  1558. if (!_.IsIntScalarType(offset_type) ||
  1559. _.GetBitWidth(offset_type) != size_t_bit_width) {
  1560. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1561. << ext_inst_name() << ": "
  1562. << "expected operand Offset to be of type size_t ("
  1563. << size_t_bit_width
  1564. << "-bit integer for the addressing model used in the module)";
  1565. }
  1566. uint32_t p_storage_class = 0;
  1567. uint32_t p_data_type = 0;
  1568. if (!_.GetPointerTypeInfo(p_type, &p_data_type, &p_storage_class)) {
  1569. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1570. << ext_inst_name() << ": "
  1571. << "expected operand P to be a pointer";
  1572. }
  1573. if (p_storage_class != SpvStorageClassGeneric &&
  1574. p_storage_class != SpvStorageClassCrossWorkgroup &&
  1575. p_storage_class != SpvStorageClassWorkgroup &&
  1576. p_storage_class != SpvStorageClassFunction) {
  1577. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1578. << ext_inst_name() << ": "
  1579. << "expected operand P storage class to be Generic, "
  1580. "CrossWorkgroup, Workgroup or Function";
  1581. }
  1582. if (_.GetComponentType(data_type) != p_data_type) {
  1583. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1584. << ext_inst_name() << ": "
  1585. << "expected operand P data type to be equal to the type of "
  1586. "operand Data components";
  1587. }
  1588. break;
  1589. }
  1590. case OpenCLLIB::Vload_half: {
  1591. if (!_.IsFloatScalarType(result_type)) {
  1592. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1593. << ext_inst_name() << ": "
  1594. << "expected Result Type to be a float scalar type";
  1595. }
  1596. const uint32_t offset_type = _.GetOperandTypeId(inst, 4);
  1597. const uint32_t p_type = _.GetOperandTypeId(inst, 5);
  1598. const uint32_t size_t_bit_width = GetSizeTBitWidth(_);
  1599. if (!size_t_bit_width) {
  1600. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1601. << ext_inst_name()
  1602. << " can only be used with physical addressing models";
  1603. }
  1604. if (!_.IsIntScalarType(offset_type) ||
  1605. _.GetBitWidth(offset_type) != size_t_bit_width) {
  1606. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1607. << ext_inst_name() << ": "
  1608. << "expected operand Offset to be of type size_t ("
  1609. << size_t_bit_width
  1610. << "-bit integer for the addressing model used in the module)";
  1611. }
  1612. uint32_t p_storage_class = 0;
  1613. uint32_t p_data_type = 0;
  1614. if (!_.GetPointerTypeInfo(p_type, &p_data_type, &p_storage_class)) {
  1615. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1616. << ext_inst_name() << ": "
  1617. << "expected operand P to be a pointer";
  1618. }
  1619. if (p_storage_class != SpvStorageClassUniformConstant &&
  1620. p_storage_class != SpvStorageClassGeneric &&
  1621. p_storage_class != SpvStorageClassCrossWorkgroup &&
  1622. p_storage_class != SpvStorageClassWorkgroup &&
  1623. p_storage_class != SpvStorageClassFunction) {
  1624. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1625. << ext_inst_name() << ": "
  1626. << "expected operand P storage class to be UniformConstant, "
  1627. "Generic, CrossWorkgroup, Workgroup or Function";
  1628. }
  1629. if (!_.IsFloatScalarType(p_data_type) ||
  1630. _.GetBitWidth(p_data_type) != 16) {
  1631. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1632. << ext_inst_name() << ": "
  1633. << "expected operand P data type to be 16-bit float scalar";
  1634. }
  1635. break;
  1636. }
  1637. case OpenCLLIB::Vload_halfn:
  1638. case OpenCLLIB::Vloada_halfn: {
  1639. if (!_.IsFloatVectorType(result_type)) {
  1640. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1641. << ext_inst_name() << ": "
  1642. << "expected Result Type to be a float vector type";
  1643. }
  1644. const uint32_t num_components = _.GetDimension(result_type);
  1645. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1646. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1647. << ext_inst_name() << ": "
  1648. << "expected Result Type to have 2, 3, 4, 8 or 16 components";
  1649. }
  1650. const uint32_t offset_type = _.GetOperandTypeId(inst, 4);
  1651. const uint32_t p_type = _.GetOperandTypeId(inst, 5);
  1652. const uint32_t size_t_bit_width = GetSizeTBitWidth(_);
  1653. if (!size_t_bit_width) {
  1654. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1655. << ext_inst_name()
  1656. << " can only be used with physical addressing models";
  1657. }
  1658. if (!_.IsIntScalarType(offset_type) ||
  1659. _.GetBitWidth(offset_type) != size_t_bit_width) {
  1660. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1661. << ext_inst_name() << ": "
  1662. << "expected operand Offset to be of type size_t ("
  1663. << size_t_bit_width
  1664. << "-bit integer for the addressing model used in the module)";
  1665. }
  1666. uint32_t p_storage_class = 0;
  1667. uint32_t p_data_type = 0;
  1668. if (!_.GetPointerTypeInfo(p_type, &p_data_type, &p_storage_class)) {
  1669. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1670. << ext_inst_name() << ": "
  1671. << "expected operand P to be a pointer";
  1672. }
  1673. if (p_storage_class != SpvStorageClassUniformConstant &&
  1674. p_storage_class != SpvStorageClassGeneric &&
  1675. p_storage_class != SpvStorageClassCrossWorkgroup &&
  1676. p_storage_class != SpvStorageClassWorkgroup &&
  1677. p_storage_class != SpvStorageClassFunction) {
  1678. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1679. << ext_inst_name() << ": "
  1680. << "expected operand P storage class to be UniformConstant, "
  1681. "Generic, CrossWorkgroup, Workgroup or Function";
  1682. }
  1683. if (!_.IsFloatScalarType(p_data_type) ||
  1684. _.GetBitWidth(p_data_type) != 16) {
  1685. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1686. << ext_inst_name() << ": "
  1687. << "expected operand P data type to be 16-bit float scalar";
  1688. }
  1689. const uint32_t n_value = inst->word(7);
  1690. if (num_components != n_value) {
  1691. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1692. << ext_inst_name() << ": "
  1693. << "expected literal N to be equal to the number of "
  1694. "components of Result Type";
  1695. }
  1696. break;
  1697. }
  1698. case OpenCLLIB::Vstore_half:
  1699. case OpenCLLIB::Vstore_half_r:
  1700. case OpenCLLIB::Vstore_halfn:
  1701. case OpenCLLIB::Vstore_halfn_r:
  1702. case OpenCLLIB::Vstorea_halfn:
  1703. case OpenCLLIB::Vstorea_halfn_r: {
  1704. if (_.GetIdOpcode(result_type) != SpvOpTypeVoid) {
  1705. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1706. << ext_inst_name() << ": expected Result Type to be void";
  1707. }
  1708. const uint32_t data_type = _.GetOperandTypeId(inst, 4);
  1709. const uint32_t offset_type = _.GetOperandTypeId(inst, 5);
  1710. const uint32_t p_type = _.GetOperandTypeId(inst, 6);
  1711. const uint32_t data_type_bit_width = _.GetBitWidth(data_type);
  1712. if (ext_inst_key == OpenCLLIB::Vstore_half ||
  1713. ext_inst_key == OpenCLLIB::Vstore_half_r) {
  1714. if (!_.IsFloatScalarType(data_type) ||
  1715. (data_type_bit_width != 32 && data_type_bit_width != 64)) {
  1716. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1717. << ext_inst_name() << ": "
  1718. << "expected Data to be a 32 or 64-bit float scalar";
  1719. }
  1720. } else {
  1721. if (!_.IsFloatVectorType(data_type) ||
  1722. (data_type_bit_width != 32 && data_type_bit_width != 64)) {
  1723. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1724. << ext_inst_name() << ": "
  1725. << "expected Data to be a 32 or 64-bit float vector";
  1726. }
  1727. const uint32_t num_components = _.GetDimension(data_type);
  1728. if (num_components > 4 && num_components != 8 &&
  1729. num_components != 16) {
  1730. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1731. << ext_inst_name() << ": "
  1732. << "expected Data to have 2, 3, 4, 8 or 16 components";
  1733. }
  1734. }
  1735. const uint32_t size_t_bit_width = GetSizeTBitWidth(_);
  1736. if (!size_t_bit_width) {
  1737. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1738. << ext_inst_name()
  1739. << " can only be used with physical addressing models";
  1740. }
  1741. if (!_.IsIntScalarType(offset_type) ||
  1742. _.GetBitWidth(offset_type) != size_t_bit_width) {
  1743. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1744. << ext_inst_name() << ": "
  1745. << "expected operand Offset to be of type size_t ("
  1746. << size_t_bit_width
  1747. << "-bit integer for the addressing model used in the module)";
  1748. }
  1749. uint32_t p_storage_class = 0;
  1750. uint32_t p_data_type = 0;
  1751. if (!_.GetPointerTypeInfo(p_type, &p_data_type, &p_storage_class)) {
  1752. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1753. << ext_inst_name() << ": "
  1754. << "expected operand P to be a pointer";
  1755. }
  1756. if (p_storage_class != SpvStorageClassGeneric &&
  1757. p_storage_class != SpvStorageClassCrossWorkgroup &&
  1758. p_storage_class != SpvStorageClassWorkgroup &&
  1759. p_storage_class != SpvStorageClassFunction) {
  1760. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1761. << ext_inst_name() << ": "
  1762. << "expected operand P storage class to be Generic, "
  1763. "CrossWorkgroup, Workgroup or Function";
  1764. }
  1765. if (!_.IsFloatScalarType(p_data_type) ||
  1766. _.GetBitWidth(p_data_type) != 16) {
  1767. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1768. << ext_inst_name() << ": "
  1769. << "expected operand P data type to be 16-bit float scalar";
  1770. }
  1771. // Rounding mode enum is checked by assembler.
  1772. break;
  1773. }
  1774. case OpenCLLIB::Shuffle:
  1775. case OpenCLLIB::Shuffle2: {
  1776. if (!_.IsFloatVectorType(result_type) &&
  1777. !_.IsIntVectorType(result_type)) {
  1778. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1779. << ext_inst_name() << ": "
  1780. << "expected Result Type to be an int or float vector type";
  1781. }
  1782. const uint32_t result_num_components = _.GetDimension(result_type);
  1783. if (result_num_components != 2 && result_num_components != 4 &&
  1784. result_num_components != 8 && result_num_components != 16) {
  1785. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1786. << ext_inst_name() << ": "
  1787. << "expected Result Type to have 2, 4, 8 or 16 components";
  1788. }
  1789. uint32_t operand_index = 4;
  1790. const uint32_t x_type = _.GetOperandTypeId(inst, operand_index++);
  1791. if (ext_inst_key == OpenCLLIB::Shuffle2) {
  1792. const uint32_t y_type = _.GetOperandTypeId(inst, operand_index++);
  1793. if (x_type != y_type) {
  1794. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1795. << ext_inst_name() << ": "
  1796. << "expected operands X and Y to be of the same type";
  1797. }
  1798. }
  1799. const uint32_t shuffle_mask_type =
  1800. _.GetOperandTypeId(inst, operand_index++);
  1801. if (!_.IsFloatVectorType(x_type) && !_.IsIntVectorType(x_type)) {
  1802. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1803. << ext_inst_name() << ": "
  1804. << "expected operand X to be an int or float vector";
  1805. }
  1806. const uint32_t x_num_components = _.GetDimension(x_type);
  1807. if (x_num_components != 2 && x_num_components != 4 &&
  1808. x_num_components != 8 && x_num_components != 16) {
  1809. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1810. << ext_inst_name() << ": "
  1811. << "expected operand X to have 2, 4, 8 or 16 components";
  1812. }
  1813. const uint32_t result_component_type = _.GetComponentType(result_type);
  1814. if (result_component_type != _.GetComponentType(x_type)) {
  1815. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1816. << ext_inst_name() << ": "
  1817. << "expected operand X and Result Type to have equal "
  1818. "component types";
  1819. }
  1820. if (!_.IsIntVectorType(shuffle_mask_type)) {
  1821. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1822. << ext_inst_name() << ": "
  1823. << "expected operand Shuffle Mask to be an int vector";
  1824. }
  1825. if (result_num_components != _.GetDimension(shuffle_mask_type)) {
  1826. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1827. << ext_inst_name() << ": "
  1828. << "expected operand Shuffle Mask to have the same number of "
  1829. "components as Result Type";
  1830. }
  1831. if (_.GetBitWidth(result_component_type) !=
  1832. _.GetBitWidth(shuffle_mask_type)) {
  1833. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1834. << ext_inst_name() << ": "
  1835. << "expected operand Shuffle Mask components to have the same "
  1836. "bit width as Result Type components";
  1837. }
  1838. break;
  1839. }
  1840. case OpenCLLIB::Printf: {
  1841. if (!_.IsIntScalarType(result_type) ||
  1842. _.GetBitWidth(result_type) != 32) {
  1843. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1844. << ext_inst_name() << ": "
  1845. << "expected Result Type to be a 32-bit int type";
  1846. }
  1847. const uint32_t format_type = _.GetOperandTypeId(inst, 4);
  1848. uint32_t format_storage_class = 0;
  1849. uint32_t format_data_type = 0;
  1850. if (!_.GetPointerTypeInfo(format_type, &format_data_type,
  1851. &format_storage_class)) {
  1852. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1853. << ext_inst_name() << ": "
  1854. << "expected operand Format to be a pointer";
  1855. }
  1856. if (format_storage_class != SpvStorageClassUniformConstant) {
  1857. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1858. << ext_inst_name() << ": "
  1859. << "expected Format storage class to be UniformConstant";
  1860. }
  1861. if (!_.IsIntScalarType(format_data_type) ||
  1862. _.GetBitWidth(format_data_type) != 8) {
  1863. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1864. << ext_inst_name() << ": "
  1865. << "expected Format data type to be 8-bit int";
  1866. }
  1867. break;
  1868. }
  1869. case OpenCLLIB::Prefetch: {
  1870. if (_.GetIdOpcode(result_type) != SpvOpTypeVoid) {
  1871. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1872. << ext_inst_name() << ": expected Result Type to be void";
  1873. }
  1874. const uint32_t p_type = _.GetOperandTypeId(inst, 4);
  1875. const uint32_t num_elements_type = _.GetOperandTypeId(inst, 5);
  1876. uint32_t p_storage_class = 0;
  1877. uint32_t p_data_type = 0;
  1878. if (!_.GetPointerTypeInfo(p_type, &p_data_type, &p_storage_class)) {
  1879. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1880. << ext_inst_name() << ": "
  1881. << "expected operand Ptr to be a pointer";
  1882. }
  1883. if (p_storage_class != SpvStorageClassCrossWorkgroup) {
  1884. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1885. << ext_inst_name() << ": "
  1886. << "expected operand Ptr storage class to be CrossWorkgroup";
  1887. }
  1888. if (!_.IsFloatScalarOrVectorType(p_data_type) &&
  1889. !_.IsIntScalarOrVectorType(p_data_type)) {
  1890. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1891. << ext_inst_name() << ": "
  1892. << "expected Ptr data type to be int or float scalar or "
  1893. "vector";
  1894. }
  1895. const uint32_t num_components = _.GetDimension(p_data_type);
  1896. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1897. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1898. << ext_inst_name() << ": "
  1899. << "expected Result Type to be a scalar or a vector with 2, "
  1900. "3, 4, 8 or 16 components";
  1901. }
  1902. const uint32_t size_t_bit_width = GetSizeTBitWidth(_);
  1903. if (!size_t_bit_width) {
  1904. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1905. << ext_inst_name()
  1906. << " can only be used with physical addressing models";
  1907. }
  1908. if (!_.IsIntScalarType(num_elements_type) ||
  1909. _.GetBitWidth(num_elements_type) != size_t_bit_width) {
  1910. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1911. << ext_inst_name() << ": "
  1912. << "expected operand Num Elements to be of type size_t ("
  1913. << size_t_bit_width
  1914. << "-bit integer for the addressing model used in the module)";
  1915. }
  1916. break;
  1917. }
  1918. }
  1919. } else if (ext_inst_type == SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100) {
  1920. if (!_.IsVoidType(result_type)) {
  1921. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1922. << ext_inst_name() << ": "
  1923. << "expected result type must be a result id of "
  1924. << "OpTypeVoid";
  1925. }
  1926. auto num_words = inst->words().size();
  1927. const OpenCLDebugInfo100Instructions ext_inst_key =
  1928. OpenCLDebugInfo100Instructions(ext_inst_index);
  1929. switch (ext_inst_key) {
  1930. case OpenCLDebugInfo100DebugInfoNone:
  1931. case OpenCLDebugInfo100DebugNoScope:
  1932. case OpenCLDebugInfo100DebugOperation:
  1933. // The binary parser validates the opcode for DebugInfoNone,
  1934. // DebugNoScope, DebugOperation, and the literal values don't need
  1935. // further checks.
  1936. break;
  1937. case OpenCLDebugInfo100DebugCompilationUnit: {
  1938. CHECK_DEBUG_OPERAND("Source", OpenCLDebugInfo100DebugSource, 7);
  1939. break;
  1940. }
  1941. case OpenCLDebugInfo100DebugSource: {
  1942. CHECK_OPERAND("File", SpvOpString, 5);
  1943. if (num_words == 7) CHECK_OPERAND("Text", SpvOpString, 6);
  1944. break;
  1945. }
  1946. case OpenCLDebugInfo100DebugTypeBasic: {
  1947. CHECK_OPERAND("Name", SpvOpString, 5);
  1948. CHECK_OPERAND("Size", SpvOpConstant, 6);
  1949. // "Encoding" param is already validated by the binary parsing stage.
  1950. break;
  1951. }
  1952. case OpenCLDebugInfo100DebugTypePointer:
  1953. case OpenCLDebugInfo100DebugTypeQualifier: {
  1954. auto validate_base_type =
  1955. ValidateOperandBaseType(_, inst, 5, ext_inst_name);
  1956. if (validate_base_type != SPV_SUCCESS) return validate_base_type;
  1957. break;
  1958. }
  1959. case OpenCLDebugInfo100DebugTypeVector: {
  1960. auto validate_base_type =
  1961. ValidateOperandBaseType(_, inst, 5, ext_inst_name);
  1962. if (validate_base_type != SPV_SUCCESS) return validate_base_type;
  1963. uint32_t component_count = inst->word(6);
  1964. if (!component_count || component_count > 4) {
  1965. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1966. << ext_inst_name() << ": Component Count must be positive "
  1967. << "integer less than or equal to 4";
  1968. }
  1969. break;
  1970. }
  1971. case OpenCLDebugInfo100DebugTypeArray: {
  1972. auto validate_base_type =
  1973. ValidateOperandDebugType(_, "Base Type", inst, 5, ext_inst_name);
  1974. if (validate_base_type != SPV_SUCCESS) return validate_base_type;
  1975. for (uint32_t i = 6; i < num_words; ++i) {
  1976. CHECK_OPERAND("Component Count", SpvOpConstant, i);
  1977. auto* component_count = _.FindDef(inst->word(i));
  1978. if (!_.IsIntScalarType(component_count->type_id()) ||
  1979. !component_count->word(3)) {
  1980. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1981. << ext_inst_name() << ": Component Count must be positive "
  1982. << "integer";
  1983. }
  1984. }
  1985. break;
  1986. }
  1987. case OpenCLDebugInfo100DebugTypedef: {
  1988. CHECK_OPERAND("Name", SpvOpString, 5);
  1989. auto validate_base_type =
  1990. ValidateOperandBaseType(_, inst, 6, ext_inst_name);
  1991. if (validate_base_type != SPV_SUCCESS) return validate_base_type;
  1992. CHECK_DEBUG_OPERAND("Source", OpenCLDebugInfo100DebugSource, 7);
  1993. auto validate_parent =
  1994. ValidateOperandLexicalScope(_, "Parent", inst, 10, ext_inst_name);
  1995. if (validate_parent != SPV_SUCCESS) return validate_parent;
  1996. break;
  1997. }
  1998. case OpenCLDebugInfo100DebugTypeFunction: {
  1999. auto* return_type = _.FindDef(inst->word(6));
  2000. if (return_type->opcode() != SpvOpTypeVoid) {
  2001. auto validate_return = ValidateOperandDebugType(
  2002. _, "Return Type", inst, 6, ext_inst_name);
  2003. if (validate_return != SPV_SUCCESS) return validate_return;
  2004. }
  2005. for (uint32_t word_index = 7; word_index < num_words; ++word_index) {
  2006. auto validate_param = ValidateOperandDebugType(
  2007. _, "Parameter Types", inst, word_index, ext_inst_name);
  2008. if (validate_param != SPV_SUCCESS) return validate_param;
  2009. }
  2010. break;
  2011. }
  2012. case OpenCLDebugInfo100DebugTypeEnum: {
  2013. CHECK_OPERAND("Name", SpvOpString, 5);
  2014. if (!DoesDebugInfoOperandMatchExpectation(
  2015. _,
  2016. [](OpenCLDebugInfo100Instructions dbg_inst) {
  2017. return dbg_inst == OpenCLDebugInfo100DebugInfoNone;
  2018. },
  2019. inst, 6)) {
  2020. auto validate_underlying_type = ValidateOperandDebugType(
  2021. _, "Underlying Types", inst, 6, ext_inst_name);
  2022. if (validate_underlying_type != SPV_SUCCESS)
  2023. return validate_underlying_type;
  2024. }
  2025. CHECK_DEBUG_OPERAND("Source", OpenCLDebugInfo100DebugSource, 7);
  2026. auto validate_parent =
  2027. ValidateOperandLexicalScope(_, "Parent", inst, 10, ext_inst_name);
  2028. if (validate_parent != SPV_SUCCESS) return validate_parent;
  2029. CHECK_OPERAND("Size", SpvOpConstant, 11);
  2030. auto* size = _.FindDef(inst->word(11));
  2031. if (!_.IsIntScalarType(size->type_id()) || !size->word(3)) {
  2032. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2033. << ext_inst_name() << ": expected operand Size is a "
  2034. << "positive integer";
  2035. }
  2036. for (uint32_t word_index = 13; word_index + 1 < num_words;
  2037. word_index += 2) {
  2038. CHECK_OPERAND("Value", SpvOpConstant, word_index);
  2039. CHECK_OPERAND("Name", SpvOpString, word_index + 1);
  2040. }
  2041. break;
  2042. }
  2043. case OpenCLDebugInfo100DebugTypeComposite: {
  2044. CHECK_OPERAND("Name", SpvOpString, 5);
  2045. CHECK_DEBUG_OPERAND("Source", OpenCLDebugInfo100DebugSource, 7);
  2046. auto validate_parent =
  2047. ValidateOperandLexicalScope(_, "Parent", inst, 10, ext_inst_name);
  2048. if (validate_parent != SPV_SUCCESS) return validate_parent;
  2049. CHECK_OPERAND("Linkage Name", SpvOpString, 11);
  2050. if (!DoesDebugInfoOperandMatchExpectation(
  2051. _,
  2052. [](OpenCLDebugInfo100Instructions dbg_inst) {
  2053. return dbg_inst == OpenCLDebugInfo100DebugInfoNone;
  2054. },
  2055. inst, 12)) {
  2056. CHECK_OPERAND("Size", SpvOpConstant, 12);
  2057. }
  2058. for (uint32_t word_index = 14; word_index < num_words; ++word_index) {
  2059. if (!DoesDebugInfoOperandMatchExpectation(
  2060. _,
  2061. [](OpenCLDebugInfo100Instructions dbg_inst) {
  2062. return dbg_inst == OpenCLDebugInfo100DebugTypeMember ||
  2063. dbg_inst == OpenCLDebugInfo100DebugFunction ||
  2064. dbg_inst == OpenCLDebugInfo100DebugTypeInheritance;
  2065. },
  2066. inst, word_index)) {
  2067. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2068. << ext_inst_name() << ": "
  2069. << "expected operand Members "
  2070. << "must be DebugTypeMember, DebugFunction, or "
  2071. "DebugTypeInheritance";
  2072. }
  2073. }
  2074. break;
  2075. }
  2076. case OpenCLDebugInfo100DebugTypeMember: {
  2077. CHECK_OPERAND("Name", SpvOpString, 5);
  2078. auto validate_type =
  2079. ValidateOperandDebugType(_, "Type", inst, 6, ext_inst_name);
  2080. if (validate_type != SPV_SUCCESS) return validate_type;
  2081. CHECK_DEBUG_OPERAND("Source", OpenCLDebugInfo100DebugSource, 7);
  2082. CHECK_DEBUG_OPERAND("Parent", OpenCLDebugInfo100DebugTypeComposite, 10);
  2083. CHECK_OPERAND("Offset", SpvOpConstant, 11);
  2084. CHECK_OPERAND("Size", SpvOpConstant, 12);
  2085. if (num_words == 15) CHECK_OPERAND("Value", SpvOpConstant, 14);
  2086. break;
  2087. }
  2088. case OpenCLDebugInfo100DebugTypeInheritance: {
  2089. CHECK_DEBUG_OPERAND("Child", OpenCLDebugInfo100DebugTypeComposite, 5);
  2090. auto* debug_inst = _.FindDef(inst->word(5));
  2091. auto composite_type =
  2092. OpenCLDebugInfo100DebugCompositeType(debug_inst->word(6));
  2093. if (composite_type != OpenCLDebugInfo100Class &&
  2094. composite_type != OpenCLDebugInfo100Structure) {
  2095. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2096. << ext_inst_name() << ": "
  2097. << "expected operand Child must be class or struct debug type";
  2098. }
  2099. CHECK_DEBUG_OPERAND("Parent", OpenCLDebugInfo100DebugTypeComposite, 6);
  2100. debug_inst = _.FindDef(inst->word(6));
  2101. composite_type =
  2102. OpenCLDebugInfo100DebugCompositeType(debug_inst->word(6));
  2103. if (composite_type != OpenCLDebugInfo100Class &&
  2104. composite_type != OpenCLDebugInfo100Structure) {
  2105. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2106. << ext_inst_name() << ": "
  2107. << "expected operand Parent must be class or struct debug "
  2108. "type";
  2109. }
  2110. CHECK_OPERAND("Offset", SpvOpConstant, 7);
  2111. CHECK_OPERAND("Size", SpvOpConstant, 8);
  2112. break;
  2113. }
  2114. case OpenCLDebugInfo100DebugFunction: {
  2115. CHECK_OPERAND("Name", SpvOpString, 5);
  2116. auto validate_type =
  2117. ValidateOperandDebugType(_, "Type", inst, 6, ext_inst_name);
  2118. if (validate_type != SPV_SUCCESS) return validate_type;
  2119. CHECK_DEBUG_OPERAND("Source", OpenCLDebugInfo100DebugSource, 7);
  2120. auto validate_parent =
  2121. ValidateOperandLexicalScope(_, "Parent", inst, 10, ext_inst_name);
  2122. if (validate_parent != SPV_SUCCESS) return validate_parent;
  2123. CHECK_OPERAND("Linkage Name", SpvOpString, 11);
  2124. // TODO: The current OpenCL.100.DebugInfo spec says "Function
  2125. // is an OpFunction which is described by this instruction.".
  2126. // However, the function definition can be opted-out e.g.,
  2127. // inlining. We assume that Function operand can be a
  2128. // DebugInfoNone, but we must discuss it and update the spec.
  2129. if (!DoesDebugInfoOperandMatchExpectation(
  2130. _,
  2131. [](OpenCLDebugInfo100Instructions dbg_inst) {
  2132. return dbg_inst == OpenCLDebugInfo100DebugInfoNone;
  2133. },
  2134. inst, 14)) {
  2135. CHECK_OPERAND("Function", SpvOpFunction, 14);
  2136. }
  2137. if (num_words == 16) {
  2138. CHECK_DEBUG_OPERAND("Declaration",
  2139. OpenCLDebugInfo100DebugFunctionDeclaration, 15);
  2140. }
  2141. break;
  2142. }
  2143. case OpenCLDebugInfo100DebugFunctionDeclaration: {
  2144. CHECK_OPERAND("Name", SpvOpString, 5);
  2145. auto validate_type =
  2146. ValidateOperandDebugType(_, "Type", inst, 6, ext_inst_name);
  2147. if (validate_type != SPV_SUCCESS) return validate_type;
  2148. CHECK_DEBUG_OPERAND("Source", OpenCLDebugInfo100DebugSource, 7);
  2149. auto validate_parent =
  2150. ValidateOperandLexicalScope(_, "Parent", inst, 10, ext_inst_name);
  2151. if (validate_parent != SPV_SUCCESS) return validate_parent;
  2152. CHECK_OPERAND("Linkage Name", SpvOpString, 11);
  2153. break;
  2154. }
  2155. case OpenCLDebugInfo100DebugLexicalBlock: {
  2156. CHECK_DEBUG_OPERAND("Source", OpenCLDebugInfo100DebugSource, 5);
  2157. auto validate_parent =
  2158. ValidateOperandLexicalScope(_, "Parent", inst, 8, ext_inst_name);
  2159. if (validate_parent != SPV_SUCCESS) return validate_parent;
  2160. if (num_words == 10) CHECK_OPERAND("Name", SpvOpString, 9);
  2161. break;
  2162. }
  2163. case OpenCLDebugInfo100DebugScope: {
  2164. // TODO(https://gitlab.khronos.org/spirv/SPIR-V/issues/533): We are
  2165. // still in spec discussion about what must be "Scope" operand of
  2166. // DebugScope. Update this code if the conclusion is different.
  2167. auto validate_scope =
  2168. ValidateOperandLexicalScope(_, "Scope", inst, 5, ext_inst_name);
  2169. if (validate_scope != SPV_SUCCESS) return validate_scope;
  2170. if (num_words == 7) {
  2171. CHECK_DEBUG_OPERAND("Inlined At", OpenCLDebugInfo100DebugInlinedAt,
  2172. 6);
  2173. }
  2174. break;
  2175. }
  2176. case OpenCLDebugInfo100DebugLocalVariable: {
  2177. CHECK_OPERAND("Name", SpvOpString, 5);
  2178. auto validate_type =
  2179. ValidateOperandDebugType(_, "Type", inst, 6, ext_inst_name);
  2180. if (validate_type != SPV_SUCCESS) return validate_type;
  2181. CHECK_DEBUG_OPERAND("Source", OpenCLDebugInfo100DebugSource, 7);
  2182. auto validate_parent =
  2183. ValidateOperandLexicalScope(_, "Parent", inst, 10, ext_inst_name);
  2184. if (validate_parent != SPV_SUCCESS) return validate_parent;
  2185. break;
  2186. }
  2187. case OpenCLDebugInfo100DebugDeclare: {
  2188. CHECK_DEBUG_OPERAND("Local Variable",
  2189. OpenCLDebugInfo100DebugLocalVariable, 5);
  2190. // TODO: We must discuss DebugDeclare.Variable of OpenCL.100.DebugInfo.
  2191. // Currently, it says "Variable must be an id of OpVariable instruction
  2192. // which defines the local variable.", but we want to allow
  2193. // OpFunctionParameter as well.
  2194. auto* operand = _.FindDef(inst->word(6));
  2195. if (operand->opcode() != SpvOpVariable &&
  2196. operand->opcode() != SpvOpFunctionParameter) {
  2197. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2198. << ext_inst_name() << ": "
  2199. << "expected operand Variable must be a result id of "
  2200. "OpVariable or OpFunctionParameter";
  2201. }
  2202. CHECK_DEBUG_OPERAND("Expression", OpenCLDebugInfo100DebugExpression, 7);
  2203. break;
  2204. }
  2205. case OpenCLDebugInfo100DebugExpression: {
  2206. for (uint32_t word_index = 5; word_index < num_words; ++word_index) {
  2207. CHECK_DEBUG_OPERAND("Operation", OpenCLDebugInfo100DebugOperation,
  2208. word_index);
  2209. }
  2210. break;
  2211. }
  2212. // TODO: Add validation rules for remaining cases as well.
  2213. case OpenCLDebugInfo100DebugTypePtrToMember:
  2214. case OpenCLDebugInfo100DebugTypeTemplate:
  2215. case OpenCLDebugInfo100DebugTypeTemplateParameter:
  2216. case OpenCLDebugInfo100DebugTypeTemplateTemplateParameter:
  2217. case OpenCLDebugInfo100DebugTypeTemplateParameterPack:
  2218. case OpenCLDebugInfo100DebugGlobalVariable:
  2219. case OpenCLDebugInfo100DebugLexicalBlockDiscriminator:
  2220. case OpenCLDebugInfo100DebugInlinedAt:
  2221. case OpenCLDebugInfo100DebugInlinedVariable:
  2222. case OpenCLDebugInfo100DebugValue:
  2223. case OpenCLDebugInfo100DebugMacroDef:
  2224. case OpenCLDebugInfo100DebugMacroUndef:
  2225. case OpenCLDebugInfo100DebugImportedEntity:
  2226. break;
  2227. case OpenCLDebugInfo100InstructionsMax:
  2228. assert(0);
  2229. break;
  2230. }
  2231. }
  2232. return SPV_SUCCESS;
  2233. }
  2234. spv_result_t ExtensionPass(ValidationState_t& _, const Instruction* inst) {
  2235. const SpvOp opcode = inst->opcode();
  2236. if (opcode == SpvOpExtension) return ValidateExtension(_, inst);
  2237. if (opcode == SpvOpExtInstImport) return ValidateExtInstImport(_, inst);
  2238. if (opcode == SpvOpExtInst) return ValidateExtInst(_, inst);
  2239. return SPV_SUCCESS;
  2240. }
  2241. } // namespace val
  2242. } // namespace spvtools