Skip to content

Commit

Permalink
[CodeGen] Call SetInternalFunctionAttributes to attach function
Browse files Browse the repository at this point in the history
attributes to internal functions.

This patch fixes CodeGenModule::CreateGlobalInitOrDestructFunction to
use SetInternalFunctionAttributes instead of SetLLVMFunctionAttributes
to attach function attributes to internal functions.

Also, make sure the correct CGFunctionInfo is passed instead of always
passing what arrangeNullaryFunction returns.

rdar://problem/20828324

Differential Revision: http://reviews.llvm.org/D13610

llvm-svn: 251734
  • Loading branch information
ahatanaka committed Oct 31, 2015
1 parent dbb0abb commit 7791f1a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 16 deletions.
27 changes: 17 additions & 10 deletions clang/lib/CodeGen/CGDeclCXX.cpp
Expand Up @@ -193,13 +193,15 @@ llvm::Constant *CodeGenFunction::createAtExitStub(const VarDecl &VD,
llvm::raw_svector_ostream Out(FnName);
CGM.getCXXABI().getMangleContext().mangleDynamicAtExitDestructor(&VD, Out);
}

const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction();
llvm::Function *fn = CGM.CreateGlobalInitOrDestructFunction(ty, FnName.str(),
FI,
VD.getLocation());

CodeGenFunction CGF(CGM);

CGF.StartFunction(&VD, CGM.getContext().VoidTy, fn,
CGM.getTypes().arrangeNullaryFunction(), FunctionArgList());
CGF.StartFunction(&VD, CGM.getContext().VoidTy, fn, FI, FunctionArgList());

llvm::CallInst *call = CGF.Builder.CreateCall(dtor, addr);

Expand Down Expand Up @@ -247,7 +249,8 @@ void CodeGenFunction::EmitCXXGuardedInit(const VarDecl &D,
}

llvm::Function *CodeGenModule::CreateGlobalInitOrDestructFunction(
llvm::FunctionType *FTy, const Twine &Name, SourceLocation Loc, bool TLS) {
llvm::FunctionType *FTy, const Twine &Name, const CGFunctionInfo &FI,
SourceLocation Loc, bool TLS) {
llvm::Function *Fn =
llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
Name, &getModule());
Expand All @@ -257,7 +260,7 @@ llvm::Function *CodeGenModule::CreateGlobalInitOrDestructFunction(
Fn->setSection(Section);
}

SetLLVMFunctionAttributes(nullptr, getTypes().arrangeNullaryFunction(), Fn);
SetInternalFunctionAttributes(nullptr, Fn, FI);

Fn->setCallingConv(getRuntimeCC());

Expand Down Expand Up @@ -315,7 +318,9 @@ CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,

// Create a variable initialization function.
llvm::Function *Fn =
CreateGlobalInitOrDestructFunction(FTy, FnName.str(), D->getLocation());
CreateGlobalInitOrDestructFunction(FTy, FnName.str(),
getTypes().arrangeNullaryFunction(),
D->getLocation());

auto *ISA = D->getAttr<InitSegAttr>();
CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr,
Expand Down Expand Up @@ -390,7 +395,7 @@ CodeGenModule::EmitCXXGlobalInitFunc() {
return;

llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);

const CGFunctionInfo &FI = getTypes().arrangeNullaryFunction();

// Create our global initialization function.
if (!PrioritizedCXXGlobalInits.empty()) {
Expand All @@ -414,7 +419,7 @@ CodeGenModule::EmitCXXGlobalInitFunc() {
// Priority is always <= 65535 (enforced by sema).
PrioritySuffix = std::string(6-PrioritySuffix.size(), '0')+PrioritySuffix;
llvm::Function *Fn = CreateGlobalInitOrDestructFunction(
FTy, "_GLOBAL__I_" + PrioritySuffix);
FTy, "_GLOBAL__I_" + PrioritySuffix, FI);

for (; I < PrioE; ++I)
LocalCXXGlobalInits.push_back(I->second);
Expand Down Expand Up @@ -444,7 +449,7 @@ CodeGenModule::EmitCXXGlobalInitFunc() {
}

llvm::Function *Fn = CreateGlobalInitOrDestructFunction(
FTy, llvm::Twine("_GLOBAL__sub_I_", FileName));
FTy, llvm::Twine("_GLOBAL__sub_I_", FileName), FI);

CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, CXXGlobalInits);
AddGlobalCtor(Fn);
Expand All @@ -459,7 +464,9 @@ void CodeGenModule::EmitCXXGlobalDtorFunc() {
llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);

// Create our global destructor function.
llvm::Function *Fn = CreateGlobalInitOrDestructFunction(FTy, "_GLOBAL__D_a");
const CGFunctionInfo &FI = getTypes().arrangeNullaryFunction();
llvm::Function *Fn =
CreateGlobalInitOrDestructFunction(FTy, "_GLOBAL__D_a", FI);

