2
0

vector_dce.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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/vector_dce.h"
  15. #include <utility>
  16. namespace spvtools {
  17. namespace opt {
  18. namespace {
  19. const uint32_t kExtractCompositeIdInIdx = 0;
  20. const uint32_t kInsertObjectIdInIdx = 0;
  21. const uint32_t kInsertCompositeIdInIdx = 1;
  22. } // namespace
  23. Pass::Status VectorDCE::Process() {
  24. bool modified = false;
  25. for (Function& function : *get_module()) {
  26. modified |= VectorDCEFunction(&function);
  27. }
  28. return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
  29. }
  30. bool VectorDCE::VectorDCEFunction(Function* function) {
  31. LiveComponentMap live_components;
  32. FindLiveComponents(function, &live_components);
  33. return RewriteInstructions(function, live_components);
  34. }
  35. void VectorDCE::FindLiveComponents(Function* function,
  36. LiveComponentMap* live_components) {
  37. std::vector<WorkListItem> work_list;
  38. // Prime the work list. We will assume that any instruction that does
  39. // not result in a vector is live.
  40. //
  41. // Extending to structures and matrices is not as straight forward because of
  42. // the nesting. We cannot simply us a bit vector to keep track of which
  43. // components are live because of arbitrary nesting of structs.
  44. function->ForEachInst(
  45. [&work_list, this, live_components](Instruction* current_inst) {
  46. if (!HasVectorOrScalarResult(current_inst) ||
  47. !context()->IsCombinatorInstruction(current_inst)) {
  48. MarkUsesAsLive(current_inst, all_components_live_, live_components,
  49. &work_list);
  50. }
  51. });
  52. // Process the work list propagating liveness.
  53. for (uint32_t i = 0; i < work_list.size(); i++) {
  54. WorkListItem current_item = work_list[i];
  55. Instruction* current_inst = current_item.instruction;
  56. switch (current_inst->opcode()) {
  57. case SpvOpCompositeExtract:
  58. MarkExtractUseAsLive(current_inst, current_item.components,
  59. live_components, &work_list);
  60. break;
  61. case SpvOpCompositeInsert:
  62. MarkInsertUsesAsLive(current_item, live_components, &work_list);
  63. break;
  64. case SpvOpVectorShuffle:
  65. MarkVectorShuffleUsesAsLive(current_item, live_components, &work_list);
  66. break;
  67. case SpvOpCompositeConstruct:
  68. MarkCompositeContructUsesAsLive(current_item, live_components,
  69. &work_list);
  70. break;
  71. default:
  72. if (current_inst->IsScalarizable()) {
  73. MarkUsesAsLive(current_inst, current_item.components, live_components,
  74. &work_list);
  75. } else {
  76. MarkUsesAsLive(current_inst, all_components_live_, live_components,
  77. &work_list);
  78. }
  79. break;
  80. }
  81. }
  82. }
  83. void VectorDCE::MarkExtractUseAsLive(const Instruction* current_inst,
  84. const utils::BitVector& live_elements,
  85. LiveComponentMap* live_components,
  86. std::vector<WorkListItem>* work_list) {
  87. analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
  88. uint32_t operand_id =
  89. current_inst->GetSingleWordInOperand(kExtractCompositeIdInIdx);
  90. Instruction* operand_inst = def_use_mgr->GetDef(operand_id);
  91. if (HasVectorOrScalarResult(operand_inst)) {
  92. WorkListItem new_item;
  93. new_item.instruction = operand_inst;
  94. if (current_inst->NumInOperands() < 2) {
  95. new_item.components = live_elements;
  96. } else {
  97. new_item.components.Set(current_inst->GetSingleWordInOperand(1));
  98. }
  99. AddItemToWorkListIfNeeded(new_item, live_components, work_list);
  100. }
  101. }
  102. void VectorDCE::MarkInsertUsesAsLive(
  103. const VectorDCE::WorkListItem& current_item,
  104. LiveComponentMap* live_components,
  105. std::vector<VectorDCE::WorkListItem>* work_list) {
  106. analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
  107. if (current_item.instruction->NumInOperands() > 2) {
  108. uint32_t insert_position =
  109. current_item.instruction->GetSingleWordInOperand(2);
  110. // Add the elements of the composite object that are used.
  111. uint32_t operand_id = current_item.instruction->GetSingleWordInOperand(
  112. kInsertCompositeIdInIdx);
  113. Instruction* operand_inst = def_use_mgr->GetDef(operand_id);
  114. WorkListItem new_item;
  115. new_item.instruction = operand_inst;
  116. new_item.components = current_item.components;
  117. new_item.components.Clear(insert_position);
  118. AddItemToWorkListIfNeeded(new_item, live_components, work_list);
  119. // Add the element being inserted if it is used.
  120. if (current_item.components.Get(insert_position)) {
  121. uint32_t obj_operand_id =
  122. current_item.instruction->GetSingleWordInOperand(
  123. kInsertObjectIdInIdx);
  124. Instruction* obj_operand_inst = def_use_mgr->GetDef(obj_operand_id);
  125. WorkListItem new_item_for_obj;
  126. new_item_for_obj.instruction = obj_operand_inst;
  127. new_item_for_obj.components.Set(0);
  128. AddItemToWorkListIfNeeded(new_item_for_obj, live_components, work_list);
  129. }
  130. } else {
  131. // If there are no indices, then this is a copy of the object being
  132. // inserted.
  133. uint32_t object_id =
  134. current_item.instruction->GetSingleWordInOperand(kInsertObjectIdInIdx);
  135. Instruction* object_inst = def_use_mgr->GetDef(object_id);
  136. WorkListItem new_item;
  137. new_item.instruction = object_inst;
  138. new_item.components = current_item.components;
  139. AddItemToWorkListIfNeeded(new_item, live_components, work_list);
  140. }
  141. }
  142. void VectorDCE::MarkVectorShuffleUsesAsLive(
  143. const WorkListItem& current_item,
  144. VectorDCE::LiveComponentMap* live_components,
  145. std::vector<WorkListItem>* work_list) {
  146. analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
  147. WorkListItem first_operand;
  148. first_operand.instruction =
  149. def_use_mgr->GetDef(current_item.instruction->GetSingleWordInOperand(0));
  150. WorkListItem second_operand;
  151. second_operand.instruction =
  152. def_use_mgr->GetDef(current_item.instruction->GetSingleWordInOperand(1));
  153. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  154. analysis::Vector* first_type =
  155. type_mgr->GetType(first_operand.instruction->type_id())->AsVector();
  156. uint32_t size_of_first_operand = first_type->element_count();
  157. for (uint32_t in_op = 2; in_op < current_item.instruction->NumInOperands();
  158. ++in_op) {
  159. uint32_t index = current_item.instruction->GetSingleWordInOperand(in_op);
  160. if (current_item.components.Get(in_op - 2)) {
  161. if (index < size_of_first_operand) {
  162. first_operand.components.Set(index);
  163. } else {
  164. second_operand.components.Set(index - size_of_first_operand);
  165. }
  166. }
  167. }
  168. AddItemToWorkListIfNeeded(first_operand, live_components, work_list);
  169. AddItemToWorkListIfNeeded(second_operand, live_components, work_list);
  170. }
  171. void VectorDCE::MarkCompositeContructUsesAsLive(
  172. VectorDCE::WorkListItem work_item,
  173. VectorDCE::LiveComponentMap* live_components,
  174. std::vector<VectorDCE::WorkListItem>* work_list) {
  175. analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
  176. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  177. uint32_t current_component = 0;
  178. Instruction* current_inst = work_item.instruction;
  179. uint32_t num_in_operands = current_inst->NumInOperands();
  180. for (uint32_t i = 0; i < num_in_operands; ++i) {
  181. uint32_t id = current_inst->GetSingleWordInOperand(i);
  182. Instruction* op_inst = def_use_mgr->GetDef(id);
  183. if (HasScalarResult(op_inst)) {
  184. WorkListItem new_work_item;
  185. new_work_item.instruction = op_inst;
  186. if (work_item.components.Get(current_component)) {
  187. new_work_item.components.Set(0);
  188. }
  189. AddItemToWorkListIfNeeded(new_work_item, live_components, work_list);
  190. current_component++;
  191. } else {
  192. assert(HasVectorResult(op_inst));
  193. WorkListItem new_work_item;
  194. new_work_item.instruction = op_inst;
  195. uint32_t op_vector_size =
  196. type_mgr->GetType(op_inst->type_id())->AsVector()->element_count();
  197. for (uint32_t op_vector_idx = 0; op_vector_idx < op_vector_size;
  198. op_vector_idx++, current_component++) {
  199. if (work_item.components.Get(current_component)) {
  200. new_work_item.components.Set(op_vector_idx);
  201. }
  202. }
  203. AddItemToWorkListIfNeeded(new_work_item, live_components, work_list);
  204. }
  205. }
  206. }
  207. void VectorDCE::MarkUsesAsLive(
  208. Instruction* current_inst, const utils::BitVector& live_elements,
  209. LiveComponentMap* live_components,
  210. std::vector<VectorDCE::WorkListItem>* work_list) {
  211. analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
  212. current_inst->ForEachInId([&work_list, &live_elements, this, live_components,
  213. def_use_mgr](uint32_t* operand_id) {
  214. Instruction* operand_inst = def_use_mgr->GetDef(*operand_id);
  215. if (HasVectorResult(operand_inst)) {
  216. WorkListItem new_item;
  217. new_item.instruction = operand_inst;
  218. new_item.components = live_elements;
  219. AddItemToWorkListIfNeeded(new_item, live_components, work_list);
  220. } else if (HasScalarResult(operand_inst)) {
  221. WorkListItem new_item;
  222. new_item.instruction = operand_inst;
  223. new_item.components.Set(0);
  224. AddItemToWorkListIfNeeded(new_item, live_components, work_list);
  225. }
  226. });
  227. }
  228. bool VectorDCE::HasVectorOrScalarResult(const Instruction* inst) const {
  229. return HasScalarResult(inst) || HasVectorResult(inst);
  230. }
  231. bool VectorDCE::HasVectorResult(const Instruction* inst) const {
  232. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  233. if (inst->type_id() == 0) {
  234. return false;
  235. }
  236. const analysis::Type* current_type = type_mgr->GetType(inst->type_id());
  237. switch (current_type->kind()) {
  238. case analysis::Type::kVector:
  239. return true;
  240. default:
  241. return false;
  242. }
  243. }
  244. bool VectorDCE::HasScalarResult(const Instruction* inst) const {
  245. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  246. if (inst->type_id() == 0) {
  247. return false;
  248. }
  249. const analysis::Type* current_type = type_mgr->GetType(inst->type_id());
  250. switch (current_type->kind()) {
  251. case analysis::Type::kBool:
  252. case analysis::Type::kInteger:
  253. case analysis::Type::kFloat:
  254. return true;
  255. default:
  256. return false;
  257. }
  258. }
  259. bool VectorDCE::RewriteInstructions(
  260. Function* function, const VectorDCE::LiveComponentMap& live_components) {
  261. bool modified = false;
  262. function->ForEachInst(
  263. [&modified, this, live_components](Instruction* current_inst) {
  264. if (!context()->IsCombinatorInstruction(current_inst)) {
  265. return;
  266. }
  267. auto live_component = live_components.find(current_inst->result_id());
  268. if (live_component == live_components.end()) {
  269. // If this instruction is not in live_components then it does not
  270. // produce a vector, or it is never referenced and ADCE will remove
  271. // it. No point in trying to differentiate.
  272. return;
  273. }
  274. // If no element in the current instruction is used replace it with an
  275. // OpUndef.
  276. if (live_component->second.Empty()) {
  277. modified = true;
  278. uint32_t undef_id = this->Type2Undef(current_inst->type_id());
  279. context()->KillNamesAndDecorates(current_inst);
  280. context()->ReplaceAllUsesWith(current_inst->result_id(), undef_id);
  281. context()->KillInst(current_inst);
  282. return;
  283. }
  284. switch (current_inst->opcode()) {
  285. case SpvOpCompositeInsert:
  286. modified |=
  287. RewriteInsertInstruction(current_inst, live_component->second);
  288. break;
  289. case SpvOpCompositeConstruct:
  290. // TODO: The members that are not live can be replaced by an undef
  291. // or constant. This will remove uses of those values, and possibly
  292. // create opportunities for ADCE.
  293. break;
  294. default:
  295. // Do nothing.
  296. break;
  297. }
  298. });
  299. return modified;
  300. }
  301. bool VectorDCE::RewriteInsertInstruction(
  302. Instruction* current_inst, const utils::BitVector& live_components) {
  303. // If the value being inserted is not live, then we can skip the insert.
  304. if (current_inst->NumInOperands() == 2) {
  305. // If there are no indices, then this is the same as a copy.
  306. context()->KillNamesAndDecorates(current_inst->result_id());
  307. uint32_t object_id =
  308. current_inst->GetSingleWordInOperand(kInsertObjectIdInIdx);
  309. context()->ReplaceAllUsesWith(current_inst->result_id(), object_id);
  310. return true;
  311. }
  312. uint32_t insert_index = current_inst->GetSingleWordInOperand(2);
  313. if (!live_components.Get(insert_index)) {
  314. context()->KillNamesAndDecorates(current_inst->result_id());
  315. uint32_t composite_id =
  316. current_inst->GetSingleWordInOperand(kInsertCompositeIdInIdx);
  317. context()->ReplaceAllUsesWith(current_inst->result_id(), composite_id);
  318. return true;
  319. }
  320. // If the values already in the composite are not used, then replace it with
  321. // an undef.
  322. utils::BitVector temp = live_components;
  323. temp.Clear(insert_index);
  324. if (temp.Empty()) {
  325. context()->ForgetUses(current_inst);
  326. uint32_t undef_id = Type2Undef(current_inst->type_id());
  327. current_inst->SetInOperand(kInsertCompositeIdInIdx, {undef_id});
  328. context()->AnalyzeUses(current_inst);
  329. return true;
  330. }
  331. return false;
  332. }
  333. void VectorDCE::AddItemToWorkListIfNeeded(
  334. WorkListItem work_item, VectorDCE::LiveComponentMap* live_components,
  335. std::vector<WorkListItem>* work_list) {
  336. Instruction* current_inst = work_item.instruction;
  337. auto it = live_components->find(current_inst->result_id());
  338. if (it == live_components->end()) {
  339. live_components->emplace(
  340. std::make_pair(current_inst->result_id(), work_item.components));
  341. work_list->emplace_back(work_item);
  342. } else {
  343. if (it->second.Or(work_item.components)) {
  344. work_list->emplace_back(work_item);
  345. }
  346. }
  347. }
  348. } // namespace opt
  349. } // namespace spvtools