LLVM 19.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
29using namespace llvm;
31 : PointerSize(PointerSize), Bound(0) {}
32
34 Register VReg,
36 const SPIRVInstrInfo &TII) {
38 assignSPIRVTypeToVReg(SpirvType, VReg, *CurMF);
39 return SpirvType;
40}
41
45 const SPIRVInstrInfo &TII) {
47 assignSPIRVTypeToVReg(SpirvType, VReg, *CurMF);
48 return SpirvType;
49}
50
52 SPIRVType *BaseType, unsigned NumElements, Register VReg, MachineInstr &I,
53 const SPIRVInstrInfo &TII) {
54 SPIRVType *SpirvType =
56 assignSPIRVTypeToVReg(SpirvType, VReg, *CurMF);
57 return SpirvType;
58}
59
61 const Type *Type, Register VReg, MachineIRBuilder &MIRBuilder,
62 SPIRV::AccessQualifier::AccessQualifier AccessQual, bool EmitIR) {
63 SPIRVType *SpirvType =
64 getOrCreateSPIRVType(Type, MIRBuilder, AccessQual, EmitIR);
65 assignSPIRVTypeToVReg(SpirvType, VReg, MIRBuilder.getMF());
66 return SpirvType;
67}
68
70 Register VReg,
71 MachineFunction &MF) {
72 VRegToTypeMap[&MF][VReg] = SpirvType;
73}
74
76 auto &MRI = MIRBuilder.getMF().getRegInfo();
77 auto Res = MRI.createGenericVirtualRegister(LLT::scalar(32));
78 MRI.setRegClass(Res, &SPIRV::TYPERegClass);
79 return Res;
80}
81
83 auto Res = MRI.createGenericVirtualRegister(LLT::scalar(32));
84 MRI.setRegClass(Res, &SPIRV::TYPERegClass);
85 return Res;
86}
87
88SPIRVType *SPIRVGlobalRegistry::getOpTypeBool(MachineIRBuilder &MIRBuilder) {
89 return MIRBuilder.buildInstr(SPIRV::OpTypeBool)
90 .addDef(createTypeVReg(MIRBuilder));
91}
92
93SPIRVType *SPIRVGlobalRegistry::getOpTypeInt(uint32_t Width,
94 MachineIRBuilder &MIRBuilder,
95 bool IsSigned) {
96 assert(Width <= 64 && "Unsupported integer width!");
97 const SPIRVSubtarget &ST =
98 cast<SPIRVSubtarget>(MIRBuilder.getMF().getSubtarget());
99 if (ST.canUseExtension(
100 SPIRV::Extension::SPV_INTEL_arbitrary_precision_integers)) {
101 MIRBuilder.buildInstr(SPIRV::OpExtension)
102 .addImm(SPIRV::Extension::SPV_INTEL_arbitrary_precision_integers);
103 MIRBuilder.buildInstr(SPIRV::OpCapability)
104 .addImm(SPIRV::Capability::ArbitraryPrecisionIntegersINTEL);
105 } else if (Width <= 8)
106 Width = 8;
107 else if (Width <= 16)
108 Width = 16;
109 else if (Width <= 32)
110 Width = 32;
111 else if (Width <= 64)
112 Width = 64;
113
114 auto MIB = MIRBuilder.buildInstr(SPIRV::OpTypeInt)
115 .addDef(createTypeVReg(MIRBuilder))
116 .addImm(Width)
117 .addImm(IsSigned ? 1 : 0);
118 return MIB;
119}
120
121SPIRVType *SPIRVGlobalRegistry::getOpTypeFloat(uint32_t Width,
122 MachineIRBuilder &MIRBuilder) {
123 auto MIB = MIRBuilder.buildInstr(SPIRV::OpTypeFloat)
124 .addDef(createTypeVReg(MIRBuilder))
125 .addImm(Width);
126 return MIB;
127}
128
129SPIRVType *SPIRVGlobalRegistry::getOpTypeVoid(MachineIRBuilder &MIRBuilder) {
130 return MIRBuilder.buildInstr(SPIRV::OpTypeVoid)
131 .addDef(createTypeVReg(MIRBuilder));
132}
133
134SPIRVType *SPIRVGlobalRegistry::getOpTypeVector(uint32_t NumElems,
135 SPIRVType *ElemType,
136 MachineIRBuilder &MIRBuilder) {
137 auto EleOpc = ElemType->getOpcode();
138 (void)EleOpc;
139 assert((EleOpc == SPIRV::OpTypeInt || EleOpc == SPIRV::OpTypeFloat ||
140 EleOpc == SPIRV::OpTypeBool) &&
141 "Invalid vector element type");
142
143 auto MIB = MIRBuilder.buildInstr(SPIRV::OpTypeVector)
144 .addDef(createTypeVReg(MIRBuilder))
145 .addUse(getSPIRVTypeID(ElemType))
146 .addImm(NumElems);
147 return MIB;
148}
149
150std::tuple<Register, ConstantInt *, bool>
151SPIRVGlobalRegistry::getOrCreateConstIntReg(uint64_t Val, SPIRVType *SpvType,
152 MachineIRBuilder *MIRBuilder,
154 const SPIRVInstrInfo *TII) {
155 const IntegerType *LLVMIntTy;
156 if (SpvType)
157 LLVMIntTy = cast<IntegerType>(getTypeForSPIRVType(SpvType));
158 else
160 bool NewInstr = false;
161 // Find a constant in DT or build a new one.
162 ConstantInt *CI = ConstantInt::get(const_cast<IntegerType *>(LLVMIntTy), Val);
163 Register Res = DT.find(CI, CurMF);
164 if (!Res.isValid()) {
165 unsigned BitWidth = SpvType ? getScalarOrVectorBitWidth(SpvType) : 32;
166 // TODO: handle cases where the type is not 32bit wide
167 // TODO: https://github.com/llvm/llvm-project/issues/88129
168 LLT LLTy = LLT::scalar(32);
170 CurMF->getRegInfo().setRegClass(Res, &SPIRV::IDRegClass);
171 if (MIRBuilder)
172 assignTypeToVReg(LLVMIntTy, Res, *MIRBuilder);
173 else
175 DT.add(CI, CurMF, Res);
176 NewInstr = true;
177 }
178 return std::make_tuple(Res, CI, NewInstr);
179}
180
181std::tuple<Register, ConstantFP *, bool, unsigned>
182SPIRVGlobalRegistry::getOrCreateConstFloatReg(APFloat Val, SPIRVType *SpvType,
183 MachineIRBuilder *MIRBuilder,
185 const SPIRVInstrInfo *TII) {
186 const Type *LLVMFloatTy;
188 unsigned BitWidth = 32;
189 if (SpvType)
190 LLVMFloatTy = getTypeForSPIRVType(SpvType);
191 else {
192 LLVMFloatTy = Type::getFloatTy(Ctx);
193 if (MIRBuilder)
194 SpvType = getOrCreateSPIRVType(LLVMFloatTy, *MIRBuilder);
195 }
196 bool NewInstr = false;
197 // Find a constant in DT or build a new one.
198 auto *const CI = ConstantFP::get(Ctx, Val);
199 Register Res = DT.find(CI, CurMF);
200 if (!Res.isValid()) {
201 if (SpvType)
203 // TODO: handle cases where the type is not 32bit wide
204 // TODO: https://github.com/llvm/llvm-project/issues/88129
205 LLT LLTy = LLT::scalar(32);
207 CurMF->getRegInfo().setRegClass(Res, &SPIRV::IDRegClass);
208 if (MIRBuilder)
209 assignTypeToVReg(LLVMFloatTy, Res, *MIRBuilder);
210 else
212 DT.add(CI, CurMF, Res);
213 NewInstr = true;
214 }
215 return std::make_tuple(Res, CI, NewInstr, BitWidth);
216}
217
219 SPIRVType *SpvType,
220 const SPIRVInstrInfo &TII,
221 bool ZeroAsNull) {
222 assert(SpvType);
223 ConstantFP *CI;
224 Register Res;
225 bool New;
226 unsigned BitWidth;
227 std::tie(Res, CI, New, BitWidth) =
228 getOrCreateConstFloatReg(Val, SpvType, nullptr, &I, &TII);
229 // If we have found Res register which is defined by the passed G_CONSTANT
230 // machine instruction, a new constant instruction should be created.
231 if (!New && (!I.getOperand(0).isReg() || Res != I.getOperand(0).getReg()))
232 return Res;
234 MachineBasicBlock &BB = *I.getParent();
235 // In OpenCL OpConstantNull - Scalar floating point: +0.0 (all bits 0)
236 if (Val.isPosZero() && ZeroAsNull) {
237 MIB = BuildMI(BB, I, I.getDebugLoc(), TII.get(SPIRV::OpConstantNull))
238 .addDef(Res)
239 .addUse(getSPIRVTypeID(SpvType));
240 } else {
241 MIB = BuildMI(BB, I, I.getDebugLoc(), TII.get(SPIRV::OpConstantF))
242 .addDef(Res)
243 .addUse(getSPIRVTypeID(SpvType));
244 addNumImm(
246 MIB);
247 }
248 const auto &ST = CurMF->getSubtarget();
249 constrainSelectedInstRegOperands(*MIB, *ST.getInstrInfo(),
250 *ST.getRegisterInfo(), *ST.getRegBankInfo());
251 return Res;
252}
253
255 SPIRVType *SpvType,
256 const SPIRVInstrInfo &TII,
257 bool ZeroAsNull) {
258 assert(SpvType);
259 ConstantInt *CI;
260 Register Res;
261 bool New;
262 std::tie(Res, CI, New) =
263 getOrCreateConstIntReg(Val, SpvType, nullptr, &I, &TII);
264 // If we have found Res register which is defined by the passed G_CONSTANT
265 // machine instruction, a new constant instruction should be created.
266 if (!New && (!I.getOperand(0).isReg() || Res != I.getOperand(0).getReg()))
267 return Res;
269 MachineBasicBlock &BB = *I.getParent();
270 if (Val || !ZeroAsNull) {
271 MIB = BuildMI(BB, I, I.getDebugLoc(), TII.get(SPIRV::OpConstantI))
272 .addDef(Res)
273 .addUse(getSPIRVTypeID(SpvType));
274 addNumImm(APInt(getScalarOrVectorBitWidth(SpvType), Val), MIB);
275 } else {
276 MIB = BuildMI(BB, I, I.getDebugLoc(), TII.get(SPIRV::OpConstantNull))
277 .addDef(Res)
278 .addUse(getSPIRVTypeID(SpvType));
279 }
280 const auto &ST = CurMF->getSubtarget();
281 constrainSelectedInstRegOperands(*MIB, *ST.getInstrInfo(),
282 *ST.getRegisterInfo(), *ST.getRegBankInfo());
283 return Res;
284}
285
287 MachineIRBuilder &MIRBuilder,
288 SPIRVType *SpvType,
289 bool EmitIR) {
290 auto &MF = MIRBuilder.getMF();
291 const IntegerType *LLVMIntTy;
292 if (SpvType)
293 LLVMIntTy = cast<IntegerType>(getTypeForSPIRVType(SpvType));
294 else
295 LLVMIntTy = IntegerType::getInt32Ty(MF.getFunction().getContext());
296 // Find a constant in DT or build a new one.
297 const auto ConstInt =
298 ConstantInt::get(const_cast<IntegerType *>(LLVMIntTy), Val);
299 Register Res = DT.find(ConstInt, &MF);
300 if (!Res.isValid()) {
301 unsigned BitWidth = SpvType ? getScalarOrVectorBitWidth(SpvType) : 32;
302 LLT LLTy = LLT::scalar(EmitIR ? BitWidth : 32);
303 Res = MF.getRegInfo().createGenericVirtualRegister(LLTy);
304 MF.getRegInfo().setRegClass(Res, &SPIRV::IDRegClass);
305 assignTypeToVReg(LLVMIntTy, Res, MIRBuilder,
306 SPIRV::AccessQualifier::ReadWrite, EmitIR);
307 DT.add(ConstInt, &MIRBuilder.getMF(), Res);
308 if (EmitIR) {
309 MIRBuilder.buildConstant(Res, *ConstInt);
310 } else {
312 if (Val) {
313 assert(SpvType);
314 MIB = MIRBuilder.buildInstr(SPIRV::OpConstantI)
315 .addDef(Res)
316 .addUse(getSPIRVTypeID(SpvType));
317 addNumImm(APInt(BitWidth, Val), MIB);
318 } else {
319 assert(SpvType);
320 MIB = MIRBuilder.buildInstr(SPIRV::OpConstantNull)
321 .addDef(Res)
322 .addUse(getSPIRVTypeID(SpvType));
323 }
324 const auto &Subtarget = CurMF->getSubtarget();
325 constrainSelectedInstRegOperands(*MIB, *Subtarget.getInstrInfo(),
326 *Subtarget.getRegisterInfo(),
327 *Subtarget.getRegBankInfo());
328 }
329 }
330 return Res;
331}
332
334 MachineIRBuilder &MIRBuilder,
335 SPIRVType *SpvType) {
336 auto &MF = MIRBuilder.getMF();
337 auto &Ctx = MF.getFunction().getContext();
338 if (!SpvType) {
339 const Type *LLVMFPTy = Type::getFloatTy(Ctx);
340 SpvType = getOrCreateSPIRVType(LLVMFPTy, MIRBuilder);
341 }
342 // Find a constant in DT or build a new one.
343 const auto ConstFP = ConstantFP::get(Ctx, Val);
344 Register Res = DT.find(ConstFP, &MF);
345 if (!Res.isValid()) {
346 Res = MF.getRegInfo().createGenericVirtualRegister(LLT::scalar(32));
347 MF.getRegInfo().setRegClass(Res, &SPIRV::IDRegClass);
348 assignSPIRVTypeToVReg(SpvType, Res, MF);
349 DT.add(ConstFP, &MF, Res);
350
352 MIB = MIRBuilder.buildInstr(SPIRV::OpConstantF)
353 .addDef(Res)
354 .addUse(getSPIRVTypeID(SpvType));
355 addNumImm(ConstFP->getValueAPF().bitcastToAPInt(), MIB);
356 }
357
358 return Res;
359}
360
361Register SPIRVGlobalRegistry::getOrCreateBaseRegister(Constant *Val,
363 SPIRVType *SpvType,
364 const SPIRVInstrInfo &TII,
365 unsigned BitWidth) {
366 SPIRVType *Type = SpvType;
367 if (SpvType->getOpcode() == SPIRV::OpTypeVector ||
368 SpvType->getOpcode() == SPIRV::OpTypeArray) {
369 auto EleTypeReg = SpvType->getOperand(1).getReg();
370 Type = getSPIRVTypeForVReg(EleTypeReg);
371 }
372 if (Type->getOpcode() == SPIRV::OpTypeFloat) {
374 return getOrCreateConstFP(dyn_cast<ConstantFP>(Val)->getValue(), I,
375 SpvBaseType, TII);
376 }
377 assert(Type->getOpcode() == SPIRV::OpTypeInt);
380 SpvBaseType, TII);
381}
382
383Register SPIRVGlobalRegistry::getOrCreateCompositeOrNull(
384 Constant *Val, MachineInstr &I, SPIRVType *SpvType,
385 const SPIRVInstrInfo &TII, Constant *CA, unsigned BitWidth,
386 unsigned ElemCnt, bool ZeroAsNull) {
387 // Find a constant vector in DT or build a new one.
388 Register Res = DT.find(CA, CurMF);
389 // If no values are attached, the composite is null constant.
390 bool IsNull = Val->isNullValue() && ZeroAsNull;
391 if (!Res.isValid()) {
392 // SpvScalConst should be created before SpvVecConst to avoid undefined ID
393 // error on validation.
394 // TODO: can moved below once sorting of types/consts/defs is implemented.
395 Register SpvScalConst;
396 if (!IsNull)
397 SpvScalConst = getOrCreateBaseRegister(Val, I, SpvType, TII, BitWidth);
398
399 // TODO: handle cases where the type is not 32bit wide
400 // TODO: https://github.com/llvm/llvm-project/issues/88129
401 LLT LLTy = LLT::scalar(32);
402 Register SpvVecConst =
404 CurMF->getRegInfo().setRegClass(SpvVecConst, &SPIRV::IDRegClass);
405 assignSPIRVTypeToVReg(SpvType, SpvVecConst, *CurMF);
406 DT.add(CA, CurMF, SpvVecConst);
408 MachineBasicBlock &BB = *I.getParent();
409 if (!IsNull) {
410 MIB = BuildMI(BB, I, I.getDebugLoc(), TII.get(SPIRV::OpConstantComposite))
411 .addDef(SpvVecConst)
412 .addUse(getSPIRVTypeID(SpvType));
413 for (unsigned i = 0; i < ElemCnt; ++i)
414 MIB.addUse(SpvScalConst);
415 } else {
416 MIB = BuildMI(BB, I, I.getDebugLoc(), TII.get(SPIRV::OpConstantNull))
417 .addDef(SpvVecConst)
418 .addUse(getSPIRVTypeID(SpvType));
419 }
420 const auto &Subtarget = CurMF->getSubtarget();
421 constrainSelectedInstRegOperands(*MIB, *Subtarget.getInstrInfo(),
422 *Subtarget.getRegisterInfo(),
423 *Subtarget.getRegBankInfo());
424 return SpvVecConst;
425 }
426 return Res;
427}
428
431 SPIRVType *SpvType,
432 const SPIRVInstrInfo &TII,
433 bool ZeroAsNull) {
434 const Type *LLVMTy = getTypeForSPIRVType(SpvType);
435 assert(LLVMTy->isVectorTy());
436 const FixedVectorType *LLVMVecTy = cast<FixedVectorType>(LLVMTy);
437 Type *LLVMBaseTy = LLVMVecTy->getElementType();
438 assert(LLVMBaseTy->isIntegerTy());
439 auto *ConstVal = ConstantInt::get(LLVMBaseTy, Val);
440 auto *ConstVec =
441 ConstantVector::getSplat(LLVMVecTy->getElementCount(), ConstVal);
442 unsigned BW = getScalarOrVectorBitWidth(SpvType);
443 return getOrCreateCompositeOrNull(ConstVal, I, SpvType, TII, ConstVec, BW,
444 SpvType->getOperand(2).getImm(),
445 ZeroAsNull);
446}
447
450 SPIRVType *SpvType,
451 const SPIRVInstrInfo &TII,
452 bool ZeroAsNull) {
453 const Type *LLVMTy = getTypeForSPIRVType(SpvType);
454 assert(LLVMTy->isVectorTy());
455 const FixedVectorType *LLVMVecTy = cast<FixedVectorType>(LLVMTy);
456 Type *LLVMBaseTy = LLVMVecTy->getElementType();
457 assert(LLVMBaseTy->isFloatingPointTy());
458 auto *ConstVal = ConstantFP::get(LLVMBaseTy, Val);
459 auto *ConstVec =
460 ConstantVector::getSplat(LLVMVecTy->getElementCount(), ConstVal);
461 unsigned BW = getScalarOrVectorBitWidth(SpvType);
462 return getOrCreateCompositeOrNull(ConstVal, I, SpvType, TII, ConstVec, BW,
463 SpvType->getOperand(2).getImm(),
464 ZeroAsNull);
465}
466
469 SPIRVType *SpvType,
470 const SPIRVInstrInfo &TII) {
471 const Type *LLVMTy = getTypeForSPIRVType(SpvType);
472 assert(LLVMTy->isArrayTy());
473 const ArrayType *LLVMArrTy = cast<ArrayType>(LLVMTy);
474 Type *LLVMBaseTy = LLVMArrTy->getElementType();
475 auto *ConstInt = ConstantInt::get(LLVMBaseTy, Val);
476 auto *ConstArr =
477 ConstantArray::get(const_cast<ArrayType *>(LLVMArrTy), {ConstInt});
478 SPIRVType *SpvBaseTy = getSPIRVTypeForVReg(SpvType->getOperand(1).getReg());
479 unsigned BW = getScalarOrVectorBitWidth(SpvBaseTy);
480 return getOrCreateCompositeOrNull(ConstInt, I, SpvType, TII, ConstArr, BW,
481 LLVMArrTy->getNumElements());
482}
483
484Register SPIRVGlobalRegistry::getOrCreateIntCompositeOrNull(
485 uint64_t Val, MachineIRBuilder &MIRBuilder, SPIRVType *SpvType, bool EmitIR,
486 Constant *CA, unsigned BitWidth, unsigned ElemCnt) {
487 Register Res = DT.find(CA, CurMF);
488 if (!Res.isValid()) {
489 Register SpvScalConst;
490 if (Val || EmitIR) {
491 SPIRVType *SpvBaseType =
493 SpvScalConst = buildConstantInt(Val, MIRBuilder, SpvBaseType, EmitIR);
494 }
495 LLT LLTy = EmitIR ? LLT::fixed_vector(ElemCnt, BitWidth) : LLT::scalar(32);
496 Register SpvVecConst =
498 CurMF->getRegInfo().setRegClass(SpvVecConst, &SPIRV::IDRegClass);
499 assignSPIRVTypeToVReg(SpvType, SpvVecConst, *CurMF);
500 DT.add(CA, CurMF, SpvVecConst);
501 if (EmitIR) {
502 MIRBuilder.buildSplatVector(SpvVecConst, SpvScalConst);
503 } else {
504 if (Val) {
505 auto MIB = MIRBuilder.buildInstr(SPIRV::OpConstantComposite)
506 .addDef(SpvVecConst)
507 .addUse(getSPIRVTypeID(SpvType));
508 for (unsigned i = 0; i < ElemCnt; ++i)
509 MIB.addUse(SpvScalConst);
510 } else {
511 MIRBuilder.buildInstr(SPIRV::OpConstantNull)
512 .addDef(SpvVecConst)
513 .addUse(getSPIRVTypeID(SpvType));
514 }
515 }
516 return SpvVecConst;
517 }
518 return Res;
519}
520
523 MachineIRBuilder &MIRBuilder,
524 SPIRVType *SpvType, bool EmitIR) {
525 const Type *LLVMTy = getTypeForSPIRVType(SpvType);
526 assert(LLVMTy->isVectorTy());
527 const FixedVectorType *LLVMVecTy = cast<FixedVectorType>(LLVMTy);
528 Type *LLVMBaseTy = LLVMVecTy->getElementType();
529 const auto ConstInt = ConstantInt::get(LLVMBaseTy, Val);
530 auto ConstVec =
531 ConstantVector::getSplat(LLVMVecTy->getElementCount(), ConstInt);
532 unsigned BW = getScalarOrVectorBitWidth(SpvType);
533 return getOrCreateIntCompositeOrNull(Val, MIRBuilder, SpvType, EmitIR,
534 ConstVec, BW,
535 SpvType->getOperand(2).getImm());
536}
537
540 MachineIRBuilder &MIRBuilder,
541 SPIRVType *SpvType, bool EmitIR) {
542 const Type *LLVMTy = getTypeForSPIRVType(SpvType);
543 assert(LLVMTy->isArrayTy());
544 const ArrayType *LLVMArrTy = cast<ArrayType>(LLVMTy);
545 Type *LLVMBaseTy = LLVMArrTy->getElementType();
546 const auto ConstInt = ConstantInt::get(LLVMBaseTy, Val);
547 auto ConstArr =
548 ConstantArray::get(const_cast<ArrayType *>(LLVMArrTy), {ConstInt});
549 SPIRVType *SpvBaseTy = getSPIRVTypeForVReg(SpvType->getOperand(1).getReg());
550 unsigned BW = getScalarOrVectorBitWidth(SpvBaseTy);
551 return getOrCreateIntCompositeOrNull(Val, MIRBuilder, SpvType, EmitIR,
552 ConstArr, BW,
553 LLVMArrTy->getNumElements());
554}
555
558 SPIRVType *SpvType) {
559 const Type *LLVMTy = getTypeForSPIRVType(SpvType);
560 const TypedPointerType *LLVMPtrTy = cast<TypedPointerType>(LLVMTy);
561 // Find a constant in DT or build a new one.
563 LLVMPtrTy->getElementType(), LLVMPtrTy->getAddressSpace()));
564 Register Res = DT.find(CP, CurMF);
565 if (!Res.isValid()) {
566 LLT LLTy = LLT::pointer(LLVMPtrTy->getAddressSpace(), PointerSize);
568 CurMF->getRegInfo().setRegClass(Res, &SPIRV::IDRegClass);
569 assignSPIRVTypeToVReg(SpvType, Res, *CurMF);
570 MIRBuilder.buildInstr(SPIRV::OpConstantNull)
571 .addDef(Res)
572 .addUse(getSPIRVTypeID(SpvType));
573 DT.add(CP, CurMF, Res);
574 }
575 return Res;
576}
577
579 Register ResReg, unsigned AddrMode, unsigned Param, unsigned FilerMode,
580 MachineIRBuilder &MIRBuilder, SPIRVType *SpvType) {
581 SPIRVType *SampTy;
582 if (SpvType)
583 SampTy = getOrCreateSPIRVType(getTypeForSPIRVType(SpvType), MIRBuilder);
584 else if ((SampTy = getOrCreateSPIRVTypeByName("opencl.sampler_t",
585 MIRBuilder)) == nullptr)
586 report_fatal_error("Unable to recognize SPIRV type name: opencl.sampler_t");
587
588 auto Sampler =
589 ResReg.isValid()
590 ? ResReg
591 : MIRBuilder.getMRI()->createVirtualRegister(&SPIRV::IDRegClass);
592 auto Res = MIRBuilder.buildInstr(SPIRV::OpConstantSampler)
593 .addDef(Sampler)
594 .addUse(getSPIRVTypeID(SampTy))
596 .addImm(Param)
597 .addImm(FilerMode);
598 assert(Res->getOperand(0).isReg());
599 return Res->getOperand(0).getReg();
600}
601
604 const GlobalValue *GV, SPIRV::StorageClass::StorageClass Storage,
605 const MachineInstr *Init, bool IsConst, bool HasLinkageTy,
606 SPIRV::LinkageType::LinkageType LinkageType, MachineIRBuilder &MIRBuilder,
607 bool IsInstSelector) {
608 const GlobalVariable *GVar = nullptr;
609 if (GV)
610 GVar = cast<const GlobalVariable>(GV);
611 else {
612 // If GV is not passed explicitly, use the name to find or construct
613 // the global variable.
614 Module *M = MIRBuilder.getMF().getFunction().getParent();
615 GVar = M->getGlobalVariable(Name);
616 if (GVar == nullptr) {
617 const Type *Ty = getTypeForSPIRVType(BaseType); // TODO: check type.
618 // Module takes ownership of the global var.
619 GVar = new GlobalVariable(*M, const_cast<Type *>(Ty), false,
621 Twine(Name));
622 }
623 GV = GVar;
624 }
625 Register Reg = DT.find(GVar, &MIRBuilder.getMF());
626 if (Reg.isValid()) {
627 if (Reg != ResVReg)
628 MIRBuilder.buildCopy(ResVReg, Reg);
629 return ResVReg;
630 }
631
632 auto MIB = MIRBuilder.buildInstr(SPIRV::OpVariable)
633 .addDef(ResVReg)
635 .addImm(static_cast<uint32_t>(Storage));
636
637 if (Init != 0) {
638 MIB.addUse(Init->getOperand(0).getReg());
639 }
640
641 // ISel may introduce a new register on this step, so we need to add it to
642 // DT and correct its type avoiding fails on the next stage.
643 if (IsInstSelector) {
644 const auto &Subtarget = CurMF->getSubtarget();
645 constrainSelectedInstRegOperands(*MIB, *Subtarget.getInstrInfo(),
646 *Subtarget.getRegisterInfo(),
647 *Subtarget.getRegBankInfo());
648 }
649 Reg = MIB->getOperand(0).getReg();
650 DT.add(GVar, &MIRBuilder.getMF(), Reg);
651
652 // Set to Reg the same type as ResVReg has.
653 auto MRI = MIRBuilder.getMRI();
654 assert(MRI->getType(ResVReg).isPointer() && "Pointer type is expected");
655 if (Reg != ResVReg) {
656 LLT RegLLTy =
657 LLT::pointer(MRI->getType(ResVReg).getAddressSpace(), getPointerSize());
658 MRI->setType(Reg, RegLLTy);
659 assignSPIRVTypeToVReg(BaseType, Reg, MIRBuilder.getMF());
660 } else {
661 // Our knowledge about the type may be updated.
662 // If that's the case, we need to update a type
663 // associated with the register.
664 SPIRVType *DefType = getSPIRVTypeForVReg(ResVReg);
665 if (!DefType || DefType != BaseType)
666 assignSPIRVTypeToVReg(BaseType, Reg, MIRBuilder.getMF());
667 }
668
669 // If it's a global variable with name, output OpName for it.
670 if (GVar && GVar->hasName())
671 buildOpName(Reg, GVar->getName(), MIRBuilder);
672
673 // Output decorations for the GV.
674 // TODO: maybe move to GenerateDecorations pass.
675 const SPIRVSubtarget &ST =
676 cast<SPIRVSubtarget>(MIRBuilder.getMF().getSubtarget());
677 if (IsConst && ST.isOpenCLEnv())
678 buildOpDecorate(Reg, MIRBuilder, SPIRV::Decoration::Constant, {});
679
680 if (GVar && GVar->getAlign().valueOrOne().value() != 1) {
681 unsigned Alignment = (unsigned)GVar->getAlign().valueOrOne().value();
682 buildOpDecorate(Reg, MIRBuilder, SPIRV::Decoration::Alignment, {Alignment});
683 }
684
685 if (HasLinkageTy)
686 buildOpDecorate(Reg, MIRBuilder, SPIRV::Decoration::LinkageAttributes,
687 {static_cast<uint32_t>(LinkageType)}, Name);
688
689 SPIRV::BuiltIn::BuiltIn BuiltInId;
690 if (getSpirvBuiltInIdByName(Name, BuiltInId))
691 buildOpDecorate(Reg, MIRBuilder, SPIRV::Decoration::BuiltIn,
692 {static_cast<uint32_t>(BuiltInId)});
693
694 return Reg;
695}
696
697SPIRVType *SPIRVGlobalRegistry::getOpTypeArray(uint32_t NumElems,
698 SPIRVType *ElemType,
699 MachineIRBuilder &MIRBuilder,
700 bool EmitIR) {
701 assert((ElemType->getOpcode() != SPIRV::OpTypeVoid) &&
702 "Invalid array element type");
703 Register NumElementsVReg =
704 buildConstantInt(NumElems, MIRBuilder, nullptr, EmitIR);
705 auto MIB = MIRBuilder.buildInstr(SPIRV::OpTypeArray)
706 .addDef(createTypeVReg(MIRBuilder))
707 .addUse(getSPIRVTypeID(ElemType))
708 .addUse(NumElementsVReg);
709 return MIB;
710}
711
712SPIRVType *SPIRVGlobalRegistry::getOpTypeOpaque(const StructType *Ty,
713 MachineIRBuilder &MIRBuilder) {
714 assert(Ty->hasName());
715 const StringRef Name = Ty->hasName() ? Ty->getName() : "";
716 Register ResVReg = createTypeVReg(MIRBuilder);
717 auto MIB = MIRBuilder.buildInstr(SPIRV::OpTypeOpaque).addDef(ResVReg);
718 addStringImm(Name, MIB);
719 buildOpName(ResVReg, Name, MIRBuilder);
720 return MIB;
721}
722
723SPIRVType *SPIRVGlobalRegistry::getOpTypeStruct(const StructType *Ty,
724 MachineIRBuilder &MIRBuilder,
725 bool EmitIR) {
726 SmallVector<Register, 4> FieldTypes;
727 for (const auto &Elem : Ty->elements()) {
728 SPIRVType *ElemTy =
729 findSPIRVType(toTypedPointer(Elem, Ty->getContext()), MIRBuilder);
730 assert(ElemTy && ElemTy->getOpcode() != SPIRV::OpTypeVoid &&
731 "Invalid struct element type");
732 FieldTypes.push_back(getSPIRVTypeID(ElemTy));
733 }
734 Register ResVReg = createTypeVReg(MIRBuilder);
735 auto MIB = MIRBuilder.buildInstr(SPIRV::OpTypeStruct).addDef(ResVReg);
736 for (const auto &Ty : FieldTypes)
737 MIB.addUse(Ty);
738 if (Ty->hasName())
739 buildOpName(ResVReg, Ty->getName(), MIRBuilder);
740 if (Ty->isPacked())
741 buildOpDecorate(ResVReg, MIRBuilder, SPIRV::Decoration::CPacked, {});
742 return MIB;
743}
744
745SPIRVType *SPIRVGlobalRegistry::getOrCreateSpecialType(
746 const Type *Ty, MachineIRBuilder &MIRBuilder,
747 SPIRV::AccessQualifier::AccessQualifier AccQual) {
748 assert(isSpecialOpaqueType(Ty) && "Not a special opaque builtin type");
749 return SPIRV::lowerBuiltinType(Ty, AccQual, MIRBuilder, this);
750}
751
752SPIRVType *SPIRVGlobalRegistry::getOpTypePointer(
753 SPIRV::StorageClass::StorageClass SC, SPIRVType *ElemType,
754 MachineIRBuilder &MIRBuilder, Register Reg) {
755 if (!Reg.isValid())
756 Reg = createTypeVReg(MIRBuilder);
757 return MIRBuilder.buildInstr(SPIRV::OpTypePointer)
758 .addDef(Reg)
759 .addImm(static_cast<uint32_t>(SC))
760 .addUse(getSPIRVTypeID(ElemType));
761}
762
763SPIRVType *SPIRVGlobalRegistry::getOpTypeForwardPointer(
764 SPIRV::StorageClass::StorageClass SC, MachineIRBuilder &MIRBuilder) {
765 return MIRBuilder.buildInstr(SPIRV::OpTypeForwardPointer)
766 .addUse(createTypeVReg(MIRBuilder))
767 .addImm(static_cast<uint32_t>(SC));
768}
769
770SPIRVType *SPIRVGlobalRegistry::getOpTypeFunction(
771 SPIRVType *RetType, const SmallVectorImpl<SPIRVType *> &ArgTypes,
772 MachineIRBuilder &MIRBuilder) {
773 auto MIB = MIRBuilder.buildInstr(SPIRV::OpTypeFunction)
774 .addDef(createTypeVReg(MIRBuilder))
775 .addUse(getSPIRVTypeID(RetType));
776 for (const SPIRVType *ArgType : ArgTypes)
777 MIB.addUse(getSPIRVTypeID(ArgType));
778 return MIB;
779}
780
782 const Type *Ty, SPIRVType *RetType,
783 const SmallVectorImpl<SPIRVType *> &ArgTypes,
784 MachineIRBuilder &MIRBuilder) {
785 Register Reg = DT.find(Ty, &MIRBuilder.getMF());
786 if (Reg.isValid())
787 return getSPIRVTypeForVReg(Reg);
788 SPIRVType *SpirvType = getOpTypeFunction(RetType, ArgTypes, MIRBuilder);
789 DT.add(Ty, CurMF, getSPIRVTypeID(SpirvType));
790 return finishCreatingSPIRVType(Ty, SpirvType);
791}
792
793SPIRVType *SPIRVGlobalRegistry::findSPIRVType(
794 const Type *Ty, MachineIRBuilder &MIRBuilder,
795 SPIRV::AccessQualifier::AccessQualifier AccQual, bool EmitIR) {
796 Register Reg = DT.find(Ty, &MIRBuilder.getMF());
797 if (Reg.isValid())
798 return getSPIRVTypeForVReg(Reg);
799 if (ForwardPointerTypes.contains(Ty))
800 return ForwardPointerTypes[Ty];
801 return restOfCreateSPIRVType(Ty, MIRBuilder, AccQual, EmitIR);
802}
803
805 assert(SpirvType && "Attempting to get type id for nullptr type.");
806 if (SpirvType->getOpcode() == SPIRV::OpTypeForwardPointer)
807 return SpirvType->uses().begin()->getReg();
808 return SpirvType->defs().begin()->getReg();
809}
810
811SPIRVType *SPIRVGlobalRegistry::createSPIRVType(
812 const Type *Ty, MachineIRBuilder &MIRBuilder,
813 SPIRV::AccessQualifier::AccessQualifier AccQual, bool EmitIR) {
814 if (isSpecialOpaqueType(Ty))
815 return getOrCreateSpecialType(Ty, MIRBuilder, AccQual);
816 auto &TypeToSPIRVTypeMap = DT.getTypes()->getAllUses();
817 auto t = TypeToSPIRVTypeMap.find(Ty);
818 if (t != TypeToSPIRVTypeMap.end()) {
819 auto tt = t->second.find(&MIRBuilder.getMF());
820 if (tt != t->second.end())
821 return getSPIRVTypeForVReg(tt->second);
822 }
823
824 if (auto IType = dyn_cast<IntegerType>(Ty)) {
825 const unsigned Width = IType->getBitWidth();
826 return Width == 1 ? getOpTypeBool(MIRBuilder)
827 : getOpTypeInt(Width, MIRBuilder, false);
828 }
829 if (Ty->isFloatingPointTy())
830 return getOpTypeFloat(Ty->getPrimitiveSizeInBits(), MIRBuilder);
831 if (Ty->isVoidTy())
832 return getOpTypeVoid(MIRBuilder);
833 if (Ty->isVectorTy()) {
834 SPIRVType *El =
835 findSPIRVType(cast<FixedVectorType>(Ty)->getElementType(), MIRBuilder);
836 return getOpTypeVector(cast<FixedVectorType>(Ty)->getNumElements(), El,
837 MIRBuilder);
838 }
839 if (Ty->isArrayTy()) {
840 SPIRVType *El = findSPIRVType(Ty->getArrayElementType(), MIRBuilder);
841 return getOpTypeArray(Ty->getArrayNumElements(), El, MIRBuilder, EmitIR);
842 }
843 if (auto SType = dyn_cast<StructType>(Ty)) {
844 if (SType->isOpaque())
845 return getOpTypeOpaque(SType, MIRBuilder);
846 return getOpTypeStruct(SType, MIRBuilder, EmitIR);
847 }
848 if (auto FType = dyn_cast<FunctionType>(Ty)) {
849 SPIRVType *RetTy = findSPIRVType(FType->getReturnType(), MIRBuilder);
851 for (const auto &t : FType->params()) {
852 ParamTypes.push_back(findSPIRVType(t, MIRBuilder));
853 }
854 return getOpTypeFunction(RetTy, ParamTypes, MIRBuilder);
855 }
856 unsigned AddrSpace = 0xFFFF;
857 if (auto PType = dyn_cast<TypedPointerType>(Ty))
858 AddrSpace = PType->getAddressSpace();
859 else if (auto PType = dyn_cast<PointerType>(Ty))
860 AddrSpace = PType->getAddressSpace();
861 else
862 report_fatal_error("Unable to convert LLVM type to SPIRVType", true);
863
864 SPIRVType *SpvElementType = nullptr;
865 if (auto PType = dyn_cast<TypedPointerType>(Ty))
866 SpvElementType = getOrCreateSPIRVType(PType->getElementType(), MIRBuilder,
867 AccQual, EmitIR);
868 else
869 SpvElementType = getOrCreateSPIRVIntegerType(8, MIRBuilder);
870
871 // Get access to information about available extensions
872 const SPIRVSubtarget *ST =
873 static_cast<const SPIRVSubtarget *>(&MIRBuilder.getMF().getSubtarget());
874 auto SC = addressSpaceToStorageClass(AddrSpace, *ST);
875 // Null pointer means we have a loop in type definitions, make and
876 // return corresponding OpTypeForwardPointer.
877 if (SpvElementType == nullptr) {
878 if (!ForwardPointerTypes.contains(Ty))
879 ForwardPointerTypes[Ty] = getOpTypeForwardPointer(SC, MIRBuilder);
880 return ForwardPointerTypes[Ty];
881 }
882 // If we have forward pointer associated with this type, use its register
883 // operand to create OpTypePointer.
884 if (ForwardPointerTypes.contains(Ty)) {
885 Register Reg = getSPIRVTypeID(ForwardPointerTypes[Ty]);
886 return getOpTypePointer(SC, SpvElementType, MIRBuilder, Reg);
887 }
888
889 return getOrCreateSPIRVPointerType(SpvElementType, MIRBuilder, SC);
890}
891
892SPIRVType *SPIRVGlobalRegistry::restOfCreateSPIRVType(
893 const Type *Ty, MachineIRBuilder &MIRBuilder,
894 SPIRV::AccessQualifier::AccessQualifier AccessQual, bool EmitIR) {
895 if (TypesInProcessing.count(Ty) && !isPointerTy(Ty))
896 return nullptr;
897 TypesInProcessing.insert(Ty);
898 SPIRVType *SpirvType = createSPIRVType(Ty, MIRBuilder, AccessQual, EmitIR);
899 TypesInProcessing.erase(Ty);
900 VRegToTypeMap[&MIRBuilder.getMF()][getSPIRVTypeID(SpirvType)] = SpirvType;
901 SPIRVToLLVMType[SpirvType] = Ty;
902 Register Reg = DT.find(Ty, &MIRBuilder.getMF());
903 // Do not add OpTypeForwardPointer to DT, a corresponding normal pointer type
904 // will be added later. For special types it is already added to DT.
905 if (SpirvType->getOpcode() != SPIRV::OpTypeForwardPointer && !Reg.isValid() &&
906 !isSpecialOpaqueType(Ty)) {
907 if (!isPointerTy(Ty))
908 DT.add(Ty, &MIRBuilder.getMF(), getSPIRVTypeID(SpirvType));
909 else if (isTypedPointerTy(Ty))
910 DT.add(cast<TypedPointerType>(Ty)->getElementType(),
911 getPointerAddressSpace(Ty), &MIRBuilder.getMF(),
912 getSPIRVTypeID(SpirvType));
913 else
914 DT.add(Type::getInt8Ty(MIRBuilder.getMF().getFunction().getContext()),
915 getPointerAddressSpace(Ty), &MIRBuilder.getMF(),
916 getSPIRVTypeID(SpirvType));
917 }
918
919 return SpirvType;
920}
921
922SPIRVType *
924 const MachineFunction *MF) const {
925 auto t = VRegToTypeMap.find(MF ? MF : CurMF);
926 if (t != VRegToTypeMap.end()) {
927 auto tt = t->second.find(VReg);
928 if (tt != t->second.end())
929 return tt->second;
930 }
931 return nullptr;
932}
933
935 const Type *Ty, MachineIRBuilder &MIRBuilder,
936 SPIRV::AccessQualifier::AccessQualifier AccessQual, bool EmitIR) {
937 Register Reg;
938 if (!isPointerTy(Ty))
939 Reg = DT.find(Ty, &MIRBuilder.getMF());
940 else if (isTypedPointerTy(Ty))
941 Reg = DT.find(cast<TypedPointerType>(Ty)->getElementType(),
942 getPointerAddressSpace(Ty), &MIRBuilder.getMF());
943 else
944 Reg =
945 DT.find(Type::getInt8Ty(MIRBuilder.getMF().getFunction().getContext()),
946 getPointerAddressSpace(Ty), &MIRBuilder.getMF());
947
948 if (Reg.isValid() && !isSpecialOpaqueType(Ty))
949 return getSPIRVTypeForVReg(Reg);
950 TypesInProcessing.clear();
951 SPIRVType *STy = restOfCreateSPIRVType(Ty, MIRBuilder, AccessQual, EmitIR);
952 // Create normal pointer types for the corresponding OpTypeForwardPointers.
953 for (auto &CU : ForwardPointerTypes) {
954 const Type *Ty2 = CU.first;
955 SPIRVType *STy2 = CU.second;
956 if ((Reg = DT.find(Ty2, &MIRBuilder.getMF())).isValid())
957 STy2 = getSPIRVTypeForVReg(Reg);
958 else
959 STy2 = restOfCreateSPIRVType(Ty2, MIRBuilder, AccessQual, EmitIR);
960 if (Ty == Ty2)
961 STy = STy2;
962 }
963 ForwardPointerTypes.clear();
964 return STy;
965}
966
968 unsigned TypeOpcode) const {
970 assert(Type && "isScalarOfType VReg has no type assigned");
971 return Type->getOpcode() == TypeOpcode;
972}
973
975 unsigned TypeOpcode) const {
977 assert(Type && "isScalarOrVectorOfType VReg has no type assigned");
978 if (Type->getOpcode() == TypeOpcode)
979 return true;
980 if (Type->getOpcode() == SPIRV::OpTypeVector) {
981 Register ScalarTypeVReg = Type->getOperand(1).getReg();
982 SPIRVType *ScalarType = getSPIRVTypeForVReg(ScalarTypeVReg);
983 return ScalarType->getOpcode() == TypeOpcode;
984 }
985 return false;
986}
987
988unsigned
991}
992
993unsigned
995 if (!Type)
996 return 0;
997 return Type->getOpcode() == SPIRV::OpTypeVector
998 ? static_cast<unsigned>(Type->getOperand(2).getImm())
999 : 1;
1000}
1001
1002unsigned
1004 assert(Type && "Invalid Type pointer");
1005 if (Type->getOpcode() == SPIRV::OpTypeVector) {
1006 auto EleTypeReg = Type->getOperand(1).getReg();
1007 Type = getSPIRVTypeForVReg(EleTypeReg);
1008 }
1009 if (Type->getOpcode() == SPIRV::OpTypeInt ||
1010 Type->getOpcode() == SPIRV::OpTypeFloat)
1011 return Type->getOperand(1).getImm();
1012 if (Type->getOpcode() == SPIRV::OpTypeBool)
1013 return 1;
1014 llvm_unreachable("Attempting to get bit width of non-integer/float type.");
1015}
1016
1018 const SPIRVType *Type) const {
1019 assert(Type && "Invalid Type pointer");
1020 unsigned NumElements = 1;
1021 if (Type->getOpcode() == SPIRV::OpTypeVector) {
1022 NumElements = static_cast<unsigned>(Type->getOperand(2).getImm());
1023 Type = getSPIRVTypeForVReg(Type->getOperand(1).getReg());
1024 }
1025 return Type->getOpcode() == SPIRV::OpTypeInt ||
1026 Type->getOpcode() == SPIRV::OpTypeFloat
1027 ? NumElements * Type->getOperand(1).getImm()
1028 : 0;
1029}
1030
1032 const SPIRVType *Type) const {
1033 if (Type && Type->getOpcode() == SPIRV::OpTypeVector)
1034 Type = getSPIRVTypeForVReg(Type->getOperand(1).getReg());
1035 return Type && Type->getOpcode() == SPIRV::OpTypeInt ? Type : nullptr;
1036}
1037
1040 return IntType && IntType->getOperand(2).getImm() != 0;
1041}
1042
1044 SPIRVType *PtrType = getSPIRVTypeForVReg(PtrReg);
1045 SPIRVType *ElemType =
1046 PtrType && PtrType->getOpcode() == SPIRV::OpTypePointer
1047 ? getSPIRVTypeForVReg(PtrType->getOperand(2).getReg())
1048 : nullptr;
1049 return ElemType ? ElemType->getOpcode() : 0;
1050}
1051
1053 const SPIRVType *Type2) const {
1054 if (!Type1 || !Type2)
1055 return false;
1056 auto Op1 = Type1->getOpcode(), Op2 = Type2->getOpcode();
1057 // Ignore difference between <1.5 and >=1.5 protocol versions:
1058 // it's valid if either Result Type or Operand is a pointer, and the other
1059 // is a pointer, an integer scalar, or an integer vector.
1060 if (Op1 == SPIRV::OpTypePointer &&
1061 (Op2 == SPIRV::OpTypePointer || retrieveScalarOrVectorIntType(Type2)))
1062 return true;
1063 if (Op2 == SPIRV::OpTypePointer &&
1064 (Op1 == SPIRV::OpTypePointer || retrieveScalarOrVectorIntType(Type1)))
1065 return true;
1066 unsigned Bits1 = getNumScalarOrVectorTotalBitWidth(Type1),
1068 return Bits1 > 0 && Bits1 == Bits2;
1069}
1070
1071SPIRV::StorageClass::StorageClass
1074 assert(Type && Type->getOpcode() == SPIRV::OpTypePointer &&
1075 Type->getOperand(1).isImm() && "Pointer type is expected");
1076 return static_cast<SPIRV::StorageClass::StorageClass>(
1077 Type->getOperand(1).getImm());
1078}
1079
1081 MachineIRBuilder &MIRBuilder, SPIRVType *SampledType, SPIRV::Dim::Dim Dim,
1082 uint32_t Depth, uint32_t Arrayed, uint32_t Multisampled, uint32_t Sampled,
1083 SPIRV::ImageFormat::ImageFormat ImageFormat,
1084 SPIRV::AccessQualifier::AccessQualifier AccessQual) {
1085 SPIRV::ImageTypeDescriptor TD(SPIRVToLLVMType.lookup(SampledType), Dim, Depth,
1086 Arrayed, Multisampled, Sampled, ImageFormat,
1087 AccessQual);
1088 if (auto *Res = checkSpecialInstr(TD, MIRBuilder))
1089 return Res;
1090 Register ResVReg = createTypeVReg(MIRBuilder);
1091 DT.add(TD, &MIRBuilder.getMF(), ResVReg);
1092 return MIRBuilder.buildInstr(SPIRV::OpTypeImage)
1093 .addDef(ResVReg)
1094 .addUse(getSPIRVTypeID(SampledType))
1095 .addImm(Dim)
1096 .addImm(Depth) // Depth (whether or not it is a Depth image).
1097 .addImm(Arrayed) // Arrayed.
1098 .addImm(Multisampled) // Multisampled (0 = only single-sample).
1099 .addImm(Sampled) // Sampled (0 = usage known at runtime).
1100 .addImm(ImageFormat)
1101 .addImm(AccessQual);
1102}
1103
1104SPIRVType *
1107 if (auto *Res = checkSpecialInstr(TD, MIRBuilder))
1108 return Res;
1109 Register ResVReg = createTypeVReg(MIRBuilder);
1110 DT.add(TD, &MIRBuilder.getMF(), ResVReg);
1111 return MIRBuilder.buildInstr(SPIRV::OpTypeSampler).addDef(ResVReg);
1112}
1113
1115 MachineIRBuilder &MIRBuilder,
1116 SPIRV::AccessQualifier::AccessQualifier AccessQual) {
1117 SPIRV::PipeTypeDescriptor TD(AccessQual);
1118 if (auto *Res = checkSpecialInstr(TD, MIRBuilder))
1119 return Res;
1120 Register ResVReg = createTypeVReg(MIRBuilder);
1121 DT.add(TD, &MIRBuilder.getMF(), ResVReg);
1122 return MIRBuilder.buildInstr(SPIRV::OpTypePipe)
1123 .addDef(ResVReg)
1124 .addImm(AccessQual);
1125}
1126
1128 MachineIRBuilder &MIRBuilder) {
1130 if (auto *Res = checkSpecialInstr(TD, MIRBuilder))
1131 return Res;
1132 Register ResVReg = createTypeVReg(MIRBuilder);
1133 DT.add(TD, &MIRBuilder.getMF(), ResVReg);
1134 return MIRBuilder.buildInstr(SPIRV::OpTypeDeviceEvent).addDef(ResVReg);
1135}
1136
1138 SPIRVType *ImageType, MachineIRBuilder &MIRBuilder) {
1140 SPIRVToLLVMType.lookup(MIRBuilder.getMF().getRegInfo().getVRegDef(
1141 ImageType->getOperand(1).getReg())),
1142 ImageType);
1143 if (auto *Res = checkSpecialInstr(TD, MIRBuilder))
1144 return Res;
1145 Register ResVReg = createTypeVReg(MIRBuilder);
1146 DT.add(TD, &MIRBuilder.getMF(), ResVReg);
1147 return MIRBuilder.buildInstr(SPIRV::OpTypeSampledImage)
1148 .addDef(ResVReg)
1149 .addUse(getSPIRVTypeID(ImageType));
1150}
1151
1153 const Type *Ty, MachineIRBuilder &MIRBuilder, unsigned Opcode) {
1154 Register ResVReg = DT.find(Ty, &MIRBuilder.getMF());
1155 if (ResVReg.isValid())
1156 return MIRBuilder.getMF().getRegInfo().getUniqueVRegDef(ResVReg);
1157 ResVReg = createTypeVReg(MIRBuilder);
1158 SPIRVType *SpirvTy = MIRBuilder.buildInstr(Opcode).addDef(ResVReg);
1159 DT.add(Ty, &MIRBuilder.getMF(), ResVReg);
1160 return SpirvTy;
1161}
1162
1163const MachineInstr *
1164SPIRVGlobalRegistry::checkSpecialInstr(const SPIRV::SpecialTypeDescriptor &TD,
1165 MachineIRBuilder &MIRBuilder) {
1166 Register Reg = DT.find(TD, &MIRBuilder.getMF());
1167 if (Reg.isValid())
1168 return MIRBuilder.getMF().getRegInfo().getUniqueVRegDef(Reg);
1169 return nullptr;
1170}
1171
1172// Returns nullptr if unable to recognize SPIRV type name
1174 StringRef TypeStr, MachineIRBuilder &MIRBuilder,
1175 SPIRV::StorageClass::StorageClass SC,
1176 SPIRV::AccessQualifier::AccessQualifier AQ) {
1177 unsigned VecElts = 0;
1178 auto &Ctx = MIRBuilder.getMF().getFunction().getContext();
1179
1180 // Parse strings representing either a SPIR-V or OpenCL builtin type.
1181 if (hasBuiltinTypePrefix(TypeStr))
1183 TypeStr.str(), MIRBuilder.getContext()),
1184 MIRBuilder, AQ);
1185
1186 // Parse type name in either "typeN" or "type vector[N]" format, where
1187 // N is the number of elements of the vector.
1188 Type *Ty;
1189
1190 Ty = parseBasicTypeName(TypeStr, Ctx);
1191 if (!Ty)
1192 // Unable to recognize SPIRV type name
1193 return nullptr;
1194
1195 auto SpirvTy = getOrCreateSPIRVType(Ty, MIRBuilder, AQ);
1196
1197 // Handle "type*" or "type* vector[N]".
1198 if (TypeStr.starts_with("*")) {
1199 SpirvTy = getOrCreateSPIRVPointerType(SpirvTy, MIRBuilder, SC);
1200 TypeStr = TypeStr.substr(strlen("*"));
1201 }
1202
1203 // Handle "typeN*" or "type vector[N]*".
1204 bool IsPtrToVec = TypeStr.consume_back("*");
1205
1206 if (TypeStr.consume_front(" vector[")) {
1207 TypeStr = TypeStr.substr(0, TypeStr.find(']'));
1208 }
1209 TypeStr.getAsInteger(10, VecElts);
1210 if (VecElts > 0)
1211 SpirvTy = getOrCreateSPIRVVectorType(SpirvTy, VecElts, MIRBuilder);
1212
1213 if (IsPtrToVec)
1214 SpirvTy = getOrCreateSPIRVPointerType(SpirvTy, MIRBuilder, SC);
1215
1216 return SpirvTy;
1217}
1218
1219SPIRVType *
1221 MachineIRBuilder &MIRBuilder) {
1222 return getOrCreateSPIRVType(
1224 MIRBuilder);
1225}
1226
1227SPIRVType *SPIRVGlobalRegistry::finishCreatingSPIRVType(const Type *LLVMTy,
1228 SPIRVType *SpirvType) {
1229 assert(CurMF == SpirvType->getMF());
1230 VRegToTypeMap[CurMF][getSPIRVTypeID(SpirvType)] = SpirvType;
1231 SPIRVToLLVMType[SpirvType] = LLVMTy;
1232 return SpirvType;
1233}
1234
1236 MachineInstr &I,
1237 const SPIRVInstrInfo &TII,
1238 unsigned SPIRVOPcode,
1239 Type *LLVMTy) {
1240 Register Reg = DT.find(LLVMTy, CurMF);
1241 if (Reg.isValid())
1242 return getSPIRVTypeForVReg(Reg);
1243 MachineBasicBlock &BB = *I.getParent();
1244 auto MIB = BuildMI(BB, I, I.getDebugLoc(), TII.get(SPIRVOPcode))
1247 .addImm(0);
1248 DT.add(LLVMTy, CurMF, getSPIRVTypeID(MIB));
1249 return finishCreatingSPIRVType(LLVMTy, MIB);
1250}
1251
1253 unsigned BitWidth, MachineInstr &I, const SPIRVInstrInfo &TII) {
1255 return getOrCreateSPIRVType(BitWidth, I, TII, SPIRV::OpTypeInt, LLVMTy);
1256}
1258 unsigned BitWidth, MachineInstr &I, const SPIRVInstrInfo &TII) {
1260 Type *LLVMTy;
1261 switch (BitWidth) {
1262 case 16:
1263 LLVMTy = Type::getHalfTy(Ctx);
1264 break;
1265 case 32:
1266 LLVMTy = Type::getFloatTy(Ctx);
1267 break;
1268 case 64:
1269 LLVMTy = Type::getDoubleTy(Ctx);
1270 break;
1271 default:
1272 llvm_unreachable("Bit width is of unexpected size.");
1273 }
1274 return getOrCreateSPIRVType(BitWidth, I, TII, SPIRV::OpTypeFloat, LLVMTy);
1275}
1276
1277SPIRVType *
1279 return getOrCreateSPIRVType(
1280 IntegerType::get(MIRBuilder.getMF().getFunction().getContext(), 1),
1281 MIRBuilder);
1282}
1283
1284SPIRVType *
1286 const SPIRVInstrInfo &TII) {
1288 Register Reg = DT.find(LLVMTy, CurMF);
1289 if (Reg.isValid())
1290 return getSPIRVTypeForVReg(Reg);
1291 MachineBasicBlock &BB = *I.getParent();
1292 auto MIB = BuildMI(BB, I, I.getDebugLoc(), TII.get(SPIRV::OpTypeBool))
1294 DT.add(LLVMTy, CurMF, getSPIRVTypeID(MIB));
1295 return finishCreatingSPIRVType(LLVMTy, MIB);
1296}
1297
1299 SPIRVType *BaseType, unsigned NumElements, MachineIRBuilder &MIRBuilder) {
1300 return getOrCreateSPIRVType(
1302 NumElements),
1303 MIRBuilder);
1304}
1305
1307 SPIRVType *BaseType, unsigned NumElements, MachineInstr &I,
1308 const SPIRVInstrInfo &TII) {
1309 Type *LLVMTy = FixedVectorType::get(
1310 const_cast<Type *>(getTypeForSPIRVType(BaseType)), NumElements);
1311 Register Reg = DT.find(LLVMTy, CurMF);
1312 if (Reg.isValid())
1313 return getSPIRVTypeForVReg(Reg);
1314 MachineBasicBlock &BB = *I.getParent();
1315 auto MIB = BuildMI(BB, I, I.getDebugLoc(), TII.get(SPIRV::OpTypeVector))
1318 .addImm(NumElements);
1319 DT.add(LLVMTy, CurMF, getSPIRVTypeID(MIB));
1320 return finishCreatingSPIRVType(LLVMTy, MIB);
1321}
1322
1324 SPIRVType *BaseType, unsigned NumElements, MachineInstr &I,
1325 const SPIRVInstrInfo &TII) {
1326 Type *LLVMTy = ArrayType::get(
1327 const_cast<Type *>(getTypeForSPIRVType(BaseType)), NumElements);
1328 Register Reg = DT.find(LLVMTy, CurMF);
1329 if (Reg.isValid())
1330 return getSPIRVTypeForVReg(Reg);
1331 MachineBasicBlock &BB = *I.getParent();
1332 SPIRVType *SpirvType = getOrCreateSPIRVIntegerType(32, I, TII);
1333 Register Len = getOrCreateConstInt(NumElements, I, SpirvType, TII);
1334 auto MIB = BuildMI(BB, I, I.getDebugLoc(), TII.get(SPIRV::OpTypeArray))
1337 .addUse(Len);
1338 DT.add(LLVMTy, CurMF, getSPIRVTypeID(MIB));
1339 return finishCreatingSPIRVType(LLVMTy, MIB);
1340}
1341
1343 SPIRVType *BaseType, MachineIRBuilder &MIRBuilder,
1344 SPIRV::StorageClass::StorageClass SC) {
1345 const Type *PointerElementType = getTypeForSPIRVType(BaseType);
1347 Type *LLVMTy = TypedPointerType::get(const_cast<Type *>(PointerElementType),
1348 AddressSpace);
1349 // check if this type is already available
1350 Register Reg = DT.find(PointerElementType, AddressSpace, CurMF);
1351 if (Reg.isValid())
1352 return getSPIRVTypeForVReg(Reg);
1353 // create a new type
1354 auto MIB = BuildMI(MIRBuilder.getMBB(), MIRBuilder.getInsertPt(),
1355 MIRBuilder.getDebugLoc(),
1356 MIRBuilder.getTII().get(SPIRV::OpTypePointer))
1358 .addImm(static_cast<uint32_t>(SC))
1360 DT.add(PointerElementType, AddressSpace, CurMF, getSPIRVTypeID(MIB));
1361 return finishCreatingSPIRVType(LLVMTy, MIB);
1362}
1363
1366 SPIRV::StorageClass::StorageClass SC) {
1367 MachineIRBuilder MIRBuilder(I);
1368 return getOrCreateSPIRVPointerType(BaseType, MIRBuilder, SC);
1369}
1370
1372 SPIRVType *SpvType,
1373 const SPIRVInstrInfo &TII) {
1374 assert(SpvType);
1375 const Type *LLVMTy = getTypeForSPIRVType(SpvType);
1376 assert(LLVMTy);
1377 // Find a constant in DT or build a new one.
1378 UndefValue *UV = UndefValue::get(const_cast<Type *>(LLVMTy));
1379 Register Res = DT.find(UV, CurMF);
1380 if (Res.isValid())
1381 return Res;
1382 LLT LLTy = LLT::scalar(32);
1384 CurMF->getRegInfo().setRegClass(Res, &SPIRV::IDRegClass);
1385 assignSPIRVTypeToVReg(SpvType, Res, *CurMF);
1386 DT.add(UV, CurMF, Res);
1387
1389 MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(SPIRV::OpUndef))
1390 .addDef(Res)
1391 .addUse(getSPIRVTypeID(SpvType));
1392 const auto &ST = CurMF->getSubtarget();
1393 constrainSelectedInstRegOperands(*MIB, *ST.getInstrInfo(),
1394 *ST.getRegisterInfo(), *ST.getRegBankInfo());
1395 return Res;
1396}
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:1810
const HexagonInstrInfo * TII
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static Register createTypeVReg(MachineIRBuilder &MIRBuilder)
APInt bitcastToAPInt() const
Definition: APFloat.h:1210
bool isPosZero() const
Definition: APFloat.h:1306
Class for arbitrary precision integers.
Definition: APInt.h:76
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1491
int64_t getSExtValue() const
Get sign extended value.
Definition: APInt.h:1513
Class to represent array types.
Definition: DerivedTypes.h:371
uint64_t getNumElements() const
Definition: DerivedTypes.h:383
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:647
Type * getElementType() const
Definition: DerivedTypes.h:384
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1291
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:268
const APFloat & getValueAPF() const
Definition: Constants.h:311
This is the shared class of boolean and integer constants.
Definition: Constants.h:80
static ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
Definition: Constants.cpp:1775
static Constant * getSplat(ElementCount EC, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
Definition: Constants.cpp:1449
This is an important base class in LLVM.
Definition: Constant.h:41
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:1758
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition: Constants.cpp:90
Class to represent fixed width SIMD vectors.
Definition: DerivedTypes.h:539
static FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition: Type.cpp:692
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:356
MaybeAlign getAlign() const
Returns the alignment of the given variable or function.
Definition: GlobalObject.h:80
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:40
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:278
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
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.
LLVMContext & getContext() const
const TargetInstrInfo & getTII()
MachineBasicBlock::iterator getInsertPt()
Current insertion point for new instructions.
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.
MachineInstrBuilder buildSplatVector(const DstOp &Res, const SrcOp &Val)
Build and insert Res = G_SPLAT_VECTOR Val.
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:546
iterator_range< mop_iterator > uses()
Returns a range that includes all operands that are register uses.
Definition: MachineInstr.h:710
iterator_range< mop_iterator > defs()
Returns a range over all explicit operands that are register definitions.
Definition: MachineInstr.h:699
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:556
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.
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)
SPIRVType * assignFloatTypeToVReg(unsigned BitWidth, Register VReg, MachineInstr &I, const SPIRVInstrInfo &TII)
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)
Register getSPIRVTypeID(const SPIRVType *SpirvType) const
SPIRVType * getOrCreateSPIRVType(const Type *Type, MachineIRBuilder &MIRBuilder, SPIRV::AccessQualifier::AccessQualifier AQ=SPIRV::AccessQualifier::ReadWrite, bool EmitIR=true)
void assignSPIRVTypeToVReg(SPIRVType *Type, Register VReg, MachineFunction &MF)
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 * 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)
bool isScalarOrVectorOfType(Register VReg, unsigned TypeOpcode) const
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 * getOrCreateSPIRVVectorType(SPIRVType *BaseType, unsigned NumElements, MachineIRBuilder &MIRBuilder)
SPIRVType * getOrCreateSPIRVIntegerType(unsigned BitWidth, MachineIRBuilder &MIRBuilder)
Register getOrCreateConsIntArray(uint64_t Val, MachineInstr &I, SPIRVType *SpvType, const SPIRVInstrInfo &TII)
SPIRVType * getOrCreateSPIRVArrayType(SPIRVType *BaseType, unsigned NumElements, MachineInstr &I, const SPIRVInstrInfo &TII)
SPIRV::StorageClass::StorageClass getPointerStorageClass(Register VReg) const
SPIRVType * getOrCreateOpTypeSampler(MachineIRBuilder &MIRBuilder)
Register buildConstantInt(uint64_t Val, MachineIRBuilder &MIRBuilder, SPIRVType *SpvType=nullptr, bool EmitIR=true)
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
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:586
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
bool consume_back(StringRef Suffix)
Returns true if this StringRef has the given suffix and removes that suffix.
Definition: StringRef.h:651
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:466
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:222
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:567
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:257
bool consume_front(StringRef Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
Definition: StringRef.h:631
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:293
Class to represent struct types.
Definition: DerivedTypes.h:216
ArrayRef< Type * > elements() const
Definition: DerivedTypes.h:333
bool isPacked() const
Definition: DerivedTypes.h:278
bool hasName() const
Return true if this is a named struct that has a non-empty name.
Definition: DerivedTypes.h:304
StringRef getName() const
Return the name for this struct type if it has an identity.
Definition: Type.cpp:590
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:265
bool isArrayTy() const
True if this is an instance of ArrayType.
Definition: Type.h:252
Type * getArrayElementType() const
Definition: Type.h:404
uint64_t getArrayNumElements() const
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:129
static IntegerType * getInt8Ty(LLVMContext &C)
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
Definition: Type.h:185
static IntegerType * getInt32Ty(LLVMContext &C)
static Type * getFloatTy(LLVMContext &C)
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:228
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:140
A few GPU targets, such as DXIL and SPIR-V, have typed pointers.
Type * getElementType() const
static TypedPointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
unsigned getAddressSpace() const
Return the address space of the Pointer type.
'undef' values are things that do not have specified contents.
Definition: Constants.h:1348
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1808
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:641
Type * getElementType() const
Definition: DerivedTypes.h:436
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ SC
CHAIN = SC CHAIN, Imm128 - System call.
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)
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:100
unsigned storageClassToAddressSpace(SPIRV::StorageClass::StorageClass SC)
Definition: SPIRVUtils.cpp:138
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:122
void addNumImm(const APInt &Imm, MachineInstrBuilder &MIB)
Definition: SPIRVUtils.cpp:80
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:153
AddressSpace
Definition: NVPTXBaseInfo.h:21
bool getSpirvBuiltInIdByName(llvm::StringRef Name, SPIRV::BuiltIn::BuiltIn &BI)
bool isTypedPointerTy(const Type *T)
Definition: SPIRVUtils.h:106
void buildOpDecorate(Register Reg, MachineIRBuilder &MIRBuilder, SPIRV::Decoration::Decoration Dec, const std::vector< uint32_t > &DecArgs, StringRef StrImm)
Definition: SPIRVUtils.cpp:117
bool isSpecialOpaqueType(const Type *Ty)
Definition: SPIRVUtils.cpp:351
bool isPointerTy(const Type *T)
Definition: SPIRVUtils.h:116
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
SPIRV::StorageClass::StorageClass addressSpaceToStorageClass(unsigned AddrSpace, const SPIRVSubtarget &STI)
Definition: SPIRVUtils.cpp:162
Type * toTypedPointer(Type *Ty, LLVMContext &Ctx)
Definition: SPIRVUtils.h:152
Type * parseBasicTypeName(StringRef &TypeName, LLVMContext &Ctx)
Definition: SPIRVUtils.cpp:372
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:191
bool hasBuiltinTypePrefix(StringRef Name)
Definition: SPIRVUtils.cpp:344
void addStringImm(const StringRef &Str, MCInst &Inst)
Definition: SPIRVUtils.cpp:51
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