LLVM 24.0.0git
SPIRVRemoveUnusedResources.cpp
Go to the documentation of this file.
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"
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
29using namespace llvm;
30
32 if (!M.getContext()
33 .getLibraryOptions()
34 .get<SPIRVOptions>()
35 .enable_remove_unused_resources)
36 return false;
37
39 SmallVector<IntrinsicInst *> CounterHandles;
41
42 for (Function &F : M) {
43 for (Instruction &I : instructions(F)) {
44 if (auto *II = dyn_cast<IntrinsicInst>(&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(II);
51 break;
52 case Intrinsic::spv_resource_counterhandlefrombinding:
53 case Intrinsic::spv_resource_counterhandlefromimplicitbinding:
54 CounterHandles.push_back(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(Handle->users())) {
65 auto *SI = dyn_cast<StoreInst>(U);
66 if (!SI || SI->isVolatile() || SI->getValueOperand() != Handle)
67 continue;
68 auto *GV = dyn_cast<GlobalVariable>(SI->getPointerOperand());
69 if (!GV || !GV->hasLocalLinkage() || !GV->hasOneUser())
70 continue;
71 SI->eraseFromParent();
72 DeadGlobals.insert(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 Handle->getOperand(ResourceNameOpIndex)))
81 DeadGlobals.insert(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
110
111namespace {
112class SPIRVRemoveUnusedResourcesLegacy : public ModulePass {
113public:
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};
124char SPIRVRemoveUnusedResourcesLegacy::ID = 0;
125} // namespace
126
127INITIALIZE_PASS(SPIRVRemoveUnusedResourcesLegacy, DEBUG_TYPE,
128 "SPIRV Remove Unused Resources", false, false)
129
131 return new SPIRVRemoveUnusedResourcesLegacy();
132}
Expand Atomic instructions
static bool removeUnusedResources(Function &F)
#define DEBUG_TYPE
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static bool removeUnusedResources(Module &M)
This file contains some templates that are useful if you are working with the STL at all.
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallVector class.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
iterator_range< user_iterator > users()
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition Pass.h:253
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:157
A SetVector that performs no allocations if smaller than a certain size.
Definition SetVector.h:346
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Value * getOperand(unsigned i) const
Definition User.h:207
bool use_empty() const
Definition Value.h:348
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:649
ModulePass * createSPIRVRemoveUnusedResourcesLegacyPass()
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39