vector_dce.cpp 15 KB

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