40constexpr unsigned CudaFatMagic = 0x466243b1;
41constexpr unsigned HIPFatMagic = 0x48495046;
44 return M.getDataLayout().getIntPtrType(M.getContext());
50 return T.isOSBinFormatMachO() ?
"__TEXT,__StaticInit" :
".text.startup";
136 auto [EntriesB, EntriesE] = EntryArray;
142 ImagesInits.
reserve(Bufs.size());
148 ".omp_offloading.device_image" + Suffix);
150 Image->setSection(Relocatable ?
".llvm.offloading.relocatable"
151 :
".llvm.offloading");
169 Binary.bytes_begin() + Header->EntriesOffset);
170 BeginOffset = Entry->ImageOffset;
171 EndOffset = Entry->ImageOffset + Entry->ImageSize;
174 auto *Begin = ConstantInt::get(
getSizeTTy(M), BeginOffset);
185 ImageE, EntriesB, EntriesE));
195 ".omp_offloading.device_images" + Suffix);
206 ".omp_offloading.descriptor" + Suffix);
215 ".omp_offloading.descriptor_unreg" + Suffix, &M);
216 Func->setSection(getStartupSection(M.getTargetTriple()));
222 M.getOrInsertFunction(
"__tgt_unregister_lib", UnRegFuncTy);
226 Builder.CreateCall(UnRegFuncC, BinDesc);
227 Builder.CreateRetVoid();
237 ".omp_offloading.descriptor_reg" + Suffix, &M);
238 Func->setSection(getStartupSection(M.getTargetTriple()));
244 M.getOrInsertFunction(
"__tgt_register_lib", RegFuncTy);
248 FunctionCallee AtExit = M.getOrInsertFunction(
"atexit", AtExitTy);
250 Function *UnregFunc = createUnregisterFunction(M, BinDesc, Suffix);
255 Builder.CreateCall(RegFuncC, BinDesc);
261 Builder.CreateCall(AtExit, UnregFunc);
262 Builder.CreateRetVoid();
294 IsHIP ? (
Triple.
isMacOSX() ?
"__HIP,__hip_fatbin" :
".hip_fatbin")
295 : (
Triple.isMacOSX() ?
"__NV_CUDA,__nv_fatbin" :
".nv_fatbin");
299 ".fatbin_image" + Suffix);
300 Fatbin->setSection(FatbinConstantSection);
304 IsHIP ? (
Triple.
isMacOSX() ?
"__HIP,__fatbin" :
".hipFatBinSegment")
305 : (
Triple.isMacOSX() ?
"__NV_CUDA,__fatbin" :
".nvFatBinSegment");
318 FatbinInitializer,
".fatbin_wrapper" + Suffix);
319 FatbinDesc->setSection(FatbinWrapperSection);
320 FatbinDesc->setAlignment(
Align(8));
321 FatbinDesc->setNoSanitizeMetadata();
355 bool EmitSurfacesAndTextures) {
357 auto [EntriesB, EntriesE] = EntryArray;
366 Int8PtrTy, Int8PtrTy, Int8PtrTy, Int8PtrTy, Int32PtrTy},
369 IsHIP ?
"__hipRegisterFunction" :
"__cudaRegisterFunction", RegFuncTy);
378 IsHIP ?
"__hipRegisterVar" :
"__cudaRegisterVar", RegVarTy);
383 {Int8PtrPtrTy, Int8PtrTy, Int8PtrTy, Int8PtrTy,
387 IsHIP ?
"__hipRegisterManagedVar" :
"__cudaRegisterManagedVar",
393 {Int8PtrPtrTy, Int8PtrTy, Int8PtrTy, Int8PtrTy,
397 IsHIP ?
"__hipRegisterSurface" :
"__cudaRegisterSurface", RegSurfaceTy);
406 IsHIP ?
"__hipRegisterTexture" :
"__cudaRegisterTexture", RegTextureTy);
412 IsHIP ?
".hip.globals_reg" :
".cuda.globals_reg", &M);
413 RegGlobalsFn->setSection(getStartupSection(M.getTargetTriple()));
428 auto *EntryCmp = Builder.CreateICmpNE(EntriesB, EntriesE);
429 Builder.CreateCondBr(EntryCmp, EntryBB, ExitBB);
430 Builder.SetInsertPoint(EntryBB);
436 auto *Addr = Builder.CreateLoad(Int8PtrTy, AddrPtr,
"addr");
441 auto *AuxAddr = Builder.CreateLoad(Int8PtrTy, AuxAddrPtr,
"aux_addr");
451 auto *
Name = Builder.CreateLoad(Int8PtrTy, NamePtr,
"name");
466 auto *
Data = Builder.CreateTrunc(
469 auto *
Type = Builder.CreateAnd(
473 auto *ExternBit = Builder.CreateAnd(
476 auto *
Extern = Builder.CreateLShr(
481 auto *
Const = Builder.CreateLShr(
483 auto *NormalizedBit = Builder.CreateAnd(
486 auto *Normalized = Builder.CreateLShr(
488 auto *KindCond = Builder.CreateICmpEQ(
492 Builder.CreateCondBr(KindCond, IfKindBB, IfEndBB);
493 Builder.SetInsertPoint(IfKindBB);
494 auto *FnCond = Builder.CreateICmpEQ(
496 Builder.CreateCondBr(FnCond, IfThenBB, IfElseBB);
499 Builder.SetInsertPoint(IfThenBB);
502 {RegGlobalsFn->arg_begin(), Addr,
Name,
Name,
507 Builder.CreateBr(IfEndBB);
508 Builder.SetInsertPoint(IfElseBB);
510 auto *
Switch = Builder.CreateSwitch(
Type, IfEndBB);
512 Builder.SetInsertPoint(SwGlobalBB);
513 Builder.CreateCall(RegVar,
516 Builder.CreateBr(IfEndBB);
521 Builder.SetInsertPoint(SwManagedBB);
522 Builder.CreateCall(RegManagedVar, {RegGlobalsFn->arg_begin(), AuxAddr, Addr,
524 Builder.CreateBr(IfEndBB);
528 Builder.SetInsertPoint(SwSurfaceBB);
529 if (EmitSurfacesAndTextures)
530 Builder.CreateCall(RegSurface, {RegGlobalsFn->arg_begin(), Addr,
Name,
Name,
532 Builder.CreateBr(IfEndBB);
537 Builder.SetInsertPoint(SwTextureBB);
538 if (EmitSurfacesAndTextures)
539 Builder.CreateCall(RegTexture, {RegGlobalsFn->arg_begin(), Addr,
Name,
Name,
541 Builder.CreateBr(IfEndBB);
545 Builder.SetInsertPoint(IfEndBB);
546 auto *NewEntry = Builder.CreateInBoundsGEP(
548 auto *
Cmp = Builder.CreateICmpEQ(NewEntry, EntriesE);
549 Entry->addIncoming(EntriesB, &RegGlobalsFn->getEntryBlock());
550 Entry->addIncoming(NewEntry, IfEndBB);
551 Builder.CreateCondBr(Cmp, ExitBB, EntryBB);
552 Builder.SetInsertPoint(ExitBB);
553 Builder.CreateRetVoid();
563 bool EmitSurfacesAndTextures) {
568 (IsHIP ?
".hip.fatbin_reg" :
".cuda.fatbin_reg") + Suffix, &M);
569 CtorFunc->setSection(getStartupSection(M.getTargetTriple()));
574 (IsHIP ?
".hip.fatbin_unreg" :
".cuda.fatbin_unreg") + Suffix, &M);
575 DtorFunc->setSection(getStartupSection(M.getTargetTriple()));
582 IsHIP ?
"__hipRegisterFatBinary" :
"__cudaRegisterFatBinary", RegFatTy);
587 M.getOrInsertFunction(
"__cudaRegisterFatBinaryEnd", RegFatEndTy);
592 IsHIP ?
"__hipUnregisterFatBinary" :
"__cudaUnregisterFatBinary",
597 FunctionCallee AtExit = M.getOrInsertFunction(
"atexit", AtExitTy);
602 (IsHIP ?
".hip.binary_handle" :
".cuda.binary_handle") + Suffix);
606 CallInst *Handle = CtorBuilder.CreateCall(
609 CtorBuilder.CreateAlignedStore(
610 Handle, BinaryHandleGlobal,
611 Align(M.getDataLayout().getPointerTypeSize(PtrTy)));
612 CtorBuilder.CreateCall(createRegisterGlobalsFunction(M, IsHIP, EntryArray,
614 EmitSurfacesAndTextures),
617 CtorBuilder.CreateCall(RegFatbinEnd, Handle);
618 CtorBuilder.CreateCall(AtExit, DtorFunc);
619 CtorBuilder.CreateRetVoid();
625 LoadInst *BinaryHandle = DtorBuilder.CreateAlignedLoad(
626 PtrTy, BinaryHandleGlobal,
627 Align(M.getDataLayout().getPointerTypeSize(PtrTy)));
628 DtorBuilder.CreateCall(UnregFatbin, BinaryHandle);
629 DtorBuilder.CreateRetVoid();
638 SYCLWrapper(
Module &M,
const SYCLJITOptions &Options,
bool IsFinalizedImage)
640 IsFinalizedImage(IsFinalizedImage) {}
645 std::pair<Constant *, Constant *> embedBinary(ArrayRef<char> Buffer) {
647 GlobalVariable *BinaryGV =
new GlobalVariable(
649 Arr,
".sycl_offloading.binary");
653 BinaryGV->
setSection(IsFinalizedImage ?
".sycl_fatbin"
654 :
".llvm.offloading");
656 IntegerType *Int64Ty = Type::getInt64Ty(C);
664 Function *createRegisterFatbinFunction(Constant *Start, Constant *
Size) {
665 FunctionType *FuncTy =
666 FunctionType::get(Type::getVoidTy(C),
false);
668 Twine(
"sycl") +
".descriptor_reg", &M);
669 Func->setSection(getStartupSection(M.getTargetTriple()));
672 IntegerType *Int64Ty = Type::getInt64Ty(C);
673 FunctionType *RegFuncTy =
674 FunctionType::get(Type::getVoidTy(C), {PtrTy, Int64Ty},
676 FunctionCallee RegFuncC =
677 M.getOrInsertFunction(
"__sycl_register_lib", RegFuncTy);
679 FunctionType *AtExitTy =
680 FunctionType::get(Type::getInt32Ty(C), PtrTy,
false);
681 FunctionCallee AtExit = M.getOrInsertFunction(
"atexit", AtExitTy);
683 Function *UnregFunc = createUnregisterFunction(Start,
Size);
686 Builder.CreateCall(RegFuncC, {
Start,
Size});
691 Builder.CreateCall(AtExit, UnregFunc);
692 Builder.CreateRetVoid();
698 Function *createUnregisterFunction(Constant *Start, Constant *
Size) {
699 FunctionType *FuncTy =
700 FunctionType::get(Type::getVoidTy(C),
false);
702 "sycl.descriptor_unreg", &M);
703 Func->setSection(getStartupSection(M.getTargetTriple()));
706 IntegerType *Int64Ty = Type::getInt64Ty(C);
707 FunctionType *UnRegFuncTy =
708 FunctionType::get(Type::getVoidTy(C), {PtrTy, Int64Ty},
710 FunctionCallee UnRegFuncC =
711 M.getOrInsertFunction(
"__sycl_unregister_lib", UnRegFuncTy);
714 Builder.CreateCall(UnRegFuncC, {
Start,
Size});
715 Builder.CreateRetVoid();
722 SYCLJITOptions Options;
723 bool IsFinalizedImage;
732 createBinDesc(M, Images, EntryArray, Suffix, Relocatable);
735 "No binary descriptors created.");
736 createRegisterFunction(M,
Desc, Suffix);
743 bool EmitSurfacesAndTextures) {
747 "No fatbin section created.");
749 createRegisterFatbinFunction(M,
Desc,
false, EntryArray, Suffix,
750 EmitSurfacesAndTextures);
756 bool EmitSurfacesAndTextures) {
760 "No fatbin section created.");
762 createRegisterFatbinFunction(M,
Desc,
true, EntryArray, Suffix,
763 EmitSurfacesAndTextures);
769 bool IsFinalizedImage,
771 SYCLWrapper W(M,
Options, IsFinalizedImage);
772 auto [Start,
Size] = W.embedBinary(Buffer);
773 Function *RegisterFunc = W.createRegisterFatbinFunction(Start,
Size);
774 if (RegistrationFunc) {
775 *RegistrationFunc = RegisterFunc;
static IntegerType * getSizeTTy(IRBuilderBase &B, const TargetLibraryInfo *TLI)
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Module.h This file contains the declarations for the Module class.
Func getContext().diagnose(DiagnosticInfoUnsupported(Func
This file defines the SmallVector class.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
Get the array size.
static LLVM_ABI ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
This class represents a function call, abstracting a target machine's calling convention.
static LLVM_ABI Constant * get(ArrayType *T, ArrayRef< Constant * > V)
static Constant * get(LLVMContext &Context, ArrayRef< ElementTy > Elts)
get() constructor - Return a constant with array type with an element count and element type matching...
static LLVM_ABI Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList, GEPNoWrapFlags NW=GEPNoWrapFlags::none(), std::optional< ConstantRange > InRange=std::nullopt, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
static LLVM_ABI ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
static LLVM_ABI Constant * get(StructType *T, ArrayRef< Constant * > V)
This is an important base class in LLVM.
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
static LLVM_ABI FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
LLVM_ABI void setSection(StringRef S)
Change the section for this global.
void setUnnamedAddr(UnnamedAddr Val)
@ InternalLinkage
Rename collisions when linking (static functions).
Type * getValueType() const
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Class to represent integer types.
This is an important class for using LLVM in a threaded context.
An instruction for reading from memory.
A Module instance is used to store all the information related to an LLVM module.
static PointerType * getUnqual(LLVMContext &C)
This constructs an opaque pointer to an object in the default address space (address space zero).
static LLVM_ABI PointerType * get(LLVMContext &C, unsigned AddressSpace)
This constructs an opaque pointer to an object in a numbered address space.
void reserve(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Class to represent struct types.
static LLVM_ABI StructType * getTypeByName(LLVMContext &C, StringRef Name)
Return the type with the specified name, or null if there is none by that name.
static LLVM_ABI StructType * create(LLVMContext &Context, StringRef Name)
This creates an identified struct.
Triple - Helper class for working with autoconf configuration names.
bool isMacOSX() const
Is this a Mac OS X triple.
The instances of the Type class are immutable: once they are created, they are never changed.
static LLVM_ABI IntegerType * getInt64Ty(LLVMContext &C)
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
static LLVM_ABI IntegerType * getInt16Ty(LLVMContext &C)
Type * getType() const
All values are typed, get the type of this value.
static uint64_t getAlignment()
@ Switch
The "resume-switch" lowering, where there are separate resume and destroy functions that are shared b...
LLVM_ABI StructType * getEntryTy(Module &M)
Returns the type of the offloading entry we use to store kernels and globals that will be registered ...
@ OffloadGlobalSurfaceEntry
Mark the entry as a surface variable.
@ OffloadGlobalTextureEntry
Mark the entry as a texture variable.
@ OffloadGlobalNormalized
Mark the entry as being a normalized surface.
@ OffloadGlobalEntry
Mark the entry as a global entry.
@ OffloadGlobalManagedEntry
Mark the entry as a managed global variable.
@ OffloadGlobalExtern
Mark the entry as being extern.
@ OffloadGlobalConstant
Mark the entry as being constant.
LLVM_ABI llvm::Error wrapSYCLBinaries(llvm::Module &M, llvm::ArrayRef< char > Buffer, SYCLJITOptions Options=SYCLJITOptions(), bool IsFinalizedImage=false, llvm::Function **RegistrationFunc=nullptr)
Wraps OffloadBinaries in the given Buffers into the module M as global symbols and registers the imag...
LLVM_ABI llvm::Error wrapOpenMPBinaries(llvm::Module &M, llvm::ArrayRef< llvm::ArrayRef< char > > Images, EntryArrayTy EntryArray, llvm::StringRef Suffix="", bool Relocatable=false)
Wraps the input device images into the module M as global symbols and registers the images with the O...
std::pair< Constant *, Constant * > EntryArrayTy
LLVM_ABI llvm::Error wrapHIPBinary(llvm::Module &M, llvm::ArrayRef< char > Images, EntryArrayTy EntryArray, llvm::StringRef Suffix="", bool EmitSurfacesAndTextures=true)
Wraps the input bundled image into the module M as global symbols and registers the images with the H...
LLVM_ABI llvm::Error wrapCudaBinary(llvm::Module &M, llvm::ArrayRef< char > Images, EntryArrayTy EntryArray, llvm::StringRef Suffix="", bool EmitSurfacesAndTextures=true)
Wraps the input fatbinary image into the module M as global symbols and registers the images with the...
NodeAddr< FuncNode * > Func
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI void appendToGlobalCtors(Module &M, Function *F, int Priority, Constant *Data=nullptr)
Append F to the list of global ctors of module M with the given Priority.
@ Extern
Replace returns with jump to thunk, don't emit thunk.
This struct is a compact representation of a valid (non-zero power of two) alignment.
@ offload_binary
LLVM offload object file.