const_folding_rules.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  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 true if |type| is Float or a vector of Float.
  21. bool HasFloatingPoint(const analysis::Type* type) {
  22. if (type->AsFloat()) {
  23. return true;
  24. } else if (const analysis::Vector* vec_type = type->AsVector()) {
  25. return vec_type->element_type()->AsFloat() != nullptr;
  26. }
  27. return false;
  28. }
  29. // Folds an OpcompositeExtract where input is a composite constant.
  30. ConstantFoldingRule FoldExtractWithConstants() {
  31. return [](IRContext* context, Instruction* inst,
  32. const std::vector<const analysis::Constant*>& constants)
  33. -> const analysis::Constant* {
  34. const analysis::Constant* c = constants[kExtractCompositeIdInIdx];
  35. if (c == nullptr) {
  36. return nullptr;
  37. }
  38. for (uint32_t i = 1; i < inst->NumInOperands(); ++i) {
  39. uint32_t element_index = inst->GetSingleWordInOperand(i);
  40. if (c->AsNullConstant()) {
  41. // Return Null for the return type.
  42. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  43. analysis::TypeManager* type_mgr = context->get_type_mgr();
  44. return const_mgr->GetConstant(type_mgr->GetType(inst->type_id()), {});
  45. }
  46. auto cc = c->AsCompositeConstant();
  47. assert(cc != nullptr);
  48. auto components = cc->GetComponents();
  49. // Protect against invalid IR. Refuse to fold if the index is out
  50. // of bounds.
  51. if (element_index >= components.size()) return nullptr;
  52. c = components[element_index];
  53. }
  54. return c;
  55. };
  56. }
  57. ConstantFoldingRule FoldVectorShuffleWithConstants() {
  58. return [](IRContext* context, Instruction* inst,
  59. const std::vector<const analysis::Constant*>& constants)
  60. -> const analysis::Constant* {
  61. assert(inst->opcode() == SpvOpVectorShuffle);
  62. const analysis::Constant* c1 = constants[0];
  63. const analysis::Constant* c2 = constants[1];
  64. if (c1 == nullptr || c2 == nullptr) {
  65. return nullptr;
  66. }
  67. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  68. const analysis::Type* element_type = c1->type()->AsVector()->element_type();
  69. std::vector<const analysis::Constant*> c1_components;
  70. if (const analysis::VectorConstant* vec_const = c1->AsVectorConstant()) {
  71. c1_components = vec_const->GetComponents();
  72. } else {
  73. assert(c1->AsNullConstant());
  74. const analysis::Constant* element =
  75. const_mgr->GetConstant(element_type, {});
  76. c1_components.resize(c1->type()->AsVector()->element_count(), element);
  77. }
  78. std::vector<const analysis::Constant*> c2_components;
  79. if (const analysis::VectorConstant* vec_const = c2->AsVectorConstant()) {
  80. c2_components = vec_const->GetComponents();
  81. } else {
  82. assert(c2->AsNullConstant());
  83. const analysis::Constant* element =
  84. const_mgr->GetConstant(element_type, {});
  85. c2_components.resize(c2->type()->AsVector()->element_count(), element);
  86. }
  87. std::vector<uint32_t> ids;
  88. const uint32_t undef_literal_value = 0xffffffff;
  89. for (uint32_t i = 2; i < inst->NumInOperands(); ++i) {
  90. uint32_t index = inst->GetSingleWordInOperand(i);
  91. if (index == undef_literal_value) {
  92. // Don't fold shuffle with undef literal value.
  93. return nullptr;
  94. } else if (index < c1_components.size()) {
  95. Instruction* member_inst =
  96. const_mgr->GetDefiningInstruction(c1_components[index]);
  97. ids.push_back(member_inst->result_id());
  98. } else {
  99. Instruction* member_inst = const_mgr->GetDefiningInstruction(
  100. c2_components[index - c1_components.size()]);
  101. ids.push_back(member_inst->result_id());
  102. }
  103. }
  104. analysis::TypeManager* type_mgr = context->get_type_mgr();
  105. return const_mgr->GetConstant(type_mgr->GetType(inst->type_id()), ids);
  106. };
  107. }
  108. ConstantFoldingRule FoldVectorTimesScalar() {
  109. return [](IRContext* context, Instruction* inst,
  110. const std::vector<const analysis::Constant*>& constants)
  111. -> const analysis::Constant* {
  112. assert(inst->opcode() == SpvOpVectorTimesScalar);
  113. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  114. analysis::TypeManager* type_mgr = context->get_type_mgr();
  115. if (!inst->IsFloatingPointFoldingAllowed()) {
  116. if (HasFloatingPoint(type_mgr->GetType(inst->type_id()))) {
  117. return nullptr;
  118. }
  119. }
  120. const analysis::Constant* c1 = constants[0];
  121. const analysis::Constant* c2 = constants[1];
  122. if (c1 && c1->IsZero()) {
  123. return c1;
  124. }
  125. if (c2 && c2->IsZero()) {
  126. // Get or create the NullConstant for this type.
  127. std::vector<uint32_t> ids;
  128. return const_mgr->GetConstant(type_mgr->GetType(inst->type_id()), ids);
  129. }
  130. if (c1 == nullptr || c2 == nullptr) {
  131. return nullptr;
  132. }
  133. // Check result type.
  134. const analysis::Type* result_type = type_mgr->GetType(inst->type_id());
  135. const analysis::Vector* vector_type = result_type->AsVector();
  136. assert(vector_type != nullptr);
  137. const analysis::Type* element_type = vector_type->element_type();
  138. assert(element_type != nullptr);
  139. const analysis::Float* float_type = element_type->AsFloat();
  140. assert(float_type != nullptr);
  141. // Check types of c1 and c2.
  142. assert(c1->type()->AsVector() == vector_type);
  143. assert(c1->type()->AsVector()->element_type() == element_type &&
  144. c2->type() == element_type);
  145. // Get a float vector that is the result of vector-times-scalar.
  146. std::vector<const analysis::Constant*> c1_components =
  147. c1->GetVectorComponents(const_mgr);
  148. std::vector<uint32_t> ids;
  149. if (float_type->width() == 32) {
  150. float scalar = c2->GetFloat();
  151. for (uint32_t i = 0; i < c1_components.size(); ++i) {
  152. utils::FloatProxy<float> result(c1_components[i]->GetFloat() * scalar);
  153. std::vector<uint32_t> words = result.GetWords();
  154. const analysis::Constant* new_elem =
  155. const_mgr->GetConstant(float_type, words);
  156. ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
  157. }
  158. return const_mgr->GetConstant(vector_type, ids);
  159. } else if (float_type->width() == 64) {
  160. double scalar = c2->GetDouble();
  161. for (uint32_t i = 0; i < c1_components.size(); ++i) {
  162. utils::FloatProxy<double> result(c1_components[i]->GetDouble() *
  163. scalar);
  164. std::vector<uint32_t> words = result.GetWords();
  165. const analysis::Constant* new_elem =
  166. const_mgr->GetConstant(float_type, words);
  167. ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
  168. }
  169. return const_mgr->GetConstant(vector_type, ids);
  170. }
  171. return nullptr;
  172. };
  173. }
  174. ConstantFoldingRule FoldCompositeWithConstants() {
  175. // Folds an OpCompositeConstruct where all of the inputs are constants to a
  176. // constant. A new constant is created if necessary.
  177. return [](IRContext* context, Instruction* inst,
  178. const std::vector<const analysis::Constant*>& constants)
  179. -> const analysis::Constant* {
  180. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  181. analysis::TypeManager* type_mgr = context->get_type_mgr();
  182. const analysis::Type* new_type = type_mgr->GetType(inst->type_id());
  183. Instruction* type_inst =
  184. context->get_def_use_mgr()->GetDef(inst->type_id());
  185. std::vector<uint32_t> ids;
  186. for (uint32_t i = 0; i < constants.size(); ++i) {
  187. const analysis::Constant* element_const = constants[i];
  188. if (element_const == nullptr) {
  189. return nullptr;
  190. }
  191. uint32_t component_type_id = 0;
  192. if (type_inst->opcode() == SpvOpTypeStruct) {
  193. component_type_id = type_inst->GetSingleWordInOperand(i);
  194. } else if (type_inst->opcode() == SpvOpTypeArray) {
  195. component_type_id = type_inst->GetSingleWordInOperand(0);
  196. }
  197. uint32_t element_id =
  198. const_mgr->FindDeclaredConstant(element_const, component_type_id);
  199. if (element_id == 0) {
  200. return nullptr;
  201. }
  202. ids.push_back(element_id);
  203. }
  204. return const_mgr->GetConstant(new_type, ids);
  205. };
  206. }
  207. // The interface for a function that returns the result of applying a scalar
  208. // floating-point binary operation on |a| and |b|. The type of the return value
  209. // will be |type|. The input constants must also be of type |type|.
  210. using UnaryScalarFoldingRule = std::function<const analysis::Constant*(
  211. const analysis::Type* result_type, const analysis::Constant* a,
  212. analysis::ConstantManager*)>;
  213. // The interface for a function that returns the result of applying a scalar
  214. // floating-point binary operation on |a| and |b|. The type of the return value
  215. // will be |type|. The input constants must also be of type |type|.
  216. using BinaryScalarFoldingRule = std::function<const analysis::Constant*(
  217. const analysis::Type* result_type, const analysis::Constant* a,
  218. const analysis::Constant* b, analysis::ConstantManager*)>;
  219. // Returns a |ConstantFoldingRule| that folds unary floating point scalar ops
  220. // using |scalar_rule| and unary float point vectors ops by applying
  221. // |scalar_rule| to the elements of the vector. The |ConstantFoldingRule|
  222. // that is returned assumes that |constants| contains 1 entry. If they are
  223. // not |nullptr|, then their type is either |Float| or |Integer| or a |Vector|
  224. // whose element type is |Float| or |Integer|.
  225. ConstantFoldingRule FoldFPUnaryOp(UnaryScalarFoldingRule scalar_rule) {
  226. return [scalar_rule](IRContext* context, Instruction* inst,
  227. const std::vector<const analysis::Constant*>& constants)
  228. -> const analysis::Constant* {
  229. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  230. analysis::TypeManager* type_mgr = context->get_type_mgr();
  231. const analysis::Type* result_type = type_mgr->GetType(inst->type_id());
  232. const analysis::Vector* vector_type = result_type->AsVector();
  233. if (!inst->IsFloatingPointFoldingAllowed()) {
  234. return nullptr;
  235. }
  236. if (constants[0] == nullptr) {
  237. return nullptr;
  238. }
  239. if (vector_type != nullptr) {
  240. std::vector<const analysis::Constant*> a_components;
  241. std::vector<const analysis::Constant*> results_components;
  242. a_components = constants[0]->GetVectorComponents(const_mgr);
  243. // Fold each component of the vector.
  244. for (uint32_t i = 0; i < a_components.size(); ++i) {
  245. results_components.push_back(scalar_rule(vector_type->element_type(),
  246. a_components[i], const_mgr));
  247. if (results_components[i] == nullptr) {
  248. return nullptr;
  249. }
  250. }
  251. // Build the constant object and return it.
  252. std::vector<uint32_t> ids;
  253. for (const analysis::Constant* member : results_components) {
  254. ids.push_back(const_mgr->GetDefiningInstruction(member)->result_id());
  255. }
  256. return const_mgr->GetConstant(vector_type, ids);
  257. } else {
  258. return scalar_rule(result_type, constants[0], const_mgr);
  259. }
  260. };
  261. }
  262. // Returns the result of folding the constants in |constants| according the
  263. // |scalar_rule|. If |result_type| is a vector, then |scalar_rule| is applied
  264. // per component.
  265. const analysis::Constant* FoldFPBinaryOp(
  266. BinaryScalarFoldingRule scalar_rule, uint32_t result_type_id,
  267. const std::vector<const analysis::Constant*>& constants,
  268. IRContext* context) {
  269. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  270. analysis::TypeManager* type_mgr = context->get_type_mgr();
  271. const analysis::Type* result_type = type_mgr->GetType(result_type_id);
  272. const analysis::Vector* vector_type = result_type->AsVector();
  273. if (constants[0] == nullptr || constants[1] == nullptr) {
  274. return nullptr;
  275. }
  276. if (vector_type != nullptr) {
  277. std::vector<const analysis::Constant*> a_components;
  278. std::vector<const analysis::Constant*> b_components;
  279. std::vector<const analysis::Constant*> results_components;
  280. a_components = constants[0]->GetVectorComponents(const_mgr);
  281. b_components = constants[1]->GetVectorComponents(const_mgr);
  282. // Fold each component of the vector.
  283. for (uint32_t i = 0; i < a_components.size(); ++i) {
  284. results_components.push_back(scalar_rule(vector_type->element_type(),
  285. a_components[i], b_components[i],
  286. const_mgr));
  287. if (results_components[i] == nullptr) {
  288. return nullptr;
  289. }
  290. }
  291. // Build the constant object and return it.
  292. std::vector<uint32_t> ids;
  293. for (const analysis::Constant* member : results_components) {
  294. ids.push_back(const_mgr->GetDefiningInstruction(member)->result_id());
  295. }
  296. return const_mgr->GetConstant(vector_type, ids);
  297. } else {
  298. return scalar_rule(result_type, constants[0], constants[1], const_mgr);
  299. }
  300. }
  301. // Returns a |ConstantFoldingRule| that folds floating point scalars using
  302. // |scalar_rule| and vectors of floating point by applying |scalar_rule| to the
  303. // elements of the vector. The |ConstantFoldingRule| that is returned assumes
  304. // that |constants| contains 2 entries. If they are not |nullptr|, then their
  305. // type is either |Float| or a |Vector| whose element type is |Float|.
  306. ConstantFoldingRule FoldFPBinaryOp(BinaryScalarFoldingRule scalar_rule) {
  307. return [scalar_rule](IRContext* context, Instruction* inst,
  308. const std::vector<const analysis::Constant*>& constants)
  309. -> const analysis::Constant* {
  310. if (!inst->IsFloatingPointFoldingAllowed()) {
  311. return nullptr;
  312. }
  313. if (inst->opcode() == SpvOpExtInst) {
  314. return FoldFPBinaryOp(scalar_rule, inst->type_id(),
  315. {constants[1], constants[2]}, context);
  316. }
  317. return FoldFPBinaryOp(scalar_rule, inst->type_id(), constants, context);
  318. };
  319. }
  320. // This macro defines a |UnaryScalarFoldingRule| that performs float to
  321. // integer conversion.
  322. // TODO(greg-lunarg): Support for 64-bit integer types.
  323. UnaryScalarFoldingRule FoldFToIOp() {
  324. return [](const analysis::Type* result_type, const analysis::Constant* a,
  325. analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
  326. assert(result_type != nullptr && a != nullptr);
  327. const analysis::Integer* integer_type = result_type->AsInteger();
  328. const analysis::Float* float_type = a->type()->AsFloat();
  329. assert(float_type != nullptr);
  330. assert(integer_type != nullptr);
  331. if (integer_type->width() != 32) return nullptr;
  332. if (float_type->width() == 32) {
  333. float fa = a->GetFloat();
  334. uint32_t result = integer_type->IsSigned()
  335. ? static_cast<uint32_t>(static_cast<int32_t>(fa))
  336. : static_cast<uint32_t>(fa);
  337. std::vector<uint32_t> words = {result};
  338. return const_mgr->GetConstant(result_type, words);
  339. } else if (float_type->width() == 64) {
  340. double fa = a->GetDouble();
  341. uint32_t result = integer_type->IsSigned()
  342. ? static_cast<uint32_t>(static_cast<int32_t>(fa))
  343. : static_cast<uint32_t>(fa);
  344. std::vector<uint32_t> words = {result};
  345. return const_mgr->GetConstant(result_type, words);
  346. }
  347. return nullptr;
  348. };
  349. }
  350. // This function defines a |UnaryScalarFoldingRule| that performs integer to
  351. // float conversion.
  352. // TODO(greg-lunarg): Support for 64-bit integer types.
  353. UnaryScalarFoldingRule FoldIToFOp() {
  354. return [](const analysis::Type* result_type, const analysis::Constant* a,
  355. analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
  356. assert(result_type != nullptr && a != nullptr);
  357. const analysis::Integer* integer_type = a->type()->AsInteger();
  358. const analysis::Float* float_type = result_type->AsFloat();
  359. assert(float_type != nullptr);
  360. assert(integer_type != nullptr);
  361. if (integer_type->width() != 32) return nullptr;
  362. uint32_t ua = a->GetU32();
  363. if (float_type->width() == 32) {
  364. float result_val = integer_type->IsSigned()
  365. ? static_cast<float>(static_cast<int32_t>(ua))
  366. : static_cast<float>(ua);
  367. utils::FloatProxy<float> result(result_val);
  368. std::vector<uint32_t> words = {result.data()};
  369. return const_mgr->GetConstant(result_type, words);
  370. } else if (float_type->width() == 64) {
  371. double result_val = integer_type->IsSigned()
  372. ? static_cast<double>(static_cast<int32_t>(ua))
  373. : static_cast<double>(ua);
  374. utils::FloatProxy<double> result(result_val);
  375. std::vector<uint32_t> words = result.GetWords();
  376. return const_mgr->GetConstant(result_type, words);
  377. }
  378. return nullptr;
  379. };
  380. }
  381. // This defines a |UnaryScalarFoldingRule| that performs |OpQuantizeToF16|.
  382. UnaryScalarFoldingRule FoldQuantizeToF16Scalar() {
  383. return [](const analysis::Type* result_type, const analysis::Constant* a,
  384. analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
  385. assert(result_type != nullptr && a != nullptr);
  386. const analysis::Float* float_type = a->type()->AsFloat();
  387. assert(float_type != nullptr);
  388. if (float_type->width() != 32) {
  389. return nullptr;
  390. }
  391. float fa = a->GetFloat();
  392. utils::HexFloat<utils::FloatProxy<float>> orignal(fa);
  393. utils::HexFloat<utils::FloatProxy<utils::Float16>> quantized(0);
  394. utils::HexFloat<utils::FloatProxy<float>> result(0.0f);
  395. orignal.castTo(quantized, utils::round_direction::kToZero);
  396. quantized.castTo(result, utils::round_direction::kToZero);
  397. std::vector<uint32_t> words = {result.getBits()};
  398. return const_mgr->GetConstant(result_type, words);
  399. };
  400. }
  401. // This macro defines a |BinaryScalarFoldingRule| that applies |op|. The
  402. // operator |op| must work for both float and double, and use syntax "f1 op f2".
  403. #define FOLD_FPARITH_OP(op) \
  404. [](const analysis::Type* result_type_in_macro, const analysis::Constant* a, \
  405. const analysis::Constant* b, \
  406. analysis::ConstantManager* const_mgr_in_macro) \
  407. -> const analysis::Constant* { \
  408. assert(result_type_in_macro != nullptr && a != nullptr && b != nullptr); \
  409. assert(result_type_in_macro == a->type() && \
  410. result_type_in_macro == b->type()); \
  411. const analysis::Float* float_type_in_macro = \
  412. result_type_in_macro->AsFloat(); \
  413. assert(float_type_in_macro != nullptr); \
  414. if (float_type_in_macro->width() == 32) { \
  415. float fa = a->GetFloat(); \
  416. float fb = b->GetFloat(); \
  417. utils::FloatProxy<float> result_in_macro(fa op fb); \
  418. std::vector<uint32_t> words_in_macro = result_in_macro.GetWords(); \
  419. return const_mgr_in_macro->GetConstant(result_type_in_macro, \
  420. words_in_macro); \
  421. } else if (float_type_in_macro->width() == 64) { \
  422. double fa = a->GetDouble(); \
  423. double fb = b->GetDouble(); \
  424. utils::FloatProxy<double> result_in_macro(fa op fb); \
  425. std::vector<uint32_t> words_in_macro = result_in_macro.GetWords(); \
  426. return const_mgr_in_macro->GetConstant(result_type_in_macro, \
  427. words_in_macro); \
  428. } \
  429. return nullptr; \
  430. }
  431. // Define the folding rule for conversion between floating point and integer
  432. ConstantFoldingRule FoldFToI() { return FoldFPUnaryOp(FoldFToIOp()); }
  433. ConstantFoldingRule FoldIToF() { return FoldFPUnaryOp(FoldIToFOp()); }
  434. ConstantFoldingRule FoldQuantizeToF16() {
  435. return FoldFPUnaryOp(FoldQuantizeToF16Scalar());
  436. }
  437. // Define the folding rules for subtraction, addition, multiplication, and
  438. // division for floating point values.
  439. ConstantFoldingRule FoldFSub() { return FoldFPBinaryOp(FOLD_FPARITH_OP(-)); }
  440. ConstantFoldingRule FoldFAdd() { return FoldFPBinaryOp(FOLD_FPARITH_OP(+)); }
  441. ConstantFoldingRule FoldFMul() { return FoldFPBinaryOp(FOLD_FPARITH_OP(*)); }
  442. ConstantFoldingRule FoldFDiv() { return FoldFPBinaryOp(FOLD_FPARITH_OP(/)); }
  443. bool CompareFloatingPoint(bool op_result, bool op_unordered,
  444. bool need_ordered) {
  445. if (need_ordered) {
  446. // operands are ordered and Operand 1 is |op| Operand 2
  447. return !op_unordered && op_result;
  448. } else {
  449. // operands are unordered or Operand 1 is |op| Operand 2
  450. return op_unordered || op_result;
  451. }
  452. }
  453. // This macro defines a |BinaryScalarFoldingRule| that applies |op|. The
  454. // operator |op| must work for both float and double, and use syntax "f1 op f2".
  455. #define FOLD_FPCMP_OP(op, ord) \
  456. [](const analysis::Type* result_type, const analysis::Constant* a, \
  457. const analysis::Constant* b, \
  458. analysis::ConstantManager* const_mgr) -> const analysis::Constant* { \
  459. assert(result_type != nullptr && a != nullptr && b != nullptr); \
  460. assert(result_type->AsBool()); \
  461. assert(a->type() == b->type()); \
  462. const analysis::Float* float_type = a->type()->AsFloat(); \
  463. assert(float_type != nullptr); \
  464. if (float_type->width() == 32) { \
  465. float fa = a->GetFloat(); \
  466. float fb = b->GetFloat(); \
  467. bool result = CompareFloatingPoint( \
  468. fa op fb, std::isnan(fa) || std::isnan(fb), ord); \
  469. std::vector<uint32_t> words = {uint32_t(result)}; \
  470. return const_mgr->GetConstant(result_type, words); \
  471. } else if (float_type->width() == 64) { \
  472. double fa = a->GetDouble(); \
  473. double fb = b->GetDouble(); \
  474. bool result = CompareFloatingPoint( \
  475. fa op fb, std::isnan(fa) || std::isnan(fb), ord); \
  476. std::vector<uint32_t> words = {uint32_t(result)}; \
  477. return const_mgr->GetConstant(result_type, words); \
  478. } \
  479. return nullptr; \
  480. }
  481. // Define the folding rules for ordered and unordered comparison for floating
  482. // point values.
  483. ConstantFoldingRule FoldFOrdEqual() {
  484. return FoldFPBinaryOp(FOLD_FPCMP_OP(==, true));
  485. }
  486. ConstantFoldingRule FoldFUnordEqual() {
  487. return FoldFPBinaryOp(FOLD_FPCMP_OP(==, false));
  488. }
  489. ConstantFoldingRule FoldFOrdNotEqual() {
  490. return FoldFPBinaryOp(FOLD_FPCMP_OP(!=, true));
  491. }
  492. ConstantFoldingRule FoldFUnordNotEqual() {
  493. return FoldFPBinaryOp(FOLD_FPCMP_OP(!=, false));
  494. }
  495. ConstantFoldingRule FoldFOrdLessThan() {
  496. return FoldFPBinaryOp(FOLD_FPCMP_OP(<, true));
  497. }
  498. ConstantFoldingRule FoldFUnordLessThan() {
  499. return FoldFPBinaryOp(FOLD_FPCMP_OP(<, false));
  500. }
  501. ConstantFoldingRule FoldFOrdGreaterThan() {
  502. return FoldFPBinaryOp(FOLD_FPCMP_OP(>, true));
  503. }
  504. ConstantFoldingRule FoldFUnordGreaterThan() {
  505. return FoldFPBinaryOp(FOLD_FPCMP_OP(>, false));
  506. }
  507. ConstantFoldingRule FoldFOrdLessThanEqual() {
  508. return FoldFPBinaryOp(FOLD_FPCMP_OP(<=, true));
  509. }
  510. ConstantFoldingRule FoldFUnordLessThanEqual() {
  511. return FoldFPBinaryOp(FOLD_FPCMP_OP(<=, false));
  512. }
  513. ConstantFoldingRule FoldFOrdGreaterThanEqual() {
  514. return FoldFPBinaryOp(FOLD_FPCMP_OP(>=, true));
  515. }
  516. ConstantFoldingRule FoldFUnordGreaterThanEqual() {
  517. return FoldFPBinaryOp(FOLD_FPCMP_OP(>=, false));
  518. }
  519. // Folds an OpDot where all of the inputs are constants to a
  520. // constant. A new constant is created if necessary.
  521. ConstantFoldingRule FoldOpDotWithConstants() {
  522. return [](IRContext* context, Instruction* inst,
  523. const std::vector<const analysis::Constant*>& constants)
  524. -> const analysis::Constant* {
  525. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  526. analysis::TypeManager* type_mgr = context->get_type_mgr();
  527. const analysis::Type* new_type = type_mgr->GetType(inst->type_id());
  528. assert(new_type->AsFloat() && "OpDot should have a float return type.");
  529. const analysis::Float* float_type = new_type->AsFloat();
  530. if (!inst->IsFloatingPointFoldingAllowed()) {
  531. return nullptr;
  532. }
  533. // If one of the operands is 0, then the result is 0.
  534. bool has_zero_operand = false;
  535. for (int i = 0; i < 2; ++i) {
  536. if (constants[i]) {
  537. if (constants[i]->AsNullConstant() ||
  538. constants[i]->AsVectorConstant()->IsZero()) {
  539. has_zero_operand = true;
  540. break;
  541. }
  542. }
  543. }
  544. if (has_zero_operand) {
  545. if (float_type->width() == 32) {
  546. utils::FloatProxy<float> result(0.0f);
  547. std::vector<uint32_t> words = result.GetWords();
  548. return const_mgr->GetConstant(float_type, words);
  549. }
  550. if (float_type->width() == 64) {
  551. utils::FloatProxy<double> result(0.0);
  552. std::vector<uint32_t> words = result.GetWords();
  553. return const_mgr->GetConstant(float_type, words);
  554. }
  555. return nullptr;
  556. }
  557. if (constants[0] == nullptr || constants[1] == nullptr) {
  558. return nullptr;
  559. }
  560. std::vector<const analysis::Constant*> a_components;
  561. std::vector<const analysis::Constant*> b_components;
  562. a_components = constants[0]->GetVectorComponents(const_mgr);
  563. b_components = constants[1]->GetVectorComponents(const_mgr);
  564. utils::FloatProxy<double> result(0.0);
  565. std::vector<uint32_t> words = result.GetWords();
  566. const analysis::Constant* result_const =
  567. const_mgr->GetConstant(float_type, words);
  568. for (uint32_t i = 0; i < a_components.size() && result_const != nullptr;
  569. ++i) {
  570. if (a_components[i] == nullptr || b_components[i] == nullptr) {
  571. return nullptr;
  572. }
  573. const analysis::Constant* component = FOLD_FPARITH_OP(*)(
  574. new_type, a_components[i], b_components[i], const_mgr);
  575. if (component == nullptr) {
  576. return nullptr;
  577. }
  578. result_const =
  579. FOLD_FPARITH_OP(+)(new_type, result_const, component, const_mgr);
  580. }
  581. return result_const;
  582. };
  583. }
  584. // This function defines a |UnaryScalarFoldingRule| that subtracts the constant
  585. // from zero.
  586. UnaryScalarFoldingRule FoldFNegateOp() {
  587. return [](const analysis::Type* result_type, const analysis::Constant* a,
  588. analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
  589. assert(result_type != nullptr && a != nullptr);
  590. assert(result_type == a->type());
  591. const analysis::Float* float_type = result_type->AsFloat();
  592. assert(float_type != nullptr);
  593. if (float_type->width() == 32) {
  594. float fa = a->GetFloat();
  595. utils::FloatProxy<float> result(-fa);
  596. std::vector<uint32_t> words = result.GetWords();
  597. return const_mgr->GetConstant(result_type, words);
  598. } else if (float_type->width() == 64) {
  599. double da = a->GetDouble();
  600. utils::FloatProxy<double> result(-da);
  601. std::vector<uint32_t> words = result.GetWords();
  602. return const_mgr->GetConstant(result_type, words);
  603. }
  604. return nullptr;
  605. };
  606. }
  607. ConstantFoldingRule FoldFNegate() { return FoldFPUnaryOp(FoldFNegateOp()); }
  608. ConstantFoldingRule FoldFClampFeedingCompare(uint32_t cmp_opcode) {
  609. return [cmp_opcode](IRContext* context, Instruction* inst,
  610. const std::vector<const analysis::Constant*>& constants)
  611. -> const analysis::Constant* {
  612. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  613. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  614. if (!inst->IsFloatingPointFoldingAllowed()) {
  615. return nullptr;
  616. }
  617. uint32_t non_const_idx = (constants[0] ? 1 : 0);
  618. uint32_t operand_id = inst->GetSingleWordInOperand(non_const_idx);
  619. Instruction* operand_inst = def_use_mgr->GetDef(operand_id);
  620. analysis::TypeManager* type_mgr = context->get_type_mgr();
  621. const analysis::Type* operand_type =
  622. type_mgr->GetType(operand_inst->type_id());
  623. if (!operand_type->AsFloat()) {
  624. return nullptr;
  625. }
  626. if (operand_type->AsFloat()->width() != 32 &&
  627. operand_type->AsFloat()->width() != 64) {
  628. return nullptr;
  629. }
  630. if (operand_inst->opcode() != SpvOpExtInst) {
  631. return nullptr;
  632. }
  633. if (operand_inst->GetSingleWordInOperand(1) != GLSLstd450FClamp) {
  634. return nullptr;
  635. }
  636. if (constants[1] == nullptr && constants[0] == nullptr) {
  637. return nullptr;
  638. }
  639. uint32_t max_id = operand_inst->GetSingleWordInOperand(4);
  640. const analysis::Constant* max_const =
  641. const_mgr->FindDeclaredConstant(max_id);
  642. uint32_t min_id = operand_inst->GetSingleWordInOperand(3);
  643. const analysis::Constant* min_const =
  644. const_mgr->FindDeclaredConstant(min_id);
  645. bool found_result = false;
  646. bool result = false;
  647. switch (cmp_opcode) {
  648. case SpvOpFOrdLessThan:
  649. case SpvOpFUnordLessThan:
  650. case SpvOpFOrdGreaterThanEqual:
  651. case SpvOpFUnordGreaterThanEqual:
  652. if (constants[0]) {
  653. if (min_const) {
  654. if (constants[0]->GetValueAsDouble() <
  655. min_const->GetValueAsDouble()) {
  656. found_result = true;
  657. result = (cmp_opcode == SpvOpFOrdLessThan ||
  658. cmp_opcode == SpvOpFUnordLessThan);
  659. }
  660. }
  661. if (max_const) {
  662. if (constants[0]->GetValueAsDouble() >=
  663. max_const->GetValueAsDouble()) {
  664. found_result = true;
  665. result = !(cmp_opcode == SpvOpFOrdLessThan ||
  666. cmp_opcode == SpvOpFUnordLessThan);
  667. }
  668. }
  669. }
  670. if (constants[1]) {
  671. if (max_const) {
  672. if (max_const->GetValueAsDouble() <
  673. constants[1]->GetValueAsDouble()) {
  674. found_result = true;
  675. result = (cmp_opcode == SpvOpFOrdLessThan ||
  676. cmp_opcode == SpvOpFUnordLessThan);
  677. }
  678. }
  679. if (min_const) {
  680. if (min_const->GetValueAsDouble() >=
  681. constants[1]->GetValueAsDouble()) {
  682. found_result = true;
  683. result = !(cmp_opcode == SpvOpFOrdLessThan ||
  684. cmp_opcode == SpvOpFUnordLessThan);
  685. }
  686. }
  687. }
  688. break;
  689. case SpvOpFOrdGreaterThan:
  690. case SpvOpFUnordGreaterThan:
  691. case SpvOpFOrdLessThanEqual:
  692. case SpvOpFUnordLessThanEqual:
  693. if (constants[0]) {
  694. if (min_const) {
  695. if (constants[0]->GetValueAsDouble() <=
  696. min_const->GetValueAsDouble()) {
  697. found_result = true;
  698. result = (cmp_opcode == SpvOpFOrdLessThanEqual ||
  699. cmp_opcode == SpvOpFUnordLessThanEqual);
  700. }
  701. }
  702. if (max_const) {
  703. if (constants[0]->GetValueAsDouble() >
  704. max_const->GetValueAsDouble()) {
  705. found_result = true;
  706. result = !(cmp_opcode == SpvOpFOrdLessThanEqual ||
  707. cmp_opcode == SpvOpFUnordLessThanEqual);
  708. }
  709. }
  710. }
  711. if (constants[1]) {
  712. if (max_const) {
  713. if (max_const->GetValueAsDouble() <=
  714. constants[1]->GetValueAsDouble()) {
  715. found_result = true;
  716. result = (cmp_opcode == SpvOpFOrdLessThanEqual ||
  717. cmp_opcode == SpvOpFUnordLessThanEqual);
  718. }
  719. }
  720. if (min_const) {
  721. if (min_const->GetValueAsDouble() >
  722. constants[1]->GetValueAsDouble()) {
  723. found_result = true;
  724. result = !(cmp_opcode == SpvOpFOrdLessThanEqual ||
  725. cmp_opcode == SpvOpFUnordLessThanEqual);
  726. }
  727. }
  728. }
  729. break;
  730. default:
  731. return nullptr;
  732. }
  733. if (!found_result) {
  734. return nullptr;
  735. }
  736. const analysis::Type* bool_type =
  737. context->get_type_mgr()->GetType(inst->type_id());
  738. const analysis::Constant* result_const =
  739. const_mgr->GetConstant(bool_type, {static_cast<uint32_t>(result)});
  740. assert(result_const);
  741. return result_const;
  742. };
  743. }
  744. ConstantFoldingRule FoldFMix() {
  745. return [](IRContext* context, Instruction* inst,
  746. const std::vector<const analysis::Constant*>& constants)
  747. -> const analysis::Constant* {
  748. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  749. assert(inst->opcode() == SpvOpExtInst &&
  750. "Expecting an extended instruction.");
  751. assert(inst->GetSingleWordInOperand(0) ==
  752. context->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
  753. "Expecting a GLSLstd450 extended instruction.");
  754. assert(inst->GetSingleWordInOperand(1) == GLSLstd450FMix &&
  755. "Expecting and FMix instruction.");
  756. if (!inst->IsFloatingPointFoldingAllowed()) {
  757. return nullptr;
  758. }
  759. // Make sure all FMix operands are constants.
  760. for (uint32_t i = 1; i < 4; i++) {
  761. if (constants[i] == nullptr) {
  762. return nullptr;
  763. }
  764. }
  765. const analysis::Constant* one;
  766. bool is_vector = false;
  767. const analysis::Type* result_type = constants[1]->type();
  768. const analysis::Type* base_type = result_type;
  769. if (base_type->AsVector()) {
  770. is_vector = true;
  771. base_type = base_type->AsVector()->element_type();
  772. }
  773. assert(base_type->AsFloat() != nullptr &&
  774. "FMix is suppose to act on floats or vectors of floats.");
  775. if (base_type->AsFloat()->width() == 32) {
  776. one = const_mgr->GetConstant(base_type,
  777. utils::FloatProxy<float>(1.0f).GetWords());
  778. } else {
  779. one = const_mgr->GetConstant(base_type,
  780. utils::FloatProxy<double>(1.0).GetWords());
  781. }
  782. if (is_vector) {
  783. uint32_t one_id = const_mgr->GetDefiningInstruction(one)->result_id();
  784. one =
  785. const_mgr->GetConstant(result_type, std::vector<uint32_t>(4, one_id));
  786. }
  787. const analysis::Constant* temp1 = FoldFPBinaryOp(
  788. FOLD_FPARITH_OP(-), inst->type_id(), {one, constants[3]}, context);
  789. if (temp1 == nullptr) {
  790. return nullptr;
  791. }
  792. const analysis::Constant* temp2 = FoldFPBinaryOp(
  793. FOLD_FPARITH_OP(*), inst->type_id(), {constants[1], temp1}, context);
  794. if (temp2 == nullptr) {
  795. return nullptr;
  796. }
  797. const analysis::Constant* temp3 =
  798. FoldFPBinaryOp(FOLD_FPARITH_OP(*), inst->type_id(),
  799. {constants[2], constants[3]}, context);
  800. if (temp3 == nullptr) {
  801. return nullptr;
  802. }
  803. return FoldFPBinaryOp(FOLD_FPARITH_OP(+), inst->type_id(), {temp2, temp3},
  804. context);
  805. };
  806. }
  807. template <class IntType>
  808. IntType FoldIClamp(IntType x, IntType min_val, IntType max_val) {
  809. if (x < min_val) {
  810. x = min_val;
  811. }
  812. if (x > max_val) {
  813. x = max_val;
  814. }
  815. return x;
  816. }
  817. const analysis::Constant* FoldMin(const analysis::Type* result_type,
  818. const analysis::Constant* a,
  819. const analysis::Constant* b,
  820. analysis::ConstantManager*) {
  821. if (const analysis::Integer* int_type = result_type->AsInteger()) {
  822. if (int_type->width() == 32) {
  823. if (int_type->IsSigned()) {
  824. int32_t va = a->GetS32();
  825. int32_t vb = b->GetS32();
  826. return (va < vb ? a : b);
  827. } else {
  828. uint32_t va = a->GetU32();
  829. uint32_t vb = b->GetU32();
  830. return (va < vb ? a : b);
  831. }
  832. } else if (int_type->width() == 64) {
  833. if (int_type->IsSigned()) {
  834. int64_t va = a->GetS64();
  835. int64_t vb = b->GetS64();
  836. return (va < vb ? a : b);
  837. } else {
  838. uint64_t va = a->GetU64();
  839. uint64_t vb = b->GetU64();
  840. return (va < vb ? a : b);
  841. }
  842. }
  843. } else if (const analysis::Float* float_type = result_type->AsFloat()) {
  844. if (float_type->width() == 32) {
  845. float va = a->GetFloat();
  846. float vb = b->GetFloat();
  847. return (va < vb ? a : b);
  848. } else if (float_type->width() == 64) {
  849. double va = a->GetDouble();
  850. double vb = b->GetDouble();
  851. return (va < vb ? a : b);
  852. }
  853. }
  854. return nullptr;
  855. }
  856. const analysis::Constant* FoldMax(const analysis::Type* result_type,
  857. const analysis::Constant* a,
  858. const analysis::Constant* b,
  859. analysis::ConstantManager*) {
  860. if (const analysis::Integer* int_type = result_type->AsInteger()) {
  861. if (int_type->width() == 32) {
  862. if (int_type->IsSigned()) {
  863. int32_t va = a->GetS32();
  864. int32_t vb = b->GetS32();
  865. return (va > vb ? a : b);
  866. } else {
  867. uint32_t va = a->GetU32();
  868. uint32_t vb = b->GetU32();
  869. return (va > vb ? a : b);
  870. }
  871. } else if (int_type->width() == 64) {
  872. if (int_type->IsSigned()) {
  873. int64_t va = a->GetS64();
  874. int64_t vb = b->GetS64();
  875. return (va > vb ? a : b);
  876. } else {
  877. uint64_t va = a->GetU64();
  878. uint64_t vb = b->GetU64();
  879. return (va > vb ? a : b);
  880. }
  881. }
  882. } else if (const analysis::Float* float_type = result_type->AsFloat()) {
  883. if (float_type->width() == 32) {
  884. float va = a->GetFloat();
  885. float vb = b->GetFloat();
  886. return (va > vb ? a : b);
  887. } else if (float_type->width() == 64) {
  888. double va = a->GetDouble();
  889. double vb = b->GetDouble();
  890. return (va > vb ? a : b);
  891. }
  892. }
  893. return nullptr;
  894. }
  895. // Fold an clamp instruction when all three operands are constant.
  896. const analysis::Constant* FoldClamp1(
  897. IRContext* context, Instruction* inst,
  898. const std::vector<const analysis::Constant*>& constants) {
  899. assert(inst->opcode() == SpvOpExtInst &&
  900. "Expecting an extended instruction.");
  901. assert(inst->GetSingleWordInOperand(0) ==
  902. context->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
  903. "Expecting a GLSLstd450 extended instruction.");
  904. // Make sure all Clamp operands are constants.
  905. for (uint32_t i = 1; i < 3; i++) {
  906. if (constants[i] == nullptr) {
  907. return nullptr;
  908. }
  909. }
  910. const analysis::Constant* temp = FoldFPBinaryOp(
  911. FoldMax, inst->type_id(), {constants[1], constants[2]}, context);
  912. if (temp == nullptr) {
  913. return nullptr;
  914. }
  915. return FoldFPBinaryOp(FoldMin, inst->type_id(), {temp, constants[3]},
  916. context);
  917. }
  918. // Fold a clamp instruction when |x >= min_val|.
  919. const analysis::Constant* FoldClamp2(
  920. IRContext* context, Instruction* inst,
  921. const std::vector<const analysis::Constant*>& constants) {
  922. assert(inst->opcode() == SpvOpExtInst &&
  923. "Expecting an extended instruction.");
  924. assert(inst->GetSingleWordInOperand(0) ==
  925. context->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
  926. "Expecting a GLSLstd450 extended instruction.");
  927. const analysis::Constant* x = constants[1];
  928. const analysis::Constant* min_val = constants[2];
  929. if (x == nullptr || min_val == nullptr) {
  930. return nullptr;
  931. }
  932. const analysis::Constant* temp =
  933. FoldFPBinaryOp(FoldMax, inst->type_id(), {x, min_val}, context);
  934. if (temp == min_val) {
  935. // We can assume that |min_val| is less than |max_val|. Therefore, if the
  936. // result of the max operation is |min_val|, we know the result of the min
  937. // operation, even if |max_val| is not a constant.
  938. return min_val;
  939. }
  940. return nullptr;
  941. }
  942. // Fold a clamp instruction when |x >= max_val|.
  943. const analysis::Constant* FoldClamp3(
  944. IRContext* context, Instruction* inst,
  945. const std::vector<const analysis::Constant*>& constants) {
  946. assert(inst->opcode() == SpvOpExtInst &&
  947. "Expecting an extended instruction.");
  948. assert(inst->GetSingleWordInOperand(0) ==
  949. context->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
  950. "Expecting a GLSLstd450 extended instruction.");
  951. const analysis::Constant* x = constants[1];
  952. const analysis::Constant* max_val = constants[3];
  953. if (x == nullptr || max_val == nullptr) {
  954. return nullptr;
  955. }
  956. const analysis::Constant* temp =
  957. FoldFPBinaryOp(FoldMin, inst->type_id(), {x, max_val}, context);
  958. if (temp == max_val) {
  959. // We can assume that |min_val| is less than |max_val|. Therefore, if the
  960. // result of the max operation is |min_val|, we know the result of the min
  961. // operation, even if |max_val| is not a constant.
  962. return max_val;
  963. }
  964. return nullptr;
  965. }
  966. } // namespace
  967. void ConstantFoldingRules::AddFoldingRules() {
  968. // Add all folding rules to the list for the opcodes to which they apply.
  969. // Note that the order in which rules are added to the list matters. If a rule
  970. // applies to the instruction, the rest of the rules will not be attempted.
  971. // Take that into consideration.
  972. rules_[SpvOpCompositeConstruct].push_back(FoldCompositeWithConstants());
  973. rules_[SpvOpCompositeExtract].push_back(FoldExtractWithConstants());
  974. rules_[SpvOpConvertFToS].push_back(FoldFToI());
  975. rules_[SpvOpConvertFToU].push_back(FoldFToI());
  976. rules_[SpvOpConvertSToF].push_back(FoldIToF());
  977. rules_[SpvOpConvertUToF].push_back(FoldIToF());
  978. rules_[SpvOpDot].push_back(FoldOpDotWithConstants());
  979. rules_[SpvOpFAdd].push_back(FoldFAdd());
  980. rules_[SpvOpFDiv].push_back(FoldFDiv());
  981. rules_[SpvOpFMul].push_back(FoldFMul());
  982. rules_[SpvOpFSub].push_back(FoldFSub());
  983. rules_[SpvOpFOrdEqual].push_back(FoldFOrdEqual());
  984. rules_[SpvOpFUnordEqual].push_back(FoldFUnordEqual());
  985. rules_[SpvOpFOrdNotEqual].push_back(FoldFOrdNotEqual());
  986. rules_[SpvOpFUnordNotEqual].push_back(FoldFUnordNotEqual());
  987. rules_[SpvOpFOrdLessThan].push_back(FoldFOrdLessThan());
  988. rules_[SpvOpFOrdLessThan].push_back(
  989. FoldFClampFeedingCompare(SpvOpFOrdLessThan));
  990. rules_[SpvOpFUnordLessThan].push_back(FoldFUnordLessThan());
  991. rules_[SpvOpFUnordLessThan].push_back(
  992. FoldFClampFeedingCompare(SpvOpFUnordLessThan));
  993. rules_[SpvOpFOrdGreaterThan].push_back(FoldFOrdGreaterThan());
  994. rules_[SpvOpFOrdGreaterThan].push_back(
  995. FoldFClampFeedingCompare(SpvOpFOrdGreaterThan));
  996. rules_[SpvOpFUnordGreaterThan].push_back(FoldFUnordGreaterThan());
  997. rules_[SpvOpFUnordGreaterThan].push_back(
  998. FoldFClampFeedingCompare(SpvOpFUnordGreaterThan));
  999. rules_[SpvOpFOrdLessThanEqual].push_back(FoldFOrdLessThanEqual());
  1000. rules_[SpvOpFOrdLessThanEqual].push_back(
  1001. FoldFClampFeedingCompare(SpvOpFOrdLessThanEqual));
  1002. rules_[SpvOpFUnordLessThanEqual].push_back(FoldFUnordLessThanEqual());
  1003. rules_[SpvOpFUnordLessThanEqual].push_back(
  1004. FoldFClampFeedingCompare(SpvOpFUnordLessThanEqual));
  1005. rules_[SpvOpFOrdGreaterThanEqual].push_back(FoldFOrdGreaterThanEqual());
  1006. rules_[SpvOpFOrdGreaterThanEqual].push_back(
  1007. FoldFClampFeedingCompare(SpvOpFOrdGreaterThanEqual));
  1008. rules_[SpvOpFUnordGreaterThanEqual].push_back(FoldFUnordGreaterThanEqual());
  1009. rules_[SpvOpFUnordGreaterThanEqual].push_back(
  1010. FoldFClampFeedingCompare(SpvOpFUnordGreaterThanEqual));
  1011. rules_[SpvOpVectorShuffle].push_back(FoldVectorShuffleWithConstants());
  1012. rules_[SpvOpVectorTimesScalar].push_back(FoldVectorTimesScalar());
  1013. rules_[SpvOpFNegate].push_back(FoldFNegate());
  1014. rules_[SpvOpQuantizeToF16].push_back(FoldQuantizeToF16());
  1015. // Add rules for GLSLstd450
  1016. FeatureManager* feature_manager = context_->get_feature_mgr();
  1017. uint32_t ext_inst_glslstd450_id =
  1018. feature_manager->GetExtInstImportId_GLSLstd450();
  1019. if (ext_inst_glslstd450_id != 0) {
  1020. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FMix}].push_back(FoldFMix());
  1021. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SMin}].push_back(
  1022. FoldFPBinaryOp(FoldMin));
  1023. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UMin}].push_back(
  1024. FoldFPBinaryOp(FoldMin));
  1025. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FMin}].push_back(
  1026. FoldFPBinaryOp(FoldMin));
  1027. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SMax}].push_back(
  1028. FoldFPBinaryOp(FoldMax));
  1029. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UMax}].push_back(
  1030. FoldFPBinaryOp(FoldMax));
  1031. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FMax}].push_back(
  1032. FoldFPBinaryOp(FoldMax));
  1033. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UClamp}].push_back(
  1034. FoldClamp1);
  1035. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UClamp}].push_back(
  1036. FoldClamp2);
  1037. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UClamp}].push_back(
  1038. FoldClamp3);
  1039. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SClamp}].push_back(
  1040. FoldClamp1);
  1041. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SClamp}].push_back(
  1042. FoldClamp2);
  1043. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SClamp}].push_back(
  1044. FoldClamp3);
  1045. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FClamp}].push_back(
  1046. FoldClamp1);
  1047. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FClamp}].push_back(
  1048. FoldClamp2);
  1049. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FClamp}].push_back(
  1050. FoldClamp3);
  1051. }
  1052. }
  1053. } // namespace opt
  1054. } // namespace spvtools