LLVM 22.0.0git
SPIRVUtils.cpp
Go to the documentation of this file.
1//===--- SPIRVUtils.cpp ---- SPIR-V Utility Functions -----------*- 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 miscellaneous utility functions.
10//
11//===----------------------------------------------------------------------===//
12
13#include "SPIRVUtils.h"
15#include "SPIRV.h"
16#include "SPIRVGlobalRegistry.h"
17#include "SPIRVInstrInfo.h"
18#include "SPIRVSubtarget.h"
19#include "llvm/ADT/StringRef.h"
26#include "llvm/IR/IntrinsicsSPIRV.h"
27#include <queue>
28#include <vector>
29
30namespace llvm {
31
32// The following functions are used to add these string literals as a series of
33// 32-bit integer operands with the correct format, and unpack them if necessary
34// when making string comparisons in compiler passes.
35// SPIR-V requires null-terminated UTF-8 strings padded to 32-bit alignment.
36static uint32_t convertCharsToWord(const StringRef &Str, unsigned i) {
37 uint32_t Word = 0u; // Build up this 32-bit word from 4 8-bit chars.
38 for (unsigned WordIndex = 0; WordIndex < 4; ++WordIndex) {
39 unsigned StrIndex = i + WordIndex;
40 uint8_t CharToAdd = 0; // Initilize char as padding/null.
41 if (StrIndex < Str.size()) { // If it's within the string, get a real char.
42 CharToAdd = Str[StrIndex];
43 }
44 Word |= (CharToAdd << (WordIndex * 8));
45 }
46 return Word;
47}
48
49// Get length including padding and null terminator.
50static size_t getPaddedLen(const StringRef &Str) {
51 return (Str.size() + 4) & ~3;
52}
53
54void addStringImm(const StringRef &Str, MCInst &Inst) {
55 const size_t PaddedLen = getPaddedLen(Str);
56 for (unsigned i = 0; i < PaddedLen; i += 4) {
57 // Add an operand for the 32-bits of chars or padding.
59 }
60}
61
63 const size_t PaddedLen = getPaddedLen(Str);
64 for (unsigned i = 0; i < PaddedLen; i += 4) {
65 // Add an operand for the 32-bits of chars or padding.
66 MIB.addImm(convertCharsToWord(Str, i));
67 }
68}
69
71 std::vector<Value *> &Args) {
72 const size_t PaddedLen = getPaddedLen(Str);
73 for (unsigned i = 0; i < PaddedLen; i += 4) {
74 // Add a vector element for the 32-bits of chars or padding.
75 Args.push_back(B.getInt32(convertCharsToWord(Str, i)));
76 }
77}
78
79std::string getStringImm(const MachineInstr &MI, unsigned StartIndex) {
80 return getSPIRVStringOperand(MI, StartIndex);
81}
82
85 assert(Def && Def->getOpcode() == TargetOpcode::G_GLOBAL_VALUE &&
86 "Expected G_GLOBAL_VALUE");
87 const GlobalValue *GV = Def->getOperand(1).getGlobal();
88 Value *V = GV->getOperand(0);
90 return CDA->getAsCString().str();
91}
92
93void addNumImm(const APInt &Imm, MachineInstrBuilder &MIB) {
94 const auto Bitwidth = Imm.getBitWidth();
95 if (Bitwidth == 1)
96 return; // Already handled
97 else if (Bitwidth <= 32) {
98 MIB.addImm(Imm.getZExtValue());
99 // Asm Printer needs this info to print floating-type correctly
100 if (Bitwidth == 16)
102 return;
103 } else if (Bitwidth <= 64) {
104 uint64_t FullImm = Imm.getZExtValue();
105 uint32_t LowBits = FullImm & 0xffffffff;
106 uint32_t HighBits = (FullImm >> 32) & 0xffffffff;
107 MIB.addImm(LowBits).addImm(HighBits);
108 // Asm Printer needs this info to print 64-bit operands correctly
110 return;
111 }
112 report_fatal_error("Unsupported constant bitwidth");
113}
114
116 MachineIRBuilder &MIRBuilder) {
117 if (!Name.empty()) {
118 auto MIB = MIRBuilder.buildInstr(SPIRV::OpName).addUse(Target);
119 addStringImm(Name, MIB);
120 }
121}
122
124 const SPIRVInstrInfo &TII) {
125 if (!Name.empty()) {
126 auto MIB =
127 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(SPIRV::OpName))
128 .addUse(Target);
129 addStringImm(Name, MIB);
130 }
131}
132
134 const std::vector<uint32_t> &DecArgs,
135 StringRef StrImm) {
136 if (!StrImm.empty())
137 addStringImm(StrImm, MIB);
138 for (const auto &DecArg : DecArgs)
139 MIB.addImm(DecArg);
140}
141
143 SPIRV::Decoration::Decoration Dec,
144 const std::vector<uint32_t> &DecArgs, StringRef StrImm) {
145 auto MIB = MIRBuilder.buildInstr(SPIRV::OpDecorate)
146 .addUse(Reg)
147 .addImm(static_cast<uint32_t>(Dec));
148 finishBuildOpDecorate(MIB, DecArgs, StrImm);
149}
150
152 SPIRV::Decoration::Decoration Dec,
153 const std::vector<uint32_t> &DecArgs, StringRef StrImm) {
154 MachineBasicBlock &MBB = *I.getParent();
155 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(SPIRV::OpDecorate))
156 .addUse(Reg)
157 .addImm(static_cast<uint32_t>(Dec));
158 finishBuildOpDecorate(MIB, DecArgs, StrImm);
159}
160
162 SPIRV::Decoration::Decoration Dec, uint32_t Member,
163 const std::vector<uint32_t> &DecArgs,
164 StringRef StrImm) {
165 auto MIB = MIRBuilder.buildInstr(SPIRV::OpMemberDecorate)
166 .addUse(Reg)
167 .addImm(Member)
168 .addImm(static_cast<uint32_t>(Dec));
169 finishBuildOpDecorate(MIB, DecArgs, StrImm);
170}
171
173 const SPIRVInstrInfo &TII,
174 SPIRV::Decoration::Decoration Dec, uint32_t Member,
175 const std::vector<uint32_t> &DecArgs,
176 StringRef StrImm) {
177 MachineBasicBlock &MBB = *I.getParent();
178 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(SPIRV::OpMemberDecorate))
179 .addUse(Reg)
180 .addImm(Member)
181 .addImm(static_cast<uint32_t>(Dec));
182 finishBuildOpDecorate(MIB, DecArgs, StrImm);
183}
184
186 const MDNode *GVarMD, const SPIRVSubtarget &ST) {
187 for (unsigned I = 0, E = GVarMD->getNumOperands(); I != E; ++I) {
188 auto *OpMD = dyn_cast<MDNode>(GVarMD->getOperand(I));
189 if (!OpMD)
190 report_fatal_error("Invalid decoration");
191 if (OpMD->getNumOperands() == 0)
192 report_fatal_error("Expect operand(s) of the decoration");
193 ConstantInt *DecorationId =
194 mdconst::dyn_extract<ConstantInt>(OpMD->getOperand(0));
195 if (!DecorationId)
196 report_fatal_error("Expect SPIR-V <Decoration> operand to be the first "
197 "element of the decoration");
198
199 // The goal of `spirv.Decorations` metadata is to provide a way to
200 // represent SPIR-V entities that do not map to LLVM in an obvious way.
201 // FP flags do have obvious matches between LLVM IR and SPIR-V.
202 // Additionally, we have no guarantee at this point that the flags passed
203 // through the decoration are not violated already in the optimizer passes.
204 // Therefore, we simply ignore FP flags, including NoContraction, and
205 // FPFastMathMode.
206 if (DecorationId->getZExtValue() ==
207 static_cast<uint32_t>(SPIRV::Decoration::NoContraction) ||
208 DecorationId->getZExtValue() ==
209 static_cast<uint32_t>(SPIRV::Decoration::FPFastMathMode)) {
210 continue; // Ignored.
211 }
212 auto MIB = MIRBuilder.buildInstr(SPIRV::OpDecorate)
213 .addUse(Reg)
214 .addImm(static_cast<uint32_t>(DecorationId->getZExtValue()));
215 for (unsigned OpI = 1, OpE = OpMD->getNumOperands(); OpI != OpE; ++OpI) {
216 if (ConstantInt *OpV =
217 mdconst::dyn_extract<ConstantInt>(OpMD->getOperand(OpI)))
218 MIB.addImm(static_cast<uint32_t>(OpV->getZExtValue()));
219 else if (MDString *OpV = dyn_cast<MDString>(OpMD->getOperand(OpI)))
220 addStringImm(OpV->getString(), MIB);
221 else
222 report_fatal_error("Unexpected operand of the decoration");
223 }
224 }
225}
226
228 MachineFunction *MF = I.getParent()->getParent();
229 MachineBasicBlock *MBB = &MF->front();
230 MachineBasicBlock::iterator It = MBB->SkipPHIsAndLabels(MBB->begin()),
231 E = MBB->end();
232 bool IsHeader = false;
233 unsigned Opcode;
234 for (; It != E && It != I; ++It) {
235 Opcode = It->getOpcode();
236 if (Opcode == SPIRV::OpFunction || Opcode == SPIRV::OpFunctionParameter) {
237 IsHeader = true;
238 } else if (IsHeader &&
239 !(Opcode == SPIRV::ASSIGN_TYPE || Opcode == SPIRV::OpLabel)) {
240 ++It;
241 break;
242 }
243 }
244 return It;
245}
246
249 if (I == MBB->begin())
250 return I;
251 --I;
252 while (I->isTerminator() || I->isDebugValue()) {
253 if (I == MBB->begin())
254 break;
255 --I;
256 }
257 return I;
258}
259
260SPIRV::StorageClass::StorageClass
261addressSpaceToStorageClass(unsigned AddrSpace, const SPIRVSubtarget &STI) {
262 switch (AddrSpace) {
263 case 0:
264 return SPIRV::StorageClass::Function;
265 case 1:
266 return SPIRV::StorageClass::CrossWorkgroup;
267 case 2:
268 return SPIRV::StorageClass::UniformConstant;
269 case 3:
270 return SPIRV::StorageClass::Workgroup;
271 case 4:
272 return SPIRV::StorageClass::Generic;
273 case 5:
274 return STI.canUseExtension(SPIRV::Extension::SPV_INTEL_usm_storage_classes)
275 ? SPIRV::StorageClass::DeviceOnlyINTEL
276 : SPIRV::StorageClass::CrossWorkgroup;
277 case 6:
278 return STI.canUseExtension(SPIRV::Extension::SPV_INTEL_usm_storage_classes)
279 ? SPIRV::StorageClass::HostOnlyINTEL
280 : SPIRV::StorageClass::CrossWorkgroup;
281 case 7:
282 return SPIRV::StorageClass::Input;
283 case 8:
284 return SPIRV::StorageClass::Output;
285 case 9:
286 return SPIRV::StorageClass::CodeSectionINTEL;
287 case 10:
288 return SPIRV::StorageClass::Private;
289 case 11:
290 return SPIRV::StorageClass::StorageBuffer;
291 case 12:
292 return SPIRV::StorageClass::Uniform;
293 default:
294 report_fatal_error("Unknown address space");
295 }
296}
297
298SPIRV::MemorySemantics::MemorySemantics
299getMemSemanticsForStorageClass(SPIRV::StorageClass::StorageClass SC) {
300 switch (SC) {
301 case SPIRV::StorageClass::StorageBuffer:
302 case SPIRV::StorageClass::Uniform:
303 return SPIRV::MemorySemantics::UniformMemory;
304 case SPIRV::StorageClass::Workgroup:
305 return SPIRV::MemorySemantics::WorkgroupMemory;
306 case SPIRV::StorageClass::CrossWorkgroup:
307 return SPIRV::MemorySemantics::CrossWorkgroupMemory;
308 case SPIRV::StorageClass::AtomicCounter:
309 return SPIRV::MemorySemantics::AtomicCounterMemory;
310 case SPIRV::StorageClass::Image:
311 return SPIRV::MemorySemantics::ImageMemory;
312 default:
313 return SPIRV::MemorySemantics::None;
314 }
315}
316
317SPIRV::MemorySemantics::MemorySemantics getMemSemantics(AtomicOrdering Ord) {
318 switch (Ord) {
320 return SPIRV::MemorySemantics::Acquire;
322 return SPIRV::MemorySemantics::Release;
324 return SPIRV::MemorySemantics::AcquireRelease;
326 return SPIRV::MemorySemantics::SequentiallyConsistent;
330 return SPIRV::MemorySemantics::None;
331 }
332 llvm_unreachable(nullptr);
333}
334
335SPIRV::Scope::Scope getMemScope(LLVMContext &Ctx, SyncScope::ID Id) {
336 // Named by
337 // https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#_scope_id.
338 // We don't need aliases for Invocation and CrossDevice, as we already have
339 // them covered by "singlethread" and "" strings respectively (see
340 // implementation of LLVMContext::LLVMContext()).
341 static const llvm::SyncScope::ID SubGroup =
342 Ctx.getOrInsertSyncScopeID("subgroup");
343 static const llvm::SyncScope::ID WorkGroup =
344 Ctx.getOrInsertSyncScopeID("workgroup");
345 static const llvm::SyncScope::ID Device =
346 Ctx.getOrInsertSyncScopeID("device");
347
349 return SPIRV::Scope::Invocation;
350 else if (Id == llvm::SyncScope::System)
351 return SPIRV::Scope::CrossDevice;
352 else if (Id == SubGroup)
353 return SPIRV::Scope::Subgroup;
354 else if (Id == WorkGroup)
355 return SPIRV::Scope::Workgroup;
356 else if (Id == Device)
357 return SPIRV::Scope::Device;
358 return SPIRV::Scope::CrossDevice;
359}
360
362 const MachineRegisterInfo *MRI) {
363 MachineInstr *MI = MRI->getVRegDef(ConstReg);
364 MachineInstr *ConstInstr =
365 MI->getOpcode() == SPIRV::G_TRUNC || MI->getOpcode() == SPIRV::G_ZEXT
366 ? MRI->getVRegDef(MI->getOperand(1).getReg())
367 : MI;
368 if (auto *GI = dyn_cast<GIntrinsic>(ConstInstr)) {
369 if (GI->is(Intrinsic::spv_track_constant)) {
370 ConstReg = ConstInstr->getOperand(2).getReg();
371 return MRI->getVRegDef(ConstReg);
372 }
373 } else if (ConstInstr->getOpcode() == SPIRV::ASSIGN_TYPE) {
374 ConstReg = ConstInstr->getOperand(1).getReg();
375 return MRI->getVRegDef(ConstReg);
376 } else if (ConstInstr->getOpcode() == TargetOpcode::G_CONSTANT ||
377 ConstInstr->getOpcode() == TargetOpcode::G_FCONSTANT) {
378 ConstReg = ConstInstr->getOperand(0).getReg();
379 return ConstInstr;
380 }
381 return MRI->getVRegDef(ConstReg);
382}
383
385 const MachineInstr *MI = getDefInstrMaybeConstant(ConstReg, MRI);
386 assert(MI && MI->getOpcode() == TargetOpcode::G_CONSTANT);
387 return MI->getOperand(1).getCImm()->getValue().getZExtValue();
388}
389
391 const MachineInstr *MI = getDefInstrMaybeConstant(ConstReg, MRI);
392 assert(MI && MI->getOpcode() == TargetOpcode::G_CONSTANT);
393 return MI->getOperand(1).getCImm()->getSExtValue();
394}
395
396bool isSpvIntrinsic(const MachineInstr &MI, Intrinsic::ID IntrinsicID) {
397 if (const auto *GI = dyn_cast<GIntrinsic>(&MI))
398 return GI->is(IntrinsicID);
399 return false;
400}
401
402Type *getMDOperandAsType(const MDNode *N, unsigned I) {
403 Type *ElementTy = cast<ValueAsMetadata>(N->getOperand(I))->getType();
404 return toTypedPointer(ElementTy);
405}
406
407// The set of names is borrowed from the SPIR-V translator.
408// TODO: may be implemented in SPIRVBuiltins.td.
409static bool isPipeOrAddressSpaceCastBI(const StringRef MangledName) {
410 return MangledName == "write_pipe_2" || MangledName == "read_pipe_2" ||
411 MangledName == "write_pipe_2_bl" || MangledName == "read_pipe_2_bl" ||
412 MangledName == "write_pipe_4" || MangledName == "read_pipe_4" ||
413 MangledName == "reserve_write_pipe" ||
414 MangledName == "reserve_read_pipe" ||
415 MangledName == "commit_write_pipe" ||
416 MangledName == "commit_read_pipe" ||
417 MangledName == "work_group_reserve_write_pipe" ||
418 MangledName == "work_group_reserve_read_pipe" ||
419 MangledName == "work_group_commit_write_pipe" ||
420 MangledName == "work_group_commit_read_pipe" ||
421 MangledName == "get_pipe_num_packets_ro" ||
422 MangledName == "get_pipe_max_packets_ro" ||
423 MangledName == "get_pipe_num_packets_wo" ||
424 MangledName == "get_pipe_max_packets_wo" ||
425 MangledName == "sub_group_reserve_write_pipe" ||
426 MangledName == "sub_group_reserve_read_pipe" ||
427 MangledName == "sub_group_commit_write_pipe" ||
428 MangledName == "sub_group_commit_read_pipe" ||
429 MangledName == "to_global" || MangledName == "to_local" ||
430 MangledName == "to_private";
431}
432
433static bool isEnqueueKernelBI(const StringRef MangledName) {
434 return MangledName == "__enqueue_kernel_basic" ||
435 MangledName == "__enqueue_kernel_basic_events" ||
436 MangledName == "__enqueue_kernel_varargs" ||
437 MangledName == "__enqueue_kernel_events_varargs";
438}
439
440static bool isKernelQueryBI(const StringRef MangledName) {
441 return MangledName == "__get_kernel_work_group_size_impl" ||
442 MangledName == "__get_kernel_sub_group_count_for_ndrange_impl" ||
443 MangledName == "__get_kernel_max_sub_group_size_for_ndrange_impl" ||
444 MangledName == "__get_kernel_preferred_work_group_size_multiple_impl";
445}
446
448 if (!Name.starts_with("__"))
449 return false;
450
451 return isEnqueueKernelBI(Name) || isKernelQueryBI(Name) ||
452 isPipeOrAddressSpaceCastBI(Name.drop_front(2)) ||
453 Name == "__translate_sampler_initializer";
454}
455
457 bool IsNonMangledOCL = isNonMangledOCLBuiltin(Name);
458 bool IsNonMangledSPIRV = Name.starts_with("__spirv_");
459 bool IsNonMangledHLSL = Name.starts_with("__hlsl_");
460 bool IsMangled = Name.starts_with("_Z");
461
462 // Otherwise use simple demangling to return the function name.
463 if (IsNonMangledOCL || IsNonMangledSPIRV || IsNonMangledHLSL || !IsMangled)
464 return Name.str();
465
466 // Try to use the itanium demangler.
467 if (char *DemangledName = itaniumDemangle(Name.data())) {
468 std::string Result = DemangledName;
469 free(DemangledName);
470 return Result;
471 }
472
473 // Autocheck C++, maybe need to do explicit check of the source language.
474 // OpenCL C++ built-ins are declared in cl namespace.
475 // TODO: consider using 'St' abbriviation for cl namespace mangling.
476 // Similar to ::std:: in C++.
477 size_t Start, Len = 0;
478 size_t DemangledNameLenStart = 2;
479 if (Name.starts_with("_ZN")) {
480 // Skip CV and ref qualifiers.
481 size_t NameSpaceStart = Name.find_first_not_of("rVKRO", 3);
482 // All built-ins are in the ::cl:: namespace.
483 if (Name.substr(NameSpaceStart, 11) != "2cl7__spirv")
484 return std::string();
485 DemangledNameLenStart = NameSpaceStart + 11;
486 }
487 Start = Name.find_first_not_of("0123456789", DemangledNameLenStart);
488 [[maybe_unused]] bool Error =
489 Name.substr(DemangledNameLenStart, Start - DemangledNameLenStart)
490 .getAsInteger(10, Len);
491 assert(!Error && "Failed to parse demangled name length");
492 return Name.substr(Start, Len).str();
493}
494
496 if (Name.starts_with("opencl.") || Name.starts_with("ocl_") ||
497 Name.starts_with("spirv."))
498 return true;
499 return false;
500}
501
502bool isSpecialOpaqueType(const Type *Ty) {
503 if (const TargetExtType *ExtTy = dyn_cast<TargetExtType>(Ty))
504 return isTypedPointerWrapper(ExtTy)
505 ? false
506 : hasBuiltinTypePrefix(ExtTy->getName());
507
508 return false;
509}
510
511bool isEntryPoint(const Function &F) {
512 // OpenCL handling: any function with the SPIR_KERNEL
513 // calling convention will be a potential entry point.
514 if (F.getCallingConv() == CallingConv::SPIR_KERNEL)
515 return true;
516
517 // HLSL handling: special attribute are emitted from the
518 // front-end.
519 if (F.getFnAttribute("hlsl.shader").isValid())
520 return true;
521
522 return false;
523}
524
526 TypeName.consume_front("atomic_");
527 if (TypeName.consume_front("void"))
528 return Type::getVoidTy(Ctx);
529 else if (TypeName.consume_front("bool") || TypeName.consume_front("_Bool"))
530 return Type::getIntNTy(Ctx, 1);
531 else if (TypeName.consume_front("char") ||
532 TypeName.consume_front("signed char") ||
533 TypeName.consume_front("unsigned char") ||
534 TypeName.consume_front("uchar"))
535 return Type::getInt8Ty(Ctx);
536 else if (TypeName.consume_front("short") ||
537 TypeName.consume_front("signed short") ||
538 TypeName.consume_front("unsigned short") ||
539 TypeName.consume_front("ushort"))
540 return Type::getInt16Ty(Ctx);
541 else if (TypeName.consume_front("int") ||
542 TypeName.consume_front("signed int") ||
543 TypeName.consume_front("unsigned int") ||
544 TypeName.consume_front("uint"))
545 return Type::getInt32Ty(Ctx);
546 else if (TypeName.consume_front("long") ||
547 TypeName.consume_front("signed long") ||
548 TypeName.consume_front("unsigned long") ||
549 TypeName.consume_front("ulong"))
550 return Type::getInt64Ty(Ctx);
551 else if (TypeName.consume_front("half") ||
552 TypeName.consume_front("_Float16") ||
553 TypeName.consume_front("__fp16"))
554 return Type::getHalfTy(Ctx);
555 else if (TypeName.consume_front("float"))
556 return Type::getFloatTy(Ctx);
557 else if (TypeName.consume_front("double"))
558 return Type::getDoubleTy(Ctx);
559
560 // Unable to recognize SPIRV type name
561 return nullptr;
562}
563
564std::unordered_set<BasicBlock *>
565PartialOrderingVisitor::getReachableFrom(BasicBlock *Start) {
566 std::queue<BasicBlock *> ToVisit;
567 ToVisit.push(Start);
568
569 std::unordered_set<BasicBlock *> Output;
570 while (ToVisit.size() != 0) {
571 BasicBlock *BB = ToVisit.front();
572 ToVisit.pop();
573
574 if (Output.count(BB) != 0)
575 continue;
576 Output.insert(BB);
577
578 for (BasicBlock *Successor : successors(BB)) {
579 if (DT.dominates(Successor, BB))
580 continue;
581 ToVisit.push(Successor);
582 }
583 }
584
585 return Output;
586}
587
588bool PartialOrderingVisitor::CanBeVisited(BasicBlock *BB) const {
589 for (BasicBlock *P : predecessors(BB)) {
590 // Ignore back-edges.
591 if (DT.dominates(BB, P))
592 continue;
593
594 // One of the predecessor hasn't been visited. Not ready yet.
595 if (BlockToOrder.count(P) == 0)
596 return false;
597
598 // If the block is a loop exit, the loop must be finished before
599 // we can continue.
600 Loop *L = LI.getLoopFor(P);
601 if (L == nullptr || L->contains(BB))
602 continue;
603
604 // SPIR-V requires a single back-edge. And the backend first
605 // step transforms loops into the simplified format. If we have
606 // more than 1 back-edge, something is wrong.
607 assert(L->getNumBackEdges() <= 1);
608
609 // If the loop has no latch, loop's rank won't matter, so we can
610 // proceed.
611 BasicBlock *Latch = L->getLoopLatch();
612 assert(Latch);
613 if (Latch == nullptr)
614 continue;
615
616 // The latch is not ready yet, let's wait.
617 if (BlockToOrder.count(Latch) == 0)
618 return false;
619 }
620
621 return true;
622}
623
625 auto It = BlockToOrder.find(BB);
626 if (It != BlockToOrder.end())
627 return It->second.Rank;
628
629 size_t result = 0;
630 for (BasicBlock *P : predecessors(BB)) {
631 // Ignore back-edges.
632 if (DT.dominates(BB, P))
633 continue;
634
635 auto Iterator = BlockToOrder.end();
636 Loop *L = LI.getLoopFor(P);
637 BasicBlock *Latch = L ? L->getLoopLatch() : nullptr;
638
639 // If the predecessor is either outside a loop, or part of
640 // the same loop, simply take its rank + 1.
641 if (L == nullptr || L->contains(BB) || Latch == nullptr) {
642 Iterator = BlockToOrder.find(P);
643 } else {
644 // Otherwise, take the loop's rank (highest rank in the loop) as base.
645 // Since loops have a single latch, highest rank is easy to find.
646 // If the loop has no latch, then it doesn't matter.
647 Iterator = BlockToOrder.find(Latch);
648 }
649
650 assert(Iterator != BlockToOrder.end());
651 result = std::max(result, Iterator->second.Rank + 1);
652 }
653
654 return result;
655}
656
657size_t PartialOrderingVisitor::visit(BasicBlock *BB, size_t Unused) {
658 ToVisit.push(BB);
659 Queued.insert(BB);
660
661 size_t QueueIndex = 0;
662 while (ToVisit.size() != 0) {
663 BasicBlock *BB = ToVisit.front();
664 ToVisit.pop();
665
666 if (!CanBeVisited(BB)) {
667 ToVisit.push(BB);
668 if (QueueIndex >= ToVisit.size())
670 "No valid candidate in the queue. Is the graph reducible?");
671 QueueIndex++;
672 continue;
673 }
674
675 QueueIndex = 0;
676 size_t Rank = GetNodeRank(BB);
677 OrderInfo Info = {Rank, BlockToOrder.size()};
678 BlockToOrder.emplace(BB, Info);
679
680 for (BasicBlock *S : successors(BB)) {
681 if (Queued.count(S) != 0)
682 continue;
683 ToVisit.push(S);
684 Queued.insert(S);
685 }
686 }
687
688 return 0;
689}
690
692 DT.recalculate(F);
693 LI = LoopInfo(DT);
694
695 visit(&*F.begin(), 0);
696
697 Order.reserve(F.size());
698 for (auto &[BB, Info] : BlockToOrder)
699 Order.emplace_back(BB);
700
701 std::sort(Order.begin(), Order.end(), [&](const auto &LHS, const auto &RHS) {
702 return compare(LHS, RHS);
703 });
704}
705
707 const BasicBlock *RHS) const {
708 const OrderInfo &InfoLHS = BlockToOrder.at(const_cast<BasicBlock *>(LHS));
709 const OrderInfo &InfoRHS = BlockToOrder.at(const_cast<BasicBlock *>(RHS));
710 if (InfoLHS.Rank != InfoRHS.Rank)
711 return InfoLHS.Rank < InfoRHS.Rank;
712 return InfoLHS.TraversalIndex < InfoRHS.TraversalIndex;
713}
714
716 BasicBlock &Start, std::function<bool(BasicBlock *)> Op) {
717 std::unordered_set<BasicBlock *> Reachable = getReachableFrom(&Start);
718 assert(BlockToOrder.count(&Start) != 0);
719
720 // Skipping blocks with a rank inferior to |Start|'s rank.
721 auto It = Order.begin();
722 while (It != Order.end() && *It != &Start)
723 ++It;
724
725 // This is unexpected. Worst case |Start| is the last block,
726 // so It should point to the last block, not past-end.
727 assert(It != Order.end());
728
729 // By default, there is no rank limit. Setting it to the maximum value.
730 std::optional<size_t> EndRank = std::nullopt;
731 for (; It != Order.end(); ++It) {
732 if (EndRank.has_value() && BlockToOrder[*It].Rank > *EndRank)
733 break;
734
735 if (Reachable.count(*It) == 0) {
736 continue;
737 }
738
739 if (!Op(*It)) {
740 EndRank = BlockToOrder[*It].Rank;
741 }
742 }
743}
744
746 if (F.size() == 0)
747 return false;
748
749 bool Modified = false;
750 std::vector<BasicBlock *> Order;
751 Order.reserve(F.size());
752
754 llvm::append_range(Order, RPOT);
755
756 assert(&*F.begin() == Order[0]);
757 BasicBlock *LastBlock = &*F.begin();
758 for (BasicBlock *BB : Order) {
759 if (BB != LastBlock && &*LastBlock->getNextNode() != BB) {
760 Modified = true;
761 BB->moveAfter(LastBlock);
762 }
763 LastBlock = BB;
764 }
765
766 return Modified;
767}
768
770 MachineInstr *MaybeDef = MRI.getVRegDef(Reg);
771 if (MaybeDef && MaybeDef->getOpcode() == SPIRV::ASSIGN_TYPE)
772 MaybeDef = MRI.getVRegDef(MaybeDef->getOperand(1).getReg());
773 return MaybeDef;
774}
775
776bool getVacantFunctionName(Module &M, std::string &Name) {
777 // It's a bit of paranoia, but still we don't want to have even a chance that
778 // the loop will work for too long.
779 constexpr unsigned MaxIters = 1024;
780 for (unsigned I = 0; I < MaxIters; ++I) {
781 std::string OrdName = Name + Twine(I).str();
782 if (!M.getFunction(OrdName)) {
783 Name = std::move(OrdName);
784 return true;
785 }
786 }
787 return false;
788}
789
790// Assign SPIR-V type to the register. If the register has no valid assigned
791// class, set register LLT type and class according to the SPIR-V type.
794 bool Force) {
795 GR->assignSPIRVTypeToVReg(SpvType, Reg, MF);
796 if (!MRI->getRegClassOrNull(Reg) || Force) {
797 MRI->setRegClass(Reg, GR->getRegClass(SpvType));
798 MRI->setType(Reg, GR->getRegType(SpvType));
799 }
800}
801
802// Create a SPIR-V type, assign SPIR-V type to the register. If the register has
803// no valid assigned class, set register LLT type and class according to the
804// SPIR-V type.
806 MachineIRBuilder &MIRBuilder,
807 SPIRV::AccessQualifier::AccessQualifier AccessQual,
808 bool EmitIR, bool Force) {
810 GR->getOrCreateSPIRVType(Ty, MIRBuilder, AccessQual, EmitIR),
811 GR, MIRBuilder.getMRI(), MIRBuilder.getMF(), Force);
812}
813
814// Create a virtual register and assign SPIR-V type to the register. Set
815// register LLT type and class according to the SPIR-V type.
818 const MachineFunction &MF) {
819 Register Reg = MRI->createVirtualRegister(GR->getRegClass(SpvType));
820 MRI->setType(Reg, GR->getRegType(SpvType));
821 GR->assignSPIRVTypeToVReg(SpvType, Reg, MF);
822 return Reg;
823}
824
825// Create a virtual register and assign SPIR-V type to the register. Set
826// register LLT type and class according to the SPIR-V type.
828 MachineIRBuilder &MIRBuilder) {
829 return createVirtualRegister(SpvType, GR, MIRBuilder.getMRI(),
830 MIRBuilder.getMF());
831}
832
833// Create a SPIR-V type, virtual register and assign SPIR-V type to the
834// register. Set register LLT type and class according to the SPIR-V type.
836 const Type *Ty, SPIRVGlobalRegistry *GR, MachineIRBuilder &MIRBuilder,
837 SPIRV::AccessQualifier::AccessQualifier AccessQual, bool EmitIR) {
839 GR->getOrCreateSPIRVType(Ty, MIRBuilder, AccessQual, EmitIR), GR,
840 MIRBuilder);
841}
842
844 Value *Arg, Value *Arg2, ArrayRef<Constant *> Imms,
845 IRBuilder<> &B) {
847 Args.push_back(Arg2);
848 Args.push_back(buildMD(Arg));
849 llvm::append_range(Args, Imms);
850 return B.CreateIntrinsic(IntrID, {Types}, Args);
851}
852
853// Return true if there is an opaque pointer type nested in the argument.
854bool isNestedPointer(const Type *Ty) {
855 if (Ty->isPtrOrPtrVectorTy())
856 return true;
857 if (const FunctionType *RefTy = dyn_cast<FunctionType>(Ty)) {
858 if (isNestedPointer(RefTy->getReturnType()))
859 return true;
860 for (const Type *ArgTy : RefTy->params())
861 if (isNestedPointer(ArgTy))
862 return true;
863 return false;
864 }
865 if (const ArrayType *RefTy = dyn_cast<ArrayType>(Ty))
866 return isNestedPointer(RefTy->getElementType());
867 return false;
868}
869
870bool isSpvIntrinsic(const Value *Arg) {
871 if (const auto *II = dyn_cast<IntrinsicInst>(Arg))
872 if (Function *F = II->getCalledFunction())
873 if (F->getName().starts_with("llvm.spv."))
874 return true;
875 return false;
876}
877
878// Function to create continued instructions for SPV_INTEL_long_composites
879// extension
880SmallVector<MachineInstr *, 4>
882 unsigned MinWC, unsigned ContinuedOpcode,
883 ArrayRef<Register> Args, Register ReturnRegister,
885
887 constexpr unsigned MaxWordCount = UINT16_MAX;
888 const size_t NumElements = Args.size();
889 size_t MaxNumElements = MaxWordCount - MinWC;
890 size_t SPIRVStructNumElements = NumElements;
891
892 if (NumElements > MaxNumElements) {
893 // Do adjustments for continued instructions which always had only one
894 // minumum word count.
895 SPIRVStructNumElements = MaxNumElements;
896 MaxNumElements = MaxWordCount - 1;
897 }
898
899 auto MIB =
900 MIRBuilder.buildInstr(Opcode).addDef(ReturnRegister).addUse(TypeID);
901
902 for (size_t I = 0; I < SPIRVStructNumElements; ++I)
903 MIB.addUse(Args[I]);
904
905 Instructions.push_back(MIB.getInstr());
906
907 for (size_t I = SPIRVStructNumElements; I < NumElements;
908 I += MaxNumElements) {
909 auto MIB = MIRBuilder.buildInstr(ContinuedOpcode);
910 for (size_t J = I; J < std::min(I + MaxNumElements, NumElements); ++J)
911 MIB.addUse(Args[J]);
912 Instructions.push_back(MIB.getInstr());
913 }
914 return Instructions;
915}
916
918 unsigned LC = SPIRV::LoopControl::None;
919 // Currently used only to store PartialCount value. Later when other
920 // LoopControls are added - this map should be sorted before making
921 // them loop_merge operands to satisfy 3.23. Loop Control requirements.
922 std::vector<std::pair<unsigned, unsigned>> MaskToValueMap;
923 if (getBooleanLoopAttribute(L, "llvm.loop.unroll.disable")) {
924 LC |= SPIRV::LoopControl::DontUnroll;
925 } else {
926 if (getBooleanLoopAttribute(L, "llvm.loop.unroll.enable") ||
927 getBooleanLoopAttribute(L, "llvm.loop.unroll.full")) {
928 LC |= SPIRV::LoopControl::Unroll;
929 }
930 std::optional<int> Count =
931 getOptionalIntLoopAttribute(L, "llvm.loop.unroll.count");
932 if (Count && Count != 1) {
933 LC |= SPIRV::LoopControl::PartialCount;
934 MaskToValueMap.emplace_back(
935 std::make_pair(SPIRV::LoopControl::PartialCount, *Count));
936 }
937 }
938 SmallVector<unsigned, 1> Result = {LC};
939 for (auto &[Mask, Val] : MaskToValueMap)
940 Result.push_back(Val);
941 return Result;
942}
943
944const std::set<unsigned> &getTypeFoldingSupportedOpcodes() {
945 // clang-format off
946 static const std::set<unsigned> TypeFoldingSupportingOpcs = {
947 TargetOpcode::G_ADD,
948 TargetOpcode::G_FADD,
949 TargetOpcode::G_STRICT_FADD,
950 TargetOpcode::G_SUB,
951 TargetOpcode::G_FSUB,
952 TargetOpcode::G_STRICT_FSUB,
953 TargetOpcode::G_MUL,
954 TargetOpcode::G_FMUL,
955 TargetOpcode::G_STRICT_FMUL,
956 TargetOpcode::G_SDIV,
957 TargetOpcode::G_UDIV,
958 TargetOpcode::G_FDIV,
959 TargetOpcode::G_STRICT_FDIV,
960 TargetOpcode::G_SREM,
961 TargetOpcode::G_UREM,
962 TargetOpcode::G_FREM,
963 TargetOpcode::G_STRICT_FREM,
964 TargetOpcode::G_FNEG,
965 TargetOpcode::G_CONSTANT,
966 TargetOpcode::G_FCONSTANT,
967 TargetOpcode::G_AND,
968 TargetOpcode::G_OR,
969 TargetOpcode::G_XOR,
970 TargetOpcode::G_SHL,
971 TargetOpcode::G_ASHR,
972 TargetOpcode::G_LSHR,
973 TargetOpcode::G_SELECT,
974 TargetOpcode::G_EXTRACT_VECTOR_ELT,
975 };
976 // clang-format on
977 return TypeFoldingSupportingOpcs;
978}
979
980bool isTypeFoldingSupported(unsigned Opcode) {
981 return getTypeFoldingSupportedOpcodes().count(Opcode) > 0;
982}
983
984// Traversing [g]MIR accounting for pseudo-instructions.
986 return (Def->getOpcode() == SPIRV::ASSIGN_TYPE ||
987 Def->getOpcode() == TargetOpcode::COPY)
988 ? MRI->getVRegDef(Def->getOperand(1).getReg())
989 : Def;
990}
991
993 if (MachineInstr *Def = MRI->getVRegDef(MO.getReg()))
994 return passCopy(Def, MRI);
995 return nullptr;
996}
997
999 if (MachineInstr *Def = getDef(MO, MRI)) {
1000 if (Def->getOpcode() == TargetOpcode::G_CONSTANT ||
1001 Def->getOpcode() == SPIRV::OpConstantI)
1002 return Def;
1003 }
1004 return nullptr;
1005}
1006
1007int64_t foldImm(const MachineOperand &MO, const MachineRegisterInfo *MRI) {
1008 if (MachineInstr *Def = getImm(MO, MRI)) {
1009 if (Def->getOpcode() == SPIRV::OpConstantI)
1010 return Def->getOperand(2).getImm();
1011 if (Def->getOpcode() == TargetOpcode::G_CONSTANT)
1012 return Def->getOperand(1).getCImm()->getZExtValue();
1013 }
1014 llvm_unreachable("Unexpected integer constant pattern");
1015}
1016
1018 const MachineInstr *ResType) {
1019 return foldImm(ResType->getOperand(2), MRI);
1020}
1021
1024 // Find the position to insert the OpVariable instruction.
1025 // We will insert it after the last OpFunctionParameter, if any, or
1026 // after OpFunction otherwise.
1027 MachineBasicBlock::iterator VarPos = BB.begin();
1028 while (VarPos != BB.end() && VarPos->getOpcode() != SPIRV::OpFunction) {
1029 ++VarPos;
1030 }
1031 // Advance VarPos to the next instruction after OpFunction, it will either
1032 // be an OpFunctionParameter, so that we can start the next loop, or the
1033 // position to insert the OpVariable instruction.
1034 ++VarPos;
1035 while (VarPos != BB.end() &&
1036 VarPos->getOpcode() == SPIRV::OpFunctionParameter) {
1037 ++VarPos;
1038 }
1039 // VarPos is now pointing at after the last OpFunctionParameter, if any,
1040 // or after OpFunction, if no parameters.
1041 return VarPos != BB.end() && VarPos->getOpcode() == SPIRV::OpLabel ? ++VarPos
1042 : VarPos;
1043}
1044
1045std::optional<SPIRV::LinkageType::LinkageType>
1047 if (GV.hasLocalLinkage() || GV.hasHiddenVisibility())
1048 return std::nullopt;
1049
1050 if (GV.isDeclarationForLinker())
1051 return SPIRV::LinkageType::Import;
1052
1053 if (GV.hasLinkOnceODRLinkage() &&
1054 ST.canUseExtension(SPIRV::Extension::SPV_KHR_linkonce_odr))
1055 return SPIRV::LinkageType::LinkOnceODR;
1056
1057 return SPIRV::LinkageType::Export;
1058}
1059
1060} // namespace llvm
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Analysis containing CSE Info
Definition CSEInfo.cpp:27
Declares convenience wrapper classes for interpreting MachineInstr instances as specific generic oper...
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
This file declares the MachineIRBuilder class.
Register Reg
Type::TypeID TypeID
uint64_t IntrinsicInst * II
#define P(N)
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition APInt.h:78
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
Class to represent array types.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
LLVM_ABI void moveAfter(BasicBlock *MovePos)
Unlink this basic block from its current function and insert it right after MovePos in the function M...
const Instruction & front() const
Definition BasicBlock.h:482
This class represents a function call, abstracting a target machine's calling convention.
An array constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition Constants.h:702
StringRef getAsCString() const
If this array is isCString(), then this method returns the array (without the trailing null byte) as ...
Definition Constants.h:675
This is the shared class of boolean and integer constants.
Definition Constants.h:87
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:163
bool dominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
dominates - Returns true iff A dominates B.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Class to represent function types.
bool hasLocalLinkage() const
bool hasHiddenVisibility() const
bool isDeclarationForLinker() const
bool hasLinkOnceODRLinkage() const
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2788
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
void addOperand(const MCOperand Op)
Definition MCInst.h:215
static MCOperand createImm(int64_t Val)
Definition MCInst.h:145
Metadata node.
Definition Metadata.h:1078
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1442
unsigned getNumOperands() const
Return number of MDNode operands.
Definition Metadata.h:1448
A single uniqued string.
Definition Metadata.h:721
MachineInstrBundleIterator< MachineInstr > iterator
const MachineBasicBlock & front() const
Helper class to build MachineInstr.
MachineInstrBuilder buildInstr(unsigned Opcode)
Build and insert <empty> = Opcode <empty>.
MachineFunction & getMF()
Getter for the function we currently build.
MachineRegisterInfo * getMRI()
Getter for MRI.
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.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
void setAsmPrinterFlag(uint8_t Flag)
Set a flag for the AsmPrinter.
const MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
size_t GetNodeRank(BasicBlock *BB) const
void partialOrderVisit(BasicBlock &Start, std::function< bool(BasicBlock *)> Op)
bool compare(const BasicBlock *LHS, const BasicBlock *RHS) const
Wrapper class representing virtual and physical registers.
Definition Register.h:19
void assignSPIRVTypeToVReg(SPIRVType *Type, Register VReg, const MachineFunction &MF)
SPIRVType * getOrCreateSPIRVType(const Type *Type, MachineInstr &I, SPIRV::AccessQualifier::AccessQualifier AQ, bool EmitIR)
const TargetRegisterClass * getRegClass(SPIRVType *SpvType) const
LLT getRegType(SPIRVType *SpvType) const
bool canUseExtension(SPIRV::Extension::Extension E) const
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
std::string str() const
str - Get the contents as an std::string.
Definition StringRef.h:225
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:143
Class to represent target extensions types, which are generally unintrospectable from target-independ...
Target - Wrapper for Target specific information.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM_ABI std::string str() const
Return the twine contents as a std::string.
Definition Twine.cpp:17
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
static LLVM_ABI IntegerType * getInt64Ty(LLVMContext &C)
Definition Type.cpp:298
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:297
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:281
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:295
static LLVM_ABI IntegerType * getInt16Ty(LLVMContext &C)
Definition Type.cpp:296
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:301
static LLVM_ABI Type * getDoubleTy(LLVMContext &C)
Definition Type.cpp:286
static LLVM_ABI Type * getFloatTy(LLVMContext &C)
Definition Type.cpp:285
static LLVM_ABI Type * getHalfTy(LLVMContext &C)
Definition Type.cpp:283
Value * getOperand(unsigned i) const
Definition User.h:232
LLVM Value Representation.
Definition Value.h:75
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition ilist_node.h:348
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ SPIR_KERNEL
Used for SPIR kernel functions.
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
@ SingleThread
Synchronized with respect to signal handlers executing in the same thread.
Definition LLVMContext.h:55
@ System
Synchronized with respect to all concurrently executing threads.
Definition LLVMContext.h:58
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > dyn_extract(Y &&MD)
Extract a Value from Metadata, if any.
Definition Metadata.h:695
This is an optimization pass for GlobalISel generic memory operations.
void buildOpName(Register Target, const StringRef &Name, MachineIRBuilder &MIRBuilder)
bool getVacantFunctionName(Module &M, std::string &Name)
std::string getStringImm(const MachineInstr &MI, unsigned StartIndex)
LLVM_ABI bool getBooleanLoopAttribute(const Loop *TheLoop, StringRef Name)
Returns true if Name is applied to TheLoop and enabled.
int64_t getIConstValSext(Register ConstReg, const MachineRegisterInfo *MRI)
bool isTypedPointerWrapper(const TargetExtType *ExtTy)
Definition SPIRVUtils.h:381
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
static void finishBuildOpDecorate(MachineInstrBuilder &MIB, const std::vector< uint32_t > &DecArgs, StringRef StrImm)
bool isTypeFoldingSupported(unsigned Opcode)
static uint32_t convertCharsToWord(const StringRef &Str, unsigned i)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
MachineInstr * getDef(const MachineOperand &MO, const MachineRegisterInfo *MRI)
void addNumImm(const APInt &Imm, MachineInstrBuilder &MIB)
auto successors(const MachineBasicBlock *BB)
CallInst * buildIntrWithMD(Intrinsic::ID IntrID, ArrayRef< Type * > Types, Value *Arg, Value *Arg2, ArrayRef< Constant * > Imms, IRBuilder<> &B)
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2136
unsigned getArrayComponentCount(const MachineRegisterInfo *MRI, const MachineInstr *ResType)
bool sortBlocks(Function &F)
SmallVector< unsigned, 1 > getSpirvLoopControlOperandsFromLoopMetadata(Loop *L)
uint64_t getIConstVal(Register ConstReg, const MachineRegisterInfo *MRI)
SmallVector< MachineInstr *, 4 > createContinuedInstructions(MachineIRBuilder &MIRBuilder, unsigned Opcode, unsigned MinWC, unsigned ContinuedOpcode, ArrayRef< Register > Args, Register ReturnRegister, Register TypeID)
SPIRV::MemorySemantics::MemorySemantics getMemSemanticsForStorageClass(SPIRV::StorageClass::StorageClass SC)
MachineBasicBlock::iterator getFirstValidInstructionInsertPoint(MachineBasicBlock &BB)
bool isNestedPointer(const Type *Ty)
MetadataAsValue * buildMD(Value *Arg)
Definition SPIRVUtils.h:491
std::string getOclOrSpirvBuiltinDemangledName(StringRef Name)
void buildOpDecorate(Register Reg, MachineIRBuilder &MIRBuilder, SPIRV::Decoration::Decoration Dec, const std::vector< uint32_t > &DecArgs, StringRef StrImm)
MachineBasicBlock::iterator getOpVariableMBBIt(MachineInstr &I)
Register createVirtualRegister(SPIRVType *SpvType, SPIRVGlobalRegistry *GR, MachineRegisterInfo *MRI, const MachineFunction &MF)
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
std::string getSPIRVStringOperand(const InstType &MI, unsigned StartIndex)
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:436
DEMANGLE_ABI char * itaniumDemangle(std::string_view mangled_name, bool ParseParams=true)
Returns a non-NULL pointer to a NUL-terminated C style string that should be explicitly freed,...
bool isSpecialOpaqueType(const Type *Ty)
void setRegClassType(Register Reg, SPIRVType *SpvType, SPIRVGlobalRegistry *GR, MachineRegisterInfo *MRI, const MachineFunction &MF, bool Force)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
MachineBasicBlock::iterator getInsertPtValidEnd(MachineBasicBlock *MBB)
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
const MachineInstr SPIRVType
static bool isNonMangledOCLBuiltin(StringRef Name)
MachineInstr * passCopy(MachineInstr *Def, const MachineRegisterInfo *MRI)
std::optional< SPIRV::LinkageType::LinkageType > getSpirvLinkageTypeFor(const SPIRVSubtarget &ST, const GlobalValue &GV)
bool isEntryPoint(const Function &F)
const std::set< unsigned > & getTypeFoldingSupportedOpcodes()
SPIRV::StorageClass::StorageClass addressSpaceToStorageClass(unsigned AddrSpace, const SPIRVSubtarget &STI)
LLVM_ABI std::optional< int > getOptionalIntLoopAttribute(const Loop *TheLoop, StringRef Name)
Find named metadata for a loop with an integer value.
AtomicOrdering
Atomic ordering for LLVM's memory model.
SPIRV::Scope::Scope getMemScope(LLVMContext &Ctx, SyncScope::ID Id)
static bool isPipeOrAddressSpaceCastBI(const StringRef MangledName)
void buildOpSpirvDecorations(Register Reg, MachineIRBuilder &MIRBuilder, const MDNode *GVarMD, const SPIRVSubtarget &ST)
std::string getStringValueFromReg(Register Reg, MachineRegisterInfo &MRI)
int64_t foldImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
Type * parseBasicTypeName(StringRef &TypeName, LLVMContext &Ctx)
DWARFExpression::Operation Op
MachineInstr * getDefInstrMaybeConstant(Register &ConstReg, const MachineRegisterInfo *MRI)
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)
Type * getMDOperandAsType(const MDNode *N, unsigned I)
auto predecessors(const MachineBasicBlock *BB)
static size_t getPaddedLen(const StringRef &Str)
bool isSpvIntrinsic(const MachineInstr &MI, Intrinsic::ID IntrinsicID)
void addStringImm(const StringRef &Str, MCInst &Inst)
static bool isKernelQueryBI(const StringRef MangledName)
MachineInstr * getVRegDef(MachineRegisterInfo &MRI, Register Reg)
static bool isEnqueueKernelBI(const StringRef MangledName)
SPIRV::MemorySemantics::MemorySemantics getMemSemantics(AtomicOrdering Ord)
#define N