InstBuilderAuto.cpp 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060
  1. //===-- InstBuilder.cpp - SPIR-V instruction builder ------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // AUTOMATICALLY GENERATED from the SPIR-V JSON grammar:
  10. // spirv.core.grammar.json.
  11. // DO NOT MODIFY!
  12. #include "clang/SPIRV/InstBuilder.h"
  13. namespace clang {
  14. namespace spirv {
  15. static_assert(spv::Version == 0x00010300 && spv::Revision == 1,
  16. "Needs to regenerate outdated InstBuilder");
  17. namespace {
  18. inline bool bitEnumContains(spv::ImageOperandsMask bits,
  19. spv::ImageOperandsMask bit) {
  20. return (uint32_t(bits) & uint32_t(bit)) != 0;
  21. }
  22. inline bool bitEnumContains(spv::LoopControlMask bits,
  23. spv::LoopControlMask bit) {
  24. return (uint32_t(bits) & uint32_t(bit)) != 0;
  25. }
  26. inline bool bitEnumContains(spv::MemoryAccessMask bits,
  27. spv::MemoryAccessMask bit) {
  28. return (uint32_t(bits) & uint32_t(bit)) != 0;
  29. }
  30. } // namespace
  31. InstBuilder::InstBuilder(WordConsumer consumer)
  32. : TheConsumer(consumer), TheStatus(Status::Success) {}
  33. void InstBuilder::setConsumer(WordConsumer consumer) { TheConsumer = consumer; }
  34. const WordConsumer &InstBuilder::getConsumer() const { return TheConsumer; }
  35. InstBuilder::Status InstBuilder::x() {
  36. if (TheConsumer == nullptr)
  37. return Status::NullConsumer;
  38. if (TheStatus != Status::Success)
  39. return TheStatus;
  40. if (!Expectation.empty()) {
  41. switch (Expectation.front()) {
  42. case OperandKind::BuiltIn:
  43. return Status::ExpectBuiltIn;
  44. case OperandKind::FPFastMathMode:
  45. return Status::ExpectFPFastMathMode;
  46. case OperandKind::FPRoundingMode:
  47. return Status::ExpectFPRoundingMode;
  48. case OperandKind::FunctionParameterAttribute:
  49. return Status::ExpectFunctionParameterAttribute;
  50. case OperandKind::IdRef:
  51. return Status::ExpectIdRef;
  52. case OperandKind::LinkageType:
  53. return Status::ExpectLinkageType;
  54. case OperandKind::LiteralInteger:
  55. return Status::ExpectLiteralInteger;
  56. case OperandKind::LiteralString:
  57. return Status::ExpectLiteralString;
  58. }
  59. }
  60. if (!TheInst.empty())
  61. TheInst.front() |= uint32_t(TheInst.size()) << 16;
  62. TheConsumer(std::move(TheInst));
  63. TheInst.clear();
  64. return TheStatus;
  65. }
  66. void InstBuilder::clear() {
  67. TheInst.clear();
  68. Expectation.clear();
  69. TheStatus = Status::Success;
  70. }
  71. InstBuilder &InstBuilder::opSourceContinued(llvm::StringRef continued_source) {
  72. if (!TheInst.empty()) {
  73. TheStatus = Status::NestedInst;
  74. return *this;
  75. }
  76. TheInst.reserve(2);
  77. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpSourceContinued));
  78. encodeString(continued_source);
  79. return *this;
  80. }
  81. InstBuilder &InstBuilder::opSource(spv::SourceLanguage source_language,
  82. uint32_t version,
  83. llvm::Optional<uint32_t> file,
  84. llvm::Optional<llvm::StringRef> source) {
  85. if (!TheInst.empty()) {
  86. TheStatus = Status::NestedInst;
  87. return *this;
  88. }
  89. TheInst.reserve(5);
  90. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpSource));
  91. TheInst.emplace_back(static_cast<uint32_t>(source_language));
  92. TheInst.emplace_back(version);
  93. if (file.hasValue()) {
  94. const auto &val = file.getValue();
  95. TheInst.emplace_back(val);
  96. }
  97. if (source.hasValue()) {
  98. const auto &val = source.getValue();
  99. encodeString(val);
  100. }
  101. return *this;
  102. }
  103. InstBuilder &InstBuilder::opSourceExtension(std::string extension) {
  104. if (!TheInst.empty()) {
  105. TheStatus = Status::NestedInst;
  106. return *this;
  107. }
  108. TheInst.reserve(2);
  109. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpSourceExtension));
  110. encodeString(extension);
  111. return *this;
  112. }
  113. InstBuilder &InstBuilder::opName(uint32_t target, std::string name) {
  114. if (!TheInst.empty()) {
  115. TheStatus = Status::NestedInst;
  116. return *this;
  117. }
  118. TheInst.reserve(3);
  119. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpName));
  120. TheInst.emplace_back(target);
  121. encodeString(name);
  122. return *this;
  123. }
  124. InstBuilder &InstBuilder::opMemberName(uint32_t type, uint32_t member,
  125. std::string name) {
  126. if (!TheInst.empty()) {
  127. TheStatus = Status::NestedInst;
  128. return *this;
  129. }
  130. TheInst.reserve(4);
  131. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpMemberName));
  132. TheInst.emplace_back(type);
  133. TheInst.emplace_back(member);
  134. encodeString(name);
  135. return *this;
  136. }
  137. InstBuilder &InstBuilder::opString(uint32_t result_id, std::string string) {
  138. if (!TheInst.empty()) {
  139. TheStatus = Status::NestedInst;
  140. return *this;
  141. }
  142. if (result_id == 0) {
  143. TheStatus = Status::ZeroResultId;
  144. return *this;
  145. }
  146. TheInst.reserve(3);
  147. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpString));
  148. TheInst.emplace_back(result_id);
  149. encodeString(string);
  150. return *this;
  151. }
  152. InstBuilder &InstBuilder::opLine(uint32_t file, uint32_t line,
  153. uint32_t column) {
  154. if (!TheInst.empty()) {
  155. TheStatus = Status::NestedInst;
  156. return *this;
  157. }
  158. TheInst.reserve(4);
  159. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpLine));
  160. TheInst.emplace_back(file);
  161. TheInst.emplace_back(line);
  162. TheInst.emplace_back(column);
  163. return *this;
  164. }
  165. InstBuilder &InstBuilder::opExtension(std::string name) {
  166. if (!TheInst.empty()) {
  167. TheStatus = Status::NestedInst;
  168. return *this;
  169. }
  170. TheInst.reserve(2);
  171. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpExtension));
  172. encodeString(name);
  173. return *this;
  174. }
  175. InstBuilder &InstBuilder::opExtInstImport(uint32_t result_id,
  176. std::string name) {
  177. if (!TheInst.empty()) {
  178. TheStatus = Status::NestedInst;
  179. return *this;
  180. }
  181. if (result_id == 0) {
  182. TheStatus = Status::ZeroResultId;
  183. return *this;
  184. }
  185. TheInst.reserve(3);
  186. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpExtInstImport));
  187. TheInst.emplace_back(result_id);
  188. encodeString(name);
  189. return *this;
  190. }
  191. InstBuilder &
  192. InstBuilder::opExtInst(uint32_t result_type, uint32_t result_id, uint32_t set,
  193. uint32_t instruction,
  194. llvm::ArrayRef<uint32_t> operand_1_operand_2_) {
  195. if (!TheInst.empty()) {
  196. TheStatus = Status::NestedInst;
  197. return *this;
  198. }
  199. if (result_type == 0) {
  200. TheStatus = Status::ZeroResultType;
  201. return *this;
  202. }
  203. if (result_id == 0) {
  204. TheStatus = Status::ZeroResultId;
  205. return *this;
  206. }
  207. TheInst.reserve(6);
  208. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpExtInst));
  209. TheInst.emplace_back(result_type);
  210. TheInst.emplace_back(result_id);
  211. TheInst.emplace_back(set);
  212. TheInst.emplace_back(instruction);
  213. TheInst.insert(TheInst.end(), operand_1_operand_2_.begin(),
  214. operand_1_operand_2_.end());
  215. return *this;
  216. }
  217. InstBuilder &InstBuilder::opMemoryModel(spv::AddressingModel addressing_model,
  218. spv::MemoryModel memory_model) {
  219. if (!TheInst.empty()) {
  220. TheStatus = Status::NestedInst;
  221. return *this;
  222. }
  223. TheInst.reserve(3);
  224. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpMemoryModel));
  225. TheInst.emplace_back(static_cast<uint32_t>(addressing_model));
  226. TheInst.emplace_back(static_cast<uint32_t>(memory_model));
  227. return *this;
  228. }
  229. InstBuilder &InstBuilder::opEntryPoint(spv::ExecutionModel execution_model,
  230. uint32_t entry_point, std::string name,
  231. llvm::ArrayRef<uint32_t> interface) {
  232. if (!TheInst.empty()) {
  233. TheStatus = Status::NestedInst;
  234. return *this;
  235. }
  236. TheInst.reserve(5);
  237. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpEntryPoint));
  238. TheInst.emplace_back(static_cast<uint32_t>(execution_model));
  239. TheInst.emplace_back(entry_point);
  240. encodeString(name);
  241. TheInst.insert(TheInst.end(), interface.begin(), interface.end());
  242. return *this;
  243. }
  244. InstBuilder &InstBuilder::opExecutionMode(uint32_t entry_point,
  245. spv::ExecutionMode mode) {
  246. if (!TheInst.empty()) {
  247. TheStatus = Status::NestedInst;
  248. return *this;
  249. }
  250. TheInst.reserve(3);
  251. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpExecutionMode));
  252. TheInst.emplace_back(entry_point);
  253. encodeExecutionMode(mode);
  254. return *this;
  255. }
  256. InstBuilder &InstBuilder::opCapability(spv::Capability capability) {
  257. if (!TheInst.empty()) {
  258. TheStatus = Status::NestedInst;
  259. return *this;
  260. }
  261. TheInst.reserve(2);
  262. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpCapability));
  263. TheInst.emplace_back(static_cast<uint32_t>(capability));
  264. return *this;
  265. }
  266. InstBuilder &InstBuilder::opTypeVoid(uint32_t result_id) {
  267. if (!TheInst.empty()) {
  268. TheStatus = Status::NestedInst;
  269. return *this;
  270. }
  271. if (result_id == 0) {
  272. TheStatus = Status::ZeroResultId;
  273. return *this;
  274. }
  275. TheInst.reserve(2);
  276. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpTypeVoid));
  277. TheInst.emplace_back(result_id);
  278. return *this;
  279. }
  280. InstBuilder &InstBuilder::opTypeBool(uint32_t result_id) {
  281. if (!TheInst.empty()) {
  282. TheStatus = Status::NestedInst;
  283. return *this;
  284. }
  285. if (result_id == 0) {
  286. TheStatus = Status::ZeroResultId;
  287. return *this;
  288. }
  289. TheInst.reserve(2);
  290. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpTypeBool));
  291. TheInst.emplace_back(result_id);
  292. return *this;
  293. }
  294. InstBuilder &InstBuilder::opTypeInt(uint32_t result_id, uint32_t width,
  295. uint32_t signedness) {
  296. if (!TheInst.empty()) {
  297. TheStatus = Status::NestedInst;
  298. return *this;
  299. }
  300. if (result_id == 0) {
  301. TheStatus = Status::ZeroResultId;
  302. return *this;
  303. }
  304. TheInst.reserve(4);
  305. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpTypeInt));
  306. TheInst.emplace_back(result_id);
  307. TheInst.emplace_back(width);
  308. TheInst.emplace_back(signedness);
  309. return *this;
  310. }
  311. InstBuilder &InstBuilder::opTypeFloat(uint32_t result_id, uint32_t width) {
  312. if (!TheInst.empty()) {
  313. TheStatus = Status::NestedInst;
  314. return *this;
  315. }
  316. if (result_id == 0) {
  317. TheStatus = Status::ZeroResultId;
  318. return *this;
  319. }
  320. TheInst.reserve(3);
  321. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpTypeFloat));
  322. TheInst.emplace_back(result_id);
  323. TheInst.emplace_back(width);
  324. return *this;
  325. }
  326. InstBuilder &InstBuilder::opTypeVector(uint32_t result_id,
  327. uint32_t component_type,
  328. uint32_t component_count) {
  329. if (!TheInst.empty()) {
  330. TheStatus = Status::NestedInst;
  331. return *this;
  332. }
  333. if (result_id == 0) {
  334. TheStatus = Status::ZeroResultId;
  335. return *this;
  336. }
  337. TheInst.reserve(4);
  338. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpTypeVector));
  339. TheInst.emplace_back(result_id);
  340. TheInst.emplace_back(component_type);
  341. TheInst.emplace_back(component_count);
  342. return *this;
  343. }
  344. InstBuilder &InstBuilder::opTypeMatrix(uint32_t result_id, uint32_t column_type,
  345. uint32_t column_count) {
  346. if (!TheInst.empty()) {
  347. TheStatus = Status::NestedInst;
  348. return *this;
  349. }
  350. if (result_id == 0) {
  351. TheStatus = Status::ZeroResultId;
  352. return *this;
  353. }
  354. TheInst.reserve(4);
  355. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpTypeMatrix));
  356. TheInst.emplace_back(result_id);
  357. TheInst.emplace_back(column_type);
  358. TheInst.emplace_back(column_count);
  359. return *this;
  360. }
  361. InstBuilder &InstBuilder::opTypeImage(
  362. uint32_t result_id, uint32_t sampled_type, spv::Dim dim, uint32_t depth,
  363. uint32_t arrayed, uint32_t ms, uint32_t sampled,
  364. spv::ImageFormat image_format,
  365. llvm::Optional<spv::AccessQualifier> access_qualifier) {
  366. if (!TheInst.empty()) {
  367. TheStatus = Status::NestedInst;
  368. return *this;
  369. }
  370. if (result_id == 0) {
  371. TheStatus = Status::ZeroResultId;
  372. return *this;
  373. }
  374. TheInst.reserve(10);
  375. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpTypeImage));
  376. TheInst.emplace_back(result_id);
  377. TheInst.emplace_back(sampled_type);
  378. TheInst.emplace_back(static_cast<uint32_t>(dim));
  379. TheInst.emplace_back(depth);
  380. TheInst.emplace_back(arrayed);
  381. TheInst.emplace_back(ms);
  382. TheInst.emplace_back(sampled);
  383. TheInst.emplace_back(static_cast<uint32_t>(image_format));
  384. if (access_qualifier.hasValue()) {
  385. const auto &val = access_qualifier.getValue();
  386. TheInst.emplace_back(static_cast<uint32_t>(val));
  387. }
  388. return *this;
  389. }
  390. InstBuilder &InstBuilder::opTypeSampler(uint32_t result_id) {
  391. if (!TheInst.empty()) {
  392. TheStatus = Status::NestedInst;
  393. return *this;
  394. }
  395. if (result_id == 0) {
  396. TheStatus = Status::ZeroResultId;
  397. return *this;
  398. }
  399. TheInst.reserve(2);
  400. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpTypeSampler));
  401. TheInst.emplace_back(result_id);
  402. return *this;
  403. }
  404. InstBuilder &InstBuilder::opTypeSampledImage(uint32_t result_id,
  405. uint32_t image_type) {
  406. if (!TheInst.empty()) {
  407. TheStatus = Status::NestedInst;
  408. return *this;
  409. }
  410. if (result_id == 0) {
  411. TheStatus = Status::ZeroResultId;
  412. return *this;
  413. }
  414. TheInst.reserve(3);
  415. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpTypeSampledImage));
  416. TheInst.emplace_back(result_id);
  417. TheInst.emplace_back(image_type);
  418. return *this;
  419. }
  420. InstBuilder &InstBuilder::opTypeArray(uint32_t result_id, uint32_t element_type,
  421. uint32_t length) {
  422. if (!TheInst.empty()) {
  423. TheStatus = Status::NestedInst;
  424. return *this;
  425. }
  426. if (result_id == 0) {
  427. TheStatus = Status::ZeroResultId;
  428. return *this;
  429. }
  430. TheInst.reserve(4);
  431. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpTypeArray));
  432. TheInst.emplace_back(result_id);
  433. TheInst.emplace_back(element_type);
  434. TheInst.emplace_back(length);
  435. return *this;
  436. }
  437. InstBuilder &InstBuilder::opTypeRuntimeArray(uint32_t result_id,
  438. uint32_t element_type) {
  439. if (!TheInst.empty()) {
  440. TheStatus = Status::NestedInst;
  441. return *this;
  442. }
  443. if (result_id == 0) {
  444. TheStatus = Status::ZeroResultId;
  445. return *this;
  446. }
  447. TheInst.reserve(3);
  448. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpTypeRuntimeArray));
  449. TheInst.emplace_back(result_id);
  450. TheInst.emplace_back(element_type);
  451. return *this;
  452. }
  453. InstBuilder &InstBuilder::opTypeStruct(
  454. uint32_t result_id, llvm::ArrayRef<uint32_t> member_0_type_member_1_type_) {
  455. if (!TheInst.empty()) {
  456. TheStatus = Status::NestedInst;
  457. return *this;
  458. }
  459. if (result_id == 0) {
  460. TheStatus = Status::ZeroResultId;
  461. return *this;
  462. }
  463. TheInst.reserve(3);
  464. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpTypeStruct));
  465. TheInst.emplace_back(result_id);
  466. TheInst.insert(TheInst.end(), member_0_type_member_1_type_.begin(),
  467. member_0_type_member_1_type_.end());
  468. return *this;
  469. }
  470. InstBuilder &
  471. InstBuilder::opTypeOpaque(uint32_t result_id,
  472. std::string the_name_of_the_opaque_type) {
  473. if (!TheInst.empty()) {
  474. TheStatus = Status::NestedInst;
  475. return *this;
  476. }
  477. if (result_id == 0) {
  478. TheStatus = Status::ZeroResultId;
  479. return *this;
  480. }
  481. TheInst.reserve(3);
  482. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpTypeOpaque));
  483. TheInst.emplace_back(result_id);
  484. encodeString(the_name_of_the_opaque_type);
  485. return *this;
  486. }
  487. InstBuilder &InstBuilder::opTypePointer(uint32_t result_id,
  488. spv::StorageClass storage_class,
  489. uint32_t type) {
  490. if (!TheInst.empty()) {
  491. TheStatus = Status::NestedInst;
  492. return *this;
  493. }
  494. if (result_id == 0) {
  495. TheStatus = Status::ZeroResultId;
  496. return *this;
  497. }
  498. TheInst.reserve(4);
  499. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpTypePointer));
  500. TheInst.emplace_back(result_id);
  501. TheInst.emplace_back(static_cast<uint32_t>(storage_class));
  502. TheInst.emplace_back(type);
  503. return *this;
  504. }
  505. InstBuilder &InstBuilder::opTypeFunction(
  506. uint32_t result_id, uint32_t return_type,
  507. llvm::ArrayRef<uint32_t> parameter_0_type_parameter_1_type_) {
  508. if (!TheInst.empty()) {
  509. TheStatus = Status::NestedInst;
  510. return *this;
  511. }
  512. if (result_id == 0) {
  513. TheStatus = Status::ZeroResultId;
  514. return *this;
  515. }
  516. TheInst.reserve(4);
  517. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpTypeFunction));
  518. TheInst.emplace_back(result_id);
  519. TheInst.emplace_back(return_type);
  520. TheInst.insert(TheInst.end(), parameter_0_type_parameter_1_type_.begin(),
  521. parameter_0_type_parameter_1_type_.end());
  522. return *this;
  523. }
  524. InstBuilder &InstBuilder::opTypeEvent(uint32_t result_id) {
  525. if (!TheInst.empty()) {
  526. TheStatus = Status::NestedInst;
  527. return *this;
  528. }
  529. if (result_id == 0) {
  530. TheStatus = Status::ZeroResultId;
  531. return *this;
  532. }
  533. TheInst.reserve(2);
  534. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpTypeEvent));
  535. TheInst.emplace_back(result_id);
  536. return *this;
  537. }
  538. InstBuilder &InstBuilder::opTypeDeviceEvent(uint32_t result_id) {
  539. if (!TheInst.empty()) {
  540. TheStatus = Status::NestedInst;
  541. return *this;
  542. }
  543. if (result_id == 0) {
  544. TheStatus = Status::ZeroResultId;
  545. return *this;
  546. }
  547. TheInst.reserve(2);
  548. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpTypeDeviceEvent));
  549. TheInst.emplace_back(result_id);
  550. return *this;
  551. }
  552. InstBuilder &InstBuilder::opTypeReserveId(uint32_t result_id) {
  553. if (!TheInst.empty()) {
  554. TheStatus = Status::NestedInst;
  555. return *this;
  556. }
  557. if (result_id == 0) {
  558. TheStatus = Status::ZeroResultId;
  559. return *this;
  560. }
  561. TheInst.reserve(2);
  562. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpTypeReserveId));
  563. TheInst.emplace_back(result_id);
  564. return *this;
  565. }
  566. InstBuilder &InstBuilder::opTypeQueue(uint32_t result_id) {
  567. if (!TheInst.empty()) {
  568. TheStatus = Status::NestedInst;
  569. return *this;
  570. }
  571. if (result_id == 0) {
  572. TheStatus = Status::ZeroResultId;
  573. return *this;
  574. }
  575. TheInst.reserve(2);
  576. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpTypeQueue));
  577. TheInst.emplace_back(result_id);
  578. return *this;
  579. }
  580. InstBuilder &InstBuilder::opTypePipe(uint32_t result_id,
  581. spv::AccessQualifier qualifier) {
  582. if (!TheInst.empty()) {
  583. TheStatus = Status::NestedInst;
  584. return *this;
  585. }
  586. if (result_id == 0) {
  587. TheStatus = Status::ZeroResultId;
  588. return *this;
  589. }
  590. TheInst.reserve(3);
  591. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpTypePipe));
  592. TheInst.emplace_back(result_id);
  593. TheInst.emplace_back(static_cast<uint32_t>(qualifier));
  594. return *this;
  595. }
  596. InstBuilder &
  597. InstBuilder::opTypeForwardPointer(uint32_t pointer_type,
  598. spv::StorageClass storage_class) {
  599. if (!TheInst.empty()) {
  600. TheStatus = Status::NestedInst;
  601. return *this;
  602. }
  603. TheInst.reserve(3);
  604. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpTypeForwardPointer));
  605. TheInst.emplace_back(pointer_type);
  606. TheInst.emplace_back(static_cast<uint32_t>(storage_class));
  607. return *this;
  608. }
  609. InstBuilder &InstBuilder::opConstantTrue(uint32_t result_type,
  610. uint32_t result_id) {
  611. if (!TheInst.empty()) {
  612. TheStatus = Status::NestedInst;
  613. return *this;
  614. }
  615. if (result_type == 0) {
  616. TheStatus = Status::ZeroResultType;
  617. return *this;
  618. }
  619. if (result_id == 0) {
  620. TheStatus = Status::ZeroResultId;
  621. return *this;
  622. }
  623. TheInst.reserve(3);
  624. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpConstantTrue));
  625. TheInst.emplace_back(result_type);
  626. TheInst.emplace_back(result_id);
  627. return *this;
  628. }
  629. InstBuilder &InstBuilder::opConstantFalse(uint32_t result_type,
  630. uint32_t result_id) {
  631. if (!TheInst.empty()) {
  632. TheStatus = Status::NestedInst;
  633. return *this;
  634. }
  635. if (result_type == 0) {
  636. TheStatus = Status::ZeroResultType;
  637. return *this;
  638. }
  639. if (result_id == 0) {
  640. TheStatus = Status::ZeroResultId;
  641. return *this;
  642. }
  643. TheInst.reserve(3);
  644. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpConstantFalse));
  645. TheInst.emplace_back(result_type);
  646. TheInst.emplace_back(result_id);
  647. return *this;
  648. }
  649. InstBuilder &
  650. InstBuilder::opConstantComposite(uint32_t result_type, uint32_t result_id,
  651. llvm::ArrayRef<uint32_t> constituents) {
  652. if (!TheInst.empty()) {
  653. TheStatus = Status::NestedInst;
  654. return *this;
  655. }
  656. if (result_type == 0) {
  657. TheStatus = Status::ZeroResultType;
  658. return *this;
  659. }
  660. if (result_id == 0) {
  661. TheStatus = Status::ZeroResultId;
  662. return *this;
  663. }
  664. TheInst.reserve(4);
  665. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpConstantComposite));
  666. TheInst.emplace_back(result_type);
  667. TheInst.emplace_back(result_id);
  668. TheInst.insert(TheInst.end(), constituents.begin(), constituents.end());
  669. return *this;
  670. }
  671. InstBuilder &InstBuilder::opConstantNull(uint32_t result_type,
  672. uint32_t result_id) {
  673. if (!TheInst.empty()) {
  674. TheStatus = Status::NestedInst;
  675. return *this;
  676. }
  677. if (result_type == 0) {
  678. TheStatus = Status::ZeroResultType;
  679. return *this;
  680. }
  681. if (result_id == 0) {
  682. TheStatus = Status::ZeroResultId;
  683. return *this;
  684. }
  685. TheInst.reserve(3);
  686. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpConstantNull));
  687. TheInst.emplace_back(result_type);
  688. TheInst.emplace_back(result_id);
  689. return *this;
  690. }
  691. InstBuilder &InstBuilder::opSpecConstantTrue(uint32_t result_type,
  692. uint32_t result_id) {
  693. if (!TheInst.empty()) {
  694. TheStatus = Status::NestedInst;
  695. return *this;
  696. }
  697. if (result_type == 0) {
  698. TheStatus = Status::ZeroResultType;
  699. return *this;
  700. }
  701. if (result_id == 0) {
  702. TheStatus = Status::ZeroResultId;
  703. return *this;
  704. }
  705. TheInst.reserve(3);
  706. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpSpecConstantTrue));
  707. TheInst.emplace_back(result_type);
  708. TheInst.emplace_back(result_id);
  709. return *this;
  710. }
  711. InstBuilder &InstBuilder::opSpecConstantFalse(uint32_t result_type,
  712. uint32_t result_id) {
  713. if (!TheInst.empty()) {
  714. TheStatus = Status::NestedInst;
  715. return *this;
  716. }
  717. if (result_type == 0) {
  718. TheStatus = Status::ZeroResultType;
  719. return *this;
  720. }
  721. if (result_id == 0) {
  722. TheStatus = Status::ZeroResultId;
  723. return *this;
  724. }
  725. TheInst.reserve(3);
  726. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpSpecConstantFalse));
  727. TheInst.emplace_back(result_type);
  728. TheInst.emplace_back(result_id);
  729. return *this;
  730. }
  731. InstBuilder &
  732. InstBuilder::opSpecConstantComposite(uint32_t result_type, uint32_t result_id,
  733. llvm::ArrayRef<uint32_t> constituents) {
  734. if (!TheInst.empty()) {
  735. TheStatus = Status::NestedInst;
  736. return *this;
  737. }
  738. if (result_type == 0) {
  739. TheStatus = Status::ZeroResultType;
  740. return *this;
  741. }
  742. if (result_id == 0) {
  743. TheStatus = Status::ZeroResultId;
  744. return *this;
  745. }
  746. TheInst.reserve(4);
  747. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpSpecConstantComposite));
  748. TheInst.emplace_back(result_type);
  749. TheInst.emplace_back(result_id);
  750. TheInst.insert(TheInst.end(), constituents.begin(), constituents.end());
  751. return *this;
  752. }
  753. InstBuilder &InstBuilder::opFunction(uint32_t result_type, uint32_t result_id,
  754. spv::FunctionControlMask function_control,
  755. uint32_t function_type) {
  756. if (!TheInst.empty()) {
  757. TheStatus = Status::NestedInst;
  758. return *this;
  759. }
  760. if (result_type == 0) {
  761. TheStatus = Status::ZeroResultType;
  762. return *this;
  763. }
  764. if (result_id == 0) {
  765. TheStatus = Status::ZeroResultId;
  766. return *this;
  767. }
  768. TheInst.reserve(5);
  769. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpFunction));
  770. TheInst.emplace_back(result_type);
  771. TheInst.emplace_back(result_id);
  772. TheInst.emplace_back(static_cast<uint32_t>(function_control));
  773. TheInst.emplace_back(function_type);
  774. return *this;
  775. }
  776. InstBuilder &InstBuilder::opFunctionParameter(uint32_t result_type,
  777. uint32_t result_id) {
  778. if (!TheInst.empty()) {
  779. TheStatus = Status::NestedInst;
  780. return *this;
  781. }
  782. if (result_type == 0) {
  783. TheStatus = Status::ZeroResultType;
  784. return *this;
  785. }
  786. if (result_id == 0) {
  787. TheStatus = Status::ZeroResultId;
  788. return *this;
  789. }
  790. TheInst.reserve(3);
  791. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpFunctionParameter));
  792. TheInst.emplace_back(result_type);
  793. TheInst.emplace_back(result_id);
  794. return *this;
  795. }
  796. InstBuilder &InstBuilder::opFunctionEnd() {
  797. if (!TheInst.empty()) {
  798. TheStatus = Status::NestedInst;
  799. return *this;
  800. }
  801. TheInst.reserve(1);
  802. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpFunctionEnd));
  803. return *this;
  804. }
  805. InstBuilder &
  806. InstBuilder::opFunctionCall(uint32_t result_type, uint32_t result_id,
  807. uint32_t function,
  808. llvm::ArrayRef<uint32_t> argument_0_argument_1_) {
  809. if (!TheInst.empty()) {
  810. TheStatus = Status::NestedInst;
  811. return *this;
  812. }
  813. if (result_type == 0) {
  814. TheStatus = Status::ZeroResultType;
  815. return *this;
  816. }
  817. if (result_id == 0) {
  818. TheStatus = Status::ZeroResultId;
  819. return *this;
  820. }
  821. TheInst.reserve(5);
  822. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpFunctionCall));
  823. TheInst.emplace_back(result_type);
  824. TheInst.emplace_back(result_id);
  825. TheInst.emplace_back(function);
  826. TheInst.insert(TheInst.end(), argument_0_argument_1_.begin(),
  827. argument_0_argument_1_.end());
  828. return *this;
  829. }
  830. InstBuilder &InstBuilder::opVariable(uint32_t result_type, uint32_t result_id,
  831. spv::StorageClass storage_class,
  832. llvm::Optional<uint32_t> initializer) {
  833. if (!TheInst.empty()) {
  834. TheStatus = Status::NestedInst;
  835. return *this;
  836. }
  837. if (result_type == 0) {
  838. TheStatus = Status::ZeroResultType;
  839. return *this;
  840. }
  841. if (result_id == 0) {
  842. TheStatus = Status::ZeroResultId;
  843. return *this;
  844. }
  845. TheInst.reserve(5);
  846. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpVariable));
  847. TheInst.emplace_back(result_type);
  848. TheInst.emplace_back(result_id);
  849. TheInst.emplace_back(static_cast<uint32_t>(storage_class));
  850. if (initializer.hasValue()) {
  851. const auto &val = initializer.getValue();
  852. TheInst.emplace_back(val);
  853. }
  854. return *this;
  855. }
  856. InstBuilder &InstBuilder::opImageTexelPointer(uint32_t result_type,
  857. uint32_t result_id,
  858. uint32_t image,
  859. uint32_t coordinate,
  860. uint32_t sample) {
  861. if (!TheInst.empty()) {
  862. TheStatus = Status::NestedInst;
  863. return *this;
  864. }
  865. if (result_type == 0) {
  866. TheStatus = Status::ZeroResultType;
  867. return *this;
  868. }
  869. if (result_id == 0) {
  870. TheStatus = Status::ZeroResultId;
  871. return *this;
  872. }
  873. TheInst.reserve(6);
  874. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpImageTexelPointer));
  875. TheInst.emplace_back(result_type);
  876. TheInst.emplace_back(result_id);
  877. TheInst.emplace_back(image);
  878. TheInst.emplace_back(coordinate);
  879. TheInst.emplace_back(sample);
  880. return *this;
  881. }
  882. InstBuilder &
  883. InstBuilder::opLoad(uint32_t result_type, uint32_t result_id, uint32_t pointer,
  884. llvm::Optional<spv::MemoryAccessMask> memory_access) {
  885. if (!TheInst.empty()) {
  886. TheStatus = Status::NestedInst;
  887. return *this;
  888. }
  889. if (result_type == 0) {
  890. TheStatus = Status::ZeroResultType;
  891. return *this;
  892. }
  893. if (result_id == 0) {
  894. TheStatus = Status::ZeroResultId;
  895. return *this;
  896. }
  897. TheInst.reserve(5);
  898. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpLoad));
  899. TheInst.emplace_back(result_type);
  900. TheInst.emplace_back(result_id);
  901. TheInst.emplace_back(pointer);
  902. if (memory_access.hasValue()) {
  903. const auto &val = memory_access.getValue();
  904. encodeMemoryAccess(val);
  905. }
  906. return *this;
  907. }
  908. InstBuilder &
  909. InstBuilder::opStore(uint32_t pointer, uint32_t object,
  910. llvm::Optional<spv::MemoryAccessMask> memory_access) {
  911. if (!TheInst.empty()) {
  912. TheStatus = Status::NestedInst;
  913. return *this;
  914. }
  915. TheInst.reserve(4);
  916. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpStore));
  917. TheInst.emplace_back(pointer);
  918. TheInst.emplace_back(object);
  919. if (memory_access.hasValue()) {
  920. const auto &val = memory_access.getValue();
  921. encodeMemoryAccess(val);
  922. }
  923. return *this;
  924. }
  925. InstBuilder &InstBuilder::opAccessChain(uint32_t result_type,
  926. uint32_t result_id, uint32_t base,
  927. llvm::ArrayRef<uint32_t> indexes) {
  928. if (!TheInst.empty()) {
  929. TheStatus = Status::NestedInst;
  930. return *this;
  931. }
  932. if (result_type == 0) {
  933. TheStatus = Status::ZeroResultType;
  934. return *this;
  935. }
  936. if (result_id == 0) {
  937. TheStatus = Status::ZeroResultId;
  938. return *this;
  939. }
  940. TheInst.reserve(5);
  941. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpAccessChain));
  942. TheInst.emplace_back(result_type);
  943. TheInst.emplace_back(result_id);
  944. TheInst.emplace_back(base);
  945. TheInst.insert(TheInst.end(), indexes.begin(), indexes.end());
  946. return *this;
  947. }
  948. InstBuilder &InstBuilder::opDecorate(uint32_t target,
  949. spv::Decoration decoration) {
  950. if (!TheInst.empty()) {
  951. TheStatus = Status::NestedInst;
  952. return *this;
  953. }
  954. TheInst.reserve(3);
  955. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpDecorate));
  956. TheInst.emplace_back(target);
  957. encodeDecoration(decoration);
  958. return *this;
  959. }
  960. InstBuilder &InstBuilder::opMemberDecorate(uint32_t structure_type,
  961. uint32_t member,
  962. spv::Decoration decoration) {
  963. if (!TheInst.empty()) {
  964. TheStatus = Status::NestedInst;
  965. return *this;
  966. }
  967. TheInst.reserve(4);
  968. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpMemberDecorate));
  969. TheInst.emplace_back(structure_type);
  970. TheInst.emplace_back(member);
  971. encodeDecoration(decoration);
  972. return *this;
  973. }
  974. InstBuilder &InstBuilder::opDecorationGroup(uint32_t result_id) {
  975. if (!TheInst.empty()) {
  976. TheStatus = Status::NestedInst;
  977. return *this;
  978. }
  979. if (result_id == 0) {
  980. TheStatus = Status::ZeroResultId;
  981. return *this;
  982. }
  983. TheInst.reserve(2);
  984. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpDecorationGroup));
  985. TheInst.emplace_back(result_id);
  986. return *this;
  987. }
  988. InstBuilder &InstBuilder::opGroupDecorate(uint32_t decoration_group,
  989. llvm::ArrayRef<uint32_t> targets) {
  990. if (!TheInst.empty()) {
  991. TheStatus = Status::NestedInst;
  992. return *this;
  993. }
  994. TheInst.reserve(3);
  995. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpGroupDecorate));
  996. TheInst.emplace_back(decoration_group);
  997. TheInst.insert(TheInst.end(), targets.begin(), targets.end());
  998. return *this;
  999. }
  1000. InstBuilder &InstBuilder::opGroupMemberDecorate(
  1001. uint32_t decoration_group,
  1002. llvm::ArrayRef<std::pair<uint32_t, uint32_t>> targets) {
  1003. if (!TheInst.empty()) {
  1004. TheStatus = Status::NestedInst;
  1005. return *this;
  1006. }
  1007. TheInst.reserve(3);
  1008. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpGroupMemberDecorate));
  1009. TheInst.emplace_back(decoration_group);
  1010. for (const auto &param : targets) {
  1011. TheInst.emplace_back(param.first);
  1012. TheInst.emplace_back(param.second);
  1013. ;
  1014. }
  1015. return *this;
  1016. }
  1017. InstBuilder &InstBuilder::opVectorShuffle(uint32_t result_type,
  1018. uint32_t result_id, uint32_t vector_1,
  1019. uint32_t vector_2,
  1020. llvm::ArrayRef<uint32_t> components) {
  1021. if (!TheInst.empty()) {
  1022. TheStatus = Status::NestedInst;
  1023. return *this;
  1024. }
  1025. if (result_type == 0) {
  1026. TheStatus = Status::ZeroResultType;
  1027. return *this;
  1028. }
  1029. if (result_id == 0) {
  1030. TheStatus = Status::ZeroResultId;
  1031. return *this;
  1032. }
  1033. TheInst.reserve(6);
  1034. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpVectorShuffle));
  1035. TheInst.emplace_back(result_type);
  1036. TheInst.emplace_back(result_id);
  1037. TheInst.emplace_back(vector_1);
  1038. TheInst.emplace_back(vector_2);
  1039. for (const auto &param : components) {
  1040. TheInst.emplace_back(param);
  1041. ;
  1042. }
  1043. return *this;
  1044. }
  1045. InstBuilder &
  1046. InstBuilder::opCompositeConstruct(uint32_t result_type, uint32_t result_id,
  1047. llvm::ArrayRef<uint32_t> constituents) {
  1048. if (!TheInst.empty()) {
  1049. TheStatus = Status::NestedInst;
  1050. return *this;
  1051. }
  1052. if (result_type == 0) {
  1053. TheStatus = Status::ZeroResultType;
  1054. return *this;
  1055. }
  1056. if (result_id == 0) {
  1057. TheStatus = Status::ZeroResultId;
  1058. return *this;
  1059. }
  1060. TheInst.reserve(4);
  1061. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpCompositeConstruct));
  1062. TheInst.emplace_back(result_type);
  1063. TheInst.emplace_back(result_id);
  1064. TheInst.insert(TheInst.end(), constituents.begin(), constituents.end());
  1065. return *this;
  1066. }
  1067. InstBuilder &InstBuilder::opCompositeExtract(uint32_t result_type,
  1068. uint32_t result_id,
  1069. uint32_t composite,
  1070. llvm::ArrayRef<uint32_t> indexes) {
  1071. if (!TheInst.empty()) {
  1072. TheStatus = Status::NestedInst;
  1073. return *this;
  1074. }
  1075. if (result_type == 0) {
  1076. TheStatus = Status::ZeroResultType;
  1077. return *this;
  1078. }
  1079. if (result_id == 0) {
  1080. TheStatus = Status::ZeroResultId;
  1081. return *this;
  1082. }
  1083. TheInst.reserve(5);
  1084. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpCompositeExtract));
  1085. TheInst.emplace_back(result_type);
  1086. TheInst.emplace_back(result_id);
  1087. TheInst.emplace_back(composite);
  1088. for (const auto &param : indexes) {
  1089. TheInst.emplace_back(param);
  1090. ;
  1091. }
  1092. return *this;
  1093. }
  1094. InstBuilder &InstBuilder::opCompositeInsert(uint32_t result_type,
  1095. uint32_t result_id, uint32_t object,
  1096. uint32_t composite,
  1097. llvm::ArrayRef<uint32_t> indexes) {
  1098. if (!TheInst.empty()) {
  1099. TheStatus = Status::NestedInst;
  1100. return *this;
  1101. }
  1102. if (result_type == 0) {
  1103. TheStatus = Status::ZeroResultType;
  1104. return *this;
  1105. }
  1106. if (result_id == 0) {
  1107. TheStatus = Status::ZeroResultId;
  1108. return *this;
  1109. }
  1110. TheInst.reserve(6);
  1111. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpCompositeInsert));
  1112. TheInst.emplace_back(result_type);
  1113. TheInst.emplace_back(result_id);
  1114. TheInst.emplace_back(object);
  1115. TheInst.emplace_back(composite);
  1116. for (const auto &param : indexes) {
  1117. TheInst.emplace_back(param);
  1118. ;
  1119. }
  1120. return *this;
  1121. }
  1122. InstBuilder &InstBuilder::opTranspose(uint32_t result_type, uint32_t result_id,
  1123. uint32_t matrix) {
  1124. if (!TheInst.empty()) {
  1125. TheStatus = Status::NestedInst;
  1126. return *this;
  1127. }
  1128. if (result_type == 0) {
  1129. TheStatus = Status::ZeroResultType;
  1130. return *this;
  1131. }
  1132. if (result_id == 0) {
  1133. TheStatus = Status::ZeroResultId;
  1134. return *this;
  1135. }
  1136. TheInst.reserve(4);
  1137. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpTranspose));
  1138. TheInst.emplace_back(result_type);
  1139. TheInst.emplace_back(result_id);
  1140. TheInst.emplace_back(matrix);
  1141. return *this;
  1142. }
  1143. InstBuilder &InstBuilder::opSampledImage(uint32_t result_type,
  1144. uint32_t result_id, uint32_t image,
  1145. uint32_t sampler) {
  1146. if (!TheInst.empty()) {
  1147. TheStatus = Status::NestedInst;
  1148. return *this;
  1149. }
  1150. if (result_type == 0) {
  1151. TheStatus = Status::ZeroResultType;
  1152. return *this;
  1153. }
  1154. if (result_id == 0) {
  1155. TheStatus = Status::ZeroResultId;
  1156. return *this;
  1157. }
  1158. TheInst.reserve(5);
  1159. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpSampledImage));
  1160. TheInst.emplace_back(result_type);
  1161. TheInst.emplace_back(result_id);
  1162. TheInst.emplace_back(image);
  1163. TheInst.emplace_back(sampler);
  1164. return *this;
  1165. }
  1166. InstBuilder &InstBuilder::opImageSampleImplicitLod(
  1167. uint32_t result_type, uint32_t result_id, uint32_t sampled_image,
  1168. uint32_t coordinate,
  1169. llvm::Optional<spv::ImageOperandsMask> image_operands) {
  1170. if (!TheInst.empty()) {
  1171. TheStatus = Status::NestedInst;
  1172. return *this;
  1173. }
  1174. if (result_type == 0) {
  1175. TheStatus = Status::ZeroResultType;
  1176. return *this;
  1177. }
  1178. if (result_id == 0) {
  1179. TheStatus = Status::ZeroResultId;
  1180. return *this;
  1181. }
  1182. TheInst.reserve(6);
  1183. TheInst.emplace_back(
  1184. static_cast<uint32_t>(spv::Op::OpImageSampleImplicitLod));
  1185. TheInst.emplace_back(result_type);
  1186. TheInst.emplace_back(result_id);
  1187. TheInst.emplace_back(sampled_image);
  1188. TheInst.emplace_back(coordinate);
  1189. if (image_operands.hasValue()) {
  1190. const auto &val = image_operands.getValue();
  1191. encodeImageOperands(val);
  1192. }
  1193. return *this;
  1194. }
  1195. InstBuilder &InstBuilder::opImageSampleExplicitLod(
  1196. uint32_t result_type, uint32_t result_id, uint32_t sampled_image,
  1197. uint32_t coordinate, spv::ImageOperandsMask image_operands) {
  1198. if (!TheInst.empty()) {
  1199. TheStatus = Status::NestedInst;
  1200. return *this;
  1201. }
  1202. if (result_type == 0) {
  1203. TheStatus = Status::ZeroResultType;
  1204. return *this;
  1205. }
  1206. if (result_id == 0) {
  1207. TheStatus = Status::ZeroResultId;
  1208. return *this;
  1209. }
  1210. TheInst.reserve(6);
  1211. TheInst.emplace_back(
  1212. static_cast<uint32_t>(spv::Op::OpImageSampleExplicitLod));
  1213. TheInst.emplace_back(result_type);
  1214. TheInst.emplace_back(result_id);
  1215. TheInst.emplace_back(sampled_image);
  1216. TheInst.emplace_back(coordinate);
  1217. encodeImageOperands(image_operands);
  1218. return *this;
  1219. }
  1220. InstBuilder &InstBuilder::opImageSampleDrefImplicitLod(
  1221. uint32_t result_type, uint32_t result_id, uint32_t sampled_image,
  1222. uint32_t coordinate, uint32_t dref,
  1223. llvm::Optional<spv::ImageOperandsMask> image_operands) {
  1224. if (!TheInst.empty()) {
  1225. TheStatus = Status::NestedInst;
  1226. return *this;
  1227. }
  1228. if (result_type == 0) {
  1229. TheStatus = Status::ZeroResultType;
  1230. return *this;
  1231. }
  1232. if (result_id == 0) {
  1233. TheStatus = Status::ZeroResultId;
  1234. return *this;
  1235. }
  1236. TheInst.reserve(7);
  1237. TheInst.emplace_back(
  1238. static_cast<uint32_t>(spv::Op::OpImageSampleDrefImplicitLod));
  1239. TheInst.emplace_back(result_type);
  1240. TheInst.emplace_back(result_id);
  1241. TheInst.emplace_back(sampled_image);
  1242. TheInst.emplace_back(coordinate);
  1243. TheInst.emplace_back(dref);
  1244. if (image_operands.hasValue()) {
  1245. const auto &val = image_operands.getValue();
  1246. encodeImageOperands(val);
  1247. }
  1248. return *this;
  1249. }
  1250. InstBuilder &InstBuilder::opImageSampleDrefExplicitLod(
  1251. uint32_t result_type, uint32_t result_id, uint32_t sampled_image,
  1252. uint32_t coordinate, uint32_t dref, spv::ImageOperandsMask image_operands) {
  1253. if (!TheInst.empty()) {
  1254. TheStatus = Status::NestedInst;
  1255. return *this;
  1256. }
  1257. if (result_type == 0) {
  1258. TheStatus = Status::ZeroResultType;
  1259. return *this;
  1260. }
  1261. if (result_id == 0) {
  1262. TheStatus = Status::ZeroResultId;
  1263. return *this;
  1264. }
  1265. TheInst.reserve(7);
  1266. TheInst.emplace_back(
  1267. static_cast<uint32_t>(spv::Op::OpImageSampleDrefExplicitLod));
  1268. TheInst.emplace_back(result_type);
  1269. TheInst.emplace_back(result_id);
  1270. TheInst.emplace_back(sampled_image);
  1271. TheInst.emplace_back(coordinate);
  1272. TheInst.emplace_back(dref);
  1273. encodeImageOperands(image_operands);
  1274. return *this;
  1275. }
  1276. InstBuilder &InstBuilder::opImageFetch(
  1277. uint32_t result_type, uint32_t result_id, uint32_t image,
  1278. uint32_t coordinate,
  1279. llvm::Optional<spv::ImageOperandsMask> image_operands) {
  1280. if (!TheInst.empty()) {
  1281. TheStatus = Status::NestedInst;
  1282. return *this;
  1283. }
  1284. if (result_type == 0) {
  1285. TheStatus = Status::ZeroResultType;
  1286. return *this;
  1287. }
  1288. if (result_id == 0) {
  1289. TheStatus = Status::ZeroResultId;
  1290. return *this;
  1291. }
  1292. TheInst.reserve(6);
  1293. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpImageFetch));
  1294. TheInst.emplace_back(result_type);
  1295. TheInst.emplace_back(result_id);
  1296. TheInst.emplace_back(image);
  1297. TheInst.emplace_back(coordinate);
  1298. if (image_operands.hasValue()) {
  1299. const auto &val = image_operands.getValue();
  1300. encodeImageOperands(val);
  1301. }
  1302. return *this;
  1303. }
  1304. InstBuilder &InstBuilder::opImageGather(
  1305. uint32_t result_type, uint32_t result_id, uint32_t sampled_image,
  1306. uint32_t coordinate, uint32_t component,
  1307. llvm::Optional<spv::ImageOperandsMask> image_operands) {
  1308. if (!TheInst.empty()) {
  1309. TheStatus = Status::NestedInst;
  1310. return *this;
  1311. }
  1312. if (result_type == 0) {
  1313. TheStatus = Status::ZeroResultType;
  1314. return *this;
  1315. }
  1316. if (result_id == 0) {
  1317. TheStatus = Status::ZeroResultId;
  1318. return *this;
  1319. }
  1320. TheInst.reserve(7);
  1321. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpImageGather));
  1322. TheInst.emplace_back(result_type);
  1323. TheInst.emplace_back(result_id);
  1324. TheInst.emplace_back(sampled_image);
  1325. TheInst.emplace_back(coordinate);
  1326. TheInst.emplace_back(component);
  1327. if (image_operands.hasValue()) {
  1328. const auto &val = image_operands.getValue();
  1329. encodeImageOperands(val);
  1330. }
  1331. return *this;
  1332. }
  1333. InstBuilder &InstBuilder::opImageDrefGather(
  1334. uint32_t result_type, uint32_t result_id, uint32_t sampled_image,
  1335. uint32_t coordinate, uint32_t dref,
  1336. llvm::Optional<spv::ImageOperandsMask> image_operands) {
  1337. if (!TheInst.empty()) {
  1338. TheStatus = Status::NestedInst;
  1339. return *this;
  1340. }
  1341. if (result_type == 0) {
  1342. TheStatus = Status::ZeroResultType;
  1343. return *this;
  1344. }
  1345. if (result_id == 0) {
  1346. TheStatus = Status::ZeroResultId;
  1347. return *this;
  1348. }
  1349. TheInst.reserve(7);
  1350. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpImageDrefGather));
  1351. TheInst.emplace_back(result_type);
  1352. TheInst.emplace_back(result_id);
  1353. TheInst.emplace_back(sampled_image);
  1354. TheInst.emplace_back(coordinate);
  1355. TheInst.emplace_back(dref);
  1356. if (image_operands.hasValue()) {
  1357. const auto &val = image_operands.getValue();
  1358. encodeImageOperands(val);
  1359. }
  1360. return *this;
  1361. }
  1362. InstBuilder &InstBuilder::opImageRead(
  1363. uint32_t result_type, uint32_t result_id, uint32_t image,
  1364. uint32_t coordinate,
  1365. llvm::Optional<spv::ImageOperandsMask> image_operands) {
  1366. if (!TheInst.empty()) {
  1367. TheStatus = Status::NestedInst;
  1368. return *this;
  1369. }
  1370. if (result_type == 0) {
  1371. TheStatus = Status::ZeroResultType;
  1372. return *this;
  1373. }
  1374. if (result_id == 0) {
  1375. TheStatus = Status::ZeroResultId;
  1376. return *this;
  1377. }
  1378. TheInst.reserve(6);
  1379. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpImageRead));
  1380. TheInst.emplace_back(result_type);
  1381. TheInst.emplace_back(result_id);
  1382. TheInst.emplace_back(image);
  1383. TheInst.emplace_back(coordinate);
  1384. if (image_operands.hasValue()) {
  1385. const auto &val = image_operands.getValue();
  1386. encodeImageOperands(val);
  1387. }
  1388. return *this;
  1389. }
  1390. InstBuilder &InstBuilder::opImageWrite(
  1391. uint32_t image, uint32_t coordinate, uint32_t texel,
  1392. llvm::Optional<spv::ImageOperandsMask> image_operands) {
  1393. if (!TheInst.empty()) {
  1394. TheStatus = Status::NestedInst;
  1395. return *this;
  1396. }
  1397. TheInst.reserve(5);
  1398. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpImageWrite));
  1399. TheInst.emplace_back(image);
  1400. TheInst.emplace_back(coordinate);
  1401. TheInst.emplace_back(texel);
  1402. if (image_operands.hasValue()) {
  1403. const auto &val = image_operands.getValue();
  1404. encodeImageOperands(val);
  1405. }
  1406. return *this;
  1407. }
  1408. InstBuilder &InstBuilder::opImageQueryFormat(uint32_t result_type,
  1409. uint32_t result_id,
  1410. uint32_t image) {
  1411. if (!TheInst.empty()) {
  1412. TheStatus = Status::NestedInst;
  1413. return *this;
  1414. }
  1415. if (result_type == 0) {
  1416. TheStatus = Status::ZeroResultType;
  1417. return *this;
  1418. }
  1419. if (result_id == 0) {
  1420. TheStatus = Status::ZeroResultId;
  1421. return *this;
  1422. }
  1423. TheInst.reserve(4);
  1424. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpImageQueryFormat));
  1425. TheInst.emplace_back(result_type);
  1426. TheInst.emplace_back(result_id);
  1427. TheInst.emplace_back(image);
  1428. return *this;
  1429. }
  1430. InstBuilder &InstBuilder::opImageQueryOrder(uint32_t result_type,
  1431. uint32_t result_id,
  1432. uint32_t image) {
  1433. if (!TheInst.empty()) {
  1434. TheStatus = Status::NestedInst;
  1435. return *this;
  1436. }
  1437. if (result_type == 0) {
  1438. TheStatus = Status::ZeroResultType;
  1439. return *this;
  1440. }
  1441. if (result_id == 0) {
  1442. TheStatus = Status::ZeroResultId;
  1443. return *this;
  1444. }
  1445. TheInst.reserve(4);
  1446. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpImageQueryOrder));
  1447. TheInst.emplace_back(result_type);
  1448. TheInst.emplace_back(result_id);
  1449. TheInst.emplace_back(image);
  1450. return *this;
  1451. }
  1452. InstBuilder &InstBuilder::opImageQuerySizeLod(uint32_t result_type,
  1453. uint32_t result_id,
  1454. uint32_t image,
  1455. uint32_t level_of_detail) {
  1456. if (!TheInst.empty()) {
  1457. TheStatus = Status::NestedInst;
  1458. return *this;
  1459. }
  1460. if (result_type == 0) {
  1461. TheStatus = Status::ZeroResultType;
  1462. return *this;
  1463. }
  1464. if (result_id == 0) {
  1465. TheStatus = Status::ZeroResultId;
  1466. return *this;
  1467. }
  1468. TheInst.reserve(5);
  1469. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpImageQuerySizeLod));
  1470. TheInst.emplace_back(result_type);
  1471. TheInst.emplace_back(result_id);
  1472. TheInst.emplace_back(image);
  1473. TheInst.emplace_back(level_of_detail);
  1474. return *this;
  1475. }
  1476. InstBuilder &InstBuilder::opImageQuerySize(uint32_t result_type,
  1477. uint32_t result_id, uint32_t image) {
  1478. if (!TheInst.empty()) {
  1479. TheStatus = Status::NestedInst;
  1480. return *this;
  1481. }
  1482. if (result_type == 0) {
  1483. TheStatus = Status::ZeroResultType;
  1484. return *this;
  1485. }
  1486. if (result_id == 0) {
  1487. TheStatus = Status::ZeroResultId;
  1488. return *this;
  1489. }
  1490. TheInst.reserve(4);
  1491. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpImageQuerySize));
  1492. TheInst.emplace_back(result_type);
  1493. TheInst.emplace_back(result_id);
  1494. TheInst.emplace_back(image);
  1495. return *this;
  1496. }
  1497. InstBuilder &InstBuilder::opImageQueryLod(uint32_t result_type,
  1498. uint32_t result_id,
  1499. uint32_t sampled_image,
  1500. uint32_t coordinate) {
  1501. if (!TheInst.empty()) {
  1502. TheStatus = Status::NestedInst;
  1503. return *this;
  1504. }
  1505. if (result_type == 0) {
  1506. TheStatus = Status::ZeroResultType;
  1507. return *this;
  1508. }
  1509. if (result_id == 0) {
  1510. TheStatus = Status::ZeroResultId;
  1511. return *this;
  1512. }
  1513. TheInst.reserve(5);
  1514. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpImageQueryLod));
  1515. TheInst.emplace_back(result_type);
  1516. TheInst.emplace_back(result_id);
  1517. TheInst.emplace_back(sampled_image);
  1518. TheInst.emplace_back(coordinate);
  1519. return *this;
  1520. }
  1521. InstBuilder &InstBuilder::opImageQueryLevels(uint32_t result_type,
  1522. uint32_t result_id,
  1523. uint32_t image) {
  1524. if (!TheInst.empty()) {
  1525. TheStatus = Status::NestedInst;
  1526. return *this;
  1527. }
  1528. if (result_type == 0) {
  1529. TheStatus = Status::ZeroResultType;
  1530. return *this;
  1531. }
  1532. if (result_id == 0) {
  1533. TheStatus = Status::ZeroResultId;
  1534. return *this;
  1535. }
  1536. TheInst.reserve(4);
  1537. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpImageQueryLevels));
  1538. TheInst.emplace_back(result_type);
  1539. TheInst.emplace_back(result_id);
  1540. TheInst.emplace_back(image);
  1541. return *this;
  1542. }
  1543. InstBuilder &InstBuilder::opImageQuerySamples(uint32_t result_type,
  1544. uint32_t result_id,
  1545. uint32_t image) {
  1546. if (!TheInst.empty()) {
  1547. TheStatus = Status::NestedInst;
  1548. return *this;
  1549. }
  1550. if (result_type == 0) {
  1551. TheStatus = Status::ZeroResultType;
  1552. return *this;
  1553. }
  1554. if (result_id == 0) {
  1555. TheStatus = Status::ZeroResultId;
  1556. return *this;
  1557. }
  1558. TheInst.reserve(4);
  1559. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpImageQuerySamples));
  1560. TheInst.emplace_back(result_type);
  1561. TheInst.emplace_back(result_id);
  1562. TheInst.emplace_back(image);
  1563. return *this;
  1564. }
  1565. InstBuilder &InstBuilder::opSelect(uint32_t result_type, uint32_t result_id,
  1566. uint32_t condition, uint32_t object_1,
  1567. uint32_t object_2) {
  1568. if (!TheInst.empty()) {
  1569. TheStatus = Status::NestedInst;
  1570. return *this;
  1571. }
  1572. if (result_type == 0) {
  1573. TheStatus = Status::ZeroResultType;
  1574. return *this;
  1575. }
  1576. if (result_id == 0) {
  1577. TheStatus = Status::ZeroResultId;
  1578. return *this;
  1579. }
  1580. TheInst.reserve(6);
  1581. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpSelect));
  1582. TheInst.emplace_back(result_type);
  1583. TheInst.emplace_back(result_id);
  1584. TheInst.emplace_back(condition);
  1585. TheInst.emplace_back(object_1);
  1586. TheInst.emplace_back(object_2);
  1587. return *this;
  1588. }
  1589. InstBuilder &InstBuilder::opBitFieldInsert(uint32_t result_type,
  1590. uint32_t result_id, uint32_t base,
  1591. uint32_t insert, uint32_t offset,
  1592. uint32_t count) {
  1593. if (!TheInst.empty()) {
  1594. TheStatus = Status::NestedInst;
  1595. return *this;
  1596. }
  1597. if (result_type == 0) {
  1598. TheStatus = Status::ZeroResultType;
  1599. return *this;
  1600. }
  1601. if (result_id == 0) {
  1602. TheStatus = Status::ZeroResultId;
  1603. return *this;
  1604. }
  1605. TheInst.reserve(7);
  1606. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpBitFieldInsert));
  1607. TheInst.emplace_back(result_type);
  1608. TheInst.emplace_back(result_id);
  1609. TheInst.emplace_back(base);
  1610. TheInst.emplace_back(insert);
  1611. TheInst.emplace_back(offset);
  1612. TheInst.emplace_back(count);
  1613. return *this;
  1614. }
  1615. InstBuilder &InstBuilder::opBitFieldSExtract(uint32_t result_type,
  1616. uint32_t result_id, uint32_t base,
  1617. uint32_t offset, uint32_t count) {
  1618. if (!TheInst.empty()) {
  1619. TheStatus = Status::NestedInst;
  1620. return *this;
  1621. }
  1622. if (result_type == 0) {
  1623. TheStatus = Status::ZeroResultType;
  1624. return *this;
  1625. }
  1626. if (result_id == 0) {
  1627. TheStatus = Status::ZeroResultId;
  1628. return *this;
  1629. }
  1630. TheInst.reserve(6);
  1631. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpBitFieldSExtract));
  1632. TheInst.emplace_back(result_type);
  1633. TheInst.emplace_back(result_id);
  1634. TheInst.emplace_back(base);
  1635. TheInst.emplace_back(offset);
  1636. TheInst.emplace_back(count);
  1637. return *this;
  1638. }
  1639. InstBuilder &InstBuilder::opBitFieldUExtract(uint32_t result_type,
  1640. uint32_t result_id, uint32_t base,
  1641. uint32_t offset, uint32_t count) {
  1642. if (!TheInst.empty()) {
  1643. TheStatus = Status::NestedInst;
  1644. return *this;
  1645. }
  1646. if (result_type == 0) {
  1647. TheStatus = Status::ZeroResultType;
  1648. return *this;
  1649. }
  1650. if (result_id == 0) {
  1651. TheStatus = Status::ZeroResultId;
  1652. return *this;
  1653. }
  1654. TheInst.reserve(6);
  1655. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpBitFieldUExtract));
  1656. TheInst.emplace_back(result_type);
  1657. TheInst.emplace_back(result_id);
  1658. TheInst.emplace_back(base);
  1659. TheInst.emplace_back(offset);
  1660. TheInst.emplace_back(count);
  1661. return *this;
  1662. }
  1663. InstBuilder &InstBuilder::opBitReverse(uint32_t result_type, uint32_t result_id,
  1664. uint32_t base) {
  1665. if (!TheInst.empty()) {
  1666. TheStatus = Status::NestedInst;
  1667. return *this;
  1668. }
  1669. if (result_type == 0) {
  1670. TheStatus = Status::ZeroResultType;
  1671. return *this;
  1672. }
  1673. if (result_id == 0) {
  1674. TheStatus = Status::ZeroResultId;
  1675. return *this;
  1676. }
  1677. TheInst.reserve(4);
  1678. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpBitReverse));
  1679. TheInst.emplace_back(result_type);
  1680. TheInst.emplace_back(result_id);
  1681. TheInst.emplace_back(base);
  1682. return *this;
  1683. }
  1684. InstBuilder &InstBuilder::opBitCount(uint32_t result_type, uint32_t result_id,
  1685. uint32_t base) {
  1686. if (!TheInst.empty()) {
  1687. TheStatus = Status::NestedInst;
  1688. return *this;
  1689. }
  1690. if (result_type == 0) {
  1691. TheStatus = Status::ZeroResultType;
  1692. return *this;
  1693. }
  1694. if (result_id == 0) {
  1695. TheStatus = Status::ZeroResultId;
  1696. return *this;
  1697. }
  1698. TheInst.reserve(4);
  1699. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpBitCount));
  1700. TheInst.emplace_back(result_type);
  1701. TheInst.emplace_back(result_id);
  1702. TheInst.emplace_back(base);
  1703. return *this;
  1704. }
  1705. InstBuilder &InstBuilder::opDPdx(uint32_t result_type, uint32_t result_id,
  1706. uint32_t p) {
  1707. if (!TheInst.empty()) {
  1708. TheStatus = Status::NestedInst;
  1709. return *this;
  1710. }
  1711. if (result_type == 0) {
  1712. TheStatus = Status::ZeroResultType;
  1713. return *this;
  1714. }
  1715. if (result_id == 0) {
  1716. TheStatus = Status::ZeroResultId;
  1717. return *this;
  1718. }
  1719. TheInst.reserve(4);
  1720. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpDPdx));
  1721. TheInst.emplace_back(result_type);
  1722. TheInst.emplace_back(result_id);
  1723. TheInst.emplace_back(p);
  1724. return *this;
  1725. }
  1726. InstBuilder &InstBuilder::opDPdy(uint32_t result_type, uint32_t result_id,
  1727. uint32_t p) {
  1728. if (!TheInst.empty()) {
  1729. TheStatus = Status::NestedInst;
  1730. return *this;
  1731. }
  1732. if (result_type == 0) {
  1733. TheStatus = Status::ZeroResultType;
  1734. return *this;
  1735. }
  1736. if (result_id == 0) {
  1737. TheStatus = Status::ZeroResultId;
  1738. return *this;
  1739. }
  1740. TheInst.reserve(4);
  1741. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpDPdy));
  1742. TheInst.emplace_back(result_type);
  1743. TheInst.emplace_back(result_id);
  1744. TheInst.emplace_back(p);
  1745. return *this;
  1746. }
  1747. InstBuilder &InstBuilder::opFwidth(uint32_t result_type, uint32_t result_id,
  1748. uint32_t p) {
  1749. if (!TheInst.empty()) {
  1750. TheStatus = Status::NestedInst;
  1751. return *this;
  1752. }
  1753. if (result_type == 0) {
  1754. TheStatus = Status::ZeroResultType;
  1755. return *this;
  1756. }
  1757. if (result_id == 0) {
  1758. TheStatus = Status::ZeroResultId;
  1759. return *this;
  1760. }
  1761. TheInst.reserve(4);
  1762. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpFwidth));
  1763. TheInst.emplace_back(result_type);
  1764. TheInst.emplace_back(result_id);
  1765. TheInst.emplace_back(p);
  1766. return *this;
  1767. }
  1768. InstBuilder &InstBuilder::opDPdxFine(uint32_t result_type, uint32_t result_id,
  1769. uint32_t p) {
  1770. if (!TheInst.empty()) {
  1771. TheStatus = Status::NestedInst;
  1772. return *this;
  1773. }
  1774. if (result_type == 0) {
  1775. TheStatus = Status::ZeroResultType;
  1776. return *this;
  1777. }
  1778. if (result_id == 0) {
  1779. TheStatus = Status::ZeroResultId;
  1780. return *this;
  1781. }
  1782. TheInst.reserve(4);
  1783. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpDPdxFine));
  1784. TheInst.emplace_back(result_type);
  1785. TheInst.emplace_back(result_id);
  1786. TheInst.emplace_back(p);
  1787. return *this;
  1788. }
  1789. InstBuilder &InstBuilder::opDPdyFine(uint32_t result_type, uint32_t result_id,
  1790. uint32_t p) {
  1791. if (!TheInst.empty()) {
  1792. TheStatus = Status::NestedInst;
  1793. return *this;
  1794. }
  1795. if (result_type == 0) {
  1796. TheStatus = Status::ZeroResultType;
  1797. return *this;
  1798. }
  1799. if (result_id == 0) {
  1800. TheStatus = Status::ZeroResultId;
  1801. return *this;
  1802. }
  1803. TheInst.reserve(4);
  1804. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpDPdyFine));
  1805. TheInst.emplace_back(result_type);
  1806. TheInst.emplace_back(result_id);
  1807. TheInst.emplace_back(p);
  1808. return *this;
  1809. }
  1810. InstBuilder &InstBuilder::opFwidthFine(uint32_t result_type, uint32_t result_id,
  1811. uint32_t p) {
  1812. if (!TheInst.empty()) {
  1813. TheStatus = Status::NestedInst;
  1814. return *this;
  1815. }
  1816. if (result_type == 0) {
  1817. TheStatus = Status::ZeroResultType;
  1818. return *this;
  1819. }
  1820. if (result_id == 0) {
  1821. TheStatus = Status::ZeroResultId;
  1822. return *this;
  1823. }
  1824. TheInst.reserve(4);
  1825. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpFwidthFine));
  1826. TheInst.emplace_back(result_type);
  1827. TheInst.emplace_back(result_id);
  1828. TheInst.emplace_back(p);
  1829. return *this;
  1830. }
  1831. InstBuilder &InstBuilder::opDPdxCoarse(uint32_t result_type, uint32_t result_id,
  1832. uint32_t p) {
  1833. if (!TheInst.empty()) {
  1834. TheStatus = Status::NestedInst;
  1835. return *this;
  1836. }
  1837. if (result_type == 0) {
  1838. TheStatus = Status::ZeroResultType;
  1839. return *this;
  1840. }
  1841. if (result_id == 0) {
  1842. TheStatus = Status::ZeroResultId;
  1843. return *this;
  1844. }
  1845. TheInst.reserve(4);
  1846. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpDPdxCoarse));
  1847. TheInst.emplace_back(result_type);
  1848. TheInst.emplace_back(result_id);
  1849. TheInst.emplace_back(p);
  1850. return *this;
  1851. }
  1852. InstBuilder &InstBuilder::opDPdyCoarse(uint32_t result_type, uint32_t result_id,
  1853. uint32_t p) {
  1854. if (!TheInst.empty()) {
  1855. TheStatus = Status::NestedInst;
  1856. return *this;
  1857. }
  1858. if (result_type == 0) {
  1859. TheStatus = Status::ZeroResultType;
  1860. return *this;
  1861. }
  1862. if (result_id == 0) {
  1863. TheStatus = Status::ZeroResultId;
  1864. return *this;
  1865. }
  1866. TheInst.reserve(4);
  1867. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpDPdyCoarse));
  1868. TheInst.emplace_back(result_type);
  1869. TheInst.emplace_back(result_id);
  1870. TheInst.emplace_back(p);
  1871. return *this;
  1872. }
  1873. InstBuilder &InstBuilder::opFwidthCoarse(uint32_t result_type,
  1874. uint32_t result_id, uint32_t p) {
  1875. if (!TheInst.empty()) {
  1876. TheStatus = Status::NestedInst;
  1877. return *this;
  1878. }
  1879. if (result_type == 0) {
  1880. TheStatus = Status::ZeroResultType;
  1881. return *this;
  1882. }
  1883. if (result_id == 0) {
  1884. TheStatus = Status::ZeroResultId;
  1885. return *this;
  1886. }
  1887. TheInst.reserve(4);
  1888. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpFwidthCoarse));
  1889. TheInst.emplace_back(result_type);
  1890. TheInst.emplace_back(result_id);
  1891. TheInst.emplace_back(p);
  1892. return *this;
  1893. }
  1894. InstBuilder &InstBuilder::opEmitVertex() {
  1895. if (!TheInst.empty()) {
  1896. TheStatus = Status::NestedInst;
  1897. return *this;
  1898. }
  1899. TheInst.reserve(1);
  1900. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpEmitVertex));
  1901. return *this;
  1902. }
  1903. InstBuilder &InstBuilder::opEndPrimitive() {
  1904. if (!TheInst.empty()) {
  1905. TheStatus = Status::NestedInst;
  1906. return *this;
  1907. }
  1908. TheInst.reserve(1);
  1909. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpEndPrimitive));
  1910. return *this;
  1911. }
  1912. InstBuilder &InstBuilder::opControlBarrier(uint32_t execution, uint32_t memory,
  1913. uint32_t semantics) {
  1914. if (!TheInst.empty()) {
  1915. TheStatus = Status::NestedInst;
  1916. return *this;
  1917. }
  1918. TheInst.reserve(4);
  1919. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpControlBarrier));
  1920. TheInst.emplace_back(execution);
  1921. TheInst.emplace_back(memory);
  1922. TheInst.emplace_back(semantics);
  1923. return *this;
  1924. }
  1925. InstBuilder &InstBuilder::opMemoryBarrier(uint32_t memory, uint32_t semantics) {
  1926. if (!TheInst.empty()) {
  1927. TheStatus = Status::NestedInst;
  1928. return *this;
  1929. }
  1930. TheInst.reserve(3);
  1931. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpMemoryBarrier));
  1932. TheInst.emplace_back(memory);
  1933. TheInst.emplace_back(semantics);
  1934. return *this;
  1935. }
  1936. InstBuilder &InstBuilder::opAtomicExchange(uint32_t result_type,
  1937. uint32_t result_id, uint32_t pointer,
  1938. uint32_t scope, uint32_t semantics,
  1939. uint32_t value) {
  1940. if (!TheInst.empty()) {
  1941. TheStatus = Status::NestedInst;
  1942. return *this;
  1943. }
  1944. if (result_type == 0) {
  1945. TheStatus = Status::ZeroResultType;
  1946. return *this;
  1947. }
  1948. if (result_id == 0) {
  1949. TheStatus = Status::ZeroResultId;
  1950. return *this;
  1951. }
  1952. TheInst.reserve(7);
  1953. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpAtomicExchange));
  1954. TheInst.emplace_back(result_type);
  1955. TheInst.emplace_back(result_id);
  1956. TheInst.emplace_back(pointer);
  1957. TheInst.emplace_back(scope);
  1958. TheInst.emplace_back(semantics);
  1959. TheInst.emplace_back(value);
  1960. return *this;
  1961. }
  1962. InstBuilder &InstBuilder::opAtomicCompareExchange(
  1963. uint32_t result_type, uint32_t result_id, uint32_t pointer, uint32_t scope,
  1964. uint32_t equal, uint32_t unequal, uint32_t value, uint32_t comparator) {
  1965. if (!TheInst.empty()) {
  1966. TheStatus = Status::NestedInst;
  1967. return *this;
  1968. }
  1969. if (result_type == 0) {
  1970. TheStatus = Status::ZeroResultType;
  1971. return *this;
  1972. }
  1973. if (result_id == 0) {
  1974. TheStatus = Status::ZeroResultId;
  1975. return *this;
  1976. }
  1977. TheInst.reserve(9);
  1978. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpAtomicCompareExchange));
  1979. TheInst.emplace_back(result_type);
  1980. TheInst.emplace_back(result_id);
  1981. TheInst.emplace_back(pointer);
  1982. TheInst.emplace_back(scope);
  1983. TheInst.emplace_back(equal);
  1984. TheInst.emplace_back(unequal);
  1985. TheInst.emplace_back(value);
  1986. TheInst.emplace_back(comparator);
  1987. return *this;
  1988. }
  1989. InstBuilder &InstBuilder::opLoopMerge(uint32_t merge_block,
  1990. uint32_t continue_target,
  1991. spv::LoopControlMask loop_control) {
  1992. if (!TheInst.empty()) {
  1993. TheStatus = Status::NestedInst;
  1994. return *this;
  1995. }
  1996. TheInst.reserve(4);
  1997. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpLoopMerge));
  1998. TheInst.emplace_back(merge_block);
  1999. TheInst.emplace_back(continue_target);
  2000. encodeLoopControl(loop_control);
  2001. return *this;
  2002. }
  2003. InstBuilder &
  2004. InstBuilder::opSelectionMerge(uint32_t merge_block,
  2005. spv::SelectionControlMask selection_control) {
  2006. if (!TheInst.empty()) {
  2007. TheStatus = Status::NestedInst;
  2008. return *this;
  2009. }
  2010. TheInst.reserve(3);
  2011. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpSelectionMerge));
  2012. TheInst.emplace_back(merge_block);
  2013. TheInst.emplace_back(static_cast<uint32_t>(selection_control));
  2014. return *this;
  2015. }
  2016. InstBuilder &InstBuilder::opLabel(uint32_t result_id) {
  2017. if (!TheInst.empty()) {
  2018. TheStatus = Status::NestedInst;
  2019. return *this;
  2020. }
  2021. if (result_id == 0) {
  2022. TheStatus = Status::ZeroResultId;
  2023. return *this;
  2024. }
  2025. TheInst.reserve(2);
  2026. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpLabel));
  2027. TheInst.emplace_back(result_id);
  2028. return *this;
  2029. }
  2030. InstBuilder &InstBuilder::opBranch(uint32_t target_label) {
  2031. if (!TheInst.empty()) {
  2032. TheStatus = Status::NestedInst;
  2033. return *this;
  2034. }
  2035. TheInst.reserve(2);
  2036. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpBranch));
  2037. TheInst.emplace_back(target_label);
  2038. return *this;
  2039. }
  2040. InstBuilder &
  2041. InstBuilder::opBranchConditional(uint32_t condition, uint32_t true_label,
  2042. uint32_t false_label,
  2043. llvm::ArrayRef<uint32_t> branch_weights) {
  2044. if (!TheInst.empty()) {
  2045. TheStatus = Status::NestedInst;
  2046. return *this;
  2047. }
  2048. TheInst.reserve(5);
  2049. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpBranchConditional));
  2050. TheInst.emplace_back(condition);
  2051. TheInst.emplace_back(true_label);
  2052. TheInst.emplace_back(false_label);
  2053. for (const auto &param : branch_weights) {
  2054. TheInst.emplace_back(param);
  2055. ;
  2056. }
  2057. return *this;
  2058. }
  2059. InstBuilder &
  2060. InstBuilder::opSwitch(uint32_t selector, uint32_t default_target,
  2061. llvm::ArrayRef<std::pair<uint32_t, uint32_t>> target) {
  2062. if (!TheInst.empty()) {
  2063. TheStatus = Status::NestedInst;
  2064. return *this;
  2065. }
  2066. TheInst.reserve(4);
  2067. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpSwitch));
  2068. TheInst.emplace_back(selector);
  2069. TheInst.emplace_back(default_target);
  2070. for (const auto &param : target) {
  2071. TheInst.emplace_back(param.first);
  2072. TheInst.emplace_back(param.second);
  2073. ;
  2074. }
  2075. return *this;
  2076. }
  2077. InstBuilder &InstBuilder::opKill() {
  2078. if (!TheInst.empty()) {
  2079. TheStatus = Status::NestedInst;
  2080. return *this;
  2081. }
  2082. TheInst.reserve(1);
  2083. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpKill));
  2084. return *this;
  2085. }
  2086. InstBuilder &InstBuilder::opReturn() {
  2087. if (!TheInst.empty()) {
  2088. TheStatus = Status::NestedInst;
  2089. return *this;
  2090. }
  2091. TheInst.reserve(1);
  2092. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpReturn));
  2093. return *this;
  2094. }
  2095. InstBuilder &InstBuilder::opReturnValue(uint32_t value) {
  2096. if (!TheInst.empty()) {
  2097. TheStatus = Status::NestedInst;
  2098. return *this;
  2099. }
  2100. TheInst.reserve(2);
  2101. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpReturnValue));
  2102. TheInst.emplace_back(value);
  2103. return *this;
  2104. }
  2105. InstBuilder &InstBuilder::opUnreachable() {
  2106. if (!TheInst.empty()) {
  2107. TheStatus = Status::NestedInst;
  2108. return *this;
  2109. }
  2110. TheInst.reserve(1);
  2111. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpUnreachable));
  2112. return *this;
  2113. }
  2114. InstBuilder &InstBuilder::opImageSparseSampleImplicitLod(
  2115. uint32_t result_type, uint32_t result_id, uint32_t sampled_image,
  2116. uint32_t coordinate,
  2117. llvm::Optional<spv::ImageOperandsMask> image_operands) {
  2118. if (!TheInst.empty()) {
  2119. TheStatus = Status::NestedInst;
  2120. return *this;
  2121. }
  2122. if (result_type == 0) {
  2123. TheStatus = Status::ZeroResultType;
  2124. return *this;
  2125. }
  2126. if (result_id == 0) {
  2127. TheStatus = Status::ZeroResultId;
  2128. return *this;
  2129. }
  2130. TheInst.reserve(6);
  2131. TheInst.emplace_back(
  2132. static_cast<uint32_t>(spv::Op::OpImageSparseSampleImplicitLod));
  2133. TheInst.emplace_back(result_type);
  2134. TheInst.emplace_back(result_id);
  2135. TheInst.emplace_back(sampled_image);
  2136. TheInst.emplace_back(coordinate);
  2137. if (image_operands.hasValue()) {
  2138. const auto &val = image_operands.getValue();
  2139. encodeImageOperands(val);
  2140. }
  2141. return *this;
  2142. }
  2143. InstBuilder &InstBuilder::opImageSparseSampleExplicitLod(
  2144. uint32_t result_type, uint32_t result_id, uint32_t sampled_image,
  2145. uint32_t coordinate, spv::ImageOperandsMask image_operands) {
  2146. if (!TheInst.empty()) {
  2147. TheStatus = Status::NestedInst;
  2148. return *this;
  2149. }
  2150. if (result_type == 0) {
  2151. TheStatus = Status::ZeroResultType;
  2152. return *this;
  2153. }
  2154. if (result_id == 0) {
  2155. TheStatus = Status::ZeroResultId;
  2156. return *this;
  2157. }
  2158. TheInst.reserve(6);
  2159. TheInst.emplace_back(
  2160. static_cast<uint32_t>(spv::Op::OpImageSparseSampleExplicitLod));
  2161. TheInst.emplace_back(result_type);
  2162. TheInst.emplace_back(result_id);
  2163. TheInst.emplace_back(sampled_image);
  2164. TheInst.emplace_back(coordinate);
  2165. encodeImageOperands(image_operands);
  2166. return *this;
  2167. }
  2168. InstBuilder &InstBuilder::opImageSparseSampleDrefImplicitLod(
  2169. uint32_t result_type, uint32_t result_id, uint32_t sampled_image,
  2170. uint32_t coordinate, uint32_t dref,
  2171. llvm::Optional<spv::ImageOperandsMask> image_operands) {
  2172. if (!TheInst.empty()) {
  2173. TheStatus = Status::NestedInst;
  2174. return *this;
  2175. }
  2176. if (result_type == 0) {
  2177. TheStatus = Status::ZeroResultType;
  2178. return *this;
  2179. }
  2180. if (result_id == 0) {
  2181. TheStatus = Status::ZeroResultId;
  2182. return *this;
  2183. }
  2184. TheInst.reserve(7);
  2185. TheInst.emplace_back(
  2186. static_cast<uint32_t>(spv::Op::OpImageSparseSampleDrefImplicitLod));
  2187. TheInst.emplace_back(result_type);
  2188. TheInst.emplace_back(result_id);
  2189. TheInst.emplace_back(sampled_image);
  2190. TheInst.emplace_back(coordinate);
  2191. TheInst.emplace_back(dref);
  2192. if (image_operands.hasValue()) {
  2193. const auto &val = image_operands.getValue();
  2194. encodeImageOperands(val);
  2195. }
  2196. return *this;
  2197. }
  2198. InstBuilder &InstBuilder::opImageSparseSampleDrefExplicitLod(
  2199. uint32_t result_type, uint32_t result_id, uint32_t sampled_image,
  2200. uint32_t coordinate, uint32_t dref, spv::ImageOperandsMask image_operands) {
  2201. if (!TheInst.empty()) {
  2202. TheStatus = Status::NestedInst;
  2203. return *this;
  2204. }
  2205. if (result_type == 0) {
  2206. TheStatus = Status::ZeroResultType;
  2207. return *this;
  2208. }
  2209. if (result_id == 0) {
  2210. TheStatus = Status::ZeroResultId;
  2211. return *this;
  2212. }
  2213. TheInst.reserve(7);
  2214. TheInst.emplace_back(
  2215. static_cast<uint32_t>(spv::Op::OpImageSparseSampleDrefExplicitLod));
  2216. TheInst.emplace_back(result_type);
  2217. TheInst.emplace_back(result_id);
  2218. TheInst.emplace_back(sampled_image);
  2219. TheInst.emplace_back(coordinate);
  2220. TheInst.emplace_back(dref);
  2221. encodeImageOperands(image_operands);
  2222. return *this;
  2223. }
  2224. InstBuilder &InstBuilder::opImageSparseFetch(
  2225. uint32_t result_type, uint32_t result_id, uint32_t image,
  2226. uint32_t coordinate,
  2227. llvm::Optional<spv::ImageOperandsMask> image_operands) {
  2228. if (!TheInst.empty()) {
  2229. TheStatus = Status::NestedInst;
  2230. return *this;
  2231. }
  2232. if (result_type == 0) {
  2233. TheStatus = Status::ZeroResultType;
  2234. return *this;
  2235. }
  2236. if (result_id == 0) {
  2237. TheStatus = Status::ZeroResultId;
  2238. return *this;
  2239. }
  2240. TheInst.reserve(6);
  2241. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpImageSparseFetch));
  2242. TheInst.emplace_back(result_type);
  2243. TheInst.emplace_back(result_id);
  2244. TheInst.emplace_back(image);
  2245. TheInst.emplace_back(coordinate);
  2246. if (image_operands.hasValue()) {
  2247. const auto &val = image_operands.getValue();
  2248. encodeImageOperands(val);
  2249. }
  2250. return *this;
  2251. }
  2252. InstBuilder &InstBuilder::opImageSparseGather(
  2253. uint32_t result_type, uint32_t result_id, uint32_t sampled_image,
  2254. uint32_t coordinate, uint32_t component,
  2255. llvm::Optional<spv::ImageOperandsMask> image_operands) {
  2256. if (!TheInst.empty()) {
  2257. TheStatus = Status::NestedInst;
  2258. return *this;
  2259. }
  2260. if (result_type == 0) {
  2261. TheStatus = Status::ZeroResultType;
  2262. return *this;
  2263. }
  2264. if (result_id == 0) {
  2265. TheStatus = Status::ZeroResultId;
  2266. return *this;
  2267. }
  2268. TheInst.reserve(7);
  2269. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpImageSparseGather));
  2270. TheInst.emplace_back(result_type);
  2271. TheInst.emplace_back(result_id);
  2272. TheInst.emplace_back(sampled_image);
  2273. TheInst.emplace_back(coordinate);
  2274. TheInst.emplace_back(component);
  2275. if (image_operands.hasValue()) {
  2276. const auto &val = image_operands.getValue();
  2277. encodeImageOperands(val);
  2278. }
  2279. return *this;
  2280. }
  2281. InstBuilder &InstBuilder::opImageSparseDrefGather(
  2282. uint32_t result_type, uint32_t result_id, uint32_t sampled_image,
  2283. uint32_t coordinate, uint32_t dref,
  2284. llvm::Optional<spv::ImageOperandsMask> image_operands) {
  2285. if (!TheInst.empty()) {
  2286. TheStatus = Status::NestedInst;
  2287. return *this;
  2288. }
  2289. if (result_type == 0) {
  2290. TheStatus = Status::ZeroResultType;
  2291. return *this;
  2292. }
  2293. if (result_id == 0) {
  2294. TheStatus = Status::ZeroResultId;
  2295. return *this;
  2296. }
  2297. TheInst.reserve(7);
  2298. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpImageSparseDrefGather));
  2299. TheInst.emplace_back(result_type);
  2300. TheInst.emplace_back(result_id);
  2301. TheInst.emplace_back(sampled_image);
  2302. TheInst.emplace_back(coordinate);
  2303. TheInst.emplace_back(dref);
  2304. if (image_operands.hasValue()) {
  2305. const auto &val = image_operands.getValue();
  2306. encodeImageOperands(val);
  2307. }
  2308. return *this;
  2309. }
  2310. InstBuilder &InstBuilder::opImageSparseTexelsResident(uint32_t result_type,
  2311. uint32_t result_id,
  2312. uint32_t resident_code) {
  2313. if (!TheInst.empty()) {
  2314. TheStatus = Status::NestedInst;
  2315. return *this;
  2316. }
  2317. if (result_type == 0) {
  2318. TheStatus = Status::ZeroResultType;
  2319. return *this;
  2320. }
  2321. if (result_id == 0) {
  2322. TheStatus = Status::ZeroResultId;
  2323. return *this;
  2324. }
  2325. TheInst.reserve(4);
  2326. TheInst.emplace_back(
  2327. static_cast<uint32_t>(spv::Op::OpImageSparseTexelsResident));
  2328. TheInst.emplace_back(result_type);
  2329. TheInst.emplace_back(result_id);
  2330. TheInst.emplace_back(resident_code);
  2331. return *this;
  2332. }
  2333. InstBuilder &InstBuilder::opImageSparseRead(
  2334. uint32_t result_type, uint32_t result_id, uint32_t image,
  2335. uint32_t coordinate,
  2336. llvm::Optional<spv::ImageOperandsMask> image_operands) {
  2337. if (!TheInst.empty()) {
  2338. TheStatus = Status::NestedInst;
  2339. return *this;
  2340. }
  2341. if (result_type == 0) {
  2342. TheStatus = Status::ZeroResultType;
  2343. return *this;
  2344. }
  2345. if (result_id == 0) {
  2346. TheStatus = Status::ZeroResultId;
  2347. return *this;
  2348. }
  2349. TheInst.reserve(6);
  2350. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpImageSparseRead));
  2351. TheInst.emplace_back(result_type);
  2352. TheInst.emplace_back(result_id);
  2353. TheInst.emplace_back(image);
  2354. TheInst.emplace_back(coordinate);
  2355. if (image_operands.hasValue()) {
  2356. const auto &val = image_operands.getValue();
  2357. encodeImageOperands(val);
  2358. }
  2359. return *this;
  2360. }
  2361. InstBuilder &InstBuilder::opModuleProcessed(std::string process) {
  2362. if (!TheInst.empty()) {
  2363. TheStatus = Status::NestedInst;
  2364. return *this;
  2365. }
  2366. TheInst.reserve(2);
  2367. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpModuleProcessed));
  2368. encodeString(process);
  2369. return *this;
  2370. }
  2371. InstBuilder &InstBuilder::opExecutionModeId(uint32_t entry_point,
  2372. spv::ExecutionMode mode) {
  2373. if (!TheInst.empty()) {
  2374. TheStatus = Status::NestedInst;
  2375. return *this;
  2376. }
  2377. TheInst.reserve(3);
  2378. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpExecutionModeId));
  2379. TheInst.emplace_back(entry_point);
  2380. encodeExecutionMode(mode);
  2381. return *this;
  2382. }
  2383. InstBuilder &InstBuilder::opDecorateId(uint32_t target,
  2384. spv::Decoration decoration) {
  2385. if (!TheInst.empty()) {
  2386. TheStatus = Status::NestedInst;
  2387. return *this;
  2388. }
  2389. TheInst.reserve(3);
  2390. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpDecorateId));
  2391. TheInst.emplace_back(target);
  2392. encodeDecoration(decoration);
  2393. return *this;
  2394. }
  2395. InstBuilder &InstBuilder::opDecorateStringGOOGLE(uint32_t target,
  2396. spv::Decoration decoration) {
  2397. if (!TheInst.empty()) {
  2398. TheStatus = Status::NestedInst;
  2399. return *this;
  2400. }
  2401. TheInst.reserve(3);
  2402. TheInst.emplace_back(static_cast<uint32_t>(spv::Op::OpDecorateStringGOOGLE));
  2403. TheInst.emplace_back(target);
  2404. encodeDecoration(decoration);
  2405. return *this;
  2406. }
  2407. InstBuilder &
  2408. InstBuilder::opMemberDecorateStringGOOGLE(uint32_t struct_type, uint32_t member,
  2409. spv::Decoration decoration) {
  2410. if (!TheInst.empty()) {
  2411. TheStatus = Status::NestedInst;
  2412. return *this;
  2413. }
  2414. TheInst.reserve(4);
  2415. TheInst.emplace_back(
  2416. static_cast<uint32_t>(spv::Op::OpMemberDecorateStringGOOGLE));
  2417. TheInst.emplace_back(struct_type);
  2418. TheInst.emplace_back(member);
  2419. encodeDecoration(decoration);
  2420. return *this;
  2421. }
  2422. void InstBuilder::encodeImageOperands(spv::ImageOperandsMask value) {
  2423. if (bitEnumContains(value, spv::ImageOperandsMask::Bias)) {
  2424. Expectation.emplace_back(OperandKind::IdRef);
  2425. }
  2426. if (bitEnumContains(value, spv::ImageOperandsMask::Lod)) {
  2427. Expectation.emplace_back(OperandKind::IdRef);
  2428. }
  2429. if (bitEnumContains(value, spv::ImageOperandsMask::Grad)) {
  2430. Expectation.emplace_back(OperandKind::IdRef);
  2431. Expectation.emplace_back(OperandKind::IdRef);
  2432. }
  2433. if (bitEnumContains(value, spv::ImageOperandsMask::ConstOffset)) {
  2434. Expectation.emplace_back(OperandKind::IdRef);
  2435. }
  2436. if (bitEnumContains(value, spv::ImageOperandsMask::Offset)) {
  2437. Expectation.emplace_back(OperandKind::IdRef);
  2438. }
  2439. if (bitEnumContains(value, spv::ImageOperandsMask::ConstOffsets)) {
  2440. Expectation.emplace_back(OperandKind::IdRef);
  2441. }
  2442. if (bitEnumContains(value, spv::ImageOperandsMask::Sample)) {
  2443. Expectation.emplace_back(OperandKind::IdRef);
  2444. }
  2445. if (bitEnumContains(value, spv::ImageOperandsMask::MinLod)) {
  2446. Expectation.emplace_back(OperandKind::IdRef);
  2447. }
  2448. TheInst.emplace_back(static_cast<uint32_t>(value));
  2449. }
  2450. void InstBuilder::encodeLoopControl(spv::LoopControlMask value) {
  2451. if (bitEnumContains(value, spv::LoopControlMask::DependencyLength)) {
  2452. Expectation.emplace_back(OperandKind::LiteralInteger);
  2453. }
  2454. TheInst.emplace_back(static_cast<uint32_t>(value));
  2455. }
  2456. void InstBuilder::encodeMemoryAccess(spv::MemoryAccessMask value) {
  2457. if (bitEnumContains(value, spv::MemoryAccessMask::Aligned)) {
  2458. Expectation.emplace_back(OperandKind::LiteralInteger);
  2459. }
  2460. TheInst.emplace_back(static_cast<uint32_t>(value));
  2461. }
  2462. void InstBuilder::encodeExecutionMode(spv::ExecutionMode value) {
  2463. switch (value) {
  2464. case spv::ExecutionMode::Invocations: {
  2465. Expectation.emplace_back(OperandKind::LiteralInteger);
  2466. } break;
  2467. case spv::ExecutionMode::LocalSize: {
  2468. Expectation.emplace_back(OperandKind::LiteralInteger);
  2469. Expectation.emplace_back(OperandKind::LiteralInteger);
  2470. Expectation.emplace_back(OperandKind::LiteralInteger);
  2471. } break;
  2472. case spv::ExecutionMode::LocalSizeHint: {
  2473. Expectation.emplace_back(OperandKind::LiteralInteger);
  2474. Expectation.emplace_back(OperandKind::LiteralInteger);
  2475. Expectation.emplace_back(OperandKind::LiteralInteger);
  2476. } break;
  2477. case spv::ExecutionMode::OutputVertices: {
  2478. Expectation.emplace_back(OperandKind::LiteralInteger);
  2479. } break;
  2480. case spv::ExecutionMode::VecTypeHint: {
  2481. Expectation.emplace_back(OperandKind::LiteralInteger);
  2482. } break;
  2483. case spv::ExecutionMode::SubgroupSize: {
  2484. Expectation.emplace_back(OperandKind::LiteralInteger);
  2485. } break;
  2486. case spv::ExecutionMode::SubgroupsPerWorkgroup: {
  2487. Expectation.emplace_back(OperandKind::LiteralInteger);
  2488. } break;
  2489. case spv::ExecutionMode::SubgroupsPerWorkgroupId: {
  2490. Expectation.emplace_back(OperandKind::IdRef);
  2491. } break;
  2492. case spv::ExecutionMode::LocalSizeId: {
  2493. Expectation.emplace_back(OperandKind::IdRef);
  2494. Expectation.emplace_back(OperandKind::IdRef);
  2495. Expectation.emplace_back(OperandKind::IdRef);
  2496. } break;
  2497. case spv::ExecutionMode::LocalSizeHintId: {
  2498. Expectation.emplace_back(OperandKind::IdRef);
  2499. } break;
  2500. default:
  2501. break;
  2502. }
  2503. TheInst.emplace_back(static_cast<uint32_t>(value));
  2504. }
  2505. void InstBuilder::encodeDecoration(spv::Decoration value) {
  2506. switch (value) {
  2507. case spv::Decoration::SpecId: {
  2508. Expectation.emplace_back(OperandKind::LiteralInteger);
  2509. } break;
  2510. case spv::Decoration::ArrayStride: {
  2511. Expectation.emplace_back(OperandKind::LiteralInteger);
  2512. } break;
  2513. case spv::Decoration::MatrixStride: {
  2514. Expectation.emplace_back(OperandKind::LiteralInteger);
  2515. } break;
  2516. case spv::Decoration::BuiltIn: {
  2517. Expectation.emplace_back(OperandKind::BuiltIn);
  2518. } break;
  2519. case spv::Decoration::Stream: {
  2520. Expectation.emplace_back(OperandKind::LiteralInteger);
  2521. } break;
  2522. case spv::Decoration::Location: {
  2523. Expectation.emplace_back(OperandKind::LiteralInteger);
  2524. } break;
  2525. case spv::Decoration::Component: {
  2526. Expectation.emplace_back(OperandKind::LiteralInteger);
  2527. } break;
  2528. case spv::Decoration::Index: {
  2529. Expectation.emplace_back(OperandKind::LiteralInteger);
  2530. } break;
  2531. case spv::Decoration::Binding: {
  2532. Expectation.emplace_back(OperandKind::LiteralInteger);
  2533. } break;
  2534. case spv::Decoration::DescriptorSet: {
  2535. Expectation.emplace_back(OperandKind::LiteralInteger);
  2536. } break;
  2537. case spv::Decoration::Offset: {
  2538. Expectation.emplace_back(OperandKind::LiteralInteger);
  2539. } break;
  2540. case spv::Decoration::XfbBuffer: {
  2541. Expectation.emplace_back(OperandKind::LiteralInteger);
  2542. } break;
  2543. case spv::Decoration::XfbStride: {
  2544. Expectation.emplace_back(OperandKind::LiteralInteger);
  2545. } break;
  2546. case spv::Decoration::FuncParamAttr: {
  2547. Expectation.emplace_back(OperandKind::FunctionParameterAttribute);
  2548. } break;
  2549. case spv::Decoration::FPRoundingMode: {
  2550. Expectation.emplace_back(OperandKind::FPRoundingMode);
  2551. } break;
  2552. case spv::Decoration::FPFastMathMode: {
  2553. Expectation.emplace_back(OperandKind::FPFastMathMode);
  2554. } break;
  2555. case spv::Decoration::LinkageAttributes: {
  2556. Expectation.emplace_back(OperandKind::LiteralString);
  2557. Expectation.emplace_back(OperandKind::LinkageType);
  2558. } break;
  2559. case spv::Decoration::InputAttachmentIndex: {
  2560. Expectation.emplace_back(OperandKind::LiteralInteger);
  2561. } break;
  2562. case spv::Decoration::Alignment: {
  2563. Expectation.emplace_back(OperandKind::LiteralInteger);
  2564. } break;
  2565. case spv::Decoration::MaxByteOffset: {
  2566. Expectation.emplace_back(OperandKind::LiteralInteger);
  2567. } break;
  2568. case spv::Decoration::AlignmentId: {
  2569. Expectation.emplace_back(OperandKind::IdRef);
  2570. } break;
  2571. case spv::Decoration::MaxByteOffsetId: {
  2572. Expectation.emplace_back(OperandKind::IdRef);
  2573. } break;
  2574. case spv::Decoration::SecondaryViewportRelativeNV: {
  2575. Expectation.emplace_back(OperandKind::LiteralInteger);
  2576. } break;
  2577. case spv::Decoration::HlslCounterBufferGOOGLE: {
  2578. Expectation.emplace_back(OperandKind::IdRef);
  2579. } break;
  2580. case spv::Decoration::HlslSemanticGOOGLE: {
  2581. Expectation.emplace_back(OperandKind::LiteralString);
  2582. } break;
  2583. default:
  2584. break;
  2585. }
  2586. TheInst.emplace_back(static_cast<uint32_t>(value));
  2587. }
  2588. InstBuilder &InstBuilder::fPFastMathMode(spv::FPFastMathModeMask value) {
  2589. if (Expectation.front() != OperandKind::FPFastMathMode) {
  2590. TheStatus = Status::ExpectFPFastMathMode;
  2591. return *this;
  2592. }
  2593. Expectation.pop_front();
  2594. TheInst.emplace_back(static_cast<uint32_t>(value));
  2595. return *this;
  2596. }
  2597. InstBuilder &InstBuilder::fPRoundingMode(spv::FPRoundingMode value) {
  2598. if (Expectation.front() != OperandKind::FPRoundingMode) {
  2599. TheStatus = Status::ExpectFPRoundingMode;
  2600. return *this;
  2601. }
  2602. Expectation.pop_front();
  2603. TheInst.emplace_back(static_cast<uint32_t>(value));
  2604. return *this;
  2605. }
  2606. InstBuilder &InstBuilder::linkageType(spv::LinkageType value) {
  2607. if (Expectation.front() != OperandKind::LinkageType) {
  2608. TheStatus = Status::ExpectLinkageType;
  2609. return *this;
  2610. }
  2611. Expectation.pop_front();
  2612. TheInst.emplace_back(static_cast<uint32_t>(value));
  2613. return *this;
  2614. }
  2615. InstBuilder &
  2616. InstBuilder::functionParameterAttribute(spv::FunctionParameterAttribute value) {
  2617. if (Expectation.front() != OperandKind::FunctionParameterAttribute) {
  2618. TheStatus = Status::ExpectFunctionParameterAttribute;
  2619. return *this;
  2620. }
  2621. Expectation.pop_front();
  2622. TheInst.emplace_back(static_cast<uint32_t>(value));
  2623. return *this;
  2624. }
  2625. InstBuilder &InstBuilder::builtIn(spv::BuiltIn value) {
  2626. if (Expectation.front() != OperandKind::BuiltIn) {
  2627. TheStatus = Status::ExpectBuiltIn;
  2628. return *this;
  2629. }
  2630. Expectation.pop_front();
  2631. TheInst.emplace_back(static_cast<uint32_t>(value));
  2632. return *this;
  2633. }
  2634. InstBuilder &InstBuilder::idRef(uint32_t value) {
  2635. if (Expectation.front() != OperandKind::IdRef) {
  2636. TheStatus = Status::ExpectIdRef;
  2637. return *this;
  2638. }
  2639. Expectation.pop_front();
  2640. TheInst.emplace_back(value);
  2641. return *this;
  2642. }
  2643. InstBuilder &InstBuilder::literalInteger(uint32_t value) {
  2644. if (Expectation.front() != OperandKind::LiteralInteger) {
  2645. TheStatus = Status::ExpectLiteralInteger;
  2646. return *this;
  2647. }
  2648. Expectation.pop_front();
  2649. TheInst.emplace_back(value);
  2650. return *this;
  2651. }
  2652. InstBuilder &InstBuilder::literalString(std::string value) {
  2653. if (Expectation.front() != OperandKind::LiteralString) {
  2654. TheStatus = Status::ExpectLiteralString;
  2655. return *this;
  2656. }
  2657. Expectation.pop_front();
  2658. encodeString(value);
  2659. return *this;
  2660. }
  2661. } // end namespace spirv
  2662. } // end namespace clang