|
| 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 "DXILForwardHandleAccesses.h" |
| 10 | +#include "DXILShaderFlags.h" |
| 11 | +#include "DirectX.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/IntrinsicInst.h" |
| 17 | +#include "llvm/IR/Intrinsics.h" |
| 18 | +#include "llvm/IR/IntrinsicsDirectX.h" |
| 19 | +#include "llvm/IR/Module.h" |
| 20 | +#include "llvm/InitializePasses.h" |
| 21 | +#include "llvm/Pass.h" |
| 22 | +#include "llvm/Transforms/Utils/Local.h" |
| 23 | + |
| 24 | +#define DEBUG_TYPE "dxil-forward-handle-accesses" |
| 25 | + |
| 26 | +using namespace llvm; |
| 27 | + |
| 28 | +static void diagnoseAmbiguousHandle(IntrinsicInst *NewII, |
| 29 | + IntrinsicInst *PrevII) { |
| 30 | + Function *F = NewII->getFunction(); |
| 31 | + LLVMContext &Context = F->getParent()->getContext(); |
| 32 | + Context.diagnose(DiagnosticInfoGeneric( |
| 33 | + Twine("Handle at \"") + NewII->getName() + "\" overwrites handle at \"" + |
| 34 | + PrevII->getName() + "\"")); |
| 35 | +} |
| 36 | + |
| 37 | +static void diagnoseHandleNotFound(LoadInst *LI) { |
| 38 | + Function *F = LI->getFunction(); |
| 39 | + LLVMContext &Context = F->getParent()->getContext(); |
| 40 | + Context.diagnose(DiagnosticInfoGeneric( |
| 41 | + LI, Twine("Load of \"") + LI->getPointerOperand()->getName() + |
| 42 | + "\" is not a global resource handle")); |
| 43 | +} |
| 44 | + |
| 45 | +static void diagnoseUndominatedLoad(LoadInst *LI, IntrinsicInst *Handle) { |
| 46 | + Function *F = LI->getFunction(); |
| 47 | + LLVMContext &Context = F->getParent()->getContext(); |
| 48 | + Context.diagnose(DiagnosticInfoGeneric( |
| 49 | + LI, Twine("Load at \"") + LI->getName() + |
| 50 | + "\" is not dominated by handle creation at \"" + |
| 51 | + Handle->getName() + "\"")); |
| 52 | +} |
| 53 | + |
| 54 | +static void |
| 55 | +processHandle(IntrinsicInst *II, |
| 56 | + DenseMap<GlobalVariable *, IntrinsicInst *> &HandleMap) { |
| 57 | + for (User *U : II->users()) |
| 58 | + if (auto *SI = dyn_cast<StoreInst>(U)) |
| 59 | + if (auto *GV = dyn_cast<GlobalVariable>(SI->getPointerOperand())) { |
| 60 | + auto Entry = HandleMap.try_emplace(GV, II); |
| 61 | + if (Entry.second) |
| 62 | + LLVM_DEBUG(dbgs() << "Added " << GV->getName() << " to handle map\n"); |
| 63 | + else |
| 64 | + diagnoseAmbiguousHandle(II, Entry.first->second); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +static bool forwardHandleAccesses(Function &F, DominatorTree &DT) { |
| 69 | + bool Changed = false; |
| 70 | + |
| 71 | + DenseMap<GlobalVariable *, IntrinsicInst *> HandleMap; |
| 72 | + SmallVector<LoadInst *> LoadsToProcess; |
| 73 | + for (BasicBlock &BB : F) |
| 74 | + for (Instruction &Inst : BB) |
| 75 | + if (auto *II = dyn_cast<IntrinsicInst>(&Inst)) { |
| 76 | + switch (II->getIntrinsicID()) { |
| 77 | + case Intrinsic::dx_resource_handlefrombinding: |
| 78 | + processHandle(II, HandleMap); |
| 79 | + break; |
| 80 | + default: |
| 81 | + continue; |
| 82 | + } |
| 83 | + } else if (auto *LI = dyn_cast<LoadInst>(&Inst)) |
| 84 | + if (isa<dxil::AnyResourceExtType>(LI->getType())) |
| 85 | + LoadsToProcess.push_back(LI); |
| 86 | + |
| 87 | + for (LoadInst *LI : LoadsToProcess) { |
| 88 | + Value *V = LI->getPointerOperand(); |
| 89 | + auto *GV = dyn_cast<GlobalVariable>(LI->getPointerOperand()); |
| 90 | + |
| 91 | + // If we didn't find the global, we may need to walk through a level of |
| 92 | + // indirection. This generally happens at -O0. |
| 93 | + if (!GV) |
| 94 | + if (auto *NestedLI = dyn_cast<LoadInst>(V)) { |
| 95 | + BasicBlock::iterator BBI(NestedLI); |
| 96 | + Value *Loaded = FindAvailableLoadedValue( |
| 97 | + NestedLI, NestedLI->getParent(), BBI, 0, nullptr, nullptr); |
| 98 | + GV = dyn_cast_or_null<GlobalVariable>(Loaded); |
| 99 | + } |
| 100 | + |
| 101 | + auto It = HandleMap.find(GV); |
| 102 | + if (It == HandleMap.end()) { |
| 103 | + diagnoseHandleNotFound(LI); |
| 104 | + continue; |
| 105 | + } |
| 106 | + Changed = true; |
| 107 | + |
| 108 | + if (!DT.dominates(It->second, LI)) { |
| 109 | + diagnoseUndominatedLoad(LI, It->second); |
| 110 | + continue; |
| 111 | + } |
| 112 | + |
| 113 | + LLVM_DEBUG(dbgs() << "Replacing uses of " << GV->getName() << " at " |
| 114 | + << LI->getName() << " with " << It->second->getName() |
| 115 | + << "\n"); |
| 116 | + LI->replaceAllUsesWith(It->second); |
| 117 | + LI->eraseFromParent(); |
| 118 | + } |
| 119 | + |
| 120 | + return Changed; |
| 121 | +} |
| 122 | + |
| 123 | +PreservedAnalyses DXILForwardHandleAccesses::run(Function &F, |
| 124 | + FunctionAnalysisManager &AM) { |
| 125 | + PreservedAnalyses PA; |
| 126 | + |
| 127 | + DominatorTree *DT = &AM.getResult<DominatorTreeAnalysis>(F); |
| 128 | + bool Changed = forwardHandleAccesses(F, *DT); |
| 129 | + |
| 130 | + if (!Changed) |
| 131 | + return PreservedAnalyses::all(); |
| 132 | + return PA; |
| 133 | +} |
| 134 | + |
| 135 | +namespace { |
| 136 | +class DXILForwardHandleAccessesLegacy : public FunctionPass { |
| 137 | +public: |
| 138 | + bool runOnFunction(Function &F) override { |
| 139 | + DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 140 | + return forwardHandleAccesses(F, *DT); |
| 141 | + } |
| 142 | + StringRef getPassName() const override { |
| 143 | + return "DXIL Forward Handle Accesses"; |
| 144 | + } |
| 145 | + |
| 146 | + void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 147 | + AU.addRequired<DominatorTreeWrapperPass>(); |
| 148 | + } |
| 149 | + |
| 150 | + DXILForwardHandleAccessesLegacy() : FunctionPass(ID) {} |
| 151 | + |
| 152 | + static char ID; // Pass identification. |
| 153 | +}; |
| 154 | +char DXILForwardHandleAccessesLegacy::ID = 0; |
| 155 | +} // end anonymous namespace |
| 156 | + |
| 157 | +INITIALIZE_PASS_BEGIN(DXILForwardHandleAccessesLegacy, DEBUG_TYPE, |
| 158 | + "DXIL Forward Handle Accesses", false, false) |
| 159 | +INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 160 | +INITIALIZE_PASS_END(DXILForwardHandleAccessesLegacy, DEBUG_TYPE, |
| 161 | + "DXIL Forward Handle Accesses", false, false) |
| 162 | + |
| 163 | +FunctionPass *llvm::createDXILForwardHandleAccessesLegacyPass() { |
| 164 | + return new DXILForwardHandleAccessesLegacy(); |
| 165 | +} |
0 commit comments