const_folding_rules.cpp 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  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. const analysis::Constant* arg =
  237. (inst->opcode() == SpvOpExtInst) ? constants[1] : constants[0];
  238. if (arg == nullptr) {
  239. return nullptr;
  240. }
  241. if (vector_type != nullptr) {
  242. std::vector<const analysis::Constant*> a_components;
  243. std::vector<const analysis::Constant*> results_components;
  244. a_components = arg->GetVectorComponents(const_mgr);
  245. // Fold each component of the vector.
  246. for (uint32_t i = 0; i < a_components.size(); ++i) {
  247. results_components.push_back(scalar_rule(vector_type->element_type(),
  248. a_components[i], const_mgr));
  249. if (results_components[i] == nullptr) {
  250. return nullptr;
  251. }
  252. }
  253. // Build the constant object and return it.
  254. std::vector<uint32_t> ids;
  255. for (const analysis::Constant* member : results_components) {
  256. ids.push_back(const_mgr->GetDefiningInstruction(member)->result_id());
  257. }
  258. return const_mgr->GetConstant(vector_type, ids);
  259. } else {
  260. return scalar_rule(result_type, arg, const_mgr);
  261. }
  262. };
  263. }
  264. // Returns the result of folding the constants in |constants| according the
  265. // |scalar_rule|. If |result_type| is a vector, then |scalar_rule| is applied
  266. // per component.
  267. const analysis::Constant* FoldFPBinaryOp(
  268. BinaryScalarFoldingRule scalar_rule, uint32_t result_type_id,
  269. const std::vector<const analysis::Constant*>& constants,
  270. IRContext* context) {
  271. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  272. analysis::TypeManager* type_mgr = context->get_type_mgr();
  273. const analysis::Type* result_type = type_mgr->GetType(result_type_id);
  274. const analysis::Vector* vector_type = result_type->AsVector();
  275. if (constants[0] == nullptr || constants[1] == nullptr) {
  276. return nullptr;
  277. }
  278. if (vector_type != nullptr) {
  279. std::vector<const analysis::Constant*> a_components;
  280. std::vector<const analysis::Constant*> b_components;
  281. std::vector<const analysis::Constant*> results_components;
  282. a_components = constants[0]->GetVectorComponents(const_mgr);
  283. b_components = constants[1]->GetVectorComponents(const_mgr);
  284. // Fold each component of the vector.
  285. for (uint32_t i = 0; i < a_components.size(); ++i) {
  286. results_components.push_back(scalar_rule(vector_type->element_type(),
  287. a_components[i], b_components[i],
  288. const_mgr));
  289. if (results_components[i] == nullptr) {
  290. return nullptr;
  291. }
  292. }
  293. // Build the constant object and return it.
  294. std::vector<uint32_t> ids;
  295. for (const analysis::Constant* member : results_components) {
  296. ids.push_back(const_mgr->GetDefiningInstruction(member)->result_id());
  297. }
  298. return const_mgr->GetConstant(vector_type, ids);
  299. } else {
  300. return scalar_rule(result_type, constants[0], constants[1], const_mgr);
  301. }
  302. }
  303. // Returns a |ConstantFoldingRule| that folds floating point scalars using
  304. // |scalar_rule| and vectors of floating point by applying |scalar_rule| to the
  305. // elements of the vector. The |ConstantFoldingRule| that is returned assumes
  306. // that |constants| contains 2 entries. If they are not |nullptr|, then their
  307. // type is either |Float| or a |Vector| whose element type is |Float|.
  308. ConstantFoldingRule FoldFPBinaryOp(BinaryScalarFoldingRule scalar_rule) {
  309. return [scalar_rule](IRContext* context, Instruction* inst,
  310. const std::vector<const analysis::Constant*>& constants)
  311. -> const analysis::Constant* {
  312. if (!inst->IsFloatingPointFoldingAllowed()) {
  313. return nullptr;
  314. }
  315. if (inst->opcode() == SpvOpExtInst) {
  316. return FoldFPBinaryOp(scalar_rule, inst->type_id(),
  317. {constants[1], constants[2]}, context);
  318. }
  319. return FoldFPBinaryOp(scalar_rule, inst->type_id(), constants, context);
  320. };
  321. }
  322. // This macro defines a |UnaryScalarFoldingRule| that performs float to
  323. // integer conversion.
  324. // TODO(greg-lunarg): Support for 64-bit integer types.
  325. UnaryScalarFoldingRule FoldFToIOp() {
  326. return [](const analysis::Type* result_type, const analysis::Constant* a,
  327. analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
  328. assert(result_type != nullptr && a != nullptr);
  329. const analysis::Integer* integer_type = result_type->AsInteger();
  330. const analysis::Float* float_type = a->type()->AsFloat();
  331. assert(float_type != nullptr);
  332. assert(integer_type != nullptr);
  333. if (integer_type->width() != 32) return nullptr;
  334. if (float_type->width() == 32) {
  335. float fa = a->GetFloat();
  336. uint32_t result = integer_type->IsSigned()
  337. ? static_cast<uint32_t>(static_cast<int32_t>(fa))
  338. : static_cast<uint32_t>(fa);
  339. std::vector<uint32_t> words = {result};
  340. return const_mgr->GetConstant(result_type, words);
  341. } else if (float_type->width() == 64) {
  342. double fa = a->GetDouble();
  343. uint32_t result = integer_type->IsSigned()
  344. ? static_cast<uint32_t>(static_cast<int32_t>(fa))
  345. : static_cast<uint32_t>(fa);
  346. std::vector<uint32_t> words = {result};
  347. return const_mgr->GetConstant(result_type, words);
  348. }
  349. return nullptr;
  350. };
  351. }
  352. // This function defines a |UnaryScalarFoldingRule| that performs integer to
  353. // float conversion.
  354. // TODO(greg-lunarg): Support for 64-bit integer types.
  355. UnaryScalarFoldingRule FoldIToFOp() {
  356. return [](const analysis::Type* result_type, const analysis::Constant* a,
  357. analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
  358. assert(result_type != nullptr && a != nullptr);
  359. const analysis::Integer* integer_type = a->type()->AsInteger();
  360. const analysis::Float* float_type = result_type->AsFloat();
  361. assert(float_type != nullptr);
  362. assert(integer_type != nullptr);
  363. if (integer_type->width() != 32) return nullptr;
  364. uint32_t ua = a->GetU32();
  365. if (float_type->width() == 32) {
  366. float result_val = integer_type->IsSigned()
  367. ? static_cast<float>(static_cast<int32_t>(ua))
  368. : static_cast<float>(ua);
  369. utils::FloatProxy<float> result(result_val);
  370. std::vector<uint32_t> words = {result.data()};
  371. return const_mgr->GetConstant(result_type, words);
  372. } else if (float_type->width() == 64) {
  373. double result_val = integer_type->IsSigned()
  374. ? static_cast<double>(static_cast<int32_t>(ua))
  375. : static_cast<double>(ua);
  376. utils::FloatProxy<double> result(result_val);
  377. std::vector<uint32_t> words = result.GetWords();
  378. return const_mgr->GetConstant(result_type, words);
  379. }
  380. return nullptr;
  381. };
  382. }
  383. // This defines a |UnaryScalarFoldingRule| that performs |OpQuantizeToF16|.
  384. UnaryScalarFoldingRule FoldQuantizeToF16Scalar() {
  385. return [](const analysis::Type* result_type, const analysis::Constant* a,
  386. analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
  387. assert(result_type != nullptr && a != nullptr);
  388. const analysis::Float* float_type = a->type()->AsFloat();
  389. assert(float_type != nullptr);
  390. if (float_type->width() != 32) {
  391. return nullptr;
  392. }
  393. float fa = a->GetFloat();
  394. utils::HexFloat<utils::FloatProxy<float>> orignal(fa);
  395. utils::HexFloat<utils::FloatProxy<utils::Float16>> quantized(0);
  396. utils::HexFloat<utils::FloatProxy<float>> result(0.0f);
  397. orignal.castTo(quantized, utils::round_direction::kToZero);
  398. quantized.castTo(result, utils::round_direction::kToZero);
  399. std::vector<uint32_t> words = {result.getBits()};
  400. return const_mgr->GetConstant(result_type, words);
  401. };
  402. }
  403. // This macro defines a |BinaryScalarFoldingRule| that applies |op|. The
  404. // operator |op| must work for both float and double, and use syntax "f1 op f2".
  405. #define FOLD_FPARITH_OP(op) \
  406. [](const analysis::Type* result_type_in_macro, const analysis::Constant* a, \
  407. const analysis::Constant* b, \
  408. analysis::ConstantManager* const_mgr_in_macro) \
  409. -> const analysis::Constant* { \
  410. assert(result_type_in_macro != nullptr && a != nullptr && b != nullptr); \
  411. assert(result_type_in_macro == a->type() && \
  412. result_type_in_macro == b->type()); \
  413. const analysis::Float* float_type_in_macro = \
  414. result_type_in_macro->AsFloat(); \
  415. assert(float_type_in_macro != nullptr); \
  416. if (float_type_in_macro->width() == 32) { \
  417. float fa = a->GetFloat(); \
  418. float fb = b->GetFloat(); \
  419. utils::FloatProxy<float> result_in_macro(fa op fb); \
  420. std::vector<uint32_t> words_in_macro = result_in_macro.GetWords(); \
  421. return const_mgr_in_macro->GetConstant(result_type_in_macro, \
  422. words_in_macro); \
  423. } else if (float_type_in_macro->width() == 64) { \
  424. double fa = a->GetDouble(); \
  425. double fb = b->GetDouble(); \
  426. utils::FloatProxy<double> result_in_macro(fa op fb); \
  427. std::vector<uint32_t> words_in_macro = result_in_macro.GetWords(); \
  428. return const_mgr_in_macro->GetConstant(result_type_in_macro, \
  429. words_in_macro); \
  430. } \
  431. return nullptr; \
  432. }
  433. // Define the folding rule for conversion between floating point and integer
  434. ConstantFoldingRule FoldFToI() { return FoldFPUnaryOp(FoldFToIOp()); }
  435. ConstantFoldingRule FoldIToF() { return FoldFPUnaryOp(FoldIToFOp()); }
  436. ConstantFoldingRule FoldQuantizeToF16() {
  437. return FoldFPUnaryOp(FoldQuantizeToF16Scalar());
  438. }
  439. // Define the folding rules for subtraction, addition, multiplication, and
  440. // division for floating point values.
  441. ConstantFoldingRule FoldFSub() { return FoldFPBinaryOp(FOLD_FPARITH_OP(-)); }
  442. ConstantFoldingRule FoldFAdd() { return FoldFPBinaryOp(FOLD_FPARITH_OP(+)); }
  443. ConstantFoldingRule FoldFMul() { return FoldFPBinaryOp(FOLD_FPARITH_OP(*)); }
  444. ConstantFoldingRule FoldFDiv() { return FoldFPBinaryOp(FOLD_FPARITH_OP(/)); }
  445. bool CompareFloatingPoint(bool op_result, bool op_unordered,
  446. bool need_ordered) {
  447. if (need_ordered) {
  448. // operands are ordered and Operand 1 is |op| Operand 2
  449. return !op_unordered && op_result;
  450. } else {
  451. // operands are unordered or Operand 1 is |op| Operand 2
  452. return op_unordered || op_result;
  453. }
  454. }
  455. // This macro defines a |BinaryScalarFoldingRule| that applies |op|. The
  456. // operator |op| must work for both float and double, and use syntax "f1 op f2".
  457. #define FOLD_FPCMP_OP(op, ord) \
  458. [](const analysis::Type* result_type, const analysis::Constant* a, \
  459. const analysis::Constant* b, \
  460. analysis::ConstantManager* const_mgr) -> const analysis::Constant* { \
  461. assert(result_type != nullptr && a != nullptr && b != nullptr); \
  462. assert(result_type->AsBool()); \
  463. assert(a->type() == b->type()); \
  464. const analysis::Float* float_type = a->type()->AsFloat(); \
  465. assert(float_type != nullptr); \
  466. if (float_type->width() == 32) { \
  467. float fa = a->GetFloat(); \
  468. float fb = b->GetFloat(); \
  469. bool result = CompareFloatingPoint( \
  470. fa op fb, std::isnan(fa) || std::isnan(fb), ord); \
  471. std::vector<uint32_t> words = {uint32_t(result)}; \
  472. return const_mgr->GetConstant(result_type, words); \
  473. } else if (float_type->width() == 64) { \
  474. double fa = a->GetDouble(); \
  475. double fb = b->GetDouble(); \
  476. bool result = CompareFloatingPoint( \
  477. fa op fb, std::isnan(fa) || std::isnan(fb), ord); \
  478. std::vector<uint32_t> words = {uint32_t(result)}; \
  479. return const_mgr->GetConstant(result_type, words); \
  480. } \
  481. return nullptr; \
  482. }
  483. // Define the folding rules for ordered and unordered comparison for floating
  484. // point values.
  485. ConstantFoldingRule FoldFOrdEqual() {
  486. return FoldFPBinaryOp(FOLD_FPCMP_OP(==, true));
  487. }
  488. ConstantFoldingRule FoldFUnordEqual() {
  489. return FoldFPBinaryOp(FOLD_FPCMP_OP(==, false));
  490. }
  491. ConstantFoldingRule FoldFOrdNotEqual() {
  492. return FoldFPBinaryOp(FOLD_FPCMP_OP(!=, true));
  493. }
  494. ConstantFoldingRule FoldFUnordNotEqual() {
  495. return FoldFPBinaryOp(FOLD_FPCMP_OP(!=, false));
  496. }
  497. ConstantFoldingRule FoldFOrdLessThan() {
  498. return FoldFPBinaryOp(FOLD_FPCMP_OP(<, true));
  499. }
  500. ConstantFoldingRule FoldFUnordLessThan() {
  501. return FoldFPBinaryOp(FOLD_FPCMP_OP(<, false));
  502. }
  503. ConstantFoldingRule FoldFOrdGreaterThan() {
  504. return FoldFPBinaryOp(FOLD_FPCMP_OP(>, true));
  505. }
  506. ConstantFoldingRule FoldFUnordGreaterThan() {
  507. return FoldFPBinaryOp(FOLD_FPCMP_OP(>, false));
  508. }
  509. ConstantFoldingRule FoldFOrdLessThanEqual() {
  510. return FoldFPBinaryOp(FOLD_FPCMP_OP(<=, true));
  511. }
  512. ConstantFoldingRule FoldFUnordLessThanEqual() {
  513. return FoldFPBinaryOp(FOLD_FPCMP_OP(<=, false));
  514. }
  515. ConstantFoldingRule FoldFOrdGreaterThanEqual() {
  516. return FoldFPBinaryOp(FOLD_FPCMP_OP(>=, true));
  517. }
  518. ConstantFoldingRule FoldFUnordGreaterThanEqual() {
  519. return FoldFPBinaryOp(FOLD_FPCMP_OP(>=, false));
  520. }
  521. // Folds an OpDot where all of the inputs are constants to a
  522. // constant. A new constant is created if necessary.
  523. ConstantFoldingRule FoldOpDotWithConstants() {
  524. return [](IRContext* context, Instruction* inst,
  525. const std::vector<const analysis::Constant*>& constants)
  526. -> const analysis::Constant* {
  527. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  528. analysis::TypeManager* type_mgr = context->get_type_mgr();
  529. const analysis::Type* new_type = type_mgr->GetType(inst->type_id());
  530. assert(new_type->AsFloat() && "OpDot should have a float return type.");
  531. const analysis::Float* float_type = new_type->AsFloat();
  532. if (!inst->IsFloatingPointFoldingAllowed()) {
  533. return nullptr;
  534. }
  535. // If one of the operands is 0, then the result is 0.
  536. bool has_zero_operand = false;
  537. for (int i = 0; i < 2; ++i) {
  538. if (constants[i]) {
  539. if (constants[i]->AsNullConstant() ||
  540. constants[i]->AsVectorConstant()->IsZero()) {
  541. has_zero_operand = true;
  542. break;
  543. }
  544. }
  545. }
  546. if (has_zero_operand) {
  547. if (float_type->width() == 32) {
  548. utils::FloatProxy<float> result(0.0f);
  549. std::vector<uint32_t> words = result.GetWords();
  550. return const_mgr->GetConstant(float_type, words);
  551. }
  552. if (float_type->width() == 64) {
  553. utils::FloatProxy<double> result(0.0);
  554. std::vector<uint32_t> words = result.GetWords();
  555. return const_mgr->GetConstant(float_type, words);
  556. }
  557. return nullptr;
  558. }
  559. if (constants[0] == nullptr || constants[1] == nullptr) {
  560. return nullptr;
  561. }
  562. std::vector<const analysis::Constant*> a_components;
  563. std::vector<const analysis::Constant*> b_components;
  564. a_components = constants[0]->GetVectorComponents(const_mgr);
  565. b_components = constants[1]->GetVectorComponents(const_mgr);
  566. utils::FloatProxy<double> result(0.0);
  567. std::vector<uint32_t> words = result.GetWords();
  568. const analysis::Constant* result_const =
  569. const_mgr->GetConstant(float_type, words);
  570. for (uint32_t i = 0; i < a_components.size() && result_const != nullptr;
  571. ++i) {
  572. if (a_components[i] == nullptr || b_components[i] == nullptr) {
  573. return nullptr;
  574. }
  575. const analysis::Constant* component = FOLD_FPARITH_OP(*)(
  576. new_type, a_components[i], b_components[i], const_mgr);
  577. if (component == nullptr) {
  578. return nullptr;
  579. }
  580. result_const =
  581. FOLD_FPARITH_OP(+)(new_type, result_const, component, const_mgr);
  582. }
  583. return result_const;
  584. };
  585. }
  586. // This function defines a |UnaryScalarFoldingRule| that subtracts the constant
  587. // from zero.
  588. UnaryScalarFoldingRule FoldFNegateOp() {
  589. return [](const analysis::Type* result_type, const analysis::Constant* a,
  590. analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
  591. assert(result_type != nullptr && a != nullptr);
  592. assert(result_type == a->type());
  593. const analysis::Float* float_type = result_type->AsFloat();
  594. assert(float_type != nullptr);
  595. if (float_type->width() == 32) {
  596. float fa = a->GetFloat();
  597. utils::FloatProxy<float> result(-fa);
  598. std::vector<uint32_t> words = result.GetWords();
  599. return const_mgr->GetConstant(result_type, words);
  600. } else if (float_type->width() == 64) {
  601. double da = a->GetDouble();
  602. utils::FloatProxy<double> result(-da);
  603. std::vector<uint32_t> words = result.GetWords();
  604. return const_mgr->GetConstant(result_type, words);
  605. }
  606. return nullptr;
  607. };
  608. }
  609. ConstantFoldingRule FoldFNegate() { return FoldFPUnaryOp(FoldFNegateOp()); }
  610. ConstantFoldingRule FoldFClampFeedingCompare(uint32_t cmp_opcode) {
  611. return [cmp_opcode](IRContext* context, Instruction* inst,
  612. const std::vector<const analysis::Constant*>& constants)
  613. -> const analysis::Constant* {
  614. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  615. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  616. if (!inst->IsFloatingPointFoldingAllowed()) {
  617. return nullptr;
  618. }
  619. uint32_t non_const_idx = (constants[0] ? 1 : 0);
  620. uint32_t operand_id = inst->GetSingleWordInOperand(non_const_idx);
  621. Instruction* operand_inst = def_use_mgr->GetDef(operand_id);
  622. analysis::TypeManager* type_mgr = context->get_type_mgr();
  623. const analysis::Type* operand_type =
  624. type_mgr->GetType(operand_inst->type_id());
  625. if (!operand_type->AsFloat()) {
  626. return nullptr;
  627. }
  628. if (operand_type->AsFloat()->width() != 32 &&
  629. operand_type->AsFloat()->width() != 64) {
  630. return nullptr;
  631. }
  632. if (operand_inst->opcode() != SpvOpExtInst) {
  633. return nullptr;
  634. }
  635. if (operand_inst->GetSingleWordInOperand(1) != GLSLstd450FClamp) {
  636. return nullptr;
  637. }
  638. if (constants[1] == nullptr && constants[0] == nullptr) {
  639. return nullptr;
  640. }
  641. uint32_t max_id = operand_inst->GetSingleWordInOperand(4);
  642. const analysis::Constant* max_const =
  643. const_mgr->FindDeclaredConstant(max_id);
  644. uint32_t min_id = operand_inst->GetSingleWordInOperand(3);
  645. const analysis::Constant* min_const =
  646. const_mgr->FindDeclaredConstant(min_id);
  647. bool found_result = false;
  648. bool result = false;
  649. switch (cmp_opcode) {
  650. case SpvOpFOrdLessThan:
  651. case SpvOpFUnordLessThan:
  652. case SpvOpFOrdGreaterThanEqual:
  653. case SpvOpFUnordGreaterThanEqual:
  654. if (constants[0]) {
  655. if (min_const) {
  656. if (constants[0]->GetValueAsDouble() <
  657. min_const->GetValueAsDouble()) {
  658. found_result = true;
  659. result = (cmp_opcode == SpvOpFOrdLessThan ||
  660. cmp_opcode == SpvOpFUnordLessThan);
  661. }
  662. }
  663. if (max_const) {
  664. if (constants[0]->GetValueAsDouble() >=
  665. max_const->GetValueAsDouble()) {
  666. found_result = true;
  667. result = !(cmp_opcode == SpvOpFOrdLessThan ||
  668. cmp_opcode == SpvOpFUnordLessThan);
  669. }
  670. }
  671. }
  672. if (constants[1]) {
  673. if (max_const) {
  674. if (max_const->GetValueAsDouble() <
  675. constants[1]->GetValueAsDouble()) {
  676. found_result = true;
  677. result = (cmp_opcode == SpvOpFOrdLessThan ||
  678. cmp_opcode == SpvOpFUnordLessThan);
  679. }
  680. }
  681. if (min_const) {
  682. if (min_const->GetValueAsDouble() >=
  683. constants[1]->GetValueAsDouble()) {
  684. found_result = true;
  685. result = !(cmp_opcode == SpvOpFOrdLessThan ||
  686. cmp_opcode == SpvOpFUnordLessThan);
  687. }
  688. }
  689. }
  690. break;
  691. case SpvOpFOrdGreaterThan:
  692. case SpvOpFUnordGreaterThan:
  693. case SpvOpFOrdLessThanEqual:
  694. case SpvOpFUnordLessThanEqual:
  695. if (constants[0]) {
  696. if (min_const) {
  697. if (constants[0]->GetValueAsDouble() <=
  698. min_const->GetValueAsDouble()) {
  699. found_result = true;
  700. result = (cmp_opcode == SpvOpFOrdLessThanEqual ||
  701. cmp_opcode == SpvOpFUnordLessThanEqual);
  702. }
  703. }
  704. if (max_const) {
  705. if (constants[0]->GetValueAsDouble() >
  706. max_const->GetValueAsDouble()) {
  707. found_result = true;
  708. result = !(cmp_opcode == SpvOpFOrdLessThanEqual ||
  709. cmp_opcode == SpvOpFUnordLessThanEqual);
  710. }
  711. }
  712. }
  713. if (constants[1]) {
  714. if (max_const) {
  715. if (max_const->GetValueAsDouble() <=
  716. constants[1]->GetValueAsDouble()) {
  717. found_result = true;
  718. result = (cmp_opcode == SpvOpFOrdLessThanEqual ||
  719. cmp_opcode == SpvOpFUnordLessThanEqual);
  720. }
  721. }
  722. if (min_const) {
  723. if (min_const->GetValueAsDouble() >
  724. constants[1]->GetValueAsDouble()) {
  725. found_result = true;
  726. result = !(cmp_opcode == SpvOpFOrdLessThanEqual ||
  727. cmp_opcode == SpvOpFUnordLessThanEqual);
  728. }
  729. }
  730. }
  731. break;
  732. default:
  733. return nullptr;
  734. }
  735. if (!found_result) {
  736. return nullptr;
  737. }
  738. const analysis::Type* bool_type =
  739. context->get_type_mgr()->GetType(inst->type_id());
  740. const analysis::Constant* result_const =
  741. const_mgr->GetConstant(bool_type, {static_cast<uint32_t>(result)});
  742. assert(result_const);
  743. return result_const;
  744. };
  745. }
  746. ConstantFoldingRule FoldFMix() {
  747. return [](IRContext* context, Instruction* inst,
  748. const std::vector<const analysis::Constant*>& constants)
  749. -> const analysis::Constant* {
  750. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  751. assert(inst->opcode() == SpvOpExtInst &&
  752. "Expecting an extended instruction.");
  753. assert(inst->GetSingleWordInOperand(0) ==
  754. context->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
  755. "Expecting a GLSLstd450 extended instruction.");
  756. assert(inst->GetSingleWordInOperand(1) == GLSLstd450FMix &&
  757. "Expecting and FMix instruction.");
  758. if (!inst->IsFloatingPointFoldingAllowed()) {
  759. return nullptr;
  760. }
  761. // Make sure all FMix operands are constants.
  762. for (uint32_t i = 1; i < 4; i++) {
  763. if (constants[i] == nullptr) {
  764. return nullptr;
  765. }
  766. }
  767. const analysis::Constant* one;
  768. bool is_vector = false;
  769. const analysis::Type* result_type = constants[1]->type();
  770. const analysis::Type* base_type = result_type;
  771. if (base_type->AsVector()) {
  772. is_vector = true;
  773. base_type = base_type->AsVector()->element_type();
  774. }
  775. assert(base_type->AsFloat() != nullptr &&
  776. "FMix is suppose to act on floats or vectors of floats.");
  777. if (base_type->AsFloat()->width() == 32) {
  778. one = const_mgr->GetConstant(base_type,
  779. utils::FloatProxy<float>(1.0f).GetWords());
  780. } else {
  781. one = const_mgr->GetConstant(base_type,
  782. utils::FloatProxy<double>(1.0).GetWords());
  783. }
  784. if (is_vector) {
  785. uint32_t one_id = const_mgr->GetDefiningInstruction(one)->result_id();
  786. one =
  787. const_mgr->GetConstant(result_type, std::vector<uint32_t>(4, one_id));
  788. }
  789. const analysis::Constant* temp1 = FoldFPBinaryOp(
  790. FOLD_FPARITH_OP(-), inst->type_id(), {one, constants[3]}, context);
  791. if (temp1 == nullptr) {
  792. return nullptr;
  793. }
  794. const analysis::Constant* temp2 = FoldFPBinaryOp(
  795. FOLD_FPARITH_OP(*), inst->type_id(), {constants[1], temp1}, context);
  796. if (temp2 == nullptr) {
  797. return nullptr;
  798. }
  799. const analysis::Constant* temp3 =
  800. FoldFPBinaryOp(FOLD_FPARITH_OP(*), inst->type_id(),
  801. {constants[2], constants[3]}, context);
  802. if (temp3 == nullptr) {
  803. return nullptr;
  804. }
  805. return FoldFPBinaryOp(FOLD_FPARITH_OP(+), inst->type_id(), {temp2, temp3},
  806. context);
  807. };
  808. }
  809. template <class IntType>
  810. IntType FoldIClamp(IntType x, IntType min_val, IntType max_val) {
  811. if (x < min_val) {
  812. x = min_val;
  813. }
  814. if (x > max_val) {
  815. x = max_val;
  816. }
  817. return x;
  818. }
  819. const analysis::Constant* FoldMin(const analysis::Type* result_type,
  820. const analysis::Constant* a,
  821. const analysis::Constant* b,
  822. analysis::ConstantManager*) {
  823. if (const analysis::Integer* int_type = result_type->AsInteger()) {
  824. if (int_type->width() == 32) {
  825. if (int_type->IsSigned()) {
  826. int32_t va = a->GetS32();
  827. int32_t vb = b->GetS32();
  828. return (va < vb ? a : b);
  829. } else {
  830. uint32_t va = a->GetU32();
  831. uint32_t vb = b->GetU32();
  832. return (va < vb ? a : b);
  833. }
  834. } else if (int_type->width() == 64) {
  835. if (int_type->IsSigned()) {
  836. int64_t va = a->GetS64();
  837. int64_t vb = b->GetS64();
  838. return (va < vb ? a : b);
  839. } else {
  840. uint64_t va = a->GetU64();
  841. uint64_t vb = b->GetU64();
  842. return (va < vb ? a : b);
  843. }
  844. }
  845. } else if (const analysis::Float* float_type = result_type->AsFloat()) {
  846. if (float_type->width() == 32) {
  847. float va = a->GetFloat();
  848. float vb = b->GetFloat();
  849. return (va < vb ? a : b);
  850. } else if (float_type->width() == 64) {
  851. double va = a->GetDouble();
  852. double vb = b->GetDouble();
  853. return (va < vb ? a : b);
  854. }
  855. }
  856. return nullptr;
  857. }
  858. const analysis::Constant* FoldMax(const analysis::Type* result_type,
  859. const analysis::Constant* a,
  860. const analysis::Constant* b,
  861. analysis::ConstantManager*) {
  862. if (const analysis::Integer* int_type = result_type->AsInteger()) {
  863. if (int_type->width() == 32) {
  864. if (int_type->IsSigned()) {
  865. int32_t va = a->GetS32();
  866. int32_t vb = b->GetS32();
  867. return (va > vb ? a : b);
  868. } else {
  869. uint32_t va = a->GetU32();
  870. uint32_t vb = b->GetU32();
  871. return (va > vb ? a : b);
  872. }
  873. } else if (int_type->width() == 64) {
  874. if (int_type->IsSigned()) {
  875. int64_t va = a->GetS64();
  876. int64_t vb = b->GetS64();
  877. return (va > vb ? a : b);
  878. } else {
  879. uint64_t va = a->GetU64();
  880. uint64_t vb = b->GetU64();
  881. return (va > vb ? a : b);
  882. }
  883. }
  884. } else if (const analysis::Float* float_type = result_type->AsFloat()) {
  885. if (float_type->width() == 32) {
  886. float va = a->GetFloat();
  887. float vb = b->GetFloat();
  888. return (va > vb ? a : b);
  889. } else if (float_type->width() == 64) {
  890. double va = a->GetDouble();
  891. double vb = b->GetDouble();
  892. return (va > vb ? a : b);
  893. }
  894. }
  895. return nullptr;
  896. }
  897. // Fold an clamp instruction when all three operands are constant.
  898. const analysis::Constant* FoldClamp1(
  899. IRContext* context, Instruction* inst,
  900. const std::vector<const analysis::Constant*>& constants) {
  901. assert(inst->opcode() == SpvOpExtInst &&
  902. "Expecting an extended instruction.");
  903. assert(inst->GetSingleWordInOperand(0) ==
  904. context->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
  905. "Expecting a GLSLstd450 extended instruction.");
  906. // Make sure all Clamp operands are constants.
  907. for (uint32_t i = 1; i < 4; i++) {
  908. if (constants[i] == nullptr) {
  909. return nullptr;
  910. }
  911. }
  912. const analysis::Constant* temp = FoldFPBinaryOp(
  913. FoldMax, inst->type_id(), {constants[1], constants[2]}, context);
  914. if (temp == nullptr) {
  915. return nullptr;
  916. }
  917. return FoldFPBinaryOp(FoldMin, inst->type_id(), {temp, constants[3]},
  918. context);
  919. }
  920. // Fold a clamp instruction when |x <= min_val|.
  921. const analysis::Constant* FoldClamp2(
  922. IRContext* context, Instruction* inst,
  923. const std::vector<const analysis::Constant*>& constants) {
  924. assert(inst->opcode() == SpvOpExtInst &&
  925. "Expecting an extended instruction.");
  926. assert(inst->GetSingleWordInOperand(0) ==
  927. context->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
  928. "Expecting a GLSLstd450 extended instruction.");
  929. const analysis::Constant* x = constants[1];
  930. const analysis::Constant* min_val = constants[2];
  931. if (x == nullptr || min_val == nullptr) {
  932. return nullptr;
  933. }
  934. const analysis::Constant* temp =
  935. FoldFPBinaryOp(FoldMax, inst->type_id(), {x, min_val}, context);
  936. if (temp == min_val) {
  937. // We can assume that |min_val| is less than |max_val|. Therefore, if the
  938. // result of the max operation is |min_val|, we know the result of the min
  939. // operation, even if |max_val| is not a constant.
  940. return min_val;
  941. }
  942. return nullptr;
  943. }
  944. // Fold a clamp instruction when |x >= max_val|.
  945. const analysis::Constant* FoldClamp3(
  946. IRContext* context, Instruction* inst,
  947. const std::vector<const analysis::Constant*>& constants) {
  948. assert(inst->opcode() == SpvOpExtInst &&
  949. "Expecting an extended instruction.");
  950. assert(inst->GetSingleWordInOperand(0) ==
  951. context->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
  952. "Expecting a GLSLstd450 extended instruction.");
  953. const analysis::Constant* x = constants[1];
  954. const analysis::Constant* max_val = constants[3];
  955. if (x == nullptr || max_val == nullptr) {
  956. return nullptr;
  957. }
  958. const analysis::Constant* temp =
  959. FoldFPBinaryOp(FoldMin, inst->type_id(), {x, max_val}, context);
  960. if (temp == max_val) {
  961. // We can assume that |min_val| is less than |max_val|. Therefore, if the
  962. // result of the max operation is |min_val|, we know the result of the min
  963. // operation, even if |max_val| is not a constant.
  964. return max_val;
  965. }
  966. return nullptr;
  967. }
  968. UnaryScalarFoldingRule FoldFTranscendentalUnary(double (*fp)(double)) {
  969. return
  970. [fp](const analysis::Type* result_type, const analysis::Constant* a,
  971. analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
  972. assert(result_type != nullptr && a != nullptr);
  973. const analysis::Float* float_type = a->type()->AsFloat();
  974. assert(float_type != nullptr);
  975. assert(float_type == result_type->AsFloat());
  976. if (float_type->width() == 32) {
  977. float fa = a->GetFloat();
  978. float res = static_cast<float>(fp(fa));
  979. utils::FloatProxy<float> result(res);
  980. std::vector<uint32_t> words = result.GetWords();
  981. return const_mgr->GetConstant(result_type, words);
  982. } else if (float_type->width() == 64) {
  983. double fa = a->GetDouble();
  984. double res = fp(fa);
  985. utils::FloatProxy<double> result(res);
  986. std::vector<uint32_t> words = result.GetWords();
  987. return const_mgr->GetConstant(result_type, words);
  988. }
  989. return nullptr;
  990. };
  991. }
  992. BinaryScalarFoldingRule FoldFTranscendentalBinary(double (*fp)(double,
  993. double)) {
  994. return
  995. [fp](const analysis::Type* result_type, const analysis::Constant* a,
  996. const analysis::Constant* b,
  997. analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
  998. assert(result_type != nullptr && a != nullptr);
  999. const analysis::Float* float_type = a->type()->AsFloat();
  1000. assert(float_type != nullptr);
  1001. assert(float_type == result_type->AsFloat());
  1002. assert(float_type == b->type()->AsFloat());
  1003. if (float_type->width() == 32) {
  1004. float fa = a->GetFloat();
  1005. float fb = b->GetFloat();
  1006. float res = static_cast<float>(fp(fa, fb));
  1007. utils::FloatProxy<float> result(res);
  1008. std::vector<uint32_t> words = result.GetWords();
  1009. return const_mgr->GetConstant(result_type, words);
  1010. } else if (float_type->width() == 64) {
  1011. double fa = a->GetDouble();
  1012. double fb = b->GetDouble();
  1013. double res = fp(fa, fb);
  1014. utils::FloatProxy<double> result(res);
  1015. std::vector<uint32_t> words = result.GetWords();
  1016. return const_mgr->GetConstant(result_type, words);
  1017. }
  1018. return nullptr;
  1019. };
  1020. }
  1021. } // namespace
  1022. void ConstantFoldingRules::AddFoldingRules() {
  1023. // Add all folding rules to the list for the opcodes to which they apply.
  1024. // Note that the order in which rules are added to the list matters. If a rule
  1025. // applies to the instruction, the rest of the rules will not be attempted.
  1026. // Take that into consideration.
  1027. rules_[SpvOpCompositeConstruct].push_back(FoldCompositeWithConstants());
  1028. rules_[SpvOpCompositeExtract].push_back(FoldExtractWithConstants());
  1029. rules_[SpvOpConvertFToS].push_back(FoldFToI());
  1030. rules_[SpvOpConvertFToU].push_back(FoldFToI());
  1031. rules_[SpvOpConvertSToF].push_back(FoldIToF());
  1032. rules_[SpvOpConvertUToF].push_back(FoldIToF());
  1033. rules_[SpvOpDot].push_back(FoldOpDotWithConstants());
  1034. rules_[SpvOpFAdd].push_back(FoldFAdd());
  1035. rules_[SpvOpFDiv].push_back(FoldFDiv());
  1036. rules_[SpvOpFMul].push_back(FoldFMul());
  1037. rules_[SpvOpFSub].push_back(FoldFSub());
  1038. rules_[SpvOpFOrdEqual].push_back(FoldFOrdEqual());
  1039. rules_[SpvOpFUnordEqual].push_back(FoldFUnordEqual());
  1040. rules_[SpvOpFOrdNotEqual].push_back(FoldFOrdNotEqual());
  1041. rules_[SpvOpFUnordNotEqual].push_back(FoldFUnordNotEqual());
  1042. rules_[SpvOpFOrdLessThan].push_back(FoldFOrdLessThan());
  1043. rules_[SpvOpFOrdLessThan].push_back(
  1044. FoldFClampFeedingCompare(SpvOpFOrdLessThan));
  1045. rules_[SpvOpFUnordLessThan].push_back(FoldFUnordLessThan());
  1046. rules_[SpvOpFUnordLessThan].push_back(
  1047. FoldFClampFeedingCompare(SpvOpFUnordLessThan));
  1048. rules_[SpvOpFOrdGreaterThan].push_back(FoldFOrdGreaterThan());
  1049. rules_[SpvOpFOrdGreaterThan].push_back(
  1050. FoldFClampFeedingCompare(SpvOpFOrdGreaterThan));
  1051. rules_[SpvOpFUnordGreaterThan].push_back(FoldFUnordGreaterThan());
  1052. rules_[SpvOpFUnordGreaterThan].push_back(
  1053. FoldFClampFeedingCompare(SpvOpFUnordGreaterThan));
  1054. rules_[SpvOpFOrdLessThanEqual].push_back(FoldFOrdLessThanEqual());
  1055. rules_[SpvOpFOrdLessThanEqual].push_back(
  1056. FoldFClampFeedingCompare(SpvOpFOrdLessThanEqual));
  1057. rules_[SpvOpFUnordLessThanEqual].push_back(FoldFUnordLessThanEqual());
  1058. rules_[SpvOpFUnordLessThanEqual].push_back(
  1059. FoldFClampFeedingCompare(SpvOpFUnordLessThanEqual));
  1060. rules_[SpvOpFOrdGreaterThanEqual].push_back(FoldFOrdGreaterThanEqual());
  1061. rules_[SpvOpFOrdGreaterThanEqual].push_back(
  1062. FoldFClampFeedingCompare(SpvOpFOrdGreaterThanEqual));
  1063. rules_[SpvOpFUnordGreaterThanEqual].push_back(FoldFUnordGreaterThanEqual());
  1064. rules_[SpvOpFUnordGreaterThanEqual].push_back(
  1065. FoldFClampFeedingCompare(SpvOpFUnordGreaterThanEqual));
  1066. rules_[SpvOpVectorShuffle].push_back(FoldVectorShuffleWithConstants());
  1067. rules_[SpvOpVectorTimesScalar].push_back(FoldVectorTimesScalar());
  1068. rules_[SpvOpFNegate].push_back(FoldFNegate());
  1069. rules_[SpvOpQuantizeToF16].push_back(FoldQuantizeToF16());
  1070. // Add rules for GLSLstd450
  1071. FeatureManager* feature_manager = context_->get_feature_mgr();
  1072. uint32_t ext_inst_glslstd450_id =
  1073. feature_manager->GetExtInstImportId_GLSLstd450();
  1074. if (ext_inst_glslstd450_id != 0) {
  1075. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FMix}].push_back(FoldFMix());
  1076. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SMin}].push_back(
  1077. FoldFPBinaryOp(FoldMin));
  1078. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UMin}].push_back(
  1079. FoldFPBinaryOp(FoldMin));
  1080. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FMin}].push_back(
  1081. FoldFPBinaryOp(FoldMin));
  1082. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SMax}].push_back(
  1083. FoldFPBinaryOp(FoldMax));
  1084. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UMax}].push_back(
  1085. FoldFPBinaryOp(FoldMax));
  1086. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FMax}].push_back(
  1087. FoldFPBinaryOp(FoldMax));
  1088. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UClamp}].push_back(
  1089. FoldClamp1);
  1090. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UClamp}].push_back(
  1091. FoldClamp2);
  1092. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UClamp}].push_back(
  1093. FoldClamp3);
  1094. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SClamp}].push_back(
  1095. FoldClamp1);
  1096. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SClamp}].push_back(
  1097. FoldClamp2);
  1098. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SClamp}].push_back(
  1099. FoldClamp3);
  1100. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FClamp}].push_back(
  1101. FoldClamp1);
  1102. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FClamp}].push_back(
  1103. FoldClamp2);
  1104. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FClamp}].push_back(
  1105. FoldClamp3);
  1106. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Sin}].push_back(
  1107. FoldFPUnaryOp(FoldFTranscendentalUnary(std::sin)));
  1108. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Cos}].push_back(
  1109. FoldFPUnaryOp(FoldFTranscendentalUnary(std::cos)));
  1110. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Tan}].push_back(
  1111. FoldFPUnaryOp(FoldFTranscendentalUnary(std::tan)));
  1112. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Asin}].push_back(
  1113. FoldFPUnaryOp(FoldFTranscendentalUnary(std::asin)));
  1114. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Acos}].push_back(
  1115. FoldFPUnaryOp(FoldFTranscendentalUnary(std::acos)));
  1116. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Atan}].push_back(
  1117. FoldFPUnaryOp(FoldFTranscendentalUnary(std::atan)));
  1118. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Exp}].push_back(
  1119. FoldFPUnaryOp(FoldFTranscendentalUnary(std::exp)));
  1120. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Log}].push_back(
  1121. FoldFPUnaryOp(FoldFTranscendentalUnary(std::log)));
  1122. #ifdef __ANDROID__
  1123. // Android NDK r15c tageting ABI 15 doesn't have full support for C++11
  1124. // (no std::exp2/log2). ::exp2 is available from C99 but ::log2 isn't
  1125. // available up until ABI 18 so we use a shim
  1126. auto log2_shim = [](double v) -> double { return log(v) / log(2.0); };
  1127. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Exp2}].push_back(
  1128. FoldFPUnaryOp(FoldFTranscendentalUnary(::exp2)));
  1129. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Log2}].push_back(
  1130. FoldFPUnaryOp(FoldFTranscendentalUnary(log2_shim)));
  1131. #else
  1132. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Exp2}].push_back(
  1133. FoldFPUnaryOp(FoldFTranscendentalUnary(std::exp2)));
  1134. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Log2}].push_back(
  1135. FoldFPUnaryOp(FoldFTranscendentalUnary(std::log2)));
  1136. #endif
  1137. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Sqrt}].push_back(
  1138. FoldFPUnaryOp(FoldFTranscendentalUnary(std::sqrt)));
  1139. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Atan2}].push_back(
  1140. FoldFPBinaryOp(FoldFTranscendentalBinary(std::atan2)));
  1141. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Pow}].push_back(
  1142. FoldFPBinaryOp(FoldFTranscendentalBinary(std::pow)));
  1143. }
  1144. }
  1145. } // namespace opt
  1146. } // namespace spvtools