[cfe-commits] r134266 - in /cfe/trunk: lib/CodeGen/CGDeclCXX.cpp test/CodeGenCXX/global-init.cpp

Douglas Gregor dgregor at apple.com
Fri Jul 1 14:54:36 PDT 2011


Author: dgregor
Date: Fri Jul  1 16:54:36 2011
New Revision: 134266

URL: http://llvm.org/viewvc/llvm-project?rev=134266&view=rev
Log:
Emit guard variables for any weak global that has a run-time
initializer. Previously, we only used guard variables for weak static
data members. Fixes <rdar://problem/9692249>.

Modified:
    cfe/trunk/lib/CodeGen/CGDeclCXX.cpp
    cfe/trunk/test/CodeGenCXX/global-init.cpp

Modified: cfe/trunk/lib/CodeGen/CGDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDeclCXX.cpp?rev=134266&r1=134265&r2=134266&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGDeclCXX.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGDeclCXX.cpp Fri Jul  1 16:54:36 2011
@@ -269,12 +269,11 @@
                 getTypes().getNullaryFunctionInfo(),
                 FunctionArgList(), SourceLocation());
 
-  // Use guarded initialization if the global variable is weak due to
-  // being a class template's static data member.  These will always
-  // have weak_odr linkage.
-  if (Addr->getLinkage() == llvm::GlobalValue::WeakODRLinkage &&
-      D->isStaticDataMember() &&
-      D->getInstantiatedFromStaticDataMember()) {
+  // Use guarded initialization if the global variable is weak. This
+  // occurs for, e.g., instantiated static data members and
+  // definitions explicitly marked weak.
+  if (Addr->getLinkage() == llvm::GlobalValue::WeakODRLinkage ||
+      Addr->getLinkage() == llvm::GlobalValue::WeakAnyLinkage) {
     EmitCXXGuardedInit(*D, Addr);
   } else {
     EmitCXXGlobalVarDeclInit(*D, Addr);

Modified: cfe/trunk/test/CodeGenCXX/global-init.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/global-init.cpp?rev=134266&r1=134265&r2=134266&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/global-init.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/global-init.cpp Fri Jul  1 16:54:36 2011
@@ -70,6 +70,20 @@
   const char *test() { return var; }
 }
 
+namespace test6 {
+  struct A {
+    A();
+  };
+  extern int foo();
+
+  // This needs an initialization function and guard variables.
+  // CHECK: load i8* bitcast (i64* @_ZGVN5test61xE
+  // CHECK: [[CALL:%.*]] = call i32 @_ZN5test63fooEv
+  // CHECK-NEXT: store i32 [[CALL]], i32* @_ZN5test61xE
+  // CHECK-NEXT: store i64 1, i64* @_ZGVN5test61xE
+  __attribute__((weak)) int x = foo();
+}
+
 namespace PR5974 {
   struct A { int a; };
   struct B { int b; };
@@ -97,15 +111,6 @@
   };
 }
 
-namespace test6 {
-  struct A {
-    A();
-  };
-  extern int foo();
-
-  // This needs an initialization function but not guard variables.
-  __attribute__((weak)) int x = foo();
-}
 
 // At the end of the file, we check that y is initialized before z.
 





More information about the cfe-commits mailing list