LLVM 23.0.0git
SPIRVPushConstantAccess.cpp
Go to the documentation of this file.
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
19#include "SPIRV.h"
20#include "SPIRVSubtarget.h"
21#include "SPIRVTargetMachine.h"
22#include "SPIRVUtils.h"
24#include "llvm/IR/IRBuilder.h"
25#include "llvm/IR/IntrinsicsSPIRV.h"
26#include "llvm/IR/Module.h"
28
29#define DEBUG_TYPE "spirv-pushconstant-access"
30using namespace llvm;
31
33 bool Changed = false;
34 for (GlobalVariable &GV : make_early_inc_range(M.globals())) {
35 if (GV.getAddressSpace() !=
36 storageClassToAddressSpace(SPIRV::StorageClass::PushConstant))
37 continue;
38
41
43 M.getContext(), "spirv.PushConstant", {GV.getValueType()});
44 GlobalVariable *NewGV =
45 new GlobalVariable(M, PCType, GV.isConstant(), GV.getLinkage(),
46 /* initializer= */ nullptr, GV.getName(),
47 /* InsertBefore= */ &GV, GV.getThreadLocalMode(),
48 GV.getAddressSpace(), GV.isExternallyInitialized());
49 NewGV->setVisibility(GV.getVisibility());
50
51 for (User *U : make_early_inc_range(GV.users())) {
53 IRBuilder<> Builder(I);
54 Value *GetPointerCall = Builder.CreateIntrinsic(
55 NewGV->getType(), Intrinsic::spv_pushconstant_getpointer, {NewGV});
56 GR->buildAssignPtr(Builder, GV.getValueType(), GetPointerCall);
57
58 I->replaceUsesOfWith(&GV, GetPointerCall);
59 }
60
61 GV.eraseFromParent();
62 Changed = true;
63 }
64
65 return Changed;
66}
67
70 const SPIRVSubtarget *ST = TM.getSubtargetImpl();
71 SPIRVGlobalRegistry *GR = ST->getSPIRVGlobalRegistry();
74}
75
76namespace {
77class SPIRVPushConstantAccessLegacy : public ModulePass {
78 SPIRVTargetMachine *TM = nullptr;
79
80public:
81 bool runOnModule(Module &M) override {
82 const SPIRVSubtarget *ST = TM->getSubtargetImpl();
83 SPIRVGlobalRegistry *GR = ST->getSPIRVGlobalRegistry();
84 return replacePushConstantAccesses(M, GR);
85 }
86 StringRef getPassName() const override {
87 return "SPIRV push constant Access";
88 }
89 SPIRVPushConstantAccessLegacy(SPIRVTargetMachine *TM)
90 : ModulePass(ID), TM(TM) {}
91
92 static char ID; // Pass identification.
93};
94char SPIRVPushConstantAccessLegacy::ID = 0;
95} // end anonymous namespace
96
97INITIALIZE_PASS(SPIRVPushConstantAccessLegacy, DEBUG_TYPE,
98 "SPIRV push constant Access", false, false)
99
102 return new SPIRVPushConstantAccessLegacy(TM);
103}
#define DEBUG_TYPE
Module.h This file contains the declarations for the Module class.
#define I(x, y, z)
Definition MD5.cpp:57
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static bool replacePushConstantAccesses(Module &M, SPIRVGlobalRegistry *GR)
PointerType * getType() const
Global values are always pointers.
void setVisibility(VisibilityTypes V)
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2822
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition Pass.h:255
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
void buildAssignPtr(IRBuilder<> &B, Type *ElemTy, Value *Arg)
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
const SPIRVSubtarget * getSubtargetImpl() const
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
static LLVM_ABI TargetExtType * get(LLVMContext &Context, StringRef Name, ArrayRef< Type * > Types={}, ArrayRef< unsigned > Ints={})
Return a target extension type having the specified name and optional type and integer parameters.
Definition Type.cpp:978
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM Value Representation.
Definition Value.h:75
Changed
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
ModulePass * createSPIRVPushConstantAccessLegacyPass(SPIRVTargetMachine *TM)
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:634
constexpr unsigned storageClassToAddressSpace(SPIRV::StorageClass::StorageClass SC)
Definition SPIRVUtils.h:248
LLVM_ABI bool convertUsersOfConstantsToInstructions(ArrayRef< Constant * > Consts, Function *RestrictToFunc=nullptr, bool RemoveDeadConstants=true, bool IncludeSelf=false)
Replace constant expressions users of the given constants with instructions.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39