vector_dce.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. constexpr uint32_t kExtractCompositeIdInIdx = 0;
  20. constexpr uint32_t kInsertObjectIdInIdx = 0;
  21. constexpr 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 (current_inst->IsCommonDebugInstr()) {
  47. return;
  48. }
  49. if (!HasVectorOrScalarResult(current_inst) ||
  50. !context()->IsCombinatorInstruction(current_inst)) {
  51. MarkUsesAsLive(current_inst, all_components_live_, live_components,
  52. &work_list);
  53. }
  54. });
  55. // Process the work list propagating liveness.
  56. for (uint32_t i = 0; i < work_list.size(); i++) {
  57. WorkListItem current_item = work_list[i];
  58. Instruction* current_inst = current_item.instruction;
  59. switch (current_inst->opcode()) {
  60. case spv::Op::OpCompositeExtract:
  61. MarkExtractUseAsLive(current_inst, current_item.components,
  62. live_components, &work_list);
  63. break;
  64. case spv::Op::OpCompositeInsert:
  65. MarkInsertUsesAsLive(current_item, live_components, &work_list);
  66. break;
  67. case spv::Op::OpVectorShuffle:
  68. MarkVectorShuffleUsesAsLive(current_item, live_components, &work_list);
  69. break;
  70. case spv::Op::OpCompositeConstruct:
  71. MarkCompositeContructUsesAsLive(current_item, live_components,
  72. &work_list);
  73. break;
  74. default:
  75. if (current_inst->IsScalarizable()) {
  76. MarkUsesAsLive(current_inst, current_item.components, live_components,
  77. &work_list);
  78. } else {
  79. MarkUsesAsLive(current_inst, all_components_live_, live_components,
  80. &work_list);
  81. }
  82. break;
  83. }
  84. }
  85. }
  86. void VectorDCE::MarkExtractUseAsLive(const Instruction* current_inst,
  87. const utils::BitVector& live_elements,
  88. LiveComponentMap* live_components,
  89. std::vector<WorkListItem>* work_list) {
  90. analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
  91. uint32_t operand_id =
  92. current_inst->GetSingleWordInOperand(kExtractCompositeIdInIdx);
  93. Instruction* operand_inst = def_use_mgr->GetDef(operand_id);
  94. if (HasVectorOrScalarResult(operand_inst)) {
  95. WorkListItem new_item;
  96. new_item.instruction = operand_inst;
  97. if (current_inst->NumInOperands() < 2) {
  98. new_item.components = live_elements;
  99. } else {
  100. uint32_t element_index = current_inst->GetSingleWordInOperand(1);
  101. uint32_t item_size = GetVectorComponentCount(operand_inst->type_id());
  102. if (element_index < item_size) {
  103. new_item.components.Set(element_index);
  104. }
  105. }
  106. AddItemToWorkListIfNeeded(new_item, live_components, work_list);
  107. }
  108. }
  109. void VectorDCE::MarkInsertUsesAsLive(
  110. const VectorDCE::WorkListItem& current_item,
  111. LiveComponentMap* live_components,
  112. std::vector<VectorDCE::WorkListItem>* work_list) {
  113. analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
  114. if (current_item.instruction->NumInOperands() > 2) {
  115. uint32_t insert_position =
  116. current_item.instruction->GetSingleWordInOperand(2);
  117. // Add the elements of the composite object that are used.
  118. uint32_t operand_id = current_item.instruction->GetSingleWordInOperand(
  119. kInsertCompositeIdInIdx);
  120. Instruction* operand_inst = def_use_mgr->GetDef(operand_id);
  121. WorkListItem new_item;
  122. new_item.instruction = operand_inst;
  123. new_item.components = current_item.components;
  124. new_item.components.Clear(insert_position);
  125. AddItemToWorkListIfNeeded(new_item, live_components, work_list);
  126. // Add the element being inserted if it is used.
  127. if (current_item.components.Get(insert_position)) {
  128. uint32_t obj_operand_id =
  129. current_item.instruction->GetSingleWordInOperand(
  130. kInsertObjectIdInIdx);
  131. Instruction* obj_operand_inst = def_use_mgr->GetDef(obj_operand_id);
  132. WorkListItem new_item_for_obj;
  133. new_item_for_obj.instruction = obj_operand_inst;
  134. new_item_for_obj.components.Set(0);
  135. AddItemToWorkListIfNeeded(new_item_for_obj, live_components, work_list);
  136. }
  137. } else {
  138. // If there are no indices, then this is a copy of the object being
  139. // inserted.
  140. uint32_t object_id =
  141. current_item.instruction->GetSingleWordInOperand(kInsertObjectIdInIdx);
  142. Instruction* object_inst = def_use_mgr->GetDef(object_id);
  143. WorkListItem new_item;
  144. new_item.instruction = object_inst;
  145. new_item.components = current_item.components;
  146. AddItemToWorkListIfNeeded(new_item, live_components, work_list);
  147. }
  148. }
  149. void VectorDCE::MarkVectorShuffleUsesAsLive(
  150. const WorkListItem& current_item,
  151. VectorDCE::LiveComponentMap* live_components,
  152. std::vector<WorkListItem>* work_list) {
  153. analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
  154. WorkListItem first_operand;
  155. first_operand.instruction =
  156. def_use_mgr->GetDef(current_item.instruction->GetSingleWordInOperand(0));
  157. WorkListItem second_operand;
  158. second_operand.instruction =
  159. def_use_mgr->GetDef(current_item.instruction->GetSingleWordInOperand(1));
  160. uint32_t size_of_first_operand =
  161. GetVectorComponentCount(first_operand.instruction->type_id());
  162. uint32_t size_of_second_operand =
  163. GetVectorComponentCount(second_operand.instruction->type_id());
  164. for (uint32_t in_op = 2; in_op < current_item.instruction->NumInOperands();
  165. ++in_op) {
  166. uint32_t index = current_item.instruction->GetSingleWordInOperand(in_op);
  167. if (current_item.components.Get(in_op - 2)) {
  168. if (index < size_of_first_operand) {
  169. first_operand.components.Set(index);
  170. } else if (index - size_of_first_operand < size_of_second_operand) {
  171. second_operand.components.Set(index - size_of_first_operand);
  172. }
  173. }
  174. }
  175. AddItemToWorkListIfNeeded(first_operand, live_components, work_list);
  176. AddItemToWorkListIfNeeded(second_operand, live_components, work_list);
  177. }
  178. void VectorDCE::MarkCompositeContructUsesAsLive(
  179. VectorDCE::WorkListItem work_item,
  180. VectorDCE::LiveComponentMap* live_components,
  181. std::vector<VectorDCE::WorkListItem>* work_list) {
  182. analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
  183. uint32_t current_component = 0;
  184. Instruction* current_inst = work_item.instruction;
  185. uint32_t num_in_operands = current_inst->NumInOperands();
  186. for (uint32_t i = 0; i < num_in_operands; ++i) {
  187. uint32_t id = current_inst->GetSingleWordInOperand(i);
  188. Instruction* op_inst = def_use_mgr->GetDef(id);
  189. if (HasScalarResult(op_inst)) {
  190. WorkListItem new_work_item;
  191. new_work_item.instruction = op_inst;
  192. if (work_item.components.Get(current_component)) {
  193. new_work_item.components.Set(0);
  194. }
  195. AddItemToWorkListIfNeeded(new_work_item, live_components, work_list);
  196. current_component++;
  197. } else {
  198. assert(HasVectorResult(op_inst));
  199. WorkListItem new_work_item;
  200. new_work_item.instruction = op_inst;
  201. uint32_t op_vector_size = GetVectorComponentCount(op_inst->type_id());
  202. for (uint32_t op_vector_idx = 0; op_vector_idx < op_vector_size;
  203. op_vector_idx++, current_component++) {
  204. if (work_item.components.Get(current_component)) {
  205. new_work_item.components.Set(op_vector_idx);
  206. }
  207. }
  208. AddItemToWorkListIfNeeded(new_work_item, live_components, work_list);
  209. }
  210. }
  211. }
  212. void VectorDCE::MarkUsesAsLive(
  213. Instruction* current_inst, const utils::BitVector& live_elements,
  214. LiveComponentMap* live_components,
  215. std::vector<VectorDCE::WorkListItem>* work_list) {
  216. analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
  217. current_inst->ForEachInId([&work_list, &live_elements, this, live_components,
  218. def_use_mgr](uint32_t* operand_id) {
  219. Instruction* operand_inst = def_use_mgr->GetDef(*operand_id);
  220. if (HasVectorResult(operand_inst)) {
  221. WorkListItem new_item;
  222. new_item.instruction = operand_inst;
  223. new_item.components = live_elements;
  224. AddItemToWorkListIfNeeded(new_item, live_components, work_list);
  225. } else if (HasScalarResult(operand_inst)) {
  226. WorkListItem new_item;
  227. new_item.instruction = operand_inst;
  228. new_item.components.Set(0);
  229. AddItemToWorkListIfNeeded(new_item, live_components, work_list);
  230. }
  231. });
  232. }
  233. bool VectorDCE::HasVectorOrScalarResult(const Instruction* inst) const {
  234. return HasScalarResult(inst) || HasVectorResult(inst);
  235. }
  236. bool VectorDCE::HasVectorResult(const Instruction* inst) const {
  237. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  238. if (inst->type_id() == 0) {
  239. return false;
  240. }
  241. const analysis::Type* current_type = type_mgr->GetType(inst->type_id());
  242. switch (current_type->kind()) {
  243. case analysis::Type::kVector:
  244. return true;
  245. default:
  246. return false;
  247. }
  248. }
  249. bool VectorDCE::HasScalarResult(const Instruction* inst) const {
  250. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  251. if (inst->type_id() == 0) {
  252. return false;
  253. }
  254. const analysis::Type* current_type = type_mgr->GetType(inst->type_id());
  255. switch (current_type->kind()) {
  256. case analysis::Type::kBool:
  257. case analysis::Type::kInteger:
  258. case analysis::Type::kFloat:
  259. return true;
  260. default:
  261. return false;
  262. }
  263. }
  264. uint32_t VectorDCE::GetVectorComponentCount(uint32_t type_id) {
  265. assert(type_id != 0 &&
  266. "Trying to get the vector element count, but the type id is 0");
  267. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  268. const analysis::Type* type = type_mgr->GetType(type_id);
  269. const analysis::Vector* vector_type = type->AsVector();
  270. assert(
  271. vector_type &&
  272. "Trying to get the vector element count, but the type is not a vector");
  273. return vector_type->element_count();
  274. }
  275. bool VectorDCE::RewriteInstructions(
  276. Function* function, const VectorDCE::LiveComponentMap& live_components) {
  277. bool modified = false;
  278. // Kill DebugValue in the middle of the instruction iteration will result
  279. // in accessing a dangling pointer. We keep dead DebugValue instructions
  280. // in |dead_dbg_value| to kill them once after the iteration.
  281. std::vector<Instruction*> dead_dbg_value;
  282. function->ForEachInst([&modified, this, live_components,
  283. &dead_dbg_value](Instruction* current_inst) {
  284. if (!context()->IsCombinatorInstruction(current_inst)) {
  285. return;
  286. }
  287. auto live_component = live_components.find(current_inst->result_id());
  288. if (live_component == live_components.end()) {
  289. // If this instruction is not in live_components then it does not
  290. // produce a vector, or it is never referenced and ADCE will remove
  291. // it. No point in trying to differentiate.
  292. return;
  293. }
  294. // If no element in the current instruction is used replace it with an
  295. // OpUndef.
  296. if (live_component->second.Empty()) {
  297. modified = true;
  298. MarkDebugValueUsesAsDead(current_inst, &dead_dbg_value);
  299. uint32_t undef_id = this->Type2Undef(current_inst->type_id());
  300. context()->KillNamesAndDecorates(current_inst);
  301. context()->ReplaceAllUsesWith(current_inst->result_id(), undef_id);
  302. context()->KillInst(current_inst);
  303. return;
  304. }
  305. switch (current_inst->opcode()) {
  306. case spv::Op::OpCompositeInsert:
  307. modified |= RewriteInsertInstruction(
  308. current_inst, live_component->second, &dead_dbg_value);
  309. break;
  310. case spv::Op::OpCompositeConstruct:
  311. // TODO: The members that are not live can be replaced by an undef
  312. // or constant. This will remove uses of those values, and possibly
  313. // create opportunities for ADCE.
  314. break;
  315. default:
  316. // Do nothing.
  317. break;
  318. }
  319. });
  320. for (auto* i : dead_dbg_value) context()->KillInst(i);
  321. return modified;
  322. }
  323. bool VectorDCE::RewriteInsertInstruction(
  324. Instruction* current_inst, const utils::BitVector& live_components,
  325. std::vector<Instruction*>* dead_dbg_value) {
  326. // If the value being inserted is not live, then we can skip the insert.
  327. if (current_inst->NumInOperands() == 2) {
  328. // If there are no indices, then this is the same as a copy.
  329. context()->KillNamesAndDecorates(current_inst->result_id());
  330. uint32_t object_id =
  331. current_inst->GetSingleWordInOperand(kInsertObjectIdInIdx);
  332. context()->ReplaceAllUsesWith(current_inst->result_id(), object_id);
  333. return true;
  334. }
  335. uint32_t insert_index = current_inst->GetSingleWordInOperand(2);
  336. if (!live_components.Get(insert_index)) {
  337. MarkDebugValueUsesAsDead(current_inst, dead_dbg_value);
  338. context()->KillNamesAndDecorates(current_inst->result_id());
  339. uint32_t composite_id =
  340. current_inst->GetSingleWordInOperand(kInsertCompositeIdInIdx);
  341. context()->ReplaceAllUsesWith(current_inst->result_id(), composite_id);
  342. return true;
  343. }
  344. // If the values already in the composite are not used, then replace it with
  345. // an undef.
  346. utils::BitVector temp = live_components;
  347. temp.Clear(insert_index);
  348. if (temp.Empty()) {
  349. context()->ForgetUses(current_inst);
  350. uint32_t undef_id = Type2Undef(current_inst->type_id());
  351. current_inst->SetInOperand(kInsertCompositeIdInIdx, {undef_id});
  352. context()->AnalyzeUses(current_inst);
  353. return true;
  354. }
  355. return false;
  356. }
  357. void VectorDCE::MarkDebugValueUsesAsDead(
  358. Instruction* composite, std::vector<Instruction*>* dead_dbg_value) {
  359. context()->get_def_use_mgr()->ForEachUser(
  360. composite, [&dead_dbg_value](Instruction* use) {
  361. if (use->GetCommonDebugOpcode() == CommonDebugInfoDebugValue)
  362. dead_dbg_value->push_back(use);
  363. });
  364. }
  365. void VectorDCE::AddItemToWorkListIfNeeded(
  366. WorkListItem work_item, VectorDCE::LiveComponentMap* live_components,
  367. std::vector<WorkListItem>* work_list) {
  368. Instruction* current_inst = work_item.instruction;
  369. auto it = live_components->find(current_inst->result_id());
  370. if (it == live_components->end()) {
  371. live_components->emplace(
  372. std::make_pair(current_inst->result_id(), work_item.components));
  373. work_list->emplace_back(work_item);
  374. } else {
  375. if (it->second.Or(work_item.components)) {
  376. work_list->emplace_back(work_item);
  377. }
  378. }
  379. }
  380. } // namespace opt
  381. } // namespace spvtools