CodeGenFunction(*this).GenerateCXXGlobalDtorsFunc(Fn, CXXGlobalDtors);
AddGlobalDtor(Fn);
Expand Down Expand Up @@ -584,7 +591,7 @@ llvm::Function *CodeGenFunction::generateDestroyHelper(
getContext().VoidTy, args, FunctionType::ExtInfo(), /*variadic=*/false);
llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
llvm::Function *fn = CGM.CreateGlobalInitOrDestructFunction(
FTy, "__cxx_global_array_dtor", VD->getLocation());
FTy, "__cxx_global_array_dtor", FI, VD->getLocation());

CurEHLocation = VD->getLocStart();

Expand Down
7 changes: 4 additions & 3 deletions clang/lib/CodeGen/CGOpenMPRuntime.cpp
Expand Up @@ -1104,7 +1104,7 @@ llvm::Function *CGOpenMPRuntime::emitThreadPrivateVarDefinition(
/*isVariadic=*/false);
auto FTy = CGM.getTypes().GetFunctionType(FI);
auto Fn = CGM.CreateGlobalInitOrDestructFunction(
FTy, ".__kmpc_global_ctor_.", Loc);
FTy, ".__kmpc_global_ctor_.", FI, Loc);
CtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidPtrTy, Fn, FI,
Args, SourceLocation());
auto ArgVal = CtorCGF.EmitLoadOfScalar(
Expand Down Expand Up @@ -1136,7 +1136,7 @@ llvm::Function *CGOpenMPRuntime::emitThreadPrivateVarDefinition(
/*isVariadic=*/false);
auto FTy = CGM.getTypes().GetFunctionType(FI);
auto Fn = CGM.CreateGlobalInitOrDestructFunction(
FTy, ".__kmpc_global_dtor_.", Loc);
FTy, ".__kmpc_global_dtor_.", FI, Loc);
DtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, Args,
SourceLocation());
auto ArgVal = DtorCGF.EmitLoadOfScalar(
Expand Down Expand Up @@ -1174,7 +1174,8 @@ llvm::Function *CGOpenMPRuntime::emitThreadPrivateVarDefinition(
auto InitFunctionTy =
llvm::FunctionType::get(CGM.VoidTy, /*isVarArg*/ false);
auto InitFunction = CGM.CreateGlobalInitOrDestructFunction(
InitFunctionTy, ".__omp_threadprivate_init_.");
InitFunctionTy, ".__omp_threadprivate_init_.",
CGM.getTypes().arrangeNullaryFunction());
CodeGenFunction InitCGF(CGM);
FunctionArgList ArgList;
InitCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, InitFunction,
Expand Down
1 change: 1 addition & 0 deletions clang/lib/CodeGen/CodeGenModule.h
Expand Up @@ -686,6 +686,7 @@ class CodeGenModule : public CodeGenTypeCache {

llvm::Function *
CreateGlobalInitOrDestructFunction(llvm::FunctionType *ty, const Twine &name,
const CGFunctionInfo &FI,
SourceLocation Loc = SourceLocation(),
bool TLS = false);

Expand Down
3 changes: 2 additions & 1 deletion clang/lib/CodeGen/ItaniumCXXABI.cpp
Expand Up @@ -2209,7 +2209,8 @@ void ItaniumCXXABI::EmitThreadLocalInitFuncs(
// Generate a guarded initialization function.
llvm::FunctionType *FTy =
llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
InitFunc = CGM.CreateGlobalInitOrDestructFunction(FTy, "__tls_init",
const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction();
InitFunc = CGM.CreateGlobalInitOrDestructFunction(FTy, "__tls_init", FI,
SourceLocation(),
/*TLS=*/true);
llvm::GlobalVariable *Guard = new llvm::GlobalVariable(
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CodeGen/MicrosoftCXXABI.cpp
Expand Up @@ -2214,8 +2214,8 @@ void MicrosoftCXXABI::EmitThreadLocalInitFuncs(
llvm::FunctionType *FTy =
llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
llvm::Function *InitFunc = CGM.CreateGlobalInitOrDestructFunction(
FTy, "__tls_init", SourceLocation(),
/*TLS=*/true);
FTy, "__tls_init", CGM.getTypes().arrangeNullaryFunction(),
SourceLocation(), /*TLS=*/true);
CodeGenFunction(CGM).GenerateCXXGlobalInitFunc(InitFunc, NonComdatInits);

AddToXDU(InitFunc);
Expand Down
12 changes: 12 additions & 0 deletions clang/test/CodeGenCXX/funcattrs-global-ctor-dtor.cpp
@@ -0,0 +1,12 @@
// RUN: %clang_cc1 %s -triple x86_64-apple-darwin -S -stack-protector 2 -emit-llvm -o - | FileCheck %s

class A {
public:
virtual ~A() {}
};

A g;

// CHECK: define internal void @__cxx_global_var_init() [[ATTR0:#[0-9]+]]
// CHECK: define internal void @_GLOBAL__sub_I_funcattrs_global_ctor_dtor.cpp() [[ATTR0]]
// CHECK: attributes [[ATTR0]] = {{{.*}} sspstrong {{.*}}}

0 comments on commit 7791f1a

Please sign in to comment.