vector_dce.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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, live_components, &work_list);
  59. break;
  60. case SpvOpCompositeInsert:
  61. MarkInsertUsesAsLive(current_item, live_components, &work_list);
  62. break;
  63. case SpvOpVectorShuffle:
  64. MarkVectorShuffleUsesAsLive(current_item, live_components, &work_list);
  65. break;
  66. case SpvOpCompositeConstruct:
  67. MarkCompositeContructUsesAsLive(current_item, live_components,
  68. &work_list);
  69. break;
  70. default:
  71. if (current_inst->IsScalarizable()) {
  72. MarkUsesAsLive(current_inst, current_item.components, live_components,
  73. &work_list);
  74. } else {
  75. MarkUsesAsLive(current_inst, all_components_live_, live_components,
  76. &work_list);
  77. }
  78. break;
  79. }
  80. }
  81. }
  82. void VectorDCE::MarkExtractUseAsLive(const Instruction* current_inst,
  83. LiveComponentMap* live_components,
  84. std::vector<WorkListItem>* work_list) {
  85. analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
  86. uint32_t operand_id =
  87. current_inst->GetSingleWordInOperand(kExtractCompositeIdInIdx);
  88. Instruction* operand_inst = def_use_mgr->GetDef(operand_id);
  89. if (HasVectorOrScalarResult(operand_inst)) {
  90. WorkListItem new_item;
  91. new_item.instruction = operand_inst;
  92. new_item.components.Set(current_inst->GetSingleWordInOperand(1));
  93. AddItemToWorkListIfNeeded(new_item, live_components, work_list);
  94. }
  95. }
  96. void VectorDCE::MarkInsertUsesAsLive(
  97. const VectorDCE::WorkListItem& current_item,
  98. LiveComponentMap* live_components,
  99. std::vector<VectorDCE::WorkListItem>* work_list) {
  100. analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
  101. uint32_t insert_position =
  102. current_item.instruction->GetSingleWordInOperand(2);
  103. // Add the elements of the composite object that are used.
  104. uint32_t operand_id =
  105. current_item.instruction->GetSingleWordInOperand(kInsertCompositeIdInIdx);
  106. Instruction* operand_inst = def_use_mgr->GetDef(operand_id);
  107. WorkListItem new_item;
  108. new_item.instruction = operand_inst;
  109. new_item.components = current_item.components;
  110. new_item.components.Clear(insert_position);
  111. AddItemToWorkListIfNeeded(new_item, live_components, work_list);
  112. // Add the element being inserted if it is used.
  113. if (current_item.components.Get(insert_position)) {
  114. uint32_t obj_operand_id =
  115. current_item.instruction->GetSingleWordInOperand(kInsertObjectIdInIdx);
  116. Instruction* obj_operand_inst = def_use_mgr->GetDef(obj_operand_id);
  117. WorkListItem new_item_for_obj;
  118. new_item_for_obj.instruction = obj_operand_inst;
  119. new_item_for_obj.components.Set(0);
  120. AddItemToWorkListIfNeeded(new_item_for_obj, live_components, work_list);
  121. }
  122. }
  123. void VectorDCE::MarkVectorShuffleUsesAsLive(
  124. const WorkListItem& current_item,
  125. VectorDCE::LiveComponentMap* live_components,
  126. std::vector<WorkListItem>* work_list) {
  127. analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
  128. WorkListItem first_operand;
  129. first_operand.instruction =
  130. def_use_mgr->GetDef(current_item.instruction->GetSingleWordInOperand(0));
  131. WorkListItem second_operand;
  132. second_operand.instruction =
  133. def_use_mgr->GetDef(current_item.instruction->GetSingleWordInOperand(1));
  134. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  135. analysis::Vector* first_type =
  136. type_mgr->GetType(first_operand.instruction->type_id())->AsVector();
  137. uint32_t size_of_first_operand = first_type->element_count();
  138. for (uint32_t in_op = 2; in_op < current_item.instruction->NumInOperands();
  139. ++in_op) {
  140. uint32_t index = current_item.instruction->GetSingleWordInOperand(in_op);
  141. if (current_item.components.Get(in_op - 2)) {
  142. if (index < size_of_first_operand) {
  143. first_operand.components.Set(index);
  144. } else {
  145. second_operand.components.Set(index - size_of_first_operand);
  146. }
  147. }
  148. }
  149. AddItemToWorkListIfNeeded(first_operand, live_components, work_list);
  150. AddItemToWorkListIfNeeded(second_operand, live_components, work_list);
  151. }
  152. void VectorDCE::MarkCompositeContructUsesAsLive(
  153. VectorDCE::WorkListItem work_item,
  154. VectorDCE::LiveComponentMap* live_components,
  155. std::vector<VectorDCE::WorkListItem>* work_list) {
  156. analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
  157. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  158. uint32_t current_component = 0;
  159. Instruction* current_inst = work_item.instruction;
  160. uint32_t num_in_operands = current_inst->NumInOperands();
  161. for (uint32_t i = 0; i < num_in_operands; ++i) {
  162. uint32_t id = current_inst->GetSingleWordInOperand(i);
  163. Instruction* op_inst = def_use_mgr->GetDef(id);
  164. if (HasScalarResult(op_inst)) {
  165. WorkListItem new_work_item;
  166. new_work_item.instruction = op_inst;
  167. if (work_item.components.Get(current_component)) {
  168. new_work_item.components.Set(0);
  169. }
  170. AddItemToWorkListIfNeeded(new_work_item, live_components, work_list);
  171. current_component++;
  172. } else {
  173. assert(HasVectorResult(op_inst));
  174. WorkListItem new_work_item;
  175. new_work_item.instruction = op_inst;
  176. uint32_t op_vector_size =
  177. type_mgr->GetType(op_inst->type_id())->AsVector()->element_count();
  178. for (uint32_t op_vector_idx = 0; op_vector_idx < op_vector_size;
  179. op_vector_idx++, current_component++) {
  180. if (work_item.components.Get(current_component)) {
  181. new_work_item.components.Set(op_vector_idx);
  182. }
  183. }
  184. AddItemToWorkListIfNeeded(new_work_item, live_components, work_list);
  185. }
  186. }
  187. }
  188. void VectorDCE::MarkUsesAsLive(
  189. Instruction* current_inst, const utils::BitVector& live_elements,
  190. LiveComponentMap* live_components,
  191. std::vector<VectorDCE::WorkListItem>* work_list) {
  192. analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
  193. current_inst->ForEachInId([&work_list, &live_elements, this, live_components,
  194. def_use_mgr](uint32_t* operand_id) {
  195. Instruction* operand_inst = def_use_mgr->GetDef(*operand_id);
  196. if (HasVectorResult(operand_inst)) {
  197. WorkListItem new_item;
  198. new_item.instruction = operand_inst;
  199. new_item.components = live_elements;
  200. AddItemToWorkListIfNeeded(new_item, live_components, work_list);
  201. } else if (HasScalarResult(operand_inst)) {
  202. WorkListItem new_item;
  203. new_item.instruction = operand_inst;
  204. new_item.components.Set(0);
  205. AddItemToWorkListIfNeeded(new_item, live_components, work_list);
  206. }
  207. });
  208. }
  209. bool VectorDCE::HasVectorOrScalarResult(const Instruction* inst) const {
  210. return HasScalarResult(inst) || HasVectorResult(inst);
  211. }
  212. bool VectorDCE::HasVectorResult(const Instruction* inst) const {
  213. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  214. if (inst->type_id() == 0) {
  215. return false;
  216. }
  217. const analysis::Type* current_type = type_mgr->GetType(inst->type_id());
  218. switch (current_type->kind()) {
  219. case analysis::Type::kVector:
  220. return true;
  221. default:
  222. return false;
  223. }
  224. }
  225. bool VectorDCE::HasScalarResult(const Instruction* inst) const {
  226. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  227. if (inst->type_id() == 0) {
  228. return false;
  229. }
  230. const analysis::Type* current_type = type_mgr->GetType(inst->type_id());
  231. switch (current_type->kind()) {
  232. case analysis::Type::kBool:
  233. case analysis::Type::kInteger:
  234. case analysis::Type::kFloat:
  235. return true;
  236. default:
  237. return false;
  238. }
  239. }
  240. bool VectorDCE::RewriteInstructions(
  241. Function* function, const VectorDCE::LiveComponentMap& live_components) {
  242. bool modified = false;
  243. function->ForEachInst(
  244. [&modified, this, live_components](Instruction* current_inst) {
  245. if (!context()->IsCombinatorInstruction(current_inst)) {
  246. return;
  247. }
  248. auto live_component = live_components.find(current_inst->result_id());
  249. if (live_component == live_components.end()) {
  250. // If this instruction is not in live_components then it does not
  251. // produce a vector, or it is never referenced and ADCE will remove
  252. // it. No point in trying to differentiate.
  253. return;
  254. }
  255. // If no element in the current instruction is used replace it with an
  256. // OpUndef.
  257. if (live_component->second.Empty()) {
  258. modified = true;
  259. uint32_t undef_id = this->Type2Undef(current_inst->type_id());
  260. context()->KillNamesAndDecorates(current_inst);
  261. context()->ReplaceAllUsesWith(current_inst->result_id(), undef_id);
  262. context()->KillInst(current_inst);
  263. return;
  264. }
  265. switch (current_inst->opcode()) {
  266. case SpvOpCompositeInsert:
  267. modified |=
  268. RewriteInsertInstruction(current_inst, live_component->second);
  269. break;
  270. case SpvOpCompositeConstruct:
  271. // TODO: The members that are not live can be replaced by an undef
  272. // or constant. This will remove uses of those values, and possibly
  273. // create opportunities for ADCE.
  274. break;
  275. default:
  276. // Do nothing.
  277. break;
  278. }
  279. });
  280. return modified;
  281. }
  282. bool VectorDCE::RewriteInsertInstruction(
  283. Instruction* current_inst, const utils::BitVector& live_components) {
  284. // If the value being inserted is not live, then we can skip the insert.
  285. bool modified = false;
  286. uint32_t insert_index = current_inst->GetSingleWordInOperand(2);
  287. if (!live_components.Get(insert_index)) {
  288. modified = true;
  289. context()->KillNamesAndDecorates(current_inst->result_id());
  290. uint32_t composite_id =
  291. current_inst->GetSingleWordInOperand(kInsertCompositeIdInIdx);
  292. context()->ReplaceAllUsesWith(current_inst->result_id(), composite_id);
  293. }
  294. // If the values already in the composite are not used, then replace it with
  295. // an undef.
  296. utils::BitVector temp = live_components;
  297. temp.Clear(insert_index);
  298. if (temp.Empty()) {
  299. context()->ForgetUses(current_inst);
  300. uint32_t undef_id = Type2Undef(current_inst->type_id());
  301. current_inst->SetInOperand(kInsertCompositeIdInIdx, {undef_id});
  302. context()->AnalyzeUses(current_inst);
  303. }
  304. return modified;
  305. }
  306. void VectorDCE::AddItemToWorkListIfNeeded(
  307. WorkListItem work_item, VectorDCE::LiveComponentMap* live_components,
  308. std::vector<WorkListItem>* work_list) {
  309. Instruction* current_inst = work_item.instruction;
  310. auto it = live_components->find(current_inst->result_id());
  311. if (it == live_components->end()) {
  312. live_components->emplace(
  313. std::make_pair(current_inst->result_id(), work_item.components));
  314. work_list->emplace_back(work_item);
  315. } else {
  316. if (it->second.Or(work_item.components)) {
  317. work_list->emplace_back(work_item);
  318. }
  319. }
  320. }
  321. } // namespace opt
  322. } // namespace spvtools