LLVM 22.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"
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
38 GV.removeDeadConstantUsers();
39
41 M.getContext(), "spirv.PushConstant", {GV.getValueType()});
42 GlobalVariable *NewGV =
43 new GlobalVariable(M, PCType, GV.isConstant(), GV.getLinkage(),
44 /* initializer= */ nullptr, GV.getName(),
45 /* InsertBefore= */ &GV, GV.getThreadLocalMode(),
46 GV.getAddressSpace(), GV.isExternallyInitialized());
47
48 for (User *U : make_early_inc_range(GV.users())) {
50 IRBuilder<> Builder(I);
51 Value *GetPointerCall = Builder.CreateIntrinsic(
52 NewGV->getType(), Intrinsic::spv_pushconstant_getpointer, {NewGV});
53 GR->buildAssignPtr(Builder, GV.getValueType(), GetPointerCall);
54
55 I->replaceUsesOfWith(&GV, GetPointerCall);
56 }
57
58 GV.eraseFromParent();
59 Changed = true;
60 }
61
62 return Changed;
63}
64
67 const SPIRVSubtarget *ST = TM.getSubtargetImpl();
68 SPIRVGlobalRegistry *GR = ST->getSPIRVGlobalRegistry();
71}
72
73namespace {
74class SPIRVPushConstantAccessLegacy : public ModulePass {
75 SPIRVTargetMachine *TM = nullptr;
76
77public:
78 bool runOnModule(Module &M) override {
79 const SPIRVSubtarget *ST = TM->getSubtargetImpl();
80 SPIRVGlobalRegistry *GR = ST->getSPIRVGlobalRegistry();
81 return replacePushConstantAccesses(M, GR);
82 }
83 StringRef getPassName() const override {
84 return "SPIRV push constant Access";
85 }
86 SPIRVPushConstantAccessLegacy(SPIRVTargetMachine *TM)
87 : ModulePass(ID), TM(TM) {}
88
89 static char ID; // Pass identification.
90};
91char SPIRVPushConstantAccessLegacy::ID = 0;
92} // end anonymous namespace
93
94INITIALIZE_PASS(SPIRVPushConstantAccessLegacy, DEBUG_TYPE,
95 "SPIRV push constant Access", false, false)
96
99 return new SPIRVPushConstantAccessLegacy(TM);
100}
#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.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2794
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
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:907
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
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.
Definition Types.h:26
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:632
constexpr unsigned storageClassToAddressSpace(SPIRV::StorageClass::StorageClass SC)
Definition SPIRVUtils.h:244
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