LLVM 20.0.0git
SPIRVGlobalRegistry.cpp
Go to the documentation of this file.
1//===-- SPIRVGlobalRegistry.cpp - 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// This file contains the implementation of the SPIRVGlobalRegistry class,
10// which is used to maintain rich type information required for SPIR-V even
11// after lowering from LLVM IR to GMIR. It can convert an llvm::Type into
12// an OpTypeXXX instruction, and map it to a virtual register. Also it builds
13// and supports consistency of constants and global variables.
14//
15//===----------------------------------------------------------------------===//
16
17#include "SPIRVGlobalRegistry.h"
18#include "SPIRV.h"
19#include "SPIRVBuiltins.h"
20#include "SPIRVSubtarget.h"
21#include "SPIRVTargetMachine.h"
22#include "SPIRVUtils.h"
23#include "llvm/ADT/APInt.h"
24#include "llvm/IR/Constants.h"
25#include "llvm/IR/Type.h"
27#include <cassert>
28#include <functional>
29
30using namespace llvm;
31
32inline unsigned typeToAddressSpace(const Type *Ty) {
33 if (auto PType = dyn_cast<TypedPointerType>(Ty))
34 return PType->getAddressSpace();
35 if (auto PType = dyn_cast<PointerType>(Ty))
36 return PType->getAddressSpace();
37 if (auto *ExtTy = dyn_cast<TargetExtType>(Ty);
38 ExtTy && isTypedPointerWrapper(ExtTy))
39 return ExtTy->getIntParameter(0);
40 report_fatal_error("Unable to convert LLVM type to SPIRVType", true);
41}
42
44 : PointerSize(PointerSize), Bound(0) {}
45
47 Register VReg,
49 const SPIRVInstrInfo &TII) {
51 assignSPIRVTypeToVReg(SpirvType, VReg, *CurMF);
52 return SpirvType;
53}
54
58 const SPIRVInstrInfo &TII) {
60 assignSPIRVTypeToVReg(SpirvType, VReg, *CurMF);
61 return SpirvType;
62}
63
65 SPIRVType *BaseType, unsigned NumElements, Register VReg, MachineInstr &I,
66 const SPIRVInstrInfo &TII) {
67 SPIRVType *SpirvType =
69 assignSPIRVTypeToVReg(SpirvType, VReg, *CurMF);
70 return SpirvType;
71}
72
74 const Type *Type, Register VReg, MachineIRBuilder &MIRBuilder,
75 SPIRV::AccessQualifier::AccessQualifier AccessQual, bool EmitIR) {
76 SPIRVType *SpirvType =
77 getOrCreateSPIRVType(Type, MIRBuilder, AccessQual, EmitIR);
78 assignSPIRVTypeToVReg(SpirvType, VReg, MIRBuilder.getMF());
79 return SpirvType;
80}
81
83 Register VReg,
84 const MachineFunction &MF) {
85 VRegToTypeMap[&MF][VReg] = SpirvType;
86}
87
89 auto Res = MRI.createGenericVirtualRegister(LLT::scalar(64));
90 MRI.setRegClass(Res, &SPIRV::TYPERegClass);
91 return Res;
92}
93
95 return createTypeVReg(MIRBuilder.getMF().getRegInfo());
96}
97
98SPIRVType *SPIRVGlobalRegistry::getOpTypeBool(MachineIRBuilder &MIRBuilder) {
99 return createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
100 return MIRBuilder.buildInstr(SPIRV::OpTypeBool)
101 .addDef(createTypeVReg(MIRBuilder));
102 });
103}
104
105unsigned SPIRVGlobalRegistry::adjustOpTypeIntWidth(unsigned Width) const {
106 if (Width > 64)
107 report_fatal_error("Unsupported integer width!");
108 const SPIRVSubtarget &ST = cast<SPIRVSubtarget>(CurMF->getSubtarget());
109 if (ST.canUseExtension(
110 SPIRV::Extension::SPV_INTEL_arbitrary_precision_integers))
111 return Width;
112 if (Width <= 8)
113 Width = 8;
114 else if (Width <= 16)
115 Width = 16;
116 else if (Width <= 32)
117 Width = 32;
118 else
119 Width = 64;
120 return Width;
121}
122
123SPIRVType *SPIRVGlobalRegistry::getOpTypeInt(unsigned Width,
124 MachineIRBuilder &MIRBuilder,
125 bool IsSigned) {
126 Width = adjustOpTypeIntWidth(Width);
127 const SPIRVSubtarget &ST =
128 cast<SPIRVSubtarget>(MIRBuilder.getMF().getSubtarget());
129 return createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
130 if (ST.canUseExtension(
131 SPIRV::Extension::SPV_INTEL_arbitrary_precision_integers)) {
132 MIRBuilder.buildInstr(SPIRV::OpExtension)
133 .addImm(SPIRV::Extension::SPV_INTEL_arbitrary_precision_integers);
134 MIRBuilder.buildInstr(SPIRV::OpCapability)
135 .addImm(SPIRV::Capability::ArbitraryPrecisionIntegersINTEL);
136 }
137 return MIRBuilder.buildInstr(SPIRV::OpTypeInt)
138 .addDef(createTypeVReg(MIRBuilder))
139 .addImm(Width)
140 .addImm(IsSigned ? 1 : 0);
141 });
142}
143
144SPIRVType *SPIRVGlobalRegistry::getOpTypeFloat(uint32_t Width,
145 MachineIRBuilder &MIRBuilder) {
146 return createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
147 return MIRBuilder.buildInstr(SPIRV::OpTypeFloat)
148 .addDef(createTypeVReg(MIRBuilder))
149 .addImm(Width);
150 });
151}
152
153SPIRVType *SPIRVGlobalRegistry::getOpTypeVoid(MachineIRBuilder &MIRBuilder) {
154 return createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
155 return MIRBuilder.buildInstr(SPIRV::OpTypeVoid)
156 .addDef(createTypeVReg(MIRBuilder));
157 });
158}
159
161 // TODO:
162 // - take into account duplicate tracker case which is a known issue,
163 // - review other data structure wrt. possible issues related to removal
164 // of a machine instruction during instruction selection.
165 const MachineFunction *MF = MI->getParent()->getParent();
166 auto It = LastInsertedTypeMap.find(MF);
167 if (It == LastInsertedTypeMap.end())
168 return;
169 if (It->second == MI)
170 LastInsertedTypeMap.erase(MF);
171}
172
173SPIRVType *SPIRVGlobalRegistry::createOpType(
174 MachineIRBuilder &MIRBuilder,
175 std::function<MachineInstr *(MachineIRBuilder &)> Op) {
176 auto oldInsertPoint = MIRBuilder.getInsertPt();
177 MachineBasicBlock *OldMBB = &MIRBuilder.getMBB();
178 MachineBasicBlock *NewMBB = &*MIRBuilder.getMF().begin();
179
180 auto LastInsertedType = LastInsertedTypeMap.find(CurMF);
181 if (LastInsertedType != LastInsertedTypeMap.end()) {
182 auto It = LastInsertedType->second->getIterator();
183 // It might happen that this instruction was removed from the first MBB,
184 // hence the Parent's check.
186 if (It->getParent() != NewMBB)
187 InsertAt = oldInsertPoint->getParent() == NewMBB
188 ? oldInsertPoint
189 : getInsertPtValidEnd(NewMBB);
190 else if (It->getNextNode())
191 InsertAt = It->getNextNode()->getIterator();
192 else
193 InsertAt = getInsertPtValidEnd(NewMBB);
194 MIRBuilder.setInsertPt(*NewMBB, InsertAt);
195 } else {
196 MIRBuilder.setInsertPt(*NewMBB, NewMBB->begin());
197 auto Result = LastInsertedTypeMap.try_emplace(CurMF, nullptr);
198 assert(Result.second);
199 LastInsertedType = Result.first;
200 }
201
202 MachineInstr *Type = Op(MIRBuilder);
203 // We expect all users of this function to insert definitions at the insertion
204 // point set above that is always the first MBB.
205 assert(Type->getParent() == NewMBB);
206 LastInsertedType->second = Type;
207
208 MIRBuilder.setInsertPt(*OldMBB, oldInsertPoint);
209 return Type;
210}
211
212SPIRVType *SPIRVGlobalRegistry::getOpTypeVector(uint32_t NumElems,
213 SPIRVType *ElemType,
214 MachineIRBuilder &MIRBuilder) {
215 auto EleOpc = ElemType->getOpcode();
216 (void)EleOpc;
217 assert((EleOpc == SPIRV::OpTypeInt || EleOpc == SPIRV::OpTypeFloat ||
218 EleOpc == SPIRV::OpTypeBool) &&
219 "Invalid vector element type");
220
221 return createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
222 return MIRBuilder.buildInstr(SPIRV::OpTypeVector)
223 .addDef(createTypeVReg(MIRBuilder))
224 .addUse(getSPIRVTypeID(ElemType))
225 .addImm(NumElems);
226 });
227}
228
229std::tuple<Register, ConstantInt *, bool, unsigned>
230SPIRVGlobalRegistry::getOrCreateConstIntReg(uint64_t Val, SPIRVType *SpvType,
231 MachineIRBuilder *MIRBuilder,
233 const SPIRVInstrInfo *TII) {
234 assert(SpvType);
235 const IntegerType *LLVMIntTy =
236 cast<IntegerType>(getTypeForSPIRVType(SpvType));
237 unsigned BitWidth = getScalarOrVectorBitWidth(SpvType);
238 bool NewInstr = false;
239 // Find a constant in DT or build a new one.
240 ConstantInt *CI = ConstantInt::get(const_cast<IntegerType *>(LLVMIntTy), Val);
241 Register Res = DT.find(CI, CurMF);
242 if (!Res.isValid()) {
243 Res =
245 CurMF->getRegInfo().setRegClass(Res, &SPIRV::iIDRegClass);
246 if (MIRBuilder)
247 assignTypeToVReg(LLVMIntTy, Res, *MIRBuilder);
248 else
250 DT.add(CI, CurMF, Res);
251 NewInstr = true;
252 }
253 return std::make_tuple(Res, CI, NewInstr, BitWidth);
254}
255
256std::tuple<Register, ConstantFP *, bool, unsigned>
257SPIRVGlobalRegistry::getOrCreateConstFloatReg(APFloat Val, SPIRVType *SpvType,
258 MachineIRBuilder *MIRBuilder,
260 const SPIRVInstrInfo *TII) {
261 assert(SpvType);
263 const Type *LLVMFloatTy = getTypeForSPIRVType(SpvType);
264 unsigned BitWidth = getScalarOrVectorBitWidth(SpvType);
265 bool NewInstr = false;
266 // Find a constant in DT or build a new one.
267 auto *const CI = ConstantFP::get(Ctx, Val);
268 Register Res = DT.find(CI, CurMF);
269 if (!Res.isValid()) {
270 Res =
272 CurMF->getRegInfo().setRegClass(Res, &SPIRV::fIDRegClass);
273 if (MIRBuilder)
274 assignTypeToVReg(LLVMFloatTy, Res, *MIRBuilder);
275 else
277 DT.add(CI, CurMF, Res);
278 NewInstr = true;
279 }
280 return std::make_tuple(Res, CI, NewInstr, BitWidth);
281}
282
284 SPIRVType *SpvType,
285 const SPIRVInstrInfo &TII,
286 bool ZeroAsNull) {
287 assert(SpvType);
288 ConstantFP *CI;
289 Register Res;
290 bool New;
291 unsigned BitWidth;
292 std::tie(Res, CI, New, BitWidth) =
293 getOrCreateConstFloatReg(Val, SpvType, nullptr, &I, &TII);
294 // If we have found Res register which is defined by the passed G_CONSTANT
295 // machine instruction, a new constant instruction should be created.
296 if (!New && (!I.getOperand(0).isReg() || Res != I.getOperand(0).getReg()))
297 return Res;
298 MachineIRBuilder MIRBuilder(I);
299 createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
301 // In OpenCL OpConstantNull - Scalar floating point: +0.0 (all bits 0)
302 if (Val.isPosZero() && ZeroAsNull) {
303 MIB = MIRBuilder.buildInstr(SPIRV::OpConstantNull)
304 .addDef(Res)
305 .addUse(getSPIRVTypeID(SpvType));
306 } else {
307 MIB = MIRBuilder.buildInstr(SPIRV::OpConstantF)
308 .addDef(Res)
309 .addUse(getSPIRVTypeID(SpvType));
310 addNumImm(
311 APInt(BitWidth, CI->getValueAPF().bitcastToAPInt().getZExtValue()),
312 MIB);
313 }
314 const auto &ST = CurMF->getSubtarget();
316 *MIB, *ST.getInstrInfo(), *ST.getRegisterInfo(), *ST.getRegBankInfo());
317 return MIB;
318 });
319 return Res;
320}
321
323 SPIRVType *SpvType,
324 const SPIRVInstrInfo &TII,
325 bool ZeroAsNull) {
326 assert(SpvType);
327 ConstantInt *CI;
328 Register Res;
329 bool New;
330 unsigned BitWidth;
331 std::tie(Res, CI, New, BitWidth) =
332 getOrCreateConstIntReg(Val, SpvType, nullptr, &I, &TII);
333 // If we have found Res register which is defined by the passed G_CONSTANT
334 // machine instruction, a new constant instruction should be created.
335 if (!New && (!I.getOperand(0).isReg() || Res != I.getOperand(0).getReg()))
336 return Res;
337
338 MachineIRBuilder MIRBuilder(I);
339 createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
341 if (Val || !ZeroAsNull) {
342 MIB = MIRBuilder.buildInstr(SPIRV::OpConstantI)
343 .addDef(Res)
344 .addUse(getSPIRVTypeID(SpvType));
345 addNumImm(APInt(BitWidth, Val), MIB);
346 } else {
347 MIB = MIRBuilder.buildInstr(SPIRV::OpConstantNull)
348 .addDef(Res)
349 .addUse(getSPIRVTypeID(SpvType));
350 }
351 const auto &ST = CurMF->getSubtarget();
353 *MIB, *ST.getInstrInfo(), *ST.getRegisterInfo(), *ST.getRegBankInfo());
354 return MIB;
355 });
356 return Res;
357}
358
360 MachineIRBuilder &MIRBuilder,
361 SPIRVType *SpvType, bool EmitIR,
362 bool ZeroAsNull) {
363 assert(SpvType);
364 auto &MF = MIRBuilder.getMF();
365 const IntegerType *LLVMIntTy =
366 cast<IntegerType>(getTypeForSPIRVType(SpvType));
367 // Find a constant in DT or build a new one.
368 const auto ConstInt =
369 ConstantInt::get(const_cast<IntegerType *>(LLVMIntTy), Val);
370 Register Res = DT.find(ConstInt, &MF);
371 if (!Res.isValid()) {
372 unsigned BitWidth = getScalarOrVectorBitWidth(SpvType);
373 LLT LLTy = LLT::scalar(BitWidth);
374 Res = MF.getRegInfo().createGenericVirtualRegister(LLTy);
375 MF.getRegInfo().setRegClass(Res, &SPIRV::iIDRegClass);
376 assignTypeToVReg(LLVMIntTy, Res, MIRBuilder,
377 SPIRV::AccessQualifier::ReadWrite, EmitIR);
378 DT.add(ConstInt, &MIRBuilder.getMF(), Res);
379 if (EmitIR) {
380 MIRBuilder.buildConstant(Res, *ConstInt);
381 } else {
382 Register SpvTypeReg = getSPIRVTypeID(SpvType);
383 createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
385 if (Val || !ZeroAsNull) {
386 MIB = MIRBuilder.buildInstr(SPIRV::OpConstantI)
387 .addDef(Res)
388 .addUse(SpvTypeReg);
389 addNumImm(APInt(BitWidth, Val), MIB);
390 } else {
391 MIB = MIRBuilder.buildInstr(SPIRV::OpConstantNull)
392 .addDef(Res)
393 .addUse(SpvTypeReg);
394 }
395 const auto &Subtarget = CurMF->getSubtarget();
396 constrainSelectedInstRegOperands(*MIB, *Subtarget.getInstrInfo(),
397 *Subtarget.getRegisterInfo(),
398 *Subtarget.getRegBankInfo());
399 return MIB;
400 });
401 }
402 }
403 return Res;
404}
405
407 MachineIRBuilder &MIRBuilder,
408 SPIRVType *SpvType) {
409 auto &MF = MIRBuilder.getMF();
410 auto &Ctx = MF.getFunction().getContext();
411 if (!SpvType) {
412 const Type *LLVMFPTy = Type::getFloatTy(Ctx);
413 SpvType = getOrCreateSPIRVType(LLVMFPTy, MIRBuilder);
414 }
415 // Find a constant in DT or build a new one.
416 const auto ConstFP = ConstantFP::get(Ctx, Val);
417 Register Res = DT.find(ConstFP, &MF);
418 if (!Res.isValid()) {
419 Res = MF.getRegInfo().createGenericVirtualRegister(
421 MF.getRegInfo().setRegClass(Res, &SPIRV::fIDRegClass);
422 assignSPIRVTypeToVReg(SpvType, Res, MF);
423 DT.add(ConstFP, &MF, Res);
424 createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
426 MIB = MIRBuilder.buildInstr(SPIRV::OpConstantF)
427 .addDef(Res)
428 .addUse(getSPIRVTypeID(SpvType));
429 addNumImm(ConstFP->getValueAPF().bitcastToAPInt(), MIB);
430 return MIB;
431 });
432 }
433
434 return Res;
435}
436
437Register SPIRVGlobalRegistry::getOrCreateBaseRegister(
438 Constant *Val, MachineInstr &I, SPIRVType *SpvType,
439 const SPIRVInstrInfo &TII, unsigned BitWidth, bool ZeroAsNull) {
440 SPIRVType *Type = SpvType;
441 if (SpvType->getOpcode() == SPIRV::OpTypeVector ||
442 SpvType->getOpcode() == SPIRV::OpTypeArray) {
443 auto EleTypeReg = SpvType->getOperand(1).getReg();
444 Type = getSPIRVTypeForVReg(EleTypeReg);
445 }
446 if (Type->getOpcode() == SPIRV::OpTypeFloat) {
448 return getOrCreateConstFP(dyn_cast<ConstantFP>(Val)->getValue(), I,
449 SpvBaseType, TII, ZeroAsNull);
450 }
451 assert(Type->getOpcode() == SPIRV::OpTypeInt);
454 SpvBaseType, TII, ZeroAsNull);
455}
456
457Register SPIRVGlobalRegistry::getOrCreateCompositeOrNull(
458 Constant *Val, MachineInstr &I, SPIRVType *SpvType,
459 const SPIRVInstrInfo &TII, Constant *CA, unsigned BitWidth,
460 unsigned ElemCnt, bool ZeroAsNull) {
461 // Find a constant vector or array in DT or build a new one.
462 Register Res = DT.find(CA, CurMF);
463 // If no values are attached, the composite is null constant.
464 bool IsNull = Val->isNullValue() && ZeroAsNull;
465 if (!Res.isValid()) {
466 // SpvScalConst should be created before SpvVecConst to avoid undefined ID
467 // error on validation.
468 // TODO: can moved below once sorting of types/consts/defs is implemented.
469 Register SpvScalConst;
470 if (!IsNull)
471 SpvScalConst =
472 getOrCreateBaseRegister(Val, I, SpvType, TII, BitWidth, ZeroAsNull);
473
474 LLT LLTy = LLT::scalar(64);
475 Register SpvVecConst =
477 CurMF->getRegInfo().setRegClass(SpvVecConst, getRegClass(SpvType));
478 assignSPIRVTypeToVReg(SpvType, SpvVecConst, *CurMF);
479 DT.add(CA, CurMF, SpvVecConst);
480 MachineIRBuilder MIRBuilder(I);
481 createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
483 if (!IsNull) {
484 MIB = MIRBuilder.buildInstr(SPIRV::OpConstantComposite)
485 .addDef(SpvVecConst)
486 .addUse(getSPIRVTypeID(SpvType));
487 for (unsigned i = 0; i < ElemCnt; ++i)
488 MIB.addUse(SpvScalConst);
489 } else {
490 MIB = MIRBuilder.buildInstr(SPIRV::OpConstantNull)
491 .addDef(SpvVecConst)
492 .addUse(getSPIRVTypeID(SpvType));
493 }
494 const auto &Subtarget = CurMF->getSubtarget();
495 constrainSelectedInstRegOperands(*MIB, *Subtarget.getInstrInfo(),
496 *Subtarget.getRegisterInfo(),
497 *Subtarget.getRegBankInfo());
498 return MIB;
499 });
500 return SpvVecConst;
501 }
502 return Res;
503}
504
507 SPIRVType *SpvType,
508 const SPIRVInstrInfo &TII,
509 bool ZeroAsNull) {
510 const Type *LLVMTy = getTypeForSPIRVType(SpvType);
511 assert(LLVMTy->isVectorTy());
512 const FixedVectorType *LLVMVecTy = cast<FixedVectorType>(LLVMTy);
513 Type *LLVMBaseTy = LLVMVecTy->getElementType();
514 assert(LLVMBaseTy->isIntegerTy());
515 auto *ConstVal = ConstantInt::get(LLVMBaseTy, Val);
516 auto *ConstVec =
517 ConstantVector::getSplat(LLVMVecTy->getElementCount(), ConstVal);
518 unsigned BW = getScalarOrVectorBitWidth(SpvType);
519 return getOrCreateCompositeOrNull(ConstVal, I, SpvType, TII, ConstVec, BW,
520 SpvType->getOperand(2).getImm(),
521 ZeroAsNull);
522}
523
526 SPIRVType *SpvType,
527 const SPIRVInstrInfo &TII,
528 bool ZeroAsNull) {
529 const Type *LLVMTy = getTypeForSPIRVType(SpvType);
530 assert(LLVMTy->isVectorTy());
531 const FixedVectorType *LLVMVecTy = cast<FixedVectorType>(LLVMTy);
532 Type *LLVMBaseTy = LLVMVecTy->getElementType();
533 assert(LLVMBaseTy->isFloatingPointTy());
534 auto *ConstVal = ConstantFP::get(LLVMBaseTy, Val);
535 auto *ConstVec =
536 ConstantVector::getSplat(LLVMVecTy->getElementCount(), ConstVal);
537 unsigned BW = getScalarOrVectorBitWidth(SpvType);
538 return getOrCreateCompositeOrNull(ConstVal, I, SpvType, TII, ConstVec, BW,
539 SpvType->getOperand(2).getImm(),
540 ZeroAsNull);
541}
542
544 uint64_t Val, size_t Num, MachineInstr &I, SPIRVType *SpvType,
545 const SPIRVInstrInfo &TII) {
546 const Type *LLVMTy = getTypeForSPIRVType(SpvType);
547 assert(LLVMTy->isArrayTy());
548 const ArrayType *LLVMArrTy = cast<ArrayType>(LLVMTy);
549 Type *LLVMBaseTy = LLVMArrTy->getElementType();
550 Constant *CI = ConstantInt::get(LLVMBaseTy, Val);
551 SPIRVType *SpvBaseTy = getSPIRVTypeForVReg(SpvType->getOperand(1).getReg());
552 unsigned BW = getScalarOrVectorBitWidth(SpvBaseTy);
553 // The following is reasonably unique key that is better that [Val]. The naive
554 // alternative would be something along the lines of:
555 // SmallVector<Constant *> NumCI(Num, CI);
556 // Constant *UniqueKey =
557 // ConstantArray::get(const_cast<ArrayType*>(LLVMArrTy), NumCI);
558 // that would be a truly unique but dangerous key, because it could lead to
559 // the creation of constants of arbitrary length (that is, the parameter of
560 // memset) which were missing in the original module.
562 {PoisonValue::get(const_cast<ArrayType *>(LLVMArrTy)),
563 ConstantInt::get(LLVMBaseTy, Val), ConstantInt::get(LLVMBaseTy, Num)});
564 return getOrCreateCompositeOrNull(CI, I, SpvType, TII, UniqueKey, BW,
565 LLVMArrTy->getNumElements());
566}
567
568Register SPIRVGlobalRegistry::getOrCreateIntCompositeOrNull(
569 uint64_t Val, MachineIRBuilder &MIRBuilder, SPIRVType *SpvType, bool EmitIR,
570 Constant *CA, unsigned BitWidth, unsigned ElemCnt) {
571 Register Res = DT.find(CA, CurMF);
572 if (!Res.isValid()) {
573 Register SpvScalConst;
574 if (Val || EmitIR) {
575 SPIRVType *SpvBaseType =
577 SpvScalConst = buildConstantInt(Val, MIRBuilder, SpvBaseType, EmitIR);
578 }
579 LLT LLTy = EmitIR ? LLT::fixed_vector(ElemCnt, BitWidth) : LLT::scalar(64);
580 Register SpvVecConst =
582 CurMF->getRegInfo().setRegClass(SpvVecConst, &SPIRV::iIDRegClass);
583 assignSPIRVTypeToVReg(SpvType, SpvVecConst, *CurMF);
584 DT.add(CA, CurMF, SpvVecConst);
585 if (EmitIR) {
586 MIRBuilder.buildSplatBuildVector(SpvVecConst, SpvScalConst);
587 } else {
588 createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
589 if (Val) {
590 auto MIB = MIRBuilder.buildInstr(SPIRV::OpConstantComposite)
591 .addDef(SpvVecConst)
592 .addUse(getSPIRVTypeID(SpvType));
593 for (unsigned i = 0; i < ElemCnt; ++i)
594 MIB.addUse(SpvScalConst);
595 return MIB;
596 } else {
597 return MIRBuilder.buildInstr(SPIRV::OpConstantNull)
598 .addDef(SpvVecConst)
599 .addUse(getSPIRVTypeID(SpvType));
600 }
601 });
602 }
603 return SpvVecConst;
604 }
605 return Res;
606}
607
610 MachineIRBuilder &MIRBuilder,
611 SPIRVType *SpvType, bool EmitIR) {
612 const Type *LLVMTy = getTypeForSPIRVType(SpvType);
613 assert(LLVMTy->isVectorTy());
614 const FixedVectorType *LLVMVecTy = cast<FixedVectorType>(LLVMTy);
615 Type *LLVMBaseTy = LLVMVecTy->getElementType();
616 const auto ConstInt = ConstantInt::get(LLVMBaseTy, Val);
617 auto ConstVec =
618 ConstantVector::getSplat(LLVMVecTy->getElementCount(), ConstInt);
619 unsigned BW = getScalarOrVectorBitWidth(SpvType);
620 return getOrCreateIntCompositeOrNull(Val, MIRBuilder, SpvType, EmitIR,
621 ConstVec, BW,
622 SpvType->getOperand(2).getImm());
623}
624
627 SPIRVType *SpvType) {
628 const Type *LLVMTy = getTypeForSPIRVType(SpvType);
629 unsigned AddressSpace = typeToAddressSpace(LLVMTy);
630 // Find a constant in DT or build a new one.
633 Register Res = DT.find(CP, CurMF);
634 if (!Res.isValid()) {
635 LLT LLTy = LLT::pointer(AddressSpace, PointerSize);
637 CurMF->getRegInfo().setRegClass(Res, &SPIRV::pIDRegClass);
638 assignSPIRVTypeToVReg(SpvType, Res, *CurMF);
639 createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
640 return MIRBuilder.buildInstr(SPIRV::OpConstantNull)
641 .addDef(Res)
642 .addUse(getSPIRVTypeID(SpvType));
643 });
644 DT.add(CP, CurMF, Res);
645 }
646 return Res;
647}
648
650 Register ResReg, unsigned AddrMode, unsigned Param, unsigned FilerMode,
651 MachineIRBuilder &MIRBuilder, SPIRVType *SpvType) {
652 SPIRVType *SampTy;
653 if (SpvType)
654 SampTy = getOrCreateSPIRVType(getTypeForSPIRVType(SpvType), MIRBuilder);
655 else if ((SampTy = getOrCreateSPIRVTypeByName("opencl.sampler_t",
656 MIRBuilder)) == nullptr)
657 report_fatal_error("Unable to recognize SPIRV type name: opencl.sampler_t");
658
659 auto Sampler =
660 ResReg.isValid()
661 ? ResReg
662 : MIRBuilder.getMRI()->createVirtualRegister(&SPIRV::iIDRegClass);
663 auto Res = createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
664 return MIRBuilder.buildInstr(SPIRV::OpConstantSampler)
665 .addDef(Sampler)
666 .addUse(getSPIRVTypeID(SampTy))
667 .addImm(AddrMode)
668 .addImm(Param)
669 .addImm(FilerMode);
670 });
671 assert(Res->getOperand(0).isReg());
672 return Res->getOperand(0).getReg();
673}
674
677 const GlobalValue *GV, SPIRV::StorageClass::StorageClass Storage,
678 const MachineInstr *Init, bool IsConst, bool HasLinkageTy,
679 SPIRV::LinkageType::LinkageType LinkageType, MachineIRBuilder &MIRBuilder,
680 bool IsInstSelector) {
681 const GlobalVariable *GVar = nullptr;
682 if (GV)
683 GVar = cast<const GlobalVariable>(GV);
684 else {
685 // If GV is not passed explicitly, use the name to find or construct
686 // the global variable.
687 Module *M = MIRBuilder.getMF().getFunction().getParent();
688 GVar = M->getGlobalVariable(Name);
689 if (GVar == nullptr) {
690 const Type *Ty = getTypeForSPIRVType(BaseType); // TODO: check type.
691 // Module takes ownership of the global var.
692 GVar = new GlobalVariable(*M, const_cast<Type *>(Ty), false,
694 Twine(Name));
695 }
696 GV = GVar;
697 }
698 Register Reg = DT.find(GVar, &MIRBuilder.getMF());
699 if (Reg.isValid()) {
700 if (Reg != ResVReg)
701 MIRBuilder.buildCopy(ResVReg, Reg);
702 return ResVReg;
703 }
704
705 auto MIB = MIRBuilder.buildInstr(SPIRV::OpVariable)
706 .addDef(ResVReg)
708 .addImm(static_cast<uint32_t>(Storage));
709
710 if (Init != 0) {
711 MIB.addUse(Init->getOperand(0).getReg());
712 }
713
714 // ISel may introduce a new register on this step, so we need to add it to
715 // DT and correct its type avoiding fails on the next stage.
716 if (IsInstSelector) {
717 const auto &Subtarget = CurMF->getSubtarget();
718 constrainSelectedInstRegOperands(*MIB, *Subtarget.getInstrInfo(),
719 *Subtarget.getRegisterInfo(),
720 *Subtarget.getRegBankInfo());
721 }
722 Reg = MIB->getOperand(0).getReg();
723 DT.add(GVar, &MIRBuilder.getMF(), Reg);
724
725 // Set to Reg the same type as ResVReg has.
726 auto MRI = MIRBuilder.getMRI();
727 if (Reg != ResVReg) {
728 LLT RegLLTy =
729 LLT::pointer(MRI->getType(ResVReg).getAddressSpace(), getPointerSize());
730 MRI->setType(Reg, RegLLTy);
731 assignSPIRVTypeToVReg(BaseType, Reg, MIRBuilder.getMF());
732 } else {
733 // Our knowledge about the type may be updated.
734 // If that's the case, we need to update a type
735 // associated with the register.
736 SPIRVType *DefType = getSPIRVTypeForVReg(ResVReg);
737 if (!DefType || DefType != BaseType)
738 assignSPIRVTypeToVReg(BaseType, Reg, MIRBuilder.getMF());
739 }
740
741 // If it's a global variable with name, output OpName for it.
742 if (GVar && GVar->hasName())
743 buildOpName(Reg, GVar->getName(), MIRBuilder);
744
745 // Output decorations for the GV.
746 // TODO: maybe move to GenerateDecorations pass.
747 const SPIRVSubtarget &ST =
748 cast<SPIRVSubtarget>(MIRBuilder.getMF().getSubtarget());
749 if (IsConst && ST.isOpenCLEnv())
750 buildOpDecorate(Reg, MIRBuilder, SPIRV::Decoration::Constant, {});
751
752 if (GVar && GVar->getAlign().valueOrOne().value() != 1) {
753 unsigned Alignment = (unsigned)GVar->getAlign().valueOrOne().value();
754 buildOpDecorate(Reg, MIRBuilder, SPIRV::Decoration::Alignment, {Alignment});
755 }
756
757 if (HasLinkageTy)
758 buildOpDecorate(Reg, MIRBuilder, SPIRV::Decoration::LinkageAttributes,
759 {static_cast<uint32_t>(LinkageType)}, Name);
760
761 SPIRV::BuiltIn::BuiltIn BuiltInId;
762 if (getSpirvBuiltInIdByName(Name, BuiltInId))
763 buildOpDecorate(Reg, MIRBuilder, SPIRV::Decoration::BuiltIn,
764 {static_cast<uint32_t>(BuiltInId)});
765
766 // If it's a global variable with "spirv.Decorations" metadata node
767 // recognize it as a SPIR-V friendly LLVM IR and parse "spirv.Decorations"
768 // arguments.
769 MDNode *GVarMD = nullptr;
770 if (GVar && (GVarMD = GVar->getMetadata("spirv.Decorations")) != nullptr)
771 buildOpSpirvDecorations(Reg, MIRBuilder, GVarMD);
772
773 return Reg;
774}
775
776static std::string GetSpirvImageTypeName(const SPIRVType *Type,
777 MachineIRBuilder &MIRBuilder,
778 const std::string &Prefix);
779
780static std::string buildSpirvTypeName(const SPIRVType *Type,
781 MachineIRBuilder &MIRBuilder) {
782 switch (Type->getOpcode()) {
783 case SPIRV::OpTypeSampledImage: {
784 return GetSpirvImageTypeName(Type, MIRBuilder, "sampled_image_");
785 }
786 case SPIRV::OpTypeImage: {
787 return GetSpirvImageTypeName(Type, MIRBuilder, "image_");
788 }
789 case SPIRV::OpTypeArray: {
790 MachineRegisterInfo *MRI = MIRBuilder.getMRI();
791 Register ElementTypeReg = Type->getOperand(1).getReg();
792 auto *ElementType = MRI->getUniqueVRegDef(ElementTypeReg);
793 const SPIRVType *TypeInst = MRI->getVRegDef(Type->getOperand(2).getReg());
794 assert(TypeInst->getOpcode() != SPIRV::OpConstantI);
795 MachineInstr *ImmInst = MRI->getVRegDef(TypeInst->getOperand(1).getReg());
796 assert(ImmInst->getOpcode() == TargetOpcode::G_CONSTANT);
797 uint32_t ArraySize = ImmInst->getOperand(1).getCImm()->getZExtValue();
798 return (buildSpirvTypeName(ElementType, MIRBuilder) + Twine("[") +
799 Twine(ArraySize) + Twine("]"))
800 .str();
801 }
802 case SPIRV::OpTypeFloat:
803 return ("f" + Twine(Type->getOperand(1).getImm())).str();
804 case SPIRV::OpTypeSampler:
805 return ("sampler");
806 case SPIRV::OpTypeInt:
807 if (Type->getOperand(2).getImm())
808 return ("i" + Twine(Type->getOperand(1).getImm())).str();
809 return ("u" + Twine(Type->getOperand(1).getImm())).str();
810 default:
811 llvm_unreachable("Trying to the the name of an unknown type.");
812 }
813}
814
815static std::string GetSpirvImageTypeName(const SPIRVType *Type,
816 MachineIRBuilder &MIRBuilder,
817 const std::string &Prefix) {
818 Register SampledTypeReg = Type->getOperand(1).getReg();
819 auto *SampledType = MIRBuilder.getMRI()->getUniqueVRegDef(SampledTypeReg);
820 std::string TypeName = Prefix + buildSpirvTypeName(SampledType, MIRBuilder);
821 for (uint32_t I = 2; I < Type->getNumOperands(); ++I) {
822 TypeName = (TypeName + '_' + Twine(Type->getOperand(I).getImm())).str();
823 }
824 return TypeName;
825}
826
828 const SPIRVType *VarType, uint32_t Set, uint32_t Binding,
829 MachineIRBuilder &MIRBuilder) {
830 SPIRVType *VarPointerTypeReg = getOrCreateSPIRVPointerType(
831 VarType, MIRBuilder, SPIRV::StorageClass::UniformConstant);
832 Register VarReg =
833 MIRBuilder.getMRI()->createVirtualRegister(&SPIRV::iIDRegClass);
834
835 // TODO: The name should come from the llvm-ir, but how that name will be
836 // passed from the HLSL to the backend has not been decided. Using this place
837 // holder for now.
838 std::string Name = ("__resource_" + buildSpirvTypeName(VarType, MIRBuilder) +
839 "_" + Twine(Set) + "_" + Twine(Binding))
840 .str();
841 buildGlobalVariable(VarReg, VarPointerTypeReg, Name, nullptr,
842 SPIRV::StorageClass::UniformConstant, nullptr, false,
843 false, SPIRV::LinkageType::Import, MIRBuilder, false);
844
845 buildOpDecorate(VarReg, MIRBuilder, SPIRV::Decoration::DescriptorSet, {Set});
846 buildOpDecorate(VarReg, MIRBuilder, SPIRV::Decoration::Binding, {Binding});
847 return VarReg;
848}
849
850SPIRVType *SPIRVGlobalRegistry::getOpTypeArray(uint32_t NumElems,
851 SPIRVType *ElemType,
852 MachineIRBuilder &MIRBuilder,
853 bool EmitIR) {
854 assert((ElemType->getOpcode() != SPIRV::OpTypeVoid) &&
855 "Invalid array element type");
856 SPIRVType *SpvTypeInt32 = getOrCreateSPIRVIntegerType(32, MIRBuilder);
857 Register NumElementsVReg =
858 buildConstantInt(NumElems, MIRBuilder, SpvTypeInt32, EmitIR);
859 return createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
860 return MIRBuilder.buildInstr(SPIRV::OpTypeArray)
861 .addDef(createTypeVReg(MIRBuilder))
862 .addUse(getSPIRVTypeID(ElemType))
863 .addUse(NumElementsVReg);
864 });
865}
866
867SPIRVType *SPIRVGlobalRegistry::getOpTypeOpaque(const StructType *Ty,
868 MachineIRBuilder &MIRBuilder) {
869 assert(Ty->hasName());
870 const StringRef Name = Ty->hasName() ? Ty->getName() : "";
871 Register ResVReg = createTypeVReg(MIRBuilder);
872 return createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
873 auto MIB = MIRBuilder.buildInstr(SPIRV::OpTypeOpaque).addDef(ResVReg);
874 addStringImm(Name, MIB);
875 buildOpName(ResVReg, Name, MIRBuilder);
876 return MIB;
877 });
878}
879
880SPIRVType *SPIRVGlobalRegistry::getOpTypeStruct(const StructType *Ty,
881 MachineIRBuilder &MIRBuilder,
882 bool EmitIR) {
883 SmallVector<Register, 4> FieldTypes;
884 for (const auto &Elem : Ty->elements()) {
885 SPIRVType *ElemTy = findSPIRVType(toTypedPointer(Elem), MIRBuilder);
886 assert(ElemTy && ElemTy->getOpcode() != SPIRV::OpTypeVoid &&
887 "Invalid struct element type");
888 FieldTypes.push_back(getSPIRVTypeID(ElemTy));
889 }
890 Register ResVReg = createTypeVReg(MIRBuilder);
891 return createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
892 auto MIB = MIRBuilder.buildInstr(SPIRV::OpTypeStruct).addDef(ResVReg);
893 for (const auto &Ty : FieldTypes)
894 MIB.addUse(Ty);
895 if (Ty->hasName())
896 buildOpName(ResVReg, Ty->getName(), MIRBuilder);
897 if (Ty->isPacked())
898 buildOpDecorate(ResVReg, MIRBuilder, SPIRV::Decoration::CPacked, {});
899 return MIB;
900 });
901}
902
903SPIRVType *SPIRVGlobalRegistry::getOrCreateSpecialType(
904 const Type *Ty, MachineIRBuilder &MIRBuilder,
905 SPIRV::AccessQualifier::AccessQualifier AccQual) {
906 assert(isSpecialOpaqueType(Ty) && "Not a special opaque builtin type");
907 return SPIRV::lowerBuiltinType(Ty, AccQual, MIRBuilder, this);
908}
909
910SPIRVType *SPIRVGlobalRegistry::getOpTypePointer(
911 SPIRV::StorageClass::StorageClass SC, SPIRVType *ElemType,
912 MachineIRBuilder &MIRBuilder, Register Reg) {
913 if (!Reg.isValid())
914 Reg = createTypeVReg(MIRBuilder);
915
916 return createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
917 return MIRBuilder.buildInstr(SPIRV::OpTypePointer)
918 .addDef(Reg)
919 .addImm(static_cast<uint32_t>(SC))
920 .addUse(getSPIRVTypeID(ElemType));
921 });
922}
923
924SPIRVType *SPIRVGlobalRegistry::getOpTypeForwardPointer(
925 SPIRV::StorageClass::StorageClass SC, MachineIRBuilder &MIRBuilder) {
926 return createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
927 return MIRBuilder.buildInstr(SPIRV::OpTypeForwardPointer)
928 .addUse(createTypeVReg(MIRBuilder))
929 .addImm(static_cast<uint32_t>(SC));
930 });
931}
932
933SPIRVType *SPIRVGlobalRegistry::getOpTypeFunction(
934 SPIRVType *RetType, const SmallVectorImpl<SPIRVType *> &ArgTypes,
935 MachineIRBuilder &MIRBuilder) {
936 auto MIB = MIRBuilder.buildInstr(SPIRV::OpTypeFunction)
937 .addDef(createTypeVReg(MIRBuilder))
938 .addUse(getSPIRVTypeID(RetType));
939 for (const SPIRVType *ArgType : ArgTypes)
940 MIB.addUse(getSPIRVTypeID(ArgType));
941 return MIB;
942}
943
945 const Type *Ty, SPIRVType *RetType,
946 const SmallVectorImpl<SPIRVType *> &ArgTypes,
947 MachineIRBuilder &MIRBuilder) {
948 Register Reg = DT.find(Ty, &MIRBuilder.getMF());
949 if (Reg.isValid())
950 return getSPIRVTypeForVReg(Reg);
951 SPIRVType *SpirvType = getOpTypeFunction(RetType, ArgTypes, MIRBuilder);
952 DT.add(Ty, CurMF, getSPIRVTypeID(SpirvType));
953 return finishCreatingSPIRVType(Ty, SpirvType);
954}
955
956SPIRVType *SPIRVGlobalRegistry::findSPIRVType(
957 const Type *Ty, MachineIRBuilder &MIRBuilder,
958 SPIRV::AccessQualifier::AccessQualifier AccQual, bool EmitIR) {
959 Ty = adjustIntTypeByWidth(Ty);
960 Register Reg = DT.find(Ty, &MIRBuilder.getMF());
961 if (Reg.isValid())
962 return getSPIRVTypeForVReg(Reg);
963 if (ForwardPointerTypes.contains(Ty))
964 return ForwardPointerTypes[Ty];
965 return restOfCreateSPIRVType(Ty, MIRBuilder, AccQual, EmitIR);
966}
967
969 assert(SpirvType && "Attempting to get type id for nullptr type.");
970 if (SpirvType->getOpcode() == SPIRV::OpTypeForwardPointer)
971 return SpirvType->uses().begin()->getReg();
972 return SpirvType->defs().begin()->getReg();
973}
974
975// We need to use a new LLVM integer type if there is a mismatch between
976// number of bits in LLVM and SPIRV integer types to let DuplicateTracker
977// ensure uniqueness of a SPIRV type by the corresponding LLVM type. Without
978// such an adjustment SPIRVGlobalRegistry::getOpTypeInt() could create the
979// same "OpTypeInt 8" type for a series of LLVM integer types with number of
980// bits less than 8. This would lead to duplicate type definitions
981// eventually due to the method that DuplicateTracker utilizes to reason
982// about uniqueness of type records.
983const Type *SPIRVGlobalRegistry::adjustIntTypeByWidth(const Type *Ty) const {
984 if (auto IType = dyn_cast<IntegerType>(Ty)) {
985 unsigned SrcBitWidth = IType->getBitWidth();
986 if (SrcBitWidth > 1) {
987 unsigned BitWidth = adjustOpTypeIntWidth(SrcBitWidth);
988 // Maybe change source LLVM type to keep DuplicateTracker consistent.
989 if (SrcBitWidth != BitWidth)
991 }
992 }
993 return Ty;
994}
995
996SPIRVType *SPIRVGlobalRegistry::createSPIRVType(
997 const Type *Ty, MachineIRBuilder &MIRBuilder,
998 SPIRV::AccessQualifier::AccessQualifier AccQual, bool EmitIR) {
999 if (isSpecialOpaqueType(Ty))
1000 return getOrCreateSpecialType(Ty, MIRBuilder, AccQual);
1001 auto &TypeToSPIRVTypeMap = DT.getTypes()->getAllUses();
1002 auto t = TypeToSPIRVTypeMap.find(Ty);
1003 if (t != TypeToSPIRVTypeMap.end()) {
1004 auto tt = t->second.find(&MIRBuilder.getMF());
1005 if (tt != t->second.end())
1006 return getSPIRVTypeForVReg(tt->second);
1007 }
1008
1009 if (auto IType = dyn_cast<IntegerType>(Ty)) {
1010 const unsigned Width = IType->getBitWidth();
1011 return Width == 1 ? getOpTypeBool(MIRBuilder)
1012 : getOpTypeInt(Width, MIRBuilder, false);
1013 }
1014 if (Ty->isFloatingPointTy())
1015 return getOpTypeFloat(Ty->getPrimitiveSizeInBits(), MIRBuilder);
1016 if (Ty->isVoidTy())
1017 return getOpTypeVoid(MIRBuilder);
1018 if (Ty->isVectorTy()) {
1019 SPIRVType *El =
1020 findSPIRVType(cast<FixedVectorType>(Ty)->getElementType(), MIRBuilder);
1021 return getOpTypeVector(cast<FixedVectorType>(Ty)->getNumElements(), El,
1022 MIRBuilder);
1023 }
1024 if (Ty->isArrayTy()) {
1025 SPIRVType *El = findSPIRVType(Ty->getArrayElementType(), MIRBuilder);
1026 return getOpTypeArray(Ty->getArrayNumElements(), El, MIRBuilder, EmitIR);
1027 }
1028 if (auto SType = dyn_cast<StructType>(Ty)) {
1029 if (SType->isOpaque())
1030 return getOpTypeOpaque(SType, MIRBuilder);
1031 return getOpTypeStruct(SType, MIRBuilder, EmitIR);
1032 }
1033 if (auto FType = dyn_cast<FunctionType>(Ty)) {
1034 SPIRVType *RetTy = findSPIRVType(FType->getReturnType(), MIRBuilder);
1035 SmallVector<SPIRVType *, 4> ParamTypes;
1036 for (const auto &t : FType->params()) {
1037 ParamTypes.push_back(findSPIRVType(t, MIRBuilder));
1038 }
1039 return getOpTypeFunction(RetTy, ParamTypes, MIRBuilder);
1040 }
1041
1042 unsigned AddrSpace = typeToAddressSpace(Ty);
1043 SPIRVType *SpvElementType = nullptr;
1044 if (Type *ElemTy = ::getPointeeType(Ty))
1045 SpvElementType = getOrCreateSPIRVType(ElemTy, MIRBuilder, AccQual, EmitIR);
1046 else
1047 SpvElementType = getOrCreateSPIRVIntegerType(8, MIRBuilder);
1048
1049 // Get access to information about available extensions
1050 const SPIRVSubtarget *ST =
1051 static_cast<const SPIRVSubtarget *>(&MIRBuilder.getMF().getSubtarget());
1052 auto SC = addressSpaceToStorageClass(AddrSpace, *ST);
1053 // Null pointer means we have a loop in type definitions, make and
1054 // return corresponding OpTypeForwardPointer.
1055 if (SpvElementType == nullptr) {
1056 if (!ForwardPointerTypes.contains(Ty))
1057 ForwardPointerTypes[Ty] = getOpTypeForwardPointer(SC, MIRBuilder);
1058 return ForwardPointerTypes[Ty];
1059 }
1060 // If we have forward pointer associated with this type, use its register
1061 // operand to create OpTypePointer.
1062 if (ForwardPointerTypes.contains(Ty)) {
1063 Register Reg = getSPIRVTypeID(ForwardPointerTypes[Ty]);
1064 return getOpTypePointer(SC, SpvElementType, MIRBuilder, Reg);
1065 }
1066
1067 return getOrCreateSPIRVPointerType(SpvElementType, MIRBuilder, SC);
1068}
1069
1070SPIRVType *SPIRVGlobalRegistry::restOfCreateSPIRVType(
1071 const Type *Ty, MachineIRBuilder &MIRBuilder,
1072 SPIRV::AccessQualifier::AccessQualifier AccessQual, bool EmitIR) {
1073 if (TypesInProcessing.count(Ty) && !isPointerTyOrWrapper(Ty))
1074 return nullptr;
1075 TypesInProcessing.insert(Ty);
1076 SPIRVType *SpirvType = createSPIRVType(Ty, MIRBuilder, AccessQual, EmitIR);
1077 TypesInProcessing.erase(Ty);
1078 VRegToTypeMap[&MIRBuilder.getMF()][getSPIRVTypeID(SpirvType)] = SpirvType;
1079 SPIRVToLLVMType[SpirvType] = unifyPtrType(Ty);
1080 Register Reg = DT.find(Ty, &MIRBuilder.getMF());
1081 // Do not add OpTypeForwardPointer to DT, a corresponding normal pointer type
1082 // will be added later. For special types it is already added to DT.
1083 if (SpirvType->getOpcode() != SPIRV::OpTypeForwardPointer && !Reg.isValid() &&
1084 !isSpecialOpaqueType(Ty)) {
1085 if (auto *ExtTy = dyn_cast<TargetExtType>(Ty);
1086 ExtTy && isTypedPointerWrapper(ExtTy))
1087 DT.add(ExtTy->getTypeParameter(0), ExtTy->getIntParameter(0),
1088 &MIRBuilder.getMF(), getSPIRVTypeID(SpirvType));
1089 else if (!isPointerTy(Ty))
1090 DT.add(Ty, &MIRBuilder.getMF(), getSPIRVTypeID(SpirvType));
1091 else if (isTypedPointerTy(Ty))
1092 DT.add(cast<TypedPointerType>(Ty)->getElementType(),
1093 getPointerAddressSpace(Ty), &MIRBuilder.getMF(),
1094 getSPIRVTypeID(SpirvType));
1095 else
1096 DT.add(Type::getInt8Ty(MIRBuilder.getMF().getFunction().getContext()),
1097 getPointerAddressSpace(Ty), &MIRBuilder.getMF(),
1098 getSPIRVTypeID(SpirvType));
1099 }
1100
1101 return SpirvType;
1102}
1103
1104SPIRVType *
1106 const MachineFunction *MF) const {
1107 auto t = VRegToTypeMap.find(MF ? MF : CurMF);
1108 if (t != VRegToTypeMap.end()) {
1109 auto tt = t->second.find(VReg);
1110 if (tt != t->second.end())
1111 return tt->second;
1112 }
1113 return nullptr;
1114}
1115
1117 MachineInstr *Instr = getVRegDef(CurMF->getRegInfo(), VReg);
1118 return getSPIRVTypeForVReg(Instr->getOperand(1).getReg());
1119}
1120
1122 const Type *Ty, MachineIRBuilder &MIRBuilder,
1123 SPIRV::AccessQualifier::AccessQualifier AccessQual, bool EmitIR) {
1124 Register Reg;
1125 if (auto *ExtTy = dyn_cast<TargetExtType>(Ty);
1126 ExtTy && isTypedPointerWrapper(ExtTy)) {
1127 Reg = DT.find(ExtTy->getTypeParameter(0), ExtTy->getIntParameter(0),
1128 &MIRBuilder.getMF());
1129 } else if (!isPointerTy(Ty)) {
1130 Ty = adjustIntTypeByWidth(Ty);
1131 Reg = DT.find(Ty, &MIRBuilder.getMF());
1132 } else if (isTypedPointerTy(Ty)) {
1133 Reg = DT.find(cast<TypedPointerType>(Ty)->getElementType(),
1134 getPointerAddressSpace(Ty), &MIRBuilder.getMF());
1135 } else {
1136 Reg =
1137 DT.find(Type::getInt8Ty(MIRBuilder.getMF().getFunction().getContext()),
1138 getPointerAddressSpace(Ty), &MIRBuilder.getMF());
1139 }
1140
1141 if (Reg.isValid() && !isSpecialOpaqueType(Ty))
1142 return getSPIRVTypeForVReg(Reg);
1143 TypesInProcessing.clear();
1144 SPIRVType *STy = restOfCreateSPIRVType(Ty, MIRBuilder, AccessQual, EmitIR);
1145 // Create normal pointer types for the corresponding OpTypeForwardPointers.
1146 for (auto &CU : ForwardPointerTypes) {
1147 const Type *Ty2 = CU.first;
1148 SPIRVType *STy2 = CU.second;
1149 if ((Reg = DT.find(Ty2, &MIRBuilder.getMF())).isValid())
1150 STy2 = getSPIRVTypeForVReg(Reg);
1151 else
1152 STy2 = restOfCreateSPIRVType(Ty2, MIRBuilder, AccessQual, EmitIR);
1153 if (Ty == Ty2)
1154 STy = STy2;
1155 }
1156 ForwardPointerTypes.clear();
1157 return STy;
1158}
1159
1161 unsigned TypeOpcode) const {
1163 assert(Type && "isScalarOfType VReg has no type assigned");
1164 return Type->getOpcode() == TypeOpcode;
1165}
1166
1168 unsigned TypeOpcode) const {
1170 assert(Type && "isScalarOrVectorOfType VReg has no type assigned");
1171 if (Type->getOpcode() == TypeOpcode)
1172 return true;
1173 if (Type->getOpcode() == SPIRV::OpTypeVector) {
1174 Register ScalarTypeVReg = Type->getOperand(1).getReg();
1175 SPIRVType *ScalarType = getSPIRVTypeForVReg(ScalarTypeVReg);
1176 return ScalarType->getOpcode() == TypeOpcode;
1177 }
1178 return false;
1179}
1180
1181unsigned
1184}
1185
1186unsigned
1188 if (!Type)
1189 return 0;
1190 return Type->getOpcode() == SPIRV::OpTypeVector
1191 ? static_cast<unsigned>(Type->getOperand(2).getImm())
1192 : 1;
1193}
1194
1195SPIRVType *
1198}
1199
1200SPIRVType *
1202 if (!Type)
1203 return nullptr;
1204 Register ScalarReg = Type->getOpcode() == SPIRV::OpTypeVector
1205 ? Type->getOperand(1).getReg()
1206 : Type->getOperand(0).getReg();
1207 SPIRVType *ScalarType = getSPIRVTypeForVReg(ScalarReg);
1208 assert(isScalarOrVectorOfType(Type->getOperand(0).getReg(),
1209 ScalarType->getOpcode()));
1210 return ScalarType;
1211}
1212
1213unsigned
1215 assert(Type && "Invalid Type pointer");
1216 if (Type->getOpcode() == SPIRV::OpTypeVector) {
1217 auto EleTypeReg = Type->getOperand(1).getReg();
1218 Type = getSPIRVTypeForVReg(EleTypeReg);
1219 }
1220 if (Type->getOpcode() == SPIRV::OpTypeInt ||
1221 Type->getOpcode() == SPIRV::OpTypeFloat)
1222 return Type->getOperand(1).getImm();
1223 if (Type->getOpcode() == SPIRV::OpTypeBool)
1224 return 1;
1225 llvm_unreachable("Attempting to get bit width of non-integer/float type.");
1226}
1227
1229 const SPIRVType *Type) const {
1230 assert(Type && "Invalid Type pointer");
1231 unsigned NumElements = 1;
1232 if (Type->getOpcode() == SPIRV::OpTypeVector) {
1233 NumElements = static_cast<unsigned>(Type->getOperand(2).getImm());
1234 Type = getSPIRVTypeForVReg(Type->getOperand(1).getReg());
1235 }
1236 return Type->getOpcode() == SPIRV::OpTypeInt ||
1237 Type->getOpcode() == SPIRV::OpTypeFloat
1238 ? NumElements * Type->getOperand(1).getImm()
1239 : 0;
1240}
1241
1243 const SPIRVType *Type) const {
1244 if (Type && Type->getOpcode() == SPIRV::OpTypeVector)
1245 Type = getSPIRVTypeForVReg(Type->getOperand(1).getReg());
1246 return Type && Type->getOpcode() == SPIRV::OpTypeInt ? Type : nullptr;
1247}
1248
1251 return IntType && IntType->getOperand(2).getImm() != 0;
1252}
1253
1255 return PtrType && PtrType->getOpcode() == SPIRV::OpTypePointer
1256 ? getSPIRVTypeForVReg(PtrType->getOperand(2).getReg())
1257 : nullptr;
1258}
1259
1261 SPIRVType *ElemType = getPointeeType(getSPIRVTypeForVReg(PtrReg));
1262 return ElemType ? ElemType->getOpcode() : 0;
1263}
1264
1266 const SPIRVType *Type2) const {
1267 if (!Type1 || !Type2)
1268 return false;
1269 auto Op1 = Type1->getOpcode(), Op2 = Type2->getOpcode();
1270 // Ignore difference between <1.5 and >=1.5 protocol versions:
1271 // it's valid if either Result Type or Operand is a pointer, and the other
1272 // is a pointer, an integer scalar, or an integer vector.
1273 if (Op1 == SPIRV::OpTypePointer &&
1274 (Op2 == SPIRV::OpTypePointer || retrieveScalarOrVectorIntType(Type2)))
1275 return true;
1276 if (Op2 == SPIRV::OpTypePointer &&
1277 (Op1 == SPIRV::OpTypePointer || retrieveScalarOrVectorIntType(Type1)))
1278 return true;
1279 unsigned Bits1 = getNumScalarOrVectorTotalBitWidth(Type1),
1281 return Bits1 > 0 && Bits1 == Bits2;
1282}
1283
1284SPIRV::StorageClass::StorageClass
1287 assert(Type && Type->getOpcode() == SPIRV::OpTypePointer &&
1288 Type->getOperand(1).isImm() && "Pointer type is expected");
1290}
1291
1292SPIRV::StorageClass::StorageClass
1294 return static_cast<SPIRV::StorageClass::StorageClass>(
1295 Type->getOperand(1).getImm());
1296}
1297
1299 MachineIRBuilder &MIRBuilder, SPIRVType *SampledType, SPIRV::Dim::Dim Dim,
1300 uint32_t Depth, uint32_t Arrayed, uint32_t Multisampled, uint32_t Sampled,
1301 SPIRV::ImageFormat::ImageFormat ImageFormat,
1302 SPIRV::AccessQualifier::AccessQualifier AccessQual) {
1303 auto TD = SPIRV::make_descr_image(SPIRVToLLVMType.lookup(SampledType), Dim,
1304 Depth, Arrayed, Multisampled, Sampled,
1305 ImageFormat, AccessQual);
1306 if (auto *Res = checkSpecialInstr(TD, MIRBuilder))
1307 return Res;
1308 Register ResVReg = createTypeVReg(MIRBuilder);
1309 DT.add(TD, &MIRBuilder.getMF(), ResVReg);
1310 auto MIB = MIRBuilder.buildInstr(SPIRV::OpTypeImage)
1311 .addDef(ResVReg)
1312 .addUse(getSPIRVTypeID(SampledType))
1313 .addImm(Dim)
1314 .addImm(Depth) // Depth (whether or not it is a Depth image).
1315 .addImm(Arrayed) // Arrayed.
1316 .addImm(Multisampled) // Multisampled (0 = only single-sample).
1317 .addImm(Sampled) // Sampled (0 = usage known at runtime).
1318 .addImm(ImageFormat);
1319
1320 if (AccessQual != SPIRV::AccessQualifier::None)
1321 MIB.addImm(AccessQual);
1322 return MIB;
1323}
1324
1325SPIRVType *
1327 auto TD = SPIRV::make_descr_sampler();
1328 if (auto *Res = checkSpecialInstr(TD, MIRBuilder))
1329 return Res;
1330 Register ResVReg = createTypeVReg(MIRBuilder);
1331 DT.add(TD, &MIRBuilder.getMF(), ResVReg);
1332 return MIRBuilder.buildInstr(SPIRV::OpTypeSampler).addDef(ResVReg);
1333}
1334
1336 MachineIRBuilder &MIRBuilder,
1337 SPIRV::AccessQualifier::AccessQualifier AccessQual) {
1338 auto TD = SPIRV::make_descr_pipe(AccessQual);
1339 if (auto *Res = checkSpecialInstr(TD, MIRBuilder))
1340 return Res;
1341 Register ResVReg = createTypeVReg(MIRBuilder);
1342 DT.add(TD, &MIRBuilder.getMF(), ResVReg);
1343 return MIRBuilder.buildInstr(SPIRV::OpTypePipe)
1344 .addDef(ResVReg)
1345 .addImm(AccessQual);
1346}
1347
1349 MachineIRBuilder &MIRBuilder) {
1350 auto TD = SPIRV::make_descr_event();
1351 if (auto *Res = checkSpecialInstr(TD, MIRBuilder))
1352 return Res;
1353 Register ResVReg = createTypeVReg(MIRBuilder);
1354 DT.add(TD, &MIRBuilder.getMF(), ResVReg);
1355 return MIRBuilder.buildInstr(SPIRV::OpTypeDeviceEvent).addDef(ResVReg);
1356}
1357
1359 SPIRVType *ImageType, MachineIRBuilder &MIRBuilder) {
1361 SPIRVToLLVMType.lookup(MIRBuilder.getMF().getRegInfo().getVRegDef(
1362 ImageType->getOperand(1).getReg())),
1363 ImageType);
1364 if (auto *Res = checkSpecialInstr(TD, MIRBuilder))
1365 return Res;
1366 Register ResVReg = createTypeVReg(MIRBuilder);
1367 DT.add(TD, &MIRBuilder.getMF(), ResVReg);
1368 return MIRBuilder.buildInstr(SPIRV::OpTypeSampledImage)
1369 .addDef(ResVReg)
1370 .addUse(getSPIRVTypeID(ImageType));
1371}
1372
1374 MachineIRBuilder &MIRBuilder, const TargetExtType *ExtensionType,
1375 const SPIRVType *ElemType, uint32_t Scope, uint32_t Rows, uint32_t Columns,
1376 uint32_t Use) {
1377 Register ResVReg = DT.find(ExtensionType, &MIRBuilder.getMF());
1378 if (ResVReg.isValid())
1379 return MIRBuilder.getMF().getRegInfo().getUniqueVRegDef(ResVReg);
1380 ResVReg = createTypeVReg(MIRBuilder);
1381 SPIRVType *SpvTypeInt32 = getOrCreateSPIRVIntegerType(32, MIRBuilder);
1382 SPIRVType *SpirvTy =
1383 MIRBuilder.buildInstr(SPIRV::OpTypeCooperativeMatrixKHR)
1384 .addDef(ResVReg)
1385 .addUse(getSPIRVTypeID(ElemType))
1386 .addUse(buildConstantInt(Scope, MIRBuilder, SpvTypeInt32, true))
1387 .addUse(buildConstantInt(Rows, MIRBuilder, SpvTypeInt32, true))
1388 .addUse(buildConstantInt(Columns, MIRBuilder, SpvTypeInt32, true))
1389 .addUse(buildConstantInt(Use, MIRBuilder, SpvTypeInt32, true));
1390 DT.add(ExtensionType, &MIRBuilder.getMF(), ResVReg);
1391 return SpirvTy;
1392}
1393
1395 const Type *Ty, MachineIRBuilder &MIRBuilder, unsigned Opcode) {
1396 Register ResVReg = DT.find(Ty, &MIRBuilder.getMF());
1397 if (ResVReg.isValid())
1398 return MIRBuilder.getMF().getRegInfo().getUniqueVRegDef(ResVReg);
1399 ResVReg = createTypeVReg(MIRBuilder);
1400 SPIRVType *SpirvTy = MIRBuilder.buildInstr(Opcode).addDef(ResVReg);
1401 DT.add(Ty, &MIRBuilder.getMF(), ResVReg);
1402 return SpirvTy;
1403}
1404
1405const MachineInstr *
1406SPIRVGlobalRegistry::checkSpecialInstr(const SPIRV::SpecialTypeDescriptor &TD,
1407 MachineIRBuilder &MIRBuilder) {
1408 Register Reg = DT.find(TD, &MIRBuilder.getMF());
1409 if (Reg.isValid())
1410 return MIRBuilder.getMF().getRegInfo().getUniqueVRegDef(Reg);
1411 return nullptr;
1412}
1413
1414// Returns nullptr if unable to recognize SPIRV type name
1416 StringRef TypeStr, MachineIRBuilder &MIRBuilder,
1417 SPIRV::StorageClass::StorageClass SC,
1418 SPIRV::AccessQualifier::AccessQualifier AQ) {
1419 unsigned VecElts = 0;
1420 auto &Ctx = MIRBuilder.getMF().getFunction().getContext();
1421
1422 // Parse strings representing either a SPIR-V or OpenCL builtin type.
1423 if (hasBuiltinTypePrefix(TypeStr))
1425 TypeStr.str(), MIRBuilder.getContext()),
1426 MIRBuilder, AQ);
1427
1428 // Parse type name in either "typeN" or "type vector[N]" format, where
1429 // N is the number of elements of the vector.
1430 Type *Ty;
1431
1432 Ty = parseBasicTypeName(TypeStr, Ctx);
1433 if (!Ty)
1434 // Unable to recognize SPIRV type name
1435 return nullptr;
1436
1437 auto SpirvTy = getOrCreateSPIRVType(Ty, MIRBuilder, AQ);
1438
1439 // Handle "type*" or "type* vector[N]".
1440 if (TypeStr.starts_with("*")) {
1441 SpirvTy = getOrCreateSPIRVPointerType(SpirvTy, MIRBuilder, SC);
1442 TypeStr = TypeStr.substr(strlen("*"));
1443 }
1444
1445 // Handle "typeN*" or "type vector[N]*".
1446 bool IsPtrToVec = TypeStr.consume_back("*");
1447
1448 if (TypeStr.consume_front(" vector[")) {
1449 TypeStr = TypeStr.substr(0, TypeStr.find(']'));
1450 }
1451 TypeStr.getAsInteger(10, VecElts);
1452 if (VecElts > 0)
1453 SpirvTy = getOrCreateSPIRVVectorType(SpirvTy, VecElts, MIRBuilder);
1454
1455 if (IsPtrToVec)
1456 SpirvTy = getOrCreateSPIRVPointerType(SpirvTy, MIRBuilder, SC);
1457
1458 return SpirvTy;
1459}
1460
1461SPIRVType *
1463 MachineIRBuilder &MIRBuilder) {
1464 return getOrCreateSPIRVType(
1466 MIRBuilder);
1467}
1468
1469SPIRVType *SPIRVGlobalRegistry::finishCreatingSPIRVType(const Type *LLVMTy,
1470 SPIRVType *SpirvType) {
1471 assert(CurMF == SpirvType->getMF());
1472 VRegToTypeMap[CurMF][getSPIRVTypeID(SpirvType)] = SpirvType;
1473 SPIRVToLLVMType[SpirvType] = unifyPtrType(LLVMTy);
1474 return SpirvType;
1475}
1476
1478 MachineInstr &I,
1479 const SPIRVInstrInfo &TII,
1480 unsigned SPIRVOPcode,
1481 Type *LLVMTy) {
1482 Register Reg = DT.find(LLVMTy, CurMF);
1483 if (Reg.isValid())
1484 return getSPIRVTypeForVReg(Reg);
1485 MachineBasicBlock &BB = *I.getParent();
1486 auto MIB = BuildMI(BB, I, I.getDebugLoc(), TII.get(SPIRVOPcode))
1489 .addImm(0);
1490 DT.add(LLVMTy, CurMF, getSPIRVTypeID(MIB));
1491 return finishCreatingSPIRVType(LLVMTy, MIB);
1492}
1493
1495 unsigned BitWidth, MachineInstr &I, const SPIRVInstrInfo &TII) {
1496 // Maybe adjust bit width to keep DuplicateTracker consistent. Without
1497 // such an adjustment SPIRVGlobalRegistry::getOpTypeInt() could create, for
1498 // example, the same "OpTypeInt 8" type for a series of LLVM integer types
1499 // with number of bits less than 8, causing duplicate type definitions.
1500 BitWidth = adjustOpTypeIntWidth(BitWidth);
1502 return getOrCreateSPIRVType(BitWidth, I, TII, SPIRV::OpTypeInt, LLVMTy);
1503}
1504
1506 unsigned BitWidth, MachineInstr &I, const SPIRVInstrInfo &TII) {
1508 Type *LLVMTy;
1509 switch (BitWidth) {
1510 case 16:
1511 LLVMTy = Type::getHalfTy(Ctx);
1512 break;
1513 case 32:
1514 LLVMTy = Type::getFloatTy(Ctx);
1515 break;
1516 case 64:
1517 LLVMTy = Type::getDoubleTy(Ctx);
1518 break;
1519 default:
1520 llvm_unreachable("Bit width is of unexpected size.");
1521 }
1522 return getOrCreateSPIRVType(BitWidth, I, TII, SPIRV::OpTypeFloat, LLVMTy);
1523}
1524
1525SPIRVType *
1527 return getOrCreateSPIRVType(
1528 IntegerType::get(MIRBuilder.getMF().getFunction().getContext(), 1),
1529 MIRBuilder);
1530}
1531
1532SPIRVType *
1534 const SPIRVInstrInfo &TII) {
1536 Register Reg = DT.find(LLVMTy, CurMF);
1537 if (Reg.isValid())
1538 return getSPIRVTypeForVReg(Reg);
1539 MachineBasicBlock &BB = *I.getParent();
1540 auto MIB = BuildMI(BB, I, I.getDebugLoc(), TII.get(SPIRV::OpTypeBool))
1542 DT.add(LLVMTy, CurMF, getSPIRVTypeID(MIB));
1543 return finishCreatingSPIRVType(LLVMTy, MIB);
1544}
1545
1547 SPIRVType *BaseType, unsigned NumElements, MachineIRBuilder &MIRBuilder) {
1548 return getOrCreateSPIRVType(
1550 NumElements),
1551 MIRBuilder);
1552}
1553
1555 SPIRVType *BaseType, unsigned NumElements, MachineInstr &I,
1556 const SPIRVInstrInfo &TII) {
1557 Type *LLVMTy = FixedVectorType::get(
1558 const_cast<Type *>(getTypeForSPIRVType(BaseType)), NumElements);
1559 Register Reg = DT.find(LLVMTy, CurMF);
1560 if (Reg.isValid())
1561 return getSPIRVTypeForVReg(Reg);
1562 MachineBasicBlock &BB = *I.getParent();
1563 auto MIB = BuildMI(BB, I, I.getDebugLoc(), TII.get(SPIRV::OpTypeVector))
1566 .addImm(NumElements);
1567 DT.add(LLVMTy, CurMF, getSPIRVTypeID(MIB));
1568 return finishCreatingSPIRVType(LLVMTy, MIB);
1569}
1570
1572 SPIRVType *BaseType, unsigned NumElements, MachineInstr &I,
1573 const SPIRVInstrInfo &TII) {
1574 Type *LLVMTy = ArrayType::get(
1575 const_cast<Type *>(getTypeForSPIRVType(BaseType)), NumElements);
1576 Register Reg = DT.find(LLVMTy, CurMF);
1577 if (Reg.isValid())
1578 return getSPIRVTypeForVReg(Reg);
1579 MachineBasicBlock &BB = *I.getParent();
1580 SPIRVType *SpvTypeInt32 = getOrCreateSPIRVIntegerType(32, I, TII);
1581 Register Len = getOrCreateConstInt(NumElements, I, SpvTypeInt32, TII);
1582 auto MIB = BuildMI(BB, I, I.getDebugLoc(), TII.get(SPIRV::OpTypeArray))
1585 .addUse(Len);
1586 DT.add(LLVMTy, CurMF, getSPIRVTypeID(MIB));
1587 return finishCreatingSPIRVType(LLVMTy, MIB);
1588}
1589
1591 SPIRVType *BaseType, MachineIRBuilder &MIRBuilder,
1592 SPIRV::StorageClass::StorageClass SC) {
1593 const Type *PointerElementType = getTypeForSPIRVType(BaseType);
1595 Type *LLVMTy = TypedPointerType::get(const_cast<Type *>(PointerElementType),
1596 AddressSpace);
1597 // check if this type is already available
1598 Register Reg = DT.find(PointerElementType, AddressSpace, CurMF);
1599 if (Reg.isValid())
1600 return getSPIRVTypeForVReg(Reg);
1601 // create a new type
1602 return createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
1603 auto MIB = BuildMI(MIRBuilder.getMBB(), MIRBuilder.getInsertPt(),
1604 MIRBuilder.getDebugLoc(),
1605 MIRBuilder.getTII().get(SPIRV::OpTypePointer))
1607 .addImm(static_cast<uint32_t>(SC))
1609 DT.add(PointerElementType, AddressSpace, CurMF, getSPIRVTypeID(MIB));
1610 finishCreatingSPIRVType(LLVMTy, MIB);
1611 return MIB;
1612 });
1613}
1614
1617 SPIRV::StorageClass::StorageClass SC) {
1618 MachineIRBuilder MIRBuilder(I);
1619 return getOrCreateSPIRVPointerType(BaseType, MIRBuilder, SC);
1620}
1621
1623 SPIRVType *SpvType,
1624 const SPIRVInstrInfo &TII) {
1625 assert(SpvType);
1626 const Type *LLVMTy = getTypeForSPIRVType(SpvType);
1627 assert(LLVMTy);
1628 // Find a constant in DT or build a new one.
1629 UndefValue *UV = UndefValue::get(const_cast<Type *>(LLVMTy));
1630 Register Res = DT.find(UV, CurMF);
1631 if (Res.isValid())
1632 return Res;
1633 LLT LLTy = LLT::scalar(64);
1635 CurMF->getRegInfo().setRegClass(Res, &SPIRV::iIDRegClass);
1636 assignSPIRVTypeToVReg(SpvType, Res, *CurMF);
1637 DT.add(UV, CurMF, Res);
1638
1640 MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(SPIRV::OpUndef))
1641 .addDef(Res)
1642 .addUse(getSPIRVTypeID(SpvType));
1643 const auto &ST = CurMF->getSubtarget();
1644 constrainSelectedInstRegOperands(*MIB, *ST.getInstrInfo(),
1645 *ST.getRegisterInfo(), *ST.getRegBankInfo());
1646 return Res;
1647}
1648
1649const TargetRegisterClass *
1651 unsigned Opcode = SpvType->getOpcode();
1652 switch (Opcode) {
1653 case SPIRV::OpTypeFloat:
1654 return &SPIRV::fIDRegClass;
1655 case SPIRV::OpTypePointer:
1656 return &SPIRV::pIDRegClass;
1657 case SPIRV::OpTypeVector: {
1658 SPIRVType *ElemType = getSPIRVTypeForVReg(SpvType->getOperand(1).getReg());
1659 unsigned ElemOpcode = ElemType ? ElemType->getOpcode() : 0;
1660 if (ElemOpcode == SPIRV::OpTypeFloat)
1661 return &SPIRV::vfIDRegClass;
1662 if (ElemOpcode == SPIRV::OpTypePointer)
1663 return &SPIRV::vpIDRegClass;
1664 return &SPIRV::vIDRegClass;
1665 }
1666 }
1667 return &SPIRV::iIDRegClass;
1668}
1669
1670inline unsigned getAS(SPIRVType *SpvType) {
1672 static_cast<SPIRV::StorageClass::StorageClass>(
1673 SpvType->getOperand(1).getImm()));
1674}
1675
1677 unsigned Opcode = SpvType ? SpvType->getOpcode() : 0;
1678 switch (Opcode) {
1679 case SPIRV::OpTypeInt:
1680 case SPIRV::OpTypeFloat:
1681 case SPIRV::OpTypeBool:
1682 return LLT::scalar(getScalarOrVectorBitWidth(SpvType));
1683 case SPIRV::OpTypePointer:
1684 return LLT::pointer(getAS(SpvType), getPointerSize());
1685 case SPIRV::OpTypeVector: {
1686 SPIRVType *ElemType = getSPIRVTypeForVReg(SpvType->getOperand(1).getReg());
1687 LLT ET;
1688 switch (ElemType ? ElemType->getOpcode() : 0) {
1689 case SPIRV::OpTypePointer:
1690 ET = LLT::pointer(getAS(ElemType), getPointerSize());
1691 break;
1692 case SPIRV::OpTypeInt:
1693 case SPIRV::OpTypeFloat:
1694 case SPIRV::OpTypeBool:
1695 ET = LLT::scalar(getScalarOrVectorBitWidth(ElemType));
1696 break;
1697 default:
1698 ET = LLT::scalar(64);
1699 }
1700 return LLT::fixed_vector(
1701 static_cast<unsigned>(SpvType->getOperand(2).getImm()), ET);
1702 }
1703 }
1704 return LLT::scalar(64);
1705}
unsigned const MachineRegisterInfo * MRI
This file implements a class to represent arbitrary precision integral constant values and operations...
This file contains the declarations for the subclasses of Constant, which represent the different fla...
return RetTy
std::string Name
ELFYAML::ELF_REL Type2
Definition: ELFYAML.cpp:1833
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition: MD5.cpp:58
if(PassOpts->AAPipeline)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static unsigned getNumElements(Type *Ty)
static Register createTypeVReg(MachineRegisterInfo &MRI)
unsigned getAS(SPIRVType *SpvType)
static std::string GetSpirvImageTypeName(const SPIRVType *Type, MachineIRBuilder &MIRBuilder, const std::string &Prefix)
static std::string buildSpirvTypeName(const SPIRVType *Type, MachineIRBuilder &MIRBuilder)
unsigned typeToAddressSpace(const Type *Ty)
bool isPosZero() const
Definition: APFloat.h:1451
Class for arbitrary precision integers.
Definition: APInt.h:78
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1520
Class to represent array types.
Definition: DerivedTypes.h:395
uint64_t getNumElements() const
Definition: DerivedTypes.h:407
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Type * getElementType() const
Definition: DerivedTypes.h:408
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:271
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:157
static ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
Definition: Constants.cpp:1826
static Constant * getAnon(ArrayRef< Constant * > V, bool Packed=false)
Return an anonymous struct that has the specified elements.
Definition: Constants.h:480
static Constant * getSplat(ElementCount EC, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
Definition: Constants.cpp:1472
This is an important base class in LLVM.
Definition: Constant.h:42
const APInt & getUniqueInteger() const
If C is a constant integer then return its value, otherwise C must be a vector of constant integers,...
Definition: Constants.cpp:1771
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition: Constants.cpp:90
This class represents an Operation in the Expression.
Class to represent fixed width SIMD vectors.
Definition: DerivedTypes.h:563
static FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition: Type.cpp:791
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:369
MaybeAlign getAlign() const
Returns the alignment of the given variable or function.
Definition: GlobalObject.h:79
MDNode * getMetadata(unsigned KindID) const
Get the current metadata attachments for the given kind, if any.
Definition: Value.h:565
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:656
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:52
Class to represent integer types.
Definition: DerivedTypes.h:42
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:311
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
Definition: LowLevelType.h:42
static constexpr LLT pointer(unsigned AddressSpace, unsigned SizeInBits)
Get a low-level pointer in the given address space.
Definition: LowLevelType.h:57
static constexpr LLT fixed_vector(unsigned NumElements, unsigned ScalarSizeInBits)
Get a low-level fixed-width vector of some number of elements and element width.
Definition: LowLevelType.h:100
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode.
Definition: MCInstrInfo.h:63
Metadata node.
Definition: Metadata.h:1069
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
Helper class to build MachineInstr.
void setInsertPt(MachineBasicBlock &MBB, MachineBasicBlock::iterator II)
Set the insertion point before the specified position.
LLVMContext & getContext() const
const TargetInstrInfo & getTII()
MachineBasicBlock::iterator getInsertPt()
Current insertion point for new instructions.
MachineInstrBuilder buildSplatBuildVector(const DstOp &Res, const SrcOp &Src)
Build and insert Res = G_BUILD_VECTOR with Src replicated to fill the number of elements.
MachineInstrBuilder buildInstr(unsigned Opcode)
Build and insert <empty> = Opcode <empty>.
MachineFunction & getMF()
Getter for the function we currently build.
const MachineBasicBlock & getMBB() const
Getter for the basic block we currently build.
const DebugLoc & getDebugLoc()
Get the current instruction's debug location.
MachineRegisterInfo * getMRI()
Getter for MRI.
MachineInstrBuilder buildCopy(const DstOp &Res, const SrcOp &Op)
Build and insert Res = COPY Op.
virtual MachineInstrBuilder buildConstant(const DstOp &Res, const ConstantInt &Val)
Build and insert Res = G_CONSTANT Val.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addUse(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & addDef(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register definition operand.
Representation of each machine instruction.
Definition: MachineInstr.h:69
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:575
iterator_range< mop_iterator > uses()
Returns a range that includes all operands which may be register uses.
Definition: MachineInstr.h:739
iterator_range< mop_iterator > defs()
Returns a range over all explicit operands that are register definitions.
Definition: MachineInstr.h:728
const MachineFunction * getMF() const
Return the function that contains the basic block that this instruction belongs to.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:585
const ConstantInt * getCImm() const
int64_t getImm() const
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
void setRegClass(Register Reg, const TargetRegisterClass *RC)
setRegClass - Set the register class of the specified virtual register.
Register createGenericVirtualRegister(LLT Ty, StringRef Name="")
Create and return a new generic virtual register with low-level type Ty.
MachineInstr * getUniqueVRegDef(Register Reg) const
getUniqueVRegDef - Return the unique machine instr that defines the specified virtual register or nul...
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1878
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
constexpr bool isValid() const
Definition: Register.h:116
void add(const Type *Ty, const MachineFunction *MF, Register R)
Register find(const Type *Ty, const MachineFunction *MF)
const SPIRVDuplicatesTracker< Type > * getTypes()
SPIRVType * getOrCreateOpTypePipe(MachineIRBuilder &MIRBuilder, SPIRV::AccessQualifier::AccessQualifier AccQual)
unsigned getNumScalarOrVectorTotalBitWidth(const SPIRVType *Type) const
SPIRVType * getSPIRVTypeForVReg(Register VReg, const MachineFunction *MF=nullptr) const
Register getOrCreateConstInt(uint64_t Val, MachineInstr &I, SPIRVType *SpvType, const SPIRVInstrInfo &TII, bool ZeroAsNull=true)
Register getOrCreateGlobalVariableWithBinding(const SPIRVType *VarType, uint32_t Set, uint32_t Binding, MachineIRBuilder &MIRBuilder)
SPIRVType * assignFloatTypeToVReg(unsigned BitWidth, Register VReg, MachineInstr &I, const SPIRVInstrInfo &TII)
void assignSPIRVTypeToVReg(SPIRVType *Type, Register VReg, const MachineFunction &MF)
SPIRVType * assignVectTypeToVReg(SPIRVType *BaseType, unsigned NumElements, Register VReg, MachineInstr &I, const SPIRVInstrInfo &TII)
Register getOrCreateUndef(MachineInstr &I, SPIRVType *SpvType, const SPIRVInstrInfo &TII)
SPIRVType * getOrCreateSPIRVBoolType(MachineIRBuilder &MIRBuilder)
Register getOrCreateConsIntVector(uint64_t Val, MachineIRBuilder &MIRBuilder, SPIRVType *SpvType, bool EmitIR=true)
const Type * getTypeForSPIRVType(const SPIRVType *Ty) const
Register buildConstantSampler(Register Res, unsigned AddrMode, unsigned Param, unsigned FilerMode, MachineIRBuilder &MIRBuilder, SPIRVType *SpvType)
bool isBitcastCompatible(const SPIRVType *Type1, const SPIRVType *Type2) const
unsigned getScalarOrVectorComponentCount(Register VReg) const
SPIRVType * getOrCreateSPIRVFloatType(unsigned BitWidth, MachineInstr &I, const SPIRVInstrInfo &TII)
SPIRVType * getOrCreateOpTypeImage(MachineIRBuilder &MIRBuilder, SPIRVType *SampledType, SPIRV::Dim::Dim Dim, uint32_t Depth, uint32_t Arrayed, uint32_t Multisampled, uint32_t Sampled, SPIRV::ImageFormat::ImageFormat ImageFormat, SPIRV::AccessQualifier::AccessQualifier AccQual)
bool isScalarOrVectorSigned(const SPIRVType *Type) const
SPIRVGlobalRegistry(unsigned PointerSize)
SPIRVType * getOrCreateOpTypeByOpcode(const Type *Ty, MachineIRBuilder &MIRBuilder, unsigned Opcode)
Register buildConstantFP(APFloat Val, MachineIRBuilder &MIRBuilder, SPIRVType *SpvType=nullptr)
SPIRVType * getPointeeType(SPIRVType *PtrType)
void invalidateMachineInstr(MachineInstr *MI)
Register getSPIRVTypeID(const SPIRVType *SpirvType) const
SPIRVType * getOrCreateSPIRVType(const Type *Type, MachineIRBuilder &MIRBuilder, SPIRV::AccessQualifier::AccessQualifier AQ=SPIRV::AccessQualifier::ReadWrite, bool EmitIR=true)
bool isScalarOfType(Register VReg, unsigned TypeOpcode) const
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)
SPIRVType * assignIntTypeToVReg(unsigned BitWidth, Register VReg, MachineInstr &I, const SPIRVInstrInfo &TII)
unsigned getPointeeTypeOp(Register PtrReg)
SPIRVType * getOrCreateOpTypeSampledImage(SPIRVType *ImageType, MachineIRBuilder &MIRBuilder)
SPIRVType * getOrCreateSPIRVTypeByName(StringRef TypeStr, MachineIRBuilder &MIRBuilder, SPIRV::StorageClass::StorageClass SC=SPIRV::StorageClass::Function, SPIRV::AccessQualifier::AccessQualifier AQ=SPIRV::AccessQualifier::ReadWrite)
SPIRVType * getScalarOrVectorComponentType(Register VReg) const
SPIRVType * assignTypeToVReg(const Type *Type, Register VReg, MachineIRBuilder &MIRBuilder, SPIRV::AccessQualifier::AccessQualifier AQ=SPIRV::AccessQualifier::ReadWrite, bool EmitIR=true)
SPIRVType * getOrCreateOpTypeFunctionWithArgs(const Type *Ty, SPIRVType *RetType, const SmallVectorImpl< SPIRVType * > &ArgTypes, MachineIRBuilder &MIRBuilder)
const TargetRegisterClass * getRegClass(SPIRVType *SpvType) const
bool isScalarOrVectorOfType(Register VReg, unsigned TypeOpcode) const
Register getOrCreateConstIntArray(uint64_t Val, size_t Num, MachineInstr &I, SPIRVType *SpvType, const SPIRVInstrInfo &TII)
Register getOrCreateConstVector(uint64_t Val, MachineInstr &I, SPIRVType *SpvType, const SPIRVInstrInfo &TII, bool ZeroAsNull=true)
SPIRVType * getOrCreateOpTypeDeviceEvent(MachineIRBuilder &MIRBuilder)
SPIRVType * getOrCreateSPIRVPointerType(SPIRVType *BaseType, MachineIRBuilder &MIRBuilder, SPIRV::StorageClass::StorageClass SClass=SPIRV::StorageClass::Function)
SPIRVType * getOrCreateOpTypeCoopMatr(MachineIRBuilder &MIRBuilder, const TargetExtType *ExtensionType, const SPIRVType *ElemType, uint32_t Scope, uint32_t Rows, uint32_t Columns, uint32_t Use)
SPIRVType * getResultType(Register VReg)
SPIRVType * getOrCreateSPIRVVectorType(SPIRVType *BaseType, unsigned NumElements, MachineIRBuilder &MIRBuilder)
SPIRVType * getOrCreateSPIRVIntegerType(unsigned BitWidth, MachineIRBuilder &MIRBuilder)
LLT getRegType(SPIRVType *SpvType) const
SPIRVType * getOrCreateSPIRVArrayType(SPIRVType *BaseType, unsigned NumElements, MachineInstr &I, const SPIRVInstrInfo &TII)
SPIRV::StorageClass::StorageClass getPointerStorageClass(Register VReg) const
SPIRVType * getOrCreateOpTypeSampler(MachineIRBuilder &MIRBuilder)
Register getOrCreateConstFP(APFloat Val, MachineInstr &I, SPIRVType *SpvType, const SPIRVInstrInfo &TII, bool ZeroAsNull=true)
Register getOrCreateConstNullPtr(MachineIRBuilder &MIRBuilder, SPIRVType *SpvType)
unsigned getScalarOrVectorBitWidth(const SPIRVType *Type) const
Register buildConstantInt(uint64_t Val, MachineIRBuilder &MIRBuilder, SPIRVType *SpvType, bool EmitIR=true, bool ZeroAsNull=true)
const SPIRVType * retrieveScalarOrVectorIntType(const SPIRVType *Type) const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:573
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
bool consume_back(StringRef Suffix)
Returns true if this StringRef has the given suffix and removes that suffix.
Definition: StringRef.h:655
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:470
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:229
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:571
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:265
bool consume_front(StringRef Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
Definition: StringRef.h:635
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:297
Class to represent struct types.
Definition: DerivedTypes.h:218
ArrayRef< Type * > elements() const
Definition: DerivedTypes.h:357
bool isPacked() const
Definition: DerivedTypes.h:284
bool hasName() const
Return true if this is a named struct that has a non-empty name.
Definition: DerivedTypes.h:326
StringRef getName() const
Return the name for this struct type if it has an identity.
Definition: Type.cpp:689
Class to represent target extensions types, which are generally unintrospectable from target-independ...
Definition: DerivedTypes.h:744
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
static Type * getHalfTy(LLVMContext &C)
static Type * getDoubleTy(LLVMContext &C)
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:270
bool isArrayTy() const
True if this is an instance of ArrayType.
Definition: Type.h:261
Type * getArrayElementType() const
Definition: Type.h:411
uint64_t getArrayNumElements() const
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:128
static IntegerType * getInt8Ty(LLVMContext &C)
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
Definition: Type.h:184
static Type * getFloatTy(LLVMContext &C)
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:237
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
bool isVoidTy() const
Return true if this is 'void'.
Definition: Type.h:139
static TypedPointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
'undef' values are things that do not have specified contents.
Definition: Constants.h:1412
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1859
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
bool hasName() const
Definition: Value.h:261
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
ElementCount getElementCount() const
Return an ElementCount instance to represent the (possibly scalable) number of elements in the vector...
Definition: DerivedTypes.h:665
Type * getElementType() const
Definition: DerivedTypes.h:460
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ SC
CHAIN = SC CHAIN, Imm128 - System call.
SpecialTypeDescriptor make_descr_pipe(uint8_t AQ)
SpecialTypeDescriptor make_descr_sampler()
std::tuple< const Type *, unsigned, unsigned > SpecialTypeDescriptor
SpecialTypeDescriptor make_descr_event()
SpecialTypeDescriptor make_descr_image(const Type *SampledTy, unsigned Dim, unsigned Depth, unsigned Arrayed, unsigned MS, unsigned Sampled, unsigned ImageFormat, unsigned AQ=0)
TargetExtType * parseBuiltinTypeNameToTargetExtType(std::string TypeName, LLVMContext &Context)
Translates a string representing a SPIR-V or OpenCL builtin type to a TargetExtType that can be furth...
SPIRVType * lowerBuiltinType(const Type *OpaqueType, SPIRV::AccessQualifier::AccessQualifier AccessQual, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
SpecialTypeDescriptor make_descr_sampled_image(const Type *SampledTy, const MachineInstr *ImageTy)
Reg
All possible values of the reg field in the ModR/M byte.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void buildOpName(Register Target, const StringRef &Name, MachineIRBuilder &MIRBuilder)
Definition: SPIRVUtils.cpp:103
bool isTypedPointerWrapper(const TargetExtType *ExtTy)
Definition: SPIRVUtils.h:292
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
unsigned getPointerAddressSpace(const Type *T)
Definition: SPIRVUtils.h:256
void addNumImm(const APInt &Imm, MachineInstrBuilder &MIB)
Definition: SPIRVUtils.cpp:83
bool 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:155
constexpr unsigned storageClassToAddressSpace(SPIRV::StorageClass::StorageClass SC)
Definition: SPIRVUtils.h:161
bool getSpirvBuiltInIdByName(llvm::StringRef Name, SPIRV::BuiltIn::BuiltIn &BI)
bool isTypedPointerTy(const Type *T)
Definition: SPIRVUtils.h:240
void buildOpDecorate(Register Reg, MachineIRBuilder &MIRBuilder, SPIRV::Decoration::Decoration Dec, const std::vector< uint32_t > &DecArgs, StringRef StrImm)
Definition: SPIRVUtils.cpp:130
Type * toTypedPointer(Type *Ty)
Definition: SPIRVUtils.h:347
bool isSpecialOpaqueType(const Type *Ty)
Definition: SPIRVUtils.cpp:436
bool isPointerTy(const Type *T)
Definition: SPIRVUtils.h:250
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
MachineBasicBlock::iterator getInsertPtValidEnd(MachineBasicBlock *MBB)
Definition: SPIRVUtils.cpp:197
const Type * unifyPtrType(const Type *Ty)
Definition: SPIRVUtils.h:374
SPIRV::StorageClass::StorageClass addressSpaceToStorageClass(unsigned AddrSpace, const SPIRVSubtarget &STI)
Definition: SPIRVUtils.cpp:211
Type * parseBasicTypeName(StringRef &TypeName, LLVMContext &Ctx)
Definition: SPIRVUtils.cpp:459
DWARFExpression::Operation Op
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:217
bool hasBuiltinTypePrefix(StringRef Name)
Definition: SPIRVUtils.cpp:429
bool isPointerTyOrWrapper(const Type *Ty)
Definition: SPIRVUtils.h:299
void addStringImm(const StringRef &Str, MCInst &Inst)
Definition: SPIRVUtils.cpp:54
MachineInstr * getVRegDef(MachineRegisterInfo &MRI, Register Reg)
Definition: SPIRVUtils.cpp:704
void buildOpSpirvDecorations(Register Reg, MachineIRBuilder &MIRBuilder, const MDNode *GVarMD)
Definition: SPIRVUtils.cpp:149
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition: Alignment.h:85
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
Definition: Alignment.h:141