[lld] [lld][ELF] Resolve Symbols with same comdat index as Defined Symbols (PR #115140)

via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 9 05:27:35 PST 2024


https://github.com/pranav-159 updated https://github.com/llvm/llvm-project/pull/115140

>From 1cde8af787e1a11f6cdd773adde7abdd2f2147d7 Mon Sep 17 00:00:00 2001
From: pranav <Pranav.Gorantla at amd.com>
Date: Wed, 6 Nov 2024 14:45:43 +0530
Subject: [PATCH 1/2] [lld][ELF] Resolve Symbols with same comdat index as
 Defined Symbols

---
 lld/ELF/InputFiles.cpp                        | 13 +++++--
 .../comdat-weakodr-linkonceodr-visibility.ll  | 36 +++++++++++++++++++
 2 files changed, 47 insertions(+), 2 deletions(-)
 create mode 100644 lld/test/ELF/lto/comdat-weakodr-linkonceodr-visibility.ll

diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index 2084fcfd4d651a..e516f34c6badd9 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -1726,13 +1726,22 @@ static void createBitcodeSymbol(Ctx &ctx, Symbol *&sym,
     sym = ctx.symtab->insert(objSym.getName());
   }
 
-  int c = objSym.getComdatIndex();
-  if (objSym.isUndefined() || (c != -1 && !keptComdats[c])) {
+  if (objSym.isUndefined()) {
     Undefined newSym(&f, StringRef(), binding, visibility, type);
     sym->resolve(ctx, newSym);
     sym->referenced = true;
     return;
   }
+  int c = objSym.getComdatIndex();
+  if (c != -1 && !keptComdats[c]) {
+    Defined newSym(ctx, &f, StringRef(), binding, visibility, type, 0, 0,
+                   nullptr);
+    if (objSym.canBeOmittedFromSymbolTable())
+      newSym.exportDynamic = false;
+    sym->resolve(ctx, newSym);
+    sym->referenced = true;
+    return;
+  }
 
   if (objSym.isCommon()) {
     sym->resolve(ctx, CommonSymbol{ctx, &f, StringRef(), binding, visibility,
diff --git a/lld/test/ELF/lto/comdat-weakodr-linkonceodr-visibility.ll b/lld/test/ELF/lto/comdat-weakodr-linkonceodr-visibility.ll
new file mode 100644
index 00000000000000..776c7ff6f6bee7
--- /dev/null
+++ b/lld/test/ELF/lto/comdat-weakodr-linkonceodr-visibility.ll
@@ -0,0 +1,36 @@
+; RUN: rm -rf %t.dir
+; RUN: split-file %s %t.dir
+; RUN: cd %t.dir
+; RUN: echo %t.dir
+; RUN: llvm-as explicit.ll -o explicit.bc
+; RUN: llvm-as implicit.ll -o implicit.bc
+
+
+;; Case 1:
+; RUN: ld.lld explicit.bc implicit.bc -o case1.so -shared -save-temps
+; RUN: llvm-nm case1.so.0.2.internalize.bc | FileCheck %s
+
+;; Case 2:
+; RUN: ld.lld implicit.bc explicit.bc -o case2.so -shared -save-temps
+; RUN: llvm-nm case2.so.0.2.internalize.bc | FileCheck %s
+
+; CHECK: W foo
+
+;--- explicit.ll
+target triple = "x86_64-unknown-linux-gnu"
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+
+$foo = comdat any
+define weak_odr void @foo() local_unnamed_addr comdat {
+  ret void
+}
+
+
+;--- implicit.ll
+target triple = "x86_64-unknown-linux-gnu"
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+
+$foo = comdat any
+define linkonce_odr void @foo() local_unnamed_addr comdat {
+  ret void
+}

>From 1ffb3189353ed521474edb85c84c3e79e903c07e Mon Sep 17 00:00:00 2001
From: pranav <Pranav.Gorantla at amd.com>
Date: Mon, 9 Dec 2024 18:41:04 +0530
Subject: [PATCH 2/2] Used ltoCanOmit and modifed the InputFiles.cpp
 Accordingly, Modified and added the testcase to internalize-exportdyn.ll

---
 lld/ELF/InputFiles.cpp                        |  5 ++-
 .../ELF/lto/Inputs/internalize-exportdyn.ll   | 10 ++++++
 .../comdat-weakodr-linkonceodr-visibility.ll  | 36 -------------------
 lld/test/ELF/lto/internalize-exportdyn.ll     | 15 ++++++++
 4 files changed, 27 insertions(+), 39 deletions(-)
 delete mode 100644 lld/test/ELF/lto/comdat-weakodr-linkonceodr-visibility.ll

diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index e516f34c6badd9..c335e931867343 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -1736,10 +1736,9 @@ static void createBitcodeSymbol(Ctx &ctx, Symbol *&sym,
   if (c != -1 && !keptComdats[c]) {
     Defined newSym(ctx, &f, StringRef(), binding, visibility, type, 0, 0,
                    nullptr);
-    if (objSym.canBeOmittedFromSymbolTable())
-      newSym.exportDynamic = false;
+    sym->ltoCanOmit = objSym.canBeOmittedFromSymbolTable() &&
+                      (!sym->isDefined() || sym->ltoCanOmit);
     sym->resolve(ctx, newSym);
-    sym->referenced = true;
     return;
   }
 
diff --git a/lld/test/ELF/lto/Inputs/internalize-exportdyn.ll b/lld/test/ELF/lto/Inputs/internalize-exportdyn.ll
index 585b99ae5a513a..3166166a658560 100644
--- a/lld/test/ELF/lto/Inputs/internalize-exportdyn.ll
+++ b/lld/test/ELF/lto/Inputs/internalize-exportdyn.ll
@@ -1,6 +1,16 @@
 target triple = "x86_64-unknown-linux-gnu"
 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
 
+$f1 = comdat any
+$f2 = comdat any
+
 define weak_odr void @bah() {
   ret void
 }
+
+define linkonce_odr void @f1() local_unnamed_addr comdat {
+  ret void
+}
+define weak_odr void @f2() local_unnamed_addr comdat {
+  ret void
+}
\ No newline at end of file
diff --git a/lld/test/ELF/lto/comdat-weakodr-linkonceodr-visibility.ll b/lld/test/ELF/lto/comdat-weakodr-linkonceodr-visibility.ll
deleted file mode 100644
index 776c7ff6f6bee7..00000000000000
--- a/lld/test/ELF/lto/comdat-weakodr-linkonceodr-visibility.ll
+++ /dev/null
@@ -1,36 +0,0 @@
-; RUN: rm -rf %t.dir
-; RUN: split-file %s %t.dir
-; RUN: cd %t.dir
-; RUN: echo %t.dir
-; RUN: llvm-as explicit.ll -o explicit.bc
-; RUN: llvm-as implicit.ll -o implicit.bc
-
-
-;; Case 1:
-; RUN: ld.lld explicit.bc implicit.bc -o case1.so -shared -save-temps
-; RUN: llvm-nm case1.so.0.2.internalize.bc | FileCheck %s
-
-;; Case 2:
-; RUN: ld.lld implicit.bc explicit.bc -o case2.so -shared -save-temps
-; RUN: llvm-nm case2.so.0.2.internalize.bc | FileCheck %s
-
-; CHECK: W foo
-
-;--- explicit.ll
-target triple = "x86_64-unknown-linux-gnu"
-target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
-
-$foo = comdat any
-define weak_odr void @foo() local_unnamed_addr comdat {
-  ret void
-}
-
-
-;--- implicit.ll
-target triple = "x86_64-unknown-linux-gnu"
-target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
-
-$foo = comdat any
-define linkonce_odr void @foo() local_unnamed_addr comdat {
-  ret void
-}
diff --git a/lld/test/ELF/lto/internalize-exportdyn.ll b/lld/test/ELF/lto/internalize-exportdyn.ll
index f02d3b375dad50..ceaf31b4c8d2cf 100644
--- a/lld/test/ELF/lto/internalize-exportdyn.ll
+++ b/lld/test/ELF/lto/internalize-exportdyn.ll
@@ -9,6 +9,9 @@
 target triple = "x86_64-unknown-linux-gnu"
 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
 
+$f1 = comdat any
+$f2 = comdat any
+
 @c = linkonce_odr constant i32 1
 @g = linkonce_odr global i32 1
 @u_c = linkonce_odr unnamed_addr constant i32 1
@@ -46,6 +49,14 @@ define linkonce_odr void @baz() {
 
 @use_baz = global ptr @baz
 
+define weak_odr void @f1() local_unnamed_addr comdat {
+  ret void
+}
+
+define linkonce_odr void @f2() local_unnamed_addr comdat {
+  ret void
+}
+
 ; Check what gets internalized.
 ; CHECK: @c = weak_odr dso_local constant i32 1
 ; CHECK: @g = weak_odr dso_local global i32 1
@@ -60,6 +71,8 @@ define linkonce_odr void @baz() {
 ; CHECK: define internal void @zed2()
 ; CHECK: define weak_odr dso_local void @bah()
 ; CHECK: define weak_odr dso_local void @baz()
+; CHECK: define weak_odr dso_local void @f1() comdat
+; CHECK: define weak_odr dso_local void @f2() comdat
 
 ; DSO: @c = weak_odr constant i32 1
 ; DSO: @g = weak_odr global i32 1
@@ -74,3 +87,5 @@ define linkonce_odr void @baz() {
 ; DSO: define internal void @zed2()
 ; DSO: define weak_odr void @bah()
 ; DSO: define weak_odr void @baz()
+; DSO: define weak_odr void @f1() comdat
+; DSO: define weak_odr void @f2() comdat



More information about the llvm-commits mailing list