forked from OSchip/llvm-project
Fix for PR5967: Make const-marking for LLVM globals correct for cases requiring
run-time initialization, and emit run-time initializers aggresively to avoid ordering issues with deferred globals. llvm-svn: 92976
This commit is contained in:
parent
fa1e36d0de
commit
5866fe306d
|
@ -119,6 +119,22 @@ CodeGenFunction::EmitCXXGlobalDtorRegistration(llvm::Constant *DtorFn,
|
|||
Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args));
|
||||
}
|
||||
|
||||
void
|
||||
CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D) {
|
||||
const llvm::FunctionType *FTy
|
||||
= llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
|
||||
false);
|
||||
|
||||
// Create a variable initialization function.
|
||||
llvm::Function *Fn =
|
||||
llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
|
||||
"__cxx_global_var_init", &TheModule);
|
||||
|
||||
CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D);
|
||||
|
||||
CXXGlobalInits.push_back(Fn);
|
||||
}
|
||||
|
||||
void
|
||||
CodeGenModule::EmitCXXGlobalInitFunc() {
|
||||
if (CXXGlobalInits.empty())
|
||||
|
@ -140,18 +156,26 @@ CodeGenModule::EmitCXXGlobalInitFunc() {
|
|||
AddGlobalCtor(Fn);
|
||||
}
|
||||
|
||||
void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
|
||||
const VarDecl *D) {
|
||||
StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
|
||||
SourceLocation());
|
||||
|
||||
llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D);
|
||||
EmitCXXGlobalVarDeclInit(*D, DeclPtr);
|
||||
|
||||
FinishFunction();
|
||||
}
|
||||
|
||||
void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
|
||||
const VarDecl **Decls,
|
||||
llvm::Constant **Decls,
|
||||
unsigned NumDecls) {
|
||||
StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
|
||||
SourceLocation());
|
||||
|
||||
for (unsigned i = 0; i != NumDecls; ++i) {
|
||||
const VarDecl *D = Decls[i];
|
||||
for (unsigned i = 0; i != NumDecls; ++i)
|
||||
Builder.CreateCall(Decls[i]);
|
||||
|
||||
llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D);
|
||||
EmitCXXGlobalVarDeclInit(*D, DeclPtr);
|
||||
}
|
||||
FinishFunction();
|
||||
}
|
||||
|
||||
|
|
|
@ -1185,9 +1185,11 @@ public:
|
|||
/// GenerateCXXGlobalInitFunc - Generates code for initializing global
|
||||
/// variables.
|
||||
void GenerateCXXGlobalInitFunc(llvm::Function *Fn,
|
||||
const VarDecl **Decls,
|
||||
llvm::Constant **Decls,
|
||||
unsigned NumDecls);
|
||||
|
||||
void GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn, const VarDecl *D);
|
||||
|
||||
void EmitCXXConstructExpr(llvm::Value *Dest, const CXXConstructExpr *E);
|
||||
|
||||
RValue EmitCXXExprWithTemporaries(const CXXExprWithTemporaries *E,
|
||||
|
|
|
@ -66,10 +66,8 @@ CodeGenModule::~CodeGenModule() {
|
|||
}
|
||||
|
||||
void CodeGenModule::Release() {
|
||||
// We need to call this first because it can add deferred declarations.
|
||||
EmitCXXGlobalInitFunc();
|
||||
|
||||
EmitDeferred();
|
||||
EmitCXXGlobalInitFunc();
|
||||
if (Runtime)
|
||||
if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction())
|
||||
AddGlobalCtor(ObjCInitFunction);
|
||||
|
@ -971,6 +969,7 @@ GetLinkageForVariable(ASTContext &Context, const VarDecl *VD) {
|
|||
void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) {
|
||||
llvm::Constant *Init = 0;
|
||||
QualType ASTTy = D->getType();
|
||||
bool NonConstInit = false;
|
||||
|
||||
if (D->getInit() == 0) {
|
||||
// This is a tentative definition; tentative definitions are
|
||||
|
@ -990,8 +989,9 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) {
|
|||
if (!Init) {
|
||||
QualType T = D->getInit()->getType();
|
||||
if (getLangOptions().CPlusPlus) {
|
||||
CXXGlobalInits.push_back(D);
|
||||
EmitCXXGlobalVarDeclInitFunc(D);
|
||||
Init = EmitNullConstant(T);
|
||||
NonConstInit = true;
|
||||
} else {
|
||||
ErrorUnsupported(D, "static initializer");
|
||||
Init = llvm::UndefValue::get(getTypes().ConvertType(T));
|
||||
|
@ -1052,7 +1052,7 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) {
|
|||
|
||||
// If it is safe to mark the global 'constant', do so now.
|
||||
GV->setConstant(false);
|
||||
if (DeclIsConstantGlobal(Context, D))
|
||||
if (!NonConstInit && DeclIsConstantGlobal(Context, D))
|
||||
GV->setConstant(true);
|
||||
|
||||
GV->setAlignment(getContext().getDeclAlignInBytes(D));
|
||||
|
|
|
@ -153,7 +153,7 @@ class CodeGenModule : public BlockModule {
|
|||
|
||||
/// CXXGlobalInits - Variables with global initializers that need to run
|
||||
/// before main.
|
||||
std::vector<const VarDecl*> CXXGlobalInits;
|
||||
std::vector<llvm::Constant*> CXXGlobalInits;
|
||||
|
||||
/// CFConstantStringClassRef - Cached reference to the class for constant
|
||||
/// strings. This value has type int * but is actually an Obj-C class pointer.
|
||||
|
@ -475,7 +475,9 @@ private:
|
|||
|
||||
/// EmitCXXGlobalInitFunc - Emit a function that initializes C++ globals.
|
||||
void EmitCXXGlobalInitFunc();
|
||||
|
||||
|
||||
void EmitCXXGlobalVarDeclInitFunc(const VarDecl *D);
|
||||
|
||||
// FIXME: Hardcoding priority here is gross.
|
||||
void AddGlobalCtor(llvm::Function *Ctor, int Priority=65535);
|
||||
void AddGlobalDtor(llvm::Function *Dtor, int Priority=65535);
|
||||
|
|
|
@ -15,6 +15,9 @@ class C {
|
|||
virtual void bar3() __attribute__((aligned(1024)));
|
||||
} c;
|
||||
|
||||
// CHECK:.align 1, 0x90
|
||||
// CHECK-NEXT:.globl __ZN1CC1Ev
|
||||
|
||||
void C::bar1() { }
|
||||
|
||||
// CHECK:.align 1, 0x90
|
||||
|
|
|
@ -45,15 +45,14 @@ M gm;
|
|||
|
||||
int main() {M m1;}
|
||||
|
||||
// CHECK-LP64: callq __ZN1MC1Ev
|
||||
// CHECK-LP64: callq __ZN1MD1Ev
|
||||
// CHECK-LP64: .globl __ZN1MD1Ev
|
||||
// CHECK-LP64-NEXT: .weak_definition __ZN1MD1Ev
|
||||
// CHECK-LP64-NEXT: __ZN1MD1Ev:
|
||||
// CHECK-LP64: callq __ZN1MC1Ev
|
||||
// CHECK-LP64: callq __ZN1MD1Ev
|
||||
|
||||
|
||||
// CHECK-LP32: call L__ZN1MC1Ev
|
||||
// CHECK-LP32: call L__ZN1MD1Ev
|
||||
// CHECK-LP32: .globl __ZN1MD1Ev
|
||||
// CHECK-LP32-NEXT: .weak_definition __ZN1MD1Ev
|
||||
// CHECK-LP32-NEXT:__ZN1MD1Ev:
|
||||
// CHECK-LP32: call L__ZN1MC1Ev
|
||||
// CHECK-LP32: call L__ZN1MD1Ev
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s
|
||||
// PR5967
|
||||
|
||||
extern void* foo;
|
||||
static void* const a = foo;
|
||||
void* bar() { return a; }
|
||||
|
||||
// CHECK: @a = internal global i8* null
|
||||
|
||||
// CHECK: define internal void @__cxx_global_var_init
|
||||
// CHECK: load i8** @foo
|
||||
// CHECK: ret void
|
||||
|
||||
// CHECK: define internal void @__cxx_global_initialization
|
||||
// CHECK: call void @__cxx_global_var_init()
|
||||
// CHECK: ret void
|
|
@ -121,12 +121,6 @@ void use_F(F<char> &fc) {
|
|||
// CHECK: @_ZTI1EIlE = weak_odr constant
|
||||
// CHECK: @_ZTV1EIlE = weak_odr constant
|
||||
|
||||
// The anonymous struct for e has no linkage, so the vtable should have
|
||||
// internal linkage.
|
||||
// CHECK: @"_ZTS3$_0" = internal constant
|
||||
// CHECK: @"_ZTI3$_0" = internal constant
|
||||
// CHECK: @"_ZTV3$_0" = internal constant
|
||||
|
||||
// F<long> is an implicit template instantiation with no key function,
|
||||
// so its vtable should have weak_odr linkage.
|
||||
// CHECK: @_ZTS1FIlE = weak_odr constant
|
||||
|
@ -144,6 +138,12 @@ void use_F(F<char> &fc) {
|
|||
// its vtable, not define it.
|
||||
// CHECK: @_ZTV1EIiE = external constant
|
||||
|
||||
// The anonymous struct for e has no linkage, so the vtable should have
|
||||
// internal linkage.
|
||||
// CHECK: @"_ZTS3$_0" = internal constant
|
||||
// CHECK: @"_ZTI3$_0" = internal constant
|
||||
// CHECK: @"_ZTV3$_0" = internal constant
|
||||
|
||||
// The A vtable should have internal linkage since it is inside an anonymous
|
||||
// namespace.
|
||||
// CHECK: @_ZTSN12_GLOBAL__N_11AE = internal constant
|
||||
|
|
Loading…
Reference in New Issue