const_folding_rules.cpp 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910
  1. // Copyright (c) 2018 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "source/opt/const_folding_rules.h"
  15. #include "source/opt/ir_context.h"
  16. namespace spvtools {
  17. namespace opt {
  18. namespace {
  19. constexpr uint32_t kExtractCompositeIdInIdx = 0;
  20. // Returns a constants with the value NaN of the given type. Only works for
  21. // 32-bit and 64-bit float point types. Returns |nullptr| if an error occurs.
  22. const analysis::Constant* GetNan(const analysis::Type* type,
  23. analysis::ConstantManager* const_mgr) {
  24. const analysis::Float* float_type = type->AsFloat();
  25. if (float_type == nullptr) {
  26. return nullptr;
  27. }
  28. switch (float_type->width()) {
  29. case 32:
  30. return const_mgr->GetFloatConst(std::numeric_limits<float>::quiet_NaN());
  31. case 64:
  32. return const_mgr->GetDoubleConst(
  33. std::numeric_limits<double>::quiet_NaN());
  34. default:
  35. return nullptr;
  36. }
  37. }
  38. // Returns a constants with the value INF of the given type. Only works for
  39. // 32-bit and 64-bit float point types. Returns |nullptr| if an error occurs.
  40. const analysis::Constant* GetInf(const analysis::Type* type,
  41. analysis::ConstantManager* const_mgr) {
  42. const analysis::Float* float_type = type->AsFloat();
  43. if (float_type == nullptr) {
  44. return nullptr;
  45. }
  46. switch (float_type->width()) {
  47. case 32:
  48. return const_mgr->GetFloatConst(std::numeric_limits<float>::infinity());
  49. case 64:
  50. return const_mgr->GetDoubleConst(std::numeric_limits<double>::infinity());
  51. default:
  52. return nullptr;
  53. }
  54. }
  55. // Returns true if |type| is Float or a vector of Float.
  56. bool HasFloatingPoint(const analysis::Type* type) {
  57. if (type->AsFloat()) {
  58. return true;
  59. } else if (const analysis::Vector* vec_type = type->AsVector()) {
  60. return vec_type->element_type()->AsFloat() != nullptr;
  61. }
  62. return false;
  63. }
  64. // Returns a constants with the value |-val| of the given type. Only works for
  65. // 32-bit and 64-bit float point types. Returns |nullptr| if an error occurs.
  66. const analysis::Constant* NegateFPConst(const analysis::Type* result_type,
  67. const analysis::Constant* val,
  68. analysis::ConstantManager* const_mgr) {
  69. const analysis::Float* float_type = result_type->AsFloat();
  70. assert(float_type != nullptr);
  71. if (float_type->width() == 32) {
  72. float fa = val->GetFloat();
  73. return const_mgr->GetFloatConst(-fa);
  74. } else if (float_type->width() == 64) {
  75. double da = val->GetDouble();
  76. return const_mgr->GetDoubleConst(-da);
  77. }
  78. return nullptr;
  79. }
  80. // Returns a constants with the value |-val| of the given type.
  81. const analysis::Constant* NegateIntConst(const analysis::Type* result_type,
  82. const analysis::Constant* val,
  83. analysis::ConstantManager* const_mgr) {
  84. const analysis::Integer* int_type = result_type->AsInteger();
  85. assert(int_type != nullptr);
  86. if (val->AsNullConstant()) {
  87. return val;
  88. }
  89. uint64_t new_value = static_cast<uint64_t>(-val->GetSignExtendedValue());
  90. return const_mgr->GetIntConst(new_value, int_type->width(),
  91. int_type->IsSigned());
  92. }
  93. // Folds an OpcompositeExtract where input is a composite constant.
  94. ConstantFoldingRule FoldExtractWithConstants() {
  95. return [](IRContext* context, Instruction* inst,
  96. const std::vector<const analysis::Constant*>& constants)
  97. -> const analysis::Constant* {
  98. const analysis::Constant* c = constants[kExtractCompositeIdInIdx];
  99. if (c == nullptr) {
  100. return nullptr;
  101. }
  102. for (uint32_t i = 1; i < inst->NumInOperands(); ++i) {
  103. uint32_t element_index = inst->GetSingleWordInOperand(i);
  104. if (c->AsNullConstant()) {
  105. // Return Null for the return type.
  106. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  107. analysis::TypeManager* type_mgr = context->get_type_mgr();
  108. return const_mgr->GetConstant(type_mgr->GetType(inst->type_id()), {});
  109. }
  110. auto cc = c->AsCompositeConstant();
  111. assert(cc != nullptr);
  112. auto components = cc->GetComponents();
  113. // Protect against invalid IR. Refuse to fold if the index is out
  114. // of bounds.
  115. if (element_index >= components.size()) return nullptr;
  116. c = components[element_index];
  117. }
  118. return c;
  119. };
  120. }
  121. // Folds an OpcompositeInsert where input is a composite constant.
  122. ConstantFoldingRule FoldInsertWithConstants() {
  123. return [](IRContext* context, Instruction* inst,
  124. const std::vector<const analysis::Constant*>& constants)
  125. -> const analysis::Constant* {
  126. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  127. const analysis::Constant* object = constants[0];
  128. const analysis::Constant* composite = constants[1];
  129. if (object == nullptr || composite == nullptr) {
  130. return nullptr;
  131. }
  132. // If there is more than 1 index, then each additional constant used by the
  133. // index will need to be recreated to use the inserted object.
  134. std::vector<const analysis::Constant*> chain;
  135. std::vector<const analysis::Constant*> components;
  136. const analysis::Type* type = nullptr;
  137. const uint32_t final_index = (inst->NumInOperands() - 1);
  138. // Work down hierarchy of all indexes
  139. for (uint32_t i = 2; i < inst->NumInOperands(); ++i) {
  140. type = composite->type();
  141. if (composite->AsNullConstant()) {
  142. // Make new composite so it can be inserted in the index with the
  143. // non-null value
  144. if (const auto new_composite =
  145. const_mgr->GetNullCompositeConstant(type)) {
  146. // Keep track of any indexes along the way to last index
  147. if (i != final_index) {
  148. chain.push_back(new_composite);
  149. }
  150. components = new_composite->AsCompositeConstant()->GetComponents();
  151. } else {
  152. // Unsupported input type (such as structs)
  153. return nullptr;
  154. }
  155. } else {
  156. // Keep track of any indexes along the way to last index
  157. if (i != final_index) {
  158. chain.push_back(composite);
  159. }
  160. components = composite->AsCompositeConstant()->GetComponents();
  161. }
  162. const uint32_t index = inst->GetSingleWordInOperand(i);
  163. composite = components[index];
  164. }
  165. // Final index in hierarchy is inserted with new object.
  166. const uint32_t final_operand = inst->GetSingleWordInOperand(final_index);
  167. std::vector<uint32_t> ids;
  168. for (size_t i = 0; i < components.size(); i++) {
  169. const analysis::Constant* constant =
  170. (i == final_operand) ? object : components[i];
  171. Instruction* member_inst = const_mgr->GetDefiningInstruction(constant);
  172. ids.push_back(member_inst->result_id());
  173. }
  174. const analysis::Constant* new_constant = const_mgr->GetConstant(type, ids);
  175. // Work backwards up the chain and replace each index with new constant.
  176. for (size_t i = chain.size(); i > 0; i--) {
  177. // Need to insert any previous instruction into the module first.
  178. // Can't just insert in types_values_begin() because it will move above
  179. // where the types are declared.
  180. // Can't compare with location of inst because not all new added
  181. // instructions are added to types_values_
  182. auto iter = context->types_values_end();
  183. Module::inst_iterator* pos = &iter;
  184. const_mgr->BuildInstructionAndAddToModule(new_constant, pos);
  185. composite = chain[i - 1];
  186. components = composite->AsCompositeConstant()->GetComponents();
  187. type = composite->type();
  188. ids.clear();
  189. for (size_t k = 0; k < components.size(); k++) {
  190. const uint32_t index =
  191. inst->GetSingleWordInOperand(1 + static_cast<uint32_t>(i));
  192. const analysis::Constant* constant =
  193. (k == index) ? new_constant : components[k];
  194. const uint32_t constant_id =
  195. const_mgr->FindDeclaredConstant(constant, 0);
  196. ids.push_back(constant_id);
  197. }
  198. new_constant = const_mgr->GetConstant(type, ids);
  199. }
  200. // If multiple constants were created, only need to return the top index.
  201. return new_constant;
  202. };
  203. }
  204. ConstantFoldingRule FoldVectorShuffleWithConstants() {
  205. return [](IRContext* context, Instruction* inst,
  206. const std::vector<const analysis::Constant*>& constants)
  207. -> const analysis::Constant* {
  208. assert(inst->opcode() == spv::Op::OpVectorShuffle);
  209. const analysis::Constant* c1 = constants[0];
  210. const analysis::Constant* c2 = constants[1];
  211. if (c1 == nullptr || c2 == nullptr) {
  212. return nullptr;
  213. }
  214. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  215. const analysis::Type* element_type = c1->type()->AsVector()->element_type();
  216. std::vector<const analysis::Constant*> c1_components;
  217. if (const analysis::VectorConstant* vec_const = c1->AsVectorConstant()) {
  218. c1_components = vec_const->GetComponents();
  219. } else {
  220. assert(c1->AsNullConstant());
  221. const analysis::Constant* element =
  222. const_mgr->GetConstant(element_type, {});
  223. c1_components.resize(c1->type()->AsVector()->element_count(), element);
  224. }
  225. std::vector<const analysis::Constant*> c2_components;
  226. if (const analysis::VectorConstant* vec_const = c2->AsVectorConstant()) {
  227. c2_components = vec_const->GetComponents();
  228. } else {
  229. assert(c2->AsNullConstant());
  230. const analysis::Constant* element =
  231. const_mgr->GetConstant(element_type, {});
  232. c2_components.resize(c2->type()->AsVector()->element_count(), element);
  233. }
  234. std::vector<uint32_t> ids;
  235. const uint32_t undef_literal_value = 0xffffffff;
  236. for (uint32_t i = 2; i < inst->NumInOperands(); ++i) {
  237. uint32_t index = inst->GetSingleWordInOperand(i);
  238. if (index == undef_literal_value) {
  239. // Don't fold shuffle with undef literal value.
  240. return nullptr;
  241. } else if (index < c1_components.size()) {
  242. Instruction* member_inst =
  243. const_mgr->GetDefiningInstruction(c1_components[index]);
  244. ids.push_back(member_inst->result_id());
  245. } else {
  246. Instruction* member_inst = const_mgr->GetDefiningInstruction(
  247. c2_components[index - c1_components.size()]);
  248. ids.push_back(member_inst->result_id());
  249. }
  250. }
  251. analysis::TypeManager* type_mgr = context->get_type_mgr();
  252. return const_mgr->GetConstant(type_mgr->GetType(inst->type_id()), ids);
  253. };
  254. }
  255. ConstantFoldingRule FoldVectorTimesScalar() {
  256. return [](IRContext* context, Instruction* inst,
  257. const std::vector<const analysis::Constant*>& constants)
  258. -> const analysis::Constant* {
  259. assert(inst->opcode() == spv::Op::OpVectorTimesScalar);
  260. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  261. analysis::TypeManager* type_mgr = context->get_type_mgr();
  262. if (!inst->IsFloatingPointFoldingAllowed()) {
  263. if (HasFloatingPoint(type_mgr->GetType(inst->type_id()))) {
  264. return nullptr;
  265. }
  266. }
  267. const analysis::Constant* c1 = constants[0];
  268. const analysis::Constant* c2 = constants[1];
  269. if (c1 && c1->IsZero()) {
  270. return c1;
  271. }
  272. if (c2 && c2->IsZero()) {
  273. // Get or create the NullConstant for this type.
  274. std::vector<uint32_t> ids;
  275. return const_mgr->GetConstant(type_mgr->GetType(inst->type_id()), ids);
  276. }
  277. if (c1 == nullptr || c2 == nullptr) {
  278. return nullptr;
  279. }
  280. // Check result type.
  281. const analysis::Type* result_type = type_mgr->GetType(inst->type_id());
  282. const analysis::Vector* vector_type = result_type->AsVector();
  283. assert(vector_type != nullptr);
  284. const analysis::Type* element_type = vector_type->element_type();
  285. assert(element_type != nullptr);
  286. const analysis::Float* float_type = element_type->AsFloat();
  287. assert(float_type != nullptr);
  288. // Check types of c1 and c2.
  289. assert(c1->type()->AsVector() == vector_type);
  290. assert(c1->type()->AsVector()->element_type() == element_type &&
  291. c2->type() == element_type);
  292. // Get a float vector that is the result of vector-times-scalar.
  293. std::vector<const analysis::Constant*> c1_components =
  294. c1->GetVectorComponents(const_mgr);
  295. std::vector<uint32_t> ids;
  296. if (float_type->width() == 32) {
  297. float scalar = c2->GetFloat();
  298. for (uint32_t i = 0; i < c1_components.size(); ++i) {
  299. utils::FloatProxy<float> result(c1_components[i]->GetFloat() * scalar);
  300. std::vector<uint32_t> words = result.GetWords();
  301. const analysis::Constant* new_elem =
  302. const_mgr->GetConstant(float_type, words);
  303. ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
  304. }
  305. return const_mgr->GetConstant(vector_type, ids);
  306. } else if (float_type->width() == 64) {
  307. double scalar = c2->GetDouble();
  308. for (uint32_t i = 0; i < c1_components.size(); ++i) {
  309. utils::FloatProxy<double> result(c1_components[i]->GetDouble() *
  310. scalar);
  311. std::vector<uint32_t> words = result.GetWords();
  312. const analysis::Constant* new_elem =
  313. const_mgr->GetConstant(float_type, words);
  314. ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
  315. }
  316. return const_mgr->GetConstant(vector_type, ids);
  317. }
  318. return nullptr;
  319. };
  320. }
  321. // Returns to the constant that results from tranposing |matrix|. The result
  322. // will have type |result_type|, and |matrix| must exist in |context|. The
  323. // result constant will also exist in |context|.
  324. const analysis::Constant* TransposeMatrix(const analysis::Constant* matrix,
  325. analysis::Matrix* result_type,
  326. IRContext* context) {
  327. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  328. if (matrix->AsNullConstant() != nullptr) {
  329. return const_mgr->GetNullCompositeConstant(result_type);
  330. }
  331. const auto& columns = matrix->AsMatrixConstant()->GetComponents();
  332. uint32_t number_of_rows = columns[0]->type()->AsVector()->element_count();
  333. // Collect the ids of the elements in their new positions.
  334. std::vector<std::vector<uint32_t>> result_elements(number_of_rows);
  335. for (const analysis::Constant* column : columns) {
  336. if (column->AsNullConstant()) {
  337. column = const_mgr->GetNullCompositeConstant(column->type());
  338. }
  339. const auto& column_components = column->AsVectorConstant()->GetComponents();
  340. for (uint32_t row = 0; row < number_of_rows; ++row) {
  341. result_elements[row].push_back(
  342. const_mgr->GetDefiningInstruction(column_components[row])
  343. ->result_id());
  344. }
  345. }
  346. // Create the constant for each row in the result, and collect the ids.
  347. std::vector<uint32_t> result_columns(number_of_rows);
  348. for (uint32_t col = 0; col < number_of_rows; ++col) {
  349. auto* element = const_mgr->GetConstant(result_type->element_type(),
  350. result_elements[col]);
  351. result_columns[col] =
  352. const_mgr->GetDefiningInstruction(element)->result_id();
  353. }
  354. // Create the matrix constant from the row ids, and return it.
  355. return const_mgr->GetConstant(result_type, result_columns);
  356. }
  357. const analysis::Constant* FoldTranspose(
  358. IRContext* context, Instruction* inst,
  359. const std::vector<const analysis::Constant*>& constants) {
  360. assert(inst->opcode() == spv::Op::OpTranspose);
  361. analysis::TypeManager* type_mgr = context->get_type_mgr();
  362. if (!inst->IsFloatingPointFoldingAllowed()) {
  363. if (HasFloatingPoint(type_mgr->GetType(inst->type_id()))) {
  364. return nullptr;
  365. }
  366. }
  367. const analysis::Constant* matrix = constants[0];
  368. if (matrix == nullptr) {
  369. return nullptr;
  370. }
  371. auto* result_type = type_mgr->GetType(inst->type_id());
  372. return TransposeMatrix(matrix, result_type->AsMatrix(), context);
  373. }
  374. ConstantFoldingRule FoldVectorTimesMatrix() {
  375. return [](IRContext* context, Instruction* inst,
  376. const std::vector<const analysis::Constant*>& constants)
  377. -> const analysis::Constant* {
  378. assert(inst->opcode() == spv::Op::OpVectorTimesMatrix);
  379. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  380. analysis::TypeManager* type_mgr = context->get_type_mgr();
  381. if (!inst->IsFloatingPointFoldingAllowed()) {
  382. if (HasFloatingPoint(type_mgr->GetType(inst->type_id()))) {
  383. return nullptr;
  384. }
  385. }
  386. const analysis::Constant* c1 = constants[0];
  387. const analysis::Constant* c2 = constants[1];
  388. if (c1 == nullptr || c2 == nullptr) {
  389. return nullptr;
  390. }
  391. // Check result type.
  392. const analysis::Type* result_type = type_mgr->GetType(inst->type_id());
  393. const analysis::Vector* vector_type = result_type->AsVector();
  394. assert(vector_type != nullptr);
  395. const analysis::Type* element_type = vector_type->element_type();
  396. assert(element_type != nullptr);
  397. const analysis::Float* float_type = element_type->AsFloat();
  398. assert(float_type != nullptr);
  399. // Check types of c1 and c2.
  400. assert(c1->type()->AsVector() == vector_type);
  401. assert(c1->type()->AsVector()->element_type() == element_type &&
  402. c2->type()->AsMatrix()->element_type() == vector_type);
  403. uint32_t resultVectorSize = result_type->AsVector()->element_count();
  404. std::vector<uint32_t> ids;
  405. if ((c1 && c1->IsZero()) || (c2 && c2->IsZero())) {
  406. std::vector<uint32_t> words(float_type->width() / 32, 0);
  407. for (uint32_t i = 0; i < resultVectorSize; ++i) {
  408. const analysis::Constant* new_elem =
  409. const_mgr->GetConstant(float_type, words);
  410. ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
  411. }
  412. return const_mgr->GetConstant(vector_type, ids);
  413. }
  414. // Get a float vector that is the result of vector-times-matrix.
  415. std::vector<const analysis::Constant*> c1_components =
  416. c1->GetVectorComponents(const_mgr);
  417. std::vector<const analysis::Constant*> c2_components =
  418. c2->AsMatrixConstant()->GetComponents();
  419. if (float_type->width() == 32) {
  420. for (uint32_t i = 0; i < resultVectorSize; ++i) {
  421. float result_scalar = 0.0f;
  422. if (!c2_components[i]->AsNullConstant()) {
  423. const analysis::VectorConstant* c2_vec =
  424. c2_components[i]->AsVectorConstant();
  425. for (uint32_t j = 0; j < c2_vec->GetComponents().size(); ++j) {
  426. float c1_scalar = c1_components[j]->GetFloat();
  427. float c2_scalar = c2_vec->GetComponents()[j]->GetFloat();
  428. result_scalar += c1_scalar * c2_scalar;
  429. }
  430. }
  431. utils::FloatProxy<float> result(result_scalar);
  432. std::vector<uint32_t> words = result.GetWords();
  433. const analysis::Constant* new_elem =
  434. const_mgr->GetConstant(float_type, words);
  435. ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
  436. }
  437. return const_mgr->GetConstant(vector_type, ids);
  438. } else if (float_type->width() == 64) {
  439. for (uint32_t i = 0; i < c2_components.size(); ++i) {
  440. double result_scalar = 0.0;
  441. if (!c2_components[i]->AsNullConstant()) {
  442. const analysis::VectorConstant* c2_vec =
  443. c2_components[i]->AsVectorConstant();
  444. for (uint32_t j = 0; j < c2_vec->GetComponents().size(); ++j) {
  445. double c1_scalar = c1_components[j]->GetDouble();
  446. double c2_scalar = c2_vec->GetComponents()[j]->GetDouble();
  447. result_scalar += c1_scalar * c2_scalar;
  448. }
  449. }
  450. utils::FloatProxy<double> result(result_scalar);
  451. std::vector<uint32_t> words = result.GetWords();
  452. const analysis::Constant* new_elem =
  453. const_mgr->GetConstant(float_type, words);
  454. ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
  455. }
  456. return const_mgr->GetConstant(vector_type, ids);
  457. }
  458. return nullptr;
  459. };
  460. }
  461. ConstantFoldingRule FoldMatrixTimesVector() {
  462. return [](IRContext* context, Instruction* inst,
  463. const std::vector<const analysis::Constant*>& constants)
  464. -> const analysis::Constant* {
  465. assert(inst->opcode() == spv::Op::OpMatrixTimesVector);
  466. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  467. analysis::TypeManager* type_mgr = context->get_type_mgr();
  468. if (!inst->IsFloatingPointFoldingAllowed()) {
  469. if (HasFloatingPoint(type_mgr->GetType(inst->type_id()))) {
  470. return nullptr;
  471. }
  472. }
  473. const analysis::Constant* c1 = constants[0];
  474. const analysis::Constant* c2 = constants[1];
  475. if (c1 == nullptr || c2 == nullptr) {
  476. return nullptr;
  477. }
  478. // Check result type.
  479. const analysis::Type* result_type = type_mgr->GetType(inst->type_id());
  480. const analysis::Vector* vector_type = result_type->AsVector();
  481. assert(vector_type != nullptr);
  482. const analysis::Type* element_type = vector_type->element_type();
  483. assert(element_type != nullptr);
  484. const analysis::Float* float_type = element_type->AsFloat();
  485. assert(float_type != nullptr);
  486. // Check types of c1 and c2.
  487. assert(c1->type()->AsMatrix()->element_type() == vector_type);
  488. assert(c2->type()->AsVector()->element_type() == element_type);
  489. uint32_t resultVectorSize = result_type->AsVector()->element_count();
  490. std::vector<uint32_t> ids;
  491. if ((c1 && c1->IsZero()) || (c2 && c2->IsZero())) {
  492. std::vector<uint32_t> words(float_type->width() / 32, 0);
  493. for (uint32_t i = 0; i < resultVectorSize; ++i) {
  494. const analysis::Constant* new_elem =
  495. const_mgr->GetConstant(float_type, words);
  496. ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
  497. }
  498. return const_mgr->GetConstant(vector_type, ids);
  499. }
  500. // Get a float vector that is the result of matrix-times-vector.
  501. std::vector<const analysis::Constant*> c1_components =
  502. c1->AsMatrixConstant()->GetComponents();
  503. std::vector<const analysis::Constant*> c2_components =
  504. c2->GetVectorComponents(const_mgr);
  505. if (float_type->width() == 32) {
  506. for (uint32_t i = 0; i < resultVectorSize; ++i) {
  507. float result_scalar = 0.0f;
  508. for (uint32_t j = 0; j < c1_components.size(); ++j) {
  509. if (!c1_components[j]->AsNullConstant()) {
  510. float c1_scalar = c1_components[j]
  511. ->AsVectorConstant()
  512. ->GetComponents()[i]
  513. ->GetFloat();
  514. float c2_scalar = c2_components[j]->GetFloat();
  515. result_scalar += c1_scalar * c2_scalar;
  516. }
  517. }
  518. utils::FloatProxy<float> result(result_scalar);
  519. std::vector<uint32_t> words = result.GetWords();
  520. const analysis::Constant* new_elem =
  521. const_mgr->GetConstant(float_type, words);
  522. ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
  523. }
  524. return const_mgr->GetConstant(vector_type, ids);
  525. } else if (float_type->width() == 64) {
  526. for (uint32_t i = 0; i < resultVectorSize; ++i) {
  527. double result_scalar = 0.0;
  528. for (uint32_t j = 0; j < c1_components.size(); ++j) {
  529. if (!c1_components[j]->AsNullConstant()) {
  530. double c1_scalar = c1_components[j]
  531. ->AsVectorConstant()
  532. ->GetComponents()[i]
  533. ->GetDouble();
  534. double c2_scalar = c2_components[j]->GetDouble();
  535. result_scalar += c1_scalar * c2_scalar;
  536. }
  537. }
  538. utils::FloatProxy<double> result(result_scalar);
  539. std::vector<uint32_t> words = result.GetWords();
  540. const analysis::Constant* new_elem =
  541. const_mgr->GetConstant(float_type, words);
  542. ids.push_back(const_mgr->GetDefiningInstruction(new_elem)->result_id());
  543. }
  544. return const_mgr->GetConstant(vector_type, ids);
  545. }
  546. return nullptr;
  547. };
  548. }
  549. ConstantFoldingRule FoldCompositeWithConstants() {
  550. // Folds an OpCompositeConstruct where all of the inputs are constants to a
  551. // constant. A new constant is created if necessary.
  552. return [](IRContext* context, Instruction* inst,
  553. const std::vector<const analysis::Constant*>& constants)
  554. -> const analysis::Constant* {
  555. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  556. analysis::TypeManager* type_mgr = context->get_type_mgr();
  557. const analysis::Type* new_type = type_mgr->GetType(inst->type_id());
  558. Instruction* type_inst =
  559. context->get_def_use_mgr()->GetDef(inst->type_id());
  560. std::vector<uint32_t> ids;
  561. for (uint32_t i = 0; i < constants.size(); ++i) {
  562. const analysis::Constant* element_const = constants[i];
  563. if (element_const == nullptr) {
  564. return nullptr;
  565. }
  566. uint32_t component_type_id = 0;
  567. if (type_inst->opcode() == spv::Op::OpTypeStruct) {
  568. component_type_id = type_inst->GetSingleWordInOperand(i);
  569. } else if (type_inst->opcode() == spv::Op::OpTypeArray) {
  570. component_type_id = type_inst->GetSingleWordInOperand(0);
  571. }
  572. uint32_t element_id =
  573. const_mgr->FindDeclaredConstant(element_const, component_type_id);
  574. if (element_id == 0) {
  575. return nullptr;
  576. }
  577. ids.push_back(element_id);
  578. }
  579. return const_mgr->GetConstant(new_type, ids);
  580. };
  581. }
  582. // The interface for a function that returns the result of applying a scalar
  583. // floating-point binary operation on |a| and |b|. The type of the return value
  584. // will be |type|. The input constants must also be of type |type|.
  585. using UnaryScalarFoldingRule = std::function<const analysis::Constant*(
  586. const analysis::Type* result_type, const analysis::Constant* a,
  587. analysis::ConstantManager*)>;
  588. // The interface for a function that returns the result of applying a scalar
  589. // floating-point binary operation on |a| and |b|. The type of the return value
  590. // will be |type|. The input constants must also be of type |type|.
  591. using BinaryScalarFoldingRule = std::function<const analysis::Constant*(
  592. const analysis::Type* result_type, const analysis::Constant* a,
  593. const analysis::Constant* b, analysis::ConstantManager*)>;
  594. // Returns a |ConstantFoldingRule| that folds unary scalar ops
  595. // using |scalar_rule| and unary vectors ops by applying
  596. // |scalar_rule| to the elements of the vector. The |ConstantFoldingRule|
  597. // that is returned assumes that |constants| contains 1 entry. If they are
  598. // not |nullptr|, then their type is either |Float| or |Integer| or a |Vector|
  599. // whose element type is |Float| or |Integer|.
  600. ConstantFoldingRule FoldUnaryOp(UnaryScalarFoldingRule scalar_rule) {
  601. return [scalar_rule](IRContext* context, Instruction* inst,
  602. const std::vector<const analysis::Constant*>& constants)
  603. -> const analysis::Constant* {
  604. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  605. analysis::TypeManager* type_mgr = context->get_type_mgr();
  606. const analysis::Type* result_type = type_mgr->GetType(inst->type_id());
  607. const analysis::Vector* vector_type = result_type->AsVector();
  608. const analysis::Constant* arg =
  609. (inst->opcode() == spv::Op::OpExtInst) ? constants[1] : constants[0];
  610. if (arg == nullptr) {
  611. return nullptr;
  612. }
  613. if (vector_type != nullptr) {
  614. std::vector<const analysis::Constant*> a_components;
  615. std::vector<const analysis::Constant*> results_components;
  616. a_components = arg->GetVectorComponents(const_mgr);
  617. // Fold each component of the vector.
  618. for (uint32_t i = 0; i < a_components.size(); ++i) {
  619. results_components.push_back(scalar_rule(vector_type->element_type(),
  620. a_components[i], const_mgr));
  621. if (results_components[i] == nullptr) {
  622. return nullptr;
  623. }
  624. }
  625. // Build the constant object and return it.
  626. std::vector<uint32_t> ids;
  627. for (const analysis::Constant* member : results_components) {
  628. ids.push_back(const_mgr->GetDefiningInstruction(member)->result_id());
  629. }
  630. return const_mgr->GetConstant(vector_type, ids);
  631. } else {
  632. return scalar_rule(result_type, arg, const_mgr);
  633. }
  634. };
  635. }
  636. // Returns a |ConstantFoldingRule| that folds binary scalar ops
  637. // using |scalar_rule| and binary vectors ops by applying
  638. // |scalar_rule| to the elements of the vector. The folding rule assumes that op
  639. // has two inputs. For regular instruction, those are in operands 0 and 1. For
  640. // extended instruction, they are in operands 1 and 2. If an element in
  641. // |constants| is not nullprt, then the constant's type is |Float|, |Integer|,
  642. // or |Vector| whose element type is |Float| or |Integer|.
  643. ConstantFoldingRule FoldBinaryOp(BinaryScalarFoldingRule scalar_rule) {
  644. return [scalar_rule](IRContext* context, Instruction* inst,
  645. const std::vector<const analysis::Constant*>& constants)
  646. -> const analysis::Constant* {
  647. assert(constants.size() == inst->NumInOperands());
  648. assert(constants.size() == (inst->opcode() == spv::Op::OpExtInst ? 3 : 2));
  649. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  650. analysis::TypeManager* type_mgr = context->get_type_mgr();
  651. const analysis::Type* result_type = type_mgr->GetType(inst->type_id());
  652. const analysis::Vector* vector_type = result_type->AsVector();
  653. const analysis::Constant* arg1 =
  654. (inst->opcode() == spv::Op::OpExtInst) ? constants[1] : constants[0];
  655. const analysis::Constant* arg2 =
  656. (inst->opcode() == spv::Op::OpExtInst) ? constants[2] : constants[1];
  657. if (arg1 == nullptr || arg2 == nullptr) {
  658. return nullptr;
  659. }
  660. if (vector_type == nullptr) {
  661. return scalar_rule(result_type, arg1, arg2, const_mgr);
  662. }
  663. std::vector<const analysis::Constant*> a_components;
  664. std::vector<const analysis::Constant*> b_components;
  665. std::vector<const analysis::Constant*> results_components;
  666. a_components = arg1->GetVectorComponents(const_mgr);
  667. b_components = arg2->GetVectorComponents(const_mgr);
  668. assert(a_components.size() == b_components.size());
  669. // Fold each component of the vector.
  670. for (uint32_t i = 0; i < a_components.size(); ++i) {
  671. results_components.push_back(scalar_rule(vector_type->element_type(),
  672. a_components[i], b_components[i],
  673. const_mgr));
  674. if (results_components[i] == nullptr) {
  675. return nullptr;
  676. }
  677. }
  678. // Build the constant object and return it.
  679. std::vector<uint32_t> ids;
  680. for (const analysis::Constant* member : results_components) {
  681. ids.push_back(const_mgr->GetDefiningInstruction(member)->result_id());
  682. }
  683. return const_mgr->GetConstant(vector_type, ids);
  684. };
  685. }
  686. // Returns a |ConstantFoldingRule| that folds unary floating point scalar ops
  687. // using |scalar_rule| and unary float point vectors ops by applying
  688. // |scalar_rule| to the elements of the vector. The |ConstantFoldingRule|
  689. // that is returned assumes that |constants| contains 1 entry. If they are
  690. // not |nullptr|, then their type is either |Float| or |Integer| or a |Vector|
  691. // whose element type is |Float| or |Integer|.
  692. ConstantFoldingRule FoldFPUnaryOp(UnaryScalarFoldingRule scalar_rule) {
  693. auto folding_rule = FoldUnaryOp(scalar_rule);
  694. return [folding_rule](IRContext* context, Instruction* inst,
  695. const std::vector<const analysis::Constant*>& constants)
  696. -> const analysis::Constant* {
  697. if (!inst->IsFloatingPointFoldingAllowed()) {
  698. return nullptr;
  699. }
  700. return folding_rule(context, inst, constants);
  701. };
  702. }
  703. // Returns the result of folding the constants in |constants| according the
  704. // |scalar_rule|. If |result_type| is a vector, then |scalar_rule| is applied
  705. // per component.
  706. const analysis::Constant* FoldFPBinaryOp(
  707. BinaryScalarFoldingRule scalar_rule, uint32_t result_type_id,
  708. const std::vector<const analysis::Constant*>& constants,
  709. IRContext* context) {
  710. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  711. analysis::TypeManager* type_mgr = context->get_type_mgr();
  712. const analysis::Type* result_type = type_mgr->GetType(result_type_id);
  713. const analysis::Vector* vector_type = result_type->AsVector();
  714. if (constants[0] == nullptr || constants[1] == nullptr) {
  715. return nullptr;
  716. }
  717. if (vector_type != nullptr) {
  718. std::vector<const analysis::Constant*> a_components;
  719. std::vector<const analysis::Constant*> b_components;
  720. std::vector<const analysis::Constant*> results_components;
  721. a_components = constants[0]->GetVectorComponents(const_mgr);
  722. b_components = constants[1]->GetVectorComponents(const_mgr);
  723. // Fold each component of the vector.
  724. for (uint32_t i = 0; i < a_components.size(); ++i) {
  725. results_components.push_back(scalar_rule(vector_type->element_type(),
  726. a_components[i], b_components[i],
  727. const_mgr));
  728. if (results_components[i] == nullptr) {
  729. return nullptr;
  730. }
  731. }
  732. // Build the constant object and return it.
  733. std::vector<uint32_t> ids;
  734. for (const analysis::Constant* member : results_components) {
  735. ids.push_back(const_mgr->GetDefiningInstruction(member)->result_id());
  736. }
  737. return const_mgr->GetConstant(vector_type, ids);
  738. } else {
  739. return scalar_rule(result_type, constants[0], constants[1], const_mgr);
  740. }
  741. }
  742. // Returns a |ConstantFoldingRule| that folds floating point scalars using
  743. // |scalar_rule| and vectors of floating point by applying |scalar_rule| to the
  744. // elements of the vector. The |ConstantFoldingRule| that is returned assumes
  745. // that |constants| contains 2 entries. If they are not |nullptr|, then their
  746. // type is either |Float| or a |Vector| whose element type is |Float|.
  747. ConstantFoldingRule FoldFPBinaryOp(BinaryScalarFoldingRule scalar_rule) {
  748. return [scalar_rule](IRContext* context, Instruction* inst,
  749. const std::vector<const analysis::Constant*>& constants)
  750. -> const analysis::Constant* {
  751. if (!inst->IsFloatingPointFoldingAllowed()) {
  752. return nullptr;
  753. }
  754. if (inst->opcode() == spv::Op::OpExtInst) {
  755. return FoldFPBinaryOp(scalar_rule, inst->type_id(),
  756. {constants[1], constants[2]}, context);
  757. }
  758. return FoldFPBinaryOp(scalar_rule, inst->type_id(), constants, context);
  759. };
  760. }
  761. // This macro defines a |UnaryScalarFoldingRule| that performs float to
  762. // integer conversion.
  763. // TODO(greg-lunarg): Support for 64-bit integer types.
  764. UnaryScalarFoldingRule FoldFToIOp() {
  765. return [](const analysis::Type* result_type, const analysis::Constant* a,
  766. analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
  767. assert(result_type != nullptr && a != nullptr);
  768. const analysis::Integer* integer_type = result_type->AsInteger();
  769. const analysis::Float* float_type = a->type()->AsFloat();
  770. assert(float_type != nullptr);
  771. assert(integer_type != nullptr);
  772. if (integer_type->width() != 32) return nullptr;
  773. if (float_type->width() == 32) {
  774. float fa = a->GetFloat();
  775. uint32_t result = integer_type->IsSigned()
  776. ? static_cast<uint32_t>(static_cast<int32_t>(fa))
  777. : static_cast<uint32_t>(fa);
  778. std::vector<uint32_t> words = {result};
  779. return const_mgr->GetConstant(result_type, words);
  780. } else if (float_type->width() == 64) {
  781. double fa = a->GetDouble();
  782. uint32_t result = integer_type->IsSigned()
  783. ? static_cast<uint32_t>(static_cast<int32_t>(fa))
  784. : static_cast<uint32_t>(fa);
  785. std::vector<uint32_t> words = {result};
  786. return const_mgr->GetConstant(result_type, words);
  787. }
  788. return nullptr;
  789. };
  790. }
  791. // This function defines a |UnaryScalarFoldingRule| that performs integer to
  792. // float conversion.
  793. // TODO(greg-lunarg): Support for 64-bit integer types.
  794. UnaryScalarFoldingRule FoldIToFOp() {
  795. return [](const analysis::Type* result_type, const analysis::Constant* a,
  796. analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
  797. assert(result_type != nullptr && a != nullptr);
  798. const analysis::Integer* integer_type = a->type()->AsInteger();
  799. const analysis::Float* float_type = result_type->AsFloat();
  800. assert(float_type != nullptr);
  801. assert(integer_type != nullptr);
  802. if (integer_type->width() != 32) return nullptr;
  803. uint32_t ua = a->GetU32();
  804. if (float_type->width() == 32) {
  805. float result_val = integer_type->IsSigned()
  806. ? static_cast<float>(static_cast<int32_t>(ua))
  807. : static_cast<float>(ua);
  808. utils::FloatProxy<float> result(result_val);
  809. std::vector<uint32_t> words = {result.data()};
  810. return const_mgr->GetConstant(result_type, words);
  811. } else if (float_type->width() == 64) {
  812. double result_val = integer_type->IsSigned()
  813. ? static_cast<double>(static_cast<int32_t>(ua))
  814. : static_cast<double>(ua);
  815. utils::FloatProxy<double> result(result_val);
  816. std::vector<uint32_t> words = result.GetWords();
  817. return const_mgr->GetConstant(result_type, words);
  818. }
  819. return nullptr;
  820. };
  821. }
  822. // This defines a |UnaryScalarFoldingRule| that performs |OpQuantizeToF16|.
  823. UnaryScalarFoldingRule FoldQuantizeToF16Scalar() {
  824. return [](const analysis::Type* result_type, const analysis::Constant* a,
  825. analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
  826. assert(result_type != nullptr && a != nullptr);
  827. const analysis::Float* float_type = a->type()->AsFloat();
  828. assert(float_type != nullptr);
  829. if (float_type->width() != 32) {
  830. return nullptr;
  831. }
  832. float fa = a->GetFloat();
  833. utils::HexFloat<utils::FloatProxy<float>> orignal(fa);
  834. utils::HexFloat<utils::FloatProxy<utils::Float16>> quantized(0);
  835. utils::HexFloat<utils::FloatProxy<float>> result(0.0f);
  836. orignal.castTo(quantized, utils::round_direction::kToZero);
  837. quantized.castTo(result, utils::round_direction::kToZero);
  838. std::vector<uint32_t> words = {result.getBits()};
  839. return const_mgr->GetConstant(result_type, words);
  840. };
  841. }
  842. // This macro defines a |BinaryScalarFoldingRule| that applies |op|. The
  843. // operator |op| must work for both float and double, and use syntax "f1 op f2".
  844. #define FOLD_FPARITH_OP(op) \
  845. [](const analysis::Type* result_type_in_macro, const analysis::Constant* a, \
  846. const analysis::Constant* b, \
  847. analysis::ConstantManager* const_mgr_in_macro) \
  848. -> const analysis::Constant* { \
  849. assert(result_type_in_macro != nullptr && a != nullptr && b != nullptr); \
  850. assert(result_type_in_macro == a->type() && \
  851. result_type_in_macro == b->type()); \
  852. const analysis::Float* float_type_in_macro = \
  853. result_type_in_macro->AsFloat(); \
  854. assert(float_type_in_macro != nullptr); \
  855. if (float_type_in_macro->width() == 32) { \
  856. float fa = a->GetFloat(); \
  857. float fb = b->GetFloat(); \
  858. utils::FloatProxy<float> result_in_macro(fa op fb); \
  859. std::vector<uint32_t> words_in_macro = result_in_macro.GetWords(); \
  860. return const_mgr_in_macro->GetConstant(result_type_in_macro, \
  861. words_in_macro); \
  862. } else if (float_type_in_macro->width() == 64) { \
  863. double fa = a->GetDouble(); \
  864. double fb = b->GetDouble(); \
  865. utils::FloatProxy<double> result_in_macro(fa op fb); \
  866. std::vector<uint32_t> words_in_macro = result_in_macro.GetWords(); \
  867. return const_mgr_in_macro->GetConstant(result_type_in_macro, \
  868. words_in_macro); \
  869. } \
  870. return nullptr; \
  871. }
  872. // Define the folding rule for conversion between floating point and integer
  873. ConstantFoldingRule FoldFToI() { return FoldFPUnaryOp(FoldFToIOp()); }
  874. ConstantFoldingRule FoldIToF() { return FoldFPUnaryOp(FoldIToFOp()); }
  875. ConstantFoldingRule FoldQuantizeToF16() {
  876. return FoldFPUnaryOp(FoldQuantizeToF16Scalar());
  877. }
  878. // Define the folding rules for subtraction, addition, multiplication, and
  879. // division for floating point values.
  880. ConstantFoldingRule FoldFSub() { return FoldFPBinaryOp(FOLD_FPARITH_OP(-)); }
  881. ConstantFoldingRule FoldFAdd() { return FoldFPBinaryOp(FOLD_FPARITH_OP(+)); }
  882. ConstantFoldingRule FoldFMul() { return FoldFPBinaryOp(FOLD_FPARITH_OP(*)); }
  883. // Returns the constant that results from evaluating |numerator| / 0.0. Returns
  884. // |nullptr| if the result could not be evaluated.
  885. const analysis::Constant* FoldFPScalarDivideByZero(
  886. const analysis::Type* result_type, const analysis::Constant* numerator,
  887. analysis::ConstantManager* const_mgr) {
  888. if (numerator == nullptr) {
  889. return nullptr;
  890. }
  891. if (numerator->IsZero()) {
  892. return GetNan(result_type, const_mgr);
  893. }
  894. const analysis::Constant* result = GetInf(result_type, const_mgr);
  895. if (result == nullptr) {
  896. return nullptr;
  897. }
  898. if (numerator->AsFloatConstant()->GetValueAsDouble() < 0.0) {
  899. result = NegateFPConst(result_type, result, const_mgr);
  900. }
  901. return result;
  902. }
  903. // Returns the result of folding |numerator| / |denominator|. Returns |nullptr|
  904. // if it cannot be folded.
  905. const analysis::Constant* FoldScalarFPDivide(
  906. const analysis::Type* result_type, const analysis::Constant* numerator,
  907. const analysis::Constant* denominator,
  908. analysis::ConstantManager* const_mgr) {
  909. if (denominator == nullptr) {
  910. return nullptr;
  911. }
  912. if (denominator->IsZero()) {
  913. return FoldFPScalarDivideByZero(result_type, numerator, const_mgr);
  914. }
  915. uint32_t width = denominator->type()->AsFloat()->width();
  916. if (width != 32 && width != 64) {
  917. return nullptr;
  918. }
  919. const analysis::FloatConstant* denominator_float =
  920. denominator->AsFloatConstant();
  921. if (denominator_float && denominator->GetValueAsDouble() == -0.0) {
  922. const analysis::Constant* result =
  923. FoldFPScalarDivideByZero(result_type, numerator, const_mgr);
  924. if (result != nullptr)
  925. result = NegateFPConst(result_type, result, const_mgr);
  926. return result;
  927. } else {
  928. return FOLD_FPARITH_OP(/)(result_type, numerator, denominator, const_mgr);
  929. }
  930. }
  931. // Returns the constant folding rule to fold |OpFDiv| with two constants.
  932. ConstantFoldingRule FoldFDiv() { return FoldFPBinaryOp(FoldScalarFPDivide); }
  933. bool CompareFloatingPoint(bool op_result, bool op_unordered,
  934. bool need_ordered) {
  935. if (need_ordered) {
  936. // operands are ordered and Operand 1 is |op| Operand 2
  937. return !op_unordered && op_result;
  938. } else {
  939. // operands are unordered or Operand 1 is |op| Operand 2
  940. return op_unordered || op_result;
  941. }
  942. }
  943. // This macro defines a |BinaryScalarFoldingRule| that applies |op|. The
  944. // operator |op| must work for both float and double, and use syntax "f1 op f2".
  945. #define FOLD_FPCMP_OP(op, ord) \
  946. [](const analysis::Type* result_type, const analysis::Constant* a, \
  947. const analysis::Constant* b, \
  948. analysis::ConstantManager* const_mgr) -> const analysis::Constant* { \
  949. assert(result_type != nullptr && a != nullptr && b != nullptr); \
  950. assert(result_type->AsBool()); \
  951. assert(a->type() == b->type()); \
  952. const analysis::Float* float_type = a->type()->AsFloat(); \
  953. assert(float_type != nullptr); \
  954. if (float_type->width() == 32) { \
  955. float fa = a->GetFloat(); \
  956. float fb = b->GetFloat(); \
  957. bool result = CompareFloatingPoint( \
  958. fa op fb, std::isnan(fa) || std::isnan(fb), ord); \
  959. std::vector<uint32_t> words = {uint32_t(result)}; \
  960. return const_mgr->GetConstant(result_type, words); \
  961. } else if (float_type->width() == 64) { \
  962. double fa = a->GetDouble(); \
  963. double fb = b->GetDouble(); \
  964. bool result = CompareFloatingPoint( \
  965. fa op fb, std::isnan(fa) || std::isnan(fb), ord); \
  966. std::vector<uint32_t> words = {uint32_t(result)}; \
  967. return const_mgr->GetConstant(result_type, words); \
  968. } \
  969. return nullptr; \
  970. }
  971. // Define the folding rules for ordered and unordered comparison for floating
  972. // point values.
  973. ConstantFoldingRule FoldFOrdEqual() {
  974. return FoldFPBinaryOp(FOLD_FPCMP_OP(==, true));
  975. }
  976. ConstantFoldingRule FoldFUnordEqual() {
  977. return FoldFPBinaryOp(FOLD_FPCMP_OP(==, false));
  978. }
  979. ConstantFoldingRule FoldFOrdNotEqual() {
  980. return FoldFPBinaryOp(FOLD_FPCMP_OP(!=, true));
  981. }
  982. ConstantFoldingRule FoldFUnordNotEqual() {
  983. return FoldFPBinaryOp(FOLD_FPCMP_OP(!=, false));
  984. }
  985. ConstantFoldingRule FoldFOrdLessThan() {
  986. return FoldFPBinaryOp(FOLD_FPCMP_OP(<, true));
  987. }
  988. ConstantFoldingRule FoldFUnordLessThan() {
  989. return FoldFPBinaryOp(FOLD_FPCMP_OP(<, false));
  990. }
  991. ConstantFoldingRule FoldFOrdGreaterThan() {
  992. return FoldFPBinaryOp(FOLD_FPCMP_OP(>, true));
  993. }
  994. ConstantFoldingRule FoldFUnordGreaterThan() {
  995. return FoldFPBinaryOp(FOLD_FPCMP_OP(>, false));
  996. }
  997. ConstantFoldingRule FoldFOrdLessThanEqual() {
  998. return FoldFPBinaryOp(FOLD_FPCMP_OP(<=, true));
  999. }
  1000. ConstantFoldingRule FoldFUnordLessThanEqual() {
  1001. return FoldFPBinaryOp(FOLD_FPCMP_OP(<=, false));
  1002. }
  1003. ConstantFoldingRule FoldFOrdGreaterThanEqual() {
  1004. return FoldFPBinaryOp(FOLD_FPCMP_OP(>=, true));
  1005. }
  1006. ConstantFoldingRule FoldFUnordGreaterThanEqual() {
  1007. return FoldFPBinaryOp(FOLD_FPCMP_OP(>=, false));
  1008. }
  1009. // Folds an OpDot where all of the inputs are constants to a
  1010. // constant. A new constant is created if necessary.
  1011. ConstantFoldingRule FoldOpDotWithConstants() {
  1012. return [](IRContext* context, Instruction* inst,
  1013. const std::vector<const analysis::Constant*>& constants)
  1014. -> const analysis::Constant* {
  1015. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  1016. analysis::TypeManager* type_mgr = context->get_type_mgr();
  1017. const analysis::Type* new_type = type_mgr->GetType(inst->type_id());
  1018. assert(new_type->AsFloat() && "OpDot should have a float return type.");
  1019. const analysis::Float* float_type = new_type->AsFloat();
  1020. if (!inst->IsFloatingPointFoldingAllowed()) {
  1021. return nullptr;
  1022. }
  1023. // If one of the operands is 0, then the result is 0.
  1024. bool has_zero_operand = false;
  1025. for (int i = 0; i < 2; ++i) {
  1026. if (constants[i]) {
  1027. if (constants[i]->AsNullConstant() ||
  1028. constants[i]->AsVectorConstant()->IsZero()) {
  1029. has_zero_operand = true;
  1030. break;
  1031. }
  1032. }
  1033. }
  1034. if (has_zero_operand) {
  1035. if (float_type->width() == 32) {
  1036. utils::FloatProxy<float> result(0.0f);
  1037. std::vector<uint32_t> words = result.GetWords();
  1038. return const_mgr->GetConstant(float_type, words);
  1039. }
  1040. if (float_type->width() == 64) {
  1041. utils::FloatProxy<double> result(0.0);
  1042. std::vector<uint32_t> words = result.GetWords();
  1043. return const_mgr->GetConstant(float_type, words);
  1044. }
  1045. return nullptr;
  1046. }
  1047. if (constants[0] == nullptr || constants[1] == nullptr) {
  1048. return nullptr;
  1049. }
  1050. std::vector<const analysis::Constant*> a_components;
  1051. std::vector<const analysis::Constant*> b_components;
  1052. a_components = constants[0]->GetVectorComponents(const_mgr);
  1053. b_components = constants[1]->GetVectorComponents(const_mgr);
  1054. utils::FloatProxy<double> result(0.0);
  1055. std::vector<uint32_t> words = result.GetWords();
  1056. const analysis::Constant* result_const =
  1057. const_mgr->GetConstant(float_type, words);
  1058. for (uint32_t i = 0; i < a_components.size() && result_const != nullptr;
  1059. ++i) {
  1060. if (a_components[i] == nullptr || b_components[i] == nullptr) {
  1061. return nullptr;
  1062. }
  1063. const analysis::Constant* component = FOLD_FPARITH_OP(*)(
  1064. new_type, a_components[i], b_components[i], const_mgr);
  1065. if (component == nullptr) {
  1066. return nullptr;
  1067. }
  1068. result_const =
  1069. FOLD_FPARITH_OP(+)(new_type, result_const, component, const_mgr);
  1070. }
  1071. return result_const;
  1072. };
  1073. }
  1074. ConstantFoldingRule FoldFNegate() { return FoldFPUnaryOp(NegateFPConst); }
  1075. ConstantFoldingRule FoldSNegate() { return FoldUnaryOp(NegateIntConst); }
  1076. ConstantFoldingRule FoldFClampFeedingCompare(spv::Op cmp_opcode) {
  1077. return [cmp_opcode](IRContext* context, Instruction* inst,
  1078. const std::vector<const analysis::Constant*>& constants)
  1079. -> const analysis::Constant* {
  1080. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  1081. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  1082. if (!inst->IsFloatingPointFoldingAllowed()) {
  1083. return nullptr;
  1084. }
  1085. uint32_t non_const_idx = (constants[0] ? 1 : 0);
  1086. uint32_t operand_id = inst->GetSingleWordInOperand(non_const_idx);
  1087. Instruction* operand_inst = def_use_mgr->GetDef(operand_id);
  1088. analysis::TypeManager* type_mgr = context->get_type_mgr();
  1089. const analysis::Type* operand_type =
  1090. type_mgr->GetType(operand_inst->type_id());
  1091. if (!operand_type->AsFloat()) {
  1092. return nullptr;
  1093. }
  1094. if (operand_type->AsFloat()->width() != 32 &&
  1095. operand_type->AsFloat()->width() != 64) {
  1096. return nullptr;
  1097. }
  1098. if (operand_inst->opcode() != spv::Op::OpExtInst) {
  1099. return nullptr;
  1100. }
  1101. if (operand_inst->GetSingleWordInOperand(1) != GLSLstd450FClamp) {
  1102. return nullptr;
  1103. }
  1104. if (constants[1] == nullptr && constants[0] == nullptr) {
  1105. return nullptr;
  1106. }
  1107. uint32_t max_id = operand_inst->GetSingleWordInOperand(4);
  1108. const analysis::Constant* max_const =
  1109. const_mgr->FindDeclaredConstant(max_id);
  1110. uint32_t min_id = operand_inst->GetSingleWordInOperand(3);
  1111. const analysis::Constant* min_const =
  1112. const_mgr->FindDeclaredConstant(min_id);
  1113. bool found_result = false;
  1114. bool result = false;
  1115. switch (cmp_opcode) {
  1116. case spv::Op::OpFOrdLessThan:
  1117. case spv::Op::OpFUnordLessThan:
  1118. case spv::Op::OpFOrdGreaterThanEqual:
  1119. case spv::Op::OpFUnordGreaterThanEqual:
  1120. if (constants[0]) {
  1121. if (min_const) {
  1122. if (constants[0]->GetValueAsDouble() <
  1123. min_const->GetValueAsDouble()) {
  1124. found_result = true;
  1125. result = (cmp_opcode == spv::Op::OpFOrdLessThan ||
  1126. cmp_opcode == spv::Op::OpFUnordLessThan);
  1127. }
  1128. }
  1129. if (max_const) {
  1130. if (constants[0]->GetValueAsDouble() >=
  1131. max_const->GetValueAsDouble()) {
  1132. found_result = true;
  1133. result = !(cmp_opcode == spv::Op::OpFOrdLessThan ||
  1134. cmp_opcode == spv::Op::OpFUnordLessThan);
  1135. }
  1136. }
  1137. }
  1138. if (constants[1]) {
  1139. if (max_const) {
  1140. if (max_const->GetValueAsDouble() <
  1141. constants[1]->GetValueAsDouble()) {
  1142. found_result = true;
  1143. result = (cmp_opcode == spv::Op::OpFOrdLessThan ||
  1144. cmp_opcode == spv::Op::OpFUnordLessThan);
  1145. }
  1146. }
  1147. if (min_const) {
  1148. if (min_const->GetValueAsDouble() >=
  1149. constants[1]->GetValueAsDouble()) {
  1150. found_result = true;
  1151. result = !(cmp_opcode == spv::Op::OpFOrdLessThan ||
  1152. cmp_opcode == spv::Op::OpFUnordLessThan);
  1153. }
  1154. }
  1155. }
  1156. break;
  1157. case spv::Op::OpFOrdGreaterThan:
  1158. case spv::Op::OpFUnordGreaterThan:
  1159. case spv::Op::OpFOrdLessThanEqual:
  1160. case spv::Op::OpFUnordLessThanEqual:
  1161. if (constants[0]) {
  1162. if (min_const) {
  1163. if (constants[0]->GetValueAsDouble() <=
  1164. min_const->GetValueAsDouble()) {
  1165. found_result = true;
  1166. result = (cmp_opcode == spv::Op::OpFOrdLessThanEqual ||
  1167. cmp_opcode == spv::Op::OpFUnordLessThanEqual);
  1168. }
  1169. }
  1170. if (max_const) {
  1171. if (constants[0]->GetValueAsDouble() >
  1172. max_const->GetValueAsDouble()) {
  1173. found_result = true;
  1174. result = !(cmp_opcode == spv::Op::OpFOrdLessThanEqual ||
  1175. cmp_opcode == spv::Op::OpFUnordLessThanEqual);
  1176. }
  1177. }
  1178. }
  1179. if (constants[1]) {
  1180. if (max_const) {
  1181. if (max_const->GetValueAsDouble() <=
  1182. constants[1]->GetValueAsDouble()) {
  1183. found_result = true;
  1184. result = (cmp_opcode == spv::Op::OpFOrdLessThanEqual ||
  1185. cmp_opcode == spv::Op::OpFUnordLessThanEqual);
  1186. }
  1187. }
  1188. if (min_const) {
  1189. if (min_const->GetValueAsDouble() >
  1190. constants[1]->GetValueAsDouble()) {
  1191. found_result = true;
  1192. result = !(cmp_opcode == spv::Op::OpFOrdLessThanEqual ||
  1193. cmp_opcode == spv::Op::OpFUnordLessThanEqual);
  1194. }
  1195. }
  1196. }
  1197. break;
  1198. default:
  1199. return nullptr;
  1200. }
  1201. if (!found_result) {
  1202. return nullptr;
  1203. }
  1204. const analysis::Type* bool_type =
  1205. context->get_type_mgr()->GetType(inst->type_id());
  1206. const analysis::Constant* result_const =
  1207. const_mgr->GetConstant(bool_type, {static_cast<uint32_t>(result)});
  1208. assert(result_const);
  1209. return result_const;
  1210. };
  1211. }
  1212. ConstantFoldingRule FoldFMix() {
  1213. return [](IRContext* context, Instruction* inst,
  1214. const std::vector<const analysis::Constant*>& constants)
  1215. -> const analysis::Constant* {
  1216. analysis::ConstantManager* const_mgr = context->get_constant_mgr();
  1217. assert(inst->opcode() == spv::Op::OpExtInst &&
  1218. "Expecting an extended instruction.");
  1219. assert(inst->GetSingleWordInOperand(0) ==
  1220. context->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
  1221. "Expecting a GLSLstd450 extended instruction.");
  1222. assert(inst->GetSingleWordInOperand(1) == GLSLstd450FMix &&
  1223. "Expecting and FMix instruction.");
  1224. if (!inst->IsFloatingPointFoldingAllowed()) {
  1225. return nullptr;
  1226. }
  1227. // Make sure all FMix operands are constants.
  1228. for (uint32_t i = 1; i < 4; i++) {
  1229. if (constants[i] == nullptr) {
  1230. return nullptr;
  1231. }
  1232. }
  1233. const analysis::Constant* one;
  1234. bool is_vector = false;
  1235. const analysis::Type* result_type = constants[1]->type();
  1236. const analysis::Type* base_type = result_type;
  1237. if (base_type->AsVector()) {
  1238. is_vector = true;
  1239. base_type = base_type->AsVector()->element_type();
  1240. }
  1241. assert(base_type->AsFloat() != nullptr &&
  1242. "FMix is suppose to act on floats or vectors of floats.");
  1243. if (base_type->AsFloat()->width() == 32) {
  1244. one = const_mgr->GetConstant(base_type,
  1245. utils::FloatProxy<float>(1.0f).GetWords());
  1246. } else {
  1247. one = const_mgr->GetConstant(base_type,
  1248. utils::FloatProxy<double>(1.0).GetWords());
  1249. }
  1250. if (is_vector) {
  1251. uint32_t one_id = const_mgr->GetDefiningInstruction(one)->result_id();
  1252. one =
  1253. const_mgr->GetConstant(result_type, std::vector<uint32_t>(4, one_id));
  1254. }
  1255. const analysis::Constant* temp1 = FoldFPBinaryOp(
  1256. FOLD_FPARITH_OP(-), inst->type_id(), {one, constants[3]}, context);
  1257. if (temp1 == nullptr) {
  1258. return nullptr;
  1259. }
  1260. const analysis::Constant* temp2 = FoldFPBinaryOp(
  1261. FOLD_FPARITH_OP(*), inst->type_id(), {constants[1], temp1}, context);
  1262. if (temp2 == nullptr) {
  1263. return nullptr;
  1264. }
  1265. const analysis::Constant* temp3 =
  1266. FoldFPBinaryOp(FOLD_FPARITH_OP(*), inst->type_id(),
  1267. {constants[2], constants[3]}, context);
  1268. if (temp3 == nullptr) {
  1269. return nullptr;
  1270. }
  1271. return FoldFPBinaryOp(FOLD_FPARITH_OP(+), inst->type_id(), {temp2, temp3},
  1272. context);
  1273. };
  1274. }
  1275. const analysis::Constant* FoldMin(const analysis::Type* result_type,
  1276. const analysis::Constant* a,
  1277. const analysis::Constant* b,
  1278. analysis::ConstantManager*) {
  1279. if (const analysis::Integer* int_type = result_type->AsInteger()) {
  1280. if (int_type->width() == 32) {
  1281. if (int_type->IsSigned()) {
  1282. int32_t va = a->GetS32();
  1283. int32_t vb = b->GetS32();
  1284. return (va < vb ? a : b);
  1285. } else {
  1286. uint32_t va = a->GetU32();
  1287. uint32_t vb = b->GetU32();
  1288. return (va < vb ? a : b);
  1289. }
  1290. } else if (int_type->width() == 64) {
  1291. if (int_type->IsSigned()) {
  1292. int64_t va = a->GetS64();
  1293. int64_t vb = b->GetS64();
  1294. return (va < vb ? a : b);
  1295. } else {
  1296. uint64_t va = a->GetU64();
  1297. uint64_t vb = b->GetU64();
  1298. return (va < vb ? a : b);
  1299. }
  1300. }
  1301. } else if (const analysis::Float* float_type = result_type->AsFloat()) {
  1302. if (float_type->width() == 32) {
  1303. float va = a->GetFloat();
  1304. float vb = b->GetFloat();
  1305. return (va < vb ? a : b);
  1306. } else if (float_type->width() == 64) {
  1307. double va = a->GetDouble();
  1308. double vb = b->GetDouble();
  1309. return (va < vb ? a : b);
  1310. }
  1311. }
  1312. return nullptr;
  1313. }
  1314. const analysis::Constant* FoldMax(const analysis::Type* result_type,
  1315. const analysis::Constant* a,
  1316. const analysis::Constant* b,
  1317. analysis::ConstantManager*) {
  1318. if (const analysis::Integer* int_type = result_type->AsInteger()) {
  1319. if (int_type->width() == 32) {
  1320. if (int_type->IsSigned()) {
  1321. int32_t va = a->GetS32();
  1322. int32_t vb = b->GetS32();
  1323. return (va > vb ? a : b);
  1324. } else {
  1325. uint32_t va = a->GetU32();
  1326. uint32_t vb = b->GetU32();
  1327. return (va > vb ? a : b);
  1328. }
  1329. } else if (int_type->width() == 64) {
  1330. if (int_type->IsSigned()) {
  1331. int64_t va = a->GetS64();
  1332. int64_t vb = b->GetS64();
  1333. return (va > vb ? a : b);
  1334. } else {
  1335. uint64_t va = a->GetU64();
  1336. uint64_t vb = b->GetU64();
  1337. return (va > vb ? a : b);
  1338. }
  1339. }
  1340. } else if (const analysis::Float* float_type = result_type->AsFloat()) {
  1341. if (float_type->width() == 32) {
  1342. float va = a->GetFloat();
  1343. float vb = b->GetFloat();
  1344. return (va > vb ? a : b);
  1345. } else if (float_type->width() == 64) {
  1346. double va = a->GetDouble();
  1347. double vb = b->GetDouble();
  1348. return (va > vb ? a : b);
  1349. }
  1350. }
  1351. return nullptr;
  1352. }
  1353. // Fold an clamp instruction when all three operands are constant.
  1354. const analysis::Constant* FoldClamp1(
  1355. IRContext* context, Instruction* inst,
  1356. const std::vector<const analysis::Constant*>& constants) {
  1357. assert(inst->opcode() == spv::Op::OpExtInst &&
  1358. "Expecting an extended instruction.");
  1359. assert(inst->GetSingleWordInOperand(0) ==
  1360. context->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
  1361. "Expecting a GLSLstd450 extended instruction.");
  1362. // Make sure all Clamp operands are constants.
  1363. for (uint32_t i = 1; i < 4; i++) {
  1364. if (constants[i] == nullptr) {
  1365. return nullptr;
  1366. }
  1367. }
  1368. const analysis::Constant* temp = FoldFPBinaryOp(
  1369. FoldMax, inst->type_id(), {constants[1], constants[2]}, context);
  1370. if (temp == nullptr) {
  1371. return nullptr;
  1372. }
  1373. return FoldFPBinaryOp(FoldMin, inst->type_id(), {temp, constants[3]},
  1374. context);
  1375. }
  1376. // Fold a clamp instruction when |x <= min_val|.
  1377. const analysis::Constant* FoldClamp2(
  1378. IRContext* context, Instruction* inst,
  1379. const std::vector<const analysis::Constant*>& constants) {
  1380. assert(inst->opcode() == spv::Op::OpExtInst &&
  1381. "Expecting an extended instruction.");
  1382. assert(inst->GetSingleWordInOperand(0) ==
  1383. context->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
  1384. "Expecting a GLSLstd450 extended instruction.");
  1385. const analysis::Constant* x = constants[1];
  1386. const analysis::Constant* min_val = constants[2];
  1387. if (x == nullptr || min_val == nullptr) {
  1388. return nullptr;
  1389. }
  1390. const analysis::Constant* temp =
  1391. FoldFPBinaryOp(FoldMax, inst->type_id(), {x, min_val}, context);
  1392. if (temp == min_val) {
  1393. // We can assume that |min_val| is less than |max_val|. Therefore, if the
  1394. // result of the max operation is |min_val|, we know the result of the min
  1395. // operation, even if |max_val| is not a constant.
  1396. return min_val;
  1397. }
  1398. return nullptr;
  1399. }
  1400. // Fold a clamp instruction when |x >= max_val|.
  1401. const analysis::Constant* FoldClamp3(
  1402. IRContext* context, Instruction* inst,
  1403. const std::vector<const analysis::Constant*>& constants) {
  1404. assert(inst->opcode() == spv::Op::OpExtInst &&
  1405. "Expecting an extended instruction.");
  1406. assert(inst->GetSingleWordInOperand(0) ==
  1407. context->get_feature_mgr()->GetExtInstImportId_GLSLstd450() &&
  1408. "Expecting a GLSLstd450 extended instruction.");
  1409. const analysis::Constant* x = constants[1];
  1410. const analysis::Constant* max_val = constants[3];
  1411. if (x == nullptr || max_val == nullptr) {
  1412. return nullptr;
  1413. }
  1414. const analysis::Constant* temp =
  1415. FoldFPBinaryOp(FoldMin, inst->type_id(), {x, max_val}, context);
  1416. if (temp == max_val) {
  1417. // We can assume that |min_val| is less than |max_val|. Therefore, if the
  1418. // result of the max operation is |min_val|, we know the result of the min
  1419. // operation, even if |max_val| is not a constant.
  1420. return max_val;
  1421. }
  1422. return nullptr;
  1423. }
  1424. UnaryScalarFoldingRule FoldFTranscendentalUnary(double (*fp)(double)) {
  1425. return
  1426. [fp](const analysis::Type* result_type, const analysis::Constant* a,
  1427. analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
  1428. assert(result_type != nullptr && a != nullptr);
  1429. const analysis::Float* float_type = a->type()->AsFloat();
  1430. assert(float_type != nullptr);
  1431. assert(float_type == result_type->AsFloat());
  1432. if (float_type->width() == 32) {
  1433. float fa = a->GetFloat();
  1434. float res = static_cast<float>(fp(fa));
  1435. utils::FloatProxy<float> result(res);
  1436. std::vector<uint32_t> words = result.GetWords();
  1437. return const_mgr->GetConstant(result_type, words);
  1438. } else if (float_type->width() == 64) {
  1439. double fa = a->GetDouble();
  1440. double res = fp(fa);
  1441. utils::FloatProxy<double> result(res);
  1442. std::vector<uint32_t> words = result.GetWords();
  1443. return const_mgr->GetConstant(result_type, words);
  1444. }
  1445. return nullptr;
  1446. };
  1447. }
  1448. BinaryScalarFoldingRule FoldFTranscendentalBinary(double (*fp)(double,
  1449. double)) {
  1450. return
  1451. [fp](const analysis::Type* result_type, const analysis::Constant* a,
  1452. const analysis::Constant* b,
  1453. analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
  1454. assert(result_type != nullptr && a != nullptr);
  1455. const analysis::Float* float_type = a->type()->AsFloat();
  1456. assert(float_type != nullptr);
  1457. assert(float_type == result_type->AsFloat());
  1458. assert(float_type == b->type()->AsFloat());
  1459. if (float_type->width() == 32) {
  1460. float fa = a->GetFloat();
  1461. float fb = b->GetFloat();
  1462. float res = static_cast<float>(fp(fa, fb));
  1463. utils::FloatProxy<float> result(res);
  1464. std::vector<uint32_t> words = result.GetWords();
  1465. return const_mgr->GetConstant(result_type, words);
  1466. } else if (float_type->width() == 64) {
  1467. double fa = a->GetDouble();
  1468. double fb = b->GetDouble();
  1469. double res = fp(fa, fb);
  1470. utils::FloatProxy<double> result(res);
  1471. std::vector<uint32_t> words = result.GetWords();
  1472. return const_mgr->GetConstant(result_type, words);
  1473. }
  1474. return nullptr;
  1475. };
  1476. }
  1477. enum Sign { Signed, Unsigned };
  1478. // Returns a BinaryScalarFoldingRule that applies `op` to the scalars.
  1479. // The `signedness` is used to determine if the operands should be interpreted
  1480. // as signed or unsigned. If the operands are signed, the value will be sign
  1481. // extended before the value is passed to `op`. Otherwise the values will be
  1482. // zero extended.
  1483. template <Sign signedness>
  1484. BinaryScalarFoldingRule FoldBinaryIntegerOperation(uint64_t (*op)(uint64_t,
  1485. uint64_t)) {
  1486. return
  1487. [op](const analysis::Type* result_type, const analysis::Constant* a,
  1488. const analysis::Constant* b,
  1489. analysis::ConstantManager* const_mgr) -> const analysis::Constant* {
  1490. assert(result_type != nullptr && a != nullptr && b != nullptr);
  1491. const analysis::Integer* integer_type = result_type->AsInteger();
  1492. assert(integer_type != nullptr);
  1493. assert(a->type()->kind() == analysis::Type::kInteger);
  1494. assert(b->type()->kind() == analysis::Type::kInteger);
  1495. assert(integer_type->width() == a->type()->AsInteger()->width());
  1496. assert(integer_type->width() == b->type()->AsInteger()->width());
  1497. // In SPIR-V, all operations support unsigned types, but the way they
  1498. // are interpreted depends on the opcode. This is why we use the
  1499. // template argument to determine how to interpret the operands.
  1500. uint64_t ia = (signedness == Signed ? a->GetSignExtendedValue()
  1501. : a->GetZeroExtendedValue());
  1502. uint64_t ib = (signedness == Signed ? b->GetSignExtendedValue()
  1503. : b->GetZeroExtendedValue());
  1504. uint64_t result = op(ia, ib);
  1505. const analysis::Constant* result_constant =
  1506. const_mgr->GenerateIntegerConstant(integer_type, result);
  1507. return result_constant;
  1508. };
  1509. }
  1510. // A scalar folding rule that folds OpSConvert.
  1511. const analysis::Constant* FoldScalarSConvert(
  1512. const analysis::Type* result_type, const analysis::Constant* a,
  1513. analysis::ConstantManager* const_mgr) {
  1514. assert(result_type != nullptr);
  1515. assert(a != nullptr);
  1516. assert(const_mgr != nullptr);
  1517. const analysis::Integer* integer_type = result_type->AsInteger();
  1518. assert(integer_type && "The result type of an SConvert");
  1519. int64_t value = a->GetSignExtendedValue();
  1520. return const_mgr->GenerateIntegerConstant(integer_type, value);
  1521. }
  1522. // A scalar folding rule that folds OpUConvert.
  1523. const analysis::Constant* FoldScalarUConvert(
  1524. const analysis::Type* result_type, const analysis::Constant* a,
  1525. analysis::ConstantManager* const_mgr) {
  1526. assert(result_type != nullptr);
  1527. assert(a != nullptr);
  1528. assert(const_mgr != nullptr);
  1529. const analysis::Integer* integer_type = result_type->AsInteger();
  1530. assert(integer_type && "The result type of an UConvert");
  1531. uint64_t value = a->GetZeroExtendedValue();
  1532. // If the operand was an unsigned value with less than 32-bit, it would have
  1533. // been sign extended earlier, and we need to clear those bits.
  1534. auto* operand_type = a->type()->AsInteger();
  1535. value = utils::ClearHighBits(value, 64 - operand_type->width());
  1536. return const_mgr->GenerateIntegerConstant(integer_type, value);
  1537. }
  1538. } // namespace
  1539. void ConstantFoldingRules::AddFoldingRules() {
  1540. // Add all folding rules to the list for the opcodes to which they apply.
  1541. // Note that the order in which rules are added to the list matters. If a rule
  1542. // applies to the instruction, the rest of the rules will not be attempted.
  1543. // Take that into consideration.
  1544. rules_[spv::Op::OpCompositeConstruct].push_back(FoldCompositeWithConstants());
  1545. rules_[spv::Op::OpCompositeExtract].push_back(FoldExtractWithConstants());
  1546. rules_[spv::Op::OpCompositeInsert].push_back(FoldInsertWithConstants());
  1547. rules_[spv::Op::OpConvertFToS].push_back(FoldFToI());
  1548. rules_[spv::Op::OpConvertFToU].push_back(FoldFToI());
  1549. rules_[spv::Op::OpConvertSToF].push_back(FoldIToF());
  1550. rules_[spv::Op::OpConvertUToF].push_back(FoldIToF());
  1551. rules_[spv::Op::OpSConvert].push_back(FoldUnaryOp(FoldScalarSConvert));
  1552. rules_[spv::Op::OpUConvert].push_back(FoldUnaryOp(FoldScalarUConvert));
  1553. rules_[spv::Op::OpDot].push_back(FoldOpDotWithConstants());
  1554. rules_[spv::Op::OpFAdd].push_back(FoldFAdd());
  1555. rules_[spv::Op::OpFDiv].push_back(FoldFDiv());
  1556. rules_[spv::Op::OpFMul].push_back(FoldFMul());
  1557. rules_[spv::Op::OpFSub].push_back(FoldFSub());
  1558. rules_[spv::Op::OpFOrdEqual].push_back(FoldFOrdEqual());
  1559. rules_[spv::Op::OpFUnordEqual].push_back(FoldFUnordEqual());
  1560. rules_[spv::Op::OpFOrdNotEqual].push_back(FoldFOrdNotEqual());
  1561. rules_[spv::Op::OpFUnordNotEqual].push_back(FoldFUnordNotEqual());
  1562. rules_[spv::Op::OpFOrdLessThan].push_back(FoldFOrdLessThan());
  1563. rules_[spv::Op::OpFOrdLessThan].push_back(
  1564. FoldFClampFeedingCompare(spv::Op::OpFOrdLessThan));
  1565. rules_[spv::Op::OpFUnordLessThan].push_back(FoldFUnordLessThan());
  1566. rules_[spv::Op::OpFUnordLessThan].push_back(
  1567. FoldFClampFeedingCompare(spv::Op::OpFUnordLessThan));
  1568. rules_[spv::Op::OpFOrdGreaterThan].push_back(FoldFOrdGreaterThan());
  1569. rules_[spv::Op::OpFOrdGreaterThan].push_back(
  1570. FoldFClampFeedingCompare(spv::Op::OpFOrdGreaterThan));
  1571. rules_[spv::Op::OpFUnordGreaterThan].push_back(FoldFUnordGreaterThan());
  1572. rules_[spv::Op::OpFUnordGreaterThan].push_back(
  1573. FoldFClampFeedingCompare(spv::Op::OpFUnordGreaterThan));
  1574. rules_[spv::Op::OpFOrdLessThanEqual].push_back(FoldFOrdLessThanEqual());
  1575. rules_[spv::Op::OpFOrdLessThanEqual].push_back(
  1576. FoldFClampFeedingCompare(spv::Op::OpFOrdLessThanEqual));
  1577. rules_[spv::Op::OpFUnordLessThanEqual].push_back(FoldFUnordLessThanEqual());
  1578. rules_[spv::Op::OpFUnordLessThanEqual].push_back(
  1579. FoldFClampFeedingCompare(spv::Op::OpFUnordLessThanEqual));
  1580. rules_[spv::Op::OpFOrdGreaterThanEqual].push_back(FoldFOrdGreaterThanEqual());
  1581. rules_[spv::Op::OpFOrdGreaterThanEqual].push_back(
  1582. FoldFClampFeedingCompare(spv::Op::OpFOrdGreaterThanEqual));
  1583. rules_[spv::Op::OpFUnordGreaterThanEqual].push_back(
  1584. FoldFUnordGreaterThanEqual());
  1585. rules_[spv::Op::OpFUnordGreaterThanEqual].push_back(
  1586. FoldFClampFeedingCompare(spv::Op::OpFUnordGreaterThanEqual));
  1587. rules_[spv::Op::OpVectorShuffle].push_back(FoldVectorShuffleWithConstants());
  1588. rules_[spv::Op::OpVectorTimesScalar].push_back(FoldVectorTimesScalar());
  1589. rules_[spv::Op::OpVectorTimesMatrix].push_back(FoldVectorTimesMatrix());
  1590. rules_[spv::Op::OpMatrixTimesVector].push_back(FoldMatrixTimesVector());
  1591. rules_[spv::Op::OpTranspose].push_back(FoldTranspose);
  1592. rules_[spv::Op::OpFNegate].push_back(FoldFNegate());
  1593. rules_[spv::Op::OpSNegate].push_back(FoldSNegate());
  1594. rules_[spv::Op::OpQuantizeToF16].push_back(FoldQuantizeToF16());
  1595. rules_[spv::Op::OpIAdd].push_back(
  1596. FoldBinaryOp(FoldBinaryIntegerOperation<Unsigned>(
  1597. [](uint64_t a, uint64_t b) { return a + b; })));
  1598. rules_[spv::Op::OpISub].push_back(
  1599. FoldBinaryOp(FoldBinaryIntegerOperation<Unsigned>(
  1600. [](uint64_t a, uint64_t b) { return a - b; })));
  1601. rules_[spv::Op::OpIMul].push_back(
  1602. FoldBinaryOp(FoldBinaryIntegerOperation<Unsigned>(
  1603. [](uint64_t a, uint64_t b) { return a * b; })));
  1604. rules_[spv::Op::OpUDiv].push_back(
  1605. FoldBinaryOp(FoldBinaryIntegerOperation<Unsigned>(
  1606. [](uint64_t a, uint64_t b) { return (b != 0 ? a / b : 0); })));
  1607. rules_[spv::Op::OpSDiv].push_back(FoldBinaryOp(
  1608. FoldBinaryIntegerOperation<Signed>([](uint64_t a, uint64_t b) {
  1609. return (b != 0 ? static_cast<uint64_t>(static_cast<int64_t>(a) /
  1610. static_cast<int64_t>(b))
  1611. : 0);
  1612. })));
  1613. rules_[spv::Op::OpUMod].push_back(
  1614. FoldBinaryOp(FoldBinaryIntegerOperation<Unsigned>(
  1615. [](uint64_t a, uint64_t b) { return (b != 0 ? a % b : 0); })));
  1616. rules_[spv::Op::OpSRem].push_back(FoldBinaryOp(
  1617. FoldBinaryIntegerOperation<Signed>([](uint64_t a, uint64_t b) {
  1618. return (b != 0 ? static_cast<uint64_t>(static_cast<int64_t>(a) %
  1619. static_cast<int64_t>(b))
  1620. : 0);
  1621. })));
  1622. rules_[spv::Op::OpSMod].push_back(FoldBinaryOp(
  1623. FoldBinaryIntegerOperation<Signed>([](uint64_t a, uint64_t b) {
  1624. if (b == 0) return static_cast<uint64_t>(0ull);
  1625. int64_t signed_a = static_cast<int64_t>(a);
  1626. int64_t signed_b = static_cast<int64_t>(b);
  1627. int64_t result = signed_a % signed_b;
  1628. if ((signed_b < 0) != (result < 0)) result += signed_b;
  1629. return static_cast<uint64_t>(result);
  1630. })));
  1631. // Add rules for GLSLstd450
  1632. FeatureManager* feature_manager = context_->get_feature_mgr();
  1633. uint32_t ext_inst_glslstd450_id =
  1634. feature_manager->GetExtInstImportId_GLSLstd450();
  1635. if (ext_inst_glslstd450_id != 0) {
  1636. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FMix}].push_back(FoldFMix());
  1637. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SMin}].push_back(
  1638. FoldFPBinaryOp(FoldMin));
  1639. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UMin}].push_back(
  1640. FoldFPBinaryOp(FoldMin));
  1641. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FMin}].push_back(
  1642. FoldFPBinaryOp(FoldMin));
  1643. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SMax}].push_back(
  1644. FoldFPBinaryOp(FoldMax));
  1645. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UMax}].push_back(
  1646. FoldFPBinaryOp(FoldMax));
  1647. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FMax}].push_back(
  1648. FoldFPBinaryOp(FoldMax));
  1649. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UClamp}].push_back(
  1650. FoldClamp1);
  1651. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UClamp}].push_back(
  1652. FoldClamp2);
  1653. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450UClamp}].push_back(
  1654. FoldClamp3);
  1655. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SClamp}].push_back(
  1656. FoldClamp1);
  1657. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SClamp}].push_back(
  1658. FoldClamp2);
  1659. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450SClamp}].push_back(
  1660. FoldClamp3);
  1661. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FClamp}].push_back(
  1662. FoldClamp1);
  1663. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FClamp}].push_back(
  1664. FoldClamp2);
  1665. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450FClamp}].push_back(
  1666. FoldClamp3);
  1667. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Sin}].push_back(
  1668. FoldFPUnaryOp(FoldFTranscendentalUnary(std::sin)));
  1669. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Cos}].push_back(
  1670. FoldFPUnaryOp(FoldFTranscendentalUnary(std::cos)));
  1671. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Tan}].push_back(
  1672. FoldFPUnaryOp(FoldFTranscendentalUnary(std::tan)));
  1673. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Asin}].push_back(
  1674. FoldFPUnaryOp(FoldFTranscendentalUnary(std::asin)));
  1675. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Acos}].push_back(
  1676. FoldFPUnaryOp(FoldFTranscendentalUnary(std::acos)));
  1677. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Atan}].push_back(
  1678. FoldFPUnaryOp(FoldFTranscendentalUnary(std::atan)));
  1679. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Exp}].push_back(
  1680. FoldFPUnaryOp(FoldFTranscendentalUnary(std::exp)));
  1681. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Log}].push_back(
  1682. FoldFPUnaryOp(FoldFTranscendentalUnary(std::log)));
  1683. #ifdef __ANDROID__
  1684. // Android NDK r15c targeting ABI 15 doesn't have full support for C++11
  1685. // (no std::exp2/log2). ::exp2 is available from C99 but ::log2 isn't
  1686. // available up until ABI 18 so we use a shim
  1687. auto log2_shim = [](double v) -> double { return log(v) / log(2.0); };
  1688. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Exp2}].push_back(
  1689. FoldFPUnaryOp(FoldFTranscendentalUnary(::exp2)));
  1690. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Log2}].push_back(
  1691. FoldFPUnaryOp(FoldFTranscendentalUnary(log2_shim)));
  1692. #else
  1693. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Exp2}].push_back(
  1694. FoldFPUnaryOp(FoldFTranscendentalUnary(std::exp2)));
  1695. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Log2}].push_back(
  1696. FoldFPUnaryOp(FoldFTranscendentalUnary(std::log2)));
  1697. #endif
  1698. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Sqrt}].push_back(
  1699. FoldFPUnaryOp(FoldFTranscendentalUnary(std::sqrt)));
  1700. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Atan2}].push_back(
  1701. FoldFPBinaryOp(FoldFTranscendentalBinary(std::atan2)));
  1702. ext_rules_[{ext_inst_glslstd450_id, GLSLstd450Pow}].push_back(
  1703. FoldFPBinaryOp(FoldFTranscendentalBinary(std::pow)));
  1704. }
  1705. }
  1706. } // namespace opt
  1707. } // namespace spvtools