| 1 | //===- SPIRVRemoveUnusedResources.cpp - Remove unused resources ----------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // \file Pass for removing unused resources and their associated |
| 10 | // global variables |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "SPIRV.h" |
| 15 | #include "SPIRVSubtarget.h" |
| 16 | #include "llvm/ADT/STLExtras.h" |
| 17 | #include "llvm/ADT/SetVector.h" |
| 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | #include "llvm/IR/GlobalVariable.h" |
| 20 | #include "llvm/IR/InstIterator.h" |
| 21 | #include "llvm/IR/Instructions.h" |
| 22 | #include "llvm/IR/IntrinsicInst.h" |
| 23 | #include "llvm/IR/IntrinsicsSPIRV.h" |
| 24 | #include "llvm/IR/Module.h" |
| 25 | #include "llvm/Pass.h" |
| 26 | |
| 27 | #define DEBUG_TYPE "spirv-remove-unused-resources" |
| 28 | |
| 29 | using namespace llvm; |
| 30 | |
| 31 | static bool removeUnusedResources(Module &M) { |
| 32 | if (!M.getContext() |
| 33 | .getLibraryOptions() |
| 34 | .get<SPIRVOptions>() |
| 35 | .enable_remove_unused_resources) |
| 36 | return false; |
| 37 | |
| 38 | SmallVector<IntrinsicInst *> Handles; |
| 39 | SmallVector<IntrinsicInst *> CounterHandles; |
| 40 | SmallSetVector<GlobalVariable *, 4> DeadGlobals; |
| 41 | |
| 42 | for (Function &F : M) { |
| 43 | for (Instruction &I : instructions(F)) { |
| 44 | if (auto *II = dyn_cast<IntrinsicInst>(Val: &I)) { |
| 45 | switch (II->getIntrinsicID()) { |
| 46 | default: |
| 47 | continue; |
| 48 | case Intrinsic::spv_resource_handlefrombinding: |
| 49 | case Intrinsic::spv_resource_handlefromimplicitbinding: |
| 50 | Handles.push_back(Elt: II); |
| 51 | break; |
| 52 | case Intrinsic::spv_resource_counterhandlefrombinding: |
| 53 | case Intrinsic::spv_resource_counterhandlefromimplicitbinding: |
| 54 | CounterHandles.push_back(Elt: II); |
| 55 | break; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | bool Changed = false; |
| 62 | |
| 63 | auto VisitHandle = [&](Instruction *Handle, bool IsCounter) { |
| 64 | for (User *U : make_early_inc_range(Range: Handle->users())) { |
| 65 | auto *SI = dyn_cast<StoreInst>(Val: U); |
| 66 | if (!SI || SI->isVolatile() || SI->getValueOperand() != Handle) |
| 67 | continue; |
| 68 | auto *GV = dyn_cast<GlobalVariable>(Val: SI->getPointerOperand()); |
| 69 | if (!GV || !GV->hasLocalLinkage() || !GV->hasOneUser()) |
| 70 | continue; |
| 71 | SI->eraseFromParent(); |
| 72 | DeadGlobals.insert(X: GV); |
| 73 | Changed = true; |
| 74 | } |
| 75 | |
| 76 | const unsigned ResourceNameOpIndex = 4; |
| 77 | if (Handle->use_empty()) { |
| 78 | if (!IsCounter) { |
| 79 | if (auto *Name = dyn_cast<GlobalVariable>( |
| 80 | Val: Handle->getOperand(i: ResourceNameOpIndex))) |
| 81 | DeadGlobals.insert(X: Name); |
| 82 | } |
| 83 | Handle->eraseFromParent(); |
| 84 | Changed = true; |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | // Visit implicit counters first since they may use other handles |
| 89 | // that are otherwise dead |
| 90 | for (auto *Counter : CounterHandles) |
| 91 | VisitHandle(Counter, true); |
| 92 | for (auto *Handle : Handles) |
| 93 | VisitHandle(Handle, false); |
| 94 | |
| 95 | for (auto *GV : DeadGlobals) { |
| 96 | if (GV->user_empty()) { |
| 97 | GV->eraseFromParent(); |
| 98 | Changed = true; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return Changed; |
| 103 | } |
| 104 | |
| 105 | PreservedAnalyses |
| 106 | SPIRVRemoveUnusedResourcesPass::run(Module &M, ModuleAnalysisManager &AM) { |
| 107 | return removeUnusedResources(M) ? PreservedAnalyses::none() |
| 108 | : PreservedAnalyses::all(); |
| 109 | } |
| 110 | |
| 111 | namespace { |
| 112 | class SPIRVRemoveUnusedResourcesLegacy : public ModulePass { |
| 113 | public: |
| 114 | SPIRVRemoveUnusedResourcesLegacy() : ModulePass(ID) {} |
| 115 | |
| 116 | bool runOnModule(Module &M) override { return removeUnusedResources(M); } |
| 117 | |
| 118 | StringRef getPassName() const override { |
| 119 | return "SPIRV Remove Unused Resources" ; |
| 120 | } |
| 121 | |
| 122 | static char ID; |
| 123 | }; |
| 124 | char SPIRVRemoveUnusedResourcesLegacy::ID = 0; |
| 125 | } // namespace |
| 126 | |
| 127 | INITIALIZE_PASS(SPIRVRemoveUnusedResourcesLegacy, DEBUG_TYPE, |
| 128 | "SPIRV Remove Unused Resources" , false, false) |
| 129 | |
| 130 | ModulePass *llvm::createSPIRVRemoveUnusedResourcesLegacyPass() { |
| 131 | return new SPIRVRemoveUnusedResourcesLegacy(); |
| 132 | } |
| 133 | |