diff options
author | Eli Friedman <efriedma@quicinc.com> | 2024-11-14 14:35:40 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-14 14:35:40 -0800 |
commit | 6bd3f2e8986f4eb7972bc0102ff4a706dacc0d48 (patch) | |
tree | fb49674c356ece81f111711f62f851b049a09dc5 /clang/lib/CodeGen/CodeGenModule.cpp | |
parent | 691bd184e628bac8a2d7385dba1057cfcd844689 (diff) | |
download | llvm-6bd3f2e8986f4eb7972bc0102ff4a706dacc0d48.zip llvm-6bd3f2e8986f4eb7972bc0102ff4a706dacc0d48.tar.gz llvm-6bd3f2e8986f4eb7972bc0102ff4a706dacc0d48.tar.bz2 |
[clang codegen] Add CreateRuntimeFunction overload that takes a clang type. (#113506)
Correctly computing the LLVM types/attributes is complicated in general,
so add a variant which does that for you.
Diffstat (limited to 'clang/lib/CodeGen/CodeGenModule.cpp')
-rw-r--r-- | clang/lib/CodeGen/CodeGenModule.cpp | 64 |
1 files changed, 49 insertions, 15 deletions
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index ba376f9..e1537e8 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -4903,6 +4903,52 @@ GetRuntimeFunctionDecl(ASTContext &C, StringRef Name) { return nullptr; } +static void setWindowsItaniumDLLImport(CodeGenModule &CGM, bool Local, + llvm::Function *F, StringRef Name) { + // In Windows Itanium environments, try to mark runtime functions + // dllimport. For Mingw and MSVC, don't. We don't really know if the user + // will link their standard library statically or dynamically. Marking + // functions imported when they are not imported can cause linker errors + // and warnings. + if (!Local && CGM.getTriple().isWindowsItaniumEnvironment() && + !CGM.getCodeGenOpts().LTOVisibilityPublicStd) { + const FunctionDecl *FD = GetRuntimeFunctionDecl(CGM.getContext(), Name); + if (!FD || FD->hasAttr<DLLImportAttr>()) { + F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); + F->setLinkage(llvm::GlobalValue::ExternalLinkage); + } + } +} + +llvm::FunctionCallee CodeGenModule::CreateRuntimeFunction( + QualType ReturnTy, ArrayRef<QualType> ArgTys, StringRef Name, + llvm::AttributeList ExtraAttrs, bool Local, bool AssumeConvergent) { + if (AssumeConvergent) { + ExtraAttrs = + ExtraAttrs.addFnAttribute(VMContext, llvm::Attribute::Convergent); + } + + QualType FTy = Context.getFunctionType(ReturnTy, ArgTys, + FunctionProtoType::ExtProtoInfo()); + const CGFunctionInfo &Info = getTypes().arrangeFreeFunctionType( + Context.getCanonicalType(FTy).castAs<FunctionProtoType>()); + auto *ConvTy = getTypes().GetFunctionType(Info); + llvm::Constant *C = GetOrCreateLLVMFunction( + Name, ConvTy, GlobalDecl(), /*ForVTable=*/false, + /*DontDefer=*/false, /*IsThunk=*/false, ExtraAttrs); + + if (auto *F = dyn_cast<llvm::Function>(C)) { + if (F->empty()) { + SetLLVMFunctionAttributes(GlobalDecl(), Info, F, /*IsThunk*/ false); + // FIXME: Set calling-conv properly in ExtProtoInfo + F->setCallingConv(getRuntimeCC()); + setWindowsItaniumDLLImport(*this, Local, F, Name); + setDSOLocal(F); + } + } + return {ConvTy, C}; +} + /// CreateRuntimeFunction - Create a new runtime function with the specified /// type and name. llvm::FunctionCallee @@ -4922,24 +4968,12 @@ CodeGenModule::CreateRuntimeFunction(llvm::FunctionType *FTy, StringRef Name, if (auto *F = dyn_cast<llvm::Function>(C)) { if (F->empty()) { F->setCallingConv(getRuntimeCC()); - - // In Windows Itanium environments, try to mark runtime functions - // dllimport. For Mingw and MSVC, don't. We don't really know if the user - // will link their standard library statically or dynamically. Marking - // functions imported when they are not imported can cause linker errors - // and warnings. - if (!Local && getTriple().isWindowsItaniumEnvironment() && - !getCodeGenOpts().LTOVisibilityPublicStd) { - const FunctionDecl *FD = GetRuntimeFunctionDecl(Context, Name); - if (!FD || FD->hasAttr<DLLImportAttr>()) { - F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); - F->setLinkage(llvm::GlobalValue::ExternalLinkage); - } - } + setWindowsItaniumDLLImport(*this, Local, F, Name); setDSOLocal(F); // FIXME: We should use CodeGenModule::SetLLVMFunctionAttributes() instead // of trying to approximate the attributes using the LLVM function - // signature. This requires revising the API of CreateRuntimeFunction(). + // signature. The other overload of CreateRuntimeFunction does this; it + // should be used for new code. markRegisterParameterAttributes(F); } } |