LLVM 24.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_SPIRVGLOBALREGISTRY_H
17#define LLVM_LIB_TARGET_SPIRV_SPIRVGLOBALREGISTRY_H
18
20#include "SPIRVIRMapping.h"
21#include "SPIRVInstrInfo.h"
22#include "SPIRVTypeInst.h"
23#include "llvm/ADT/DenseSet.h"
25#include "llvm/IR/Constant.h"
27
28namespace llvm {
29class SPIRVSubtarget;
30
31using StructOffsetDecorator = std::function<void(Register)>;
32
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).
41 VRegToTypeMap;
42
44
45 // map a Function to its definition (as a machine instruction operand)
48 // map function pointer (as a machine instruction operand) to the used
49 // Function
51 // Maps Functions to their calls (in a form of the machine instruction,
52 // OpFunctionCall) that happened before the definition is available
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)
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.
71
72 // if a function returns a pointer, this is to map it into TypedPointerType
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.
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.
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.
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);
111 findSPIRVType(const Type *Ty, MachineIRBuilder &MIRBuilder,
112 SPIRV::AccessQualifier::AccessQualifier accessQual,
113 bool ExplicitLayoutRequired, bool EmitIR);
115 restOfCreateSPIRVType(const Type *Type, MachineIRBuilder &MIRBuilder,
116 SPIRV::AccessQualifier::AccessQualifier AccessQual,
117 bool ExplicitLayoutRequired, bool EmitIR);
118 // Key SpirvType by Ty unless Ty already has a type in MF.
119 void addTypeKey(const Type *Ty, bool ExplicitLayoutRequired,
120 SPIRVTypeInst SpirvType, const MachineFunction &MF);
121
122 // Internal function creating the Types/Constants at the correct position
123 // in the function by tweaking the passed "MIRBuilder" insertion point and
124 // restoring it to the correct position. "Op" should be the function creating
125 // the specific operation you need, and should return the newly created
126 // instruction.
127 const MachineInstr *createConstOrTypeAtFunctionEntry(
128 MachineIRBuilder &MIRBuilder,
129 std::function<MachineInstr *(MachineIRBuilder &)> Op);
130
131public:
133
135
136 void setBound(unsigned V) { Bound = V; }
137 unsigned getBound() { return Bound; }
138
139 void addGlobalObject(const Value *V, const MachineFunction *MF, Register R) {
140 Reg2GO[std::make_pair(MF, R)] = V;
141 }
143 auto It = Reg2GO.find(std::make_pair(MF, R));
144 return It == Reg2GO.end() ? nullptr : It->second;
145 }
146
147 // Add a record to the map of function return pointer types.
148 void addReturnType(const Function *ArgF, TypedPointerType *DerivedTy) {
149 FunResPointerTypes[ArgF] = DerivedTy;
150 }
151 // Find a record in the map of function return pointer types.
153 auto It = FunResPointerTypes.find(ArgF);
154 return It == FunResPointerTypes.end() ? nullptr : It->second;
155 }
156
157 // A registry of "assign type" records:
158 // - Add a record.
159 void addAssignPtrTypeInstr(Value *Val, CallInst *AssignPtrTyCI) {
160 AssignPtrTypeInstr[Val] = AssignPtrTyCI;
161 }
162 // - Remove a record.
164 AssignPtrTypeInstr.erase(OldVal);
165 }
166 // - Find a record.
168 auto It = AssignPtrTypeInstr.find(Val);
169 return It == AssignPtrTypeInstr.end() ? nullptr : It->second;
170 }
171 // - Find a record and update its key or add a new record, if found.
173 bool DeleteOld) {
174 if (CallInst *CI = findAssignPtrTypeInstr(OldVal)) {
175 if (DeleteOld)
176 AssignPtrTypeInstr.erase(OldVal);
177 AssignPtrTypeInstr[NewVal] = CI;
178 }
179 }
180
181 // A registry of mutated values
182 // (see `SPIRVPrepareFunctions::removeAggregateTypesFromSignature()`):
183 // - Add a record.
184 void addMutated(Value *Val, Type *Ty) { MutatedAggRet[Val] = Ty; }
185 // - Find a record.
186 Type *findMutated(const Value *Val) {
187 auto It = MutatedAggRet.find(Val);
188 return It == MutatedAggRet.end() ? nullptr : It->second;
189 }
190
191 // A registry of value's attributes (type, name)
192 // - Add a record.
193 void addValueAttrs(MachineInstr *Key, std::pair<Type *, std::string> Val) {
194 ValueAttrs[Key] = Val;
195 }
196 // - Find a record.
197 bool findValueAttrs(const MachineInstr *Key, Type *&Ty, StringRef &Name) {
198 auto It = ValueAttrs.find(Key);
199 if (It == ValueAttrs.end())
200 return false;
201 Ty = It->second.first;
202 Name = It->second.second;
203 return true;
204 }
205
206 // Deduced element types of untyped pointers and composites:
207 // - Add a record to the map of deduced element types.
208 void addDeducedElementType(Value *Val, Type *Ty) { DeducedElTys[Val] = Ty; }
209 // - Find a record in the map of deduced element types.
211 auto It = DeducedElTys.find(Val);
212 return It == DeducedElTys.end() ? nullptr : It->second;
213 }
214 // - Find a record and update its key or add a new record, if found.
216 bool DeleteOld) {
217 if (Type *Ty = findDeducedElementType(OldVal)) {
218 if (DeleteOld)
219 DeducedElTys.erase(OldVal);
220 DeducedElTys[NewVal] = Ty;
221 }
222 }
223 // - Add a record to the map of deduced composite types.
225 DeducedNestedTys[Val] = Ty;
226 }
227 // - Find a record in the map of deduced composite types.
229 auto It = DeducedNestedTys.find(Val);
230 return It == DeducedNestedTys.end() ? nullptr : It->second;
231 }
232
233 // Store the element type associated with an untyped-pointer register.
235 UntypedPointerElementTypes[{CurMF, Reg}] = ElemType;
236 }
237 // Get the element type associated with an untyped-pointer register.
239 auto It = UntypedPointerElementTypes.find({CurMF, Reg});
240 return It == UntypedPointerElementTypes.end() ? nullptr : It->second;
241 }
242 // - Find a type of the given Global value
244 // we may know element type if it was deduced earlier
245 Type *ElementTy = findDeducedElementType(Global);
246 if (!ElementTy) {
247 // or we may know element type if it's associated with a composite
248 // value
249 if (Value *GlobalElem =
250 Global->getNumOperands() > 0 ? Global->getOperand(0) : nullptr)
251 ElementTy = findDeducedCompositeType(GlobalElem);
252 else if (const Function *Fn = dyn_cast<Function>(Global))
253 ElementTy = SPIRV::getOriginalFunctionType(*Fn);
254 }
255 return ElementTy ? ElementTy : Global->getValueType();
256 }
257
258 // Map a machine operand that represents a use of a function via function
259 // pointer to a machine operand that represents the function definition.
260 // Return either the register or invalid value, because we have no context for
261 // a good diagnostic message in case of unexpectedly missing references.
263 auto ResF = InstrToFunction.find(Use);
264 if (ResF == InstrToFunction.end())
265 return nullptr;
266 auto ResReg = FunctionToInstr.find(ResF->second);
267 return ResReg == FunctionToInstr.end() ? nullptr : ResReg->second;
268 }
269
270 // Map a Function to a machine instruction that represents the function
271 // definition.
273 if (!F)
274 return nullptr;
275 auto MOIt = FunctionToInstr.find(F);
276 return MOIt == FunctionToInstr.end() ? nullptr : MOIt->second->getParent();
277 }
278
279 // Map a Function to a machine instruction that represents the function
280 // definition.
282 if (!MI)
283 return nullptr;
284 auto FIt = FunctionToInstrRev.find(MI);
285 return FIt == FunctionToInstrRev.end() ? nullptr : FIt->second;
286 }
287
288 // map function pointer (as a machine instruction operand) to the used
289 // Function
291 InstrToFunction[MO] = F;
292 }
293
294 // map a Function to its definition (as a machine instruction)
296 FunctionToInstr[F] = MO;
297 FunctionToInstrRev[MO->getParent()] = F;
298 }
299
300 // Return true if any OpConstantFunctionPointerINTEL were generated
301 bool hasConstFunPtr() { return !InstrToFunction.empty(); }
302
303 // Add a record about forward function call.
305 ForwardCalls[F].insert(MI);
306 }
307
308 // Map a Function to the vector of machine instructions that represents
309 // forward function calls or to nullptr if not found.
311 auto It = ForwardCalls.find(F);
312 return It == ForwardCalls.end() ? nullptr : &It->second;
313 }
314
315 // Get or create a SPIR-V type corresponding the given LLVM IR type,
316 // and map it to the given VReg.
318 MachineIRBuilder &MIRBuilder,
319 SPIRV::AccessQualifier::AccessQualifier AQ,
320 bool EmitIR);
323
324 // In cases where the SPIR-V type is already known, this function can be
325 // used to map it to the given VReg.
327 const MachineFunction &MF);
328
329 // Either generate a new OpTypeXXX instruction or return an existing one
330 // corresponding to the given LLVM IR type.
331 // EmitIR controls if we emit GMIR or SPV constants (e.g. for array sizes)
332 // because this method may be called from InstructionSelector and we don't
333 // want to emit extra IR instructions there.
335 SPIRV::AccessQualifier::AccessQualifier AQ,
336 bool EmitIR) {
337 MachineIRBuilder MIRBuilder(I);
338 return getOrCreateSPIRVType(Type, MIRBuilder, AQ, EmitIR);
339 }
340
342 MachineIRBuilder &MIRBuilder,
343 SPIRV::AccessQualifier::AccessQualifier AQ,
344 bool EmitIR) {
345 return getOrCreateSPIRVType(Type, MIRBuilder, AQ, false, EmitIR);
346 }
347
349 auto Res = SPIRVToLLVMType.find(Ty);
350 assert(Res != SPIRVToLLVMType.end());
351 return Res->second;
352 }
353
354 // Return a pointee's type, or nullptr otherwise.
356
357 // Either generate a new OpTypeXXX instruction or return an existing one
358 // corresponding to the given string containing the name of the builtin type.
359 // Return nullptr if unable to recognize SPIRV type name from `TypeStr`.
361 StringRef TypeStr, MachineIRBuilder &MIRBuilder, bool EmitIR,
362 SPIRV::StorageClass::StorageClass SC = SPIRV::StorageClass::Function,
363 SPIRV::AccessQualifier::AccessQualifier AQ =
364 SPIRV::AccessQualifier::ReadWrite);
365
366 // Return the SPIR-V type instruction corresponding to the given VReg, or
367 // nullptr if no such type instruction exists. The second argument MF
368 // allows to search for the association in a context of the machine functions
369 // than the current one, without switching between different "current" machine
370 // functions.
372 const MachineFunction *MF = nullptr) const;
373
374 // Return the result type of the instruction defining the register.
376
377 // Return the VReg holding the result of the given OpTypeXXX instruction.
378 Register getSPIRVTypeID(SPIRVTypeInst SpirvType) const;
379
380 // Return previous value of the current machine function
382 MachineFunction *Ret = CurMF;
383 CurMF = &MF;
384 return Ret;
385 }
386
387 // Return true if the type is an aggregate type.
389 return Type && (Type->getOpcode() == SPIRV::OpTypeStruct ||
390 Type->getOpcode() == SPIRV::OpTypeArray);
391 }
392
393 // Whether the given VReg has an OpTypeXXX instruction mapped to it with the
394 // given opcode (e.g. OpTypeFloat).
395 bool isScalarOfType(Register VReg, unsigned TypeOpcode) const;
396
397 // Return true if the given VReg's assigned SPIR-V type is either a scalar
398 // matching the given opcode, or a vector with an element type matching that
399 // opcode (e.g. OpTypeBool, or OpTypeVector %x 4, where %x is OpTypeBool).
400 bool isScalarOrVectorOfType(Register VReg, unsigned TypeOpcode) const;
401
402 // Returns true if `Type` is a resource type. This could be an image type
403 // or a struct for a buffer decorated with the block decoration.
405
406 // Return number of elements in a vector if the argument is associated with
407 // a vector type. Return 1 for a scalar type, and 0 for a missing type.
408 unsigned getScalarOrVectorComponentCount(Register VReg) const;
410
411 // Return the component type in a vector if the argument is associated with
412 // a vector type. Returns the argument itself for other types, and nullptr
413 // for a missing type.
415
416 // For vectors or scalars of booleans, integers and floats, return the scalar
417 // type's bitwidth. Otherwise calls llvm_unreachable().
419
420 // For vectors or scalars of integers and floats, return total bitwidth of the
421 // argument. Otherwise returns 0.
423
424 // True if a pointer to this element type must stay typed rather than become
425 // OpTypeUntypedPointerKHR. Such an element type is either a function type,
426 // which an untyped pointer cannot express, or an opaque builtin type such as
427 // an image or a sampler.
428 bool shouldKeepTypedPtrType(SPIRVTypeInst ElemType) const;
429
430 // True if a pointer to this element type should be emitted as
431 // OpTypeUntypedPointerKHR rather than OpTypePointer.
433 const SPIRVSubtarget &ST) const;
434
435 // Byte size of a pointer value's IR-deduced element type, or 0 if unknown.
436 // Array indexing and copy strides work in terms of the alloc size, so this
437 // reports the size a value of that type occupies in an array. For OpenCL
438 // that means a 3-component vector is as large as a 4-component one.
439 unsigned getDeducedPointeeByteSize(const Value *PtrVal) {
440 if (Type *ElemTy = findDeducedElementType(PtrVal))
441 return DL.getTypeAllocSize(ElemTy).getFixedValue();
442 return 0;
443 }
444
445 // Returns either pointer to integer type, that may be a type of vector
446 // elements or an original type, or nullptr if the argument is niether
447 // an integer scalar, nor an integer vector
449
450 // For integer vectors or scalars, return whether the integers are signed.
452
453 // Gets the storage class of the pointer type assigned to this vreg.
454 SPIRV::StorageClass::StorageClass getPointerStorageClass(Register VReg) const;
455 SPIRV::StorageClass::StorageClass
457
458 // Return the number of bits SPIR-V pointers and size_t variables require.
459 unsigned getPointerSize() const {
460 return DL.getPointerSizeInBits(/* AS = */ 0);
461 }
462
463 // Returns true if two types are defined and are compatible in a sense of
464 // OpBitcast instruction
465 bool isBitcastCompatible(SPIRVTypeInst Type1, SPIRVTypeInst Type2) const;
466
467 // Informs about removal of the machine instruction and invalidates data
468 // structures referring this instruction.
470
471 // Rebuild the state of MF after loading MIR.
472 void restoreFromMIR(const MachineFunction &MF);
473
474 // (VReg, type ID) pairs of non-type VRegs, sorted by VReg. Skips VRegs not
475 // referenced in MF and VRegs whose type decl is no longer in MF.
477 getVRegTypeIDs(const MachineFunction &MF) const;
478
479 // Types and constants are inserted after this one at the entry of MF.
482
483private:
484 SPIRVTypeInst getOpTypeBool(MachineIRBuilder &MIRBuilder);
485
486 const Type *adjustIntTypeByWidth(const Type *Ty) const;
487 unsigned adjustOpTypeIntWidth(unsigned Width) const;
488
490 MachineIRBuilder &MIRBuilder,
491 SPIRV::AccessQualifier::AccessQualifier AQ,
492 bool ExplicitLayoutRequired, bool EmitIR);
493
494 SPIRVTypeInst getOpTypeInt(unsigned Width, MachineIRBuilder &MIRBuilder,
495 bool IsSigned = false);
496
497 SPIRVTypeInst getOpTypeFloat(uint32_t Width, MachineIRBuilder &MIRBuilder);
498
499 SPIRVTypeInst getOpTypeFloat(uint32_t Width, MachineIRBuilder &MIRBuilder,
500 SPIRV::FPEncoding::FPEncoding FPEncode);
501
502 SPIRVTypeInst getOpTypeVectorImpl(uint32_t NumElems, SPIRVTypeInst ElemType,
503 MachineIRBuilder &MIRBuilder,
504 bool IsLongVectorEXT = false);
505
506 SPIRVTypeInst getOpTypeVector(uint32_t NumElems, SPIRVTypeInst ElemType,
507 MachineIRBuilder &MIRBuilder);
508
509 SPIRVTypeInst getOpTypeVectorIdEXT(uint32_t NumElems, SPIRVTypeInst ElemType,
510 MachineIRBuilder &MIRBuilder);
511
512 SPIRVTypeInst getOpTypeArray(uint32_t NumElems, SPIRVTypeInst ElemType,
513 MachineIRBuilder &MIRBuilder,
514 bool ExplicitLayoutRequired, bool EmitIR);
515
516 SPIRVTypeInst getOpTypeOpaque(const StructType *Ty,
517 MachineIRBuilder &MIRBuilder);
518
519 SPIRVTypeInst getOpTypeStruct(const StructType *Ty,
520 MachineIRBuilder &MIRBuilder,
521 SPIRV::AccessQualifier::AccessQualifier AccQual,
522 StructOffsetDecorator Decorator, bool EmitIR);
523
524 SPIRVTypeInst getOpTypePointer(SPIRV::StorageClass::StorageClass SC,
525 SPIRVTypeInst ElemType,
526 MachineIRBuilder &MIRBuilder, Register Reg);
527
529 getOpTypeFunction(const FunctionType *Ty, SPIRVTypeInst RetType,
530 const SmallVectorImpl<SPIRVTypeInst> &ArgTypes,
531 MachineIRBuilder &MIRBuilder);
532
534 getOrCreateSpecialType(const Type *Ty, MachineIRBuilder &MIRBuilder,
535 SPIRV::AccessQualifier::AccessQualifier AccQual);
536
537 SPIRVTypeInst finishCreatingSPIRVType(const Type *LLVMTy,
538 SPIRVTypeInst SpirvType);
539 Register getOrCreateBaseRegister(Constant *Val, MachineInstr &I,
540 SPIRVTypeInst SpvType,
541 const SPIRVInstrInfo &TII, unsigned BitWidth,
542 bool ZeroAsNull);
543 Register getOrCreateCompositeOrNull(Constant *Val, MachineInstr &I,
544 SPIRVTypeInst SpvType,
545 const SPIRVInstrInfo &TII, Constant *CA,
546 unsigned BitWidth, unsigned ElemCnt,
547 bool ZeroAsNull = true);
548
549 Register getOrCreateIntCompositeOrNull(uint64_t Val,
550 MachineIRBuilder &MIRBuilder,
551 SPIRVTypeInst SpvType, bool EmitIR,
552 Constant *CA, unsigned BitWidth,
553 unsigned ElemCnt);
554
555 // Returns a pointer to a SPIR-V pointer type with the given base type and
556 // storage class. It is the responsibility of the caller to make sure the
557 // decorations on the base type are valid for the given storage class. For
558 // example, it has the correct offset and stride decorations.
559 // ForceTyped keeps an OpTypePointer even when untyped pointers are available,
560 // for cases where the pointee type must be preserved (e.g. a byval/byref/sret
561 // aggregate argument).
562 SPIRVTypeInst getOrCreateSPIRVPointerTypeInternal(
564 SPIRV::StorageClass::StorageClass SC, bool ForceTyped = false);
565
566 void addStructOffsetDecorations(Register Reg, StructType *Ty,
567 MachineIRBuilder &MIRBuilder);
568 void addArrayStrideDecorations(Register Reg, Type *ElementType,
569 MachineIRBuilder &MIRBuilder);
570
572
574 getOrCreateOpTypeImage(MachineIRBuilder &MIRBuilder,
575 SPIRVTypeInst SampledType, SPIRV::Dim::Dim Dim,
576 uint32_t Depth, uint32_t Arrayed,
577 uint32_t Multisampled, uint32_t Sampled,
578 SPIRV::ImageFormat::ImageFormat ImageFormat,
579 SPIRV::AccessQualifier::AccessQualifier AccQual);
580
581public:
583 SPIRVTypeInst SpvType, bool EmitIR,
584 bool ZeroAsNull = true);
586 SPIRVTypeInst SpvType, const SPIRVInstrInfo &TII,
587 bool ZeroAsNull = true);
589 SPIRVTypeInst SpvType, const SPIRVInstrInfo &TII,
590 bool ZeroAsNull = true);
592 SPIRVTypeInst SpvType, const SPIRVInstrInfo &TII,
593 bool ZeroAsNull);
595 SPIRVTypeInst SpvType, const SPIRVInstrInfo &TII,
596 bool ZeroAsNull = true);
598 SPIRVTypeInst SpvType, const SPIRVInstrInfo &TII,
599 bool ZeroAsNull);
601 SPIRVTypeInst SpvType = nullptr);
602
604 SPIRVTypeInst SpvType,
605 const SPIRVInstrInfo &TII,
606 bool ZeroAsNull = true);
608 SPIRVTypeInst SpvType,
609 const SPIRVInstrInfo &TII,
610 bool ZeroAsNull = true);
612 SPIRVTypeInst SpvType,
613 const SPIRVInstrInfo &TII,
614 bool ZeroAsNull = true);
616 SPIRVTypeInst SpvType,
617 const SPIRVInstrInfo &TII);
619 SPIRVTypeInst SpvType, bool EmitIR);
621 SPIRVTypeInst SpvType);
622 Register buildConstantSampler(Register Res, unsigned AddrMode, unsigned Param,
623 unsigned FilerMode,
624 MachineIRBuilder &MIRBuilder);
626 const SPIRVInstrInfo &TII);
629 const GlobalValue *GV, SPIRV::StorageClass::StorageClass Storage,
630 const MachineInstr *Init, bool IsConst,
631 const std::optional<SPIRV::LinkageType::LinkageType> &LinkageType,
632 MachineIRBuilder &MIRBuilder, bool IsInstSelector);
635 StringRef Name,
636 MachineIRBuilder &MIRBuilder);
637
638 // Convenient helpers for getting types with check for duplicates.
640 MachineIRBuilder &MIRBuilder);
642 const SPIRVInstrInfo &TII);
644 const SPIRVInstrInfo &TII,
645 unsigned SPIRVOPcode, Type *LLVMTy);
647 const SPIRVInstrInfo &TII);
649 bool EmitIR);
651 const SPIRVInstrInfo &TII);
653 unsigned NumElements,
654 MachineIRBuilder &MIRBuilder,
655 bool EmitIR);
657 unsigned NumElements,
659 const SPIRVInstrInfo &TII);
661
662 // Returns a pointer to a SPIR-V pointer type with the given base type and
663 // storage class. The base type will be translated to a SPIR-V type, and the
664 // appropriate layout decorations will be added to the base type.
665 // See getOrCreateSPIRVPointerTypeInternal for ForceTyped.
668 MachineIRBuilder &MIRBuilder,
669 SPIRV::StorageClass::StorageClass SC,
670 bool ForceTyped = false, bool EmitIR = true);
673 SPIRV::StorageClass::StorageClass SC,
674 bool ForceTyped = false);
675
676 // Like getOrCreateSPIRVPointerType, but always returns an OpTypePointer even
677 // when untyped pointers are available. Use this when the pointee type must be
678 // preserved (e.g. a byval/byref/sret aggregate argument).
681 MachineIRBuilder &MIRBuilder,
682 SPIRV::StorageClass::StorageClass SC) {
683 return getOrCreateSPIRVPointerType(BaseType, MIRBuilder, SC,
684 /*ForceTyped=*/true);
685 }
686
687 // Returns a pointer to a SPIR-V pointer type with the given base type and
688 // storage class. It is the responsibility of the caller to make sure the
689 // decorations on the base type are valid for the given storage class. For
690 // example, it has the correct offset and stride decorations.
693 SPIRV::StorageClass::StorageClass SC, bool EmitIR = true);
694
695 // Returns a pointer to a SPIR-V pointer type that is the same as `PtrType`
696 // except the stroage class has been changed to `SC`. It is the responsibility
697 // of the caller to be sure that the original and new storage class have the
698 // same layout requirements.
700 SPIRV::StorageClass::StorageClass SC,
701 MachineInstr &I);
702
703 // Returns OpTypeUntypedPointerKHR for the given storage class.
705 getOrCreateSPIRVUntypedPointerType(SPIRV::StorageClass::StorageClass SC,
706 MachineIRBuilder &MIRBuilder);
707
710 SPIRV::StorageClass::StorageClass SC,
711 bool IsWritable, bool EmitIr = false);
712
714 uint64_t PaddingSizeInBytes,
715 uint64_t OffsetInBytes = 0);
716
718 Type *ElemType);
719
721 const TargetExtType *T,
722 bool EmitIr = false);
723
725 getImageType(const TargetExtType *ExtensionType,
726 const SPIRV::AccessQualifier::AccessQualifier Qualifier,
727 MachineIRBuilder &MIRBuilder);
728
730
732 MachineIRBuilder &MIRBuilder);
734 const TargetExtType *ExtensionType,
735 SPIRVTypeInst ElemType,
736 uint32_t Scope, uint32_t Rows,
737 uint32_t Columns, uint32_t Use,
738 bool EmitIR);
741 SPIRV::AccessQualifier::AccessQualifier AccQual);
744 const Type *Ty, SPIRVTypeInst RetType,
745 const SmallVectorImpl<SPIRVTypeInst> &ArgTypes,
746 MachineIRBuilder &MIRBuilder);
748 MachineIRBuilder &MIRBuilder,
749 unsigned Opcode);
750
752 MachineIRBuilder &MIRBuilder,
753 unsigned Opcode,
755
756 const TargetRegisterClass *getRegClass(SPIRVTypeInst SpvType) const;
757 LLT getRegType(SPIRVTypeInst SpvType) const;
758
760 const MDNode *AliasingListMD);
762 uint32_t Dec, const MDNode *GVarMD);
763 // Replace all uses of a |Old| with |New| updates the global registry type
764 // mappings.
765 void replaceAllUsesWith(Value *Old, Value *New, bool DeleteOld = true);
766
767 void buildAssignType(IRBuilder<> &B, Type *Ty, Value *Arg,
768 bool CanUseAnyVectorRank);
770 void updateAssignType(CallInst *AssignCI, Value *Arg, Value *OfType);
771};
772} // end namespace llvm
773#endif // LLVM_LIB_TARGET_SPIRV_SPIRVGLOBALREGISTRY_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const AArch64InstrInfo & TII
uint64_t uint64_t
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
DXIL Resource Implicit Binding
This file defines the DenseSet and SmallDenseSet classes.
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This file declares the MachineIRBuilder class.
Register Reg
#define T
SI Fold Operands
BaseType
A given derived pointer can have multiple base pointers through phi/selects.
Class for arbitrary precision integers.
Definition APInt.h:78
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
This class represents a function call, abstracting a target machine's calling convention.
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:420
This is the shared class of boolean and integer constants.
Definition Constants.h:87
This is an important base class in LLVM.
Definition Constant.h:44
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Implements a dense probed hash-table based set.
Definition DenseSet.h:283
Class to represent function types.
iterator end()
Definition Function.h:849
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2990
Metadata node.
Definition Metadata.h:1087
Helper class to build MachineInstr.
Representation of each machine instruction.
const MachineBasicBlock * getParent() const
MachineOperand class - Representation of each machine instruction operand.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
void buildAssignType(IRBuilder<> &B, Type *Ty, Value *Arg, bool CanUseAnyVectorRank)
void recordFunctionDefinition(const Function *F, const MachineOperand *MO)
SPIRVTypeInst getImageType(const TargetExtType *ExtensionType, const SPIRV::AccessQualifier::AccessQualifier Qualifier, MachineIRBuilder &MIRBuilder)
const TypedPointerType * findReturnType(const Function *ArgF)
bool isScalarOrVectorSigned(SPIRVTypeInst Type) const
void addForwardCall(const Function *F, MachineInstr *MI)
void addAssignPtrTypeInstr(Value *Val, CallInst *AssignPtrTyCI)
const Value * getGlobalObject(const MachineFunction *MF, Register R)
SPIRVTypeInst getOrCreateOpTypeSampledImage(SPIRVTypeInst ImageType, MachineIRBuilder &MIRBuilder)
unsigned getNumScalarOrVectorTotalBitWidth(SPIRVTypeInst Type) const
SPIRVTypeInst getOrCreateUnknownType(const Type *Ty, MachineIRBuilder &MIRBuilder, unsigned Opcode, const ArrayRef< MachineOperand > Operands)
SPIRVTypeInst getOrCreateSPIRVType(const Type *Type, MachineIRBuilder &MIRBuilder, SPIRV::AccessQualifier::AccessQualifier AQ, bool EmitIR)
void assignSPIRVTypeToVReg(SPIRVTypeInst Type, Register VReg, const MachineFunction &MF)
SPIRVTypeInst getOrCreateOpTypeFunctionWithArgs(const Type *Ty, SPIRVTypeInst RetType, const SmallVectorImpl< SPIRVTypeInst > &ArgTypes, MachineIRBuilder &MIRBuilder)
void buildAssignPtr(IRBuilder<> &B, Type *ElemTy, Value *Arg)
const TargetRegisterClass * getRegClass(SPIRVTypeInst SpvType) const
SPIRVTypeInst getOrCreateSPIRVPointerType(const Type *BaseType, MachineIRBuilder &MIRBuilder, SPIRV::StorageClass::StorageClass SC, bool ForceTyped=false, bool EmitIR=true)
MachineInstr * getOrAddMemAliasingINTELInst(MachineIRBuilder &MIRBuilder, const MDNode *AliasingListMD)
bool isAggregateType(SPIRVTypeInst Type) const
SPIRVTypeInst getOrCreateSPIRVTypedPointerType(const Type *BaseType, MachineIRBuilder &MIRBuilder, SPIRV::StorageClass::StorageClass SC)
unsigned getScalarOrVectorBitWidth(SPIRVTypeInst Type) const
void setUntypedPtrElementType(Register Reg, SPIRVTypeInst ElemType)
SPIRVTypeInst getOrCreateSPIRVIntegerType(unsigned BitWidth, MachineIRBuilder &MIRBuilder)
SPIRVTypeInst getOrCreateSPIRVVectorType(SPIRVTypeInst BaseType, unsigned NumElements, MachineIRBuilder &MIRBuilder, bool EmitIR)
SPIRVTypeInst getOrCreateSPIRVTypeByName(StringRef TypeStr, MachineIRBuilder &MIRBuilder, bool EmitIR, SPIRV::StorageClass::StorageClass SC=SPIRV::StorageClass::Function, SPIRV::AccessQualifier::AccessQualifier AQ=SPIRV::AccessQualifier::ReadWrite)
Register buildGlobalVariable(Register Reg, SPIRVTypeInst BaseType, StringRef Name, const GlobalValue *GV, SPIRV::StorageClass::StorageClass Storage, const MachineInstr *Init, bool IsConst, const std::optional< SPIRV::LinkageType::LinkageType > &LinkageType, MachineIRBuilder &MIRBuilder, bool IsInstSelector)
SPIRVTypeInst assignIntTypeToVReg(unsigned BitWidth, Register VReg, MachineInstr &I, const SPIRVInstrInfo &TII)
SPIRVTypeInst getResultType(Register VReg, MachineFunction *MF=nullptr)
Type * findDeducedCompositeType(const Value *Val)
void replaceAllUsesWith(Value *Old, Value *New, bool DeleteOld=true)
SPIRVTypeInst getOrCreateOpTypeByOpcode(const Type *Ty, MachineIRBuilder &MIRBuilder, unsigned Opcode)
unsigned getScalarOrVectorComponentCount(Register VReg) const
const Type * getTypeForSPIRVType(SPIRVTypeInst Ty) const
bool isBitcastCompatible(SPIRVTypeInst Type1, SPIRVTypeInst Type2) const
void addDeducedElementType(Value *Val, Type *Ty)
bool shouldKeepTypedPtrType(SPIRVTypeInst ElemType) const
Register getOrCreateConstFP(APFloat Val, MachineInstr &I, SPIRVTypeInst SpvType, const SPIRVInstrInfo &TII, bool ZeroAsNull=true)
const MachineInstr * getFunctionDefinition(const Function *F)
LLT getRegType(SPIRVTypeInst SpvType) const
void addReturnType(const Function *ArgF, TypedPointerType *DerivedTy)
const MachineOperand * getFunctionDefinitionByUse(const MachineOperand *Use)
Register getLastInsertedTypeReg(const MachineFunction &MF) const
SPIRVTypeInst getOpTypeVoid(MachineIRBuilder &MIRBuilder)
void invalidateMachineInstr(MachineInstr *MI)
bool isResourceType(SPIRVTypeInst Type) const
SPIRVTypeInst getOrCreateSPIRVBoolType(MachineIRBuilder &MIRBuilder, bool EmitIR)
void addMutated(Value *Val, Type *Ty)
void setLastInsertedType(const MachineFunction &MF, MachineInstr *MI)
void updateIfExistDeducedElementType(Value *OldVal, Value *NewVal, bool DeleteOld)
bool isScalarOfType(Register VReg, unsigned TypeOpcode) const
Register getSPIRVTypeID(SPIRVTypeInst SpirvType) const
Register getOrCreateConstInt(uint64_t Val, MachineInstr &I, SPIRVTypeInst SpvType, const SPIRVInstrInfo &TII, bool ZeroAsNull=true)
Register getOrCreateConstIntArray(uint64_t Val, size_t Num, MachineInstr &I, SPIRVTypeInst SpvType, const SPIRVInstrInfo &TII)
bool findValueAttrs(const MachineInstr *Key, Type *&Ty, StringRef &Name)
unsigned getDeducedPointeeByteSize(const Value *PtrVal)
SPIRVTypeInst retrieveScalarOrVectorIntType(SPIRVTypeInst Type) const
Register getOrCreateGlobalVariableWithBinding(SPIRVTypeInst VarType, uint32_t Set, uint32_t Binding, StringRef Name, MachineIRBuilder &MIRBuilder)
SPIRVTypeInst getOrCreateOpTypeCoopMatr(MachineIRBuilder &MIRBuilder, const TargetExtType *ExtensionType, SPIRVTypeInst ElemType, uint32_t Scope, uint32_t Rows, uint32_t Columns, uint32_t Use, bool EmitIR)
SPIRVTypeInst changePointerStorageClass(SPIRVTypeInst PtrType, SPIRV::StorageClass::StorageClass SC, MachineInstr &I)
Type * findMutated(const Value *Val)
Register getOrCreateConstVector(uint64_t Val, MachineInstr &I, SPIRVTypeInst SpvType, const SPIRVInstrInfo &TII, bool ZeroAsNull=true)
Register buildConstantFP(APFloat Val, MachineIRBuilder &MIRBuilder, SPIRVTypeInst SpvType=nullptr)
SPIRVTypeInst getOrCreateOpTypePipe(MachineIRBuilder &MIRBuilder, SPIRV::AccessQualifier::AccessQualifier AccQual)
void addGlobalObject(const Value *V, const MachineFunction *MF, Register R)
SPIRVTypeInst getScalarOrVectorComponentType(SPIRVTypeInst Type) const
void addDeducedCompositeType(Value *Val, Type *Ty)
void recordFunctionPointer(const MachineOperand *MO, const Function *F)
SPIRVTypeInst getOrCreateSPIRVFloatType(unsigned BitWidth, MachineInstr &I, const SPIRVInstrInfo &TII)
SPIRVTypeInst getOrCreateVulkanBufferType(MachineIRBuilder &MIRBuilder, Type *ElemType, SPIRV::StorageClass::StorageClass SC, bool IsWritable, bool EmitIr=false)
SPIRVTypeInst getPointeeType(SPIRVTypeInst PtrType)
SmallPtrSet< MachineInstr *, 8 > * getForwardCalls(const Function *F)
SPIRVTypeInst getOrCreateSPIRVType(const Type *Type, MachineInstr &I, SPIRV::AccessQualifier::AccessQualifier AQ, bool EmitIR)
Register getOrCreateConsIntVector(uint64_t Val, MachineIRBuilder &MIRBuilder, SPIRVTypeInst SpvType, bool EmitIR)
void updateIfExistAssignPtrTypeInstr(Value *OldVal, Value *NewVal, bool DeleteOld)
SPIRVTypeInst assignTypeToVReg(const Type *Type, Register VReg, MachineIRBuilder &MIRBuilder, SPIRV::AccessQualifier::AccessQualifier AQ, bool EmitIR)
bool isScalarOrVectorOfType(Register VReg, unsigned TypeOpcode) const
SPIRVTypeInst getOrCreateLayoutType(MachineIRBuilder &MIRBuilder, const TargetExtType *T, bool EmitIr=false)
MachineFunction * setCurrentFunc(MachineFunction &MF)
void restoreFromMIR(const MachineFunction &MF)
SPIRVTypeInst getOrCreatePaddingType(MachineIRBuilder &MIRBuilder, uint64_t PaddingSizeInBytes, uint64_t OffsetInBytes=0)
Register createConstInt(const ConstantInt *CI, MachineInstr &I, SPIRVTypeInst SpvType, const SPIRVInstrInfo &TII, bool ZeroAsNull)
Register getOrCreateConstNullPtr(MachineIRBuilder &MIRBuilder, SPIRVTypeInst SpvType)
SPIRVTypeInst getSPIRVTypeForVReg(Register VReg, const MachineFunction *MF=nullptr) const
SPIRVTypeInst getOrCreateSPIRVUntypedPointerType(SPIRV::StorageClass::StorageClass SC, MachineIRBuilder &MIRBuilder)
Type * getDeducedGlobalValueType(const GlobalValue *Global)
Register getOrCreateUndef(MachineInstr &I, SPIRVTypeInst SpvType, const SPIRVInstrInfo &TII)
SPIRVTypeInst getOrCreateOpTypeSampler(MachineIRBuilder &MIRBuilder)
void addValueAttrs(MachineInstr *Key, std::pair< Type *, std::string > Val)
const Function * getFunctionByDefinition(const MachineInstr *MI)
void buildMemAliasingOpDecorate(Register Reg, MachineIRBuilder &MIRBuilder, uint32_t Dec, const MDNode *GVarMD)
SPIRV::StorageClass::StorageClass getPointerStorageClass(Register VReg) const
void removeAssignPtrTypeInstr(Value *OldVal)
bool shouldUseUntypedPointer(SPIRVTypeInst ElemType, const SPIRVSubtarget &ST) const
Type * findDeducedElementType(const Value *Val)
Register buildConstantSampler(Register Res, unsigned AddrMode, unsigned Param, unsigned FilerMode, MachineIRBuilder &MIRBuilder)
SmallVector< std::pair< Register, Register > > getVRegTypeIDs(const MachineFunction &MF) const
SPIRVTypeInst getUntypedPtrElementType(Register Reg) const
void updateAssignType(CallInst *AssignCI, Value *Arg, Value *OfType)
CallInst * findAssignPtrTypeInstr(const Value *Val)
Register buildConstantInt(uint64_t Val, MachineIRBuilder &MIRBuilder, SPIRVTypeInst SpvType, bool EmitIR, bool ZeroAsNull=true)
SPIRVTypeInst getOrCreateVulkanPushConstantType(MachineIRBuilder &MIRBuilder, Type *ElemType)
Register createConstFP(const ConstantFP *CF, MachineInstr &I, SPIRVTypeInst SpvType, const SPIRVInstrInfo &TII, bool ZeroAsNull)
SPIRVTypeInst getOrCreateOpTypeDeviceEvent(MachineIRBuilder &MIRBuilder)
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
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
Class to represent struct types.
Class to represent target extensions types, which are generally unintrospectable from target-independ...
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
A few GPU targets, such as DXIL and SPIR-V, have typed pointers.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM Value Representation.
Definition Value.h:75
FunctionType * getOriginalFunctionType(const Function &F)
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI void constrainSelectedInstRegOperands(MachineInstr &I, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI, const RegisterBankInfo &RBI)
Mutate the newly-selected instruction I to constrain its (possibly generic) virtual register operands...
Definition Utils.cpp:163
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
std::function< void(Register)> StructOffsetDecorator
@ Global
Append to llvm.global_dtors.
DWARFExpression::Operation Op
constexpr unsigned BitWidth
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58