1//===- DXILResourceAccess.cpp - Resource access via load/store ------------===//
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#include "DirectX.h"
10#include "llvm/ADT/SetVector.h"
11#include "llvm/Analysis/VectorUtils.h"
12#include "llvm/IR/BasicBlock.h"
13#include "llvm/IR/DiagnosticInfo.h"
14#include "llvm/IR/Dominators.h"
15#include "llvm/IR/Instruction.h"
16#include "llvm/IR/Instructions.h"
17#include "llvm/IR/IntrinsicInst.h"
18#include "llvm/IR/Intrinsics.h"
19#include "llvm/IR/IntrinsicsDirectX.h"
20#include "llvm/IR/LLVMContext.h"
21#include "llvm/InitializePasses.h"
22#include "llvm/Transforms/Utils/ValueMapper.h"
23
24#define DEBUG_TYPE "dxil-remove-unused-resources"
25
26// Hidden option to disable the pass to make it easier to test
27// other passes related to DXIL resources using llc.
28static llvm::cl::opt<bool> DisableDXILRemoveUnusedResources(
29 "disable-dxil-remove-unused-resources",
30 llvm::cl::desc("Disable dxil-remove-unused-resources pass"),
31 llvm::cl::init(Val: false), llvm::cl::Hidden);
32
33using namespace llvm;
34
35static bool isResourceHandleCreation(Intrinsic::ID ID) {
36 return ID == Intrinsic::dx_resource_handlefrombinding ||
37 ID == Intrinsic::dx_resource_handlefromimplicitbinding ||
38 ID == Intrinsic::dx_resource_handlefromheap;
39}
40
41// Removes all calls to resource handle creation intrinsics that
42// either are not used, or their only use is in a store instruction, which
43// stores the initialized handle into a global variable that does not have
44// external linkage and that is not used anywhere else in the module.
45static bool removeUnusedResources(Function &F) {
46 if (DisableDXILRemoveUnusedResources)
47 return false;
48
49 SmallVector<Instruction *> DeadInstr;
50 SmallSetVector<GlobalVariable *, 4> DeadGlobals;
51 for (BasicBlock &BB : make_early_inc_range(Range&: F)) {
52 for (Instruction &I : BB) {
53 if (auto *II = dyn_cast<IntrinsicInst>(Val: &I)) {
54 if (!isResourceHandleCreation(ID: II->getIntrinsicID()))
55 continue;
56 if (II->user_empty()) {
57 // Initialized handle is not used anywhere.
58 DeadInstr.push_back(Elt: II);
59 continue;
60 }
61 if (!II->hasOneUser())
62 continue;
63
64 // Initialized handle is only used in one store instruction, the store
65 // is into global variable, and that global variable is not used
66 // anywhere else and does not have external linkage.
67 auto *SI = dyn_cast<StoreInst>(Val: *II->user_begin());
68 if (!SI)
69 continue;
70 assert(SI->getValueOperand() == II &&
71 "expected value operand to be the resource handle");
72
73 GlobalVariable *GV = dyn_cast<GlobalVariable>(Val: SI->getPointerOperand());
74 if (!GV || GV->hasExternalLinkage())
75 continue;
76
77 if (GV->hasOneUser()) {
78 assert(*GV->user_begin() == SI &&
79 "expected single user to be the store instruction");
80 DeadInstr.push_back(Elt: SI);
81 DeadInstr.push_back(Elt: II);
82 DeadGlobals.insert(X: GV);
83 }
84 }
85 }
86 }
87
88 if (DeadInstr.empty())
89 return false;
90
91 for (auto *Instr : DeadInstr) {
92 if (auto *II = dyn_cast<IntrinsicInst>(Val: Instr)) {
93 assert(isResourceHandleCreation(II->getIntrinsicID()));
94 // A heap resource does not have an associated global variable to remove.
95 if (II->getIntrinsicID() == Intrinsic::dx_resource_handlefromheap)
96 continue;
97 const unsigned ResourceNameOpIndex = 4;
98 GlobalVariable *ResourceName = dyn_cast_or_null<GlobalVariable>(
99 Val: II->getArgOperand(i: ResourceNameOpIndex));
100 if (ResourceName)
101 DeadGlobals.insert(X: ResourceName);
102 }
103 Instr->eraseFromParent();
104 }
105
106 for (auto *GV : DeadGlobals)
107 if (GV->use_empty())
108 GV->eraseFromParent();
109
110 return true;
111}
112
113PreservedAnalyses
114DXILRemoveUnusedResourcesPass::run(Function &F, FunctionAnalysisManager &AM) {
115 removeUnusedResources(F);
116 return PreservedAnalyses::all();
117}
118
119namespace {
120class DXILRemoveUnusedResourcesLegacy : public FunctionPass {
121public:
122 bool runOnFunction(Function &F) override { return removeUnusedResources(F); }
123 StringRef getPassName() const override {
124 return "DXIL Remove Unused Resources";
125 }
126 DXILRemoveUnusedResourcesLegacy() : FunctionPass(ID) {}
127
128 static char ID; // Pass identification.
129 void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {
130 AU.setPreservesAll();
131 }
132};
133char DXILRemoveUnusedResourcesLegacy::ID = 0;
134} // end anonymous namespace
135
136INITIALIZE_PASS_BEGIN(DXILRemoveUnusedResourcesLegacy, DEBUG_TYPE,
137 "DXIL Remove Unused Resources", false, false)
138INITIALIZE_PASS_DEPENDENCY(DXILResourceTypeWrapperPass)
139INITIALIZE_PASS_END(DXILRemoveUnusedResourcesLegacy, DEBUG_TYPE,
140 "DXIL Remove Unused Resources", false, false)
141
142FunctionPass *llvm::createDXILRemoveUnusedResourcesLegacyPass() {
143 return new DXILRemoveUnusedResourcesLegacy();
144}
145