fold.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. // Copyright (c) 2017 Google Inc.
  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/fold.h"
  15. #include <cassert>
  16. #include <cstdint>
  17. #include <vector>
  18. #include "source/opt/const_folding_rules.h"
  19. #include "source/opt/def_use_manager.h"
  20. #include "source/opt/folding_rules.h"
  21. #include "source/opt/ir_context.h"
  22. namespace spvtools {
  23. namespace opt {
  24. namespace {
  25. #ifndef INT32_MIN
  26. #define INT32_MIN (-2147483648)
  27. #endif
  28. #ifndef INT32_MAX
  29. #define INT32_MAX 2147483647
  30. #endif
  31. #ifndef UINT32_MAX
  32. #define UINT32_MAX 0xffffffff /* 4294967295U */
  33. #endif
  34. } // namespace
  35. uint32_t InstructionFolder::UnaryOperate(spv::Op opcode,
  36. uint32_t operand) const {
  37. switch (opcode) {
  38. // Arthimetics
  39. case spv::Op::OpSNegate: {
  40. int32_t s_operand = static_cast<int32_t>(operand);
  41. if (s_operand == std::numeric_limits<int32_t>::min()) {
  42. return s_operand;
  43. }
  44. return -s_operand;
  45. }
  46. case spv::Op::OpNot:
  47. return ~operand;
  48. case spv::Op::OpLogicalNot:
  49. return !static_cast<bool>(operand);
  50. case spv::Op::OpUConvert:
  51. return operand;
  52. case spv::Op::OpSConvert:
  53. return operand;
  54. default:
  55. assert(false &&
  56. "Unsupported unary operation for OpSpecConstantOp instruction");
  57. return 0u;
  58. }
  59. }
  60. uint32_t InstructionFolder::BinaryOperate(spv::Op opcode, uint32_t a,
  61. uint32_t b) const {
  62. switch (opcode) {
  63. // Shifting
  64. case spv::Op::OpShiftRightLogical:
  65. if (b >= 32) {
  66. // This is undefined behaviour when |b| > 32. Choose 0 for consistency.
  67. // When |b| == 32, doing the shift in C++ in undefined, but the result
  68. // will be 0, so just return that value.
  69. return 0;
  70. }
  71. return a >> b;
  72. case spv::Op::OpShiftRightArithmetic:
  73. if (b > 32) {
  74. // This is undefined behaviour. Choose 0 for consistency.
  75. return 0;
  76. }
  77. if (b == 32) {
  78. // Doing the shift in C++ is undefined, but the result is defined in the
  79. // spir-v spec. Find that value another way.
  80. if (static_cast<int32_t>(a) >= 0) {
  81. return 0;
  82. } else {
  83. return static_cast<uint32_t>(-1);
  84. }
  85. }
  86. return (static_cast<int32_t>(a)) >> b;
  87. case spv::Op::OpShiftLeftLogical:
  88. if (b >= 32) {
  89. // This is undefined behaviour when |b| > 32. Choose 0 for consistency.
  90. // When |b| == 32, doing the shift in C++ in undefined, but the result
  91. // will be 0, so just return that value.
  92. return 0;
  93. }
  94. return a << b;
  95. // Bitwise operations
  96. case spv::Op::OpBitwiseOr:
  97. return a | b;
  98. case spv::Op::OpBitwiseAnd:
  99. return a & b;
  100. case spv::Op::OpBitwiseXor:
  101. return a ^ b;
  102. // Logical
  103. case spv::Op::OpLogicalEqual:
  104. return (static_cast<bool>(a)) == (static_cast<bool>(b));
  105. case spv::Op::OpLogicalNotEqual:
  106. return (static_cast<bool>(a)) != (static_cast<bool>(b));
  107. case spv::Op::OpLogicalOr:
  108. return (static_cast<bool>(a)) || (static_cast<bool>(b));
  109. case spv::Op::OpLogicalAnd:
  110. return (static_cast<bool>(a)) && (static_cast<bool>(b));
  111. // Comparison
  112. case spv::Op::OpIEqual:
  113. return a == b;
  114. case spv::Op::OpINotEqual:
  115. return a != b;
  116. case spv::Op::OpULessThan:
  117. return a < b;
  118. case spv::Op::OpSLessThan:
  119. return (static_cast<int32_t>(a)) < (static_cast<int32_t>(b));
  120. case spv::Op::OpUGreaterThan:
  121. return a > b;
  122. case spv::Op::OpSGreaterThan:
  123. return (static_cast<int32_t>(a)) > (static_cast<int32_t>(b));
  124. case spv::Op::OpULessThanEqual:
  125. return a <= b;
  126. case spv::Op::OpSLessThanEqual:
  127. return (static_cast<int32_t>(a)) <= (static_cast<int32_t>(b));
  128. case spv::Op::OpUGreaterThanEqual:
  129. return a >= b;
  130. case spv::Op::OpSGreaterThanEqual:
  131. return (static_cast<int32_t>(a)) >= (static_cast<int32_t>(b));
  132. default:
  133. assert(false &&
  134. "Unsupported binary operation for OpSpecConstantOp instruction");
  135. return 0u;
  136. }
  137. }
  138. uint32_t InstructionFolder::TernaryOperate(spv::Op opcode, uint32_t a,
  139. uint32_t b, uint32_t c) const {
  140. switch (opcode) {
  141. case spv::Op::OpSelect:
  142. return (static_cast<bool>(a)) ? b : c;
  143. default:
  144. assert(false &&
  145. "Unsupported ternary operation for OpSpecConstantOp instruction");
  146. return 0u;
  147. }
  148. }
  149. uint32_t InstructionFolder::OperateWords(
  150. spv::Op opcode, const std::vector<uint32_t>& operand_words) const {
  151. switch (operand_words.size()) {
  152. case 1:
  153. return UnaryOperate(opcode, operand_words.front());
  154. case 2:
  155. return BinaryOperate(opcode, operand_words.front(), operand_words.back());
  156. case 3:
  157. return TernaryOperate(opcode, operand_words[0], operand_words[1],
  158. operand_words[2]);
  159. default:
  160. assert(false && "Invalid number of operands");
  161. return 0;
  162. }
  163. }
  164. bool InstructionFolder::FoldInstructionInternal(Instruction* inst) const {
  165. auto identity_map = [](uint32_t id) { return id; };
  166. Instruction* folded_inst = FoldInstructionToConstant(inst, identity_map);
  167. if (folded_inst != nullptr) {
  168. inst->SetOpcode(spv::Op::OpCopyObject);
  169. inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {folded_inst->result_id()}}});
  170. return true;
  171. }
  172. analysis::ConstantManager* const_manager = context_->get_constant_mgr();
  173. std::vector<const analysis::Constant*> constants =
  174. const_manager->GetOperandConstants(inst);
  175. for (const FoldingRule& rule :
  176. GetFoldingRules().GetRulesForInstruction(inst)) {
  177. if (rule(context_, inst, constants)) {
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183. // Returns the result of performing an operation on scalar constant operands.
  184. // This function extracts the operand values as 32 bit words and returns the
  185. // result in 32 bit word. Scalar constants with longer than 32-bit width are
  186. // not accepted in this function.
  187. uint32_t InstructionFolder::FoldScalars(
  188. spv::Op opcode,
  189. const std::vector<const analysis::Constant*>& operands) const {
  190. assert(IsFoldableOpcode(opcode) &&
  191. "Unhandled instruction opcode in FoldScalars");
  192. std::vector<uint32_t> operand_values_in_raw_words;
  193. for (const auto& operand : operands) {
  194. if (const analysis::ScalarConstant* scalar = operand->AsScalarConstant()) {
  195. const auto& scalar_words = scalar->words();
  196. assert(scalar_words.size() == 1 &&
  197. "Scalar constants with longer than 32-bit width are not allowed "
  198. "in FoldScalars()");
  199. operand_values_in_raw_words.push_back(scalar_words.front());
  200. } else if (operand->AsNullConstant()) {
  201. operand_values_in_raw_words.push_back(0u);
  202. } else {
  203. assert(false &&
  204. "FoldScalars() only accepts ScalarConst or NullConst type of "
  205. "constant");
  206. }
  207. }
  208. return OperateWords(opcode, operand_values_in_raw_words);
  209. }
  210. bool InstructionFolder::FoldBinaryIntegerOpToConstant(
  211. Instruction* inst, const std::function<uint32_t(uint32_t)>& id_map,
  212. uint32_t* result) const {
  213. spv::Op opcode = inst->opcode();
  214. analysis::ConstantManager* const_manger = context_->get_constant_mgr();
  215. uint32_t ids[2];
  216. const analysis::IntConstant* constants[2];
  217. for (uint32_t i = 0; i < 2; i++) {
  218. const Operand* operand = &inst->GetInOperand(i);
  219. if (operand->type != SPV_OPERAND_TYPE_ID) {
  220. return false;
  221. }
  222. ids[i] = id_map(operand->words[0]);
  223. const analysis::Constant* constant =
  224. const_manger->FindDeclaredConstant(ids[i]);
  225. constants[i] = (constant != nullptr ? constant->AsIntConstant() : nullptr);
  226. }
  227. switch (opcode) {
  228. // Arthimetics
  229. case spv::Op::OpIMul:
  230. for (uint32_t i = 0; i < 2; i++) {
  231. if (constants[i] != nullptr && constants[i]->IsZero()) {
  232. *result = 0;
  233. return true;
  234. }
  235. }
  236. break;
  237. case spv::Op::OpUDiv:
  238. case spv::Op::OpSDiv:
  239. case spv::Op::OpSRem:
  240. case spv::Op::OpSMod:
  241. case spv::Op::OpUMod:
  242. // This changes undefined behaviour (ie divide by 0) into a 0.
  243. for (uint32_t i = 0; i < 2; i++) {
  244. if (constants[i] != nullptr && constants[i]->IsZero()) {
  245. *result = 0;
  246. return true;
  247. }
  248. }
  249. break;
  250. // Shifting
  251. case spv::Op::OpShiftRightLogical:
  252. case spv::Op::OpShiftLeftLogical:
  253. if (constants[1] != nullptr) {
  254. // When shifting by a value larger than the size of the result, the
  255. // result is undefined. We are setting the undefined behaviour to a
  256. // result of 0. If the shift amount is the same as the size of the
  257. // result, then the result is defined, and it 0.
  258. uint32_t shift_amount = constants[1]->GetU32BitValue();
  259. if (shift_amount >= 32) {
  260. *result = 0;
  261. return true;
  262. }
  263. }
  264. break;
  265. // Bitwise operations
  266. case spv::Op::OpBitwiseOr:
  267. for (uint32_t i = 0; i < 2; i++) {
  268. if (constants[i] != nullptr) {
  269. // TODO: Change the mask against a value based on the bit width of the
  270. // instruction result type. This way we can handle say 16-bit values
  271. // as well.
  272. uint32_t mask = constants[i]->GetU32BitValue();
  273. if (mask == 0xFFFFFFFF) {
  274. *result = 0xFFFFFFFF;
  275. return true;
  276. }
  277. }
  278. }
  279. break;
  280. case spv::Op::OpBitwiseAnd:
  281. for (uint32_t i = 0; i < 2; i++) {
  282. if (constants[i] != nullptr) {
  283. if (constants[i]->IsZero()) {
  284. *result = 0;
  285. return true;
  286. }
  287. }
  288. }
  289. break;
  290. // Comparison
  291. case spv::Op::OpULessThan:
  292. if (constants[0] != nullptr &&
  293. constants[0]->GetU32BitValue() == UINT32_MAX) {
  294. *result = false;
  295. return true;
  296. }
  297. if (constants[1] != nullptr && constants[1]->GetU32BitValue() == 0) {
  298. *result = false;
  299. return true;
  300. }
  301. break;
  302. case spv::Op::OpSLessThan:
  303. if (constants[0] != nullptr &&
  304. constants[0]->GetS32BitValue() == INT32_MAX) {
  305. *result = false;
  306. return true;
  307. }
  308. if (constants[1] != nullptr &&
  309. constants[1]->GetS32BitValue() == INT32_MIN) {
  310. *result = false;
  311. return true;
  312. }
  313. break;
  314. case spv::Op::OpUGreaterThan:
  315. if (constants[0] != nullptr && constants[0]->IsZero()) {
  316. *result = false;
  317. return true;
  318. }
  319. if (constants[1] != nullptr &&
  320. constants[1]->GetU32BitValue() == UINT32_MAX) {
  321. *result = false;
  322. return true;
  323. }
  324. break;
  325. case spv::Op::OpSGreaterThan:
  326. if (constants[0] != nullptr &&
  327. constants[0]->GetS32BitValue() == INT32_MIN) {
  328. *result = false;
  329. return true;
  330. }
  331. if (constants[1] != nullptr &&
  332. constants[1]->GetS32BitValue() == INT32_MAX) {
  333. *result = false;
  334. return true;
  335. }
  336. break;
  337. case spv::Op::OpULessThanEqual:
  338. if (constants[0] != nullptr && constants[0]->IsZero()) {
  339. *result = true;
  340. return true;
  341. }
  342. if (constants[1] != nullptr &&
  343. constants[1]->GetU32BitValue() == UINT32_MAX) {
  344. *result = true;
  345. return true;
  346. }
  347. break;
  348. case spv::Op::OpSLessThanEqual:
  349. if (constants[0] != nullptr &&
  350. constants[0]->GetS32BitValue() == INT32_MIN) {
  351. *result = true;
  352. return true;
  353. }
  354. if (constants[1] != nullptr &&
  355. constants[1]->GetS32BitValue() == INT32_MAX) {
  356. *result = true;
  357. return true;
  358. }
  359. break;
  360. case spv::Op::OpUGreaterThanEqual:
  361. if (constants[0] != nullptr &&
  362. constants[0]->GetU32BitValue() == UINT32_MAX) {
  363. *result = true;
  364. return true;
  365. }
  366. if (constants[1] != nullptr && constants[1]->GetU32BitValue() == 0) {
  367. *result = true;
  368. return true;
  369. }
  370. break;
  371. case spv::Op::OpSGreaterThanEqual:
  372. if (constants[0] != nullptr &&
  373. constants[0]->GetS32BitValue() == INT32_MAX) {
  374. *result = true;
  375. return true;
  376. }
  377. if (constants[1] != nullptr &&
  378. constants[1]->GetS32BitValue() == INT32_MIN) {
  379. *result = true;
  380. return true;
  381. }
  382. break;
  383. default:
  384. break;
  385. }
  386. return false;
  387. }
  388. bool InstructionFolder::FoldBinaryBooleanOpToConstant(
  389. Instruction* inst, const std::function<uint32_t(uint32_t)>& id_map,
  390. uint32_t* result) const {
  391. spv::Op opcode = inst->opcode();
  392. analysis::ConstantManager* const_manger = context_->get_constant_mgr();
  393. uint32_t ids[2];
  394. const analysis::BoolConstant* constants[2];
  395. for (uint32_t i = 0; i < 2; i++) {
  396. const Operand* operand = &inst->GetInOperand(i);
  397. if (operand->type != SPV_OPERAND_TYPE_ID) {
  398. return false;
  399. }
  400. ids[i] = id_map(operand->words[0]);
  401. const analysis::Constant* constant =
  402. const_manger->FindDeclaredConstant(ids[i]);
  403. constants[i] = (constant != nullptr ? constant->AsBoolConstant() : nullptr);
  404. }
  405. switch (opcode) {
  406. // Logical
  407. case spv::Op::OpLogicalOr:
  408. for (uint32_t i = 0; i < 2; i++) {
  409. if (constants[i] != nullptr) {
  410. if (constants[i]->value()) {
  411. *result = true;
  412. return true;
  413. }
  414. }
  415. }
  416. break;
  417. case spv::Op::OpLogicalAnd:
  418. for (uint32_t i = 0; i < 2; i++) {
  419. if (constants[i] != nullptr) {
  420. if (!constants[i]->value()) {
  421. *result = false;
  422. return true;
  423. }
  424. }
  425. }
  426. break;
  427. default:
  428. break;
  429. }
  430. return false;
  431. }
  432. bool InstructionFolder::FoldIntegerOpToConstant(
  433. Instruction* inst, const std::function<uint32_t(uint32_t)>& id_map,
  434. uint32_t* result) const {
  435. assert(IsFoldableOpcode(inst->opcode()) &&
  436. "Unhandled instruction opcode in FoldScalars");
  437. switch (inst->NumInOperands()) {
  438. case 2:
  439. return FoldBinaryIntegerOpToConstant(inst, id_map, result) ||
  440. FoldBinaryBooleanOpToConstant(inst, id_map, result);
  441. default:
  442. return false;
  443. }
  444. }
  445. std::vector<uint32_t> InstructionFolder::FoldVectors(
  446. spv::Op opcode, uint32_t num_dims,
  447. const std::vector<const analysis::Constant*>& operands) const {
  448. assert(IsFoldableOpcode(opcode) &&
  449. "Unhandled instruction opcode in FoldVectors");
  450. std::vector<uint32_t> result;
  451. for (uint32_t d = 0; d < num_dims; d++) {
  452. std::vector<uint32_t> operand_values_for_one_dimension;
  453. for (const auto& operand : operands) {
  454. if (const analysis::VectorConstant* vector_operand =
  455. operand->AsVectorConstant()) {
  456. // Extract the raw value of the scalar component constants
  457. // in 32-bit words here. The reason of not using FoldScalars() here
  458. // is that we do not create temporary null constants as components
  459. // when the vector operand is a NullConstant because Constant creation
  460. // may need extra checks for the validity and that is not managed in
  461. // here.
  462. if (const analysis::ScalarConstant* scalar_component =
  463. vector_operand->GetComponents().at(d)->AsScalarConstant()) {
  464. const auto& scalar_words = scalar_component->words();
  465. assert(
  466. scalar_words.size() == 1 &&
  467. "Vector components with longer than 32-bit width are not allowed "
  468. "in FoldVectors()");
  469. operand_values_for_one_dimension.push_back(scalar_words.front());
  470. } else if (operand->AsNullConstant()) {
  471. operand_values_for_one_dimension.push_back(0u);
  472. } else {
  473. assert(false &&
  474. "VectorConst should only has ScalarConst or NullConst as "
  475. "components");
  476. }
  477. } else if (operand->AsNullConstant()) {
  478. operand_values_for_one_dimension.push_back(0u);
  479. } else {
  480. assert(false &&
  481. "FoldVectors() only accepts VectorConst or NullConst type of "
  482. "constant");
  483. }
  484. }
  485. result.push_back(OperateWords(opcode, operand_values_for_one_dimension));
  486. }
  487. return result;
  488. }
  489. bool InstructionFolder::IsFoldableOpcode(spv::Op opcode) const {
  490. // NOTE: Extend to more opcodes as new cases are handled in the folder
  491. // functions.
  492. switch (opcode) {
  493. case spv::Op::OpBitwiseAnd:
  494. case spv::Op::OpBitwiseOr:
  495. case spv::Op::OpBitwiseXor:
  496. case spv::Op::OpIAdd:
  497. case spv::Op::OpIEqual:
  498. case spv::Op::OpIMul:
  499. case spv::Op::OpINotEqual:
  500. case spv::Op::OpISub:
  501. case spv::Op::OpLogicalAnd:
  502. case spv::Op::OpLogicalEqual:
  503. case spv::Op::OpLogicalNot:
  504. case spv::Op::OpLogicalNotEqual:
  505. case spv::Op::OpLogicalOr:
  506. case spv::Op::OpNot:
  507. case spv::Op::OpSDiv:
  508. case spv::Op::OpSelect:
  509. case spv::Op::OpSGreaterThan:
  510. case spv::Op::OpSGreaterThanEqual:
  511. case spv::Op::OpShiftLeftLogical:
  512. case spv::Op::OpShiftRightArithmetic:
  513. case spv::Op::OpShiftRightLogical:
  514. case spv::Op::OpSLessThan:
  515. case spv::Op::OpSLessThanEqual:
  516. case spv::Op::OpSMod:
  517. case spv::Op::OpSNegate:
  518. case spv::Op::OpSRem:
  519. case spv::Op::OpSConvert:
  520. case spv::Op::OpUConvert:
  521. case spv::Op::OpUDiv:
  522. case spv::Op::OpUGreaterThan:
  523. case spv::Op::OpUGreaterThanEqual:
  524. case spv::Op::OpULessThan:
  525. case spv::Op::OpULessThanEqual:
  526. case spv::Op::OpUMod:
  527. return true;
  528. default:
  529. return false;
  530. }
  531. }
  532. bool InstructionFolder::IsFoldableConstant(
  533. const analysis::Constant* cst) const {
  534. // Currently supported constants are 32-bit values or null constants.
  535. if (const analysis::ScalarConstant* scalar = cst->AsScalarConstant())
  536. return scalar->words().size() == 1;
  537. else
  538. return cst->AsNullConstant() != nullptr;
  539. }
  540. Instruction* InstructionFolder::FoldInstructionToConstant(
  541. Instruction* inst, std::function<uint32_t(uint32_t)> id_map) const {
  542. analysis::ConstantManager* const_mgr = context_->get_constant_mgr();
  543. if (!inst->IsFoldableByFoldScalar() && !inst->IsFoldableByFoldVector() &&
  544. !GetConstantFoldingRules().HasFoldingRule(inst)) {
  545. return nullptr;
  546. }
  547. // Collect the values of the constant parameters.
  548. std::vector<const analysis::Constant*> constants;
  549. bool missing_constants = false;
  550. inst->ForEachInId([&constants, &missing_constants, const_mgr,
  551. &id_map](uint32_t* op_id) {
  552. uint32_t id = id_map(*op_id);
  553. const analysis::Constant* const_op = const_mgr->FindDeclaredConstant(id);
  554. if (!const_op) {
  555. constants.push_back(nullptr);
  556. missing_constants = true;
  557. } else {
  558. constants.push_back(const_op);
  559. }
  560. });
  561. const analysis::Constant* folded_const = nullptr;
  562. for (auto rule : GetConstantFoldingRules().GetRulesForInstruction(inst)) {
  563. folded_const = rule(context_, inst, constants);
  564. if (folded_const != nullptr) {
  565. Instruction* const_inst =
  566. const_mgr->GetDefiningInstruction(folded_const, inst->type_id());
  567. if (const_inst == nullptr) {
  568. return nullptr;
  569. }
  570. assert(const_inst->type_id() == inst->type_id());
  571. // May be a new instruction that needs to be analysed.
  572. context_->UpdateDefUse(const_inst);
  573. return const_inst;
  574. }
  575. }
  576. bool successful = false;
  577. // If all parameters are constant, fold the instruction to a constant.
  578. if (inst->IsFoldableByFoldScalar()) {
  579. uint32_t result_val = 0;
  580. if (!missing_constants) {
  581. result_val = FoldScalars(inst->opcode(), constants);
  582. successful = true;
  583. }
  584. if (!successful) {
  585. successful = FoldIntegerOpToConstant(inst, id_map, &result_val);
  586. }
  587. if (successful) {
  588. const analysis::Constant* result_const =
  589. const_mgr->GetConstant(const_mgr->GetType(inst), {result_val});
  590. Instruction* folded_inst =
  591. const_mgr->GetDefiningInstruction(result_const, inst->type_id());
  592. return folded_inst;
  593. }
  594. } else if (inst->IsFoldableByFoldVector()) {
  595. std::vector<uint32_t> result_val;
  596. if (!missing_constants) {
  597. if (Instruction* inst_type =
  598. context_->get_def_use_mgr()->GetDef(inst->type_id())) {
  599. result_val = FoldVectors(
  600. inst->opcode(), inst_type->GetSingleWordInOperand(1), constants);
  601. successful = true;
  602. }
  603. }
  604. if (successful) {
  605. const analysis::Constant* result_const =
  606. const_mgr->GetNumericVectorConstantWithWords(
  607. const_mgr->GetType(inst)->AsVector(), result_val);
  608. Instruction* folded_inst =
  609. const_mgr->GetDefiningInstruction(result_const, inst->type_id());
  610. return folded_inst;
  611. }
  612. }
  613. return nullptr;
  614. }
  615. bool InstructionFolder::IsFoldableType(Instruction* type_inst) const {
  616. return IsFoldableScalarType(type_inst) || IsFoldableVectorType(type_inst);
  617. }
  618. bool InstructionFolder::IsFoldableScalarType(Instruction* type_inst) const {
  619. // Support 32-bit integers.
  620. if (type_inst->opcode() == spv::Op::OpTypeInt) {
  621. return type_inst->GetSingleWordInOperand(0) == 32;
  622. }
  623. // Support booleans.
  624. if (type_inst->opcode() == spv::Op::OpTypeBool) {
  625. return true;
  626. }
  627. // Nothing else yet.
  628. return false;
  629. }
  630. bool InstructionFolder::IsFoldableVectorType(Instruction* type_inst) const {
  631. // Support vectors with foldable components
  632. if (type_inst->opcode() == spv::Op::OpTypeVector) {
  633. uint32_t component_type_id = type_inst->GetSingleWordInOperand(0);
  634. Instruction* def_component_type =
  635. context_->get_def_use_mgr()->GetDef(component_type_id);
  636. return def_component_type != nullptr &&
  637. IsFoldableScalarType(def_component_type);
  638. }
  639. // Nothing else yet.
  640. return false;
  641. }
  642. bool InstructionFolder::FoldInstruction(Instruction* inst) const {
  643. bool modified = false;
  644. Instruction* folded_inst(inst);
  645. while (folded_inst->opcode() != spv::Op::OpCopyObject &&
  646. FoldInstructionInternal(&*folded_inst)) {
  647. modified = true;
  648. }
  649. return modified;
  650. }
  651. } // namespace opt
  652. } // namespace spvtools