[llvm] r264192 - Fix logic for which symbols to keep with comdats.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 23 14:16:33 PDT 2016


Author: rafael
Date: Wed Mar 23 16:16:33 2016
New Revision: 264192

URL: http://llvm.org/viewvc/llvm-project?rev=264192&view=rev
Log:
Fix logic for which symbols to keep with comdats.

If a comdat is dropped, all symbols in it are dropped.
If a comdat is kept, the symbols survive to pass regular symbol
resolution.
With this patch we do that for all global symbols.

The added test is a copy of test/tools/gold/X86/comdat.ll that we now
pass.

Added:
    llvm/trunk/test/Linker/Inputs/comdat16.ll
    llvm/trunk/test/Linker/comdat16.ll
Modified:
    llvm/trunk/lib/Linker/LinkModules.cpp
    llvm/trunk/test/Linker/constructor-comdat.ll

Modified: llvm/trunk/lib/Linker/LinkModules.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Linker/LinkModules.cpp?rev=264192&r1=264191&r2=264192&view=diff
==============================================================================
--- llvm/trunk/lib/Linker/LinkModules.cpp (original)
+++ llvm/trunk/lib/Linker/LinkModules.cpp Wed Mar 23 16:16:33 2016
@@ -422,9 +422,8 @@ bool ModuleLinker::linkIfNeeded(GlobalVa
     bool LinkFromSrc;
     Comdat::SelectionKind SK;
     std::tie(SK, LinkFromSrc) = ComdatsChosen[SC];
-    if (LinkFromSrc)
-      ValuesToLink.insert(&GV);
-    return false;
+    if (!LinkFromSrc)
+      return false;
   }
 
   bool LinkFromSrc = true;
