LLVM  16.0.0git
SPIRVGlobalRegistry.h
Go to the documentation of this file.
1 //===-- SPIRVGlobalRegistry.h - SPIR-V Global Registry ----------*- 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 // SPIRVGlobalRegistry is used to maintain rich type information required for
10 // SPIR-V even after lowering from LLVM IR to GMIR. It can convert an llvm::Type
11 // into an OpTypeXXX instruction, and map it to a virtual register. Also it
12 // builds and supports consistency of constants and global variables.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_LIB_TARGET_SPIRV_SPIRVTYPEMANAGER_H
17 #define LLVM_LIB_TARGET_SPIRV_SPIRVTYPEMANAGER_H
18 
20 #include "SPIRVDuplicatesTracker.h"
21 #include "SPIRVInstrInfo.h"
23 
24 namespace llvm {
25 using SPIRVType = const MachineInstr;
26 
28  // Registers holding values which have types associated with them.
29  // Initialized upon VReg definition in IRTranslator.
30  // Do not confuse this with DuplicatesTracker as DT maps Type* to <MF, Reg>
31  // where Reg = OpType...
32  // while VRegToTypeMap tracks SPIR-V type assigned to other regs (i.e. not
33  // type-declaring ones).
35  VRegToTypeMap;
36 
38 
40 
41  SmallPtrSet<const Type *, 4> TypesInProcessing;
42  DenseMap<const Type *, SPIRVType *> ForwardPointerTypes;
43 
44  // Number of bits pointers and size_t integers require.
45  const unsigned PointerSize;
46 
47  // Add a new OpTypeXXX instruction without checking for duplicates.
48  SPIRVType *createSPIRVType(const Type *Type, MachineIRBuilder &MIRBuilder,
50  SPIRV::AccessQualifier::ReadWrite,
51  bool EmitIR = true);
52  SPIRVType *findSPIRVType(const Type *Ty, MachineIRBuilder &MIRBuilder,
54  SPIRV::AccessQualifier::ReadWrite,
55  bool EmitIR = true);
56  SPIRVType *
57  restOfCreateSPIRVType(const Type *Type, MachineIRBuilder &MIRBuilder,
59  bool EmitIR);
60 
61 public:
62  SPIRVGlobalRegistry(unsigned PointerSize);
63 
65 
66  void add(const Constant *C, MachineFunction *MF, Register R) {
67  DT.add(C, MF, R);
68  }
69 
70  void add(const GlobalVariable *GV, MachineFunction *MF, Register R) {
71  DT.add(GV, MF, R);
72  }
73 
74  void add(const Function *F, MachineFunction *MF, Register R) {
75  DT.add(F, MF, R);
76  }
77 
78  void add(const Argument *Arg, MachineFunction *MF, Register R) {
79  DT.add(Arg, MF, R);
80  }
81 
83  return DT.find(C, MF);
84  }
85 
87  return DT.find(GV, MF);
88  }
89 
91  return DT.find(F, MF);
92  }
93 
94  void buildDepsGraph(std::vector<SPIRV::DTSortableEntry *> &Graph,
95  MachineModuleInfo *MMI = nullptr) {
96  DT.buildDepsGraph(Graph, MMI);
97  }
98 
99  // Get or create a SPIR-V type corresponding the given LLVM IR type,
100  // and map it to the given VReg by creating an ASSIGN_TYPE instruction.
102  MachineIRBuilder &MIRBuilder,
104  SPIRV::AccessQualifier::ReadWrite,
105  bool EmitIR = true);
107  MachineInstr &I, const SPIRVInstrInfo &TII);
108  SPIRVType *assignVectTypeToVReg(SPIRVType *BaseType, unsigned NumElements,
109  Register VReg, MachineInstr &I,
110  const SPIRVInstrInfo &TII);
111 
112  // In cases where the SPIR-V type is already known, this function can be
113  // used to map it to the given VReg via an ASSIGN_TYPE instruction.
115  MachineFunction &MF);
116 
117  // Either generate a new OpTypeXXX instruction or return an existing one
118  // corresponding to the given LLVM IR type.
119  // EmitIR controls if we emit GMIR or SPV constants (e.g. for array sizes)
120  // because this method may be called from InstructionSelector and we don't
121  // want to emit extra IR instructions there.
123  MachineIRBuilder &MIRBuilder,
125  SPIRV::AccessQualifier::ReadWrite,
126  bool EmitIR = true);
127 
128  const Type *getTypeForSPIRVType(const SPIRVType *Ty) const {
129  auto Res = SPIRVToLLVMType.find(Ty);
130  assert(Res != SPIRVToLLVMType.end());
131  return Res->second;
132  }
133 
134  // Return the SPIR-V type instruction corresponding to the given VReg, or
135  // nullptr if no such type instruction exists.
137 
138  // Whether the given VReg has a SPIR-V type mapped to it yet.
139  bool hasSPIRVTypeForVReg(Register VReg) const {
140  return getSPIRVTypeForVReg(VReg) != nullptr;
141  }
142 
143  // Return the VReg holding the result of the given OpTypeXXX instruction.
144  Register getSPIRVTypeID(const SPIRVType *SpirvType) const;
145 
146  void setCurrentFunc(MachineFunction &MF) { CurMF = &MF; }
147 
148  // Whether the given VReg has an OpTypeXXX instruction mapped to it with the
149  // given opcode (e.g. OpTypeFloat).
150  bool isScalarOfType(Register VReg, unsigned TypeOpcode) const;
151 
152  // Return true if the given VReg's assigned SPIR-V type is either a scalar
153  // matching the given opcode, or a vector with an element type matching that
154  // opcode (e.g. OpTypeBool, or OpTypeVector %x 4, where %x is OpTypeBool).
155  bool isScalarOrVectorOfType(Register VReg, unsigned TypeOpcode) const;
156 
157  // For vectors or scalars of ints/floats, return the scalar type's bitwidth.
158  unsigned getScalarOrVectorBitWidth(const SPIRVType *Type) const;
159 
160  // For integer vectors or scalars, return whether the integers are signed.
161  bool isScalarOrVectorSigned(const SPIRVType *Type) const;
162 
163  // Gets the storage class of the pointer type assigned to this vreg.
165 
166  // Return the number of bits SPIR-V pointers and size_t variables require.
167  unsigned getPointerSize() const { return PointerSize; }
168 
169 private:
170  SPIRVType *getOpTypeBool(MachineIRBuilder &MIRBuilder);
171 
172  SPIRVType *getOpTypeInt(uint32_t Width, MachineIRBuilder &MIRBuilder,
173  bool IsSigned = false);
174 
175  SPIRVType *getOpTypeFloat(uint32_t Width, MachineIRBuilder &MIRBuilder);
176 
177  SPIRVType *getOpTypeVoid(MachineIRBuilder &MIRBuilder);
178 
179  SPIRVType *getOpTypeVector(uint32_t NumElems, SPIRVType *ElemType,
180  MachineIRBuilder &MIRBuilder);
181 
182  SPIRVType *getOpTypeArray(uint32_t NumElems, SPIRVType *ElemType,
183  MachineIRBuilder &MIRBuilder, bool EmitIR = true);
184 
185  SPIRVType *getOpTypeOpaque(const StructType *Ty,
186  MachineIRBuilder &MIRBuilder);
187 
188  SPIRVType *getOpTypeStruct(const StructType *Ty, MachineIRBuilder &MIRBuilder,
189  bool EmitIR = true);
190 
192  SPIRVType *ElemType, MachineIRBuilder &MIRBuilder,
193  Register Reg);
194 
195  SPIRVType *getOpTypeForwardPointer(SPIRV::StorageClass::StorageClass SC,
196  MachineIRBuilder &MIRBuilder);
197 
198  SPIRVType *getOpTypeFunction(SPIRVType *RetType,
199  const SmallVectorImpl<SPIRVType *> &ArgTypes,
200  MachineIRBuilder &MIRBuilder);
201  std::tuple<Register, ConstantInt *, bool> getOrCreateConstIntReg(
202  uint64_t Val, SPIRVType *SpvType, MachineIRBuilder *MIRBuilder,
203  MachineInstr *I = nullptr, const SPIRVInstrInfo *TII = nullptr);
204  SPIRVType *finishCreatingSPIRVType(const Type *LLVMTy, SPIRVType *SpirvType);
205 
206 public:
208  SPIRVType *SpvType = nullptr, bool EmitIR = true);
210  SPIRVType *SpvType, const SPIRVInstrInfo &TII);
212  SPIRVType *SpvType = nullptr);
214  SPIRVType *SpvType,
215  const SPIRVInstrInfo &TII);
217  const SPIRVInstrInfo &TII);
219  StringRef Name, const GlobalValue *GV,
221  const MachineInstr *Init, bool IsConst,
222  bool HasLinkageTy,
223  SPIRV::LinkageType::LinkageType LinkageType,
224  MachineIRBuilder &MIRBuilder,
225  bool IsInstSelector);
226 
227  // Convenient helpers for getting types with check for duplicates.
229  MachineIRBuilder &MIRBuilder);
231  const SPIRVInstrInfo &TII);
234  const SPIRVInstrInfo &TII);
236  unsigned NumElements,
237  MachineIRBuilder &MIRBuilder);
239  unsigned NumElements, MachineInstr &I,
240  const SPIRVInstrInfo &TII);
242  SPIRVType *BaseType, MachineIRBuilder &MIRBuilder,
243  SPIRV::StorageClass::StorageClass SClass = SPIRV::StorageClass::Function);
246  SPIRV::StorageClass::StorageClass SClass = SPIRV::StorageClass::Function);
248  const Type *Ty, SPIRVType *RetType,
249  const SmallVectorImpl<SPIRVType *> &ArgTypes,
250  MachineIRBuilder &MIRBuilder);
251 };
252 } // end namespace llvm
253 #endif // LLLVM_LIB_TARGET_SPIRV_SPIRVTYPEMANAGER_H
llvm::Argument
This class represents an incoming formal argument to a Function.
Definition: Argument.h:28
llvm::SPIRVGlobalRegistry::find
Register find(const GlobalVariable *GV, MachineFunction *MF)
Definition: SPIRVGlobalRegistry.h:86
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::SPIRVGlobalRegistry::isScalarOrVectorOfType
bool isScalarOrVectorOfType(Register VReg, unsigned TypeOpcode) const
Definition: SPIRVGlobalRegistry.cpp:634
llvm::SPIRVGlobalRegistry::isScalarOfType
bool isScalarOfType(Register VReg, unsigned TypeOpcode) const
Definition: SPIRVGlobalRegistry.cpp:627
llvm::SPIRVGlobalRegistry::SPIRVGlobalRegistry
SPIRVGlobalRegistry(unsigned PointerSize)
Definition: SPIRVGlobalRegistry.cpp:24
llvm::SPIRVGlobalRegistry::getOrCreateOpTypeFunctionWithArgs
SPIRVType * getOrCreateOpTypeFunctionWithArgs(const Type *Ty, SPIRVType *RetType, const SmallVectorImpl< SPIRVType * > &ArgTypes, MachineIRBuilder &MIRBuilder)
Definition: SPIRVGlobalRegistry.cpp:471
llvm::Function
Definition: Function.h:60
llvm::SPIRVGlobalRegistry::getOrCreateSPIRVPointerType
SPIRVType * getOrCreateSPIRVPointerType(SPIRVType *BaseType, MachineIRBuilder &MIRBuilder, SPIRV::StorageClass::StorageClass SClass=SPIRV::StorageClass::Function)
Definition: SPIRVGlobalRegistry.cpp:758
llvm::SPIRVGlobalRegistry::assignVectTypeToVReg
SPIRVType * assignVectTypeToVReg(SPIRVType *BaseType, unsigned NumElements, Register VReg, MachineInstr &I, const SPIRVInstrInfo &TII)
Definition: SPIRVGlobalRegistry.cpp:36
llvm::SPIRVGlobalRegistry::getScalarOrVectorBitWidth
unsigned getScalarOrVectorBitWidth(const SPIRVType *Type) const
Definition: SPIRVGlobalRegistry.cpp:649
llvm::X86Disassembler::Reg
Reg
All possible values of the reg field in the ModR/M byte.
Definition: X86DisassemblerDecoder.h:462
llvm::GlobalVariable
Definition: GlobalVariable.h:39
llvm::SPIRVGeneralDuplicatesTracker::buildDepsGraph
void buildDepsGraph(std::vector< SPIRV::DTSortableEntry * > &Graph, MachineModuleInfo *MMI)
Definition: SPIRVDuplicatesTracker.cpp:33
llvm::SPIRVGeneralDuplicatesTracker::find
Register find(const Type *T, const MachineFunction *MF)
Definition: SPIRVDuplicatesTracker.h:153
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::SPIRVGlobalRegistry::assignSPIRVTypeToVReg
void assignSPIRVTypeToVReg(SPIRVType *Type, Register VReg, MachineFunction &MF)
Definition: SPIRVGlobalRegistry.cpp:55
llvm::SPIRVGlobalRegistry::setCurrentFunc
void setCurrentFunc(MachineFunction &MF)
Definition: SPIRVGlobalRegistry.h:146
llvm::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:450
llvm::SPIRVGlobalRegistry::assignTypeToVReg
SPIRVType * assignTypeToVReg(const Type *Type, Register VReg, MachineIRBuilder &MIRBuilder, SPIRV::AccessQualifier::AccessQualifier AQ=SPIRV::AccessQualifier::ReadWrite, bool EmitIR=true)
Definition: SPIRVGlobalRegistry.cpp:45
MachineIRBuilder.h
BaseType
llvm::SPIRVGlobalRegistry::CurMF
MachineFunction * CurMF
Definition: SPIRVGlobalRegistry.h:64
llvm::SPIRVGlobalRegistry::getSPIRVTypeForVReg
SPIRVType * getSPIRVTypeForVReg(Register VReg) const
Definition: SPIRVGlobalRegistry.cpp:594
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::SPIRVGlobalRegistry::getTypeForSPIRVType
const Type * getTypeForSPIRVType(const SPIRVType *Ty) const
Definition: SPIRVGlobalRegistry.h:128
llvm::AMDGPU::HSAMD::AccessQualifier
AccessQualifier
Access qualifiers.
Definition: AMDGPUMetadata.h:58
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:186
llvm::SPIRVGlobalRegistry::add
void add(const Argument *Arg, MachineFunction *MF, Register R)
Definition: SPIRVGlobalRegistry.h:78
llvm::SPIRVGlobalRegistry::buildConstantFP
Register buildConstantFP(APFloat Val, MachineIRBuilder &MIRBuilder, SPIRVType *SpvType=nullptr)
Definition: SPIRVGlobalRegistry.cpp:222
llvm::PPCISD::SC
@ SC
CHAIN = SC CHAIN, Imm128 - System call.
Definition: PPCISelLowering.h:418
llvm::SPIRVGlobalRegistry::add
void add(const GlobalVariable *GV, MachineFunction *MF, Register R)
Definition: SPIRVGlobalRegistry.h:70
SPIRVBaseInfo.h
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
StorageClass
COFF::SymbolStorageClass StorageClass
Definition: COFFYAML.cpp:360
TII
const HexagonInstrInfo * TII
Definition: HexagonCopyToCombine.cpp:125
llvm::MachineModuleInfo
This class contains meta information specific to a module.
Definition: MachineModuleInfo.h:74
llvm::SPIRVGlobalRegistry::getOrCreateUndef
Register getOrCreateUndef(MachineInstr &I, SPIRVType *SpvType, const SPIRVInstrInfo &TII)
Definition: SPIRVGlobalRegistry.cpp:784
llvm::SPIRVGlobalRegistry::getPointerSize
unsigned getPointerSize() const
Definition: SPIRVGlobalRegistry.h:167
SPIRVInstrInfo.h
llvm::APFloat
Definition: APFloat.h:701
llvm::GlobalValue
Definition: GlobalValue.h:44
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::MachineIRBuilder
Helper class to build MachineInstr.
Definition: MachineIRBuilder.h:219
llvm::SPIRVGlobalRegistry::add
void add(const Constant *C, MachineFunction *MF, Register R)
Definition: SPIRVGlobalRegistry.h:66
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:66
uint64_t
llvm::DenseMap
Definition: DenseMap.h:714
llvm::SPIRVGlobalRegistry::getOrCreateConstInt
Register getOrCreateConstInt(uint64_t Val, MachineInstr &I, SPIRVType *SpvType, const SPIRVInstrInfo &TII)
Definition: SPIRVGlobalRegistry.cpp:145
llvm::SPIRVGlobalRegistry::assignIntTypeToVReg
SPIRVType * assignIntTypeToVReg(unsigned BitWidth, Register VReg, MachineInstr &I, const SPIRVInstrInfo &TII)
Definition: SPIRVGlobalRegistry.cpp:27
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::SPIRVGlobalRegistry::getOrCreateConsIntVector
Register getOrCreateConsIntVector(uint64_t Val, MachineInstr &I, SPIRVType *SpvType, const SPIRVInstrInfo &TII)
Definition: SPIRVGlobalRegistry.cpp:247
llvm::SPIRVGlobalRegistry::add
void add(const Function *F, MachineFunction *MF, Register R)
Definition: SPIRVGlobalRegistry.h:74
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::find
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:150
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::SPIRVGlobalRegistry::buildDepsGraph
void buildDepsGraph(std::vector< SPIRV::DTSortableEntry * > &Graph, MachineModuleInfo *MMI=nullptr)
Definition: SPIRVGlobalRegistry.h:94
llvm::SPIRVGlobalRegistry
Definition: SPIRVGlobalRegistry.h:27
SPIRVDuplicatesTracker.h
llvm::MachineFunction
Definition: MachineFunction.h:257
llvm::AMDGPU::HSAMD::Kernel::Arg::Key::IsConst
constexpr char IsConst[]
Key for Kernel::Arg::Metadata::mIsConst.
Definition: AMDGPUMetadata.h:195
llvm::SPIRVInstrInfo
Definition: SPIRVInstrInfo.h:24
llvm::StructType
Class to represent struct types.
Definition: DerivedTypes.h:213
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::SPIRVGeneralDuplicatesTracker
Definition: SPIRVDuplicatesTracker.h:110
uint32_t
llvm::SPIRVGlobalRegistry::getOrCreateSPIRVBoolType
SPIRVType * getOrCreateSPIRVBoolType(MachineIRBuilder &MIRBuilder)
Definition: SPIRVGlobalRegistry.cpp:715
llvm::SPIRVGlobalRegistry::getSPIRVTypeID
Register getSPIRVTypeID(const SPIRVType *SpirvType) const
Definition: SPIRVGlobalRegistry.cpp:493
llvm::Init
Definition: Record.h:281
llvm::Register
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::end
iterator end()
Definition: DenseMap.h:84
llvm::SPIRVGlobalRegistry::getOrCreateSPIRVIntegerType
SPIRVType * getOrCreateSPIRVIntegerType(unsigned BitWidth, MachineIRBuilder &MIRBuilder)
Definition: SPIRVGlobalRegistry.cpp:684
llvm::BitWidth
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:147
llvm::SPIRVGlobalRegistry::find
Register find(const Constant *C, MachineFunction *MF)
Definition: SPIRVGlobalRegistry.h:82
llvm::AMDGPU::Hwreg::Width
Width
Definition: SIDefines.h:439
llvm::SPIRVGlobalRegistry::buildConstantInt
Register buildConstantInt(uint64_t Val, MachineIRBuilder &MIRBuilder, SPIRVType *SpvType=nullptr, bool EmitIR=true)
Definition: SPIRVGlobalRegistry.cpp:176
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
llvm::SPIRVGlobalRegistry::hasSPIRVTypeForVReg
bool hasSPIRVTypeForVReg(Register VReg) const
Definition: SPIRVGlobalRegistry.h:139
llvm::SPIRVGlobalRegistry::getPointerStorageClass
SPIRV::StorageClass::StorageClass getPointerStorageClass(Register VReg) const
Definition: SPIRVGlobalRegistry.cpp:675
llvm::SPIRVGeneralDuplicatesTracker::add
void add(const Type *T, const MachineFunction *MF, Register R)
Definition: SPIRVDuplicatesTracker.h:133
llvm::SPIRVGlobalRegistry::isScalarOrVectorSigned
bool isScalarOrVectorSigned(const SPIRVType *Type) const
Definition: SPIRVGlobalRegistry.cpp:663
llvm::SPIRVGlobalRegistry::getOrCreateSPIRVVectorType
SPIRVType * getOrCreateSPIRVVectorType(SPIRVType *BaseType, unsigned NumElements, MachineIRBuilder &MIRBuilder)
Definition: SPIRVGlobalRegistry.cpp:734
llvm::SPIRVGlobalRegistry::find
Register find(const Function *F, MachineFunction *MF)
Definition: SPIRVGlobalRegistry.h:90
llvm::SPIRVGlobalRegistry::buildGlobalVariable
Register buildGlobalVariable(Register Reg, SPIRVType *BaseType, StringRef Name, const GlobalValue *GV, SPIRV::StorageClass::StorageClass Storage, const MachineInstr *Init, bool IsConst, bool HasLinkageTy, SPIRV::LinkageType::LinkageType LinkageType, MachineIRBuilder &MIRBuilder, bool IsInstSelector)
Definition: SPIRVGlobalRegistry.cpp:297
llvm::SPIRVGlobalRegistry::getOrCreateSPIRVType
SPIRVType * getOrCreateSPIRVType(const Type *Type, MachineIRBuilder &MIRBuilder, SPIRV::AccessQualifier::AccessQualifier AQ=SPIRV::AccessQualifier::ReadWrite, bool EmitIR=true)
Definition: SPIRVGlobalRegistry.cpp:604