[PATCH] Do not emit thunks with available_externally linkage in comdats

Derek Schuff dschuff at google.com
Thu May 7 15:01:36 PDT 2015


Hi rnk, rafael,

Functions with available_externally linkage will not be emitted to object
files (they will just be undefined symbols), so it does not make sense to
put them in comdats.

http://reviews.llvm.org/D9580

Files:
  lib/CodeGen/CGVTables.cpp
  test/CodeGenCXX/thunks.cpp

Index: lib/CodeGen/CGVTables.cpp
===================================================================
--- lib/CodeGen/CGVTables.cpp
+++ lib/CodeGen/CGVTables.cpp
@@ -378,9 +378,6 @@
   // Set the right linkage.
   CGM.setFunctionLinkage(GD, Fn);
 
-  if (CGM.supportsCOMDAT() && Fn->isWeakForLinker())
-    Fn->setComdat(CGM.getModule().getOrInsertComdat(Fn->getName()));
-
   // Set the right visibility.
   const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
   setThunkVisibility(CGM, MD, Thunk, Fn);
@@ -461,6 +458,9 @@
     CGM.getCXXABI().setThunkLinkage(ThunkFn, ForVTable, GD,
                                     !Thunk.Return.isEmpty());
   }
+  if (CGM.supportsCOMDAT() && ThunkFn->isWeakForLinker() &&
+      !ThunkFn->hasAvailableExternallyLinkage())
+    ThunkFn->setComdat(CGM.getModule().getOrInsertComdat(ThunkFn->getName()));
 }
 
 void CodeGenVTables::maybeEmitThunkForVTable(GlobalDecl GD,
Index: test/CodeGenCXX/thunks.cpp
===================================================================
--- test/CodeGenCXX/thunks.cpp
+++ test/CodeGenCXX/thunks.cpp
@@ -1,5 +1,5 @@
 // RUN: %clang_cc1 %s -triple=x86_64-pc-linux-gnu -munwind-tables -emit-llvm -o - | FileCheck %s
-// RUN: %clang_cc1 %s -triple=x86_64-pc-linux-gnu -munwind-tables -emit-llvm -o - -O1 -disable-llvm-optzns | FileCheck %s
+// RUN: %clang_cc1 %s -triple=x86_64-pc-linux-gnu -munwind-tables -emit-llvm -o - -O1 -disable-llvm-optzns | FileCheck %s --check-prefix=CHECK --check-prefix=CHECKOPT
 
 namespace Test1 {
 
@@ -361,6 +361,28 @@
   // CHECK: declare void @_ZThn8_N6Test151C1fEiz
 }
 
+namespace Test16 {
+
+// Check that the thunk for 'B::f' has available_externally linkage
+// and is not in a comdat.
+
+template <class C>
+struct A {
+  virtual void f();
+};
+
+template <class D>
+struct B : virtual A<D> {
+  virtual void f() { }
+};
+
+extern template struct B<int>;
+
+void f(B<int> b) {
+  b.f();
+}
+}
+
 /**** The following has to go at the end of the file ****/
 
 // This is from Test5:
@@ -371,4 +393,7 @@
 // CHECK-LABEL: define linkonce_odr void @_ZN6Test101C3fooEv
 // CHECK-LABEL: define linkonce_odr void @_ZThn8_N6Test101C3fooEv
 
+// CHECKOPT-LABEL: define available_externally void @_ZTv0_n24_N6Test161BIiE1fEv
+// CHECKOPT-NOT: comdat
+
 // CHECK: attributes [[NUW]] = { nounwind uwtable{{.*}} }

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D9580.25246.patch
Type: text/x-patch
Size: 2320 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150507/dce13c0b/attachment.bin>


More information about the cfe-commits mailing list