aggressive_dead_code_elim_pass.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. // Copyright (c) 2017 The Khronos Group Inc.
  2. // Copyright (c) 2017 Valve Corporation
  3. // Copyright (c) 2017 LunarG Inc.
  4. // Copyright (c) 2018-2021 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/eliminate_dead_functions_util.h"
  23. #include "source/opt/ir_builder.h"
  24. #include "source/opt/iterator.h"
  25. #include "source/opt/reflect.h"
  26. #include "source/spirv_constant.h"
  27. namespace spvtools {
  28. namespace opt {
  29. namespace {
  30. const uint32_t kTypePointerStorageClassInIdx = 0;
  31. const uint32_t kEntryPointFunctionIdInIdx = 1;
  32. const uint32_t kSelectionMergeMergeBlockIdInIdx = 0;
  33. const uint32_t kLoopMergeContinueBlockIdInIdx = 1;
  34. const uint32_t kCopyMemoryTargetAddrInIdx = 0;
  35. const uint32_t kCopyMemorySourceAddrInIdx = 1;
  36. const uint32_t kLoadSourceAddrInIdx = 0;
  37. const uint32_t kDebugDeclareOperandVariableIndex = 5;
  38. const uint32_t kGlobalVariableVariableIndex = 12;
  39. // Sorting functor to present annotation instructions in an easy-to-process
  40. // order. The functor orders by opcode first and falls back on unique id
  41. // ordering if both instructions have the same opcode.
  42. //
  43. // Desired priority:
  44. // SpvOpGroupDecorate
  45. // SpvOpGroupMemberDecorate
  46. // SpvOpDecorate
  47. // SpvOpMemberDecorate
  48. // SpvOpDecorateId
  49. // SpvOpDecorateStringGOOGLE
  50. // SpvOpDecorationGroup
  51. struct DecorationLess {
  52. bool operator()(const Instruction* lhs, const Instruction* rhs) const {
  53. assert(lhs && rhs);
  54. SpvOp lhsOp = lhs->opcode();
  55. SpvOp rhsOp = rhs->opcode();
  56. if (lhsOp != rhsOp) {
  57. #define PRIORITY_CASE(opcode) \
  58. if (lhsOp == opcode && rhsOp != opcode) return true; \
  59. if (rhsOp == opcode && lhsOp != opcode) return false;
  60. // OpGroupDecorate and OpGroupMember decorate are highest priority to
  61. // eliminate dead targets early and simplify subsequent checks.
  62. PRIORITY_CASE(SpvOpGroupDecorate)
  63. PRIORITY_CASE(SpvOpGroupMemberDecorate)
  64. PRIORITY_CASE(SpvOpDecorate)
  65. PRIORITY_CASE(SpvOpMemberDecorate)
  66. PRIORITY_CASE(SpvOpDecorateId)
  67. PRIORITY_CASE(SpvOpDecorateStringGOOGLE)
  68. // OpDecorationGroup is lowest priority to ensure use/def chains remain
  69. // usable for instructions that target this group.
  70. PRIORITY_CASE(SpvOpDecorationGroup)
  71. #undef PRIORITY_CASE
  72. }
  73. // Fall back to maintain total ordering (compare unique ids).
  74. return *lhs < *rhs;
  75. }
  76. };
  77. } // namespace
  78. bool AggressiveDCEPass::IsVarOfStorage(uint32_t varId, uint32_t storageClass) {
  79. if (varId == 0) return false;
  80. const Instruction* varInst = get_def_use_mgr()->GetDef(varId);
  81. const SpvOp op = varInst->opcode();
  82. if (op != SpvOpVariable) return false;
  83. const uint32_t varTypeId = varInst->type_id();
  84. const Instruction* varTypeInst = get_def_use_mgr()->GetDef(varTypeId);
  85. if (varTypeInst->opcode() != SpvOpTypePointer) return false;
  86. return varTypeInst->GetSingleWordInOperand(kTypePointerStorageClassInIdx) ==
  87. storageClass;
  88. }
  89. bool AggressiveDCEPass::IsLocalVar(uint32_t varId, Function* func) {
  90. if (IsVarOfStorage(varId, SpvStorageClassFunction)) {
  91. return true;
  92. }
  93. if (!IsVarOfStorage(varId, SpvStorageClassPrivate) &&
  94. !IsVarOfStorage(varId, SpvStorageClassWorkgroup)) {
  95. return false;
  96. }
  97. // For a variable in the Private or WorkGroup storage class, the variable will
  98. // get a new instance for every call to an entry point. If the entry point
  99. // does not have a call, then no other function can read or write to that
  100. // instance of the variable.
  101. return IsEntryPointWithNoCalls(func);
  102. }
  103. void AggressiveDCEPass::AddStores(Function* func, uint32_t ptrId) {
  104. get_def_use_mgr()->ForEachUser(ptrId, [this, ptrId, func](Instruction* user) {
  105. // If the user is not a part of |func|, skip it.
  106. BasicBlock* blk = context()->get_instr_block(user);
  107. if (blk && blk->GetParent() != func) return;
  108. switch (user->opcode()) {
  109. case SpvOpAccessChain:
  110. case SpvOpInBoundsAccessChain:
  111. case SpvOpCopyObject:
  112. this->AddStores(func, user->result_id());
  113. break;
  114. case SpvOpLoad:
  115. break;
  116. case SpvOpCopyMemory:
  117. case SpvOpCopyMemorySized:
  118. if (user->GetSingleWordInOperand(kCopyMemoryTargetAddrInIdx) == ptrId) {
  119. AddToWorklist(user);
  120. }
  121. break;
  122. // If default, assume it stores e.g. frexp, modf, function call
  123. case SpvOpStore:
  124. default:
  125. AddToWorklist(user);
  126. break;
  127. }
  128. });
  129. }
  130. bool AggressiveDCEPass::AllExtensionsSupported() const {
  131. // If any extension not in allowlist, return false
  132. for (auto& ei : get_module()->extensions()) {
  133. const char* extName =
  134. reinterpret_cast<const char*>(&ei.GetInOperand(0).words[0]);
  135. if (extensions_allowlist_.find(extName) == extensions_allowlist_.end())
  136. return false;
  137. }
  138. // Only allow NonSemantic.Shader.DebugInfo.100, we cannot safely optimise
  139. // around unknown extended instruction sets even if they are non-semantic
  140. for (auto& inst : context()->module()->ext_inst_imports()) {
  141. assert(inst.opcode() == SpvOpExtInstImport &&
  142. "Expecting an import of an extension's instruction set.");
  143. const char* extension_name =
  144. reinterpret_cast<const char*>(&inst.GetInOperand(0).words[0]);
  145. if (0 == std::strncmp(extension_name, "NonSemantic.", 12) &&
  146. 0 != std::strncmp(extension_name, "NonSemantic.Shader.DebugInfo.100",
  147. 32)) {
  148. return false;
  149. }
  150. }
  151. return true;
  152. }
  153. bool AggressiveDCEPass::IsTargetDead(Instruction* inst) {
  154. const uint32_t tId = inst->GetSingleWordInOperand(0);
  155. Instruction* tInst = get_def_use_mgr()->GetDef(tId);
  156. if (IsAnnotationInst(tInst->opcode())) {
  157. // This must be a decoration group. We go through annotations in a specific
  158. // order. So if this is not used by any group or group member decorates, it
  159. // is dead.
  160. assert(tInst->opcode() == SpvOpDecorationGroup);
  161. bool dead = true;
  162. get_def_use_mgr()->ForEachUser(tInst, [&dead](Instruction* user) {
  163. if (user->opcode() == SpvOpGroupDecorate ||
  164. user->opcode() == SpvOpGroupMemberDecorate)
  165. dead = false;
  166. });
  167. return dead;
  168. }
  169. return !IsLive(tInst);
  170. }
  171. void AggressiveDCEPass::ProcessLoad(Function* func, uint32_t varId) {
  172. // Only process locals
  173. if (!IsLocalVar(varId, func)) return;
  174. // Return if already processed
  175. if (live_local_vars_.find(varId) != live_local_vars_.end()) return;
  176. // Mark all stores to varId as live
  177. AddStores(func, varId);
  178. // Cache varId as processed
  179. live_local_vars_.insert(varId);
  180. }
  181. void AggressiveDCEPass::AddBranch(uint32_t labelId, BasicBlock* bp) {
  182. std::unique_ptr<Instruction> newBranch(
  183. new Instruction(context(), SpvOpBranch, 0, 0,
  184. {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {labelId}}}));
  185. context()->AnalyzeDefUse(&*newBranch);
  186. context()->set_instr_block(&*newBranch, bp);
  187. bp->AddInstruction(std::move(newBranch));
  188. }
  189. void AggressiveDCEPass::AddBreaksAndContinuesToWorklist(
  190. Instruction* mergeInst) {
  191. assert(mergeInst->opcode() == SpvOpSelectionMerge ||
  192. mergeInst->opcode() == SpvOpLoopMerge);
  193. BasicBlock* header = context()->get_instr_block(mergeInst);
  194. const uint32_t mergeId = mergeInst->GetSingleWordInOperand(0);
  195. get_def_use_mgr()->ForEachUser(mergeId, [header, this](Instruction* user) {
  196. if (!user->IsBranch()) return;
  197. BasicBlock* block = context()->get_instr_block(user);
  198. if (BlockIsInConstruct(header, block)) {
  199. // This is a break from the loop.
  200. AddToWorklist(user);
  201. // Add branch's merge if there is one.
  202. Instruction* userMerge = GetMergeInstruction(user);
  203. if (userMerge != nullptr) AddToWorklist(userMerge);
  204. }
  205. });
  206. if (mergeInst->opcode() != SpvOpLoopMerge) {
  207. return;
  208. }
  209. // For loops we need to find the continues as well.
  210. const uint32_t contId =
  211. mergeInst->GetSingleWordInOperand(kLoopMergeContinueBlockIdInIdx);
  212. get_def_use_mgr()->ForEachUser(contId, [&contId, this](Instruction* user) {
  213. SpvOp op = user->opcode();
  214. if (op == SpvOpBranchConditional || op == SpvOpSwitch) {
  215. // A conditional branch or switch can only be a continue if it does not
  216. // have a merge instruction or its merge block is not the continue block.
  217. Instruction* hdrMerge = GetMergeInstruction(user);
  218. if (hdrMerge != nullptr && hdrMerge->opcode() == SpvOpSelectionMerge) {
  219. uint32_t hdrMergeId =
  220. hdrMerge->GetSingleWordInOperand(kSelectionMergeMergeBlockIdInIdx);
  221. if (hdrMergeId == contId) return;
  222. // Need to mark merge instruction too
  223. AddToWorklist(hdrMerge);
  224. }
  225. } else if (op == SpvOpBranch) {
  226. // An unconditional branch can only be a continue if it is not
  227. // branching to its own merge block.
  228. BasicBlock* blk = context()->get_instr_block(user);
  229. Instruction* hdrBranch = GetHeaderBranch(blk);
  230. if (hdrBranch == nullptr) return;
  231. Instruction* hdrMerge = GetMergeInstruction(hdrBranch);
  232. if (hdrMerge->opcode() == SpvOpLoopMerge) return;
  233. uint32_t hdrMergeId =
  234. hdrMerge->GetSingleWordInOperand(kSelectionMergeMergeBlockIdInIdx);
  235. if (contId == hdrMergeId) return;
  236. } else {
  237. return;
  238. }
  239. AddToWorklist(user);
  240. });
  241. }
  242. bool AggressiveDCEPass::AggressiveDCE(Function* func) {
  243. std::list<BasicBlock*> structured_order;
  244. cfg()->ComputeStructuredOrder(func, &*func->begin(), &structured_order);
  245. live_local_vars_.clear();
  246. InitializeWorkList(func, structured_order);
  247. ProcessWorkList(func);
  248. return KillDeadInstructions(func, structured_order);
  249. }
  250. bool AggressiveDCEPass::KillDeadInstructions(
  251. const Function* func, std::list<BasicBlock*>& structured_order) {
  252. bool modified = false;
  253. for (auto bi = structured_order.begin(); bi != structured_order.end();) {
  254. uint32_t merge_block_id = 0;
  255. (*bi)->ForEachInst([this, &modified, &merge_block_id](Instruction* inst) {
  256. if (IsLive(inst)) return;
  257. if (inst->opcode() == SpvOpLabel) return;
  258. // If dead instruction is selection merge, remember merge block
  259. // for new branch at end of block
  260. if (inst->opcode() == SpvOpSelectionMerge ||
  261. inst->opcode() == SpvOpLoopMerge)
  262. merge_block_id = inst->GetSingleWordInOperand(0);
  263. to_kill_.push_back(inst);
  264. modified = true;
  265. });
  266. // If a structured if or loop was deleted, add a branch to its merge
  267. // block, and traverse to the merge block and continue processing there.
  268. // We know the block still exists because the label is not deleted.
  269. if (merge_block_id != 0) {
  270. AddBranch(merge_block_id, *bi);
  271. for (++bi; (*bi)->id() != merge_block_id; ++bi) {
  272. }
  273. auto merge_terminator = (*bi)->terminator();
  274. if (merge_terminator->opcode() == SpvOpUnreachable) {
  275. // The merge was unreachable. This is undefined behaviour so just
  276. // return (or return an undef). Then mark the new return as live.
  277. auto func_ret_type_inst = get_def_use_mgr()->GetDef(func->type_id());
  278. if (func_ret_type_inst->opcode() == SpvOpTypeVoid) {
  279. merge_terminator->SetOpcode(SpvOpReturn);
  280. } else {
  281. // Find an undef for the return value and make sure it gets kept by
  282. // the pass.
  283. auto undef_id = Type2Undef(func->type_id());
  284. auto undef = get_def_use_mgr()->GetDef(undef_id);
  285. live_insts_.Set(undef->unique_id());
  286. merge_terminator->SetOpcode(SpvOpReturnValue);
  287. merge_terminator->SetInOperands({{SPV_OPERAND_TYPE_ID, {undef_id}}});
  288. get_def_use_mgr()->AnalyzeInstUse(merge_terminator);
  289. }
  290. live_insts_.Set(merge_terminator->unique_id());
  291. }
  292. } else {
  293. Instruction* inst = (*bi)->terminator();
  294. if (!IsLive(inst)) {
  295. // If the terminator is not live, this block has no live instructions,
  296. // and it will be unreachable.
  297. AddUnreachable(*bi);
  298. }
  299. ++bi;
  300. }
  301. }
  302. return modified;
  303. }
  304. void AggressiveDCEPass::ProcessWorkList(Function* func) {
  305. while (!worklist_.empty()) {
  306. Instruction* live_inst = worklist_.front();
  307. worklist_.pop();
  308. AddOperandsToWorkList(live_inst);
  309. MarkBlockAsLive(live_inst);
  310. MarkLoadedVariablesAsLive(func, live_inst);
  311. AddDecorationsToWorkList(live_inst);
  312. AddDebugInstructionsToWorkList(live_inst);
  313. }
  314. }
  315. void AggressiveDCEPass::AddDebugInstructionsToWorkList(
  316. const Instruction* inst) {
  317. for (auto& line_inst : inst->dbg_line_insts()) {
  318. if (line_inst.IsDebugLineInst()) {
  319. AddOperandsToWorkList(&line_inst);
  320. }
  321. }
  322. if (inst->GetDebugScope().GetLexicalScope() != kNoDebugScope) {
  323. auto* scope =
  324. get_def_use_mgr()->GetDef(inst->GetDebugScope().GetLexicalScope());
  325. AddToWorklist(scope);
  326. }
  327. if (inst->GetDebugInlinedAt() != kNoInlinedAt) {
  328. auto* inlined_at = get_def_use_mgr()->GetDef(inst->GetDebugInlinedAt());
  329. AddToWorklist(inlined_at);
  330. }
  331. }
  332. void AggressiveDCEPass::AddDecorationsToWorkList(const Instruction* inst) {
  333. // Add OpDecorateId instructions that apply to this instruction to the work
  334. // list. We use the decoration manager to look through the group
  335. // decorations to get to the OpDecorate* instructions themselves.
  336. auto decorations =
  337. get_decoration_mgr()->GetDecorationsFor(inst->result_id(), false);
  338. for (Instruction* dec : decorations) {
  339. // We only care about OpDecorateId instructions because the are the only
  340. // decorations that will reference an id that will have to be kept live
  341. // because of that use.
  342. if (dec->opcode() != SpvOpDecorateId) {
  343. continue;
  344. }
  345. if (dec->GetSingleWordInOperand(1) ==
  346. SpvDecorationHlslCounterBufferGOOGLE) {
  347. // These decorations should not force the use id to be live. It will be
  348. // removed if either the target or the in operand are dead.
  349. continue;
  350. }
  351. AddToWorklist(dec);
  352. }
  353. }
  354. void AggressiveDCEPass::MarkLoadedVariablesAsLive(Function* func,
  355. Instruction* inst) {
  356. std::vector<uint32_t> live_variables = GetLoadedVariables(inst);
  357. for (uint32_t var_id : live_variables) {
  358. ProcessLoad(func, var_id);
  359. }
  360. }
  361. std::vector<uint32_t> AggressiveDCEPass::GetLoadedVariables(Instruction* inst) {
  362. if (inst->opcode() == SpvOpFunctionCall) {
  363. return GetLoadedVariablesFromFunctionCall(inst);
  364. }
  365. uint32_t var_id = GetLoadedVariableFromNonFunctionCalls(inst);
  366. if (var_id == 0) {
  367. return {};
  368. }
  369. return {var_id};
  370. }
  371. uint32_t AggressiveDCEPass::GetLoadedVariableFromNonFunctionCalls(
  372. Instruction* inst) {
  373. std::vector<uint32_t> live_variables;
  374. if (inst->IsAtomicWithLoad()) {
  375. return GetVariableId(inst->GetSingleWordInOperand(kLoadSourceAddrInIdx));
  376. }
  377. switch (inst->opcode()) {
  378. case SpvOpLoad:
  379. case SpvOpImageTexelPointer:
  380. return GetVariableId(inst->GetSingleWordInOperand(kLoadSourceAddrInIdx));
  381. case SpvOpCopyMemory:
  382. case SpvOpCopyMemorySized:
  383. return GetVariableId(
  384. inst->GetSingleWordInOperand(kCopyMemorySourceAddrInIdx));
  385. default:
  386. break;
  387. }
  388. switch (inst->GetCommonDebugOpcode()) {
  389. case CommonDebugInfoDebugDeclare:
  390. return inst->GetSingleWordOperand(kDebugDeclareOperandVariableIndex);
  391. case CommonDebugInfoDebugValue: {
  392. analysis::DebugInfoManager* debug_info_mgr =
  393. context()->get_debug_info_mgr();
  394. return debug_info_mgr->GetVariableIdOfDebugValueUsedForDeclare(inst);
  395. }
  396. default:
  397. break;
  398. }
  399. return 0;
  400. }
  401. std::vector<uint32_t> AggressiveDCEPass::GetLoadedVariablesFromFunctionCall(
  402. const Instruction* inst) {
  403. assert(inst->opcode() == SpvOpFunctionCall);
  404. std::vector<uint32_t> live_variables;
  405. inst->ForEachInId([this, &live_variables](const uint32_t* operand_id) {
  406. if (!IsPtr(*operand_id)) return;
  407. uint32_t var_id = GetVariableId(*operand_id);
  408. live_variables.push_back(var_id);
  409. });
  410. return live_variables;
  411. }
  412. uint32_t AggressiveDCEPass::GetVariableId(uint32_t ptr_id) {
  413. assert(IsPtr(ptr_id) &&
  414. "Cannot get the variable when input is not a pointer.");
  415. uint32_t varId = 0;
  416. (void)GetPtr(ptr_id, &varId);
  417. return varId;
  418. }
  419. void AggressiveDCEPass::MarkBlockAsLive(Instruction* inst) {
  420. BasicBlock* basic_block = context()->get_instr_block(inst);
  421. if (basic_block == nullptr) {
  422. return;
  423. }
  424. // If we intend to keep this instruction, we need the block label and
  425. // block terminator to have a valid block for the instruction.
  426. AddToWorklist(basic_block->GetLabelInst());
  427. // We need to mark the successors blocks that follow as live. If this is
  428. // header of the merge construct, the construct may be folded, but we will
  429. // definitely need the merge label. If it is not a construct, the terminator
  430. // must be live, and the successor blocks will be marked as live when
  431. // processing the terminator.
  432. uint32_t merge_id = basic_block->MergeBlockIdIfAny();
  433. if (merge_id == 0) {
  434. AddToWorklist(basic_block->terminator());
  435. } else {
  436. AddToWorklist(context()->get_def_use_mgr()->GetDef(merge_id));
  437. }
  438. // Mark the structured control flow constructs that contains this block as
  439. // live. If |inst| is an instruction in the loop header, then it is part of
  440. // the loop, so the loop construct must be live. We exclude the label because
  441. // it does not matter how many times it is executed. This could be extended
  442. // to more instructions, but we will need it for now.
  443. if (inst->opcode() != SpvOpLabel)
  444. MarkLoopConstructAsLiveIfLoopHeader(basic_block);
  445. Instruction* next_branch_inst = GetBranchForNextHeader(basic_block);
  446. if (next_branch_inst != nullptr) {
  447. AddToWorklist(next_branch_inst);
  448. Instruction* mergeInst = GetMergeInstruction(next_branch_inst);
  449. AddToWorklist(mergeInst);
  450. }
  451. if (inst->opcode() == SpvOpLoopMerge ||
  452. inst->opcode() == SpvOpSelectionMerge) {
  453. AddBreaksAndContinuesToWorklist(inst);
  454. }
  455. }
  456. void AggressiveDCEPass::MarkLoopConstructAsLiveIfLoopHeader(
  457. BasicBlock* basic_block) {
  458. // If this is the header for a loop, then loop structure needs to keep as well
  459. // because the loop header is also part of the loop.
  460. Instruction* merge_inst = basic_block->GetLoopMergeInst();
  461. if (merge_inst != nullptr) {
  462. AddToWorklist(basic_block->terminator());
  463. AddToWorklist(merge_inst);
  464. }
  465. }
  466. void AggressiveDCEPass::AddOperandsToWorkList(const Instruction* inst) {
  467. inst->ForEachInId([this](const uint32_t* iid) {
  468. Instruction* inInst = get_def_use_mgr()->GetDef(*iid);
  469. AddToWorklist(inInst);
  470. });
  471. if (inst->type_id() != 0) {
  472. AddToWorklist(get_def_use_mgr()->GetDef(inst->type_id()));
  473. }
  474. }
  475. void AggressiveDCEPass::InitializeWorkList(
  476. Function* func, std::list<BasicBlock*>& structured_order) {
  477. AddToWorklist(&func->DefInst());
  478. MarkFunctionParameterAsLive(func);
  479. MarkFirstBlockAsLive(func);
  480. // Add instructions with external side effects to the worklist. Also add
  481. // branches that are not attached to a structured construct.
  482. // TODO(s-perron): The handling of branch seems to be adhoc. This needs to be
  483. // cleaned up.
  484. for (auto& bi : structured_order) {
  485. for (auto ii = bi->begin(); ii != bi->end(); ++ii) {
  486. SpvOp op = ii->opcode();
  487. if (ii->IsBranch()) {
  488. continue;
  489. }
  490. switch (op) {
  491. case SpvOpStore: {
  492. uint32_t var_id = 0;
  493. (void)GetPtr(&*ii, &var_id);
  494. if (!IsLocalVar(var_id, func)) AddToWorklist(&*ii);
  495. } break;
  496. case SpvOpCopyMemory:
  497. case SpvOpCopyMemorySized: {
  498. uint32_t var_id = 0;
  499. uint32_t target_addr_id =
  500. ii->GetSingleWordInOperand(kCopyMemoryTargetAddrInIdx);
  501. (void)GetPtr(target_addr_id, &var_id);
  502. if (!IsLocalVar(var_id, func)) AddToWorklist(&*ii);
  503. } break;
  504. case SpvOpLoopMerge:
  505. case SpvOpSelectionMerge:
  506. case SpvOpUnreachable:
  507. break;
  508. default: {
  509. // Function calls, atomics, function params, function returns, etc.
  510. if (!ii->IsOpcodeSafeToDelete()) {
  511. AddToWorklist(&*ii);
  512. }
  513. } break;
  514. }
  515. }
  516. }
  517. }
  518. void AggressiveDCEPass::InitializeModuleScopeLiveInstructions() {
  519. // Keep all execution modes.
  520. for (auto& exec : get_module()->execution_modes()) {
  521. AddToWorklist(&exec);
  522. }
  523. // Keep all entry points.
  524. for (auto& entry : get_module()->entry_points()) {
  525. if (get_module()->version() >= SPV_SPIRV_VERSION_WORD(1, 4) &&
  526. !preserve_interface_) {
  527. // In SPIR-V 1.4 and later, entry points must list all global variables
  528. // used. DCE can still remove non-input/output variables and update the
  529. // interface list. Mark the entry point as live and inputs and outputs as
  530. // live, but defer decisions all other interfaces.
  531. live_insts_.Set(entry.unique_id());
  532. // The actual function is live always.
  533. AddToWorklist(
  534. get_def_use_mgr()->GetDef(entry.GetSingleWordInOperand(1u)));
  535. for (uint32_t i = 3; i < entry.NumInOperands(); ++i) {
  536. auto* var = get_def_use_mgr()->GetDef(entry.GetSingleWordInOperand(i));
  537. auto storage_class = var->GetSingleWordInOperand(0u);
  538. if (storage_class == SpvStorageClassInput ||
  539. storage_class == SpvStorageClassOutput) {
  540. AddToWorklist(var);
  541. }
  542. }
  543. } else {
  544. AddToWorklist(&entry);
  545. }
  546. }
  547. for (auto& anno : get_module()->annotations()) {
  548. if (anno.opcode() == SpvOpDecorate) {
  549. // Keep workgroup size.
  550. if (anno.GetSingleWordInOperand(1u) == SpvDecorationBuiltIn &&
  551. anno.GetSingleWordInOperand(2u) == SpvBuiltInWorkgroupSize) {
  552. AddToWorklist(&anno);
  553. }
  554. if (context()->preserve_bindings()) {
  555. // Keep all bindings.
  556. if ((anno.GetSingleWordInOperand(1u) == SpvDecorationDescriptorSet) ||
  557. (anno.GetSingleWordInOperand(1u) == SpvDecorationBinding)) {
  558. AddToWorklist(&anno);
  559. }
  560. }
  561. if (context()->preserve_spec_constants()) {
  562. // Keep all specialization constant instructions
  563. if (anno.GetSingleWordInOperand(1u) == SpvDecorationSpecId) {
  564. AddToWorklist(&anno);
  565. }
  566. }
  567. }
  568. }
  569. // For each DebugInfo GlobalVariable keep all operands except the Variable.
  570. // Later, if the variable is killed with KillInst(), we will set the operand
  571. // to DebugInfoNone. Create and save DebugInfoNone now for this possible
  572. // later use. This is slightly unoptimal, but it avoids generating it during
  573. // instruction killing when the module is not consistent.
  574. bool debug_global_seen = false;
  575. for (auto& dbg : get_module()->ext_inst_debuginfo()) {
  576. if (dbg.GetCommonDebugOpcode() != CommonDebugInfoDebugGlobalVariable)
  577. continue;
  578. debug_global_seen = true;
  579. dbg.ForEachInId([this](const uint32_t* iid) {
  580. Instruction* in_inst = get_def_use_mgr()->GetDef(*iid);
  581. if (in_inst->opcode() == SpvOpVariable) return;
  582. AddToWorklist(in_inst);
  583. });
  584. }
  585. if (debug_global_seen) {
  586. auto dbg_none = context()->get_debug_info_mgr()->GetDebugInfoNone();
  587. AddToWorklist(dbg_none);
  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()->ProcessReachableCallTree(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. assert((to_kill_.empty() || modified) &&
  628. "A dead instruction was identified, but no change recorded.");
  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()->ProcessReachableCallTree(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.
  641. std::unordered_set<const Function*> live_function_set;
  642. ProcessFunction mark_live = [&live_function_set](Function* fp) {
  643. live_function_set.insert(fp);
  644. return false;
  645. };
  646. context()->ProcessReachableCallTree(mark_live);
  647. bool modified = false;
  648. for (auto funcIter = get_module()->begin();
  649. funcIter != get_module()->end();) {
  650. if (live_function_set.count(&*funcIter) == 0) {
  651. modified = true;
  652. funcIter =
  653. eliminatedeadfunctionsutil::EliminateFunction(context(), &funcIter);
  654. } else {
  655. ++funcIter;
  656. }
  657. }
  658. return modified;
  659. }
  660. bool AggressiveDCEPass::ProcessGlobalValues() {
  661. // Remove debug and annotation statements referencing dead instructions.
  662. // This must be done before killing the instructions, otherwise there are
  663. // dead objects in the def/use database.
  664. bool modified = false;
  665. Instruction* instruction = &*get_module()->debug2_begin();
  666. while (instruction) {
  667. if (instruction->opcode() != SpvOpName) {
  668. instruction = instruction->NextNode();
  669. continue;
  670. }
  671. if (IsTargetDead(instruction)) {
  672. instruction = context()->KillInst(instruction);
  673. modified = true;
  674. } else {
  675. instruction = instruction->NextNode();
  676. }
  677. }
  678. // This code removes all unnecessary decorations safely (see #1174). It also
  679. // does so in a more efficient manner than deleting them only as the targets
  680. // are deleted.
  681. std::vector<Instruction*> annotations;
  682. for (auto& inst : get_module()->annotations()) annotations.push_back(&inst);
  683. std::sort(annotations.begin(), annotations.end(), DecorationLess());
  684. for (auto annotation : annotations) {
  685. switch (annotation->opcode()) {
  686. case SpvOpDecorate:
  687. case SpvOpMemberDecorate:
  688. case SpvOpDecorateStringGOOGLE:
  689. case SpvOpMemberDecorateStringGOOGLE:
  690. if (IsTargetDead(annotation)) {
  691. context()->KillInst(annotation);
  692. modified = true;
  693. }
  694. break;
  695. case SpvOpDecorateId:
  696. if (IsTargetDead(annotation)) {
  697. context()->KillInst(annotation);
  698. modified = true;
  699. } else {
  700. if (annotation->GetSingleWordInOperand(1) ==
  701. SpvDecorationHlslCounterBufferGOOGLE) {
  702. // HlslCounterBuffer will reference an id other than the target.
  703. // If that id is dead, then the decoration can be removed as well.
  704. uint32_t counter_buffer_id = annotation->GetSingleWordInOperand(2);
  705. Instruction* counter_buffer_inst =
  706. get_def_use_mgr()->GetDef(counter_buffer_id);
  707. if (!IsLive(counter_buffer_inst)) {
  708. context()->KillInst(annotation);
  709. modified = true;
  710. }
  711. }
  712. }
  713. break;
  714. case SpvOpGroupDecorate: {
  715. // Go through the targets of this group decorate. Remove each dead
  716. // target. If all targets are dead, remove this decoration.
  717. bool dead = true;
  718. bool removed_operand = false;
  719. for (uint32_t i = 1; i < annotation->NumOperands();) {
  720. Instruction* opInst =
  721. get_def_use_mgr()->GetDef(annotation->GetSingleWordOperand(i));
  722. if (!IsLive(opInst)) {
  723. // Don't increment |i|.
  724. annotation->RemoveOperand(i);
  725. modified = true;
  726. removed_operand = true;
  727. } else {
  728. i++;
  729. dead = false;
  730. }
  731. }
  732. if (dead) {
  733. context()->KillInst(annotation);
  734. modified = true;
  735. } else if (removed_operand) {
  736. context()->UpdateDefUse(annotation);
  737. }
  738. break;
  739. }
  740. case SpvOpGroupMemberDecorate: {
  741. // Go through the targets of this group member decorate. Remove each
  742. // dead target (and member index). If all targets are dead, remove this
  743. // decoration.
  744. bool dead = true;
  745. bool removed_operand = false;
  746. for (uint32_t i = 1; i < annotation->NumOperands();) {
  747. Instruction* opInst =
  748. get_def_use_mgr()->GetDef(annotation->GetSingleWordOperand(i));
  749. if (!IsLive(opInst)) {
  750. // Don't increment |i|.
  751. annotation->RemoveOperand(i + 1);
  752. annotation->RemoveOperand(i);
  753. modified = true;
  754. removed_operand = true;
  755. } else {
  756. i += 2;
  757. dead = false;
  758. }
  759. }
  760. if (dead) {
  761. context()->KillInst(annotation);
  762. modified = true;
  763. } else if (removed_operand) {
  764. context()->UpdateDefUse(annotation);
  765. }
  766. break;
  767. }
  768. case SpvOpDecorationGroup:
  769. // By the time we hit decoration groups we've checked everything that
  770. // can target them. So if they have no uses they must be dead.
  771. if (get_def_use_mgr()->NumUsers(annotation) == 0) {
  772. context()->KillInst(annotation);
  773. modified = true;
  774. }
  775. break;
  776. default:
  777. assert(false);
  778. break;
  779. }
  780. }
  781. for (auto& dbg : get_module()->ext_inst_debuginfo()) {
  782. if (IsLive(&dbg)) continue;
  783. // Save GlobalVariable if its variable is live, otherwise null out variable
  784. // index
  785. if (dbg.GetCommonDebugOpcode() == CommonDebugInfoDebugGlobalVariable) {
  786. auto var_id = dbg.GetSingleWordOperand(kGlobalVariableVariableIndex);
  787. Instruction* var_inst = get_def_use_mgr()->GetDef(var_id);
  788. if (IsLive(var_inst)) continue;
  789. context()->ForgetUses(&dbg);
  790. dbg.SetOperand(
  791. kGlobalVariableVariableIndex,
  792. {context()->get_debug_info_mgr()->GetDebugInfoNone()->result_id()});
  793. context()->AnalyzeUses(&dbg);
  794. continue;
  795. }
  796. to_kill_.push_back(&dbg);
  797. modified = true;
  798. }
  799. // Since ADCE is disabled for non-shaders, we don't check for export linkage
  800. // attributes here.
  801. for (auto& val : get_module()->types_values()) {
  802. if (!IsLive(&val)) {
  803. // Save forwarded pointer if pointer is live since closure does not mark
  804. // this live as it does not have a result id. This is a little too
  805. // conservative since it is not known if the structure type that needed
  806. // it is still live. TODO(greg-lunarg): Only save if needed.
  807. if (val.opcode() == SpvOpTypeForwardPointer) {
  808. uint32_t ptr_ty_id = val.GetSingleWordInOperand(0);
  809. Instruction* ptr_ty_inst = get_def_use_mgr()->GetDef(ptr_ty_id);
  810. if (IsLive(ptr_ty_inst)) continue;
  811. }
  812. to_kill_.push_back(&val);
  813. modified = true;
  814. }
  815. }
  816. if (get_module()->version() >= SPV_SPIRV_VERSION_WORD(1, 4) &&
  817. !preserve_interface_) {
  818. // Remove the dead interface variables from the entry point interface list.
  819. for (auto& entry : get_module()->entry_points()) {
  820. std::vector<Operand> new_operands;
  821. for (uint32_t i = 0; i < entry.NumInOperands(); ++i) {
  822. if (i < 3) {
  823. // Execution model, function id and name are always valid.
  824. new_operands.push_back(entry.GetInOperand(i));
  825. } else {
  826. auto* var =
  827. get_def_use_mgr()->GetDef(entry.GetSingleWordInOperand(i));
  828. if (IsLive(var)) {
  829. new_operands.push_back(entry.GetInOperand(i));
  830. }
  831. }
  832. }
  833. if (new_operands.size() != entry.NumInOperands()) {
  834. entry.SetInOperands(std::move(new_operands));
  835. get_def_use_mgr()->UpdateDefUse(&entry);
  836. }
  837. }
  838. }
  839. return modified;
  840. }
  841. Pass::Status AggressiveDCEPass::Process() {
  842. // Initialize extensions allowlist
  843. InitExtensions();
  844. return ProcessImpl();
  845. }
  846. void AggressiveDCEPass::InitExtensions() {
  847. extensions_allowlist_.clear();
  848. extensions_allowlist_.insert({
  849. "SPV_AMD_shader_explicit_vertex_parameter",
  850. "SPV_AMD_shader_trinary_minmax",
  851. "SPV_AMD_gcn_shader",
  852. "SPV_KHR_shader_ballot",
  853. "SPV_AMD_shader_ballot",
  854. "SPV_AMD_gpu_shader_half_float",
  855. "SPV_KHR_shader_draw_parameters",
  856. "SPV_KHR_subgroup_vote",
  857. "SPV_KHR_8bit_storage",
  858. "SPV_KHR_16bit_storage",
  859. "SPV_KHR_device_group",
  860. "SPV_KHR_multiview",
  861. "SPV_NVX_multiview_per_view_attributes",
  862. "SPV_NV_viewport_array2",
  863. "SPV_NV_stereo_view_rendering",
  864. "SPV_NV_sample_mask_override_coverage",
  865. "SPV_NV_geometry_shader_passthrough",
  866. "SPV_AMD_texture_gather_bias_lod",
  867. "SPV_KHR_storage_buffer_storage_class",
  868. // SPV_KHR_variable_pointers
  869. // Currently do not support extended pointer expressions
  870. "SPV_AMD_gpu_shader_int16",
  871. "SPV_KHR_post_depth_coverage",
  872. "SPV_KHR_shader_atomic_counter_ops",
  873. "SPV_EXT_shader_stencil_export",
  874. "SPV_EXT_shader_viewport_index_layer",
  875. "SPV_AMD_shader_image_load_store_lod",
  876. "SPV_AMD_shader_fragment_mask",
  877. "SPV_EXT_fragment_fully_covered",
  878. "SPV_AMD_gpu_shader_half_float_fetch",
  879. "SPV_GOOGLE_decorate_string",
  880. "SPV_GOOGLE_hlsl_functionality1",
  881. "SPV_GOOGLE_user_type",
  882. "SPV_NV_shader_subgroup_partitioned",
  883. "SPV_EXT_demote_to_helper_invocation",
  884. "SPV_EXT_descriptor_indexing",
  885. "SPV_NV_fragment_shader_barycentric",
  886. "SPV_NV_compute_shader_derivatives",
  887. "SPV_NV_shader_image_footprint",
  888. "SPV_NV_shading_rate",
  889. "SPV_NV_mesh_shader",
  890. "SPV_NV_ray_tracing",
  891. "SPV_KHR_ray_tracing",
  892. "SPV_KHR_ray_query",
  893. "SPV_EXT_fragment_invocation_density",
  894. "SPV_EXT_physical_storage_buffer",
  895. "SPV_KHR_terminate_invocation",
  896. "SPV_KHR_shader_clock",
  897. "SPV_KHR_vulkan_memory_model",
  898. "SPV_KHR_subgroup_uniform_control_flow",
  899. "SPV_KHR_integer_dot_product",
  900. "SPV_EXT_shader_image_int64",
  901. "SPV_KHR_non_semantic_info",
  902. });
  903. }
  904. Instruction* AggressiveDCEPass::GetHeaderBranch(BasicBlock* blk) {
  905. if (blk == nullptr) {
  906. return nullptr;
  907. }
  908. BasicBlock* header_block = GetHeaderBlock(blk);
  909. if (header_block == nullptr) {
  910. return nullptr;
  911. }
  912. return header_block->terminator();
  913. }
  914. BasicBlock* AggressiveDCEPass::GetHeaderBlock(BasicBlock* blk) const {
  915. if (blk == nullptr) {
  916. return nullptr;
  917. }
  918. BasicBlock* header_block = nullptr;
  919. if (blk->IsLoopHeader()) {
  920. header_block = blk;
  921. } else {
  922. uint32_t header =
  923. context()->GetStructuredCFGAnalysis()->ContainingConstruct(blk->id());
  924. header_block = context()->get_instr_block(header);
  925. }
  926. return header_block;
  927. }
  928. Instruction* AggressiveDCEPass::GetMergeInstruction(Instruction* inst) {
  929. BasicBlock* bb = context()->get_instr_block(inst);
  930. if (bb == nullptr) {
  931. return nullptr;
  932. }
  933. return bb->GetMergeInst();
  934. }
  935. Instruction* AggressiveDCEPass::GetBranchForNextHeader(BasicBlock* blk) {
  936. if (blk == nullptr) {
  937. return nullptr;
  938. }
  939. if (blk->IsLoopHeader()) {
  940. uint32_t header =
  941. context()->GetStructuredCFGAnalysis()->ContainingConstruct(blk->id());
  942. blk = context()->get_instr_block(header);
  943. }
  944. return GetHeaderBranch(blk);
  945. }
  946. void AggressiveDCEPass::MarkFunctionParameterAsLive(const Function* func) {
  947. func->ForEachParam(
  948. [this](const Instruction* param) {
  949. AddToWorklist(const_cast<Instruction*>(param));
  950. },
  951. false);
  952. }
  953. bool AggressiveDCEPass::BlockIsInConstruct(BasicBlock* header_block,
  954. BasicBlock* bb) {
  955. if (bb == nullptr || header_block == nullptr) {
  956. return false;
  957. }
  958. uint32_t current_header = bb->id();
  959. while (current_header != 0) {
  960. if (current_header == header_block->id()) return true;
  961. current_header = context()->GetStructuredCFGAnalysis()->ContainingConstruct(
  962. current_header);
  963. }
  964. return false;
  965. }
  966. bool AggressiveDCEPass::IsEntryPointWithNoCalls(Function* func) {
  967. auto cached_result = entry_point_with_no_calls_cache_.find(func->result_id());
  968. if (cached_result != entry_point_with_no_calls_cache_.end()) {
  969. return cached_result->second;
  970. }
  971. bool result = IsEntryPoint(func) && !HasCall(func);
  972. entry_point_with_no_calls_cache_[func->result_id()] = result;
  973. return result;
  974. }
  975. bool AggressiveDCEPass::IsEntryPoint(Function* func) {
  976. for (const Instruction& entry_point : get_module()->entry_points()) {
  977. uint32_t entry_point_id =
  978. entry_point.GetSingleWordInOperand(kEntryPointFunctionIdInIdx);
  979. if (entry_point_id == func->result_id()) {
  980. return true;
  981. }
  982. }
  983. return false;
  984. }
  985. bool AggressiveDCEPass::HasCall(Function* func) {
  986. return !func->WhileEachInst(
  987. [](Instruction* inst) { return inst->opcode() != SpvOpFunctionCall; });
  988. }
  989. void AggressiveDCEPass::MarkFirstBlockAsLive(Function* func) {
  990. BasicBlock* first_block = &*func->begin();
  991. MarkBlockAsLive(first_block->GetLabelInst());
  992. }
  993. void AggressiveDCEPass::AddUnreachable(BasicBlock*& block) {
  994. InstructionBuilder builder(
  995. context(), block,
  996. IRContext::kAnalysisInstrToBlockMapping | IRContext::kAnalysisDefUse);
  997. builder.AddUnreachable();
  998. }
  999. } // namespace opt
  1000. } // namespace spvtools