const_folding_rules.cpp 53 KB

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