1//===- DXILForwardHandleAccesses.cpp - Cleanup Handles --------------------===//
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 "DXILShaderFlags.h"
10#include "DirectX.h"
11#include "llvm/ADT/STLExtras.h"
12#include "llvm/Analysis/DXILResource.h"
13#include "llvm/Analysis/Loads.h"
14#include "llvm/IR/DiagnosticInfo.h"
15#include "llvm/IR/Dominators.h"
16#include "llvm/IR/InstrTypes.h"
17#include "llvm/IR/Instructions.h"
18#include "llvm/IR/IntrinsicInst.h"
19#include "llvm/IR/Intrinsics.h"
20#include "llvm/IR/IntrinsicsDirectX.h"
21#include "llvm/IR/Module.h"
22#include "llvm/InitializePasses.h"
23#include "llvm/Pass.h"
24#include "llvm/Transforms/Utils/Local.h"
25
26#define DEBUG_TYPE "dxil-forward-handle-accesses"
27
28using namespace llvm;
29
30static void diagnoseAmbiguousHandle(IntrinsicInst *NewII,
31 IntrinsicInst *PrevII) {
32 Function *F = NewII->getFunction();
33 LLVMContext &Context = F->getParent()->getContext();
34 Context.diagnose(DI: DiagnosticInfoGeneric(
35 Twine("Handle at \"") + NewII->getName() + "\" overwrites handle at \"" +
36 PrevII->getName() + "\""));
37}
38
39static void diagnoseHandleNotFound(LoadInst *LI) {
40 Function *F = LI->getFunction();
41 LLVMContext &Context = F->getParent()->getContext();
42 Context.diagnose(DI: DiagnosticInfoGeneric(
43 LI, Twine("Load of \"") + LI->getPointerOperand()->getName() +
44 "\" is not a global resource handle"));
45}
46
47static void diagnoseUndominatedLoad(LoadInst *LI, IntrinsicInst *Handle) {
48 Function *F = LI->getFunction();
49 LLVMContext &Context = F->getParent()->getContext();
50 Context.diagnose(DI: DiagnosticInfoGeneric(
51 LI, Twine("Load at \"") + LI->getName() +
52 "\" is not dominated by handle creation at \"" +
53 Handle->getName() + "\""));
54}
55
56static void
57processHandle(IntrinsicInst *II,
58 DenseMap<GlobalVariable *, IntrinsicInst *> &HandleMap) {
59 for (User *U : II->users())
60 if (auto *SI = dyn_cast<StoreInst>(Val: U))
61 if (auto *GV = dyn_cast<GlobalVariable>(Val: SI->getPointerOperand())) {
62 auto Entry = HandleMap.try_emplace(Key: GV, Args&: II);
63 if (Entry.second)
64 LLVM_DEBUG(dbgs() << "Added " << GV->getName() << " to handle map\n");
65 else
66 diagnoseAmbiguousHandle(NewII: II, PrevII: Entry.first->second);
67 }
68}
69
70static bool forwardHandleAccesses(Function &F, DominatorTree &DT) {
71 bool Changed = false;
72
73 DenseMap<GlobalVariable *, IntrinsicInst *> HandleMap;
74 SmallVector<LoadInst *> LoadsToProcess;
75 DenseMap<AllocaInst *, SmallVector<IntrinsicInst *>> LifeTimeIntrinsicMap;
76 for (BasicBlock &BB : F)
77 for (Instruction &Inst : BB)
78 if (auto *II = dyn_cast<IntrinsicInst>(Val: &Inst)) {
79 switch (II->getIntrinsicID()) {
80 case Intrinsic::dx_resource_handlefrombinding:
81 case Intrinsic::dx_resource_handlefromimplicitbinding:
82 processHandle(II, HandleMap);
83 break;
84 case Intrinsic::lifetime_start:
85 case Intrinsic::lifetime_end:
86 if (II->arg_size() >= 1) {
87 Value *Ptr = II->getArgOperand(i: 0);
88 if (auto *Alloca = dyn_cast<AllocaInst>(Val: Ptr))
89 LifeTimeIntrinsicMap[Alloca].push_back(Elt: II);
90 }
91 break;
92 default:
93 continue;
94 }
95 } else if (auto *LI = dyn_cast<LoadInst>(Val: &Inst))
96 if (isa<dxil::AnyResourceExtType>(Val: LI->getType()))
97 LoadsToProcess.push_back(Elt: LI);
98
99 for (LoadInst *LI : LoadsToProcess) {
100 Value *V = LI->getPointerOperand();
101 auto *GV = dyn_cast<GlobalVariable>(Val: V);
102
103 // If we didn't find the global, we may need to walk through a level of
104 // indirection. This generally happens at -O0.
105 if (!GV) {
106 if (auto *NestedLI = dyn_cast<LoadInst>(Val: V)) {
107 BasicBlock::iterator BBI(NestedLI);
108 Value *Loaded = FindAvailableLoadedValue(
109 Load: NestedLI, ScanBB: NestedLI->getParent(), ScanFrom&: BBI, MaxInstsToScan: 0, AA: nullptr, IsLoadCSE: nullptr);
110 GV = dyn_cast_or_null<GlobalVariable>(Val: Loaded);
111 } else if (auto *NestedAlloca = dyn_cast<AllocaInst>(Val: V)) {
112
113 if (auto It = LifeTimeIntrinsicMap.find(Val: NestedAlloca);
114 It != LifeTimeIntrinsicMap.end()) {
115 llvm::for_each(Range&: It->second,
116 F: [](IntrinsicInst *II) { II->eraseFromParent(); });
117 LifeTimeIntrinsicMap.erase(I: It);
118 }
119
120 for (auto *User : NestedAlloca->users()) {
121 auto *Store = dyn_cast<StoreInst>(Val: User);
122 if (!Store)
123 continue;
124
125 Value *StoredVal = Store->getValueOperand();
126 if (!StoredVal)
127 continue;
128
129 // Try direct global match
130 GV = dyn_cast<GlobalVariable>(Val: StoredVal);
131 if (GV)
132 break;
133
134 // If it's a load, check its source
135 if (auto *Load = dyn_cast<LoadInst>(Val: StoredVal)) {
136 GV = dyn_cast<GlobalVariable>(Val: Load->getPointerOperand());
137 if (GV)
138 break;
139
140 // If loading from an unmodified stack copy of the global, reuse the
141 // global's value. Note: we are just repeating what we are doing for
142 // the load case for the alloca store pattern.
143 BasicBlock::iterator BBI(Load);
144 Value *Loaded = FindAvailableLoadedValue(Load, ScanBB: Load->getParent(),
145 ScanFrom&: BBI, MaxInstsToScan: 0, AA: nullptr, IsLoadCSE: nullptr);
146 GV = dyn_cast<GlobalVariable>(Val: Loaded);
147 if (GV)
148 break;
149 }
150 }
151 }
152 }
153
154 auto It = HandleMap.find(Val: GV);
155 if (It == HandleMap.end()) {
156 diagnoseHandleNotFound(LI);
157 continue;
158 }
159 Changed = true;
160
161 if (!DT.dominates(Def: It->second, User: LI)) {
162 diagnoseUndominatedLoad(LI, Handle: It->second);
163 continue;
164 }
165
166 LLVM_DEBUG(dbgs() << "Replacing uses of " << GV->getName() << " at "
167 << LI->getName() << " with " << It->second->getName()
168 << "\n");
169 LI->replaceAllUsesWith(V: It->second);
170 LI->eraseFromParent();
171 }
172
173 return Changed;
174}
175
176PreservedAnalyses
177DXILForwardHandleAccessesPass::run(Function &F, FunctionAnalysisManager &AM) {
178 PreservedAnalyses PA;
179
180 DominatorTree *DT = &AM.getResult<DominatorTreeAnalysis>(IR&: F);
181 bool Changed = forwardHandleAccesses(F, DT&: *DT);
182
183 if (!Changed)
184 return PreservedAnalyses::all();
185 return PA;
186}
187
188namespace {
189class DXILForwardHandleAccessesLegacy : public FunctionPass {
190public:
191 bool runOnFunction(Function &F) override {
192 DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
193 return forwardHandleAccesses(F, DT&: *DT);
194 }
195 StringRef getPassName() const override {
196 return "DXIL Forward Handle Accesses";
197 }
198
199 void getAnalysisUsage(AnalysisUsage &AU) const override {
200 AU.addRequired<DominatorTreeWrapperPass>();
201 }
202
203 DXILForwardHandleAccessesLegacy() : FunctionPass(ID) {}
204
205 static char ID; // Pass identification.
206};
207char DXILForwardHandleAccessesLegacy::ID = 0;
208} // end anonymous namespace
209
210INITIALIZE_PASS_BEGIN(DXILForwardHandleAccessesLegacy, DEBUG_TYPE,
211 "DXIL Forward Handle Accesses", false, false)
212INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
213INITIALIZE_PASS_END(DXILForwardHandleAccessesLegacy, DEBUG_TYPE,
214 "DXIL Forward Handle Accesses", false, false)
215
216FunctionPass *llvm::createDXILForwardHandleAccessesLegacyPass() {
217 return new DXILForwardHandleAccessesLegacy();
218}
219