[lld] 8ce3750 - [lld-macho] Set FinalDefinitionInLinkageUnit on most LTO externs

Jez Ng via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 15 17:25:14 PDT 2022


Author: Jez Ng
Date: 2022-03-15T20:25:06-04:00
New Revision: 8ce3750ff62d68e7428a828530b9b6a8cc93e813

URL: https://github.com/llvm/llvm-project/commit/8ce3750ff62d68e7428a828530b9b6a8cc93e813
DIFF: https://github.com/llvm/llvm-project/commit/8ce3750ff62d68e7428a828530b9b6a8cc93e813.diff

LOG: [lld-macho] Set FinalDefinitionInLinkageUnit on most LTO externs

Since Mach-O has a two-level namespace (unlike ELF), we can usually set
this property to true.

(I believe this setting is only available in the new LTO backend, so I
can't really use ld64 / libLTO's behavior as a reference here... I'm
just doing what I think is correct.)

See {D119294} for the work done to calculate the `interposable` used in
this diff.

Reviewed By: MaskRay

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

Added: 
    lld/test/MachO/lto-final-definition.ll

Modified: 
    lld/MachO/LTO.cpp
    lld/test/MachO/lto-internalize.ll

Removed: 
    


################################################################################
diff  --git a/lld/MachO/LTO.cpp b/lld/MachO/LTO.cpp
index c2863c78744c4..3962de8756428 100644
--- a/lld/MachO/LTO.cpp
+++ b/lld/MachO/LTO.cpp
@@ -79,11 +79,15 @@ void BitcodeCompiler::add(BitcodeFile &f) {
     // be removed.
     r.Prevailing = !objSym.isUndefined() && sym->getFile() == &f;
 
-    if (const auto *defined = dyn_cast<Defined>(sym))
+    if (const auto *defined = dyn_cast<Defined>(sym)) {
       r.ExportDynamic =
           defined->isExternal() && !defined->privateExtern && exportDynamic;
-    else if (const auto *common = dyn_cast<CommonSymbol>(sym))
+      r.FinalDefinitionInLinkageUnit =
+          !defined->isExternalWeakDef() && !defined->interposable;
+    } else if (const auto *common = dyn_cast<CommonSymbol>(sym)) {
       r.ExportDynamic = !common->privateExtern && exportDynamic;
+      r.FinalDefinitionInLinkageUnit = true;
+    }
 
     r.VisibleToRegularObj =
         sym->isUsedInRegularObj || (r.Prevailing && r.ExportDynamic);

diff  --git a/lld/test/MachO/lto-final-definition.ll b/lld/test/MachO/lto-final-definition.ll
new file mode 100644
index 0000000000000..e0feb43211344
--- /dev/null
+++ b/lld/test/MachO/lto-final-definition.ll
@@ -0,0 +1,27 @@
+; REQUIRES: x86
+; RUN: rm -rf %t; mkdir %t
+; RUN: llvm-as %s -o %t/test.o
+; RUN: %lld -lSystem -dylib %t/test.o -o %t/test -save-temps
+; RUN: llvm-dis %t/test.0.2.internalize.bc -o - | FileCheck %s
+; RUN: %lld -lSystem -dylib %t/test.o -o %t/flat-namespace.dylib -save-temps \
+; RUN:   -flat_namespace
+; RUN: llvm-dis %t/flat-namespace.dylib.0.2.internalize.bc -o - | FileCheck %s \
+; RUN:   --check-prefix=NO-DSO-LOCAL
+
+;; f() is never dso_local since it is a weak external.
+; CHECK:        define weak_odr void @f()
+; CHECK:        define dso_local void @main()
+
+; NO-DSO-LOCAL: define weak_odr void @f()
+; NO-DSO-LOCAL: define void @main()
+
+target triple = "x86_64-apple-macosx10.15.0"
+target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+
+define weak_odr void @f() {
+  ret void
+}
+
+define void @main() {
+  ret void
+}

diff  --git a/lld/test/MachO/lto-internalize.ll b/lld/test/MachO/lto-internalize.ll
index 755484c216ff1..75962aec96d3f 100644
--- a/lld/test/MachO/lto-internalize.ll
+++ b/lld/test/MachO/lto-internalize.ll
@@ -17,7 +17,7 @@
 
 ;; Check that main is not internalized. This covers the case of bitcode symbols
 ;; referenced by undefined symbols that don't belong to any InputFile.
-; CHECK: define void @main()
+; CHECK: define dso_local void @main()
 
 ;; Check that the foo and bar functions are correctly internalized.
 ; CHECK: define internal void @bar()
@@ -25,7 +25,7 @@
 
 ;; Check that a bitcode symbol that is referenced by a regular object file isn't
 ;; internalized.
-; CHECK: define void @used_in_regular_obj()
+; CHECK: define dso_local void @used_in_regular_obj()
 
 ;; Check that a bitcode symbol that is defined in another bitcode file gets
 ;; internalized.
@@ -51,13 +51,13 @@
 
 ;; Note that only foo() gets internalized here; everything else that isn't
 ;; hidden must be exported.
-; DYN: @comm = common global
+; DYN: @comm = common dso_local global
 ; DYN: @comm_hide = internal global
-; DYN: define void @main()
-; DYN: define void @bar()
+; DYN: define dso_local void @main()
+; DYN: define dso_local void @bar()
 ; DYN: define internal void @foo()
-; DYN: define void @used_in_regular_obj()
-; DYN: define void @baz()
+; DYN: define dso_local void @used_in_regular_obj()
+; DYN: define dso_local void @baz()
 
 ; DYN-SYMS-DAG: (__TEXT,__text) external _bar
 ; DYN-SYMS-DAG: (__TEXT,__text) external _baz


        


More information about the llvm-commits mailing list