[lld] r280242 - Internalize common variables.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 31 06:42:08 PDT 2016


Author: rafael
Date: Wed Aug 31 08:42:08 2016
New Revision: 280242

URL: http://llvm.org/viewvc/llvm-project?rev=280242&view=rev
Log:
Internalize common variables.

Before this lld was always creating common symbols itself. It worked,
but prevented them from being internalized when possible.

Now it preserves common symbols is the bitcode and they are internalized.

Fixes pr30184.

Added:
    lld/trunk/test/ELF/lto/Inputs/common3.ll
    lld/trunk/test/ELF/lto/common3.ll
Modified:
    lld/trunk/ELF/LTO.cpp
    lld/trunk/ELF/SymbolTable.cpp
    lld/trunk/test/ELF/lto/common2.ll

Modified: lld/trunk/ELF/LTO.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LTO.cpp?rev=280242&r1=280241&r2=280242&view=diff
==============================================================================
--- lld/trunk/ELF/LTO.cpp (original)
+++ lld/trunk/ELF/LTO.cpp Wed Aug 31 08:42:08 2016
@@ -202,8 +202,6 @@ void BitcodeCompiler::add(BitcodeFile &F
       continue;
     }
     SymbolBody *B = S->body();
-    if (B->kind() != SymbolBody::DefinedRegularKind)
-      continue;
     if (B->File != &F)
       continue;
 
@@ -221,7 +219,12 @@ void BitcodeCompiler::add(BitcodeFile &F
     // needs to be able to replace the original definition without conflicting.
     // In the latter case, we need to allow the combined LTO object to provide a
     // definition with the same name, for example when doing parallel codegen.
-    undefine(S);
+    if (auto *C = dyn_cast<DefinedCommon>(B)) {
+      if (auto *GO = dyn_cast<GlobalObject>(GV))
+        GO->setAlignment(C->Alignment);
+    } else {
+      undefine(S);
+    }
 
     if (!GV)
       // Module asm symbol.

Modified: lld/trunk/ELF/SymbolTable.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SymbolTable.cpp?rev=280242&r1=280241&r2=280242&view=diff
==============================================================================
--- lld/trunk/ELF/SymbolTable.cpp (original)
+++ lld/trunk/ELF/SymbolTable.cpp Wed Aug 31 08:42:08 2016
@@ -351,7 +351,7 @@ Symbol *SymbolTable<ELFT>::addCommon(Str
   bool WasInserted;
   std::tie(S, WasInserted) =
       insert(N, Type, StOther & 3, /*CanOmitFromDynSym*/ false, HasUnnamedAddr,
-             /*IsUsedInRegularObj*/ true, File);
+             !isa<BitcodeFile>(File), File);
   int Cmp = compareDefined(S, WasInserted, Binding);
   if (Cmp > 0) {
     S->Binding = Binding;
@@ -368,8 +368,9 @@ Symbol *SymbolTable<ELFT>::addCommon(Str
     if (Config->WarnCommon)
       warning("multiple common of " + S->body()->getName());
 
-    C->Size = std::max(C->Size, Size);
-    C->Alignment = std::max(C->Alignment, Alignment);
+    Alignment = C->Alignment = std::max(C->Alignment, Alignment);
+    if (Size > C->Size)
+      replaceBody<DefinedCommon>(S, N, Size, Alignment, StOther, Type, File);
   }
   return S;
 }

Added: lld/trunk/test/ELF/lto/Inputs/common3.ll
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/lto/Inputs/common3.ll?rev=280242&view=auto
==============================================================================
--- lld/trunk/test/ELF/lto/Inputs/common3.ll (added)
+++ lld/trunk/test/ELF/lto/Inputs/common3.ll Wed Aug 31 08:42:08 2016
@@ -0,0 +1,3 @@
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+ at a = common hidden global i64 0, align 4

Modified: lld/trunk/test/ELF/lto/common2.ll
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/lto/common2.ll?rev=280242&r1=280241&r2=280242&view=diff
==============================================================================
--- lld/trunk/test/ELF/lto/common2.ll (original)
+++ lld/trunk/test/ELF/lto/common2.ll Wed Aug 31 08:42:08 2016
@@ -7,15 +7,18 @@ target datalayout = "e-m:e-i64:64-f80:12
 target triple = "x86_64-unknown-linux-gnu"
 
 @a = common global i8 0, align 8
+; CHECK-DAG: @a = common global i8 0, align 8
 
-; Shared library case, we ensure that the bitcode generated file
-; has not the a symbol but is appears in the final shared library
-; produced.
-; CHECK-NOT: @a = common global i8 0, align 8
+ at b = common hidden global i32 0, align 4
+define i32 @f() {
+  %t = load i32, i32* @b, align 4
+  ret i32 %t
+}
+; CHECK-DAG: @b = internal global i32 0, align 4
 
 ; SHARED: Symbol {
 ; SHARED:   Name: a
-; SHARED-NEXT:   Value: 0x2000
+; SHARED-NEXT:   Value:
 ; SHARED-NEXT:   Size: 1
 ; SHARED-NEXT:   Binding: Global
 ; SHARED-NEXT:   Type: Object

Added: lld/trunk/test/ELF/lto/common3.ll
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/lto/common3.ll?rev=280242&view=auto
==============================================================================
--- lld/trunk/test/ELF/lto/common3.ll (added)
+++ lld/trunk/test/ELF/lto/common3.ll Wed Aug 31 08:42:08 2016
@@ -0,0 +1,14 @@
+; RUN: llvm-as %s -o %t1.o
+; RUN: llvm-as %S/Inputs/common3.ll -o %t2.o
+; RUN: ld.lld -m elf_x86_64 %t1.o %t2.o -o %t -shared -save-temps
+; RUN: llvm-dis < %t.lto.bc | FileCheck %s
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+ at a = common hidden global i32 0, align 8
+define i32 @f() {
+  %t = load i32, i32* @a, align 4
+  ret i32 %t
+}
+
+; CHECK: @a = internal global i64 0, align 8




More information about the llvm-commits mailing list