[lld] r280144 - [LTO] Fix the logic for dropping unnamed_addr.

Davide Italiano via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 30 13:15:04 PDT 2016


Author: davide
Date: Tue Aug 30 15:15:03 2016
New Revision: 280144

URL: http://llvm.org/viewvc/llvm-project?rev=280144&view=rev
Log:
[LTO] Fix the logic for dropping unnamed_addr.

Differential Revision:  https://reviews.llvm.org/D24037

Added:
    lld/trunk/test/ELF/lto/Inputs/unnamed-addr-drop.ll
    lld/trunk/test/ELF/lto/unnamed-addr-drop.ll
Modified:
    lld/trunk/ELF/InputFiles.cpp
    lld/trunk/ELF/LTO.cpp
    lld/trunk/ELF/SymbolTable.cpp
    lld/trunk/ELF/SymbolTable.h
    lld/trunk/ELF/Symbols.h
    lld/trunk/test/ELF/lto/unnamed-addr-lib.ll

Modified: lld/trunk/ELF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.cpp?rev=280144&r1=280143&r2=280144&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.cpp (original)
+++ lld/trunk/ELF/InputFiles.cpp Tue Aug 30 15:15:03 2016
@@ -382,12 +382,13 @@ SymbolBody *elf::ObjectFile<ELFT>::creat
   case SHN_UNDEF:
     return elf::Symtab<ELFT>::X
         ->addUndefined(Name, Binding, Sym->st_other, Sym->getType(),
-                       /*CanOmitFromDynSym*/ false, this)
+                       /*CanOmitFromDynSym*/ false, /*HasUnnamedAddr*/ false,
+                       this)
         ->body();
   case SHN_COMMON:
     return elf::Symtab<ELFT>::X
         ->addCommon(Name, Sym->st_size, Sym->st_value, Binding, Sym->st_other,
-                    Sym->getType(), this)
+                    Sym->getType(), /*HasUnnamedAddr*/ false, this)
         ->body();
   }
 
@@ -400,7 +401,8 @@ SymbolBody *elf::ObjectFile<ELFT>::creat
     if (Sec == &InputSection<ELFT>::Discarded)
       return elf::Symtab<ELFT>::X
           ->addUndefined(Name, Binding, Sym->st_other, Sym->getType(),
-                         /*CanOmitFromDynSym*/ false, this)
+                         /*CanOmitFromDynSym*/ false,
+                         /*HasUnnamedAddr*/ false, this)
           ->body();
     return elf::Symtab<ELFT>::X->addRegular(Name, *Sym, Sec)->body();
   }
@@ -646,23 +648,29 @@ Symbol *BitcodeFile::createSymbol(const
   }
 
   uint8_t Visibility;
