SpvBuilder.cpp 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094
  1. //
  2. // Copyright (C) 2014-2015 LunarG, Inc.
  3. // Copyright (C) 2015-2018 Google, Inc.
  4. //
  5. // All rights reserved.
  6. //
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions
  9. // are met:
  10. //
  11. // Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. //
  14. // Redistributions in binary form must reproduce the above
  15. // copyright notice, this list of conditions and the following
  16. // disclaimer in the documentation and/or other materials provided
  17. // with the distribution.
  18. //
  19. // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
  20. // contributors may be used to endorse or promote products derived
  21. // from this software without specific prior written permission.
  22. //
  23. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  30. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  33. // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. // POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. // Helper for making SPIR-V IR. Generally, this is documented in the header
  37. // SpvBuilder.h.
  38. //
  39. #include <cassert>
  40. #include <cstdlib>
  41. #include <unordered_set>
  42. #include <algorithm>
  43. #include "SpvBuilder.h"
  44. #ifndef GLSLANG_WEB
  45. #include "hex_float.h"
  46. #endif
  47. #ifndef _WIN32
  48. #include <cstdio>
  49. #endif
  50. namespace spv {
  51. Builder::Builder(unsigned int spvVersion, unsigned int magicNumber, SpvBuildLogger* buildLogger) :
  52. spvVersion(spvVersion),
  53. source(SourceLanguageUnknown),
  54. sourceVersion(0),
  55. sourceFileStringId(NoResult),
  56. currentLine(0),
  57. currentFile(nullptr),
  58. emitOpLines(false),
  59. addressModel(AddressingModelLogical),
  60. memoryModel(MemoryModelGLSL450),
  61. builderNumber(magicNumber),
  62. buildPoint(0),
  63. uniqueId(0),
  64. entryPointFunction(0),
  65. generatingOpCodeForSpecConst(false),
  66. logger(buildLogger)
  67. {
  68. clearAccessChain();
  69. }
  70. Builder::~Builder()
  71. {
  72. }
  73. Id Builder::import(const char* name)
  74. {
  75. Instruction* import = new Instruction(getUniqueId(), NoType, OpExtInstImport);
  76. import->addStringOperand(name);
  77. module.mapInstruction(import);
  78. imports.push_back(std::unique_ptr<Instruction>(import));
  79. return import->getResultId();
  80. }
  81. // Emit instruction for non-filename-based #line directives (ie. no filename
  82. // seen yet): emit an OpLine if we've been asked to emit OpLines and the line
  83. // number has changed since the last time, and is a valid line number.
  84. void Builder::setLine(int lineNum)
  85. {
  86. if (lineNum != 0 && lineNum != currentLine) {
  87. currentLine = lineNum;
  88. if (emitOpLines)
  89. addLine(sourceFileStringId, currentLine, 0);
  90. }
  91. }
  92. // If no filename, do non-filename-based #line emit. Else do filename-based emit.
  93. // Emit OpLine if we've been asked to emit OpLines and the line number or filename
  94. // has changed since the last time, and line number is valid.
  95. void Builder::setLine(int lineNum, const char* filename)
  96. {
  97. if (filename == nullptr) {
  98. setLine(lineNum);
  99. return;
  100. }
  101. if ((lineNum != 0 && lineNum != currentLine) || currentFile == nullptr ||
  102. strncmp(filename, currentFile, strlen(currentFile) + 1) != 0) {
  103. currentLine = lineNum;
  104. currentFile = filename;
  105. if (emitOpLines) {
  106. spv::Id strId = getStringId(filename);
  107. addLine(strId, currentLine, 0);
  108. }
  109. }
  110. }
  111. void Builder::addLine(Id fileName, int lineNum, int column)
  112. {
  113. Instruction* line = new Instruction(OpLine);
  114. line->addIdOperand(fileName);
  115. line->addImmediateOperand(lineNum);
  116. line->addImmediateOperand(column);
  117. buildPoint->addInstruction(std::unique_ptr<Instruction>(line));
  118. }
  119. // For creating new groupedTypes (will return old type if the requested one was already made).
  120. Id Builder::makeVoidType()
  121. {
  122. Instruction* type;
  123. if (groupedTypes[OpTypeVoid].size() == 0) {
  124. type = new Instruction(getUniqueId(), NoType, OpTypeVoid);
  125. groupedTypes[OpTypeVoid].push_back(type);
  126. constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
  127. module.mapInstruction(type);
  128. } else
  129. type = groupedTypes[OpTypeVoid].back();
  130. return type->getResultId();
  131. }
  132. Id Builder::makeBoolType()
  133. {
  134. Instruction* type;
  135. if (groupedTypes[OpTypeBool].size() == 0) {
  136. type = new Instruction(getUniqueId(), NoType, OpTypeBool);
  137. groupedTypes[OpTypeBool].push_back(type);
  138. constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
  139. module.mapInstruction(type);
  140. } else
  141. type = groupedTypes[OpTypeBool].back();
  142. return type->getResultId();
  143. }
  144. Id Builder::makeSamplerType()
  145. {
  146. Instruction* type;
  147. if (groupedTypes[OpTypeSampler].size() == 0) {
  148. type = new Instruction(getUniqueId(), NoType, OpTypeSampler);
  149. groupedTypes[OpTypeSampler].push_back(type);
  150. constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
  151. module.mapInstruction(type);
  152. } else
  153. type = groupedTypes[OpTypeSampler].back();
  154. return type->getResultId();
  155. }
  156. Id Builder::makePointer(StorageClass storageClass, Id pointee)
  157. {
  158. // try to find it
  159. Instruction* type;
  160. for (int t = 0; t < (int)groupedTypes[OpTypePointer].size(); ++t) {
  161. type = groupedTypes[OpTypePointer][t];
  162. if (type->getImmediateOperand(0) == (unsigned)storageClass &&
  163. type->getIdOperand(1) == pointee)
  164. return type->getResultId();
  165. }
  166. // not found, make it
  167. type = new Instruction(getUniqueId(), NoType, OpTypePointer);
  168. type->addImmediateOperand(storageClass);
  169. type->addIdOperand(pointee);
  170. groupedTypes[OpTypePointer].push_back(type);
  171. constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
  172. module.mapInstruction(type);
  173. return type->getResultId();
  174. }
  175. Id Builder::makeForwardPointer(StorageClass storageClass)
  176. {
  177. // Caching/uniquifying doesn't work here, because we don't know the
  178. // pointee type and there can be multiple forward pointers of the same
  179. // storage type. Somebody higher up in the stack must keep track.
  180. Instruction* type = new Instruction(getUniqueId(), NoType, OpTypeForwardPointer);
  181. type->addImmediateOperand(storageClass);
  182. constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
  183. module.mapInstruction(type);
  184. return type->getResultId();
  185. }
  186. Id Builder::makePointerFromForwardPointer(StorageClass storageClass, Id forwardPointerType, Id pointee)
  187. {
  188. // try to find it
  189. Instruction* type;
  190. for (int t = 0; t < (int)groupedTypes[OpTypePointer].size(); ++t) {
  191. type = groupedTypes[OpTypePointer][t];
  192. if (type->getImmediateOperand(0) == (unsigned)storageClass &&
  193. type->getIdOperand(1) == pointee)
  194. return type->getResultId();
  195. }
  196. type = new Instruction(forwardPointerType, NoType, OpTypePointer);
  197. type->addImmediateOperand(storageClass);
  198. type->addIdOperand(pointee);
  199. groupedTypes[OpTypePointer].push_back(type);
  200. constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
  201. module.mapInstruction(type);
  202. return type->getResultId();
  203. }
  204. Id Builder::makeIntegerType(int width, bool hasSign)
  205. {
  206. #ifdef GLSLANG_WEB
  207. assert(width == 32);
  208. width = 32;
  209. #endif
  210. // try to find it
  211. Instruction* type;
  212. for (int t = 0; t < (int)groupedTypes[OpTypeInt].size(); ++t) {
  213. type = groupedTypes[OpTypeInt][t];
  214. if (type->getImmediateOperand(0) == (unsigned)width &&
  215. type->getImmediateOperand(1) == (hasSign ? 1u : 0u))
  216. return type->getResultId();
  217. }
  218. // not found, make it
  219. type = new Instruction(getUniqueId(), NoType, OpTypeInt);
  220. type->addImmediateOperand(width);
  221. type->addImmediateOperand(hasSign ? 1 : 0);
  222. groupedTypes[OpTypeInt].push_back(type);
  223. constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
  224. module.mapInstruction(type);
  225. // deal with capabilities
  226. switch (width) {
  227. case 8:
  228. case 16:
  229. // these are currently handled by storage-type declarations and post processing
  230. break;
  231. case 64:
  232. addCapability(CapabilityInt64);
  233. break;
  234. default:
  235. break;
  236. }
  237. return type->getResultId();
  238. }
  239. Id Builder::makeFloatType(int width)
  240. {
  241. #ifdef GLSLANG_WEB
  242. assert(width == 32);
  243. width = 32;
  244. #endif
  245. // try to find it
  246. Instruction* type;
  247. for (int t = 0; t < (int)groupedTypes[OpTypeFloat].size(); ++t) {
  248. type = groupedTypes[OpTypeFloat][t];
  249. if (type->getImmediateOperand(0) == (unsigned)width)
  250. return type->getResultId();
  251. }
  252. // not found, make it
  253. type = new Instruction(getUniqueId(), NoType, OpTypeFloat);
  254. type->addImmediateOperand(width);
  255. groupedTypes[OpTypeFloat].push_back(type);
  256. constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
  257. module.mapInstruction(type);
  258. // deal with capabilities
  259. switch (width) {
  260. case 16:
  261. // currently handled by storage-type declarations and post processing
  262. break;
  263. case 64:
  264. addCapability(CapabilityFloat64);
  265. break;
  266. default:
  267. break;
  268. }
  269. return type->getResultId();
  270. }
  271. // Make a struct without checking for duplication.
  272. // See makeStructResultType() for non-decorated structs
  273. // needed as the result of some instructions, which does
  274. // check for duplicates.
  275. Id Builder::makeStructType(const std::vector<Id>& members, const char* name)
  276. {
  277. // Don't look for previous one, because in the general case,
  278. // structs can be duplicated except for decorations.
  279. // not found, make it
  280. Instruction* type = new Instruction(getUniqueId(), NoType, OpTypeStruct);
  281. for (int op = 0; op < (int)members.size(); ++op)
  282. type->addIdOperand(members[op]);
  283. groupedTypes[OpTypeStruct].push_back(type);
  284. constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
  285. module.mapInstruction(type);
  286. addName(type->getResultId(), name);
  287. return type->getResultId();
  288. }
  289. // Make a struct for the simple results of several instructions,
  290. // checking for duplication.
  291. Id Builder::makeStructResultType(Id type0, Id type1)
  292. {
  293. // try to find it
  294. Instruction* type;
  295. for (int t = 0; t < (int)groupedTypes[OpTypeStruct].size(); ++t) {
  296. type = groupedTypes[OpTypeStruct][t];
  297. if (type->getNumOperands() != 2)
  298. continue;
  299. if (type->getIdOperand(0) != type0 ||
  300. type->getIdOperand(1) != type1)
  301. continue;
  302. return type->getResultId();
  303. }
  304. // not found, make it
  305. std::vector<spv::Id> members;
  306. members.push_back(type0);
  307. members.push_back(type1);
  308. return makeStructType(members, "ResType");
  309. }
  310. Id Builder::makeVectorType(Id component, int size)
  311. {
  312. // try to find it
  313. Instruction* type;
  314. for (int t = 0; t < (int)groupedTypes[OpTypeVector].size(); ++t) {
  315. type = groupedTypes[OpTypeVector][t];
  316. if (type->getIdOperand(0) == component &&
  317. type->getImmediateOperand(1) == (unsigned)size)
  318. return type->getResultId();
  319. }
  320. // not found, make it
  321. type = new Instruction(getUniqueId(), NoType, OpTypeVector);
  322. type->addIdOperand(component);
  323. type->addImmediateOperand(size);
  324. groupedTypes[OpTypeVector].push_back(type);
  325. constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
  326. module.mapInstruction(type);
  327. return type->getResultId();
  328. }
  329. Id Builder::makeMatrixType(Id component, int cols, int rows)
  330. {
  331. assert(cols <= maxMatrixSize && rows <= maxMatrixSize);
  332. Id column = makeVectorType(component, rows);
  333. // try to find it
  334. Instruction* type;
  335. for (int t = 0; t < (int)groupedTypes[OpTypeMatrix].size(); ++t) {
  336. type = groupedTypes[OpTypeMatrix][t];
  337. if (type->getIdOperand(0) == column &&
  338. type->getImmediateOperand(1) == (unsigned)cols)
  339. return type->getResultId();
  340. }
  341. // not found, make it
  342. type = new Instruction(getUniqueId(), NoType, OpTypeMatrix);
  343. type->addIdOperand(column);
  344. type->addImmediateOperand(cols);
  345. groupedTypes[OpTypeMatrix].push_back(type);
  346. constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
  347. module.mapInstruction(type);
  348. return type->getResultId();
  349. }
  350. Id Builder::makeCooperativeMatrixType(Id component, Id scope, Id rows, Id cols)
  351. {
  352. // try to find it
  353. Instruction* type;
  354. for (int t = 0; t < (int)groupedTypes[OpTypeCooperativeMatrixNV].size(); ++t) {
  355. type = groupedTypes[OpTypeCooperativeMatrixNV][t];
  356. if (type->getIdOperand(0) == component &&
  357. type->getIdOperand(1) == scope &&
  358. type->getIdOperand(2) == rows &&
  359. type->getIdOperand(3) == cols)
  360. return type->getResultId();
  361. }
  362. // not found, make it
  363. type = new Instruction(getUniqueId(), NoType, OpTypeCooperativeMatrixNV);
  364. type->addIdOperand(component);
  365. type->addIdOperand(scope);
  366. type->addIdOperand(rows);
  367. type->addIdOperand(cols);
  368. groupedTypes[OpTypeCooperativeMatrixNV].push_back(type);
  369. constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
  370. module.mapInstruction(type);
  371. return type->getResultId();
  372. }
  373. // TODO: performance: track arrays per stride
  374. // If a stride is supplied (non-zero) make an array.
  375. // If no stride (0), reuse previous array types.
  376. // 'size' is an Id of a constant or specialization constant of the array size
  377. Id Builder::makeArrayType(Id element, Id sizeId, int stride)
  378. {
  379. Instruction* type;
  380. if (stride == 0) {
  381. // try to find existing type
  382. for (int t = 0; t < (int)groupedTypes[OpTypeArray].size(); ++t) {
  383. type = groupedTypes[OpTypeArray][t];
  384. if (type->getIdOperand(0) == element &&
  385. type->getIdOperand(1) == sizeId)
  386. return type->getResultId();
  387. }
  388. }
  389. // not found, make it
  390. type = new Instruction(getUniqueId(), NoType, OpTypeArray);
  391. type->addIdOperand(element);
  392. type->addIdOperand(sizeId);
  393. groupedTypes[OpTypeArray].push_back(type);
  394. constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
  395. module.mapInstruction(type);
  396. return type->getResultId();
  397. }
  398. Id Builder::makeRuntimeArray(Id element)
  399. {
  400. Instruction* type = new Instruction(getUniqueId(), NoType, OpTypeRuntimeArray);
  401. type->addIdOperand(element);
  402. constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
  403. module.mapInstruction(type);
  404. return type->getResultId();
  405. }
  406. Id Builder::makeFunctionType(Id returnType, const std::vector<Id>& paramTypes)
  407. {
  408. // try to find it
  409. Instruction* type;
  410. for (int t = 0; t < (int)groupedTypes[OpTypeFunction].size(); ++t) {
  411. type = groupedTypes[OpTypeFunction][t];
  412. if (type->getIdOperand(0) != returnType || (int)paramTypes.size() != type->getNumOperands() - 1)
  413. continue;
  414. bool mismatch = false;
  415. for (int p = 0; p < (int)paramTypes.size(); ++p) {
  416. if (paramTypes[p] != type->getIdOperand(p + 1)) {
  417. mismatch = true;
  418. break;
  419. }
  420. }
  421. if (! mismatch)
  422. return type->getResultId();
  423. }
  424. // not found, make it
  425. type = new Instruction(getUniqueId(), NoType, OpTypeFunction);
  426. type->addIdOperand(returnType);
  427. for (int p = 0; p < (int)paramTypes.size(); ++p)
  428. type->addIdOperand(paramTypes[p]);
  429. groupedTypes[OpTypeFunction].push_back(type);
  430. constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
  431. module.mapInstruction(type);
  432. return type->getResultId();
  433. }
  434. Id Builder::makeImageType(Id sampledType, Dim dim, bool depth, bool arrayed, bool ms, unsigned sampled, ImageFormat format)
  435. {
  436. assert(sampled == 1 || sampled == 2);
  437. // try to find it
  438. Instruction* type;
  439. for (int t = 0; t < (int)groupedTypes[OpTypeImage].size(); ++t) {
  440. type = groupedTypes[OpTypeImage][t];
  441. if (type->getIdOperand(0) == sampledType &&
  442. type->getImmediateOperand(1) == (unsigned int)dim &&
  443. type->getImmediateOperand(2) == ( depth ? 1u : 0u) &&
  444. type->getImmediateOperand(3) == (arrayed ? 1u : 0u) &&
  445. type->getImmediateOperand(4) == ( ms ? 1u : 0u) &&
  446. type->getImmediateOperand(5) == sampled &&
  447. type->getImmediateOperand(6) == (unsigned int)format)
  448. return type->getResultId();
  449. }
  450. // not found, make it
  451. type = new Instruction(getUniqueId(), NoType, OpTypeImage);
  452. type->addIdOperand(sampledType);
  453. type->addImmediateOperand( dim);
  454. type->addImmediateOperand( depth ? 1 : 0);
  455. type->addImmediateOperand(arrayed ? 1 : 0);
  456. type->addImmediateOperand( ms ? 1 : 0);
  457. type->addImmediateOperand(sampled);
  458. type->addImmediateOperand((unsigned int)format);
  459. groupedTypes[OpTypeImage].push_back(type);
  460. constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
  461. module.mapInstruction(type);
  462. #ifndef GLSLANG_WEB
  463. // deal with capabilities
  464. switch (dim) {
  465. case DimBuffer:
  466. if (sampled == 1)
  467. addCapability(CapabilitySampledBuffer);
  468. else
  469. addCapability(CapabilityImageBuffer);
  470. break;
  471. case Dim1D:
  472. if (sampled == 1)
  473. addCapability(CapabilitySampled1D);
  474. else
  475. addCapability(CapabilityImage1D);
  476. break;
  477. case DimCube:
  478. if (arrayed) {
  479. if (sampled == 1)
  480. addCapability(CapabilitySampledCubeArray);
  481. else
  482. addCapability(CapabilityImageCubeArray);
  483. }
  484. break;
  485. case DimRect:
  486. if (sampled == 1)
  487. addCapability(CapabilitySampledRect);
  488. else
  489. addCapability(CapabilityImageRect);
  490. break;
  491. case DimSubpassData:
  492. addCapability(CapabilityInputAttachment);
  493. break;
  494. default:
  495. break;
  496. }
  497. if (ms) {
  498. if (sampled == 2) {
  499. // Images used with subpass data are not storage
  500. // images, so don't require the capability for them.
  501. if (dim != Dim::DimSubpassData)
  502. addCapability(CapabilityStorageImageMultisample);
  503. if (arrayed)
  504. addCapability(CapabilityImageMSArray);
  505. }
  506. }
  507. #endif
  508. return type->getResultId();
  509. }
  510. Id Builder::makeSampledImageType(Id imageType)
  511. {
  512. // try to find it
  513. Instruction* type;
  514. for (int t = 0; t < (int)groupedTypes[OpTypeSampledImage].size(); ++t) {
  515. type = groupedTypes[OpTypeSampledImage][t];
  516. if (type->getIdOperand(0) == imageType)
  517. return type->getResultId();
  518. }
  519. // not found, make it
  520. type = new Instruction(getUniqueId(), NoType, OpTypeSampledImage);
  521. type->addIdOperand(imageType);
  522. groupedTypes[OpTypeSampledImage].push_back(type);
  523. constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
  524. module.mapInstruction(type);
  525. return type->getResultId();
  526. }
  527. #ifndef GLSLANG_WEB
  528. Id Builder::makeAccelerationStructureNVType()
  529. {
  530. Instruction *type;
  531. if (groupedTypes[OpTypeAccelerationStructureNV].size() == 0) {
  532. type = new Instruction(getUniqueId(), NoType, OpTypeAccelerationStructureNV);
  533. groupedTypes[OpTypeAccelerationStructureNV].push_back(type);
  534. constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
  535. module.mapInstruction(type);
  536. } else {
  537. type = groupedTypes[OpTypeAccelerationStructureNV].back();
  538. }
  539. return type->getResultId();
  540. }
  541. #endif
  542. Id Builder::getDerefTypeId(Id resultId) const
  543. {
  544. Id typeId = getTypeId(resultId);
  545. assert(isPointerType(typeId));
  546. return module.getInstruction(typeId)->getIdOperand(1);
  547. }
  548. Op Builder::getMostBasicTypeClass(Id typeId) const
  549. {
  550. Instruction* instr = module.getInstruction(typeId);
  551. Op typeClass = instr->getOpCode();
  552. switch (typeClass)
  553. {
  554. case OpTypeVector:
  555. case OpTypeMatrix:
  556. case OpTypeArray:
  557. case OpTypeRuntimeArray:
  558. return getMostBasicTypeClass(instr->getIdOperand(0));
  559. case OpTypePointer:
  560. return getMostBasicTypeClass(instr->getIdOperand(1));
  561. default:
  562. return typeClass;
  563. }
  564. }
  565. int Builder::getNumTypeConstituents(Id typeId) const
  566. {
  567. Instruction* instr = module.getInstruction(typeId);
  568. switch (instr->getOpCode())
  569. {
  570. case OpTypeBool:
  571. case OpTypeInt:
  572. case OpTypeFloat:
  573. case OpTypePointer:
  574. return 1;
  575. case OpTypeVector:
  576. case OpTypeMatrix:
  577. return instr->getImmediateOperand(1);
  578. case OpTypeArray:
  579. {
  580. Id lengthId = instr->getIdOperand(1);
  581. return module.getInstruction(lengthId)->getImmediateOperand(0);
  582. }
  583. case OpTypeStruct:
  584. return instr->getNumOperands();
  585. case OpTypeCooperativeMatrixNV:
  586. // has only one constituent when used with OpCompositeConstruct.
  587. return 1;
  588. default:
  589. assert(0);
  590. return 1;
  591. }
  592. }
  593. // Return the lowest-level type of scalar that an homogeneous composite is made out of.
  594. // Typically, this is just to find out if something is made out of ints or floats.
  595. // However, it includes returning a structure, if say, it is an array of structure.
  596. Id Builder::getScalarTypeId(Id typeId) const
  597. {
  598. Instruction* instr = module.getInstruction(typeId);
  599. Op typeClass = instr->getOpCode();
  600. switch (typeClass)
  601. {
  602. case OpTypeVoid:
  603. case OpTypeBool:
  604. case OpTypeInt:
  605. case OpTypeFloat:
  606. case OpTypeStruct:
  607. return instr->getResultId();
  608. case OpTypeVector:
  609. case OpTypeMatrix:
  610. case OpTypeArray:
  611. case OpTypeRuntimeArray:
  612. case OpTypePointer:
  613. return getScalarTypeId(getContainedTypeId(typeId));
  614. default:
  615. assert(0);
  616. return NoResult;
  617. }
  618. }
  619. // Return the type of 'member' of a composite.
  620. Id Builder::getContainedTypeId(Id typeId, int member) const
  621. {
  622. Instruction* instr = module.getInstruction(typeId);
  623. Op typeClass = instr->getOpCode();
  624. switch (typeClass)
  625. {
  626. case OpTypeVector:
  627. case OpTypeMatrix:
  628. case OpTypeArray:
  629. case OpTypeRuntimeArray:
  630. case OpTypeCooperativeMatrixNV:
  631. return instr->getIdOperand(0);
  632. case OpTypePointer:
  633. return instr->getIdOperand(1);
  634. case OpTypeStruct:
  635. return instr->getIdOperand(member);
  636. default:
  637. assert(0);
  638. return NoResult;
  639. }
  640. }
  641. // Return the immediately contained type of a given composite type.
  642. Id Builder::getContainedTypeId(Id typeId) const
  643. {
  644. return getContainedTypeId(typeId, 0);
  645. }
  646. // Returns true if 'typeId' is or contains a scalar type declared with 'typeOp'
  647. // of width 'width'. The 'width' is only consumed for int and float types.
  648. // Returns false otherwise.
  649. bool Builder::containsType(Id typeId, spv::Op typeOp, unsigned int width) const
  650. {
  651. const Instruction& instr = *module.getInstruction(typeId);
  652. Op typeClass = instr.getOpCode();
  653. switch (typeClass)
  654. {
  655. case OpTypeInt:
  656. case OpTypeFloat:
  657. return typeClass == typeOp && instr.getImmediateOperand(0) == width;
  658. case OpTypeStruct:
  659. for (int m = 0; m < instr.getNumOperands(); ++m) {
  660. if (containsType(instr.getIdOperand(m), typeOp, width))
  661. return true;
  662. }
  663. return false;
  664. case OpTypePointer:
  665. return false;
  666. case OpTypeVector:
  667. case OpTypeMatrix:
  668. case OpTypeArray:
  669. case OpTypeRuntimeArray:
  670. return containsType(getContainedTypeId(typeId), typeOp, width);
  671. default:
  672. return typeClass == typeOp;
  673. }
  674. }
  675. // return true if the type is a pointer to PhysicalStorageBufferEXT or an
  676. // array of such pointers. These require restrict/aliased decorations.
  677. bool Builder::containsPhysicalStorageBufferOrArray(Id typeId) const
  678. {
  679. const Instruction& instr = *module.getInstruction(typeId);
  680. Op typeClass = instr.getOpCode();
  681. switch (typeClass)
  682. {
  683. case OpTypePointer:
  684. return getTypeStorageClass(typeId) == StorageClassPhysicalStorageBufferEXT;
  685. case OpTypeArray:
  686. return containsPhysicalStorageBufferOrArray(getContainedTypeId(typeId));
  687. default:
  688. return false;
  689. }
  690. }
  691. // See if a scalar constant of this type has already been created, so it
  692. // can be reused rather than duplicated. (Required by the specification).
  693. Id Builder::findScalarConstant(Op typeClass, Op opcode, Id typeId, unsigned value)
  694. {
  695. Instruction* constant;
  696. for (int i = 0; i < (int)groupedConstants[typeClass].size(); ++i) {
  697. constant = groupedConstants[typeClass][i];
  698. if (constant->getOpCode() == opcode &&
  699. constant->getTypeId() == typeId &&
  700. constant->getImmediateOperand(0) == value)
  701. return constant->getResultId();
  702. }
  703. return 0;
  704. }
  705. // Version of findScalarConstant (see above) for scalars that take two operands (e.g. a 'double' or 'int64').
  706. Id Builder::findScalarConstant(Op typeClass, Op opcode, Id typeId, unsigned v1, unsigned v2)
  707. {
  708. Instruction* constant;
  709. for (int i = 0; i < (int)groupedConstants[typeClass].size(); ++i) {
  710. constant = groupedConstants[typeClass][i];
  711. if (constant->getOpCode() == opcode &&
  712. constant->getTypeId() == typeId &&
  713. constant->getImmediateOperand(0) == v1 &&
  714. constant->getImmediateOperand(1) == v2)
  715. return constant->getResultId();
  716. }
  717. return 0;
  718. }
  719. // Return true if consuming 'opcode' means consuming a constant.
  720. // "constant" here means after final transform to executable code,
  721. // the value consumed will be a constant, so includes specialization.
  722. bool Builder::isConstantOpCode(Op opcode) const
  723. {
  724. switch (opcode) {
  725. case OpUndef:
  726. case OpConstantTrue:
  727. case OpConstantFalse:
  728. case OpConstant:
  729. case OpConstantComposite:
  730. case OpConstantSampler:
  731. case OpConstantNull:
  732. case OpSpecConstantTrue:
  733. case OpSpecConstantFalse:
  734. case OpSpecConstant:
  735. case OpSpecConstantComposite:
  736. case OpSpecConstantOp:
  737. return true;
  738. default:
  739. return false;
  740. }
  741. }
  742. // Return true if consuming 'opcode' means consuming a specialization constant.
  743. bool Builder::isSpecConstantOpCode(Op opcode) const
  744. {
  745. switch (opcode) {
  746. case OpSpecConstantTrue:
  747. case OpSpecConstantFalse:
  748. case OpSpecConstant:
  749. case OpSpecConstantComposite:
  750. case OpSpecConstantOp:
  751. return true;
  752. default:
  753. return false;
  754. }
  755. }
  756. Id Builder::makeBoolConstant(bool b, bool specConstant)
  757. {
  758. Id typeId = makeBoolType();
  759. Instruction* constant;
  760. Op opcode = specConstant ? (b ? OpSpecConstantTrue : OpSpecConstantFalse) : (b ? OpConstantTrue : OpConstantFalse);
  761. // See if we already made it. Applies only to regular constants, because specialization constants
  762. // must remain distinct for the purpose of applying a SpecId decoration.
  763. if (! specConstant) {
  764. Id existing = 0;
  765. for (int i = 0; i < (int)groupedConstants[OpTypeBool].size(); ++i) {
  766. constant = groupedConstants[OpTypeBool][i];
  767. if (constant->getTypeId() == typeId && constant->getOpCode() == opcode)
  768. existing = constant->getResultId();
  769. }
  770. if (existing)
  771. return existing;
  772. }
  773. // Make it
  774. Instruction* c = new Instruction(getUniqueId(), typeId, opcode);
  775. constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(c));
  776. groupedConstants[OpTypeBool].push_back(c);
  777. module.mapInstruction(c);
  778. return c->getResultId();
  779. }
  780. Id Builder::makeIntConstant(Id typeId, unsigned value, bool specConstant)
  781. {
  782. Op opcode = specConstant ? OpSpecConstant : OpConstant;
  783. // See if we already made it. Applies only to regular constants, because specialization constants
  784. // must remain distinct for the purpose of applying a SpecId decoration.
  785. if (! specConstant) {
  786. Id existing = findScalarConstant(OpTypeInt, opcode, typeId, value);
  787. if (existing)
  788. return existing;
  789. }
  790. Instruction* c = new Instruction(getUniqueId(), typeId, opcode);
  791. c->addImmediateOperand(value);
  792. constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(c));
  793. groupedConstants[OpTypeInt].push_back(c);
  794. module.mapInstruction(c);
  795. return c->getResultId();
  796. }
  797. Id Builder::makeInt64Constant(Id typeId, unsigned long long value, bool specConstant)
  798. {
  799. Op opcode = specConstant ? OpSpecConstant : OpConstant;
  800. unsigned op1 = value & 0xFFFFFFFF;
  801. unsigned op2 = value >> 32;
  802. // See if we already made it. Applies only to regular constants, because specialization constants
  803. // must remain distinct for the purpose of applying a SpecId decoration.
  804. if (! specConstant) {
  805. Id existing = findScalarConstant(OpTypeInt, opcode, typeId, op1, op2);
  806. if (existing)
  807. return existing;
  808. }
  809. Instruction* c = new Instruction(getUniqueId(), typeId, opcode);
  810. c->addImmediateOperand(op1);
  811. c->addImmediateOperand(op2);
  812. constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(c));
  813. groupedConstants[OpTypeInt].push_back(c);
  814. module.mapInstruction(c);
  815. return c->getResultId();
  816. }
  817. Id Builder::makeFloatConstant(float f, bool specConstant)
  818. {
  819. Op opcode = specConstant ? OpSpecConstant : OpConstant;
  820. Id typeId = makeFloatType(32);
  821. union { float fl; unsigned int ui; } u;
  822. u.fl = f;
  823. unsigned value = u.ui;
  824. // See if we already made it. Applies only to regular constants, because specialization constants
  825. // must remain distinct for the purpose of applying a SpecId decoration.
  826. if (! specConstant) {
  827. Id existing = findScalarConstant(OpTypeFloat, opcode, typeId, value);
  828. if (existing)
  829. return existing;
  830. }
  831. Instruction* c = new Instruction(getUniqueId(), typeId, opcode);
  832. c->addImmediateOperand(value);
  833. constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(c));
  834. groupedConstants[OpTypeFloat].push_back(c);
  835. module.mapInstruction(c);
  836. return c->getResultId();
  837. }
  838. Id Builder::makeDoubleConstant(double d, bool specConstant)
  839. {
  840. #ifdef GLSLANG_WEB
  841. assert(0);
  842. return NoResult;
  843. #else
  844. Op opcode = specConstant ? OpSpecConstant : OpConstant;
  845. Id typeId = makeFloatType(64);
  846. union { double db; unsigned long long ull; } u;
  847. u.db = d;
  848. unsigned long long value = u.ull;
  849. unsigned op1 = value & 0xFFFFFFFF;
  850. unsigned op2 = value >> 32;
  851. // See if we already made it. Applies only to regular constants, because specialization constants
  852. // must remain distinct for the purpose of applying a SpecId decoration.
  853. if (! specConstant) {
  854. Id existing = findScalarConstant(OpTypeFloat, opcode, typeId, op1, op2);
  855. if (existing)
  856. return existing;
  857. }
  858. Instruction* c = new Instruction(getUniqueId(), typeId, opcode);
  859. c->addImmediateOperand(op1);
  860. c->addImmediateOperand(op2);
  861. constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(c));
  862. groupedConstants[OpTypeFloat].push_back(c);
  863. module.mapInstruction(c);
  864. return c->getResultId();
  865. #endif
  866. }
  867. Id Builder::makeFloat16Constant(float f16, bool specConstant)
  868. {
  869. #ifdef GLSLANG_WEB
  870. assert(0);
  871. return NoResult;
  872. #else
  873. Op opcode = specConstant ? OpSpecConstant : OpConstant;
  874. Id typeId = makeFloatType(16);
  875. spvutils::HexFloat<spvutils::FloatProxy<float>> fVal(f16);
  876. spvutils::HexFloat<spvutils::FloatProxy<spvutils::Float16>> f16Val(0);
  877. fVal.castTo(f16Val, spvutils::kRoundToZero);
  878. unsigned value = f16Val.value().getAsFloat().get_value();
  879. // See if we already made it. Applies only to regular constants, because specialization constants
  880. // must remain distinct for the purpose of applying a SpecId decoration.
  881. if (!specConstant) {
  882. Id existing = findScalarConstant(OpTypeFloat, opcode, typeId, value);
  883. if (existing)
  884. return existing;
  885. }
  886. Instruction* c = new Instruction(getUniqueId(), typeId, opcode);
  887. c->addImmediateOperand(value);
  888. constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(c));
  889. groupedConstants[OpTypeFloat].push_back(c);
  890. module.mapInstruction(c);
  891. return c->getResultId();
  892. #endif
  893. }
  894. Id Builder::makeFpConstant(Id type, double d, bool specConstant)
  895. {
  896. #ifdef GLSLANG_WEB
  897. const int width = 32;
  898. assert(width == getScalarTypeWidth(type));
  899. #else
  900. const int width = getScalarTypeWidth(type);
  901. #endif
  902. assert(isFloatType(type));
  903. switch (width) {
  904. case 16:
  905. return makeFloat16Constant((float)d, specConstant);
  906. case 32:
  907. return makeFloatConstant((float)d, specConstant);
  908. case 64:
  909. return makeDoubleConstant(d, specConstant);
  910. default:
  911. break;
  912. }
  913. assert(false);
  914. return NoResult;
  915. }
  916. Id Builder::findCompositeConstant(Op typeClass, Id typeId, const std::vector<Id>& comps)
  917. {
  918. Instruction* constant = 0;
  919. bool found = false;
  920. for (int i = 0; i < (int)groupedConstants[typeClass].size(); ++i) {
  921. constant = groupedConstants[typeClass][i];
  922. if (constant->getTypeId() != typeId)
  923. continue;
  924. // same contents?
  925. bool mismatch = false;
  926. for (int op = 0; op < constant->getNumOperands(); ++op) {
  927. if (constant->getIdOperand(op) != comps[op]) {
  928. mismatch = true;
  929. break;
  930. }
  931. }
  932. if (! mismatch) {
  933. found = true;
  934. break;
  935. }
  936. }
  937. return found ? constant->getResultId() : NoResult;
  938. }
  939. Id Builder::findStructConstant(Id typeId, const std::vector<Id>& comps)
  940. {
  941. Instruction* constant = 0;
  942. bool found = false;
  943. for (int i = 0; i < (int)groupedStructConstants[typeId].size(); ++i) {
  944. constant = groupedStructConstants[typeId][i];
  945. // same contents?
  946. bool mismatch = false;
  947. for (int op = 0; op < constant->getNumOperands(); ++op) {
  948. if (constant->getIdOperand(op) != comps[op]) {
  949. mismatch = true;
  950. break;
  951. }
  952. }
  953. if (! mismatch) {
  954. found = true;
  955. break;
  956. }
  957. }
  958. return found ? constant->getResultId() : NoResult;
  959. }
  960. // Comments in header
  961. Id Builder::makeCompositeConstant(Id typeId, const std::vector<Id>& members, bool specConstant)
  962. {
  963. Op opcode = specConstant ? OpSpecConstantComposite : OpConstantComposite;
  964. assert(typeId);
  965. Op typeClass = getTypeClass(typeId);
  966. switch (typeClass) {
  967. case OpTypeVector:
  968. case OpTypeArray:
  969. case OpTypeMatrix:
  970. case OpTypeCooperativeMatrixNV:
  971. if (! specConstant) {
  972. Id existing = findCompositeConstant(typeClass, typeId, members);
  973. if (existing)
  974. return existing;
  975. }
  976. break;
  977. case OpTypeStruct:
  978. if (! specConstant) {
  979. Id existing = findStructConstant(typeId, members);
  980. if (existing)
  981. return existing;
  982. }
  983. break;
  984. default:
  985. assert(0);
  986. return makeFloatConstant(0.0);
  987. }
  988. Instruction* c = new Instruction(getUniqueId(), typeId, opcode);
  989. for (int op = 0; op < (int)members.size(); ++op)
  990. c->addIdOperand(members[op]);
  991. constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(c));
  992. if (typeClass == OpTypeStruct)
  993. groupedStructConstants[typeId].push_back(c);
  994. else
  995. groupedConstants[typeClass].push_back(c);
  996. module.mapInstruction(c);
  997. return c->getResultId();
  998. }
  999. Instruction* Builder::addEntryPoint(ExecutionModel model, Function* function, const char* name)
  1000. {
  1001. Instruction* entryPoint = new Instruction(OpEntryPoint);
  1002. entryPoint->addImmediateOperand(model);
  1003. entryPoint->addIdOperand(function->getId());
  1004. entryPoint->addStringOperand(name);
  1005. entryPoints.push_back(std::unique_ptr<Instruction>(entryPoint));
  1006. return entryPoint;
  1007. }
  1008. // Currently relying on the fact that all 'value' of interest are small non-negative values.
  1009. void Builder::addExecutionMode(Function* entryPoint, ExecutionMode mode, int value1, int value2, int value3)
  1010. {
  1011. Instruction* instr = new Instruction(OpExecutionMode);
  1012. instr->addIdOperand(entryPoint->getId());
  1013. instr->addImmediateOperand(mode);
  1014. if (value1 >= 0)
  1015. instr->addImmediateOperand(value1);
  1016. if (value2 >= 0)
  1017. instr->addImmediateOperand(value2);
  1018. if (value3 >= 0)
  1019. instr->addImmediateOperand(value3);
  1020. executionModes.push_back(std::unique_ptr<Instruction>(instr));
  1021. }
  1022. void Builder::addName(Id id, const char* string)
  1023. {
  1024. Instruction* name = new Instruction(OpName);
  1025. name->addIdOperand(id);
  1026. name->addStringOperand(string);
  1027. names.push_back(std::unique_ptr<Instruction>(name));
  1028. }
  1029. void Builder::addMemberName(Id id, int memberNumber, const char* string)
  1030. {
  1031. Instruction* name = new Instruction(OpMemberName);
  1032. name->addIdOperand(id);
  1033. name->addImmediateOperand(memberNumber);
  1034. name->addStringOperand(string);
  1035. names.push_back(std::unique_ptr<Instruction>(name));
  1036. }
  1037. void Builder::addDecoration(Id id, Decoration decoration, int num)
  1038. {
  1039. if (decoration == spv::DecorationMax)
  1040. return;
  1041. Instruction* dec = new Instruction(OpDecorate);
  1042. dec->addIdOperand(id);
  1043. dec->addImmediateOperand(decoration);
  1044. if (num >= 0)
  1045. dec->addImmediateOperand(num);
  1046. decorations.push_back(std::unique_ptr<Instruction>(dec));
  1047. }
  1048. void Builder::addDecoration(Id id, Decoration decoration, const char* s)
  1049. {
  1050. if (decoration == spv::DecorationMax)
  1051. return;
  1052. Instruction* dec = new Instruction(OpDecorateStringGOOGLE);
  1053. dec->addIdOperand(id);
  1054. dec->addImmediateOperand(decoration);
  1055. dec->addStringOperand(s);
  1056. decorations.push_back(std::unique_ptr<Instruction>(dec));
  1057. }
  1058. void Builder::addDecorationId(Id id, Decoration decoration, Id idDecoration)
  1059. {
  1060. if (decoration == spv::DecorationMax)
  1061. return;
  1062. Instruction* dec = new Instruction(OpDecorateId);
  1063. dec->addIdOperand(id);
  1064. dec->addImmediateOperand(decoration);
  1065. dec->addIdOperand(idDecoration);
  1066. decorations.push_back(std::unique_ptr<Instruction>(dec));
  1067. }
  1068. void Builder::addMemberDecoration(Id id, unsigned int member, Decoration decoration, int num)
  1069. {
  1070. if (decoration == spv::DecorationMax)
  1071. return;
  1072. Instruction* dec = new Instruction(OpMemberDecorate);
  1073. dec->addIdOperand(id);
  1074. dec->addImmediateOperand(member);
  1075. dec->addImmediateOperand(decoration);
  1076. if (num >= 0)
  1077. dec->addImmediateOperand(num);
  1078. decorations.push_back(std::unique_ptr<Instruction>(dec));
  1079. }
  1080. void Builder::addMemberDecoration(Id id, unsigned int member, Decoration decoration, const char *s)
  1081. {
  1082. if (decoration == spv::DecorationMax)
  1083. return;
  1084. Instruction* dec = new Instruction(OpMemberDecorateStringGOOGLE);
  1085. dec->addIdOperand(id);
  1086. dec->addImmediateOperand(member);
  1087. dec->addImmediateOperand(decoration);
  1088. dec->addStringOperand(s);
  1089. decorations.push_back(std::unique_ptr<Instruction>(dec));
  1090. }
  1091. // Comments in header
  1092. Function* Builder::makeEntryPoint(const char* entryPoint)
  1093. {
  1094. assert(! entryPointFunction);
  1095. Block* entry;
  1096. std::vector<Id> params;
  1097. std::vector<std::vector<Decoration>> decorations;
  1098. entryPointFunction = makeFunctionEntry(NoPrecision, makeVoidType(), entryPoint, params, decorations, &entry);
  1099. return entryPointFunction;
  1100. }
  1101. // Comments in header
  1102. Function* Builder::makeFunctionEntry(Decoration precision, Id returnType, const char* name,
  1103. const std::vector<Id>& paramTypes, const std::vector<std::vector<Decoration>>& decorations, Block **entry)
  1104. {
  1105. // Make the function and initial instructions in it
  1106. Id typeId = makeFunctionType(returnType, paramTypes);
  1107. Id firstParamId = paramTypes.size() == 0 ? 0 : getUniqueIds((int)paramTypes.size());
  1108. Function* function = new Function(getUniqueId(), returnType, typeId, firstParamId, module);
  1109. // Set up the precisions
  1110. setPrecision(function->getId(), precision);
  1111. for (unsigned p = 0; p < (unsigned)decorations.size(); ++p) {
  1112. for (int d = 0; d < (int)decorations[p].size(); ++d)
  1113. addDecoration(firstParamId + p, decorations[p][d]);
  1114. }
  1115. // CFG
  1116. if (entry) {
  1117. *entry = new Block(getUniqueId(), *function);
  1118. function->addBlock(*entry);
  1119. setBuildPoint(*entry);
  1120. }
  1121. if (name)
  1122. addName(function->getId(), name);
  1123. functions.push_back(std::unique_ptr<Function>(function));
  1124. return function;
  1125. }
  1126. // Comments in header
  1127. void Builder::makeReturn(bool implicit, Id retVal)
  1128. {
  1129. if (retVal) {
  1130. Instruction* inst = new Instruction(NoResult, NoType, OpReturnValue);
  1131. inst->addIdOperand(retVal);
  1132. buildPoint->addInstruction(std::unique_ptr<Instruction>(inst));
  1133. } else
  1134. buildPoint->addInstruction(std::unique_ptr<Instruction>(new Instruction(NoResult, NoType, OpReturn)));
  1135. if (! implicit)
  1136. createAndSetNoPredecessorBlock("post-return");
  1137. }
  1138. // Comments in header
  1139. void Builder::leaveFunction()
  1140. {
  1141. Block* block = buildPoint;
  1142. Function& function = buildPoint->getParent();
  1143. assert(block);
  1144. // If our function did not contain a return, add a return void now.
  1145. if (! block->isTerminated()) {
  1146. if (function.getReturnType() == makeVoidType())
  1147. makeReturn(true);
  1148. else {
  1149. makeReturn(true, createUndefined(function.getReturnType()));
  1150. }
  1151. }
  1152. }
  1153. // Comments in header
  1154. void Builder::makeDiscard()
  1155. {
  1156. buildPoint->addInstruction(std::unique_ptr<Instruction>(new Instruction(OpKill)));
  1157. createAndSetNoPredecessorBlock("post-discard");
  1158. }
  1159. // Comments in header
  1160. Id Builder::createVariable(StorageClass storageClass, Id type, const char* name, Id initializer)
  1161. {
  1162. Id pointerType = makePointer(storageClass, type);
  1163. Instruction* inst = new Instruction(getUniqueId(), pointerType, OpVariable);
  1164. inst->addImmediateOperand(storageClass);
  1165. if (initializer != NoResult)
  1166. inst->addIdOperand(initializer);
  1167. switch (storageClass) {
  1168. case StorageClassFunction:
  1169. // Validation rules require the declaration in the entry block
  1170. buildPoint->getParent().addLocalVariable(std::unique_ptr<Instruction>(inst));
  1171. break;
  1172. default:
  1173. constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(inst));
  1174. module.mapInstruction(inst);
  1175. break;
  1176. }
  1177. if (name)
  1178. addName(inst->getResultId(), name);
  1179. return inst->getResultId();
  1180. }
  1181. // Comments in header
  1182. Id Builder::createUndefined(Id type)
  1183. {
  1184. Instruction* inst = new Instruction(getUniqueId(), type, OpUndef);
  1185. buildPoint->addInstruction(std::unique_ptr<Instruction>(inst));
  1186. return inst->getResultId();
  1187. }
  1188. // av/vis/nonprivate are unnecessary and illegal for some storage classes.
  1189. spv::MemoryAccessMask Builder::sanitizeMemoryAccessForStorageClass(spv::MemoryAccessMask memoryAccess, StorageClass sc) const
  1190. {
  1191. switch (sc) {
  1192. case spv::StorageClassUniform:
  1193. case spv::StorageClassWorkgroup:
  1194. case spv::StorageClassStorageBuffer:
  1195. case spv::StorageClassPhysicalStorageBufferEXT:
  1196. break;
  1197. default:
  1198. memoryAccess = spv::MemoryAccessMask(memoryAccess &
  1199. ~(spv::MemoryAccessMakePointerAvailableKHRMask |
  1200. spv::MemoryAccessMakePointerVisibleKHRMask |
  1201. spv::MemoryAccessNonPrivatePointerKHRMask));
  1202. break;
  1203. }
  1204. return memoryAccess;
  1205. }
  1206. // Comments in header
  1207. void Builder::createStore(Id rValue, Id lValue, spv::MemoryAccessMask memoryAccess, spv::Scope scope, unsigned int alignment)
  1208. {
  1209. Instruction* store = new Instruction(OpStore);
  1210. store->addIdOperand(lValue);
  1211. store->addIdOperand(rValue);
  1212. memoryAccess = sanitizeMemoryAccessForStorageClass(memoryAccess, getStorageClass(lValue));
  1213. if (memoryAccess != MemoryAccessMaskNone) {
  1214. store->addImmediateOperand(memoryAccess);
  1215. if (memoryAccess & spv::MemoryAccessAlignedMask) {
  1216. store->addImmediateOperand(alignment);
  1217. }
  1218. if (memoryAccess & spv::MemoryAccessMakePointerAvailableKHRMask) {
  1219. store->addIdOperand(makeUintConstant(scope));
  1220. }
  1221. }
  1222. buildPoint->addInstruction(std::unique_ptr<Instruction>(store));
  1223. }
  1224. // Comments in header
  1225. Id Builder::createLoad(Id lValue, spv::MemoryAccessMask memoryAccess, spv::Scope scope, unsigned int alignment)
  1226. {
  1227. Instruction* load = new Instruction(getUniqueId(), getDerefTypeId(lValue), OpLoad);
  1228. load->addIdOperand(lValue);
  1229. memoryAccess = sanitizeMemoryAccessForStorageClass(memoryAccess, getStorageClass(lValue));
  1230. if (memoryAccess != MemoryAccessMaskNone) {
  1231. load->addImmediateOperand(memoryAccess);
  1232. if (memoryAccess & spv::MemoryAccessAlignedMask) {
  1233. load->addImmediateOperand(alignment);
  1234. }
  1235. if (memoryAccess & spv::MemoryAccessMakePointerVisibleKHRMask) {
  1236. load->addIdOperand(makeUintConstant(scope));
  1237. }
  1238. }
  1239. buildPoint->addInstruction(std::unique_ptr<Instruction>(load));
  1240. return load->getResultId();
  1241. }
  1242. // Comments in header
  1243. Id Builder::createAccessChain(StorageClass storageClass, Id base, const std::vector<Id>& offsets)
  1244. {
  1245. // Figure out the final resulting type.
  1246. spv::Id typeId = getTypeId(base);
  1247. assert(isPointerType(typeId) && offsets.size() > 0);
  1248. typeId = getContainedTypeId(typeId);
  1249. for (int i = 0; i < (int)offsets.size(); ++i) {
  1250. if (isStructType(typeId)) {
  1251. assert(isConstantScalar(offsets[i]));
  1252. typeId = getContainedTypeId(typeId, getConstantScalar(offsets[i]));
  1253. } else
  1254. typeId = getContainedTypeId(typeId, offsets[i]);
  1255. }
  1256. typeId = makePointer(storageClass, typeId);
  1257. // Make the instruction
  1258. Instruction* chain = new Instruction(getUniqueId(), typeId, OpAccessChain);
  1259. chain->addIdOperand(base);
  1260. for (int i = 0; i < (int)offsets.size(); ++i)
  1261. chain->addIdOperand(offsets[i]);
  1262. buildPoint->addInstruction(std::unique_ptr<Instruction>(chain));
  1263. return chain->getResultId();
  1264. }
  1265. Id Builder::createArrayLength(Id base, unsigned int member)
  1266. {
  1267. spv::Id intType = makeUintType(32);
  1268. Instruction* length = new Instruction(getUniqueId(), intType, OpArrayLength);
  1269. length->addIdOperand(base);
  1270. length->addImmediateOperand(member);
  1271. buildPoint->addInstruction(std::unique_ptr<Instruction>(length));
  1272. return length->getResultId();
  1273. }
  1274. Id Builder::createCooperativeMatrixLength(Id type)
  1275. {
  1276. spv::Id intType = makeUintType(32);
  1277. // Generate code for spec constants if in spec constant operation
  1278. // generation mode.
  1279. if (generatingOpCodeForSpecConst) {
  1280. return createSpecConstantOp(OpCooperativeMatrixLengthNV, intType, std::vector<Id>(1, type), std::vector<Id>());
  1281. }
  1282. Instruction* length = new Instruction(getUniqueId(), intType, OpCooperativeMatrixLengthNV);
  1283. length->addIdOperand(type);
  1284. buildPoint->addInstruction(std::unique_ptr<Instruction>(length));
  1285. return length->getResultId();
  1286. }
  1287. Id Builder::createCompositeExtract(Id composite, Id typeId, unsigned index)
  1288. {
  1289. // Generate code for spec constants if in spec constant operation
  1290. // generation mode.
  1291. if (generatingOpCodeForSpecConst) {
  1292. return createSpecConstantOp(OpCompositeExtract, typeId, std::vector<Id>(1, composite), std::vector<Id>(1, index));
  1293. }
  1294. Instruction* extract = new Instruction(getUniqueId(), typeId, OpCompositeExtract);
  1295. extract->addIdOperand(composite);
  1296. extract->addImmediateOperand(index);
  1297. buildPoint->addInstruction(std::unique_ptr<Instruction>(extract));
  1298. return extract->getResultId();
  1299. }
  1300. Id Builder::createCompositeExtract(Id composite, Id typeId, const std::vector<unsigned>& indexes)
  1301. {
  1302. // Generate code for spec constants if in spec constant operation
  1303. // generation mode.
  1304. if (generatingOpCodeForSpecConst) {
  1305. return createSpecConstantOp(OpCompositeExtract, typeId, std::vector<Id>(1, composite), indexes);
  1306. }
  1307. Instruction* extract = new Instruction(getUniqueId(), typeId, OpCompositeExtract);
  1308. extract->addIdOperand(composite);
  1309. for (int i = 0; i < (int)indexes.size(); ++i)
  1310. extract->addImmediateOperand(indexes[i]);
  1311. buildPoint->addInstruction(std::unique_ptr<Instruction>(extract));
  1312. return extract->getResultId();
  1313. }
  1314. Id Builder::createCompositeInsert(Id object, Id composite, Id typeId, unsigned index)
  1315. {
  1316. Instruction* insert = new Instruction(getUniqueId(), typeId, OpCompositeInsert);
  1317. insert->addIdOperand(object);
  1318. insert->addIdOperand(composite);
  1319. insert->addImmediateOperand(index);
  1320. buildPoint->addInstruction(std::unique_ptr<Instruction>(insert));
  1321. return insert->getResultId();
  1322. }
  1323. Id Builder::createCompositeInsert(Id object, Id composite, Id typeId, const std::vector<unsigned>& indexes)
  1324. {
  1325. Instruction* insert = new Instruction(getUniqueId(), typeId, OpCompositeInsert);
  1326. insert->addIdOperand(object);
  1327. insert->addIdOperand(composite);
  1328. for (int i = 0; i < (int)indexes.size(); ++i)
  1329. insert->addImmediateOperand(indexes[i]);
  1330. buildPoint->addInstruction(std::unique_ptr<Instruction>(insert));
  1331. return insert->getResultId();
  1332. }
  1333. Id Builder::createVectorExtractDynamic(Id vector, Id typeId, Id componentIndex)
  1334. {
  1335. Instruction* extract = new Instruction(getUniqueId(), typeId, OpVectorExtractDynamic);
  1336. extract->addIdOperand(vector);
  1337. extract->addIdOperand(componentIndex);
  1338. buildPoint->addInstruction(std::unique_ptr<Instruction>(extract));
  1339. return extract->getResultId();
  1340. }
  1341. Id Builder::createVectorInsertDynamic(Id vector, Id typeId, Id component, Id componentIndex)
  1342. {
  1343. Instruction* insert = new Instruction(getUniqueId(), typeId, OpVectorInsertDynamic);
  1344. insert->addIdOperand(vector);
  1345. insert->addIdOperand(component);
  1346. insert->addIdOperand(componentIndex);
  1347. buildPoint->addInstruction(std::unique_ptr<Instruction>(insert));
  1348. return insert->getResultId();
  1349. }
  1350. // An opcode that has no operands, no result id, and no type
  1351. void Builder::createNoResultOp(Op opCode)
  1352. {
  1353. Instruction* op = new Instruction(opCode);
  1354. buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
  1355. }
  1356. // An opcode that has one id operand, no result id, and no type
  1357. void Builder::createNoResultOp(Op opCode, Id operand)
  1358. {
  1359. Instruction* op = new Instruction(opCode);
  1360. op->addIdOperand(operand);
  1361. buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
  1362. }
  1363. // An opcode that has one or more operands, no result id, and no type
  1364. void Builder::createNoResultOp(Op opCode, const std::vector<Id>& operands)
  1365. {
  1366. Instruction* op = new Instruction(opCode);
  1367. for (auto it = operands.cbegin(); it != operands.cend(); ++it) {
  1368. op->addIdOperand(*it);
  1369. }
  1370. buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
  1371. }
  1372. // An opcode that has multiple operands, no result id, and no type
  1373. void Builder::createNoResultOp(Op opCode, const std::vector<IdImmediate>& operands)
  1374. {
  1375. Instruction* op = new Instruction(opCode);
  1376. for (auto it = operands.cbegin(); it != operands.cend(); ++it) {
  1377. if (it->isId)
  1378. op->addIdOperand(it->word);
  1379. else
  1380. op->addImmediateOperand(it->word);
  1381. }
  1382. buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
  1383. }
  1384. void Builder::createControlBarrier(Scope execution, Scope memory, MemorySemanticsMask semantics)
  1385. {
  1386. Instruction* op = new Instruction(OpControlBarrier);
  1387. op->addIdOperand(makeUintConstant(execution));
  1388. op->addIdOperand(makeUintConstant(memory));
  1389. op->addIdOperand(makeUintConstant(semantics));
  1390. buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
  1391. }
  1392. void Builder::createMemoryBarrier(unsigned executionScope, unsigned memorySemantics)
  1393. {
  1394. Instruction* op = new Instruction(OpMemoryBarrier);
  1395. op->addIdOperand(makeUintConstant(executionScope));
  1396. op->addIdOperand(makeUintConstant(memorySemantics));
  1397. buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
  1398. }
  1399. // An opcode that has one operands, a result id, and a type
  1400. Id Builder::createUnaryOp(Op opCode, Id typeId, Id operand)
  1401. {
  1402. // Generate code for spec constants if in spec constant operation
  1403. // generation mode.
  1404. if (generatingOpCodeForSpecConst) {
  1405. return createSpecConstantOp(opCode, typeId, std::vector<Id>(1, operand), std::vector<Id>());
  1406. }
  1407. Instruction* op = new Instruction(getUniqueId(), typeId, opCode);
  1408. op->addIdOperand(operand);
  1409. buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
  1410. return op->getResultId();
  1411. }
  1412. Id Builder::createBinOp(Op opCode, Id typeId, Id left, Id right)
  1413. {
  1414. // Generate code for spec constants if in spec constant operation
  1415. // generation mode.
  1416. if (generatingOpCodeForSpecConst) {
  1417. std::vector<Id> operands(2);
  1418. operands[0] = left; operands[1] = right;
  1419. return createSpecConstantOp(opCode, typeId, operands, std::vector<Id>());
  1420. }
  1421. Instruction* op = new Instruction(getUniqueId(), typeId, opCode);
  1422. op->addIdOperand(left);
  1423. op->addIdOperand(right);
  1424. buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
  1425. return op->getResultId();
  1426. }
  1427. Id Builder::createTriOp(Op opCode, Id typeId, Id op1, Id op2, Id op3)
  1428. {
  1429. // Generate code for spec constants if in spec constant operation
  1430. // generation mode.
  1431. if (generatingOpCodeForSpecConst) {
  1432. std::vector<Id> operands(3);
  1433. operands[0] = op1;
  1434. operands[1] = op2;
  1435. operands[2] = op3;
  1436. return createSpecConstantOp(
  1437. opCode, typeId, operands, std::vector<Id>());
  1438. }
  1439. Instruction* op = new Instruction(getUniqueId(), typeId, opCode);
  1440. op->addIdOperand(op1);
  1441. op->addIdOperand(op2);
  1442. op->addIdOperand(op3);
  1443. buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
  1444. return op->getResultId();
  1445. }
  1446. Id Builder::createOp(Op opCode, Id typeId, const std::vector<Id>& operands)
  1447. {
  1448. Instruction* op = new Instruction(getUniqueId(), typeId, opCode);
  1449. for (auto it = operands.cbegin(); it != operands.cend(); ++it)
  1450. op->addIdOperand(*it);
  1451. buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
  1452. return op->getResultId();
  1453. }
  1454. Id Builder::createOp(Op opCode, Id typeId, const std::vector<IdImmediate>& operands)
  1455. {
  1456. Instruction* op = new Instruction(getUniqueId(), typeId, opCode);
  1457. for (auto it = operands.cbegin(); it != operands.cend(); ++it) {
  1458. if (it->isId)
  1459. op->addIdOperand(it->word);
  1460. else
  1461. op->addImmediateOperand(it->word);
  1462. }
  1463. buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
  1464. return op->getResultId();
  1465. }
  1466. Id Builder::createSpecConstantOp(Op opCode, Id typeId, const std::vector<Id>& operands, const std::vector<unsigned>& literals)
  1467. {
  1468. Instruction* op = new Instruction(getUniqueId(), typeId, OpSpecConstantOp);
  1469. op->addImmediateOperand((unsigned) opCode);
  1470. for (auto it = operands.cbegin(); it != operands.cend(); ++it)
  1471. op->addIdOperand(*it);
  1472. for (auto it = literals.cbegin(); it != literals.cend(); ++it)
  1473. op->addImmediateOperand(*it);
  1474. module.mapInstruction(op);
  1475. constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(op));
  1476. return op->getResultId();
  1477. }
  1478. Id Builder::createFunctionCall(spv::Function* function, const std::vector<spv::Id>& args)
  1479. {
  1480. Instruction* op = new Instruction(getUniqueId(), function->getReturnType(), OpFunctionCall);
  1481. op->addIdOperand(function->getId());
  1482. for (int a = 0; a < (int)args.size(); ++a)
  1483. op->addIdOperand(args[a]);
  1484. buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
  1485. return op->getResultId();
  1486. }
  1487. // Comments in header
  1488. Id Builder::createRvalueSwizzle(Decoration precision, Id typeId, Id source, const std::vector<unsigned>& channels)
  1489. {
  1490. if (channels.size() == 1)
  1491. return setPrecision(createCompositeExtract(source, typeId, channels.front()), precision);
  1492. if (generatingOpCodeForSpecConst) {
  1493. std::vector<Id> operands(2);
  1494. operands[0] = operands[1] = source;
  1495. return setPrecision(createSpecConstantOp(OpVectorShuffle, typeId, operands, channels), precision);
  1496. }
  1497. Instruction* swizzle = new Instruction(getUniqueId(), typeId, OpVectorShuffle);
  1498. assert(isVector(source));
  1499. swizzle->addIdOperand(source);
  1500. swizzle->addIdOperand(source);
  1501. for (int i = 0; i < (int)channels.size(); ++i)
  1502. swizzle->addImmediateOperand(channels[i]);
  1503. buildPoint->addInstruction(std::unique_ptr<Instruction>(swizzle));
  1504. return setPrecision(swizzle->getResultId(), precision);
  1505. }
  1506. // Comments in header
  1507. Id Builder::createLvalueSwizzle(Id typeId, Id target, Id source, const std::vector<unsigned>& channels)
  1508. {
  1509. if (channels.size() == 1 && getNumComponents(source) == 1)
  1510. return createCompositeInsert(source, target, typeId, channels.front());
  1511. Instruction* swizzle = new Instruction(getUniqueId(), typeId, OpVectorShuffle);
  1512. assert(isVector(target));
  1513. swizzle->addIdOperand(target);
  1514. assert(getNumComponents(source) == (int)channels.size());
  1515. assert(isVector(source));
  1516. swizzle->addIdOperand(source);
  1517. // Set up an identity shuffle from the base value to the result value
  1518. unsigned int components[4];
  1519. int numTargetComponents = getNumComponents(target);
  1520. for (int i = 0; i < numTargetComponents; ++i)
  1521. components[i] = i;
  1522. // Punch in the l-value swizzle
  1523. for (int i = 0; i < (int)channels.size(); ++i)
  1524. components[channels[i]] = numTargetComponents + i;
  1525. // finish the instruction with these components selectors
  1526. for (int i = 0; i < numTargetComponents; ++i)
  1527. swizzle->addImmediateOperand(components[i]);
  1528. buildPoint->addInstruction(std::unique_ptr<Instruction>(swizzle));
  1529. return swizzle->getResultId();
  1530. }
  1531. // Comments in header
  1532. void Builder::promoteScalar(Decoration precision, Id& left, Id& right)
  1533. {
  1534. int direction = getNumComponents(right) - getNumComponents(left);
  1535. if (direction > 0)
  1536. left = smearScalar(precision, left, makeVectorType(getTypeId(left), getNumComponents(right)));
  1537. else if (direction < 0)
  1538. right = smearScalar(precision, right, makeVectorType(getTypeId(right), getNumComponents(left)));
  1539. return;
  1540. }
  1541. // Comments in header
  1542. Id Builder::smearScalar(Decoration precision, Id scalar, Id vectorType)
  1543. {
  1544. assert(getNumComponents(scalar) == 1);
  1545. assert(getTypeId(scalar) == getScalarTypeId(vectorType));
  1546. int numComponents = getNumTypeComponents(vectorType);
  1547. if (numComponents == 1)
  1548. return scalar;
  1549. Instruction* smear = nullptr;
  1550. if (generatingOpCodeForSpecConst) {
  1551. auto members = std::vector<spv::Id>(numComponents, scalar);
  1552. // Sometime even in spec-constant-op mode, the temporary vector created by
  1553. // promoting a scalar might not be a spec constant. This should depend on
  1554. // the scalar.
  1555. // e.g.:
  1556. // const vec2 spec_const_result = a_spec_const_vec2 + a_front_end_const_scalar;
  1557. // In such cases, the temporary vector created from a_front_end_const_scalar
  1558. // is not a spec constant vector, even though the binary operation node is marked
  1559. // as 'specConstant' and we are in spec-constant-op mode.
  1560. auto result_id = makeCompositeConstant(vectorType, members, isSpecConstant(scalar));
  1561. smear = module.getInstruction(result_id);
  1562. } else {
  1563. smear = new Instruction(getUniqueId(), vectorType, OpCompositeConstruct);
  1564. for (int c = 0; c < numComponents; ++c)
  1565. smear->addIdOperand(scalar);
  1566. buildPoint->addInstruction(std::unique_ptr<Instruction>(smear));
  1567. }
  1568. return setPrecision(smear->getResultId(), precision);
  1569. }
  1570. // Comments in header
  1571. Id Builder::createBuiltinCall(Id resultType, Id builtins, int entryPoint, const std::vector<Id>& args)
  1572. {
  1573. Instruction* inst = new Instruction(getUniqueId(), resultType, OpExtInst);
  1574. inst->addIdOperand(builtins);
  1575. inst->addImmediateOperand(entryPoint);
  1576. for (int arg = 0; arg < (int)args.size(); ++arg)
  1577. inst->addIdOperand(args[arg]);
  1578. buildPoint->addInstruction(std::unique_ptr<Instruction>(inst));
  1579. return inst->getResultId();
  1580. }
  1581. // Accept all parameters needed to create a texture instruction.
  1582. // Create the correct instruction based on the inputs, and make the call.
  1583. Id Builder::createTextureCall(Decoration precision, Id resultType, bool sparse, bool fetch, bool proj, bool gather,
  1584. bool noImplicitLod, const TextureParameters& parameters, ImageOperandsMask signExtensionMask)
  1585. {
  1586. static const int maxTextureArgs = 10;
  1587. Id texArgs[maxTextureArgs] = {};
  1588. //
  1589. // Set up the fixed arguments
  1590. //
  1591. int numArgs = 0;
  1592. bool explicitLod = false;
  1593. texArgs[numArgs++] = parameters.sampler;
  1594. texArgs[numArgs++] = parameters.coords;
  1595. if (parameters.Dref != NoResult)
  1596. texArgs[numArgs++] = parameters.Dref;
  1597. if (parameters.component != NoResult)
  1598. texArgs[numArgs++] = parameters.component;
  1599. #ifndef GLSLANG_WEB
  1600. if (parameters.granularity != NoResult)
  1601. texArgs[numArgs++] = parameters.granularity;
  1602. if (parameters.coarse != NoResult)
  1603. texArgs[numArgs++] = parameters.coarse;
  1604. #endif
  1605. //
  1606. // Set up the optional arguments
  1607. //
  1608. int optArgNum = numArgs; // track which operand, if it exists, is the mask of optional arguments
  1609. ++numArgs; // speculatively make room for the mask operand
  1610. ImageOperandsMask mask = ImageOperandsMaskNone; // the mask operand
  1611. if (parameters.bias) {
  1612. mask = (ImageOperandsMask)(mask | ImageOperandsBiasMask);
  1613. texArgs[numArgs++] = parameters.bias;
  1614. }
  1615. if (parameters.lod) {
  1616. mask = (ImageOperandsMask)(mask | ImageOperandsLodMask);
  1617. texArgs[numArgs++] = parameters.lod;
  1618. explicitLod = true;
  1619. } else if (parameters.gradX) {
  1620. mask = (ImageOperandsMask)(mask | ImageOperandsGradMask);
  1621. texArgs[numArgs++] = parameters.gradX;
  1622. texArgs[numArgs++] = parameters.gradY;
  1623. explicitLod = true;
  1624. } else if (noImplicitLod && ! fetch && ! gather) {
  1625. // have to explicitly use lod of 0 if not allowed to have them be implicit, and
  1626. // we would otherwise be about to issue an implicit instruction
  1627. mask = (ImageOperandsMask)(mask | ImageOperandsLodMask);
  1628. texArgs[numArgs++] = makeFloatConstant(0.0);
  1629. explicitLod = true;
  1630. }
  1631. if (parameters.offset) {
  1632. if (isConstant(parameters.offset))
  1633. mask = (ImageOperandsMask)(mask | ImageOperandsConstOffsetMask);
  1634. else {
  1635. addCapability(CapabilityImageGatherExtended);
  1636. mask = (ImageOperandsMask)(mask | ImageOperandsOffsetMask);
  1637. }
  1638. texArgs[numArgs++] = parameters.offset;
  1639. }
  1640. if (parameters.offsets) {
  1641. addCapability(CapabilityImageGatherExtended);
  1642. mask = (ImageOperandsMask)(mask | ImageOperandsConstOffsetsMask);
  1643. texArgs[numArgs++] = parameters.offsets;
  1644. }
  1645. #ifndef GLSLANG_WEB
  1646. if (parameters.sample) {
  1647. mask = (ImageOperandsMask)(mask | ImageOperandsSampleMask);
  1648. texArgs[numArgs++] = parameters.sample;
  1649. }
  1650. if (parameters.lodClamp) {
  1651. // capability if this bit is used
  1652. addCapability(CapabilityMinLod);
  1653. mask = (ImageOperandsMask)(mask | ImageOperandsMinLodMask);
  1654. texArgs[numArgs++] = parameters.lodClamp;
  1655. }
  1656. if (parameters.nonprivate) {
  1657. mask = mask | ImageOperandsNonPrivateTexelKHRMask;
  1658. }
  1659. if (parameters.volatil) {
  1660. mask = mask | ImageOperandsVolatileTexelKHRMask;
  1661. }
  1662. #endif
  1663. mask = mask | signExtensionMask;
  1664. if (mask == ImageOperandsMaskNone)
  1665. --numArgs; // undo speculative reservation for the mask argument
  1666. else
  1667. texArgs[optArgNum] = mask;
  1668. //
  1669. // Set up the instruction
  1670. //
  1671. Op opCode = OpNop; // All paths below need to set this
  1672. if (fetch) {
  1673. if (sparse)
  1674. opCode = OpImageSparseFetch;
  1675. else
  1676. opCode = OpImageFetch;
  1677. #ifndef GLSLANG_WEB
  1678. } else if (parameters.granularity && parameters.coarse) {
  1679. opCode = OpImageSampleFootprintNV;
  1680. } else if (gather) {
  1681. if (parameters.Dref)
  1682. if (sparse)
  1683. opCode = OpImageSparseDrefGather;
  1684. else
  1685. opCode = OpImageDrefGather;
  1686. else
  1687. if (sparse)
  1688. opCode = OpImageSparseGather;
  1689. else
  1690. opCode = OpImageGather;
  1691. #endif
  1692. } else if (explicitLod) {
  1693. if (parameters.Dref) {
  1694. if (proj)
  1695. if (sparse)
  1696. opCode = OpImageSparseSampleProjDrefExplicitLod;
  1697. else
  1698. opCode = OpImageSampleProjDrefExplicitLod;
  1699. else
  1700. if (sparse)
  1701. opCode = OpImageSparseSampleDrefExplicitLod;
  1702. else
  1703. opCode = OpImageSampleDrefExplicitLod;
  1704. } else {
  1705. if (proj)
  1706. if (sparse)
  1707. opCode = OpImageSparseSampleProjExplicitLod;
  1708. else
  1709. opCode = OpImageSampleProjExplicitLod;
  1710. else
  1711. if (sparse)
  1712. opCode = OpImageSparseSampleExplicitLod;
  1713. else
  1714. opCode = OpImageSampleExplicitLod;
  1715. }
  1716. } else {
  1717. if (parameters.Dref) {
  1718. if (proj)
  1719. if (sparse)
  1720. opCode = OpImageSparseSampleProjDrefImplicitLod;
  1721. else
  1722. opCode = OpImageSampleProjDrefImplicitLod;
  1723. else
  1724. if (sparse)
  1725. opCode = OpImageSparseSampleDrefImplicitLod;
  1726. else
  1727. opCode = OpImageSampleDrefImplicitLod;
  1728. } else {
  1729. if (proj)
  1730. if (sparse)
  1731. opCode = OpImageSparseSampleProjImplicitLod;
  1732. else
  1733. opCode = OpImageSampleProjImplicitLod;
  1734. else
  1735. if (sparse)
  1736. opCode = OpImageSparseSampleImplicitLod;
  1737. else
  1738. opCode = OpImageSampleImplicitLod;
  1739. }
  1740. }
  1741. // See if the result type is expecting a smeared result.
  1742. // This happens when a legacy shadow*() call is made, which
  1743. // gets a vec4 back instead of a float.
  1744. Id smearedType = resultType;
  1745. if (! isScalarType(resultType)) {
  1746. switch (opCode) {
  1747. case OpImageSampleDrefImplicitLod:
  1748. case OpImageSampleDrefExplicitLod:
  1749. case OpImageSampleProjDrefImplicitLod:
  1750. case OpImageSampleProjDrefExplicitLod:
  1751. resultType = getScalarTypeId(resultType);
  1752. break;
  1753. default:
  1754. break;
  1755. }
  1756. }
  1757. Id typeId0 = 0;
  1758. Id typeId1 = 0;
  1759. if (sparse) {
  1760. typeId0 = resultType;
  1761. typeId1 = getDerefTypeId(parameters.texelOut);
  1762. resultType = makeStructResultType(typeId0, typeId1);
  1763. }
  1764. // Build the SPIR-V instruction
  1765. Instruction* textureInst = new Instruction(getUniqueId(), resultType, opCode);
  1766. for (int op = 0; op < optArgNum; ++op)
  1767. textureInst->addIdOperand(texArgs[op]);
  1768. if (optArgNum < numArgs)
  1769. textureInst->addImmediateOperand(texArgs[optArgNum]);
  1770. for (int op = optArgNum + 1; op < numArgs; ++op)
  1771. textureInst->addIdOperand(texArgs[op]);
  1772. setPrecision(textureInst->getResultId(), precision);
  1773. buildPoint->addInstruction(std::unique_ptr<Instruction>(textureInst));
  1774. Id resultId = textureInst->getResultId();
  1775. if (sparse) {
  1776. // set capability
  1777. addCapability(CapabilitySparseResidency);
  1778. // Decode the return type that was a special structure
  1779. createStore(createCompositeExtract(resultId, typeId1, 1), parameters.texelOut);
  1780. resultId = createCompositeExtract(resultId, typeId0, 0);
  1781. setPrecision(resultId, precision);
  1782. } else {
  1783. // When a smear is needed, do it, as per what was computed
  1784. // above when resultType was changed to a scalar type.
  1785. if (resultType != smearedType)
  1786. resultId = smearScalar(precision, resultId, smearedType);
  1787. }
  1788. return resultId;
  1789. }
  1790. // Comments in header
  1791. Id Builder::createTextureQueryCall(Op opCode, const TextureParameters& parameters, bool isUnsignedResult)
  1792. {
  1793. // Figure out the result type
  1794. Id resultType = 0;
  1795. switch (opCode) {
  1796. case OpImageQuerySize:
  1797. case OpImageQuerySizeLod:
  1798. {
  1799. int numComponents = 0;
  1800. switch (getTypeDimensionality(getImageType(parameters.sampler))) {
  1801. case Dim1D:
  1802. case DimBuffer:
  1803. numComponents = 1;
  1804. break;
  1805. case Dim2D:
  1806. case DimCube:
  1807. case DimRect:
  1808. case DimSubpassData:
  1809. numComponents = 2;
  1810. break;
  1811. case Dim3D:
  1812. numComponents = 3;
  1813. break;
  1814. default:
  1815. assert(0);
  1816. break;
  1817. }
  1818. if (isArrayedImageType(getImageType(parameters.sampler)))
  1819. ++numComponents;
  1820. Id intType = isUnsignedResult ? makeUintType(32) : makeIntType(32);
  1821. if (numComponents == 1)
  1822. resultType = intType;
  1823. else
  1824. resultType = makeVectorType(intType, numComponents);
  1825. break;
  1826. }
  1827. case OpImageQueryLod:
  1828. resultType = makeVectorType(getScalarTypeId(getTypeId(parameters.coords)), 2);
  1829. break;
  1830. case OpImageQueryLevels:
  1831. case OpImageQuerySamples:
  1832. resultType = isUnsignedResult ? makeUintType(32) : makeIntType(32);
  1833. break;
  1834. default:
  1835. assert(0);
  1836. break;
  1837. }
  1838. Instruction* query = new Instruction(getUniqueId(), resultType, opCode);
  1839. query->addIdOperand(parameters.sampler);
  1840. if (parameters.coords)
  1841. query->addIdOperand(parameters.coords);
  1842. if (parameters.lod)
  1843. query->addIdOperand(parameters.lod);
  1844. buildPoint->addInstruction(std::unique_ptr<Instruction>(query));
  1845. addCapability(CapabilityImageQuery);
  1846. return query->getResultId();
  1847. }
  1848. // External comments in header.
  1849. // Operates recursively to visit the composite's hierarchy.
  1850. Id Builder::createCompositeCompare(Decoration precision, Id value1, Id value2, bool equal)
  1851. {
  1852. Id boolType = makeBoolType();
  1853. Id valueType = getTypeId(value1);
  1854. Id resultId = NoResult;
  1855. int numConstituents = getNumTypeConstituents(valueType);
  1856. // Scalars and Vectors
  1857. if (isScalarType(valueType) || isVectorType(valueType)) {
  1858. assert(valueType == getTypeId(value2));
  1859. // These just need a single comparison, just have
  1860. // to figure out what it is.
  1861. Op op;
  1862. switch (getMostBasicTypeClass(valueType)) {
  1863. case OpTypeFloat:
  1864. op = equal ? OpFOrdEqual : OpFOrdNotEqual;
  1865. break;
  1866. case OpTypeInt:
  1867. default:
  1868. op = equal ? OpIEqual : OpINotEqual;
  1869. break;
  1870. case OpTypeBool:
  1871. op = equal ? OpLogicalEqual : OpLogicalNotEqual;
  1872. precision = NoPrecision;
  1873. break;
  1874. }
  1875. if (isScalarType(valueType)) {
  1876. // scalar
  1877. resultId = createBinOp(op, boolType, value1, value2);
  1878. } else {
  1879. // vector
  1880. resultId = createBinOp(op, makeVectorType(boolType, numConstituents), value1, value2);
  1881. setPrecision(resultId, precision);
  1882. // reduce vector compares...
  1883. resultId = createUnaryOp(equal ? OpAll : OpAny, boolType, resultId);
  1884. }
  1885. return setPrecision(resultId, precision);
  1886. }
  1887. // Only structs, arrays, and matrices should be left.
  1888. // They share in common the reduction operation across their constituents.
  1889. assert(isAggregateType(valueType) || isMatrixType(valueType));
  1890. // Compare each pair of constituents
  1891. for (int constituent = 0; constituent < numConstituents; ++constituent) {
  1892. std::vector<unsigned> indexes(1, constituent);
  1893. Id constituentType1 = getContainedTypeId(getTypeId(value1), constituent);
  1894. Id constituentType2 = getContainedTypeId(getTypeId(value2), constituent);
  1895. Id constituent1 = createCompositeExtract(value1, constituentType1, indexes);
  1896. Id constituent2 = createCompositeExtract(value2, constituentType2, indexes);
  1897. Id subResultId = createCompositeCompare(precision, constituent1, constituent2, equal);
  1898. if (constituent == 0)
  1899. resultId = subResultId;
  1900. else
  1901. resultId = setPrecision(createBinOp(equal ? OpLogicalAnd : OpLogicalOr, boolType, resultId, subResultId), precision);
  1902. }
  1903. return resultId;
  1904. }
  1905. // OpCompositeConstruct
  1906. Id Builder::createCompositeConstruct(Id typeId, const std::vector<Id>& constituents)
  1907. {
  1908. assert(isAggregateType(typeId) || (getNumTypeConstituents(typeId) > 1 && getNumTypeConstituents(typeId) == (int)constituents.size()));
  1909. if (generatingOpCodeForSpecConst) {
  1910. // Sometime, even in spec-constant-op mode, the constant composite to be
  1911. // constructed may not be a specialization constant.
  1912. // e.g.:
  1913. // const mat2 m2 = mat2(a_spec_const, a_front_end_const, another_front_end_const, third_front_end_const);
  1914. // The first column vector should be a spec constant one, as a_spec_const is a spec constant.
  1915. // The second column vector should NOT be spec constant, as it does not contain any spec constants.
  1916. // To handle such cases, we check the constituents of the constant vector to determine whether this
  1917. // vector should be created as a spec constant.
  1918. return makeCompositeConstant(typeId, constituents,
  1919. std::any_of(constituents.begin(), constituents.end(),
  1920. [&](spv::Id id) { return isSpecConstant(id); }));
  1921. }
  1922. Instruction* op = new Instruction(getUniqueId(), typeId, OpCompositeConstruct);
  1923. for (int c = 0; c < (int)constituents.size(); ++c)
  1924. op->addIdOperand(constituents[c]);
  1925. buildPoint->addInstruction(std::unique_ptr<Instruction>(op));
  1926. return op->getResultId();
  1927. }
  1928. // Vector or scalar constructor
  1929. Id Builder::createConstructor(Decoration precision, const std::vector<Id>& sources, Id resultTypeId)
  1930. {
  1931. Id result = NoResult;
  1932. unsigned int numTargetComponents = getNumTypeComponents(resultTypeId);
  1933. unsigned int targetComponent = 0;
  1934. // Special case: when calling a vector constructor with a single scalar
  1935. // argument, smear the scalar
  1936. if (sources.size() == 1 && isScalar(sources[0]) && numTargetComponents > 1)
  1937. return smearScalar(precision, sources[0], resultTypeId);
  1938. // accumulate the arguments for OpCompositeConstruct
  1939. std::vector<Id> constituents;
  1940. Id scalarTypeId = getScalarTypeId(resultTypeId);
  1941. // lambda to store the result of visiting an argument component
  1942. const auto latchResult = [&](Id comp) {
  1943. if (numTargetComponents > 1)
  1944. constituents.push_back(comp);
  1945. else
  1946. result = comp;
  1947. ++targetComponent;
  1948. };
  1949. // lambda to visit a vector argument's components
  1950. const auto accumulateVectorConstituents = [&](Id sourceArg) {
  1951. unsigned int sourceSize = getNumComponents(sourceArg);
  1952. unsigned int sourcesToUse = sourceSize;
  1953. if (sourcesToUse + targetComponent > numTargetComponents)
  1954. sourcesToUse = numTargetComponents - targetComponent;
  1955. for (unsigned int s = 0; s < sourcesToUse; ++s) {
  1956. std::vector<unsigned> swiz;
  1957. swiz.push_back(s);
  1958. latchResult(createRvalueSwizzle(precision, scalarTypeId, sourceArg, swiz));
  1959. }
  1960. };
  1961. // lambda to visit a matrix argument's components
  1962. const auto accumulateMatrixConstituents = [&](Id sourceArg) {
  1963. unsigned int sourceSize = getNumColumns(sourceArg) * getNumRows(sourceArg);
  1964. unsigned int sourcesToUse = sourceSize;
  1965. if (sourcesToUse + targetComponent > numTargetComponents)
  1966. sourcesToUse = numTargetComponents - targetComponent;
  1967. int col = 0;
  1968. int row = 0;
  1969. for (unsigned int s = 0; s < sourcesToUse; ++s) {
  1970. if (row >= getNumRows(sourceArg)) {
  1971. row = 0;
  1972. col++;
  1973. }
  1974. std::vector<Id> indexes;
  1975. indexes.push_back(col);
  1976. indexes.push_back(row);
  1977. latchResult(createCompositeExtract(sourceArg, scalarTypeId, indexes));
  1978. row++;
  1979. }
  1980. };
  1981. // Go through the source arguments, each one could have either
  1982. // a single or multiple components to contribute.
  1983. for (unsigned int i = 0; i < sources.size(); ++i) {
  1984. if (isScalar(sources[i]) || isPointer(sources[i]))
  1985. latchResult(sources[i]);
  1986. else if (isVector(sources[i]))
  1987. accumulateVectorConstituents(sources[i]);
  1988. else if (isMatrix(sources[i]))
  1989. accumulateMatrixConstituents(sources[i]);
  1990. else
  1991. assert(0);
  1992. if (targetComponent >= numTargetComponents)
  1993. break;
  1994. }
  1995. // If the result is a vector, make it from the gathered constituents.
  1996. if (constituents.size() > 0)
  1997. result = createCompositeConstruct(resultTypeId, constituents);
  1998. return setPrecision(result, precision);
  1999. }
  2000. // Comments in header
  2001. Id Builder::createMatrixConstructor(Decoration precision, const std::vector<Id>& sources, Id resultTypeId)
  2002. {
  2003. Id componentTypeId = getScalarTypeId(resultTypeId);
  2004. int numCols = getTypeNumColumns(resultTypeId);
  2005. int numRows = getTypeNumRows(resultTypeId);
  2006. Instruction* instr = module.getInstruction(componentTypeId);
  2007. #ifdef GLSLANG_WEB
  2008. const unsigned bitCount = 32;
  2009. assert(bitCount == instr->getImmediateOperand(0));
  2010. #else
  2011. const unsigned bitCount = instr->getImmediateOperand(0);
  2012. #endif
  2013. // Optimize matrix constructed from a bigger matrix
  2014. if (isMatrix(sources[0]) && getNumColumns(sources[0]) >= numCols && getNumRows(sources[0]) >= numRows) {
  2015. // To truncate the matrix to a smaller number of rows/columns, we need to:
  2016. // 1. For each column, extract the column and truncate it to the required size using shuffle
  2017. // 2. Assemble the resulting matrix from all columns
  2018. Id matrix = sources[0];
  2019. Id columnTypeId = getContainedTypeId(resultTypeId);
  2020. Id sourceColumnTypeId = getContainedTypeId(getTypeId(matrix));
  2021. std::vector<unsigned> channels;
  2022. for (int row = 0; row < numRows; ++row)
  2023. channels.push_back(row);
  2024. std::vector<Id> matrixColumns;
  2025. for (int col = 0; col < numCols; ++col) {
  2026. std::vector<unsigned> indexes;
  2027. indexes.push_back(col);
  2028. Id colv = createCompositeExtract(matrix, sourceColumnTypeId, indexes);
  2029. setPrecision(colv, precision);
  2030. if (numRows != getNumRows(matrix)) {
  2031. matrixColumns.push_back(createRvalueSwizzle(precision, columnTypeId, colv, channels));
  2032. } else {
  2033. matrixColumns.push_back(colv);
  2034. }
  2035. }
  2036. return setPrecision(createCompositeConstruct(resultTypeId, matrixColumns), precision);
  2037. }
  2038. // Otherwise, will use a two step process
  2039. // 1. make a compile-time 2D array of values
  2040. // 2. construct a matrix from that array
  2041. // Step 1.
  2042. // initialize the array to the identity matrix
  2043. Id ids[maxMatrixSize][maxMatrixSize];
  2044. Id one = (bitCount == 64 ? makeDoubleConstant(1.0) : makeFloatConstant(1.0));
  2045. Id zero = (bitCount == 64 ? makeDoubleConstant(0.0) : makeFloatConstant(0.0));
  2046. for (int col = 0; col < 4; ++col) {
  2047. for (int row = 0; row < 4; ++row) {
  2048. if (col == row)
  2049. ids[col][row] = one;
  2050. else
  2051. ids[col][row] = zero;
  2052. }
  2053. }
  2054. // modify components as dictated by the arguments
  2055. if (sources.size() == 1 && isScalar(sources[0])) {
  2056. // a single scalar; resets the diagonals
  2057. for (int col = 0; col < 4; ++col)
  2058. ids[col][col] = sources[0];
  2059. } else if (isMatrix(sources[0])) {
  2060. // constructing from another matrix; copy over the parts that exist in both the argument and constructee
  2061. Id matrix = sources[0];
  2062. int minCols = std::min(numCols, getNumColumns(matrix));
  2063. int minRows = std::min(numRows, getNumRows(matrix));
  2064. for (int col = 0; col < minCols; ++col) {
  2065. std::vector<unsigned> indexes;
  2066. indexes.push_back(col);
  2067. for (int row = 0; row < minRows; ++row) {
  2068. indexes.push_back(row);
  2069. ids[col][row] = createCompositeExtract(matrix, componentTypeId, indexes);
  2070. indexes.pop_back();
  2071. setPrecision(ids[col][row], precision);
  2072. }
  2073. }
  2074. } else {
  2075. // fill in the matrix in column-major order with whatever argument components are available
  2076. int row = 0;
  2077. int col = 0;
  2078. for (int arg = 0; arg < (int)sources.size(); ++arg) {
  2079. Id argComp = sources[arg];
  2080. for (int comp = 0; comp < getNumComponents(sources[arg]); ++comp) {
  2081. if (getNumComponents(sources[arg]) > 1) {
  2082. argComp = createCompositeExtract(sources[arg], componentTypeId, comp);
  2083. setPrecision(argComp, precision);
  2084. }
  2085. ids[col][row++] = argComp;
  2086. if (row == numRows) {
  2087. row = 0;
  2088. col++;
  2089. }
  2090. }
  2091. }
  2092. }
  2093. // Step 2: Construct a matrix from that array.
  2094. // First make the column vectors, then make the matrix.
  2095. // make the column vectors
  2096. Id columnTypeId = getContainedTypeId(resultTypeId);
  2097. std::vector<Id> matrixColumns;
  2098. for (int col = 0; col < numCols; ++col) {
  2099. std::vector<Id> vectorComponents;
  2100. for (int row = 0; row < numRows; ++row)
  2101. vectorComponents.push_back(ids[col][row]);
  2102. Id column = createCompositeConstruct(columnTypeId, vectorComponents);
  2103. setPrecision(column, precision);
  2104. matrixColumns.push_back(column);
  2105. }
  2106. // make the matrix
  2107. return setPrecision(createCompositeConstruct(resultTypeId, matrixColumns), precision);
  2108. }
  2109. // Comments in header
  2110. Builder::If::If(Id cond, unsigned int ctrl, Builder& gb) :
  2111. builder(gb),
  2112. condition(cond),
  2113. control(ctrl),
  2114. elseBlock(0)
  2115. {
  2116. function = &builder.getBuildPoint()->getParent();
  2117. // make the blocks, but only put the then-block into the function,
  2118. // the else-block and merge-block will be added later, in order, after
  2119. // earlier code is emitted
  2120. thenBlock = new Block(builder.getUniqueId(), *function);
  2121. mergeBlock = new Block(builder.getUniqueId(), *function);
  2122. // Save the current block, so that we can add in the flow control split when
  2123. // makeEndIf is called.
  2124. headerBlock = builder.getBuildPoint();
  2125. function->addBlock(thenBlock);
  2126. builder.setBuildPoint(thenBlock);
  2127. }
  2128. // Comments in header
  2129. void Builder::If::makeBeginElse()
  2130. {
  2131. // Close out the "then" by having it jump to the mergeBlock
  2132. builder.createBranch(mergeBlock);
  2133. // Make the first else block and add it to the function
  2134. elseBlock = new Block(builder.getUniqueId(), *function);
  2135. function->addBlock(elseBlock);
  2136. // Start building the else block
  2137. builder.setBuildPoint(elseBlock);
  2138. }
  2139. // Comments in header
  2140. void Builder::If::makeEndIf()
  2141. {
  2142. // jump to the merge block
  2143. builder.createBranch(mergeBlock);
  2144. // Go back to the headerBlock and make the flow control split
  2145. builder.setBuildPoint(headerBlock);
  2146. builder.createSelectionMerge(mergeBlock, control);
  2147. if (elseBlock)
  2148. builder.createConditionalBranch(condition, thenBlock, elseBlock);
  2149. else
  2150. builder.createConditionalBranch(condition, thenBlock, mergeBlock);
  2151. // add the merge block to the function
  2152. function->addBlock(mergeBlock);
  2153. builder.setBuildPoint(mergeBlock);
  2154. }
  2155. // Comments in header
  2156. void Builder::makeSwitch(Id selector, unsigned int control, int numSegments, const std::vector<int>& caseValues,
  2157. const std::vector<int>& valueIndexToSegment, int defaultSegment,
  2158. std::vector<Block*>& segmentBlocks)
  2159. {
  2160. Function& function = buildPoint->getParent();
  2161. // make all the blocks
  2162. for (int s = 0; s < numSegments; ++s)
  2163. segmentBlocks.push_back(new Block(getUniqueId(), function));
  2164. Block* mergeBlock = new Block(getUniqueId(), function);
  2165. // make and insert the switch's selection-merge instruction
  2166. createSelectionMerge(mergeBlock, control);
  2167. // make the switch instruction
  2168. Instruction* switchInst = new Instruction(NoResult, NoType, OpSwitch);
  2169. switchInst->addIdOperand(selector);
  2170. auto defaultOrMerge = (defaultSegment >= 0) ? segmentBlocks[defaultSegment] : mergeBlock;
  2171. switchInst->addIdOperand(defaultOrMerge->getId());
  2172. defaultOrMerge->addPredecessor(buildPoint);
  2173. for (int i = 0; i < (int)caseValues.size(); ++i) {
  2174. switchInst->addImmediateOperand(caseValues[i]);
  2175. switchInst->addIdOperand(segmentBlocks[valueIndexToSegment[i]]->getId());
  2176. segmentBlocks[valueIndexToSegment[i]]->addPredecessor(buildPoint);
  2177. }
  2178. buildPoint->addInstruction(std::unique_ptr<Instruction>(switchInst));
  2179. // push the merge block
  2180. switchMerges.push(mergeBlock);
  2181. }
  2182. // Comments in header
  2183. void Builder::addSwitchBreak()
  2184. {
  2185. // branch to the top of the merge block stack
  2186. createBranch(switchMerges.top());
  2187. createAndSetNoPredecessorBlock("post-switch-break");
  2188. }
  2189. // Comments in header
  2190. void Builder::nextSwitchSegment(std::vector<Block*>& segmentBlock, int nextSegment)
  2191. {
  2192. int lastSegment = nextSegment - 1;
  2193. if (lastSegment >= 0) {
  2194. // Close out previous segment by jumping, if necessary, to next segment
  2195. if (! buildPoint->isTerminated())
  2196. createBranch(segmentBlock[nextSegment]);
  2197. }
  2198. Block* block = segmentBlock[nextSegment];
  2199. block->getParent().addBlock(block);
  2200. setBuildPoint(block);
  2201. }
  2202. // Comments in header
  2203. void Builder::endSwitch(std::vector<Block*>& /*segmentBlock*/)
  2204. {
  2205. // Close out previous segment by jumping, if necessary, to next segment
  2206. if (! buildPoint->isTerminated())
  2207. addSwitchBreak();
  2208. switchMerges.top()->getParent().addBlock(switchMerges.top());
  2209. setBuildPoint(switchMerges.top());
  2210. switchMerges.pop();
  2211. }
  2212. Block& Builder::makeNewBlock()
  2213. {
  2214. Function& function = buildPoint->getParent();
  2215. auto block = new Block(getUniqueId(), function);
  2216. function.addBlock(block);
  2217. return *block;
  2218. }
  2219. Builder::LoopBlocks& Builder::makeNewLoop()
  2220. {
  2221. // This verbosity is needed to simultaneously get the same behavior
  2222. // everywhere (id's in the same order), have a syntax that works
  2223. // across lots of versions of C++, have no warnings from pedantic
  2224. // compilation modes, and leave the rest of the code alone.
  2225. Block& head = makeNewBlock();
  2226. Block& body = makeNewBlock();
  2227. Block& merge = makeNewBlock();
  2228. Block& continue_target = makeNewBlock();
  2229. LoopBlocks blocks(head, body, merge, continue_target);
  2230. loops.push(blocks);
  2231. return loops.top();
  2232. }
  2233. void Builder::createLoopContinue()
  2234. {
  2235. createBranch(&loops.top().continue_target);
  2236. // Set up a block for dead code.
  2237. createAndSetNoPredecessorBlock("post-loop-continue");
  2238. }
  2239. void Builder::createLoopExit()
  2240. {
  2241. createBranch(&loops.top().merge);
  2242. // Set up a block for dead code.
  2243. createAndSetNoPredecessorBlock("post-loop-break");
  2244. }
  2245. void Builder::closeLoop()
  2246. {
  2247. loops.pop();
  2248. }
  2249. void Builder::clearAccessChain()
  2250. {
  2251. accessChain.base = NoResult;
  2252. accessChain.indexChain.clear();
  2253. accessChain.instr = NoResult;
  2254. accessChain.swizzle.clear();
  2255. accessChain.component = NoResult;
  2256. accessChain.preSwizzleBaseType = NoType;
  2257. accessChain.isRValue = false;
  2258. accessChain.coherentFlags.clear();
  2259. accessChain.alignment = 0;
  2260. }
  2261. // Comments in header
  2262. void Builder::accessChainPushSwizzle(std::vector<unsigned>& swizzle, Id preSwizzleBaseType, AccessChain::CoherentFlags coherentFlags, unsigned int alignment)
  2263. {
  2264. accessChain.coherentFlags |= coherentFlags;
  2265. accessChain.alignment |= alignment;
  2266. // swizzles can be stacked in GLSL, but simplified to a single
  2267. // one here; the base type doesn't change
  2268. if (accessChain.preSwizzleBaseType == NoType)
  2269. accessChain.preSwizzleBaseType = preSwizzleBaseType;
  2270. // if needed, propagate the swizzle for the current access chain
  2271. if (accessChain.swizzle.size() > 0) {
  2272. std::vector<unsigned> oldSwizzle = accessChain.swizzle;
  2273. accessChain.swizzle.resize(0);
  2274. for (unsigned int i = 0; i < swizzle.size(); ++i) {
  2275. assert(swizzle[i] < oldSwizzle.size());
  2276. accessChain.swizzle.push_back(oldSwizzle[swizzle[i]]);
  2277. }
  2278. } else
  2279. accessChain.swizzle = swizzle;
  2280. // determine if we need to track this swizzle anymore
  2281. simplifyAccessChainSwizzle();
  2282. }
  2283. // Comments in header
  2284. void Builder::accessChainStore(Id rvalue, spv::MemoryAccessMask memoryAccess, spv::Scope scope, unsigned int alignment)
  2285. {
  2286. assert(accessChain.isRValue == false);
  2287. transferAccessChainSwizzle(true);
  2288. Id base = collapseAccessChain();
  2289. Id source = rvalue;
  2290. // dynamic component should be gone
  2291. assert(accessChain.component == NoResult);
  2292. // If swizzle still exists, it is out-of-order or not full, we must load the target vector,
  2293. // extract and insert elements to perform writeMask and/or swizzle.
  2294. if (accessChain.swizzle.size() > 0) {
  2295. Id tempBaseId = createLoad(base);
  2296. source = createLvalueSwizzle(getTypeId(tempBaseId), tempBaseId, source, accessChain.swizzle);
  2297. }
  2298. // take LSB of alignment
  2299. alignment = alignment & ~(alignment & (alignment-1));
  2300. if (getStorageClass(base) == StorageClassPhysicalStorageBufferEXT) {
  2301. memoryAccess = (spv::MemoryAccessMask)(memoryAccess | spv::MemoryAccessAlignedMask);
  2302. }
  2303. createStore(source, base, memoryAccess, scope, alignment);
  2304. }
  2305. // Comments in header
  2306. Id Builder::accessChainLoad(Decoration precision, Decoration nonUniform, Id resultType, spv::MemoryAccessMask memoryAccess, spv::Scope scope, unsigned int alignment)
  2307. {
  2308. Id id;
  2309. if (accessChain.isRValue) {
  2310. // transfer access chain, but try to stay in registers
  2311. transferAccessChainSwizzle(false);
  2312. if (accessChain.indexChain.size() > 0) {
  2313. Id swizzleBase = accessChain.preSwizzleBaseType != NoType ? accessChain.preSwizzleBaseType : resultType;
  2314. // if all the accesses are constants, we can use OpCompositeExtract
  2315. std::vector<unsigned> indexes;
  2316. bool constant = true;
  2317. for (int i = 0; i < (int)accessChain.indexChain.size(); ++i) {
  2318. if (isConstantScalar(accessChain.indexChain[i]))
  2319. indexes.push_back(getConstantScalar(accessChain.indexChain[i]));
  2320. else {
  2321. constant = false;
  2322. break;
  2323. }
  2324. }
  2325. if (constant) {
  2326. id = createCompositeExtract(accessChain.base, swizzleBase, indexes);
  2327. } else {
  2328. Id lValue = NoResult;
  2329. if (spvVersion >= Spv_1_4) {
  2330. // make a new function variable for this r-value, using an initializer,
  2331. // and mark it as NonWritable so that downstream it can be detected as a lookup
  2332. // table
  2333. lValue = createVariable(StorageClassFunction, getTypeId(accessChain.base), "indexable",
  2334. accessChain.base);
  2335. addDecoration(lValue, DecorationNonWritable);
  2336. } else {
  2337. lValue = createVariable(StorageClassFunction, getTypeId(accessChain.base), "indexable");
  2338. // store into it
  2339. createStore(accessChain.base, lValue);
  2340. }
  2341. // move base to the new variable
  2342. accessChain.base = lValue;
  2343. accessChain.isRValue = false;
  2344. // load through the access chain
  2345. id = createLoad(collapseAccessChain());
  2346. }
  2347. setPrecision(id, precision);
  2348. } else
  2349. id = accessChain.base; // no precision, it was set when this was defined
  2350. } else {
  2351. transferAccessChainSwizzle(true);
  2352. // take LSB of alignment
  2353. alignment = alignment & ~(alignment & (alignment-1));
  2354. if (getStorageClass(accessChain.base) == StorageClassPhysicalStorageBufferEXT) {
  2355. memoryAccess = (spv::MemoryAccessMask)(memoryAccess | spv::MemoryAccessAlignedMask);
  2356. }
  2357. // load through the access chain
  2358. id = createLoad(collapseAccessChain(), memoryAccess, scope, alignment);
  2359. setPrecision(id, precision);
  2360. addDecoration(id, nonUniform);
  2361. }
  2362. // Done, unless there are swizzles to do
  2363. if (accessChain.swizzle.size() == 0 && accessChain.component == NoResult)
  2364. return id;
  2365. // Do remaining swizzling
  2366. // Do the basic swizzle
  2367. if (accessChain.swizzle.size() > 0) {
  2368. Id swizzledType = getScalarTypeId(getTypeId(id));
  2369. if (accessChain.swizzle.size() > 1)
  2370. swizzledType = makeVectorType(swizzledType, (int)accessChain.swizzle.size());
  2371. id = createRvalueSwizzle(precision, swizzledType, id, accessChain.swizzle);
  2372. }
  2373. // Do the dynamic component
  2374. if (accessChain.component != NoResult)
  2375. id = setPrecision(createVectorExtractDynamic(id, resultType, accessChain.component), precision);
  2376. addDecoration(id, nonUniform);
  2377. return id;
  2378. }
  2379. Id Builder::accessChainGetLValue()
  2380. {
  2381. assert(accessChain.isRValue == false);
  2382. transferAccessChainSwizzle(true);
  2383. Id lvalue = collapseAccessChain();
  2384. // If swizzle exists, it is out-of-order or not full, we must load the target vector,
  2385. // extract and insert elements to perform writeMask and/or swizzle. This does not
  2386. // go with getting a direct l-value pointer.
  2387. assert(accessChain.swizzle.size() == 0);
  2388. assert(accessChain.component == NoResult);
  2389. return lvalue;
  2390. }
  2391. // comment in header
  2392. Id Builder::accessChainGetInferredType()
  2393. {
  2394. // anything to operate on?
  2395. if (accessChain.base == NoResult)
  2396. return NoType;
  2397. Id type = getTypeId(accessChain.base);
  2398. // do initial dereference
  2399. if (! accessChain.isRValue)
  2400. type = getContainedTypeId(type);
  2401. // dereference each index
  2402. for (auto it = accessChain.indexChain.cbegin(); it != accessChain.indexChain.cend(); ++it) {
  2403. if (isStructType(type))
  2404. type = getContainedTypeId(type, getConstantScalar(*it));
  2405. else
  2406. type = getContainedTypeId(type);
  2407. }
  2408. // dereference swizzle
  2409. if (accessChain.swizzle.size() == 1)
  2410. type = getContainedTypeId(type);
  2411. else if (accessChain.swizzle.size() > 1)
  2412. type = makeVectorType(getContainedTypeId(type), (int)accessChain.swizzle.size());
  2413. // dereference component selection
  2414. if (accessChain.component)
  2415. type = getContainedTypeId(type);
  2416. return type;
  2417. }
  2418. void Builder::dump(std::vector<unsigned int>& out) const
  2419. {
  2420. // Header, before first instructions:
  2421. out.push_back(MagicNumber);
  2422. out.push_back(spvVersion);
  2423. out.push_back(builderNumber);
  2424. out.push_back(uniqueId + 1);
  2425. out.push_back(0);
  2426. // Capabilities
  2427. for (auto it = capabilities.cbegin(); it != capabilities.cend(); ++it) {
  2428. Instruction capInst(0, 0, OpCapability);
  2429. capInst.addImmediateOperand(*it);
  2430. capInst.dump(out);
  2431. }
  2432. for (auto it = extensions.cbegin(); it != extensions.cend(); ++it) {
  2433. Instruction extInst(0, 0, OpExtension);
  2434. extInst.addStringOperand(it->c_str());
  2435. extInst.dump(out);
  2436. }
  2437. dumpInstructions(out, imports);
  2438. Instruction memInst(0, 0, OpMemoryModel);
  2439. memInst.addImmediateOperand(addressModel);
  2440. memInst.addImmediateOperand(memoryModel);
  2441. memInst.dump(out);
  2442. // Instructions saved up while building:
  2443. dumpInstructions(out, entryPoints);
  2444. dumpInstructions(out, executionModes);
  2445. // Debug instructions
  2446. dumpInstructions(out, strings);
  2447. dumpSourceInstructions(out);
  2448. for (int e = 0; e < (int)sourceExtensions.size(); ++e) {
  2449. Instruction sourceExtInst(0, 0, OpSourceExtension);
  2450. sourceExtInst.addStringOperand(sourceExtensions[e]);
  2451. sourceExtInst.dump(out);
  2452. }
  2453. dumpInstructions(out, names);
  2454. dumpModuleProcesses(out);
  2455. // Annotation instructions
  2456. dumpInstructions(out, decorations);
  2457. dumpInstructions(out, constantsTypesGlobals);
  2458. dumpInstructions(out, externals);
  2459. // The functions
  2460. module.dump(out);
  2461. }
  2462. //
  2463. // Protected methods.
  2464. //
  2465. // Turn the described access chain in 'accessChain' into an instruction(s)
  2466. // computing its address. This *cannot* include complex swizzles, which must
  2467. // be handled after this is called.
  2468. //
  2469. // Can generate code.
  2470. Id Builder::collapseAccessChain()
  2471. {
  2472. assert(accessChain.isRValue == false);
  2473. // did we already emit an access chain for this?
  2474. if (accessChain.instr != NoResult)
  2475. return accessChain.instr;
  2476. // If we have a dynamic component, we can still transfer
  2477. // that into a final operand to the access chain. We need to remap the
  2478. // dynamic component through the swizzle to get a new dynamic component to
  2479. // update.
  2480. //
  2481. // This was not done in transferAccessChainSwizzle() because it might
  2482. // generate code.
  2483. remapDynamicSwizzle();
  2484. if (accessChain.component != NoResult) {
  2485. // transfer the dynamic component to the access chain
  2486. accessChain.indexChain.push_back(accessChain.component);
  2487. accessChain.component = NoResult;
  2488. }
  2489. // note that non-trivial swizzling is left pending
  2490. // do we have an access chain?
  2491. if (accessChain.indexChain.size() == 0)
  2492. return accessChain.base;
  2493. // emit the access chain
  2494. StorageClass storageClass = (StorageClass)module.getStorageClass(getTypeId(accessChain.base));
  2495. accessChain.instr = createAccessChain(storageClass, accessChain.base, accessChain.indexChain);
  2496. return accessChain.instr;
  2497. }
  2498. // For a dynamic component selection of a swizzle.
  2499. //
  2500. // Turn the swizzle and dynamic component into just a dynamic component.
  2501. //
  2502. // Generates code.
  2503. void Builder::remapDynamicSwizzle()
  2504. {
  2505. // do we have a swizzle to remap a dynamic component through?
  2506. if (accessChain.component != NoResult && accessChain.swizzle.size() > 1) {
  2507. // build a vector of the swizzle for the component to map into
  2508. std::vector<Id> components;
  2509. for (int c = 0; c < (int)accessChain.swizzle.size(); ++c)
  2510. components.push_back(makeUintConstant(accessChain.swizzle[c]));
  2511. Id mapType = makeVectorType(makeUintType(32), (int)accessChain.swizzle.size());
  2512. Id map = makeCompositeConstant(mapType, components);
  2513. // use it
  2514. accessChain.component = createVectorExtractDynamic(map, makeUintType(32), accessChain.component);
  2515. accessChain.swizzle.clear();
  2516. }
  2517. }
  2518. // clear out swizzle if it is redundant, that is reselecting the same components
  2519. // that would be present without the swizzle.
  2520. void Builder::simplifyAccessChainSwizzle()
  2521. {
  2522. // If the swizzle has fewer components than the vector, it is subsetting, and must stay
  2523. // to preserve that fact.
  2524. if (getNumTypeComponents(accessChain.preSwizzleBaseType) > (int)accessChain.swizzle.size())
  2525. return;
  2526. // if components are out of order, it is a swizzle
  2527. for (unsigned int i = 0; i < accessChain.swizzle.size(); ++i) {
  2528. if (i != accessChain.swizzle[i])
  2529. return;
  2530. }
  2531. // otherwise, there is no need to track this swizzle
  2532. accessChain.swizzle.clear();
  2533. if (accessChain.component == NoResult)
  2534. accessChain.preSwizzleBaseType = NoType;
  2535. }
  2536. // To the extent any swizzling can become part of the chain
  2537. // of accesses instead of a post operation, make it so.
  2538. // If 'dynamic' is true, include transferring the dynamic component,
  2539. // otherwise, leave it pending.
  2540. //
  2541. // Does not generate code. just updates the access chain.
  2542. void Builder::transferAccessChainSwizzle(bool dynamic)
  2543. {
  2544. // non existent?
  2545. if (accessChain.swizzle.size() == 0 && accessChain.component == NoResult)
  2546. return;
  2547. // too complex?
  2548. // (this requires either a swizzle, or generating code for a dynamic component)
  2549. if (accessChain.swizzle.size() > 1)
  2550. return;
  2551. // single component, either in the swizzle and/or dynamic component
  2552. if (accessChain.swizzle.size() == 1) {
  2553. assert(accessChain.component == NoResult);
  2554. // handle static component selection
  2555. accessChain.indexChain.push_back(makeUintConstant(accessChain.swizzle.front()));
  2556. accessChain.swizzle.clear();
  2557. accessChain.preSwizzleBaseType = NoType;
  2558. } else if (dynamic && accessChain.component != NoResult) {
  2559. assert(accessChain.swizzle.size() == 0);
  2560. // handle dynamic component
  2561. accessChain.indexChain.push_back(accessChain.component);
  2562. accessChain.preSwizzleBaseType = NoType;
  2563. accessChain.component = NoResult;
  2564. }
  2565. }
  2566. // Utility method for creating a new block and setting the insert point to
  2567. // be in it. This is useful for flow-control operations that need a "dummy"
  2568. // block proceeding them (e.g. instructions after a discard, etc).
  2569. void Builder::createAndSetNoPredecessorBlock(const char* /*name*/)
  2570. {
  2571. Block* block = new Block(getUniqueId(), buildPoint->getParent());
  2572. block->setUnreachable();
  2573. buildPoint->getParent().addBlock(block);
  2574. setBuildPoint(block);
  2575. // if (name)
  2576. // addName(block->getId(), name);
  2577. }
  2578. // Comments in header
  2579. void Builder::createBranch(Block* block)
  2580. {
  2581. Instruction* branch = new Instruction(OpBranch);
  2582. branch->addIdOperand(block->getId());
  2583. buildPoint->addInstruction(std::unique_ptr<Instruction>(branch));
  2584. block->addPredecessor(buildPoint);
  2585. }
  2586. void Builder::createSelectionMerge(Block* mergeBlock, unsigned int control)
  2587. {
  2588. Instruction* merge = new Instruction(OpSelectionMerge);
  2589. merge->addIdOperand(mergeBlock->getId());
  2590. merge->addImmediateOperand(control);
  2591. buildPoint->addInstruction(std::unique_ptr<Instruction>(merge));
  2592. }
  2593. void Builder::createLoopMerge(Block* mergeBlock, Block* continueBlock, unsigned int control,
  2594. const std::vector<unsigned int>& operands)
  2595. {
  2596. Instruction* merge = new Instruction(OpLoopMerge);
  2597. merge->addIdOperand(mergeBlock->getId());
  2598. merge->addIdOperand(continueBlock->getId());
  2599. merge->addImmediateOperand(control);
  2600. for (int op = 0; op < (int)operands.size(); ++op)
  2601. merge->addImmediateOperand(operands[op]);
  2602. buildPoint->addInstruction(std::unique_ptr<Instruction>(merge));
  2603. }
  2604. void Builder::createConditionalBranch(Id condition, Block* thenBlock, Block* elseBlock)
  2605. {
  2606. Instruction* branch = new Instruction(OpBranchConditional);
  2607. branch->addIdOperand(condition);
  2608. branch->addIdOperand(thenBlock->getId());
  2609. branch->addIdOperand(elseBlock->getId());
  2610. buildPoint->addInstruction(std::unique_ptr<Instruction>(branch));
  2611. thenBlock->addPredecessor(buildPoint);
  2612. elseBlock->addPredecessor(buildPoint);
  2613. }
  2614. // OpSource
  2615. // [OpSourceContinued]
  2616. // ...
  2617. void Builder::dumpSourceInstructions(const spv::Id fileId, const std::string& text,
  2618. std::vector<unsigned int>& out) const
  2619. {
  2620. const int maxWordCount = 0xFFFF;
  2621. const int opSourceWordCount = 4;
  2622. const int nonNullBytesPerInstruction = 4 * (maxWordCount - opSourceWordCount) - 1;
  2623. if (source != SourceLanguageUnknown) {
  2624. // OpSource Language Version File Source
  2625. Instruction sourceInst(NoResult, NoType, OpSource);
  2626. sourceInst.addImmediateOperand(source);
  2627. sourceInst.addImmediateOperand(sourceVersion);
  2628. // File operand
  2629. if (fileId != NoResult) {
  2630. sourceInst.addIdOperand(fileId);
  2631. // Source operand
  2632. if (text.size() > 0) {
  2633. int nextByte = 0;
  2634. std::string subString;
  2635. while ((int)text.size() - nextByte > 0) {
  2636. subString = text.substr(nextByte, nonNullBytesPerInstruction);
  2637. if (nextByte == 0) {
  2638. // OpSource
  2639. sourceInst.addStringOperand(subString.c_str());
  2640. sourceInst.dump(out);
  2641. } else {
  2642. // OpSourcContinued
  2643. Instruction sourceContinuedInst(OpSourceContinued);
  2644. sourceContinuedInst.addStringOperand(subString.c_str());
  2645. sourceContinuedInst.dump(out);
  2646. }
  2647. nextByte += nonNullBytesPerInstruction;
  2648. }
  2649. } else
  2650. sourceInst.dump(out);
  2651. } else
  2652. sourceInst.dump(out);
  2653. }
  2654. }
  2655. // Dump an OpSource[Continued] sequence for the source and every include file
  2656. void Builder::dumpSourceInstructions(std::vector<unsigned int>& out) const
  2657. {
  2658. dumpSourceInstructions(sourceFileStringId, sourceText, out);
  2659. for (auto iItr = includeFiles.begin(); iItr != includeFiles.end(); ++iItr)
  2660. dumpSourceInstructions(iItr->first, *iItr->second, out);
  2661. }
  2662. void Builder::dumpInstructions(std::vector<unsigned int>& out, const std::vector<std::unique_ptr<Instruction> >& instructions) const
  2663. {
  2664. for (int i = 0; i < (int)instructions.size(); ++i) {
  2665. instructions[i]->dump(out);
  2666. }
  2667. }
  2668. void Builder::dumpModuleProcesses(std::vector<unsigned int>& out) const
  2669. {
  2670. for (int i = 0; i < (int)moduleProcesses.size(); ++i) {
  2671. Instruction moduleProcessed(OpModuleProcessed);
  2672. moduleProcessed.addStringOperand(moduleProcesses[i]);
  2673. moduleProcessed.dump(out);
  2674. }
  2675. }
  2676. }; // end spv namespace