const_folding_rules.cpp 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562
  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/const_folding_rules.h"
  15. #include "source/opt/ir_context.h"
  16. namespace spvtools {
  17. namespace opt {
  18. namespace {
  19. const uint32_t kExtractCompositeIdInIdx = 0;
  20. // Returns a constants with the value NaN of the given type. Only works for
  21. // 32-bit and 64-bit float point types. Returns |nullptr| if an error occurs.
  22. const analysis::Constant* GetNan(const analysis::Type* type,
  23. analysis::ConstantManager* const_mgr) {
  24. const analysis::Float* float_type = type->AsFloat();
  25. if (float_type == nullptr) {
  26. return nullptr;
  27. }
  28. switch (float_type->width()) {
  29. case 32:
  30. return const_mgr->GetFloatConst(std::numeric_limits<float>::quiet_NaN());
  31. case 64:
  32. return const_mgr->GetDoubleConst(
  33. std::numeric_limits<double>::quiet_NaN());
  34. default:
  35. return nullptr;
  36. }
  37. }
  38. // Returns a constants with the value INF of the given type. Only works for
  39. // 32-bit and 64-bit float point types. Returns |nullptr| if an error occurs.
  40. const analysis::Constant* GetInf(const analysis::Type* type,
  41. analysis::ConstantManager* const_mgr) {
  42. const analysis::Float* float_type = type->AsFloat();
  43. if (float_type == nullptr) {
  44. return nullptr;
  45. }
  46. switch (float_type->width()) {
  47. case 32:
  48. return const_mgr->GetFloatConst(std::numeric_limits<float>::infinity());
  49. case 64:
  50. return const_mgr->GetDoubleConst(std::numeric_limits<double>::infinity());
  51. default:
  52. return nullptr;
  53. }
  54. }
  55. // Returns true if |type| is Float or a vector of Float.
  56. bool HasFloatingPoint(const analysis::Type* type) {
  57. if (type->AsFloat()) {
  58. return true;
  59. } else if (const analysis::Vector* vec_type = type->AsVector()) {
  60. return vec_type->element_type()->AsFloat() != nullptr;
  61. }
  62. return false;
  63. }
  64. // Returns a constants with the value |-val| of the given type. Only works for
  65. // 32-bit and 64-bit float point types. Returns |nullptr| if an error occurs.
  66. const analysis::Constant* negateFPConst(const analysis::Type* result_type,
  67. const analysis::Constant* val,
  68. analysis::ConstantManager* const_mgr) {
  69. const analysis::Float* float_type = result_type->AsFloat();
  70. assert(float_type != nullptr);
  71. if (float_type->width() == 32) {
  72. float fa = val->GetFloat();
  73. return const_mgr->GetFloatConst(-fa);
  74. } else if (float_type->width() == 64) {
  75. double da = val->GetDouble();
  76. return const_mgr->GetDoubleConst(-da);
  77. }
  78. return nullptr;
  79. }
  80. // Folds an OpcompositeExtract where input is a composite constant.
  81. ConstantFoldingRule FoldExtractWithConstants() {
  82. return [](IRContext* context, Instruction* inst,
  83. const std::vector<const analysis::Constant*>& constants)
  84. -> const analysis::Constant* {
  85. const analysis::Constant* c = constants[kExtractCompositeIdInIdx];
  86. if (c == nullptr) {
  87. return nullptr;
  88. }
  89. for (uint32_t i = 1; i < inst->NumInOperands(); ++i) {
  90. uint32_t element_index = inst->GetSingleWordInOperand(i);
  91. if (c->AsNullConstant()) {
  92. // Return Null for the return type.
  93. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  94. analysis::TypeManager* type_mgr = context->get_type_mgr();
  95. return const_mgr->GetConstant(type_mgr->GetType(inst->type_id()), {});
  96. }
  97. auto cc = c->AsCompositeConstant();
  98. assert(cc != nullptr);
  99. auto components = cc->GetComponents();
  100. // Protect against invalid IR. Refuse to fold if the index is out
  101. // of bounds.
  102. if (element_index >= components.size()) return nullptr;
  103. c = components[element_index];
  104. }
  105. return c;
  106. };
  107. }
  108. ConstantFoldingRule FoldVectorShuffleWithConstants() {
  109. return [](IRContext* context, Instruction* inst,
  110. const std::vector<const analysis::Constant*>& constants)
  111. -> const analysis::Constant* {
  112. assert(inst->opcode() == SpvOpVectorShuffle);
  113. const analysis::Constant* c1 = constants[0];
  114. const analysis::Constant* c2 = constants[1];
  115. if (c1 == nullptr || c2 == nullptr) {
  116. return nullptr;
  117. }
  118. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  119. const analysis::Type* element_type = c1->type()->AsVector()->element_type();
  120. std::vector<const analysis::Constant*> c1_components;
  121. if (const analysis::VectorConstant* vec_const = c1->AsVectorConstant()) {
  122. c1_components = vec_const->GetComponents();
  123. } else {
  124. assert(c1->AsNullConstant());
  125. const analysis::Constant* element =
  126. const_mgr->GetConstant(element_type, {});
  127. c1_components.resize(c1->type()->AsVector()->element_count(), element);
  128. }
  129. std::vector<const analysis::Constant*> c2_components;
  130. if (const analysis::VectorConstant* vec_const = c2->AsVectorConstant()) {
  131. c2_components = vec_const->GetComponents();
  132. } else {
  133. assert(c2->AsNullConstant());
  134. const analysis::Constant* element =
  135. const_mgr->GetConstant(element_type, {});
  136. c2_components.resize(c2->type()->AsVector()->element_count(), element);
  137. }
  138. std::vector<uint32_t> ids;
  139. const uint32_t undef_literal_value = 0xffffffff;
  140. for (uint32_t i = 2; i < inst->NumInOperands(); ++i) {
  141. uint32_t index = inst->GetSingleWordInOperand(i);
  142. if (index == undef_literal_value) {
  143. // Don't fold shuffle with undef literal value.
  144. return nullptr;
  145. } else if (index < c1_components.size()) {
  146. Instruction* member_inst =
  147. const_mgr->GetDefiningInstruction(c1_components[index]);
  148. ids.push_back(member_inst->result_id());
  149. } else {
  150. Instruction* member_inst = const_mgr->GetDefiningInstruction(
  151. c2_components[index - c1_components.size()]);
  152. ids.push_back(member_inst->result_id());
  153. }
  154. }
  155. analysis::TypeManager* type_mgr = context->get_type_mgr();
  156. return const_mgr->GetConstant(type_mgr->GetType(inst->type_id()), ids);
  157. };
  158. }
  159. ConstantFoldingRule FoldVectorTimesScalar() {
  160. return [](IRContext* context, Instruction* inst,
  161. const std::vector<const analysis::Constant*>& constants)
  162. -> const analysis::Constant* {
  163. assert(inst->opcode() == SpvOpVectorTimesScalar);
  164. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  165. analysis::TypeManager* type_mgr = context->get_type_mgr();
  166. if (!inst->IsFloatingPointFoldingAllowed()) {
  167. if (HasFloatingPoint(type_mgr->GetType(inst->type_id()))) {
  168. return nullptr;
  169. }
  170. }
  171. const analysis::Constant* c1 = constants[0];
  172. const analysis::Constant* c2 = constants[1];
  173. if (c1 && c1->IsZero()) {
  174. return c1;
  175. }
  176. if (c2 && c2->IsZero()) {
  177. // Get or create the NullConstant for this type.
  178. std::vector<uint32_t> ids;
  179. return const_mgr->GetConstant(type_mgr->GetType(inst->type_id()), ids);
  180. }
  181. if (c1 == nullptr || c2 == nullptr) {
  182. return nullptr;
  183. }
  184. // Check result type.
  185. const analysis::Type* result_type = type_mgr->GetType(inst->type_id());
  186. const analysis::Vector* vector_type = result_type->AsVector();
  187. assert(vector_type != nullptr);
  188. const analysis::Type* element_type = vector_type->element_type();
  189. assert(element_type != nullptr);
  190. const analysis::Float* float_type = element_type->AsFloat();
  191. assert(float_type != nullptr);
  192. // Check types of c1 and c2.
  193. assert(c1->type()->AsVector() == vector_type);
  194. assert(c1->type()->AsVector()->element_type() == element_type &&
  195. c2->type() == element_type);
  196. // Get a float vector that is the result of vector-times-scalar.
  197. std::vector<const analysis::Constant*> c1_components =
  198. c1->GetVectorComponents(const_mgr);
  199. std::vector<uint32_t> ids;
  200. if (float_type->width() == 32) {
  201. float scalar = c2->GetFloat();
  202. for (uint32_t i = 0; i < c1_components.size(); ++i) {
  203. utils::FloatProxy<float> result(c1_components[i]->GetFloat() * scalar);
  204. std::vector<uint32_t> words = result.GetWords();
  205. const analysis::Constant* new_elem =
  206. const_mgr->GetConstant(float_type, words);
  207. ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
  208. }
  209. return const_mgr->GetConstant(vector_type, ids);
  210. } else if (float_type->width() == 64) {
  211. double scalar = c2->GetDouble();
  212. for (uint32_t i = 0; i < c1_components.size(); ++i) {
  213. utils::FloatProxy<double> result(c1_components[i]->GetDouble() *
  214. scalar);
  215. std::vector<uint32_t> words = result.GetWords();
  216. const analysis::Constant* new_elem =
  217. const_mgr->GetConstant(float_type, words);
  218. ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
  219. }
  220. return const_mgr->GetConstant(vector_type, ids);
  221. }
  222. return nullptr;
  223. };
  224. }
  225. ConstantFoldingRule FoldVectorTimesMatrix() {
  226. return [](IRContext* context, Instruction* inst,
  227. const std::vector<const analysis::Constant*>& constants)
  228. -> const analysis::Constant* {
  229. assert(inst->opcode() == SpvOpVectorTimesMatrix);
  230. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  231. analysis::TypeManager* type_mgr = context->get_type_mgr();
  232. if (!inst->IsFloatingPointFoldingAllowed()) {
  233. if (HasFloatingPoint(type_mgr->GetType(inst->type_id()))) {
  234. return nullptr;
  235. }
  236. }
  237. const analysis::Constant* c1 = constants[0];
  238. const analysis::Constant* c2 = constants[1];
  239. if (c1 == nullptr || c2 == nullptr) {
  240. return nullptr;
  241. }
  242. // Check result type.
  243. const analysis::Type* result_type = type_mgr->GetType(inst->type_id());
  244. const analysis::Vector* vector_type = result_type->AsVector();
  245. assert(vector_type != nullptr);
  246. const analysis::Type* element_type = vector_type->element_type();
  247. assert(element_type != nullptr);
  248. const analysis::Float* float_type = element_type->AsFloat();
  249. assert(float_type != nullptr);
  250. // Check types of c1 and c2.
  251. assert(c1->type()->AsVector() == vector_type);
  252. assert(c1->type()->AsVector()->element_type() == element_type &&
  253. c2->type()->AsMatrix()->element_type() == vector_type);
  254. // Get a float vector that is the result of vector-times-matrix.
  255. std::vector<const analysis::Constant*> c1_components =
  256. c1->GetVectorComponents(const_mgr);
  257. std::vector<const analysis::Constant*> c2_components =
  258. c2->AsMatrixConstant()->GetComponents();
  259. uint32_t resultVectorSize = result_type->AsVector()->element_count();
  260. std::vector<uint32_t> ids;
  261. if ((c1 && c1->IsZero()) || (c2 && c2->IsZero())) {
  262. std::vector<uint32_t> words(float_type->width() / 32, 0);
  263. for (uint32_t i = 0; i < resultVectorSize; ++i) {
  264. const analysis::Constant* new_elem =
  265. const_mgr->GetConstant(float_type, words);
  266. ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
  267. }
  268. return const_mgr->GetConstant(vector_type, ids);
  269. }
  270. if (float_type->width() == 32) {
  271. for (uint32_t i = 0; i < resultVectorSize; ++i) {
  272. float result_scalar = 0.0f;
  273. const analysis::VectorConstant* c2_vec =
  274. c2_components[i]->AsVectorConstant();
  275. for (uint32_t j = 0; j < c2_vec->GetComponents().size(); ++j) {
  276. float c1_scalar = c1_components[j]->GetFloat();
  277. float c2_scalar = c2_vec->GetComponents()[j]->GetFloat();
  278. result_scalar += c1_scalar * c2_scalar;
  279. }
  280. utils::FloatProxy<float> result(result_scalar);
  281. std::vector<uint32_t> words = result.GetWords();
  282. const analysis::Constant* new_elem =
  283. const_mgr->GetConstant(float_type, words);
  284. ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
  285. }
  286. return const_mgr->GetConstant(vector_type, ids);
  287. } else if (float_type->width() == 64) {
  288. for (uint32_t i = 0; i < c2_components.size(); ++i) {
  289. double result_scalar = 0.0;
  290. const analysis::VectorConstant* c2_vec =
  291. c2_components[i]->AsVectorConstant();
  292. for (uint32_t j = 0; j < c2_vec->GetComponents().size(); ++j) {
  293. double c1_scalar = c1_components[j]->GetDouble();
  294. double c2_scalar = c2_vec->GetComponents()[j]->GetDouble();
  295. result_scalar += c1_scalar * c2_scalar;
  296. }
  297. utils::FloatProxy<double> result(result_scalar);
  298. std::vector<uint32_t> words = result.GetWords();
  299. const analysis::Constant* new_elem =
  300. const_mgr->GetConstant(float_type, words);
  301. ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
  302. }
  303. return const_mgr->GetConstant(vector_type, ids);
  304. }
  305. return nullptr;
  306. };
  307. }
  308. ConstantFoldingRule FoldMatrixTimesVector() {
  309. return [](IRContext* context, Instruction* inst,
  310. const std::vector<const analysis::Constant*>& constants)
  311. -> const analysis::Constant* {
  312. assert(inst->opcode() == SpvOpMatrixTimesVector);
  313. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  314. analysis::TypeManager* type_mgr = context->get_type_mgr();
  315. if (!inst->IsFloatingPointFoldingAllowed()) {
  316. if (HasFloatingPoint(type_mgr->GetType(inst->type_id()))) {
  317. return nullptr;
  318. }
  319. }
  320. const analysis::Constant* c1 = constants[0];
  321. const analysis::Constant* c2 = constants[1];
  322. if (c1 == nullptr || c2 == nullptr) {
  323. return nullptr;
  324. }
  325. // Check result type.
  326. const analysis::Type* result_type = type_mgr->GetType(inst->type_id());
  327. const analysis::Vector* vector_type = result_type->AsVector();
  328. assert(vector_type != nullptr);
  329. const analysis::Type* element_type = vector_type->element_type();
  330. assert(element_type != nullptr);
  331. const analysis::Float* float_type = element_type->AsFloat();
  332. assert(float_type != nullptr);
  333. // Check types of c1 and c2.
  334. assert(c1->type()->AsMatrix()->element_type() == vector_type);
  335. assert(c2->type()->AsVector()->element_type() == element_type);
  336. // Get a float vector that is the result of matrix-times-vector.
  337. std::vector<const analysis::Constant*> c1_components =
  338. c1->AsMatrixConstant()->GetComponents();
  339. std::vector<const analysis::Constant*> c2_components =
  340. c2->GetVectorComponents(const_mgr);
  341. uint32_t resultVectorSize = result_type->AsVector()->element_count();
  342. std::vector<uint32_t> ids;
  343. if ((c1 && c1->IsZero()) || (c2 && c2->IsZero())) {
  344. std::vector<uint32_t> words(float_type->width() / 32, 0);
  345. for (uint32_t i = 0; i < resultVectorSize; ++i) {
  346. const analysis::Constant* new_elem =
  347. const_mgr->GetConstant(float_type, words);
  348. ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
  349. }
  350. return const_mgr->GetConstant(vector_type, ids);
  351. }
  352. if (float_type->width() == 32) {
  353. for (uint32_t i = 0; i < resultVectorSize; ++i) {
  354. float result_scalar = 0.0f;
  355. for (uint32_t j = 0; j < c1_components.size(); ++j) {
  356. float c1_scalar = c1_components[j]
  357. ->AsVectorConstant()
  358. ->GetComponents()[i]
  359. ->GetFloat();
  360. float c2_scalar = c2_components[j]->GetFloat();
  361. result_scalar += c1_scalar * c2_scalar;
  362. }
  363. utils::FloatProxy<float> result(result_scalar);
  364. std::vector<uint32_t> words = result.GetWords();
  365. const analysis::Constant* new_elem =
  366. const_mgr->GetConstant(float_type, words);
  367. ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
  368. }
  369. return const_mgr->GetConstant(vector_type, ids);
  370. } else if (float_type->width() == 64) {
  371. for (uint32_t i = 0; i < resultVectorSize; ++i) {
  372. double result_scalar = 0.0;
  373. for (uint32_t j = 0; j < c1_components.size(); ++j) {
  374. double c1_scalar = c1_components[j]
  375. ->AsVectorConstant()
  376. ->GetComponents()[i]
  377. ->GetDouble();
  378. double c2_scalar = c2_components[j]->GetDouble();
  379. result_scalar += c1_scalar * c2_scalar;
  380. }
  381. utils::FloatProxy<double> result(result_scalar);
  382. std::vector<uint32_t> words = result.GetWords();
  383. const analysis::Constant* new_elem =
  384. const_mgr->GetConstant(float_type, words);
  385. ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
  386. }
  387. return const_mgr->GetConstant(vector_type, ids);
  388. }
  389. return nullptr;
  390. };
  391. }
  392. ConstantFoldingRule FoldCompositeWithConstants() {
  393. // Folds an OpCompositeConstruct where all of the inputs are constants to a
  394. // constant. A new constant is created if necessary.
  395. return [](IRContext* context, Instruction* inst,
  396. const std::vector<const analysis::Constant*>& constants)
  397. -> const analysis::Constant* {
  398. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  399. analysis::TypeManager* type_mgr = context->get_type_mgr();
  400. const analysis::Type* new_type = type_mgr->GetType(inst->type_id());
  401. Instruction* type_inst =
  402. context->get_def_use_mgr()->GetDef(inst->type_id());
  403. std::vector<uint32_t> ids;
  404. for (uint32_t i = 0; i < constants.size(); ++i) {
  405. const analysis::Constant* element_const = constants[i];
  406. if (element_const == nullptr) {
  407. return nullptr;
  408. }
  409. uint32_t component_type_id = 0;
  410. if (type_inst->opcode() == SpvOpTypeStruct) {
  411. component_type_id = type_inst->GetSingleWordInOperand(i);
  412. } else if (type_inst->opcode() == SpvOpTypeArray) {
  413. component_type_id = type_inst->GetSingleWordInOperand(0);
  414. }
  415. uint32_t element_id =
  416. const_mgr->FindDeclaredConstant(element_const, component_type_id);
  417. if (element_id == 0) {
  418. return nullptr;
  419. }
  420. ids.push_back(element_id);
  421. }
  422. return const_mgr->GetConstant(new_type, ids);
  423. };
  424. }
  425. // The interface for a function that returns the result of applying a scalar
  426. // floating-point binary operation on |a| and |b|. The type of the return value
  427. // will be |type|. The input constants must also be of type |type|.
  428. using UnaryScalarFoldingRule = std::function<const analysis::Constant*(
  429. const analysis::Type* result_type, const analysis::Constant* a,
  430. analysis::ConstantManager*)>;
  431. // The interface for a function that returns the result of applying a scalar
  432. // floating-point binary operation on |a| and |b|. The type of the return value
  433. // will be |type|. The input constants must also be of type |type|.
  434. using BinaryScalarFoldingRule = std::function<const analysis::Constant*(
  435. const analysis::Type* result_type, const analysis::Constant* a,
  436. const analysis::Constant* b, analysis::ConstantManager*)>;
  437. // Returns a |ConstantFoldingRule| that folds unary floating point scalar ops
  438. // using |scalar_rule| and unary float point vectors ops by applying
  439. // |scalar_rule| to the elements of the vector. The |ConstantFoldingRule|
  440. // that is returned assumes that |constants| contains 1 entry. If they are
  441. // not |nullptr|, then their type is either |Float| or |Integer| or a |Vector|
  442. // whose element type is |Float| or |Integer|.
  443. ConstantFoldingRule FoldFPUnaryOp(UnaryScalarFoldingRule scalar_rule) {
  444. return [scalar_rule](IRContext* context, Instruction* inst,
  445. const std::vector<const analysis::Constant*>& constants)
  446. -> const analysis::Constant* {
  447. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  448. analysis::TypeManager* type_mgr = context->get_type_mgr();
  449. const analysis::Type* result_type = type_mgr->GetType(inst->type_id());
  450. const analysis::Vector* vector_type = result_type->AsVector();
  451. if (!inst->IsFloatingPointFoldingAllowed()) {
  452. return nullptr;
  453. }
  454. const analysis::Constant* arg =
  455. (inst->opcode() == SpvOpExtInst) ? constants[1] : constants[0];
  456. if (arg == nullptr) {
  457. return nullptr;
  458. }
  459. if (vector_type != nullptr) {
  460. std::vector<const analysis::Constant*> a_components;
  461. std::vector<const analysis::Constant*> results_components;
  462. a_components = arg->GetVectorComponents(const_mgr);
  463. // Fold each component of the vector.
  464. for (uint32_t i = 0; i < a_components.size(); ++i) {
  465. results_components.push_back(scalar_rule(vector_type->element_type(),
  466. a_components[i], const_mgr));
  467. if (results_components[i] == nullptr) {
  468. return nullptr;
  469. }
  470. }
  471. // Build the constant object and return it.
  472. std::vector<uint32_t> ids;
  473. for (const analysis::Constant* member : results_components) {
  474. ids.push_back(const_mgr->GetDefiningInstruction(member)->result_id());
  475. }
  476. return const_mgr->GetConstant(vector_type, ids);
  477. } else {
  478. return scalar_rule(result_type, arg, const_mgr);
  479. }
  480. };
  481. }
  482. // Returns the result of folding the constants in |constants| according the
  483. // |scalar_rule|. If |result_type| is a vector, then |scalar_rule| is applied
  484. // per component.
  485. const analysis::Constant* FoldFPBinaryOp(
  486. BinaryScalarFoldingRule scalar_rule, uint32_t result_type_id,
  487. const std::vector<const analysis::Constant*>& constants,
  488. IRContext* context) {
  489. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  490. analysis::TypeManager* type_mgr = context->get_type_mgr();
  491. const analysis::Type* result_type = type_mgr->GetType(result_type_id);
  492. const analysis::Vector* vector_type = result_type->AsVector();
  493. if (constants[0] == nullptr || constants[1] == nullptr) {
  494. return nullptr;
  495. }
  496. if (vector_type != nullptr) {
  497. std::vector<const analysis::Constant*> a_components;
  498. std::vector<const analysis::Constant*> b_components;
  499. std::vector<const analysis::Constant*> results_components;
  500. a_components = constants[0]->GetVectorComponents(const_mgr);
  501. b_components = constants[1]->GetVectorComponents(const_mgr);
  502. // Fold each component of the vector.
  503. for (uint32_t i = 0; i < a_components.size(); ++i) {
  504. results_components.push_back(scalar_rule(vector_type->element_type(),
  505. a_components[i], b_components[i],
  506. const_mgr));
  507. if (results_components[i] == nullptr) {
  508. return nullptr;
  509. }
  510. }
  511. // Build the constant object and return it.
  512. std::vector<uint32_t> ids;
  513. for (const analysis::Constant* member : results_components) {
  514. ids.push_back(const_mgr->GetDefiningInstruction(member)->result_id());
  515. }
  516. return const_mgr->GetConstant(vector_type, ids);
  517. } else {
  518. return scalar_rule(result_type, constants[0], constants[1], const_mgr);
  519. }
  520. }
  521. // Returns a |ConstantFoldingRule| that folds floating point scalars using
  522. // |scalar_rule| and vectors of floating point by applying |scalar_rule| to the
  523. // elements of the vector. The |ConstantFoldingRule| that is returned assumes
  524. // that |constants| contains 2 entries. If they are not |nullptr|, then their
  525. // type is either |Float| or a |Vector| whose element type is |Float|.
  526. ConstantFoldingRule FoldFPBinaryOp(BinaryScalarFoldingRule scalar_rule) {
  527. return [scalar_rule](IRContext* context, Instruction* inst,
  528. const std::vector<const analysis::Constant*>& constants)
  529. -> const analysis::Constant* {
  530. if (!inst->IsFloatingPointFoldingAllowed()) {
  531. return nullptr;
  532. }
  533. if (inst->opcode() == SpvOpExtInst) {
  534. return FoldFPBinaryOp(scalar_rule, inst->type_id(),
  535. {constants[1], constants[2]}, context);
  536. }
  537. return FoldFPBinaryOp(scalar_rule, inst->type_id(), constants, context);
  538. };
  539. }
  540. // This macro defines a |UnaryScalarFoldingRule| that performs float to
  541. // integer conversion.
  542. // TODO(greg-lunarg): Support for 64-bit integer types.
  543. UnaryScalarFoldingRule FoldFToIOp() {
  544. return [](const analysis::Type* result_type, const analysis::Constant* a,
  545. analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
  546. assert(result_type != nullptr && a != nullptr);
  547. const analysis::Integer* integer_type = result_type->AsInteger();
  548. const analysis::Float* float_type = a->type()->AsFloat();
  549. assert(float_type != nullptr);
  550. assert(integer_type != nullptr);
  551. if (integer_type->width() != 32) return nullptr;
  552. if (float_type->width() == 32) {
  553. float fa = a->GetFloat();
  554. uint32_t result = integer_type->IsSigned()
  555. ? static_cast<uint32_t>(static_cast<int32_t>(fa))
  556. : static_cast<uint32_t>(fa);
  557. std::vector<uint32_t> words = {result};
  558. return const_mgr->GetConstant(result_type, words);
  559. } else if (float_type->width() == 64) {
  560. double fa = a->GetDouble();
  561. uint32_t result = integer_type->IsSigned()
  562. ? static_cast<uint32_t>(static_cast<int32_t>(fa))
  563. : static_cast<uint32_t>(fa);
  564. std::vector<uint32_t> words = {result};
  565. return const_mgr->GetConstant(result_type, words);
  566. }
  567. return nullptr;
  568. };
  569. }
  570. // This function defines a |UnaryScalarFoldingRule| that performs integer to
  571. // float conversion.
  572. // TODO(greg-lunarg): Support for 64-bit integer types.
  573. UnaryScalarFoldingRule FoldIToFOp() {
  574. return [](const analysis::Type* result_type, const analysis::Constant* a,
  575. analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
  576. assert(result_type != nullptr && a != nullptr);
  577. const analysis::Integer* integer_type = a->type()->AsInteger();
  578. const analysis::Float* float_type = result_type->AsFloat();
  579. assert(float_type != nullptr);
  580. assert(integer_type != nullptr);
  581. if (integer_type->width() != 32) return nullptr;
  582. uint32_t ua = a->GetU32();
  583. if (float_type->width() == 32) {
  584. float result_val = integer_type->IsSigned()
  585. ? static_cast<float>(static_cast<int32_t>(ua))
  586. : static_cast<float>(ua);
  587. utils::FloatProxy<float> result(result_val);
  588. std::vector<uint32_t> words = {result.data()};
  589. return const_mgr->GetConstant(result_type, words);
  590. } else if (float_type->width() == 64) {
  591. double result_val = integer_type->IsSigned()
  592. ? static_cast<double>(static_cast<int32_t>(ua))
  593. : static_cast<double>(ua);
  594. utils::FloatProxy<double> result(result_val);
  595. std::vector<uint32_t> words = result.GetWords();
  596. return const_mgr->GetConstant(result_type, words);
  597. }
  598. return nullptr;
  599. };
  600. }
  601. // This defines a |UnaryScalarFoldingRule| that performs |OpQuantizeToF16|.
  602. UnaryScalarFoldingRule FoldQuantizeToF16Scalar() {
  603. return [](const analysis::Type* result_type, const analysis::Constant* a,
  604. analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
  605. assert(result_type != nullptr && a != nullptr);
  606. const analysis::Float* float_type = a->type()->AsFloat();
  607. assert(float_type != nullptr);
  608. if (float_type->width() != 32) {
  609. return nullptr;
  610. }
  611. float fa = a->GetFloat();
  612. utils::HexFloat<utils::FloatProxy<float>> orignal(fa);
  613. utils::HexFloat<utils::FloatProxy<utils::Float16>> quantized(0);
  614. utils::HexFloat<utils::FloatProxy<float>> result(0.0f);
  615. orignal.castTo(quantized, utils::round_direction::kToZero);
  616. quantized.castTo(result, utils::round_direction::kToZero);
  617. std::vector<uint32_t> words = {result.getBits()};
  618. return const_mgr->GetConstant(result_type, words);
  619. };
  620. }
  621. // This macro defines a |BinaryScalarFoldingRule| that applies |op|. The
  622. // operator |op| must work for both float and double, and use syntax "f1 op f2".
  623. #define FOLD_FPARITH_OP(op) \
  624. [](const analysis::Type* result_type_in_macro, const analysis::Constant* a, \
  625. const analysis::Constant* b, \
  626. analysis::ConstantManager* const_mgr_in_macro) \
  627. -> const analysis::Constant* { \
  628. assert(result_type_in_macro != nullptr && a != nullptr && b != nullptr); \
  629. assert(result_type_in_macro == a->type() && \
  630. result_type_in_macro == b->type()); \
  631. const analysis::Float* float_type_in_macro = \
  632. result_type_in_macro->AsFloat(); \
  633. assert(float_type_in_macro != nullptr); \
  634. if (float_type_in_macro->width() == 32) { \
  635. float fa = a->GetFloat(); \
  636. float fb = b->GetFloat(); \
  637. utils::FloatProxy<float> result_in_macro(fa op fb); \
  638. std::vector<uint32_t> words_in_macro = result_in_macro.GetWords(); \
  639. return const_mgr_in_macro->GetConstant(result_type_in_macro, \
  640. words_in_macro); \
  641. } else if (float_type_in_macro->width() == 64) { \
  642. double fa = a->GetDouble(); \
  643. double fb = b->GetDouble(); \
  644. utils::FloatProxy<double> result_in_macro(fa op fb); \
  645. std::vector<uint32_t> words_in_macro = result_in_macro.GetWords(); \
  646. return const_mgr_in_macro->GetConstant(result_type_in_macro, \
  647. words_in_macro); \
  648. } \
  649. return nullptr; \
  650. }
  651. // Define the folding rule for conversion between floating point and integer
  652. ConstantFoldingRule FoldFToI() { return FoldFPUnaryOp(FoldFToIOp()); }
  653. ConstantFoldingRule FoldIToF() { return FoldFPUnaryOp(FoldIToFOp()); }
  654. ConstantFoldingRule FoldQuantizeToF16() {
  655. return FoldFPUnaryOp(FoldQuantizeToF16Scalar());
  656. }
  657. // Define the folding rules for subtraction, addition, multiplication, and
  658. // division for floating point values.
  659. ConstantFoldingRule FoldFSub() { return FoldFPBinaryOp(FOLD_FPARITH_OP(-)); }
  660. ConstantFoldingRule FoldFAdd() { return FoldFPBinaryOp(FOLD_FPARITH_OP(+)); }
  661. ConstantFoldingRule FoldFMul() { return FoldFPBinaryOp(FOLD_FPARITH_OP(*)); }
  662. // Returns the constant that results from evaluating |numerator| / 0.0. Returns
  663. // |nullptr| if the result could not be evaluated.
  664. const analysis::Constant* FoldFPScalarDivideByZero(
  665. const analysis::Type* result_type, const analysis::Constant* numerator,
  666. analysis::ConstantManager* const_mgr) {
  667. if (numerator == nullptr) {
  668. return nullptr;
  669. }
  670. if (numerator->IsZero()) {
  671. return GetNan(result_type, const_mgr);
  672. }
  673. const analysis::Constant* result = GetInf(result_type, const_mgr);
  674. if (result == nullptr) {
  675. return nullptr;
  676. }
  677. if (numerator->AsFloatConstant()->GetValueAsDouble() < 0.0) {
  678. result = negateFPConst(result_type, result, const_mgr);
  679. }
  680. return result;
  681. }
  682. // Returns the result of folding |numerator| / |denominator|. Returns |nullptr|
  683. // if it cannot be folded.
  684. const analysis::Constant* FoldScalarFPDivide(
  685. const analysis::Type* result_type, const analysis::Constant* numerator,
  686. const analysis::Constant* denominator,
  687. analysis::ConstantManager* const_mgr) {
  688. if (denominator == nullptr) {
  689. return nullptr;
  690. }
  691. if (denominator->IsZero()) {
  692. return FoldFPScalarDivideByZero(result_type, numerator, const_mgr);
  693. }
  694. const analysis::FloatConstant* denominator_float =
  695. denominator->AsFloatConstant();
  696. if (denominator_float && denominator->GetValueAsDouble() == -0.0) {
  697. const analysis::Constant* result =
  698. FoldFPScalarDivideByZero(result_type, numerator, const_mgr);
  699. if (result != nullptr)
  700. result = negateFPConst(result_type, result, const_mgr);
  701. return result;
  702. } else {
  703. return FOLD_FPARITH_OP(/)(result_type, numerator, denominator, const_mgr);
  704. }
  705. }
  706. // Returns the constant folding rule to fold |OpFDiv| with two constants.
  707. ConstantFoldingRule FoldFDiv() { return FoldFPBinaryOp(FoldScalarFPDivide); }
  708. bool CompareFloatingPoint(bool op_result, bool op_unordered,
  709. bool need_ordered) {
  710. if (need_ordered) {
  711. // operands are ordered and Operand 1 is |op| Operand 2
  712. return !op_unordered && op_result;
  713. } else {
  714. // operands are unordered or Operand 1 is |op| Operand 2
  715. return op_unordered || op_result;
  716. }
  717. }
  718. // This macro defines a |BinaryScalarFoldingRule| that applies |op|. The
  719. // operator |op| must work for both float and double, and use syntax "f1 op f2".
  720. #define FOLD_FPCMP_OP(op, ord) \
  721. [](const analysis::Type* result_type, const analysis::Constant* a, \
  722. const analysis::Constant* b, \
  723. analysis::ConstantManager* const_mgr) -> const analysis::Constant* { \
  724. assert(result_type != nullptr && a != nullptr && b != nullptr); \
  725. assert(result_type->AsBool()); \
  726. assert(a->type() == b->type()); \
  727. const analysis::Float* float_type = a->type()->AsFloat(); \
  728. assert(float_type != nullptr); \
  729. if (float_type->width() == 32) { \
  730. float fa = a->GetFloat(); \
  731. float fb = b->GetFloat(); \
  732. bool result = CompareFloatingPoint( \
  733. fa op fb, std::isnan(fa) || std::isnan(fb), ord); \
  734. std::vector<uint32_t> words = {uint32_t(result)}; \
  735. return const_mgr->GetConstant(result_type, words); \
  736. } else if (float_type->width() == 64) { \
  737. double fa = a->GetDouble(); \
  738. double fb = b->GetDouble(); \
  739. bool result = CompareFloatingPoint( \
  740. fa op fb, std::isnan(fa) || std::isnan(fb), ord); \
  741. std::vector<uint32_t> words = {uint32_t(result)}; \
  742. return const_mgr->GetConstant(result_type, words); \
  743. } \
  744. return nullptr; \
  745. }
  746. // Define the folding rules for ordered and unordered comparison for floating
  747. // point values.
  748. ConstantFoldingRule FoldFOrdEqual() {
  749. return FoldFPBinaryOp(FOLD_FPCMP_OP(==, true));
  750. }
  751. ConstantFoldingRule FoldFUnordEqual() {
  752. return FoldFPBinaryOp(FOLD_FPCMP_OP(==, false));
  753. }
  754. ConstantFoldingRule FoldFOrdNotEqual() {
  755. return FoldFPBinaryOp(FOLD_FPCMP_OP(!=, true));
  756. }
  757. ConstantFoldingRule FoldFUnordNotEqual() {
  758. return FoldFPBinaryOp(FOLD_FPCMP_OP(!=, false));
  759. }
  760. ConstantFoldingRule FoldFOrdLessThan() {
  761. return FoldFPBinaryOp(FOLD_FPCMP_OP(<, true));
  762. }
  763. ConstantFoldingRule FoldFUnordLessThan() {
  764. return FoldFPBinaryOp(FOLD_FPCMP_OP(<, false));
  765. }
  766. ConstantFoldingRule FoldFOrdGreaterThan() {
  767. return FoldFPBinaryOp(FOLD_FPCMP_OP(>, true));
  768. }
  769. ConstantFoldingRule FoldFUnordGreaterThan() {
  770. return FoldFPBinaryOp(FOLD_FPCMP_OP(>, false));
  771. }
  772. ConstantFoldingRule FoldFOrdLessThanEqual() {
  773. return FoldFPBinaryOp(FOLD_FPCMP_OP(<=, true));
  774. }
  775. ConstantFoldingRule FoldFUnordLessThanEqual() {
  776. return FoldFPBinaryOp(FOLD_FPCMP_OP(<=, false));
  777. }
  778. ConstantFoldingRule FoldFOrdGreaterThanEqual() {
  779. return FoldFPBinaryOp(FOLD_FPCMP_OP(>=, true));
  780. }
  781. ConstantFoldingRule FoldFUnordGreaterThanEqual() {
  782. return FoldFPBinaryOp(FOLD_FPCMP_OP(>=, false));
  783. }
  784. // Folds an OpDot where all of the inputs are constants to a
  785. // constant. A new constant is created if necessary.
  786. ConstantFoldingRule FoldOpDotWithConstants() {
  787. return [](IRContext* context, Instruction* inst,
  788. const std::vector<const analysis::Constant*>& constants)
  789. -> const analysis::Constant* {
  790. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  791. analysis::TypeManager* type_mgr = context->get_type_mgr();
  792. const analysis::Type* new_type = type_mgr->GetType(inst->type_id());
  793. assert(new_type->AsFloat() && "OpDot should have a float return type.");
  794. const analysis::Float* float_type = new_type->AsFloat();
  795. if (!inst->IsFloatingPointFoldingAllowed()) {
  796. return nullptr;
  797. }
  798. // If one of the operands is 0, then the result is 0.
  799. bool has_zero_operand = false;
  800. for (int i = 0; i < 2; ++i) {
  801. if (constants[i]) {
  802. if (constants[i]->AsNullConstant() ||
  803. constants[i]->AsVectorConstant()->IsZero()) {
  804. has_zero_operand = true;
  805. break;
  806. }
  807. }
  808. }
  809. if (has_zero_operand) {
  810. if (float_type->width() == 32) {
  811. utils::FloatProxy<float> result(0.0f);
  812. std::vector<uint32_t> words = result.GetWords();
  813. return const_mgr->GetConstant(float_type, words);
  814. }
  815. if (float_type->width() == 64) {
  816. utils::FloatProxy<double> result(0.0);
  817. std::vector<uint32_t> words = result.GetWords();
  818. return const_mgr->GetConstant(float_type, words);
  819. }
  820. return nullptr;
  821. }
  822. if (constants[0] == nullptr || constants[1] == nullptr) {
  823. return nullptr;
  824. }
  825. std::vector<const analysis::Constant*> a_components;
  826. std::vector<const analysis::Constant*> b_components;
  827. a_components = constants[0]->GetVectorComponents(const_mgr);
  828. b_components = constants[1]->GetVectorComponents(const_mgr);
  829. utils::FloatProxy<double> result(0.0);
  830. std::vector<uint32_t> words = result.GetWords();
  831. const analysis::Constant* result_const =
  832. const_mgr->GetConstant(float_type, words);
  833. for (uint32_t i = 0; i < a_components.size() && result_const != nullptr;
  834. ++i) {
  835. if (a_components[i] == nullptr || b_components[i] == nullptr) {
  836. return nullptr;
  837. }
  838. const analysis::Constant* component = FOLD_FPARITH_OP(*)(
  839. new_type, a_components[i], b_components[i], const_mgr);
  840. if (component == nullptr) {
  841. return nullptr;
  842. }
  843. result_const =
  844. FOLD_FPARITH_OP(+)(new_type, result_const, component, const_mgr);
  845. }
  846. return result_const;
  847. };
  848. }
  849. // This function defines a |UnaryScalarFoldingRule| that subtracts the constant
  850. // from zero.
  851. UnaryScalarFoldingRule FoldFNegateOp() {
  852. return [](const analysis::Type* result_type, const analysis::Constant* a,
  853. analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
  854. assert(result_type != nullptr && a != nullptr);
  855. assert(result_type == a->type());
  856. return negateFPConst(result_type, a, const_mgr);
  857. };
  858. }
  859. ConstantFoldingRule FoldFNegate() { return FoldFPUnaryOp(FoldFNegateOp()); }
  860. ConstantFoldingRule FoldFClampFeedingCompare(uint32_t cmp_opcode) {
  861. return [cmp_opcode](IRContext* context, Instruction* inst,
  862. const std::vector<const analysis::Constant*>& constants)
  863. -> const analysis::Constant* {
  864. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  865. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  866. if (!inst->IsFloatingPointFoldingAllowed()) {
  867. return nullptr;
  868. }
  869. uint32_t non_const_idx = (constants[0] ? 1 : 0);
  870. uint32_t operand_id = inst->GetSingleWordInOperand(non_const_idx);
  871. Instruction* operand_inst = def_use_mgr->GetDef(operand_id);
  872. analysis::TypeManager* type_mgr = context->get_type_mgr();
  873. const analysis::Type* operand_type =
  874. type_mgr->GetType(operand_inst->type_id());
  875. if (!operand_type->AsFloat()) {
  876. return nullptr;
  877. }
  878. if (operand_type->AsFloat()->width() != 32 &&
  879. operand_type->AsFloat()->width() != 64) {
  880. return nullptr;
  881. }
  882. if (operand_inst->opcode() != SpvOpExtInst) {
  883. return nullptr;
  884. }
  885. if (operand_inst->GetSingleWordInOperand(1) != GLSLstd450FClamp) {
  886. return nullptr;
  887. }
  888. if (constants[1] == nullptr && constants[0] == nullptr) {
  889. return nullptr;
  890. }
  891. uint32_t max_id = operand_inst->GetSingleWordInOperand(4);
  892. const analysis::Constant* max_const =
  893. const_mgr->FindDeclaredConstant(max_id);
  894. uint32_t min_id = operand_inst->GetSingleWordInOperand(3);
  895. const analysis::Constant* min_const =
  896. const_mgr->FindDeclaredConstant(min_id);
  897. bool found_result = false;
  898. bool result = false;
  899. switch (cmp_opcode) {
  900. case SpvOpFOrdLessThan:
  901. case SpvOpFUnordLessThan:
  902. case SpvOpFOrdGreaterThanEqual:
  903. case SpvOpFUnordGreaterThanEqual:
  904. if (constants[0]) {
  905. if (min_const) {
  906. if (constants[0]->GetValueAsDouble() <
  907. min_const->GetValueAsDouble()) {
  908. found_result = true;
  909. result = (cmp_opcode == SpvOpFOrdLessThan ||
  910. cmp_opcode == SpvOpFUnordLessThan);
  911. }
  912. }
  913. if (max_const) {
  914. if (constants[0]->GetValueAsDouble() >=
  915. max_const->GetValueAsDouble()) {
  916. found_result = true;
  917. result = !(cmp_opcode == SpvOpFOrdLessThan ||
  918. cmp_opcode == SpvOpFUnordLessThan);
  919. }
  920. }
  921. }
  922. if (constants[1]) {
  923. if (max_const) {
  924. if (max_const->GetValueAsDouble() <
  925. constants[1]->GetValueAsDouble()) {
  926. found_result = true;
  927. result = (cmp_opcode == SpvOpFOrdLessThan ||
  928. cmp_opcode == SpvOpFUnordLessThan);
  929. }
  930. }
  931. if (min_const) {
  932. if (min_const->GetValueAsDouble() >=
  933. constants[1]->GetValueAsDouble()) {
  934. found_result = true;
  935. result = !(cmp_opcode == SpvOpFOrdLessThan ||
  936. cmp_opcode == SpvOpFUnordLessThan);
  937. }
  938. }
  939. }
  940. break;
  941. case SpvOpFOrdGreaterThan:
  942. case SpvOpFUnordGreaterThan:
  943. case SpvOpFOrdLessThanEqual:
  944. case SpvOpFUnordLessThanEqual:
  945. if (constants[0]) {
  946. if (min_const) {
  947. if (constants[0]->GetValueAsDouble() <=
  948. min_const->GetValueAsDouble()) {
  949. found_result = true;
  950. result = (cmp_opcode == SpvOpFOrdLessThanEqual ||
  951. cmp_opcode == SpvOpFUnordLessThanEqual);
  952. }
  953. }
  954. if (max_const) {
  955. if (constants[0]->GetValueAsDouble() >
  956. max_const->GetValueAsDouble()) {
  957. found_result = true;
  958. result = !(cmp_opcode == SpvOpFOrdLessThanEqual ||
  959. cmp_opcode == SpvOpFUnordLessThanEqual);
  960. }
  961. }
  962. }
  963. if (constants[1]) {
  964. if (max_const) {
  965. if (max_const->GetValueAsDouble() <=
  966. constants[1]->GetValueAsDouble()) {
  967. found_result = true;
  968. result = (cmp_opcode == SpvOpFOrdLessThanEqual ||
  969. cmp_opcode == SpvOpFUnordLessThanEqual);
  970. }
  971. }
  972. if (min_const) {
  973. if (min_const->GetValueAsDouble() >
  974. constants[1]->GetValueAsDouble()) {
  975. found_result = true;
  976. result = !(cmp_opcode == SpvOpFOrdLessThanEqual ||
  977. cmp_opcode == SpvOpFUnordLessThanEqual);
  978. }
  979. }
  980. }
  981. break;
  982. default:
  983. return nullptr;
  984. }
  985. if (!found_result) {
  986. return nullptr;
  987. }
  988. const analysis::Type* bool_type =
  989. context->get_type_mgr()->GetType(inst->type_id());
  990. const analysis::Constant* result_const =
  991. const_mgr->GetConstant(bool_type, {static_cast<uint32_t>(result)});
  992. assert(result_const);
  993. return result_const;
  994. };
  995. }
  996. ConstantFoldingRule FoldFMix() {
  997. return [](IRContext* context, Instruction* inst,
  998. const std::vector<const analysis::Constant*>& constants)
  999. -> const analysis::Constant* {
  1000. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  1001. assert(inst->opcode() == SpvOpExtInst &&
  1002. "Expecting an extended instruction.");
  1003. assert(inst->GetSingleWordInOperand(0) ==
  1004. context->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
  1005. "Expecting a GLSLstd450 extended instruction.");
  1006. assert(inst->GetSingleWordInOperand(1) == GLSLstd450FMix &&
  1007. "Expecting and FMix instruction.");
  1008. if (!inst->IsFloatingPointFoldingAllowed()) {
  1009. return nullptr;
  1010. }
  1011. // Make sure all FMix operands are constants.
  1012. for (uint32_t i = 1; i < 4; i++) {
  1013. if (constants[i] == nullptr) {
  1014. return nullptr;
  1015. }
  1016. }
  1017. const analysis::Constant* one;
  1018. bool is_vector = false;
  1019. const analysis::Type* result_type = constants[1]->type();
  1020. const analysis::Type* base_type = result_type;
  1021. if (base_type->AsVector()) {
  1022. is_vector = true;
  1023. base_type = base_type->AsVector()->element_type();
  1024. }
  1025. assert(base_type->AsFloat() != nullptr &&
  1026. "FMix is suppose to act on floats or vectors of floats.");
  1027. if (base_type->AsFloat()->width() == 32) {
  1028. one = const_mgr->GetConstant(base_type,
  1029. utils::FloatProxy<float>(1.0f).GetWords());
  1030. } else {
  1031. one = const_mgr->GetConstant(base_type,
  1032. utils::FloatProxy<double>(1.0).GetWords());
  1033. }
  1034. if (is_vector) {
  1035. uint32_t one_id = const_mgr->GetDefiningInstruction(one)->result_id();
  1036. one =
  1037. const_mgr->GetConstant(result_type, std::vector<uint32_t>(4, one_id));
  1038. }
  1039. const analysis::Constant* temp1 = FoldFPBinaryOp(
  1040. FOLD_FPARITH_OP(-), inst->type_id(), {one, constants[3]}, context);
  1041. if (temp1 == nullptr) {
  1042. return nullptr;
  1043. }
  1044. const analysis::Constant* temp2 = FoldFPBinaryOp(
  1045. FOLD_FPARITH_OP(*), inst->type_id(), {constants[1], temp1}, context);
  1046. if (temp2 == nullptr) {
  1047. return nullptr;
  1048. }
  1049. const analysis::Constant* temp3 =
  1050. FoldFPBinaryOp(FOLD_FPARITH_OP(*), inst->type_id(),
  1051. {constants[2], constants[3]}, context);
  1052. if (temp3 == nullptr) {
  1053. return nullptr;
  1054. }
  1055. return FoldFPBinaryOp(FOLD_FPARITH_OP(+), inst->type_id(), {temp2, temp3},
  1056. context);
  1057. };
  1058. }
  1059. template <class IntType>
  1060. IntType FoldIClamp(IntType x, IntType min_val, IntType max_val) {
  1061. if (x < min_val) {
  1062. x = min_val;
  1063. }
  1064. if (x > max_val) {
  1065. x = max_val;
  1066. }
  1067. return x;
  1068. }
  1069. const analysis::Constant* FoldMin(const analysis::Type* result_type,
  1070. const analysis::Constant* a,
  1071. const analysis::Constant* b,
  1072. analysis::ConstantManager*) {
  1073. if (const analysis::Integer* int_type = result_type->AsInteger()) {
  1074. if (int_type->width() == 32) {
  1075. if (int_type->IsSigned()) {
  1076. int32_t va = a->GetS32();
  1077. int32_t vb = b->GetS32();
  1078. return (va < vb ? a : b);
  1079. } else {
  1080. uint32_t va = a->GetU32();
  1081. uint32_t vb = b->GetU32();
  1082. return (va < vb ? a : b);
  1083. }
  1084. } else if (int_type->width() == 64) {
  1085. if (int_type->IsSigned()) {
  1086. int64_t va = a->GetS64();
  1087. int64_t vb = b->GetS64();
  1088. return (va < vb ? a : b);
  1089. } else {
  1090. uint64_t va = a->GetU64();
  1091. uint64_t vb = b->GetU64();
  1092. return (va < vb ? a : b);
  1093. }
  1094. }
  1095. } else if (const analysis::Float* float_type = result_type->AsFloat()) {
  1096. if (float_type->width() == 32) {
  1097. float va = a->GetFloat();
  1098. float vb = b->GetFloat();
  1099. return (va < vb ? a : b);
  1100. } else if (float_type->width() == 64) {
  1101. double va = a->GetDouble();
  1102. double vb = b->GetDouble();
  1103. return (va < vb ? a : b);
  1104. }
  1105. }
  1106. return nullptr;
  1107. }
  1108. const analysis::Constant* FoldMax(const analysis::Type* result_type,
  1109. const analysis::Constant* a,
  1110. const analysis::Constant* b,
  1111. analysis::ConstantManager*) {
  1112. if (const analysis::Integer* int_type = result_type->AsInteger()) {
  1113. if (int_type->width() == 32) {
  1114. if (int_type->IsSigned()) {
  1115. int32_t va = a->GetS32();
  1116. int32_t vb = b->GetS32();
  1117. return (va > vb ? a : b);
  1118. } else {
  1119. uint32_t va = a->GetU32();
  1120. uint32_t vb = b->GetU32();
  1121. return (va > vb ? a : b);
  1122. }
  1123. } else if (int_type->width() == 64) {
  1124. if (int_type->IsSigned()) {
  1125. int64_t va = a->GetS64();
  1126. int64_t vb = b->GetS64();
  1127. return (va > vb ? a : b);
  1128. } else {
  1129. uint64_t va = a->GetU64();
  1130. uint64_t vb = b->GetU64();
  1131. return (va > vb ? a : b);
  1132. }
  1133. }
  1134. } else if (const analysis::Float* float_type = result_type->AsFloat()) {
  1135. if (float_type->width() == 32) {
  1136. float va = a->GetFloat();
  1137. float vb = b->GetFloat();
  1138. return (va > vb ? a : b);
  1139. } else if (float_type->width() == 64) {
  1140. double va = a->GetDouble();
  1141. double vb = b->GetDouble();
  1142. return (va > vb ? a : b);
  1143. }
  1144. }
  1145. return nullptr;
  1146. }
  1147. // Fold an clamp instruction when all three operands are constant.
  1148. const analysis::Constant* FoldClamp1(
  1149. IRContext* context, Instruction* inst,
  1150. const std::vector<const analysis::Constant*>& constants) {
  1151. assert(inst->opcode() == SpvOpExtInst &&
  1152. "Expecting an extended instruction.");
  1153. assert(inst->GetSingleWordInOperand(0) ==
  1154. context->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
  1155. "Expecting a GLSLstd450 extended instruction.");
  1156. // Make sure all Clamp operands are constants.
  1157. for (uint32_t i = 1; i < 4; i++) {
  1158. if (constants[i] == nullptr) {
  1159. return nullptr;
  1160. }
  1161. }
  1162. const analysis::Constant* temp = FoldFPBinaryOp(
  1163. FoldMax, inst->type_id(), {constants[1], constants[2]}, context);
  1164. if (temp == nullptr) {
  1165. return nullptr;
  1166. }
  1167. return FoldFPBinaryOp(FoldMin, inst->type_id(), {temp, constants[3]},
  1168. context);
  1169. }
  1170. // Fold a clamp instruction when |x <= min_val|.
  1171. const analysis::Constant* FoldClamp2(
  1172. IRContext* context, Instruction* inst,
  1173. const std::vector<const analysis::Constant*>& constants) {
  1174. assert(inst->opcode() == SpvOpExtInst &&
  1175. "Expecting an extended instruction.");
  1176. assert(inst->GetSingleWordInOperand(0) ==
  1177. context->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
  1178. "Expecting a GLSLstd450 extended instruction.");
  1179. const analysis::Constant* x = constants[1];
  1180. const analysis::Constant* min_val = constants[2];
  1181. if (x == nullptr || min_val == nullptr) {
  1182. return nullptr;
  1183. }
  1184. const analysis::Constant* temp =
  1185. FoldFPBinaryOp(FoldMax, inst->type_id(), {x, min_val}, context);
  1186. if (temp == min_val) {
  1187. // We can assume that |min_val| is less than |max_val|. Therefore, if the
  1188. // result of the max operation is |min_val|, we know the result of the min
  1189. // operation, even if |max_val| is not a constant.
  1190. return min_val;
  1191. }
  1192. return nullptr;
  1193. }
  1194. // Fold a clamp instruction when |x >= max_val|.
  1195. const analysis::Constant* FoldClamp3(
  1196. IRContext* context, Instruction* inst,
  1197. const std::vector<const analysis::Constant*>& constants) {
  1198. assert(inst->opcode() == SpvOpExtInst &&
  1199. "Expecting an extended instruction.");
  1200. assert(inst->GetSingleWordInOperand(0) ==
  1201. context->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
  1202. "Expecting a GLSLstd450 extended instruction.");
  1203. const analysis::Constant* x = constants[1];
  1204. const analysis::Constant* max_val = constants[3];
  1205. if (x == nullptr || max_val == nullptr) {
  1206. return nullptr;
  1207. }
  1208. const analysis::Constant* temp =
  1209. FoldFPBinaryOp(FoldMin, inst->type_id(), {x, max_val}, context);
  1210. if (temp == max_val) {
  1211. // We can assume that |min_val| is less than |max_val|. Therefore, if the
  1212. // result of the max operation is |min_val|, we know the result of the min
  1213. // operation, even if |max_val| is not a constant.
  1214. return max_val;
  1215. }
  1216. return nullptr;
  1217. }
  1218. UnaryScalarFoldingRule FoldFTranscendentalUnary(double (*fp)(double)) {
  1219. return
  1220. [fp](const analysis::Type* result_type, const analysis::Constant* a,
  1221. analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
  1222. assert(result_type != nullptr && a != nullptr);
  1223. const analysis::Float* float_type = a->type()->AsFloat();
  1224. assert(float_type != nullptr);
  1225. assert(float_type == result_type->AsFloat());
  1226. if (float_type->width() == 32) {
  1227. float fa = a->GetFloat();
  1228. float res = static_cast<float>(fp(fa));
  1229. utils::FloatProxy<float> result(res);
  1230. std::vector<uint32_t> words = result.GetWords();
  1231. return const_mgr->GetConstant(result_type, words);
  1232. } else if (float_type->width() == 64) {
  1233. double fa = a->GetDouble();
  1234. double res = fp(fa);
  1235. utils::FloatProxy<double> result(res);
  1236. std::vector<uint32_t> words = result.GetWords();
  1237. return const_mgr->GetConstant(result_type, words);
  1238. }
  1239. return nullptr;
  1240. };
  1241. }
  1242. BinaryScalarFoldingRule FoldFTranscendentalBinary(double (*fp)(double,
  1243. double)) {
  1244. return
  1245. [fp](const analysis::Type* result_type, const analysis::Constant* a,
  1246. const analysis::Constant* b,
  1247. analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
  1248. assert(result_type != nullptr && a != nullptr);
  1249. const analysis::Float* float_type = a->type()->AsFloat();
  1250. assert(float_type != nullptr);
  1251. assert(float_type == result_type->AsFloat());
  1252. assert(float_type == b->type()->AsFloat());
  1253. if (float_type->width() == 32) {
  1254. float fa = a->GetFloat();
  1255. float fb = b->GetFloat();
  1256. float res = static_cast<float>(fp(fa, fb));
  1257. utils::FloatProxy<float> result(res);
  1258. std::vector<uint32_t> words = result.GetWords();
  1259. return const_mgr->GetConstant(result_type, words);
  1260. } else if (float_type->width() == 64) {
  1261. double fa = a->GetDouble();
  1262. double fb = b->GetDouble();
  1263. double res = fp(fa, fb);
  1264. utils::FloatProxy<double> result(res);
  1265. std::vector<uint32_t> words = result.GetWords();
  1266. return const_mgr->GetConstant(result_type, words);
  1267. }
  1268. return nullptr;
  1269. };
  1270. }
  1271. } // namespace
  1272. void ConstantFoldingRules::AddFoldingRules() {
  1273. // Add all folding rules to the list for the opcodes to which they apply.
  1274. // Note that the order in which rules are added to the list matters. If a rule
  1275. // applies to the instruction, the rest of the rules will not be attempted.
  1276. // Take that into consideration.
  1277. rules_[SpvOpCompositeConstruct].push_back(FoldCompositeWithConstants());
  1278. rules_[SpvOpCompositeExtract].push_back(FoldExtractWithConstants());
  1279. rules_[SpvOpConvertFToS].push_back(FoldFToI());
  1280. rules_[SpvOpConvertFToU].push_back(FoldFToI());
  1281. rules_[SpvOpConvertSToF].push_back(FoldIToF());
  1282. rules_[SpvOpConvertUToF].push_back(FoldIToF());
  1283. rules_[SpvOpDot].push_back(FoldOpDotWithConstants());
  1284. rules_[SpvOpFAdd].push_back(FoldFAdd());
  1285. rules_[SpvOpFDiv].push_back(FoldFDiv());
  1286. rules_[SpvOpFMul].push_back(FoldFMul());
  1287. rules_[SpvOpFSub].push_back(FoldFSub());
  1288. rules_[SpvOpFOrdEqual].push_back(FoldFOrdEqual());
  1289. rules_[SpvOpFUnordEqual].push_back(FoldFUnordEqual());
  1290. rules_[SpvOpFOrdNotEqual].push_back(FoldFOrdNotEqual());
  1291. rules_[SpvOpFUnordNotEqual].push_back(FoldFUnordNotEqual());
  1292. rules_[SpvOpFOrdLessThan].push_back(FoldFOrdLessThan());
  1293. rules_[SpvOpFOrdLessThan].push_back(
  1294. FoldFClampFeedingCompare(SpvOpFOrdLessThan));
  1295. rules_[SpvOpFUnordLessThan].push_back(FoldFUnordLessThan());
  1296. rules_[SpvOpFUnordLessThan].push_back(
  1297. FoldFClampFeedingCompare(SpvOpFUnordLessThan));
  1298. rules_[SpvOpFOrdGreaterThan].push_back(FoldFOrdGreaterThan());
  1299. rules_[SpvOpFOrdGreaterThan].push_back(
  1300. FoldFClampFeedingCompare(SpvOpFOrdGreaterThan));
  1301. rules_[SpvOpFUnordGreaterThan].push_back(FoldFUnordGreaterThan());
  1302. rules_[SpvOpFUnordGreaterThan].push_back(
  1303. FoldFClampFeedingCompare(SpvOpFUnordGreaterThan));
  1304. rules_[SpvOpFOrdLessThanEqual].push_back(FoldFOrdLessThanEqual());
  1305. rules_[SpvOpFOrdLessThanEqual].push_back(
  1306. FoldFClampFeedingCompare(SpvOpFOrdLessThanEqual));
  1307. rules_[SpvOpFUnordLessThanEqual].push_back(FoldFUnordLessThanEqual());
  1308. rules_[SpvOpFUnordLessThanEqual].push_back(
  1309. FoldFClampFeedingCompare(SpvOpFUnordLessThanEqual));
  1310. rules_[SpvOpFOrdGreaterThanEqual].push_back(FoldFOrdGreaterThanEqual());
  1311. rules_[SpvOpFOrdGreaterThanEqual].push_back(
  1312. FoldFClampFeedingCompare(SpvOpFOrdGreaterThanEqual));
  1313. rules_[SpvOpFUnordGreaterThanEqual].push_back(FoldFUnordGreaterThanEqual());
  1314. rules_[SpvOpFUnordGreaterThanEqual].push_back(
  1315. FoldFClampFeedingCompare(SpvOpFUnordGreaterThanEqual));
  1316. rules_[SpvOpVectorShuffle].push_back(FoldVectorShuffleWithConstants());
  1317. rules_[SpvOpVectorTimesScalar].push_back(FoldVectorTimesScalar());
  1318. rules_[SpvOpVectorTimesMatrix].push_back(FoldVectorTimesMatrix());
  1319. rules_[SpvOpMatrixTimesVector].push_back(FoldMatrixTimesVector());
  1320. rules_[SpvOpFNegate].push_back(FoldFNegate());
  1321. rules_[SpvOpQuantizeToF16].push_back(FoldQuantizeToF16());
  1322. // Add rules for GLSLstd450
  1323. FeatureManager* feature_manager = context_->get_feature_mgr();
  1324. uint32_t ext_inst_glslstd450_id =
  1325. feature_manager->GetExtInstImportId_GLSLstd450();
  1326. if (ext_inst_glslstd450_id != 0) {
  1327. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FMix}].push_back(FoldFMix());
  1328. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SMin}].push_back(
  1329. FoldFPBinaryOp(FoldMin));
  1330. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UMin}].push_back(
  1331. FoldFPBinaryOp(FoldMin));
  1332. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FMin}].push_back(
  1333. FoldFPBinaryOp(FoldMin));
  1334. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SMax}].push_back(
  1335. FoldFPBinaryOp(FoldMax));
  1336. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UMax}].push_back(
  1337. FoldFPBinaryOp(FoldMax));
  1338. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FMax}].push_back(
  1339. FoldFPBinaryOp(FoldMax));
  1340. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UClamp}].push_back(
  1341. FoldClamp1);
  1342. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UClamp}].push_back(
  1343. FoldClamp2);
  1344. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UClamp}].push_back(
  1345. FoldClamp3);
  1346. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SClamp}].push_back(
  1347. FoldClamp1);
  1348. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SClamp}].push_back(
  1349. FoldClamp2);
  1350. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SClamp}].push_back(
  1351. FoldClamp3);
  1352. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FClamp}].push_back(
  1353. FoldClamp1);
  1354. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FClamp}].push_back(
  1355. FoldClamp2);
  1356. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FClamp}].push_back(
  1357. FoldClamp3);
  1358. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Sin}].push_back(
  1359. FoldFPUnaryOp(FoldFTranscendentalUnary(std::sin)));
  1360. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Cos}].push_back(
  1361. FoldFPUnaryOp(FoldFTranscendentalUnary(std::cos)));
  1362. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Tan}].push_back(
  1363. FoldFPUnaryOp(FoldFTranscendentalUnary(std::tan)));
  1364. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Asin}].push_back(
  1365. FoldFPUnaryOp(FoldFTranscendentalUnary(std::asin)));
  1366. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Acos}].push_back(
  1367. FoldFPUnaryOp(FoldFTranscendentalUnary(std::acos)));
  1368. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Atan}].push_back(
  1369. FoldFPUnaryOp(FoldFTranscendentalUnary(std::atan)));
  1370. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Exp}].push_back(
  1371. FoldFPUnaryOp(FoldFTranscendentalUnary(std::exp)));
  1372. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Log}].push_back(
  1373. FoldFPUnaryOp(FoldFTranscendentalUnary(std::log)));
  1374. #ifdef __ANDROID__
  1375. // Android NDK r15c targeting ABI 15 doesn't have full support for C++11
  1376. // (no std::exp2/log2). ::exp2 is available from C99 but ::log2 isn't
  1377. // available up until ABI 18 so we use a shim
  1378. auto log2_shim = [](double v) -> double { return log(v) / log(2.0); };
  1379. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Exp2}].push_back(
  1380. FoldFPUnaryOp(FoldFTranscendentalUnary(::exp2)));
  1381. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Log2}].push_back(
  1382. FoldFPUnaryOp(FoldFTranscendentalUnary(log2_shim)));
  1383. #else
  1384. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Exp2}].push_back(
  1385. FoldFPUnaryOp(FoldFTranscendentalUnary(std::exp2)));
  1386. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Log2}].push_back(
  1387. FoldFPUnaryOp(FoldFTranscendentalUnary(std::log2)));
  1388. #endif
  1389. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Sqrt}].push_back(
  1390. FoldFPUnaryOp(FoldFTranscendentalUnary(std::sqrt)));
  1391. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Atan2}].push_back(
  1392. FoldFPBinaryOp(FoldFTranscendentalBinary(std::atan2)));
  1393. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Pow}].push_back(
  1394. FoldFPBinaryOp(FoldFTranscendentalBinary(std::pow)));
  1395. }
  1396. }
  1397. } // namespace opt
  1398. } // namespace spvtools