|
| 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 "DXILResourceAccess.h" |
| 10 | +#include "DirectX.h" |
| 11 | +#include "llvm/Analysis/DXILResource.h" |
| 12 | +#include "llvm/IR/Dominators.h" |
| 13 | +#include "llvm/IR/IRBuilder.h" |
| 14 | +#include "llvm/IR/Instructions.h" |
| 15 | +#include "llvm/IR/IntrinsicInst.h" |
| 16 | +#include "llvm/IR/Intrinsics.h" |
| 17 | +#include "llvm/IR/IntrinsicsDirectX.h" |
| 18 | +#include "llvm/InitializePasses.h" |
| 19 | + |
| 20 | +#define DEBUG_TYPE "dxil-resource-access" |
| 21 | + |
| 22 | +using namespace llvm; |
| 23 | + |
| 24 | +static void replaceTypedBufferAccess(IntrinsicInst *II, |
| 25 | + dxil::ResourceTypeInfo &RTI) { |
| 26 | + const DataLayout &DL = II->getDataLayout(); |
| 27 | + |
| 28 | + auto *HandleType = cast<TargetExtType>(II->getOperand(0)->getType()); |
| 29 | + assert(HandleType->getName() == "dx.TypedBuffer" && |
| 30 | + "Unexpected typed buffer type"); |
| 31 | + Type *ContainedType = HandleType->getTypeParameter(0); |
| 32 | + |
| 33 | + // We need the size of an element in bytes so that we can calculate the offset |
| 34 | + // in elements given a total offset in bytes later. |
| 35 | + Type *ScalarType = ContainedType->getScalarType(); |
| 36 | + uint64_t ScalarSize = DL.getTypeSizeInBits(ScalarType) / 8; |
| 37 | + |
| 38 | + // Process users keeping track of indexing accumulated from GEPs. |
| 39 | + struct AccessAndIndex { |
| 40 | + User *Access; |
| 41 | + Value *Index; |
| 42 | + }; |
| 43 | + SmallVector<AccessAndIndex> Worklist; |
| 44 | + for (User *U : II->users()) |
| 45 | + Worklist.push_back({U, nullptr}); |
| 46 | + |
| 47 | + SmallVector<Instruction *> DeadInsts; |
| 48 | + while (!Worklist.empty()) { |
| 49 | + AccessAndIndex Current = Worklist.back(); |
| 50 | + Worklist.pop_back(); |
| 51 | + |
| 52 | + if (auto *GEP = dyn_cast<GetElementPtrInst>(Current.Access)) { |
| 53 | + IRBuilder<> Builder(GEP); |
| 54 | + |
| 55 | + Value *Index; |
| 56 | + APInt ConstantOffset(DL.getIndexTypeSizeInBits(GEP->getType()), 0); |
| 57 | + if (GEP->accumulateConstantOffset(DL, ConstantOffset)) { |
| 58 | + APInt Scaled = ConstantOffset.udiv(ScalarSize); |
| 59 | + Index = ConstantInt::get(Builder.getInt32Ty(), Scaled); |
| 60 | + } else { |
| 61 | + auto IndexIt = GEP->idx_begin(); |
| 62 | + assert(cast<ConstantInt>(IndexIt)->getZExtValue() == 0 && |
| 63 | + "GEP is not indexing through pointer"); |
| 64 | + ++IndexIt; |
| 65 | + Index = *IndexIt; |
| 66 | + assert(++IndexIt == GEP->idx_end() && "Too many indices in GEP"); |
| 67 | + } |
| 68 | + |
| 69 | + for (User *U : GEP->users()) |
| 70 | + Worklist.push_back({U, Index}); |
| 71 | + DeadInsts.push_back(GEP); |
| 72 | + |
| 73 | + } else if (auto *SI = dyn_cast<StoreInst>(Current.Access)) { |
| 74 | + assert(SI->getValueOperand() != II && "Pointer escaped!"); |
| 75 | + IRBuilder<> Builder(SI); |
| 76 | + |
| 77 | + Value *V = SI->getValueOperand(); |
| 78 | + if (V->getType() == ContainedType) { |
| 79 | + // V is already the right type. |
| 80 | + } else if (V->getType() == ScalarType) { |
| 81 | + // We're storing a scalar, so we need to load the current value and only |
| 82 | + // replace the relevant part. |
| 83 | + auto *Load = Builder.CreateIntrinsic( |
| 84 | + ContainedType, Intrinsic::dx_typedBufferLoad, |
| 85 | + {II->getOperand(0), II->getOperand(1)}); |
| 86 | + // If we have an offset from seeing a GEP earlier, use it. |
| 87 | + Value *IndexOp = Current.Index |
| 88 | + ? Current.Index |
| 89 | + : ConstantInt::get(Builder.getInt32Ty(), 0); |
| 90 | + V = Builder.CreateInsertElement(Load, V, IndexOp); |
| 91 | + } else { |
| 92 | + llvm_unreachable("Store to typed resource has invalid type"); |
| 93 | + } |
| 94 | + |
| 95 | + auto *Inst = Builder.CreateIntrinsic( |
| 96 | + Builder.getVoidTy(), Intrinsic::dx_typedBufferStore, |
| 97 | + {II->getOperand(0), II->getOperand(1), V}); |
| 98 | + SI->replaceAllUsesWith(Inst); |
| 99 | + DeadInsts.push_back(SI); |
| 100 | + |
| 101 | + } else if (auto *LI = dyn_cast<LoadInst>(Current.Access)) { |
| 102 | + IRBuilder<> Builder(LI); |
| 103 | + Value *V = |
| 104 | + Builder.CreateIntrinsic(ContainedType, Intrinsic::dx_typedBufferLoad, |
| 105 | + {II->getOperand(0), II->getOperand(1)}); |
| 106 | + if (Current.Index) |
| 107 | + V = Builder.CreateExtractElement(V, Current.Index); |
| 108 | + |
| 109 | + LI->replaceAllUsesWith(V); |
| 110 | + DeadInsts.push_back(LI); |
| 111 | + |
| 112 | + } else |
| 113 | + llvm_unreachable("Unhandled instruction - pointer escaped?"); |
| 114 | + } |
| 115 | + |
| 116 | + // Traverse the now-dead instructions in RPO and remove them. |
| 117 | + for (Instruction *Dead : llvm::reverse(DeadInsts)) |
| 118 | + Dead->eraseFromParent(); |
| 119 | + II->eraseFromParent(); |
| 120 | +} |
| 121 | + |
| 122 | +static bool transformResourcePointers(Function &F, DXILResourceTypeMap &DRTM) { |
| 123 | + bool Changed = false; |
| 124 | + SmallVector<std::pair<IntrinsicInst *, dxil::ResourceTypeInfo>> Resources; |
| 125 | + for (BasicBlock &BB : F) |
| 126 | + for (Instruction &I : BB) |
| 127 | + if (auto *II = dyn_cast<IntrinsicInst>(&I)) |
| 128 | + if (II->getIntrinsicID() == Intrinsic::dx_resource_getpointer) { |
| 129 | + auto *HandleTy = cast<TargetExtType>(II->getArgOperand(0)->getType()); |
| 130 | + Resources.emplace_back(II, DRTM[HandleTy]); |
| 131 | + } |
| 132 | + |
| 133 | + for (auto &[II, RI] : Resources) { |
| 134 | + if (RI.isTyped()) { |
| 135 | + Changed = true; |
| 136 | + replaceTypedBufferAccess(II, RI); |
| 137 | + } |
| 138 | + |
| 139 | + // TODO: handle other resource types. We should probably have an |
| 140 | + // `unreachable` here once we've added support for all of them. |
| 141 | + } |
| 142 | + |
| 143 | + return Changed; |
| 144 | +} |
| 145 | + |
| 146 | +PreservedAnalyses DXILResourceAccess::run(Function &F, |
| 147 | + FunctionAnalysisManager &FAM) { |
| 148 | + auto &MAMProxy = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F); |
| 149 | + DXILResourceTypeMap *DRTM = |
| 150 | + MAMProxy.getCachedResult<DXILResourceTypeAnalysis>(*F.getParent()); |
| 151 | + assert(DRTM && "DXILResourceTypeAnalysis must be available"); |
| 152 | + |
| 153 | + bool MadeChanges = transformResourcePointers(F, *DRTM); |
| 154 | + if (!MadeChanges) |
| 155 | + return PreservedAnalyses::all(); |
| 156 | + |
| 157 | + PreservedAnalyses PA; |
| 158 | + PA.preserve<DXILResourceTypeAnalysis>(); |
| 159 | + PA.preserve<DominatorTreeAnalysis>(); |
| 160 | + return PA; |
| 161 | +} |
| 162 | + |
| 163 | +namespace { |
| 164 | +class DXILResourceAccessLegacy : public FunctionPass { |
| 165 | +public: |
| 166 | + bool runOnFunction(Function &F) override { |
| 167 | + DXILResourceTypeMap &DRTM = |
| 168 | + getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap(); |
| 169 | + |
| 170 | + return transformResourcePointers(F, DRTM); |
| 171 | + } |
| 172 | + StringRef getPassName() const override { return "DXIL Resource Access"; } |
| 173 | + DXILResourceAccessLegacy() : FunctionPass(ID) {} |
| 174 | + |
| 175 | + static char ID; // Pass identification. |
| 176 | + void getAnalysisUsage(llvm::AnalysisUsage &AU) const override { |
| 177 | + AU.addRequired<DXILResourceTypeWrapperPass>(); |
| 178 | + AU.addPreserved<DominatorTreeWrapperPass>(); |
| 179 | + } |
| 180 | +}; |
| 181 | +char DXILResourceAccessLegacy::ID = 0; |
| 182 | +} // end anonymous namespace |
| 183 | + |
| 184 | +INITIALIZE_PASS_BEGIN(DXILResourceAccessLegacy, DEBUG_TYPE, |
| 185 | + "DXIL Resource Access", false, false) |
| 186 | +INITIALIZE_PASS_DEPENDENCY(DXILResourceTypeWrapperPass) |
| 187 | +INITIALIZE_PASS_END(DXILResourceAccessLegacy, DEBUG_TYPE, |
| 188 | + "DXIL Resource Access", false, false) |
| 189 | + |
| 190 | +FunctionPass *llvm::createDXILResourceAccessLegacyPass() { |
| 191 | + return new DXILResourceAccessLegacy(); |
| 192 | +} |
0 commit comments