LLVM 22.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 "SPIRVUtils.h"
22#include "llvm/ADT/APInt.h"
23#include "llvm/IR/Constants.h"
25#include "llvm/IR/Function.h"
27#include "llvm/IR/Intrinsics.h"
28#include "llvm/IR/IntrinsicsSPIRV.h"
29#include "llvm/IR/Type.h"
32#include <cassert>
33#include <functional>
34
35using namespace llvm;
36
37static bool allowEmitFakeUse(const Value *Arg) {
38 if (isSpvIntrinsic(Arg))
39 return false;
41 return false;
42 if (const auto *LI = dyn_cast<LoadInst>(Arg))
43 if (LI->getType()->isAggregateType())
44 return false;
45 return true;
46}
47
48static unsigned typeToAddressSpace(const Type *Ty) {
49 if (auto PType = dyn_cast<TypedPointerType>(Ty))
50 return PType->getAddressSpace();
51 if (auto PType = dyn_cast<PointerType>(Ty))
52 return PType->getAddressSpace();
53 if (auto *ExtTy = dyn_cast<TargetExtType>(Ty);
54 ExtTy && isTypedPointerWrapper(ExtTy))
55 return ExtTy->getIntParameter(0);
56 reportFatalInternalError("Unable to convert LLVM type to SPIRVType");
57}
58
59static bool
60storageClassRequiresExplictLayout(SPIRV::StorageClass::StorageClass SC) {
61 switch (SC) {
62 case SPIRV::StorageClass::Uniform:
63 case SPIRV::StorageClass::PushConstant:
64 case SPIRV::StorageClass::StorageBuffer:
65 case SPIRV::StorageClass::PhysicalStorageBufferEXT:
66 return true;
67 case SPIRV::StorageClass::UniformConstant:
68 case SPIRV::StorageClass::Input:
69 case SPIRV::StorageClass::Output:
70 case SPIRV::StorageClass::Workgroup:
71 case SPIRV::StorageClass::CrossWorkgroup:
72 case SPIRV::StorageClass::Private:
73 case SPIRV::StorageClass::Function:
74 case SPIRV::StorageClass::Generic:
75 case SPIRV::StorageClass::AtomicCounter:
76 case SPIRV::StorageClass::Image:
77 case SPIRV::StorageClass::CallableDataNV:
78 case SPIRV::StorageClass::IncomingCallableDataNV:
79 case SPIRV::StorageClass::RayPayloadNV:
80 case SPIRV::StorageClass::HitAttributeNV:
81 case SPIRV::StorageClass::IncomingRayPayloadNV:
82 case SPIRV::StorageClass::ShaderRecordBufferNV:
83 case SPIRV::StorageClass::CodeSectionINTEL:
84 case SPIRV::StorageClass::DeviceOnlyINTEL:
85 case SPIRV::StorageClass::HostOnlyINTEL:
86 return false;
87 }
88 llvm_unreachable("Unknown SPIRV::StorageClass enum");
89}
90
92 : PointerSize(PointerSize), Bound(0), CurMF(nullptr) {}
93
95 Register VReg,
97 const SPIRVInstrInfo &TII) {
99 assignSPIRVTypeToVReg(SpirvType, VReg, *CurMF);
100 return SpirvType;
101}
102
103SPIRVType *
106 const SPIRVInstrInfo &TII) {
108 assignSPIRVTypeToVReg(SpirvType, VReg, *CurMF);
109 return SpirvType;
110}
111
113 SPIRVType *BaseType, unsigned NumElements, Register VReg, MachineInstr &I,
114 const SPIRVInstrInfo &TII) {
115 SPIRVType *SpirvType =
117 assignSPIRVTypeToVReg(SpirvType, VReg, *CurMF);
118 return SpirvType;
119}
120
122 const Type *Type, Register VReg, MachineIRBuilder &MIRBuilder,
123 SPIRV::AccessQualifier::AccessQualifier AccessQual, bool EmitIR) {
124 SPIRVType *SpirvType =
125 getOrCreateSPIRVType(Type, MIRBuilder, AccessQual, EmitIR);
126 assignSPIRVTypeToVReg(SpirvType, VReg, MIRBuilder.getMF());
127 return SpirvType;
128}
129
131 Register VReg,
132 const MachineFunction &MF) {
133 VRegToTypeMap[&MF][VReg] = SpirvType;
134}
135
137 auto Res = MRI.createGenericVirtualRegister(LLT::scalar(64));
138 MRI.setRegClass(Res, &SPIRV::TYPERegClass);
139 return Res;
140}
141
143 return createTypeVReg(MIRBuilder.getMF().getRegInfo());
144}
145
146SPIRVType *SPIRVGlobalRegistry::getOpTypeBool(MachineIRBuilder &MIRBuilder) {
147 return createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
148 return MIRBuilder.buildInstr(SPIRV::OpTypeBool)
149 .addDef(createTypeVReg(MIRBuilder));
150 });
151}
152
153unsigned SPIRVGlobalRegistry::adjustOpTypeIntWidth(unsigned Width) const {
154 if (Width > 64)
155 report_fatal_error("Unsupported integer width!");
156 const SPIRVSubtarget &ST = cast<SPIRVSubtarget>(CurMF->getSubtarget());
157 if (ST.canUseExtension(
158 SPIRV::Extension::SPV_ALTERA_arbitrary_precision_integers) ||
159 ST.canUseExtension(SPIRV::Extension::SPV_INTEL_int4))
160 return Width;
161 if (Width <= 8)
162 Width = 8;
163 else if (Width <= 16)
164 Width = 16;
165 else if (Width <= 32)
166 Width = 32;
167 else
168 Width = 64;
169 return Width;
170}
171
172SPIRVType *SPIRVGlobalRegistry::getOpTypeInt(unsigned Width,
173 MachineIRBuilder &MIRBuilder,
174 bool IsSigned) {
175 Width = adjustOpTypeIntWidth(Width);
176 const SPIRVSubtarget &ST =
178 return createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
179 if (Width == 4 && ST.canUseExtension(SPIRV::Extension::SPV_INTEL_int4)) {
180 MIRBuilder.buildInstr(SPIRV::OpExtension)
181 .addImm(SPIRV::Extension::SPV_INTEL_int4);
182 MIRBuilder.buildInstr(SPIRV::OpCapability)
183 .addImm(SPIRV::Capability::Int4TypeINTEL);
184 } else if ((!isPowerOf2_32(Width) || Width < 8) &&
185 ST.canUseExtension(
186 SPIRV::Extension::SPV_ALTERA_arbitrary_precision_integers)) {
187 MIRBuilder.buildInstr(SPIRV::OpExtension)
188 .addImm(SPIRV::Extension::SPV_ALTERA_arbitrary_precision_integers);
189 MIRBuilder.buildInstr(SPIRV::OpCapability)
190 .addImm(SPIRV::Capability::ArbitraryPrecisionIntegersALTERA);
191 }
192 return MIRBuilder.buildInstr(SPIRV::OpTypeInt)
193 .addDef(createTypeVReg(MIRBuilder))
194 .addImm(Width)
195 .addImm(IsSigned ? 1 : 0);
196 });
197}
198
199SPIRVType *SPIRVGlobalRegistry::getOpTypeFloat(uint32_t Width,
200 MachineIRBuilder &MIRBuilder) {
201 return createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
202 return MIRBuilder.buildInstr(SPIRV::OpTypeFloat)
203 .addDef(createTypeVReg(MIRBuilder))
204 .addImm(Width);
205 });
206}
207
208SPIRVType *
209SPIRVGlobalRegistry::getOpTypeFloat(uint32_t Width,
210 MachineIRBuilder &MIRBuilder,
211 SPIRV::FPEncoding::FPEncoding FPEncode) {
212 return createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
213 return MIRBuilder.buildInstr(SPIRV::OpTypeFloat)
214 .addDef(createTypeVReg(MIRBuilder))
215 .addImm(Width)
216 .addImm(FPEncode);
217 });
218}
219
220SPIRVType *SPIRVGlobalRegistry::getOpTypeVoid(MachineIRBuilder &MIRBuilder) {
221 return createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
222 return MIRBuilder.buildInstr(SPIRV::OpTypeVoid)
223 .addDef(createTypeVReg(MIRBuilder));
224 });
225}
226
228 // Other maps that may hold MachineInstr*:
229 // - VRegToTypeMap: We cannot remove the definitions of `MI` from
230 // VRegToTypeMap because some calls to invalidateMachineInstr are replacing MI
231 // with another instruction defining the same register. We expect that if MI
232 // is a type instruction, and it is still referenced in VRegToTypeMap, then
233 // those registers are dead or the VRegToTypeMap is out-of-date. We do not
234 // expect passes to ask for the SPIR-V type of a dead register. If the
235 // VRegToTypeMap is out-of-date already, then there was an error before. We
236 // cannot add an assert to verify this because the VRegToTypeMap can be
237 // out-of-date.
238 // - FunctionToInstr & FunctionToInstrRev: At this point, we should not be
239 // deleting functions. No need to update.
240 // - AliasInstMDMap: Would require a linear search, and the Intel Alias
241 // instruction are not instructions instruction selection will be able to
242 // remove.
243
244 const SPIRVSubtarget &ST = MI->getMF()->getSubtarget<SPIRVSubtarget>();
245 [[maybe_unused]] const SPIRVInstrInfo *TII = ST.getInstrInfo();
246 assert(!TII->isAliasingInstr(*MI) &&
247 "Cannot invalidate aliasing instructions.");
248 assert(MI->getOpcode() != SPIRV::OpFunction &&
249 "Cannot invalidate OpFunction.");
250
251 if (MI->getOpcode() == SPIRV::OpFunctionCall) {
252 if (const auto *F = dyn_cast<Function>(MI->getOperand(2).getGlobal())) {
253 auto It = ForwardCalls.find(F);
254 if (It != ForwardCalls.end()) {
255 It->second.erase(MI);
256 if (It->second.empty())
257 ForwardCalls.erase(It);
258 }
259 }
260 }
261
262 const MachineFunction *MF = MI->getMF();
263 auto It = LastInsertedTypeMap.find(MF);
264 if (It != LastInsertedTypeMap.end() && It->second == MI)
265 LastInsertedTypeMap.erase(MF);
266 // remove from the duplicate tracker to avoid incorrect reuse
267 erase(MI);
268}
269
270SPIRVType *SPIRVGlobalRegistry::createOpType(
271 MachineIRBuilder &MIRBuilder,
272 std::function<MachineInstr *(MachineIRBuilder &)> Op) {
273 auto oldInsertPoint = MIRBuilder.getInsertPt();
274 MachineBasicBlock *OldMBB = &MIRBuilder.getMBB();
275 MachineBasicBlock *NewMBB = &*MIRBuilder.getMF().begin();
276
277 auto LastInsertedType = LastInsertedTypeMap.find(CurMF);
278 if (LastInsertedType != LastInsertedTypeMap.end()) {
279 auto It = LastInsertedType->second->getIterator();
280 // It might happen that this instruction was removed from the first MBB,
281 // hence the Parent's check.
283 if (It->getParent() != NewMBB)
284 InsertAt = oldInsertPoint->getParent() == NewMBB
285 ? oldInsertPoint
286 : getInsertPtValidEnd(NewMBB);
287 else if (It->getNextNode())
288 InsertAt = It->getNextNode()->getIterator();
289 else
290 InsertAt = getInsertPtValidEnd(NewMBB);
291 MIRBuilder.setInsertPt(*NewMBB, InsertAt);
292 } else {
293 MIRBuilder.setInsertPt(*NewMBB, NewMBB->begin());
294 auto Result = LastInsertedTypeMap.try_emplace(CurMF, nullptr);
295 assert(Result.second);
296 LastInsertedType = Result.first;
297 }
298
299 MachineInstr *Type = Op(MIRBuilder);
300 // We expect all users of this function to insert definitions at the insertion
301 // point set above that is always the first MBB.
302 assert(Type->getParent() == NewMBB);
303 LastInsertedType->second = Type;
304
305 MIRBuilder.setInsertPt(*OldMBB, oldInsertPoint);
306 return Type;
307}
308
309SPIRVType *SPIRVGlobalRegistry::getOpTypeVector(uint32_t NumElems,
310 SPIRVType *ElemType,
311 MachineIRBuilder &MIRBuilder) {
312 auto EleOpc = ElemType->getOpcode();
313 (void)EleOpc;
314 assert((EleOpc == SPIRV::OpTypeInt || EleOpc == SPIRV::OpTypeFloat ||
315 EleOpc == SPIRV::OpTypeBool) &&
316 "Invalid vector element type");
317
318 return createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
319 return MIRBuilder.buildInstr(SPIRV::OpTypeVector)
320 .addDef(createTypeVReg(MIRBuilder))
321 .addUse(getSPIRVTypeID(ElemType))
322 .addImm(NumElems);
323 });
324}
325
327 SPIRVType *SpvType,
328 const SPIRVInstrInfo &TII,
329 bool ZeroAsNull) {
330 LLVMContext &Ctx = CurMF->getFunction().getContext();
331 auto *const CF = ConstantFP::get(Ctx, Val);
332 const MachineInstr *MI = findMI(CF, CurMF);
333 if (MI && (MI->getOpcode() == SPIRV::OpConstantNull ||
334 MI->getOpcode() == SPIRV::OpConstantF))
335 return MI->getOperand(0).getReg();
336 return createConstFP(CF, I, SpvType, TII, ZeroAsNull);
337}
338
340 MachineInstr &I, SPIRVType *SpvType,
341 const SPIRVInstrInfo &TII,
342 bool ZeroAsNull) {
343 unsigned BitWidth = getScalarOrVectorBitWidth(SpvType);
344 LLT LLTy = LLT::scalar(BitWidth);
345 Register Res = CurMF->getRegInfo().createGenericVirtualRegister(LLTy);
346 CurMF->getRegInfo().setRegClass(Res, &SPIRV::fIDRegClass);
347 assignSPIRVTypeToVReg(SpvType, Res, *CurMF);
348
349 MachineInstr *DepMI = const_cast<MachineInstr *>(SpvType);
350 MachineIRBuilder MIRBuilder(*DepMI->getParent(), DepMI->getIterator());
351 SPIRVType *NewType =
352 createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
354 // In OpenCL OpConstantNull - Scalar floating point: +0.0 (all bits 0)
355 if (CF->getValue().isPosZero() && ZeroAsNull) {
356 MIB = MIRBuilder.buildInstr(SPIRV::OpConstantNull)
357 .addDef(Res)
358 .addUse(getSPIRVTypeID(SpvType));
359 } else {
360 MIB = MIRBuilder.buildInstr(SPIRV::OpConstantF)
361 .addDef(Res)
362 .addUse(getSPIRVTypeID(SpvType));
365 MIB);
366 }
367 const auto &ST = CurMF->getSubtarget();
368 constrainSelectedInstRegOperands(*MIB, *ST.getInstrInfo(),
369 *ST.getRegisterInfo(),
370 *ST.getRegBankInfo());
371 return MIB;
372 });
373 add(CF, NewType);
374 return Res;
375}
376
378 SPIRVType *SpvType,
379 const SPIRVInstrInfo &TII,
380 bool ZeroAsNull) {
382 auto *const CI = ConstantInt::get(const_cast<IntegerType *>(Ty), Val);
383 const MachineInstr *MI = findMI(CI, CurMF);
384 if (MI && (MI->getOpcode() == SPIRV::OpConstantNull ||
385 MI->getOpcode() == SPIRV::OpConstantI))
386 return MI->getOperand(0).getReg();
387 return createConstInt(CI, I, SpvType, TII, ZeroAsNull);
388}
389
392 SPIRVType *SpvType,
393 const SPIRVInstrInfo &TII,
394 bool ZeroAsNull) {
395 unsigned BitWidth = getScalarOrVectorBitWidth(SpvType);
396 LLT LLTy = LLT::scalar(BitWidth);
397 Register Res = CurMF->getRegInfo().createGenericVirtualRegister(LLTy);
398 CurMF->getRegInfo().setRegClass(Res, &SPIRV::iIDRegClass);
400
401 MachineInstr *DepMI = const_cast<MachineInstr *>(SpvType);
402 MachineIRBuilder MIRBuilder(*DepMI->getParent(), DepMI->getIterator());
403 SPIRVType *NewType =
404 createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
406 if (BitWidth == 1) {
407 MIB = MIRBuilder
408 .buildInstr(CI->isZero() ? SPIRV::OpConstantFalse
409 : SPIRV::OpConstantTrue)
410 .addDef(Res)
411 .addUse(getSPIRVTypeID(SpvType));
412 } else if (!CI->isZero() || !ZeroAsNull) {
413 MIB = MIRBuilder.buildInstr(SPIRV::OpConstantI)
414 .addDef(Res)
415 .addUse(getSPIRVTypeID(SpvType));
416 addNumImm(APInt(BitWidth, CI->getZExtValue()), MIB);
417 } else {
418 MIB = MIRBuilder.buildInstr(SPIRV::OpConstantNull)
419 .addDef(Res)
420 .addUse(getSPIRVTypeID(SpvType));
421 }
422 const auto &ST = CurMF->getSubtarget();
423 constrainSelectedInstRegOperands(*MIB, *ST.getInstrInfo(),
424 *ST.getRegisterInfo(),
425 *ST.getRegBankInfo());
426 return MIB;
427 });
428 add(CI, NewType);
429 return Res;
430}
431
433 MachineIRBuilder &MIRBuilder,
434 SPIRVType *SpvType, bool EmitIR,
435 bool ZeroAsNull) {
436 assert(SpvType);
437 auto &MF = MIRBuilder.getMF();
439 auto *const CI = ConstantInt::get(const_cast<IntegerType *>(Ty), Val);
440 Register Res = find(CI, &MF);
441 if (Res.isValid())
442 return Res;
443
444 unsigned BitWidth = getScalarOrVectorBitWidth(SpvType);
445 LLT LLTy = LLT::scalar(BitWidth);
446 MachineRegisterInfo &MRI = MF.getRegInfo();
447 Res = MRI.createGenericVirtualRegister(LLTy);
448 MRI.setRegClass(Res, &SPIRV::iIDRegClass);
449 assignTypeToVReg(Ty, Res, MIRBuilder, SPIRV::AccessQualifier::ReadWrite,
450 EmitIR);
451
452 SPIRVType *NewType =
453 createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
454 if (EmitIR)
455 return MIRBuilder.buildConstant(Res, *CI);
456 Register SpvTypeReg = getSPIRVTypeID(SpvType);
458 if (Val || !ZeroAsNull) {
459 MIB = MIRBuilder.buildInstr(SPIRV::OpConstantI)
460 .addDef(Res)
461 .addUse(SpvTypeReg);
462 addNumImm(APInt(BitWidth, Val), MIB);
463 } else {
464 MIB = MIRBuilder.buildInstr(SPIRV::OpConstantNull)
465 .addDef(Res)
466 .addUse(SpvTypeReg);
467 }
468 const auto &Subtarget = CurMF->getSubtarget();
469 constrainSelectedInstRegOperands(*MIB, *Subtarget.getInstrInfo(),
470 *Subtarget.getRegisterInfo(),
471 *Subtarget.getRegBankInfo());
472 return MIB;
473 });
474 add(CI, NewType);
475 return Res;
476}
477
479 MachineIRBuilder &MIRBuilder,
480 SPIRVType *SpvType) {
481 auto &MF = MIRBuilder.getMF();
482 LLVMContext &Ctx = MF.getFunction().getContext();
483 if (!SpvType)
484 SpvType = getOrCreateSPIRVType(Type::getFloatTy(Ctx), MIRBuilder,
485 SPIRV::AccessQualifier::ReadWrite, true);
486 auto *const CF = ConstantFP::get(Ctx, Val);
487 Register Res = find(CF, &MF);
488 if (Res.isValid())
489 return Res;
490
492 Res = MF.getRegInfo().createGenericVirtualRegister(LLTy);
493 MF.getRegInfo().setRegClass(Res, &SPIRV::fIDRegClass);
494 assignSPIRVTypeToVReg(SpvType, Res, MF);
495
496 SPIRVType *NewType =
497 createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
499 MIB = MIRBuilder.buildInstr(SPIRV::OpConstantF)
500 .addDef(Res)
501 .addUse(getSPIRVTypeID(SpvType));
502 addNumImm(CF->getValueAPF().bitcastToAPInt(), MIB);
503 return MIB;
504 });
505 add(CF, NewType);
506 return Res;
507}
508
509Register SPIRVGlobalRegistry::getOrCreateBaseRegister(
510 Constant *Val, MachineInstr &I, SPIRVType *SpvType,
511 const SPIRVInstrInfo &TII, unsigned BitWidth, bool ZeroAsNull) {
512 SPIRVType *Type = SpvType;
513 if (SpvType->getOpcode() == SPIRV::OpTypeVector ||
514 SpvType->getOpcode() == SPIRV::OpTypeArray) {
515 auto EleTypeReg = SpvType->getOperand(1).getReg();
516 Type = getSPIRVTypeForVReg(EleTypeReg);
517 }
518 if (Type->getOpcode() == SPIRV::OpTypeFloat) {
520 return getOrCreateConstFP(cast<ConstantFP>(Val)->getValue(), I, SpvBaseType,
521 TII, ZeroAsNull);
522 }
523 assert(Type->getOpcode() == SPIRV::OpTypeInt);
526 SpvBaseType, TII, ZeroAsNull);
527}
528
529Register SPIRVGlobalRegistry::getOrCreateCompositeOrNull(
530 Constant *Val, MachineInstr &I, SPIRVType *SpvType,
531 const SPIRVInstrInfo &TII, Constant *CA, unsigned BitWidth,
532 unsigned ElemCnt, bool ZeroAsNull) {
533 if (Register R = find(CA, CurMF); R.isValid())
534 return R;
535
536 bool IsNull = Val->isNullValue() && ZeroAsNull;
537 Register ElemReg;
538 if (!IsNull)
539 ElemReg =
540 getOrCreateBaseRegister(Val, I, SpvType, TII, BitWidth, ZeroAsNull);
541
542 LLT LLTy = LLT::scalar(64);
543 Register Res = CurMF->getRegInfo().createGenericVirtualRegister(LLTy);
544 CurMF->getRegInfo().setRegClass(Res, getRegClass(SpvType));
545 assignSPIRVTypeToVReg(SpvType, Res, *CurMF);
546
547 MachineInstr *DepMI = const_cast<MachineInstr *>(SpvType);
548 MachineIRBuilder MIRBuilder(*DepMI->getParent(), DepMI->getIterator());
549 const MachineInstr *NewMI =
550 createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
551 MachineInstrBuilder MIB;
552 if (!IsNull) {
553 MIB = MIRBuilder.buildInstr(SPIRV::OpConstantComposite)
554 .addDef(Res)
555 .addUse(getSPIRVTypeID(SpvType));
556 for (unsigned i = 0; i < ElemCnt; ++i)
557 MIB.addUse(ElemReg);
558 } else {
559 MIB = MIRBuilder.buildInstr(SPIRV::OpConstantNull)
560 .addDef(Res)
561 .addUse(getSPIRVTypeID(SpvType));
562 }
563 const auto &Subtarget = CurMF->getSubtarget();
564 constrainSelectedInstRegOperands(*MIB, *Subtarget.getInstrInfo(),
565 *Subtarget.getRegisterInfo(),
566 *Subtarget.getRegBankInfo());
567 return MIB;
568 });
569 add(CA, NewMI);
570 return Res;
571}
572
575 SPIRVType *SpvType,
576 const SPIRVInstrInfo &TII,
577 bool ZeroAsNull) {
578 const Type *LLVMTy = getTypeForSPIRVType(SpvType);
579 assert(LLVMTy->isVectorTy());
580 const FixedVectorType *LLVMVecTy = cast<FixedVectorType>(LLVMTy);
581 Type *LLVMBaseTy = LLVMVecTy->getElementType();
582 assert(LLVMBaseTy->isIntegerTy());
583 auto *ConstVal = ConstantInt::get(LLVMBaseTy, Val);
584 auto *ConstVec =
585 ConstantVector::getSplat(LLVMVecTy->getElementCount(), ConstVal);
586 unsigned BW = getScalarOrVectorBitWidth(SpvType);
587 return getOrCreateCompositeOrNull(ConstVal, I, SpvType, TII, ConstVec, BW,
588 SpvType->getOperand(2).getImm(),
589 ZeroAsNull);
590}
591
594 SPIRVType *SpvType,
595 const SPIRVInstrInfo &TII,
596 bool ZeroAsNull) {
597 const Type *LLVMTy = getTypeForSPIRVType(SpvType);
598 assert(LLVMTy->isVectorTy());
599 const FixedVectorType *LLVMVecTy = cast<FixedVectorType>(LLVMTy);
600 Type *LLVMBaseTy = LLVMVecTy->getElementType();
601 assert(LLVMBaseTy->isFloatingPointTy());
602 auto *ConstVal = ConstantFP::get(LLVMBaseTy, Val);
603 auto *ConstVec =
604 ConstantVector::getSplat(LLVMVecTy->getElementCount(), ConstVal);
605 unsigned BW = getScalarOrVectorBitWidth(SpvType);
606 return getOrCreateCompositeOrNull(ConstVal, I, SpvType, TII, ConstVec, BW,
607 SpvType->getOperand(2).getImm(),
608 ZeroAsNull);
609}
610
612 uint64_t Val, size_t Num, MachineInstr &I, SPIRVType *SpvType,
613 const SPIRVInstrInfo &TII) {
614 const Type *LLVMTy = getTypeForSPIRVType(SpvType);
615 assert(LLVMTy->isArrayTy());
616 const ArrayType *LLVMArrTy = cast<ArrayType>(LLVMTy);
617 Type *LLVMBaseTy = LLVMArrTy->getElementType();
618 Constant *CI = ConstantInt::get(LLVMBaseTy, Val);
619 SPIRVType *SpvBaseTy = getSPIRVTypeForVReg(SpvType->getOperand(1).getReg());
620 unsigned BW = getScalarOrVectorBitWidth(SpvBaseTy);
621 // The following is reasonably unique key that is better that [Val]. The naive
622 // alternative would be something along the lines of:
623 // SmallVector<Constant *> NumCI(Num, CI);
624 // Constant *UniqueKey =
625 // ConstantArray::get(const_cast<ArrayType*>(LLVMArrTy), NumCI);
626 // that would be a truly unique but dangerous key, because it could lead to
627 // the creation of constants of arbitrary length (that is, the parameter of
628 // memset) which were missing in the original module.
630 {PoisonValue::get(const_cast<ArrayType *>(LLVMArrTy)),
631 ConstantInt::get(LLVMBaseTy, Val), ConstantInt::get(LLVMBaseTy, Num)});
632 return getOrCreateCompositeOrNull(CI, I, SpvType, TII, UniqueKey, BW,
633 LLVMArrTy->getNumElements());
634}
635
636Register SPIRVGlobalRegistry::getOrCreateIntCompositeOrNull(
637 uint64_t Val, MachineIRBuilder &MIRBuilder, SPIRVType *SpvType, bool EmitIR,
638 Constant *CA, unsigned BitWidth, unsigned ElemCnt) {
639 if (Register R = find(CA, CurMF); R.isValid())
640 return R;
641
642 Register ElemReg;
643 if (Val || EmitIR) {
644 SPIRVType *SpvBaseType = getOrCreateSPIRVIntegerType(BitWidth, MIRBuilder);
645 ElemReg = buildConstantInt(Val, MIRBuilder, SpvBaseType, EmitIR);
646 }
647 LLT LLTy = EmitIR ? LLT::fixed_vector(ElemCnt, BitWidth) : LLT::scalar(64);
648 Register Res = CurMF->getRegInfo().createGenericVirtualRegister(LLTy);
649 CurMF->getRegInfo().setRegClass(Res, &SPIRV::iIDRegClass);
650 assignSPIRVTypeToVReg(SpvType, Res, *CurMF);
651
652 const MachineInstr *NewMI =
653 createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
654 if (EmitIR)
655 return MIRBuilder.buildSplatBuildVector(Res, ElemReg);
656
657 if (Val) {
658 auto MIB = MIRBuilder.buildInstr(SPIRV::OpConstantComposite)
659 .addDef(Res)
660 .addUse(getSPIRVTypeID(SpvType));
661 for (unsigned i = 0; i < ElemCnt; ++i)
662 MIB.addUse(ElemReg);
663 return MIB;
664 }
665
666 return MIRBuilder.buildInstr(SPIRV::OpConstantNull)
667 .addDef(Res)
668 .addUse(getSPIRVTypeID(SpvType));
669 });
670 add(CA, NewMI);
671 return Res;
672}
673
676 MachineIRBuilder &MIRBuilder,
677 SPIRVType *SpvType, bool EmitIR) {
678 const Type *LLVMTy = getTypeForSPIRVType(SpvType);
679 assert(LLVMTy->isVectorTy());
680 const FixedVectorType *LLVMVecTy = cast<FixedVectorType>(LLVMTy);
681 Type *LLVMBaseTy = LLVMVecTy->getElementType();
682 const auto ConstInt = ConstantInt::get(LLVMBaseTy, Val);
683 auto ConstVec =
684 ConstantVector::getSplat(LLVMVecTy->getElementCount(), ConstInt);
685 unsigned BW = getScalarOrVectorBitWidth(SpvType);
686 return getOrCreateIntCompositeOrNull(Val, MIRBuilder, SpvType, EmitIR,
687 ConstVec, BW,
688 SpvType->getOperand(2).getImm());
689}
690
693 SPIRVType *SpvType) {
694 const Type *Ty = getTypeForSPIRVType(SpvType);
695 unsigned AddressSpace = typeToAddressSpace(Ty);
696 Type *ElemTy = ::getPointeeType(Ty);
697 assert(ElemTy);
700 Register Res = find(CP, CurMF);
701 if (Res.isValid())
702 return Res;
703
704 LLT LLTy = LLT::pointer(AddressSpace, PointerSize);
705 Res = CurMF->getRegInfo().createGenericVirtualRegister(LLTy);
706 CurMF->getRegInfo().setRegClass(Res, &SPIRV::pIDRegClass);
707 assignSPIRVTypeToVReg(SpvType, Res, *CurMF);
708
709 const MachineInstr *NewMI =
710 createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
711 return MIRBuilder.buildInstr(SPIRV::OpConstantNull)
712 .addDef(Res)
713 .addUse(getSPIRVTypeID(SpvType));
714 });
715 add(CP, NewMI);
716 return Res;
717}
718
721 unsigned Param, unsigned FilerMode,
722 MachineIRBuilder &MIRBuilder) {
723 auto Sampler =
724 ResReg.isValid()
725 ? ResReg
726 : MIRBuilder.getMRI()->createVirtualRegister(&SPIRV::iIDRegClass);
727 SPIRVType *TypeSampler = getOrCreateOpTypeSampler(MIRBuilder);
728 Register TypeSamplerReg = getSPIRVTypeID(TypeSampler);
729 // We cannot use createOpType() logic here, because of the
730 // GlobalISel/IRTranslator.cpp check for a tail call that expects that
731 // MIRBuilder.getInsertPt() has a previous instruction. If this constant is
732 // inserted as a result of "__translate_sampler_initializer()" this would
733 // break this IRTranslator assumption.
734 MIRBuilder.buildInstr(SPIRV::OpConstantSampler)
735 .addDef(Sampler)
736 .addUse(TypeSamplerReg)
738 .addImm(Param)
739 .addImm(FilerMode);
740 return Sampler;
741}
742
744 Register ResVReg, SPIRVType *BaseType, StringRef Name,
745 const GlobalValue *GV, SPIRV::StorageClass::StorageClass Storage,
746 const MachineInstr *Init, bool IsConst,
747 const std::optional<SPIRV::LinkageType::LinkageType> &LinkageType,
748 MachineIRBuilder &MIRBuilder, bool IsInstSelector) {
749 const GlobalVariable *GVar = nullptr;
750 if (GV) {
752 } else {
753 // If GV is not passed explicitly, use the name to find or construct
754 // the global variable.
755 Module *M = MIRBuilder.getMF().getFunction().getParent();
756 GVar = M->getGlobalVariable(Name);
757 if (GVar == nullptr) {
758 const Type *Ty = getTypeForSPIRVType(BaseType); // TODO: check type.
759 // Module takes ownership of the global var.
760 GVar = new GlobalVariable(*M, const_cast<Type *>(Ty), false,
762 Twine(Name));
763 }
764 GV = GVar;
765 }
766
767 const MachineFunction *MF = &MIRBuilder.getMF();
768 Register Reg = find(GVar, MF);
769 if (Reg.isValid()) {
770 if (Reg != ResVReg)
771 MIRBuilder.buildCopy(ResVReg, Reg);
772 return ResVReg;
773 }
774
775 auto MIB = MIRBuilder.buildInstr(SPIRV::OpVariable)
776 .addDef(ResVReg)
778 .addImm(static_cast<uint32_t>(Storage));
779 if (Init)
780 MIB.addUse(Init->getOperand(0).getReg());
781 // ISel may introduce a new register on this step, so we need to add it to
782 // DT and correct its type avoiding fails on the next stage.
783 if (IsInstSelector) {
784 const auto &Subtarget = CurMF->getSubtarget();
785 constrainSelectedInstRegOperands(*MIB, *Subtarget.getInstrInfo(),
786 *Subtarget.getRegisterInfo(),
787 *Subtarget.getRegBankInfo());
788 }
789 add(GVar, MIB);
790
791 Reg = MIB->getOperand(0).getReg();
792 addGlobalObject(GVar, MF, Reg);
793
794 // Set to Reg the same type as ResVReg has.
795 auto MRI = MIRBuilder.getMRI();
796 if (Reg != ResVReg) {
797 LLT RegLLTy =
798 LLT::pointer(MRI->getType(ResVReg).getAddressSpace(), getPointerSize());
799 MRI->setType(Reg, RegLLTy);
800 assignSPIRVTypeToVReg(BaseType, Reg, MIRBuilder.getMF());
801 } else {
802 // Our knowledge about the type may be updated.
803 // If that's the case, we need to update a type
804 // associated with the register.
805 SPIRVType *DefType = getSPIRVTypeForVReg(ResVReg);
806 if (!DefType || DefType != BaseType)
807 assignSPIRVTypeToVReg(BaseType, Reg, MIRBuilder.getMF());
808 }
809
810 // If it's a global variable with name, output OpName for it.
811 if (GVar && GVar->hasName())
812 buildOpName(Reg, GVar->getName(), MIRBuilder);
813
814 // Output decorations for the GV.
815 // TODO: maybe move to GenerateDecorations pass.
816 const SPIRVSubtarget &ST =
818 if (IsConst && !ST.isShader())
819 buildOpDecorate(Reg, MIRBuilder, SPIRV::Decoration::Constant, {});
820
821 if (GVar && GVar->getAlign().valueOrOne().value() != 1 && !ST.isShader()) {
822 unsigned Alignment = (unsigned)GVar->getAlign().valueOrOne().value();
823 buildOpDecorate(Reg, MIRBuilder, SPIRV::Decoration::Alignment, {Alignment});
824 }
825
826 if (LinkageType)
827 buildOpDecorate(Reg, MIRBuilder, SPIRV::Decoration::LinkageAttributes,
828 {static_cast<uint32_t>(*LinkageType)}, Name);
829
830 SPIRV::BuiltIn::BuiltIn BuiltInId;
831 if (getSpirvBuiltInIdByName(Name, BuiltInId))
832 buildOpDecorate(Reg, MIRBuilder, SPIRV::Decoration::BuiltIn,
833 {static_cast<uint32_t>(BuiltInId)});
834
835 // If it's a global variable with "spirv.Decorations" metadata node
836 // recognize it as a SPIR-V friendly LLVM IR and parse "spirv.Decorations"
837 // arguments.
838 MDNode *GVarMD = nullptr;
839 if (GVar && (GVarMD = GVar->getMetadata("spirv.Decorations")) != nullptr)
840 buildOpSpirvDecorations(Reg, MIRBuilder, GVarMD, ST);
841
842 return Reg;
843}
844
845// Returns a name based on the Type. Notes that this does not look at
846// decorations, and will return the same string for two types that are the same
847// except for decorations.
849 const SPIRVType *VarType, uint32_t Set, uint32_t Binding, StringRef Name,
850 MachineIRBuilder &MIRBuilder) {
851 Register VarReg =
852 MIRBuilder.getMRI()->createVirtualRegister(&SPIRV::iIDRegClass);
853
854 buildGlobalVariable(VarReg, VarType, Name, nullptr,
855 getPointerStorageClass(VarType), nullptr, false,
856 std::nullopt, MIRBuilder, false);
857
858 buildOpDecorate(VarReg, MIRBuilder, SPIRV::Decoration::DescriptorSet, {Set});
859 buildOpDecorate(VarReg, MIRBuilder, SPIRV::Decoration::Binding, {Binding});
860 return VarReg;
861}
862
863// TODO: Double check the calls to getOpTypeArray to make sure that `ElemType`
864// is explicitly laid out when required.
865SPIRVType *SPIRVGlobalRegistry::getOpTypeArray(uint32_t NumElems,
866 SPIRVType *ElemType,
867 MachineIRBuilder &MIRBuilder,
868 bool ExplicitLayoutRequired,
869 bool EmitIR) {
870 assert((ElemType->getOpcode() != SPIRV::OpTypeVoid) &&
871 "Invalid array element type");
872 SPIRVType *SpvTypeInt32 = getOrCreateSPIRVIntegerType(32, MIRBuilder);
873 SPIRVType *ArrayType = nullptr;
874 const SPIRVSubtarget &ST =
876 if (NumElems != 0) {
877 Register NumElementsVReg =
878 buildConstantInt(NumElems, MIRBuilder, SpvTypeInt32, EmitIR);
879 ArrayType = createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
880 return MIRBuilder.buildInstr(SPIRV::OpTypeArray)
881 .addDef(createTypeVReg(MIRBuilder))
882 .addUse(getSPIRVTypeID(ElemType))
883 .addUse(NumElementsVReg);
884 });
885 } else {
886 if (!ST.isShader()) {
888 "Runtime arrays are not allowed in non-shader "
889 "SPIR-V modules");
890 return nullptr;
891 }
892 ArrayType = createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
893 return MIRBuilder.buildInstr(SPIRV::OpTypeRuntimeArray)
894 .addDef(createTypeVReg(MIRBuilder))
895 .addUse(getSPIRVTypeID(ElemType));
896 });
897 }
898
899 if (ExplicitLayoutRequired && !isResourceType(ElemType)) {
900 Type *ET = const_cast<Type *>(getTypeForSPIRVType(ElemType));
901 addArrayStrideDecorations(ArrayType->defs().begin()->getReg(), ET,
902 MIRBuilder);
903 }
904
905 return ArrayType;
906}
907
908SPIRVType *SPIRVGlobalRegistry::getOpTypeOpaque(const StructType *Ty,
909 MachineIRBuilder &MIRBuilder) {
910 assert(Ty->hasName());
911 const StringRef Name = Ty->hasName() ? Ty->getName() : "";
912 Register ResVReg = createTypeVReg(MIRBuilder);
913 return createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
914 auto MIB = MIRBuilder.buildInstr(SPIRV::OpTypeOpaque).addDef(ResVReg);
915 addStringImm(Name, MIB);
916 buildOpName(ResVReg, Name, MIRBuilder);
917 return MIB;
918 });
919}
920
921SPIRVType *SPIRVGlobalRegistry::getOpTypeStruct(
922 const StructType *Ty, MachineIRBuilder &MIRBuilder,
923 SPIRV::AccessQualifier::AccessQualifier AccQual,
924 StructOffsetDecorator Decorator, bool EmitIR) {
925 Type *OriginalElementType = nullptr;
926 uint64_t TotalSize = 0;
927 if (matchPeeledArrayPattern(Ty, OriginalElementType, TotalSize)) {
928 SPIRVType *ElementSPIRVType = findSPIRVType(
929 OriginalElementType, MIRBuilder, AccQual,
930 /* ExplicitLayoutRequired= */ Decorator != nullptr, EmitIR);
931 return getOpTypeArray(TotalSize, ElementSPIRVType, MIRBuilder,
932 /*ExplicitLayoutRequired=*/Decorator != nullptr,
933 EmitIR);
934 }
935
936 const SPIRVSubtarget &ST =
938 SmallVector<Register, 4> FieldTypes;
939 constexpr unsigned MaxWordCount = UINT16_MAX;
940 const size_t NumElements = Ty->getNumElements();
941
942 size_t MaxNumElements = MaxWordCount - 2;
943 size_t SPIRVStructNumElements = NumElements;
944 if (NumElements > MaxNumElements) {
945 // Do adjustments for continued instructions.
946 SPIRVStructNumElements = MaxNumElements;
947 MaxNumElements = MaxWordCount - 1;
948 }
949
950 for (const auto &Elem : Ty->elements()) {
951 SPIRVType *ElemTy = findSPIRVType(
952 toTypedPointer(Elem), MIRBuilder, AccQual,
953 /* ExplicitLayoutRequired= */ Decorator != nullptr, EmitIR);
954 assert(ElemTy && ElemTy->getOpcode() != SPIRV::OpTypeVoid &&
955 "Invalid struct element type");
956 FieldTypes.push_back(getSPIRVTypeID(ElemTy));
957 }
958 Register ResVReg = createTypeVReg(MIRBuilder);
959 if (Ty->hasName())
960 buildOpName(ResVReg, Ty->getName(), MIRBuilder);
961 if (Ty->isPacked() && !ST.isShader())
962 buildOpDecorate(ResVReg, MIRBuilder, SPIRV::Decoration::CPacked, {});
963
964 SPIRVType *SPVType =
965 createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
966 auto MIBStruct =
967 MIRBuilder.buildInstr(SPIRV::OpTypeStruct).addDef(ResVReg);
968 for (size_t I = 0; I < SPIRVStructNumElements; ++I)
969 MIBStruct.addUse(FieldTypes[I]);
970 for (size_t I = SPIRVStructNumElements; I < NumElements;
971 I += MaxNumElements) {
972 auto MIBCont =
973 MIRBuilder.buildInstr(SPIRV::OpTypeStructContinuedINTEL);
974 for (size_t J = I; J < std::min(I + MaxNumElements, NumElements); ++J)
975 MIBCont.addUse(FieldTypes[I]);
976 }
977 return MIBStruct;
978 });
979
980 if (Decorator)
981 Decorator(SPVType->defs().begin()->getReg());
982
983 return SPVType;
984}
985
986SPIRVType *SPIRVGlobalRegistry::getOrCreateSpecialType(
987 const Type *Ty, MachineIRBuilder &MIRBuilder,
988 SPIRV::AccessQualifier::AccessQualifier AccQual) {
989 assert(isSpecialOpaqueType(Ty) && "Not a special opaque builtin type");
990 return SPIRV::lowerBuiltinType(Ty, AccQual, MIRBuilder, this);
991}
992
993SPIRVType *SPIRVGlobalRegistry::getOpTypePointer(
994 SPIRV::StorageClass::StorageClass SC, SPIRVType *ElemType,
995 MachineIRBuilder &MIRBuilder, Register Reg) {
996 if (!Reg.isValid())
997 Reg = createTypeVReg(MIRBuilder);
998
999 return createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
1000 return MIRBuilder.buildInstr(SPIRV::OpTypePointer)
1001 .addDef(Reg)
1002 .addImm(static_cast<uint32_t>(SC))
1003 .addUse(getSPIRVTypeID(ElemType));
1004 });
1005}
1006
1007SPIRVType *SPIRVGlobalRegistry::getOpTypeForwardPointer(
1008 SPIRV::StorageClass::StorageClass SC, MachineIRBuilder &MIRBuilder) {
1009 return createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
1010 return MIRBuilder.buildInstr(SPIRV::OpTypeForwardPointer)
1011 .addUse(createTypeVReg(MIRBuilder))
1012 .addImm(static_cast<uint32_t>(SC));
1013 });
1014}
1015
1016SPIRVType *SPIRVGlobalRegistry::getOpTypeFunction(
1017 const FunctionType *Ty, SPIRVType *RetType,
1018 const SmallVectorImpl<SPIRVType *> &ArgTypes,
1019 MachineIRBuilder &MIRBuilder) {
1020 if (Ty->isVarArg()) {
1021 Function &Fn = MIRBuilder.getMF().getFunction();
1022 Ty->getContext().diagnose(DiagnosticInfoUnsupported(
1023 Fn, "SPIR-V does not support variadic functions",
1024 MIRBuilder.getDebugLoc()));
1025 }
1026 return createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
1027 auto MIB = MIRBuilder.buildInstr(SPIRV::OpTypeFunction)
1028 .addDef(createTypeVReg(MIRBuilder))
1029 .addUse(getSPIRVTypeID(RetType));
1030 for (const SPIRVType *ArgType : ArgTypes)
1031 MIB.addUse(getSPIRVTypeID(ArgType));
1032 return MIB;
1033 });
1034}
1035
1037 const Type *Ty, SPIRVType *RetType,
1038 const SmallVectorImpl<SPIRVType *> &ArgTypes,
1039 MachineIRBuilder &MIRBuilder) {
1040 if (const MachineInstr *MI = findMI(Ty, false, &MIRBuilder.getMF()))
1041 return MI;
1042 const MachineInstr *NewMI =
1043 getOpTypeFunction(cast<FunctionType>(Ty), RetType, ArgTypes, MIRBuilder);
1044 add(Ty, false, NewMI);
1045 return finishCreatingSPIRVType(Ty, NewMI);
1046}
1047
1048SPIRVType *SPIRVGlobalRegistry::findSPIRVType(
1049 const Type *Ty, MachineIRBuilder &MIRBuilder,
1050 SPIRV::AccessQualifier::AccessQualifier AccQual,
1051 bool ExplicitLayoutRequired, bool EmitIR) {
1052 Ty = adjustIntTypeByWidth(Ty);
1053 // TODO: findMI needs to know if a layout is required.
1054 if (const MachineInstr *MI =
1055 findMI(Ty, ExplicitLayoutRequired, &MIRBuilder.getMF()))
1056 return MI;
1057 if (auto It = ForwardPointerTypes.find(Ty); It != ForwardPointerTypes.end())
1058 return It->second;
1059 return restOfCreateSPIRVType(Ty, MIRBuilder, AccQual, ExplicitLayoutRequired,
1060 EmitIR);
1061}
1062
1064 assert(SpirvType && "Attempting to get type id for nullptr type.");
1065 if (SpirvType->getOpcode() == SPIRV::OpTypeForwardPointer ||
1066 SpirvType->getOpcode() == SPIRV::OpTypeStructContinuedINTEL)
1067 return SpirvType->uses().begin()->getReg();
1068 return SpirvType->defs().begin()->getReg();
1069}
1070
1071// We need to use a new LLVM integer type if there is a mismatch between
1072// number of bits in LLVM and SPIRV integer types to let DuplicateTracker
1073// ensure uniqueness of a SPIRV type by the corresponding LLVM type. Without
1074// such an adjustment SPIRVGlobalRegistry::getOpTypeInt() could create the
1075// same "OpTypeInt 8" type for a series of LLVM integer types with number of
1076// bits less than 8. This would lead to duplicate type definitions
1077// eventually due to the method that DuplicateTracker utilizes to reason
1078// about uniqueness of type records.
1079const Type *SPIRVGlobalRegistry::adjustIntTypeByWidth(const Type *Ty) const {
1080 if (auto IType = dyn_cast<IntegerType>(Ty)) {
1081 unsigned SrcBitWidth = IType->getBitWidth();
1082 if (SrcBitWidth > 1) {
1083 unsigned BitWidth = adjustOpTypeIntWidth(SrcBitWidth);
1084 // Maybe change source LLVM type to keep DuplicateTracker consistent.
1085 if (SrcBitWidth != BitWidth)
1086 Ty = IntegerType::get(Ty->getContext(), BitWidth);
1087 }
1088 }
1089 return Ty;
1090}
1091
1092SPIRVType *SPIRVGlobalRegistry::createSPIRVType(
1093 const Type *Ty, MachineIRBuilder &MIRBuilder,
1094 SPIRV::AccessQualifier::AccessQualifier AccQual,
1095 bool ExplicitLayoutRequired, bool EmitIR) {
1096 if (isSpecialOpaqueType(Ty))
1097 return getOrCreateSpecialType(Ty, MIRBuilder, AccQual);
1098
1099 if (const MachineInstr *MI =
1100 findMI(Ty, ExplicitLayoutRequired, &MIRBuilder.getMF()))
1101 return MI;
1102
1103 if (auto IType = dyn_cast<IntegerType>(Ty)) {
1104 const unsigned Width = IType->getBitWidth();
1105 return Width == 1 ? getOpTypeBool(MIRBuilder)
1106 : getOpTypeInt(Width, MIRBuilder, false);
1107 }
1108 if (Ty->isFloatingPointTy()) {
1109 if (Ty->isBFloatTy()) {
1110 return getOpTypeFloat(Ty->getPrimitiveSizeInBits(), MIRBuilder,
1111 SPIRV::FPEncoding::BFloat16KHR);
1112 } else {
1113 return getOpTypeFloat(Ty->getPrimitiveSizeInBits(), MIRBuilder);
1114 }
1115 }
1116 if (Ty->isVoidTy())
1117 return getOpTypeVoid(MIRBuilder);
1118 if (Ty->isVectorTy()) {
1119 SPIRVType *El =
1120 findSPIRVType(cast<FixedVectorType>(Ty)->getElementType(), MIRBuilder,
1121 AccQual, ExplicitLayoutRequired, EmitIR);
1122 return getOpTypeVector(cast<FixedVectorType>(Ty)->getNumElements(), El,
1123 MIRBuilder);
1124 }
1125 if (Ty->isArrayTy()) {
1126 SPIRVType *El = findSPIRVType(Ty->getArrayElementType(), MIRBuilder,
1127 AccQual, ExplicitLayoutRequired, EmitIR);
1128 return getOpTypeArray(Ty->getArrayNumElements(), El, MIRBuilder,
1129 ExplicitLayoutRequired, EmitIR);
1130 }
1131 if (auto SType = dyn_cast<StructType>(Ty)) {
1132 if (SType->isOpaque())
1133 return getOpTypeOpaque(SType, MIRBuilder);
1134
1135 StructOffsetDecorator Decorator = nullptr;
1136 if (ExplicitLayoutRequired) {
1137 Decorator = [&MIRBuilder, SType, this](Register Reg) {
1138 addStructOffsetDecorations(Reg, const_cast<StructType *>(SType),
1139 MIRBuilder);
1140 };
1141 }
1142 return getOpTypeStruct(SType, MIRBuilder, AccQual, std::move(Decorator),
1143 EmitIR);
1144 }
1145 if (auto FType = dyn_cast<FunctionType>(Ty)) {
1146 SPIRVType *RetTy = findSPIRVType(FType->getReturnType(), MIRBuilder,
1147 AccQual, ExplicitLayoutRequired, EmitIR);
1148 SmallVector<SPIRVType *, 4> ParamTypes;
1149 for (const auto &ParamTy : FType->params())
1150 ParamTypes.push_back(findSPIRVType(ParamTy, MIRBuilder, AccQual,
1151 ExplicitLayoutRequired, EmitIR));
1152 return getOpTypeFunction(FType, RetTy, ParamTypes, MIRBuilder);
1153 }
1154
1155 unsigned AddrSpace = typeToAddressSpace(Ty);
1156 SPIRVType *SpvElementType = nullptr;
1157 if (Type *ElemTy = ::getPointeeType(Ty))
1158 SpvElementType = getOrCreateSPIRVType(ElemTy, MIRBuilder, AccQual, EmitIR);
1159 else
1160 SpvElementType = getOrCreateSPIRVIntegerType(8, MIRBuilder);
1161
1162 // Get access to information about available extensions
1163 const SPIRVSubtarget *ST =
1164 static_cast<const SPIRVSubtarget *>(&MIRBuilder.getMF().getSubtarget());
1165 auto SC = addressSpaceToStorageClass(AddrSpace, *ST);
1166
1167 Type *ElemTy = ::getPointeeType(Ty);
1168 if (!ElemTy) {
1169 ElemTy = Type::getInt8Ty(MIRBuilder.getContext());
1170 }
1171
1172 // If we have forward pointer associated with this type, use its register
1173 // operand to create OpTypePointer.
1174 if (auto It = ForwardPointerTypes.find(Ty); It != ForwardPointerTypes.end()) {
1175 Register Reg = getSPIRVTypeID(It->second);
1176 // TODO: what does getOpTypePointer do?
1177 return getOpTypePointer(SC, SpvElementType, MIRBuilder, Reg);
1178 }
1179
1180 return getOrCreateSPIRVPointerType(ElemTy, MIRBuilder, SC);
1181}
1182
1183SPIRVType *SPIRVGlobalRegistry::restOfCreateSPIRVType(
1184 const Type *Ty, MachineIRBuilder &MIRBuilder,
1185 SPIRV::AccessQualifier::AccessQualifier AccessQual,
1186 bool ExplicitLayoutRequired, bool EmitIR) {
1187 // TODO: Could this create a problem if one requires an explicit layout, and
1188 // the next time it does not?
1189 if (TypesInProcessing.count(Ty) && !isPointerTyOrWrapper(Ty))
1190 return nullptr;
1191 TypesInProcessing.insert(Ty);
1192 SPIRVType *SpirvType = createSPIRVType(Ty, MIRBuilder, AccessQual,
1193 ExplicitLayoutRequired, EmitIR);
1194 TypesInProcessing.erase(Ty);
1195 VRegToTypeMap[&MIRBuilder.getMF()][getSPIRVTypeID(SpirvType)] = SpirvType;
1196
1197 // TODO: We could end up with two SPIR-V types pointing to the same llvm type.
1198 // Is that a problem?
1199 SPIRVToLLVMType[SpirvType] = unifyPtrType(Ty);
1200
1201 if (SpirvType->getOpcode() == SPIRV::OpTypeForwardPointer ||
1202 findMI(Ty, false, &MIRBuilder.getMF()) || isSpecialOpaqueType(Ty))
1203 return SpirvType;
1204
1205 if (auto *ExtTy = dyn_cast<TargetExtType>(Ty);
1206 ExtTy && isTypedPointerWrapper(ExtTy))
1207 add(ExtTy->getTypeParameter(0), ExtTy->getIntParameter(0), SpirvType);
1208 else if (!isPointerTy(Ty))
1209 add(Ty, ExplicitLayoutRequired, SpirvType);
1210 else if (isTypedPointerTy(Ty))
1211 add(cast<TypedPointerType>(Ty)->getElementType(),
1212 getPointerAddressSpace(Ty), SpirvType);
1213 else
1215 getPointerAddressSpace(Ty), SpirvType);
1216 return SpirvType;
1217}
1218
1219SPIRVType *
1221 const MachineFunction *MF) const {
1222 auto t = VRegToTypeMap.find(MF ? MF : CurMF);
1223 if (t != VRegToTypeMap.end()) {
1224 auto tt = t->second.find(VReg);
1225 if (tt != t->second.end())
1226 return tt->second;
1227 }
1228 return nullptr;
1229}
1230
1232 MachineFunction *MF) {
1233 if (!MF)
1234 MF = CurMF;
1235 MachineInstr *Instr = getVRegDef(MF->getRegInfo(), VReg);
1236 return getSPIRVTypeForVReg(Instr->getOperand(1).getReg(), MF);
1237}
1238
1240 const Type *Ty, MachineIRBuilder &MIRBuilder,
1241 SPIRV::AccessQualifier::AccessQualifier AccessQual,
1242 bool ExplicitLayoutRequired, bool EmitIR) {
1243 const MachineFunction *MF = &MIRBuilder.getMF();
1244 Register Reg;
1245 if (auto *ExtTy = dyn_cast<TargetExtType>(Ty);
1246 ExtTy && isTypedPointerWrapper(ExtTy))
1247 Reg = find(ExtTy->getTypeParameter(0), ExtTy->getIntParameter(0), MF);
1248 else if (!isPointerTy(Ty))
1249 Reg = find(Ty = adjustIntTypeByWidth(Ty), ExplicitLayoutRequired, MF);
1250 else if (isTypedPointerTy(Ty))
1251 Reg = find(cast<TypedPointerType>(Ty)->getElementType(),
1252 getPointerAddressSpace(Ty), MF);
1253 else
1254 Reg = find(Type::getInt8Ty(MIRBuilder.getMF().getFunction().getContext()),
1255 getPointerAddressSpace(Ty), MF);
1256 if (Reg.isValid() && !isSpecialOpaqueType(Ty))
1257 return getSPIRVTypeForVReg(Reg);
1258
1259 TypesInProcessing.clear();
1260 SPIRVType *STy = restOfCreateSPIRVType(Ty, MIRBuilder, AccessQual,
1261 ExplicitLayoutRequired, EmitIR);
1262 // Create normal pointer types for the corresponding OpTypeForwardPointers.
1263 for (auto &CU : ForwardPointerTypes) {
1264 // Pointer type themselves do not require an explicit layout. The types
1265 // they pointer to might, but that is taken care of when creating the type.
1266 bool PtrNeedsLayout = false;
1267 const Type *Ty2 = CU.first;
1268 SPIRVType *STy2 = CU.second;
1269 if ((Reg = find(Ty2, PtrNeedsLayout, MF)).isValid())
1270 STy2 = getSPIRVTypeForVReg(Reg);
1271 else
1272 STy2 = restOfCreateSPIRVType(Ty2, MIRBuilder, AccessQual, PtrNeedsLayout,
1273 EmitIR);
1274 if (Ty == Ty2)
1275 STy = STy2;
1276 }
1277 ForwardPointerTypes.clear();
1278 return STy;
1279}
1280
1282 unsigned TypeOpcode) const {
1284 assert(Type && "isScalarOfType VReg has no type assigned");
1285 return Type->getOpcode() == TypeOpcode;
1286}
1287
1289 unsigned TypeOpcode) const {
1291 assert(Type && "isScalarOrVectorOfType VReg has no type assigned");
1292 if (Type->getOpcode() == TypeOpcode)
1293 return true;
1294 if (Type->getOpcode() == SPIRV::OpTypeVector) {
1295 Register ScalarTypeVReg = Type->getOperand(1).getReg();
1296 SPIRVType *ScalarType = getSPIRVTypeForVReg(ScalarTypeVReg);
1297 return ScalarType->getOpcode() == TypeOpcode;
1298 }
1299 return false;
1300}
1301
1303 switch (Type->getOpcode()) {
1304 case SPIRV::OpTypeImage:
1305 case SPIRV::OpTypeSampler:
1306 case SPIRV::OpTypeSampledImage:
1307 return true;
1308 case SPIRV::OpTypeStruct:
1309 return hasBlockDecoration(Type);
1310 default:
1311 return false;
1312 }
1313 return false;
1314}
1315unsigned
1319
1320unsigned
1322 if (!Type)
1323 return 0;
1324 return Type->getOpcode() == SPIRV::OpTypeVector
1325 ? static_cast<unsigned>(Type->getOperand(2).getImm())
1326 : 1;
1327}
1328
1329SPIRVType *
1333
1334SPIRVType *
1336 if (!Type)
1337 return nullptr;
1338 Register ScalarReg = Type->getOpcode() == SPIRV::OpTypeVector
1339 ? Type->getOperand(1).getReg()
1340 : Type->getOperand(0).getReg();
1341 SPIRVType *ScalarType = getSPIRVTypeForVReg(ScalarReg);
1342 assert(isScalarOrVectorOfType(Type->getOperand(0).getReg(),
1343 ScalarType->getOpcode()));
1344 return ScalarType;
1345}
1346
1347unsigned
1349 assert(Type && "Invalid Type pointer");
1350 if (Type->getOpcode() == SPIRV::OpTypeVector) {
1351 auto EleTypeReg = Type->getOperand(1).getReg();
1352 Type = getSPIRVTypeForVReg(EleTypeReg);
1353 }
1354 if (Type->getOpcode() == SPIRV::OpTypeInt ||
1355 Type->getOpcode() == SPIRV::OpTypeFloat)
1356 return Type->getOperand(1).getImm();
1357 if (Type->getOpcode() == SPIRV::OpTypeBool)
1358 return 1;
1359 llvm_unreachable("Attempting to get bit width of non-integer/float type.");
1360}
1361
1363 const SPIRVType *Type) const {
1364 assert(Type && "Invalid Type pointer");
1365 unsigned NumElements = 1;
1366 if (Type->getOpcode() == SPIRV::OpTypeVector) {
1367 NumElements = static_cast<unsigned>(Type->getOperand(2).getImm());
1368 Type = getSPIRVTypeForVReg(Type->getOperand(1).getReg());
1369 }
1370 return Type->getOpcode() == SPIRV::OpTypeInt ||
1371 Type->getOpcode() == SPIRV::OpTypeFloat
1372 ? NumElements * Type->getOperand(1).getImm()
1373 : 0;
1374}
1375
1377 const SPIRVType *Type) const {
1378 if (Type && Type->getOpcode() == SPIRV::OpTypeVector)
1379 Type = getSPIRVTypeForVReg(Type->getOperand(1).getReg());
1380 return Type && Type->getOpcode() == SPIRV::OpTypeInt ? Type : nullptr;
1381}
1382
1385 return IntType && IntType->getOperand(2).getImm() != 0;
1386}
1387
1389 return PtrType && PtrType->getOpcode() == SPIRV::OpTypePointer
1390 ? getSPIRVTypeForVReg(PtrType->getOperand(2).getReg())
1391 : nullptr;
1392}
1393
1395 SPIRVType *ElemType = getPointeeType(getSPIRVTypeForVReg(PtrReg));
1396 return ElemType ? ElemType->getOpcode() : 0;
1397}
1398
1400 const SPIRVType *Type2) const {
1401 if (!Type1 || !Type2)
1402 return false;
1403 auto Op1 = Type1->getOpcode(), Op2 = Type2->getOpcode();
1404 // Ignore difference between <1.5 and >=1.5 protocol versions:
1405 // it's valid if either Result Type or Operand is a pointer, and the other
1406 // is a pointer, an integer scalar, or an integer vector.
1407 if (Op1 == SPIRV::OpTypePointer &&
1408 (Op2 == SPIRV::OpTypePointer || retrieveScalarOrVectorIntType(Type2)))
1409 return true;
1410 if (Op2 == SPIRV::OpTypePointer &&
1411 (Op1 == SPIRV::OpTypePointer || retrieveScalarOrVectorIntType(Type1)))
1412 return true;
1413 unsigned Bits1 = getNumScalarOrVectorTotalBitWidth(Type1),
1414 Bits2 = getNumScalarOrVectorTotalBitWidth(Type2);
1415 return Bits1 > 0 && Bits1 == Bits2;
1416}
1417
1418SPIRV::StorageClass::StorageClass
1421 assert(Type && Type->getOpcode() == SPIRV::OpTypePointer &&
1422 Type->getOperand(1).isImm() && "Pointer type is expected");
1424}
1425
1426SPIRV::StorageClass::StorageClass
1428 return static_cast<SPIRV::StorageClass::StorageClass>(
1429 Type->getOperand(1).getImm());
1430}
1431
1433 MachineIRBuilder &MIRBuilder, Type *ElemType,
1434 SPIRV::StorageClass::StorageClass SC, bool IsWritable, bool EmitIr) {
1435 auto Key = SPIRV::irhandle_vkbuffer(ElemType, SC, IsWritable);
1436 if (const MachineInstr *MI = findMI(Key, &MIRBuilder.getMF()))
1437 return MI;
1438
1439 bool ExplicitLayoutRequired = storageClassRequiresExplictLayout(SC);
1440 // We need to get the SPIR-V type for the element here, so we can add the
1441 // decoration to it.
1442 auto *T = StructType::create(ElemType);
1443 auto *BlockType =
1444 getOrCreateSPIRVType(T, MIRBuilder, SPIRV::AccessQualifier::None,
1445 ExplicitLayoutRequired, EmitIr);
1446
1447 buildOpDecorate(BlockType->defs().begin()->getReg(), MIRBuilder,
1448 SPIRV::Decoration::Block, {});
1449
1450 if (!IsWritable) {
1451 buildOpMemberDecorate(BlockType->defs().begin()->getReg(), MIRBuilder,
1452 SPIRV::Decoration::NonWritable, 0, {});
1453 }
1454
1455 SPIRVType *R = getOrCreateSPIRVPointerTypeInternal(BlockType, MIRBuilder, SC);
1456 add(Key, R);
1457 return R;
1458}
1459
1460SPIRVType *
1463 if (const MachineInstr *MI = findMI(Key, &MIRBuilder.getMF()))
1464 return MI;
1465 auto *T = Type::getInt8Ty(MIRBuilder.getContext());
1466 SPIRVType *R = getOrCreateSPIRVIntegerType(8, MIRBuilder);
1467 finishCreatingSPIRVType(T, R);
1468 add(Key, R);
1469 return R;
1470}
1471
1473 MachineIRBuilder &MIRBuilder, const TargetExtType *T, bool EmitIr) {
1474 auto Key = SPIRV::handle(T);
1475 if (const MachineInstr *MI = findMI(Key, &MIRBuilder.getMF()))
1476 return MI;
1477
1478 StructType *ST = cast<StructType>(T->getTypeParameter(0));
1479 ArrayRef<uint32_t> Offsets = T->int_params().slice(1);
1480 assert(ST->getNumElements() == Offsets.size());
1481
1482 StructOffsetDecorator Decorator = [&MIRBuilder, &Offsets](Register Reg) {
1483 for (uint32_t I = 0; I < Offsets.size(); ++I) {
1484 buildOpMemberDecorate(Reg, MIRBuilder, SPIRV::Decoration::Offset, I,
1485 {Offsets[I]});
1486 }
1487 };
1488
1489 // We need a new OpTypeStruct instruction because decorations will be
1490 // different from a struct with an explicit layout created from a different
1491 // entry point.
1492 SPIRVType *SPIRVStructType =
1493 getOpTypeStruct(ST, MIRBuilder, SPIRV::AccessQualifier::None,
1494 std::move(Decorator), EmitIr);
1495 add(Key, SPIRVStructType);
1496 return SPIRVStructType;
1497}
1498
1500 const TargetExtType *ExtensionType,
1501 const SPIRV::AccessQualifier::AccessQualifier Qualifier,
1502 MachineIRBuilder &MIRBuilder) {
1503 assert(ExtensionType->getNumTypeParameters() == 1 &&
1504 "SPIR-V image builtin type must have sampled type parameter!");
1505 const SPIRVType *SampledType =
1506 getOrCreateSPIRVType(ExtensionType->getTypeParameter(0), MIRBuilder,
1507 SPIRV::AccessQualifier::ReadWrite, true);
1508 assert((ExtensionType->getNumIntParameters() == 7 ||
1509 ExtensionType->getNumIntParameters() == 6) &&
1510 "Invalid number of parameters for SPIR-V image builtin!");
1511
1512 SPIRV::AccessQualifier::AccessQualifier accessQualifier =
1513 SPIRV::AccessQualifier::None;
1514 if (ExtensionType->getNumIntParameters() == 7) {
1515 accessQualifier = Qualifier == SPIRV::AccessQualifier::WriteOnly
1516 ? SPIRV::AccessQualifier::WriteOnly
1517 : SPIRV::AccessQualifier::AccessQualifier(
1518 ExtensionType->getIntParameter(6));
1519 }
1520
1521 // Create or get an existing type from GlobalRegistry.
1522 SPIRVType *R = getOrCreateOpTypeImage(
1523 MIRBuilder, SampledType,
1524 SPIRV::Dim::Dim(ExtensionType->getIntParameter(0)),
1525 ExtensionType->getIntParameter(1), ExtensionType->getIntParameter(2),
1526 ExtensionType->getIntParameter(3), ExtensionType->getIntParameter(4),
1527 SPIRV::ImageFormat::ImageFormat(ExtensionType->getIntParameter(5)),
1528 accessQualifier);
1529 SPIRVToLLVMType[R] = ExtensionType;
1530 return R;
1531}
1532
1533SPIRVType *SPIRVGlobalRegistry::getOrCreateOpTypeImage(
1534 MachineIRBuilder &MIRBuilder, SPIRVType *SampledType, SPIRV::Dim::Dim Dim,
1535 uint32_t Depth, uint32_t Arrayed, uint32_t Multisampled, uint32_t Sampled,
1536 SPIRV::ImageFormat::ImageFormat ImageFormat,
1537 SPIRV::AccessQualifier::AccessQualifier AccessQual) {
1538 auto Key = SPIRV::irhandle_image(SPIRVToLLVMType.lookup(SampledType), Dim,
1539 Depth, Arrayed, Multisampled, Sampled,
1540 ImageFormat, AccessQual);
1541 if (const MachineInstr *MI = findMI(Key, &MIRBuilder.getMF()))
1542 return MI;
1543 const MachineInstr *NewMI =
1544 createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
1545 auto MIB =
1546 MIRBuilder.buildInstr(SPIRV::OpTypeImage)
1547 .addDef(createTypeVReg(MIRBuilder))
1548 .addUse(getSPIRVTypeID(SampledType))
1549 .addImm(Dim)
1550 .addImm(Depth) // Depth (whether or not it is a Depth image).
1551 .addImm(Arrayed) // Arrayed.
1552 .addImm(Multisampled) // Multisampled (0 = only single-sample).
1553 .addImm(Sampled) // Sampled (0 = usage known at runtime).
1554 .addImm(ImageFormat);
1555 if (AccessQual != SPIRV::AccessQualifier::None)
1556 MIB.addImm(AccessQual);
1557 return MIB;
1558 });
1559 add(Key, NewMI);
1560 return NewMI;
1561}
1562
1563SPIRVType *
1566 const MachineFunction *MF = &MIRBuilder.getMF();
1567 if (const MachineInstr *MI = findMI(Key, MF))
1568 return MI;
1569 const MachineInstr *NewMI =
1570 createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
1571 return MIRBuilder.buildInstr(SPIRV::OpTypeSampler)
1572 .addDef(createTypeVReg(MIRBuilder));
1573 });
1574 add(Key, NewMI);
1575 return NewMI;
1576}
1577
1579 MachineIRBuilder &MIRBuilder,
1580 SPIRV::AccessQualifier::AccessQualifier AccessQual) {
1581 auto Key = SPIRV::irhandle_pipe(AccessQual);
1582 if (const MachineInstr *MI = findMI(Key, &MIRBuilder.getMF()))
1583 return MI;
1584 const MachineInstr *NewMI =
1585 createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
1586 return MIRBuilder.buildInstr(SPIRV::OpTypePipe)
1587 .addDef(createTypeVReg(MIRBuilder))
1588 .addImm(AccessQual);
1589 });
1590 add(Key, NewMI);
1591 return NewMI;
1592}
1593
1595 MachineIRBuilder &MIRBuilder) {
1596 auto Key = SPIRV::irhandle_event();
1597 if (const MachineInstr *MI = findMI(Key, &MIRBuilder.getMF()))
1598 return MI;
1599 const MachineInstr *NewMI =
1600 createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
1601 return MIRBuilder.buildInstr(SPIRV::OpTypeDeviceEvent)
1602 .addDef(createTypeVReg(MIRBuilder));
1603 });
1604 add(Key, NewMI);
1605 return NewMI;
1606}
1607
1609 SPIRVType *ImageType, MachineIRBuilder &MIRBuilder) {
1611 SPIRVToLLVMType.lookup(MIRBuilder.getMF().getRegInfo().getVRegDef(
1612 ImageType->getOperand(1).getReg())),
1613 ImageType);
1614 if (const MachineInstr *MI = findMI(Key, &MIRBuilder.getMF()))
1615 return MI;
1616 const MachineInstr *NewMI =
1617 createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
1618 return MIRBuilder.buildInstr(SPIRV::OpTypeSampledImage)
1619 .addDef(createTypeVReg(MIRBuilder))
1620 .addUse(getSPIRVTypeID(ImageType));
1621 });
1622 add(Key, NewMI);
1623 return NewMI;
1624}
1625
1627 MachineIRBuilder &MIRBuilder, const TargetExtType *ExtensionType,
1628 const SPIRVType *ElemType, uint32_t Scope, uint32_t Rows, uint32_t Columns,
1629 uint32_t Use, bool EmitIR) {
1630 if (const MachineInstr *MI =
1631 findMI(ExtensionType, false, &MIRBuilder.getMF()))
1632 return MI;
1633 const MachineInstr *NewMI =
1634 createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
1635 SPIRVType *SpvTypeInt32 = getOrCreateSPIRVIntegerType(32, MIRBuilder);
1636 const Type *ET = getTypeForSPIRVType(ElemType);
1637 if (ET->isIntegerTy() && ET->getIntegerBitWidth() == 4 &&
1639 .canUseExtension(SPIRV::Extension::SPV_INTEL_int4)) {
1640 MIRBuilder.buildInstr(SPIRV::OpCapability)
1641 .addImm(SPIRV::Capability::Int4CooperativeMatrixINTEL);
1642 }
1643 return MIRBuilder.buildInstr(SPIRV::OpTypeCooperativeMatrixKHR)
1644 .addDef(createTypeVReg(MIRBuilder))
1645 .addUse(getSPIRVTypeID(ElemType))
1646 .addUse(buildConstantInt(Scope, MIRBuilder, SpvTypeInt32, EmitIR))
1647 .addUse(buildConstantInt(Rows, MIRBuilder, SpvTypeInt32, EmitIR))
1648 .addUse(buildConstantInt(Columns, MIRBuilder, SpvTypeInt32, EmitIR))
1649 .addUse(buildConstantInt(Use, MIRBuilder, SpvTypeInt32, EmitIR));
1650 });
1651 add(ExtensionType, false, NewMI);
1652 return NewMI;
1653}
1654
1656 const Type *Ty, MachineIRBuilder &MIRBuilder, unsigned Opcode) {
1657 if (const MachineInstr *MI = findMI(Ty, false, &MIRBuilder.getMF()))
1658 return MI;
1659 const MachineInstr *NewMI =
1660 createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
1661 return MIRBuilder.buildInstr(Opcode).addDef(createTypeVReg(MIRBuilder));
1662 });
1663 add(Ty, false, NewMI);
1664 return NewMI;
1665}
1666
1668 const Type *Ty, MachineIRBuilder &MIRBuilder, unsigned Opcode,
1669 const ArrayRef<MCOperand> Operands) {
1670 if (const MachineInstr *MI = findMI(Ty, false, &MIRBuilder.getMF()))
1671 return MI;
1672 Register ResVReg = createTypeVReg(MIRBuilder);
1673 const MachineInstr *NewMI =
1674 createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
1675 MachineInstrBuilder MIB = MIRBuilder.buildInstr(SPIRV::UNKNOWN_type)
1676 .addDef(ResVReg)
1677 .addImm(Opcode);
1678 for (MCOperand Operand : Operands) {
1679 if (Operand.isReg()) {
1680 MIB.addUse(Operand.getReg());
1681 } else if (Operand.isImm()) {
1682 MIB.addImm(Operand.getImm());
1683 }
1684 }
1685 return MIB;
1686 });
1687 add(Ty, false, NewMI);
1688 return NewMI;
1689}
1690
1691// Returns nullptr if unable to recognize SPIRV type name
1693 StringRef TypeStr, MachineIRBuilder &MIRBuilder, bool EmitIR,
1694 SPIRV::StorageClass::StorageClass SC,
1695 SPIRV::AccessQualifier::AccessQualifier AQ) {
1696 unsigned VecElts = 0;
1697 auto &Ctx = MIRBuilder.getMF().getFunction().getContext();
1698
1699 // Parse strings representing either a SPIR-V or OpenCL builtin type.
1700 if (hasBuiltinTypePrefix(TypeStr))
1702 TypeStr.str(), MIRBuilder.getContext()),
1703 MIRBuilder, AQ, false, true);
1704
1705 // Parse type name in either "typeN" or "type vector[N]" format, where
1706 // N is the number of elements of the vector.
1707 Type *Ty;
1708
1709 Ty = parseBasicTypeName(TypeStr, Ctx);
1710 if (!Ty)
1711 // Unable to recognize SPIRV type name
1712 return nullptr;
1713
1714 const SPIRVType *SpirvTy =
1715 getOrCreateSPIRVType(Ty, MIRBuilder, AQ, false, true);
1716
1717 // Handle "type*" or "type* vector[N]".
1718 if (TypeStr.consume_front("*"))
1719 SpirvTy = getOrCreateSPIRVPointerType(Ty, MIRBuilder, SC);
1720
1721 // Handle "typeN*" or "type vector[N]*".
1722 bool IsPtrToVec = TypeStr.consume_back("*");
1723
1724 if (TypeStr.consume_front(" vector[")) {
1725 TypeStr = TypeStr.substr(0, TypeStr.find(']'));
1726 }
1727 TypeStr.getAsInteger(10, VecElts);
1728 if (VecElts > 0)
1729 SpirvTy = getOrCreateSPIRVVectorType(SpirvTy, VecElts, MIRBuilder, EmitIR);
1730
1731 if (IsPtrToVec)
1732 SpirvTy = getOrCreateSPIRVPointerType(SpirvTy, MIRBuilder, SC);
1733
1734 return SpirvTy;
1735}
1736
1737SPIRVType *
1739 MachineIRBuilder &MIRBuilder) {
1740 return getOrCreateSPIRVType(
1742 MIRBuilder, SPIRV::AccessQualifier::ReadWrite, false, true);
1743}
1744
1745SPIRVType *SPIRVGlobalRegistry::finishCreatingSPIRVType(const Type *LLVMTy,
1746 SPIRVType *SpirvType) {
1747 assert(CurMF == SpirvType->getMF());
1748 VRegToTypeMap[CurMF][getSPIRVTypeID(SpirvType)] = SpirvType;
1749 SPIRVToLLVMType[SpirvType] = unifyPtrType(LLVMTy);
1750 return SpirvType;
1751}
1752
1754 MachineInstr &I,
1755 const SPIRVInstrInfo &TII,
1756 unsigned SPIRVOPcode,
1757 Type *Ty) {
1758 if (const MachineInstr *MI = findMI(Ty, false, CurMF))
1759 return MI;
1760 MachineBasicBlock &DepMBB = I.getMF()->front();
1761 MachineIRBuilder MIRBuilder(DepMBB, DepMBB.getFirstNonPHI());
1762 const MachineInstr *NewMI =
1763 createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
1764 auto NewTypeMI = BuildMI(MIRBuilder.getMBB(), *MIRBuilder.getInsertPt(),
1765 MIRBuilder.getDL(), TII.get(SPIRVOPcode))
1766 .addDef(createTypeVReg(CurMF->getRegInfo()))
1767 .addImm(BitWidth);
1768 // Don't add Encoding to FP type
1769 if (!Ty->isFloatTy()) {
1770 return NewTypeMI.addImm(0);
1771 } else {
1772 return NewTypeMI;
1773 }
1774 });
1775 add(Ty, false, NewMI);
1776 return finishCreatingSPIRVType(Ty, NewMI);
1777}
1778
1780 unsigned BitWidth, MachineInstr &I, const SPIRVInstrInfo &TII) {
1781 // Maybe adjust bit width to keep DuplicateTracker consistent. Without
1782 // such an adjustment SPIRVGlobalRegistry::getOpTypeInt() could create, for
1783 // example, the same "OpTypeInt 8" type for a series of LLVM integer types
1784 // with number of bits less than 8, causing duplicate type definitions.
1785 if (BitWidth > 1)
1786 BitWidth = adjustOpTypeIntWidth(BitWidth);
1787 Type *LLVMTy = IntegerType::get(CurMF->getFunction().getContext(), BitWidth);
1788 return getOrCreateSPIRVType(BitWidth, I, TII, SPIRV::OpTypeInt, LLVMTy);
1789}
1790
1792 unsigned BitWidth, MachineInstr &I, const SPIRVInstrInfo &TII) {
1793 LLVMContext &Ctx = CurMF->getFunction().getContext();
1794 Type *LLVMTy;
1795 switch (BitWidth) {
1796 case 16:
1797 LLVMTy = Type::getHalfTy(Ctx);
1798 break;
1799 case 32:
1800 LLVMTy = Type::getFloatTy(Ctx);
1801 break;
1802 case 64:
1803 LLVMTy = Type::getDoubleTy(Ctx);
1804 break;
1805 default:
1806 llvm_unreachable("Bit width is of unexpected size.");
1807 }
1808 return getOrCreateSPIRVType(BitWidth, I, TII, SPIRV::OpTypeFloat, LLVMTy);
1809}
1810
1811SPIRVType *
1813 bool EmitIR) {
1814 return getOrCreateSPIRVType(
1815 IntegerType::get(MIRBuilder.getMF().getFunction().getContext(), 1),
1816 MIRBuilder, SPIRV::AccessQualifier::ReadWrite, false, EmitIR);
1817}
1818
1819SPIRVType *
1821 const SPIRVInstrInfo &TII) {
1822 Type *Ty = IntegerType::get(CurMF->getFunction().getContext(), 1);
1823 if (const MachineInstr *MI = findMI(Ty, false, CurMF))
1824 return MI;
1825 MachineBasicBlock &DepMBB = I.getMF()->front();
1826 MachineIRBuilder MIRBuilder(DepMBB, DepMBB.getFirstNonPHI());
1827 const MachineInstr *NewMI =
1828 createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
1829 return BuildMI(MIRBuilder.getMBB(), *MIRBuilder.getInsertPt(),
1830 MIRBuilder.getDL(), TII.get(SPIRV::OpTypeBool))
1831 .addDef(createTypeVReg(CurMF->getRegInfo()));
1832 });
1833 add(Ty, false, NewMI);
1834 return finishCreatingSPIRVType(Ty, NewMI);
1835}
1836
1838 SPIRVType *BaseType, unsigned NumElements, MachineIRBuilder &MIRBuilder,
1839 bool EmitIR) {
1840 return getOrCreateSPIRVType(
1842 NumElements),
1843 MIRBuilder, SPIRV::AccessQualifier::ReadWrite, false, EmitIR);
1844}
1845
1847 SPIRVType *BaseType, unsigned NumElements, MachineInstr &I,
1848 const SPIRVInstrInfo &TII) {
1850 const_cast<Type *>(getTypeForSPIRVType(BaseType)), NumElements);
1851 if (const MachineInstr *MI = findMI(Ty, false, CurMF))
1852 return MI;
1853 MachineInstr *DepMI = const_cast<MachineInstr *>(BaseType);
1854 MachineIRBuilder MIRBuilder(*DepMI->getParent(), DepMI->getIterator());
1855 const MachineInstr *NewMI =
1856 createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
1857 return BuildMI(MIRBuilder.getMBB(), *MIRBuilder.getInsertPt(),
1858 MIRBuilder.getDL(), TII.get(SPIRV::OpTypeVector))
1859 .addDef(createTypeVReg(CurMF->getRegInfo()))
1861 .addImm(NumElements);
1862 });
1863 add(Ty, false, NewMI);
1864 return finishCreatingSPIRVType(Ty, NewMI);
1865}
1866
1868 const Type *BaseType, MachineInstr &I,
1869 SPIRV::StorageClass::StorageClass SC) {
1870 MachineIRBuilder MIRBuilder(I);
1871 return getOrCreateSPIRVPointerType(BaseType, MIRBuilder, SC);
1872}
1873
1875 const Type *BaseType, MachineIRBuilder &MIRBuilder,
1876 SPIRV::StorageClass::StorageClass SC) {
1877 // TODO: Need to check if EmitIr should always be true.
1878 SPIRVType *SpirvBaseType = getOrCreateSPIRVType(
1879 BaseType, MIRBuilder, SPIRV::AccessQualifier::ReadWrite,
1881 assert(SpirvBaseType);
1882 return getOrCreateSPIRVPointerTypeInternal(SpirvBaseType, MIRBuilder, SC);
1883}
1884
1886 SPIRVType *PtrType, SPIRV::StorageClass::StorageClass SC, MachineInstr &I) {
1887 [[maybe_unused]] SPIRV::StorageClass::StorageClass OldSC =
1888 getPointerStorageClass(PtrType);
1891
1892 SPIRVType *PointeeType = getPointeeType(PtrType);
1893 MachineIRBuilder MIRBuilder(I);
1894 return getOrCreateSPIRVPointerTypeInternal(PointeeType, MIRBuilder, SC);
1895}
1896
1898 SPIRVType *BaseType, MachineIRBuilder &MIRBuilder,
1899 SPIRV::StorageClass::StorageClass SC) {
1900 const Type *LLVMType = getTypeForSPIRVType(BaseType);
1902 SPIRVType *R = getOrCreateSPIRVPointerType(LLVMType, MIRBuilder, SC);
1903 assert(
1904 getPointeeType(R) == BaseType &&
1905 "The base type was not correctly laid out for the given storage class.");
1906 return R;
1907}
1908
1909SPIRVType *SPIRVGlobalRegistry::getOrCreateSPIRVPointerTypeInternal(
1910 SPIRVType *BaseType, MachineIRBuilder &MIRBuilder,
1911 SPIRV::StorageClass::StorageClass SC) {
1912 const Type *PointerElementType = getTypeForSPIRVType(BaseType);
1914 if (const MachineInstr *MI = findMI(PointerElementType, AddressSpace, CurMF))
1915 return MI;
1916 Type *Ty = TypedPointerType::get(const_cast<Type *>(PointerElementType),
1917 AddressSpace);
1918 const MachineInstr *NewMI =
1919 createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
1920 return BuildMI(MIRBuilder.getMBB(), MIRBuilder.getInsertPt(),
1921 MIRBuilder.getDebugLoc(),
1922 MIRBuilder.getTII().get(SPIRV::OpTypePointer))
1924 .addImm(static_cast<uint32_t>(SC))
1926 });
1927 add(PointerElementType, AddressSpace, NewMI);
1928 return finishCreatingSPIRVType(Ty, NewMI);
1929}
1930
1932 SPIRVType *SpvType,
1933 const SPIRVInstrInfo &TII) {
1934 UndefValue *UV =
1935 UndefValue::get(const_cast<Type *>(getTypeForSPIRVType(SpvType)));
1936 Register Res = find(UV, CurMF);
1937 if (Res.isValid())
1938 return Res;
1939
1940 LLT LLTy = LLT::scalar(64);
1941 Res = CurMF->getRegInfo().createGenericVirtualRegister(LLTy);
1942 CurMF->getRegInfo().setRegClass(Res, &SPIRV::iIDRegClass);
1943 assignSPIRVTypeToVReg(SpvType, Res, *CurMF);
1944
1945 MachineInstr *DepMI = const_cast<MachineInstr *>(SpvType);
1946 MachineIRBuilder MIRBuilder(*DepMI->getParent(), DepMI->getIterator());
1947 const MachineInstr *NewMI =
1948 createOpType(MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
1949 auto MIB = BuildMI(MIRBuilder.getMBB(), *MIRBuilder.getInsertPt(),
1950 MIRBuilder.getDL(), TII.get(SPIRV::OpUndef))
1951 .addDef(Res)
1952 .addUse(getSPIRVTypeID(SpvType));
1953 const auto &ST = CurMF->getSubtarget();
1954 constrainSelectedInstRegOperands(*MIB, *ST.getInstrInfo(),
1955 *ST.getRegisterInfo(),
1956 *ST.getRegBankInfo());
1957 return MIB;
1958 });
1959 add(UV, NewMI);
1960 return Res;
1961}
1962
1963const TargetRegisterClass *
1965 unsigned Opcode = SpvType->getOpcode();
1966 switch (Opcode) {
1967 case SPIRV::OpTypeFloat:
1968 return &SPIRV::fIDRegClass;
1969 case SPIRV::OpTypePointer:
1970 return &SPIRV::pIDRegClass;
1971 case SPIRV::OpTypeVector: {
1972 SPIRVType *ElemType = getSPIRVTypeForVReg(SpvType->getOperand(1).getReg());
1973 unsigned ElemOpcode = ElemType ? ElemType->getOpcode() : 0;
1974 if (ElemOpcode == SPIRV::OpTypeFloat)
1975 return &SPIRV::vfIDRegClass;
1976 if (ElemOpcode == SPIRV::OpTypePointer)
1977 return &SPIRV::vpIDRegClass;
1978 return &SPIRV::vIDRegClass;
1979 }
1980 }
1981 return &SPIRV::iIDRegClass;
1982}
1983
1984inline unsigned getAS(SPIRVType *SpvType) {
1986 static_cast<SPIRV::StorageClass::StorageClass>(
1987 SpvType->getOperand(1).getImm()));
1988}
1989
1991 unsigned Opcode = SpvType ? SpvType->getOpcode() : 0;
1992 switch (Opcode) {
1993 case SPIRV::OpTypeInt:
1994 case SPIRV::OpTypeFloat:
1995 case SPIRV::OpTypeBool:
1996 return LLT::scalar(getScalarOrVectorBitWidth(SpvType));
1997 case SPIRV::OpTypePointer:
1998 return LLT::pointer(getAS(SpvType), getPointerSize());
1999 case SPIRV::OpTypeVector: {
2000 SPIRVType *ElemType = getSPIRVTypeForVReg(SpvType->getOperand(1).getReg());
2001 LLT ET;
2002 switch (ElemType ? ElemType->getOpcode() : 0) {
2003 case SPIRV::OpTypePointer:
2004 ET = LLT::pointer(getAS(ElemType), getPointerSize());
2005 break;
2006 case SPIRV::OpTypeInt:
2007 case SPIRV::OpTypeFloat:
2008 case SPIRV::OpTypeBool:
2009 ET = LLT::scalar(getScalarOrVectorBitWidth(ElemType));
2010 break;
2011 default:
2012 ET = LLT::scalar(64);
2013 }
2014 return LLT::fixed_vector(
2015 static_cast<unsigned>(SpvType->getOperand(2).getImm()), ET);
2016 }
2017 }
2018 return LLT::scalar(64);
2019}
2020
2021// Aliasing list MD contains several scope MD nodes whithin it. Each scope MD
2022// has a selfreference and an extra MD node for aliasing domain and also it
2023// can contain an optional string operand. Domain MD contains a self-reference
2024// with an optional string operand. Here we unfold the list, creating SPIR-V
2025// aliasing instructions.
2026// TODO: add support for an optional string operand.
2028 MachineIRBuilder &MIRBuilder, const MDNode *AliasingListMD) {
2029 if (AliasingListMD->getNumOperands() == 0)
2030 return nullptr;
2031 if (auto L = AliasInstMDMap.find(AliasingListMD); L != AliasInstMDMap.end())
2032 return L->second;
2033
2035 MachineRegisterInfo *MRI = MIRBuilder.getMRI();
2036 for (const MDOperand &MDListOp : AliasingListMD->operands()) {
2037 if (MDNode *ScopeMD = dyn_cast<MDNode>(MDListOp)) {
2038 if (ScopeMD->getNumOperands() < 2)
2039 return nullptr;
2040 MDNode *DomainMD = dyn_cast<MDNode>(ScopeMD->getOperand(1));
2041 if (!DomainMD)
2042 return nullptr;
2043 auto *Domain = [&] {
2044 auto D = AliasInstMDMap.find(DomainMD);
2045 if (D != AliasInstMDMap.end())
2046 return D->second;
2047 const Register Ret = MRI->createVirtualRegister(&SPIRV::IDRegClass);
2048 auto MIB =
2049 MIRBuilder.buildInstr(SPIRV::OpAliasDomainDeclINTEL).addDef(Ret);
2050 return MIB.getInstr();
2051 }();
2052 AliasInstMDMap.insert(std::make_pair(DomainMD, Domain));
2053 auto *Scope = [&] {
2054 auto S = AliasInstMDMap.find(ScopeMD);
2055 if (S != AliasInstMDMap.end())
2056 return S->second;
2057 const Register Ret = MRI->createVirtualRegister(&SPIRV::IDRegClass);
2058 auto MIB = MIRBuilder.buildInstr(SPIRV::OpAliasScopeDeclINTEL)
2059 .addDef(Ret)
2060 .addUse(Domain->getOperand(0).getReg());
2061 return MIB.getInstr();
2062 }();
2063 AliasInstMDMap.insert(std::make_pair(ScopeMD, Scope));
2064 ScopeList.push_back(Scope);
2065 }
2066 }
2067
2068 const Register Ret = MRI->createVirtualRegister(&SPIRV::IDRegClass);
2069 auto MIB =
2070 MIRBuilder.buildInstr(SPIRV::OpAliasScopeListDeclINTEL).addDef(Ret);
2071 for (auto *Scope : ScopeList)
2072 MIB.addUse(Scope->getOperand(0).getReg());
2073 auto List = MIB.getInstr();
2074 AliasInstMDMap.insert(std::make_pair(AliasingListMD, List));
2075 return List;
2076}
2077
2079 Register Reg, MachineIRBuilder &MIRBuilder, uint32_t Dec,
2080 const MDNode *AliasingListMD) {
2081 MachineInstr *AliasList =
2082 getOrAddMemAliasingINTELInst(MIRBuilder, AliasingListMD);
2083 if (!AliasList)
2084 return;
2085 MIRBuilder.buildInstr(SPIRV::OpDecorate)
2086 .addUse(Reg)
2087 .addImm(Dec)
2088 .addUse(AliasList->getOperand(0).getReg());
2089}
2091 bool DeleteOld) {
2092 Old->replaceAllUsesWith(New);
2093 updateIfExistDeducedElementType(Old, New, DeleteOld);
2094 updateIfExistAssignPtrTypeInstr(Old, New, DeleteOld);
2095}
2096
2098 Value *Arg) {
2099 Value *OfType = getNormalizedPoisonValue(Ty);
2100 CallInst *AssignCI = nullptr;
2101 if (Arg->getType()->isAggregateType() && Ty->isAggregateType() &&
2102 allowEmitFakeUse(Arg)) {
2103 LLVMContext &Ctx = Arg->getContext();
2106 MDString::get(Ctx, Arg->getName())};
2107 B.CreateIntrinsic(Intrinsic::spv_value_md,
2108 {MetadataAsValue::get(Ctx, MDTuple::get(Ctx, ArgMDs))});
2109 AssignCI = B.CreateIntrinsic(Intrinsic::fake_use, {Arg});
2110 } else {
2111 AssignCI = buildIntrWithMD(Intrinsic::spv_assign_type, {Arg->getType()},
2112 OfType, Arg, {}, B);
2113 }
2114 addAssignPtrTypeInstr(Arg, AssignCI);
2115}
2116
2118 Value *Arg) {
2119 Value *OfType = PoisonValue::get(ElemTy);
2120 CallInst *AssignPtrTyCI = findAssignPtrTypeInstr(Arg);
2121 Function *CurrF =
2122 B.GetInsertBlock() ? B.GetInsertBlock()->getParent() : nullptr;
2123 if (AssignPtrTyCI == nullptr ||
2124 AssignPtrTyCI->getParent()->getParent() != CurrF) {
2125 AssignPtrTyCI = buildIntrWithMD(
2126 Intrinsic::spv_assign_ptr_type, {Arg->getType()}, OfType, Arg,
2127 {B.getInt32(getPointerAddressSpace(Arg->getType()))}, B);
2128 addDeducedElementType(AssignPtrTyCI, ElemTy);
2129 addDeducedElementType(Arg, ElemTy);
2130 addAssignPtrTypeInstr(Arg, AssignPtrTyCI);
2131 } else {
2132 updateAssignType(AssignPtrTyCI, Arg, OfType);
2133 }
2134}
2135
2137 Value *OfType) {
2138 AssignCI->setArgOperand(1, buildMD(OfType));
2139 if (cast<IntrinsicInst>(AssignCI)->getIntrinsicID() !=
2140 Intrinsic::spv_assign_ptr_type)
2141 return;
2142
2143 // update association with the pointee type
2144 Type *ElemTy = OfType->getType();
2145 addDeducedElementType(AssignCI, ElemTy);
2146 addDeducedElementType(Arg, ElemTy);
2147}
2148
2149void SPIRVGlobalRegistry::addStructOffsetDecorations(
2150 Register Reg, StructType *Ty, MachineIRBuilder &MIRBuilder) {
2151 DataLayout DL;
2152 ArrayRef<TypeSize> Offsets = DL.getStructLayout(Ty)->getMemberOffsets();
2153 for (uint32_t I = 0; I < Ty->getNumElements(); ++I) {
2154 buildOpMemberDecorate(Reg, MIRBuilder, SPIRV::Decoration::Offset, I,
2155 {static_cast<uint32_t>(Offsets[I])});
2156 }
2157}
2158
2159void SPIRVGlobalRegistry::addArrayStrideDecorations(
2160 Register Reg, Type *ElementType, MachineIRBuilder &MIRBuilder) {
2161 uint32_t SizeInBytes = DataLayout().getTypeSizeInBits(ElementType) / 8;
2162 buildOpDecorate(Reg, MIRBuilder, SPIRV::Decoration::ArrayStride,
2163 {SizeInBytes});
2164}
2165
2166bool SPIRVGlobalRegistry::hasBlockDecoration(SPIRVType *Type) const {
2168 for (const MachineInstr &Use :
2169 Type->getMF()->getRegInfo().use_instructions(Def)) {
2170 if (Use.getOpcode() != SPIRV::OpDecorate)
2171 continue;
2172
2173 if (Use.getOperand(1).getImm() == SPIRV::Decoration::Block)
2174 return true;
2175 }
2176 return false;
2177}
unsigned const MachineRegisterInfo * MRI
static unsigned getIntrinsicID(const SDNode *N)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const TargetInstrInfo & TII
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis false
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define T
static bool isValid(const char C)
Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
static unsigned getNumElements(Type *Ty)
static bool storageClassRequiresExplictLayout(SPIRV::StorageClass::StorageClass SC)
static Register createTypeVReg(MachineRegisterInfo &MRI)
unsigned getAS(SPIRVType *SpvType)
static bool allowEmitFakeUse(const Value *Arg)
static unsigned typeToAddressSpace(const Type *Ty)
APInt bitcastToAPInt() const
Definition APFloat.h:1335
bool isPosZero() const
Definition APFloat.h:1442
Class for arbitrary precision integers.
Definition APInt.h:78
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1541
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Class to represent array types.
uint64_t getNumElements() const
Type * getElementType() const
void setArgOperand(unsigned i, Value *v)
This class represents a function call, abstracting a target machine's calling convention.
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:282
const APFloat & getValue() const
Definition Constants.h:326
const APFloat & getValueAPF() const
Definition Constants.h:325
This is the shared class of boolean and integer constants.
Definition Constants.h:87
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition Constants.h:219
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:168
static Constant * getAnon(ArrayRef< Constant * > V, bool Packed=false)
Return an anonymous struct that has the specified elements.
Definition Constants.h:491
static LLVM_ABI ConstantTargetNone * get(TargetExtType *T)
Static factory methods - Return objects of the specified value.
static LLVM_ABI Constant * getSplat(ElementCount EC, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
This is an important base class in LLVM.
Definition Constant.h:43
LLVM_ABI const APInt & getUniqueInteger() const
If C is a constant integer then return its value, otherwise C must be a vector of constant integers,...
LLVM_ABI bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition Constants.cpp:90
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
Class to represent fixed width SIMD vectors.
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition Type.cpp:802
Class to represent function types.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition Function.cpp:359
MDNode * getMetadata(unsigned KindID) const
Get the current metadata attachments for the given kind, if any.
Definition Value.h:576
Module * getParent()
Get the module that this global value is contained inside of...
@ ExternalLinkage
Externally visible function.
Definition GlobalValue.h:53
MaybeAlign getAlign() const
Returns the alignment of the given variable.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2788
Class to represent integer types.
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition Type.cpp:318
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
static constexpr LLT pointer(unsigned AddressSpace, unsigned SizeInBits)
Get a low-level pointer in the given address space.
static constexpr LLT fixed_vector(unsigned NumElements, unsigned ScalarSizeInBits)
Get a low-level fixed-width vector of some number of elements and element width.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode.
Definition MCInstrInfo.h:90
Instances of this class represent operands of the MCInst class.
Definition MCInst.h:40
Metadata node.
Definition Metadata.h:1078
ArrayRef< MDOperand > operands() const
Definition Metadata.h:1440
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1569
unsigned getNumOperands() const
Return number of MDNode operands.
Definition Metadata.h:1448
Tracking metadata reference owned by Metadata.
Definition Metadata.h:900
static LLVM_ABI MDString * get(LLVMContext &Context, StringRef Str)
Definition Metadata.cpp:608
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1526
LLVM_ABI iterator getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
MachineInstrBundleIterator< MachineInstr > iterator
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>.
const DebugLoc & getDL()
Getter for DebugLoc.
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.
MachineInstr * getInstr() const
If conversion operators fail, use this method to get the MachineInstr explicitly.
const MachineInstrBuilder & addDef(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register definition operand.
Representation of each machine instruction.
mop_range defs()
Returns all explicit operands that are register definitions.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
LLVM_ABI void insert(mop_iterator InsertBefore, ArrayRef< MachineOperand > Ops)
Inserts Ops BEFORE It. Can untie/retie tied operands.
mop_range uses()
Returns all operands which may be register uses.
LLVM_ABI const MachineFunction * getMF() const
Return the function that contains the basic block that this instruction belongs to.
const MachineOperand & getOperand(unsigned i) const
int64_t getImm() const
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
static LLVM_ABI MetadataAsValue * get(LLVMContext &Context, Metadata *MD)
Definition Metadata.cpp:104
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isValid() const
Definition Register.h:112
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 * getResultType(Register VReg, MachineFunction *MF=nullptr)
void addAssignPtrTypeInstr(Value *Val, CallInst *AssignPtrTyCI)
SPIRVType * getOrCreateSPIRVBoolType(MachineIRBuilder &MIRBuilder, bool EmitIR)
void buildAssignPtr(IRBuilder<> &B, Type *ElemTy, Value *Arg)
SPIRVType * getOrCreatePaddingType(MachineIRBuilder &MIRBuilder)
SPIRVType * assignFloatTypeToVReg(unsigned BitWidth, Register VReg, MachineInstr &I, const SPIRVInstrInfo &TII)
MachineInstr * getOrAddMemAliasingINTELInst(MachineIRBuilder &MIRBuilder, const MDNode *AliasingListMD)
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)
Register buildGlobalVariable(Register Reg, SPIRVType *BaseType, StringRef Name, const GlobalValue *GV, SPIRV::StorageClass::StorageClass Storage, const MachineInstr *Init, bool IsConst, const std::optional< SPIRV::LinkageType::LinkageType > &LinkageType, MachineIRBuilder &MIRBuilder, bool IsInstSelector)
void replaceAllUsesWith(Value *Old, Value *New, bool DeleteOld=true)
SPIRVType * changePointerStorageClass(SPIRVType *PtrType, SPIRV::StorageClass::StorageClass SC, MachineInstr &I)
const Type * getTypeForSPIRVType(const SPIRVType *Ty) const
SPIRVType * getOrCreateUnknownType(const Type *Ty, MachineIRBuilder &MIRBuilder, unsigned Opcode, const ArrayRef< MCOperand > Operands)
bool isBitcastCompatible(const SPIRVType *Type1, const SPIRVType *Type2) const
unsigned getScalarOrVectorComponentCount(Register VReg) const
Register createConstInt(const ConstantInt *CI, MachineInstr &I, SPIRVType *SpvType, const SPIRVInstrInfo &TII, bool ZeroAsNull)
SPIRVType * getOrCreateSPIRVFloatType(unsigned BitWidth, MachineInstr &I, const SPIRVInstrInfo &TII)
bool isScalarOrVectorSigned(const SPIRVType *Type) const
void addDeducedElementType(Value *Val, Type *Ty)
SPIRVGlobalRegistry(unsigned PointerSize)
Register getOrCreateGlobalVariableWithBinding(const SPIRVType *VarType, uint32_t Set, uint32_t Binding, StringRef Name, MachineIRBuilder &MIRBuilder)
SPIRVType * getOrCreateSPIRVType(const Type *Type, MachineInstr &I, SPIRV::AccessQualifier::AccessQualifier AQ, bool EmitIR)
SPIRVType * getOrCreateSPIRVPointerType(const Type *BaseType, MachineIRBuilder &MIRBuilder, SPIRV::StorageClass::StorageClass SC)
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
Register createConstFP(const ConstantFP *CF, MachineInstr &I, SPIRVType *SpvType, const SPIRVInstrInfo &TII, bool ZeroAsNull)
void updateIfExistDeducedElementType(Value *OldVal, Value *NewVal, bool DeleteOld)
bool isScalarOfType(Register VReg, unsigned TypeOpcode) const
SPIRVType * assignIntTypeToVReg(unsigned BitWidth, Register VReg, MachineInstr &I, const SPIRVInstrInfo &TII)
unsigned getPointeeTypeOp(Register PtrReg)
SPIRVType * getOrCreateOpTypeSampledImage(SPIRVType *ImageType, MachineIRBuilder &MIRBuilder)
SPIRVType * getOrCreateVulkanBufferType(MachineIRBuilder &MIRBuilder, Type *ElemType, SPIRV::StorageClass::StorageClass SC, bool IsWritable, bool EmitIr=false)
SPIRVType * getOrCreateSPIRVTypeByName(StringRef TypeStr, MachineIRBuilder &MIRBuilder, bool EmitIR, SPIRV::StorageClass::StorageClass SC=SPIRV::StorageClass::Function, SPIRV::AccessQualifier::AccessQualifier AQ=SPIRV::AccessQualifier::ReadWrite)
SPIRVType * getOrCreateLayoutType(MachineIRBuilder &MIRBuilder, const TargetExtType *T, bool EmitIr=false)
void addGlobalObject(const Value *V, const MachineFunction *MF, Register R)
SPIRVType * getScalarOrVectorComponentType(Register VReg) const
SPIRVType * getOrCreateOpTypeFunctionWithArgs(const Type *Ty, SPIRVType *RetType, const SmallVectorImpl< SPIRVType * > &ArgTypes, MachineIRBuilder &MIRBuilder)
void buildAssignType(IRBuilder<> &B, Type *Ty, Value *Arg)
Register getOrCreateConsIntVector(uint64_t Val, MachineIRBuilder &MIRBuilder, SPIRVType *SpvType, bool EmitIR)
const TargetRegisterClass * getRegClass(SPIRVType *SpvType) const
void updateIfExistAssignPtrTypeInstr(Value *OldVal, Value *NewVal, bool DeleteOld)
SPIRVType * getOrCreateSPIRVVectorType(SPIRVType *BaseType, unsigned NumElements, MachineIRBuilder &MIRBuilder, bool EmitIR)
SPIRVType * getOrCreateOpTypeCoopMatr(MachineIRBuilder &MIRBuilder, const TargetExtType *ExtensionType, const SPIRVType *ElemType, uint32_t Scope, uint32_t Rows, uint32_t Columns, uint32_t Use, bool EmitIR)
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 * getImageType(const TargetExtType *ExtensionType, const SPIRV::AccessQualifier::AccessQualifier Qualifier, MachineIRBuilder &MIRBuilder)
bool isResourceType(SPIRVType *Type) const
SPIRVType * getOrCreateSPIRVIntegerType(unsigned BitWidth, MachineIRBuilder &MIRBuilder)
Register buildConstantInt(uint64_t Val, MachineIRBuilder &MIRBuilder, SPIRVType *SpvType, bool EmitIR, bool ZeroAsNull=true)
SPIRVType * assignTypeToVReg(const Type *Type, Register VReg, MachineIRBuilder &MIRBuilder, SPIRV::AccessQualifier::AccessQualifier AQ, bool EmitIR)
LLT getRegType(SPIRVType *SpvType) const
void buildMemAliasingOpDecorate(Register Reg, MachineIRBuilder &MIRBuilder, uint32_t Dec, const MDNode *GVarMD)
SPIRV::StorageClass::StorageClass getPointerStorageClass(Register VReg) const
SPIRVType * getOrCreateOpTypeSampler(MachineIRBuilder &MIRBuilder)
Register buildConstantSampler(Register Res, unsigned AddrMode, unsigned Param, unsigned FilerMode, MachineIRBuilder &MIRBuilder)
void updateAssignType(CallInst *AssignCI, Value *Arg, Value *OfType)
Register getOrCreateConstFP(APFloat Val, MachineInstr &I, SPIRVType *SpvType, const SPIRVInstrInfo &TII, bool ZeroAsNull=true)
CallInst * findAssignPtrTypeInstr(const Value *Val)
Register getOrCreateConstNullPtr(MachineIRBuilder &MIRBuilder, SPIRVType *SpvType)
unsigned getScalarOrVectorBitWidth(const SPIRVType *Type) const
const SPIRVType * retrieveScalarOrVectorIntType(const SPIRVType *Type) const
const MachineInstr * findMI(SPIRV::IRHandle Handle, const MachineFunction *MF)
bool erase(const MachineInstr *MI)
bool add(SPIRV::IRHandle Handle, const MachineInstr *MI)
Register find(SPIRV::IRHandle Handle, const MachineFunction *MF)
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
bool consume_back(StringRef Suffix)
Returns true if this StringRef has the given suffix and removes that suffix.
Definition StringRef.h:657
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition StringRef.h:472
std::string str() const
str - Get the contents as an std::string.
Definition StringRef.h:225
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:573
bool consume_front(StringRef Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
Definition StringRef.h:637
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.
ArrayRef< Type * > elements() const
static LLVM_ABI StructType * create(LLVMContext &Context, StringRef Name)
This creates an identified struct.
Definition Type.cpp:619
bool isPacked() const
unsigned getNumElements() const
Random access to the elements.
bool hasName() const
Return true if this is a named struct that has a non-empty name.
LLVM_ABI StringRef getName() const
Return the name for this struct type if it has an identity.
Definition Type.cpp:696
Class to represent target extensions types, which are generally unintrospectable from target-independ...
unsigned getNumIntParameters() const
Type * getTypeParameter(unsigned i) const
unsigned getNumTypeParameters() const
unsigned getIntParameter(unsigned i) const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
LLVM_ABI unsigned getIntegerBitWidth() const
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:273
bool isArrayTy() const
True if this is an instance of ArrayType.
Definition Type.h:264
Type * getArrayElementType() const
Definition Type.h:408
bool isBFloatTy() const
Return true if this is 'bfloat', a 16-bit bfloat type.
Definition Type.h:145
LLVM_ABI uint64_t getArrayNumElements() const
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:294
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:197
bool isAggregateType() const
Return true if the type is an aggregate type.
Definition Type.h:304
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
Definition Type.h:184
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:240
static LLVM_ABI Type * getDoubleTy(LLVMContext &C)
Definition Type.cpp:285
static LLVM_ABI Type * getFloatTy(LLVMContext &C)
Definition Type.cpp:284
static LLVM_ABI Type * getHalfTy(LLVMContext &C)
Definition Type.cpp:282
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:139
static LLVM_ABI 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:1430
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
static ConstantAsMetadata * getConstant(Value *C)
Definition Metadata.h:480
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:546
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.cpp:1099
bool hasName() const
Definition Value.h:262
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
ElementCount getElementCount() const
Return an ElementCount instance to represent the (possibly scalable) number of elements in the vector...
Type * getElementType() const
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
IteratorT begin() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
IRHandle handle(const Type *Ty)
IRHandle irhandle_sampled_image(const Type *SampledTy, const MachineInstr *ImageTy)
IRHandle irhandle_padding()
IRHandle irhandle_vkbuffer(const Type *ElementType, StorageClass::StorageClass SC, bool IsWriteable)
IRHandle irhandle_sampler()
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)
IRHandle irhandle_event()
IRHandle irhandle_pipe(uint8_t AQ)
IRHandle irhandle_image(const Type *SampledTy, unsigned Dim, unsigned Depth, unsigned Arrayed, unsigned MS, unsigned Sampled, unsigned ImageFormat, unsigned AQ=0)
NodeAddr< DefNode * > Def
Definition RDFGraph.h:384
NodeAddr< UseNode * > Use
Definition RDFGraph.h:385
This is an optimization pass for GlobalISel generic memory operations.
void buildOpName(Register Target, const StringRef &Name, MachineIRBuilder &MIRBuilder)
bool isTypedPointerWrapper(const TargetExtType *ExtTy)
Definition SPIRVUtils.h:401
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:365
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
void addNumImm(const APInt &Imm, MachineInstrBuilder &MIB)
LLVM_ABI 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
CallInst * buildIntrWithMD(Intrinsic::ID IntrID, ArrayRef< Type * > Types, Value *Arg, Value *Arg2, ArrayRef< Constant * > Imms, IRBuilder<> &B)
bool matchPeeledArrayPattern(const StructType *Ty, Type *&OriginalElementType, uint64_t &TotalSize)
LLVM_ABI void reportFatalInternalError(Error Err)
Report a fatal error that indicates a bug in LLVM.
Definition Error.cpp:177
constexpr unsigned storageClassToAddressSpace(SPIRV::StorageClass::StorageClass SC)
Definition SPIRVUtils.h:244
bool getSpirvBuiltInIdByName(llvm::StringRef Name, SPIRV::BuiltIn::BuiltIn &BI)
MetadataAsValue * buildMD(Value *Arg)
Definition SPIRVUtils.h:511
bool isTypedPointerTy(const Type *T)
Definition SPIRVUtils.h:349
void buildOpDecorate(Register Reg, MachineIRBuilder &MIRBuilder, SPIRV::Decoration::Decoration Dec, const std::vector< uint32_t > &DecArgs, StringRef StrImm)
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
Type * getTypedPointerWrapper(Type *ElemTy, unsigned AS)
Definition SPIRVUtils.h:396
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:279
void buildOpMemberDecorate(Register Reg, MachineIRBuilder &MIRBuilder, SPIRV::Decoration::Decoration Dec, uint32_t Member, const std::vector< uint32_t > &DecArgs, StringRef StrImm)
Type * toTypedPointer(Type *Ty)
Definition SPIRVUtils.h:456
bool isSpecialOpaqueType(const Type *Ty)
bool isPointerTy(const Type *T)
Definition SPIRVUtils.h:359
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
MachineBasicBlock::iterator getInsertPtValidEnd(MachineBasicBlock *MBB)
const Type * unifyPtrType(const Type *Ty)
Definition SPIRVUtils.h:483
const MachineInstr SPIRVType
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
std::function< void(Register)> StructOffsetDecorator
SPIRV::StorageClass::StorageClass addressSpaceToStorageClass(unsigned AddrSpace, const SPIRVSubtarget &STI)
void buildOpSpirvDecorations(Register Reg, MachineIRBuilder &MIRBuilder, const MDNode *GVarMD, const SPIRVSubtarget &ST)
Type * parseBasicTypeName(StringRef &TypeName, LLVMContext &Ctx)
DWARFExpression::Operation Op
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
bool hasBuiltinTypePrefix(StringRef Name)
bool isPointerTyOrWrapper(const Type *Ty)
Definition SPIRVUtils.h:408
bool isSpvIntrinsic(const MachineInstr &MI, Intrinsic::ID IntrinsicID)
PoisonValue * getNormalizedPoisonValue(Type *Ty)
Definition SPIRVUtils.h:507
void addStringImm(const StringRef &Str, MCInst &Inst)
MachineInstr * getVRegDef(MachineRegisterInfo &MRI, Register Reg)
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:180
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
Definition Alignment.h:130