[clang] 809a1e0 - [CodeGenModule] Set dso_local for Mach-O GlobalValue

Fangrui Song via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 30 20:52:19 PST 2020


Author: Fangrui Song
Date: 2020-12-30T20:52:01-08:00
New Revision: 809a1e0ffd7af40ee27270ff8ba2ffc927330e71

URL: https://github.com/llvm/llvm-project/commit/809a1e0ffd7af40ee27270ff8ba2ffc927330e71
DIFF: https://github.com/llvm/llvm-project/commit/809a1e0ffd7af40ee27270ff8ba2ffc927330e71.diff

LOG: [CodeGenModule] Set dso_local for Mach-O GlobalValue

* static relocation model: always
* other relocation models: if isStrongDefinitionForLinker

This will make LLVM IR emitted for COFF/Mach-O and executable ELF similar.

Added: 
    

Modified: 
    clang/lib/CodeGen/CodeGenModule.cpp
    clang/test/CodeGenCXX/const-init.cpp
    clang/test/CodeGenCXX/linkage.cpp
    clang/test/CodeGenCXX/type_visibility.cpp
    clang/test/CodeGenCXX/visibility.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 6d14298d9f5f..bf0a38bf83ab 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -946,14 +946,20 @@ static bool shouldAssumeDSOLocal(const CodeGenModule &CGM,
   if (TT.isOSBinFormatCOFF() || (TT.isOSWindows() && TT.isOSBinFormatMachO()))
     return true;
 
+  const auto &CGOpts = CGM.getCodeGenOpts();
+  llvm::Reloc::Model RM = CGOpts.RelocationModel;
+  const auto &LOpts = CGM.getLangOpts();
+
+  if (TT.isOSBinFormatMachO()) {
+    if (RM == llvm::Reloc::Static)
+      return true;
+    return GV->isStrongDefinitionForLinker();
+  }
+
   // Only handle COFF and ELF for now.
   if (!TT.isOSBinFormatELF())
     return false;
 
-  // If this is not an executable, don't assume anything is local.
-  const auto &CGOpts = CGM.getCodeGenOpts();
-  llvm::Reloc::Model RM = CGOpts.RelocationModel;
-  const auto &LOpts = CGM.getLangOpts();
   if (RM != llvm::Reloc::Static && !LOpts.PIE) {
     // On ELF, if -fno-semantic-interposition is specified, we can set dso_local
     // if using a local alias is preferable (can avoid GOT indirection).

diff  --git a/clang/test/CodeGenCXX/const-init.cpp b/clang/test/CodeGenCXX/const-init.cpp
index f5c9dae7ba4b..5b305bc5e4d6 100644
--- a/clang/test/CodeGenCXX/const-init.cpp
+++ b/clang/test/CodeGenCXX/const-init.cpp
@@ -2,17 +2,17 @@
 // RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -std=c++98 -o - %s | FileCheck %s
 // RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -std=c++11 -o - %s | FileCheck %s
 
-// CHECK: @a = global i32 10
+// CHECK: @a = dso_local global i32 10
 int a = 10;
-// CHECK: @ar = constant i32* @a
+// CHECK: @ar = dso_local constant i32* @a
 int &ar = a;
 
 void f();
-// CHECK: @fr = constant void ()* @_Z1fv
+// CHECK: @fr = dso_local constant void ()* @_Z1fv
 void (&fr)() = f;
 
 struct S { int& a; };
-// CHECK: @s = global %struct.S { i32* @a }
+// CHECK: @s = dso_local global %struct.S { i32* @a }
 S s = { a };
 
 // PR5581
@@ -23,7 +23,7 @@ class C {
   unsigned f;
 };
 
-// CHECK: @_ZN6PR55812g0E = global %"class.PR5581::C" { i32 1 }
+// CHECK: @_ZN6PR55812g0E = dso_local global %"class.PR5581::C" { i32 1 }
 C g0 = { C::e1 };
 }
 
@@ -39,10 +39,10 @@ namespace test2 {
     static int g();
   } a;
 
-  // CHECK: @_ZN5test22t0E = global double {{1\.0+e\+0+}}, align 8
-  // CHECK: @_ZN5test22t1E = global [2 x double] [double {{1\.0+e\+0+}}, double {{5\.0+e-0*}}1], align 16
-  // CHECK: @_ZN5test22t2E = global double* @_ZN5test21A1d
-  // CHECK: @_ZN5test22t3E = global {{.*}} @_ZN5test21A1g
+  // CHECK: @_ZN5test22t0E = dso_local global double {{1\.0+e\+0+}}, align 8
+  // CHECK: @_ZN5test22t1E = dso_local global [2 x double] [double {{1\.0+e\+0+}}, double {{5\.0+e-0*}}1], align 16
+  // CHECK: @_ZN5test22t2E = dso_local global double* @_ZN5test21A1d
+  // CHECK: @_ZN5test22t3E = dso_local global {{.*}} @_ZN5test21A1g
   double t0 = A::d;
   double t1[] = { A::d, A::f };
   const double *t2 = &a.d;
@@ -50,7 +50,7 @@ namespace test2 {
 }
 
 // We don't expect to fold this in the frontend, but make sure it doesn't crash.
-// CHECK: @PR9558 = global float 0.000000e+0
+// CHECK: @PR9558 = dso_local global float 0.000000e+0
 float PR9558 = reinterpret_cast<const float&>("asd");
 
 // An initialized const automatic variable cannot be promoted to a constant
@@ -66,7 +66,7 @@ int writeToMutable() {
 
 // Make sure we don't try to fold this in the frontend; the backend can't
 // handle it.
-// CHECK: @PR11705 = global i128 0
+// CHECK: @PR11705 = dso_local global i128 0
 __int128_t PR11705 = (__int128_t)&PR11705;
 
 // Make sure we don't try to fold this either.
@@ -77,11 +77,11 @@ void UnfoldableAddrLabelDiff() { static __int128_t x = (long)&&a-(long)&&b; a:b:
 // CHECK: @_ZZ21FoldableAddrLabelDiffvE1x = internal global i64 sub (i64 ptrtoint (i8* blockaddress(@_Z21FoldableAddrLabelDiffv
 void FoldableAddrLabelDiff() { static long x = (long)&&a-(long)&&b; a:b:return;}
 
-// CHECK: @i = constant i32* bitcast (float* @PR9558 to i32*)
+// CHECK: @i = dso_local constant i32* bitcast (float* @PR9558 to i32*)
 int &i = reinterpret_cast<int&>(PR9558);
 
 int arr[2];
-// CHECK: @pastEnd = constant i32* bitcast (i8* getelementptr (i8, i8* bitcast ([2 x i32]* @arr to i8*), i64 8) to i32*)
+// CHECK: @pastEnd = dso_local constant i32* bitcast (i8* getelementptr (i8, i8* bitcast ([2 x i32]* @arr to i8*), i64 8) to i32*)
 int &pastEnd = arr[2];
 
 struct X {

diff  --git a/clang/test/CodeGenCXX/linkage.cpp b/clang/test/CodeGenCXX/linkage.cpp
index 69b426269ccd..d6c45cefd378 100644
--- a/clang/test/CodeGenCXX/linkage.cpp
+++ b/clang/test/CodeGenCXX/linkage.cpp
@@ -226,5 +226,5 @@ namespace test18 {
   template<template<typename> class> struct A {};
   struct B { template<typename> struct C; };
   void f(A<B::C>) {}
-  // CHECK-DAG: define void @_ZN6test181fENS_1AINS_1B1CEEE(
+  // CHECK-DAG: define dso_local void @_ZN6test181fENS_1AINS_1B1CEEE(
 }

diff  --git a/clang/test/CodeGenCXX/type_visibility.cpp b/clang/test/CodeGenCXX/type_visibility.cpp
index a7b7198a23fa..0096525cd772 100644
--- a/clang/test/CodeGenCXX/type_visibility.cpp
+++ b/clang/test/CodeGenCXX/type_visibility.cpp
@@ -110,14 +110,14 @@ namespace type0 {
   };
 
   void A::foo() {}
-  // FUNS-LABEL:        define void @_ZN5type01A3fooEv(
-  // VARS:        @_ZTVN5type01AE = unnamed_addr constant
-  // VARS:        @_ZTSN5type01AE = constant
-  // VARS:        @_ZTIN5type01AE = constant
+  // FUNS-LABEL:        define{{.*}} void @_ZN5type01A3fooEv(
+  // VARS:        @_ZTVN5type01AE = dso_local unnamed_addr constant
+  // VARS:        @_ZTSN5type01AE = dso_local constant
+  // VARS:        @_ZTIN5type01AE = dso_local constant
   // FUNS-HIDDEN-LABEL: define hidden void @_ZN5type01A3fooEv(
-  // VARS-HIDDEN: @_ZTVN5type01AE = unnamed_addr constant
-  // VARS-HIDDEN: @_ZTSN5type01AE = constant
-  // VARS-HIDDEN: @_ZTIN5type01AE = constant
+  // VARS-HIDDEN: @_ZTVN5type01AE = dso_local unnamed_addr constant
+  // VARS-HIDDEN: @_ZTSN5type01AE = dso_local constant
+  // VARS-HIDDEN: @_ZTIN5type01AE = dso_local constant
 }
 
 namespace type1 {
@@ -127,13 +127,13 @@ namespace type1 {
 
   void A::foo() {}
   // FUNS-LABEL:        define hidden void @_ZN5type11A3fooEv(
-  // VARS:        @_ZTVN5type11AE = unnamed_addr constant
-  // VARS:        @_ZTSN5type11AE = constant
-  // VARS:        @_ZTIN5type11AE = constant
+  // VARS:        @_ZTVN5type11AE = dso_local unnamed_addr constant
+  // VARS:        @_ZTSN5type11AE = dso_local constant
+  // VARS:        @_ZTIN5type11AE = dso_local constant
   // FUNS-HIDDEN-LABEL: define hidden void @_ZN5type11A3fooEv(
-  // VARS-HIDDEN: @_ZTVN5type11AE = unnamed_addr constant
-  // VARS-HIDDEN: @_ZTSN5type11AE = constant
-  // VARS-HIDDEN: @_ZTIN5type11AE = constant
+  // VARS-HIDDEN: @_ZTVN5type11AE = dso_local unnamed_addr constant
+  // VARS-HIDDEN: @_ZTSN5type11AE = dso_local constant
+  // VARS-HIDDEN: @_ZTIN5type11AE = dso_local constant
 }
 
 namespace type2 {
@@ -142,7 +142,7 @@ namespace type2 {
   };
 
   void A::foo() {}
-  // FUNS-LABEL:        define void @_ZN5type21A3fooEv(
+  // FUNS-LABEL:        define dso_local void @_ZN5type21A3fooEv(
   // VARS:        @_ZTVN5type21AE = hidden unnamed_addr constant
   // VARS:        @_ZTSN5type21AE = hidden constant
   // VARS:        @_ZTIN5type21AE = hidden constant
@@ -158,11 +158,11 @@ namespace type3 {
   };
 
   void A::foo() {}
-  // FUNS-LABEL:        define void @_ZN5type31A3fooEv(
+  // FUNS-LABEL:        define dso_local void @_ZN5type31A3fooEv(
   // VARS:        @_ZTVN5type31AE = hidden unnamed_addr constant
   // VARS:        @_ZTSN5type31AE = hidden constant
   // VARS:        @_ZTIN5type31AE = hidden constant
-  // FUNS-HIDDEN-LABEL: define void @_ZN5type31A3fooEv(
+  // FUNS-HIDDEN-LABEL: define dso_local void @_ZN5type31A3fooEv(
   // VARS-HIDDEN: @_ZTVN5type31AE = hidden unnamed_addr constant
   // VARS-HIDDEN: @_ZTSN5type31AE = hidden constant
   // VARS-HIDDEN: @_ZTIN5type31AE = hidden constant

diff  --git a/clang/test/CodeGenCXX/visibility.cpp b/clang/test/CodeGenCXX/visibility.cpp
index aff6554282ca..48ec1b8b712f 100644
--- a/clang/test/CodeGenCXX/visibility.cpp
+++ b/clang/test/CodeGenCXX/visibility.cpp
@@ -18,7 +18,7 @@ namespace test30 {
   };
   H DEFAULT a;
   X<&a> b;
-  // CHECK: _ZN6test301bE = global
+  // CHECK: _ZN6test301bE = dso_local global
   // CHECK-HIDDEN: _ZN6test301bE = hidden global
 }
 
@@ -33,7 +33,7 @@ namespace test25 {
   class DEFAULT A { };
 
   X<int>::definition<A> a;
-  // CHECK: @_ZN6test251aE = global
+  // CHECK: @_ZN6test251aE = dso_local global
   // CHECK-HIDDEN: @_ZN6test251aE = hidden global
 }
 
@@ -41,7 +41,7 @@ namespace test28 {
   class DEFAULT foo {
   };
   foo myvec;
-  // CHECK: @_ZN6test285myvecE = global
+  // CHECK: @_ZN6test285myvecE = dso_local global
   // CHECK-HIDDEN: @_ZN6test285myvecE = hidden global
 }
 
@@ -53,8 +53,8 @@ namespace test29 {
   DEFAULT extern RECT data_rect;
   RECT data_rect = { -1};
 #pragma GCC visibility pop
-  // CHECK: @_ZN6test299data_rectE = global
-  // CHECK-HIDDEN: @_ZN6test299data_rectE = global
+  // CHECK: @_ZN6test299data_rectE = dso_local global
+  // CHECK-HIDDEN: @_ZN6test299data_rectE = dso_local global
 }
 
 namespace test40 {
@@ -103,17 +103,17 @@ namespace test48 {
 
 // CHECK: @_ZN5Test425VariableInHiddenNamespaceE = hidden global i32 10
 // CHECK: @_ZN5Test71aE = hidden global
-// CHECK: @_ZN5Test71bE = global
-// CHECK: @test9_var = global
-// CHECK-HIDDEN: @test9_var = global
+// CHECK: @_ZN5Test71bE = dso_local global
+// CHECK: @test9_var = dso_local global
+// CHECK-HIDDEN: @test9_var = dso_local global
 // CHECK: @_ZN6Test121A6hiddenE = external hidden global
 // CHECK: @_ZN6Test121A7visibleE = external global
 // CHECK-HIDDEN: @_ZN6Test121A6hiddenE = external hidden global
 // CHECK-HIDDEN: @_ZN6Test121A7visibleE = external global
 // CHECK: @_ZN6Test131B1aE = hidden global
-// CHECK: @_ZN6Test131C1aE = global
+// CHECK: @_ZN6Test131C1aE = dso_local global
 // CHECK-HIDDEN: @_ZN6Test131B1aE = hidden global
-// CHECK-HIDDEN: @_ZN6Test131C1aE = global
+// CHECK-HIDDEN: @_ZN6Test131C1aE = dso_local global
 // CHECK: @_ZN6Test143varE = external global
 // CHECK-HIDDEN: @_ZN6Test143varE = external global
 // CHECK: @_ZN6Test154TempINS_1AEE5Inner6bufferE = external global [0 x i8]
@@ -134,8 +134,8 @@ namespace test27 {
 
   void C<int>::D::g() {
   }
-  // CHECK: _ZTVN6test271CIiE1DE = unnamed_addr constant
-  // CHECK-HIDDEN: _ZTVN6test271CIiE1DE = unnamed_addr constant
+  // CHECK: _ZTVN6test271CIiE1DE = dso_local unnamed_addr constant
+  // CHECK-HIDDEN: _ZTVN6test271CIiE1DE = dso_local unnamed_addr constant
 }
 
 // CHECK: @_ZTVN5Test63fooE = linkonce_odr hidden unnamed_addr constant
@@ -195,7 +195,7 @@ namespace Test4 HIDDEN {
   };
   
   // A has default visibility.
-  // CHECK-LABEL: define void @_ZN5Test41A1fEv
+  // CHECK-LABEL: define dso_local void @_ZN5Test41A1fEv
   void A::f() { } 
 }
 
@@ -209,7 +209,7 @@ namespace Test5 {
   
   namespace NS {
     // g is in NS, but this NS decl is not hidden.
-    // CHECK-LABEL: define void @_ZN5Test52NS1gEv
+    // CHECK-LABEL: define dso_local void @_ZN5Test52NS1gEv
     void g() { }
   }
 }
@@ -268,8 +268,8 @@ namespace Test9 {
     void DEFAULT test9_fun(struct A *a) { }
     struct A DEFAULT test9_var; // above
   }
-  // CHECK-LABEL: define void @test9_fun(
-  // CHECK-HIDDEN-LABEL: define void @test9_fun(
+  // CHECK-LABEL: define dso_local void @test9_fun(
+  // CHECK-HIDDEN-LABEL: define dso_local void @test9_fun(
 
   void test() {
     A a = test9_var;
@@ -285,8 +285,8 @@ namespace Test10 {
     void foo(A*);
   };
 
-  // CHECK-LABEL: define void @_ZN6Test101B3fooEPNS_1AE(
-  // CHECK-HIDDEN-LABEL: define void @_ZN6Test101B3fooEPNS_1AE(
+  // CHECK-LABEL: define dso_local void @_ZN6Test101B3fooEPNS_1AE(
+  // CHECK-HIDDEN-LABEL: define dso_local void @_ZN6Test101B3fooEPNS_1AE(
   void B::foo(A*) {}
 }
 
@@ -507,7 +507,7 @@ namespace Test20 {
     static void test3();
   };
 
-  // CHECK-LABEL: define void @_ZN6Test201AILj1EE5test2Ev()
+  // CHECK-LABEL: define dso_local void @_ZN6Test201AILj1EE5test2Ev()
   void A<1>::test2() {}
 
   // CHECK: declare void @_ZN6Test201AILj1EE5test3Ev()
@@ -684,8 +684,8 @@ namespace test26 {
   template<>
   void C<int>::f() { }
 
-  // CHECK-LABEL: define void @_ZN6test261CIiE1fEv
-  // CHECK-HIDDEN-LABEL: define void @_ZN6test261CIiE1fEv
+  // CHECK-LABEL: define dso_local void @_ZN6test261CIiE1fEv
+  // CHECK-HIDDEN-LABEL: define dso_local void @_ZN6test261CIiE1fEv
 }
 
 namespace test31 {
@@ -709,8 +709,8 @@ namespace test32 {
   };
   void A::B::baz() {
   }
-  // CHECK-LABEL: define void @_ZN6test321A1B3bazEv
-  // CHECK-HIDDEN-LABEL: define void @_ZN6test321A1B3bazEv
+  // CHECK-LABEL: define dso_local void @_ZN6test321A1B3bazEv
+  // CHECK-HIDDEN-LABEL: define dso_local void @_ZN6test321A1B3bazEv
 }
 
 namespace test33 {
@@ -829,8 +829,8 @@ namespace test42 {
   };
   void bar<foo>::zed() {
   }
-  // CHECK-LABEL: define void @_ZN6test423barINS_3fooEE3zedEv
-  // CHECK-HIDDEN-LABEL: define void @_ZN6test423barINS_3fooEE3zedEv
+  // CHECK-LABEL: define dso_local void @_ZN6test423barINS_3fooEE3zedEv
+  // CHECK-HIDDEN-LABEL: define dso_local void @_ZN6test423barINS_3fooEE3zedEv
 }
 
 namespace test43 {
@@ -842,8 +842,8 @@ namespace test43 {
   template <>
   DEFAULT void bar<foo>() {
   }
-  // CHECK-LABEL: define void @_ZN6test433barINS_3fooEEEvv
-  // CHECK-HIDDEN-LABEL: define void @_ZN6test433barINS_3fooEEEvv
+  // CHECK-LABEL: define dso_local void @_ZN6test433barINS_3fooEEEvv
+  // CHECK-HIDDEN-LABEL: define dso_local void @_ZN6test433barINS_3fooEEEvv
 }
 
 namespace test44 {
@@ -1208,10 +1208,10 @@ namespace test65 {
     static void foo() {}
   };
 
-  // CHECK-LABEL: define void @_ZN6test651BINS_1AEE4funcEv()
+  // CHECK-LABEL: define dso_local void @_ZN6test651BINS_1AEE4funcEv()
   template <> DEFAULT void B<A>::func() {}
 
-  // CHECK-LABEL: define void @_ZN6test651BINS_1AEE6funcT2IS1_EEvv()
+  // CHECK-LABEL: define dso_local void @_ZN6test651BINS_1AEE6funcT2IS1_EEvv()
   template <> template <> DEFAULT void B<A>::funcT2<A>() {}
 
   // CHECK-LABEL: define linkonce_odr void @_ZN6test651BINS_1AEE6funcT1IiEEvv()
@@ -1314,6 +1314,6 @@ namespace test69 {
   }
   namespace foo __attribute__((visibility("hidden"))) {
   }
-  // CHECK-LABEL: define void @_ZN6test693foo1fEv
+  // CHECK-LABEL: define dso_local void @_ZN6test693foo1fEv
   // CHECK-HIDDEN-LABEL: define hidden void @_ZN6test693foo1fEv
 }


        


More information about the cfe-commits mailing list