aggressive_dead_code_elim_pass.cpp 35 KB

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