validate_extensions.cpp 148 KB

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