LLVM 23.0.0git
SPIRVPreLegalizer.cpp
Go to the documentation of this file.
1//===-- SPIRVPreLegalizer.cpp - prepare IR for legalization -----*- 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// The pass prepares IR for legalization: it assigns SPIR-V types to registers
10// and removes intrinsics which holded these types during IR translation.
11// Also it processes constants and registers them in GR to avoid duplication.
12//
13//===----------------------------------------------------------------------===//
14
15#include "SPIRV.h"
16#include "SPIRVSubtarget.h"
17#include "SPIRVUtils.h"
21#include "llvm/IR/Attributes.h"
22#include "llvm/IR/Constants.h"
23#include "llvm/IR/IntrinsicsSPIRV.h"
24
25#define DEBUG_TYPE "spirv-prelegalizer"
26
27using namespace llvm;
28
29namespace {
30class SPIRVPreLegalizer : public MachineFunctionPass {
31public:
32 static char ID;
33 SPIRVPreLegalizer() : MachineFunctionPass(ID) {}
34 bool runOnMachineFunction(MachineFunction &MF) override;
35 void getAnalysisUsage(AnalysisUsage &AU) const override;
36};
37} // namespace
38
39void SPIRVPreLegalizer::getAnalysisUsage(AnalysisUsage &AU) const {
40 AU.addPreserved<GISelValueTrackingAnalysisLegacy>();
42}
43
44static void
46 const SPIRVSubtarget &STI,
47 DenseMap<MachineInstr *, Type *> &TargetExtConstTypes) {
49 DenseMap<MachineInstr *, Register> RegsAlreadyAddedToDT;
50 SmallVector<MachineInstr *, 10> ToErase, ToEraseComposites;
51 for (MachineBasicBlock &MBB : MF) {
52 for (MachineInstr &MI : MBB) {
53 if (!isSpvIntrinsic(MI, Intrinsic::spv_track_constant))
54 continue;
55 ToErase.push_back(&MI);
56 Register SrcReg = MI.getOperand(2).getReg();
57 auto *Const =
59 MI.getOperand(3).getMetadata()->getOperand(0))
60 ->getValue());
61 if (auto *GV = dyn_cast<GlobalValue>(Const)) {
62 Register Reg = GR->find(GV, &MF);
63 if (!Reg.isValid()) {
64 GR->add(GV, MRI.getVRegDef(SrcReg));
65 GR->addGlobalObject(GV, &MF, SrcReg);
66 } else
67 RegsAlreadyAddedToDT[&MI] = Reg;
68 } else {
69 Register Reg = GR->find(Const, &MF);
70 if (!Reg.isValid()) {
71 if (auto *ConstVec = dyn_cast<ConstantDataVector>(Const)) {
72 auto *BuildVec = MRI.getVRegDef(SrcReg);
73 assert(BuildVec &&
74 BuildVec->getOpcode() == TargetOpcode::G_BUILD_VECTOR);
75 GR->add(Const, BuildVec);
76 for (unsigned i = 0; i < ConstVec->getNumElements(); ++i) {
77 // Ensure that OpConstantComposite reuses a constant when it's
78 // already created and available in the same machine function.
79 Constant *ElemConst = ConstVec->getElementAsConstant(i);
80 Register ElemReg = GR->find(ElemConst, &MF);
81 if (!ElemReg.isValid())
82 GR->add(ElemConst,
83 MRI.getVRegDef(BuildVec->getOperand(1 + i).getReg()));
84 else
85 BuildVec->getOperand(1 + i).setReg(ElemReg);
86 }
87 }
88 if (Const->getType()->isTargetExtTy()) {
89 // remember association so that we can restore it when assign types
90 MachineInstr *SrcMI = MRI.getVRegDef(SrcReg);
91 if (SrcMI)
92 GR->add(Const, SrcMI);
93 if (SrcMI && (SrcMI->getOpcode() == TargetOpcode::G_CONSTANT ||
94 SrcMI->getOpcode() == TargetOpcode::G_IMPLICIT_DEF))
95 TargetExtConstTypes[SrcMI] = Const->getType();
96 if (Const->isNullValue()) {
97 MachineBasicBlock &DepMBB = MF.front();
98 MachineIRBuilder MIB(DepMBB, DepMBB.getFirstNonPHI());
100 Const->getType(), MIB, SPIRV::AccessQualifier::ReadWrite,
101 true);
102 assert(SrcMI && "Expected source instruction to be valid");
103 SrcMI->setDesc(STI.getInstrInfo()->get(SPIRV::OpConstantNull));
105 GR->getSPIRVTypeID(ExtType), false));
106 }
107 }
108 } else {
109 RegsAlreadyAddedToDT[&MI] = Reg;
110 // This MI is unused and will be removed. If the MI uses
111 // const_composite, it will be unused and should be removed too.
112 assert(MI.getOperand(2).isReg() && "Reg operand is expected");
113 MachineInstr *SrcMI = MRI.getVRegDef(MI.getOperand(2).getReg());
114 if (SrcMI && isSpvIntrinsic(*SrcMI, Intrinsic::spv_const_composite))
115 ToEraseComposites.push_back(SrcMI);
116 }
117 }
118 }
119 }
120 for (MachineInstr *MI : ToErase) {
121 Register Reg = MI->getOperand(2).getReg();
122 auto It = RegsAlreadyAddedToDT.find(MI);
123 if (It != RegsAlreadyAddedToDT.end())
124 Reg = It->second;
125 auto *RC = MRI.getRegClassOrNull(MI->getOperand(0).getReg());
126 if (!MRI.getRegClassOrNull(Reg) && RC)
127 MRI.setRegClass(Reg, RC);
128 MRI.replaceRegWith(MI->getOperand(0).getReg(), Reg);
130 MI->eraseFromParent();
131 }
132 for (MachineInstr *MI : ToEraseComposites) {
134 MI->eraseFromParent();
135 }
136}
137
140 MachineIRBuilder MIB) {
142 for (MachineBasicBlock &MBB : MF) {
143 for (MachineInstr &MI : MBB) {
144 if (!isSpvIntrinsic(MI, Intrinsic::spv_assign_name))
145 continue;
146 const MDNode *MD = MI.getOperand(2).getMetadata();
147 StringRef ValueName = cast<MDString>(MD->getOperand(0))->getString();
148 if (ValueName.size() > 0) {
149 MIB.setInsertPt(*MI.getParent(), MI);
150 buildOpName(MI.getOperand(1).getReg(), ValueName, MIB);
151 }
152 ToErase.push_back(&MI);
153 }
154 for (MachineInstr *MI : ToErase) {
156 MI->eraseFromParent();
157 }
158 ToErase.clear();
159 }
160}
161
164 for (MachineRegisterInfo::use_instr_iterator I = MRI->use_instr_begin(Reg),
165 IE = MRI->use_instr_end();
166 I != IE; ++I) {
167 MachineInstr *UseMI = &*I;
168 if ((isSpvIntrinsic(*UseMI, Intrinsic::spv_assign_ptr_type) ||
169 isSpvIntrinsic(*UseMI, Intrinsic::spv_assign_type)) &&
170 UseMI->getOperand(1).getReg() == Reg)
171 return UseMI;
172 }
173 return nullptr;
174}
175
177 Register ResVReg, Register OpReg) {
178 SPIRVTypeInst ResType = GR->getSPIRVTypeForVReg(ResVReg);
179 SPIRVTypeInst OpType = GR->getSPIRVTypeForVReg(OpReg);
180 assert(ResType && OpType && "Operand types are expected");
181 if (!GR->isBitcastCompatible(ResType, OpType))
182 report_fatal_error("incompatible result and operand types in a bitcast");
184 if (!MRI->getRegClassOrNull(ResVReg))
185 MRI->setRegClass(ResVReg, GR->getRegClass(ResType));
186 if (ResType == OpType)
187 MIB.buildInstr(TargetOpcode::COPY).addDef(ResVReg).addUse(OpReg);
188 else
189 MIB.buildInstr(SPIRV::OpBitcast)
190 .addDef(ResVReg)
191 .addUse(GR->getSPIRVTypeID(ResType))
192 .addUse(OpReg);
193}
194
195// We lower G_BITCAST to OpBitcast here to avoid a MachineVerifier error.
196// The verifier checks if the source and destination LLTs of a G_BITCAST are
197// different, but this check is too strict for SPIR-V's typed pointers, which
198// may have the same LLT but different SPIRV type (e.g. pointers to different
199// pointee types). By lowering to OpBitcast here, we bypass the verifier's
200// check. See discussion in https://github.com/llvm/llvm-project/pull/110270
201// for more context.
202//
203// We also handle the llvm.spv.bitcast intrinsic here. If the source and
204// destination SPIR-V types are the same, we lower it to a COPY to enable
205// further optimizations like copy propagation.
207 MachineIRBuilder MIB) {
209 for (MachineBasicBlock &MBB : MF) {
210 for (MachineInstr &MI : MBB) {
211 if (isSpvIntrinsic(MI, Intrinsic::spv_bitcast)) {
212 Register DstReg = MI.getOperand(0).getReg();
213 Register SrcReg = MI.getOperand(2).getReg();
214 SPIRVTypeInst DstType = GR->getSPIRVTypeForVReg(DstReg);
215 assert(
216 DstType &&
217 "Expected destination SPIR-V type to have been assigned already.");
218 SPIRVTypeInst SrcType = GR->getSPIRVTypeForVReg(SrcReg);
219 assert(SrcType &&
220 "Expected source SPIR-V type to have been assigned already.");
221 if (DstType == SrcType) {
222 MIB.setInsertPt(*MI.getParent(), MI);
223 MIB.buildCopy(DstReg, SrcReg);
224 ToErase.push_back(&MI);
225 continue;
226 }
227 }
228
229 if (MI.getOpcode() != TargetOpcode::G_BITCAST)
230 continue;
231
232 MIB.setInsertPt(*MI.getParent(), MI);
233 buildOpBitcast(GR, MIB, MI.getOperand(0).getReg(),
234 MI.getOperand(1).getReg());
235 ToErase.push_back(&MI);
236 }
237 }
238 for (MachineInstr *MI : ToErase) {
240 MI->eraseFromParent();
241 }
242}
243
245 MachineIRBuilder MIB) {
246 // Get access to information about available extensions
247 const SPIRVSubtarget *ST =
248 static_cast<const SPIRVSubtarget *>(&MIB.getMF().getSubtarget());
250 for (MachineBasicBlock &MBB : MF) {
251 for (MachineInstr &MI : MBB) {
252 if (!isSpvIntrinsic(MI, Intrinsic::spv_ptrcast))
253 continue;
254 assert(MI.getOperand(2).isReg());
255 MIB.setInsertPt(*MI.getParent(), MI);
256 ToErase.push_back(&MI);
257 Register Def = MI.getOperand(0).getReg();
258 Register Source = MI.getOperand(2).getReg();
259 Type *ElemTy = getMDOperandAsType(MI.getOperand(3).getMetadata(), 0);
260 auto SC =
261 isa<FunctionType>(ElemTy)
262 ? SPIRV::StorageClass::CodeSectionINTEL
263 : addressSpaceToStorageClass(MI.getOperand(4).getImm(), *ST);
264 SPIRVTypeInst AssignedPtrType =
265 GR->getOrCreateSPIRVPointerType(ElemTy, MI, SC);
266
267 // If the ptrcast would be redundant, replace all uses with the source
268 // register.
270 if (GR->getSPIRVTypeForVReg(Source) == AssignedPtrType) {
271 // Erase Def's assign type instruction if we are going to replace Def.
272 if (MachineInstr *AssignMI = findAssignTypeInstr(Def, MRI))
273 ToErase.push_back(AssignMI);
274 MRI->replaceRegWith(Def, Source);
275 } else {
276 if (!GR->getSPIRVTypeForVReg(Def, &MF))
277 GR->assignSPIRVTypeToVReg(AssignedPtrType, Def, MF);
278 MIB.buildBitcast(Def, Source);
279 }
280 }
281 }
282 for (MachineInstr *MI : ToErase) {
284 MI->eraseFromParent();
285 }
286}
287
288// Translating GV, IRTranslator sometimes generates following IR:
289// %1 = G_GLOBAL_VALUE
290// %2 = COPY %1
291// %3 = G_ADDRSPACE_CAST %2
292//
293// or
294//
295// %1 = G_ZEXT %2
296// G_MEMCPY ... %2 ...
297//
298// New registers have no SPIRV type and no register class info.
299//
300// Set SPIRV type for GV, propagate it from GV to other instructions,
301// also set register classes.
305 MachineIRBuilder &MIB) {
306 SPIRVTypeInst SpvType = nullptr;
307 assert(MI && "Machine instr is expected");
308 if (MI->getOperand(0).isReg()) {
309 Register Reg = MI->getOperand(0).getReg();
310 SpvType = GR->getSPIRVTypeForVReg(Reg);
311 if (!SpvType) {
312 switch (MI->getOpcode()) {
313 case TargetOpcode::G_FCONSTANT:
314 case TargetOpcode::G_CONSTANT: {
315 MIB.setInsertPt(*MI->getParent(), MI);
316 Type *Ty = MI->getOperand(1).getCImm()->getType();
317 SpvType = GR->getOrCreateSPIRVType(
318 Ty, MIB, SPIRV::AccessQualifier::ReadWrite, true);
319 break;
320 }
321 case TargetOpcode::G_GLOBAL_VALUE: {
322 MIB.setInsertPt(*MI->getParent(), MI);
323 const GlobalValue *Global = MI->getOperand(1).getGlobal();
325 auto *Ty = TypedPointerType::get(ElementTy,
326 Global->getType()->getAddressSpace());
327 SpvType = GR->getOrCreateSPIRVType(
328 Ty, MIB, SPIRV::AccessQualifier::ReadWrite, true);
329 break;
330 }
331 case TargetOpcode::G_ANYEXT:
332 case TargetOpcode::G_SEXT:
333 case TargetOpcode::G_ZEXT: {
334 if (MI->getOperand(1).isReg()) {
335 if (MachineInstr *DefInstr =
336 MRI.getVRegDef(MI->getOperand(1).getReg())) {
337 if (SPIRVTypeInst Def =
338 propagateSPIRVType(DefInstr, GR, MRI, MIB)) {
339 unsigned CurrentBW = GR->getScalarOrVectorBitWidth(Def);
340 unsigned ExpectedBW =
341 std::max(MRI.getType(Reg).getScalarSizeInBits(), CurrentBW);
342 unsigned NumElements = GR->getScalarOrVectorComponentCount(Def);
343 SpvType = GR->getOrCreateSPIRVIntegerType(ExpectedBW, MIB);
344 if (NumElements > 1)
345 SpvType = GR->getOrCreateSPIRVVectorType(SpvType, NumElements,
346 MIB, true);
347 }
348 }
349 }
350 break;
351 }
352 case TargetOpcode::G_PTRTOINT:
353 SpvType = GR->getOrCreateSPIRVIntegerType(
354 MRI.getType(Reg).getScalarSizeInBits(), MIB);
355 break;
356 case TargetOpcode::G_TRUNC:
357 case TargetOpcode::G_ADDRSPACE_CAST:
358 case TargetOpcode::G_PTR_ADD:
359 case TargetOpcode::COPY: {
360 MachineOperand &Op = MI->getOperand(1);
361 MachineInstr *Def = Op.isReg() ? MRI.getVRegDef(Op.getReg()) : nullptr;
362 if (Def)
363 SpvType = propagateSPIRVType(Def, GR, MRI, MIB);
364 break;
365 }
366 default:
367 break;
368 }
369 if (SpvType) {
370 // check if the address space needs correction
371 LLT RegType = MRI.getType(Reg);
372 if (SpvType->getOpcode() == SPIRV::OpTypePointer &&
373 RegType.isPointer() &&
375 RegType.getAddressSpace()) {
376 const SPIRVSubtarget &ST =
377 MI->getParent()->getParent()->getSubtarget<SPIRVSubtarget>();
378 auto TSC = addressSpaceToStorageClass(RegType.getAddressSpace(), ST);
379 SpvType = GR->changePointerStorageClass(SpvType, TSC, *MI);
380 }
381 GR->assignSPIRVTypeToVReg(SpvType, Reg, MIB.getMF());
382 }
383 if (!MRI.getRegClassOrNull(Reg))
384 MRI.setRegClass(Reg, SpvType ? GR->getRegClass(SpvType)
385 : &SPIRV::iIDRegClass);
386 }
387 }
388 return SpvType;
389}
390
391// To support current approach and limitations wrt. bit width here we widen a
392// scalar register with a bit width greater than 1 to valid sizes and cap it to
393// 128 width.
394static unsigned widenBitWidthToNextPow2(unsigned BitWidth) {
395 if (BitWidth == 1)
396 return 1; // No need to widen 1-bit values
397 return std::min(std::max(1u << Log2_32_Ceil(BitWidth), 8u), 128u);
398}
399
401 LLT RegType = MRI.getType(Reg);
402 if (!RegType.isScalar())
403 return;
404 unsigned CurrentWidth = RegType.getScalarSizeInBits();
405 unsigned NewWidth = widenBitWidthToNextPow2(CurrentWidth);
406 if (NewWidth != CurrentWidth)
407 MRI.setType(Reg, LLT::scalar(NewWidth));
408}
409
410static void widenCImmType(MachineOperand &MOP) {
411 const ConstantInt *CImmVal = MOP.getCImm();
412 unsigned CurrentWidth = CImmVal->getBitWidth();
413 unsigned NewWidth = widenBitWidthToNextPow2(CurrentWidth);
414 if (NewWidth != CurrentWidth) {
415 // Replace the immediate value with the widened version
416 MOP.setCImm(ConstantInt::get(CImmVal->getType()->getContext(),
417 CImmVal->getValue().zextOrTrunc(NewWidth)));
418 }
419}
420
422 MachineBasicBlock &MBB = *Def->getParent();
424 Def->getNextNode() ? Def->getNextNode()->getIterator() : MBB.end();
425 // Skip all the PHI and debug instructions.
426 while (DefIt != MBB.end() &&
427 (DefIt->isPHI() || DefIt->isDebugOrPseudoInstr()))
428 DefIt = std::next(DefIt);
429 MIB.setInsertPt(MBB, DefIt);
430}
431
432namespace llvm {
436 assert((Ty || SpvType) && "Either LLVM or SPIRV type is expected.");
437 MachineInstr *Def = MRI.getVRegDef(Reg);
438 setInsertPtAfterDef(MIB, Def);
439 if (!SpvType)
440 SpvType = GR->getOrCreateSPIRVType(Ty, MIB,
441 SPIRV::AccessQualifier::ReadWrite, true);
442 if (!MRI.getRegClassOrNull(Reg))
443 MRI.setRegClass(Reg, GR->getRegClass(SpvType));
444 if (!MRI.getType(Reg).isValid())
445 MRI.setType(Reg, GR->getRegType(SpvType));
446 GR->assignSPIRVTypeToVReg(SpvType, Reg, MIB.getMF());
447}
448
451 SPIRVTypeInst KnownResType) {
452 MIB.setInsertPt(*MI.getParent(), MI.getIterator());
453 for (auto &Op : MI.operands()) {
454 if (!Op.isReg() || Op.isDef())
455 continue;
456 Register OpReg = Op.getReg();
457 SPIRVTypeInst SpvType = GR->getSPIRVTypeForVReg(OpReg);
458 if (!SpvType && KnownResType) {
459 SpvType = KnownResType;
460 GR->assignSPIRVTypeToVReg(KnownResType, OpReg, *MI.getMF());
461 }
462 assert(SpvType);
463 if (!MRI.getRegClassOrNull(OpReg))
464 MRI.setRegClass(OpReg, GR->getRegClass(SpvType));
465 if (!MRI.getType(OpReg).isValid())
466 MRI.setType(OpReg, GR->getRegType(SpvType));
467 }
468}
469} // namespace llvm
470
471static void
474 DenseMap<MachineInstr *, Type *> &TargetExtConstTypes) {
475 // Get access to information about available extensions
476 const SPIRVSubtarget *ST =
477 static_cast<const SPIRVSubtarget *>(&MIB.getMF().getSubtarget());
478
481 DenseMap<MachineInstr *, Register> RegsAlreadyAddedToDT;
482
483 bool IsExtendedInts =
484 ST->canUseExtension(
485 SPIRV::Extension::SPV_ALTERA_arbitrary_precision_integers) ||
486 ST->canUseExtension(SPIRV::Extension::SPV_KHR_bit_instructions) ||
487 ST->canUseExtension(SPIRV::Extension::SPV_INTEL_int4);
488
489 for (MachineBasicBlock *MBB : post_order(&MF)) {
490 if (MBB->empty())
491 continue;
492
493 bool ReachedBegin = false;
494 for (auto MII = std::prev(MBB->end()), Begin = MBB->begin();
495 !ReachedBegin;) {
496 MachineInstr &MI = *MII;
497 unsigned MIOp = MI.getOpcode();
498
499 if (!IsExtendedInts) {
500 // validate bit width of scalar registers and constant immediates
501 for (auto &MOP : MI.operands()) {
502 if (MOP.isReg())
503 widenScalarType(MOP.getReg(), MRI);
504 else if (MOP.isCImm())
505 widenCImmType(MOP);
506 }
507 }
508
509 if (isSpvIntrinsic(MI, Intrinsic::spv_assign_ptr_type)) {
510 Register Reg = MI.getOperand(1).getReg();
511 MIB.setInsertPt(*MI.getParent(), MI.getIterator());
512 Type *ElementTy = getMDOperandAsType(MI.getOperand(2).getMetadata(), 0);
513 SPIRVTypeInst AssignedPtrType = GR->getOrCreateSPIRVPointerType(
514 ElementTy, MI,
515 addressSpaceToStorageClass(MI.getOperand(3).getImm(), *ST));
516 MachineInstr *Def = MRI.getVRegDef(Reg);
517 assert(Def && "Expecting an instruction that defines the register");
518 // G_GLOBAL_VALUE already has type info.
519 if (Def->getOpcode() != TargetOpcode::G_GLOBAL_VALUE)
520 updateRegType(Reg, nullptr, AssignedPtrType, GR, MIB,
521 MF.getRegInfo());
522 ToErase.push_back(&MI);
523 } else if (isSpvIntrinsic(MI, Intrinsic::spv_assign_type)) {
524 Register Reg = MI.getOperand(1).getReg();
525 Type *Ty = getMDOperandAsType(MI.getOperand(2).getMetadata(), 0);
526 MachineInstr *Def = MRI.getVRegDef(Reg);
527 assert(Def && "Expecting an instruction that defines the register");
528 // G_GLOBAL_VALUE already has type info.
529 if (Def->getOpcode() != TargetOpcode::G_GLOBAL_VALUE)
530 updateRegType(Reg, Ty, nullptr, GR, MIB, MF.getRegInfo());
531 ToErase.push_back(&MI);
532 } else if (MIOp == TargetOpcode::FAKE_USE && MI.getNumOperands() > 0) {
533 MachineInstr *MdMI = MI.getPrevNode();
534 if (MdMI && isSpvIntrinsic(*MdMI, Intrinsic::spv_value_md)) {
535 // It's an internal service info from before IRTranslator passes.
536 MachineInstr *Def = getVRegDef(MRI, MI.getOperand(0).getReg());
537 for (unsigned I = 1, E = MI.getNumOperands(); I != E && Def; ++I)
538 if (getVRegDef(MRI, MI.getOperand(I).getReg()) != Def)
539 Def = nullptr;
540 if (Def) {
541 const MDNode *MD = MdMI->getOperand(1).getMetadata();
543 cast<MDString>(MD->getOperand(1))->getString();
544 const MDNode *TypeMD = cast<MDNode>(MD->getOperand(0));
545 Type *ValueTy = getMDOperandAsType(TypeMD, 0);
546 GR->addValueAttrs(Def, std::make_pair(ValueTy, ValueName.str()));
547 }
548 ToErase.push_back(MdMI);
549 }
550 ToErase.push_back(&MI);
551 } else if (MIOp == TargetOpcode::G_CONSTANT ||
552 MIOp == TargetOpcode::G_FCONSTANT ||
553 MIOp == TargetOpcode::G_BUILD_VECTOR) {
554 // %rc = G_CONSTANT ty Val
555 // Ensure %rc has a valid SPIR-V type assigned in the Global Registry.
556 Register Reg = MI.getOperand(0).getReg();
557 bool NeedAssignType = !GR->getSPIRVTypeForVReg(Reg);
558 Type *Ty = nullptr;
559 if (MIOp == TargetOpcode::G_CONSTANT) {
560 auto TargetExtIt = TargetExtConstTypes.find(&MI);
561 Ty = TargetExtIt == TargetExtConstTypes.end()
562 ? MI.getOperand(1).getCImm()->getType()
563 : TargetExtIt->second;
564 const ConstantInt *OpCI = MI.getOperand(1).getCImm();
565 // TODO: we may wish to analyze here if OpCI is zero and LLT RegType =
566 // MRI.getType(Reg); RegType.isPointer() is true, so that we observe
567 // at this point not i64/i32 constant but null pointer in the
568 // corresponding address space of RegType.getAddressSpace(). This may
569 // help to successfully validate the case when a OpConstantComposite's
570 // constituent has type that does not match Result Type of
571 // OpConstantComposite (see, for example,
572 // pointers/PtrCast-null-in-OpSpecConstantOp.ll).
573 Register PrimaryReg = GR->find(OpCI, &MF);
574 if (!PrimaryReg.isValid()) {
575 GR->add(OpCI, &MI);
576 } else if (PrimaryReg != Reg &&
577 MRI.getType(Reg) == MRI.getType(PrimaryReg)) {
578 auto *RCReg = MRI.getRegClassOrNull(Reg);
579 auto *RCPrimary = MRI.getRegClassOrNull(PrimaryReg);
580 if (!RCReg || RCPrimary == RCReg) {
581 RegsAlreadyAddedToDT[&MI] = PrimaryReg;
582 ToErase.push_back(&MI);
583 NeedAssignType = false;
584 }
585 }
586 } else if (MIOp == TargetOpcode::G_FCONSTANT) {
587 Ty = MI.getOperand(1).getFPImm()->getType();
588 } else {
589 assert(MIOp == TargetOpcode::G_BUILD_VECTOR);
590 Type *ElemTy = nullptr;
591 MachineInstr *ElemMI = MRI.getVRegDef(MI.getOperand(1).getReg());
592 assert(ElemMI);
593
594 if (ElemMI->getOpcode() == TargetOpcode::G_CONSTANT) {
595 ElemTy = ElemMI->getOperand(1).getCImm()->getType();
596 } else if (ElemMI->getOpcode() == TargetOpcode::G_FCONSTANT) {
597 ElemTy = ElemMI->getOperand(1).getFPImm()->getType();
598 } else {
599 if (SPIRVTypeInst ElemSpvType =
600 GR->getSPIRVTypeForVReg(MI.getOperand(1).getReg(), &MF))
601 ElemTy = const_cast<Type *>(GR->getTypeForSPIRVType(ElemSpvType));
602 }
603 if (ElemTy)
604 Ty = VectorType::get(
605 ElemTy, MI.getNumExplicitOperands() - MI.getNumExplicitDefs(),
606 false);
607 else
608 NeedAssignType = false;
609 }
610 if (NeedAssignType)
611 updateRegType(Reg, Ty, nullptr, GR, MIB, MRI);
612 } else if (MIOp == TargetOpcode::G_GLOBAL_VALUE) {
613 propagateSPIRVType(&MI, GR, MRI, MIB);
614 }
615
616 if (MII == Begin)
617 ReachedBegin = true;
618 else
619 --MII;
620 }
621 }
622 for (MachineInstr *MI : ToErase) {
623 auto It = RegsAlreadyAddedToDT.find(MI);
624 if (It != RegsAlreadyAddedToDT.end())
625 MRI.replaceRegWith(MI->getOperand(0).getReg(), It->second);
627 MI->eraseFromParent();
628 }
629
630 // Address the case when IRTranslator introduces instructions with new
631 // registers without associated SPIRV type.
632 for (MachineBasicBlock &MBB : MF) {
633 for (MachineInstr &MI : MBB) {
634 switch (MI.getOpcode()) {
635 case TargetOpcode::G_TRUNC:
636 case TargetOpcode::G_ANYEXT:
637 case TargetOpcode::G_SEXT:
638 case TargetOpcode::G_ZEXT:
639 case TargetOpcode::G_PTRTOINT:
640 case TargetOpcode::COPY:
641 case TargetOpcode::G_ADDRSPACE_CAST:
642 propagateSPIRVType(&MI, GR, MRI, MIB);
643 break;
644 }
645 }
646 }
647}
648
651 MachineIRBuilder MIB) {
653 for (MachineBasicBlock &MBB : MF)
654 for (MachineInstr &MI : MBB)
655 if (isTypeFoldingSupported(MI.getOpcode()))
656 processInstr(MI, MIB, MRI, GR, nullptr);
657}
658
659static Register
661 SmallVector<unsigned, 4> *Ops = nullptr) {
662 Register DefReg;
663 unsigned StartOp = InlineAsm::MIOp_FirstOperand,
665 for (unsigned Idx = StartOp, MISz = MI->getNumOperands(); Idx != MISz;
666 ++Idx) {
667 const MachineOperand &MO = MI->getOperand(Idx);
668 if (MO.isMetadata())
669 continue;
670 if (Idx == AsmDescOp && MO.isImm()) {
671 // compute the index of the next operand descriptor
672 const InlineAsm::Flag F(MO.getImm());
673 AsmDescOp += 1 + F.getNumOperandRegisters();
674 continue;
675 }
676 if (MO.isReg() && MO.isDef()) {
677 if (!Ops)
678 return MO.getReg();
679 else
680 DefReg = MO.getReg();
681 } else if (Ops) {
682 Ops->push_back(Idx);
683 }
684 }
685 return DefReg;
686}
687
688static void
690 const SPIRVSubtarget &ST, MachineIRBuilder MIRBuilder,
691 const SmallVector<MachineInstr *> &ToProcess) {
693 Register AsmTargetReg;
694 for (unsigned i = 0, Sz = ToProcess.size(); i + 1 < Sz; i += 2) {
695 MachineInstr *I1 = ToProcess[i], *I2 = ToProcess[i + 1];
696 assert(isSpvIntrinsic(*I1, Intrinsic::spv_inline_asm) && I2->isInlineAsm());
697 MIRBuilder.setInsertPt(*I2->getParent(), *I2);
698
699 if (!AsmTargetReg.isValid()) {
700 // define vendor specific assembly target or dialect
701 AsmTargetReg = MRI.createGenericVirtualRegister(LLT::scalar(32));
702 MRI.setRegClass(AsmTargetReg, &SPIRV::iIDRegClass);
703 auto AsmTargetMIB =
704 MIRBuilder.buildInstr(SPIRV::OpAsmTargetINTEL).addDef(AsmTargetReg);
705 addStringImm(ST.getTargetTripleAsStr(), AsmTargetMIB);
706 GR->add(AsmTargetMIB.getInstr(), AsmTargetMIB);
707 }
708
709 // create types
710 const MDNode *IAMD = I1->getOperand(1).getMetadata();
713 for (const auto &ArgTy : FTy->params())
714 ArgTypes.push_back(GR->getOrCreateSPIRVType(
715 ArgTy, MIRBuilder, SPIRV::AccessQualifier::ReadWrite, true));
716 SPIRVTypeInst RetType =
717 GR->getOrCreateSPIRVType(FTy->getReturnType(), MIRBuilder,
718 SPIRV::AccessQualifier::ReadWrite, true);
720 FTy, RetType, ArgTypes, MIRBuilder);
721
722 // define vendor specific assembly instructions string
723 Register AsmReg = MRI.createGenericVirtualRegister(LLT::scalar(32));
724 MRI.setRegClass(AsmReg, &SPIRV::iIDRegClass);
725 auto AsmMIB = MIRBuilder.buildInstr(SPIRV::OpAsmINTEL)
726 .addDef(AsmReg)
727 .addUse(GR->getSPIRVTypeID(RetType))
728 .addUse(GR->getSPIRVTypeID(FuncType))
729 .addUse(AsmTargetReg);
730 // inline asm string:
731 addStringImm(I2->getOperand(InlineAsm::MIOp_AsmString).getSymbolName(),
732 AsmMIB);
733 // inline asm constraint string:
734 addStringImm(cast<MDString>(I1->getOperand(2).getMetadata()->getOperand(0))
735 ->getString(),
736 AsmMIB);
737 GR->add(AsmMIB.getInstr(), AsmMIB);
738
739 // calls the inline assembly instruction
740 unsigned ExtraInfo = I2->getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
741 if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
742 MIRBuilder.buildInstr(SPIRV::OpDecorate)
743 .addUse(AsmReg)
744 .addImm(static_cast<uint32_t>(SPIRV::Decoration::SideEffectsINTEL));
745
747 if (!DefReg.isValid()) {
748 DefReg = MRI.createGenericVirtualRegister(LLT::scalar(32));
749 MRI.setRegClass(DefReg, &SPIRV::iIDRegClass);
750 SPIRVTypeInst VoidType = GR->getOrCreateSPIRVType(
751 Type::getVoidTy(MF.getFunction().getContext()), MIRBuilder,
752 SPIRV::AccessQualifier::ReadWrite, true);
753 GR->assignSPIRVTypeToVReg(VoidType, DefReg, MF);
754 }
755
756 auto AsmCall = MIRBuilder.buildInstr(SPIRV::OpAsmCallINTEL)
757 .addDef(DefReg)
758 .addUse(GR->getSPIRVTypeID(RetType))
759 .addUse(AsmReg);
760 for (unsigned IntrIdx = 3; IntrIdx < I1->getNumOperands(); ++IntrIdx)
761 AsmCall.addUse(I1->getOperand(IntrIdx).getReg());
762 }
763 for (MachineInstr *MI : ToProcess) {
765 MI->eraseFromParent();
766 }
767}
768
770 const SPIRVSubtarget &ST,
771 MachineIRBuilder MIRBuilder) {
773 for (MachineBasicBlock &MBB : MF) {
774 for (MachineInstr &MI : MBB) {
775 if (isSpvIntrinsic(MI, Intrinsic::spv_inline_asm) ||
776 MI.getOpcode() == TargetOpcode::INLINEASM)
777 ToProcess.push_back(&MI);
778 }
779 }
780 if (ToProcess.size() == 0)
781 return;
782
783 if (!ST.canUseExtension(SPIRV::Extension::SPV_INTEL_inline_assembly))
784 report_fatal_error("Inline assembly instructions require the "
785 "following SPIR-V extension: SPV_INTEL_inline_assembly",
786 false);
787
788 insertInlineAsmProcess(MF, GR, ST, MIRBuilder, ToProcess);
789}
790
792 union {
793 float F;
794 uint32_t Spir;
795 } FPMaxError;
796 FPMaxError.F = F;
797 return FPMaxError.Spir;
798}
799
801 MachineIRBuilder MIB) {
804 for (MachineBasicBlock &MBB : MF) {
805 for (MachineInstr &MI : MBB) {
806 if (!isSpvIntrinsic(MI, Intrinsic::spv_assign_decoration) &&
807 !isSpvIntrinsic(MI, Intrinsic::spv_assign_aliasing_decoration) &&
808 !isSpvIntrinsic(MI, Intrinsic::spv_assign_fpmaxerror_decoration))
809 continue;
810 MIB.setInsertPt(*MI.getParent(), MI.getNextNode());
811 if (isSpvIntrinsic(MI, Intrinsic::spv_assign_decoration)) {
812 buildOpSpirvDecorations(MI.getOperand(1).getReg(), MIB,
813 MI.getOperand(2).getMetadata(), ST);
814 } else if (isSpvIntrinsic(MI,
815 Intrinsic::spv_assign_fpmaxerror_decoration)) {
817 MI.getOperand(2).getMetadata()->getOperand(0));
818 uint32_t OpValue =
820
821 buildOpDecorate(MI.getOperand(1).getReg(), MIB,
822 SPIRV::Decoration::FPMaxErrorDecorationINTEL,
823 {OpValue});
824 } else {
825 GR->buildMemAliasingOpDecorate(MI.getOperand(1).getReg(), MIB,
826 MI.getOperand(2).getImm(),
827 MI.getOperand(3).getMetadata());
828 }
829
830 ToErase.push_back(&MI);
831 }
832 }
833 for (MachineInstr *MI : ToErase) {
835 MI->eraseFromParent();
836 }
837}
838
839// LLVM allows the switches to use registers as cases, while SPIR-V required
840// those to be immediate values. This function replaces such operands with the
841// equivalent immediate constant.
844 MachineIRBuilder MIB) {
846 for (MachineBasicBlock &MBB : MF) {
847 for (MachineInstr &MI : MBB) {
848 if (!isSpvIntrinsic(MI, Intrinsic::spv_switch))
849 continue;
850
852 NewOperands.push_back(MI.getOperand(0)); // Opcode
853 NewOperands.push_back(MI.getOperand(1)); // Condition
854 NewOperands.push_back(MI.getOperand(2)); // Default
855 for (unsigned i = 3; i < MI.getNumOperands(); i += 2) {
856 Register Reg = MI.getOperand(i).getReg();
858 NewOperands.push_back(
860
861 NewOperands.push_back(MI.getOperand(i + 1));
862 }
863
864 assert(MI.getNumOperands() == NewOperands.size());
865 while (MI.getNumOperands() > 0)
866 MI.removeOperand(0);
867 for (auto &MO : NewOperands)
868 MI.addOperand(MO);
869 }
870 }
871}
872
873// Some instructions are used during CodeGen but should never be emitted.
874// Cleaning up those.
878 for (MachineBasicBlock &MBB : MF) {
879 for (MachineInstr &MI : MBB) {
880 if (isSpvIntrinsic(MI, Intrinsic::spv_track_constant) ||
881 MI.getOpcode() == TargetOpcode::G_BRINDIRECT)
882 ToEraseMI.push_back(&MI);
883 }
884 }
885
886 for (MachineInstr *MI : ToEraseMI) {
888 MI->eraseFromParent();
889 }
890}
891
892// Find all usages of G_BLOCK_ADDR in our intrinsics and replace those
893// operands/registers by the actual MBB it references.
895 MachineIRBuilder MIB) {
896 // Gather the reverse-mapping BB -> MBB.
898 for (MachineBasicBlock &MBB : MF)
899 BB2MBB[MBB.getBasicBlock()] = &MBB;
900
901 // Gather instructions requiring patching. For now, only those can use
902 // G_BLOCK_ADDR.
903 SmallVector<MachineInstr *, 8> InstructionsToPatch;
904 for (MachineBasicBlock &MBB : MF) {
905 for (MachineInstr &MI : MBB) {
906 if (isSpvIntrinsic(MI, Intrinsic::spv_switch) ||
907 isSpvIntrinsic(MI, Intrinsic::spv_loop_merge) ||
908 isSpvIntrinsic(MI, Intrinsic::spv_selection_merge))
909 InstructionsToPatch.push_back(&MI);
910 }
911 }
912
913 // For each instruction to fix, we replace all the G_BLOCK_ADDR operands by
914 // the actual MBB it references. Once those references have been updated, we
915 // can cleanup remaining G_BLOCK_ADDR references.
916 SmallPtrSet<MachineBasicBlock *, 8> ClearAddressTaken;
919 for (MachineInstr *MI : InstructionsToPatch) {
921 for (unsigned i = 0; i < MI->getNumOperands(); ++i) {
922 // The operand is not a register, keep as-is.
923 if (!MI->getOperand(i).isReg()) {
924 NewOps.push_back(MI->getOperand(i));
925 continue;
926 }
927
928 Register Reg = MI->getOperand(i).getReg();
929 MachineInstr *BuildMBB = MRI.getVRegDef(Reg);
930 // The register is not the result of G_BLOCK_ADDR, keep as-is.
931 if (!BuildMBB || BuildMBB->getOpcode() != TargetOpcode::G_BLOCK_ADDR) {
932 NewOps.push_back(MI->getOperand(i));
933 continue;
934 }
935
936 assert(BuildMBB && BuildMBB->getOpcode() == TargetOpcode::G_BLOCK_ADDR &&
937 BuildMBB->getOperand(1).isBlockAddress() &&
938 BuildMBB->getOperand(1).getBlockAddress());
939 BasicBlock *BB =
940 BuildMBB->getOperand(1).getBlockAddress()->getBasicBlock();
941 auto It = BB2MBB.find(BB);
942 if (It == BB2MBB.end())
943 report_fatal_error("cannot find a machine basic block by a basic block "
944 "in a switch statement");
945 MachineBasicBlock *ReferencedBlock = It->second;
946 NewOps.push_back(MachineOperand::CreateMBB(ReferencedBlock));
947
948 ClearAddressTaken.insert(ReferencedBlock);
949 ToEraseMI.insert(BuildMBB);
950 }
951
952 // Replace the operands.
953 assert(MI->getNumOperands() == NewOps.size());
954 while (MI->getNumOperands() > 0)
955 MI->removeOperand(0);
956 for (auto &MO : NewOps)
957 MI->addOperand(MO);
958
959 if (MachineInstr *Next = MI->getNextNode()) {
960 if (isSpvIntrinsic(*Next, Intrinsic::spv_track_constant)) {
961 ToEraseMI.insert(Next);
962 Next = MI->getNextNode();
963 }
964 if (Next && Next->getOpcode() == TargetOpcode::G_BRINDIRECT)
965 ToEraseMI.insert(Next);
966 }
967 }
968
969 // BlockAddress operands were used to keep information between passes,
970 // let's undo the "address taken" status to reflect that Succ doesn't
971 // actually correspond to an IR-level basic block.
972 for (MachineBasicBlock *Succ : ClearAddressTaken)
973 Succ->setAddressTakenIRBlock(nullptr);
974
975 // If we just delete G_BLOCK_ADDR instructions with BlockAddress operands,
976 // this leaves their BasicBlock counterparts in a "address taken" status. This
977 // would make AsmPrinter to generate a series of unneeded labels of a "Address
978 // of block that was removed by CodeGen" kind. Let's first ensure that we
979 // don't have a dangling BlockAddress constants by zapping the BlockAddress
980 // nodes, and only after that proceed with erasing G_BLOCK_ADDR instructions.
981 Constant *Replacement =
982 ConstantInt::get(Type::getInt32Ty(MF.getFunction().getContext()), 1);
983 for (MachineInstr *BlockAddrI : ToEraseMI) {
984 if (BlockAddrI->getOpcode() == TargetOpcode::G_BLOCK_ADDR) {
985 BlockAddress *BA = const_cast<BlockAddress *>(
986 BlockAddrI->getOperand(1).getBlockAddress());
988 ConstantExpr::getIntToPtr(Replacement, BA->getType()));
989 BA->destroyConstant();
990 }
991 GR->invalidateMachineInstr(BlockAddrI);
992 BlockAddrI->eraseFromParent();
993 }
994}
995
997 if (MBB.empty())
998 return true;
999
1000 // Branching SPIR-V intrinsics are not detected by this generic method.
1001 // Thus, we can only trust negative result.
1002 if (!MBB.canFallThrough())
1003 return false;
1004
1005 // Otherwise, we must manually check if we have a SPIR-V intrinsic which
1006 // prevent an implicit fallthrough.
1007 for (MachineBasicBlock::reverse_iterator It = MBB.rbegin(), E = MBB.rend();
1008 It != E; ++It) {
1009 if (isSpvIntrinsic(*It, Intrinsic::spv_switch))
1010 return false;
1011 }
1012 return true;
1013}
1014
1016 MachineIRBuilder MIB) {
1017 // It is valid for MachineBasicBlocks to not finish with a branch instruction.
1018 // In such cases, they will simply fallthrough their immediate successor.
1019 for (MachineBasicBlock &MBB : MF) {
1021 continue;
1022
1023 assert(MBB.succ_size() == 1);
1024 MIB.setInsertPt(MBB, MBB.end());
1025 MIB.buildBr(**MBB.successors().begin());
1026 }
1027}
1028
1029bool SPIRVPreLegalizer::runOnMachineFunction(MachineFunction &MF) {
1030 // Initialize the type registry.
1031 const SPIRVSubtarget &ST = MF.getSubtarget<SPIRVSubtarget>();
1032 SPIRVGlobalRegistry *GR = ST.getSPIRVGlobalRegistry();
1033 GR->setCurrentFunc(MF);
1034 MachineIRBuilder MIB(MF);
1035 // a registry of target extension constants
1036 DenseMap<MachineInstr *, Type *> TargetExtConstTypes;
1037 // to keep record of tracked constants
1038 addConstantsToTrack(MF, GR, ST, TargetExtConstTypes);
1039 foldConstantsIntoIntrinsics(MF, GR, MIB);
1040 insertBitcasts(MF, GR, MIB);
1041 generateAssignInstrs(MF, GR, MIB, TargetExtConstTypes);
1042
1043 processSwitchesConstants(MF, GR, MIB);
1044 processBlockAddr(MF, GR, MIB);
1046
1047 processInstrsWithTypeFolding(MF, GR, MIB);
1049 insertSpirvDecorations(MF, GR, MIB);
1050 insertInlineAsm(MF, GR, ST, MIB);
1051 lowerBitcasts(MF, GR, MIB);
1052
1053 return true;
1054}
1055
1056INITIALIZE_PASS(SPIRVPreLegalizer, DEBUG_TYPE, "SPIRV pre legalizer", false,
1057 false)
1058
1059char SPIRVPreLegalizer::ID = 0;
1060
1061FunctionPass *llvm::createSPIRVPreLegalizerPass() {
1062 return new SPIRVPreLegalizer();
1063}
unsigned const MachineRegisterInfo * MRI
MachineInstrBuilder & UseMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
This file contains the simple types necessary to represent the attributes associated with functions a...
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Provides analysis for continuously CSEing during GISel passes.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Provides analysis for querying information about KnownBits during GISel passes.
#define DEBUG_TYPE
IRTranslator LLVM IR MI
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#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 INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
static Register collectInlineAsmInstrOperands(MachineInstr *MI, SmallVector< unsigned, 4 > *Ops=nullptr)
static void insertInlineAsm(MachineFunction &MF, SPIRVGlobalRegistry *GR, const SPIRVSubtarget &ST, MachineIRBuilder MIRBuilder)
static void cleanupHelperInstructions(MachineFunction &MF, SPIRVGlobalRegistry *GR)
static void insertInlineAsmProcess(MachineFunction &MF, SPIRVGlobalRegistry *GR, const SPIRVSubtarget &ST, MachineIRBuilder MIRBuilder, const SmallVector< MachineInstr * > &ToProcess)
static void removeImplicitFallthroughs(MachineFunction &MF, MachineIRBuilder MIB)
static unsigned widenBitWidthToNextPow2(unsigned BitWidth)
static void setInsertPtAfterDef(MachineIRBuilder &MIB, MachineInstr *Def)
static bool isImplicitFallthrough(MachineBasicBlock &MBB)
static void insertSpirvDecorations(MachineFunction &MF, SPIRVGlobalRegistry *GR, MachineIRBuilder MIB)
static void insertBitcasts(MachineFunction &MF, SPIRVGlobalRegistry *GR, MachineIRBuilder MIB)
static void processInstrsWithTypeFolding(MachineFunction &MF, SPIRVGlobalRegistry *GR, MachineIRBuilder MIB)
static void processSwitchesConstants(MachineFunction &MF, SPIRVGlobalRegistry *GR, MachineIRBuilder MIB)
static void lowerBitcasts(MachineFunction &MF, SPIRVGlobalRegistry *GR, MachineIRBuilder MIB)
static MachineInstr * findAssignTypeInstr(Register Reg, MachineRegisterInfo *MRI)
static void widenCImmType(MachineOperand &MOP)
static void buildOpBitcast(SPIRVGlobalRegistry *GR, MachineIRBuilder &MIB, Register ResVReg, Register OpReg)
static void processBlockAddr(MachineFunction &MF, SPIRVGlobalRegistry *GR, MachineIRBuilder MIB)
static void widenScalarType(Register Reg, MachineRegisterInfo &MRI)
static void foldConstantsIntoIntrinsics(MachineFunction &MF, SPIRVGlobalRegistry *GR, MachineIRBuilder MIB)
static void addConstantsToTrack(MachineFunction &MF, SPIRVGlobalRegistry *GR, const SPIRVSubtarget &STI, DenseMap< MachineInstr *, Type * > &TargetExtConstTypes)
static uint32_t convertFloatToSPIRVWord(float F)
static SPIRVTypeInst propagateSPIRVType(MachineInstr *MI, SPIRVGlobalRegistry *GR, MachineRegisterInfo &MRI, MachineIRBuilder &MIB)
static void generateAssignInstrs(MachineFunction &MF, SPIRVGlobalRegistry *GR, MachineIRBuilder MIB, DenseMap< MachineInstr *, Type * > &TargetExtConstTypes)
LLVM_ABI float convertToFloat() const
Converts this APFloat to host float value.
Definition APFloat.cpp:6065
LLVM_ABI APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition APInt.cpp:1044
Represent the analysis usage information of a pass.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
The address of a basic block.
Definition Constants.h:904
BasicBlock * getBasicBlock() const
Definition Constants.h:939
static LLVM_ABI Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:282
const APFloat & getValueAPF() const
Definition Constants.h:325
This is the shared class of boolean and integer constants.
Definition Constants.h:87
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition Constants.h:162
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:159
This is an important base class in LLVM.
Definition Constant.h:43
LLVM_ABI void destroyConstant()
Called if some element of this constant is no longer valid.
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:178
iterator end()
Definition DenseMap.h:81
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition Function.cpp:358
constexpr unsigned getScalarSizeInBits() const
constexpr bool isScalar() const
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
constexpr bool isPointer() const
constexpr unsigned getAddressSpace() const
Metadata node.
Definition Metadata.h:1080
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1444
LLVM_ABI iterator getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
MachineInstrBundleIterator< MachineInstr, true > reverse_iterator
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
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.
const MachineBasicBlock & front() const
Helper class to build MachineInstr.
MachineInstrBuilder buildBr(MachineBasicBlock &Dest)
Build and insert G_BR Dest.
void setInsertPt(MachineBasicBlock &MBB, MachineBasicBlock::iterator II)
Set the insertion point before the specified position.
MachineInstrBuilder buildInstr(unsigned Opcode)
Build and insert <empty> = Opcode <empty>.
MachineFunction & getMF()
Getter for the function we currently build.
MachineInstrBuilder buildBitcast(const DstOp &Dst, const SrcOp &Src)
Build and insert Dst = G_BITCAST Src.
MachineRegisterInfo * getMRI()
Getter for MRI.
MachineInstrBuilder buildCopy(const DstOp &Res, const SrcOp &Op)
Build and insert Res = COPY Op.
const MachineInstrBuilder & addUse(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addDef(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register definition operand.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
LLVM_ABI void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
LLVM_ABI void setDesc(const MCInstrDesc &TID)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one.
const MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
const ConstantInt * getCImm() const
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
const MDNode * getMetadata() const
static MachineOperand CreateCImm(const ConstantInt *CI)
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
bool isMetadata() const
isMetadata - Tests if this is a MO_Metadata operand.
const BlockAddress * getBlockAddress() const
void setCImm(const ConstantInt *CI)
bool isBlockAddress() const
isBlockAddress - Tests if this is a MO_BlockAddress operand.
Register getReg() const
getReg - Returns the register number.
const ConstantFP * getFPImm() const
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
static MachineOperand CreateMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
defusechain_instr_iterator< true, false, false, true > use_instr_iterator
use_instr_iterator/use_instr_begin/use_instr_end - Walk all uses of the specified register,...
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isValid() const
Definition Register.h:112
void assignSPIRVTypeToVReg(SPIRVTypeInst Type, Register VReg, const MachineFunction &MF)
SPIRVTypeInst getOrCreateOpTypeFunctionWithArgs(const Type *Ty, SPIRVTypeInst RetType, const SmallVectorImpl< SPIRVTypeInst > &ArgTypes, MachineIRBuilder &MIRBuilder)
const TargetRegisterClass * getRegClass(SPIRVTypeInst SpvType) const
unsigned getScalarOrVectorBitWidth(SPIRVTypeInst Type) const
SPIRVTypeInst getOrCreateSPIRVIntegerType(unsigned BitWidth, MachineIRBuilder &MIRBuilder)
SPIRVTypeInst getOrCreateSPIRVVectorType(SPIRVTypeInst BaseType, unsigned NumElements, MachineIRBuilder &MIRBuilder, bool EmitIR)
unsigned getScalarOrVectorComponentCount(Register VReg) const
const Type * getTypeForSPIRVType(SPIRVTypeInst Ty) const
bool isBitcastCompatible(SPIRVTypeInst Type1, SPIRVTypeInst Type2) const
LLT getRegType(SPIRVTypeInst SpvType) const
void invalidateMachineInstr(MachineInstr *MI)
SPIRVTypeInst getOrCreateSPIRVPointerType(const Type *BaseType, MachineIRBuilder &MIRBuilder, SPIRV::StorageClass::StorageClass SC)
Register getSPIRVTypeID(SPIRVTypeInst SpirvType) const
SPIRVTypeInst changePointerStorageClass(SPIRVTypeInst PtrType, SPIRV::StorageClass::StorageClass SC, MachineInstr &I)
void addGlobalObject(const Value *V, const MachineFunction *MF, Register R)
SPIRVTypeInst getOrCreateSPIRVType(const Type *Type, MachineInstr &I, SPIRV::AccessQualifier::AccessQualifier AQ, bool EmitIR)
SPIRVTypeInst getSPIRVTypeForVReg(Register VReg, const MachineFunction *MF=nullptr) const
Type * getDeducedGlobalValueType(const GlobalValue *Global)
void addValueAttrs(MachineInstr *Key, std::pair< Type *, std::string > Val)
void buildMemAliasingOpDecorate(Register Reg, MachineIRBuilder &MIRBuilder, uint32_t Dec, const MDNode *GVarMD)
SPIRV::StorageClass::StorageClass getPointerStorageClass(Register VReg) const
bool add(SPIRV::IRHandle Handle, const MachineInstr *MI)
Register find(SPIRV::IRHandle Handle, const MachineFunction *MF)
const SPIRVInstrInfo * getInstrInfo() const override
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
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
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:296
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:280
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:128
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.
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:553
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > dyn_extract(Y &&MD)
Extract a Value from Metadata, if any.
Definition Metadata.h:696
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
void buildOpName(Register Target, const StringRef &Name, MachineIRBuilder &MIRBuilder)
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition MathExtras.h:344
StringMapEntry< Value * > ValueName
Definition Value.h:56
bool isTypeFoldingSupported(unsigned Opcode)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
FunctionPass * createSPIRVPreLegalizerPass()
iterator_range< po_iterator< T > > post_order(const T &G)
void updateRegType(Register Reg, Type *Ty, SPIRVTypeInst SpirvTy, SPIRVGlobalRegistry *GR, MachineIRBuilder &MIB, MachineRegisterInfo &MRI)
Helper external function for assigning a SPIRV type to a register, ensuring the register class and ty...
constexpr unsigned storageClassToAddressSpace(SPIRV::StorageClass::StorageClass SC)
Definition SPIRVUtils.h:247
void buildOpDecorate(Register Reg, MachineIRBuilder &MIRBuilder, SPIRV::Decoration::Decoration Dec, const std::vector< uint32_t > &DecArgs, StringRef StrImm)
Type * toTypedPointer(Type *Ty)
Definition SPIRVUtils.h:461
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
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
@ Global
Append to llvm.global_dtors.
SPIRV::StorageClass::StorageClass addressSpaceToStorageClass(unsigned AddrSpace, const SPIRVSubtarget &STI)
void buildOpSpirvDecorations(Register Reg, MachineIRBuilder &MIRBuilder, const MDNode *GVarMD, const SPIRVSubtarget &ST)
void processInstr(MachineInstr &MI, MachineIRBuilder &MIB, MachineRegisterInfo &MRI, SPIRVGlobalRegistry *GR, SPIRVTypeInst KnownResType)
FunctionAddr VTableAddr Next
Definition InstrProf.h:141
DWARFExpression::Operation Op
MachineInstr * getDefInstrMaybeConstant(Register &ConstReg, const MachineRegisterInfo *MRI)
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
Type * getMDOperandAsType(const MDNode *N, unsigned I)
bool isSpvIntrinsic(const MachineInstr &MI, Intrinsic::ID IntrinsicID)
void addStringImm(const StringRef &Str, MCInst &Inst)
MachineInstr * getVRegDef(MachineRegisterInfo &MRI, Register Reg)