Don't use comdats for initializers on platforms that don't support it

In particular, pre-.init_array ELF uses the .ctors section mechanism.
MinGW COFF also uses .ctors, now that I think about it. Therefore,
restrict this optimization to the two platforms that are currently known
to work: ELF with .init_array and COFF with .CRT$XCU.

llvm-svn: 218287
This commit is contained in:
Reid Kleckner 2014-09-23 00:00:14 +00:00
parent 0f79772ecc
commit 6c03130542
6 changed files with 21 additions and 5 deletions

View File

@ -302,7 +302,7 @@ CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr, CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr,
PerformInit); PerformInit);
llvm::GlobalVariable *Key = supportsCOMDAT() ? Addr : nullptr; llvm::GlobalVariable *Key = supportsCOMDATInitializers() ? Addr : nullptr;
if (D->getTLSKind()) { if (D->getTLSKind()) {
// FIXME: Should we support init_priority for thread_local? // FIXME: Should we support init_priority for thread_local?

View File

@ -601,6 +601,7 @@ public:
const TargetInfo &getTarget() const { return Target; } const TargetInfo &getTarget() const { return Target; }
const llvm::Triple &getTriple() const; const llvm::Triple &getTriple() const;
bool supportsCOMDAT() const; bool supportsCOMDAT() const;
bool supportsCOMDATInitializers() const;
CGCXXABI &getCXXABI() const { return *ABI; } CGCXXABI &getCXXABI() const { return *ABI; }
llvm::LLVMContext &getLLVMContext() { return VMContext; } llvm::LLVMContext &getLLVMContext() { return VMContext; }

View File

@ -1668,7 +1668,8 @@ void ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF,
// The ABI says: It is suggested that it be emitted in the same COMDAT group // The ABI says: It is suggested that it be emitted in the same COMDAT group
// as the associated data object // as the associated data object
if (!D.isLocalVarDecl() && var->isWeakForLinker() && CGM.supportsCOMDAT()) { if (!D.isLocalVarDecl() && var->isWeakForLinker() &&
CGM.supportsCOMDATInitializers()) {
llvm::Comdat *C = CGM.getModule().getOrInsertComdat(var->getName()); llvm::Comdat *C = CGM.getModule().getOrInsertComdat(var->getName());
guard->setComdat(C); guard->setComdat(C);
var->setComdat(C); var->setComdat(C);

View File

@ -6877,6 +6877,14 @@ bool CodeGenModule::supportsCOMDAT() const {
return !getTriple().isOSBinFormatMachO(); return !getTriple().isOSBinFormatMachO();
} }
bool CodeGenModule::supportsCOMDATInitializers() const {
// We can only put initializers in comdat groups on ELF with .init_array and
// COFF with .CRT$XCU.
return supportsCOMDAT() &&
((getTriple().isOSBinFormatELF() && getCodeGenOpts().UseInitArray) ||
(getTriple().isWindowsMSVCEnvironment()));
}
const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
if (TheTargetCodeGenInfo) if (TheTargetCodeGenInfo)
return *TheTargetCodeGenInfo; return *TheTargetCodeGenInfo;

View File

@ -1,4 +1,6 @@
// RUN: %clang_cc1 -triple x86_64-pc-linux -emit-llvm -o - %s | FileCheck %s // RUN: %clang_cc1 -triple x86_64-pc-linux -emit-llvm -fuse-init-array -o - %s | FileCheck %s
// RUN: %clang_cc1 -triple x86_64-pc-linux -emit-llvm -o - %s | \
// RUN: FileCheck %s --check-prefix=CTORS
// RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -o - %s | \ // RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -o - %s | \
// RUN: FileCheck --check-prefix=MACHO %s // RUN: FileCheck --check-prefix=MACHO %s
@ -8,6 +10,8 @@
// CHECK: @_ZGVN5test31AIiE1xE = weak_odr global i64 0, comdat $_ZN5test31AIiE1xE // CHECK: @_ZGVN5test31AIiE1xE = weak_odr global i64 0, comdat $_ZN5test31AIiE1xE
// MACHO: @_ZGVN5test31AIiE1xE = weak_odr global i64 0 // MACHO: @_ZGVN5test31AIiE1xE = weak_odr global i64 0
// MACHO-NOT: comdat // MACHO-NOT: comdat
// CTORS: @_ZGVN5test31AIiE1xE = weak_odr global i64 0
// CTORS-NOT: comdat
// CHECK: _ZN5test51U2k0E = global i32 0 // CHECK: _ZN5test51U2k0E = global i32 0
// CHECK: _ZN5test51U2k1E = global i32 0 // CHECK: _ZN5test51U2k1E = global i32 0
@ -67,6 +71,8 @@ namespace test3 {
// CHECK-LABEL: define internal void @__cxx_global_var_init1() {{.*}} comdat $_ZN5test31AIiE1xE // CHECK-LABEL: define internal void @__cxx_global_var_init1() {{.*}} comdat $_ZN5test31AIiE1xE
// MACHO-LABEL: define internal void @__cxx_global_var_init1() // MACHO-LABEL: define internal void @__cxx_global_var_init1()
// MACHO-NOT: comdat // MACHO-NOT: comdat
// CTORS-LABEL: define internal void @__cxx_global_var_init1()
// CTORS-NOT: comdat
// CHECK: [[GUARDBYTE:%.*]] = load i8* bitcast (i64* @_ZGVN5test31AIiE1xE to i8*) // CHECK: [[GUARDBYTE:%.*]] = load i8* bitcast (i64* @_ZGVN5test31AIiE1xE to i8*)
// CHECK-NEXT: [[UNINITIALIZED:%.*]] = icmp eq i8 [[GUARDBYTE]], 0 // CHECK-NEXT: [[UNINITIALIZED:%.*]] = icmp eq i8 [[GUARDBYTE]], 0
// CHECK-NEXT: br i1 [[UNINITIALIZED]] // CHECK-NEXT: br i1 [[UNINITIALIZED]]

View File

@ -1,4 +1,4 @@
// RUN: %clang_cc1 %s -std=c++1y -triple=x86_64-pc-linux -emit-llvm -o - | FileCheck --check-prefix=ELF --check-prefix=ALL %s // RUN: %clang_cc1 %s -std=c++1y -triple=x86_64-pc-linux -fuse-init-array -emit-llvm -o - | FileCheck --check-prefix=ELF --check-prefix=ALL %s
// RUN: %clang_cc1 %s -std=c++1y -triple=x86_64-apple-darwin -emit-llvm -o - | FileCheck --check-prefix=MACHO --check-prefix=ALL %s // RUN: %clang_cc1 %s -std=c++1y -triple=x86_64-apple-darwin -emit-llvm -o - | FileCheck --check-prefix=MACHO --check-prefix=ALL %s
// ALL: ; ModuleID // ALL: ; ModuleID