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 {
31namespace SPIRV {
32// This code restores function args/retvalue types for composite cases
33// because the final types should still be aggregate whereas they're i32
34// during the translation to cope with aggregate flattening etc.
35// TODO: should these just return nullptr when there's no metadata?
37 FunctionType *FTy,
38 StringRef Name) {
39 if (!NMD)
40 return FTy;
41
42 constexpr auto getConstInt = [](MDNode *MD, unsigned OpId) -> ConstantInt * {
43 if (MD->getNumOperands() <= OpId)
44 return nullptr;
45 if (auto *CMeta = dyn_cast<ConstantAsMetadata>(MD->getOperand(OpId)))
46 return dyn_cast<ConstantInt>(CMeta->getValue());
47 return nullptr;
48 };
49
50 auto It = find_if(NMD->operands(), [Name](MDNode *N) {
51 if (auto *MDS = dyn_cast_or_null<MDString>(N->getOperand(0)))
52 return MDS->getString() == Name;
53 return false;
54 });
55
56 if (It == NMD->op_end())
57 return FTy;
58
59 Type *RetTy = FTy->getReturnType();
60 SmallVector<Type *, 4> PTys(FTy->params());
61
62 for (unsigned I = 1; I != (*It)->getNumOperands(); ++I) {
63 MDNode *MD = dyn_cast<MDNode>((*It)->getOperand(I));
64 assert(MD && "MDNode operand is expected");
65
66 if (auto *Const = getConstInt(MD, 0)) {
67 auto *CMeta = dyn_cast<ConstantAsMetadata>(MD->getOperand(1));
68 assert(CMeta && "ConstantAsMetadata operand is expected");
69 assert(Const->getSExtValue() >= -1);
70 // Currently -1 indicates return value, greater values mean
71 // argument numbers.
72 if (Const->getSExtValue() == -1)
73 RetTy = CMeta->getType();
74 else
75 PTys[Const->getSExtValue()] = CMeta->getType();
76 }
77 }
78
79 return FunctionType::get(RetTy, PTys, FTy->isVarArg());
80}
81
84 F.getParent()->getNamedMetadata("spv.cloned_funcs"), F.getFunctionType(),
85 F.getName());
86}
87
90 CB.getModule()->getNamedMetadata("spv.mutated_callsites"),
91 CB.getFunctionType(), CB.getName());
92}
93} // Namespace SPIRV
94
95// The following functions are used to add these string literals as a series of
96// 32-bit integer operands with the correct format, and unpack them if necessary
97// when making string comparisons in compiler passes.
98// SPIR-V requires null-terminated UTF-8 strings padded to 32-bit alignment.
99static uint32_t convertCharsToWord(const StringRef &Str, unsigned i) {
100 uint32_t Word = 0u; // Build up this 32-bit word from 4 8-bit chars.
101 for (unsigned WordIndex = 0; WordIndex < 4; ++WordIndex) {
102 unsigned StrIndex = i + WordIndex;
103 uint8_t CharToAdd = 0; // Initilize char as padding/null.
104 if (StrIndex < Str.size()) { // If it's within the string, get a real char.
105 CharToAdd = Str[StrIndex];
106 }
107 Word |= (CharToAdd << (WordIndex * 8));
108 }
109 return Word;
110}
111
112// Get length including padding and null terminator.
113static size_t getPaddedLen(const StringRef &Str) {
114 return (Str.size() + 4) & ~3;
115}
116
117void addStringImm(const StringRef &Str, MCInst &Inst) {
118 const size_t PaddedLen = getPaddedLen(Str);
119 for (unsigned i = 0; i < PaddedLen; i += 4) {
120 // Add an operand for the 32-bits of chars or padding.
122 }
123}
124
126 const size_t PaddedLen = getPaddedLen(Str);
127 for (unsigned i = 0; i < PaddedLen; i += 4) {
128 // Add an operand for the 32-bits of chars or padding.
129 MIB.addImm(convertCharsToWord(Str, i));
130 }
131}
132
134 std::vector<Value *> &Args) {
135 const size_t PaddedLen = getPaddedLen(Str);
136 for (unsigned i = 0; i < PaddedLen; i += 4) {
137 // Add a vector element for the 32-bits of chars or padding.
138 Args.push_back(B.getInt32(convertCharsToWord(Str, i)));
139 }
140}
141
142std::string getStringImm(const MachineInstr &MI, unsigned StartIndex) {
143 return getSPIRVStringOperand(MI, StartIndex);
144}
145
148 assert(Def && Def->getOpcode() == TargetOpcode::G_GLOBAL_VALUE &&
149 "Expected G_GLOBAL_VALUE");
150 const GlobalValue *GV = Def->getOperand(1).getGlobal();
151 Value *V = GV->getOperand(0);
153 return CDA->getAsCString().str();
154}
155
156void addNumImm(const APInt &Imm, MachineInstrBuilder &MIB) {
157 const auto Bitwidth = Imm.getBitWidth();
158 if (Bitwidth == 1)
159 return; // Already handled
160 else if (Bitwidth <= 32) {
161 MIB.addImm(Imm.getZExtValue());
162 // Asm Printer needs this info to print floating-type correctly
163 if (Bitwidth == 16)
165 return;
166 } else if (Bitwidth <= 64) {
167 uint64_t FullImm = Imm.getZExtValue();
168 uint32_t LowBits = FullImm & 0xffffffff;
169 uint32_t HighBits = (FullImm >> 32) & 0xffffffff;
170 MIB.addImm(LowBits).addImm(HighBits);
171 // Asm Printer needs this info to print 64-bit operands correctly
173 return;
174 } else if (Bitwidth <= 128) {
175 uint32_t LowBits = Imm.getRawData()[0] & 0xffffffff;
176 uint32_t MidBits0 = (Imm.getRawData()[0] >> 32) & 0xffffffff;
177 uint32_t MidBits1 = Imm.getRawData()[1] & 0xffffffff;
178 uint32_t HighBits = (Imm.getRawData()[1] >> 32) & 0xffffffff;
179 MIB.addImm(LowBits).addImm(MidBits0).addImm(MidBits1).addImm(HighBits);
180 return;
181 }
182 report_fatal_error("Unsupported constant bitwidth");
183}
184
186 MachineIRBuilder &MIRBuilder) {
187 if (!Name.empty()) {
188 auto MIB = MIRBuilder.buildInstr(SPIRV::OpName).addUse(Target);
189 addStringImm(Name, MIB);
190 }
191}
192
194 const SPIRVInstrInfo &TII) {
195 if (!Name.empty()) {
196 auto MIB =
197 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(SPIRV::OpName))
198 .addUse(Target);
199 addStringImm(Name, MIB);
200 }
201}
202
204 const std::vector<uint32_t> &DecArgs,
205 StringRef StrImm) {
206 if (!StrImm.empty())
207 addStringImm(StrImm, MIB);
208 for (const auto &DecArg : DecArgs)
209 MIB.addImm(DecArg);
210}
211
213 SPIRV::Decoration::Decoration Dec,
214 const std::vector<uint32_t> &DecArgs, StringRef StrImm) {
215 auto MIB = MIRBuilder.buildInstr(SPIRV::OpDecorate)
216 .addUse(Reg)
217 .addImm(static_cast<uint32_t>(Dec));
218 finishBuildOpDecorate(MIB, DecArgs, StrImm);
219}
220
222 SPIRV::Decoration::Decoration Dec,
223 const std::vector<uint32_t> &DecArgs, StringRef StrImm) {
224 MachineBasicBlock &MBB = *I.getParent();
225 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(SPIRV::OpDecorate))
226 .addUse(Reg)
227 .addImm(static_cast<uint32_t>(Dec));
228 finishBuildOpDecorate(MIB, DecArgs, StrImm);
229}
230
232 SPIRV::Decoration::Decoration Dec, uint32_t Member,
233 const std::vector<uint32_t> &DecArgs,
234 StringRef StrImm) {
235 auto MIB = MIRBuilder.buildInstr(SPIRV::OpMemberDecorate)
236 .addUse(Reg)
237 .addImm(Member)
238 .addImm(static_cast<uint32_t>(Dec));
239 finishBuildOpDecorate(MIB, DecArgs, StrImm);
240}
241
243 const SPIRVInstrInfo &TII,
244 SPIRV::Decoration::Decoration Dec, uint32_t Member,
245 const std::vector<uint32_t> &DecArgs,
246 StringRef StrImm) {
247 MachineBasicBlock &MBB = *I.getParent();
248 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(SPIRV::OpMemberDecorate))
249 .addUse(Reg)
250 .addImm(Member)
251 .addImm(static_cast<uint32_t>(Dec));
252 finishBuildOpDecorate(MIB, DecArgs, StrImm);
253}
254
256 const MDNode *GVarMD, const SPIRVSubtarget &ST) {
257 for (unsigned I = 0, E = GVarMD->getNumOperands(); I != E; ++I) {
258 auto *OpMD = dyn_cast<MDNode>(GVarMD->getOperand(I));
259 if (!OpMD)
260 report_fatal_error("Invalid decoration");
261 if (OpMD->getNumOperands() == 0)
262 report_fatal_error("Expect operand(s) of the decoration");
263 ConstantInt *DecorationId =
264 mdconst::dyn_extract<ConstantInt>(OpMD->getOperand(0));
265 if (!DecorationId)
266 report_fatal_error("Expect SPIR-V <Decoration> operand to be the first "
267 "element of the decoration");
268
269 // The goal of `spirv.Decorations` metadata is to provide a way to
270 // represent SPIR-V entities that do not map to LLVM in an obvious way.
271 // FP flags do have obvious matches between LLVM IR and SPIR-V.
272 // Additionally, we have no guarantee at this point that the flags passed
273 // through the decoration are not violated already in the optimizer passes.
274 // Therefore, we simply ignore FP flags, including NoContraction, and
275 // FPFastMathMode.
276 if (DecorationId->getZExtValue() ==
277 static_cast<uint32_t>(SPIRV::Decoration::NoContraction) ||
278 DecorationId->getZExtValue() ==
279 static_cast<uint32_t>(SPIRV::Decoration::FPFastMathMode)) {
280 continue; // Ignored.
281 }
282 auto MIB = MIRBuilder.buildInstr(SPIRV::OpDecorate)
283 .addUse(Reg)
284 .addImm(static_cast<uint32_t>(DecorationId->getZExtValue()));
285 for (unsigned OpI = 1, OpE = OpMD->getNumOperands(); OpI != OpE; ++OpI) {
286 if (ConstantInt *OpV =
287 mdconst::dyn_extract<ConstantInt>(OpMD->getOperand(OpI)))
288 MIB.addImm(static_cast<uint32_t>(OpV->getZExtValue()));
289 else if (MDString *OpV = dyn_cast<MDString>(OpMD->getOperand(OpI)))
290 addStringImm(OpV->getString(), MIB);
291 else
292 report_fatal_error("Unexpected operand of the decoration");
293 }
294 }
295}
296
298 MachineFunction *MF = I.getParent()->getParent();
299 MachineBasicBlock *MBB = &MF->front();
300 MachineBasicBlock::iterator It = MBB->SkipPHIsAndLabels(MBB->begin()),
301 E = MBB->end();
302 bool IsHeader = false;
303 unsigned Opcode;
304 for (; It != E && It != I; ++It) {
305 Opcode = It->getOpcode();
306 if (Opcode == SPIRV::OpFunction || Opcode == SPIRV::OpFunctionParameter) {
307 IsHeader = true;
308 } else if (IsHeader &&
309 !(Opcode == SPIRV::ASSIGN_TYPE || Opcode == SPIRV::OpLabel)) {
310 ++It;
311 break;
312 }
313 }
314 return It;
315}
316
319 if (I == MBB->begin())
320 return I;
321 --I;
322 while (I->isTerminator() || I->isDebugValue()) {
323 if (I == MBB->begin())
324 break;
325 --I;
326 }
327 return I;
328}
329
330SPIRV::StorageClass::StorageClass
331addressSpaceToStorageClass(unsigned AddrSpace, const SPIRVSubtarget &STI) {
332 switch (AddrSpace) {
333 case 0:
334 return SPIRV::StorageClass::Function;
335 case 1:
336 return SPIRV::StorageClass::CrossWorkgroup;
337 case 2:
338 return SPIRV::StorageClass::UniformConstant;
339 case 3:
340 return SPIRV::StorageClass::Workgroup;
341 case 4:
342 return SPIRV::StorageClass::Generic;
343 case 5:
344 return STI.canUseExtension(SPIRV::Extension::SPV_INTEL_usm_storage_classes)
345 ? SPIRV::StorageClass::DeviceOnlyINTEL
346 : SPIRV::StorageClass::CrossWorkgroup;
347 case 6:
348 return STI.canUseExtension(SPIRV::Extension::SPV_INTEL_usm_storage_classes)
349 ? SPIRV::StorageClass::HostOnlyINTEL
350 : SPIRV::StorageClass::CrossWorkgroup;
351 case 7:
352 return SPIRV::StorageClass::Input;
353 case 8:
354 return SPIRV::StorageClass::Output;
355 case 9:
356 return SPIRV::StorageClass::CodeSectionINTEL;
357 case 10:
358 return SPIRV::StorageClass::Private;
359 case 11:
360 return SPIRV::StorageClass::StorageBuffer;
361 case 12:
362 return SPIRV::StorageClass::Uniform;
363 case 13:
364 return SPIRV::StorageClass::PushConstant;
365 default:
366 report_fatal_error("Unknown address space");
367 }
368}
369
370SPIRV::MemorySemantics::MemorySemantics
371getMemSemanticsForStorageClass(SPIRV::StorageClass::StorageClass SC) {
372 switch (SC) {
373 case SPIRV::StorageClass::StorageBuffer:
374 case SPIRV::StorageClass::Uniform:
375 return SPIRV::MemorySemantics::UniformMemory;
376 case SPIRV::StorageClass::Workgroup:
377 return SPIRV::MemorySemantics::WorkgroupMemory;
378 case SPIRV::StorageClass::CrossWorkgroup:
379 return SPIRV::MemorySemantics::CrossWorkgroupMemory;
380 case SPIRV::StorageClass::AtomicCounter:
381 return SPIRV::MemorySemantics::AtomicCounterMemory;
382 case SPIRV::StorageClass::Image:
383 return SPIRV::MemorySemantics::ImageMemory;
384 default:
385 return SPIRV::MemorySemantics::None;
386 }
387}
388
389SPIRV::MemorySemantics::MemorySemantics getMemSemantics(AtomicOrdering Ord) {
390 switch (Ord) {
392 return SPIRV::MemorySemantics::Acquire;
394 return SPIRV::MemorySemantics::Release;
396 return SPIRV::MemorySemantics::AcquireRelease;
398 return SPIRV::MemorySemantics::SequentiallyConsistent;
402 return SPIRV::MemorySemantics::None;
403 }
404 llvm_unreachable(nullptr);
405}
406
407SPIRV::Scope::Scope getMemScope(LLVMContext &Ctx, SyncScope::ID Id) {
408 // Named by
409 // https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#_scope_id.
410 // We don't need aliases for Invocation and CrossDevice, as we already have
411 // them covered by "singlethread" and "" strings respectively (see
412 // implementation of LLVMContext::LLVMContext()).
413 static const llvm::SyncScope::ID SubGroup =
414 Ctx.getOrInsertSyncScopeID("subgroup");
415 static const llvm::SyncScope::ID WorkGroup =
416 Ctx.getOrInsertSyncScopeID("workgroup");
417 static const llvm::SyncScope::ID Device =
418 Ctx.getOrInsertSyncScopeID("device");
419
421 return SPIRV::Scope::Invocation;
422 else if (Id == llvm::SyncScope::System)
423 return SPIRV::Scope::CrossDevice;
424 else if (Id == SubGroup)
425 return SPIRV::Scope::Subgroup;
426 else if (Id == WorkGroup)
427 return SPIRV::Scope::Workgroup;
428 else if (Id == Device)
429 return SPIRV::Scope::Device;
430 return SPIRV::Scope::CrossDevice;
431}
432
434 const MachineRegisterInfo *MRI) {
435 MachineInstr *MI = MRI->getVRegDef(ConstReg);
436 MachineInstr *ConstInstr =
437 MI->getOpcode() == SPIRV::G_TRUNC || MI->getOpcode() == SPIRV::G_ZEXT
438 ? MRI->getVRegDef(MI->getOperand(1).getReg())
439 : MI;
440 if (auto *GI = dyn_cast<GIntrinsic>(ConstInstr)) {
441 if (GI->is(Intrinsic::spv_track_constant)) {
442 ConstReg = ConstInstr->getOperand(2).getReg();
443 return MRI->getVRegDef(ConstReg);
444 }
445 } else if (ConstInstr->getOpcode() == SPIRV::ASSIGN_TYPE) {
446 ConstReg = ConstInstr->getOperand(1).getReg();
447 return MRI->getVRegDef(ConstReg);
448 } else if (ConstInstr->getOpcode() == TargetOpcode::G_CONSTANT ||
449 ConstInstr->getOpcode() == TargetOpcode::G_FCONSTANT) {
450 ConstReg = ConstInstr->getOperand(0).getReg();
451 return ConstInstr;
452 }
453 return MRI->getVRegDef(ConstReg);
454}
455
457 const MachineInstr *MI = getDefInstrMaybeConstant(ConstReg, MRI);
458 assert(MI && MI->getOpcode() == TargetOpcode::G_CONSTANT);
459 return MI->getOperand(1).getCImm()->getValue().getZExtValue();
460}
461
463 const MachineInstr *MI = getDefInstrMaybeConstant(ConstReg, MRI);
464 assert(MI && MI->getOpcode() == TargetOpcode::G_CONSTANT);
465 return MI->getOperand(1).getCImm()->getSExtValue();
466}
467
468bool isSpvIntrinsic(const MachineInstr &MI, Intrinsic::ID IntrinsicID) {
469 if (const auto *GI = dyn_cast<GIntrinsic>(&MI))
470 return GI->is(IntrinsicID);
471 return false;
472}
473
474Type *getMDOperandAsType(const MDNode *N, unsigned I) {
475 Type *ElementTy = cast<ValueAsMetadata>(N->getOperand(I))->getType();
476 return toTypedPointer(ElementTy);
477}
478
479// The set of names is borrowed from the SPIR-V translator.
480// TODO: may be implemented in SPIRVBuiltins.td.
481static bool isPipeOrAddressSpaceCastBI(const StringRef MangledName) {
482 return MangledName == "write_pipe_2" || MangledName == "read_pipe_2" ||
483 MangledName == "write_pipe_2_bl" || MangledName == "read_pipe_2_bl" ||
484 MangledName == "write_pipe_4" || MangledName == "read_pipe_4" ||
485 MangledName == "reserve_write_pipe" ||
486 MangledName == "reserve_read_pipe" ||
487 MangledName == "commit_write_pipe" ||
488 MangledName == "commit_read_pipe" ||
489 MangledName == "work_group_reserve_write_pipe" ||
490 MangledName == "work_group_reserve_read_pipe" ||
491 MangledName == "work_group_commit_write_pipe" ||
492 MangledName == "work_group_commit_read_pipe" ||
493 MangledName == "get_pipe_num_packets_ro" ||
494 MangledName == "get_pipe_max_packets_ro" ||
495 MangledName == "get_pipe_num_packets_wo" ||
496 MangledName == "get_pipe_max_packets_wo" ||
497 MangledName == "sub_group_reserve_write_pipe" ||
498 MangledName == "sub_group_reserve_read_pipe" ||
499 MangledName == "sub_group_commit_write_pipe" ||
500 MangledName == "sub_group_commit_read_pipe" ||
501 MangledName == "to_global" || MangledName == "to_local" ||
502 MangledName == "to_private";
503}
504
505static bool isEnqueueKernelBI(const StringRef MangledName) {
506 return MangledName == "__enqueue_kernel_basic" ||
507 MangledName == "__enqueue_kernel_basic_events" ||
508 MangledName == "__enqueue_kernel_varargs" ||
509 MangledName == "__enqueue_kernel_events_varargs";
510}
511
512static bool isKernelQueryBI(const StringRef MangledName) {
513 return MangledName == "__get_kernel_work_group_size_impl" ||
514 MangledName == "__get_kernel_sub_group_count_for_ndrange_impl" ||
515 MangledName == "__get_kernel_max_sub_group_size_for_ndrange_impl" ||
516 MangledName == "__get_kernel_preferred_work_group_size_multiple_impl";
517}
518
520 if (!Name.starts_with("__"))
521 return false;
522
523 return isEnqueueKernelBI(Name) || isKernelQueryBI(Name) ||
524 isPipeOrAddressSpaceCastBI(Name.drop_front(2)) ||
525 Name == "__translate_sampler_initializer";
526}
527
529 bool IsNonMangledOCL = isNonMangledOCLBuiltin(Name);
530 bool IsNonMangledSPIRV = Name.starts_with("__spirv_");
531 bool IsNonMangledHLSL = Name.starts_with("__hlsl_");
532 bool IsMangled = Name.starts_with("_Z");
533
534 // Otherwise use simple demangling to return the function name.
535 if (IsNonMangledOCL || IsNonMangledSPIRV || IsNonMangledHLSL || !IsMangled)
536 return Name.str();
537
538 // Try to use the itanium demangler.
539 if (char *DemangledName = itaniumDemangle(Name.data())) {
540 std::string Result = DemangledName;
541 free(DemangledName);
542 return Result;
543 }
544
545 // Autocheck C++, maybe need to do explicit check of the source language.
546 // OpenCL C++ built-ins are declared in cl namespace.
547 // TODO: consider using 'St' abbriviation for cl namespace mangling.
548 // Similar to ::std:: in C++.
549 size_t Start, Len = 0;
550 size_t DemangledNameLenStart = 2;
551 if (Name.starts_with("_ZN")) {
552 // Skip CV and ref qualifiers.
553 size_t NameSpaceStart = Name.find_first_not_of("rVKRO", 3);
554 // All built-ins are in the ::cl:: namespace.
555 if (Name.substr(NameSpaceStart, 11) != "2cl7__spirv")
556 return std::string();
557 DemangledNameLenStart = NameSpaceStart + 11;
558 }
559 Start = Name.find_first_not_of("0123456789", DemangledNameLenStart);
560 [[maybe_unused]] bool Error =
561 Name.substr(DemangledNameLenStart, Start - DemangledNameLenStart)
562 .getAsInteger(10, Len);
563 assert(!Error && "Failed to parse demangled name length");
564 return Name.substr(Start, Len).str();
565}
566
568 if (Name.starts_with("opencl.") || Name.starts_with("ocl_") ||
569 Name.starts_with("spirv."))
570 return true;
571 return false;
572}
573
574bool isSpecialOpaqueType(const Type *Ty) {
575 if (const TargetExtType *ExtTy = dyn_cast<TargetExtType>(Ty))
576 return isTypedPointerWrapper(ExtTy)
577 ? false
578 : hasBuiltinTypePrefix(ExtTy->getName());
579
580 return false;
581}
582
583bool isEntryPoint(const Function &F) {
584 // OpenCL handling: any function with the SPIR_KERNEL
585 // calling convention will be a potential entry point.
586 if (F.getCallingConv() == CallingConv::SPIR_KERNEL)
587 return true;
588
589 // HLSL handling: special attribute are emitted from the
590 // front-end.
591 if (F.getFnAttribute("hlsl.shader").isValid())
592 return true;
593
594 return false;
595}
596
598 TypeName.consume_front("atomic_");
599 if (TypeName.consume_front("void"))
600 return Type::getVoidTy(Ctx);
601 else if (TypeName.consume_front("bool") || TypeName.consume_front("_Bool"))
602 return Type::getIntNTy(Ctx, 1);
603 else if (TypeName.consume_front("char") ||
604 TypeName.consume_front("signed char") ||
605 TypeName.consume_front("unsigned char") ||
606 TypeName.consume_front("uchar"))
607 return Type::getInt8Ty(Ctx);
608 else if (TypeName.consume_front("short") ||
609 TypeName.consume_front("signed short") ||
610 TypeName.consume_front("unsigned short") ||
611 TypeName.consume_front("ushort"))
612 return Type::getInt16Ty(Ctx);
613 else if (TypeName.consume_front("int") ||
614 TypeName.consume_front("signed int") ||
615 TypeName.consume_front("unsigned int") ||
616 TypeName.consume_front("uint"))
617 return Type::getInt32Ty(Ctx);
618 else if (TypeName.consume_front("long") ||
619 TypeName.consume_front("signed long") ||
620 TypeName.consume_front("unsigned long") ||
621 TypeName.consume_front("ulong"))
622 return Type::getInt64Ty(Ctx);
623 else if (TypeName.consume_front("half") ||
624 TypeName.consume_front("_Float16") ||
625 TypeName.consume_front("__fp16"))
626 return Type::getHalfTy(Ctx);
627 else if (TypeName.consume_front("float"))
628 return Type::getFloatTy(Ctx);
629 else if (TypeName.consume_front("double"))
630 return Type::getDoubleTy(Ctx);
631
632 // Unable to recognize SPIRV type name
633 return nullptr;
634}
635
636std::unordered_set<BasicBlock *>
637PartialOrderingVisitor::getReachableFrom(BasicBlock *Start) {
638 std::queue<BasicBlock *> ToVisit;
639 ToVisit.push(Start);
640
641 std::unordered_set<BasicBlock *> Output;
642 while (ToVisit.size() != 0) {
643 BasicBlock *BB = ToVisit.front();
644 ToVisit.pop();
645
646 if (Output.count(BB) != 0)
647 continue;
648 Output.insert(BB);
649
650 for (BasicBlock *Successor : successors(BB)) {
651 if (DT.dominates(Successor, BB))
652 continue;
653 ToVisit.push(Successor);
654 }
655 }
656
657 return Output;
658}
659
660bool PartialOrderingVisitor::CanBeVisited(BasicBlock *BB) const {
661 for (BasicBlock *P : predecessors(BB)) {
662 // Ignore back-edges.
663 if (DT.dominates(BB, P))
664 continue;
665
666 // One of the predecessor hasn't been visited. Not ready yet.
667 if (BlockToOrder.count(P) == 0)
668 return false;
669
670 // If the block is a loop exit, the loop must be finished before
671 // we can continue.
672 Loop *L = LI.getLoopFor(P);
673 if (L == nullptr || L->contains(BB))
674 continue;
675
676 // SPIR-V requires a single back-edge. And the backend first
677 // step transforms loops into the simplified format. If we have
678 // more than 1 back-edge, something is wrong.
679 assert(L->getNumBackEdges() <= 1);
680
681 // If the loop has no latch, loop's rank won't matter, so we can
682 // proceed.
683 BasicBlock *Latch = L->getLoopLatch();
684 assert(Latch);
685 if (Latch == nullptr)
686 continue;
687
688 // The latch is not ready yet, let's wait.
689 if (BlockToOrder.count(Latch) == 0)
690 return false;
691 }
692
693 return true;
694}
695
697 auto It = BlockToOrder.find(BB);
698 if (It != BlockToOrder.end())
699 return It->second.Rank;
700
701 size_t result = 0;
702 for (BasicBlock *P : predecessors(BB)) {
703 // Ignore back-edges.
704 if (DT.dominates(BB, P))
705 continue;
706
707 auto Iterator = BlockToOrder.end();
708 Loop *L = LI.getLoopFor(P);
709 BasicBlock *Latch = L ? L->getLoopLatch() : nullptr;
710
711 // If the predecessor is either outside a loop, or part of
712 // the same loop, simply take its rank + 1.
713 if (L == nullptr || L->contains(BB) || Latch == nullptr) {
714 Iterator = BlockToOrder.find(P);
715 } else {
716 // Otherwise, take the loop's rank (highest rank in the loop) as base.
717 // Since loops have a single latch, highest rank is easy to find.
718 // If the loop has no latch, then it doesn't matter.
719 Iterator = BlockToOrder.find(Latch);
720 }
721
722 assert(Iterator != BlockToOrder.end());
723 result = std::max(result, Iterator->second.Rank + 1);
724 }
725
726 return result;
727}
728
729size_t PartialOrderingVisitor::visit(BasicBlock *BB, size_t Unused) {
730 ToVisit.push(BB);
731 Queued.insert(BB);
732
733 size_t QueueIndex = 0;
734 while (ToVisit.size() != 0) {
735 BasicBlock *BB = ToVisit.front();
736 ToVisit.pop();
737
738 if (!CanBeVisited(BB)) {
739 ToVisit.push(BB);
740 if (QueueIndex >= ToVisit.size())
742 "No valid candidate in the queue. Is the graph reducible?");
743 QueueIndex++;
744 continue;
745 }
746
747 QueueIndex = 0;
748 size_t Rank = GetNodeRank(BB);
749 OrderInfo Info = {Rank, BlockToOrder.size()};
750 BlockToOrder.emplace(BB, Info);
751
752 for (BasicBlock *S : successors(BB)) {
753 if (Queued.count(S) != 0)
754 continue;
755 ToVisit.push(S);
756 Queued.insert(S);
757 }
758 }
759
760 return 0;
761}
762
764 DT.recalculate(F);
765 LI = LoopInfo(DT);
766
767 visit(&*F.begin(), 0);
768
769 Order.reserve(F.size());
770 for (auto &[BB, Info] : BlockToOrder)
771 Order.emplace_back(BB);
772
773 std::sort(Order.begin(), Order.end(), [&](const auto &LHS, const auto &RHS) {
774 return compare(LHS, RHS);
775 });
776}
777
779 const BasicBlock *RHS) const {
780 const OrderInfo &InfoLHS = BlockToOrder.at(const_cast<BasicBlock *>(LHS));
781 const OrderInfo &InfoRHS = BlockToOrder.at(const_cast<BasicBlock *>(RHS));
782 if (InfoLHS.Rank != InfoRHS.Rank)
783 return InfoLHS.Rank < InfoRHS.Rank;
784 return InfoLHS.TraversalIndex < InfoRHS.TraversalIndex;
785}
786
788 BasicBlock &Start, std::function<bool(BasicBlock *)> Op) {
789 std::unordered_set<BasicBlock *> Reachable = getReachableFrom(&Start);
790 assert(BlockToOrder.count(&Start) != 0);
791
792 // Skipping blocks with a rank inferior to |Start|'s rank.
793 auto It = Order.begin();
794 while (It != Order.end() && *It != &Start)
795 ++It;
796
797 // This is unexpected. Worst case |Start| is the last block,
798 // so It should point to the last block, not past-end.
799 assert(It != Order.end());
800
801 // By default, there is no rank limit. Setting it to the maximum value.
802 std::optional<size_t> EndRank = std::nullopt;
803 for (; It != Order.end(); ++It) {
804 if (EndRank.has_value() && BlockToOrder[*It].Rank > *EndRank)
805 break;
806
807 if (Reachable.count(*It) == 0) {
808 continue;
809 }
810
811 if (!Op(*It)) {
812 EndRank = BlockToOrder[*It].Rank;
813 }
814 }
815}
816
818 if (F.size() == 0)
819 return false;
820
821 bool Modified = false;
822 std::vector<BasicBlock *> Order;
823 Order.reserve(F.size());
824
826 llvm::append_range(Order, RPOT);
827
828 assert(&*F.begin() == Order[0]);
829 BasicBlock *LastBlock = &*F.begin();
830 for (BasicBlock *BB : Order) {
831 if (BB != LastBlock && &*LastBlock->getNextNode() != BB) {
832 Modified = true;
833 BB->moveAfter(LastBlock);
834 }
835 LastBlock = BB;
836 }
837
838 return Modified;
839}
840
842 MachineInstr *MaybeDef = MRI.getVRegDef(Reg);
843 if (MaybeDef && MaybeDef->getOpcode() == SPIRV::ASSIGN_TYPE)
844 MaybeDef = MRI.getVRegDef(MaybeDef->getOperand(1).getReg());
845 return MaybeDef;
846}
847
848bool getVacantFunctionName(Module &M, std::string &Name) {
849 // It's a bit of paranoia, but still we don't want to have even a chance that
850 // the loop will work for too long.
851 constexpr unsigned MaxIters = 1024;
852 for (unsigned I = 0; I < MaxIters; ++I) {
853 std::string OrdName = Name + Twine(I).str();
854 if (!M.getFunction(OrdName)) {
855 Name = std::move(OrdName);
856 return true;
857 }
858 }
859 return false;
860}
861
862// Assign SPIR-V type to the register. If the register has no valid assigned
863// class, set register LLT type and class according to the SPIR-V type.
866 bool Force) {
867 GR->assignSPIRVTypeToVReg(SpvType, Reg, MF);
868 if (!MRI->getRegClassOrNull(Reg) || Force) {
869 MRI->setRegClass(Reg, GR->getRegClass(SpvType));
870 MRI->setType(Reg, GR->getRegType(SpvType));
871 }
872}
873
874// Create a SPIR-V type, assign SPIR-V type to the register. If the register has
875// no valid assigned class, set register LLT type and class according to the
876// SPIR-V type.
878 MachineIRBuilder &MIRBuilder,
879 SPIRV::AccessQualifier::AccessQualifier AccessQual,
880 bool EmitIR, bool Force) {
882 GR->getOrCreateSPIRVType(Ty, MIRBuilder, AccessQual, EmitIR),
883 GR, MIRBuilder.getMRI(), MIRBuilder.getMF(), Force);
884}
885
886// Create a virtual register and assign SPIR-V type to the register. Set
887// register LLT type and class according to the SPIR-V type.
890 const MachineFunction &MF) {
891 Register Reg = MRI->createVirtualRegister(GR->getRegClass(SpvType));
892 MRI->setType(Reg, GR->getRegType(SpvType));
893 GR->assignSPIRVTypeToVReg(SpvType, Reg, MF);
894 return Reg;
895}
896
897// Create a virtual register and assign SPIR-V type to the register. Set
898// register LLT type and class according to the SPIR-V type.
900 MachineIRBuilder &MIRBuilder) {
901 return createVirtualRegister(SpvType, GR, MIRBuilder.getMRI(),
902 MIRBuilder.getMF());
903}
904
905// Create a SPIR-V type, virtual register and assign SPIR-V type to the
906// register. Set register LLT type and class according to the SPIR-V type.
908 const Type *Ty, SPIRVGlobalRegistry *GR, MachineIRBuilder &MIRBuilder,
909 SPIRV::AccessQualifier::AccessQualifier AccessQual, bool EmitIR) {
911 GR->getOrCreateSPIRVType(Ty, MIRBuilder, AccessQual, EmitIR), GR,
912 MIRBuilder);
913}
914
916 Value *Arg, Value *Arg2, ArrayRef<Constant *> Imms,
917 IRBuilder<> &B) {
919 Args.push_back(Arg2);
920 Args.push_back(buildMD(Arg));
921 llvm::append_range(Args, Imms);
922 return B.CreateIntrinsic(IntrID, {Types}, Args);
923}
924
925// Return true if there is an opaque pointer type nested in the argument.
926bool isNestedPointer(const Type *Ty) {
927 if (Ty->isPtrOrPtrVectorTy())
928 return true;
929 if (const FunctionType *RefTy = dyn_cast<FunctionType>(Ty)) {
930 if (isNestedPointer(RefTy->getReturnType()))
931 return true;
932 for (const Type *ArgTy : RefTy->params())
933 if (isNestedPointer(ArgTy))
934 return true;
935 return false;
936 }
937 if (const ArrayType *RefTy = dyn_cast<ArrayType>(Ty))
938 return isNestedPointer(RefTy->getElementType());
939 return false;
940}
941
942bool isSpvIntrinsic(const Value *Arg) {
943 if (const auto *II = dyn_cast<IntrinsicInst>(Arg))
944 if (Function *F = II->getCalledFunction())
945 if (F->getName().starts_with("llvm.spv."))
946 return true;
947 return false;
948}
949
950// Function to create continued instructions for SPV_INTEL_long_composites
951// extension
952SmallVector<MachineInstr *, 4>
954 unsigned MinWC, unsigned ContinuedOpcode,
955 ArrayRef<Register> Args, Register ReturnRegister,
957
959 constexpr unsigned MaxWordCount = UINT16_MAX;
960 const size_t NumElements = Args.size();
961 size_t MaxNumElements = MaxWordCount - MinWC;
962 size_t SPIRVStructNumElements = NumElements;
963
964 if (NumElements > MaxNumElements) {
965 // Do adjustments for continued instructions which always had only one
966 // minumum word count.
967 SPIRVStructNumElements = MaxNumElements;
968 MaxNumElements = MaxWordCount - 1;
969 }
970
971 auto MIB =
972 MIRBuilder.buildInstr(Opcode).addDef(ReturnRegister).addUse(TypeID);
973
974 for (size_t I = 0; I < SPIRVStructNumElements; ++I)
975 MIB.addUse(Args[I]);
976
977 Instructions.push_back(MIB.getInstr());
978
979 for (size_t I = SPIRVStructNumElements; I < NumElements;
980 I += MaxNumElements) {
981 auto MIB = MIRBuilder.buildInstr(ContinuedOpcode);
982 for (size_t J = I; J < std::min(I + MaxNumElements, NumElements); ++J)
983 MIB.addUse(Args[J]);
984 Instructions.push_back(MIB.getInstr());
985 }
986 return Instructions;
987}
988
990 unsigned LC = SPIRV::LoopControl::None;
991 // Currently used only to store PartialCount value. Later when other
992 // LoopControls are added - this map should be sorted before making
993 // them loop_merge operands to satisfy 3.23. Loop Control requirements.
994 std::vector<std::pair<unsigned, unsigned>> MaskToValueMap;
995 if (getBooleanLoopAttribute(L, "llvm.loop.unroll.disable")) {
996 LC |= SPIRV::LoopControl::DontUnroll;
997 } else {
998 if (getBooleanLoopAttribute(L, "llvm.loop.unroll.enable") ||
999 getBooleanLoopAttribute(L, "llvm.loop.unroll.full")) {
1000 LC |= SPIRV::LoopControl::Unroll;
1001 }
1002 std::optional<int> Count =
1003 getOptionalIntLoopAttribute(L, "llvm.loop.unroll.count");
1004 if (Count && Count != 1) {
1005 LC |= SPIRV::LoopControl::PartialCount;
1006 MaskToValueMap.emplace_back(
1007 std::make_pair(SPIRV::LoopControl::PartialCount, *Count));
1008 }
1009 }
1010 SmallVector<unsigned, 1> Result = {LC};
1011 for (auto &[Mask, Val] : MaskToValueMap)
1012 Result.push_back(Val);
1013 return Result;
1014}
1015
1016const std::set<unsigned> &getTypeFoldingSupportedOpcodes() {
1017 // clang-format off
1018 static const std::set<unsigned> TypeFoldingSupportingOpcs = {
1019 TargetOpcode::G_ADD,
1020 TargetOpcode::G_FADD,
1021 TargetOpcode::G_STRICT_FADD,
1022 TargetOpcode::G_SUB,
1023 TargetOpcode::G_FSUB,
1024 TargetOpcode::G_STRICT_FSUB,
1025 TargetOpcode::G_MUL,
1026 TargetOpcode::G_FMUL,
1027 TargetOpcode::G_STRICT_FMUL,
1028 TargetOpcode::G_SDIV,
1029 TargetOpcode::G_UDIV,
1030 TargetOpcode::G_FDIV,
1031 TargetOpcode::G_STRICT_FDIV,
1032 TargetOpcode::G_SREM,
1033 TargetOpcode::G_UREM,
1034 TargetOpcode::G_FREM,
1035 TargetOpcode::G_STRICT_FREM,
1036 TargetOpcode::G_FNEG,
1037 TargetOpcode::G_CONSTANT,
1038 TargetOpcode::G_FCONSTANT,
1039 TargetOpcode::G_AND,
1040 TargetOpcode::G_OR,
1041 TargetOpcode::G_XOR,
1042 TargetOpcode::G_SHL,
1043 TargetOpcode::G_ASHR,
1044 TargetOpcode::G_LSHR,
1045 TargetOpcode::G_SELECT,
1046 TargetOpcode::G_EXTRACT_VECTOR_ELT,
1047 };
1048 // clang-format on
1049 return TypeFoldingSupportingOpcs;
1050}
1051
1052bool isTypeFoldingSupported(unsigned Opcode) {
1053 return getTypeFoldingSupportedOpcodes().count(Opcode) > 0;
1054}
1055
1056// Traversing [g]MIR accounting for pseudo-instructions.
1058 return (Def->getOpcode() == SPIRV::ASSIGN_TYPE ||
1059 Def->getOpcode() == TargetOpcode::COPY)
1060 ? MRI->getVRegDef(Def->getOperand(1).getReg())
1061 : Def;
1062}
1063
1065 if (MachineInstr *Def = MRI->getVRegDef(MO.getReg()))
1066 return passCopy(Def, MRI);
1067 return nullptr;
1068}
1069
1071 if (MachineInstr *Def = getDef(MO, MRI)) {
1072 if (Def->getOpcode() == TargetOpcode::G_CONSTANT ||
1073 Def->getOpcode() == SPIRV::OpConstantI)
1074 return Def;
1075 }
1076 return nullptr;
1077}
1078
1079int64_t foldImm(const MachineOperand &MO, const MachineRegisterInfo *MRI) {
1080 if (MachineInstr *Def = getImm(MO, MRI)) {
1081 if (Def->getOpcode() == SPIRV::OpConstantI)
1082 return Def->getOperand(2).getImm();
1083 if (Def->getOpcode() == TargetOpcode::G_CONSTANT)
1084 return Def->getOperand(1).getCImm()->getZExtValue();
1085 }
1086 llvm_unreachable("Unexpected integer constant pattern");
1087}
1088
1090 const MachineInstr *ResType) {
1091 return foldImm(ResType->getOperand(2), MRI);
1092}
1093
1096 // Find the position to insert the OpVariable instruction.
1097 // We will insert it after the last OpFunctionParameter, if any, or
1098 // after OpFunction otherwise.
1099 MachineBasicBlock::iterator VarPos = BB.begin();
1100 while (VarPos != BB.end() && VarPos->getOpcode() != SPIRV::OpFunction) {
1101 ++VarPos;
1102 }
1103 // Advance VarPos to the next instruction after OpFunction, it will either
1104 // be an OpFunctionParameter, so that we can start the next loop, or the
1105 // position to insert the OpVariable instruction.
1106 ++VarPos;
1107 while (VarPos != BB.end() &&
1108 VarPos->getOpcode() == SPIRV::OpFunctionParameter) {
1109 ++VarPos;
1110 }
1111 // VarPos is now pointing at after the last OpFunctionParameter, if any,
1112 // or after OpFunction, if no parameters.
1113 return VarPos != BB.end() && VarPos->getOpcode() == SPIRV::OpLabel ? ++VarPos
1114 : VarPos;
1115}
1116
1117bool matchPeeledArrayPattern(const StructType *Ty, Type *&OriginalElementType,
1118 uint64_t &TotalSize) {
1119 // An array of N padded structs is represented as {[N-1 x <{T, pad}>], T}.
1120 if (Ty->getStructNumElements() != 2)
1121 return false;
1122
1123 Type *FirstElement = Ty->getStructElementType(0);
1124 Type *SecondElement = Ty->getStructElementType(1);
1125
1126 if (!FirstElement->isArrayTy())
1127 return false;
1128
1129 Type *ArrayElementType = FirstElement->getArrayElementType();
1130 if (!ArrayElementType->isStructTy() ||
1131 ArrayElementType->getStructNumElements() != 2)
1132 return false;
1133
1134 Type *T_in_struct = ArrayElementType->getStructElementType(0);
1135 if (T_in_struct != SecondElement)
1136 return false;
1137
1138 auto *Padding_in_struct =
1139 dyn_cast<TargetExtType>(ArrayElementType->getStructElementType(1));
1140 if (!Padding_in_struct || Padding_in_struct->getName() != "spirv.Padding")
1141 return false;
1142
1143 const uint64_t ArraySize = FirstElement->getArrayNumElements();
1144 TotalSize = ArraySize + 1;
1145 OriginalElementType = ArrayElementType;
1146 return true;
1147}
1148
1150 if (!Ty->isStructTy())
1151 return Ty;
1152
1153 auto *STy = cast<StructType>(Ty);
1154 Type *OriginalElementType = nullptr;
1155 uint64_t TotalSize = 0;
1156 if (matchPeeledArrayPattern(STy, OriginalElementType, TotalSize)) {
1157 Type *ResultTy = ArrayType::get(
1158 reconstitutePeeledArrayType(OriginalElementType), TotalSize);
1159 return ResultTy;
1160 }
1161
1162 SmallVector<Type *, 4> NewElementTypes;
1163 bool Changed = false;
1164 for (Type *ElementTy : STy->elements()) {
1165 Type *NewElementTy = reconstitutePeeledArrayType(ElementTy);
1166 if (NewElementTy != ElementTy)
1167 Changed = true;
1168 NewElementTypes.push_back(NewElementTy);
1169 }
1170
1171 if (!Changed)
1172 return Ty;
1173
1174 Type *ResultTy;
1175 if (STy->isLiteral())
1176 ResultTy =
1177 StructType::get(STy->getContext(), NewElementTypes, STy->isPacked());
1178 else {
1179 auto *NewTy = StructType::create(STy->getContext(), STy->getName());
1180 NewTy->setBody(NewElementTypes, STy->isPacked());
1181 ResultTy = NewTy;
1182 }
1183 return ResultTy;
1184}
1185
1186std::optional<SPIRV::LinkageType::LinkageType>
1188 if (GV.hasLocalLinkage() || GV.hasHiddenVisibility())
1189 return std::nullopt;
1190
1191 if (GV.isDeclarationForLinker())
1192 return SPIRV::LinkageType::Import;
1193
1194 if (GV.hasLinkOnceODRLinkage() &&
1195 ST.canUseExtension(SPIRV::Extension::SPV_KHR_linkonce_odr))
1196 return SPIRV::LinkageType::LinkOnceODR;
1197
1198 return SPIRV::LinkageType::Export;
1199}
1200
1201} // 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:54
#define I(x, y, z)
Definition MD5.cpp:57
This file declares the MachineIRBuilder class.
Register Reg
Type::TypeID TypeID
uint64_t IntrinsicInst * II
#define P(N)
static ConstantInt * getConstInt(MDNode *MD, unsigned NumOp)
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:40
Class to represent array types.
static LLVM_ABI ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
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
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
FunctionType * getFunctionType() const
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:707
StringRef getAsCString() const
If this array is isCString(), then this method returns the array (without the trailing null byte) as ...
Definition Constants.h:680
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:168
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.
ArrayRef< Type * > params() const
bool isVarArg() const
Type * getReturnType() const
static LLVM_ABI FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
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
LLVM_ABI const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
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
NamedMDNode * getNamedMetadata(StringRef Name) const
Return the first NamedMDNode in the module with the specified name.
Definition Module.cpp:296
A tuple of MDNodes.
Definition Metadata.h:1757
op_iterator op_end()
Definition Metadata.h:1846
iterator_range< op_iterator > operands()
Definition Metadata.h:1853
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:20
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
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
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 struct types.
static LLVM_ABI StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Definition Type.cpp:413
static LLVM_ABI StructType * create(LLVMContext &Context, StringRef Name)
This creates an identified struct.
Definition Type.cpp:619
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:297
LLVM_ABI Type * getStructElementType(unsigned N) const
bool isArrayTy() const
True if this is an instance of ArrayType.
Definition Type.h:264
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:296
Type * getArrayElementType() const
Definition Type.h:408
LLVM_ABI unsigned getStructNumElements() const
LLVM_ABI uint64_t getArrayNumElements() const
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:280
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:294
bool isStructTy() const
True if this is an instance of StructType.
Definition Type.h:261
static LLVM_ABI IntegerType * getInt16Ty(LLVMContext &C)
Definition Type.cpp:295
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:300
static LLVM_ABI Type * getDoubleTy(LLVMContext &C)
Definition Type.cpp:285
static LLVM_ABI Type * getFloatTy(LLVMContext &C)
Definition Type.cpp:284
static LLVM_ABI Type * getHalfTy(LLVMContext &C)
Definition Type.cpp:282
Value * getOperand(unsigned i) const
Definition User.h:232
LLVM Value Representation.
Definition Value.h:75
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition ilist_node.h:348
Changed
#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
FunctionType * getOriginalFunctionType(const Function &F)
static FunctionType * extractFunctionTypeFromMetadata(NamedMDNode *NMD, FunctionType *FTy, StringRef Name)
@ 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:403
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)
bool matchPeeledArrayPattern(const StructType *Ty, Type *&OriginalElementType, uint64_t &TotalSize)
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2157
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:513
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:458
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 find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1779
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)
Type * reconstitutePeeledArrayType(Type *Ty)
SPIRV::MemorySemantics::MemorySemantics getMemSemantics(AtomicOrdering Ord)
#define N