[PATCH] D18536: Preserve extern_weak linkage in CloneModule.

Evgeniy Stepanov via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 28 17:46:40 PDT 2016


eugenis created this revision.
eugenis added a reviewer: pcc.
eugenis added a subscriber: llvm-commits.
eugenis set the repository for this revision to rL LLVM.

Only force "extern" linkage if the function used to be a definition
in the source module. Declarations keep their original linkage.

Repository:
  rL LLVM

http://reviews.llvm.org/D18536

Files:
  lib/Transforms/Utils/CloneModule.cpp
  test/tools/llvm-split/extern_linkage.ll

Index: test/tools/llvm-split/extern_linkage.ll
===================================================================
--- /dev/null
+++ test/tools/llvm-split/extern_linkage.ll
@@ -0,0 +1,20 @@
+; Test that extern_weak linkage is preserved.
+; RUN: llvm-split -o %t %s
+; RUN: llvm-dis -o - %t0 | FileCheck %s
+; RUN: llvm-dis -o - %t1 | FileCheck %s
+
+define void @g() {
+entry:
+  br i1 icmp ne (void (...)* @f, void (...)* null), label %if.then, label %if.end
+
+if.then:                                          ; preds = %entry
+  tail call void (...) @f()
+  br label %if.end
+
+if.end:                                           ; preds = %if.then, %entry
+  ret void
+}
+
+; @f is an extern_weak declaration in both partitions
+; CHECK: declare extern_weak void @f(...)
+declare extern_weak void @f(...)
Index: lib/Transforms/Utils/CloneModule.cpp
===================================================================
--- lib/Transforms/Utils/CloneModule.cpp
+++ lib/Transforms/Utils/CloneModule.cpp
@@ -122,27 +122,28 @@
   // Similarly, copy over function bodies now...
   //
   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
+    if (I->isDeclaration())
+      continue;
+
     Function *F = cast<Function>(VMap[&*I]);
     if (!ShouldCloneDefinition(&*I)) {
       // Skip after setting the correct linkage for an external reference.
       F->setLinkage(GlobalValue::ExternalLinkage);
       // Personality function is not valid on a declaration.
       F->setPersonalityFn(nullptr);
       continue;
     }
-    if (!I->isDeclaration()) {
-      Function::arg_iterator DestI = F->arg_begin();
-      for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
-           ++J) {
-        DestI->setName(J->getName());
-        VMap[&*J] = &*DestI++;
-      }
-
-      CloneDebugInfoMetadata(F, &*I, VMap);
-      SmallVector<ReturnInst*, 8> Returns;  // Ignore returns cloned.
-      CloneFunctionInto(F, &*I, VMap, /*ModuleLevelChanges=*/true, Returns);
+    Function::arg_iterator DestI = F->arg_begin();
+    for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
+         ++J) {
+      DestI->setName(J->getName());
+      VMap[&*J] = &*DestI++;
     }
 
+    CloneDebugInfoMetadata(F, &*I, VMap);
+    SmallVector<ReturnInst *, 8> Returns; // Ignore returns cloned.
+    CloneFunctionInto(F, &*I, VMap, /*ModuleLevelChanges=*/true, Returns);
+
     if (I->hasPersonalityFn())
       F->setPersonalityFn(MapValue(I->getPersonalityFn(), VMap));
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D18536.51865.patch
Type: text/x-patch
Size: 2519 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160329/474e5d1d/attachment.bin>


More information about the llvm-commits mailing list