aggressive_dead_code_elim_pass.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. // Copyright (c) 2017 The Khronos Group Inc.
  2. // Copyright (c) 2017 Valve Corporation
  3. // Copyright (c) 2017 LunarG Inc.
  4. // Copyright (c) 2018 Google LLC
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License");
  7. // you may not use this file except in compliance with the License.
  8. // You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. #include "source/opt/aggressive_dead_code_elim_pass.h"
  18. #include <memory>
  19. #include <stack>
  20. #include "source/cfa.h"
  21. #include "source/latest_version_glsl_std_450_header.h"
  22. #include "source/opt/iterator.h"
  23. #include "source/opt/reflect.h"
  24. namespace spvtools {
  25. namespace opt {
  26. namespace {
  27. const uint32_t kTypePointerStorageClassInIdx = 0;
  28. const uint32_t kEntryPointFunctionIdInIdx = 1;
  29. const uint32_t kSelectionMergeMergeBlockIdInIdx = 0;
  30. const uint32_t kLoopMergeMergeBlockIdInIdx = 0;
  31. const uint32_t kLoopMergeContinueBlockIdInIdx = 1;
  32. const uint32_t kCopyMemoryTargetAddrInIdx = 0;
  33. const uint32_t kCopyMemorySourceAddrInIdx = 1;
  34. // Sorting functor to present annotation instructions in an easy-to-process
  35. // order. The functor orders by opcode first and falls back on unique id
  36. // ordering if both instructions have the same opcode.
  37. //
  38. // Desired priority:
  39. // SpvOpGroupDecorate
  40. // SpvOpGroupMemberDecorate
  41. // SpvOpDecorate
  42. // SpvOpMemberDecorate
  43. // SpvOpDecorateId
  44. // SpvOpDecorateStringGOOGLE
  45. // SpvOpDecorationGroup
  46. struct DecorationLess {
  47. bool operator()(const Instruction* lhs, const Instruction* rhs) const {
  48. assert(lhs && rhs);
  49. SpvOp lhsOp = lhs->opcode();
  50. SpvOp rhsOp = rhs->opcode();
  51. if (lhsOp != rhsOp) {
  52. #define PRIORITY_CASE(opcode) \
  53. if (lhsOp == opcode && rhsOp != opcode) return true; \
  54. if (rhsOp == opcode && lhsOp != opcode) return false;
  55. // OpGroupDecorate and OpGroupMember decorate are highest priority to
  56. // eliminate dead targets early and simplify subsequent checks.
  57. PRIORITY_CASE(SpvOpGroupDecorate)
  58. PRIORITY_CASE(SpvOpGroupMemberDecorate)
  59. PRIORITY_CASE(SpvOpDecorate)
  60. PRIORITY_CASE(SpvOpMemberDecorate)
  61. PRIORITY_CASE(SpvOpDecorateId)
  62. PRIORITY_CASE(SpvOpDecorateStringGOOGLE)
  63. // OpDecorationGroup is lowest priority to ensure use/def chains remain
  64. // usable for instructions that target this group.
  65. PRIORITY_CASE(SpvOpDecorationGroup)
  66. #undef PRIORITY_CASE
  67. }
  68. // Fall back to maintain total ordering (compare unique ids).
  69. return *lhs < *rhs;
  70. }
  71. };
  72. } // namespace
  73. bool AggressiveDCEPass::IsVarOfStorage(uint32_t varId, uint32_t storageClass) {
  74. if (varId == 0) return false;
  75. const Instruction* varInst = get_def_use_mgr()->GetDef(varId);
  76. const SpvOp op = varInst->opcode();
  77. if (op != SpvOpVariable) return false;
  78. const uint32_t varTypeId = varInst->type_id();
  79. const Instruction* varTypeInst = get_def_use_mgr()->GetDef(varTypeId);
  80. if (varTypeInst->opcode() != SpvOpTypePointer) return false;
  81. return varTypeInst->GetSingleWordInOperand(kTypePointerStorageClassInIdx) ==
  82. storageClass;
  83. }
  84. bool AggressiveDCEPass::IsLocalVar(uint32_t varId) {
  85. if (IsVarOfStorage(varId, SpvStorageClassFunction)) {
  86. return true;
  87. }
  88. if (!private_like_local_) {
  89. return false;
  90. }
  91. return IsVarOfStorage(varId, SpvStorageClassPrivate) ||
  92. IsVarOfStorage(varId, SpvStorageClassWorkgroup);
  93. }
  94. void AggressiveDCEPass::AddStores(uint32_t ptrId) {
  95. get_def_use_mgr()->ForEachUser(ptrId, [this, ptrId](Instruction* user) {
  96. switch (user->opcode()) {
  97. case SpvOpAccessChain:
  98. case SpvOpInBoundsAccessChain:
  99. case SpvOpCopyObject:
  100. this->AddStores(user->result_id());
  101. break;
  102. case SpvOpLoad:
  103. break;
  104. case SpvOpCopyMemory:
  105. case SpvOpCopyMemorySized:
  106. if (user->GetSingleWordInOperand(kCopyMemoryTargetAddrInIdx) == ptrId) {
  107. AddToWorklist(user);
  108. }
  109. break;
  110. // If default, assume it stores e.g. frexp, modf, function call
  111. case SpvOpStore:
  112. default:
  113. AddToWorklist(user);
  114. break;
  115. }
  116. });
  117. }
  118. bool AggressiveDCEPass::AllExtensionsSupported() const {
  119. // If any extension not in whitelist, return false
  120. for (auto& ei : get_module()->extensions()) {
  121. const char* extName =
  122. reinterpret_cast<const char*>(&ei.GetInOperand(0).words[0]);
  123. if (extensions_whitelist_.find(extName) == extensions_whitelist_.end())
  124. return false;
  125. }
  126. return true;
  127. }
  128. bool AggressiveDCEPass::IsDead(Instruction* inst) {
  129. if (IsLive(inst)) return false;
  130. if ((inst->IsBranch() || inst->opcode() == SpvOpUnreachable) &&
  131. !IsStructuredHeader(context()->get_instr_block(inst), nullptr, nullptr,
  132. nullptr))
  133. return false;
  134. return true;
  135. }
  136. bool AggressiveDCEPass::IsTargetDead(Instruction* inst) {
  137. const uint32_t tId = inst->GetSingleWordInOperand(0);
  138. Instruction* tInst = get_def_use_mgr()->GetDef(tId);
  139. if (IsAnnotationInst(tInst->opcode())) {
  140. // This must be a decoration group. We go through annotations in a specific
  141. // order. So if this is not used by any group or group member decorates, it
  142. // is dead.
  143. assert(tInst->opcode() == SpvOpDecorationGroup);
  144. bool dead = true;
  145. get_def_use_mgr()->ForEachUser(tInst, [&dead](Instruction* user) {
  146. if (user->opcode() == SpvOpGroupDecorate ||
  147. user->opcode() == SpvOpGroupMemberDecorate)
  148. dead = false;
  149. });
  150. return dead;
  151. }
  152. return IsDead(tInst);
  153. }
  154. void AggressiveDCEPass::ProcessLoad(uint32_t varId) {
  155. // Only process locals
  156. if (!IsLocalVar(varId)) return;
  157. // Return if already processed
  158. if (live_local_vars_.find(varId) != live_local_vars_.end()) return;
  159. // Mark all stores to varId as live
  160. AddStores(varId);
  161. // Cache varId as processed
  162. live_local_vars_.insert(varId);
  163. }
  164. bool AggressiveDCEPass::IsStructuredHeader(BasicBlock* bp,
  165. Instruction** mergeInst,
  166. Instruction** branchInst,
  167. uint32_t* mergeBlockId) {
  168. if (!bp) return false;
  169. Instruction* mi = bp->GetMergeInst();
  170. if (mi == nullptr) return false;
  171. Instruction* bri = &*bp->tail();
  172. if (branchInst != nullptr) *branchInst = bri;
  173. if (mergeInst != nullptr) *mergeInst = mi;
  174. if (mergeBlockId != nullptr) *mergeBlockId = mi->GetSingleWordInOperand(0);
  175. return true;
  176. }
  177. void AggressiveDCEPass::ComputeBlock2HeaderMaps(
  178. std::list<BasicBlock*>& structuredOrder) {
  179. block2headerBranch_.clear();
  180. header2nextHeaderBranch_.clear();
  181. branch2merge_.clear();
  182. structured_order_index_.clear();
  183. std::stack<Instruction*> currentHeaderBranch;
  184. currentHeaderBranch.push(nullptr);
  185. uint32_t currentMergeBlockId = 0;
  186. uint32_t index = 0;
  187. for (auto bi = structuredOrder.begin(); bi != structuredOrder.end();
  188. ++bi, ++index) {
  189. structured_order_index_[*bi] = index;
  190. // If this block is the merge block of the current control construct,
  191. // we are leaving the current construct so we must update state
  192. if ((*bi)->id() == currentMergeBlockId) {
  193. currentHeaderBranch.pop();
  194. Instruction* chb = currentHeaderBranch.top();
  195. if (chb != nullptr)
  196. currentMergeBlockId = branch2merge_[chb]->GetSingleWordInOperand(0);
  197. }
  198. Instruction* mergeInst;
  199. Instruction* branchInst;
  200. uint32_t mergeBlockId;
  201. bool is_header =
  202. IsStructuredHeader(*bi, &mergeInst, &branchInst, &mergeBlockId);
  203. // Map header block to next enclosing header.
  204. if (is_header) header2nextHeaderBranch_[*bi] = currentHeaderBranch.top();
  205. // If this is a loop header, update state first so the block will map to
  206. // itself.
  207. if (is_header && mergeInst->opcode() == SpvOpLoopMerge) {
  208. currentHeaderBranch.push(branchInst);
  209. branch2merge_[branchInst] = mergeInst;
  210. currentMergeBlockId = mergeBlockId;
  211. }
  212. // Map the block to the current construct.
  213. block2headerBranch_[*bi] = currentHeaderBranch.top();
  214. // If this is an if header, update state so following blocks map to the if.
  215. if (is_header && mergeInst->opcode() == SpvOpSelectionMerge) {
  216. currentHeaderBranch.push(branchInst);
  217. branch2merge_[branchInst] = mergeInst;
  218. currentMergeBlockId = mergeBlockId;
  219. }
  220. }
  221. }
  222. void AggressiveDCEPass::AddBranch(uint32_t labelId, BasicBlock* bp) {
  223. std::unique_ptr<Instruction> newBranch(
  224. new Instruction(context(), SpvOpBranch, 0, 0,
  225. {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {labelId}}}));
  226. context()->AnalyzeDefUse(&*newBranch);
  227. context()->set_instr_block(&*newBranch, bp);
  228. bp->AddInstruction(std::move(newBranch));
  229. }
  230. void AggressiveDCEPass::AddBreaksAndContinuesToWorklist(
  231. Instruction* mergeInst) {
  232. assert(mergeInst->opcode() == SpvOpSelectionMerge ||
  233. mergeInst->opcode() == SpvOpLoopMerge);
  234. BasicBlock* header = context()->get_instr_block(mergeInst);
  235. uint32_t headerIndex = structured_order_index_[header];
  236. const uint32_t mergeId = mergeInst->GetSingleWordInOperand(0);
  237. BasicBlock* merge = context()->get_instr_block(mergeId);
  238. uint32_t mergeIndex = structured_order_index_[merge];
  239. get_def_use_mgr()->ForEachUser(
  240. mergeId, [headerIndex, mergeIndex, this](Instruction* user) {
  241. if (!user->IsBranch()) return;
  242. BasicBlock* block = context()->get_instr_block(user);
  243. uint32_t index = structured_order_index_[block];
  244. if (headerIndex < index && index < mergeIndex) {
  245. // This is a break from the loop.
  246. AddToWorklist(user);
  247. // Add branch's merge if there is one.
  248. Instruction* userMerge = branch2merge_[user];
  249. if (userMerge != nullptr) AddToWorklist(userMerge);
  250. }
  251. });
  252. if (mergeInst->opcode() != SpvOpLoopMerge) {
  253. return;
  254. }
  255. // For loops we need to find the continues as well.
  256. const uint32_t contId =
  257. mergeInst->GetSingleWordInOperand(kLoopMergeContinueBlockIdInIdx);
  258. get_def_use_mgr()->ForEachUser(contId, [&contId, this](Instruction* user) {
  259. SpvOp op = user->opcode();
  260. if (op == SpvOpBranchConditional || op == SpvOpSwitch) {
  261. // A conditional branch or switch can only be a continue if it does not
  262. // have a merge instruction or its merge block is not the continue block.
  263. Instruction* hdrMerge = branch2merge_[user];
  264. if (hdrMerge != nullptr && hdrMerge->opcode() == SpvOpSelectionMerge) {
  265. uint32_t hdrMergeId =
  266. hdrMerge->GetSingleWordInOperand(kSelectionMergeMergeBlockIdInIdx);
  267. if (hdrMergeId == contId) return;
  268. // Need to mark merge instruction too
  269. AddToWorklist(hdrMerge);
  270. }
  271. } else if (op == SpvOpBranch) {
  272. // An unconditional branch can only be a continue if it is not
  273. // branching to its own merge block.
  274. BasicBlock* blk = context()->get_instr_block(user);
  275. Instruction* hdrBranch = block2headerBranch_[blk];
  276. if (hdrBranch == nullptr) return;
  277. Instruction* hdrMerge = branch2merge_[hdrBranch];
  278. if (hdrMerge->opcode() == SpvOpLoopMerge) return;
  279. uint32_t hdrMergeId =
  280. hdrMerge->GetSingleWordInOperand(kSelectionMergeMergeBlockIdInIdx);
  281. if (contId == hdrMergeId) return;
  282. } else {
  283. return;
  284. }
  285. AddToWorklist(user);
  286. });
  287. }
  288. bool AggressiveDCEPass::AggressiveDCE(Function* func) {
  289. // Mark function parameters as live.
  290. AddToWorklist(&func->DefInst());
  291. func->ForEachParam(
  292. [this](const Instruction* param) {
  293. AddToWorklist(const_cast<Instruction*>(param));
  294. },
  295. false);
  296. // Compute map from block to controlling conditional branch
  297. std::list<BasicBlock*> structuredOrder;
  298. cfg()->ComputeStructuredOrder(func, &*func->begin(), &structuredOrder);
  299. ComputeBlock2HeaderMaps(structuredOrder);
  300. bool modified = false;
  301. // Add instructions with external side effects to worklist. Also add branches
  302. // EXCEPT those immediately contained in an "if" selection construct or a loop
  303. // or continue construct.
  304. // TODO(greg-lunarg): Handle Frexp, Modf more optimally
  305. call_in_func_ = false;
  306. func_is_entry_point_ = false;
  307. private_stores_.clear();
  308. // Stacks to keep track of when we are inside an if- or loop-construct.
  309. // When immediately inside an if- or loop-construct, we do not initially
  310. // mark branches live. All other branches must be marked live.
  311. std::stack<bool> assume_branches_live;
  312. std::stack<uint32_t> currentMergeBlockId;
  313. // Push sentinel values on stack for when outside of any control flow.
  314. assume_branches_live.push(true);
  315. currentMergeBlockId.push(0);
  316. for (auto bi = structuredOrder.begin(); bi != structuredOrder.end(); ++bi) {
  317. // If exiting if or loop, update stacks
  318. if ((*bi)->id() == currentMergeBlockId.top()) {
  319. assume_branches_live.pop();
  320. currentMergeBlockId.pop();
  321. }
  322. for (auto ii = (*bi)->begin(); ii != (*bi)->end(); ++ii) {
  323. SpvOp op = ii->opcode();
  324. switch (op) {
  325. case SpvOpStore: {
  326. uint32_t varId;
  327. (void)GetPtr(&*ii, &varId);
  328. // Mark stores as live if their variable is not function scope
  329. // and is not private scope. Remember private stores for possible
  330. // later inclusion. We cannot call IsLocalVar at this point because
  331. // private_like_local_ has not been set yet.
  332. if (IsVarOfStorage(varId, SpvStorageClassPrivate) ||
  333. IsVarOfStorage(varId, SpvStorageClassWorkgroup))
  334. private_stores_.push_back(&*ii);
  335. else if (!IsVarOfStorage(varId, SpvStorageClassFunction))
  336. AddToWorklist(&*ii);
  337. } break;
  338. case SpvOpCopyMemory:
  339. case SpvOpCopyMemorySized: {
  340. uint32_t varId;
  341. (void)GetPtr(ii->GetSingleWordInOperand(kCopyMemoryTargetAddrInIdx),
  342. &varId);
  343. if (IsVarOfStorage(varId, SpvStorageClassPrivate) ||
  344. IsVarOfStorage(varId, SpvStorageClassWorkgroup))
  345. private_stores_.push_back(&*ii);
  346. else if (!IsVarOfStorage(varId, SpvStorageClassFunction))
  347. AddToWorklist(&*ii);
  348. } break;
  349. case SpvOpLoopMerge: {
  350. assume_branches_live.push(false);
  351. currentMergeBlockId.push(
  352. ii->GetSingleWordInOperand(kLoopMergeMergeBlockIdInIdx));
  353. } break;
  354. case SpvOpSelectionMerge: {
  355. assume_branches_live.push(false);
  356. currentMergeBlockId.push(
  357. ii->GetSingleWordInOperand(kSelectionMergeMergeBlockIdInIdx));
  358. } break;
  359. case SpvOpSwitch:
  360. case SpvOpBranch:
  361. case SpvOpBranchConditional:
  362. case SpvOpUnreachable: {
  363. if (assume_branches_live.top()) {
  364. AddToWorklist(&*ii);
  365. }
  366. } break;
  367. default: {
  368. // Function calls, atomics, function params, function returns, etc.
  369. // TODO(greg-lunarg): function calls live only if write to non-local
  370. if (!ii->IsOpcodeSafeToDelete()) {
  371. AddToWorklist(&*ii);
  372. }
  373. // Remember function calls
  374. if (op == SpvOpFunctionCall) call_in_func_ = true;
  375. } break;
  376. }
  377. }
  378. }
  379. // See if current function is an entry point
  380. for (auto& ei : get_module()->entry_points()) {
  381. if (ei.GetSingleWordInOperand(kEntryPointFunctionIdInIdx) ==
  382. func->result_id()) {
  383. func_is_entry_point_ = true;
  384. break;
  385. }
  386. }
  387. // If the current function is an entry point and has no function calls,
  388. // we can optimize private variables as locals
  389. private_like_local_ = func_is_entry_point_ && !call_in_func_;
  390. // If privates are not like local, add their stores to worklist
  391. if (!private_like_local_)
  392. for (auto& ps : private_stores_) AddToWorklist(ps);
  393. // Perform closure on live instruction set.
  394. while (!worklist_.empty()) {
  395. Instruction* liveInst = worklist_.front();
  396. // Add all operand instructions if not already live
  397. liveInst->ForEachInId([&liveInst, this](const uint32_t* iid) {
  398. Instruction* inInst = get_def_use_mgr()->GetDef(*iid);
  399. // Do not add label if an operand of a branch. This is not needed
  400. // as part of live code discovery and can create false live code,
  401. // for example, the branch to a header of a loop.
  402. if (inInst->opcode() == SpvOpLabel && liveInst->IsBranch()) return;
  403. AddToWorklist(inInst);
  404. });
  405. if (liveInst->type_id() != 0) {
  406. AddToWorklist(get_def_use_mgr()->GetDef(liveInst->type_id()));
  407. }
  408. // If in a structured if or loop construct, add the controlling
  409. // conditional branch and its merge.
  410. BasicBlock* blk = context()->get_instr_block(liveInst);
  411. Instruction* branchInst = block2headerBranch_[blk];
  412. if (branchInst != nullptr) {
  413. AddToWorklist(branchInst);
  414. Instruction* mergeInst = branch2merge_[branchInst];
  415. AddToWorklist(mergeInst);
  416. }
  417. // If the block is a header, add the next outermost controlling
  418. // conditional branch and its merge.
  419. Instruction* nextBranchInst = header2nextHeaderBranch_[blk];
  420. if (nextBranchInst != nullptr) {
  421. AddToWorklist(nextBranchInst);
  422. Instruction* mergeInst = branch2merge_[nextBranchInst];
  423. AddToWorklist(mergeInst);
  424. }
  425. // If local load, add all variable's stores if variable not already live
  426. if (liveInst->opcode() == SpvOpLoad || liveInst->IsAtomicWithLoad()) {
  427. uint32_t varId;
  428. (void)GetPtr(liveInst, &varId);
  429. if (varId != 0) {
  430. ProcessLoad(varId);
  431. }
  432. // Process memory copies like loads
  433. } else if (liveInst->opcode() == SpvOpCopyMemory ||
  434. liveInst->opcode() == SpvOpCopyMemorySized) {
  435. uint32_t varId;
  436. (void)GetPtr(liveInst->GetSingleWordInOperand(kCopyMemorySourceAddrInIdx),
  437. &varId);
  438. if (varId != 0) {
  439. ProcessLoad(varId);
  440. }
  441. // If merge, add other branches that are part of its control structure
  442. } else if (liveInst->opcode() == SpvOpLoopMerge ||
  443. liveInst->opcode() == SpvOpSelectionMerge) {
  444. AddBreaksAndContinuesToWorklist(liveInst);
  445. // If function call, treat as if it loads from all pointer arguments
  446. } else if (liveInst->opcode() == SpvOpFunctionCall) {
  447. liveInst->ForEachInId([this](const uint32_t* iid) {
  448. // Skip non-ptr args
  449. if (!IsPtr(*iid)) return;
  450. uint32_t varId;
  451. (void)GetPtr(*iid, &varId);
  452. ProcessLoad(varId);
  453. });
  454. // If function parameter, treat as if it's result id is loaded from
  455. } else if (liveInst->opcode() == SpvOpFunctionParameter) {
  456. ProcessLoad(liveInst->result_id());
  457. // We treat an OpImageTexelPointer as a load of the pointer, and
  458. // that value is manipulated to get the result.
  459. } else if (liveInst->opcode() == SpvOpImageTexelPointer) {
  460. uint32_t varId;
  461. (void)GetPtr(liveInst, &varId);
  462. if (varId != 0) {
  463. ProcessLoad(varId);
  464. }
  465. }
  466. worklist_.pop();
  467. }
  468. // Kill dead instructions and remember dead blocks
  469. for (auto bi = structuredOrder.begin(); bi != structuredOrder.end();) {
  470. uint32_t mergeBlockId = 0;
  471. (*bi)->ForEachInst([this, &modified, &mergeBlockId](Instruction* inst) {
  472. if (!IsDead(inst)) return;
  473. if (inst->opcode() == SpvOpLabel) return;
  474. // If dead instruction is selection merge, remember merge block
  475. // for new branch at end of block
  476. if (inst->opcode() == SpvOpSelectionMerge ||
  477. inst->opcode() == SpvOpLoopMerge)
  478. mergeBlockId = inst->GetSingleWordInOperand(0);
  479. to_kill_.push_back(inst);
  480. modified = true;
  481. });
  482. // If a structured if or loop was deleted, add a branch to its merge
  483. // block, and traverse to the merge block and continue processing there.
  484. // We know the block still exists because the label is not deleted.
  485. if (mergeBlockId != 0) {
  486. AddBranch(mergeBlockId, *bi);
  487. for (++bi; (*bi)->id() != mergeBlockId; ++bi) {
  488. }
  489. } else {
  490. ++bi;
  491. }
  492. }
  493. return modified;
  494. }
  495. void AggressiveDCEPass::InitializeModuleScopeLiveInstructions() {
  496. // Keep all execution modes.
  497. for (auto& exec : get_module()->execution_modes()) {
  498. AddToWorklist(&exec);
  499. }
  500. // Keep all entry points.
  501. for (auto& entry : get_module()->entry_points()) {
  502. AddToWorklist(&entry);
  503. }
  504. // Keep workgroup size.
  505. for (auto& anno : get_module()->annotations()) {
  506. if (anno.opcode() == SpvOpDecorate) {
  507. if (anno.GetSingleWordInOperand(1u) == SpvDecorationBuiltIn &&
  508. anno.GetSingleWordInOperand(2u) == SpvBuiltInWorkgroupSize) {
  509. AddToWorklist(&anno);
  510. }
  511. }
  512. }
  513. }
  514. Pass::Status AggressiveDCEPass::ProcessImpl() {
  515. // Current functionality assumes shader capability
  516. // TODO(greg-lunarg): Handle additional capabilities
  517. if (!context()->get_feature_mgr()->HasCapability(SpvCapabilityShader))
  518. return Status::SuccessWithoutChange;
  519. // Current functionality assumes relaxed logical addressing (see
  520. // instruction.h)
  521. // TODO(greg-lunarg): Handle non-logical addressing
  522. if (context()->get_feature_mgr()->HasCapability(SpvCapabilityAddresses))
  523. return Status::SuccessWithoutChange;
  524. // If any extensions in the module are not explicitly supported,
  525. // return unmodified.
  526. if (!AllExtensionsSupported()) return Status::SuccessWithoutChange;
  527. // If the decoration manager is kept live then the context will try to keep it
  528. // up to date. ADCE deals with group decorations by changing the operands in
  529. // |OpGroupDecorate| instruction directly without informing the decoration
  530. // manager. This can put it in an invalid state which will cause an error
  531. // when the context tries to update it. To avoid this problem invalidate
  532. // the decoration manager upfront.
  533. context()->InvalidateAnalyses(IRContext::Analysis::kAnalysisDecorations);
  534. // Eliminate Dead functions.
  535. bool modified = EliminateDeadFunctions();
  536. InitializeModuleScopeLiveInstructions();
  537. // Process all entry point functions.
  538. ProcessFunction pfn = [this](Function* fp) { return AggressiveDCE(fp); };
  539. modified |= context()->ProcessEntryPointCallTree(pfn);
  540. // Process module-level instructions. Now that all live instructions have
  541. // been marked, it is safe to remove dead global values.
  542. modified |= ProcessGlobalValues();
  543. // Kill all dead instructions.
  544. for (auto inst : to_kill_) {
  545. context()->KillInst(inst);
  546. }
  547. // Cleanup all CFG including all unreachable blocks.
  548. ProcessFunction cleanup = [this](Function* f) { return CFGCleanup(f); };
  549. modified |= context()->ProcessEntryPointCallTree(cleanup);
  550. return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
  551. }
  552. bool AggressiveDCEPass::EliminateDeadFunctions() {
  553. // Identify live functions first. Those that are not live
  554. // are dead. ADCE is disabled for non-shaders so we do not check for exported
  555. // functions here.
  556. std::unordered_set<const Function*> live_function_set;
  557. ProcessFunction mark_live = [&live_function_set](Function* fp) {
  558. live_function_set.insert(fp);
  559. return false;
  560. };
  561. context()->ProcessEntryPointCallTree(mark_live);
  562. bool modified = false;
  563. for (auto funcIter = get_module()->begin();
  564. funcIter != get_module()->end();) {
  565. if (live_function_set.count(&*funcIter) == 0) {
  566. modified = true;
  567. EliminateFunction(&*funcIter);
  568. funcIter = funcIter.Erase();
  569. } else {
  570. ++funcIter;
  571. }
  572. }
  573. return modified;
  574. }
  575. void AggressiveDCEPass::EliminateFunction(Function* func) {
  576. // Remove all of the instruction in the function body
  577. func->ForEachInst([this](Instruction* inst) { context()->KillInst(inst); },
  578. true);
  579. }
  580. bool AggressiveDCEPass::ProcessGlobalValues() {
  581. // Remove debug and annotation statements referencing dead instructions.
  582. // This must be done before killing the instructions, otherwise there are
  583. // dead objects in the def/use database.
  584. bool modified = false;
  585. Instruction* instruction = &*get_module()->debug2_begin();
  586. while (instruction) {
  587. if (instruction->opcode() != SpvOpName) {
  588. instruction = instruction->NextNode();
  589. continue;
  590. }
  591. if (IsTargetDead(instruction)) {
  592. instruction = context()->KillInst(instruction);
  593. modified = true;
  594. } else {
  595. instruction = instruction->NextNode();
  596. }
  597. }
  598. // This code removes all unnecessary decorations safely (see #1174). It also
  599. // does so in a more efficient manner than deleting them only as the targets
  600. // are deleted.
  601. std::vector<Instruction*> annotations;
  602. for (auto& inst : get_module()->annotations()) annotations.push_back(&inst);
  603. std::sort(annotations.begin(), annotations.end(), DecorationLess());
  604. for (auto annotation : annotations) {
  605. switch (annotation->opcode()) {
  606. case SpvOpDecorate:
  607. case SpvOpMemberDecorate:
  608. case SpvOpDecorateStringGOOGLE:
  609. case SpvOpMemberDecorateStringGOOGLE:
  610. if (IsTargetDead(annotation)) {
  611. context()->KillInst(annotation);
  612. modified = true;
  613. }
  614. break;
  615. case SpvOpDecorateId:
  616. if (IsTargetDead(annotation)) {
  617. context()->KillInst(annotation);
  618. modified = true;
  619. } else {
  620. if (annotation->GetSingleWordInOperand(1) ==
  621. SpvDecorationHlslCounterBufferGOOGLE) {
  622. // HlslCounterBuffer will reference an id other than the target.
  623. // If that id is dead, then the decoration can be removed as well.
  624. uint32_t counter_buffer_id = annotation->GetSingleWordInOperand(2);
  625. Instruction* counter_buffer_inst =
  626. get_def_use_mgr()->GetDef(counter_buffer_id);
  627. if (IsDead(counter_buffer_inst)) {
  628. context()->KillInst(annotation);
  629. modified = true;
  630. }
  631. }
  632. }
  633. break;
  634. case SpvOpGroupDecorate: {
  635. // Go through the targets of this group decorate. Remove each dead
  636. // target. If all targets are dead, remove this decoration.
  637. bool dead = true;
  638. bool removed_operand = false;
  639. for (uint32_t i = 1; i < annotation->NumOperands();) {
  640. Instruction* opInst =
  641. get_def_use_mgr()->GetDef(annotation->GetSingleWordOperand(i));
  642. if (IsDead(opInst)) {
  643. // Don't increment |i|.
  644. annotation->RemoveOperand(i);
  645. modified = true;
  646. removed_operand = true;
  647. } else {
  648. i++;
  649. dead = false;
  650. }
  651. }
  652. if (dead) {
  653. context()->KillInst(annotation);
  654. modified = true;
  655. } else if (removed_operand) {
  656. context()->UpdateDefUse(annotation);
  657. }
  658. break;
  659. }
  660. case SpvOpGroupMemberDecorate: {
  661. // Go through the targets of this group member decorate. Remove each
  662. // dead target (and member index). If all targets are dead, remove this
  663. // decoration.
  664. bool dead = true;
  665. bool removed_operand = false;
  666. for (uint32_t i = 1; i < annotation->NumOperands();) {
  667. Instruction* opInst =
  668. get_def_use_mgr()->GetDef(annotation->GetSingleWordOperand(i));
  669. if (IsDead(opInst)) {
  670. // Don't increment |i|.
  671. annotation->RemoveOperand(i + 1);
  672. annotation->RemoveOperand(i);
  673. modified = true;
  674. removed_operand = true;
  675. } else {
  676. i += 2;
  677. dead = false;
  678. }
  679. }
  680. if (dead) {
  681. context()->KillInst(annotation);
  682. modified = true;
  683. } else if (removed_operand) {
  684. context()->UpdateDefUse(annotation);
  685. }
  686. break;
  687. }
  688. case SpvOpDecorationGroup:
  689. // By the time we hit decoration groups we've checked everything that
  690. // can target them. So if they have no uses they must be dead.
  691. if (get_def_use_mgr()->NumUsers(annotation) == 0) {
  692. context()->KillInst(annotation);
  693. modified = true;
  694. }
  695. break;
  696. default:
  697. assert(false);
  698. break;
  699. }
  700. }
  701. // Since ADCE is disabled for non-shaders, we don't check for export linkage
  702. // attributes here.
  703. for (auto& val : get_module()->types_values()) {
  704. if (IsDead(&val)) {
  705. to_kill_.push_back(&val);
  706. }
  707. }
  708. return modified;
  709. }
  710. AggressiveDCEPass::AggressiveDCEPass() = default;
  711. Pass::Status AggressiveDCEPass::Process() {
  712. // Initialize extensions whitelist
  713. InitExtensions();
  714. return ProcessImpl();
  715. }
  716. void AggressiveDCEPass::InitExtensions() {
  717. extensions_whitelist_.clear();
  718. extensions_whitelist_.insert({
  719. "SPV_AMD_shader_explicit_vertex_parameter",
  720. "SPV_AMD_shader_trinary_minmax",
  721. "SPV_AMD_gcn_shader",
  722. "SPV_KHR_shader_ballot",
  723. "SPV_AMD_shader_ballot",
  724. "SPV_AMD_gpu_shader_half_float",
  725. "SPV_KHR_shader_draw_parameters",
  726. "SPV_KHR_subgroup_vote",
  727. "SPV_KHR_16bit_storage",
  728. "SPV_KHR_device_group",
  729. "SPV_KHR_multiview",
  730. "SPV_NVX_multiview_per_view_attributes",
  731. "SPV_NV_viewport_array2",
  732. "SPV_NV_stereo_view_rendering",
  733. "SPV_NV_sample_mask_override_coverage",
  734. "SPV_NV_geometry_shader_passthrough",
  735. "SPV_AMD_texture_gather_bias_lod",
  736. "SPV_KHR_storage_buffer_storage_class",
  737. // SPV_KHR_variable_pointers
  738. // Currently do not support extended pointer expressions
  739. "SPV_AMD_gpu_shader_int16",
  740. "SPV_KHR_post_depth_coverage",
  741. "SPV_KHR_shader_atomic_counter_ops",
  742. "SPV_EXT_shader_stencil_export",
  743. "SPV_EXT_shader_viewport_index_layer",
  744. "SPV_AMD_shader_image_load_store_lod",
  745. "SPV_AMD_shader_fragment_mask",
  746. "SPV_EXT_fragment_fully_covered",
  747. "SPV_AMD_gpu_shader_half_float_fetch",
  748. "SPV_GOOGLE_decorate_string",
  749. "SPV_GOOGLE_hlsl_functionality1",
  750. "SPV_NV_shader_subgroup_partitioned",
  751. "SPV_EXT_descriptor_indexing",
  752. "SPV_NV_fragment_shader_barycentric",
  753. "SPV_NV_compute_shader_derivatives",
  754. "SPV_NV_shader_image_footprint",
  755. "SPV_NV_shading_rate",
  756. "SPV_NV_mesh_shader",
  757. "SPV_NV_ray_tracing",
  758. "SPV_EXT_fragment_invocation_density",
  759. });
  760. }
  761. } // namespace opt
  762. } // namespace spvtools