From 5866fe306d541fe04ec9c62376f4092b499f341b Mon Sep 17 00:00:00 2001 From: Eli Friedman Date: Fri, 8 Jan 2010 00:50:11 +0000 Subject: [PATCH] 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 --- clang/lib/CodeGen/CGDeclCXX.cpp | 36 +++++++++++++++---- clang/lib/CodeGen/CodeGenFunction.h | 4 ++- clang/lib/CodeGen/CodeGenModule.cpp | 10 +++--- clang/lib/CodeGen/CodeGenModule.h | 6 ++-- clang/test/CodeGenCXX/attr.cpp | 3 ++ .../default-destructor-synthesis.cpp | 9 +++-- .../test/CodeGenCXX/deferred-global-init.cpp | 16 +++++++++ clang/test/CodeGenCXX/vtable-linkage.cpp | 12 +++---- 8 files changed, 71 insertions(+), 25 deletions(-) create mode 100644 clang/test/CodeGenCXX/deferred-global-init.cpp diff --git a/clang/lib/CodeGen/CGDeclCXX.cpp b/clang/lib/CodeGen/CGDeclCXX.cpp index 0b6ea5a834b1..47773a0d69e3 100644 --- a/clang/lib/CodeGen/CGDeclCXX.cpp +++ b/clang/lib/CodeGen/CGDeclCXX.cpp @@ -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(); } diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h index 6c6282d8a4b5..f7ed4db455bf 100644 --- a/clang/lib/CodeGen/CodeGenFunction.h +++ b/clang/lib/CodeGen/CodeGenFunction.h @@ -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, diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 0dbf336dccc0..13cdc2b9dbe5 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -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)); diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h index 8f165109f5b0..47d8c54cd980 100644 --- a/clang/lib/CodeGen/CodeGenModule.h +++ b/clang/lib/CodeGen/CodeGenModule.h @@ -153,7 +153,7 @@ class CodeGenModule : public BlockModule { /// CXXGlobalInits - Variables with global initializers that need to run /// before main. - std::vector CXXGlobalInits; + std::vector 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); diff --git a/clang/test/CodeGenCXX/attr.cpp b/clang/test/CodeGenCXX/attr.cpp index 8fd86414862c..1b214b77bee7 100644 --- a/clang/test/CodeGenCXX/attr.cpp +++ b/clang/test/CodeGenCXX/attr.cpp @@ -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 diff --git a/clang/test/CodeGenCXX/default-destructor-synthesis.cpp b/clang/test/CodeGenCXX/default-destructor-synthesis.cpp index 098458d35d5c..71167a204fbb 100644 --- a/clang/test/CodeGenCXX/default-destructor-synthesis.cpp +++ b/clang/test/CodeGenCXX/default-destructor-synthesis.cpp @@ -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 diff --git a/clang/test/CodeGenCXX/deferred-global-init.cpp b/clang/test/CodeGenCXX/deferred-global-init.cpp new file mode 100644 index 000000000000..570147905548 --- /dev/null +++ b/clang/test/CodeGenCXX/deferred-global-init.cpp @@ -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 diff --git a/clang/test/CodeGenCXX/vtable-linkage.cpp b/clang/test/CodeGenCXX/vtable-linkage.cpp index 733a9f0be538..63e17431c467 100644 --- a/clang/test/CodeGenCXX/vtable-linkage.cpp +++ b/clang/test/CodeGenCXX/vtable-linkage.cpp @@ -121,12 +121,6 @@ void use_F(F &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 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 &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