|
| 1 | +//===- SPIRVCBufferAccess.cpp - Translate CBuffer Loads |
| 2 | +//--------------------===// |
| 3 | +// |
| 4 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +// See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | +// |
| 10 | +// This pass replaces all accesses to constant buffer global variables with |
| 11 | +// accesses to the proper SPIR-V resource. It's designed to run after the |
| 12 | +// DXIL preparation passes and before the main SPIR-V legalization passes. |
| 13 | +// |
| 14 | +// The pass operates as follows: |
| 15 | +// 1. It finds all constant buffers by looking for the `!hlsl.cbs` metadata. |
| 16 | +// 2. For each cbuffer, it finds the global variable holding the resource handle |
| 17 | +// and the global variables for each of the cbuffer's members. |
| 18 | +// 3. For each member variable, it creates a call to the |
| 19 | +// `llvm.spv.resource.getpointer` intrinsic. This intrinsic takes the |
| 20 | +// resource handle and the member's index within the cbuffer as arguments. |
| 21 | +// The result is a pointer to that member within the SPIR-V resource. |
| 22 | +// 4. It then replaces all uses of the original member global variable with the |
| 23 | +// pointer returned by the `getpointer` intrinsic. This effectively retargets |
| 24 | +// all loads and GEPs to the new resource pointer. |
| 25 | +// 5. Finally, it cleans up by deleting the original global variables and the |
| 26 | +// `!hlsl.cbs` metadata. |
| 27 | +// |
| 28 | +// This approach allows subsequent passes, like SPIRVEmitIntrinsics, to |
| 29 | +// correctly handle GEPs that operate on the result of the `getpointer` call, |
| 30 | +// folding them into a single OpAccessChain instruction. |
| 31 | +// |
| 32 | +//===----------------------------------------------------------------------===// |
| 33 | + |
| 34 | +#include "SPIRVCBufferAccess.h" |
| 35 | +#include "SPIRV.h" |
| 36 | +#include "llvm/Frontend/HLSL/CBuffer.h" |
| 37 | +#include "llvm/IR/IRBuilder.h" |
| 38 | +#include "llvm/IR/IntrinsicsSPIRV.h" |
| 39 | +#include "llvm/IR/Module.h" |
| 40 | + |
| 41 | +#define DEBUG_TYPE "spirv-cbuffer-access" |
| 42 | +using namespace llvm; |
| 43 | + |
| 44 | +// Finds the single instruction that defines the resource handle. This is |
| 45 | +// typically a call to `llvm.spv.resource.handlefrombinding`. |
| 46 | +static Instruction *findHandleDef(GlobalVariable *HandleVar) { |
| 47 | + for (User *U : HandleVar->users()) { |
| 48 | + if (auto *SI = dyn_cast<StoreInst>(U)) { |
| 49 | + if (auto *I = dyn_cast<Instruction>(SI->getValueOperand())) { |
| 50 | + return I; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + return nullptr; |
| 55 | +} |
| 56 | + |
| 57 | +static bool replaceCBufferAccesses(Module &M) { |
| 58 | + std::optional<hlsl::CBufferMetadata> CBufMD = hlsl::CBufferMetadata::get(M); |
| 59 | + if (!CBufMD) |
| 60 | + return false; |
| 61 | + |
| 62 | + for (const hlsl::CBufferMapping &Mapping : *CBufMD) { |
| 63 | + Instruction *HandleDef = findHandleDef(Mapping.Handle); |
| 64 | + if (!HandleDef) { |
| 65 | + // If there's no handle definition, it might be because the cbuffer is |
| 66 | + // unused. In this case, we can just clean up the globals. |
| 67 | + if (Mapping.Handle->use_empty()) { |
| 68 | + for (const auto &Member : Mapping.Members) { |
| 69 | + if (Member.GV->use_empty()) { |
| 70 | + Member.GV->eraseFromParent(); |
| 71 | + } |
| 72 | + } |
| 73 | + Mapping.Handle->eraseFromParent(); |
| 74 | + } |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + // The handle definition should dominate all uses of the cbuffer members. |
| 79 | + // We'll insert our getpointer calls right after it. |
| 80 | + IRBuilder<> Builder(HandleDef->getNextNode()); |
| 81 | + |
| 82 | + for (uint32_t Index = 0; Index < Mapping.Members.size(); ++Index) { |
| 83 | + GlobalVariable *MemberGV = Mapping.Members[Index].GV; |
| 84 | + if (MemberGV->use_empty()) { |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + // Create the getpointer intrinsic call. |
| 89 | + Value *IndexVal = Builder.getInt32(Index); |
| 90 | + Type *PtrType = MemberGV->getType(); |
| 91 | + Value *GetPointerCall = Builder.CreateIntrinsic( |
| 92 | + PtrType, Intrinsic::spv_resource_getpointer, {HandleDef, IndexVal}); |
| 93 | + |
| 94 | + // We cannot use replaceAllUsesWith here because some uses may be |
| 95 | + // ConstantExprs, which cannot be replaced with non-constants. |
| 96 | + SmallVector<User *, 4> Users(MemberGV->users()); |
| 97 | + for (User *U : Users) { |
| 98 | + if (auto *CE = dyn_cast<ConstantExpr>(U)) { |
| 99 | + SmallVector<Instruction *, 4> Insts; |
| 100 | + std::function<void(ConstantExpr *)> findInstructions = |
| 101 | + [&](ConstantExpr *Const) { |
| 102 | + for (User *ConstU : Const->users()) { |
| 103 | + if (auto *ConstCE = dyn_cast<ConstantExpr>(ConstU)) { |
| 104 | + findInstructions(ConstCE); |
| 105 | + } else if (auto *I = dyn_cast<Instruction>(ConstU)) { |
| 106 | + Insts.push_back(I); |
| 107 | + } |
| 108 | + } |
| 109 | + }; |
| 110 | + findInstructions(CE); |
| 111 | + |
| 112 | + for (Instruction *I : Insts) { |
| 113 | + Instruction *NewInst = CE->getAsInstruction(); |
| 114 | + NewInst->insertBefore(I); |
| 115 | + I->replaceUsesOfWith(CE, NewInst); |
| 116 | + NewInst->replaceUsesOfWith(MemberGV, GetPointerCall); |
| 117 | + } |
| 118 | + } else { |
| 119 | + U->replaceUsesOfWith(MemberGV, GetPointerCall); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + // Now that all uses are replaced, clean up the globals and metadata. |
| 126 | + for (const hlsl::CBufferMapping &Mapping : *CBufMD) { |
| 127 | + for (const auto &Member : Mapping.Members) { |
| 128 | + Member.GV->eraseFromParent(); |
| 129 | + } |
| 130 | + // Erase the stores to the handle variable before erasing the handle itself. |
| 131 | + SmallVector<Instruction *, 4> HandleStores; |
| 132 | + for (User *U : Mapping.Handle->users()) { |
| 133 | + if (auto *SI = dyn_cast<StoreInst>(U)) { |
| 134 | + HandleStores.push_back(SI); |
| 135 | + } |
| 136 | + } |
| 137 | + for (Instruction *I : HandleStores) { |
| 138 | + I->eraseFromParent(); |
| 139 | + } |
| 140 | + Mapping.Handle->eraseFromParent(); |
| 141 | + } |
| 142 | + |
| 143 | + CBufMD->eraseFromModule(); |
| 144 | + return true; |
| 145 | +} |
| 146 | + |
| 147 | +PreservedAnalyses SPIRVCBufferAccess::run(Module &M, |
| 148 | + ModuleAnalysisManager &AM) { |
| 149 | + if (replaceCBufferAccesses(M)) { |
| 150 | + return PreservedAnalyses::none(); |
| 151 | + } |
| 152 | + return PreservedAnalyses::all(); |
| 153 | +} |
| 154 | + |
| 155 | +namespace { |
| 156 | +class SPIRVCBufferAccessLegacy : public ModulePass { |
| 157 | +public: |
| 158 | + bool runOnModule(Module &M) override { return replaceCBufferAccesses(M); } |
| 159 | + StringRef getPassName() const override { return "SPIRV CBuffer Access"; } |
| 160 | + SPIRVCBufferAccessLegacy() : ModulePass(ID) {} |
| 161 | + |
| 162 | + static char ID; // Pass identification. |
| 163 | +}; |
| 164 | +char SPIRVCBufferAccessLegacy::ID = 0; |
| 165 | +} // end anonymous namespace |
| 166 | + |
| 167 | +INITIALIZE_PASS(SPIRVCBufferAccessLegacy, DEBUG_TYPE, "SPIRV CBuffer Access", |
| 168 | + false, false) |
| 169 | + |
| 170 | +ModulePass *llvm::createSPIRVCBufferAccessLegacyPass() { |
| 171 | + return new SPIRVCBufferAccessLegacy(); |
| 172 | +} |
0 commit comments