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_SPIRVGLOBALREGISTRY_H
17#define LLVM_LIB_TARGET_SPIRV_SPIRVGLOBALREGISTRY_H
18
19#include "MCTargetDesc/SPIRVBaseInfo.h"
20#include "SPIRVIRMapping.h"
21#include "SPIRVInstrInfo.h"
22#include "SPIRVTypeInst.h"
23#include "llvm/ADT/DenseSet.h"
24#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
25#include "llvm/IR/Constant.h"
26#include "llvm/IR/TypedPointerType.h"
27
28namespace llvm {
29class SPIRVSubtarget;
30
31using StructOffsetDecorator = std::function<void(Register)>;
32
33class SPIRVGlobalRegistry : public SPIRVIRMapping {
34 // Registers holding values which have types associated with them.
35 // Initialized upon VReg definition in IRTranslator.
36 // Do not confuse this with DuplicatesTracker as DT maps Type* to <MF, Reg>
37 // where Reg = OpType...
38 // while VRegToTypeMap tracks SPIR-V type assigned to other regs (i.e. not
39 // type-declaring ones).
40 DenseMap<const MachineFunction *, DenseMap<Register, SPIRVTypeInst>>
41 VRegToTypeMap;
42
43 DenseMap<SPIRVTypeInst, const Type *> SPIRVToLLVMType;
44
45 // map a Function to its definition (as a machine instruction operand)
46 DenseMap<const Function *, const MachineOperand *> FunctionToInstr;
47 DenseMap<const MachineInstr *, const Function *> FunctionToInstrRev;
48 // map function pointer (as a machine instruction operand) to the used
49 // Function
50 DenseMap<const MachineOperand *, const Function *> InstrToFunction;
51 // Maps Functions to their calls (in a form of the machine instruction,
52 // OpFunctionCall) that happened before the definition is available
53 DenseMap<const Function *, SmallPtrSet<MachineInstr *, 8>> ForwardCalls;
54 // map a Function to its original return type before the clone function was
55 // created during substitution of aggregate arguments
56 // (see `SPIRVPrepareFunctions::removeAggregateTypesFromSignature()`)
57 DenseMap<Value *, Type *> MutatedAggRet;
58 // map an instruction to its value's attributes (type, name)
59 DenseMap<MachineInstr *, std::pair<Type *, std::string>> ValueAttrs;
60
61 SmallPtrSet<const Type *, 4> TypesInProcessing;
62 DenseMap<const Type *, SPIRVTypeInst> ForwardPointerTypes;
63
64 // Struct types decorated with Block, recorded at the point the decoration
65 // is emitted.
66 DenseSet<SPIRVTypeInst> BlockDecoratedTypes;
67
68 // Stores for each function the last inserted SPIR-V Type.
69 // See: SPIRVGlobalRegistry::createOpType.
70 DenseMap<const MachineFunction *, MachineInstr *> LastInsertedTypeMap;
71
72 // if a function returns a pointer, this is to map it into TypedPointerType
73 DenseMap<const Function *, TypedPointerType *> FunResPointerTypes;
74
75 // Current target's datalayout.
76 DataLayout DL;
77
78 // Holds the maximum ID we have in the module.
79 unsigned Bound;
80
81 // Maps values associated with untyped pointers into deduced element types of
82 // untyped pointers.
83 DenseMap<Value *, Type *> DeducedElTys;
84 // Maps composite values to deduced types where untyped pointers are replaced
85 // with typed ones.
86 DenseMap<Value *, Type *> DeducedNestedTys;
87
88 // Element type for each untyped-pointer register, which
89 // OpTypeUntypedPointerKHR omits but OpUntypedVariableKHR needs as a Data
90 // Type.
91 DenseMap<std::pair<const MachineFunction *, Register>, SPIRVTypeInst>
92 UntypedPointerElementTypes;
93 // Maps values to "assign type" calls, thus being a registry of created
94 // Intrinsic::spv_assign_ptr_type instructions.
95 DenseMap<Value *, CallInst *> AssignPtrTypeInstr;
96
97 // Maps OpVariable and OpFunction-related v-regs to its LLVM IR definition.
98 DenseMap<std::pair<const MachineFunction *, Register>, const Value *> Reg2GO;
99
100 // map of aliasing decorations to aliasing metadata, keyed per
101 // MachineFunction: the cached instructions define virtual registers, which
102 // are only valid in the function that created them.
103 DenseMap<std::pair<const MachineFunction *, const MDNode *>, MachineInstr *>
104 AliasInstMDMap;
105
106 // Add a new OpTypeXXX instruction without checking for duplicates.
107 SPIRVTypeInst createSPIRVType(const Type *Type, MachineIRBuilder &MIRBuilder,
108 SPIRV::AccessQualifier::AccessQualifier AQ,
109 bool ExplicitLayoutRequired, bool EmitIR);
110 SPIRVTypeInst
111 findSPIRVType(const Type *Ty, MachineIRBuilder &MIRBuilder,
112 SPIRV::AccessQualifier::AccessQualifier accessQual,
113 bool ExplicitLayoutRequired, bool EmitIR);
114 SPIRVTypeInst
115 restOfCreateSPIRVType(const Type *Type, MachineIRBuilder &MIRBuilder,
116 SPIRV::AccessQualifier::AccessQualifier AccessQual,
117 bool ExplicitLayoutRequired, bool EmitIR);
118
119 // Internal function creating the Types/Constants at the correct position
120 // in the function by tweaking the passed "MIRBuilder" insertion point and
121 // restoring it to the correct position. "Op" should be the function creating
122 // the specific operation you need, and should return the newly created
123 // instruction.
124 const MachineInstr *createConstOrTypeAtFunctionEntry(
125 MachineIRBuilder &MIRBuilder,
126 std::function<MachineInstr *(MachineIRBuilder &)> Op);
127
128public:
129 SPIRVGlobalRegistry(DataLayout DL);
130
131 MachineFunction *CurMF;
132
133 void setBound(unsigned V) { Bound = V; }
134 unsigned getBound() { return Bound; }
135
136 void addGlobalObject(const Value *V, const MachineFunction *MF, Register R) {
137 Reg2GO[std::make_pair(x&: MF, y&: R)] = V;
138 }
139 const Value *getGlobalObject(const MachineFunction *MF, Register R) {
140 auto It = Reg2GO.find(Val: std::make_pair(x&: MF, y&: R));
141 return It == Reg2GO.end() ? nullptr : It->second;
142 }
143
144 // Add a record to the map of function return pointer types.
145 void addReturnType(const Function *ArgF, TypedPointerType *DerivedTy) {
146 FunResPointerTypes[ArgF] = DerivedTy;
147 }
148 // Find a record in the map of function return pointer types.
149 const TypedPointerType *findReturnType(const Function *ArgF) {
150 auto It = FunResPointerTypes.find(Val: ArgF);
151 return It == FunResPointerTypes.end() ? nullptr : It->second;
152 }
153
154 // A registry of "assign type" records:
155 // - Add a record.
156 void addAssignPtrTypeInstr(Value *Val, CallInst *AssignPtrTyCI) {
157 AssignPtrTypeInstr[Val] = AssignPtrTyCI;
158 }
159 // - Remove a record.
160 void removeAssignPtrTypeInstr(Value *OldVal) {
161 AssignPtrTypeInstr.erase(Val: OldVal);
162 }
163 // - Find a record.
164 CallInst *findAssignPtrTypeInstr(const Value *Val) {
165 auto It = AssignPtrTypeInstr.find(Val);
166 return It == AssignPtrTypeInstr.end() ? nullptr : It->second;
167 }
168 // - Find a record and update its key or add a new record, if found.
169 void updateIfExistAssignPtrTypeInstr(Value *OldVal, Value *NewVal,
170 bool DeleteOld) {
171 if (CallInst *CI = findAssignPtrTypeInstr(Val: OldVal)) {
172 if (DeleteOld)
173 AssignPtrTypeInstr.erase(Val: OldVal);
174 AssignPtrTypeInstr[NewVal] = CI;
175 }
176 }
177
178 // A registry of mutated values
179 // (see `SPIRVPrepareFunctions::removeAggregateTypesFromSignature()`):
180 // - Add a record.
181 void addMutated(Value *Val, Type *Ty) { MutatedAggRet[Val] = Ty; }
182 // - Find a record.
183 Type *findMutated(const Value *Val) {
184 auto It = MutatedAggRet.find(Val);
185 return It == MutatedAggRet.end() ? nullptr : It->second;
186 }
187
188 // A registry of value's attributes (type, name)
189 // - Add a record.
190 void addValueAttrs(MachineInstr *Key, std::pair<Type *, std::string> Val) {
191 ValueAttrs[Key] = Val;
192 }
193 // - Find a record.
194 bool findValueAttrs(const MachineInstr *Key, Type *&Ty, StringRef &Name) {
195 auto It = ValueAttrs.find(Val: Key);
196 if (It == ValueAttrs.end())
197 return false;
198 Ty = It->second.first;
199 Name = It->second.second;
200 return true;
201 }
202
203 // Deduced element types of untyped pointers and composites:
204 // - Add a record to the map of deduced element types.
205 void addDeducedElementType(Value *Val, Type *Ty) { DeducedElTys[Val] = Ty; }
206 // - Find a record in the map of deduced element types.
207 Type *findDeducedElementType(const Value *Val) {
208 auto It = DeducedElTys.find(Val);
209 return It == DeducedElTys.end() ? nullptr : It->second;
210 }
211 // - Find a record and update its key or add a new record, if found.
212 void updateIfExistDeducedElementType(Value *OldVal, Value *NewVal,
213 bool DeleteOld) {
214 if (Type *Ty = findDeducedElementType(Val: OldVal)) {
215 if (DeleteOld)
216 DeducedElTys.erase(Val: OldVal);
217 DeducedElTys[NewVal] = Ty;
218 }
219 }
220 // - Add a record to the map of deduced composite types.
221 void addDeducedCompositeType(Value *Val, Type *Ty) {
222 DeducedNestedTys[Val] = Ty;
223 }
224 // - Find a record in the map of deduced composite types.
225 Type *findDeducedCompositeType(const Value *Val) {
226 auto It = DeducedNestedTys.find(Val);
227 return It == DeducedNestedTys.end() ? nullptr : It->second;
228 }
229
230 // Store the element type associated with an untyped-pointer register.
231 void setUntypedPtrElementType(Register Reg, SPIRVTypeInst ElemType) {
232 UntypedPointerElementTypes[{CurMF, Reg}] = ElemType;
233 }
234 // Get the element type associated with an untyped-pointer register.
235 SPIRVTypeInst getUntypedPtrElementType(Register Reg) const {
236 auto It = UntypedPointerElementTypes.find(Val: {CurMF, Reg});
237 return It == UntypedPointerElementTypes.end() ? nullptr : It->second;
238 }
239 // - Find a type of the given Global value
240 Type *getDeducedGlobalValueType(const GlobalValue *Global) {
241 // we may know element type if it was deduced earlier
242 Type *ElementTy = findDeducedElementType(Val: Global);
243 if (!ElementTy) {
244 // or we may know element type if it's associated with a composite
245 // value
246 if (Value *GlobalElem =
247 Global->getNumOperands() > 0 ? Global->getOperand(i: 0) : nullptr)
248 ElementTy = findDeducedCompositeType(Val: GlobalElem);
249 else if (const Function *Fn = dyn_cast<Function>(Val: Global))
250 ElementTy = SPIRV::getOriginalFunctionType(F: *Fn);
251 }
252 return ElementTy ? ElementTy : Global->getValueType();
253 }
254
255 // Map a machine operand that represents a use of a function via function
256 // pointer to a machine operand that represents the function definition.
257 // Return either the register or invalid value, because we have no context for
258 // a good diagnostic message in case of unexpectedly missing references.
259 const MachineOperand *getFunctionDefinitionByUse(const MachineOperand *Use) {
260 auto ResF = InstrToFunction.find(Val: Use);
261 if (ResF == InstrToFunction.end())
262 return nullptr;
263 auto ResReg = FunctionToInstr.find(Val: ResF->second);
264 return ResReg == FunctionToInstr.end() ? nullptr : ResReg->second;
265 }
266
267 // Map a Function to a machine instruction that represents the function
268 // definition.
269 const MachineInstr *getFunctionDefinition(const Function *F) {
270 if (!F)
271 return nullptr;
272 auto MOIt = FunctionToInstr.find(Val: F);
273 return MOIt == FunctionToInstr.end() ? nullptr : MOIt->second->getParent();
274 }
275
276 // Map a Function to a machine instruction that represents the function
277 // definition.
278 const Function *getFunctionByDefinition(const MachineInstr *MI) {
279 if (!MI)
280 return nullptr;
281 auto FIt = FunctionToInstrRev.find(Val: MI);
282 return FIt == FunctionToInstrRev.end() ? nullptr : FIt->second;
283 }
284
285 // map function pointer (as a machine instruction operand) to the used
286 // Function
287 void recordFunctionPointer(const MachineOperand *MO, const Function *F) {
288 InstrToFunction[MO] = F;
289 }
290
291 // map a Function to its definition (as a machine instruction)
292 void recordFunctionDefinition(const Function *F, const MachineOperand *MO) {
293 FunctionToInstr[F] = MO;
294 FunctionToInstrRev[MO->getParent()] = F;
295 }
296
297 // Return true if any OpConstantFunctionPointerINTEL were generated
298 bool hasConstFunPtr() { return !InstrToFunction.empty(); }
299
300 // Add a record about forward function call.
301 void addForwardCall(const Function *F, MachineInstr *MI) {
302 ForwardCalls[F].insert(Ptr: MI);
303 }
304
305 // Map a Function to the vector of machine instructions that represents
306 // forward function calls or to nullptr if not found.
307 SmallPtrSet<MachineInstr *, 8> *getForwardCalls(const Function *F) {
308 auto It = ForwardCalls.find(Val: F);
309 return It == ForwardCalls.end() ? nullptr : &It->second;
310 }
311
312 // Get or create a SPIR-V type corresponding the given LLVM IR type,
313 // and map it to the given VReg.
314 SPIRVTypeInst assignTypeToVReg(const Type *Type, Register VReg,
315 MachineIRBuilder &MIRBuilder,
316 SPIRV::AccessQualifier::AccessQualifier AQ,
317 bool EmitIR);
318 SPIRVTypeInst assignIntTypeToVReg(unsigned BitWidth, Register VReg,
319 MachineInstr &I, const SPIRVInstrInfo &TII);
320
321 // In cases where the SPIR-V type is already known, this function can be
322 // used to map it to the given VReg.
323 void assignSPIRVTypeToVReg(SPIRVTypeInst Type, Register VReg,
324 const MachineFunction &MF);
325
326 // Either generate a new OpTypeXXX instruction or return an existing one
327 // corresponding to the given LLVM IR type.
328 // EmitIR controls if we emit GMIR or SPV constants (e.g. for array sizes)
329 // because this method may be called from InstructionSelector and we don't
330 // want to emit extra IR instructions there.
331 SPIRVTypeInst getOrCreateSPIRVType(const Type *Type, MachineInstr &I,
332 SPIRV::AccessQualifier::AccessQualifier AQ,
333 bool EmitIR) {
334 MachineIRBuilder MIRBuilder(I);
335 return getOrCreateSPIRVType(Type, MIRBuilder, AQ, EmitIR);
336 }
337
338 SPIRVTypeInst getOrCreateSPIRVType(const Type *Type,
339 MachineIRBuilder &MIRBuilder,
340 SPIRV::AccessQualifier::AccessQualifier AQ,
341 bool EmitIR) {
342 return getOrCreateSPIRVType(Type, MIRBuilder, AQ, ExplicitLayoutRequired: false, EmitIR);
343 }
344
345 const Type *getTypeForSPIRVType(SPIRVTypeInst Ty) const {
346 auto Res = SPIRVToLLVMType.find(Val: Ty);
347 assert(Res != SPIRVToLLVMType.end());
348 return Res->second;
349 }
350
351 // Return a pointee's type, or nullptr otherwise.
352 SPIRVTypeInst getPointeeType(SPIRVTypeInst PtrType);
353
354 // Either generate a new OpTypeXXX instruction or return an existing one
355 // corresponding to the given string containing the name of the builtin type.
356 // Return nullptr if unable to recognize SPIRV type name from `TypeStr`.
357 SPIRVTypeInst getOrCreateSPIRVTypeByName(
358 StringRef TypeStr, MachineIRBuilder &MIRBuilder, bool EmitIR,
359 SPIRV::StorageClass::StorageClass SC = SPIRV::StorageClass::Function,
360 SPIRV::AccessQualifier::AccessQualifier AQ =
361 SPIRV::AccessQualifier::ReadWrite);
362
363 // Return the SPIR-V type instruction corresponding to the given VReg, or
364 // nullptr if no such type instruction exists. The second argument MF
365 // allows to search for the association in a context of the machine functions
366 // than the current one, without switching between different "current" machine
367 // functions.
368 SPIRVTypeInst getSPIRVTypeForVReg(Register VReg,
369 const MachineFunction *MF = nullptr) const;
370
371 // Return the result type of the instruction defining the register.
372 SPIRVTypeInst getResultType(Register VReg, MachineFunction *MF = nullptr);
373
374 // Return the VReg holding the result of the given OpTypeXXX instruction.
375 Register getSPIRVTypeID(SPIRVTypeInst SpirvType) const;
376
377 // Return previous value of the current machine function
378 MachineFunction *setCurrentFunc(MachineFunction &MF) {
379 MachineFunction *Ret = CurMF;
380 CurMF = &MF;
381 return Ret;
382 }
383
384 // Return true if the type is an aggregate type.
385 bool isAggregateType(SPIRVTypeInst Type) const {
386 return Type && (Type->getOpcode() == SPIRV::OpTypeStruct ||
387 Type->getOpcode() == SPIRV::OpTypeArray);
388 }
389
390 // Whether the given VReg has an OpTypeXXX instruction mapped to it with the
391 // given opcode (e.g. OpTypeFloat).
392 bool isScalarOfType(Register VReg, unsigned TypeOpcode) const;
393
394 // Return true if the given VReg's assigned SPIR-V type is either a scalar
395 // matching the given opcode, or a vector with an element type matching that
396 // opcode (e.g. OpTypeBool, or OpTypeVector %x 4, where %x is OpTypeBool).
397 bool isScalarOrVectorOfType(Register VReg, unsigned TypeOpcode) const;
398
399 // Returns true if `Type` is a resource type. This could be an image type
400 // or a struct for a buffer decorated with the block decoration.
401 bool isResourceType(SPIRVTypeInst Type) const;
402
403 // Return number of elements in a vector if the argument is associated with
404 // a vector type. Return 1 for a scalar type, and 0 for a missing type.
405 unsigned getScalarOrVectorComponentCount(Register VReg) const;
406 unsigned getScalarOrVectorComponentCount(SPIRVTypeInst Type) const;
407
408 // Return the component type in a vector if the argument is associated with
409 // a vector type. Returns the argument itself for other types, and nullptr
410 // for a missing type.
411 SPIRVTypeInst getScalarOrVectorComponentType(SPIRVTypeInst Type) const;
412
413 // For vectors or scalars of booleans, integers and floats, return the scalar
414 // type's bitwidth. Otherwise calls llvm_unreachable().
415 unsigned getScalarOrVectorBitWidth(SPIRVTypeInst Type) const;
416
417 // For vectors or scalars of integers and floats, return total bitwidth of the
418 // argument. Otherwise returns 0.
419 unsigned getNumScalarOrVectorTotalBitWidth(SPIRVTypeInst Type) const;
420
421 // True if a pointer to this element type must stay typed rather than become
422 // OpTypeUntypedPointerKHR. Such an element type is either a function type,
423 // which an untyped pointer cannot express, or an opaque builtin type such as
424 // an image or a sampler.
425 bool shouldKeepTypedPtrType(SPIRVTypeInst ElemType) const;
426
427 // True if a pointer to this element type should be emitted as
428 // OpTypeUntypedPointerKHR rather than OpTypePointer.
429 bool shouldUseUntypedPointer(SPIRVTypeInst ElemType,
430 const SPIRVSubtarget &ST) const;
431
432 // Byte size of a pointer value's IR-deduced element type, or 0 if unknown.
433 // Array indexing and copy strides work in terms of the alloc size, so this
434 // reports the size a value of that type occupies in an array. For OpenCL
435 // that means a 3-component vector is as large as a 4-component one.
436 unsigned getDeducedPointeeByteSize(const Value *PtrVal) {
437 if (Type *ElemTy = findDeducedElementType(Val: PtrVal))
438 return DL.getTypeAllocSize(Ty: ElemTy).getFixedValue();
439 return 0;
440 }
441
442 // Returns either pointer to integer type, that may be a type of vector
443 // elements or an original type, or nullptr if the argument is niether
444 // an integer scalar, nor an integer vector
445 SPIRVTypeInst retrieveScalarOrVectorIntType(SPIRVTypeInst Type) const;
446
447 // For integer vectors or scalars, return whether the integers are signed.
448 bool isScalarOrVectorSigned(SPIRVTypeInst Type) const;
449
450 // Gets the storage class of the pointer type assigned to this vreg.
451 SPIRV::StorageClass::StorageClass getPointerStorageClass(Register VReg) const;
452 SPIRV::StorageClass::StorageClass
453 getPointerStorageClass(SPIRVTypeInst Type) const;
454
455 // Return the number of bits SPIR-V pointers and size_t variables require.
456 unsigned getPointerSize() const {
457 return DL.getPointerSizeInBits(/* AS = */ AS: 0);
458 }
459
460 // Returns true if two types are defined and are compatible in a sense of
461 // OpBitcast instruction
462 bool isBitcastCompatible(SPIRVTypeInst Type1, SPIRVTypeInst Type2) const;
463
464 // Informs about removal of the machine instruction and invalidates data
465 // structures referring this instruction.
466 void invalidateMachineInstr(MachineInstr *MI);
467
468private:
469 SPIRVTypeInst getOpTypeBool(MachineIRBuilder &MIRBuilder);
470
471 const Type *adjustIntTypeByWidth(const Type *Ty) const;
472 unsigned adjustOpTypeIntWidth(unsigned Width) const;
473
474 SPIRVTypeInst getOrCreateSPIRVType(const Type *Type,
475 MachineIRBuilder &MIRBuilder,
476 SPIRV::AccessQualifier::AccessQualifier AQ,
477 bool ExplicitLayoutRequired, bool EmitIR);
478
479 SPIRVTypeInst getOpTypeInt(unsigned Width, MachineIRBuilder &MIRBuilder,
480 bool IsSigned = false);
481
482 SPIRVTypeInst getOpTypeFloat(uint32_t Width, MachineIRBuilder &MIRBuilder);
483
484 SPIRVTypeInst getOpTypeFloat(uint32_t Width, MachineIRBuilder &MIRBuilder,
485 SPIRV::FPEncoding::FPEncoding FPEncode);
486
487 SPIRVTypeInst getOpTypeVectorImpl(uint32_t NumElems, SPIRVTypeInst ElemType,
488 MachineIRBuilder &MIRBuilder,
489 bool IsLongVectorEXT = false);
490
491 SPIRVTypeInst getOpTypeVector(uint32_t NumElems, SPIRVTypeInst ElemType,
492 MachineIRBuilder &MIRBuilder);
493
494 SPIRVTypeInst getOpTypeVectorIdEXT(uint32_t NumElems, SPIRVTypeInst ElemType,
495 MachineIRBuilder &MIRBuilder);
496
497 SPIRVTypeInst getOpTypeArray(uint32_t NumElems, SPIRVTypeInst ElemType,
498 MachineIRBuilder &MIRBuilder,
499 bool ExplicitLayoutRequired, bool EmitIR);
500
501 SPIRVTypeInst getOpTypeOpaque(const StructType *Ty,
502 MachineIRBuilder &MIRBuilder);
503
504 SPIRVTypeInst getOpTypeStruct(const StructType *Ty,
505 MachineIRBuilder &MIRBuilder,
506 SPIRV::AccessQualifier::AccessQualifier AccQual,
507 StructOffsetDecorator Decorator, bool EmitIR);
508
509 SPIRVTypeInst getOpTypePointer(SPIRV::StorageClass::StorageClass SC,
510 SPIRVTypeInst ElemType,
511 MachineIRBuilder &MIRBuilder, Register Reg);
512
513 SPIRVTypeInst
514 getOpTypeFunction(const FunctionType *Ty, SPIRVTypeInst RetType,
515 const SmallVectorImpl<SPIRVTypeInst> &ArgTypes,
516 MachineIRBuilder &MIRBuilder);
517
518 SPIRVTypeInst
519 getOrCreateSpecialType(const Type *Ty, MachineIRBuilder &MIRBuilder,
520 SPIRV::AccessQualifier::AccessQualifier AccQual);
521
522 SPIRVTypeInst finishCreatingSPIRVType(const Type *LLVMTy,
523 SPIRVTypeInst SpirvType);
524 Register getOrCreateBaseRegister(Constant *Val, MachineInstr &I,
525 SPIRVTypeInst SpvType,
526 const SPIRVInstrInfo &TII, unsigned BitWidth,
527 bool ZeroAsNull);
528 Register getOrCreateCompositeOrNull(Constant *Val, MachineInstr &I,
529 SPIRVTypeInst SpvType,
530 const SPIRVInstrInfo &TII, Constant *CA,
531 unsigned BitWidth, unsigned ElemCnt,
532 bool ZeroAsNull = true);
533
534 Register getOrCreateIntCompositeOrNull(uint64_t Val,
535 MachineIRBuilder &MIRBuilder,
536 SPIRVTypeInst SpvType, bool EmitIR,
537 Constant *CA, unsigned BitWidth,
538 unsigned ElemCnt);
539
540 // Returns a pointer to a SPIR-V pointer type with the given base type and
541 // storage class. It is the responsibility of the caller to make sure the
542 // decorations on the base type are valid for the given storage class. For
543 // example, it has the correct offset and stride decorations.
544 // ForceTyped keeps an OpTypePointer even when untyped pointers are available,
545 // for cases where the pointee type must be preserved (e.g. a byval/byref/sret
546 // aggregate argument).
547 SPIRVTypeInst getOrCreateSPIRVPointerTypeInternal(
548 SPIRVTypeInst BaseType, MachineIRBuilder &MIRBuilder,
549 SPIRV::StorageClass::StorageClass SC, bool ForceTyped = false);
550
551 void addStructOffsetDecorations(Register Reg, StructType *Ty,
552 MachineIRBuilder &MIRBuilder);
553 void addArrayStrideDecorations(Register Reg, Type *ElementType,
554 MachineIRBuilder &MIRBuilder);
555
556 void constrainSelectedInstRegOperands(MachineInstrBuilder &MIB) const;
557
558 SPIRVTypeInst
559 getOrCreateOpTypeImage(MachineIRBuilder &MIRBuilder,
560 SPIRVTypeInst SampledType, SPIRV::Dim::Dim Dim,
561 uint32_t Depth, uint32_t Arrayed,
562 uint32_t Multisampled, uint32_t Sampled,
563 SPIRV::ImageFormat::ImageFormat ImageFormat,
564 SPIRV::AccessQualifier::AccessQualifier AccQual);
565
566public:
567 Register buildConstantInt(uint64_t Val, MachineIRBuilder &MIRBuilder,
568 SPIRVTypeInst SpvType, bool EmitIR,
569 bool ZeroAsNull = true);
570 Register getOrCreateConstInt(uint64_t Val, MachineInstr &I,
571 SPIRVTypeInst SpvType, const SPIRVInstrInfo &TII,
572 bool ZeroAsNull = true);
573 Register getOrCreateConstInt(const APInt &Val, MachineInstr &I,
574 SPIRVTypeInst SpvType, const SPIRVInstrInfo &TII,
575 bool ZeroAsNull = true);
576 Register createConstInt(const ConstantInt *CI, MachineInstr &I,
577 SPIRVTypeInst SpvType, const SPIRVInstrInfo &TII,
578 bool ZeroAsNull);
579 Register getOrCreateConstFP(APFloat Val, MachineInstr &I,
580 SPIRVTypeInst SpvType, const SPIRVInstrInfo &TII,
581 bool ZeroAsNull = true);
582 Register createConstFP(const ConstantFP *CF, MachineInstr &I,
583 SPIRVTypeInst SpvType, const SPIRVInstrInfo &TII,
584 bool ZeroAsNull);
585 Register buildConstantFP(APFloat Val, MachineIRBuilder &MIRBuilder,
586 SPIRVTypeInst SpvType = nullptr);
587
588 Register getOrCreateConstVector(uint64_t Val, MachineInstr &I,
589 SPIRVTypeInst SpvType,
590 const SPIRVInstrInfo &TII,
591 bool ZeroAsNull = true);
592 Register getOrCreateConstVector(const APInt &Val, MachineInstr &I,
593 SPIRVTypeInst SpvType,
594 const SPIRVInstrInfo &TII,
595 bool ZeroAsNull = true);
596 Register getOrCreateConstVector(APFloat Val, MachineInstr &I,
597 SPIRVTypeInst SpvType,
598 const SPIRVInstrInfo &TII,
599 bool ZeroAsNull = true);
600 Register getOrCreateConstIntArray(uint64_t Val, size_t Num, MachineInstr &I,
601 SPIRVTypeInst SpvType,
602 const SPIRVInstrInfo &TII);
603 Register getOrCreateConsIntVector(uint64_t Val, MachineIRBuilder &MIRBuilder,
604 SPIRVTypeInst SpvType, bool EmitIR);
605 Register getOrCreateConstNullPtr(MachineIRBuilder &MIRBuilder,
606 SPIRVTypeInst SpvType);
607 Register buildConstantSampler(Register Res, unsigned AddrMode, unsigned Param,
608 unsigned FilerMode,
609 MachineIRBuilder &MIRBuilder);
610 Register getOrCreateUndef(MachineInstr &I, SPIRVTypeInst SpvType,
611 const SPIRVInstrInfo &TII);
612 Register buildGlobalVariable(
613 Register Reg, SPIRVTypeInst BaseType, StringRef Name,
614 const GlobalValue *GV, SPIRV::StorageClass::StorageClass Storage,
615 const MachineInstr *Init, bool IsConst,
616 const std::optional<SPIRV::LinkageType::LinkageType> &LinkageType,
617 MachineIRBuilder &MIRBuilder, bool IsInstSelector);
618 Register getOrCreateGlobalVariableWithBinding(SPIRVTypeInst VarType,
619 uint32_t Set, uint32_t Binding,
620 StringRef Name,
621 MachineIRBuilder &MIRBuilder);
622
623 // Convenient helpers for getting types with check for duplicates.
624 SPIRVTypeInst getOrCreateSPIRVIntegerType(unsigned BitWidth,
625 MachineIRBuilder &MIRBuilder);
626 SPIRVTypeInst getOrCreateSPIRVIntegerType(unsigned BitWidth, MachineInstr &I,
627 const SPIRVInstrInfo &TII);
628 SPIRVTypeInst getOrCreateSPIRVType(unsigned BitWidth, MachineInstr &I,
629 const SPIRVInstrInfo &TII,
630 unsigned SPIRVOPcode, Type *LLVMTy);
631 SPIRVTypeInst getOrCreateSPIRVFloatType(unsigned BitWidth, MachineInstr &I,
632 const SPIRVInstrInfo &TII);
633 SPIRVTypeInst getOrCreateSPIRVBoolType(MachineIRBuilder &MIRBuilder,
634 bool EmitIR);
635 SPIRVTypeInst getOrCreateSPIRVBoolType(MachineInstr &I,
636 const SPIRVInstrInfo &TII);
637 SPIRVTypeInst getOrCreateSPIRVVectorType(SPIRVTypeInst BaseType,
638 unsigned NumElements,
639 MachineIRBuilder &MIRBuilder,
640 bool EmitIR);
641 SPIRVTypeInst getOrCreateSPIRVVectorType(SPIRVTypeInst BaseType,
642 unsigned NumElements,
643 MachineInstr &I,
644 const SPIRVInstrInfo &TII);
645 SPIRVTypeInst getOpTypeVoid(MachineIRBuilder &MIRBuilder);
646
647 // Returns a pointer to a SPIR-V pointer type with the given base type and
648 // storage class. The base type will be translated to a SPIR-V type, and the
649 // appropriate layout decorations will be added to the base type.
650 // See getOrCreateSPIRVPointerTypeInternal for ForceTyped.
651 SPIRVTypeInst getOrCreateSPIRVPointerType(
652 const Type *BaseType, MachineIRBuilder &MIRBuilder,
653 SPIRV::StorageClass::StorageClass SC, bool ForceTyped = false);
654 SPIRVTypeInst
655 getOrCreateSPIRVPointerType(const Type *BaseType, MachineInstr &I,
656 SPIRV::StorageClass::StorageClass SC,
657 bool ForceTyped = false);
658
659 // Like getOrCreateSPIRVPointerType, but always returns an OpTypePointer even
660 // when untyped pointers are available. Use this when the pointee type must be
661 // preserved (e.g. a byval/byref/sret aggregate argument).
662 SPIRVTypeInst
663 getOrCreateSPIRVTypedPointerType(const Type *BaseType,
664 MachineIRBuilder &MIRBuilder,
665 SPIRV::StorageClass::StorageClass SC) {
666 return getOrCreateSPIRVPointerType(BaseType, MIRBuilder, SC,
667 /*ForceTyped=*/ForceTyped: true);
668 }
669
670 // Returns a pointer to a SPIR-V pointer type with the given base type and
671 // storage class. It is the responsibility of the caller to make sure the
672 // decorations on the base type are valid for the given storage class. For
673 // example, it has the correct offset and stride decorations.
674 SPIRVTypeInst
675 getOrCreateSPIRVPointerType(SPIRVTypeInst BaseType,
676 MachineIRBuilder &MIRBuilder,
677 SPIRV::StorageClass::StorageClass SC);
678
679 // Returns a pointer to a SPIR-V pointer type that is the same as `PtrType`
680 // except the stroage class has been changed to `SC`. It is the responsibility
681 // of the caller to be sure that the original and new storage class have the
682 // same layout requirements.
683 SPIRVTypeInst changePointerStorageClass(SPIRVTypeInst PtrType,
684 SPIRV::StorageClass::StorageClass SC,
685 MachineInstr &I);
686
687 // Returns OpTypeUntypedPointerKHR for the given storage class.
688 SPIRVTypeInst
689 getOrCreateSPIRVUntypedPointerType(SPIRV::StorageClass::StorageClass SC,
690 MachineIRBuilder &MIRBuilder);
691
692 SPIRVTypeInst
693 getOrCreateVulkanBufferType(MachineIRBuilder &MIRBuilder, Type *ElemType,
694 SPIRV::StorageClass::StorageClass SC,
695 bool IsWritable, bool EmitIr = false);
696
697 SPIRVTypeInst getOrCreatePaddingType(MachineIRBuilder &MIRBuilder);
698
699 SPIRVTypeInst getOrCreateVulkanPushConstantType(MachineIRBuilder &MIRBuilder,
700 Type *ElemType);
701
702 SPIRVTypeInst getOrCreateLayoutType(MachineIRBuilder &MIRBuilder,
703 const TargetExtType *T,
704 bool EmitIr = false);
705
706 SPIRVTypeInst
707 getImageType(const TargetExtType *ExtensionType,
708 const SPIRV::AccessQualifier::AccessQualifier Qualifier,
709 MachineIRBuilder &MIRBuilder);
710
711 SPIRVTypeInst getOrCreateOpTypeSampler(MachineIRBuilder &MIRBuilder);
712
713 SPIRVTypeInst getOrCreateOpTypeSampledImage(SPIRVTypeInst ImageType,
714 MachineIRBuilder &MIRBuilder);
715 SPIRVTypeInst getOrCreateOpTypeCoopMatr(MachineIRBuilder &MIRBuilder,
716 const TargetExtType *ExtensionType,
717 SPIRVTypeInst ElemType,
718 uint32_t Scope, uint32_t Rows,
719 uint32_t Columns, uint32_t Use,
720 bool EmitIR);
721 SPIRVTypeInst
722 getOrCreateOpTypePipe(MachineIRBuilder &MIRBuilder,
723 SPIRV::AccessQualifier::AccessQualifier AccQual);
724 SPIRVTypeInst getOrCreateOpTypeDeviceEvent(MachineIRBuilder &MIRBuilder);
725 SPIRVTypeInst getOrCreateOpTypeFunctionWithArgs(
726 const Type *Ty, SPIRVTypeInst RetType,
727 const SmallVectorImpl<SPIRVTypeInst> &ArgTypes,
728 MachineIRBuilder &MIRBuilder);
729 SPIRVTypeInst getOrCreateOpTypeByOpcode(const Type *Ty,
730 MachineIRBuilder &MIRBuilder,
731 unsigned Opcode);
732
733 SPIRVTypeInst getOrCreateUnknownType(const Type *Ty,
734 MachineIRBuilder &MIRBuilder,
735 unsigned Opcode,
736 const ArrayRef<MachineOperand> Operands);
737
738 const TargetRegisterClass *getRegClass(SPIRVTypeInst SpvType) const;
739 LLT getRegType(SPIRVTypeInst SpvType) const;
740
741 MachineInstr *getOrAddMemAliasingINTELInst(MachineIRBuilder &MIRBuilder,
742 const MDNode *AliasingListMD);
743 void buildMemAliasingOpDecorate(Register Reg, MachineIRBuilder &MIRBuilder,
744 uint32_t Dec, const MDNode *GVarMD);
745 // Replace all uses of a |Old| with |New| updates the global registry type
746 // mappings.
747 void replaceAllUsesWith(Value *Old, Value *New, bool DeleteOld = true);
748
749 void buildAssignType(IRBuilder<> &B, Type *Ty, Value *Arg,
750 bool CanUseAnyVectorRank);
751 void buildAssignPtr(IRBuilder<> &B, Type *ElemTy, Value *Arg);
752 void updateAssignType(CallInst *AssignCI, Value *Arg, Value *OfType);
753};
754} // end namespace llvm
755#endif // LLVM_LIB_TARGET_SPIRV_SPIRVGLOBALREGISTRY_H
756