23#include "llvm/ADT/StringRef.h"
24#include "llvm/Frontend/Offloading/Utility.h"
25#include "llvm/IR/BasicBlock.h"
26#include "llvm/IR/Constants.h"
27#include "llvm/IR/DerivedTypes.h"
28#include "llvm/IR/GlobalValue.h"
29#include "llvm/IR/ReplaceConstant.h"
30#include "llvm/ProfileData/InstrProf.h"
31#include "llvm/Support/Format.h"
32#include "llvm/Support/MD5.h"
33#include "llvm/Support/VirtualFileSystem.h"
34#include "llvm/Transforms/Utils/ModuleUtils.h"
40constexpr unsigned CudaFatMagic = 0x466243b1;
41constexpr unsigned HIPFatMagic = 0x48495046;
49 llvm::IntegerType *IntTy, *SizeTy;
51 llvm::PointerType *PtrTy;
54 llvm::LLVMContext &Context;
56 llvm::Module &TheModule;
59 llvm::Function *Kernel;
62 llvm::SmallVector<KernelInfo, 16> EmittedKernels;
66 llvm::DenseMap<StringRef, llvm::GlobalValue *> KernelHandles;
68 llvm::DenseMap<llvm::GlobalValue *, llvm::Function *> KernelStubs;
70 llvm::GlobalVariable *Var;
74 llvm::SmallVector<VarInfo, 16> DeviceVars;
78 llvm::GlobalVariable *GpuBinaryHandle =
nullptr;
83 llvm::GlobalVariable *OffloadProfShadow =
nullptr;
84 struct OffloadProfSectionShadowInfo {
85 llvm::GlobalVariable *Shadow;
86 std::string DeviceName;
88 llvm::SmallVector<OffloadProfSectionShadowInfo, 16> OffloadProfSectionShadows;
90 bool RelocatableDeviceCode;
92 std::unique_ptr<MangleContext> DeviceMC;
94 llvm::FunctionCallee getSetupArgumentFn()
const;
95 llvm::FunctionCallee getLaunchFn()
const;
97 llvm::FunctionType *getRegisterGlobalsFnTy()
const;
98 llvm::FunctionType *getCallbackFnTy()
const;
99 llvm::FunctionType *getRegisterLinkedBinaryFnTy()
const;
100 std::string addPrefixToName(StringRef FuncName)
const;
101 std::string addUnderscoredPrefixToName(StringRef FuncName)
const;
104 llvm::Function *makeRegisterGlobalsFn();
109 llvm::Constant *makeConstantString(
const std::string &Str,
110 const std::string &Name =
"") {
111 return CGM.GetAddrOfConstantCString(Str, Name).getPointer();
117 llvm::Constant *makeConstantArray(StringRef Str,
119 StringRef SectionName =
"",
120 unsigned Alignment = 0,
121 bool AddNull =
false) {
122 llvm::Constant *
Value =
123 llvm::ConstantDataArray::getString(Context, Str, AddNull);
124 auto *GV =
new llvm::GlobalVariable(
125 TheModule,
Value->getType(),
true,
126 llvm::GlobalValue::PrivateLinkage,
Value, Name);
127 if (!SectionName.empty()) {
128 GV->setSection(SectionName);
131 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::None);
134 GV->setAlignment(llvm::Align(Alignment));
139 llvm::Function *makeDummyFunction(llvm::FunctionType *FnTy) {
140 assert(FnTy->getReturnType()->isVoidTy() &&
141 "Can only generate dummy functions returning void!");
142 llvm::Function *DummyFunc = llvm::Function::Create(
143 FnTy, llvm::GlobalValue::InternalLinkage,
"dummy", &TheModule);
145 llvm::BasicBlock *DummyBlock =
146 llvm::BasicBlock::Create(Context,
"", DummyFunc);
147 CGBuilderTy FuncBuilder(CGM, Context);
148 FuncBuilder.SetInsertPoint(DummyBlock);
149 FuncBuilder.CreateRetVoid();
154 Address prepareKernelArgs(CodeGenFunction &CGF, FunctionArgList &Args);
155 Address prepareKernelArgsLLVMOffload(CodeGenFunction &CGF,
156 FunctionArgList &Args);
157 void emitDeviceStubBodyLegacy(CodeGenFunction &CGF, FunctionArgList &Args);
158 void emitDeviceStubBodyNew(CodeGenFunction &CGF, FunctionArgList &Args);
159 std::string getDeviceSideName(
const NamedDecl *ND)
override;
161 void registerDeviceVar(
const VarDecl *VD, llvm::GlobalVariable &Var,
163 DeviceVars.push_back({&Var,
165 {DeviceVarFlags::Variable, Extern,
Constant,
166 VD->hasAttr<HIPManagedAttr>(),
169 void registerDeviceSurf(
const VarDecl *VD, llvm::GlobalVariable &Var,
170 bool Extern,
int Type) {
171 DeviceVars.push_back({&Var,
173 {DeviceVarFlags::Surface, Extern,
false,
177 void registerDeviceTex(
const VarDecl *VD, llvm::GlobalVariable &Var,
178 bool Extern,
int Type,
bool Normalized) {
179 DeviceVars.push_back({&Var,
181 {DeviceVarFlags::Texture, Extern,
false,
182 false, Normalized,
Type}});
186 llvm::Function *makeModuleCtorFunction();
188 llvm::Function *makeModuleDtorFunction();
190 void transformManagedVars();
192 void createOffloadingEntries();
200 void emitOffloadProfilingSections();
203 CGNVCUDARuntime(CodeGenModule &CGM);
205 llvm::GlobalValue *getKernelHandle(llvm::Function *F, GlobalDecl GD)
override;
206 llvm::Function *getKernelStub(llvm::GlobalValue *Handle)
override {
207 auto Loc = KernelStubs.find(Handle);
208 assert(Loc != KernelStubs.end());
211 void emitDeviceStub(CodeGenFunction &CGF, FunctionArgList &Args)
override;
212 void handleVarRegistration(
const VarDecl *VD,
213 llvm::GlobalVariable &Var)
override;
215 internalizeDeviceSideVar(
const VarDecl *D,
216 llvm::GlobalValue::LinkageTypes &
Linkage)
override;
218 llvm::Function *finalizeModule()
override;
223std::string CGNVCUDARuntime::addPrefixToName(StringRef FuncName)
const {
224 return (Prefix + FuncName).str();
227CGNVCUDARuntime::addUnderscoredPrefixToName(StringRef FuncName)
const {
228 return (
"__" + Prefix + FuncName).str();
238 return std::unique_ptr<MangleContext>(
247CGNVCUDARuntime::CGNVCUDARuntime(CodeGenModule &CGM)
248 : CGCUDARuntime(CGM), Context(CGM.getLLVMContext()),
249 TheModule(CGM.getModule()),
250 RelocatableDeviceCode(CGM.getLangOpts().GPURelocatableDeviceCode),
265llvm::FunctionCallee CGNVCUDARuntime::getSetupArgumentFn()
const {
267 llvm::Type *Params[] = {PtrTy, SizeTy, SizeTy};
269 llvm::FunctionType::get(IntTy, Params,
false),
270 addPrefixToName(
"SetupArgument"));
273llvm::FunctionCallee CGNVCUDARuntime::getLaunchFn()
const {
277 llvm::FunctionType::get(IntTy, PtrTy,
false),
"hipLaunchByPtr");
284llvm::FunctionType *CGNVCUDARuntime::getRegisterGlobalsFnTy()
const {
285 return llvm::FunctionType::get(VoidTy, PtrTy,
false);
288llvm::FunctionType *CGNVCUDARuntime::getCallbackFnTy()
const {
289 return llvm::FunctionType::get(VoidTy, PtrTy,
false);
292llvm::FunctionType *CGNVCUDARuntime::getRegisterLinkedBinaryFnTy()
const {
293 llvm::Type *Params[] = {llvm::PointerType::getUnqual(Context), PtrTy, PtrTy,
294 llvm::PointerType::getUnqual(Context)};
295 return llvm::FunctionType::get(VoidTy, Params,
false);
298std::string CGNVCUDARuntime::getDeviceSideName(
const NamedDecl *ND) {
301 if (
auto *FD = dyn_cast<FunctionDecl>(ND))
302 GD = GlobalDecl(FD, KernelReferenceKind::Kernel);
305 std::string DeviceSideName;
312 SmallString<256> Buffer;
313 llvm::raw_svector_ostream
Out(Buffer);
315 DeviceSideName = std::string(
Out.str());
322 SmallString<256> Buffer;
323 llvm::raw_svector_ostream
Out(Buffer);
324 Out << DeviceSideName;
326 DeviceSideName = std::string(
Out.str());
328 return DeviceSideName;
331void CGNVCUDARuntime::emitDeviceStub(CodeGenFunction &CGF,
332 FunctionArgList &Args) {
335 dyn_cast<llvm::GlobalVariable>(KernelHandles[CGF.
CurFn->getName()])) {
336 GV->setLinkage(CGF.
CurFn->getLinkage());
337 GV->setInitializer(CGF.
CurFn);
340 CudaFeature::CUDA_USES_NEW_LAUNCH) ||
343 emitDeviceStubBodyNew(CGF, Args);
345 emitDeviceStubBodyLegacy(CGF, Args);
350Address CGNVCUDARuntime::prepareKernelArgsLLVMOffload(CodeGenFunction &CGF,
351 FunctionArgList &Args) {
352 SmallVector<llvm::Type *> ArgTypes, KernelLaunchParamsTypes;
353 for (
auto &Arg : Args)
355 llvm::StructType *KernelArgsTy = llvm::StructType::create(ArgTypes);
356 llvm::Type *KernelArgsPtrsTy = llvm::ArrayType::get(PtrTy, Args.size());
358 auto *Int32Ty = CGF.
Builder.getInt32Ty();
359 KernelLaunchParamsTypes.push_back(Int32Ty);
360 KernelLaunchParamsTypes.push_back(PtrTy);
362 llvm::StructType *KernelLaunchParamsTy =
363 llvm::StructType::create(KernelLaunchParamsTypes);
370 "kernel_launch_params");
377 for (
unsigned i = 0; i < Args.size(); ++i) {
385 return KernelLaunchParams;
388Address CGNVCUDARuntime::prepareKernelArgs(CodeGenFunction &CGF,
389 FunctionArgList &Args) {
395 llvm::ConstantInt::get(SizeTy, std::max<size_t>(1, Args.size())));
397 for (
unsigned i = 0; i < Args.size(); ++i) {
399 llvm::Value *VoidVarPtr = CGF.
Builder.CreatePointerCast(VarPtr, PtrTy);
401 VoidVarPtr, CGF.
Builder.CreateConstGEP1_32(
409void CGNVCUDARuntime::emitDeviceStubBodyNew(CodeGenFunction &CGF,
410 FunctionArgList &Args) {
413 ? prepareKernelArgsLLVMOffload(CGF, Args)
414 : prepareKernelArgs(CGF, Args);
428 TranslationUnitDecl *TUDecl = CGM.
getContext().getTranslationUnitDecl();
430 std::string KernelLaunchAPI =
"LaunchKernel";
432 LangOptions::GPUDefaultStreamKind::PerThread) {
434 KernelLaunchAPI = KernelLaunchAPI +
"_spt";
436 KernelLaunchAPI = KernelLaunchAPI +
"_ptsz";
438 auto LaunchKernelName = addPrefixToName(KernelLaunchAPI);
439 const IdentifierInfo &cudaLaunchKernelII =
441 FunctionDecl *cudaLaunchKernelFD =
nullptr;
443 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(
Result))
444 cudaLaunchKernelFD = FD;
447 if (cudaLaunchKernelFD ==
nullptr) {
449 "Can't find declaration for " + LaunchKernelName);
453 ParmVarDecl *GridDimParam = cudaLaunchKernelFD->
getParamDecl(1);
454 QualType Dim3Ty = GridDimParam->
getType();
464 llvm::FunctionType::get(IntTy,
470 addUnderscoredPrefixToName(
"PopCallConfiguration"));
479 CGF.
Builder.CreatePointerCast(KernelHandles[CGF.
CurFn->getName()], PtrTy);
480 CallArgList LaunchKernelArgs;
492 QualType QT = cudaLaunchKernelFD->
getType();
497 const CGFunctionInfo &FI =
499 llvm::FunctionCallee cudaLaunchKernelFn =
509 llvm::Function *KernelFunction = llvm::cast<llvm::Function>(
Kernel);
510 std::string GlobalVarName = (KernelFunction->getName() +
".id").str();
512 llvm::GlobalVariable *HandleVar =
513 CGM.
getModule().getNamedGlobal(GlobalVarName);
515 HandleVar =
new llvm::GlobalVariable(
517 false, KernelFunction->getLinkage(),
518 llvm::ConstantInt::get(CGM.
Int8Ty, 0), GlobalVarName);
519 HandleVar->setDSOLocal(KernelFunction->isDSOLocal());
520 HandleVar->setVisibility(KernelFunction->getVisibility());
521 if (KernelFunction->hasComdat())
522 HandleVar->setComdat(CGM.
getModule().getOrInsertComdat(GlobalVarName));
535void CGNVCUDARuntime::emitDeviceStubBodyLegacy(CodeGenFunction &CGF,
536 FunctionArgList &Args) {
538 llvm::FunctionCallee cudaSetupArgFn = getSetupArgumentFn();
541 for (
const VarDecl *A : Args) {
543 Offset = Offset.
alignTo(TInfo.Align);
544 llvm::Value *Args[] = {
547 llvm::ConstantInt::get(SizeTy, TInfo.Width.getQuantity()),
548 llvm::ConstantInt::get(SizeTy, Offset.
getQuantity()),
551 llvm::Constant *
Zero = llvm::ConstantInt::get(IntTy, 0);
552 llvm::Value *CBZero = CGF.
Builder.CreateICmpEQ(CB,
Zero);
554 CGF.
Builder.CreateCondBr(CBZero, NextBlock, EndBlock);
556 Offset += TInfo.Width;
560 llvm::FunctionCallee cudaLaunchFn = getLaunchFn();
562 CGF.
Builder.CreatePointerCast(KernelHandles[CGF.
CurFn->getName()], PtrTy);
572 llvm::GlobalVariable *ManagedVar) {
574 for (
auto &&VarUse : Var->uses()) {
575 WorkList.push_back({VarUse.getUser()});
577 while (!WorkList.empty()) {
578 auto &&WorkItem = WorkList.pop_back_val();
579 auto *
U = WorkItem.back();
581 for (
auto &&UU :
U->uses()) {
582 WorkItem.push_back(UU.getUser());
583 WorkList.push_back(WorkItem);
588 if (
auto *I = dyn_cast<llvm::Instruction>(
U)) {
589 llvm::Value *OldV = Var;
590 llvm::Instruction *NewV =
new llvm::LoadInst(
591 Var->getType(), ManagedVar,
"ld.managed",
false,
592 llvm::Align(Var->getAlignment()), I->getIterator());
596 for (
auto &&Op : WorkItem) {
598 auto *NewInst = CE->getAsInstruction();
599 NewInst->insertBefore(*I->getParent(), I->getIterator());
600 NewInst->replaceUsesOfWith(OldV, NewV);
604 I->replaceUsesOfWith(OldV, NewV);
606 llvm_unreachable(
"Invalid use of managed variable");
625llvm::Function *CGNVCUDARuntime::makeRegisterGlobalsFn() {
627 if (EmittedKernels.empty() && DeviceVars.empty())
630 llvm::Function *RegisterKernelsFunc = llvm::Function::Create(
631 getRegisterGlobalsFnTy(), llvm::GlobalValue::InternalLinkage,
632 addUnderscoredPrefixToName(
"_register_globals"), &TheModule);
633 llvm::BasicBlock *EntryBB =
634 llvm::BasicBlock::Create(Context,
"entry", RegisterKernelsFunc);
635 CGBuilderTy Builder(CGM, Context);
636 Builder.SetInsertPoint(EntryBB);
640 llvm::Type *RegisterFuncParams[] = {
641 PtrTy, PtrTy, PtrTy, PtrTy, IntTy,
642 PtrTy, PtrTy, PtrTy, PtrTy, llvm::PointerType::getUnqual(Context)};
644 llvm::FunctionType::get(IntTy, RegisterFuncParams,
false),
645 addUnderscoredPrefixToName(
"RegisterFunction"));
650 llvm::Argument &GpuBinaryHandlePtr = *RegisterKernelsFunc->arg_begin();
651 for (
auto &&I : EmittedKernels) {
652 llvm::Constant *KernelName =
654 llvm::Constant *NullPtr = llvm::ConstantPointerNull::get(PtrTy);
655 llvm::Value *Args[] = {
657 KernelHandles[I.Kernel->getName()],
660 llvm::ConstantInt::getAllOnesValue(IntTy),
665 llvm::ConstantPointerNull::get(llvm::PointerType::getUnqual(Context))};
666 Builder.CreateCall(RegisterFunc, Args);
669 llvm::Type *VarSizeTy = IntTy;
677 llvm::Type *RegisterVarParams[] = {PtrTy, PtrTy, PtrTy, PtrTy,
678 IntTy, VarSizeTy, IntTy, IntTy};
680 llvm::FunctionType::get(VoidTy, RegisterVarParams,
false),
681 addUnderscoredPrefixToName(
"RegisterVar"));
684 llvm::Type *RegisterManagedVarParams[] = {PtrTy, PtrTy, PtrTy,
685 PtrTy, VarSizeTy, IntTy};
687 llvm::FunctionType::get(VoidTy, RegisterManagedVarParams,
false),
688 addUnderscoredPrefixToName(
"RegisterManagedVar"));
692 llvm::FunctionType::get(
693 VoidTy, {PtrTy, PtrTy, PtrTy, PtrTy, IntTy, IntTy},
false),
694 addUnderscoredPrefixToName(
"RegisterSurface"));
698 llvm::FunctionType::get(
699 VoidTy, {PtrTy, PtrTy, PtrTy, PtrTy, IntTy, IntTy, IntTy},
false),
700 addUnderscoredPrefixToName(
"RegisterTexture"));
701 for (
auto &&Info : DeviceVars) {
702 llvm::GlobalVariable *Var = Info.Var;
703 assert((!Var->isDeclaration() || Info.Flags.isManaged()) &&
704 "External variables should not show up here, except HIP managed "
706 llvm::Constant *VarName = makeConstantString(getDeviceSideName(Info.D));
707 switch (Info.Flags.getKind()) {
708 case DeviceVarFlags::Variable: {
711 if (Info.Flags.isManaged()) {
712 assert(Var->getName().ends_with(
".managed") &&
713 "HIP managed variables not transformed");
714 auto *ManagedVar = CGM.
getModule().getNamedGlobal(
715 Var->getName().drop_back(StringRef(
".managed").size()));
716 llvm::Value *Args[] = {
721 llvm::ConstantInt::get(VarSizeTy, VarSize),
722 llvm::ConstantInt::get(IntTy, Var->getAlignment())};
723 if (!Var->isDeclaration())
724 Builder.CreateCall(RegisterManagedVar, Args);
726 llvm::Value *Args[] = {
731 llvm::ConstantInt::get(IntTy, Info.Flags.isExtern()),
732 llvm::ConstantInt::get(VarSizeTy, VarSize),
733 llvm::ConstantInt::get(IntTy, Info.Flags.isConstant()),
734 llvm::ConstantInt::get(IntTy, 0)};
735 Builder.CreateCall(RegisterVar, Args);
739 case DeviceVarFlags::Surface:
742 {&GpuBinaryHandlePtr, Var, VarName, VarName,
743 llvm::ConstantInt::get(IntTy, Info.Flags.getSurfTexType()),
744 llvm::ConstantInt::get(IntTy, Info.Flags.isExtern())});
746 case DeviceVarFlags::Texture:
749 {&GpuBinaryHandlePtr, Var, VarName, VarName,
750 llvm::ConstantInt::get(IntTy, Info.Flags.getSurfTexType()),
751 llvm::ConstantInt::get(IntTy, Info.Flags.isNormalized()),
752 llvm::ConstantInt::get(IntTy, Info.Flags.isExtern())});
763 if (OffloadProfShadow) {
764 llvm::Constant *Name =
765 makeConstantString(std::string(OffloadProfShadow->getName()));
766 llvm::Constant *IntZero = llvm::ConstantInt::get(IntTy, 0);
767 llvm::Value *RegisterVarArgs[] = {
773 llvm::ConstantInt::get(VarSizeTy,
777 Builder.CreateCall(RegisterVar, RegisterVarArgs);
780 llvm::FunctionType::get(VoidTy, {PtrTy},
false),
781 "__llvm_profile_offload_register_shadow_variable");
782 Builder.CreateCall(RegisterShadow, {OffloadProfShadow});
785 if (!OffloadProfSectionShadows.empty()) {
787 llvm::FunctionType::get(VoidTy, {PtrTy},
false),
788 "__llvm_profile_offload_register_section_shadow_variable");
789 llvm::Constant *IntZero = llvm::ConstantInt::get(IntTy, 0);
790 for (
const auto &Info : OffloadProfSectionShadows) {
791 llvm::Constant *Name = makeConstantString(Info.DeviceName);
792 llvm::Value *RegisterVarArgs[] = {
798 llvm::ConstantInt::get(VarSizeTy,
802 Builder.CreateCall(RegisterVar, RegisterVarArgs);
803 Builder.CreateCall(RegisterSectionShadow, {Info.Shadow});
807 Builder.CreateRetVoid();
808 return RegisterKernelsFunc;
830llvm::Function *CGNVCUDARuntime::makeModuleCtorFunction() {
835 if (CudaGpuBinaryFileName.empty() && !IsHIP)
837 if ((IsHIP || (IsCUDA && !RelocatableDeviceCode)) && EmittedKernels.empty() &&
842 llvm::Function *RegisterGlobalsFunc = makeRegisterGlobalsFn();
845 if (RelocatableDeviceCode && !RegisterGlobalsFunc)
846 RegisterGlobalsFunc = makeDummyFunction(getRegisterGlobalsFnTy());
850 llvm::FunctionType::get(PtrTy, PtrTy,
false),
851 addUnderscoredPrefixToName(
"RegisterFatBinary"));
853 llvm::StructType *FatbinWrapperTy =
854 llvm::StructType::get(IntTy, IntTy, PtrTy, PtrTy);
860 std::unique_ptr<llvm::MemoryBuffer> CudaGpuBinary =
nullptr;
861 if (!CudaGpuBinaryFileName.empty()) {
863 auto CudaGpuBinaryOrErr =
864 VFS->getBufferForFile(CudaGpuBinaryFileName, -1,
false);
865 if (std::error_code EC = CudaGpuBinaryOrErr.getError()) {
867 << CudaGpuBinaryFileName << EC.message();
870 CudaGpuBinary = std::move(CudaGpuBinaryOrErr.get());
873 llvm::Function *ModuleCtorFunc = llvm::Function::Create(
874 llvm::FunctionType::get(VoidTy,
false),
875 llvm::GlobalValue::InternalLinkage,
876 addUnderscoredPrefixToName(
"_module_ctor"), &TheModule);
877 llvm::BasicBlock *CtorEntryBB =
878 llvm::BasicBlock::Create(Context,
"entry", ModuleCtorFunc);
879 CGBuilderTy CtorBuilder(CGM, Context);
881 CtorBuilder.SetInsertPoint(CtorEntryBB);
883 const char *FatbinConstantName;
884 const char *FatbinSectionName;
885 const char *ModuleIDSectionName;
886 StringRef ModuleIDPrefix;
887 llvm::Constant *FatBinStr;
892 CGM.
getTriple().isMacOSX() ?
"__HIP,__hip_fatbin" :
".hip_fatbin";
894 CGM.
getTriple().isMacOSX() ?
"__HIP,__fatbin" :
".hipFatBinSegment";
896 ModuleIDSectionName =
897 CGM.
getTriple().isMacOSX() ?
"__HIP,__module_id" :
"__hip_module_id";
898 ModuleIDPrefix =
"__hip_";
903 const unsigned HIPCodeObjectAlign = 4096;
904 FatBinStr = makeConstantArray(std::string(CudaGpuBinary->getBuffer()),
"",
905 FatbinConstantName, HIPCodeObjectAlign);
911 FatBinStr =
new llvm::GlobalVariable(
913 true, llvm::GlobalValue::ExternalLinkage,
nullptr,
917 nullptr, llvm::GlobalVariable::NotThreadLocal);
921 FatMagic = HIPFatMagic;
923 if (RelocatableDeviceCode)
924 FatbinConstantName = CGM.
getTriple().isMacOSX()
925 ?
"__NV_CUDA,__nv_relfatbin"
929 CGM.
getTriple().isMacOSX() ?
"__NV_CUDA,__nv_fatbin" :
".nv_fatbin";
932 CGM.
getTriple().isMacOSX() ?
"__NV_CUDA,__fatbin" :
".nvFatBinSegment";
934 ModuleIDSectionName = CGM.
getTriple().isMacOSX()
935 ?
"__NV_CUDA,__nv_module_id"
937 ModuleIDPrefix =
"__nv_";
941 FatBinStr = makeConstantArray(std::string(CudaGpuBinary->getBuffer()),
"",
942 FatbinConstantName, 8);
943 FatMagic = CudaFatMagic;
947 ConstantInitBuilder Builder(CGM);
948 auto Values = Builder.beginStruct(FatbinWrapperTy);
950 Values.addInt(IntTy, FatMagic);
952 Values.addInt(IntTy, 1);
954 Values.add(FatBinStr);
956 Values.add(llvm::ConstantPointerNull::get(PtrTy));
957 llvm::GlobalVariable *FatbinWrapper = Values.finishAndCreateGlobal(
960 FatbinWrapper->setSection(FatbinSectionName);
971 auto Linkage = RelocatableDeviceCode ? llvm::GlobalValue::ExternalLinkage
972 : llvm::GlobalValue::InternalLinkage;
973 llvm::BasicBlock *IfBlock =
974 llvm::BasicBlock::Create(Context,
"if", ModuleCtorFunc);
975 llvm::BasicBlock *ExitBlock =
976 llvm::BasicBlock::Create(Context,
"exit", ModuleCtorFunc);
979 GpuBinaryHandle =
new llvm::GlobalVariable(
980 TheModule, PtrTy,
false,
Linkage,
982 !RelocatableDeviceCode ? llvm::ConstantPointerNull::get(PtrTy)
989 if (
Linkage != llvm::GlobalValue::InternalLinkage)
990 GpuBinaryHandle->setVisibility(llvm::GlobalValue::HiddenVisibility);
992 GpuBinaryHandle, PtrTy,
995 auto *HandleValue = CtorBuilder.CreateLoad(GpuBinaryAddr);
996 llvm::Constant *
Zero =
997 llvm::Constant::getNullValue(HandleValue->getType());
998 llvm::Value *EQZero = CtorBuilder.CreateICmpEQ(HandleValue,
Zero);
999 CtorBuilder.CreateCondBr(EQZero, IfBlock, ExitBlock);
1002 CtorBuilder.SetInsertPoint(IfBlock);
1004 llvm::CallInst *RegisterFatbinCall =
1005 CtorBuilder.CreateCall(RegisterFatbinFunc, FatbinWrapper);
1006 CtorBuilder.CreateStore(RegisterFatbinCall, GpuBinaryAddr);
1007 CtorBuilder.CreateBr(ExitBlock);
1010 CtorBuilder.SetInsertPoint(ExitBlock);
1012 if (RegisterGlobalsFunc) {
1013 auto *HandleValue = CtorBuilder.CreateLoad(GpuBinaryAddr);
1014 CtorBuilder.CreateCall(RegisterGlobalsFunc, HandleValue);
1017 }
else if (!RelocatableDeviceCode) {
1021 llvm::CallInst *RegisterFatbinCall =
1022 CtorBuilder.CreateCall(RegisterFatbinFunc, FatbinWrapper);
1023 GpuBinaryHandle =
new llvm::GlobalVariable(
1024 TheModule, PtrTy,
false, llvm::GlobalValue::InternalLinkage,
1025 llvm::ConstantPointerNull::get(PtrTy),
"__cuda_gpubin_handle");
1027 CtorBuilder.CreateAlignedStore(RegisterFatbinCall, GpuBinaryHandle,
1031 if (RegisterGlobalsFunc)
1032 CtorBuilder.CreateCall(RegisterGlobalsFunc, RegisterFatbinCall);
1036 CudaFeature::CUDA_USES_FATBIN_REGISTER_END)) {
1039 llvm::FunctionType::get(VoidTy, PtrTy,
false),
1040 "__cudaRegisterFatBinaryEnd");
1041 CtorBuilder.CreateCall(RegisterFatbinEndFunc, RegisterFatbinCall);
1053 SmallString<64> ModuleID;
1054 llvm::raw_svector_ostream
OS(ModuleID);
1055 OS << ModuleIDPrefix
1056 << llvm::format(
"%" PRIx64,
1057 llvm::MD5Hash(TheModule.getSourceFileName()));
1058 llvm::Constant *ModuleIDConstant = makeConstantArray(
1059 std::string(ModuleID),
"", ModuleIDSectionName, 32,
true);
1062 llvm::GlobalAlias::create(llvm::GlobalValue::ExternalLinkage,
1063 Twine(
"__fatbinwrap") + ModuleID, FatbinWrapper);
1067 SmallString<128> RegisterLinkedBinaryName(
"__cudaRegisterLinkedBinary");
1068 RegisterLinkedBinaryName += ModuleID;
1070 getRegisterLinkedBinaryFnTy(), RegisterLinkedBinaryName);
1072 assert(RegisterGlobalsFunc &&
"Expecting at least dummy function!");
1073 llvm::Value *Args[] = {RegisterGlobalsFunc, FatbinWrapper, ModuleIDConstant,
1074 makeDummyFunction(getCallbackFnTy())};
1075 CtorBuilder.CreateCall(RegisterLinkedBinaryFunc, Args);
1081 if (llvm::Function *CleanupFn = makeModuleDtorFunction()) {
1083 llvm::FunctionType *AtExitTy =
1084 llvm::FunctionType::get(IntTy, CleanupFn->getType(),
false);
1085 llvm::FunctionCallee AtExitFunc =
1088 CtorBuilder.CreateCall(AtExitFunc, CleanupFn);
1091 CtorBuilder.CreateRetVoid();
1092 return ModuleCtorFunc;
1114llvm::Function *CGNVCUDARuntime::makeModuleDtorFunction() {
1116 if (!GpuBinaryHandle)
1121 llvm::FunctionType::get(VoidTy, PtrTy,
false),
1122 addUnderscoredPrefixToName(
"UnregisterFatBinary"));
1124 llvm::Function *ModuleDtorFunc = llvm::Function::Create(
1125 llvm::FunctionType::get(VoidTy,
false),
1126 llvm::GlobalValue::InternalLinkage,
1127 addUnderscoredPrefixToName(
"_module_dtor"), &TheModule);
1129 llvm::BasicBlock *DtorEntryBB =
1130 llvm::BasicBlock::Create(Context,
"entry", ModuleDtorFunc);
1131 CGBuilderTy DtorBuilder(CGM, Context);
1132 DtorBuilder.SetInsertPoint(DtorEntryBB);
1135 GpuBinaryHandle, GpuBinaryHandle->getValueType(),
1137 auto *HandleValue = DtorBuilder.CreateLoad(GpuBinaryAddr);
1142 llvm::BasicBlock *IfBlock =
1143 llvm::BasicBlock::Create(Context,
"if", ModuleDtorFunc);
1144 llvm::BasicBlock *ExitBlock =
1145 llvm::BasicBlock::Create(Context,
"exit", ModuleDtorFunc);
1146 llvm::Constant *
Zero = llvm::Constant::getNullValue(HandleValue->getType());
1147 llvm::Value *NEZero = DtorBuilder.CreateICmpNE(HandleValue,
Zero);
1148 DtorBuilder.CreateCondBr(NEZero, IfBlock, ExitBlock);
1150 DtorBuilder.SetInsertPoint(IfBlock);
1151 DtorBuilder.CreateCall(UnregisterFatbinFunc, HandleValue);
1152 DtorBuilder.CreateStore(
Zero, GpuBinaryAddr);
1153 DtorBuilder.CreateBr(ExitBlock);
1155 DtorBuilder.SetInsertPoint(ExitBlock);
1157 DtorBuilder.CreateCall(UnregisterFatbinFunc, HandleValue);
1159 DtorBuilder.CreateRetVoid();
1160 return ModuleDtorFunc;
1164 return new CGNVCUDARuntime(CGM);
1167void CGNVCUDARuntime::internalizeDeviceSideVar(
1184 if (D->
hasAttr<CUDADeviceAttr>() || D->
hasAttr<CUDAConstantAttr>() ||
1185 D->
hasAttr<CUDASharedAttr>() ||
1188 Linkage = llvm::GlobalValue::InternalLinkage;
1192void CGNVCUDARuntime::handleVarRegistration(
const VarDecl *D,
1193 llvm::GlobalVariable &GV) {
1194 if (D->
hasAttr<CUDADeviceAttr>() || D->
hasAttr<CUDAConstantAttr>()) {
1210 D->
hasAttr<HIPManagedAttr>()) {
1212 D->
hasAttr<CUDAConstantAttr>());
1220 const TemplateArgumentList &Args = TD->getTemplateArgs();
1221 if (TD->hasAttr<CUDADeviceBuiltinSurfaceTypeAttr>()) {
1222 assert(Args.
size() == 2 &&
1223 "Unexpected number of template arguments of CUDA device "
1224 "builtin surface type.");
1225 auto SurfType = Args[1].getAsIntegral();
1227 registerDeviceSurf(D, GV, !D->
hasDefinition(), SurfType.getSExtValue());
1229 assert(Args.
size() == 3 &&
1230 "Unexpected number of template arguments of CUDA device "
1231 "builtin texture type.");
1232 auto TexType = Args[1].getAsIntegral();
1233 auto Normalized = Args[2].getAsIntegral();
1235 registerDeviceTex(D, GV, !D->
hasDefinition(), TexType.getSExtValue(),
1236 Normalized.getZExtValue());
1245void CGNVCUDARuntime::transformManagedVars() {
1246 for (
auto &&Info : DeviceVars) {
1247 llvm::GlobalVariable *Var = Info.Var;
1248 if (Info.Flags.getKind() == DeviceVarFlags::Variable &&
1249 Info.Flags.isManaged()) {
1250 auto *ManagedVar =
new llvm::GlobalVariable(
1252 false, Var->getLinkage(),
1253 Var->isDeclaration()
1255 : llvm::ConstantPointerNull::get(Var->getType()),
1257 llvm::GlobalVariable::NotThreadLocal,
1259 ? LangAS::cuda_device
1260 : LangAS::Default));
1261 ManagedVar->setDSOLocal(Var->isDSOLocal());
1262 ManagedVar->setVisibility(Var->getVisibility());
1263 ManagedVar->setExternallyInitialized(
true);
1265 ManagedVar->takeName(Var);
1266 Var->setName(Twine(ManagedVar->getName()) +
".managed");
1269 if (CGM.
getLangOpts().CUDAIsDevice && !Var->isDeclaration()) {
1270 assert(!ManagedVar->isDeclaration());
1281void CGNVCUDARuntime::createOffloadingEntries() {
1283 ? llvm::object::OffloadKind::OFK_HIP
1284 : llvm::object::OffloadKind::OFK_Cuda;
1287 Kind = llvm::object::OffloadKind::OFK_OpenMP;
1290 for (KernelInfo &I : EmittedKernels)
1291 llvm::offloading::emitOffloadingEntry(
1292 M, Kind, KernelHandles[I.Kernel->getName()],
1294 llvm::offloading::OffloadGlobalEntry);
1296 for (VarInfo &I : DeviceVars) {
1298 CGM.
getDataLayout().getTypeAllocSize(I.Var->getValueType());
1301 ?
static_cast<int32_t>(llvm::offloading::OffloadGlobalExtern)
1303 (I.Flags.isConstant()
1304 ?
static_cast<int32_t>(llvm::offloading::OffloadGlobalConstant)
1306 (I.Flags.isNormalized()
1307 ?
static_cast<int32_t>(llvm::offloading::OffloadGlobalNormalized)
1309 if (I.Flags.getKind() == DeviceVarFlags::Variable) {
1310 if (I.Flags.isManaged()) {
1311 assert(I.Var->getName().ends_with(
".managed") &&
1312 "HIP managed variables not transformed");
1314 auto *ManagedVar = M.getNamedGlobal(
1315 I.Var->getName().drop_back(StringRef(
".managed").size()));
1316 llvm::offloading::emitOffloadingEntry(
1317 M, Kind, I.Var, getDeviceSideName(I.D), VarSize,
1318 llvm::offloading::OffloadGlobalManagedEntry | Flags,
1319 I.Var->getAlignment(), ManagedVar);
1321 llvm::offloading::emitOffloadingEntry(
1322 M, Kind, I.Var, getDeviceSideName(I.D), VarSize,
1323 llvm::offloading::OffloadGlobalEntry | Flags,
1326 }
else if (I.Flags.getKind() == DeviceVarFlags::Surface) {
1327 llvm::offloading::emitOffloadingEntry(
1328 M, Kind, I.Var, getDeviceSideName(I.D), VarSize,
1329 llvm::offloading::OffloadGlobalSurfaceEntry | Flags,
1330 I.Flags.getSurfTexType());
1331 }
else if (I.Flags.getKind() == DeviceVarFlags::Texture) {
1332 llvm::offloading::emitOffloadingEntry(
1333 M, Kind, I.Var, getDeviceSideName(I.D), VarSize,
1334 llvm::offloading::OffloadGlobalTextureEntry | Flags,
1335 I.Flags.getSurfTexType());
1343 if (OffloadProfShadow) {
1344 llvm::offloading::emitOffloadingEntry(
1345 M, Kind, OffloadProfShadow, OffloadProfShadow->getName(),
1347 llvm::offloading::OffloadGlobalEntry, 0);
1349 llvm::LLVMContext &Ctx = M.getContext();
1350 auto *PtrTy = llvm::PointerType::getUnqual(Ctx);
1352 llvm::FunctionType::get(VoidTy, {PtrTy},
false),
1353 "__llvm_profile_offload_register_shadow_variable");
1355 llvm::FunctionType::get(VoidTy, {PtrTy},
false),
1356 "__llvm_profile_offload_register_section_shadow_variable");
1357 auto *CtorFn = llvm::Function::Create(
1358 llvm::FunctionType::get(VoidTy,
false),
1359 llvm::GlobalValue::InternalLinkage,
1361 auto *Entry = llvm::BasicBlock::Create(Ctx,
"entry", CtorFn);
1362 llvm::IRBuilder<> B(Entry);
1363 B.CreateCall(RegisterShadow, {OffloadProfShadow});
1364 for (
const auto &Info : OffloadProfSectionShadows) {
1365 llvm::offloading::emitOffloadingEntry(
1366 M, Kind, Info.Shadow, Info.DeviceName,
1368 llvm::offloading::OffloadGlobalEntry, 0);
1369 B.CreateCall(RegisterSectionShadow, {Info.Shadow});
1372 llvm::appendToGlobalCtors(M, CtorFn, 65535);
1379void CGNVCUDARuntime::emitOffloadProfilingSections() {
1386 if (CUIDHash.empty())
1390 llvm::LLVMContext &Ctx = M.getContext();
1391 std::string Name = (
"__llvm_profile_sections_" + CUIDHash).str();
1395 if (M.getNamedValue(Name))
1402 unsigned GlobalAS = M.getDataLayout().getDefaultGlobalsAddressSpace();
1403 std::string NamesVarPostfixVarName =
1404 std::string(llvm::getInstrProfNamesVarPostfixVarName());
1405 if (!M.getNamedValue(NamesVarPostfixVarName)) {
1406 auto *NamesVarPostfix = llvm::ConstantDataArray::getString(
1407 Ctx, (llvm::Twine(
"_") + CUIDHash).str(),
true);
1408 auto *NamesGV =
new llvm::GlobalVariable(
1409 M, NamesVarPostfix->getType(),
true,
1410 llvm::GlobalValue::PrivateLinkage, NamesVarPostfix,
1411 NamesVarPostfixVarName,
1412 nullptr, llvm::GlobalValue::NotThreadLocal,
1424 auto *PtrTy = llvm::PointerType::getUnqual(Ctx);
1425 OffloadProfShadow =
new llvm::GlobalVariable(
1426 M, PtrTy,
false, llvm::GlobalValue::ExternalLinkage,
1427 llvm::ConstantPointerNull::get(PtrTy), Name);
1430 auto AddSectionShadow = [&](StringRef
Kind,
const Twine &DeviceName) {
1431 std::string ShadowName =
1432 (Twine(
"__llvm_profile_shadow_") +
Kind +
"_" + CUIDHash +
"_" +
1433 Twine(OffloadProfSectionShadows.size()))
1435 auto *Shadow =
new llvm::GlobalVariable(
1436 M, PtrTy,
false, llvm::GlobalValue::ExternalLinkage,
1437 llvm::ConstantPointerNull::get(PtrTy), ShadowName);
1439 OffloadProfSectionShadows.push_back({Shadow, DeviceName.str()});
1444 for (
auto &&I : EmittedKernels) {
1446 AddSectionShadow(
"data", Twine(
"__profd_") + KernelName);
1447 AddSectionShadow(
"cnts", Twine(
"__profc_") + KernelName);
1448 AddSectionShadow(
"ucnts", Twine(
"__llvm_prf_unifcnt_") + KernelName);
1449 AddSectionShadow(
"names",
1450 Twine(llvm::getInstrProfNamesVarName()) +
"_" + CUIDHash);
1455llvm::Function *CGNVCUDARuntime::finalizeModule() {
1456 transformManagedVars();
1457 emitOffloadProfilingSections();
1469 for (
auto &&Info : DeviceVars) {
1470 auto Kind = Info.Flags.getKind();
1471 if (!Info.Var->isDeclaration() &&
1472 !llvm::GlobalValue::isLocalLinkage(Info.Var->getLinkage()) &&
1473 (Kind == DeviceVarFlags::Variable ||
1474 Kind == DeviceVarFlags::Surface ||
1475 Kind == DeviceVarFlags::Texture) &&
1476 Info.D->isUsed() && !Info.D->hasAttr<UsedAttr>()) {
1483 (CGM.
getLangOpts().OffloadingNewDriver && RelocatableDeviceCode))
1484 createOffloadingEntries();
1486 return makeModuleCtorFunction();
1491llvm::GlobalValue *CGNVCUDARuntime::getKernelHandle(llvm::Function *F,
1493 auto Loc = KernelHandles.find(F->getName());
1494 if (Loc != KernelHandles.end()) {
1495 auto OldHandle = Loc->second;
1496 if (KernelStubs[OldHandle] == F)
1504 KernelStubs[OldHandle] = F;
1509 KernelStubs.erase(OldHandle);
1513 KernelHandles[F->getName()] = F;
1518 auto *Var =
new llvm::GlobalVariable(
1519 TheModule, F->getType(),
true, F->getLinkage(),
1524 Var->setDSOLocal(F->isDSOLocal());
1525 Var->setVisibility(F->getVisibility());
1527 auto *FT = FD->getPrimaryTemplate();
1528 if (!FT || FT->isThisDeclarationADefinition())
1530 KernelHandles[F->getName()] = Var;
1531 KernelStubs[Var] = F;
static std::unique_ptr< MangleContext > InitDeviceMC(CodeGenModule &CGM)
static void replaceManagedVar(llvm::GlobalVariable *Var, llvm::GlobalVariable *ManagedVar)
Result
Implement __builtin_bit_cast and related operations.
MangleContext * createMangleContext(const TargetInfo *T=nullptr)
If T is null pointer, assume the target in ASTContext.
bool shouldExternalize(const Decl *D) const
Whether a C++ static variable or CUDA/HIP kernel should be externalized.
StringRef getCUIDHash() const
llvm::SetVector< const VarDecl * > CUDADeviceVarODRUsedByHost
Keep track of CUDA/HIP device-side variables ODR-used by host code.
const TargetInfo * getAuxTargetInfo() const
MangleContext * createDeviceMangleContext(const TargetInfo &T)
Creates a device mangle context to correctly mangle lambdas in a mixed architecture compile by settin...
TypeInfoChars getTypeInfoInChars(const Type *T) const
const TargetInfo & getTargetInfo() const
unsigned getTargetAddressSpace(LangAS AS) const
llvm::Align getAsAlign() const
getAsAlign - Returns Quantity as a valid llvm::Align, Beware llvm::Align assumes power of two 8-bit b...
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
static CharUnits One()
One - Construct a CharUnits quantity of one.
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
CharUnits alignTo(const CharUnits &Align) const
alignTo - Returns the next integer (mod 2**64) that is greater than or equal to this quantity and is ...
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
bool hasProfileInstr() const
Check if any form of instrumentation is on.
std::string CudaGpuBinaryFileName
Name of file passed with -fcuda-include-gpubinary option to forward to CUDA runtime back-end for inco...
llvm::Value * emitRawPointer(CodeGenFunction &CGF) const
Return the pointer contained in this class after authenticating it and adding offset to it if necessa...
llvm::PointerType * getType() const
Return the type of the pointer value.
llvm::StoreInst * CreateStore(llvm::Value *Val, Address Addr, bool IsVolatile=false)
llvm::StoreInst * CreateAlignedStore(llvm::Value *Val, llvm::Value *Addr, CharUnits Align, bool IsVolatile=false)
Address CreateConstArrayGEP(Address Addr, uint64_t Index, const llvm::Twine &Name="")
Given addr = [n x T]* ... produce name = getelementptr inbounds addr, i64 0, i64 index where i64 is a...
llvm::StoreInst * CreateDefaultAlignedStore(llvm::Value *Val, llvm::Value *Addr, bool IsVolatile=false)
Address CreateStructGEP(Address Addr, unsigned Index, const llvm::Twine &Name="")
llvm::LoadInst * CreateLoad(Address Addr, const llvm::Twine &Name="")
MangleContext & getMangleContext()
Gets the mangle context.
static CGCallee forDirect(llvm::Constant *functionPtr, const CGCalleeInfo &abstractInfo=CGCalleeInfo())
void add(RValue rvalue, QualType type)
llvm::CallBase * EmitRuntimeCallOrInvoke(llvm::FunctionCallee callee, ArrayRef< llvm::Value * > args, const Twine &name="")
Emits a call or invoke instruction to the given runtime function.
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
const LangOptions & getLangOpts() const
llvm::AllocaInst * CreateTempAlloca(llvm::Type *Ty, const Twine &Name="tmp", llvm::Value *ArraySize=nullptr)
CreateTempAlloca - This creates an alloca and inserts it into the entry block if ArraySize is nullptr...
RValue EmitCall(const CGFunctionInfo &CallInfo, const CGCallee &Callee, ReturnValueSlot ReturnValue, const CallArgList &Args, llvm::CallBase **CallOrInvoke, bool IsMustTail, SourceLocation Loc, bool IsVirtualFunctionPointerThunk=false)
EmitCall - Generate a call of the given function, expecting the given result type,...
RawAddress CreateMemTempWithoutCast(QualType T, const Twine &Name="tmp")
CreateMemTemp - Create a temporary memory object of the given type, with appropriate alignmen without...
RawAddress CreateTempAllocaWithoutCast(llvm::Type *Ty, CharUnits align, const Twine &Name="tmp", llvm::Value *ArraySize=nullptr)
CreateTempAlloca - This creates a alloca and inserts it into the entry block.
const Decl * CurFuncDecl
CurFuncDecl - Holds the Decl for the current outermost non-closure context.
llvm::Type * ConvertTypeForMem(QualType T)
void EmitBranch(llvm::BasicBlock *Block)
EmitBranch - Emit a branch to the specified basic block from the current insert block,...
Address GetAddrOfLocalVar(const VarDecl *VD)
GetAddrOfLocalVar - Return the address of a local variable.
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
This class organizes the cross-function state that is used while generating LLVM code.
llvm::Module & getModule() const
llvm::FunctionCallee CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name, llvm::AttributeList ExtraAttrs=llvm::AttributeList(), bool Local=false, bool AssumeConvergent=false)
Create or return a runtime function declaration with the specified type and name.
void addCompilerUsedGlobal(llvm::GlobalValue *GV)
Add a global to a list to be added to the llvm.compiler.used metadata.
const IntrusiveRefCntPtr< llvm::vfs::FileSystem > & getFileSystem() const
DiagnosticsEngine & getDiags() const
const LangOptions & getLangOpts() const
CodeGenTypes & getTypes()
const TargetInfo & getTarget() const
const llvm::DataLayout & getDataLayout() const
void Error(SourceLocation loc, StringRef error)
Emit a general error that something can't be done.
CGCXXABI & getCXXABI() const
SanitizerMetadata * getSanitizerMetadata()
const llvm::Triple & getTriple() const
ASTContext & getContext() const
const CodeGenOptions & getCodeGenOpts() const
StringRef getMangledName(GlobalDecl GD)
void maybeSetTrivialComdat(const Decl &D, llvm::GlobalObject &GO)
void printPostfixForExternalizedDecl(llvm::raw_ostream &OS, const Decl *D) const
Print the postfix for externalized static variable or kernels for single source offloading languages ...
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
const CGFunctionInfo & arrangeFunctionDeclaration(const GlobalDecl GD)
Free functions are functions that are compatible with an ordinary C function pointer type.
static RValue get(llvm::Value *V)
static RValue getAggregate(Address addr, bool isVolatile=false)
Convert an Address to an RValue.
lookup_result lookup(DeclarationName Name) const
lookup - Find the declarations (if any) with the given Name in this context.
SourceLocation getLocation() const
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
const ParmVarDecl * getParamDecl(unsigned i) const
GlobalDecl getWithKernelReferenceKind(KernelReferenceKind Kind)
const Decl * getDecl() const
StringRef getName() const
Return the actual identifier string.
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
std::string CUID
The user provided compilation unit ID, if non-empty.
GPUDefaultStreamKind GPUDefaultStream
The default stream kind used for HIP kernel launching.
bool shouldMangleDeclName(const NamedDecl *D)
void mangleName(GlobalDecl GD, raw_ostream &)
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
QualType getCanonicalType() const
bool isMicrosoft() const
Is this ABI an MSVC-compatible ABI?
bool isItaniumFamily() const
Does this ABI generally fall into the Itanium family of ABIs?
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
const llvm::VersionTuple & getSDKVersion() const
unsigned size() const
Retrieve the number of template arguments in this template argument list.
static DeclContext * castToDeclContext(const TranslationUnitDecl *D)
CXXRecordDecl * castAsCXXRecordDecl() const
bool isCUDADeviceBuiltinSurfaceType() const
Check if the type is the CUDA device builtin surface type.
bool isCUDADeviceBuiltinTextureType() const
Check if the type is the CUDA device builtin texture type.
Represents a variable declaration or definition.
bool isInline() const
Whether this variable is (C++1z) inline.
bool hasExternalStorage() const
Returns true if a variable has extern or private_extern storage.
DefinitionKind hasDefinition(ASTContext &) const
Check whether this variable is defined in this translation unit.
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
CGCUDARuntime * CreateNVCUDARuntime(CodeGenModule &CGM)
Creates an instance of a CUDA runtime class.
@ VFS
Remove unused -ivfsoverlay arguments.
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
@ Address
A pointer to a ValueDecl.
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
if(T->getSizeExpr()) TRY_TO(TraverseStmt(const_cast< Expr * >(T -> getSizeExpr())))
CudaVersion ToCudaVersion(llvm::VersionTuple)
bool CudaFeatureEnabled(llvm::VersionTuple, CudaFeature)
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have.
@ Type
The name was classified as a type.
U cast(CodeGen::Address addr)
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 int32_t
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
llvm::IntegerType * SizeTy
llvm::IntegerType * IntTy
int
CharUnits getSizeAlign() const
CharUnits getPointerAlign() const
llvm::PointerType * DefaultPtrTy