folding_rules.cpp 116 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149
  1. // Copyright (c) 2018 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "source/opt/folding_rules.h"
  15. #include <limits>
  16. #include <memory>
  17. #include <utility>
  18. #include "ir_builder.h"
  19. #include "source/latest_version_glsl_std_450_header.h"
  20. #include "source/opt/ir_context.h"
  21. namespace spvtools {
  22. namespace opt {
  23. namespace {
  24. constexpr uint32_t kExtractCompositeIdInIdx = 0;
  25. constexpr uint32_t kInsertObjectIdInIdx = 0;
  26. constexpr uint32_t kInsertCompositeIdInIdx = 1;
  27. constexpr uint32_t kExtInstSetIdInIdx = 0;
  28. constexpr uint32_t kExtInstInstructionInIdx = 1;
  29. constexpr uint32_t kFMixXIdInIdx = 2;
  30. constexpr uint32_t kFMixYIdInIdx = 3;
  31. constexpr uint32_t kFMixAIdInIdx = 4;
  32. constexpr uint32_t kStoreObjectInIdx = 1;
  33. // Some image instructions may contain an "image operands" argument.
  34. // Returns the operand index for the "image operands".
  35. // Returns -1 if the instruction does not have image operands.
  36. int32_t ImageOperandsMaskInOperandIndex(Instruction* inst) {
  37. const auto opcode = inst->opcode();
  38. switch (opcode) {
  39. case spv::Op::OpImageSampleImplicitLod:
  40. case spv::Op::OpImageSampleExplicitLod:
  41. case spv::Op::OpImageSampleProjImplicitLod:
  42. case spv::Op::OpImageSampleProjExplicitLod:
  43. case spv::Op::OpImageFetch:
  44. case spv::Op::OpImageRead:
  45. case spv::Op::OpImageSparseSampleImplicitLod:
  46. case spv::Op::OpImageSparseSampleExplicitLod:
  47. case spv::Op::OpImageSparseSampleProjImplicitLod:
  48. case spv::Op::OpImageSparseSampleProjExplicitLod:
  49. case spv::Op::OpImageSparseFetch:
  50. case spv::Op::OpImageSparseRead:
  51. return inst->NumOperands() > 4 ? 2 : -1;
  52. case spv::Op::OpImageSampleDrefImplicitLod:
  53. case spv::Op::OpImageSampleDrefExplicitLod:
  54. case spv::Op::OpImageSampleProjDrefImplicitLod:
  55. case spv::Op::OpImageSampleProjDrefExplicitLod:
  56. case spv::Op::OpImageGather:
  57. case spv::Op::OpImageDrefGather:
  58. case spv::Op::OpImageSparseSampleDrefImplicitLod:
  59. case spv::Op::OpImageSparseSampleDrefExplicitLod:
  60. case spv::Op::OpImageSparseSampleProjDrefImplicitLod:
  61. case spv::Op::OpImageSparseSampleProjDrefExplicitLod:
  62. case spv::Op::OpImageSparseGather:
  63. case spv::Op::OpImageSparseDrefGather:
  64. return inst->NumOperands() > 5 ? 3 : -1;
  65. case spv::Op::OpImageWrite:
  66. return inst->NumOperands() > 3 ? 3 : -1;
  67. default:
  68. return -1;
  69. }
  70. }
  71. // Returns the element width of |type|.
  72. uint32_t ElementWidth(const analysis::Type* type) {
  73. if (const analysis::CooperativeVectorNV* coopvec_type =
  74. type->AsCooperativeVectorNV()) {
  75. return ElementWidth(coopvec_type->component_type());
  76. } else if (const analysis::Vector* vec_type = type->AsVector()) {
  77. return ElementWidth(vec_type->element_type());
  78. } else if (const analysis::Float* float_type = type->AsFloat()) {
  79. return float_type->width();
  80. } else {
  81. assert(type->AsInteger());
  82. return type->AsInteger()->width();
  83. }
  84. }
  85. // Returns true if |type| is Float or a vector of Float.
  86. bool HasFloatingPoint(const analysis::Type* type) {
  87. if (type->AsFloat()) {
  88. return true;
  89. } else if (const analysis::Vector* vec_type = type->AsVector()) {
  90. return vec_type->element_type()->AsFloat() != nullptr;
  91. }
  92. return false;
  93. }
  94. // Returns false if |val| is NaN, infinite or subnormal.
  95. template <typename T>
  96. bool IsValidResult(T val) {
  97. int classified = std::fpclassify(val);
  98. switch (classified) {
  99. case FP_NAN:
  100. case FP_INFINITE:
  101. case FP_SUBNORMAL:
  102. return false;
  103. default:
  104. return true;
  105. }
  106. }
  107. // Returns true if `type` is a cooperative matrix.
  108. bool IsCooperativeMatrix(const analysis::Type* type) {
  109. return type->kind() == analysis::Type::kCooperativeMatrixKHR ||
  110. type->kind() == analysis::Type::kCooperativeMatrixNV;
  111. }
  112. const analysis::Constant* ConstInput(
  113. const std::vector<const analysis::Constant*>& constants) {
  114. return constants[0] ? constants[0] : constants[1];
  115. }
  116. Instruction* NonConstInput(IRContext* context, const analysis::Constant* c,
  117. Instruction* inst) {
  118. uint32_t in_op = c ? 1u : 0u;
  119. return context->get_def_use_mgr()->GetDef(
  120. inst->GetSingleWordInOperand(in_op));
  121. }
  122. std::vector<uint32_t> ExtractInts(uint64_t val) {
  123. std::vector<uint32_t> words;
  124. words.push_back(static_cast<uint32_t>(val));
  125. words.push_back(static_cast<uint32_t>(val >> 32));
  126. return words;
  127. }
  128. std::vector<uint32_t> GetWordsFromScalarIntConstant(
  129. const analysis::IntConstant* c) {
  130. assert(c != nullptr);
  131. uint32_t width = c->type()->AsInteger()->width();
  132. assert(width == 8 || width == 16 || width == 32 || width == 64);
  133. if (width == 64) {
  134. uint64_t uval = static_cast<uint64_t>(c->GetU64());
  135. return ExtractInts(uval);
  136. }
  137. // Section 2.2.1 of the SPIR-V spec guarantees that all integer types
  138. // smaller than 32-bits are automatically zero or sign extended to 32-bits.
  139. return {c->GetU32BitValue()};
  140. }
  141. std::vector<uint32_t> GetWordsFromScalarFloatConstant(
  142. const analysis::FloatConstant* c) {
  143. assert(c != nullptr);
  144. uint32_t width = c->type()->AsFloat()->width();
  145. assert(width == 16 || width == 32 || width == 64);
  146. if (width == 64) {
  147. utils::FloatProxy<double> result(c->GetDouble());
  148. return result.GetWords();
  149. }
  150. // Section 2.2.1 of the SPIR-V spec guarantees that all floating-point types
  151. // smaller than 32-bits are automatically zero extended to 32-bits.
  152. return {c->GetU32BitValue()};
  153. }
  154. std::vector<uint32_t> GetWordsFromNumericScalarOrVectorConstant(
  155. analysis::ConstantManager* const_mgr, const analysis::Constant* c) {
  156. if (const auto* float_constant = c->AsFloatConstant()) {
  157. return GetWordsFromScalarFloatConstant(float_constant);
  158. } else if (const auto* int_constant = c->AsIntConstant()) {
  159. return GetWordsFromScalarIntConstant(int_constant);
  160. } else if (const auto* vec_constant = c->AsVectorConstant()) {
  161. std::vector<uint32_t> words;
  162. for (const auto* comp : vec_constant->GetComponents()) {
  163. auto comp_in_words =
  164. GetWordsFromNumericScalarOrVectorConstant(const_mgr, comp);
  165. words.insert(words.end(), comp_in_words.begin(), comp_in_words.end());
  166. }
  167. return words;
  168. }
  169. return {};
  170. }
  171. const analysis::Constant* ConvertWordsToNumericScalarOrVectorConstant(
  172. analysis::ConstantManager* const_mgr, const std::vector<uint32_t>& words,
  173. const analysis::Type* type) {
  174. const spvtools::opt::analysis::Integer* int_type = type->AsInteger();
  175. if (int_type && int_type->width() <= 32) {
  176. assert(words.size() == 1);
  177. return const_mgr->GenerateIntegerConstant(int_type, words[0]);
  178. }
  179. if (int_type || type->AsFloat()) return const_mgr->GetConstant(type, words);
  180. if (const auto* vec_type = type->AsVector())
  181. return const_mgr->GetNumericVectorConstantWithWords(vec_type, words);
  182. return nullptr;
  183. }
  184. // Returns the negation of |c|. |c| must be a 32 or 64 bit floating point
  185. // constant.
  186. uint32_t NegateFloatingPointConstant(analysis::ConstantManager* const_mgr,
  187. const analysis::Constant* c) {
  188. assert(c);
  189. assert(c->type()->AsFloat());
  190. uint32_t width = c->type()->AsFloat()->width();
  191. assert(width == 32 || width == 64);
  192. std::vector<uint32_t> words;
  193. if (width == 64) {
  194. utils::FloatProxy<double> result(c->GetDouble() * -1.0);
  195. words = result.GetWords();
  196. } else {
  197. utils::FloatProxy<float> result(c->GetFloat() * -1.0f);
  198. words = result.GetWords();
  199. }
  200. const analysis::Constant* negated_const =
  201. const_mgr->GetConstant(c->type(), std::move(words));
  202. return const_mgr->GetDefiningInstruction(negated_const)->result_id();
  203. }
  204. // Negates the integer constant |c|. Returns the id of the defining instruction.
  205. uint32_t NegateIntegerConstant(analysis::ConstantManager* const_mgr,
  206. const analysis::Constant* c) {
  207. assert(c);
  208. assert(c->type()->AsInteger());
  209. uint32_t width = c->type()->AsInteger()->width();
  210. assert(width == 32 || width == 64);
  211. std::vector<uint32_t> words;
  212. if (width == 64) {
  213. uint64_t uval = static_cast<uint64_t>(0 - c->GetU64());
  214. words = ExtractInts(uval);
  215. } else {
  216. words.push_back(static_cast<uint32_t>(0 - c->GetU32()));
  217. }
  218. const analysis::Constant* negated_const =
  219. const_mgr->GetConstant(c->type(), std::move(words));
  220. return const_mgr->GetDefiningInstruction(negated_const)->result_id();
  221. }
  222. // Negates the vector constant |c|. Returns the id of the defining instruction.
  223. uint32_t NegateVectorConstant(analysis::ConstantManager* const_mgr,
  224. const analysis::Constant* c) {
  225. assert(const_mgr && c);
  226. assert(c->type()->AsVector());
  227. if (c->AsNullConstant()) {
  228. // 0.0 vs -0.0 shouldn't matter.
  229. return const_mgr->GetDefiningInstruction(c)->result_id();
  230. } else {
  231. const analysis::Type* component_type =
  232. c->AsVectorConstant()->component_type();
  233. std::vector<uint32_t> words;
  234. for (auto& comp : c->AsVectorConstant()->GetComponents()) {
  235. if (component_type->AsFloat()) {
  236. words.push_back(NegateFloatingPointConstant(const_mgr, comp));
  237. } else {
  238. assert(component_type->AsInteger());
  239. words.push_back(NegateIntegerConstant(const_mgr, comp));
  240. }
  241. }
  242. const analysis::Constant* negated_const =
  243. const_mgr->GetConstant(c->type(), std::move(words));
  244. return const_mgr->GetDefiningInstruction(negated_const)->result_id();
  245. }
  246. }
  247. // Negates |c|. Returns the id of the defining instruction.
  248. uint32_t NegateConstant(analysis::ConstantManager* const_mgr,
  249. const analysis::Constant* c) {
  250. if (c->type()->AsVector()) {
  251. return NegateVectorConstant(const_mgr, c);
  252. } else if (c->type()->AsFloat()) {
  253. return NegateFloatingPointConstant(const_mgr, c);
  254. } else {
  255. assert(c->type()->AsInteger());
  256. return NegateIntegerConstant(const_mgr, c);
  257. }
  258. }
  259. // Takes the reciprocal of |c|. |c|'s type must be Float or a vector of Float.
  260. // Returns 0 if the reciprocal is NaN, infinite or subnormal.
  261. uint32_t Reciprocal(analysis::ConstantManager* const_mgr,
  262. const analysis::Constant* c) {
  263. assert(const_mgr && c);
  264. assert(c->type()->AsFloat());
  265. uint32_t width = c->type()->AsFloat()->width();
  266. assert(width == 32 || width == 64);
  267. std::vector<uint32_t> words;
  268. if (c->IsZero()) {
  269. return 0;
  270. }
  271. if (width == 64) {
  272. spvtools::utils::FloatProxy<double> result(1.0 / c->GetDouble());
  273. if (!IsValidResult(result.getAsFloat())) return 0;
  274. words = result.GetWords();
  275. } else {
  276. spvtools::utils::FloatProxy<float> result(1.0f / c->GetFloat());
  277. if (!IsValidResult(result.getAsFloat())) return 0;
  278. words = result.GetWords();
  279. }
  280. const analysis::Constant* negated_const =
  281. const_mgr->GetConstant(c->type(), std::move(words));
  282. return const_mgr->GetDefiningInstruction(negated_const)->result_id();
  283. }
  284. // Replaces fdiv where second operand is constant with fmul.
  285. FoldingRule ReciprocalFDiv() {
  286. return [](IRContext* context, Instruction* inst,
  287. const std::vector<const analysis::Constant*>& constants) {
  288. assert(inst->opcode() == spv::Op::OpFDiv);
  289. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  290. const analysis::Type* type =
  291. context->get_type_mgr()->GetType(inst->type_id());
  292. if (IsCooperativeMatrix(type)) {
  293. return false;
  294. }
  295. if (!inst->IsFloatingPointFoldingAllowed()) return false;
  296. uint32_t width = ElementWidth(type);
  297. if (width != 32 && width != 64) return false;
  298. if (constants[1] != nullptr) {
  299. uint32_t id = 0;
  300. if (const analysis::VectorConstant* vector_const =
  301. constants[1]->AsVectorConstant()) {
  302. std::vector<uint32_t> neg_ids;
  303. for (auto& comp : vector_const->GetComponents()) {
  304. id = Reciprocal(const_mgr, comp);
  305. if (id == 0) return false;
  306. neg_ids.push_back(id);
  307. }
  308. const analysis::Constant* negated_const =
  309. const_mgr->GetConstant(constants[1]->type(), std::move(neg_ids));
  310. id = const_mgr->GetDefiningInstruction(negated_const)->result_id();
  311. } else if (constants[1]->AsFloatConstant()) {
  312. id = Reciprocal(const_mgr, constants[1]);
  313. if (id == 0) return false;
  314. } else {
  315. // Don't fold a null constant.
  316. return false;
  317. }
  318. inst->SetOpcode(spv::Op::OpFMul);
  319. inst->SetInOperands(
  320. {{SPV_OPERAND_TYPE_ID, {inst->GetSingleWordInOperand(0u)}},
  321. {SPV_OPERAND_TYPE_ID, {id}}});
  322. return true;
  323. }
  324. return false;
  325. };
  326. }
  327. // Elides consecutive negate instructions.
  328. FoldingRule MergeNegateArithmetic() {
  329. return [](IRContext* context, Instruction* inst,
  330. const std::vector<const analysis::Constant*>& constants) {
  331. assert(inst->opcode() == spv::Op::OpFNegate ||
  332. inst->opcode() == spv::Op::OpSNegate);
  333. (void)constants;
  334. const analysis::Type* type =
  335. context->get_type_mgr()->GetType(inst->type_id());
  336. if (HasFloatingPoint(type) && !inst->IsFloatingPointFoldingAllowed())
  337. return false;
  338. Instruction* op_inst =
  339. context->get_def_use_mgr()->GetDef(inst->GetSingleWordInOperand(0u));
  340. if (HasFloatingPoint(type) && !op_inst->IsFloatingPointFoldingAllowed())
  341. return false;
  342. if (op_inst->opcode() == inst->opcode()) {
  343. // Elide negates.
  344. inst->SetOpcode(spv::Op::OpCopyObject);
  345. inst->SetInOperands(
  346. {{SPV_OPERAND_TYPE_ID, {op_inst->GetSingleWordInOperand(0u)}}});
  347. return true;
  348. }
  349. return false;
  350. };
  351. }
  352. // Merges negate into a mul or div operation if that operation contains a
  353. // constant operand.
  354. // Cases:
  355. // -(x * 2) = x * -2
  356. // -(2 * x) = x * -2
  357. // -(x / 2) = x / -2
  358. // -(2 / x) = -2 / x
  359. FoldingRule MergeNegateMulDivArithmetic() {
  360. return [](IRContext* context, Instruction* inst,
  361. const std::vector<const analysis::Constant*>& constants) {
  362. assert(inst->opcode() == spv::Op::OpFNegate ||
  363. inst->opcode() == spv::Op::OpSNegate);
  364. (void)constants;
  365. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  366. const analysis::Type* type =
  367. context->get_type_mgr()->GetType(inst->type_id());
  368. if (IsCooperativeMatrix(type)) {
  369. return false;
  370. }
  371. if (HasFloatingPoint(type) && !inst->IsFloatingPointFoldingAllowed())
  372. return false;
  373. Instruction* op_inst =
  374. context->get_def_use_mgr()->GetDef(inst->GetSingleWordInOperand(0u));
  375. if (HasFloatingPoint(type) && !op_inst->IsFloatingPointFoldingAllowed())
  376. return false;
  377. uint32_t width = ElementWidth(type);
  378. if (width != 32 && width != 64) return false;
  379. spv::Op opcode = op_inst->opcode();
  380. if (opcode != spv::Op::OpFMul && opcode != spv::Op::OpFDiv &&
  381. opcode != spv::Op::OpIMul && opcode != spv::Op::OpSDiv) {
  382. return false;
  383. }
  384. std::vector<const analysis::Constant*> op_constants =
  385. const_mgr->GetOperandConstants(op_inst);
  386. // Merge negate into mul or div if one operand is constant.
  387. if (op_constants[0] == nullptr && op_constants[1] == nullptr) {
  388. return false;
  389. }
  390. bool zero_is_variable = op_constants[0] == nullptr;
  391. const analysis::Constant* c = ConstInput(op_constants);
  392. uint32_t neg_id = NegateConstant(const_mgr, c);
  393. uint32_t non_const_id = zero_is_variable
  394. ? op_inst->GetSingleWordInOperand(0u)
  395. : op_inst->GetSingleWordInOperand(1u);
  396. // Change this instruction to a mul/div.
  397. inst->SetOpcode(op_inst->opcode());
  398. if (opcode == spv::Op::OpFDiv || opcode == spv::Op::OpUDiv ||
  399. opcode == spv::Op::OpSDiv) {
  400. uint32_t op0 = zero_is_variable ? non_const_id : neg_id;
  401. uint32_t op1 = zero_is_variable ? neg_id : non_const_id;
  402. inst->SetInOperands(
  403. {{SPV_OPERAND_TYPE_ID, {op0}}, {SPV_OPERAND_TYPE_ID, {op1}}});
  404. } else {
  405. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {non_const_id}},
  406. {SPV_OPERAND_TYPE_ID, {neg_id}}});
  407. }
  408. return true;
  409. };
  410. }
  411. // Merges negate into a add or sub operation if that operation contains a
  412. // constant operand.
  413. // Cases:
  414. // -(x + 2) = -2 - x
  415. // -(2 + x) = -2 - x
  416. // -(x - 2) = 2 - x
  417. // -(2 - x) = x - 2
  418. FoldingRule MergeNegateAddSubArithmetic() {
  419. return [](IRContext* context, Instruction* inst,
  420. const std::vector<const analysis::Constant*>& constants) {
  421. assert(inst->opcode() == spv::Op::OpFNegate ||
  422. inst->opcode() == spv::Op::OpSNegate);
  423. (void)constants;
  424. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  425. const analysis::Type* type =
  426. context->get_type_mgr()->GetType(inst->type_id());
  427. if (IsCooperativeMatrix(type)) {
  428. return false;
  429. }
  430. if (HasFloatingPoint(type) && !inst->IsFloatingPointFoldingAllowed())
  431. return false;
  432. Instruction* op_inst =
  433. context->get_def_use_mgr()->GetDef(inst->GetSingleWordInOperand(0u));
  434. if (HasFloatingPoint(type) && !op_inst->IsFloatingPointFoldingAllowed())
  435. return false;
  436. uint32_t width = ElementWidth(type);
  437. if (width != 32 && width != 64) return false;
  438. if (op_inst->opcode() == spv::Op::OpFAdd ||
  439. op_inst->opcode() == spv::Op::OpFSub ||
  440. op_inst->opcode() == spv::Op::OpIAdd ||
  441. op_inst->opcode() == spv::Op::OpISub) {
  442. std::vector<const analysis::Constant*> op_constants =
  443. const_mgr->GetOperandConstants(op_inst);
  444. if (op_constants[0] || op_constants[1]) {
  445. bool zero_is_variable = op_constants[0] == nullptr;
  446. bool is_add = (op_inst->opcode() == spv::Op::OpFAdd) ||
  447. (op_inst->opcode() == spv::Op::OpIAdd);
  448. bool swap_operands = !is_add || zero_is_variable;
  449. bool negate_const = is_add;
  450. const analysis::Constant* c = ConstInput(op_constants);
  451. uint32_t const_id = 0;
  452. if (negate_const) {
  453. const_id = NegateConstant(const_mgr, c);
  454. } else {
  455. const_id = zero_is_variable ? op_inst->GetSingleWordInOperand(1u)
  456. : op_inst->GetSingleWordInOperand(0u);
  457. }
  458. // Swap operands if necessary and make the instruction a subtraction.
  459. uint32_t op0 =
  460. zero_is_variable ? op_inst->GetSingleWordInOperand(0u) : const_id;
  461. uint32_t op1 =
  462. zero_is_variable ? const_id : op_inst->GetSingleWordInOperand(1u);
  463. if (swap_operands) std::swap(op0, op1);
  464. inst->SetOpcode(HasFloatingPoint(type) ? spv::Op::OpFSub
  465. : spv::Op::OpISub);
  466. inst->SetInOperands(
  467. {{SPV_OPERAND_TYPE_ID, {op0}}, {SPV_OPERAND_TYPE_ID, {op1}}});
  468. return true;
  469. }
  470. }
  471. return false;
  472. };
  473. }
  474. // Returns true if |c| has a zero element.
  475. bool HasZero(const analysis::Constant* c) {
  476. if (c->AsNullConstant()) {
  477. return true;
  478. }
  479. if (const analysis::VectorConstant* vec_const = c->AsVectorConstant()) {
  480. for (auto& comp : vec_const->GetComponents())
  481. if (HasZero(comp)) return true;
  482. } else {
  483. assert(c->AsScalarConstant());
  484. return c->AsScalarConstant()->IsZero();
  485. }
  486. return false;
  487. }
  488. // Performs |input1| |opcode| |input2| and returns the merged constant result
  489. // id. Returns 0 if the result is not a valid value. The input types must be
  490. // Float.
  491. uint32_t PerformFloatingPointOperation(analysis::ConstantManager* const_mgr,
  492. spv::Op opcode,
  493. const analysis::Constant* input1,
  494. const analysis::Constant* input2) {
  495. const analysis::Type* type = input1->type();
  496. assert(type->AsFloat());
  497. uint32_t width = type->AsFloat()->width();
  498. assert(width == 32 || width == 64);
  499. std::vector<uint32_t> words;
  500. #define FOLD_OP(op) \
  501. if (width == 64) { \
  502. utils::FloatProxy<double> val = \
  503. input1->GetDouble() op input2->GetDouble(); \
  504. double dval = val.getAsFloat(); \
  505. if (!IsValidResult(dval)) return 0; \
  506. words = val.GetWords(); \
  507. } else { \
  508. utils::FloatProxy<float> val = input1->GetFloat() op input2->GetFloat(); \
  509. float fval = val.getAsFloat(); \
  510. if (!IsValidResult(fval)) return 0; \
  511. words = val.GetWords(); \
  512. } \
  513. static_assert(true, "require extra semicolon")
  514. switch (opcode) {
  515. case spv::Op::OpFMul:
  516. FOLD_OP(*);
  517. break;
  518. case spv::Op::OpFDiv:
  519. if (HasZero(input2)) return 0;
  520. FOLD_OP(/);
  521. break;
  522. case spv::Op::OpFAdd:
  523. FOLD_OP(+);
  524. break;
  525. case spv::Op::OpFSub:
  526. FOLD_OP(-);
  527. break;
  528. default:
  529. assert(false && "Unexpected operation");
  530. break;
  531. }
  532. #undef FOLD_OP
  533. const analysis::Constant* merged_const = const_mgr->GetConstant(type, words);
  534. return const_mgr->GetDefiningInstruction(merged_const)->result_id();
  535. }
  536. // Performs |input1| |opcode| |input2| and returns the merged constant result
  537. // id. Returns 0 if the result is not a valid value. The input types must be
  538. // Integers.
  539. uint32_t PerformIntegerOperation(analysis::ConstantManager* const_mgr,
  540. spv::Op opcode,
  541. const analysis::Constant* input1,
  542. const analysis::Constant* input2) {
  543. assert(input1->type()->AsInteger());
  544. const analysis::Integer* type = input1->type()->AsInteger();
  545. uint32_t width = type->AsInteger()->width();
  546. assert(width == 32 || width == 64);
  547. std::vector<uint32_t> words;
  548. // Regardless of the sign of the constant, folding is performed on an unsigned
  549. // interpretation of the constant data. This avoids signed integer overflow
  550. // while folding, and works because sign is irrelevant for the IAdd, ISub and
  551. // IMul instructions.
  552. #define FOLD_OP(op) \
  553. if (width == 64) { \
  554. uint64_t val = input1->GetU64() op input2->GetU64(); \
  555. words = ExtractInts(val); \
  556. } else { \
  557. uint32_t val = input1->GetU32() op input2->GetU32(); \
  558. words.push_back(val); \
  559. } \
  560. static_assert(true, "require extra semicolon")
  561. switch (opcode) {
  562. case spv::Op::OpIMul:
  563. FOLD_OP(*);
  564. break;
  565. case spv::Op::OpSDiv:
  566. case spv::Op::OpUDiv:
  567. assert(false && "Should not merge integer division");
  568. break;
  569. case spv::Op::OpIAdd:
  570. FOLD_OP(+);
  571. break;
  572. case spv::Op::OpISub:
  573. FOLD_OP(-);
  574. break;
  575. default:
  576. assert(false && "Unexpected operation");
  577. break;
  578. }
  579. #undef FOLD_OP
  580. const analysis::Constant* merged_const = const_mgr->GetConstant(type, words);
  581. return const_mgr->GetDefiningInstruction(merged_const)->result_id();
  582. }
  583. // Performs |input1| |opcode| |input2| and returns the merged constant result
  584. // id. Returns 0 if the result is not a valid value. The input types must be
  585. // Integers, Floats or Vectors of such.
  586. uint32_t PerformOperation(analysis::ConstantManager* const_mgr, spv::Op opcode,
  587. const analysis::Constant* input1,
  588. const analysis::Constant* input2) {
  589. assert(input1 && input2);
  590. const analysis::Type* type = input1->type();
  591. std::vector<uint32_t> words;
  592. if (const analysis::Vector* vector_type = type->AsVector()) {
  593. const analysis::Type* ele_type = vector_type->element_type();
  594. for (uint32_t i = 0; i != vector_type->element_count(); ++i) {
  595. uint32_t id = 0;
  596. const analysis::Constant* input1_comp = nullptr;
  597. if (const analysis::VectorConstant* input1_vector =
  598. input1->AsVectorConstant()) {
  599. input1_comp = input1_vector->GetComponents()[i];
  600. } else {
  601. assert(input1->AsNullConstant());
  602. input1_comp = const_mgr->GetConstant(ele_type, {});
  603. }
  604. const analysis::Constant* input2_comp = nullptr;
  605. if (const analysis::VectorConstant* input2_vector =
  606. input2->AsVectorConstant()) {
  607. input2_comp = input2_vector->GetComponents()[i];
  608. } else {
  609. assert(input2->AsNullConstant());
  610. input2_comp = const_mgr->GetConstant(ele_type, {});
  611. }
  612. if (ele_type->AsFloat()) {
  613. id = PerformFloatingPointOperation(const_mgr, opcode, input1_comp,
  614. input2_comp);
  615. } else {
  616. assert(ele_type->AsInteger());
  617. id = PerformIntegerOperation(const_mgr, opcode, input1_comp,
  618. input2_comp);
  619. }
  620. if (id == 0) return 0;
  621. words.push_back(id);
  622. }
  623. const analysis::Constant* merged_const =
  624. const_mgr->GetConstant(type, words);
  625. return const_mgr->GetDefiningInstruction(merged_const)->result_id();
  626. } else if (type->AsFloat()) {
  627. return PerformFloatingPointOperation(const_mgr, opcode, input1, input2);
  628. } else {
  629. assert(type->AsInteger());
  630. return PerformIntegerOperation(const_mgr, opcode, input1, input2);
  631. }
  632. }
  633. // Merges consecutive multiplies where each contains one constant operand.
  634. // Cases:
  635. // 2 * (x * 2) = x * 4
  636. // 2 * (2 * x) = x * 4
  637. // (x * 2) * 2 = x * 4
  638. // (2 * x) * 2 = x * 4
  639. FoldingRule MergeMulMulArithmetic() {
  640. return [](IRContext* context, Instruction* inst,
  641. const std::vector<const analysis::Constant*>& constants) {
  642. assert(inst->opcode() == spv::Op::OpFMul ||
  643. inst->opcode() == spv::Op::OpIMul);
  644. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  645. const analysis::Type* type =
  646. context->get_type_mgr()->GetType(inst->type_id());
  647. if (IsCooperativeMatrix(type)) {
  648. return false;
  649. }
  650. if (HasFloatingPoint(type) && !inst->IsFloatingPointFoldingAllowed())
  651. return false;
  652. uint32_t width = ElementWidth(type);
  653. if (width != 32 && width != 64) return false;
  654. // Determine the constant input and the variable input in |inst|.
  655. const analysis::Constant* const_input1 = ConstInput(constants);
  656. if (!const_input1) return false;
  657. Instruction* other_inst = NonConstInput(context, constants[0], inst);
  658. if (HasFloatingPoint(type) && !other_inst->IsFloatingPointFoldingAllowed())
  659. return false;
  660. if (other_inst->opcode() == inst->opcode()) {
  661. std::vector<const analysis::Constant*> other_constants =
  662. const_mgr->GetOperandConstants(other_inst);
  663. const analysis::Constant* const_input2 = ConstInput(other_constants);
  664. if (!const_input2) return false;
  665. bool other_first_is_variable = other_constants[0] == nullptr;
  666. uint32_t merged_id = PerformOperation(const_mgr, inst->opcode(),
  667. const_input1, const_input2);
  668. if (merged_id == 0) return false;
  669. uint32_t non_const_id = other_first_is_variable
  670. ? other_inst->GetSingleWordInOperand(0u)
  671. : other_inst->GetSingleWordInOperand(1u);
  672. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {non_const_id}},
  673. {SPV_OPERAND_TYPE_ID, {merged_id}}});
  674. return true;
  675. }
  676. return false;
  677. };
  678. }
  679. // Merges divides into subsequent multiplies if each instruction contains one
  680. // constant operand. Does not support integer operations.
  681. // Cases:
  682. // 2 * (x / 2) = x * 1
  683. // 2 * (2 / x) = 4 / x
  684. // (x / 2) * 2 = x * 1
  685. // (2 / x) * 2 = 4 / x
  686. // (y / x) * x = y
  687. // x * (y / x) = y
  688. FoldingRule MergeMulDivArithmetic() {
  689. return [](IRContext* context, Instruction* inst,
  690. const std::vector<const analysis::Constant*>& constants) {
  691. assert(inst->opcode() == spv::Op::OpFMul);
  692. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  693. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  694. const analysis::Type* type =
  695. context->get_type_mgr()->GetType(inst->type_id());
  696. if (IsCooperativeMatrix(type)) {
  697. return false;
  698. }
  699. if (!inst->IsFloatingPointFoldingAllowed()) return false;
  700. uint32_t width = ElementWidth(type);
  701. if (width != 32 && width != 64) return false;
  702. for (uint32_t i = 0; i < 2; i++) {
  703. uint32_t op_id = inst->GetSingleWordInOperand(i);
  704. Instruction* op_inst = def_use_mgr->GetDef(op_id);
  705. if (op_inst->opcode() == spv::Op::OpFDiv) {
  706. if (op_inst->GetSingleWordInOperand(1) ==
  707. inst->GetSingleWordInOperand(1 - i)) {
  708. inst->SetOpcode(spv::Op::OpCopyObject);
  709. inst->SetInOperands(
  710. {{SPV_OPERAND_TYPE_ID, {op_inst->GetSingleWordInOperand(0)}}});
  711. return true;
  712. }
  713. }
  714. }
  715. const analysis::Constant* const_input1 = ConstInput(constants);
  716. if (!const_input1) return false;
  717. Instruction* other_inst = NonConstInput(context, constants[0], inst);
  718. if (!other_inst->IsFloatingPointFoldingAllowed()) return false;
  719. if (other_inst->opcode() == spv::Op::OpFDiv) {
  720. std::vector<const analysis::Constant*> other_constants =
  721. const_mgr->GetOperandConstants(other_inst);
  722. const analysis::Constant* const_input2 = ConstInput(other_constants);
  723. if (!const_input2 || HasZero(const_input2)) return false;
  724. bool other_first_is_variable = other_constants[0] == nullptr;
  725. // If the variable value is the second operand of the divide, multiply
  726. // the constants together. Otherwise divide the constants.
  727. uint32_t merged_id = PerformOperation(
  728. const_mgr,
  729. other_first_is_variable ? other_inst->opcode() : inst->opcode(),
  730. const_input1, const_input2);
  731. if (merged_id == 0) return false;
  732. uint32_t non_const_id = other_first_is_variable
  733. ? other_inst->GetSingleWordInOperand(0u)
  734. : other_inst->GetSingleWordInOperand(1u);
  735. // If the variable value is on the second operand of the div, then this
  736. // operation is a div. Otherwise it should be a multiply.
  737. inst->SetOpcode(other_first_is_variable ? inst->opcode()
  738. : other_inst->opcode());
  739. if (other_first_is_variable) {
  740. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {non_const_id}},
  741. {SPV_OPERAND_TYPE_ID, {merged_id}}});
  742. } else {
  743. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {merged_id}},
  744. {SPV_OPERAND_TYPE_ID, {non_const_id}}});
  745. }
  746. return true;
  747. }
  748. return false;
  749. };
  750. }
  751. // Merges multiply of constant and negation.
  752. // Cases:
  753. // (-x) * 2 = x * -2
  754. // 2 * (-x) = x * -2
  755. FoldingRule MergeMulNegateArithmetic() {
  756. return [](IRContext* context, Instruction* inst,
  757. const std::vector<const analysis::Constant*>& constants) {
  758. assert(inst->opcode() == spv::Op::OpFMul ||
  759. inst->opcode() == spv::Op::OpIMul);
  760. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  761. const analysis::Type* type =
  762. context->get_type_mgr()->GetType(inst->type_id());
  763. if (IsCooperativeMatrix(type)) {
  764. return false;
  765. }
  766. bool uses_float = HasFloatingPoint(type);
  767. if (uses_float && !inst->IsFloatingPointFoldingAllowed()) return false;
  768. uint32_t width = ElementWidth(type);
  769. if (width != 32 && width != 64) return false;
  770. const analysis::Constant* const_input1 = ConstInput(constants);
  771. if (!const_input1) return false;
  772. Instruction* other_inst = NonConstInput(context, constants[0], inst);
  773. if (uses_float && !other_inst->IsFloatingPointFoldingAllowed())
  774. return false;
  775. if (other_inst->opcode() == spv::Op::OpFNegate ||
  776. other_inst->opcode() == spv::Op::OpSNegate) {
  777. uint32_t neg_id = NegateConstant(const_mgr, const_input1);
  778. inst->SetInOperands(
  779. {{SPV_OPERAND_TYPE_ID, {other_inst->GetSingleWordInOperand(0u)}},
  780. {SPV_OPERAND_TYPE_ID, {neg_id}}});
  781. return true;
  782. }
  783. return false;
  784. };
  785. }
  786. // Merges consecutive divides if each instruction contains one constant operand.
  787. // Does not support integer division.
  788. // Cases:
  789. // 2 / (x / 2) = 4 / x
  790. // 4 / (2 / x) = 2 * x
  791. // (4 / x) / 2 = 2 / x
  792. // (x / 2) / 2 = x / 4
  793. FoldingRule MergeDivDivArithmetic() {
  794. return [](IRContext* context, Instruction* inst,
  795. const std::vector<const analysis::Constant*>& constants) {
  796. assert(inst->opcode() == spv::Op::OpFDiv);
  797. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  798. const analysis::Type* type =
  799. context->get_type_mgr()->GetType(inst->type_id());
  800. if (IsCooperativeMatrix(type)) {
  801. return false;
  802. }
  803. if (!inst->IsFloatingPointFoldingAllowed()) return false;
  804. uint32_t width = ElementWidth(type);
  805. if (width != 32 && width != 64) return false;
  806. const analysis::Constant* const_input1 = ConstInput(constants);
  807. if (!const_input1 || HasZero(const_input1)) return false;
  808. Instruction* other_inst = NonConstInput(context, constants[0], inst);
  809. if (!other_inst->IsFloatingPointFoldingAllowed()) return false;
  810. bool first_is_variable = constants[0] == nullptr;
  811. if (other_inst->opcode() == inst->opcode()) {
  812. std::vector<const analysis::Constant*> other_constants =
  813. const_mgr->GetOperandConstants(other_inst);
  814. const analysis::Constant* const_input2 = ConstInput(other_constants);
  815. if (!const_input2 || HasZero(const_input2)) return false;
  816. bool other_first_is_variable = other_constants[0] == nullptr;
  817. spv::Op merge_op = inst->opcode();
  818. if (other_first_is_variable) {
  819. // Constants magnify.
  820. merge_op = spv::Op::OpFMul;
  821. }
  822. // This is an x / (*) case. Swap the inputs. Doesn't harm multiply
  823. // because it is commutative.
  824. if (first_is_variable) std::swap(const_input1, const_input2);
  825. uint32_t merged_id =
  826. PerformOperation(const_mgr, merge_op, const_input1, const_input2);
  827. if (merged_id == 0) return false;
  828. uint32_t non_const_id = other_first_is_variable
  829. ? other_inst->GetSingleWordInOperand(0u)
  830. : other_inst->GetSingleWordInOperand(1u);
  831. spv::Op op = inst->opcode();
  832. if (!first_is_variable && !other_first_is_variable) {
  833. // Effectively div of 1/x, so change to multiply.
  834. op = spv::Op::OpFMul;
  835. }
  836. uint32_t op1 = merged_id;
  837. uint32_t op2 = non_const_id;
  838. if (first_is_variable && other_first_is_variable) std::swap(op1, op2);
  839. inst->SetOpcode(op);
  840. inst->SetInOperands(
  841. {{SPV_OPERAND_TYPE_ID, {op1}}, {SPV_OPERAND_TYPE_ID, {op2}}});
  842. return true;
  843. }
  844. return false;
  845. };
  846. }
  847. // Fold multiplies succeeded by divides where each instruction contains a
  848. // constant operand. Does not support integer divide.
  849. // Cases:
  850. // 4 / (x * 2) = 2 / x
  851. // 4 / (2 * x) = 2 / x
  852. // (x * 4) / 2 = x * 2
  853. // (4 * x) / 2 = x * 2
  854. // (x * y) / x = y
  855. // (y * x) / x = y
  856. FoldingRule MergeDivMulArithmetic() {
  857. return [](IRContext* context, Instruction* inst,
  858. const std::vector<const analysis::Constant*>& constants) {
  859. assert(inst->opcode() == spv::Op::OpFDiv);
  860. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  861. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  862. const analysis::Type* type =
  863. context->get_type_mgr()->GetType(inst->type_id());
  864. if (IsCooperativeMatrix(type)) {
  865. return false;
  866. }
  867. if (!inst->IsFloatingPointFoldingAllowed()) return false;
  868. uint32_t width = ElementWidth(type);
  869. if (width != 32 && width != 64) return false;
  870. uint32_t op_id = inst->GetSingleWordInOperand(0);
  871. Instruction* op_inst = def_use_mgr->GetDef(op_id);
  872. if (op_inst->opcode() == spv::Op::OpFMul) {
  873. for (uint32_t i = 0; i < 2; i++) {
  874. if (op_inst->GetSingleWordInOperand(i) ==
  875. inst->GetSingleWordInOperand(1)) {
  876. inst->SetOpcode(spv::Op::OpCopyObject);
  877. inst->SetInOperands({{SPV_OPERAND_TYPE_ID,
  878. {op_inst->GetSingleWordInOperand(1 - i)}}});
  879. return true;
  880. }
  881. }
  882. }
  883. const analysis::Constant* const_input1 = ConstInput(constants);
  884. if (!const_input1 || HasZero(const_input1)) return false;
  885. Instruction* other_inst = NonConstInput(context, constants[0], inst);
  886. if (!other_inst->IsFloatingPointFoldingAllowed()) return false;
  887. bool first_is_variable = constants[0] == nullptr;
  888. if (other_inst->opcode() == spv::Op::OpFMul) {
  889. std::vector<const analysis::Constant*> other_constants =
  890. const_mgr->GetOperandConstants(other_inst);
  891. const analysis::Constant* const_input2 = ConstInput(other_constants);
  892. if (!const_input2) return false;
  893. bool other_first_is_variable = other_constants[0] == nullptr;
  894. // This is an x / (*) case. Swap the inputs.
  895. if (first_is_variable) std::swap(const_input1, const_input2);
  896. uint32_t merged_id = PerformOperation(const_mgr, inst->opcode(),
  897. const_input1, const_input2);
  898. if (merged_id == 0) return false;
  899. uint32_t non_const_id = other_first_is_variable
  900. ? other_inst->GetSingleWordInOperand(0u)
  901. : other_inst->GetSingleWordInOperand(1u);
  902. uint32_t op1 = merged_id;
  903. uint32_t op2 = non_const_id;
  904. if (first_is_variable) std::swap(op1, op2);
  905. // Convert to multiply
  906. if (first_is_variable) inst->SetOpcode(other_inst->opcode());
  907. inst->SetInOperands(
  908. {{SPV_OPERAND_TYPE_ID, {op1}}, {SPV_OPERAND_TYPE_ID, {op2}}});
  909. return true;
  910. }
  911. return false;
  912. };
  913. }
  914. // Fold divides of a constant and a negation.
  915. // Cases:
  916. // (-x) / 2 = x / -2
  917. // 2 / (-x) = -2 / x
  918. FoldingRule MergeDivNegateArithmetic() {
  919. return [](IRContext* context, Instruction* inst,
  920. const std::vector<const analysis::Constant*>& constants) {
  921. assert(inst->opcode() == spv::Op::OpFDiv);
  922. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  923. if (!inst->IsFloatingPointFoldingAllowed()) return false;
  924. const analysis::Constant* const_input1 = ConstInput(constants);
  925. if (!const_input1) return false;
  926. Instruction* other_inst = NonConstInput(context, constants[0], inst);
  927. if (!other_inst->IsFloatingPointFoldingAllowed()) return false;
  928. bool first_is_variable = constants[0] == nullptr;
  929. if (other_inst->opcode() == spv::Op::OpFNegate) {
  930. uint32_t neg_id = NegateConstant(const_mgr, const_input1);
  931. if (first_is_variable) {
  932. inst->SetInOperands(
  933. {{SPV_OPERAND_TYPE_ID, {other_inst->GetSingleWordInOperand(0u)}},
  934. {SPV_OPERAND_TYPE_ID, {neg_id}}});
  935. } else {
  936. inst->SetInOperands(
  937. {{SPV_OPERAND_TYPE_ID, {neg_id}},
  938. {SPV_OPERAND_TYPE_ID, {other_inst->GetSingleWordInOperand(0u)}}});
  939. }
  940. return true;
  941. }
  942. return false;
  943. };
  944. }
  945. // Folds addition of a constant and a negation.
  946. // Cases:
  947. // (-x) + 2 = 2 - x
  948. // 2 + (-x) = 2 - x
  949. FoldingRule MergeAddNegateArithmetic() {
  950. return [](IRContext* context, Instruction* inst,
  951. const std::vector<const analysis::Constant*>& constants) {
  952. assert(inst->opcode() == spv::Op::OpFAdd ||
  953. inst->opcode() == spv::Op::OpIAdd);
  954. const analysis::Type* type =
  955. context->get_type_mgr()->GetType(inst->type_id());
  956. bool uses_float = HasFloatingPoint(type);
  957. if (uses_float && !inst->IsFloatingPointFoldingAllowed()) return false;
  958. const analysis::Constant* const_input1 = ConstInput(constants);
  959. if (!const_input1) return false;
  960. Instruction* other_inst = NonConstInput(context, constants[0], inst);
  961. if (uses_float && !other_inst->IsFloatingPointFoldingAllowed())
  962. return false;
  963. if (other_inst->opcode() == spv::Op::OpSNegate ||
  964. other_inst->opcode() == spv::Op::OpFNegate) {
  965. inst->SetOpcode(HasFloatingPoint(type) ? spv::Op::OpFSub
  966. : spv::Op::OpISub);
  967. uint32_t const_id = constants[0] ? inst->GetSingleWordInOperand(0u)
  968. : inst->GetSingleWordInOperand(1u);
  969. inst->SetInOperands(
  970. {{SPV_OPERAND_TYPE_ID, {const_id}},
  971. {SPV_OPERAND_TYPE_ID, {other_inst->GetSingleWordInOperand(0u)}}});
  972. return true;
  973. }
  974. return false;
  975. };
  976. }
  977. // Folds subtraction of a constant and a negation.
  978. // Cases:
  979. // (-x) - 2 = -2 - x
  980. // 2 - (-x) = x + 2
  981. FoldingRule MergeSubNegateArithmetic() {
  982. return [](IRContext* context, Instruction* inst,
  983. const std::vector<const analysis::Constant*>& constants) {
  984. assert(inst->opcode() == spv::Op::OpFSub ||
  985. inst->opcode() == spv::Op::OpISub);
  986. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  987. const analysis::Type* type =
  988. context->get_type_mgr()->GetType(inst->type_id());
  989. if (IsCooperativeMatrix(type)) {
  990. return false;
  991. }
  992. bool uses_float = HasFloatingPoint(type);
  993. if (uses_float && !inst->IsFloatingPointFoldingAllowed()) return false;
  994. uint32_t width = ElementWidth(type);
  995. if (width != 32 && width != 64) return false;
  996. const analysis::Constant* const_input1 = ConstInput(constants);
  997. if (!const_input1) return false;
  998. Instruction* other_inst = NonConstInput(context, constants[0], inst);
  999. if (uses_float && !other_inst->IsFloatingPointFoldingAllowed())
  1000. return false;
  1001. if (other_inst->opcode() == spv::Op::OpSNegate ||
  1002. other_inst->opcode() == spv::Op::OpFNegate) {
  1003. uint32_t op1 = 0;
  1004. uint32_t op2 = 0;
  1005. spv::Op opcode = inst->opcode();
  1006. if (constants[0] != nullptr) {
  1007. op1 = other_inst->GetSingleWordInOperand(0u);
  1008. op2 = inst->GetSingleWordInOperand(0u);
  1009. opcode = HasFloatingPoint(type) ? spv::Op::OpFAdd : spv::Op::OpIAdd;
  1010. } else {
  1011. op1 = NegateConstant(const_mgr, const_input1);
  1012. op2 = other_inst->GetSingleWordInOperand(0u);
  1013. }
  1014. inst->SetOpcode(opcode);
  1015. inst->SetInOperands(
  1016. {{SPV_OPERAND_TYPE_ID, {op1}}, {SPV_OPERAND_TYPE_ID, {op2}}});
  1017. return true;
  1018. }
  1019. return false;
  1020. };
  1021. }
  1022. // Folds addition of an addition where each operation has a constant operand.
  1023. // Cases:
  1024. // (x + 2) + 2 = x + 4
  1025. // (2 + x) + 2 = x + 4
  1026. // 2 + (x + 2) = x + 4
  1027. // 2 + (2 + x) = x + 4
  1028. FoldingRule MergeAddAddArithmetic() {
  1029. return [](IRContext* context, Instruction* inst,
  1030. const std::vector<const analysis::Constant*>& constants) {
  1031. assert(inst->opcode() == spv::Op::OpFAdd ||
  1032. inst->opcode() == spv::Op::OpIAdd);
  1033. const analysis::Type* type =
  1034. context->get_type_mgr()->GetType(inst->type_id());
  1035. if (IsCooperativeMatrix(type)) {
  1036. return false;
  1037. }
  1038. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  1039. bool uses_float = HasFloatingPoint(type);
  1040. if (uses_float && !inst->IsFloatingPointFoldingAllowed()) return false;
  1041. uint32_t width = ElementWidth(type);
  1042. if (width != 32 && width != 64) return false;
  1043. const analysis::Constant* const_input1 = ConstInput(constants);
  1044. if (!const_input1) return false;
  1045. Instruction* other_inst = NonConstInput(context, constants[0], inst);
  1046. if (uses_float && !other_inst->IsFloatingPointFoldingAllowed())
  1047. return false;
  1048. if (other_inst->opcode() == spv::Op::OpFAdd ||
  1049. other_inst->opcode() == spv::Op::OpIAdd) {
  1050. std::vector<const analysis::Constant*> other_constants =
  1051. const_mgr->GetOperandConstants(other_inst);
  1052. const analysis::Constant* const_input2 = ConstInput(other_constants);
  1053. if (!const_input2) return false;
  1054. Instruction* non_const_input =
  1055. NonConstInput(context, other_constants[0], other_inst);
  1056. uint32_t merged_id = PerformOperation(const_mgr, inst->opcode(),
  1057. const_input1, const_input2);
  1058. if (merged_id == 0) return false;
  1059. inst->SetInOperands(
  1060. {{SPV_OPERAND_TYPE_ID, {non_const_input->result_id()}},
  1061. {SPV_OPERAND_TYPE_ID, {merged_id}}});
  1062. return true;
  1063. }
  1064. return false;
  1065. };
  1066. }
  1067. // Folds addition of a subtraction where each operation has a constant operand.
  1068. // Cases:
  1069. // (x - 2) + 2 = x + 0
  1070. // (2 - x) + 2 = 4 - x
  1071. // 2 + (x - 2) = x + 0
  1072. // 2 + (2 - x) = 4 - x
  1073. FoldingRule MergeAddSubArithmetic() {
  1074. return [](IRContext* context, Instruction* inst,
  1075. const std::vector<const analysis::Constant*>& constants) {
  1076. assert(inst->opcode() == spv::Op::OpFAdd ||
  1077. inst->opcode() == spv::Op::OpIAdd);
  1078. const analysis::Type* type =
  1079. context->get_type_mgr()->GetType(inst->type_id());
  1080. if (IsCooperativeMatrix(type)) {
  1081. return false;
  1082. }
  1083. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  1084. bool uses_float = HasFloatingPoint(type);
  1085. if (uses_float && !inst->IsFloatingPointFoldingAllowed()) return false;
  1086. uint32_t width = ElementWidth(type);
  1087. if (width != 32 && width != 64) return false;
  1088. const analysis::Constant* const_input1 = ConstInput(constants);
  1089. if (!const_input1) return false;
  1090. Instruction* other_inst = NonConstInput(context, constants[0], inst);
  1091. if (uses_float && !other_inst->IsFloatingPointFoldingAllowed())
  1092. return false;
  1093. if (other_inst->opcode() == spv::Op::OpFSub ||
  1094. other_inst->opcode() == spv::Op::OpISub) {
  1095. std::vector<const analysis::Constant*> other_constants =
  1096. const_mgr->GetOperandConstants(other_inst);
  1097. const analysis::Constant* const_input2 = ConstInput(other_constants);
  1098. if (!const_input2) return false;
  1099. bool first_is_variable = other_constants[0] == nullptr;
  1100. spv::Op op = inst->opcode();
  1101. uint32_t op1 = 0;
  1102. uint32_t op2 = 0;
  1103. if (first_is_variable) {
  1104. // Subtract constants. Non-constant operand is first.
  1105. op1 = other_inst->GetSingleWordInOperand(0u);
  1106. op2 = PerformOperation(const_mgr, other_inst->opcode(), const_input1,
  1107. const_input2);
  1108. } else {
  1109. // Add constants. Constant operand is first. Change the opcode.
  1110. op1 = PerformOperation(const_mgr, inst->opcode(), const_input1,
  1111. const_input2);
  1112. op2 = other_inst->GetSingleWordInOperand(1u);
  1113. op = other_inst->opcode();
  1114. }
  1115. if (op1 == 0 || op2 == 0) return false;
  1116. inst->SetOpcode(op);
  1117. inst->SetInOperands(
  1118. {{SPV_OPERAND_TYPE_ID, {op1}}, {SPV_OPERAND_TYPE_ID, {op2}}});
  1119. return true;
  1120. }
  1121. return false;
  1122. };
  1123. }
  1124. // Folds subtraction of an addition where each operand has a constant operand.
  1125. // Cases:
  1126. // (x + 2) - 2 = x + 0
  1127. // (2 + x) - 2 = x + 0
  1128. // 2 - (x + 2) = 0 - x
  1129. // 2 - (2 + x) = 0 - x
  1130. FoldingRule MergeSubAddArithmetic() {
  1131. return [](IRContext* context, Instruction* inst,
  1132. const std::vector<const analysis::Constant*>& constants) {
  1133. assert(inst->opcode() == spv::Op::OpFSub ||
  1134. inst->opcode() == spv::Op::OpISub);
  1135. const analysis::Type* type =
  1136. context->get_type_mgr()->GetType(inst->type_id());
  1137. if (IsCooperativeMatrix(type)) {
  1138. return false;
  1139. }
  1140. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  1141. bool uses_float = HasFloatingPoint(type);
  1142. if (uses_float && !inst->IsFloatingPointFoldingAllowed()) return false;
  1143. uint32_t width = ElementWidth(type);
  1144. if (width != 32 && width != 64) return false;
  1145. const analysis::Constant* const_input1 = ConstInput(constants);
  1146. if (!const_input1) return false;
  1147. Instruction* other_inst = NonConstInput(context, constants[0], inst);
  1148. if (uses_float && !other_inst->IsFloatingPointFoldingAllowed())
  1149. return false;
  1150. if (other_inst->opcode() == spv::Op::OpFAdd ||
  1151. other_inst->opcode() == spv::Op::OpIAdd) {
  1152. std::vector<const analysis::Constant*> other_constants =
  1153. const_mgr->GetOperandConstants(other_inst);
  1154. const analysis::Constant* const_input2 = ConstInput(other_constants);
  1155. if (!const_input2) return false;
  1156. Instruction* non_const_input =
  1157. NonConstInput(context, other_constants[0], other_inst);
  1158. // If the first operand of the sub is not a constant, swap the constants
  1159. // so the subtraction has the correct operands.
  1160. if (constants[0] == nullptr) std::swap(const_input1, const_input2);
  1161. // Subtract the constants.
  1162. uint32_t merged_id = PerformOperation(const_mgr, inst->opcode(),
  1163. const_input1, const_input2);
  1164. spv::Op op = inst->opcode();
  1165. uint32_t op1 = 0;
  1166. uint32_t op2 = 0;
  1167. if (constants[0] == nullptr) {
  1168. // Non-constant operand is first. Change the opcode.
  1169. op1 = non_const_input->result_id();
  1170. op2 = merged_id;
  1171. op = other_inst->opcode();
  1172. } else {
  1173. // Constant operand is first.
  1174. op1 = merged_id;
  1175. op2 = non_const_input->result_id();
  1176. }
  1177. if (op1 == 0 || op2 == 0) return false;
  1178. inst->SetOpcode(op);
  1179. inst->SetInOperands(
  1180. {{SPV_OPERAND_TYPE_ID, {op1}}, {SPV_OPERAND_TYPE_ID, {op2}}});
  1181. return true;
  1182. }
  1183. return false;
  1184. };
  1185. }
  1186. // Folds subtraction of a subtraction where each operand has a constant operand.
  1187. // Cases:
  1188. // (x - 2) - 2 = x - 4
  1189. // (2 - x) - 2 = 0 - x
  1190. // 2 - (x - 2) = 4 - x
  1191. // 2 - (2 - x) = x + 0
  1192. FoldingRule MergeSubSubArithmetic() {
  1193. return [](IRContext* context, Instruction* inst,
  1194. const std::vector<const analysis::Constant*>& constants) {
  1195. assert(inst->opcode() == spv::Op::OpFSub ||
  1196. inst->opcode() == spv::Op::OpISub);
  1197. const analysis::Type* type =
  1198. context->get_type_mgr()->GetType(inst->type_id());
  1199. if (IsCooperativeMatrix(type)) {
  1200. return false;
  1201. }
  1202. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  1203. bool uses_float = HasFloatingPoint(type);
  1204. if (uses_float && !inst->IsFloatingPointFoldingAllowed()) return false;
  1205. uint32_t width = ElementWidth(type);
  1206. if (width != 32 && width != 64) return false;
  1207. const analysis::Constant* const_input1 = ConstInput(constants);
  1208. if (!const_input1) return false;
  1209. Instruction* other_inst = NonConstInput(context, constants[0], inst);
  1210. if (uses_float && !other_inst->IsFloatingPointFoldingAllowed())
  1211. return false;
  1212. if (other_inst->opcode() == spv::Op::OpFSub ||
  1213. other_inst->opcode() == spv::Op::OpISub) {
  1214. std::vector<const analysis::Constant*> other_constants =
  1215. const_mgr->GetOperandConstants(other_inst);
  1216. const analysis::Constant* const_input2 = ConstInput(other_constants);
  1217. if (!const_input2) return false;
  1218. Instruction* non_const_input =
  1219. NonConstInput(context, other_constants[0], other_inst);
  1220. // Merge the constants.
  1221. uint32_t merged_id = 0;
  1222. spv::Op merge_op = inst->opcode();
  1223. if (other_constants[0] == nullptr) {
  1224. merge_op = uses_float ? spv::Op::OpFAdd : spv::Op::OpIAdd;
  1225. } else if (constants[0] == nullptr) {
  1226. std::swap(const_input1, const_input2);
  1227. }
  1228. merged_id =
  1229. PerformOperation(const_mgr, merge_op, const_input1, const_input2);
  1230. if (merged_id == 0) return false;
  1231. spv::Op op = inst->opcode();
  1232. if (constants[0] != nullptr && other_constants[0] != nullptr) {
  1233. // Change the operation.
  1234. op = uses_float ? spv::Op::OpFAdd : spv::Op::OpIAdd;
  1235. }
  1236. uint32_t op1 = 0;
  1237. uint32_t op2 = 0;
  1238. if ((constants[0] == nullptr) ^ (other_constants[0] == nullptr)) {
  1239. op1 = merged_id;
  1240. op2 = non_const_input->result_id();
  1241. } else {
  1242. op1 = non_const_input->result_id();
  1243. op2 = merged_id;
  1244. }
  1245. inst->SetOpcode(op);
  1246. inst->SetInOperands(
  1247. {{SPV_OPERAND_TYPE_ID, {op1}}, {SPV_OPERAND_TYPE_ID, {op2}}});
  1248. return true;
  1249. }
  1250. return false;
  1251. };
  1252. }
  1253. // Helper function for MergeGenericAddSubArithmetic. If |addend| and
  1254. // subtrahend of |sub| is the same, merge to copy of minuend of |sub|.
  1255. bool MergeGenericAddendSub(uint32_t addend, uint32_t sub, Instruction* inst) {
  1256. IRContext* context = inst->context();
  1257. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  1258. Instruction* sub_inst = def_use_mgr->GetDef(sub);
  1259. if (sub_inst->opcode() != spv::Op::OpFSub &&
  1260. sub_inst->opcode() != spv::Op::OpISub)
  1261. return false;
  1262. if (sub_inst->opcode() == spv::Op::OpFSub &&
  1263. !sub_inst->IsFloatingPointFoldingAllowed())
  1264. return false;
  1265. if (addend != sub_inst->GetSingleWordInOperand(1)) return false;
  1266. inst->SetOpcode(spv::Op::OpCopyObject);
  1267. inst->SetInOperands(
  1268. {{SPV_OPERAND_TYPE_ID, {sub_inst->GetSingleWordInOperand(0)}}});
  1269. context->UpdateDefUse(inst);
  1270. return true;
  1271. }
  1272. // Folds addition of a subtraction where the subtrahend is equal to the
  1273. // other addend. Return a copy of the minuend. Accepts generic (const and
  1274. // non-const) operands.
  1275. // Cases:
  1276. // (a - b) + b = a
  1277. // b + (a - b) = a
  1278. FoldingRule MergeGenericAddSubArithmetic() {
  1279. return [](IRContext* context, Instruction* inst,
  1280. const std::vector<const analysis::Constant*>&) {
  1281. assert(inst->opcode() == spv::Op::OpFAdd ||
  1282. inst->opcode() == spv::Op::OpIAdd);
  1283. const analysis::Type* type =
  1284. context->get_type_mgr()->GetType(inst->type_id());
  1285. if (IsCooperativeMatrix(type)) {
  1286. return false;
  1287. }
  1288. bool uses_float = HasFloatingPoint(type);
  1289. if (uses_float && !inst->IsFloatingPointFoldingAllowed()) return false;
  1290. uint32_t width = ElementWidth(type);
  1291. if (width != 32 && width != 64) return false;
  1292. uint32_t add_op0 = inst->GetSingleWordInOperand(0);
  1293. uint32_t add_op1 = inst->GetSingleWordInOperand(1);
  1294. if (MergeGenericAddendSub(add_op0, add_op1, inst)) return true;
  1295. return MergeGenericAddendSub(add_op1, add_op0, inst);
  1296. };
  1297. }
  1298. // Helper function for FactorAddMuls. If |factor0_0| is the same as |factor1_0|,
  1299. // generate |factor0_0| * (|factor0_1| + |factor1_1|).
  1300. bool FactorAddMulsOpnds(uint32_t factor0_0, uint32_t factor0_1,
  1301. uint32_t factor1_0, uint32_t factor1_1,
  1302. Instruction* inst) {
  1303. IRContext* context = inst->context();
  1304. if (factor0_0 != factor1_0) return false;
  1305. InstructionBuilder ir_builder(
  1306. context, inst,
  1307. IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
  1308. Instruction* new_add_inst = ir_builder.AddBinaryOp(
  1309. inst->type_id(), inst->opcode(), factor0_1, factor1_1);
  1310. inst->SetOpcode(inst->opcode() == spv::Op::OpFAdd ? spv::Op::OpFMul
  1311. : spv::Op::OpIMul);
  1312. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {factor0_0}},
  1313. {SPV_OPERAND_TYPE_ID, {new_add_inst->result_id()}}});
  1314. context->UpdateDefUse(inst);
  1315. return true;
  1316. }
  1317. // Perform the following factoring identity, handling all operand order
  1318. // combinations: (a * b) + (a * c) = a * (b + c)
  1319. FoldingRule FactorAddMuls() {
  1320. return [](IRContext* context, Instruction* inst,
  1321. const std::vector<const analysis::Constant*>&) {
  1322. assert(inst->opcode() == spv::Op::OpFAdd ||
  1323. inst->opcode() == spv::Op::OpIAdd);
  1324. const analysis::Type* type =
  1325. context->get_type_mgr()->GetType(inst->type_id());
  1326. bool uses_float = HasFloatingPoint(type);
  1327. if (uses_float && !inst->IsFloatingPointFoldingAllowed()) return false;
  1328. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  1329. uint32_t add_op0 = inst->GetSingleWordInOperand(0);
  1330. Instruction* add_op0_inst = def_use_mgr->GetDef(add_op0);
  1331. if (add_op0_inst->opcode() != spv::Op::OpFMul &&
  1332. add_op0_inst->opcode() != spv::Op::OpIMul)
  1333. return false;
  1334. uint32_t add_op1 = inst->GetSingleWordInOperand(1);
  1335. Instruction* add_op1_inst = def_use_mgr->GetDef(add_op1);
  1336. if (add_op1_inst->opcode() != spv::Op::OpFMul &&
  1337. add_op1_inst->opcode() != spv::Op::OpIMul)
  1338. return false;
  1339. // Only perform this optimization if both of the muls only have one use.
  1340. // Otherwise this is a deoptimization in size and performance.
  1341. if (def_use_mgr->NumUses(add_op0_inst) > 1) return false;
  1342. if (def_use_mgr->NumUses(add_op1_inst) > 1) return false;
  1343. if (add_op0_inst->opcode() == spv::Op::OpFMul &&
  1344. (!add_op0_inst->IsFloatingPointFoldingAllowed() ||
  1345. !add_op1_inst->IsFloatingPointFoldingAllowed()))
  1346. return false;
  1347. for (int i = 0; i < 2; i++) {
  1348. for (int j = 0; j < 2; j++) {
  1349. // Check if operand i in add_op0_inst matches operand j in add_op1_inst.
  1350. if (FactorAddMulsOpnds(add_op0_inst->GetSingleWordInOperand(i),
  1351. add_op0_inst->GetSingleWordInOperand(1 - i),
  1352. add_op1_inst->GetSingleWordInOperand(j),
  1353. add_op1_inst->GetSingleWordInOperand(1 - j),
  1354. inst))
  1355. return true;
  1356. }
  1357. }
  1358. return false;
  1359. };
  1360. }
  1361. FoldingRule IntMultipleBy1() {
  1362. return [](IRContext*, Instruction* inst,
  1363. const std::vector<const analysis::Constant*>& constants) {
  1364. assert(inst->opcode() == spv::Op::OpIMul &&
  1365. "Wrong opcode. Should be OpIMul.");
  1366. for (uint32_t i = 0; i < 2; i++) {
  1367. if (constants[i] == nullptr) {
  1368. continue;
  1369. }
  1370. const analysis::IntConstant* int_constant = constants[i]->AsIntConstant();
  1371. if (int_constant) {
  1372. uint32_t width = ElementWidth(int_constant->type());
  1373. if (width != 32 && width != 64) return false;
  1374. bool is_one = (width == 32) ? int_constant->GetU32BitValue() == 1u
  1375. : int_constant->GetU64BitValue() == 1ull;
  1376. if (is_one) {
  1377. inst->SetOpcode(spv::Op::OpCopyObject);
  1378. inst->SetInOperands(
  1379. {{SPV_OPERAND_TYPE_ID, {inst->GetSingleWordInOperand(1 - i)}}});
  1380. return true;
  1381. }
  1382. }
  1383. }
  1384. return false;
  1385. };
  1386. }
  1387. // Returns the number of elements that the |index|th in operand in |inst|
  1388. // contributes to the result of |inst|. |inst| must be an
  1389. // OpCompositeConstructInstruction.
  1390. uint32_t GetNumOfElementsContributedByOperand(IRContext* context,
  1391. const Instruction* inst,
  1392. uint32_t index) {
  1393. assert(inst->opcode() == spv::Op::OpCompositeConstruct);
  1394. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  1395. analysis::TypeManager* type_mgr = context->get_type_mgr();
  1396. analysis::Vector* result_type =
  1397. type_mgr->GetType(inst->type_id())->AsVector();
  1398. if (result_type == nullptr) {
  1399. // If the result of the OpCompositeConstruct is not a vector then every
  1400. // operands corresponds to a single element in the result.
  1401. return 1;
  1402. }
  1403. // If the result type is a vector then the operands are either scalars or
  1404. // vectors. If it is a scalar, then it corresponds to a single element. If it
  1405. // is a vector, then each element in the vector will be an element in the
  1406. // result.
  1407. uint32_t id = inst->GetSingleWordInOperand(index);
  1408. Instruction* def = def_use_mgr->GetDef(id);
  1409. analysis::Vector* type = type_mgr->GetType(def->type_id())->AsVector();
  1410. if (type == nullptr) {
  1411. return 1;
  1412. }
  1413. return type->element_count();
  1414. }
  1415. // Returns the in-operands for an OpCompositeExtract instruction that are needed
  1416. // to extract the |result_index|th element in the result of |inst| without using
  1417. // the result of |inst|. Returns the empty vector if |result_index| is
  1418. // out-of-bounds. |inst| must be an |OpCompositeConstruct| instruction.
  1419. std::vector<Operand> GetExtractOperandsForElementOfCompositeConstruct(
  1420. IRContext* context, const Instruction* inst, uint32_t result_index) {
  1421. assert(inst->opcode() == spv::Op::OpCompositeConstruct);
  1422. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  1423. analysis::TypeManager* type_mgr = context->get_type_mgr();
  1424. analysis::Type* result_type = type_mgr->GetType(inst->type_id());
  1425. if (result_type->AsVector() == nullptr) {
  1426. if (result_index < inst->NumInOperands()) {
  1427. uint32_t id = inst->GetSingleWordInOperand(result_index);
  1428. return {Operand(SPV_OPERAND_TYPE_ID, {id})};
  1429. }
  1430. return {};
  1431. }
  1432. // If the result type is a vector, then vector operands are concatenated.
  1433. uint32_t total_element_count = 0;
  1434. for (uint32_t idx = 0; idx < inst->NumInOperands(); ++idx) {
  1435. uint32_t element_count =
  1436. GetNumOfElementsContributedByOperand(context, inst, idx);
  1437. total_element_count += element_count;
  1438. if (result_index < total_element_count) {
  1439. std::vector<Operand> operands;
  1440. uint32_t id = inst->GetSingleWordInOperand(idx);
  1441. Instruction* operand_def = def_use_mgr->GetDef(id);
  1442. analysis::Type* operand_type = type_mgr->GetType(operand_def->type_id());
  1443. operands.push_back({SPV_OPERAND_TYPE_ID, {id}});
  1444. if (operand_type->AsVector()) {
  1445. uint32_t start_index_of_id = total_element_count - element_count;
  1446. uint32_t index_into_id = result_index - start_index_of_id;
  1447. operands.push_back({SPV_OPERAND_TYPE_LITERAL_INTEGER, {index_into_id}});
  1448. }
  1449. return operands;
  1450. }
  1451. }
  1452. return {};
  1453. }
  1454. bool CompositeConstructFeedingExtract(
  1455. IRContext* context, Instruction* inst,
  1456. const std::vector<const analysis::Constant*>&) {
  1457. // If the input to an OpCompositeExtract is an OpCompositeConstruct,
  1458. // then we can simply use the appropriate element in the construction.
  1459. assert(inst->opcode() == spv::Op::OpCompositeExtract &&
  1460. "Wrong opcode. Should be OpCompositeExtract.");
  1461. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  1462. // If there are no index operands, then this rule cannot do anything.
  1463. if (inst->NumInOperands() <= 1) {
  1464. return false;
  1465. }
  1466. uint32_t cid = inst->GetSingleWordInOperand(kExtractCompositeIdInIdx);
  1467. Instruction* cinst = def_use_mgr->GetDef(cid);
  1468. if (cinst->opcode() != spv::Op::OpCompositeConstruct) {
  1469. return false;
  1470. }
  1471. uint32_t index_into_result = inst->GetSingleWordInOperand(1);
  1472. std::vector<Operand> operands =
  1473. GetExtractOperandsForElementOfCompositeConstruct(context, cinst,
  1474. index_into_result);
  1475. if (operands.empty()) {
  1476. return false;
  1477. }
  1478. // Add the remaining indices for extraction.
  1479. for (uint32_t i = 2; i < inst->NumInOperands(); ++i) {
  1480. operands.push_back(
  1481. {SPV_OPERAND_TYPE_LITERAL_INTEGER, {inst->GetSingleWordInOperand(i)}});
  1482. }
  1483. if (operands.size() == 1) {
  1484. // If there were no extra indices, then we have the final object. No need
  1485. // to extract any more.
  1486. inst->SetOpcode(spv::Op::OpCopyObject);
  1487. }
  1488. inst->SetInOperands(std::move(operands));
  1489. return true;
  1490. }
  1491. // Walks the indexes chain from |start| to |end| of an OpCompositeInsert or
  1492. // OpCompositeExtract instruction, and returns the type id of the final element
  1493. // being accessed. Returns 0 if a valid type could not be found.
  1494. uint32_t GetElementType(uint32_t type_id, Instruction::iterator start,
  1495. Instruction::iterator end,
  1496. const analysis::DefUseManager* def_use_manager) {
  1497. for (auto index : make_range(std::move(start), std::move(end))) {
  1498. const Instruction* type_inst = def_use_manager->GetDef(type_id);
  1499. assert(index.type == SPV_OPERAND_TYPE_LITERAL_INTEGER &&
  1500. index.words.size() == 1);
  1501. if (type_inst->opcode() == spv::Op::OpTypeArray) {
  1502. type_id = type_inst->GetSingleWordInOperand(0);
  1503. } else if (type_inst->opcode() == spv::Op::OpTypeMatrix) {
  1504. type_id = type_inst->GetSingleWordInOperand(0);
  1505. } else if (type_inst->opcode() == spv::Op::OpTypeStruct) {
  1506. type_id = type_inst->GetSingleWordInOperand(index.words[0]);
  1507. } else {
  1508. return 0;
  1509. }
  1510. }
  1511. return type_id;
  1512. }
  1513. // Returns true of |inst_1| and |inst_2| have the same indexes that will be used
  1514. // to index into a composite object, excluding the last index. The two
  1515. // instructions must have the same opcode, and be either OpCompositeExtract or
  1516. // OpCompositeInsert instructions.
  1517. bool HaveSameIndexesExceptForLast(Instruction* inst_1, Instruction* inst_2) {
  1518. assert(inst_1->opcode() == inst_2->opcode() &&
  1519. "Expecting the opcodes to be the same.");
  1520. assert((inst_1->opcode() == spv::Op::OpCompositeInsert ||
  1521. inst_1->opcode() == spv::Op::OpCompositeExtract) &&
  1522. "Instructions must be OpCompositeInsert or OpCompositeExtract.");
  1523. if (inst_1->NumInOperands() != inst_2->NumInOperands()) {
  1524. return false;
  1525. }
  1526. uint32_t first_index_position =
  1527. (inst_1->opcode() == spv::Op::OpCompositeInsert ? 2 : 1);
  1528. for (uint32_t i = first_index_position; i < inst_1->NumInOperands() - 1;
  1529. i++) {
  1530. if (inst_1->GetSingleWordInOperand(i) !=
  1531. inst_2->GetSingleWordInOperand(i)) {
  1532. return false;
  1533. }
  1534. }
  1535. return true;
  1536. }
  1537. // If the OpCompositeConstruct is simply putting back together elements that
  1538. // where extracted from the same source, we can simply reuse the source.
  1539. //
  1540. // This is a common code pattern because of the way that scalar replacement
  1541. // works.
  1542. bool CompositeExtractFeedingConstruct(
  1543. IRContext* context, Instruction* inst,
  1544. const std::vector<const analysis::Constant*>&) {
  1545. assert(inst->opcode() == spv::Op::OpCompositeConstruct &&
  1546. "Wrong opcode. Should be OpCompositeConstruct.");
  1547. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  1548. uint32_t original_id = 0;
  1549. if (inst->NumInOperands() == 0) {
  1550. // The struct being constructed has no members.
  1551. return false;
  1552. }
  1553. // Check each element to make sure they are:
  1554. // - extractions
  1555. // - extracting the same position they are inserting
  1556. // - all extract from the same id.
  1557. Instruction* first_element_inst = nullptr;
  1558. for (uint32_t i = 0; i < inst->NumInOperands(); ++i) {
  1559. const uint32_t element_id = inst->GetSingleWordInOperand(i);
  1560. Instruction* element_inst = def_use_mgr->GetDef(element_id);
  1561. if (first_element_inst == nullptr) {
  1562. first_element_inst = element_inst;
  1563. }
  1564. if (element_inst->opcode() != spv::Op::OpCompositeExtract) {
  1565. return false;
  1566. }
  1567. if (!HaveSameIndexesExceptForLast(element_inst, first_element_inst)) {
  1568. return false;
  1569. }
  1570. if (element_inst->GetSingleWordInOperand(element_inst->NumInOperands() -
  1571. 1) != i) {
  1572. return false;
  1573. }
  1574. if (i == 0) {
  1575. original_id =
  1576. element_inst->GetSingleWordInOperand(kExtractCompositeIdInIdx);
  1577. } else if (original_id !=
  1578. element_inst->GetSingleWordInOperand(kExtractCompositeIdInIdx)) {
  1579. return false;
  1580. }
  1581. }
  1582. assert(first_element_inst != nullptr);
  1583. // The last check it to see that the object being extracted from is the
  1584. // correct type.
  1585. Instruction* original_inst = def_use_mgr->GetDef(original_id);
  1586. uint32_t original_type_id =
  1587. GetElementType(original_inst->type_id(), first_element_inst->begin() + 3,
  1588. first_element_inst->end() - 1, def_use_mgr);
  1589. if (inst->type_id() != original_type_id) {
  1590. return false;
  1591. }
  1592. if (first_element_inst->NumInOperands() == 2) {
  1593. // Simplify by using the original object.
  1594. inst->SetOpcode(spv::Op::OpCopyObject);
  1595. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {original_id}}});
  1596. return true;
  1597. }
  1598. // Copies the original id and all indexes except for the last to the new
  1599. // extract instruction.
  1600. inst->SetOpcode(spv::Op::OpCompositeExtract);
  1601. inst->SetInOperands(std::vector<Operand>(first_element_inst->begin() + 2,
  1602. first_element_inst->end() - 1));
  1603. return true;
  1604. }
  1605. FoldingRule InsertFeedingExtract() {
  1606. return [](IRContext* context, Instruction* inst,
  1607. const std::vector<const analysis::Constant*>&) {
  1608. assert(inst->opcode() == spv::Op::OpCompositeExtract &&
  1609. "Wrong opcode. Should be OpCompositeExtract.");
  1610. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  1611. uint32_t cid = inst->GetSingleWordInOperand(kExtractCompositeIdInIdx);
  1612. Instruction* cinst = def_use_mgr->GetDef(cid);
  1613. if (cinst->opcode() != spv::Op::OpCompositeInsert) {
  1614. return false;
  1615. }
  1616. // Find the first position where the list of insert and extract indicies
  1617. // differ, if at all.
  1618. uint32_t i;
  1619. for (i = 1; i < inst->NumInOperands(); ++i) {
  1620. if (i + 1 >= cinst->NumInOperands()) {
  1621. break;
  1622. }
  1623. if (inst->GetSingleWordInOperand(i) !=
  1624. cinst->GetSingleWordInOperand(i + 1)) {
  1625. break;
  1626. }
  1627. }
  1628. // We are extracting the element that was inserted.
  1629. if (i == inst->NumInOperands() && i + 1 == cinst->NumInOperands()) {
  1630. inst->SetOpcode(spv::Op::OpCopyObject);
  1631. inst->SetInOperands(
  1632. {{SPV_OPERAND_TYPE_ID,
  1633. {cinst->GetSingleWordInOperand(kInsertObjectIdInIdx)}}});
  1634. return true;
  1635. }
  1636. // Extracting the value that was inserted along with values for the base
  1637. // composite. Cannot do anything.
  1638. if (i == inst->NumInOperands()) {
  1639. return false;
  1640. }
  1641. // Extracting an element of the value that was inserted. Extract from
  1642. // that value directly.
  1643. if (i + 1 == cinst->NumInOperands()) {
  1644. std::vector<Operand> operands;
  1645. operands.push_back(
  1646. {SPV_OPERAND_TYPE_ID,
  1647. {cinst->GetSingleWordInOperand(kInsertObjectIdInIdx)}});
  1648. for (; i < inst->NumInOperands(); ++i) {
  1649. operands.push_back({SPV_OPERAND_TYPE_LITERAL_INTEGER,
  1650. {inst->GetSingleWordInOperand(i)}});
  1651. }
  1652. inst->SetInOperands(std::move(operands));
  1653. return true;
  1654. }
  1655. // Extracting a value that is disjoint from the element being inserted.
  1656. // Rewrite the extract to use the composite input to the insert.
  1657. std::vector<Operand> operands;
  1658. operands.push_back(
  1659. {SPV_OPERAND_TYPE_ID,
  1660. {cinst->GetSingleWordInOperand(kInsertCompositeIdInIdx)}});
  1661. for (i = 1; i < inst->NumInOperands(); ++i) {
  1662. operands.push_back({SPV_OPERAND_TYPE_LITERAL_INTEGER,
  1663. {inst->GetSingleWordInOperand(i)}});
  1664. }
  1665. inst->SetInOperands(std::move(operands));
  1666. return true;
  1667. };
  1668. }
  1669. // When a VectorShuffle is feeding an Extract, we can extract from one of the
  1670. // operands of the VectorShuffle. We just need to adjust the index in the
  1671. // extract instruction.
  1672. FoldingRule VectorShuffleFeedingExtract() {
  1673. return [](IRContext* context, Instruction* inst,
  1674. const std::vector<const analysis::Constant*>&) {
  1675. assert(inst->opcode() == spv::Op::OpCompositeExtract &&
  1676. "Wrong opcode. Should be OpCompositeExtract.");
  1677. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  1678. analysis::TypeManager* type_mgr = context->get_type_mgr();
  1679. uint32_t cid = inst->GetSingleWordInOperand(kExtractCompositeIdInIdx);
  1680. Instruction* cinst = def_use_mgr->GetDef(cid);
  1681. if (cinst->opcode() != spv::Op::OpVectorShuffle) {
  1682. return false;
  1683. }
  1684. // Find the size of the first vector operand of the VectorShuffle
  1685. Instruction* first_input =
  1686. def_use_mgr->GetDef(cinst->GetSingleWordInOperand(0));
  1687. analysis::Type* first_input_type =
  1688. type_mgr->GetType(first_input->type_id());
  1689. assert(first_input_type->AsVector() &&
  1690. "Input to vector shuffle should be vectors.");
  1691. uint32_t first_input_size = first_input_type->AsVector()->element_count();
  1692. // Get index of the element the vector shuffle is placing in the position
  1693. // being extracted.
  1694. uint32_t new_index =
  1695. cinst->GetSingleWordInOperand(2 + inst->GetSingleWordInOperand(1));
  1696. // Extracting an undefined value so fold this extract into an undef.
  1697. const uint32_t undef_literal_value = 0xffffffff;
  1698. if (new_index == undef_literal_value) {
  1699. inst->SetOpcode(spv::Op::OpUndef);
  1700. inst->SetInOperands({});
  1701. return true;
  1702. }
  1703. // Get the id of the of the vector the elemtent comes from, and update the
  1704. // index if needed.
  1705. uint32_t new_vector = 0;
  1706. if (new_index < first_input_size) {
  1707. new_vector = cinst->GetSingleWordInOperand(0);
  1708. } else {
  1709. new_vector = cinst->GetSingleWordInOperand(1);
  1710. new_index -= first_input_size;
  1711. }
  1712. // Update the extract instruction.
  1713. inst->SetInOperand(kExtractCompositeIdInIdx, {new_vector});
  1714. inst->SetInOperand(1, {new_index});
  1715. return true;
  1716. };
  1717. }
  1718. // When an FMix with is feeding an Extract that extracts an element whose
  1719. // corresponding |a| in the FMix is 0 or 1, we can extract from one of the
  1720. // operands of the FMix.
  1721. FoldingRule FMixFeedingExtract() {
  1722. return [](IRContext* context, Instruction* inst,
  1723. const std::vector<const analysis::Constant*>&) {
  1724. assert(inst->opcode() == spv::Op::OpCompositeExtract &&
  1725. "Wrong opcode. Should be OpCompositeExtract.");
  1726. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  1727. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  1728. uint32_t composite_id =
  1729. inst->GetSingleWordInOperand(kExtractCompositeIdInIdx);
  1730. Instruction* composite_inst = def_use_mgr->GetDef(composite_id);
  1731. if (composite_inst->opcode() != spv::Op::OpExtInst) {
  1732. return false;
  1733. }
  1734. uint32_t inst_set_id =
  1735. context->get_feature_mgr()->GetExtInstImportId_GLSLstd450();
  1736. if (composite_inst->GetSingleWordInOperand(kExtInstSetIdInIdx) !=
  1737. inst_set_id ||
  1738. composite_inst->GetSingleWordInOperand(kExtInstInstructionInIdx) !=
  1739. GLSLstd450FMix) {
  1740. return false;
  1741. }
  1742. // Get the |a| for the FMix instruction.
  1743. uint32_t a_id = composite_inst->GetSingleWordInOperand(kFMixAIdInIdx);
  1744. std::unique_ptr<Instruction> a(inst->Clone(context));
  1745. a->SetInOperand(kExtractCompositeIdInIdx, {a_id});
  1746. context->get_instruction_folder().FoldInstruction(a.get());
  1747. if (a->opcode() != spv::Op::OpCopyObject) {
  1748. return false;
  1749. }
  1750. const analysis::Constant* a_const =
  1751. const_mgr->FindDeclaredConstant(a->GetSingleWordInOperand(0));
  1752. if (!a_const) {
  1753. return false;
  1754. }
  1755. bool use_x = false;
  1756. assert(a_const->type()->AsFloat());
  1757. double element_value = a_const->GetValueAsDouble();
  1758. if (element_value == 0.0) {
  1759. use_x = true;
  1760. } else if (element_value == 1.0) {
  1761. use_x = false;
  1762. } else {
  1763. return false;
  1764. }
  1765. // Get the id of the of the vector the element comes from.
  1766. uint32_t new_vector = 0;
  1767. if (use_x) {
  1768. new_vector = composite_inst->GetSingleWordInOperand(kFMixXIdInIdx);
  1769. } else {
  1770. new_vector = composite_inst->GetSingleWordInOperand(kFMixYIdInIdx);
  1771. }
  1772. // Update the extract instruction.
  1773. inst->SetInOperand(kExtractCompositeIdInIdx, {new_vector});
  1774. return true;
  1775. };
  1776. }
  1777. // Returns the number of elements in the composite type |type|. Returns 0 if
  1778. // |type| is a scalar value. Return UINT32_MAX when the size is unknown at
  1779. // compile time.
  1780. uint32_t GetNumberOfElements(const analysis::Type* type) {
  1781. if (auto* vector_type = type->AsVector()) {
  1782. return vector_type->element_count();
  1783. }
  1784. if (auto* matrix_type = type->AsMatrix()) {
  1785. return matrix_type->element_count();
  1786. }
  1787. if (auto* struct_type = type->AsStruct()) {
  1788. return static_cast<uint32_t>(struct_type->element_types().size());
  1789. }
  1790. if (auto* array_type = type->AsArray()) {
  1791. if (array_type->length_info().words[0] ==
  1792. analysis::Array::LengthInfo::kConstant &&
  1793. array_type->length_info().words.size() == 2) {
  1794. return array_type->length_info().words[1];
  1795. }
  1796. return UINT32_MAX;
  1797. }
  1798. return 0;
  1799. }
  1800. // Returns a map with the set of values that were inserted into an object by
  1801. // the chain of OpCompositeInsertInstruction starting with |inst|.
  1802. // The map will map the index to the value inserted at that index. An empty map
  1803. // will be returned if the map could not be properly generated.
  1804. std::map<uint32_t, uint32_t> GetInsertedValues(Instruction* inst) {
  1805. analysis::DefUseManager* def_use_mgr = inst->context()->get_def_use_mgr();
  1806. std::map<uint32_t, uint32_t> values_inserted;
  1807. Instruction* current_inst = inst;
  1808. while (current_inst->opcode() == spv::Op::OpCompositeInsert) {
  1809. if (current_inst->NumInOperands() > inst->NumInOperands()) {
  1810. // This is to catch the case
  1811. // %2 = OpCompositeInsert %m2x2int %v2int_1_0 %m2x2int_undef 0
  1812. // %3 = OpCompositeInsert %m2x2int %int_4 %2 0 0
  1813. // %4 = OpCompositeInsert %m2x2int %v2int_2_3 %3 1
  1814. // In this case we cannot do a single construct to get the matrix.
  1815. uint32_t partially_inserted_element_index =
  1816. current_inst->GetSingleWordInOperand(inst->NumInOperands() - 1);
  1817. if (values_inserted.count(partially_inserted_element_index) == 0)
  1818. return {};
  1819. }
  1820. if (HaveSameIndexesExceptForLast(inst, current_inst)) {
  1821. values_inserted.insert(
  1822. {current_inst->GetSingleWordInOperand(current_inst->NumInOperands() -
  1823. 1),
  1824. current_inst->GetSingleWordInOperand(kInsertObjectIdInIdx)});
  1825. }
  1826. current_inst = def_use_mgr->GetDef(
  1827. current_inst->GetSingleWordInOperand(kInsertCompositeIdInIdx));
  1828. }
  1829. return values_inserted;
  1830. }
  1831. // Returns true of there is an entry in |values_inserted| for every element of
  1832. // |Type|.
  1833. bool DoInsertedValuesCoverEntireObject(
  1834. const analysis::Type* type, std::map<uint32_t, uint32_t>& values_inserted) {
  1835. uint32_t container_size = GetNumberOfElements(type);
  1836. if (container_size != values_inserted.size()) {
  1837. return false;
  1838. }
  1839. if (values_inserted.rbegin()->first >= container_size) {
  1840. return false;
  1841. }
  1842. return true;
  1843. }
  1844. // Returns id of the type of the element that immediately contains the element
  1845. // being inserted by the OpCompositeInsert instruction |inst|. Returns 0 if it
  1846. // could not be found.
  1847. uint32_t GetContainerTypeId(Instruction* inst) {
  1848. assert(inst->opcode() == spv::Op::OpCompositeInsert);
  1849. analysis::DefUseManager* def_use_manager = inst->context()->get_def_use_mgr();
  1850. uint32_t container_type_id = GetElementType(
  1851. inst->type_id(), inst->begin() + 4, inst->end() - 1, def_use_manager);
  1852. return container_type_id;
  1853. }
  1854. // Returns an OpCompositeConstruct instruction that build an object with
  1855. // |type_id| out of the values in |values_inserted|. Each value will be
  1856. // placed at the index corresponding to the value. The new instruction will
  1857. // be placed before |insert_before|.
  1858. Instruction* BuildCompositeConstruct(
  1859. uint32_t type_id, const std::map<uint32_t, uint32_t>& values_inserted,
  1860. Instruction* insert_before) {
  1861. InstructionBuilder ir_builder(
  1862. insert_before->context(), insert_before,
  1863. IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
  1864. std::vector<uint32_t> ids_in_order;
  1865. for (auto it : values_inserted) {
  1866. ids_in_order.push_back(it.second);
  1867. }
  1868. Instruction* construct =
  1869. ir_builder.AddCompositeConstruct(type_id, ids_in_order);
  1870. return construct;
  1871. }
  1872. // Replaces the OpCompositeInsert |inst| that inserts |construct| into the same
  1873. // object as |inst| with final index removed. If the resulting
  1874. // OpCompositeInsert instruction would have no remaining indexes, the
  1875. // instruction is replaced with an OpCopyObject instead.
  1876. void InsertConstructedObject(Instruction* inst, const Instruction* construct) {
  1877. if (inst->NumInOperands() == 3) {
  1878. inst->SetOpcode(spv::Op::OpCopyObject);
  1879. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {construct->result_id()}}});
  1880. } else {
  1881. inst->SetInOperand(kInsertObjectIdInIdx, {construct->result_id()});
  1882. inst->RemoveOperand(inst->NumOperands() - 1);
  1883. }
  1884. }
  1885. // Replaces a series of |OpCompositeInsert| instruction that cover the entire
  1886. // object with an |OpCompositeConstruct|.
  1887. bool CompositeInsertToCompositeConstruct(
  1888. IRContext* context, Instruction* inst,
  1889. const std::vector<const analysis::Constant*>&) {
  1890. assert(inst->opcode() == spv::Op::OpCompositeInsert &&
  1891. "Wrong opcode. Should be OpCompositeInsert.");
  1892. if (inst->NumInOperands() < 3) return false;
  1893. std::map<uint32_t, uint32_t> values_inserted = GetInsertedValues(inst);
  1894. uint32_t container_type_id = GetContainerTypeId(inst);
  1895. if (container_type_id == 0) {
  1896. return false;
  1897. }
  1898. analysis::TypeManager* type_mgr = context->get_type_mgr();
  1899. const analysis::Type* container_type = type_mgr->GetType(container_type_id);
  1900. assert(container_type && "GetContainerTypeId returned a bad id.");
  1901. if (!DoInsertedValuesCoverEntireObject(container_type, values_inserted)) {
  1902. return false;
  1903. }
  1904. Instruction* construct =
  1905. BuildCompositeConstruct(container_type_id, values_inserted, inst);
  1906. InsertConstructedObject(inst, construct);
  1907. return true;
  1908. }
  1909. FoldingRule RedundantPhi() {
  1910. // An OpPhi instruction where all values are the same or the result of the phi
  1911. // itself, can be replaced by the value itself.
  1912. return [](IRContext*, Instruction* inst,
  1913. const std::vector<const analysis::Constant*>&) {
  1914. assert(inst->opcode() == spv::Op::OpPhi &&
  1915. "Wrong opcode. Should be OpPhi.");
  1916. uint32_t incoming_value = 0;
  1917. for (uint32_t i = 0; i < inst->NumInOperands(); i += 2) {
  1918. uint32_t op_id = inst->GetSingleWordInOperand(i);
  1919. if (op_id == inst->result_id()) {
  1920. continue;
  1921. }
  1922. if (incoming_value == 0) {
  1923. incoming_value = op_id;
  1924. } else if (op_id != incoming_value) {
  1925. // Found two possible value. Can't simplify.
  1926. return false;
  1927. }
  1928. }
  1929. if (incoming_value == 0) {
  1930. // Code looks invalid. Don't do anything.
  1931. return false;
  1932. }
  1933. // We have a single incoming value. Simplify using that value.
  1934. inst->SetOpcode(spv::Op::OpCopyObject);
  1935. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {incoming_value}}});
  1936. return true;
  1937. };
  1938. }
  1939. FoldingRule BitCastScalarOrVector() {
  1940. return [](IRContext* context, Instruction* inst,
  1941. const std::vector<const analysis::Constant*>& constants) {
  1942. assert(inst->opcode() == spv::Op::OpBitcast && constants.size() == 1);
  1943. if (constants[0] == nullptr) return false;
  1944. const analysis::Type* type =
  1945. context->get_type_mgr()->GetType(inst->type_id());
  1946. if (HasFloatingPoint(type) && !inst->IsFloatingPointFoldingAllowed())
  1947. return false;
  1948. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  1949. std::vector<uint32_t> words =
  1950. GetWordsFromNumericScalarOrVectorConstant(const_mgr, constants[0]);
  1951. if (words.size() == 0) return false;
  1952. const analysis::Constant* bitcasted_constant =
  1953. ConvertWordsToNumericScalarOrVectorConstant(const_mgr, words, type);
  1954. if (!bitcasted_constant) return false;
  1955. auto new_feeder_id =
  1956. const_mgr->GetDefiningInstruction(bitcasted_constant, inst->type_id())
  1957. ->result_id();
  1958. inst->SetOpcode(spv::Op::OpCopyObject);
  1959. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {new_feeder_id}}});
  1960. return true;
  1961. };
  1962. }
  1963. FoldingRule RedundantSelect() {
  1964. // An OpSelect instruction where both values are the same or the condition is
  1965. // constant can be replaced by one of the values
  1966. return [](IRContext*, Instruction* inst,
  1967. const std::vector<const analysis::Constant*>& constants) {
  1968. assert(inst->opcode() == spv::Op::OpSelect &&
  1969. "Wrong opcode. Should be OpSelect.");
  1970. assert(inst->NumInOperands() == 3);
  1971. assert(constants.size() == 3);
  1972. uint32_t true_id = inst->GetSingleWordInOperand(1);
  1973. uint32_t false_id = inst->GetSingleWordInOperand(2);
  1974. if (true_id == false_id) {
  1975. // Both results are the same, condition doesn't matter
  1976. inst->SetOpcode(spv::Op::OpCopyObject);
  1977. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {true_id}}});
  1978. return true;
  1979. } else if (constants[0]) {
  1980. const analysis::Type* type = constants[0]->type();
  1981. if (type->AsBool()) {
  1982. // Scalar constant value, select the corresponding value.
  1983. inst->SetOpcode(spv::Op::OpCopyObject);
  1984. if (constants[0]->AsNullConstant() ||
  1985. !constants[0]->AsBoolConstant()->value()) {
  1986. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {false_id}}});
  1987. } else {
  1988. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {true_id}}});
  1989. }
  1990. return true;
  1991. } else {
  1992. assert(type->AsVector());
  1993. if (constants[0]->AsNullConstant()) {
  1994. // All values come from false id.
  1995. inst->SetOpcode(spv::Op::OpCopyObject);
  1996. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {false_id}}});
  1997. return true;
  1998. } else {
  1999. // Convert to a vector shuffle.
  2000. std::vector<Operand> ops;
  2001. ops.push_back({SPV_OPERAND_TYPE_ID, {true_id}});
  2002. ops.push_back({SPV_OPERAND_TYPE_ID, {false_id}});
  2003. const analysis::VectorConstant* vector_const =
  2004. constants[0]->AsVectorConstant();
  2005. uint32_t size =
  2006. static_cast<uint32_t>(vector_const->GetComponents().size());
  2007. for (uint32_t i = 0; i != size; ++i) {
  2008. const analysis::Constant* component =
  2009. vector_const->GetComponents()[i];
  2010. if (component->AsNullConstant() ||
  2011. !component->AsBoolConstant()->value()) {
  2012. // Selecting from the false vector which is the second input
  2013. // vector to the shuffle. Offset the index by |size|.
  2014. ops.push_back({SPV_OPERAND_TYPE_LITERAL_INTEGER, {i + size}});
  2015. } else {
  2016. // Selecting from true vector which is the first input vector to
  2017. // the shuffle.
  2018. ops.push_back({SPV_OPERAND_TYPE_LITERAL_INTEGER, {i}});
  2019. }
  2020. }
  2021. inst->SetOpcode(spv::Op::OpVectorShuffle);
  2022. inst->SetInOperands(std::move(ops));
  2023. return true;
  2024. }
  2025. }
  2026. }
  2027. return false;
  2028. };
  2029. }
  2030. enum class FloatConstantKind { Unknown, Zero, One };
  2031. FloatConstantKind getFloatConstantKind(const analysis::Constant* constant) {
  2032. if (constant == nullptr) {
  2033. return FloatConstantKind::Unknown;
  2034. }
  2035. assert(HasFloatingPoint(constant->type()) && "Unexpected constant type");
  2036. if (constant->AsNullConstant()) {
  2037. return FloatConstantKind::Zero;
  2038. } else if (const analysis::VectorConstant* vc =
  2039. constant->AsVectorConstant()) {
  2040. const std::vector<const analysis::Constant*>& components =
  2041. vc->GetComponents();
  2042. assert(!components.empty());
  2043. FloatConstantKind kind = getFloatConstantKind(components[0]);
  2044. for (size_t i = 1; i < components.size(); ++i) {
  2045. if (getFloatConstantKind(components[i]) != kind) {
  2046. return FloatConstantKind::Unknown;
  2047. }
  2048. }
  2049. return kind;
  2050. } else if (const analysis::FloatConstant* fc = constant->AsFloatConstant()) {
  2051. if (fc->IsZero()) return FloatConstantKind::Zero;
  2052. uint32_t width = fc->type()->AsFloat()->width();
  2053. if (width != 32 && width != 64) return FloatConstantKind::Unknown;
  2054. double value = (width == 64) ? fc->GetDoubleValue() : fc->GetFloatValue();
  2055. if (value == 0.0) {
  2056. return FloatConstantKind::Zero;
  2057. } else if (value == 1.0) {
  2058. return FloatConstantKind::One;
  2059. } else {
  2060. return FloatConstantKind::Unknown;
  2061. }
  2062. } else {
  2063. return FloatConstantKind::Unknown;
  2064. }
  2065. }
  2066. FoldingRule RedundantFAdd() {
  2067. return [](IRContext*, Instruction* inst,
  2068. const std::vector<const analysis::Constant*>& constants) {
  2069. assert(inst->opcode() == spv::Op::OpFAdd &&
  2070. "Wrong opcode. Should be OpFAdd.");
  2071. assert(constants.size() == 2);
  2072. if (!inst->IsFloatingPointFoldingAllowed()) {
  2073. return false;
  2074. }
  2075. FloatConstantKind kind0 = getFloatConstantKind(constants[0]);
  2076. FloatConstantKind kind1 = getFloatConstantKind(constants[1]);
  2077. if (kind0 == FloatConstantKind::Zero || kind1 == FloatConstantKind::Zero) {
  2078. inst->SetOpcode(spv::Op::OpCopyObject);
  2079. inst->SetInOperands({{SPV_OPERAND_TYPE_ID,
  2080. {inst->GetSingleWordInOperand(
  2081. kind0 == FloatConstantKind::Zero ? 1 : 0)}}});
  2082. return true;
  2083. }
  2084. return false;
  2085. };
  2086. }
  2087. FoldingRule RedundantFSub() {
  2088. return [](IRContext*, Instruction* inst,
  2089. const std::vector<const analysis::Constant*>& constants) {
  2090. assert(inst->opcode() == spv::Op::OpFSub &&
  2091. "Wrong opcode. Should be OpFSub.");
  2092. assert(constants.size() == 2);
  2093. if (!inst->IsFloatingPointFoldingAllowed()) {
  2094. return false;
  2095. }
  2096. FloatConstantKind kind0 = getFloatConstantKind(constants[0]);
  2097. FloatConstantKind kind1 = getFloatConstantKind(constants[1]);
  2098. if (kind0 == FloatConstantKind::Zero) {
  2099. inst->SetOpcode(spv::Op::OpFNegate);
  2100. inst->SetInOperands(
  2101. {{SPV_OPERAND_TYPE_ID, {inst->GetSingleWordInOperand(1)}}});
  2102. return true;
  2103. }
  2104. if (kind1 == FloatConstantKind::Zero) {
  2105. inst->SetOpcode(spv::Op::OpCopyObject);
  2106. inst->SetInOperands(
  2107. {{SPV_OPERAND_TYPE_ID, {inst->GetSingleWordInOperand(0)}}});
  2108. return true;
  2109. }
  2110. return false;
  2111. };
  2112. }
  2113. FoldingRule RedundantFMul() {
  2114. return [](IRContext*, Instruction* inst,
  2115. const std::vector<const analysis::Constant*>& constants) {
  2116. assert(inst->opcode() == spv::Op::OpFMul &&
  2117. "Wrong opcode. Should be OpFMul.");
  2118. assert(constants.size() == 2);
  2119. if (!inst->IsFloatingPointFoldingAllowed()) {
  2120. return false;
  2121. }
  2122. FloatConstantKind kind0 = getFloatConstantKind(constants[0]);
  2123. FloatConstantKind kind1 = getFloatConstantKind(constants[1]);
  2124. if (kind0 == FloatConstantKind::Zero || kind1 == FloatConstantKind::Zero) {
  2125. inst->SetOpcode(spv::Op::OpCopyObject);
  2126. inst->SetInOperands({{SPV_OPERAND_TYPE_ID,
  2127. {inst->GetSingleWordInOperand(
  2128. kind0 == FloatConstantKind::Zero ? 0 : 1)}}});
  2129. return true;
  2130. }
  2131. if (kind0 == FloatConstantKind::One || kind1 == FloatConstantKind::One) {
  2132. inst->SetOpcode(spv::Op::OpCopyObject);
  2133. inst->SetInOperands({{SPV_OPERAND_TYPE_ID,
  2134. {inst->GetSingleWordInOperand(
  2135. kind0 == FloatConstantKind::One ? 1 : 0)}}});
  2136. return true;
  2137. }
  2138. return false;
  2139. };
  2140. }
  2141. FoldingRule RedundantFDiv() {
  2142. return [](IRContext*, Instruction* inst,
  2143. const std::vector<const analysis::Constant*>& constants) {
  2144. assert(inst->opcode() == spv::Op::OpFDiv &&
  2145. "Wrong opcode. Should be OpFDiv.");
  2146. assert(constants.size() == 2);
  2147. if (!inst->IsFloatingPointFoldingAllowed()) {
  2148. return false;
  2149. }
  2150. FloatConstantKind kind0 = getFloatConstantKind(constants[0]);
  2151. FloatConstantKind kind1 = getFloatConstantKind(constants[1]);
  2152. if (kind0 == FloatConstantKind::Zero || kind1 == FloatConstantKind::One) {
  2153. inst->SetOpcode(spv::Op::OpCopyObject);
  2154. inst->SetInOperands(
  2155. {{SPV_OPERAND_TYPE_ID, {inst->GetSingleWordInOperand(0)}}});
  2156. return true;
  2157. }
  2158. return false;
  2159. };
  2160. }
  2161. FoldingRule RedundantFMod() {
  2162. return [](IRContext* context, Instruction* inst,
  2163. const std::vector<const analysis::Constant*>& constants) {
  2164. assert(inst->opcode() == spv::Op::OpFMod &&
  2165. "Wrong opcode. Should be OpFMod.");
  2166. assert(constants.size() == 2);
  2167. if (!inst->IsFloatingPointFoldingAllowed()) {
  2168. return false;
  2169. }
  2170. FloatConstantKind kind0 = getFloatConstantKind(constants[0]);
  2171. FloatConstantKind kind1 = getFloatConstantKind(constants[1]);
  2172. if (kind0 == FloatConstantKind::Zero) {
  2173. inst->SetOpcode(spv::Op::OpCopyObject);
  2174. inst->SetInOperands(
  2175. {{SPV_OPERAND_TYPE_ID, {inst->GetSingleWordInOperand(0)}}});
  2176. return true;
  2177. }
  2178. if (kind1 == FloatConstantKind::One) {
  2179. auto type = context->get_type_mgr()->GetType(inst->type_id());
  2180. std::vector<uint32_t> zero_words;
  2181. zero_words.resize(ElementWidth(type) / 32);
  2182. auto const_mgr = context->get_constant_mgr();
  2183. auto zero = const_mgr->GetConstant(type, std::move(zero_words));
  2184. auto zero_id = const_mgr->GetDefiningInstruction(zero)->result_id();
  2185. inst->SetOpcode(spv::Op::OpCopyObject);
  2186. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {zero_id}}});
  2187. return true;
  2188. }
  2189. return false;
  2190. };
  2191. }
  2192. FoldingRule RedundantFMix() {
  2193. return [](IRContext* context, Instruction* inst,
  2194. const std::vector<const analysis::Constant*>& constants) {
  2195. assert(inst->opcode() == spv::Op::OpExtInst &&
  2196. "Wrong opcode. Should be OpExtInst.");
  2197. if (!inst->IsFloatingPointFoldingAllowed()) {
  2198. return false;
  2199. }
  2200. uint32_t instSetId =
  2201. context->get_feature_mgr()->GetExtInstImportId_GLSLstd450();
  2202. if (inst->GetSingleWordInOperand(kExtInstSetIdInIdx) == instSetId &&
  2203. inst->GetSingleWordInOperand(kExtInstInstructionInIdx) ==
  2204. GLSLstd450FMix) {
  2205. assert(constants.size() == 5);
  2206. FloatConstantKind kind4 = getFloatConstantKind(constants[4]);
  2207. if (kind4 == FloatConstantKind::Zero || kind4 == FloatConstantKind::One) {
  2208. inst->SetOpcode(spv::Op::OpCopyObject);
  2209. inst->SetInOperands(
  2210. {{SPV_OPERAND_TYPE_ID,
  2211. {inst->GetSingleWordInOperand(kind4 == FloatConstantKind::Zero
  2212. ? kFMixXIdInIdx
  2213. : kFMixYIdInIdx)}}});
  2214. return true;
  2215. }
  2216. }
  2217. return false;
  2218. };
  2219. }
  2220. // Returns a folding rule that folds the instruction to operand |foldToArg|
  2221. // (0 or 1) if operand |arg| (0 or 1) is a zero constant.
  2222. FoldingRule RedundantBinaryOpWithZeroOperand(uint32_t arg, uint32_t foldToArg) {
  2223. return [arg, foldToArg](
  2224. IRContext* context, Instruction* inst,
  2225. const std::vector<const analysis::Constant*>& constants) {
  2226. assert(constants.size() == 2);
  2227. if (constants[arg] && constants[arg]->IsZero()) {
  2228. auto operand = inst->GetSingleWordInOperand(foldToArg);
  2229. auto operand_type = constants[arg]->type();
  2230. const analysis::Type* inst_type =
  2231. context->get_type_mgr()->GetType(inst->type_id());
  2232. if (inst_type->IsSame(operand_type)) {
  2233. inst->SetOpcode(spv::Op::OpCopyObject);
  2234. } else {
  2235. inst->SetOpcode(spv::Op::OpBitcast);
  2236. }
  2237. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {operand}}});
  2238. return true;
  2239. }
  2240. return false;
  2241. };
  2242. }
  2243. // This rule handles any of RedundantBinaryRhs0Ops with a 0 or vector 0 on the
  2244. // right-hand side (a | 0 => a).
  2245. static const constexpr spv::Op RedundantBinaryRhs0Ops[] = {
  2246. spv::Op::OpBitwiseOr,
  2247. spv::Op::OpBitwiseXor,
  2248. spv::Op::OpShiftRightLogical,
  2249. spv::Op::OpShiftRightArithmetic,
  2250. spv::Op::OpShiftLeftLogical,
  2251. spv::Op::OpIAdd,
  2252. spv::Op::OpISub};
  2253. FoldingRule RedundantBinaryRhs0(spv::Op op) {
  2254. assert(std::find(std::begin(RedundantBinaryRhs0Ops),
  2255. std::end(RedundantBinaryRhs0Ops),
  2256. op) != std::end(RedundantBinaryRhs0Ops) &&
  2257. "Wrong opcode.");
  2258. (void)op;
  2259. return RedundantBinaryOpWithZeroOperand(1, 0);
  2260. }
  2261. // This rule handles any of RedundantBinaryLhs0Ops with a 0 or vector 0 on the
  2262. // left-hand side (0 | a => a).
  2263. static const constexpr spv::Op RedundantBinaryLhs0Ops[] = {
  2264. spv::Op::OpBitwiseOr, spv::Op::OpBitwiseXor, spv::Op::OpIAdd};
  2265. FoldingRule RedundantBinaryLhs0(spv::Op op) {
  2266. assert(std::find(std::begin(RedundantBinaryLhs0Ops),
  2267. std::end(RedundantBinaryLhs0Ops),
  2268. op) != std::end(RedundantBinaryLhs0Ops) &&
  2269. "Wrong opcode.");
  2270. (void)op;
  2271. return RedundantBinaryOpWithZeroOperand(0, 1);
  2272. }
  2273. // This rule handles shifts and divisions of 0 or vector 0 by any amount
  2274. // (0 >> a => 0).
  2275. static const constexpr spv::Op RedundantBinaryLhs0To0Ops[] = {
  2276. spv::Op::OpShiftRightLogical,
  2277. spv::Op::OpShiftRightArithmetic,
  2278. spv::Op::OpShiftLeftLogical,
  2279. spv::Op::OpSDiv,
  2280. spv::Op::OpUDiv,
  2281. spv::Op::OpSMod,
  2282. spv::Op::OpUMod};
  2283. FoldingRule RedundantBinaryLhs0To0(spv::Op op) {
  2284. assert(std::find(std::begin(RedundantBinaryLhs0To0Ops),
  2285. std::end(RedundantBinaryLhs0To0Ops),
  2286. op) != std::end(RedundantBinaryLhs0To0Ops) &&
  2287. "Wrong opcode.");
  2288. (void)op;
  2289. return RedundantBinaryOpWithZeroOperand(0, 0);
  2290. }
  2291. // Returns true if all elements in |c| are 1.
  2292. bool IsAllInt1(const analysis::Constant* c) {
  2293. if (auto composite = c->AsCompositeConstant()) {
  2294. auto& components = composite->GetComponents();
  2295. return std::all_of(std::begin(components), std::end(components), IsAllInt1);
  2296. } else if (c->AsIntConstant()) {
  2297. return c->GetSignExtendedValue() == 1;
  2298. }
  2299. return false;
  2300. }
  2301. // This rule handles divisions by 1 or vector 1 (a / 1 => a).
  2302. FoldingRule RedundantSUDiv() {
  2303. return [](IRContext* context, Instruction* inst,
  2304. const std::vector<const analysis::Constant*>& constants) {
  2305. assert(constants.size() == 2);
  2306. assert((inst->opcode() == spv::Op::OpUDiv ||
  2307. inst->opcode() == spv::Op::OpSDiv) &&
  2308. "Wrong opcode.");
  2309. if (constants[1] && IsAllInt1(constants[1])) {
  2310. auto operand = inst->GetSingleWordInOperand(0);
  2311. auto operand_type = constants[1]->type();
  2312. const analysis::Type* inst_type =
  2313. context->get_type_mgr()->GetType(inst->type_id());
  2314. if (inst_type->IsSame(operand_type)) {
  2315. inst->SetOpcode(spv::Op::OpCopyObject);
  2316. } else {
  2317. inst->SetOpcode(spv::Op::OpBitcast);
  2318. }
  2319. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {operand}}});
  2320. return true;
  2321. }
  2322. return false;
  2323. };
  2324. }
  2325. // This rule handles modulo from division by 1 or vector 1 (a % 1 => 0).
  2326. FoldingRule RedundantSUMod() {
  2327. return [](IRContext* context, Instruction* inst,
  2328. const std::vector<const analysis::Constant*>& constants) {
  2329. assert(constants.size() == 2);
  2330. assert((inst->opcode() == spv::Op::OpUMod ||
  2331. inst->opcode() == spv::Op::OpSMod) &&
  2332. "Wrong opcode.");
  2333. if (constants[1] && IsAllInt1(constants[1])) {
  2334. auto type = context->get_type_mgr()->GetType(inst->type_id());
  2335. auto zero_id = context->get_constant_mgr()->GetNullConstId(type);
  2336. inst->SetOpcode(spv::Op::OpCopyObject);
  2337. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {zero_id}}});
  2338. return true;
  2339. }
  2340. return false;
  2341. };
  2342. }
  2343. // This rule look for a dot with a constant vector containing a single 1 and
  2344. // the rest 0s. This is the same as doing an extract.
  2345. FoldingRule DotProductDoingExtract() {
  2346. return [](IRContext* context, Instruction* inst,
  2347. const std::vector<const analysis::Constant*>& constants) {
  2348. assert(inst->opcode() == spv::Op::OpDot &&
  2349. "Wrong opcode. Should be OpDot.");
  2350. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  2351. if (!inst->IsFloatingPointFoldingAllowed()) {
  2352. return false;
  2353. }
  2354. for (int i = 0; i < 2; ++i) {
  2355. if (!constants[i]) {
  2356. continue;
  2357. }
  2358. const analysis::Vector* vector_type = constants[i]->type()->AsVector();
  2359. assert(vector_type && "Inputs to OpDot must be vectors.");
  2360. const analysis::Float* element_type =
  2361. vector_type->element_type()->AsFloat();
  2362. assert(element_type && "Inputs to OpDot must be vectors of floats.");
  2363. uint32_t element_width = element_type->width();
  2364. if (element_width != 32 && element_width != 64) {
  2365. return false;
  2366. }
  2367. std::vector<const analysis::Constant*> components;
  2368. components = constants[i]->GetVectorComponents(const_mgr);
  2369. constexpr uint32_t kNotFound = std::numeric_limits<uint32_t>::max();
  2370. uint32_t component_with_one = kNotFound;
  2371. bool all_others_zero = true;
  2372. for (uint32_t j = 0; j < components.size(); ++j) {
  2373. const analysis::Constant* element = components[j];
  2374. double value =
  2375. (element_width == 32 ? element->GetFloat() : element->GetDouble());
  2376. if (value == 0.0) {
  2377. continue;
  2378. } else if (value == 1.0) {
  2379. if (component_with_one == kNotFound) {
  2380. component_with_one = j;
  2381. } else {
  2382. component_with_one = kNotFound;
  2383. break;
  2384. }
  2385. } else {
  2386. all_others_zero = false;
  2387. break;
  2388. }
  2389. }
  2390. if (!all_others_zero || component_with_one == kNotFound) {
  2391. continue;
  2392. }
  2393. std::vector<Operand> operands;
  2394. operands.push_back(
  2395. {SPV_OPERAND_TYPE_ID, {inst->GetSingleWordInOperand(1u - i)}});
  2396. operands.push_back(
  2397. {SPV_OPERAND_TYPE_LITERAL_INTEGER, {component_with_one}});
  2398. inst->SetOpcode(spv::Op::OpCompositeExtract);
  2399. inst->SetInOperands(std::move(operands));
  2400. return true;
  2401. }
  2402. return false;
  2403. };
  2404. }
  2405. // If we are storing an undef, then we can remove the store.
  2406. //
  2407. // TODO: We can do something similar for OpImageWrite, but checking for volatile
  2408. // is complicated. Waiting to see if it is needed.
  2409. FoldingRule StoringUndef() {
  2410. return [](IRContext* context, Instruction* inst,
  2411. const std::vector<const analysis::Constant*>&) {
  2412. assert(inst->opcode() == spv::Op::OpStore &&
  2413. "Wrong opcode. Should be OpStore.");
  2414. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  2415. // If this is a volatile store, the store cannot be removed.
  2416. if (inst->NumInOperands() == 3) {
  2417. if (inst->GetSingleWordInOperand(2) &
  2418. uint32_t(spv::MemoryAccessMask::Volatile)) {
  2419. return false;
  2420. }
  2421. }
  2422. uint32_t object_id = inst->GetSingleWordInOperand(kStoreObjectInIdx);
  2423. Instruction* object_inst = def_use_mgr->GetDef(object_id);
  2424. if (object_inst->opcode() == spv::Op::OpUndef) {
  2425. inst->ToNop();
  2426. return true;
  2427. }
  2428. return false;
  2429. };
  2430. }
  2431. FoldingRule VectorShuffleFeedingShuffle() {
  2432. return [](IRContext* context, Instruction* inst,
  2433. const std::vector<const analysis::Constant*>&) {
  2434. assert(inst->opcode() == spv::Op::OpVectorShuffle &&
  2435. "Wrong opcode. Should be OpVectorShuffle.");
  2436. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  2437. analysis::TypeManager* type_mgr = context->get_type_mgr();
  2438. Instruction* feeding_shuffle_inst =
  2439. def_use_mgr->GetDef(inst->GetSingleWordInOperand(0));
  2440. analysis::Vector* op0_type =
  2441. type_mgr->GetType(feeding_shuffle_inst->type_id())->AsVector();
  2442. uint32_t op0_length = op0_type->element_count();
  2443. bool feeder_is_op0 = true;
  2444. if (feeding_shuffle_inst->opcode() != spv::Op::OpVectorShuffle) {
  2445. feeding_shuffle_inst =
  2446. def_use_mgr->GetDef(inst->GetSingleWordInOperand(1));
  2447. feeder_is_op0 = false;
  2448. }
  2449. if (feeding_shuffle_inst->opcode() != spv::Op::OpVectorShuffle) {
  2450. return false;
  2451. }
  2452. Instruction* feeder2 =
  2453. def_use_mgr->GetDef(feeding_shuffle_inst->GetSingleWordInOperand(0));
  2454. analysis::Vector* feeder_op0_type =
  2455. type_mgr->GetType(feeder2->type_id())->AsVector();
  2456. uint32_t feeder_op0_length = feeder_op0_type->element_count();
  2457. uint32_t new_feeder_id = 0;
  2458. std::vector<Operand> new_operands;
  2459. new_operands.resize(
  2460. 2, {SPV_OPERAND_TYPE_ID, {0}}); // Place holders for vector operands.
  2461. const uint32_t undef_literal = 0xffffffff;
  2462. for (uint32_t op = 2; op < inst->NumInOperands(); ++op) {
  2463. uint32_t component_index = inst->GetSingleWordInOperand(op);
  2464. // Do not interpret the undefined value literal as coming from operand 1.
  2465. if (component_index != undef_literal &&
  2466. feeder_is_op0 == (component_index < op0_length)) {
  2467. // This component comes from the feeding_shuffle_inst. Update
  2468. // |component_index| to be the index into the operand of the feeder.
  2469. // Adjust component_index to get the index into the operands of the
  2470. // feeding_shuffle_inst.
  2471. if (component_index >= op0_length) {
  2472. component_index -= op0_length;
  2473. }
  2474. component_index =
  2475. feeding_shuffle_inst->GetSingleWordInOperand(component_index + 2);
  2476. // Check if we are using a component from the first or second operand of
  2477. // the feeding instruction.
  2478. if (component_index < feeder_op0_length) {
  2479. if (new_feeder_id == 0) {
  2480. // First time through, save the id of the operand the element comes
  2481. // from.
  2482. new_feeder_id = feeding_shuffle_inst->GetSingleWordInOperand(0);
  2483. } else if (new_feeder_id !=
  2484. feeding_shuffle_inst->GetSingleWordInOperand(0)) {
  2485. // We need both elements of the feeding_shuffle_inst, so we cannot
  2486. // fold.
  2487. return false;
  2488. }
  2489. } else if (component_index != undef_literal) {
  2490. if (new_feeder_id == 0) {
  2491. // First time through, save the id of the operand the element comes
  2492. // from.
  2493. new_feeder_id = feeding_shuffle_inst->GetSingleWordInOperand(1);
  2494. } else if (new_feeder_id !=
  2495. feeding_shuffle_inst->GetSingleWordInOperand(1)) {
  2496. // We need both elements of the feeding_shuffle_inst, so we cannot
  2497. // fold.
  2498. return false;
  2499. }
  2500. component_index -= feeder_op0_length;
  2501. }
  2502. if (!feeder_is_op0 && component_index != undef_literal) {
  2503. component_index += op0_length;
  2504. }
  2505. }
  2506. new_operands.push_back(
  2507. {SPV_OPERAND_TYPE_LITERAL_INTEGER, {component_index}});
  2508. }
  2509. if (new_feeder_id == 0) {
  2510. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  2511. const analysis::Type* type =
  2512. type_mgr->GetType(feeding_shuffle_inst->type_id());
  2513. const analysis::Constant* null_const = const_mgr->GetConstant(type, {});
  2514. new_feeder_id =
  2515. const_mgr->GetDefiningInstruction(null_const, 0)->result_id();
  2516. }
  2517. if (feeder_is_op0) {
  2518. // If the size of the first vector operand changed then the indices
  2519. // referring to the second operand need to be adjusted.
  2520. Instruction* new_feeder_inst = def_use_mgr->GetDef(new_feeder_id);
  2521. analysis::Type* new_feeder_type =
  2522. type_mgr->GetType(new_feeder_inst->type_id());
  2523. uint32_t new_op0_size = new_feeder_type->AsVector()->element_count();
  2524. int32_t adjustment = op0_length - new_op0_size;
  2525. if (adjustment != 0) {
  2526. for (uint32_t i = 2; i < new_operands.size(); i++) {
  2527. uint32_t operand = inst->GetSingleWordInOperand(i);
  2528. if (operand >= op0_length && operand != undef_literal) {
  2529. new_operands[i].words[0] -= adjustment;
  2530. }
  2531. }
  2532. }
  2533. new_operands[0].words[0] = new_feeder_id;
  2534. new_operands[1] = inst->GetInOperand(1);
  2535. } else {
  2536. new_operands[1].words[0] = new_feeder_id;
  2537. new_operands[0] = inst->GetInOperand(0);
  2538. }
  2539. inst->SetInOperands(std::move(new_operands));
  2540. return true;
  2541. };
  2542. }
  2543. // Removes duplicate ids from the interface list of an OpEntryPoint
  2544. // instruction.
  2545. FoldingRule RemoveRedundantOperands() {
  2546. return [](IRContext*, Instruction* inst,
  2547. const std::vector<const analysis::Constant*>&) {
  2548. assert(inst->opcode() == spv::Op::OpEntryPoint &&
  2549. "Wrong opcode. Should be OpEntryPoint.");
  2550. bool has_redundant_operand = false;
  2551. std::unordered_set<uint32_t> seen_operands;
  2552. std::vector<Operand> new_operands;
  2553. new_operands.emplace_back(inst->GetOperand(0));
  2554. new_operands.emplace_back(inst->GetOperand(1));
  2555. new_operands.emplace_back(inst->GetOperand(2));
  2556. for (uint32_t i = 3; i < inst->NumOperands(); ++i) {
  2557. if (seen_operands.insert(inst->GetSingleWordOperand(i)).second) {
  2558. new_operands.emplace_back(inst->GetOperand(i));
  2559. } else {
  2560. has_redundant_operand = true;
  2561. }
  2562. }
  2563. if (!has_redundant_operand) {
  2564. return false;
  2565. }
  2566. inst->SetInOperands(std::move(new_operands));
  2567. return true;
  2568. };
  2569. }
  2570. // If an image instruction's operand is a constant, updates the image operand
  2571. // flag from Offset to ConstOffset.
  2572. FoldingRule UpdateImageOperands() {
  2573. return [](IRContext*, Instruction* inst,
  2574. const std::vector<const analysis::Constant*>& constants) {
  2575. const auto opcode = inst->opcode();
  2576. (void)opcode;
  2577. assert((opcode == spv::Op::OpImageSampleImplicitLod ||
  2578. opcode == spv::Op::OpImageSampleExplicitLod ||
  2579. opcode == spv::Op::OpImageSampleDrefImplicitLod ||
  2580. opcode == spv::Op::OpImageSampleDrefExplicitLod ||
  2581. opcode == spv::Op::OpImageSampleProjImplicitLod ||
  2582. opcode == spv::Op::OpImageSampleProjExplicitLod ||
  2583. opcode == spv::Op::OpImageSampleProjDrefImplicitLod ||
  2584. opcode == spv::Op::OpImageSampleProjDrefExplicitLod ||
  2585. opcode == spv::Op::OpImageFetch ||
  2586. opcode == spv::Op::OpImageGather ||
  2587. opcode == spv::Op::OpImageDrefGather ||
  2588. opcode == spv::Op::OpImageRead || opcode == spv::Op::OpImageWrite ||
  2589. opcode == spv::Op::OpImageSparseSampleImplicitLod ||
  2590. opcode == spv::Op::OpImageSparseSampleExplicitLod ||
  2591. opcode == spv::Op::OpImageSparseSampleDrefImplicitLod ||
  2592. opcode == spv::Op::OpImageSparseSampleDrefExplicitLod ||
  2593. opcode == spv::Op::OpImageSparseSampleProjImplicitLod ||
  2594. opcode == spv::Op::OpImageSparseSampleProjExplicitLod ||
  2595. opcode == spv::Op::OpImageSparseSampleProjDrefImplicitLod ||
  2596. opcode == spv::Op::OpImageSparseSampleProjDrefExplicitLod ||
  2597. opcode == spv::Op::OpImageSparseFetch ||
  2598. opcode == spv::Op::OpImageSparseGather ||
  2599. opcode == spv::Op::OpImageSparseDrefGather ||
  2600. opcode == spv::Op::OpImageSparseRead) &&
  2601. "Wrong opcode. Should be an image instruction.");
  2602. int32_t operand_index = ImageOperandsMaskInOperandIndex(inst);
  2603. if (operand_index >= 0) {
  2604. auto image_operands = inst->GetSingleWordInOperand(operand_index);
  2605. if (image_operands & uint32_t(spv::ImageOperandsMask::Offset)) {
  2606. uint32_t offset_operand_index = operand_index + 1;
  2607. if (image_operands & uint32_t(spv::ImageOperandsMask::Bias))
  2608. offset_operand_index++;
  2609. if (image_operands & uint32_t(spv::ImageOperandsMask::Lod))
  2610. offset_operand_index++;
  2611. if (image_operands & uint32_t(spv::ImageOperandsMask::Grad))
  2612. offset_operand_index += 2;
  2613. assert(((image_operands &
  2614. uint32_t(spv::ImageOperandsMask::ConstOffset)) == 0) &&
  2615. "Offset and ConstOffset may not be used together");
  2616. if (offset_operand_index < inst->NumOperands()) {
  2617. if (constants[offset_operand_index]) {
  2618. if (constants[offset_operand_index]->IsZero()) {
  2619. inst->RemoveInOperand(offset_operand_index);
  2620. } else {
  2621. image_operands = image_operands |
  2622. uint32_t(spv::ImageOperandsMask::ConstOffset);
  2623. }
  2624. image_operands =
  2625. image_operands & ~uint32_t(spv::ImageOperandsMask::Offset);
  2626. inst->SetInOperand(operand_index, {image_operands});
  2627. return true;
  2628. }
  2629. }
  2630. }
  2631. }
  2632. return false;
  2633. };
  2634. }
  2635. } // namespace
  2636. void FoldingRules::AddFoldingRules() {
  2637. // Add all folding rules to the list for the opcodes to which they apply.
  2638. // Note that the order in which rules are added to the list matters. If a rule
  2639. // applies to the instruction, the rest of the rules will not be attempted.
  2640. // Take that into consideration.
  2641. for (auto op : RedundantBinaryRhs0Ops)
  2642. rules_[op].push_back(RedundantBinaryRhs0(op));
  2643. for (auto op : RedundantBinaryLhs0Ops)
  2644. rules_[op].push_back(RedundantBinaryLhs0(op));
  2645. for (auto op : RedundantBinaryLhs0To0Ops)
  2646. rules_[op].push_back(RedundantBinaryLhs0To0(op));
  2647. rules_[spv::Op::OpSDiv].push_back(RedundantSUDiv());
  2648. rules_[spv::Op::OpUDiv].push_back(RedundantSUDiv());
  2649. rules_[spv::Op::OpSMod].push_back(RedundantSUMod());
  2650. rules_[spv::Op::OpUMod].push_back(RedundantSUMod());
  2651. rules_[spv::Op::OpBitcast].push_back(BitCastScalarOrVector());
  2652. rules_[spv::Op::OpCompositeConstruct].push_back(
  2653. CompositeExtractFeedingConstruct);
  2654. rules_[spv::Op::OpCompositeExtract].push_back(InsertFeedingExtract());
  2655. rules_[spv::Op::OpCompositeExtract].push_back(
  2656. CompositeConstructFeedingExtract);
  2657. rules_[spv::Op::OpCompositeExtract].push_back(VectorShuffleFeedingExtract());
  2658. rules_[spv::Op::OpCompositeExtract].push_back(FMixFeedingExtract());
  2659. rules_[spv::Op::OpCompositeInsert].push_back(
  2660. CompositeInsertToCompositeConstruct);
  2661. rules_[spv::Op::OpDot].push_back(DotProductDoingExtract());
  2662. rules_[spv::Op::OpEntryPoint].push_back(RemoveRedundantOperands());
  2663. rules_[spv::Op::OpFAdd].push_back(RedundantFAdd());
  2664. rules_[spv::Op::OpFAdd].push_back(MergeAddNegateArithmetic());
  2665. rules_[spv::Op::OpFAdd].push_back(MergeAddAddArithmetic());
  2666. rules_[spv::Op::OpFAdd].push_back(MergeAddSubArithmetic());
  2667. rules_[spv::Op::OpFAdd].push_back(MergeGenericAddSubArithmetic());
  2668. rules_[spv::Op::OpFAdd].push_back(FactorAddMuls());
  2669. rules_[spv::Op::OpFDiv].push_back(RedundantFDiv());
  2670. rules_[spv::Op::OpFDiv].push_back(ReciprocalFDiv());
  2671. rules_[spv::Op::OpFDiv].push_back(MergeDivDivArithmetic());
  2672. rules_[spv::Op::OpFDiv].push_back(MergeDivMulArithmetic());
  2673. rules_[spv::Op::OpFDiv].push_back(MergeDivNegateArithmetic());
  2674. rules_[spv::Op::OpFMod].push_back(RedundantFMod());
  2675. rules_[spv::Op::OpFMul].push_back(RedundantFMul());
  2676. rules_[spv::Op::OpFMul].push_back(MergeMulMulArithmetic());
  2677. rules_[spv::Op::OpFMul].push_back(MergeMulDivArithmetic());
  2678. rules_[spv::Op::OpFMul].push_back(MergeMulNegateArithmetic());
  2679. rules_[spv::Op::OpFNegate].push_back(MergeNegateArithmetic());
  2680. rules_[spv::Op::OpFNegate].push_back(MergeNegateAddSubArithmetic());
  2681. rules_[spv::Op::OpFNegate].push_back(MergeNegateMulDivArithmetic());
  2682. rules_[spv::Op::OpFSub].push_back(RedundantFSub());
  2683. rules_[spv::Op::OpFSub].push_back(MergeSubNegateArithmetic());
  2684. rules_[spv::Op::OpFSub].push_back(MergeSubAddArithmetic());
  2685. rules_[spv::Op::OpFSub].push_back(MergeSubSubArithmetic());
  2686. rules_[spv::Op::OpIAdd].push_back(MergeAddNegateArithmetic());
  2687. rules_[spv::Op::OpIAdd].push_back(MergeAddAddArithmetic());
  2688. rules_[spv::Op::OpIAdd].push_back(MergeAddSubArithmetic());
  2689. rules_[spv::Op::OpIAdd].push_back(MergeGenericAddSubArithmetic());
  2690. rules_[spv::Op::OpIAdd].push_back(FactorAddMuls());
  2691. rules_[spv::Op::OpIMul].push_back(IntMultipleBy1());
  2692. rules_[spv::Op::OpIMul].push_back(MergeMulMulArithmetic());
  2693. rules_[spv::Op::OpIMul].push_back(MergeMulNegateArithmetic());
  2694. rules_[spv::Op::OpISub].push_back(MergeSubNegateArithmetic());
  2695. rules_[spv::Op::OpISub].push_back(MergeSubAddArithmetic());
  2696. rules_[spv::Op::OpISub].push_back(MergeSubSubArithmetic());
  2697. rules_[spv::Op::OpPhi].push_back(RedundantPhi());
  2698. rules_[spv::Op::OpSNegate].push_back(MergeNegateArithmetic());
  2699. rules_[spv::Op::OpSNegate].push_back(MergeNegateMulDivArithmetic());
  2700. rules_[spv::Op::OpSNegate].push_back(MergeNegateAddSubArithmetic());
  2701. rules_[spv::Op::OpSelect].push_back(RedundantSelect());
  2702. rules_[spv::Op::OpStore].push_back(StoringUndef());
  2703. rules_[spv::Op::OpVectorShuffle].push_back(VectorShuffleFeedingShuffle());
  2704. rules_[spv::Op::OpImageSampleImplicitLod].push_back(UpdateImageOperands());
  2705. rules_[spv::Op::OpImageSampleExplicitLod].push_back(UpdateImageOperands());
  2706. rules_[spv::Op::OpImageSampleDrefImplicitLod].push_back(
  2707. UpdateImageOperands());
  2708. rules_[spv::Op::OpImageSampleDrefExplicitLod].push_back(
  2709. UpdateImageOperands());
  2710. rules_[spv::Op::OpImageSampleProjImplicitLod].push_back(
  2711. UpdateImageOperands());
  2712. rules_[spv::Op::OpImageSampleProjExplicitLod].push_back(
  2713. UpdateImageOperands());
  2714. rules_[spv::Op::OpImageSampleProjDrefImplicitLod].push_back(
  2715. UpdateImageOperands());
  2716. rules_[spv::Op::OpImageSampleProjDrefExplicitLod].push_back(
  2717. UpdateImageOperands());
  2718. rules_[spv::Op::OpImageFetch].push_back(UpdateImageOperands());
  2719. rules_[spv::Op::OpImageGather].push_back(UpdateImageOperands());
  2720. rules_[spv::Op::OpImageDrefGather].push_back(UpdateImageOperands());
  2721. rules_[spv::Op::OpImageRead].push_back(UpdateImageOperands());
  2722. rules_[spv::Op::OpImageWrite].push_back(UpdateImageOperands());
  2723. rules_[spv::Op::OpImageSparseSampleImplicitLod].push_back(
  2724. UpdateImageOperands());
  2725. rules_[spv::Op::OpImageSparseSampleExplicitLod].push_back(
  2726. UpdateImageOperands());
  2727. rules_[spv::Op::OpImageSparseSampleDrefImplicitLod].push_back(
  2728. UpdateImageOperands());
  2729. rules_[spv::Op::OpImageSparseSampleDrefExplicitLod].push_back(
  2730. UpdateImageOperands());
  2731. rules_[spv::Op::OpImageSparseSampleProjImplicitLod].push_back(
  2732. UpdateImageOperands());
  2733. rules_[spv::Op::OpImageSparseSampleProjExplicitLod].push_back(
  2734. UpdateImageOperands());
  2735. rules_[spv::Op::OpImageSparseSampleProjDrefImplicitLod].push_back(
  2736. UpdateImageOperands());
  2737. rules_[spv::Op::OpImageSparseSampleProjDrefExplicitLod].push_back(
  2738. UpdateImageOperands());
  2739. rules_[spv::Op::OpImageSparseFetch].push_back(UpdateImageOperands());
  2740. rules_[spv::Op::OpImageSparseGather].push_back(UpdateImageOperands());
  2741. rules_[spv::Op::OpImageSparseDrefGather].push_back(UpdateImageOperands());
  2742. rules_[spv::Op::OpImageSparseRead].push_back(UpdateImageOperands());
  2743. FeatureManager* feature_manager = context_->get_feature_mgr();
  2744. // Add rules for GLSLstd450
  2745. uint32_t ext_inst_glslstd450_id =
  2746. feature_manager->GetExtInstImportId_GLSLstd450();
  2747. if (ext_inst_glslstd450_id != 0) {
  2748. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FMix}].push_back(
  2749. RedundantFMix());
  2750. }
  2751. }
  2752. } // namespace opt
  2753. } // namespace spvtools