validate_extensions.cpp 135 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413
  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 <cstdlib>
  16. #include <sstream>
  17. #include <string>
  18. #include <vector>
  19. #include "spirv/unified1/NonSemanticClspvReflection.h"
  20. #include "NonSemanticShaderDebugInfo100.h"
  21. #include "OpenCLDebugInfo100.h"
  22. #include "source/common_debug_info.h"
  23. #include "source/diagnostic.h"
  24. #include "source/enum_string_mapping.h"
  25. #include "source/extensions.h"
  26. #include "source/latest_version_glsl_std_450_header.h"
  27. #include "source/latest_version_opencl_std_header.h"
  28. #include "source/opcode.h"
  29. #include "source/spirv_constant.h"
  30. #include "source/spirv_target_env.h"
  31. #include "source/val/instruction.h"
  32. #include "source/val/validate.h"
  33. #include "source/val/validation_state.h"
  34. namespace spvtools {
  35. namespace val {
  36. namespace {
  37. uint32_t GetSizeTBitWidth(const ValidationState_t& _) {
  38. if (_.addressing_model() == SpvAddressingModelPhysical32) return 32;
  39. if (_.addressing_model() == SpvAddressingModelPhysical64) return 64;
  40. return 0;
  41. }
  42. bool IsIntScalar(ValidationState_t& _, uint32_t id, bool must_len32,
  43. bool must_unsigned) {
  44. auto type = _.FindDef(id);
  45. if (!type || type->opcode() != SpvOpTypeInt) {
  46. return false;
  47. }
  48. if (must_len32 && type->GetOperandAs<uint32_t>(1) != 32) {
  49. return false;
  50. }
  51. return !must_unsigned || type->GetOperandAs<uint32_t>(2) == 0;
  52. }
  53. bool IsUint32Constant(ValidationState_t& _, uint32_t id) {
  54. auto inst = _.FindDef(id);
  55. if (!inst || inst->opcode() != SpvOpConstant) {
  56. return false;
  57. }
  58. return IsIntScalar(_, inst->type_id(), true, true);
  59. }
  60. uint32_t GetUint32Constant(ValidationState_t& _, uint32_t id) {
  61. auto inst = _.FindDef(id);
  62. return inst->word(3);
  63. }
  64. // Check that the operand of a debug info instruction |inst| at |word_index|
  65. // is a result id of an instruction with |expected_opcode|.
  66. spv_result_t ValidateOperandForDebugInfo(
  67. ValidationState_t& _, const std::string& operand_name,
  68. SpvOp expected_opcode, const Instruction* inst, uint32_t word_index,
  69. const std::function<std::string()>& ext_inst_name) {
  70. auto* operand = _.FindDef(inst->word(word_index));
  71. if (operand->opcode() != expected_opcode) {
  72. spv_opcode_desc desc = nullptr;
  73. if (_.grammar().lookupOpcode(expected_opcode, &desc) != SPV_SUCCESS ||
  74. !desc) {
  75. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  76. << ext_inst_name() << ": "
  77. << "expected operand " << operand_name << " is invalid";
  78. }
  79. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  80. << ext_inst_name() << ": "
  81. << "expected operand " << operand_name << " must be a result id of "
  82. << "Op" << desc->name;
  83. }
  84. return SPV_SUCCESS;
  85. }
  86. // For NonSemantic.Shader.DebugInfo.100 check that the operand of a debug info
  87. // instruction |inst| at |word_index| is a result id of a 32-bit integer
  88. // OpConstant instruction. For OpenCL.DebugInfo.100 the parameter is a literal
  89. // word so cannot be validated.
  90. spv_result_t ValidateUint32ConstantOperandForDebugInfo(
  91. ValidationState_t& _, const std::string& operand_name,
  92. const Instruction* inst, uint32_t word_index,
  93. const std::function<std::string()>& ext_inst_name) {
  94. if (!IsUint32Constant(_, inst->word(word_index))) {
  95. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  96. << ext_inst_name() << ": expected operand " << operand_name
  97. << " must be a result id of 32-bit unsigned OpConstant";
  98. }
  99. return SPV_SUCCESS;
  100. }
  101. #define CHECK_OPERAND(NAME, opcode, index) \
  102. do { \
  103. auto result = ValidateOperandForDebugInfo(_, NAME, opcode, inst, index, \
  104. ext_inst_name); \
  105. if (result != SPV_SUCCESS) return result; \
  106. } while (0)
  107. #define CHECK_CONST_UINT_OPERAND(NAME, index) \
  108. if (vulkanDebugInfo) { \
  109. auto result = ValidateUint32ConstantOperandForDebugInfo( \
  110. _, NAME, inst, index, ext_inst_name); \
  111. if (result != SPV_SUCCESS) return result; \
  112. }
  113. // True if the operand of a debug info instruction |inst| at |word_index|
  114. // satisfies |expectation| that is given as a function. Otherwise,
  115. // returns false.
  116. bool DoesDebugInfoOperandMatchExpectation(
  117. const ValidationState_t& _,
  118. const std::function<bool(CommonDebugInfoInstructions)>& expectation,
  119. const Instruction* inst, uint32_t word_index) {
  120. if (inst->words().size() <= word_index) return false;
  121. auto* debug_inst = _.FindDef(inst->word(word_index));
  122. if (debug_inst->opcode() != SpvOpExtInst ||
  123. (debug_inst->ext_inst_type() != SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100 &&
  124. debug_inst->ext_inst_type() !=
  125. SPV_EXT_INST_TYPE_NONSEMANTIC_SHADER_DEBUGINFO_100) ||
  126. !expectation(CommonDebugInfoInstructions(debug_inst->word(4)))) {
  127. return false;
  128. }
  129. return true;
  130. }
  131. // Overload for NonSemanticShaderDebugInfo100Instructions.
  132. bool DoesDebugInfoOperandMatchExpectation(
  133. const ValidationState_t& _,
  134. const std::function<bool(NonSemanticShaderDebugInfo100Instructions)>&
  135. expectation,
  136. const Instruction* inst, uint32_t word_index) {
  137. if (inst->words().size() <= word_index) return false;
  138. auto* debug_inst = _.FindDef(inst->word(word_index));
  139. if (debug_inst->opcode() != SpvOpExtInst ||
  140. (debug_inst->ext_inst_type() !=
  141. SPV_EXT_INST_TYPE_NONSEMANTIC_SHADER_DEBUGINFO_100) ||
  142. !expectation(
  143. NonSemanticShaderDebugInfo100Instructions(debug_inst->word(4)))) {
  144. return false;
  145. }
  146. return true;
  147. }
  148. // Check that the operand of a debug info instruction |inst| at |word_index|
  149. // is a result id of an debug info instruction whose debug instruction type
  150. // is |expected_debug_inst|.
  151. spv_result_t ValidateDebugInfoOperand(
  152. ValidationState_t& _, const std::string& debug_inst_name,
  153. CommonDebugInfoInstructions expected_debug_inst, const Instruction* inst,
  154. uint32_t word_index, const std::function<std::string()>& ext_inst_name) {
  155. std::function<bool(CommonDebugInfoInstructions)> expectation =
  156. [expected_debug_inst](CommonDebugInfoInstructions dbg_inst) {
  157. return dbg_inst == expected_debug_inst;
  158. };
  159. if (DoesDebugInfoOperandMatchExpectation(_, expectation, inst, word_index))
  160. return SPV_SUCCESS;
  161. spv_ext_inst_desc desc = nullptr;
  162. if (_.grammar().lookupExtInst(inst->ext_inst_type(), expected_debug_inst,
  163. &desc) != SPV_SUCCESS ||
  164. !desc) {
  165. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  166. << ext_inst_name() << ": "
  167. << "expected operand " << debug_inst_name << " is invalid";
  168. }
  169. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  170. << ext_inst_name() << ": "
  171. << "expected operand " << debug_inst_name << " must be a result id of "
  172. << desc->name;
  173. }
  174. #define CHECK_DEBUG_OPERAND(NAME, debug_opcode, index) \
  175. do { \
  176. auto result = ValidateDebugInfoOperand(_, NAME, debug_opcode, inst, index, \
  177. ext_inst_name); \
  178. if (result != SPV_SUCCESS) return result; \
  179. } while (0)
  180. // Check that the operand of a debug info instruction |inst| at |word_index|
  181. // is a result id of an debug info instruction with DebugTypeBasic.
  182. spv_result_t ValidateOperandBaseType(
  183. ValidationState_t& _, const Instruction* inst, uint32_t word_index,
  184. const std::function<std::string()>& ext_inst_name) {
  185. return ValidateDebugInfoOperand(_, "Base Type", CommonDebugInfoDebugTypeBasic,
  186. inst, word_index, ext_inst_name);
  187. }
  188. // Check that the operand of a debug info instruction |inst| at |word_index|
  189. // is a result id of a debug lexical scope instruction which is one of
  190. // DebugCompilationUnit, DebugFunction, DebugLexicalBlock, or
  191. // DebugTypeComposite.
  192. spv_result_t ValidateOperandLexicalScope(
  193. ValidationState_t& _, const std::string& debug_inst_name,
  194. const Instruction* inst, uint32_t word_index,
  195. const std::function<std::string()>& ext_inst_name) {
  196. std::function<bool(CommonDebugInfoInstructions)> expectation =
  197. [](CommonDebugInfoInstructions dbg_inst) {
  198. return dbg_inst == CommonDebugInfoDebugCompilationUnit ||
  199. dbg_inst == CommonDebugInfoDebugFunction ||
  200. dbg_inst == CommonDebugInfoDebugLexicalBlock ||
  201. dbg_inst == CommonDebugInfoDebugTypeComposite;
  202. };
  203. if (DoesDebugInfoOperandMatchExpectation(_, expectation, inst, word_index))
  204. return SPV_SUCCESS;
  205. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  206. << ext_inst_name() << ": "
  207. << "expected operand " << debug_inst_name
  208. << " must be a result id of a lexical scope";
  209. }
  210. // Check that the operand of a debug info instruction |inst| at |word_index|
  211. // is a result id of a debug type instruction (See DebugTypeXXX in
  212. // "4.3. Type instructions" section of OpenCL.DebugInfo.100 spec.
  213. spv_result_t ValidateOperandDebugType(
  214. ValidationState_t& _, const std::string& debug_inst_name,
  215. const Instruction* inst, uint32_t word_index,
  216. const std::function<std::string()>& ext_inst_name,
  217. bool allow_template_param) {
  218. // Check for NonSemanticShaderDebugInfo100 specific types.
  219. if (inst->ext_inst_type() ==
  220. SPV_EXT_INST_TYPE_NONSEMANTIC_SHADER_DEBUGINFO_100) {
  221. std::function<bool(NonSemanticShaderDebugInfo100Instructions)> expectation =
  222. [](NonSemanticShaderDebugInfo100Instructions dbg_inst) {
  223. return dbg_inst == NonSemanticShaderDebugInfo100DebugTypeMatrix;
  224. };
  225. if (DoesDebugInfoOperandMatchExpectation(_, expectation, inst, word_index))
  226. return SPV_SUCCESS;
  227. }
  228. // Check for common types.
  229. std::function<bool(CommonDebugInfoInstructions)> expectation =
  230. [&allow_template_param](CommonDebugInfoInstructions dbg_inst) {
  231. if (allow_template_param &&
  232. (dbg_inst == CommonDebugInfoDebugTypeTemplateParameter ||
  233. dbg_inst == CommonDebugInfoDebugTypeTemplateTemplateParameter)) {
  234. return true;
  235. }
  236. return CommonDebugInfoDebugTypeBasic <= dbg_inst &&
  237. dbg_inst <= CommonDebugInfoDebugTypeTemplate;
  238. };
  239. if (DoesDebugInfoOperandMatchExpectation(_, expectation, inst, word_index))
  240. return SPV_SUCCESS;
  241. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  242. << ext_inst_name() << ": "
  243. << "expected operand " << debug_inst_name
  244. << " is not a valid debug type";
  245. }
  246. spv_result_t ValidateClspvReflectionKernel(ValidationState_t& _,
  247. const Instruction* inst) {
  248. const auto kernel_id = inst->GetOperandAs<uint32_t>(4);
  249. const auto kernel = _.FindDef(kernel_id);
  250. if (kernel->opcode() != SpvOpFunction) {
  251. return _.diag(SPV_ERROR_INVALID_ID, inst)
  252. << "Kernel does not reference a function";
  253. }
  254. bool found_kernel = false;
  255. for (auto entry_point : _.entry_points()) {
  256. if (entry_point == kernel_id) {
  257. found_kernel = true;
  258. break;
  259. }
  260. }
  261. if (!found_kernel) {
  262. return _.diag(SPV_ERROR_INVALID_ID, inst)
  263. << "Kernel does not reference an entry-point";
  264. }
  265. const auto* exec_models = _.GetExecutionModels(kernel_id);
  266. if (!exec_models || exec_models->empty()) {
  267. return _.diag(SPV_ERROR_INVALID_ID, inst)
  268. << "Kernel does not reference an entry-point";
  269. }
  270. for (auto exec_model : *exec_models) {
  271. if (exec_model != SpvExecutionModelGLCompute) {
  272. return _.diag(SPV_ERROR_INVALID_ID, inst)
  273. << "Kernel must refer only to GLCompute entry-points";
  274. }
  275. }
  276. auto name = _.FindDef(inst->GetOperandAs<uint32_t>(5));
  277. if (!name || name->opcode() != SpvOpString) {
  278. return _.diag(SPV_ERROR_INVALID_ID, inst) << "Name must be an OpString";
  279. }
  280. const std::string name_str = name->GetOperandAs<std::string>(1);
  281. bool found = false;
  282. for (auto& desc : _.entry_point_descriptions(kernel_id)) {
  283. if (name_str == desc.name) {
  284. found = true;
  285. break;
  286. }
  287. }
  288. if (!found) {
  289. return _.diag(SPV_ERROR_INVALID_ID, inst)
  290. << "Name must match an entry-point for Kernel";
  291. }
  292. return SPV_SUCCESS;
  293. }
  294. spv_result_t ValidateClspvReflectionArgumentInfo(ValidationState_t& _,
  295. const Instruction* inst) {
  296. const auto num_operands = inst->operands().size();
  297. if (_.GetIdOpcode(inst->GetOperandAs<uint32_t>(4)) != SpvOpString) {
  298. return _.diag(SPV_ERROR_INVALID_ID, inst) << "Name must be an OpString";
  299. }
  300. if (num_operands > 5) {
  301. if (_.GetIdOpcode(inst->GetOperandAs<uint32_t>(5)) != SpvOpString) {
  302. return _.diag(SPV_ERROR_INVALID_ID, inst)
  303. << "TypeName must be an OpString";
  304. }
  305. }
  306. if (num_operands > 6) {
  307. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(6))) {
  308. return _.diag(SPV_ERROR_INVALID_ID, inst)
  309. << "AddressQualifier must be a 32-bit unsigned integer "
  310. "OpConstant";
  311. }
  312. }
  313. if (num_operands > 7) {
  314. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(7))) {
  315. return _.diag(SPV_ERROR_INVALID_ID, inst)
  316. << "AccessQualifier must be a 32-bit unsigned integer "
  317. "OpConstant";
  318. }
  319. }
  320. if (num_operands > 8) {
  321. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(8))) {
  322. return _.diag(SPV_ERROR_INVALID_ID, inst)
  323. << "TypeQualifier must be a 32-bit unsigned integer "
  324. "OpConstant";
  325. }
  326. }
  327. return SPV_SUCCESS;
  328. }
  329. spv_result_t ValidateKernelDecl(ValidationState_t& _, const Instruction* inst) {
  330. const auto decl_id = inst->GetOperandAs<uint32_t>(4);
  331. const auto decl = _.FindDef(decl_id);
  332. if (!decl || decl->opcode() != SpvOpExtInst) {
  333. return _.diag(SPV_ERROR_INVALID_ID, inst)
  334. << "Kernel must be a Kernel extended instruction";
  335. }
  336. if (decl->GetOperandAs<uint32_t>(2) != inst->GetOperandAs<uint32_t>(2)) {
  337. return _.diag(SPV_ERROR_INVALID_ID, inst)
  338. << "Kernel must be from the same extended instruction import";
  339. }
  340. const auto ext_inst =
  341. decl->GetOperandAs<NonSemanticClspvReflectionInstructions>(3);
  342. if (ext_inst != NonSemanticClspvReflectionKernel) {
  343. return _.diag(SPV_ERROR_INVALID_ID, inst)
  344. << "Kernel must be a Kernel extended instruction";
  345. }
  346. return SPV_SUCCESS;
  347. }
  348. spv_result_t ValidateArgInfo(ValidationState_t& _, const Instruction* inst,
  349. uint32_t info_index) {
  350. auto info = _.FindDef(inst->GetOperandAs<uint32_t>(info_index));
  351. if (!info || info->opcode() != SpvOpExtInst) {
  352. return _.diag(SPV_ERROR_INVALID_ID, inst)
  353. << "ArgInfo must be an ArgumentInfo extended instruction";
  354. }
  355. if (info->GetOperandAs<uint32_t>(2) != inst->GetOperandAs<uint32_t>(2)) {
  356. return _.diag(SPV_ERROR_INVALID_ID, inst)
  357. << "ArgInfo must be from the same extended instruction import";
  358. }
  359. auto ext_inst = info->GetOperandAs<NonSemanticClspvReflectionInstructions>(3);
  360. if (ext_inst != NonSemanticClspvReflectionArgumentInfo) {
  361. return _.diag(SPV_ERROR_INVALID_ID, inst)
  362. << "ArgInfo must be an ArgumentInfo extended instruction";
  363. }
  364. return SPV_SUCCESS;
  365. }
  366. spv_result_t ValidateClspvReflectionArgumentBuffer(ValidationState_t& _,
  367. const Instruction* inst) {
  368. const auto num_operands = inst->operands().size();
  369. if (auto error = ValidateKernelDecl(_, inst)) {
  370. return error;
  371. }
  372. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(5))) {
  373. return _.diag(SPV_ERROR_INVALID_ID, inst)
  374. << "Ordinal must be a 32-bit unsigned integer OpConstant";
  375. }
  376. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(6))) {
  377. return _.diag(SPV_ERROR_INVALID_ID, inst)
  378. << "DescriptorSet must be a 32-bit unsigned integer OpConstant";
  379. }
  380. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(7))) {
  381. return _.diag(SPV_ERROR_INVALID_ID, inst)
  382. << "Binding must be a 32-bit unsigned integer OpConstant";
  383. }
  384. if (num_operands == 9) {
  385. if (auto error = ValidateArgInfo(_, inst, 8)) {
  386. return error;
  387. }
  388. }
  389. return SPV_SUCCESS;
  390. }
  391. spv_result_t ValidateClspvReflectionArgumentPodBuffer(ValidationState_t& _,
  392. const Instruction* inst) {
  393. const auto num_operands = inst->operands().size();
  394. if (auto error = ValidateKernelDecl(_, inst)) {
  395. return error;
  396. }
  397. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(5))) {
  398. return _.diag(SPV_ERROR_INVALID_ID, inst)
  399. << "Ordinal must be a 32-bit unsigned integer OpConstant";
  400. }
  401. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(6))) {
  402. return _.diag(SPV_ERROR_INVALID_ID, inst)
  403. << "DescriptorSet must be a 32-bit unsigned integer OpConstant";
  404. }
  405. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(7))) {
  406. return _.diag(SPV_ERROR_INVALID_ID, inst)
  407. << "Binding must be a 32-bit unsigned integer OpConstant";
  408. }
  409. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(8))) {
  410. return _.diag(SPV_ERROR_INVALID_ID, inst)
  411. << "Offset must be a 32-bit unsigned integer OpConstant";
  412. }
  413. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(9))) {
  414. return _.diag(SPV_ERROR_INVALID_ID, inst)
  415. << "Size must be a 32-bit unsigned integer OpConstant";
  416. }
  417. if (num_operands == 11) {
  418. if (auto error = ValidateArgInfo(_, inst, 10)) {
  419. return error;
  420. }
  421. }
  422. return SPV_SUCCESS;
  423. }
  424. spv_result_t ValidateClspvReflectionArgumentPodPushConstant(
  425. ValidationState_t& _, const Instruction* inst) {
  426. const auto num_operands = inst->operands().size();
  427. if (auto error = ValidateKernelDecl(_, inst)) {
  428. return error;
  429. }
  430. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(5))) {
  431. return _.diag(SPV_ERROR_INVALID_ID, inst)
  432. << "Ordinal must be a 32-bit unsigned integer OpConstant";
  433. }
  434. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(6))) {
  435. return _.diag(SPV_ERROR_INVALID_ID, inst)
  436. << "Offset must be a 32-bit unsigned integer OpConstant";
  437. }
  438. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(7))) {
  439. return _.diag(SPV_ERROR_INVALID_ID, inst)
  440. << "Size must be a 32-bit unsigned integer OpConstant";
  441. }
  442. if (num_operands == 9) {
  443. if (auto error = ValidateArgInfo(_, inst, 8)) {
  444. return error;
  445. }
  446. }
  447. return SPV_SUCCESS;
  448. }
  449. spv_result_t ValidateClspvReflectionArgumentWorkgroup(ValidationState_t& _,
  450. const Instruction* inst) {
  451. const auto num_operands = inst->operands().size();
  452. if (auto error = ValidateKernelDecl(_, inst)) {
  453. return error;
  454. }
  455. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(5))) {
  456. return _.diag(SPV_ERROR_INVALID_ID, inst)
  457. << "Ordinal must be a 32-bit unsigned integer OpConstant";
  458. }
  459. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(6))) {
  460. return _.diag(SPV_ERROR_INVALID_ID, inst)
  461. << "SpecId must be a 32-bit unsigned integer OpConstant";
  462. }
  463. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(7))) {
  464. return _.diag(SPV_ERROR_INVALID_ID, inst)
  465. << "ElemSize must be a 32-bit unsigned integer OpConstant";
  466. }
  467. if (num_operands == 9) {
  468. if (auto error = ValidateArgInfo(_, inst, 8)) {
  469. return error;
  470. }
  471. }
  472. return SPV_SUCCESS;
  473. }
  474. spv_result_t ValidateClspvReflectionSpecConstantTriple(
  475. ValidationState_t& _, const Instruction* inst) {
  476. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(4))) {
  477. return _.diag(SPV_ERROR_INVALID_ID, inst)
  478. << "X must be a 32-bit unsigned integer OpConstant";
  479. }
  480. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(5))) {
  481. return _.diag(SPV_ERROR_INVALID_ID, inst)
  482. << "Y must be a 32-bit unsigned integer OpConstant";
  483. }
  484. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(6))) {
  485. return _.diag(SPV_ERROR_INVALID_ID, inst)
  486. << "Z must be a 32-bit unsigned integer OpConstant";
  487. }
  488. return SPV_SUCCESS;
  489. }
  490. spv_result_t ValidateClspvReflectionSpecConstantWorkDim(
  491. ValidationState_t& _, const Instruction* inst) {
  492. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(4))) {
  493. return _.diag(SPV_ERROR_INVALID_ID, inst)
  494. << "Dim must be a 32-bit unsigned integer OpConstant";
  495. }
  496. return SPV_SUCCESS;
  497. }
  498. spv_result_t ValidateClspvReflectionPushConstant(ValidationState_t& _,
  499. const Instruction* inst) {
  500. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(4))) {
  501. return _.diag(SPV_ERROR_INVALID_ID, inst)
  502. << "Offset must be a 32-bit unsigned integer OpConstant";
  503. }
  504. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(5))) {
  505. return _.diag(SPV_ERROR_INVALID_ID, inst)
  506. << "Size must be a 32-bit unsigned integer OpConstant";
  507. }
  508. return SPV_SUCCESS;
  509. }
  510. spv_result_t ValidateClspvReflectionConstantData(ValidationState_t& _,
  511. const Instruction* inst) {
  512. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(4))) {
  513. return _.diag(SPV_ERROR_INVALID_ID, inst)
  514. << "DescriptorSet must be a 32-bit unsigned integer OpConstant";
  515. }
  516. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(5))) {
  517. return _.diag(SPV_ERROR_INVALID_ID, inst)
  518. << "Binding must be a 32-bit unsigned integer OpConstant";
  519. }
  520. if (_.GetIdOpcode(inst->GetOperandAs<uint32_t>(6)) != SpvOpString) {
  521. return _.diag(SPV_ERROR_INVALID_ID, inst) << "Data must be an OpString";
  522. }
  523. return SPV_SUCCESS;
  524. }
  525. spv_result_t ValidateClspvReflectionSampler(ValidationState_t& _,
  526. const Instruction* inst) {
  527. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(4))) {
  528. return _.diag(SPV_ERROR_INVALID_ID, inst)
  529. << "DescriptorSet must be a 32-bit unsigned integer OpConstant";
  530. }
  531. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(5))) {
  532. return _.diag(SPV_ERROR_INVALID_ID, inst)
  533. << "Binding must be a 32-bit unsigned integer OpConstant";
  534. }
  535. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(6))) {
  536. return _.diag(SPV_ERROR_INVALID_ID, inst)
  537. << "Mask must be a 32-bit unsigned integer OpConstant";
  538. }
  539. return SPV_SUCCESS;
  540. }
  541. spv_result_t ValidateClspvReflectionPropertyRequiredWorkgroupSize(
  542. ValidationState_t& _, const Instruction* inst) {
  543. if (auto error = ValidateKernelDecl(_, inst)) {
  544. return error;
  545. }
  546. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(5))) {
  547. return _.diag(SPV_ERROR_INVALID_ID, inst)
  548. << "X must be a 32-bit unsigned integer OpConstant";
  549. }
  550. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(6))) {
  551. return _.diag(SPV_ERROR_INVALID_ID, inst)
  552. << "Y must be a 32-bit unsigned integer OpConstant";
  553. }
  554. if (!IsUint32Constant(_, inst->GetOperandAs<uint32_t>(7))) {
  555. return _.diag(SPV_ERROR_INVALID_ID, inst)
  556. << "Z must be a 32-bit unsigned integer OpConstant";
  557. }
  558. return SPV_SUCCESS;
  559. }
  560. spv_result_t ValidateClspvReflectionInstruction(ValidationState_t& _,
  561. const Instruction* inst,
  562. uint32_t /*version*/) {
  563. if (!_.IsVoidType(inst->type_id())) {
  564. return _.diag(SPV_ERROR_INVALID_ID, inst)
  565. << "Return Type must be OpTypeVoid";
  566. }
  567. auto ext_inst = inst->GetOperandAs<NonSemanticClspvReflectionInstructions>(3);
  568. switch (ext_inst) {
  569. case NonSemanticClspvReflectionKernel:
  570. return ValidateClspvReflectionKernel(_, inst);
  571. case NonSemanticClspvReflectionArgumentInfo:
  572. return ValidateClspvReflectionArgumentInfo(_, inst);
  573. case NonSemanticClspvReflectionArgumentStorageBuffer:
  574. case NonSemanticClspvReflectionArgumentUniform:
  575. case NonSemanticClspvReflectionArgumentSampledImage:
  576. case NonSemanticClspvReflectionArgumentStorageImage:
  577. case NonSemanticClspvReflectionArgumentSampler:
  578. return ValidateClspvReflectionArgumentBuffer(_, inst);
  579. case NonSemanticClspvReflectionArgumentPodStorageBuffer:
  580. case NonSemanticClspvReflectionArgumentPodUniform:
  581. return ValidateClspvReflectionArgumentPodBuffer(_, inst);
  582. case NonSemanticClspvReflectionArgumentPodPushConstant:
  583. return ValidateClspvReflectionArgumentPodPushConstant(_, inst);
  584. case NonSemanticClspvReflectionArgumentWorkgroup:
  585. return ValidateClspvReflectionArgumentWorkgroup(_, inst);
  586. case NonSemanticClspvReflectionSpecConstantWorkgroupSize:
  587. case NonSemanticClspvReflectionSpecConstantGlobalOffset:
  588. return ValidateClspvReflectionSpecConstantTriple(_, inst);
  589. case NonSemanticClspvReflectionSpecConstantWorkDim:
  590. return ValidateClspvReflectionSpecConstantWorkDim(_, inst);
  591. case NonSemanticClspvReflectionPushConstantGlobalOffset:
  592. case NonSemanticClspvReflectionPushConstantEnqueuedLocalSize:
  593. case NonSemanticClspvReflectionPushConstantGlobalSize:
  594. case NonSemanticClspvReflectionPushConstantRegionOffset:
  595. case NonSemanticClspvReflectionPushConstantNumWorkgroups:
  596. case NonSemanticClspvReflectionPushConstantRegionGroupOffset:
  597. return ValidateClspvReflectionPushConstant(_, inst);
  598. case NonSemanticClspvReflectionConstantDataStorageBuffer:
  599. case NonSemanticClspvReflectionConstantDataUniform:
  600. return ValidateClspvReflectionConstantData(_, inst);
  601. case NonSemanticClspvReflectionLiteralSampler:
  602. return ValidateClspvReflectionSampler(_, inst);
  603. case NonSemanticClspvReflectionPropertyRequiredWorkgroupSize:
  604. return ValidateClspvReflectionPropertyRequiredWorkgroupSize(_, inst);
  605. default:
  606. break;
  607. }
  608. return SPV_SUCCESS;
  609. }
  610. bool IsConstIntScalarTypeWith32Or64Bits(ValidationState_t& _,
  611. Instruction* instr) {
  612. if (instr->opcode() != SpvOpConstant) return false;
  613. if (!_.IsIntScalarType(instr->type_id())) return false;
  614. uint32_t size_in_bits = _.GetBitWidth(instr->type_id());
  615. return size_in_bits == 32 || size_in_bits == 64;
  616. }
  617. bool IsConstWithIntScalarType(ValidationState_t& _, const Instruction* inst,
  618. uint32_t word_index) {
  619. auto* int_scalar_const = _.FindDef(inst->word(word_index));
  620. if (int_scalar_const->opcode() == SpvOpConstant &&
  621. _.IsIntScalarType(int_scalar_const->type_id())) {
  622. return true;
  623. }
  624. return false;
  625. }
  626. bool IsDebugVariableWithIntScalarType(ValidationState_t& _,
  627. const Instruction* inst,
  628. uint32_t word_index) {
  629. auto* dbg_int_scalar_var = _.FindDef(inst->word(word_index));
  630. if (CommonDebugInfoInstructions(dbg_int_scalar_var->word(4)) ==
  631. CommonDebugInfoDebugLocalVariable ||
  632. CommonDebugInfoInstructions(dbg_int_scalar_var->word(4)) ==
  633. CommonDebugInfoDebugGlobalVariable) {
  634. auto* dbg_type = _.FindDef(dbg_int_scalar_var->word(6));
  635. if (CommonDebugInfoInstructions(dbg_type->word(4)) ==
  636. CommonDebugInfoDebugTypeBasic) {
  637. const spv_ext_inst_type_t ext_inst_type =
  638. spv_ext_inst_type_t(inst->ext_inst_type());
  639. const bool vulkanDebugInfo =
  640. ext_inst_type == SPV_EXT_INST_TYPE_NONSEMANTIC_SHADER_DEBUGINFO_100;
  641. uint32_t encoding = dbg_type->word(7);
  642. if (!vulkanDebugInfo || IsUint32Constant(_, encoding)) {
  643. auto ocl_encoding = OpenCLDebugInfo100DebugBaseTypeAttributeEncoding(
  644. vulkanDebugInfo ? GetUint32Constant(_, encoding) : encoding);
  645. if (ocl_encoding == OpenCLDebugInfo100Signed ||
  646. ocl_encoding == OpenCLDebugInfo100Unsigned) {
  647. return true;
  648. }
  649. }
  650. }
  651. }
  652. return false;
  653. }
  654. } // anonymous namespace
  655. spv_result_t ValidateExtension(ValidationState_t& _, const Instruction* inst) {
  656. if (_.version() < SPV_SPIRV_VERSION_WORD(1, 4)) {
  657. std::string extension = GetExtensionString(&(inst->c_inst()));
  658. if (extension ==
  659. ExtensionToString(kSPV_KHR_workgroup_memory_explicit_layout)) {
  660. return _.diag(SPV_ERROR_WRONG_VERSION, inst)
  661. << "SPV_KHR_workgroup_memory_explicit_layout extension "
  662. "requires SPIR-V version 1.4 or later.";
  663. }
  664. }
  665. return SPV_SUCCESS;
  666. }
  667. spv_result_t ValidateExtInstImport(ValidationState_t& _,
  668. const Instruction* inst) {
  669. const auto name_id = 1;
  670. if (_.version() <= SPV_SPIRV_VERSION_WORD(1, 5) &&
  671. !_.HasExtension(kSPV_KHR_non_semantic_info)) {
  672. const std::string name = inst->GetOperandAs<std::string>(name_id);
  673. if (name.find("NonSemantic.") == 0) {
  674. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  675. << "NonSemantic extended instruction sets cannot be declared "
  676. "without SPV_KHR_non_semantic_info.";
  677. }
  678. }
  679. return SPV_SUCCESS;
  680. }
  681. spv_result_t ValidateExtInst(ValidationState_t& _, const Instruction* inst) {
  682. const uint32_t result_type = inst->type_id();
  683. const uint32_t num_operands = static_cast<uint32_t>(inst->operands().size());
  684. const uint32_t ext_inst_set = inst->word(3);
  685. const uint32_t ext_inst_index = inst->word(4);
  686. const spv_ext_inst_type_t ext_inst_type =
  687. spv_ext_inst_type_t(inst->ext_inst_type());
  688. auto ext_inst_name = [&_, ext_inst_set, ext_inst_type, ext_inst_index]() {
  689. spv_ext_inst_desc desc = nullptr;
  690. if (_.grammar().lookupExtInst(ext_inst_type, ext_inst_index, &desc) !=
  691. SPV_SUCCESS ||
  692. !desc) {
  693. return std::string("Unknown ExtInst");
  694. }
  695. auto* import_inst = _.FindDef(ext_inst_set);
  696. assert(import_inst);
  697. std::ostringstream ss;
  698. ss << import_inst->GetOperandAs<std::string>(1);
  699. ss << " ";
  700. ss << desc->name;
  701. return ss.str();
  702. };
  703. if (ext_inst_type == SPV_EXT_INST_TYPE_GLSL_STD_450) {
  704. const GLSLstd450 ext_inst_key = GLSLstd450(ext_inst_index);
  705. switch (ext_inst_key) {
  706. case GLSLstd450Round:
  707. case GLSLstd450RoundEven:
  708. case GLSLstd450FAbs:
  709. case GLSLstd450Trunc:
  710. case GLSLstd450FSign:
  711. case GLSLstd450Floor:
  712. case GLSLstd450Ceil:
  713. case GLSLstd450Fract:
  714. case GLSLstd450Sqrt:
  715. case GLSLstd450InverseSqrt:
  716. case GLSLstd450FMin:
  717. case GLSLstd450FMax:
  718. case GLSLstd450FClamp:
  719. case GLSLstd450FMix:
  720. case GLSLstd450Step:
  721. case GLSLstd450SmoothStep:
  722. case GLSLstd450Fma:
  723. case GLSLstd450Normalize:
  724. case GLSLstd450FaceForward:
  725. case GLSLstd450Reflect:
  726. case GLSLstd450NMin:
  727. case GLSLstd450NMax:
  728. case GLSLstd450NClamp: {
  729. if (!_.IsFloatScalarOrVectorType(result_type)) {
  730. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  731. << ext_inst_name() << ": "
  732. << "expected Result Type to be a float scalar or vector type";
  733. }
  734. for (uint32_t operand_index = 4; operand_index < num_operands;
  735. ++operand_index) {
  736. const uint32_t operand_type = _.GetOperandTypeId(inst, operand_index);
  737. if (result_type != operand_type) {
  738. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  739. << ext_inst_name() << ": "
  740. << "expected types of all operands to be equal to Result "
  741. "Type";
  742. }
  743. }
  744. break;
  745. }
  746. case GLSLstd450SAbs:
  747. case GLSLstd450SSign:
  748. case GLSLstd450UMin:
  749. case GLSLstd450SMin:
  750. case GLSLstd450UMax:
  751. case GLSLstd450SMax:
  752. case GLSLstd450UClamp:
  753. case GLSLstd450SClamp:
  754. case GLSLstd450FindILsb:
  755. case GLSLstd450FindUMsb:
  756. case GLSLstd450FindSMsb: {
  757. if (!_.IsIntScalarOrVectorType(result_type)) {
  758. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  759. << ext_inst_name() << ": "
  760. << "expected Result Type to be an int scalar or vector type";
  761. }
  762. const uint32_t result_type_bit_width = _.GetBitWidth(result_type);
  763. const uint32_t result_type_dimension = _.GetDimension(result_type);
  764. for (uint32_t operand_index = 4; operand_index < num_operands;
  765. ++operand_index) {
  766. const uint32_t operand_type = _.GetOperandTypeId(inst, operand_index);
  767. if (!operand_type || !_.IsIntScalarOrVectorType(operand_type)) {
  768. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  769. << ext_inst_name() << ": "
  770. << "expected all operands to be int scalars or vectors";
  771. }
  772. if (result_type_dimension != _.GetDimension(operand_type)) {
  773. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  774. << ext_inst_name() << ": "
  775. << "expected all operands to have the same dimension as "
  776. << "Result Type";
  777. }
  778. if (result_type_bit_width != _.GetBitWidth(operand_type)) {
  779. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  780. << ext_inst_name() << ": "
  781. << "expected all operands to have the same bit width as "
  782. << "Result Type";
  783. }
  784. if (ext_inst_key == GLSLstd450FindUMsb ||
  785. ext_inst_key == GLSLstd450FindSMsb) {
  786. if (result_type_bit_width != 32) {
  787. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  788. << ext_inst_name() << ": "
  789. << "this instruction is currently limited to 32-bit width "
  790. << "components";
  791. }
  792. }
  793. }
  794. break;
  795. }
  796. case GLSLstd450Radians:
  797. case GLSLstd450Degrees:
  798. case GLSLstd450Sin:
  799. case GLSLstd450Cos:
  800. case GLSLstd450Tan:
  801. case GLSLstd450Asin:
  802. case GLSLstd450Acos:
  803. case GLSLstd450Atan:
  804. case GLSLstd450Sinh:
  805. case GLSLstd450Cosh:
  806. case GLSLstd450Tanh:
  807. case GLSLstd450Asinh:
  808. case GLSLstd450Acosh:
  809. case GLSLstd450Atanh:
  810. case GLSLstd450Exp:
  811. case GLSLstd450Exp2:
  812. case GLSLstd450Log:
  813. case GLSLstd450Log2:
  814. case GLSLstd450Atan2:
  815. case GLSLstd450Pow: {
  816. if (!_.IsFloatScalarOrVectorType(result_type)) {
  817. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  818. << ext_inst_name() << ": "
  819. << "expected Result Type to be a 16 or 32-bit scalar or "
  820. "vector float type";
  821. }
  822. const uint32_t result_type_bit_width = _.GetBitWidth(result_type);
  823. if (result_type_bit_width != 16 && result_type_bit_width != 32) {
  824. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  825. << ext_inst_name() << ": "
  826. << "expected Result Type to be a 16 or 32-bit scalar or "
  827. "vector float type";
  828. }
  829. for (uint32_t operand_index = 4; operand_index < num_operands;
  830. ++operand_index) {
  831. const uint32_t operand_type = _.GetOperandTypeId(inst, operand_index);
  832. if (result_type != operand_type) {
  833. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  834. << ext_inst_name() << ": "
  835. << "expected types of all operands to be equal to Result "
  836. "Type";
  837. }
  838. }
  839. break;
  840. }
  841. case GLSLstd450Determinant: {
  842. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  843. uint32_t num_rows = 0;
  844. uint32_t num_cols = 0;
  845. uint32_t col_type = 0;
  846. uint32_t component_type = 0;
  847. if (!_.GetMatrixTypeInfo(x_type, &num_rows, &num_cols, &col_type,
  848. &component_type) ||
  849. num_rows != num_cols) {
  850. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  851. << ext_inst_name() << ": "
  852. << "expected operand X to be a square matrix";
  853. }
  854. if (result_type != component_type) {
  855. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  856. << ext_inst_name() << ": "
  857. << "expected operand X component type to be equal to "
  858. << "Result Type";
  859. }
  860. break;
  861. }
  862. case GLSLstd450MatrixInverse: {
  863. uint32_t num_rows = 0;
  864. uint32_t num_cols = 0;
  865. uint32_t col_type = 0;
  866. uint32_t component_type = 0;
  867. if (!_.GetMatrixTypeInfo(result_type, &num_rows, &num_cols, &col_type,
  868. &component_type) ||
  869. num_rows != num_cols) {
  870. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  871. << ext_inst_name() << ": "
  872. << "expected Result Type to be a square matrix";
  873. }
  874. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  875. if (result_type != x_type) {
  876. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  877. << ext_inst_name() << ": "
  878. << "expected operand X type to be equal to Result Type";
  879. }
  880. break;
  881. }
  882. case GLSLstd450Modf: {
  883. if (!_.IsFloatScalarOrVectorType(result_type)) {
  884. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  885. << ext_inst_name() << ": "
  886. << "expected Result Type to be a scalar or vector float type";
  887. }
  888. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  889. const uint32_t i_type = _.GetOperandTypeId(inst, 5);
  890. if (x_type != result_type) {
  891. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  892. << ext_inst_name() << ": "
  893. << "expected operand X type to be equal to Result Type";
  894. }
  895. uint32_t i_storage_class = 0;
  896. uint32_t i_data_type = 0;
  897. if (!_.GetPointerTypeInfo(i_type, &i_data_type, &i_storage_class)) {
  898. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  899. << ext_inst_name() << ": "
  900. << "expected operand I to be a pointer";
  901. }
  902. if (i_data_type != result_type) {
  903. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  904. << ext_inst_name() << ": "
  905. << "expected operand I data type to be equal to Result Type";
  906. }
  907. break;
  908. }
  909. case GLSLstd450ModfStruct: {
  910. std::vector<uint32_t> result_types;
  911. if (!_.GetStructMemberTypes(result_type, &result_types) ||
  912. result_types.size() != 2 ||
  913. !_.IsFloatScalarOrVectorType(result_types[0]) ||
  914. result_types[1] != result_types[0]) {
  915. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  916. << ext_inst_name() << ": "
  917. << "expected Result Type to be a struct with two identical "
  918. << "scalar or vector float type members";
  919. }
  920. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  921. if (x_type != result_types[0]) {
  922. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  923. << ext_inst_name() << ": "
  924. << "expected operand X type to be equal to members of "
  925. << "Result Type struct";
  926. }
  927. break;
  928. }
  929. case GLSLstd450Frexp: {
  930. if (!_.IsFloatScalarOrVectorType(result_type)) {
  931. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  932. << ext_inst_name() << ": "
  933. << "expected Result Type to be a scalar or vector float type";
  934. }
  935. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  936. const uint32_t exp_type = _.GetOperandTypeId(inst, 5);
  937. if (x_type != result_type) {
  938. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  939. << ext_inst_name() << ": "
  940. << "expected operand X type to be equal to Result Type";
  941. }
  942. uint32_t exp_storage_class = 0;
  943. uint32_t exp_data_type = 0;
  944. if (!_.GetPointerTypeInfo(exp_type, &exp_data_type,
  945. &exp_storage_class)) {
  946. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  947. << ext_inst_name() << ": "
  948. << "expected operand Exp to be a pointer";
  949. }
  950. if (!_.IsIntScalarOrVectorType(exp_data_type) ||
  951. (!_.HasExtension(kSPV_AMD_gpu_shader_int16) &&
  952. _.GetBitWidth(exp_data_type) != 32) ||
  953. (_.HasExtension(kSPV_AMD_gpu_shader_int16) &&
  954. _.GetBitWidth(exp_data_type) != 16 &&
  955. _.GetBitWidth(exp_data_type) != 32)) {
  956. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  957. << ext_inst_name() << ": "
  958. << "expected operand Exp data type to be a "
  959. << (_.HasExtension(kSPV_AMD_gpu_shader_int16)
  960. ? "16-bit or 32-bit "
  961. : "32-bit ")
  962. << "int scalar or vector type";
  963. }
  964. if (_.GetDimension(result_type) != _.GetDimension(exp_data_type)) {
  965. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  966. << ext_inst_name() << ": "
  967. << "expected operand Exp data type to have the same component "
  968. << "number as Result Type";
  969. }
  970. break;
  971. }
  972. case GLSLstd450Ldexp: {
  973. if (!_.IsFloatScalarOrVectorType(result_type)) {
  974. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  975. << ext_inst_name() << ": "
  976. << "expected Result Type to be a scalar or vector float type";
  977. }
  978. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  979. const uint32_t exp_type = _.GetOperandTypeId(inst, 5);
  980. if (x_type != result_type) {
  981. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  982. << ext_inst_name() << ": "
  983. << "expected operand X type to be equal to Result Type";
  984. }
  985. if (!_.IsIntScalarOrVectorType(exp_type)) {
  986. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  987. << ext_inst_name() << ": "
  988. << "expected operand Exp to be a 32-bit int scalar "
  989. << "or vector type";
  990. }
  991. if (_.GetDimension(result_type) != _.GetDimension(exp_type)) {
  992. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  993. << ext_inst_name() << ": "
  994. << "expected operand Exp to have the same component "
  995. << "number as Result Type";
  996. }
  997. break;
  998. }
  999. case GLSLstd450FrexpStruct: {
  1000. std::vector<uint32_t> result_types;
  1001. if (!_.GetStructMemberTypes(result_type, &result_types) ||
  1002. result_types.size() != 2 ||
  1003. !_.IsFloatScalarOrVectorType(result_types[0]) ||
  1004. !_.IsIntScalarOrVectorType(result_types[1]) ||
  1005. (!_.HasExtension(kSPV_AMD_gpu_shader_int16) &&
  1006. _.GetBitWidth(result_types[1]) != 32) ||
  1007. (_.HasExtension(kSPV_AMD_gpu_shader_int16) &&
  1008. _.GetBitWidth(result_types[1]) != 16 &&
  1009. _.GetBitWidth(result_types[1]) != 32) ||
  1010. _.GetDimension(result_types[0]) !=
  1011. _.GetDimension(result_types[1])) {
  1012. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1013. << ext_inst_name() << ": "
  1014. << "expected Result Type to be a struct with two members, "
  1015. << "first member a float scalar or vector, second member a "
  1016. << (_.HasExtension(kSPV_AMD_gpu_shader_int16)
  1017. ? "16-bit or 32-bit "
  1018. : "32-bit ")
  1019. << "int scalar or vector with the same number of "
  1020. << "components as the first member";
  1021. }
  1022. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  1023. if (x_type != result_types[0]) {
  1024. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1025. << ext_inst_name() << ": "
  1026. << "expected operand X type to be equal to the first member "
  1027. << "of Result Type struct";
  1028. }
  1029. break;
  1030. }
  1031. case GLSLstd450PackSnorm4x8:
  1032. case GLSLstd450PackUnorm4x8: {
  1033. if (!_.IsIntScalarType(result_type) ||
  1034. _.GetBitWidth(result_type) != 32) {
  1035. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1036. << ext_inst_name() << ": "
  1037. << "expected Result Type to be 32-bit int scalar type";
  1038. }
  1039. const uint32_t v_type = _.GetOperandTypeId(inst, 4);
  1040. if (!_.IsFloatVectorType(v_type) || _.GetDimension(v_type) != 4 ||
  1041. _.GetBitWidth(v_type) != 32) {
  1042. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1043. << ext_inst_name() << ": "
  1044. << "expected operand V to be a 32-bit float vector of size 4";
  1045. }
  1046. break;
  1047. }
  1048. case GLSLstd450PackSnorm2x16:
  1049. case GLSLstd450PackUnorm2x16:
  1050. case GLSLstd450PackHalf2x16: {
  1051. if (!_.IsIntScalarType(result_type) ||
  1052. _.GetBitWidth(result_type) != 32) {
  1053. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1054. << ext_inst_name() << ": "
  1055. << "expected Result Type to be 32-bit int scalar type";
  1056. }
  1057. const uint32_t v_type = _.GetOperandTypeId(inst, 4);
  1058. if (!_.IsFloatVectorType(v_type) || _.GetDimension(v_type) != 2 ||
  1059. _.GetBitWidth(v_type) != 32) {
  1060. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1061. << ext_inst_name() << ": "
  1062. << "expected operand V to be a 32-bit float vector of size 2";
  1063. }
  1064. break;
  1065. }
  1066. case GLSLstd450PackDouble2x32: {
  1067. if (!_.IsFloatScalarType(result_type) ||
  1068. _.GetBitWidth(result_type) != 64) {
  1069. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1070. << ext_inst_name() << ": "
  1071. << "expected Result Type to be 64-bit float scalar type";
  1072. }
  1073. const uint32_t v_type = _.GetOperandTypeId(inst, 4);
  1074. if (!_.IsIntVectorType(v_type) || _.GetDimension(v_type) != 2 ||
  1075. _.GetBitWidth(v_type) != 32) {
  1076. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1077. << ext_inst_name() << ": "
  1078. << "expected operand V to be a 32-bit int vector of size 2";
  1079. }
  1080. break;
  1081. }
  1082. case GLSLstd450UnpackSnorm4x8:
  1083. case GLSLstd450UnpackUnorm4x8: {
  1084. if (!_.IsFloatVectorType(result_type) ||
  1085. _.GetDimension(result_type) != 4 ||
  1086. _.GetBitWidth(result_type) != 32) {
  1087. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1088. << ext_inst_name() << ": "
  1089. << "expected Result Type to be a 32-bit float vector of size "
  1090. "4";
  1091. }
  1092. const uint32_t v_type = _.GetOperandTypeId(inst, 4);
  1093. if (!_.IsIntScalarType(v_type) || _.GetBitWidth(v_type) != 32) {
  1094. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1095. << ext_inst_name() << ": "
  1096. << "expected operand P to be a 32-bit int scalar";
  1097. }
  1098. break;
  1099. }
  1100. case GLSLstd450UnpackSnorm2x16:
  1101. case GLSLstd450UnpackUnorm2x16:
  1102. case GLSLstd450UnpackHalf2x16: {
  1103. if (!_.IsFloatVectorType(result_type) ||
  1104. _.GetDimension(result_type) != 2 ||
  1105. _.GetBitWidth(result_type) != 32) {
  1106. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1107. << ext_inst_name() << ": "
  1108. << "expected Result Type to be a 32-bit float vector of size "
  1109. "2";
  1110. }
  1111. const uint32_t v_type = _.GetOperandTypeId(inst, 4);
  1112. if (!_.IsIntScalarType(v_type) || _.GetBitWidth(v_type) != 32) {
  1113. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1114. << ext_inst_name() << ": "
  1115. << "expected operand P to be a 32-bit int scalar";
  1116. }
  1117. break;
  1118. }
  1119. case GLSLstd450UnpackDouble2x32: {
  1120. if (!_.IsIntVectorType(result_type) ||
  1121. _.GetDimension(result_type) != 2 ||
  1122. _.GetBitWidth(result_type) != 32) {
  1123. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1124. << ext_inst_name() << ": "
  1125. << "expected Result Type to be a 32-bit int vector of size "
  1126. "2";
  1127. }
  1128. const uint32_t v_type = _.GetOperandTypeId(inst, 4);
  1129. if (!_.IsFloatScalarType(v_type) || _.GetBitWidth(v_type) != 64) {
  1130. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1131. << ext_inst_name() << ": "
  1132. << "expected operand V to be a 64-bit float scalar";
  1133. }
  1134. break;
  1135. }
  1136. case GLSLstd450Length: {
  1137. if (!_.IsFloatScalarType(result_type)) {
  1138. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1139. << ext_inst_name() << ": "
  1140. << "expected Result Type to be a float scalar type";
  1141. }
  1142. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  1143. if (!_.IsFloatScalarOrVectorType(x_type)) {
  1144. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1145. << ext_inst_name() << ": "
  1146. << "expected operand X to be of float scalar or vector type";
  1147. }
  1148. if (result_type != _.GetComponentType(x_type)) {
  1149. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1150. << ext_inst_name() << ": "
  1151. << "expected operand X component type to be equal to Result "
  1152. "Type";
  1153. }
  1154. break;
  1155. }
  1156. case GLSLstd450Distance: {
  1157. if (!_.IsFloatScalarType(result_type)) {
  1158. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1159. << ext_inst_name() << ": "
  1160. << "expected Result Type to be a float scalar type";
  1161. }
  1162. const uint32_t p0_type = _.GetOperandTypeId(inst, 4);
  1163. if (!_.IsFloatScalarOrVectorType(p0_type)) {
  1164. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1165. << ext_inst_name() << ": "
  1166. << "expected operand P0 to be of float scalar or vector type";
  1167. }
  1168. if (result_type != _.GetComponentType(p0_type)) {
  1169. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1170. << ext_inst_name() << ": "
  1171. << "expected operand P0 component type to be equal to "
  1172. << "Result Type";
  1173. }
  1174. const uint32_t p1_type = _.GetOperandTypeId(inst, 5);
  1175. if (!_.IsFloatScalarOrVectorType(p1_type)) {
  1176. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1177. << ext_inst_name() << ": "
  1178. << "expected operand P1 to be of float scalar or vector type";
  1179. }
  1180. if (result_type != _.GetComponentType(p1_type)) {
  1181. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1182. << ext_inst_name() << ": "
  1183. << "expected operand P1 component type to be equal to "
  1184. << "Result Type";
  1185. }
  1186. if (_.GetDimension(p0_type) != _.GetDimension(p1_type)) {
  1187. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1188. << ext_inst_name() << ": "
  1189. << "expected operands P0 and P1 to have the same number of "
  1190. << "components";
  1191. }
  1192. break;
  1193. }
  1194. case GLSLstd450Cross: {
  1195. if (!_.IsFloatVectorType(result_type)) {
  1196. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1197. << ext_inst_name() << ": "
  1198. << "expected Result Type to be a float vector type";
  1199. }
  1200. if (_.GetDimension(result_type) != 3) {
  1201. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1202. << ext_inst_name() << ": "
  1203. << "expected Result Type to have 3 components";
  1204. }
  1205. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  1206. const uint32_t y_type = _.GetOperandTypeId(inst, 5);
  1207. if (x_type != result_type) {
  1208. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1209. << ext_inst_name() << ": "
  1210. << "expected operand X type to be equal to Result Type";
  1211. }
  1212. if (y_type != result_type) {
  1213. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1214. << ext_inst_name() << ": "
  1215. << "expected operand Y type to be equal to Result Type";
  1216. }
  1217. break;
  1218. }
  1219. case GLSLstd450Refract: {
  1220. if (!_.IsFloatScalarOrVectorType(result_type)) {
  1221. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1222. << ext_inst_name() << ": "
  1223. << "expected Result Type to be a float scalar or vector type";
  1224. }
  1225. const uint32_t i_type = _.GetOperandTypeId(inst, 4);
  1226. const uint32_t n_type = _.GetOperandTypeId(inst, 5);
  1227. const uint32_t eta_type = _.GetOperandTypeId(inst, 6);
  1228. if (result_type != i_type) {
  1229. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1230. << ext_inst_name() << ": "
  1231. << "expected operand I to be of type equal to Result Type";
  1232. }
  1233. if (result_type != n_type) {
  1234. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1235. << ext_inst_name() << ": "
  1236. << "expected operand N to be of type equal to Result Type";
  1237. }
  1238. if (!_.IsFloatScalarType(eta_type)) {
  1239. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1240. << ext_inst_name() << ": "
  1241. << "expected operand Eta to be a float scalar";
  1242. }
  1243. break;
  1244. }
  1245. case GLSLstd450InterpolateAtCentroid:
  1246. case GLSLstd450InterpolateAtSample:
  1247. case GLSLstd450InterpolateAtOffset: {
  1248. if (!_.HasCapability(SpvCapabilityInterpolationFunction)) {
  1249. return _.diag(SPV_ERROR_INVALID_CAPABILITY, inst)
  1250. << ext_inst_name()
  1251. << " requires capability InterpolationFunction";
  1252. }
  1253. if (!_.IsFloatScalarOrVectorType(result_type) ||
  1254. _.GetBitWidth(result_type) != 32) {
  1255. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1256. << ext_inst_name() << ": "
  1257. << "expected Result Type to be a 32-bit float scalar "
  1258. << "or vector type";
  1259. }
  1260. // If HLSL legalization and first operand is an OpLoad, use load
  1261. // pointer as the interpolant lvalue. Else use interpolate first
  1262. // operand.
  1263. uint32_t interp_id = inst->GetOperandAs<uint32_t>(4);
  1264. auto* interp_inst = _.FindDef(interp_id);
  1265. uint32_t interpolant_type = (_.options()->before_hlsl_legalization &&
  1266. interp_inst->opcode() == SpvOpLoad)
  1267. ? _.GetOperandTypeId(interp_inst, 2)
  1268. : _.GetOperandTypeId(inst, 4);
  1269. uint32_t interpolant_storage_class = 0;
  1270. uint32_t interpolant_data_type = 0;
  1271. if (!_.GetPointerTypeInfo(interpolant_type, &interpolant_data_type,
  1272. &interpolant_storage_class)) {
  1273. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1274. << ext_inst_name() << ": "
  1275. << "expected Interpolant to be a pointer";
  1276. }
  1277. if (result_type != interpolant_data_type) {
  1278. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1279. << ext_inst_name() << ": "
  1280. << "expected Interpolant data type to be equal to Result Type";
  1281. }
  1282. if (interpolant_storage_class != SpvStorageClassInput) {
  1283. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1284. << ext_inst_name() << ": "
  1285. << "expected Interpolant storage class to be Input";
  1286. }
  1287. if (ext_inst_key == GLSLstd450InterpolateAtSample) {
  1288. const uint32_t sample_type = _.GetOperandTypeId(inst, 5);
  1289. if (!_.IsIntScalarType(sample_type) ||
  1290. _.GetBitWidth(sample_type) != 32) {
  1291. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1292. << ext_inst_name() << ": "
  1293. << "expected Sample to be 32-bit integer";
  1294. }
  1295. }
  1296. if (ext_inst_key == GLSLstd450InterpolateAtOffset) {
  1297. const uint32_t offset_type = _.GetOperandTypeId(inst, 5);
  1298. if (!_.IsFloatVectorType(offset_type) ||
  1299. _.GetDimension(offset_type) != 2 ||
  1300. _.GetBitWidth(offset_type) != 32) {
  1301. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1302. << ext_inst_name() << ": "
  1303. << "expected Offset to be a vector of 2 32-bit floats";
  1304. }
  1305. }
  1306. _.function(inst->function()->id())
  1307. ->RegisterExecutionModelLimitation(
  1308. SpvExecutionModelFragment,
  1309. ext_inst_name() +
  1310. std::string(" requires Fragment execution model"));
  1311. break;
  1312. }
  1313. case GLSLstd450IMix: {
  1314. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1315. << "Extended instruction GLSLstd450IMix is not supported";
  1316. }
  1317. case GLSLstd450Bad: {
  1318. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1319. << "Encountered extended instruction GLSLstd450Bad";
  1320. }
  1321. case GLSLstd450Count: {
  1322. assert(0);
  1323. break;
  1324. }
  1325. }
  1326. } else if (ext_inst_type == SPV_EXT_INST_TYPE_OPENCL_STD) {
  1327. const OpenCLLIB::Entrypoints ext_inst_key =
  1328. OpenCLLIB::Entrypoints(ext_inst_index);
  1329. switch (ext_inst_key) {
  1330. case OpenCLLIB::Acos:
  1331. case OpenCLLIB::Acosh:
  1332. case OpenCLLIB::Acospi:
  1333. case OpenCLLIB::Asin:
  1334. case OpenCLLIB::Asinh:
  1335. case OpenCLLIB::Asinpi:
  1336. case OpenCLLIB::Atan:
  1337. case OpenCLLIB::Atan2:
  1338. case OpenCLLIB::Atanh:
  1339. case OpenCLLIB::Atanpi:
  1340. case OpenCLLIB::Atan2pi:
  1341. case OpenCLLIB::Cbrt:
  1342. case OpenCLLIB::Ceil:
  1343. case OpenCLLIB::Copysign:
  1344. case OpenCLLIB::Cos:
  1345. case OpenCLLIB::Cosh:
  1346. case OpenCLLIB::Cospi:
  1347. case OpenCLLIB::Erfc:
  1348. case OpenCLLIB::Erf:
  1349. case OpenCLLIB::Exp:
  1350. case OpenCLLIB::Exp2:
  1351. case OpenCLLIB::Exp10:
  1352. case OpenCLLIB::Expm1:
  1353. case OpenCLLIB::Fabs:
  1354. case OpenCLLIB::Fdim:
  1355. case OpenCLLIB::Floor:
  1356. case OpenCLLIB::Fma:
  1357. case OpenCLLIB::Fmax:
  1358. case OpenCLLIB::Fmin:
  1359. case OpenCLLIB::Fmod:
  1360. case OpenCLLIB::Hypot:
  1361. case OpenCLLIB::Lgamma:
  1362. case OpenCLLIB::Log:
  1363. case OpenCLLIB::Log2:
  1364. case OpenCLLIB::Log10:
  1365. case OpenCLLIB::Log1p:
  1366. case OpenCLLIB::Logb:
  1367. case OpenCLLIB::Mad:
  1368. case OpenCLLIB::Maxmag:
  1369. case OpenCLLIB::Minmag:
  1370. case OpenCLLIB::Nextafter:
  1371. case OpenCLLIB::Pow:
  1372. case OpenCLLIB::Powr:
  1373. case OpenCLLIB::Remainder:
  1374. case OpenCLLIB::Rint:
  1375. case OpenCLLIB::Round:
  1376. case OpenCLLIB::Rsqrt:
  1377. case OpenCLLIB::Sin:
  1378. case OpenCLLIB::Sinh:
  1379. case OpenCLLIB::Sinpi:
  1380. case OpenCLLIB::Sqrt:
  1381. case OpenCLLIB::Tan:
  1382. case OpenCLLIB::Tanh:
  1383. case OpenCLLIB::Tanpi:
  1384. case OpenCLLIB::Tgamma:
  1385. case OpenCLLIB::Trunc:
  1386. case OpenCLLIB::Half_cos:
  1387. case OpenCLLIB::Half_divide:
  1388. case OpenCLLIB::Half_exp:
  1389. case OpenCLLIB::Half_exp2:
  1390. case OpenCLLIB::Half_exp10:
  1391. case OpenCLLIB::Half_log:
  1392. case OpenCLLIB::Half_log2:
  1393. case OpenCLLIB::Half_log10:
  1394. case OpenCLLIB::Half_powr:
  1395. case OpenCLLIB::Half_recip:
  1396. case OpenCLLIB::Half_rsqrt:
  1397. case OpenCLLIB::Half_sin:
  1398. case OpenCLLIB::Half_sqrt:
  1399. case OpenCLLIB::Half_tan:
  1400. case OpenCLLIB::Native_cos:
  1401. case OpenCLLIB::Native_divide:
  1402. case OpenCLLIB::Native_exp:
  1403. case OpenCLLIB::Native_exp2:
  1404. case OpenCLLIB::Native_exp10:
  1405. case OpenCLLIB::Native_log:
  1406. case OpenCLLIB::Native_log2:
  1407. case OpenCLLIB::Native_log10:
  1408. case OpenCLLIB::Native_powr:
  1409. case OpenCLLIB::Native_recip:
  1410. case OpenCLLIB::Native_rsqrt:
  1411. case OpenCLLIB::Native_sin:
  1412. case OpenCLLIB::Native_sqrt:
  1413. case OpenCLLIB::Native_tan:
  1414. case OpenCLLIB::FClamp:
  1415. case OpenCLLIB::Degrees:
  1416. case OpenCLLIB::FMax_common:
  1417. case OpenCLLIB::FMin_common:
  1418. case OpenCLLIB::Mix:
  1419. case OpenCLLIB::Radians:
  1420. case OpenCLLIB::Step:
  1421. case OpenCLLIB::Smoothstep:
  1422. case OpenCLLIB::Sign: {
  1423. if (!_.IsFloatScalarOrVectorType(result_type)) {
  1424. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1425. << ext_inst_name() << ": "
  1426. << "expected Result Type to be a float scalar or vector type";
  1427. }
  1428. const uint32_t num_components = _.GetDimension(result_type);
  1429. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1430. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1431. << ext_inst_name() << ": "
  1432. << "expected Result Type to be a scalar or a vector with 2, "
  1433. "3, 4, 8 or 16 components";
  1434. }
  1435. for (uint32_t operand_index = 4; operand_index < num_operands;
  1436. ++operand_index) {
  1437. const uint32_t operand_type = _.GetOperandTypeId(inst, operand_index);
  1438. if (result_type != operand_type) {
  1439. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1440. << ext_inst_name() << ": "
  1441. << "expected types of all operands to be equal to Result "
  1442. "Type";
  1443. }
  1444. }
  1445. break;
  1446. }
  1447. case OpenCLLIB::Fract:
  1448. case OpenCLLIB::Modf:
  1449. case OpenCLLIB::Sincos: {
  1450. if (!_.IsFloatScalarOrVectorType(result_type)) {
  1451. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1452. << ext_inst_name() << ": "
  1453. << "expected Result Type to be a float scalar or vector type";
  1454. }
  1455. const uint32_t num_components = _.GetDimension(result_type);
  1456. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1457. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1458. << ext_inst_name() << ": "
  1459. << "expected Result Type to be a scalar or a vector with 2, "
  1460. "3, 4, 8 or 16 components";
  1461. }
  1462. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  1463. if (result_type != x_type) {
  1464. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1465. << ext_inst_name() << ": "
  1466. << "expected type of operand X to be equal to Result Type";
  1467. }
  1468. const uint32_t p_type = _.GetOperandTypeId(inst, 5);
  1469. uint32_t p_storage_class = 0;
  1470. uint32_t p_data_type = 0;
  1471. if (!_.GetPointerTypeInfo(p_type, &p_data_type, &p_storage_class)) {
  1472. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1473. << ext_inst_name() << ": "
  1474. << "expected the last operand to be a pointer";
  1475. }
  1476. if (p_storage_class != SpvStorageClassGeneric &&
  1477. p_storage_class != SpvStorageClassCrossWorkgroup &&
  1478. p_storage_class != SpvStorageClassWorkgroup &&
  1479. p_storage_class != SpvStorageClassFunction) {
  1480. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1481. << ext_inst_name() << ": "
  1482. << "expected storage class of the pointer to be Generic, "
  1483. "CrossWorkgroup, Workgroup or Function";
  1484. }
  1485. if (result_type != p_data_type) {
  1486. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1487. << ext_inst_name() << ": "
  1488. << "expected data type of the pointer to be equal to Result "
  1489. "Type";
  1490. }
  1491. break;
  1492. }
  1493. case OpenCLLIB::Frexp:
  1494. case OpenCLLIB::Lgamma_r:
  1495. case OpenCLLIB::Remquo: {
  1496. if (!_.IsFloatScalarOrVectorType(result_type)) {
  1497. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1498. << ext_inst_name() << ": "
  1499. << "expected Result Type to be a float scalar or vector type";
  1500. }
  1501. const uint32_t num_components = _.GetDimension(result_type);
  1502. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1503. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1504. << ext_inst_name() << ": "
  1505. << "expected Result Type to be a scalar or a vector with 2, "
  1506. "3, 4, 8 or 16 components";
  1507. }
  1508. uint32_t operand_index = 4;
  1509. const uint32_t x_type = _.GetOperandTypeId(inst, operand_index++);
  1510. if (result_type != x_type) {
  1511. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1512. << ext_inst_name() << ": "
  1513. << "expected type of operand X to be equal to Result Type";
  1514. }
  1515. if (ext_inst_key == OpenCLLIB::Remquo) {
  1516. const uint32_t y_type = _.GetOperandTypeId(inst, operand_index++);
  1517. if (result_type != y_type) {
  1518. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1519. << ext_inst_name() << ": "
  1520. << "expected type of operand Y to be equal to Result Type";
  1521. }
  1522. }
  1523. const uint32_t p_type = _.GetOperandTypeId(inst, operand_index++);
  1524. uint32_t p_storage_class = 0;
  1525. uint32_t p_data_type = 0;
  1526. if (!_.GetPointerTypeInfo(p_type, &p_data_type, &p_storage_class)) {
  1527. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1528. << ext_inst_name() << ": "
  1529. << "expected the last operand to be a pointer";
  1530. }
  1531. if (p_storage_class != SpvStorageClassGeneric &&
  1532. p_storage_class != SpvStorageClassCrossWorkgroup &&
  1533. p_storage_class != SpvStorageClassWorkgroup &&
  1534. p_storage_class != SpvStorageClassFunction) {
  1535. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1536. << ext_inst_name() << ": "
  1537. << "expected storage class of the pointer to be Generic, "
  1538. "CrossWorkgroup, Workgroup or Function";
  1539. }
  1540. if (!_.IsIntScalarOrVectorType(p_data_type) ||
  1541. _.GetBitWidth(p_data_type) != 32) {
  1542. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1543. << ext_inst_name() << ": "
  1544. << "expected data type of the pointer to be a 32-bit int "
  1545. "scalar or vector type";
  1546. }
  1547. if (_.GetDimension(p_data_type) != num_components) {
  1548. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1549. << ext_inst_name() << ": "
  1550. << "expected data type of the pointer to have the same number "
  1551. "of components as Result Type";
  1552. }
  1553. break;
  1554. }
  1555. case OpenCLLIB::Ilogb: {
  1556. if (!_.IsIntScalarOrVectorType(result_type) ||
  1557. _.GetBitWidth(result_type) != 32) {
  1558. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1559. << ext_inst_name() << ": "
  1560. << "expected Result Type to be a 32-bit int scalar or vector "
  1561. "type";
  1562. }
  1563. const uint32_t num_components = _.GetDimension(result_type);
  1564. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1565. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1566. << ext_inst_name() << ": "
  1567. << "expected Result Type to be a scalar or a vector with 2, "
  1568. "3, 4, 8 or 16 components";
  1569. }
  1570. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  1571. if (!_.IsFloatScalarOrVectorType(x_type)) {
  1572. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1573. << ext_inst_name() << ": "
  1574. << "expected operand X to be a float scalar or vector";
  1575. }
  1576. if (_.GetDimension(x_type) != num_components) {
  1577. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1578. << ext_inst_name() << ": "
  1579. << "expected operand X to have the same number of components "
  1580. "as Result Type";
  1581. }
  1582. break;
  1583. }
  1584. case OpenCLLIB::Ldexp:
  1585. case OpenCLLIB::Pown:
  1586. case OpenCLLIB::Rootn: {
  1587. if (!_.IsFloatScalarOrVectorType(result_type)) {
  1588. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1589. << ext_inst_name() << ": "
  1590. << "expected Result Type to be a float scalar or vector type";
  1591. }
  1592. const uint32_t num_components = _.GetDimension(result_type);
  1593. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1594. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1595. << ext_inst_name() << ": "
  1596. << "expected Result Type to be a scalar or a vector with 2, "
  1597. "3, 4, 8 or 16 components";
  1598. }
  1599. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  1600. if (result_type != x_type) {
  1601. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1602. << ext_inst_name() << ": "
  1603. << "expected type of operand X to be equal to Result Type";
  1604. }
  1605. const uint32_t exp_type = _.GetOperandTypeId(inst, 5);
  1606. if (!_.IsIntScalarOrVectorType(exp_type) ||
  1607. _.GetBitWidth(exp_type) != 32) {
  1608. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1609. << ext_inst_name() << ": "
  1610. << "expected the exponent to be a 32-bit int scalar or vector";
  1611. }
  1612. if (_.GetDimension(exp_type) != num_components) {
  1613. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1614. << ext_inst_name() << ": "
  1615. << "expected the exponent to have the same number of "
  1616. "components as Result Type";
  1617. }
  1618. break;
  1619. }
  1620. case OpenCLLIB::Nan: {
  1621. if (!_.IsFloatScalarOrVectorType(result_type)) {
  1622. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1623. << ext_inst_name() << ": "
  1624. << "expected Result Type to be a float scalar or vector type";
  1625. }
  1626. const uint32_t num_components = _.GetDimension(result_type);
  1627. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1628. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1629. << ext_inst_name() << ": "
  1630. << "expected Result Type to be a scalar or a vector with 2, "
  1631. "3, 4, 8 or 16 components";
  1632. }
  1633. const uint32_t nancode_type = _.GetOperandTypeId(inst, 4);
  1634. if (!_.IsIntScalarOrVectorType(nancode_type)) {
  1635. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1636. << ext_inst_name() << ": "
  1637. << "expected Nancode to be an int scalar or vector type";
  1638. }
  1639. if (_.GetDimension(nancode_type) != num_components) {
  1640. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1641. << ext_inst_name() << ": "
  1642. << "expected Nancode to have the same number of components as "
  1643. "Result Type";
  1644. }
  1645. if (_.GetBitWidth(result_type) != _.GetBitWidth(nancode_type)) {
  1646. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1647. << ext_inst_name() << ": "
  1648. << "expected Nancode to have the same bit width as Result "
  1649. "Type";
  1650. }
  1651. break;
  1652. }
  1653. case OpenCLLIB::SAbs:
  1654. case OpenCLLIB::SAbs_diff:
  1655. case OpenCLLIB::SAdd_sat:
  1656. case OpenCLLIB::UAdd_sat:
  1657. case OpenCLLIB::SHadd:
  1658. case OpenCLLIB::UHadd:
  1659. case OpenCLLIB::SRhadd:
  1660. case OpenCLLIB::URhadd:
  1661. case OpenCLLIB::SClamp:
  1662. case OpenCLLIB::UClamp:
  1663. case OpenCLLIB::Clz:
  1664. case OpenCLLIB::Ctz:
  1665. case OpenCLLIB::SMad_hi:
  1666. case OpenCLLIB::UMad_sat:
  1667. case OpenCLLIB::SMad_sat:
  1668. case OpenCLLIB::SMax:
  1669. case OpenCLLIB::UMax:
  1670. case OpenCLLIB::SMin:
  1671. case OpenCLLIB::UMin:
  1672. case OpenCLLIB::SMul_hi:
  1673. case OpenCLLIB::Rotate:
  1674. case OpenCLLIB::SSub_sat:
  1675. case OpenCLLIB::USub_sat:
  1676. case OpenCLLIB::Popcount:
  1677. case OpenCLLIB::UAbs:
  1678. case OpenCLLIB::UAbs_diff:
  1679. case OpenCLLIB::UMul_hi:
  1680. case OpenCLLIB::UMad_hi: {
  1681. if (!_.IsIntScalarOrVectorType(result_type)) {
  1682. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1683. << ext_inst_name() << ": "
  1684. << "expected Result Type to be an int scalar or vector type";
  1685. }
  1686. const uint32_t num_components = _.GetDimension(result_type);
  1687. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1688. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1689. << ext_inst_name() << ": "
  1690. << "expected Result Type to be a scalar or a vector with 2, "
  1691. "3, 4, 8 or 16 components";
  1692. }
  1693. for (uint32_t operand_index = 4; operand_index < num_operands;
  1694. ++operand_index) {
  1695. const uint32_t operand_type = _.GetOperandTypeId(inst, operand_index);
  1696. if (result_type != operand_type) {
  1697. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1698. << ext_inst_name() << ": "
  1699. << "expected types of all operands to be equal to Result "
  1700. "Type";
  1701. }
  1702. }
  1703. break;
  1704. }
  1705. case OpenCLLIB::U_Upsample:
  1706. case OpenCLLIB::S_Upsample: {
  1707. if (!_.IsIntScalarOrVectorType(result_type)) {
  1708. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1709. << ext_inst_name() << ": "
  1710. << "expected Result Type to be an int scalar or vector "
  1711. "type";
  1712. }
  1713. const uint32_t result_num_components = _.GetDimension(result_type);
  1714. if (result_num_components > 4 && result_num_components != 8 &&
  1715. result_num_components != 16) {
  1716. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1717. << ext_inst_name() << ": "
  1718. << "expected Result Type to be a scalar or a vector with 2, "
  1719. "3, 4, 8 or 16 components";
  1720. }
  1721. const uint32_t result_bit_width = _.GetBitWidth(result_type);
  1722. if (result_bit_width != 16 && result_bit_width != 32 &&
  1723. result_bit_width != 64) {
  1724. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1725. << ext_inst_name() << ": "
  1726. << "expected bit width of Result Type components to be 16, 32 "
  1727. "or 64";
  1728. }
  1729. const uint32_t hi_type = _.GetOperandTypeId(inst, 4);
  1730. const uint32_t lo_type = _.GetOperandTypeId(inst, 5);
  1731. if (hi_type != lo_type) {
  1732. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1733. << ext_inst_name() << ": "
  1734. << "expected Hi and Lo operands to have the same type";
  1735. }
  1736. if (result_num_components != _.GetDimension(hi_type)) {
  1737. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1738. << ext_inst_name() << ": "
  1739. << "expected Hi and Lo operands to have the same number of "
  1740. "components as Result Type";
  1741. }
  1742. if (result_bit_width != 2 * _.GetBitWidth(hi_type)) {
  1743. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1744. << ext_inst_name() << ": "
  1745. << "expected bit width of components of Hi and Lo operands to "
  1746. "be half of the bit width of components of Result Type";
  1747. }
  1748. break;
  1749. }
  1750. case OpenCLLIB::SMad24:
  1751. case OpenCLLIB::UMad24:
  1752. case OpenCLLIB::SMul24:
  1753. case OpenCLLIB::UMul24: {
  1754. if (!_.IsIntScalarOrVectorType(result_type) ||
  1755. _.GetBitWidth(result_type) != 32) {
  1756. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1757. << ext_inst_name() << ": "
  1758. << "expected Result Type to be a 32-bit int scalar or vector "
  1759. "type";
  1760. }
  1761. const uint32_t num_components = _.GetDimension(result_type);
  1762. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1763. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1764. << ext_inst_name() << ": "
  1765. << "expected Result Type to be a scalar or a vector with 2, "
  1766. "3, 4, 8 or 16 components";
  1767. }
  1768. for (uint32_t operand_index = 4; operand_index < num_operands;
  1769. ++operand_index) {
  1770. const uint32_t operand_type = _.GetOperandTypeId(inst, operand_index);
  1771. if (result_type != operand_type) {
  1772. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1773. << ext_inst_name() << ": "
  1774. << "expected types of all operands to be equal to Result "
  1775. "Type";
  1776. }
  1777. }
  1778. break;
  1779. }
  1780. case OpenCLLIB::Cross: {
  1781. if (!_.IsFloatVectorType(result_type)) {
  1782. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1783. << ext_inst_name() << ": "
  1784. << "expected Result Type to be a float vector type";
  1785. }
  1786. const uint32_t num_components = _.GetDimension(result_type);
  1787. if (num_components != 3 && num_components != 4) {
  1788. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1789. << ext_inst_name() << ": "
  1790. << "expected Result Type to have 3 or 4 components";
  1791. }
  1792. const uint32_t x_type = _.GetOperandTypeId(inst, 4);
  1793. const uint32_t y_type = _.GetOperandTypeId(inst, 5);
  1794. if (x_type != result_type) {
  1795. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1796. << ext_inst_name() << ": "
  1797. << "expected operand X type to be equal to Result Type";
  1798. }
  1799. if (y_type != result_type) {
  1800. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1801. << ext_inst_name() << ": "
  1802. << "expected operand Y type to be equal to Result Type";
  1803. }
  1804. break;
  1805. }
  1806. case OpenCLLIB::Distance:
  1807. case OpenCLLIB::Fast_distance: {
  1808. if (!_.IsFloatScalarType(result_type)) {
  1809. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1810. << ext_inst_name() << ": "
  1811. << "expected Result Type to be a float scalar type";
  1812. }
  1813. const uint32_t p0_type = _.GetOperandTypeId(inst, 4);
  1814. if (!_.IsFloatScalarOrVectorType(p0_type)) {
  1815. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1816. << ext_inst_name() << ": "
  1817. << "expected operand P0 to be of float scalar or vector type";
  1818. }
  1819. const uint32_t num_components = _.GetDimension(p0_type);
  1820. if (num_components > 4) {
  1821. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1822. << ext_inst_name() << ": "
  1823. << "expected operand P0 to have no more than 4 components";
  1824. }
  1825. if (result_type != _.GetComponentType(p0_type)) {
  1826. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1827. << ext_inst_name() << ": "
  1828. << "expected operand P0 component type to be equal to "
  1829. << "Result Type";
  1830. }
  1831. const uint32_t p1_type = _.GetOperandTypeId(inst, 5);
  1832. if (p0_type != p1_type) {
  1833. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1834. << ext_inst_name() << ": "
  1835. << "expected operands P0 and P1 to be of the same type";
  1836. }
  1837. break;
  1838. }
  1839. case OpenCLLIB::Length:
  1840. case OpenCLLIB::Fast_length: {
  1841. if (!_.IsFloatScalarType(result_type)) {
  1842. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1843. << ext_inst_name() << ": "
  1844. << "expected Result Type to be a float scalar type";
  1845. }
  1846. const uint32_t p_type = _.GetOperandTypeId(inst, 4);
  1847. if (!_.IsFloatScalarOrVectorType(p_type)) {
  1848. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1849. << ext_inst_name() << ": "
  1850. << "expected operand P to be a float scalar or vector";
  1851. }
  1852. const uint32_t num_components = _.GetDimension(p_type);
  1853. if (num_components > 4) {
  1854. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1855. << ext_inst_name() << ": "
  1856. << "expected operand P to have no more than 4 components";
  1857. }
  1858. if (result_type != _.GetComponentType(p_type)) {
  1859. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1860. << ext_inst_name() << ": "
  1861. << "expected operand P component type to be equal to Result "
  1862. "Type";
  1863. }
  1864. break;
  1865. }
  1866. case OpenCLLIB::Normalize:
  1867. case OpenCLLIB::Fast_normalize: {
  1868. if (!_.IsFloatScalarOrVectorType(result_type)) {
  1869. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1870. << ext_inst_name() << ": "
  1871. << "expected Result Type to be a float scalar or vector type";
  1872. }
  1873. const uint32_t num_components = _.GetDimension(result_type);
  1874. if (num_components > 4) {
  1875. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1876. << ext_inst_name() << ": "
  1877. << "expected Result Type to have no more than 4 components";
  1878. }
  1879. const uint32_t p_type = _.GetOperandTypeId(inst, 4);
  1880. if (p_type != result_type) {
  1881. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1882. << ext_inst_name() << ": "
  1883. << "expected operand P type to be equal to Result Type";
  1884. }
  1885. break;
  1886. }
  1887. case OpenCLLIB::Bitselect: {
  1888. if (!_.IsFloatScalarOrVectorType(result_type) &&
  1889. !_.IsIntScalarOrVectorType(result_type)) {
  1890. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1891. << ext_inst_name() << ": "
  1892. << "expected Result Type to be an int or float scalar or "
  1893. "vector type";
  1894. }
  1895. const uint32_t num_components = _.GetDimension(result_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. for (uint32_t operand_index = 4; operand_index < num_operands;
  1903. ++operand_index) {
  1904. const uint32_t operand_type = _.GetOperandTypeId(inst, operand_index);
  1905. if (result_type != operand_type) {
  1906. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1907. << ext_inst_name() << ": "
  1908. << "expected types of all operands to be equal to Result "
  1909. "Type";
  1910. }
  1911. }
  1912. break;
  1913. }
  1914. case OpenCLLIB::Select: {
  1915. if (!_.IsFloatScalarOrVectorType(result_type) &&
  1916. !_.IsIntScalarOrVectorType(result_type)) {
  1917. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1918. << ext_inst_name() << ": "
  1919. << "expected Result Type to be an int or float scalar or "
  1920. "vector type";
  1921. }
  1922. const uint32_t num_components = _.GetDimension(result_type);
  1923. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1924. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1925. << ext_inst_name() << ": "
  1926. << "expected Result Type to be a scalar or a vector with 2, "
  1927. "3, 4, 8 or 16 components";
  1928. }
  1929. const uint32_t a_type = _.GetOperandTypeId(inst, 4);
  1930. const uint32_t b_type = _.GetOperandTypeId(inst, 5);
  1931. const uint32_t c_type = _.GetOperandTypeId(inst, 6);
  1932. if (result_type != a_type) {
  1933. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1934. << ext_inst_name() << ": "
  1935. << "expected operand A type to be equal to Result Type";
  1936. }
  1937. if (result_type != b_type) {
  1938. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1939. << ext_inst_name() << ": "
  1940. << "expected operand B type to be equal to Result Type";
  1941. }
  1942. if (!_.IsIntScalarOrVectorType(c_type)) {
  1943. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1944. << ext_inst_name() << ": "
  1945. << "expected operand C to be an int scalar or vector";
  1946. }
  1947. if (num_components != _.GetDimension(c_type)) {
  1948. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1949. << ext_inst_name() << ": "
  1950. << "expected operand C to have the same number of components "
  1951. "as Result Type";
  1952. }
  1953. if (_.GetBitWidth(result_type) != _.GetBitWidth(c_type)) {
  1954. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1955. << ext_inst_name() << ": "
  1956. << "expected operand C to have the same bit width as Result "
  1957. "Type";
  1958. }
  1959. break;
  1960. }
  1961. case OpenCLLIB::Vloadn: {
  1962. if (!_.IsFloatVectorType(result_type) &&
  1963. !_.IsIntVectorType(result_type)) {
  1964. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1965. << ext_inst_name() << ": "
  1966. << "expected Result Type to be an int or float vector type";
  1967. }
  1968. const uint32_t num_components = _.GetDimension(result_type);
  1969. if (num_components > 4 && num_components != 8 && num_components != 16) {
  1970. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1971. << ext_inst_name() << ": "
  1972. << "expected Result Type to have 2, 3, 4, 8 or 16 components";
  1973. }
  1974. const uint32_t offset_type = _.GetOperandTypeId(inst, 4);
  1975. const uint32_t p_type = _.GetOperandTypeId(inst, 5);
  1976. const uint32_t size_t_bit_width = GetSizeTBitWidth(_);
  1977. if (!size_t_bit_width) {
  1978. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1979. << ext_inst_name()
  1980. << " can only be used with physical addressing models";
  1981. }
  1982. if (!_.IsIntScalarType(offset_type) ||
  1983. _.GetBitWidth(offset_type) != size_t_bit_width) {
  1984. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1985. << ext_inst_name() << ": "
  1986. << "expected operand Offset to be of type size_t ("
  1987. << size_t_bit_width
  1988. << "-bit integer for the addressing model used in the module)";
  1989. }
  1990. uint32_t p_storage_class = 0;
  1991. uint32_t p_data_type = 0;
  1992. if (!_.GetPointerTypeInfo(p_type, &p_data_type, &p_storage_class)) {
  1993. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  1994. << ext_inst_name() << ": "
  1995. << "expected operand P to be a pointer";
  1996. }
  1997. if (p_storage_class != SpvStorageClassUniformConstant &&
  1998. p_storage_class != SpvStorageClassGeneric &&
  1999. p_storage_class != SpvStorageClassCrossWorkgroup &&
  2000. p_storage_class != SpvStorageClassWorkgroup &&
  2001. p_storage_class != SpvStorageClassFunction) {
  2002. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2003. << ext_inst_name() << ": "
  2004. << "expected operand P storage class to be UniformConstant, "
  2005. "Generic, CrossWorkgroup, Workgroup or Function";
  2006. }
  2007. if (_.GetComponentType(result_type) != p_data_type) {
  2008. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2009. << ext_inst_name() << ": "
  2010. << "expected operand P data type to be equal to component "
  2011. "type of Result Type";
  2012. }
  2013. const uint32_t n_value = inst->word(7);
  2014. if (num_components != n_value) {
  2015. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2016. << ext_inst_name() << ": "
  2017. << "expected literal N to be equal to the number of "
  2018. "components of Result Type";
  2019. }
  2020. break;
  2021. }
  2022. case OpenCLLIB::Vstoren: {
  2023. if (_.GetIdOpcode(result_type) != SpvOpTypeVoid) {
  2024. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2025. << ext_inst_name() << ": expected Result Type to be void";
  2026. }
  2027. const uint32_t data_type = _.GetOperandTypeId(inst, 4);
  2028. const uint32_t offset_type = _.GetOperandTypeId(inst, 5);
  2029. const uint32_t p_type = _.GetOperandTypeId(inst, 6);
  2030. if (!_.IsFloatVectorType(data_type) && !_.IsIntVectorType(data_type)) {
  2031. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2032. << ext_inst_name() << ": "
  2033. << "expected Data to be an int or float vector";
  2034. }
  2035. const uint32_t num_components = _.GetDimension(data_type);
  2036. if (num_components > 4 && num_components != 8 && num_components != 16) {
  2037. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2038. << ext_inst_name() << ": "
  2039. << "expected Data to have 2, 3, 4, 8 or 16 components";
  2040. }
  2041. const uint32_t size_t_bit_width = GetSizeTBitWidth(_);
  2042. if (!size_t_bit_width) {
  2043. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2044. << ext_inst_name()
  2045. << " can only be used with physical addressing models";
  2046. }
  2047. if (!_.IsIntScalarType(offset_type) ||
  2048. _.GetBitWidth(offset_type) != size_t_bit_width) {
  2049. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2050. << ext_inst_name() << ": "
  2051. << "expected operand Offset to be of type size_t ("
  2052. << size_t_bit_width
  2053. << "-bit integer for the addressing model used in the module)";
  2054. }
  2055. uint32_t p_storage_class = 0;
  2056. uint32_t p_data_type = 0;
  2057. if (!_.GetPointerTypeInfo(p_type, &p_data_type, &p_storage_class)) {
  2058. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2059. << ext_inst_name() << ": "
  2060. << "expected operand P to be a pointer";
  2061. }
  2062. if (p_storage_class != SpvStorageClassGeneric &&
  2063. p_storage_class != SpvStorageClassCrossWorkgroup &&
  2064. p_storage_class != SpvStorageClassWorkgroup &&
  2065. p_storage_class != SpvStorageClassFunction) {
  2066. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2067. << ext_inst_name() << ": "
  2068. << "expected operand P storage class to be Generic, "
  2069. "CrossWorkgroup, Workgroup or Function";
  2070. }
  2071. if (_.GetComponentType(data_type) != p_data_type) {
  2072. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2073. << ext_inst_name() << ": "
  2074. << "expected operand P data type to be equal to the type of "
  2075. "operand Data components";
  2076. }
  2077. break;
  2078. }
  2079. case OpenCLLIB::Vload_half: {
  2080. if (!_.IsFloatScalarType(result_type)) {
  2081. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2082. << ext_inst_name() << ": "
  2083. << "expected Result Type to be a float scalar type";
  2084. }
  2085. const uint32_t offset_type = _.GetOperandTypeId(inst, 4);
  2086. const uint32_t p_type = _.GetOperandTypeId(inst, 5);
  2087. const uint32_t size_t_bit_width = GetSizeTBitWidth(_);
  2088. if (!size_t_bit_width) {
  2089. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2090. << ext_inst_name()
  2091. << " can only be used with physical addressing models";
  2092. }
  2093. if (!_.IsIntScalarType(offset_type) ||
  2094. _.GetBitWidth(offset_type) != size_t_bit_width) {
  2095. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2096. << ext_inst_name() << ": "
  2097. << "expected operand Offset to be of type size_t ("
  2098. << size_t_bit_width
  2099. << "-bit integer for the addressing model used in the module)";
  2100. }
  2101. uint32_t p_storage_class = 0;
  2102. uint32_t p_data_type = 0;
  2103. if (!_.GetPointerTypeInfo(p_type, &p_data_type, &p_storage_class)) {
  2104. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2105. << ext_inst_name() << ": "
  2106. << "expected operand P to be a pointer";
  2107. }
  2108. if (p_storage_class != SpvStorageClassUniformConstant &&
  2109. p_storage_class != SpvStorageClassGeneric &&
  2110. p_storage_class != SpvStorageClassCrossWorkgroup &&
  2111. p_storage_class != SpvStorageClassWorkgroup &&
  2112. p_storage_class != SpvStorageClassFunction) {
  2113. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2114. << ext_inst_name() << ": "
  2115. << "expected operand P storage class to be UniformConstant, "
  2116. "Generic, CrossWorkgroup, Workgroup or Function";
  2117. }
  2118. if (!_.IsFloatScalarType(p_data_type) ||
  2119. _.GetBitWidth(p_data_type) != 16) {
  2120. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2121. << ext_inst_name() << ": "
  2122. << "expected operand P data type to be 16-bit float scalar";
  2123. }
  2124. break;
  2125. }
  2126. case OpenCLLIB::Vload_halfn:
  2127. case OpenCLLIB::Vloada_halfn: {
  2128. if (!_.IsFloatVectorType(result_type)) {
  2129. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2130. << ext_inst_name() << ": "
  2131. << "expected Result Type to be a float vector type";
  2132. }
  2133. const uint32_t num_components = _.GetDimension(result_type);
  2134. if (num_components > 4 && num_components != 8 && num_components != 16) {
  2135. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2136. << ext_inst_name() << ": "
  2137. << "expected Result Type to have 2, 3, 4, 8 or 16 components";
  2138. }
  2139. const uint32_t offset_type = _.GetOperandTypeId(inst, 4);
  2140. const uint32_t p_type = _.GetOperandTypeId(inst, 5);
  2141. const uint32_t size_t_bit_width = GetSizeTBitWidth(_);
  2142. if (!size_t_bit_width) {
  2143. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2144. << ext_inst_name()
  2145. << " can only be used with physical addressing models";
  2146. }
  2147. if (!_.IsIntScalarType(offset_type) ||
  2148. _.GetBitWidth(offset_type) != size_t_bit_width) {
  2149. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2150. << ext_inst_name() << ": "
  2151. << "expected operand Offset to be of type size_t ("
  2152. << size_t_bit_width
  2153. << "-bit integer for the addressing model used in the module)";
  2154. }
  2155. uint32_t p_storage_class = 0;
  2156. uint32_t p_data_type = 0;
  2157. if (!_.GetPointerTypeInfo(p_type, &p_data_type, &p_storage_class)) {
  2158. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2159. << ext_inst_name() << ": "
  2160. << "expected operand P to be a pointer";
  2161. }
  2162. if (p_storage_class != SpvStorageClassUniformConstant &&
  2163. p_storage_class != SpvStorageClassGeneric &&
  2164. p_storage_class != SpvStorageClassCrossWorkgroup &&
  2165. p_storage_class != SpvStorageClassWorkgroup &&
  2166. p_storage_class != SpvStorageClassFunction) {
  2167. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2168. << ext_inst_name() << ": "
  2169. << "expected operand P storage class to be UniformConstant, "
  2170. "Generic, CrossWorkgroup, Workgroup or Function";
  2171. }
  2172. if (!_.IsFloatScalarType(p_data_type) ||
  2173. _.GetBitWidth(p_data_type) != 16) {
  2174. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2175. << ext_inst_name() << ": "
  2176. << "expected operand P data type to be 16-bit float scalar";
  2177. }
  2178. const uint32_t n_value = inst->word(7);
  2179. if (num_components != n_value) {
  2180. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2181. << ext_inst_name() << ": "
  2182. << "expected literal N to be equal to the number of "
  2183. "components of Result Type";
  2184. }
  2185. break;
  2186. }
  2187. case OpenCLLIB::Vstore_half:
  2188. case OpenCLLIB::Vstore_half_r:
  2189. case OpenCLLIB::Vstore_halfn:
  2190. case OpenCLLIB::Vstore_halfn_r:
  2191. case OpenCLLIB::Vstorea_halfn:
  2192. case OpenCLLIB::Vstorea_halfn_r: {
  2193. if (_.GetIdOpcode(result_type) != SpvOpTypeVoid) {
  2194. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2195. << ext_inst_name() << ": expected Result Type to be void";
  2196. }
  2197. const uint32_t data_type = _.GetOperandTypeId(inst, 4);
  2198. const uint32_t offset_type = _.GetOperandTypeId(inst, 5);
  2199. const uint32_t p_type = _.GetOperandTypeId(inst, 6);
  2200. const uint32_t data_type_bit_width = _.GetBitWidth(data_type);
  2201. if (ext_inst_key == OpenCLLIB::Vstore_half ||
  2202. ext_inst_key == OpenCLLIB::Vstore_half_r) {
  2203. if (!_.IsFloatScalarType(data_type) ||
  2204. (data_type_bit_width != 32 && data_type_bit_width != 64)) {
  2205. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2206. << ext_inst_name() << ": "
  2207. << "expected Data to be a 32 or 64-bit float scalar";
  2208. }
  2209. } else {
  2210. if (!_.IsFloatVectorType(data_type) ||
  2211. (data_type_bit_width != 32 && data_type_bit_width != 64)) {
  2212. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2213. << ext_inst_name() << ": "
  2214. << "expected Data to be a 32 or 64-bit float vector";
  2215. }
  2216. const uint32_t num_components = _.GetDimension(data_type);
  2217. if (num_components > 4 && num_components != 8 &&
  2218. num_components != 16) {
  2219. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2220. << ext_inst_name() << ": "
  2221. << "expected Data to have 2, 3, 4, 8 or 16 components";
  2222. }
  2223. }
  2224. const uint32_t size_t_bit_width = GetSizeTBitWidth(_);
  2225. if (!size_t_bit_width) {
  2226. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2227. << ext_inst_name()
  2228. << " can only be used with physical addressing models";
  2229. }
  2230. if (!_.IsIntScalarType(offset_type) ||
  2231. _.GetBitWidth(offset_type) != size_t_bit_width) {
  2232. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2233. << ext_inst_name() << ": "
  2234. << "expected operand Offset to be of type size_t ("
  2235. << size_t_bit_width
  2236. << "-bit integer for the addressing model used in the module)";
  2237. }
  2238. uint32_t p_storage_class = 0;
  2239. uint32_t p_data_type = 0;
  2240. if (!_.GetPointerTypeInfo(p_type, &p_data_type, &p_storage_class)) {
  2241. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2242. << ext_inst_name() << ": "
  2243. << "expected operand P to be a pointer";
  2244. }
  2245. if (p_storage_class != SpvStorageClassGeneric &&
  2246. p_storage_class != SpvStorageClassCrossWorkgroup &&
  2247. p_storage_class != SpvStorageClassWorkgroup &&
  2248. p_storage_class != SpvStorageClassFunction) {
  2249. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2250. << ext_inst_name() << ": "
  2251. << "expected operand P storage class to be Generic, "
  2252. "CrossWorkgroup, Workgroup or Function";
  2253. }
  2254. if (!_.IsFloatScalarType(p_data_type) ||
  2255. _.GetBitWidth(p_data_type) != 16) {
  2256. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2257. << ext_inst_name() << ": "
  2258. << "expected operand P data type to be 16-bit float scalar";
  2259. }
  2260. // Rounding mode enum is checked by assembler.
  2261. break;
  2262. }
  2263. case OpenCLLIB::Shuffle:
  2264. case OpenCLLIB::Shuffle2: {
  2265. if (!_.IsFloatVectorType(result_type) &&
  2266. !_.IsIntVectorType(result_type)) {
  2267. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2268. << ext_inst_name() << ": "
  2269. << "expected Result Type to be an int or float vector type";
  2270. }
  2271. const uint32_t result_num_components = _.GetDimension(result_type);
  2272. if (result_num_components != 2 && result_num_components != 4 &&
  2273. result_num_components != 8 && result_num_components != 16) {
  2274. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2275. << ext_inst_name() << ": "
  2276. << "expected Result Type to have 2, 4, 8 or 16 components";
  2277. }
  2278. uint32_t operand_index = 4;
  2279. const uint32_t x_type = _.GetOperandTypeId(inst, operand_index++);
  2280. if (ext_inst_key == OpenCLLIB::Shuffle2) {
  2281. const uint32_t y_type = _.GetOperandTypeId(inst, operand_index++);
  2282. if (x_type != y_type) {
  2283. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2284. << ext_inst_name() << ": "
  2285. << "expected operands X and Y to be of the same type";
  2286. }
  2287. }
  2288. const uint32_t shuffle_mask_type =
  2289. _.GetOperandTypeId(inst, operand_index++);
  2290. if (!_.IsFloatVectorType(x_type) && !_.IsIntVectorType(x_type)) {
  2291. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2292. << ext_inst_name() << ": "
  2293. << "expected operand X to be an int or float vector";
  2294. }
  2295. const uint32_t x_num_components = _.GetDimension(x_type);
  2296. if (x_num_components != 2 && x_num_components != 4 &&
  2297. x_num_components != 8 && x_num_components != 16) {
  2298. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2299. << ext_inst_name() << ": "
  2300. << "expected operand X to have 2, 4, 8 or 16 components";
  2301. }
  2302. const uint32_t result_component_type = _.GetComponentType(result_type);
  2303. if (result_component_type != _.GetComponentType(x_type)) {
  2304. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2305. << ext_inst_name() << ": "
  2306. << "expected operand X and Result Type to have equal "
  2307. "component types";
  2308. }
  2309. if (!_.IsIntVectorType(shuffle_mask_type)) {
  2310. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2311. << ext_inst_name() << ": "
  2312. << "expected operand Shuffle Mask to be an int vector";
  2313. }
  2314. if (result_num_components != _.GetDimension(shuffle_mask_type)) {
  2315. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2316. << ext_inst_name() << ": "
  2317. << "expected operand Shuffle Mask to have the same number of "
  2318. "components as Result Type";
  2319. }
  2320. if (_.GetBitWidth(result_component_type) !=
  2321. _.GetBitWidth(shuffle_mask_type)) {
  2322. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2323. << ext_inst_name() << ": "
  2324. << "expected operand Shuffle Mask components to have the same "
  2325. "bit width as Result Type components";
  2326. }
  2327. break;
  2328. }
  2329. case OpenCLLIB::Printf: {
  2330. if (!_.IsIntScalarType(result_type) ||
  2331. _.GetBitWidth(result_type) != 32) {
  2332. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2333. << ext_inst_name() << ": "
  2334. << "expected Result Type to be a 32-bit int type";
  2335. }
  2336. const uint32_t format_type = _.GetOperandTypeId(inst, 4);
  2337. uint32_t format_storage_class = 0;
  2338. uint32_t format_data_type = 0;
  2339. if (!_.GetPointerTypeInfo(format_type, &format_data_type,
  2340. &format_storage_class)) {
  2341. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2342. << ext_inst_name() << ": "
  2343. << "expected operand Format to be a pointer";
  2344. }
  2345. if (format_storage_class != SpvStorageClassUniformConstant) {
  2346. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2347. << ext_inst_name() << ": "
  2348. << "expected Format storage class to be UniformConstant";
  2349. }
  2350. if (!_.IsIntScalarType(format_data_type) ||
  2351. _.GetBitWidth(format_data_type) != 8) {
  2352. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2353. << ext_inst_name() << ": "
  2354. << "expected Format data type to be 8-bit int";
  2355. }
  2356. break;
  2357. }
  2358. case OpenCLLIB::Prefetch: {
  2359. if (_.GetIdOpcode(result_type) != SpvOpTypeVoid) {
  2360. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2361. << ext_inst_name() << ": expected Result Type to be void";
  2362. }
  2363. const uint32_t p_type = _.GetOperandTypeId(inst, 4);
  2364. const uint32_t num_elements_type = _.GetOperandTypeId(inst, 5);
  2365. uint32_t p_storage_class = 0;
  2366. uint32_t p_data_type = 0;
  2367. if (!_.GetPointerTypeInfo(p_type, &p_data_type, &p_storage_class)) {
  2368. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2369. << ext_inst_name() << ": "
  2370. << "expected operand Ptr to be a pointer";
  2371. }
  2372. if (p_storage_class != SpvStorageClassCrossWorkgroup) {
  2373. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2374. << ext_inst_name() << ": "
  2375. << "expected operand Ptr storage class to be CrossWorkgroup";
  2376. }
  2377. if (!_.IsFloatScalarOrVectorType(p_data_type) &&
  2378. !_.IsIntScalarOrVectorType(p_data_type)) {
  2379. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2380. << ext_inst_name() << ": "
  2381. << "expected Ptr data type to be int or float scalar or "
  2382. "vector";
  2383. }
  2384. const uint32_t num_components = _.GetDimension(p_data_type);
  2385. if (num_components > 4 && num_components != 8 && num_components != 16) {
  2386. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2387. << ext_inst_name() << ": "
  2388. << "expected Result Type to be a scalar or a vector with 2, "
  2389. "3, 4, 8 or 16 components";
  2390. }
  2391. const uint32_t size_t_bit_width = GetSizeTBitWidth(_);
  2392. if (!size_t_bit_width) {
  2393. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2394. << ext_inst_name()
  2395. << " can only be used with physical addressing models";
  2396. }
  2397. if (!_.IsIntScalarType(num_elements_type) ||
  2398. _.GetBitWidth(num_elements_type) != size_t_bit_width) {
  2399. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2400. << ext_inst_name() << ": "
  2401. << "expected operand Num Elements to be of type size_t ("
  2402. << size_t_bit_width
  2403. << "-bit integer for the addressing model used in the module)";
  2404. }
  2405. break;
  2406. }
  2407. }
  2408. } else if (ext_inst_type == SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100 ||
  2409. ext_inst_type ==
  2410. SPV_EXT_INST_TYPE_NONSEMANTIC_SHADER_DEBUGINFO_100) {
  2411. if (!_.IsVoidType(result_type)) {
  2412. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2413. << ext_inst_name() << ": "
  2414. << "expected result type must be a result id of "
  2415. << "OpTypeVoid";
  2416. }
  2417. const bool vulkanDebugInfo =
  2418. ext_inst_type == SPV_EXT_INST_TYPE_NONSEMANTIC_SHADER_DEBUGINFO_100;
  2419. auto num_words = inst->words().size();
  2420. // Handle any non-common NonSemanticShaderDebugInfo instructions.
  2421. if (vulkanDebugInfo) {
  2422. const NonSemanticShaderDebugInfo100Instructions ext_inst_key =
  2423. NonSemanticShaderDebugInfo100Instructions(ext_inst_index);
  2424. switch (ext_inst_key) {
  2425. // The following block of instructions will be handled by the common
  2426. // validation.
  2427. case NonSemanticShaderDebugInfo100DebugInfoNone:
  2428. case NonSemanticShaderDebugInfo100DebugCompilationUnit:
  2429. case NonSemanticShaderDebugInfo100DebugTypeBasic:
  2430. case NonSemanticShaderDebugInfo100DebugTypePointer:
  2431. case NonSemanticShaderDebugInfo100DebugTypeQualifier:
  2432. case NonSemanticShaderDebugInfo100DebugTypeArray:
  2433. case NonSemanticShaderDebugInfo100DebugTypeVector:
  2434. case NonSemanticShaderDebugInfo100DebugTypedef:
  2435. case NonSemanticShaderDebugInfo100DebugTypeFunction:
  2436. case NonSemanticShaderDebugInfo100DebugTypeEnum:
  2437. case NonSemanticShaderDebugInfo100DebugTypeComposite:
  2438. case NonSemanticShaderDebugInfo100DebugTypeMember:
  2439. case NonSemanticShaderDebugInfo100DebugTypeInheritance:
  2440. case NonSemanticShaderDebugInfo100DebugTypePtrToMember:
  2441. case NonSemanticShaderDebugInfo100DebugTypeTemplate:
  2442. case NonSemanticShaderDebugInfo100DebugTypeTemplateParameter:
  2443. case NonSemanticShaderDebugInfo100DebugTypeTemplateTemplateParameter:
  2444. case NonSemanticShaderDebugInfo100DebugTypeTemplateParameterPack:
  2445. case NonSemanticShaderDebugInfo100DebugGlobalVariable:
  2446. case NonSemanticShaderDebugInfo100DebugFunctionDeclaration:
  2447. case NonSemanticShaderDebugInfo100DebugFunction:
  2448. case NonSemanticShaderDebugInfo100DebugLexicalBlock:
  2449. case NonSemanticShaderDebugInfo100DebugLexicalBlockDiscriminator:
  2450. case NonSemanticShaderDebugInfo100DebugScope:
  2451. case NonSemanticShaderDebugInfo100DebugNoScope:
  2452. case NonSemanticShaderDebugInfo100DebugInlinedAt:
  2453. case NonSemanticShaderDebugInfo100DebugLocalVariable:
  2454. case NonSemanticShaderDebugInfo100DebugInlinedVariable:
  2455. case NonSemanticShaderDebugInfo100DebugDeclare:
  2456. case NonSemanticShaderDebugInfo100DebugValue:
  2457. case NonSemanticShaderDebugInfo100DebugOperation:
  2458. case NonSemanticShaderDebugInfo100DebugExpression:
  2459. case NonSemanticShaderDebugInfo100DebugMacroDef:
  2460. case NonSemanticShaderDebugInfo100DebugMacroUndef:
  2461. case NonSemanticShaderDebugInfo100DebugImportedEntity:
  2462. case NonSemanticShaderDebugInfo100DebugSource:
  2463. break;
  2464. case NonSemanticShaderDebugInfo100DebugTypeMatrix: {
  2465. CHECK_DEBUG_OPERAND("Vector Type", CommonDebugInfoDebugTypeVector, 5);
  2466. CHECK_CONST_UINT_OPERAND("Vector Count", 6);
  2467. uint32_t vector_count = inst->word(6);
  2468. uint64_t const_val;
  2469. if (!_.GetConstantValUint64(vector_count, &const_val)) {
  2470. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2471. << ext_inst_name()
  2472. << ": Vector Count must be 32-bit integer OpConstant";
  2473. }
  2474. vector_count = const_val & 0xffffffff;
  2475. if (!vector_count || vector_count > 4) {
  2476. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2477. << ext_inst_name() << ": Vector Count must be positive "
  2478. << "integer less than or equal to 4";
  2479. }
  2480. break;
  2481. }
  2482. // TODO: Add validation rules for remaining cases as well.
  2483. case NonSemanticShaderDebugInfo100DebugFunctionDefinition:
  2484. case NonSemanticShaderDebugInfo100DebugSourceContinued:
  2485. case NonSemanticShaderDebugInfo100DebugLine:
  2486. case NonSemanticShaderDebugInfo100DebugNoLine:
  2487. case NonSemanticShaderDebugInfo100DebugBuildIdentifier:
  2488. case NonSemanticShaderDebugInfo100DebugStoragePath:
  2489. case NonSemanticShaderDebugInfo100DebugEntryPoint:
  2490. break;
  2491. case NonSemanticShaderDebugInfo100InstructionsMax:
  2492. assert(0);
  2493. break;
  2494. }
  2495. }
  2496. // Handle any non-common OpenCL insts, then common
  2497. if (ext_inst_type != SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100 ||
  2498. OpenCLDebugInfo100Instructions(ext_inst_index) !=
  2499. OpenCLDebugInfo100DebugModuleINTEL) {
  2500. const CommonDebugInfoInstructions ext_inst_key =
  2501. CommonDebugInfoInstructions(ext_inst_index);
  2502. switch (ext_inst_key) {
  2503. case CommonDebugInfoDebugInfoNone:
  2504. case CommonDebugInfoDebugNoScope:
  2505. break;
  2506. // The binary parser validates the opcode for DebugInfoNone,
  2507. // DebugNoScope, DebugOperation. We just check the parameters to
  2508. // DebugOperation are properly constants for vulkan debug info.
  2509. case CommonDebugInfoDebugOperation: {
  2510. CHECK_CONST_UINT_OPERAND("Operation", 5);
  2511. for (uint32_t i = 6; i < num_words; ++i) {
  2512. CHECK_CONST_UINT_OPERAND("Operand", i);
  2513. }
  2514. break;
  2515. }
  2516. case CommonDebugInfoDebugCompilationUnit: {
  2517. CHECK_CONST_UINT_OPERAND("Version", 5);
  2518. CHECK_CONST_UINT_OPERAND("DWARF Version", 6);
  2519. CHECK_DEBUG_OPERAND("Source", CommonDebugInfoDebugSource, 7);
  2520. CHECK_CONST_UINT_OPERAND("Language", 8);
  2521. break;
  2522. }
  2523. case CommonDebugInfoDebugSource: {
  2524. CHECK_OPERAND("File", SpvOpString, 5);
  2525. if (num_words == 7) CHECK_OPERAND("Text", SpvOpString, 6);
  2526. break;
  2527. }
  2528. case CommonDebugInfoDebugTypeBasic: {
  2529. CHECK_OPERAND("Name", SpvOpString, 5);
  2530. CHECK_OPERAND("Size", SpvOpConstant, 6);
  2531. CHECK_CONST_UINT_OPERAND("Encoding", 7);
  2532. break;
  2533. }
  2534. case CommonDebugInfoDebugTypePointer: {
  2535. auto validate_base_type =
  2536. ValidateOperandBaseType(_, inst, 5, ext_inst_name);
  2537. if (validate_base_type != SPV_SUCCESS) return validate_base_type;
  2538. CHECK_CONST_UINT_OPERAND("Storage Class", 6);
  2539. CHECK_CONST_UINT_OPERAND("Flags", 7);
  2540. break;
  2541. }
  2542. case CommonDebugInfoDebugTypeQualifier: {
  2543. auto validate_base_type =
  2544. ValidateOperandBaseType(_, inst, 5, ext_inst_name);
  2545. if (validate_base_type != SPV_SUCCESS) return validate_base_type;
  2546. CHECK_CONST_UINT_OPERAND("Type Qualifier", 6);
  2547. break;
  2548. }
  2549. case CommonDebugInfoDebugTypeVector: {
  2550. auto validate_base_type =
  2551. ValidateOperandBaseType(_, inst, 5, ext_inst_name);
  2552. if (validate_base_type != SPV_SUCCESS) return validate_base_type;
  2553. CHECK_CONST_UINT_OPERAND("Component Count", 6);
  2554. uint32_t component_count = inst->word(6);
  2555. if (vulkanDebugInfo) {
  2556. uint64_t const_val;
  2557. if (!_.GetConstantValUint64(component_count, &const_val)) {
  2558. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2559. << ext_inst_name()
  2560. << ": Component Count must be 32-bit integer OpConstant";
  2561. }
  2562. component_count = const_val & 0xffffffff;
  2563. }
  2564. if (!component_count || component_count > 4) {
  2565. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2566. << ext_inst_name() << ": Component Count must be positive "
  2567. << "integer less than or equal to 4";
  2568. }
  2569. break;
  2570. }
  2571. case CommonDebugInfoDebugTypeArray: {
  2572. auto validate_base_type = ValidateOperandDebugType(
  2573. _, "Base Type", inst, 5, ext_inst_name, false);
  2574. if (validate_base_type != SPV_SUCCESS) return validate_base_type;
  2575. for (uint32_t i = 6; i < num_words; ++i) {
  2576. bool invalid = false;
  2577. auto* component_count = _.FindDef(inst->word(i));
  2578. if (IsConstIntScalarTypeWith32Or64Bits(_, component_count)) {
  2579. // TODO: We need a spec discussion for the runtime array for
  2580. // OpenCL.
  2581. if (!vulkanDebugInfo && !component_count->word(3)) {
  2582. invalid = true;
  2583. }
  2584. } else if (component_count->words().size() > 6 &&
  2585. (CommonDebugInfoInstructions(component_count->word(4)) ==
  2586. CommonDebugInfoDebugLocalVariable ||
  2587. CommonDebugInfoInstructions(component_count->word(4)) ==
  2588. CommonDebugInfoDebugGlobalVariable)) {
  2589. auto* component_count_type = _.FindDef(component_count->word(6));
  2590. if (component_count_type->words().size() > 7) {
  2591. uint32_t encoding = component_count_type->word(7);
  2592. if (CommonDebugInfoInstructions(component_count_type->word(
  2593. 4)) != CommonDebugInfoDebugTypeBasic ||
  2594. (vulkanDebugInfo && !IsUint32Constant(_, encoding)) ||
  2595. OpenCLDebugInfo100DebugBaseTypeAttributeEncoding(
  2596. vulkanDebugInfo
  2597. ? GetUint32Constant(_, encoding)
  2598. : encoding) != OpenCLDebugInfo100Unsigned) {
  2599. invalid = true;
  2600. } else {
  2601. // DebugTypeBasic for DebugLocalVariable/DebugGlobalVariable
  2602. // must have Unsigned encoding and 32 or 64 as its size in
  2603. // bits.
  2604. Instruction* size_in_bits =
  2605. _.FindDef(component_count_type->word(6));
  2606. if (!_.IsIntScalarType(size_in_bits->type_id()) ||
  2607. (size_in_bits->word(3) != 32 &&
  2608. size_in_bits->word(3) != 64)) {
  2609. invalid = true;
  2610. }
  2611. }
  2612. } else {
  2613. invalid = true;
  2614. }
  2615. } else {
  2616. invalid = true;
  2617. }
  2618. if (invalid) {
  2619. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2620. << ext_inst_name() << ": Component Count must be "
  2621. << "OpConstant with a 32- or 64-bits integer scalar type "
  2622. "or "
  2623. << "DebugGlobalVariable or DebugLocalVariable with a 32- "
  2624. "or "
  2625. << "64-bits unsigned integer scalar type";
  2626. }
  2627. }
  2628. break;
  2629. }
  2630. case CommonDebugInfoDebugTypedef: {
  2631. CHECK_OPERAND("Name", SpvOpString, 5);
  2632. auto validate_base_type =
  2633. ValidateOperandBaseType(_, inst, 6, ext_inst_name);
  2634. if (validate_base_type != SPV_SUCCESS) return validate_base_type;
  2635. CHECK_DEBUG_OPERAND("Source", CommonDebugInfoDebugSource, 7);
  2636. CHECK_CONST_UINT_OPERAND("Line", 8);
  2637. CHECK_CONST_UINT_OPERAND("Column", 9);
  2638. auto validate_parent =
  2639. ValidateOperandLexicalScope(_, "Parent", inst, 10, ext_inst_name);
  2640. if (validate_parent != SPV_SUCCESS) return validate_parent;
  2641. break;
  2642. }
  2643. case CommonDebugInfoDebugTypeFunction: {
  2644. CHECK_CONST_UINT_OPERAND("Flags", 5);
  2645. auto* return_type = _.FindDef(inst->word(6));
  2646. // TODO: We need a spec discussion that we have to allow return and
  2647. // parameter types of a DebugTypeFunction to have template parameter.
  2648. if (return_type->opcode() != SpvOpTypeVoid) {
  2649. auto validate_return = ValidateOperandDebugType(
  2650. _, "Return Type", inst, 6, ext_inst_name, true);
  2651. if (validate_return != SPV_SUCCESS) return validate_return;
  2652. }
  2653. for (uint32_t word_index = 7; word_index < num_words; ++word_index) {
  2654. auto validate_param = ValidateOperandDebugType(
  2655. _, "Parameter Types", inst, word_index, ext_inst_name, true);
  2656. if (validate_param != SPV_SUCCESS) return validate_param;
  2657. }
  2658. break;
  2659. }
  2660. case CommonDebugInfoDebugTypeEnum: {
  2661. CHECK_OPERAND("Name", SpvOpString, 5);
  2662. if (!DoesDebugInfoOperandMatchExpectation(
  2663. _,
  2664. [](CommonDebugInfoInstructions dbg_inst) {
  2665. return dbg_inst == CommonDebugInfoDebugInfoNone;
  2666. },
  2667. inst, 6)) {
  2668. auto validate_underlying_type = ValidateOperandDebugType(
  2669. _, "Underlying Types", inst, 6, ext_inst_name, false);
  2670. if (validate_underlying_type != SPV_SUCCESS)
  2671. return validate_underlying_type;
  2672. }
  2673. CHECK_DEBUG_OPERAND("Source", CommonDebugInfoDebugSource, 7);
  2674. CHECK_CONST_UINT_OPERAND("Line", 8);
  2675. CHECK_CONST_UINT_OPERAND("Column", 9);
  2676. auto validate_parent =
  2677. ValidateOperandLexicalScope(_, "Parent", inst, 10, ext_inst_name);
  2678. if (validate_parent != SPV_SUCCESS) return validate_parent;
  2679. CHECK_OPERAND("Size", SpvOpConstant, 11);
  2680. auto* size = _.FindDef(inst->word(11));
  2681. if (!_.IsIntScalarType(size->type_id()) || !size->word(3)) {
  2682. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2683. << ext_inst_name() << ": expected operand Size is a "
  2684. << "positive integer";
  2685. }
  2686. CHECK_CONST_UINT_OPERAND("Flags", 12);
  2687. for (uint32_t word_index = 13; word_index + 1 < num_words;
  2688. word_index += 2) {
  2689. CHECK_OPERAND("Value", SpvOpConstant, word_index);
  2690. CHECK_OPERAND("Name", SpvOpString, word_index + 1);
  2691. }
  2692. break;
  2693. }
  2694. case CommonDebugInfoDebugTypeComposite: {
  2695. CHECK_OPERAND("Name", SpvOpString, 5);
  2696. CHECK_DEBUG_OPERAND("Source", CommonDebugInfoDebugSource, 7);
  2697. CHECK_CONST_UINT_OPERAND("Line", 8);
  2698. CHECK_CONST_UINT_OPERAND("Column", 9);
  2699. auto validate_parent =
  2700. ValidateOperandLexicalScope(_, "Parent", inst, 10, ext_inst_name);
  2701. if (validate_parent != SPV_SUCCESS) return validate_parent;
  2702. CHECK_OPERAND("Linkage Name", SpvOpString, 11);
  2703. if (!DoesDebugInfoOperandMatchExpectation(
  2704. _,
  2705. [](CommonDebugInfoInstructions dbg_inst) {
  2706. return dbg_inst == CommonDebugInfoDebugInfoNone;
  2707. },
  2708. inst, 12)) {
  2709. CHECK_OPERAND("Size", SpvOpConstant, 12);
  2710. }
  2711. CHECK_CONST_UINT_OPERAND("Flags", 13);
  2712. for (uint32_t word_index = 14; word_index < num_words; ++word_index) {
  2713. if (!DoesDebugInfoOperandMatchExpectation(
  2714. _,
  2715. [](CommonDebugInfoInstructions dbg_inst) {
  2716. return dbg_inst == CommonDebugInfoDebugTypeMember ||
  2717. dbg_inst == CommonDebugInfoDebugFunction ||
  2718. dbg_inst == CommonDebugInfoDebugTypeInheritance;
  2719. },
  2720. inst, word_index)) {
  2721. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2722. << ext_inst_name() << ": "
  2723. << "expected operand Members "
  2724. << "must be DebugTypeMember, DebugFunction, or "
  2725. "DebugTypeInheritance";
  2726. }
  2727. }
  2728. break;
  2729. }
  2730. case CommonDebugInfoDebugTypeMember: {
  2731. CHECK_OPERAND("Name", SpvOpString, 5);
  2732. // TODO: We need a spec discussion that we have to allow member types
  2733. // to have template parameter.
  2734. auto validate_type =
  2735. ValidateOperandDebugType(_, "Type", inst, 6, ext_inst_name, true);
  2736. if (validate_type != SPV_SUCCESS) return validate_type;
  2737. CHECK_DEBUG_OPERAND("Source", CommonDebugInfoDebugSource, 7);
  2738. CHECK_CONST_UINT_OPERAND("Line", 8);
  2739. CHECK_CONST_UINT_OPERAND("Column", 9);
  2740. // NonSemantic.Shader.DebugInfo doesn't have the Parent operand
  2741. if (vulkanDebugInfo) {
  2742. CHECK_OPERAND("Offset", SpvOpConstant, 10);
  2743. CHECK_OPERAND("Size", SpvOpConstant, 11);
  2744. CHECK_CONST_UINT_OPERAND("Flags", 12);
  2745. if (num_words == 14) CHECK_OPERAND("Value", SpvOpConstant, 13);
  2746. } else {
  2747. CHECK_DEBUG_OPERAND("Parent", CommonDebugInfoDebugTypeComposite,
  2748. 10);
  2749. CHECK_OPERAND("Offset", SpvOpConstant, 11);
  2750. CHECK_OPERAND("Size", SpvOpConstant, 12);
  2751. CHECK_CONST_UINT_OPERAND("Flags", 13);
  2752. if (num_words == 15) CHECK_OPERAND("Value", SpvOpConstant, 14);
  2753. }
  2754. break;
  2755. }
  2756. case CommonDebugInfoDebugTypeInheritance: {
  2757. CHECK_DEBUG_OPERAND("Child", CommonDebugInfoDebugTypeComposite, 5);
  2758. auto* debug_inst = _.FindDef(inst->word(5));
  2759. auto composite_type =
  2760. OpenCLDebugInfo100DebugCompositeType(debug_inst->word(6));
  2761. if (composite_type != OpenCLDebugInfo100Class &&
  2762. composite_type != OpenCLDebugInfo100Structure) {
  2763. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2764. << ext_inst_name() << ": "
  2765. << "expected operand Child must be class or struct debug "
  2766. "type";
  2767. }
  2768. CHECK_DEBUG_OPERAND("Parent", CommonDebugInfoDebugTypeComposite, 6);
  2769. debug_inst = _.FindDef(inst->word(6));
  2770. composite_type =
  2771. OpenCLDebugInfo100DebugCompositeType(debug_inst->word(6));
  2772. if (composite_type != OpenCLDebugInfo100Class &&
  2773. composite_type != OpenCLDebugInfo100Structure) {
  2774. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2775. << ext_inst_name() << ": "
  2776. << "expected operand Parent must be class or struct debug "
  2777. "type";
  2778. }
  2779. CHECK_OPERAND("Offset", SpvOpConstant, 7);
  2780. CHECK_OPERAND("Size", SpvOpConstant, 8);
  2781. CHECK_CONST_UINT_OPERAND("Flags", 9);
  2782. break;
  2783. }
  2784. case CommonDebugInfoDebugFunction: {
  2785. CHECK_OPERAND("Name", SpvOpString, 5);
  2786. auto validate_type = ValidateOperandDebugType(_, "Type", inst, 6,
  2787. ext_inst_name, false);
  2788. if (validate_type != SPV_SUCCESS) return validate_type;
  2789. CHECK_DEBUG_OPERAND("Source", CommonDebugInfoDebugSource, 7);
  2790. CHECK_CONST_UINT_OPERAND("Line", 8);
  2791. CHECK_CONST_UINT_OPERAND("Column", 9);
  2792. auto validate_parent =
  2793. ValidateOperandLexicalScope(_, "Parent", inst, 10, ext_inst_name);
  2794. if (validate_parent != SPV_SUCCESS) return validate_parent;
  2795. CHECK_OPERAND("Linkage Name", SpvOpString, 11);
  2796. CHECK_CONST_UINT_OPERAND("Flags", 12);
  2797. CHECK_CONST_UINT_OPERAND("Scope Line", 13);
  2798. // NonSemantic.Shader.DebugInfo.100 doesn't include a reference to the
  2799. // OpFunction
  2800. if (vulkanDebugInfo) {
  2801. if (num_words == 15) {
  2802. CHECK_DEBUG_OPERAND("Declaration",
  2803. CommonDebugInfoDebugFunctionDeclaration, 14);
  2804. }
  2805. } else {
  2806. if (!DoesDebugInfoOperandMatchExpectation(
  2807. _,
  2808. [](CommonDebugInfoInstructions dbg_inst) {
  2809. return dbg_inst == CommonDebugInfoDebugInfoNone;
  2810. },
  2811. inst, 14)) {
  2812. CHECK_OPERAND("Function", SpvOpFunction, 14);
  2813. }
  2814. if (num_words == 16) {
  2815. CHECK_DEBUG_OPERAND("Declaration",
  2816. CommonDebugInfoDebugFunctionDeclaration, 15);
  2817. }
  2818. }
  2819. break;
  2820. }
  2821. case CommonDebugInfoDebugFunctionDeclaration: {
  2822. CHECK_OPERAND("Name", SpvOpString, 5);
  2823. auto validate_type = ValidateOperandDebugType(_, "Type", inst, 6,
  2824. ext_inst_name, false);
  2825. if (validate_type != SPV_SUCCESS) return validate_type;
  2826. CHECK_DEBUG_OPERAND("Source", CommonDebugInfoDebugSource, 7);
  2827. CHECK_CONST_UINT_OPERAND("Line", 8);
  2828. CHECK_CONST_UINT_OPERAND("Column", 9);
  2829. auto validate_parent =
  2830. ValidateOperandLexicalScope(_, "Parent", inst, 10, ext_inst_name);
  2831. if (validate_parent != SPV_SUCCESS) return validate_parent;
  2832. CHECK_OPERAND("Linkage Name", SpvOpString, 11);
  2833. CHECK_CONST_UINT_OPERAND("Flags", 12);
  2834. break;
  2835. }
  2836. case CommonDebugInfoDebugLexicalBlock: {
  2837. CHECK_DEBUG_OPERAND("Source", CommonDebugInfoDebugSource, 5);
  2838. CHECK_CONST_UINT_OPERAND("Line", 6);
  2839. CHECK_CONST_UINT_OPERAND("Column", 7);
  2840. auto validate_parent =
  2841. ValidateOperandLexicalScope(_, "Parent", inst, 8, ext_inst_name);
  2842. if (validate_parent != SPV_SUCCESS) return validate_parent;
  2843. if (num_words == 10) CHECK_OPERAND("Name", SpvOpString, 9);
  2844. break;
  2845. }
  2846. case CommonDebugInfoDebugScope: {
  2847. auto validate_scope =
  2848. ValidateOperandLexicalScope(_, "Scope", inst, 5, ext_inst_name);
  2849. if (validate_scope != SPV_SUCCESS) return validate_scope;
  2850. if (num_words == 7) {
  2851. CHECK_DEBUG_OPERAND("Inlined At", CommonDebugInfoDebugInlinedAt, 6);
  2852. }
  2853. break;
  2854. }
  2855. case CommonDebugInfoDebugLocalVariable: {
  2856. CHECK_OPERAND("Name", SpvOpString, 5);
  2857. // TODO: We need a spec discussion that we have to allow local
  2858. // variable types to have template parameter.
  2859. auto validate_type =
  2860. ValidateOperandDebugType(_, "Type", inst, 6, ext_inst_name, true);
  2861. if (validate_type != SPV_SUCCESS) return validate_type;
  2862. CHECK_DEBUG_OPERAND("Source", CommonDebugInfoDebugSource, 7);
  2863. CHECK_CONST_UINT_OPERAND("Line", 8);
  2864. CHECK_CONST_UINT_OPERAND("Column", 9);
  2865. auto validate_parent =
  2866. ValidateOperandLexicalScope(_, "Parent", inst, 10, ext_inst_name);
  2867. if (validate_parent != SPV_SUCCESS) return validate_parent;
  2868. CHECK_CONST_UINT_OPERAND("Flags", 11);
  2869. if (num_words == 13) {
  2870. CHECK_CONST_UINT_OPERAND("ArgNumber", 12);
  2871. }
  2872. break;
  2873. }
  2874. case CommonDebugInfoDebugDeclare: {
  2875. CHECK_DEBUG_OPERAND("Local Variable",
  2876. CommonDebugInfoDebugLocalVariable, 5);
  2877. auto* operand = _.FindDef(inst->word(6));
  2878. if (operand->opcode() != SpvOpVariable &&
  2879. operand->opcode() != SpvOpFunctionParameter) {
  2880. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2881. << ext_inst_name() << ": "
  2882. << "expected operand Variable must be a result id of "
  2883. "OpVariable or OpFunctionParameter";
  2884. }
  2885. CHECK_DEBUG_OPERAND("Expression", CommonDebugInfoDebugExpression, 7);
  2886. if (vulkanDebugInfo) {
  2887. for (uint32_t word_index = 8; word_index < num_words;
  2888. ++word_index) {
  2889. auto index_inst = _.FindDef(inst->word(word_index));
  2890. auto type_id = index_inst != nullptr ? index_inst->type_id() : 0;
  2891. if (type_id == 0 || !IsIntScalar(_, type_id, false, false))
  2892. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2893. << ext_inst_name() << ": "
  2894. << "expected index must be scalar integer";
  2895. }
  2896. }
  2897. break;
  2898. }
  2899. case CommonDebugInfoDebugExpression: {
  2900. for (uint32_t word_index = 5; word_index < num_words; ++word_index) {
  2901. CHECK_DEBUG_OPERAND("Operation", CommonDebugInfoDebugOperation,
  2902. word_index);
  2903. }
  2904. break;
  2905. }
  2906. case CommonDebugInfoDebugTypeTemplate: {
  2907. if (!DoesDebugInfoOperandMatchExpectation(
  2908. _,
  2909. [](CommonDebugInfoInstructions dbg_inst) {
  2910. return dbg_inst == CommonDebugInfoDebugTypeComposite ||
  2911. dbg_inst == CommonDebugInfoDebugFunction;
  2912. },
  2913. inst, 5)) {
  2914. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2915. << ext_inst_name() << ": "
  2916. << "expected operand Target must be DebugTypeComposite "
  2917. << "or DebugFunction";
  2918. }
  2919. for (uint32_t word_index = 6; word_index < num_words; ++word_index) {
  2920. if (!DoesDebugInfoOperandMatchExpectation(
  2921. _,
  2922. [](CommonDebugInfoInstructions dbg_inst) {
  2923. return dbg_inst ==
  2924. CommonDebugInfoDebugTypeTemplateParameter ||
  2925. dbg_inst ==
  2926. CommonDebugInfoDebugTypeTemplateTemplateParameter;
  2927. },
  2928. inst, word_index)) {
  2929. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2930. << ext_inst_name() << ": "
  2931. << "expected operand Parameters must be "
  2932. << "DebugTypeTemplateParameter or "
  2933. << "DebugTypeTemplateTemplateParameter";
  2934. }
  2935. }
  2936. break;
  2937. }
  2938. case CommonDebugInfoDebugTypeTemplateParameter: {
  2939. CHECK_OPERAND("Name", SpvOpString, 5);
  2940. auto validate_actual_type = ValidateOperandDebugType(
  2941. _, "Actual Type", inst, 6, ext_inst_name, false);
  2942. if (validate_actual_type != SPV_SUCCESS) return validate_actual_type;
  2943. if (!DoesDebugInfoOperandMatchExpectation(
  2944. _,
  2945. [](CommonDebugInfoInstructions dbg_inst) {
  2946. return dbg_inst == CommonDebugInfoDebugInfoNone;
  2947. },
  2948. inst, 7)) {
  2949. CHECK_OPERAND("Value", SpvOpConstant, 7);
  2950. }
  2951. CHECK_DEBUG_OPERAND("Source", CommonDebugInfoDebugSource, 8);
  2952. CHECK_CONST_UINT_OPERAND("Line", 9);
  2953. CHECK_CONST_UINT_OPERAND("Column", 10);
  2954. break;
  2955. }
  2956. case CommonDebugInfoDebugGlobalVariable: {
  2957. CHECK_OPERAND("Name", SpvOpString, 5);
  2958. auto validate_type = ValidateOperandDebugType(_, "Type", inst, 6,
  2959. ext_inst_name, false);
  2960. if (validate_type != SPV_SUCCESS) return validate_type;
  2961. CHECK_DEBUG_OPERAND("Source", CommonDebugInfoDebugSource, 7);
  2962. CHECK_CONST_UINT_OPERAND("Line", 8);
  2963. CHECK_CONST_UINT_OPERAND("Column", 9);
  2964. auto validate_scope =
  2965. ValidateOperandLexicalScope(_, "Scope", inst, 10, ext_inst_name);
  2966. if (validate_scope != SPV_SUCCESS) return validate_scope;
  2967. CHECK_OPERAND("Linkage Name", SpvOpString, 11);
  2968. if (!DoesDebugInfoOperandMatchExpectation(
  2969. _,
  2970. [](CommonDebugInfoInstructions dbg_inst) {
  2971. return dbg_inst == CommonDebugInfoDebugInfoNone;
  2972. },
  2973. inst, 12)) {
  2974. auto* operand = _.FindDef(inst->word(12));
  2975. if (operand->opcode() != SpvOpVariable &&
  2976. operand->opcode() != SpvOpConstant) {
  2977. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  2978. << ext_inst_name() << ": "
  2979. << "expected operand Variable must be a result id of "
  2980. "OpVariable or OpConstant or DebugInfoNone";
  2981. }
  2982. }
  2983. if (num_words == 15) {
  2984. CHECK_DEBUG_OPERAND("Static Member Declaration",
  2985. CommonDebugInfoDebugTypeMember, 14);
  2986. }
  2987. break;
  2988. }
  2989. case CommonDebugInfoDebugInlinedAt: {
  2990. CHECK_CONST_UINT_OPERAND("Line", 5);
  2991. auto validate_scope =
  2992. ValidateOperandLexicalScope(_, "Scope", inst, 6, ext_inst_name);
  2993. if (validate_scope != SPV_SUCCESS) return validate_scope;
  2994. if (num_words == 8) {
  2995. CHECK_DEBUG_OPERAND("Inlined", CommonDebugInfoDebugInlinedAt, 7);
  2996. }
  2997. break;
  2998. }
  2999. case CommonDebugInfoDebugValue: {
  3000. CHECK_DEBUG_OPERAND("Local Variable",
  3001. CommonDebugInfoDebugLocalVariable, 5);
  3002. CHECK_DEBUG_OPERAND("Expression", CommonDebugInfoDebugExpression, 7);
  3003. for (uint32_t word_index = 8; word_index < num_words; ++word_index) {
  3004. // TODO: The following code simply checks if it is a const int
  3005. // scalar or a DebugLocalVariable or DebugGlobalVariable, but we
  3006. // have to check it using the same validation for Indexes of
  3007. // OpAccessChain.
  3008. if (!IsConstWithIntScalarType(_, inst, word_index) &&
  3009. !IsDebugVariableWithIntScalarType(_, inst, word_index)) {
  3010. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  3011. << ext_inst_name() << ": expected operand Indexes is "
  3012. << "OpConstant, DebugGlobalVariable, or "
  3013. << "type is OpConstant with an integer scalar type";
  3014. }
  3015. }
  3016. break;
  3017. }
  3018. // TODO: Add validation rules for remaining cases as well.
  3019. case CommonDebugInfoDebugTypePtrToMember:
  3020. case CommonDebugInfoDebugTypeTemplateTemplateParameter:
  3021. case CommonDebugInfoDebugTypeTemplateParameterPack:
  3022. case CommonDebugInfoDebugLexicalBlockDiscriminator:
  3023. case CommonDebugInfoDebugInlinedVariable:
  3024. case CommonDebugInfoDebugMacroDef:
  3025. case CommonDebugInfoDebugMacroUndef:
  3026. case CommonDebugInfoDebugImportedEntity:
  3027. break;
  3028. case CommonDebugInfoInstructionsMax:
  3029. assert(0);
  3030. break;
  3031. }
  3032. }
  3033. } else if (ext_inst_type == SPV_EXT_INST_TYPE_NONSEMANTIC_CLSPVREFLECTION) {
  3034. auto import_inst = _.FindDef(inst->GetOperandAs<uint32_t>(2));
  3035. const std::string name = import_inst->GetOperandAs<std::string>(1);
  3036. const std::string reflection = "NonSemantic.ClspvReflection.";
  3037. char* end_ptr;
  3038. auto version_string = name.substr(reflection.size());
  3039. if (version_string.empty()) {
  3040. return _.diag(SPV_ERROR_INVALID_DATA, import_inst)
  3041. << "Missing NonSemantic.ClspvReflection import version";
  3042. }
  3043. uint32_t version = static_cast<uint32_t>(
  3044. std::strtoul(version_string.c_str(), &end_ptr, 10));
  3045. if (end_ptr && *end_ptr != '\0') {
  3046. return _.diag(SPV_ERROR_INVALID_DATA, import_inst)
  3047. << "NonSemantic.ClspvReflection import does not encode the "
  3048. "version correctly";
  3049. }
  3050. if (version == 0 || version > NonSemanticClspvReflectionRevision) {
  3051. return _.diag(SPV_ERROR_INVALID_DATA, import_inst)
  3052. << "Unknown NonSemantic.ClspvReflection import version";
  3053. }
  3054. return ValidateClspvReflectionInstruction(_, inst, version);
  3055. }
  3056. return SPV_SUCCESS;
  3057. }
  3058. spv_result_t ExtensionPass(ValidationState_t& _, const Instruction* inst) {
  3059. const SpvOp opcode = inst->opcode();
  3060. if (opcode == SpvOpExtension) return ValidateExtension(_, inst);
  3061. if (opcode == SpvOpExtInstImport) return ValidateExtInstImport(_, inst);
  3062. if (opcode == SpvOpExtInst) return ValidateExtInst(_, inst);
  3063. return SPV_SUCCESS;
  3064. }
  3065. } // namespace val
  3066. } // namespace spvtools