-  if (GV)
+  bool HasUnnamedAddr = false;
+  if (GV) {
     Visibility = getGvVisibility(GV);
-  else
+    HasUnnamedAddr =
+        GV->getUnnamedAddr() == llvm::GlobalValue::UnnamedAddr::Global;
+  } else {
     // FIXME: Set SF_Hidden flag correctly for module asm symbols, and expose
     // protected visibility.
     Visibility = STV_DEFAULT;
+  }
 
   if (GV)
     if (const Comdat *C = GV->getComdat())
       if (!KeptComdats.count(C))
         return Symtab<ELFT>::X->addUndefined(NameRef, Binding, Visibility, Type,
-                                             CanOmitFromDynSym, this);
+                                             CanOmitFromDynSym, HasUnnamedAddr,
+                                             this);
 
   const Module &M = Obj.getModule();
   if (Flags & BasicSymbolRef::SF_Undefined)
     return Symtab<ELFT>::X->addUndefined(NameRef, Binding, Visibility, Type,
-                                         CanOmitFromDynSym, this);
+                                         CanOmitFromDynSym, HasUnnamedAddr,
+                                         this);
   if (Flags & BasicSymbolRef::SF_Common) {
     // FIXME: Set SF_Common flag correctly for module asm symbols, and expose
     // size and alignment.
@@ -670,10 +678,11 @@ Symbol *BitcodeFile::createSymbol(const
     const DataLayout &DL = M.getDataLayout();
     uint64_t Size = DL.getTypeAllocSize(GV->getValueType());
     return Symtab<ELFT>::X->addCommon(NameRef, Size, GV->getAlignment(),
-                                      Binding, Visibility, STT_OBJECT, this);
+                                      Binding, Visibility, STT_OBJECT,
+                                      HasUnnamedAddr, this);
   }
   return Symtab<ELFT>::X->addBitcode(NameRef, IsWeak, Visibility, Type,
-                                     CanOmitFromDynSym, this);
+                                     CanOmitFromDynSym, HasUnnamedAddr, this);
 }
 
 bool BitcodeFile::shouldSkip(uint32_t Flags) {

Modified: lld/trunk/ELF/LTO.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LTO.cpp?rev=280144&r1=280143&r2=280144&view=diff
==============================================================================
--- lld/trunk/ELF/LTO.cpp (original)
+++ lld/trunk/ELF/LTO.cpp Tue Aug 30 15:15:03 2016
@@ -194,6 +194,12 @@ void BitcodeCompiler::add(BitcodeFile &F
     if (BitcodeFile::shouldSkip(Flags))
       continue;
     Symbol *S = Syms[BodyIndex++];
+    if (GV) {
+      if (S->HasUnnamedAddr)
+        GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
+      else
+        GV->setUnnamedAddr(GlobalValue::UnnamedAddr::None);
+    }
     if (Flags & BasicSymbolRef::SF_Undefined) {
       handleUndefinedAsmRefs(Sym, GV, AsmUndefinedRefs);
       continue;

Modified: lld/trunk/ELF/SymbolTable.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SymbolTable.cpp?rev=280144&r1=280143&r2=280144&view=diff
==============================================================================
--- lld/trunk/ELF/SymbolTable.cpp (original)
+++ lld/trunk/ELF/SymbolTable.cpp Tue Aug 30 15:15:03 2016
@@ -219,6 +219,7 @@ std::pair<Symbol *, bool> SymbolTable<EL
     Sym->Binding = STB_WEAK;
     Sym->Visibility = STV_DEFAULT;
     Sym->IsUsedInRegularObj = false;
+    Sym->HasUnnamedAddr = true;
     Sym->ExportDynamic = false;
     Sym->Traced = V.Traced;
     std::tie(Name, Sym->VersionId) = getSymbolVersion(Name);
@@ -234,12 +235,14 @@ std::pair<Symbol *, bool> SymbolTable<EL
 template <class ELFT>
 std::pair<Symbol *, bool>
 SymbolTable<ELFT>::insert(StringRef &Name, uint8_t Type, uint8_t Visibility,
-                          bool CanOmitFromDynSym, bool IsUsedInRegularObj,
-                          InputFile *File) {
+                          bool CanOmitFromDynSym, bool HasUnnamedAddr,
+                          bool IsUsedInRegularObj, InputFile *File) {
   Symbol *S;
   bool WasInserted;
   std::tie(S, WasInserted) = insert(Name);
 
+  // Merge in the new unnamed_addr attribute.
+  S->HasUnnamedAddr &= HasUnnamedAddr;
   // Merge in the new symbol's visibility.
   S->Visibility = getMinVisibility(S->Visibility, Visibility);
   if (!CanOmitFromDynSym && (Config->Shared || Config->ExportDynamic))
@@ -268,18 +271,19 @@ std::string SymbolTable<ELFT>::conflictM
 
 template <class ELFT> Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name) {
   return addUndefined(Name, STB_GLOBAL, STV_DEFAULT, /*Type*/ 0,
-                      /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
+                      /*CanOmitFromDynSym*/ false, /*HasUnnamedAddr*/ false,
+                      /*File*/ nullptr);
 }
 
 template <class ELFT>
 Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name, uint8_t Binding,
                                         uint8_t StOther, uint8_t Type,
                                         bool CanOmitFromDynSym,
-                                        InputFile *File) {
+                                        bool HasUnnamedAddr, InputFile *File) {
   Symbol *S;
   bool WasInserted;
   std::tie(S, WasInserted) =
-      insert(Name, Type, StOther & 3, CanOmitFromDynSym,
+      insert(Name, Type, StOther & 3, CanOmitFromDynSym, HasUnnamedAddr,
              /*IsUsedInRegularObj*/ !File || !isa<BitcodeFile>(File), File);
   if (WasInserted) {
     S->Binding = Binding;
@@ -343,11 +347,11 @@ template <class ELFT>
 Symbol *SymbolTable<ELFT>::addCommon(StringRef N, uint64_t Size,
                                      uint64_t Alignment, uint8_t Binding,
                                      uint8_t StOther, uint8_t Type,
-                                     InputFile *File) {
+                                     bool HasUnnamedAddr, InputFile *File) {
   Symbol *S;
   bool WasInserted;
   std::tie(S, WasInserted) =
-      insert(N, Type, StOther & 3, /*CanOmitFromDynSym*/ false,
+      insert(N, Type, StOther & 3, /*CanOmitFromDynSym*/ false, HasUnnamedAddr,
              /*IsUsedInRegularObj*/ true, File);
   int Cmp = compareDefined(S, WasInserted, Binding);
   if (Cmp > 0) {
@@ -387,10 +391,10 @@ Symbol *SymbolTable<ELFT>::addRegular(St
                                       InputSectionBase<ELFT> *Section) {
   Symbol *S;
   bool WasInserted;
-  std::tie(S, WasInserted) =
-      insert(Name, Sym.getType(), Sym.getVisibility(),
-             /*CanOmitFromDynSym*/ false, /*IsUsedInRegularObj*/ true,
-             Section ? Section->getFile() : nullptr);
+  std::tie(S, WasInserted) = insert(
+      Name, Sym.getType(), Sym.getVisibility(),
+      /*CanOmitFromDynSym*/ false, /*HasUnnamedAddr*/ false,
+      /*IsUsedInRegularObj*/ true, Section ? Section->getFile() : nullptr);
   int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted, Sym.getBinding());
   if (Cmp > 0)
     replaceBody<DefinedRegular<ELFT>>(S, Name, Sym, Section);
@@ -406,7 +410,7 @@ Symbol *SymbolTable<ELFT>::addRegular(St
   bool WasInserted;
   std::tie(S, WasInserted) =
       insert(Name, STT_NOTYPE, StOther & 3, /*CanOmitFromDynSym*/ false,
-             /*IsUsedInRegularObj*/ true, nullptr);
+             /*HasUnnamedAddr*/ false, /*IsUsedInRegularObj*/ true, nullptr);
   int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted, Binding);
   if (Cmp > 0)
     replaceBody<DefinedRegular<ELFT>>(S, Name, StOther);
@@ -423,6 +427,7 @@ Symbol *SymbolTable<ELFT>::addSynthetic(
   bool WasInserted;
   std::tie(S, WasInserted) = insert(N, STT_NOTYPE, /*Visibility*/ StOther & 0x3,
                                     /*CanOmitFromDynSym*/ false,
+                                    /*HasUnnamedAddr*/ false,
                                     /*IsUsedInRegularObj*/ true, nullptr);
   int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted, STB_GLOBAL);
   if (Cmp > 0)
@@ -443,7 +448,7 @@ void SymbolTable<ELFT>::addShared(Shared
   bool WasInserted;
   std::tie(S, WasInserted) =
       insert(Name, Sym.getType(), STV_DEFAULT, /*CanOmitFromDynSym*/ true,
-             /*IsUsedInRegularObj*/ false, F);
+             /*HasUnnamedAddr*/ false, /*IsUsedInRegularObj*/ false, F);
   // Make sure we preempt DSO symbols with default visibility.
   if (Sym.getVisibility() == STV_DEFAULT)
     S->ExportDynamic = true;
@@ -457,11 +462,13 @@ void SymbolTable<ELFT>::addShared(Shared
 template <class ELFT>
 Symbol *SymbolTable<ELFT>::addBitcode(StringRef Name, bool IsWeak,
                                       uint8_t StOther, uint8_t Type,
-                                      bool CanOmitFromDynSym, BitcodeFile *F) {
+                                      bool CanOmitFromDynSym,
+                                      bool HasUnnamedAddr, BitcodeFile *F) {
   Symbol *S;
   bool WasInserted;
-  std::tie(S, WasInserted) = insert(Name, Type, StOther & 3, CanOmitFromDynSym,
-                                    /*IsUsedInRegularObj*/ false, F);
+  std::tie(S, WasInserted) =
+      insert(Name, Type, StOther & 3, CanOmitFromDynSym, HasUnnamedAddr,
+             /*IsUsedInRegularObj*/ false, F);
   int Cmp = compareDefinedNonCommon<ELFT>(S, WasInserted,
                                           IsWeak ? STB_WEAK : STB_GLOBAL);
   if (Cmp > 0)

Modified: lld/trunk/ELF/SymbolTable.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SymbolTable.h?rev=280144&r1=280143&r2=280144&view=diff
==============================================================================
--- lld/trunk/ELF/SymbolTable.h (original)
+++ lld/trunk/ELF/SymbolTable.h Tue Aug 30 15:15:03 2016
@@ -59,7 +59,8 @@ public:
 
   Symbol *addUndefined(StringRef Name);
   Symbol *addUndefined(StringRef Name, uint8_t Binding, uint8_t StOther,
-                       uint8_t Type, bool CanOmitFromDynSym, InputFile *File);
+                       uint8_t Type, bool CanOmitFromDynSym,
+                       bool HasUnnamedAddr, InputFile *File);
 
   Symbol *addRegular(StringRef Name, const Elf_Sym &Sym,
                      InputSectionBase<ELFT> *Section);
@@ -72,11 +73,12 @@ public:
   void addLazyArchive(ArchiveFile *F, const llvm::object::Archive::Symbol S);
   void addLazyObject(StringRef Name, LazyObjectFile &Obj);
   Symbol *addBitcode(StringRef Name, bool IsWeak, uint8_t StOther, uint8_t Type,
-                     bool CanOmitFromDynSym, BitcodeFile *File);
+                     bool CanOmitFromDynSym, bool HasUnnamedAddr,
+                     BitcodeFile *File);
 
   Symbol *addCommon(StringRef N, uint64_t Size, uint64_t Alignment,
                     uint8_t Binding, uint8_t StOther, uint8_t Type,
-                    InputFile *File);
+                    bool HasUnnamedAddr, InputFile *File);
 
   void scanUndefinedFlags();
   void scanShlibUndefined();
@@ -93,7 +95,8 @@ private:
   std::pair<Symbol *, bool> insert(StringRef &Name);
   std::pair<Symbol *, bool> insert(StringRef &Name, uint8_t Type,
                                    uint8_t Visibility, bool CanOmitFromDynSym,
-                                   bool IsUsedInRegularObj, InputFile *File);
+                                   bool HasUnnamedAddr, bool IsUsedInRegularObj,
+                                   InputFile *File);
 
   std::string conflictMsg(SymbolBody *Existing, InputFile *NewFile);
   void reportDuplicate(SymbolBody *Existing, InputFile *NewFile);

Modified: lld/trunk/ELF/Symbols.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Symbols.h?rev=280144&r1=280143&r2=280144&view=diff
==============================================================================
--- lld/trunk/ELF/Symbols.h (original)
+++ lld/trunk/ELF/Symbols.h Tue Aug 30 15:15:03 2016
@@ -413,6 +413,9 @@ struct Symbol {
   // observed non-DSO symbols.
   unsigned Visibility : 2;
 
+  // True if the symbol has unnamed_addr.
+  unsigned HasUnnamedAddr : 1;
+
   // True if the symbol was used for linking and thus need to be added to the
   // output file's symbol table. This is true for all symbols except for
   // unreferenced DSO symbols and bitcode symbols that are unreferenced except

Added: lld/trunk/test/ELF/lto/Inputs/unnamed-addr-drop.ll
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/lto/Inputs/unnamed-addr-drop.ll?rev=280144&view=auto
==============================================================================
--- lld/trunk/test/ELF/lto/Inputs/unnamed-addr-drop.ll (added)
+++ lld/trunk/test/ELF/lto/Inputs/unnamed-addr-drop.ll Tue Aug 30 15:15:03 2016
@@ -0,0 +1,4 @@
+target triple = "x86_64-unknown-linux-gnu"
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+
+ at foo = unnamed_addr constant i32 42

Added: lld/trunk/test/ELF/lto/unnamed-addr-drop.ll
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/lto/unnamed-addr-drop.ll?rev=280144&view=auto
==============================================================================
--- lld/trunk/test/ELF/lto/unnamed-addr-drop.ll (added)
+++ lld/trunk/test/ELF/lto/unnamed-addr-drop.ll Tue Aug 30 15:15:03 2016
@@ -0,0 +1,12 @@
+; RUN: llvm-as %s -o %t1.o
+; RUN: llvm-as %S/Inputs/unnamed-addr-drop.ll -o %t2.o
+; RUN: ld.lld -m elf_x86_64 %t1.o %t2.o -o %t.so -save-temps -shared
+; RUN: llvm-dis %t.so.lto.bc -o - | FileCheck %s
+
+target triple = "x86_64-unknown-linux-gnu"
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+
+ at foo = weak constant i32 41
+
+; Check that unnamed_addr is dropped during the merge.
+; CHECK: @foo = constant i32 42

Modified: lld/trunk/test/ELF/lto/unnamed-addr-lib.ll
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/lto/unnamed-addr-lib.ll?rev=280144&r1=280143&r2=280144&view=diff
==============================================================================
--- lld/trunk/test/ELF/lto/unnamed-addr-lib.ll (original)
+++ lld/trunk/test/ELF/lto/unnamed-addr-lib.ll Tue Aug 30 15:15:03 2016
@@ -11,8 +11,8 @@
 ; We could add one extra bit for ODR so that we know that preemption is not
 ; necessary, but that is probably not worth it.
 
-; CHECK: @foo = internal unnamed_addr constant i8 42
-; CHECK: @bar = weak_odr unnamed_addr constant i8 42
+; CHECK: @foo = internal constant i8 42
+; CHECK: @bar = weak_odr constant i8 42
 
 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
 target triple = "x86_64-unknown-linux-gnu"




More information about the llvm-commits mailing list