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_mdname:
49 case Intrinsic::spv_resource_handlefromimplicitbinding_mdname:
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) {
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 if (Handle->use_empty()) {
77 Handle->eraseFromParent();
78 Changed = true;
79 }
80 };
81
82 // Visit implicit counters first since they may use other handles
83 // that are otherwise dead
84 for (auto *Counter : CounterHandles)
85 VisitHandle(Counter);
86 for (auto *Handle : Handles)
87 VisitHandle(Handle);
88
89 for (auto *GV : DeadGlobals) {
90 if (GV->user_empty()) {
91 GV->eraseFromParent();
92 Changed = true;
93 }
94 }
95
96 return Changed;
97}
98
104
105namespace {
106class SPIRVRemoveUnusedResourcesLegacy : public ModulePass {
107public:
108 SPIRVRemoveUnusedResourcesLegacy() : ModulePass(ID) {}
109
110 bool runOnModule(Module &M) override { return removeUnusedResources(M); }
111
112 StringRef getPassName() const override {
113 return "SPIRV Remove Unused Resources";
114 }
115
116 static char ID;
117};
118char SPIRVRemoveUnusedResourcesLegacy::ID = 0;
119} // namespace
120
121INITIALIZE_PASS(SPIRVRemoveUnusedResourcesLegacy, DEBUG_TYPE,
122 "SPIRV Remove Unused Resources", false, false)
123
125 return new SPIRVRemoveUnusedResourcesLegacy();
126}
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.
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.
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