1//===- SPIRVCBufferAccess.cpp - Translate CBuffer Loads ---------*- C++ -*-===//
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// This pass replaces all accesses to constant buffer global variables with
10// accesses to the proper SPIR-V resource.
11//
12// The pass operates as follows:
13// 1. It finds all constant buffers by looking for the `!hlsl.cbs` metadata.
14// 2. For each cbuffer, it finds the global variable holding the resource handle
15// and the global variables for each of the cbuffer's members.
16// 3. For each member variable, it creates a call to the
17// `llvm.spv.resource.getpointer` intrinsic. This intrinsic takes the
18// resource handle and the member's index within the cbuffer as arguments.
19// The result is a pointer to that member within the SPIR-V resource.
20// 4. It then replaces all uses of the original member global variable with the
21// pointer returned by the `getpointer` intrinsic. This effectively retargets
22// all loads and GEPs to the new resource pointer.
23// 5. Finally, it cleans up by deleting the original global variables and the
24// `!hlsl.cbs` metadata.
25//
26// This approach allows subsequent passes, like SPIRVEmitIntrinsics, to
27// correctly handle GEPs that operate on the result of the `getpointer` call,
28// folding them into a single OpAccessChain instruction.
29//
30//===----------------------------------------------------------------------===//
31
32#include "SPIRV.h"
33#include "llvm/Frontend/HLSL/CBuffer.h"
34#include "llvm/IR/IRBuilder.h"
35#include "llvm/IR/IntrinsicsSPIRV.h"
36#include "llvm/IR/Module.h"
37#include "llvm/IR/ReplaceConstant.h"
38#include "llvm/Transforms/Utils/ModuleUtils.h"
39
40#define DEBUG_TYPE "spirv-cbuffer-access"
41using namespace llvm;
42
43// Finds the single instruction that defines the resource handle. This is
44// typically a call to `llvm.spv.resource.handlefrombinding`.
45static Instruction *findHandleDef(GlobalVariable *HandleVar) {
46 for (User *U : HandleVar->users()) {
47 if (auto *SI = dyn_cast<StoreInst>(Val: U)) {
48 if (auto *I = dyn_cast<Instruction>(Val: SI->getValueOperand())) {
49 return I;
50 }
51 }
52 }
53 return nullptr;
54}
55
56static bool replaceCBufferAccesses(Module &M) {
57 std::optional<hlsl::CBufferMetadata> CBufMD =
58 hlsl::CBufferMetadata::get(M, IsPadding: [](Type *Ty) {
59 if (auto *TET = dyn_cast<TargetExtType>(Val: Ty))
60 return TET->getName() == "spirv.Padding";
61 return false;
62 });
63 if (!CBufMD)
64 return false;
65
66 SmallPtrSet<GlobalVariable *, 8> CBufferHandles;
67 SmallVector<Constant *> CBufferGlobals;
68 for (const hlsl::CBufferMapping &Mapping : *CBufMD) {
69 CBufferHandles.insert(Ptr: Mapping.Handle);
70 for (const hlsl::CBufferMember &Member : Mapping.Members)
71 CBufferGlobals.push_back(Elt: Member.GV);
72 }
73 convertUsersOfConstantsToInstructions(Consts: CBufferGlobals);
74
75 for (const hlsl::CBufferMapping &Mapping : *CBufMD) {
76 Instruction *HandleDef = findHandleDef(HandleVar: Mapping.Handle);
77 if (!HandleDef) {
78 report_fatal_error(reason: "Could not find handle definition for cbuffer: " +
79 Mapping.Handle->getName());
80 }
81
82 // The handle definition should dominate all uses of the cbuffer members.
83 // We'll insert our getpointer calls right after it.
84 IRBuilder<> Builder(HandleDef->getNextNode());
85 auto *HandleTy = cast<TargetExtType>(Val: Mapping.Handle->getValueType());
86 auto *LayoutTy = cast<StructType>(Val: HandleTy->getTypeParameter(i: 0));
87 const StructLayout *SL = M.getDataLayout().getStructLayout(Ty: LayoutTy);
88
89 for (const hlsl::CBufferMember &Member : Mapping.Members) {
90 GlobalVariable *MemberGV = Member.GV;
91 if (MemberGV->use_empty()) {
92 continue;
93 }
94
95 uint32_t IndexInStruct = SL->getElementContainingOffset(FixedOffset: Member.Offset);
96
97 // Create the getpointer intrinsic call.
98 Value *IndexVal = Builder.getInt32(C: IndexInStruct);
99 Type *PtrType = MemberGV->getType();
100 Value *GetPointerCall = Builder.CreateIntrinsic(
101 RetTy: PtrType, ID: Intrinsic::spv_resource_getpointer, Args: {HandleDef, IndexVal});
102
103 MemberGV->replaceAllUsesWith(V: GetPointerCall);
104 }
105 }
106
107 // Remove cbuffer handle globals from @llvm.compiler.used list.
108 llvm::removeFromUsedLists(M, ShouldRemove: [&](Constant *C) -> bool {
109 auto *GV = dyn_cast<GlobalVariable>(Val: C);
110 return GV && CBufferHandles.contains(Ptr: GV);
111 });
112 for (GlobalVariable *HandleGV : CBufferHandles)
113 HandleGV->removeDeadConstantUsers();
114
115 // Now that all uses are replaced, clean up the globals and metadata.
116 for (const hlsl::CBufferMapping &Mapping : *CBufMD) {
117 for (const auto &Member : Mapping.Members) {
118 Member.GV->eraseFromParent();
119 }
120 // Erase the stores to the handle variable before erasing the handle itself.
121 SmallVector<Instruction *, 4> HandleStores;
122 for (User *U : Mapping.Handle->users()) {
123 if (auto *SI = dyn_cast<StoreInst>(Val: U)) {
124 HandleStores.push_back(Elt: SI);
125 }
126 }
127 for (Instruction *I : HandleStores) {
128 I->eraseFromParent();
129 }
130 Mapping.Handle->eraseFromParent();
131 }
132
133 CBufMD->eraseFromModule();
134 return true;
135}
136
137PreservedAnalyses SPIRVCBufferAccessPass::run(Module &M,
138 ModuleAnalysisManager &AM) {
139 if (replaceCBufferAccesses(M)) {
140 return PreservedAnalyses::none();
141 }
142 return PreservedAnalyses::all();
143}
144
145namespace {
146class SPIRVCBufferAccessLegacy : public ModulePass {
147public:
148 bool runOnModule(Module &M) override { return replaceCBufferAccesses(M); }
149 StringRef getPassName() const override { return "SPIRV CBuffer Access"; }
150 SPIRVCBufferAccessLegacy() : ModulePass(ID) {}
151
152 static char ID; // Pass identification.
153};
154char SPIRVCBufferAccessLegacy::ID = 0;
155} // end anonymous namespace
156
157INITIALIZE_PASS(SPIRVCBufferAccessLegacy, DEBUG_TYPE, "SPIRV CBuffer Access",
158 false, false)
159
160ModulePass *llvm::createSPIRVCBufferAccessLegacyPass() {
161 return new SPIRVCBufferAccessLegacy();
162}
163