aggressive_dead_code_elim_pass.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  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. #include "source/util/string_utils.h"
  28. namespace spvtools {
  29. namespace opt {
  30. namespace {
  31. const uint32_t kTypePointerStorageClassInIdx = 0;
  32. const uint32_t kEntryPointFunctionIdInIdx = 1;
  33. const uint32_t kSelectionMergeMergeBlockIdInIdx = 0;
  34. const uint32_t kLoopMergeContinueBlockIdInIdx = 1;
  35. const uint32_t kCopyMemoryTargetAddrInIdx = 0;
  36. const uint32_t kCopyMemorySourceAddrInIdx = 1;
  37. const uint32_t kLoadSourceAddrInIdx = 0;
  38. const uint32_t kDebugDeclareOperandVariableIndex = 5;
  39. const uint32_t kGlobalVariableVariableIndex = 12;
  40. // Sorting functor to present annotation instructions in an easy-to-process
  41. // order. The functor orders by opcode first and falls back on unique id
  42. // ordering if both instructions have the same opcode.
  43. //
  44. // Desired priority:
  45. // SpvOpGroupDecorate
  46. // SpvOpGroupMemberDecorate
  47. // SpvOpDecorate
  48. // SpvOpMemberDecorate
  49. // SpvOpDecorateId
  50. // SpvOpDecorateStringGOOGLE
  51. // SpvOpDecorationGroup
  52. struct DecorationLess {
  53. bool operator()(const Instruction* lhs, const Instruction* rhs) const {
  54. assert(lhs && rhs);
  55. SpvOp lhsOp = lhs->opcode();
  56. SpvOp rhsOp = rhs->opcode();
  57. if (lhsOp != rhsOp) {
  58. #define PRIORITY_CASE(opcode) \
  59. if (lhsOp == opcode && rhsOp != opcode) return true; \
  60. if (rhsOp == opcode && lhsOp != opcode) return false;
  61. // OpGroupDecorate and OpGroupMember decorate are highest priority to
  62. // eliminate dead targets early and simplify subsequent checks.
  63. PRIORITY_CASE(SpvOpGroupDecorate)
  64. PRIORITY_CASE(SpvOpGroupMemberDecorate)
  65. PRIORITY_CASE(SpvOpDecorate)
  66. PRIORITY_CASE(SpvOpMemberDecorate)
  67. PRIORITY_CASE(SpvOpDecorateId)
  68. PRIORITY_CASE(SpvOpDecorateStringGOOGLE)
  69. // OpDecorationGroup is lowest priority to ensure use/def chains remain
  70. // usable for instructions that target this group.
  71. PRIORITY_CASE(SpvOpDecorationGroup)
  72. #undef PRIORITY_CASE
  73. }
  74. // Fall back to maintain total ordering (compare unique ids).
  75. return *lhs < *rhs;
  76. }
  77. };
  78. } // namespace
  79. bool AggressiveDCEPass::IsVarOfStorage(uint32_t varId, uint32_t storageClass) {
  80. if (varId == 0) return false;
  81. const Instruction* varInst = get_def_use_mgr()->GetDef(varId);
  82. const SpvOp op = varInst->opcode();
  83. if (op != SpvOpVariable) return false;
  84. const uint32_t varTypeId = varInst->type_id();
  85. const Instruction* varTypeInst = get_def_use_mgr()->GetDef(varTypeId);
  86. if (varTypeInst->opcode() != SpvOpTypePointer) return false;
  87. return varTypeInst->GetSingleWordInOperand(kTypePointerStorageClassInIdx) ==
  88. storageClass;
  89. }
  90. bool AggressiveDCEPass::IsLocalVar(uint32_t varId, Function* func) {
  91. if (IsVarOfStorage(varId, SpvStorageClassFunction)) {
  92. return true;
  93. }
  94. if (!IsVarOfStorage(varId, SpvStorageClassPrivate) &&
  95. !IsVarOfStorage(varId, SpvStorageClassWorkgroup)) {
  96. return false;
  97. }
  98. // For a variable in the Private or WorkGroup storage class, the variable will
  99. // get a new instance for every call to an entry point. If the entry point
  100. // does not have a call, then no other function can read or write to that
  101. // instance of the variable.
  102. return IsEntryPointWithNoCalls(func);
  103. }
  104. void AggressiveDCEPass::AddStores(Function* func, uint32_t ptrId) {
  105. get_def_use_mgr()->ForEachUser(ptrId, [this, ptrId, func](Instruction* user) {
  106. // If the user is not a part of |func|, skip it.
  107. BasicBlock* blk = context()->get_instr_block(user);
  108. if (blk && blk->GetParent() != func) return;
  109. switch (user->opcode()) {
  110. case SpvOpAccessChain:
  111. case SpvOpInBoundsAccessChain:
  112. case SpvOpCopyObject:
  113. this->AddStores(func, user->result_id());
  114. break;
  115. case SpvOpLoad:
  116. break;
  117. case SpvOpCopyMemory:
  118. case SpvOpCopyMemorySized:
  119. if (user->GetSingleWordInOperand(kCopyMemoryTargetAddrInIdx) == ptrId) {
  120. AddToWorklist(user);
  121. }
  122. break;
  123. // If default, assume it stores e.g. frexp, modf, function call
  124. case SpvOpStore:
  125. default:
  126. AddToWorklist(user);
  127. break;
  128. }
  129. });
  130. }
  131. bool AggressiveDCEPass::AllExtensionsSupported() const {
  132. // If any extension not in allowlist, return false
  133. for (auto& ei : get_module()->extensions()) {
  134. const std::string extName = ei.GetInOperand(0).AsString();
  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 std::string extension_name = inst.GetInOperand(0).AsString();
  144. if (spvtools::utils::starts_with(extension_name, "NonSemantic.") &&
  145. extension_name != "NonSemantic.Shader.DebugInfo.100") {
  146. return false;
  147. }
  148. }
  149. return true;
  150. }
  151. bool AggressiveDCEPass::IsTargetDead(Instruction* inst) {
  152. const uint32_t tId = inst->GetSingleWordInOperand(0);
  153. Instruction* tInst = get_def_use_mgr()->GetDef(tId);
  154. if (IsAnnotationInst(tInst->opcode())) {
  155. // This must be a decoration group. We go through annotations in a specific
  156. // order. So if this is not used by any group or group member decorates, it
  157. // is dead.
  158. assert(tInst->opcode() == SpvOpDecorationGroup);
  159. bool dead = true;
  160. get_def_use_mgr()->ForEachUser(tInst, [&dead](Instruction* user) {
  161. if (user->opcode() == SpvOpGroupDecorate ||
  162. user->opcode() == SpvOpGroupMemberDecorate)
  163. dead = false;
  164. });
  165. return dead;
  166. }
  167. return !IsLive(tInst);
  168. }
  169. void AggressiveDCEPass::ProcessLoad(Function* func, uint32_t varId) {
  170. // Only process locals
  171. if (!IsLocalVar(varId, func)) return;
  172. // Return if already processed
  173. if (live_local_vars_.find(varId) != live_local_vars_.end()) return;
  174. // Mark all stores to varId as live
  175. AddStores(func, varId);
  176. // Cache varId as processed
  177. live_local_vars_.insert(varId);
  178. }
  179. void AggressiveDCEPass::AddBranch(uint32_t labelId, BasicBlock* bp) {
  180. std::unique_ptr<Instruction> newBranch(
  181. new Instruction(context(), SpvOpBranch, 0, 0,
  182. {{spv_operand_type_t::SPV_OPERAND_TYPE_ID, {labelId}}}));
  183. context()->AnalyzeDefUse(&*newBranch);
  184. context()->set_instr_block(&*newBranch, bp);
  185. bp->AddInstruction(std::move(newBranch));
  186. }
  187. void AggressiveDCEPass::AddBreaksAndContinuesToWorklist(
  188. Instruction* mergeInst) {
  189. assert(mergeInst->opcode() == SpvOpSelectionMerge ||
  190. mergeInst->opcode() == SpvOpLoopMerge);
  191. BasicBlock* header = context()->get_instr_block(mergeInst);
  192. const uint32_t mergeId = mergeInst->GetSingleWordInOperand(0);
  193. get_def_use_mgr()->ForEachUser(mergeId, [header, this](Instruction* user) {
  194. if (!user->IsBranch()) return;
  195. BasicBlock* block = context()->get_instr_block(user);
  196. if (BlockIsInConstruct(header, block)) {
  197. // This is a break from the loop.
  198. AddToWorklist(user);
  199. // Add branch's merge if there is one.
  200. Instruction* userMerge = GetMergeInstruction(user);
  201. if (userMerge != nullptr) AddToWorklist(userMerge);
  202. }
  203. });
  204. if (mergeInst->opcode() != SpvOpLoopMerge) {
  205. return;
  206. }
  207. // For loops we need to find the continues as well.
  208. const uint32_t contId =
  209. mergeInst->GetSingleWordInOperand(kLoopMergeContinueBlockIdInIdx);
  210. get_def_use_mgr()->ForEachUser(contId, [&contId, this](Instruction* user) {
  211. SpvOp op = user->opcode();
  212. if (op == SpvOpBranchConditional || op == SpvOpSwitch) {
  213. // A conditional branch or switch can only be a continue if it does not
  214. // have a merge instruction or its merge block is not the continue block.
  215. Instruction* hdrMerge = GetMergeInstruction(user);
  216. if (hdrMerge != nullptr && hdrMerge->opcode() == SpvOpSelectionMerge) {
  217. uint32_t hdrMergeId =
  218. hdrMerge->GetSingleWordInOperand(kSelectionMergeMergeBlockIdInIdx);
  219. if (hdrMergeId == contId) return;
  220. // Need to mark merge instruction too
  221. AddToWorklist(hdrMerge);
  222. }
  223. } else if (op == SpvOpBranch) {
  224. // An unconditional branch can only be a continue if it is not
  225. // branching to its own merge block.
  226. BasicBlock* blk = context()->get_instr_block(user);
  227. Instruction* hdrBranch = GetHeaderBranch(blk);
  228. if (hdrBranch == nullptr) return;
  229. Instruction* hdrMerge = GetMergeInstruction(hdrBranch);
  230. if (hdrMerge->opcode() == SpvOpLoopMerge) return;
  231. uint32_t hdrMergeId =
  232. hdrMerge->GetSingleWordInOperand(kSelectionMergeMergeBlockIdInIdx);
  233. if (contId == hdrMergeId) return;
  234. } else {
  235. return;
  236. }
  237. AddToWorklist(user);
  238. });
  239. }
  240. bool AggressiveDCEPass::AggressiveDCE(Function* func) {
  241. std::list<BasicBlock*> structured_order;
  242. cfg()->ComputeStructuredOrder(func, &*func->begin(), &structured_order);
  243. live_local_vars_.clear();
  244. InitializeWorkList(func, structured_order);
  245. ProcessWorkList(func);
  246. return KillDeadInstructions(func, structured_order);
  247. }
  248. bool AggressiveDCEPass::KillDeadInstructions(
  249. const Function* func, std::list<BasicBlock*>& structured_order) {
  250. bool modified = false;
  251. for (auto bi = structured_order.begin(); bi != structured_order.end();) {
  252. uint32_t merge_block_id = 0;
  253. (*bi)->ForEachInst([this, &modified, &merge_block_id](Instruction* inst) {
  254. if (IsLive(inst)) return;
  255. if (inst->opcode() == SpvOpLabel) return;
  256. // If dead instruction is selection merge, remember merge block
  257. // for new branch at end of block
  258. if (inst->opcode() == SpvOpSelectionMerge ||
  259. inst->opcode() == SpvOpLoopMerge)
  260. merge_block_id = inst->GetSingleWordInOperand(0);
  261. to_kill_.push_back(inst);
  262. modified = true;
  263. });
  264. // If a structured if or loop was deleted, add a branch to its merge
  265. // block, and traverse to the merge block and continue processing there.
  266. // We know the block still exists because the label is not deleted.
  267. if (merge_block_id != 0) {
  268. AddBranch(merge_block_id, *bi);
  269. for (++bi; (*bi)->id() != merge_block_id; ++bi) {
  270. }
  271. auto merge_terminator = (*bi)->terminator();
  272. if (merge_terminator->opcode() == SpvOpUnreachable) {
  273. // The merge was unreachable. This is undefined behaviour so just
  274. // return (or return an undef). Then mark the new return as live.
  275. auto func_ret_type_inst = get_def_use_mgr()->GetDef(func->type_id());
  276. if (func_ret_type_inst->opcode() == SpvOpTypeVoid) {
  277. merge_terminator->SetOpcode(SpvOpReturn);
  278. } else {
  279. // Find an undef for the return value and make sure it gets kept by
  280. // the pass.
  281. auto undef_id = Type2Undef(func->type_id());
  282. auto undef = get_def_use_mgr()->GetDef(undef_id);
  283. live_insts_.Set(undef->unique_id());
  284. merge_terminator->SetOpcode(SpvOpReturnValue);
  285. merge_terminator->SetInOperands({{SPV_OPERAND_TYPE_ID, {undef_id}}});
  286. get_def_use_mgr()->AnalyzeInstUse(merge_terminator);
  287. }
  288. live_insts_.Set(merge_terminator->unique_id());
  289. }
  290. } else {
  291. Instruction* inst = (*bi)->terminator();
  292. if (!IsLive(inst)) {
  293. // If the terminator is not live, this block has no live instructions,
  294. // and it will be unreachable.
  295. AddUnreachable(*bi);
  296. }
  297. ++bi;
  298. }
  299. }
  300. return modified;
  301. }
  302. void AggressiveDCEPass::ProcessWorkList(Function* func) {
  303. while (!worklist_.empty()) {
  304. Instruction* live_inst = worklist_.front();
  305. worklist_.pop();
  306. AddOperandsToWorkList(live_inst);
  307. MarkBlockAsLive(live_inst);
  308. MarkLoadedVariablesAsLive(func, live_inst);
  309. AddDecorationsToWorkList(live_inst);
  310. AddDebugInstructionsToWorkList(live_inst);
  311. }
  312. }
  313. void AggressiveDCEPass::AddDebugInstructionsToWorkList(
  314. const Instruction* inst) {
  315. for (auto& line_inst : inst->dbg_line_insts()) {
  316. if (line_inst.IsDebugLineInst()) {
  317. AddOperandsToWorkList(&line_inst);
  318. }
  319. }
  320. if (inst->GetDebugScope().GetLexicalScope() != kNoDebugScope) {
  321. auto* scope =
  322. get_def_use_mgr()->GetDef(inst->GetDebugScope().GetLexicalScope());
  323. AddToWorklist(scope);
  324. }
  325. if (inst->GetDebugInlinedAt() != kNoInlinedAt) {
  326. auto* inlined_at = get_def_use_mgr()->GetDef(inst->GetDebugInlinedAt());
  327. AddToWorklist(inlined_at);
  328. }
  329. }
  330. void AggressiveDCEPass::AddDecorationsToWorkList(const Instruction* inst) {
  331. // Add OpDecorateId instructions that apply to this instruction to the work
  332. // list. We use the decoration manager to look through the group
  333. // decorations to get to the OpDecorate* instructions themselves.
  334. auto decorations =
  335. get_decoration_mgr()->GetDecorationsFor(inst->result_id(), false);
  336. for (Instruction* dec : decorations) {
  337. // We only care about OpDecorateId instructions because the are the only
  338. // decorations that will reference an id that will have to be kept live
  339. // because of that use.
  340. if (dec->opcode() != SpvOpDecorateId) {
  341. continue;
  342. }
  343. if (dec->GetSingleWordInOperand(1) ==
  344. SpvDecorationHlslCounterBufferGOOGLE) {
  345. // These decorations should not force the use id to be live. It will be
  346. // removed if either the target or the in operand are dead.
  347. continue;
  348. }
  349. AddToWorklist(dec);
  350. }
  351. }
  352. void AggressiveDCEPass::MarkLoadedVariablesAsLive(Function* func,
  353. Instruction* inst) {
  354. std::vector<uint32_t> live_variables = GetLoadedVariables(inst);
  355. for (uint32_t var_id : live_variables) {
  356. ProcessLoad(func, var_id);
  357. }
  358. }
  359. std::vector<uint32_t> AggressiveDCEPass::GetLoadedVariables(Instruction* inst) {
  360. if (inst->opcode() == SpvOpFunctionCall) {
  361. return GetLoadedVariablesFromFunctionCall(inst);
  362. }
  363. uint32_t var_id = GetLoadedVariableFromNonFunctionCalls(inst);
  364. if (var_id == 0) {
  365. return {};
  366. }
  367. return {var_id};
  368. }
  369. uint32_t AggressiveDCEPass::GetLoadedVariableFromNonFunctionCalls(
  370. Instruction* inst) {
  371. std::vector<uint32_t> live_variables;
  372. if (inst->IsAtomicWithLoad()) {
  373. return GetVariableId(inst->GetSingleWordInOperand(kLoadSourceAddrInIdx));
  374. }
  375. switch (inst->opcode()) {
  376. case SpvOpLoad:
  377. case SpvOpImageTexelPointer:
  378. return GetVariableId(inst->GetSingleWordInOperand(kLoadSourceAddrInIdx));
  379. case SpvOpCopyMemory:
  380. case SpvOpCopyMemorySized:
  381. return GetVariableId(
  382. inst->GetSingleWordInOperand(kCopyMemorySourceAddrInIdx));
  383. default:
  384. break;
  385. }
  386. switch (inst->GetCommonDebugOpcode()) {
  387. case CommonDebugInfoDebugDeclare:
  388. return inst->GetSingleWordOperand(kDebugDeclareOperandVariableIndex);
  389. case CommonDebugInfoDebugValue: {
  390. analysis::DebugInfoManager* debug_info_mgr =
  391. context()->get_debug_info_mgr();
  392. return debug_info_mgr->GetVariableIdOfDebugValueUsedForDeclare(inst);
  393. }
  394. default:
  395. break;
  396. }
  397. return 0;
  398. }
  399. std::vector<uint32_t> AggressiveDCEPass::GetLoadedVariablesFromFunctionCall(
  400. const Instruction* inst) {
  401. assert(inst->opcode() == SpvOpFunctionCall);
  402. std::vector<uint32_t> live_variables;
  403. inst->ForEachInId([this, &live_variables](const uint32_t* operand_id) {
  404. if (!IsPtr(*operand_id)) return;
  405. uint32_t var_id = GetVariableId(*operand_id);
  406. live_variables.push_back(var_id);
  407. });
  408. return live_variables;
  409. }
  410. uint32_t AggressiveDCEPass::GetVariableId(uint32_t ptr_id) {
  411. assert(IsPtr(ptr_id) &&
  412. "Cannot get the variable when input is not a pointer.");
  413. uint32_t varId = 0;
  414. (void)GetPtr(ptr_id, &varId);
  415. return varId;
  416. }
  417. void AggressiveDCEPass::MarkBlockAsLive(Instruction* inst) {
  418. BasicBlock* basic_block = context()->get_instr_block(inst);
  419. if (basic_block == nullptr) {
  420. return;
  421. }
  422. // If we intend to keep this instruction, we need the block label and
  423. // block terminator to have a valid block for the instruction.
  424. AddToWorklist(basic_block->GetLabelInst());
  425. // We need to mark the successors blocks that follow as live. If this is
  426. // header of the merge construct, the construct may be folded, but we will
  427. // definitely need the merge label. If it is not a construct, the terminator
  428. // must be live, and the successor blocks will be marked as live when
  429. // processing the terminator.
  430. uint32_t merge_id = basic_block->MergeBlockIdIfAny();
  431. if (merge_id == 0) {
  432. AddToWorklist(basic_block->terminator());
  433. } else {
  434. AddToWorklist(context()->get_def_use_mgr()->GetDef(merge_id));
  435. }
  436. // Mark the structured control flow constructs that contains this block as
  437. // live. If |inst| is an instruction in the loop header, then it is part of
  438. // the loop, so the loop construct must be live. We exclude the label because
  439. // it does not matter how many times it is executed. This could be extended
  440. // to more instructions, but we will need it for now.
  441. if (inst->opcode() != SpvOpLabel)
  442. MarkLoopConstructAsLiveIfLoopHeader(basic_block);
  443. Instruction* next_branch_inst = GetBranchForNextHeader(basic_block);
  444. if (next_branch_inst != nullptr) {
  445. AddToWorklist(next_branch_inst);
  446. Instruction* mergeInst = GetMergeInstruction(next_branch_inst);
  447. AddToWorklist(mergeInst);
  448. }
  449. if (inst->opcode() == SpvOpLoopMerge ||
  450. inst->opcode() == SpvOpSelectionMerge) {
  451. AddBreaksAndContinuesToWorklist(inst);
  452. }
  453. }
  454. void AggressiveDCEPass::MarkLoopConstructAsLiveIfLoopHeader(
  455. BasicBlock* basic_block) {
  456. // If this is the header for a loop, then loop structure needs to keep as well
  457. // because the loop header is also part of the loop.
  458. Instruction* merge_inst = basic_block->GetLoopMergeInst();
  459. if (merge_inst != nullptr) {
  460. AddToWorklist(basic_block->terminator());
  461. AddToWorklist(merge_inst);
  462. }
  463. }
  464. void AggressiveDCEPass::AddOperandsToWorkList(const Instruction* inst) {
  465. inst->ForEachInId([this](const uint32_t* iid) {
  466. Instruction* inInst = get_def_use_mgr()->GetDef(*iid);
  467. AddToWorklist(inInst);
  468. });
  469. if (inst->type_id() != 0) {
  470. AddToWorklist(get_def_use_mgr()->GetDef(inst->type_id()));
  471. }
  472. }
  473. void AggressiveDCEPass::InitializeWorkList(
  474. Function* func, std::list<BasicBlock*>& structured_order) {
  475. AddToWorklist(&func->DefInst());
  476. MarkFunctionParameterAsLive(func);
  477. MarkFirstBlockAsLive(func);
  478. // Add instructions with external side effects to the worklist. Also add
  479. // branches that are not attached to a structured construct.
  480. // TODO(s-perron): The handling of branch seems to be adhoc. This needs to be
  481. // cleaned up.
  482. for (auto& bi : structured_order) {
  483. for (auto ii = bi->begin(); ii != bi->end(); ++ii) {
  484. SpvOp op = ii->opcode();
  485. if (ii->IsBranch()) {
  486. continue;
  487. }
  488. switch (op) {
  489. case SpvOpStore: {
  490. uint32_t var_id = 0;
  491. (void)GetPtr(&*ii, &var_id);
  492. if (!IsLocalVar(var_id, func)) AddToWorklist(&*ii);
  493. } break;
  494. case SpvOpCopyMemory:
  495. case SpvOpCopyMemorySized: {
  496. uint32_t var_id = 0;
  497. uint32_t target_addr_id =
  498. ii->GetSingleWordInOperand(kCopyMemoryTargetAddrInIdx);
  499. (void)GetPtr(target_addr_id, &var_id);
  500. if (!IsLocalVar(var_id, func)) AddToWorklist(&*ii);
  501. } break;
  502. case SpvOpLoopMerge:
  503. case SpvOpSelectionMerge:
  504. case SpvOpUnreachable:
  505. break;
  506. default: {
  507. // Function calls, atomics, function params, function returns, etc.
  508. if (!ii->IsOpcodeSafeToDelete()) {
  509. AddToWorklist(&*ii);
  510. }
  511. } break;
  512. }
  513. }
  514. }
  515. }
  516. void AggressiveDCEPass::InitializeModuleScopeLiveInstructions() {
  517. // Keep all execution modes.
  518. for (auto& exec : get_module()->execution_modes()) {
  519. AddToWorklist(&exec);
  520. }
  521. // Keep all entry points.
  522. for (auto& entry : get_module()->entry_points()) {
  523. if (!preserve_interface_) {
  524. live_insts_.Set(entry.unique_id());
  525. // The actual function is live always.
  526. AddToWorklist(
  527. get_def_use_mgr()->GetDef(entry.GetSingleWordInOperand(1u)));
  528. for (uint32_t i = 3; i < entry.NumInOperands(); ++i) {
  529. auto* var = get_def_use_mgr()->GetDef(entry.GetSingleWordInOperand(i));
  530. auto storage_class = var->GetSingleWordInOperand(0u);
  531. // Vulkan support outputs without an associated input, but not inputs
  532. // without an associated output.
  533. if (storage_class == SpvStorageClassOutput) {
  534. AddToWorklist(var);
  535. }
  536. }
  537. } else {
  538. AddToWorklist(&entry);
  539. }
  540. }
  541. for (auto& anno : get_module()->annotations()) {
  542. if (anno.opcode() == SpvOpDecorate) {
  543. // Keep workgroup size.
  544. if (anno.GetSingleWordInOperand(1u) == SpvDecorationBuiltIn &&
  545. anno.GetSingleWordInOperand(2u) == SpvBuiltInWorkgroupSize) {
  546. AddToWorklist(&anno);
  547. }
  548. if (context()->preserve_bindings()) {
  549. // Keep all bindings.
  550. if ((anno.GetSingleWordInOperand(1u) == SpvDecorationDescriptorSet) ||
  551. (anno.GetSingleWordInOperand(1u) == SpvDecorationBinding)) {
  552. AddToWorklist(&anno);
  553. }
  554. }
  555. if (context()->preserve_spec_constants()) {
  556. // Keep all specialization constant instructions
  557. if (anno.GetSingleWordInOperand(1u) == SpvDecorationSpecId) {
  558. AddToWorklist(&anno);
  559. }
  560. }
  561. }
  562. }
  563. // For each DebugInfo GlobalVariable keep all operands except the Variable.
  564. // Later, if the variable is killed with KillInst(), we will set the operand
  565. // to DebugInfoNone. Create and save DebugInfoNone now for this possible
  566. // later use. This is slightly unoptimal, but it avoids generating it during
  567. // instruction killing when the module is not consistent.
  568. bool debug_global_seen = false;
  569. for (auto& dbg : get_module()->ext_inst_debuginfo()) {
  570. if (dbg.GetCommonDebugOpcode() != CommonDebugInfoDebugGlobalVariable)
  571. continue;
  572. debug_global_seen = true;
  573. dbg.ForEachInId([this](const uint32_t* iid) {
  574. Instruction* in_inst = get_def_use_mgr()->GetDef(*iid);
  575. if (in_inst->opcode() == SpvOpVariable) return;
  576. AddToWorklist(in_inst);
  577. });
  578. }
  579. if (debug_global_seen) {
  580. auto dbg_none = context()->get_debug_info_mgr()->GetDebugInfoNone();
  581. AddToWorklist(dbg_none);
  582. }
  583. }
  584. Pass::Status AggressiveDCEPass::ProcessImpl() {
  585. // Current functionality assumes shader capability
  586. // TODO(greg-lunarg): Handle additional capabilities
  587. if (!context()->get_feature_mgr()->HasCapability(SpvCapabilityShader))
  588. return Status::SuccessWithoutChange;
  589. // Current functionality assumes relaxed logical addressing (see
  590. // instruction.h)
  591. // TODO(greg-lunarg): Handle non-logical addressing
  592. if (context()->get_feature_mgr()->HasCapability(SpvCapabilityAddresses))
  593. return Status::SuccessWithoutChange;
  594. // The variable pointer extension is no longer needed to use the capability,
  595. // so we have to look for the capability.
  596. if (context()->get_feature_mgr()->HasCapability(
  597. SpvCapabilityVariablePointersStorageBuffer))
  598. return Status::SuccessWithoutChange;
  599. // If any extensions in the module are not explicitly supported,
  600. // return unmodified.
  601. if (!AllExtensionsSupported()) return Status::SuccessWithoutChange;
  602. // Eliminate Dead functions.
  603. bool modified = EliminateDeadFunctions();
  604. InitializeModuleScopeLiveInstructions();
  605. // Run |AggressiveDCE| on the remaining functions. The order does not matter,
  606. // since |AggressiveDCE| is intra-procedural. This can mean that function
  607. // will become dead if all function call to them are removed. These dead
  608. // function will still be in the module after this pass. We expect this to be
  609. // rare.
  610. for (Function& fp : *context()->module()) {
  611. modified |= AggressiveDCE(&fp);
  612. }
  613. // If the decoration manager is kept live then the context will try to keep it
  614. // up to date. ADCE deals with group decorations by changing the operands in
  615. // |OpGroupDecorate| instruction directly without informing the decoration
  616. // manager. This can put it in an invalid state which will cause an error
  617. // when the context tries to update it. To avoid this problem invalidate
  618. // the decoration manager upfront.
  619. //
  620. // We kill it at now because it is used when processing the entry point
  621. // functions.
  622. context()->InvalidateAnalyses(IRContext::Analysis::kAnalysisDecorations);
  623. // Process module-level instructions. Now that all live instructions have
  624. // been marked, it is safe to remove dead global values.
  625. modified |= ProcessGlobalValues();
  626. assert((to_kill_.empty() || modified) &&
  627. "A dead instruction was identified, but no change recorded.");
  628. // Kill all dead instructions.
  629. for (auto inst : to_kill_) {
  630. context()->KillInst(inst);
  631. }
  632. // Cleanup all CFG including all unreachable blocks.
  633. for (Function& fp : *context()->module()) {
  634. modified |= CFGCleanup(&fp);
  635. }
  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 (!preserve_interface_) {
  817. // Remove the dead interface variables from the entry point interface list.
  818. for (auto& entry : get_module()->entry_points()) {
  819. std::vector<Operand> new_operands;
  820. for (uint32_t i = 0; i < entry.NumInOperands(); ++i) {
  821. if (i < 3) {
  822. // Execution model, function id and name are always valid.
  823. new_operands.push_back(entry.GetInOperand(i));
  824. } else {
  825. auto* var =
  826. get_def_use_mgr()->GetDef(entry.GetSingleWordInOperand(i));
  827. if (IsLive(var)) {
  828. new_operands.push_back(entry.GetInOperand(i));
  829. }
  830. }
  831. }
  832. if (new_operands.size() != entry.NumInOperands()) {
  833. entry.SetInOperands(std::move(new_operands));
  834. get_def_use_mgr()->UpdateDefUse(&entry);
  835. }
  836. }
  837. }
  838. return modified;
  839. }
  840. Pass::Status AggressiveDCEPass::Process() {
  841. // Initialize extensions allowlist
  842. InitExtensions();
  843. return ProcessImpl();
  844. }
  845. void AggressiveDCEPass::InitExtensions() {
  846. extensions_allowlist_.clear();
  847. extensions_allowlist_.insert({
  848. "SPV_AMD_shader_explicit_vertex_parameter",
  849. "SPV_AMD_shader_trinary_minmax",
  850. "SPV_AMD_gcn_shader",
  851. "SPV_KHR_shader_ballot",
  852. "SPV_AMD_shader_ballot",
  853. "SPV_AMD_gpu_shader_half_float",
  854. "SPV_KHR_shader_draw_parameters",
  855. "SPV_KHR_subgroup_vote",
  856. "SPV_KHR_8bit_storage",
  857. "SPV_KHR_16bit_storage",
  858. "SPV_KHR_device_group",
  859. "SPV_KHR_multiview",
  860. "SPV_NVX_multiview_per_view_attributes",
  861. "SPV_NV_viewport_array2",
  862. "SPV_NV_stereo_view_rendering",
  863. "SPV_NV_sample_mask_override_coverage",
  864. "SPV_NV_geometry_shader_passthrough",
  865. "SPV_AMD_texture_gather_bias_lod",
  866. "SPV_KHR_storage_buffer_storage_class",
  867. // SPV_KHR_variable_pointers
  868. // Currently do not support extended pointer expressions
  869. "SPV_AMD_gpu_shader_int16",
  870. "SPV_KHR_post_depth_coverage",
  871. "SPV_KHR_shader_atomic_counter_ops",
  872. "SPV_EXT_shader_stencil_export",
  873. "SPV_EXT_shader_viewport_index_layer",
  874. "SPV_AMD_shader_image_load_store_lod",
  875. "SPV_AMD_shader_fragment_mask",
  876. "SPV_EXT_fragment_fully_covered",
  877. "SPV_AMD_gpu_shader_half_float_fetch",
  878. "SPV_GOOGLE_decorate_string",
  879. "SPV_GOOGLE_hlsl_functionality1",
  880. "SPV_GOOGLE_user_type",
  881. "SPV_NV_shader_subgroup_partitioned",
  882. "SPV_EXT_demote_to_helper_invocation",
  883. "SPV_EXT_descriptor_indexing",
  884. "SPV_NV_fragment_shader_barycentric",
  885. "SPV_NV_compute_shader_derivatives",
  886. "SPV_NV_shader_image_footprint",
  887. "SPV_NV_shading_rate",
  888. "SPV_NV_mesh_shader",
  889. "SPV_NV_ray_tracing",
  890. "SPV_KHR_ray_tracing",
  891. "SPV_KHR_ray_query",
  892. "SPV_EXT_fragment_invocation_density",
  893. "SPV_EXT_physical_storage_buffer",
  894. "SPV_KHR_terminate_invocation",
  895. "SPV_KHR_shader_clock",
  896. "SPV_KHR_vulkan_memory_model",
  897. "SPV_KHR_subgroup_uniform_control_flow",
  898. "SPV_KHR_integer_dot_product",
  899. "SPV_EXT_shader_image_int64",
  900. "SPV_KHR_non_semantic_info",
  901. "SPV_KHR_uniform_group_instructions",
  902. "SPV_KHR_fragment_shader_barycentric",
  903. });
  904. }
  905. Instruction* AggressiveDCEPass::GetHeaderBranch(BasicBlock* blk) {
  906. if (blk == nullptr) {
  907. return nullptr;
  908. }
  909. BasicBlock* header_block = GetHeaderBlock(blk);
  910. if (header_block == nullptr) {
  911. return nullptr;
  912. }
  913. return header_block->terminator();
  914. }
  915. BasicBlock* AggressiveDCEPass::GetHeaderBlock(BasicBlock* blk) const {
  916. if (blk == nullptr) {
  917. return nullptr;
  918. }
  919. BasicBlock* header_block = nullptr;
  920. if (blk->IsLoopHeader()) {
  921. header_block = blk;
  922. } else {
  923. uint32_t header =
  924. context()->GetStructuredCFGAnalysis()->ContainingConstruct(blk->id());
  925. header_block = context()->get_instr_block(header);
  926. }
  927. return header_block;
  928. }
  929. Instruction* AggressiveDCEPass::GetMergeInstruction(Instruction* inst) {
  930. BasicBlock* bb = context()->get_instr_block(inst);
  931. if (bb == nullptr) {
  932. return nullptr;
  933. }
  934. return bb->GetMergeInst();
  935. }
  936. Instruction* AggressiveDCEPass::GetBranchForNextHeader(BasicBlock* blk) {
  937. if (blk == nullptr) {
  938. return nullptr;
  939. }
  940. if (blk->IsLoopHeader()) {
  941. uint32_t header =
  942. context()->GetStructuredCFGAnalysis()->ContainingConstruct(blk->id());
  943. blk = context()->get_instr_block(header);
  944. }
  945. return GetHeaderBranch(blk);
  946. }
  947. void AggressiveDCEPass::MarkFunctionParameterAsLive(const Function* func) {
  948. func->ForEachParam(
  949. [this](const Instruction* param) {
  950. AddToWorklist(const_cast<Instruction*>(param));
  951. },
  952. false);
  953. }
  954. bool AggressiveDCEPass::BlockIsInConstruct(BasicBlock* header_block,
  955. BasicBlock* bb) {
  956. if (bb == nullptr || header_block == nullptr) {
  957. return false;
  958. }
  959. uint32_t current_header = bb->id();
  960. while (current_header != 0) {
  961. if (current_header == header_block->id()) return true;
  962. current_header = context()->GetStructuredCFGAnalysis()->ContainingConstruct(
  963. current_header);
  964. }
  965. return false;
  966. }
  967. bool AggressiveDCEPass::IsEntryPointWithNoCalls(Function* func) {
  968. auto cached_result = entry_point_with_no_calls_cache_.find(func->result_id());
  969. if (cached_result != entry_point_with_no_calls_cache_.end()) {
  970. return cached_result->second;
  971. }
  972. bool result = IsEntryPoint(func) && !HasCall(func);
  973. entry_point_with_no_calls_cache_[func->result_id()] = result;
  974. return result;
  975. }
  976. bool AggressiveDCEPass::IsEntryPoint(Function* func) {
  977. for (const Instruction& entry_point : get_module()->entry_points()) {
  978. uint32_t entry_point_id =
  979. entry_point.GetSingleWordInOperand(kEntryPointFunctionIdInIdx);
  980. if (entry_point_id == func->result_id()) {
  981. return true;
  982. }
  983. }
  984. return false;
  985. }
  986. bool AggressiveDCEPass::HasCall(Function* func) {
  987. return !func->WhileEachInst(
  988. [](Instruction* inst) { return inst->opcode() != SpvOpFunctionCall; });
  989. }
  990. void AggressiveDCEPass::MarkFirstBlockAsLive(Function* func) {
  991. BasicBlock* first_block = &*func->begin();
  992. MarkBlockAsLive(first_block->GetLabelInst());
  993. }
  994. void AggressiveDCEPass::AddUnreachable(BasicBlock*& block) {
  995. InstructionBuilder builder(
  996. context(), block,
  997. IRContext::kAnalysisInstrToBlockMapping | IRContext::kAnalysisDefUse);
  998. builder.AddUnreachable();
  999. }
  1000. } // namespace opt
  1001. } // namespace spvtools