const_folding_rules.cpp 65 KB

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