LLVM 24.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
18#include "SPIRV.h"
19#include "SPIRVSubtarget.h"
20#include "SPIRVTargetMachine.h"
21#include "SPIRVUtils.h"
23#include "llvm/IR/IRBuilder.h"
24#include "llvm/IR/IntrinsicsSPIRV.h"
25#include "llvm/IR/Module.h"
27
28#define DEBUG_TYPE "spirv-pushconstant-access"
29using namespace llvm;
30
32 bool Changed = false;
33 for (GlobalVariable &GV : make_early_inc_range(M.globals())) {
34 if (GV.getAddressSpace() !=
35 storageClassToAddressSpace(SPIRV::StorageClass::PushConstant))
36 continue;
37
40
42 M.getContext(), "spirv.PushConstant", {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(GV.users())) {
52 IRBuilder<> Builder(I);
53 Value *GetPointerCall = Builder.CreateIntrinsic(
54 NewGV->getType(), Intrinsic::spv_pushconstant_getpointer, {NewGV});
55 GR->buildAssignPtr(Builder, GV.getValueType(), GetPointerCall);
56
57 I->replaceUsesOfWith(&GV, GetPointerCall);
58 }
59
60 GV.eraseFromParent();
61 Changed = true;
62 }
63
64 return Changed;
65}
66
69 const SPIRVSubtarget *ST = TM.getSubtargetImpl();
70 SPIRVGlobalRegistry *GR = ST->getSPIRVGlobalRegistry();
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
101 return new SPIRVPushConstantAccessLegacy(TM);
102}
#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:2903
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.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
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:960
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
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:633
constexpr unsigned storageClassToAddressSpace(SPIRV::StorageClass::StorageClass SC)
Definition SPIRVUtils.h:245
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