@@ -565,7 +564,8 @@ bool ModuleLinker::run() {
     if (!SC)
       continue;
     for (GlobalValue *GV2 : ComdatMembers[SC])
-      ValuesToLink.insert(GV2);
+      if (GV2->hasInternalLinkage())
+        ValuesToLink.insert(GV2);
   }
 
   if (shouldInternalizeLinkedSymbols()) {

Added: llvm/trunk/test/Linker/Inputs/comdat16.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Linker/Inputs/comdat16.ll?rev=264192&view=auto
==============================================================================
--- llvm/trunk/test/Linker/Inputs/comdat16.ll (added)
+++ llvm/trunk/test/Linker/Inputs/comdat16.ll Wed Mar 23 16:16:33 2016
@@ -0,0 +1,26 @@
+$c2 = comdat any
+$c1 = comdat any
+
+; This is only present in this file. The linker will keep $c1 from the first
+; file and this will be undefined.
+ at will_be_undefined = global i32 1, comdat($c1)
+ at use = global i32* @will_be_undefined
+
+ at v1 = weak_odr global i32 41, comdat($c2)
+define weak_odr protected i32 @f1(i8* %this) comdat($c2) {
+bb20:
+  store i8* %this, i8** null
+  br label %bb21
+bb21:
+  ret i32 41
+}
+
+ at r21 = global i32* @v1
+ at r22 = global i32(i8*)* @f1
+
+ at a21 = alias i32, i32* @v1
+ at a22 = alias i16, bitcast (i32* @v1 to i16*)
+
+ at a23 = alias i32(i8*), i32(i8*)* @f1
+ at a24 = alias i16, bitcast (i32(i8*)* @f1 to i16*)
+ at a25 = alias i16, i16* @a24

Added: llvm/trunk/test/Linker/comdat16.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Linker/comdat16.ll?rev=264192&view=auto
==============================================================================
--- llvm/trunk/test/Linker/comdat16.ll (added)
+++ llvm/trunk/test/Linker/comdat16.ll Wed Mar 23 16:16:33 2016
@@ -0,0 +1,63 @@
+; RUN: llvm-link -S -o - %s %p/Inputs/comdat16.ll | FileCheck %s
+
+$c1 = comdat any
+
+ at v1 = weak_odr global i32 42, comdat($c1)
+define weak_odr i32 @f1(i8*) comdat($c1) {
+bb10:
+  br label %bb11
+bb11:
+  ret i32 42
+}
+
+ at r11 = global i32* @v1
+ at r12 = global i32 (i8*)* @f1
+
+ at a11 = alias i32, i32* @v1
+ at a12 = alias i16, bitcast (i32* @v1 to i16*)
+
+ at a13 = alias i32 (i8*), i32 (i8*)* @f1
+ at a14 = alias i16, bitcast (i32 (i8*)* @f1 to i16*)
+ at a15 = alias i16, i16* @a14
+
+; CHECK: $c1 = comdat any
+; CHECK: $c2 = comdat any
+
+; CHECK-DAG: @will_be_undefined = external global i32
+
+; CHECK-DAG: @v1 = weak_odr global i32 42, comdat($c1)
+
+; CHECK-DAG: @r11 = global i32* @v1{{$}}
+; CHECK-DAG: @r12 = global i32 (i8*)* @f1{{$}}
+
+; CHECK-DAG: @r21 = global i32* @v1{{$}}
+; CHECK-DAG: @r22 = global i32 (i8*)* @f1{{$}}
+
+; CHECK-DAG: @v1.1 = internal global i32 41, comdat($c2)
+
+; CHECK-DAG: @a11 = alias i32, i32* @v1{{$}}
+; CHECK-DAG: @a12 = alias i16, bitcast (i32* @v1 to i16*)
+
+; CHECK-DAG: @a13 = alias i32 (i8*), i32 (i8*)* @f1{{$}}
+; CHECK-DAG: @a14 = alias i16, bitcast (i32 (i8*)* @f1 to i16*)
+
+; CHECK-DAG: @a21 = alias i32, i32* @v1.1{{$}}
+; CHECK-DAG: @a22 = alias i16, bitcast (i32* @v1.1 to i16*)
+
+; CHECK-DAG: @a23 = alias i32 (i8*), i32 (i8*)* @f1.2{{$}}
+; CHECK-DAG: @a24 = alias i16, bitcast (i32 (i8*)* @f1.2 to i16*)
+
+; CHECK:      define weak_odr protected i32 @f1(i8*) comdat($c1) {
+; CHECK-NEXT: bb10:
+; CHECK-NEXT:   br label %bb11{{$}}
+; CHECK:      bb11:
+; CHECK-NEXT:   ret i32 42
+; CHECK-NEXT: }
+
+; CHECK:      define internal i32 @f1.2(i8* %this) comdat($c2) {
+; CHECK-NEXT: bb20:
+; CHECK-NEXT:   store i8* %this, i8** null
+; CHECK-NEXT:   br label %bb21
+; CHECK:      bb21:
+; CHECK-NEXT:   ret i32 41
+; CHECK-NEXT: }

Modified: llvm/trunk/test/Linker/constructor-comdat.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Linker/constructor-comdat.ll?rev=264192&r1=264191&r2=264192&view=diff
==============================================================================
--- llvm/trunk/test/Linker/constructor-comdat.ll (original)
+++ llvm/trunk/test/Linker/constructor-comdat.ll Wed Mar 23 16:16:33 2016
@@ -1,13 +1,16 @@
 ; RUN: llvm-link %s %p/Inputs/constructor-comdat.ll -S -o - 2>&1 | FileCheck %s
-; RUN: llvm-link %p/Inputs/constructor-comdat.ll %s -S -o - 2>&1 | FileCheck %s
+; RUN: llvm-link %p/Inputs/constructor-comdat.ll %s -S -o - 2>&1 | FileCheck --check-prefix=NOCOMDAT %s
 
 $_ZN3fooIiEC5Ev = comdat any
 ; CHECK: $_ZN3fooIiEC5Ev = comdat any
+; NOCOMDAT-NOT: comdat
 
 @_ZN3fooIiEC1Ev = weak_odr alias void (), void ()* @_ZN3fooIiEC2Ev
 ; CHECK: @_ZN3fooIiEC1Ev = weak_odr alias void (), void ()* @_ZN3fooIiEC2Ev
+; NOCOMDAT-DAG: define weak_odr void @_ZN3fooIiEC1Ev() {
 
 ; CHECK: define weak_odr void @_ZN3fooIiEC2Ev() comdat($_ZN3fooIiEC5Ev) {
+; NOCOMDAT-DAG: define weak_odr void @_ZN3fooIiEC2Ev() {
 define weak_odr void @_ZN3fooIiEC2Ev() comdat($_ZN3fooIiEC5Ev) {
   ret void
 }




More information about the llvm-commits mailing list