[cfe-commits] r78159 - in /cfe/trunk: lib/CodeGen/CodeGenModule.cpp test/CodeGen/global-init.c

Chris Lattner sabre at nondot.org
Tue Aug 4 22:20:29 PDT 2009


Author: lattner
Date: Wed Aug  5 00:20:29 2009
New Revision: 78159

URL: http://llvm.org/viewvc/llvm-project?rev=78159&view=rev
Log:
weak globals that are const should get weak_odr linkage.

add a fixme about C++ const.

Modified:
    cfe/trunk/lib/CodeGen/CodeGenModule.cpp
    cfe/trunk/test/CodeGen/global-init.c

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=78159&r1=78158&r2=78159&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Wed Aug  5 00:20:29 2009
@@ -870,7 +870,15 @@
   }
 
   GV->setInitializer(Init);
-  GV->setConstant(D->getType().isConstant(Context));
+
+  // If it is safe to mark the global 'constant', do so now.
+  GV->setConstant(false);
+  if (D->getType().isConstant(Context)) {
+    // FIXME: In C++, if the variable has a non-trivial ctor/dtor or any mutable
+    // members, it cannot be declared "LLVM const".
+    GV->setConstant(true);
+  }
+  
   GV->setAlignment(getContext().getDeclAlignInBytes(D));
 
   // Set the llvm linkage type as appropriate.
@@ -880,13 +888,18 @@
     GV->setLinkage(llvm::Function::DLLImportLinkage);
   else if (D->hasAttr<DLLExportAttr>())
     GV->setLinkage(llvm::Function::DLLExportLinkage);
-  else if (D->hasAttr<WeakAttr>())
-    GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage);
-  else if (!CompileOpts.NoCommon &&
+  else if (D->hasAttr<WeakAttr>()) {
+    if (GV->isConstant())
+      GV->setLinkage(llvm::GlobalVariable::WeakODRLinkage);
+    else
+      GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage);
+  } else if (!CompileOpts.NoCommon &&
            !D->hasExternalStorage() && !D->getInit() &&
-           !D->getAttr<SectionAttr>())
+           !D->getAttr<SectionAttr>()) {
     GV->setLinkage(llvm::GlobalVariable::CommonLinkage);
-  else
+    // common vars aren't constant even if declared const.
+    GV->setConstant(false);
+  } else
     GV->setLinkage(llvm::GlobalVariable::ExternalLinkage);
 
   SetCommonAttributes(D, GV);

Modified: cfe/trunk/test/CodeGen/global-init.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/global-init.c?rev=78159&r1=78158&r2=78159&view=diff

==============================================================================
--- cfe/trunk/test/CodeGen/global-init.c (original)
+++ cfe/trunk/test/CodeGen/global-init.c Wed Aug  5 00:20:29 2009
@@ -7,8 +7,24 @@
 int a = 242;
 // CHECK: @a = global i32 242
 
+// This should get normal weak linkage.
+int c __attribute__((weak))= 0;
+// CHECK: @c = weak global i32 0
+
+
+
+// Since this is marked const, it should get weak_odr linkage, since all
+// definitions have to be the same.
+// CHECK: @d = weak_odr constant i32 0
+const int d __attribute__((weak))= 0;
+
+
+
+// NOTE: tentative definitions are processed at the end of the translation unit.
+
 // This shouldn't be emitted as common because it has an explicit section.
 // rdar://7119244
 int b __attribute__((section("foo")));
 
 // CHECK: @b = global i32 0, section "foo"
+





More information about the cfe-commits mailing list