1//===- SPIRVPushConstantAccess.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 changes the types of all the globals in the PushConstant
10// address space into a target extension type, and makes all references
11// to this global go though a custom SPIR-V intrinsic.
12//
13// This allows the backend to properly lower the push constant struct type
14// to a fully laid out type, and generate the proper OpAccessChain.
15//
16//===----------------------------------------------------------------------===//
17
18#include "SPIRV.h"
19#include "SPIRVSubtarget.h"
20#include "SPIRVTargetMachine.h"
21#include "SPIRVUtils.h"
22#include "llvm/Frontend/HLSL/CBuffer.h"
23#include "llvm/IR/IRBuilder.h"
24#include "llvm/IR/IntrinsicsSPIRV.h"
25#include "llvm/IR/Module.h"
26#include "llvm/IR/ReplaceConstant.h"
27
28#define DEBUG_TYPE "spirv-pushconstant-access"
29using namespace llvm;
30
31static bool replacePushConstantAccesses(Module &M, SPIRVGlobalRegistry *GR) {
32 bool Changed = false;
33 for (GlobalVariable &GV : make_early_inc_range(Range: M.globals())) {
34 if (GV.getAddressSpace() !=
35 storageClassToAddressSpace(SC: SPIRV::StorageClass::PushConstant))
36 continue;
37
38 convertUsersOfConstantsToInstructions(
39 Consts: llvm::SmallVector<Constant *, 1>(1, &GV));
40
41 Type *PCType = llvm::TargetExtType::get(
42 Context&: M.getContext(), Name: "spirv.PushConstant", Types: {GV.getValueType()});
43 GlobalVariable *NewGV =
44 new GlobalVariable(M, PCType, GV.isConstant(), GV.getLinkage(),
45 /* initializer= */ nullptr, GV.getName(),
46 /* InsertBefore= */ &GV, GV.getThreadLocalMode(),
47 GV.getAddressSpace(), GV.isExternallyInitialized());
48 NewGV->setVisibility(GV.getVisibility());
49
50 for (User *U : make_early_inc_range(Range: GV.users())) {
51 Instruction *I = cast<Instruction>(Val: U);
52 IRBuilder<> Builder(I);
53 Value *GetPointerCall = Builder.CreateIntrinsic(
54 RetTy: NewGV->getType(), ID: Intrinsic::spv_pushconstant_getpointer, Args: {NewGV});
55 GR->buildAssignPtr(B&: Builder, ElemTy: GV.getValueType(), Arg: GetPointerCall);
56
57 I->replaceUsesOfWith(From: &GV, To: GetPointerCall);
58 }
59
60 GV.eraseFromParent();
61 Changed = true;
62 }
63
64 return Changed;
65}
66
67PreservedAnalyses SPIRVPushConstantAccessPass::run(Module &M,
68 ModuleAnalysisManager &AM) {
69 const SPIRVSubtarget *ST = TM.getSubtargetImpl();
70 SPIRVGlobalRegistry *GR = ST->getSPIRVGlobalRegistry();
71 return replacePushConstantAccesses(M, GR) ? PreservedAnalyses::none()
72 : PreservedAnalyses::all();
73}
74
75namespace {
76class SPIRVPushConstantAccessLegacy : public ModulePass {
77 SPIRVTargetMachine *TM = nullptr;
78
79public:
80 bool runOnModule(Module &M) override {
81 const SPIRVSubtarget *ST = TM->getSubtargetImpl();
82 SPIRVGlobalRegistry *GR = ST->getSPIRVGlobalRegistry();
83 return replacePushConstantAccesses(M, GR);
84 }
85 StringRef getPassName() const override {
86 return "SPIRV push constant Access";
87 }
88 SPIRVPushConstantAccessLegacy(SPIRVTargetMachine *TM)
89 : ModulePass(ID), TM(TM) {}
90
91 static char ID; // Pass identification.
92};
93char SPIRVPushConstantAccessLegacy::ID = 0;
94} // end anonymous namespace
95
96INITIALIZE_PASS(SPIRVPushConstantAccessLegacy, DEBUG_TYPE,
97 "SPIRV push constant Access", false, false)
98
99ModulePass *
100llvm::createSPIRVPushConstantAccessLegacyPass(SPIRVTargetMachine *TM) {
101 return new SPIRVPushConstantAccessLegacy(TM);
102}
103