On 18 September 2014 21:52, Reid Kleckner <[email protected]> wrote:
> The global_ctors patch seems fine, but I think David approved the previous
> patch prematurely.
>
> The initializer code on Windows is already in its own comdat while the
> global_ctors entry is in the comdat with the global. We should leave that as
> is.
>
> I also think you need to put the the global (the key) into a proper comdat,
> and not just leave it as a linkonce_odr global.
Something like this?
Cheers,
Rafael
diff --git a/lib/CodeGen/CGDeclCXX.cpp b/lib/CodeGen/CGDeclCXX.cpp
index 91ae6a8..b0427da 100644
--- a/lib/CodeGen/CGDeclCXX.cpp
+++ b/lib/CodeGen/CGDeclCXX.cpp
@@ -298,6 +298,12 @@ CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
llvm::Function *Fn =
CreateGlobalInitOrDestructFunction(*this, FTy, FnName.str());
+ if (Addr->isWeakForLinker() && getTriple().isOSBinFormatELF()) {
+ llvm::Comdat *C = TheModule.getOrInsertComdat(Addr->getName());
+ Addr->setComdat(C);
+ Fn->setComdat(C);
+ }
+
auto *ISA = D->getAttr<InitSegAttr>();
CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr,
PerformInit);
diff --git a/lib/CodeGen/ItaniumCXXABI.cpp b/lib/CodeGen/ItaniumCXXABI.cpp
index 4a24e77..94828e4 100644
--- a/lib/CodeGen/ItaniumCXXABI.cpp
+++ b/lib/CodeGen/ItaniumCXXABI.cpp
@@ -1666,6 +1666,13 @@ void ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF,
// If the variable is thread-local, so is its guard variable.
guard->setThreadLocalMode(var->getThreadLocalMode());
+ // The ABI says: It is suggested that it be emitted in the same COMDAT group
+ // as the associated data object
+ if (var->isWeakForLinker() && CGM.getTriple().isOSBinFormatELF()) {
+ llvm::Comdat *C = CGM.getModule().getOrInsertComdat(var->getName());
+ guard->setComdat(C);
+ }
+
CGM.setStaticLocalDeclGuardAddress(&D, guard);
}
diff --git a/test/CodeGenCXX/static-data-member.cpp b/test/CodeGenCXX/static-data-member.cpp
index eea9794..5ec3755 100644
--- a/test/CodeGenCXX/static-data-member.cpp
+++ b/test/CodeGenCXX/static-data-member.cpp
@@ -1,9 +1,13 @@
-// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple x86_64-pc-linux -emit-llvm -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -o - %s | \
+// RUN: FileCheck --check-prefix=MACHO %s
// CHECK: @_ZN5test11A1aE = constant i32 10, align 4
// CHECK: @_ZN5test212_GLOBAL__N_11AIiE1xE = internal global i32 0, align 4
-// CHECK: @_ZN5test31AIiE1xE = weak_odr global i32 0, align 4
-// CHECK: @_ZGVN5test31AIiE1xE = weak_odr global i64 0
+// CHECK: @_ZN5test31AIiE1xE = weak_odr global i32 0, comdat $_ZN5test31AIiE1xE, align 4
+// CHECK: @_ZGVN5test31AIiE1xE = weak_odr global i64 0, comdat $_ZN5test31AIiE1xE
+// MACHO: @_ZGVN5test31AIiE1xE = weak_odr global i64 0
+// MACHO-NOT: comdat
// CHECK: _ZN5test51U2k0E = global i32 0
// CHECK: _ZN5test51U2k1E = global i32 0
@@ -60,7 +64,9 @@ namespace test3 {
template <class T> int A<T>::x = foo();
template struct A<int>;
- // CHECK-LABEL: define internal void @__cxx_global_var_init1()
+ // CHECK-LABEL: define internal void @__cxx_global_var_init1() {{.*}} comdat $_ZN5test31AIiE1xE
+ // MACHO-LABEL: define internal void @__cxx_global_var_init1()
+ // MACHO-NOT: comdat
// CHECK: [[GUARDBYTE:%.*]] = load i8* bitcast (i64* @_ZGVN5test31AIiE1xE to i8*)
// CHECK-NEXT: [[UNINITIALIZED:%.*]] = icmp eq i8 [[GUARDBYTE]], 0
// CHECK-NEXT: br i1 [[UNINITIALIZED]]
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits