From 24a3d0e0ad16e91eac0511689f0166c4123f83ee Mon Sep 17 00:00:00 2001
From: Teresa Johnson <tejohnson@google.com>
Date: Tue, 24 Nov 2015 16:10:43 +0000
Subject: [PATCH] [ThinLTO] Fix FunctionImport alias checking and test

Skip imports for weak_any aliases as well. Fix the test to check
non-import of weak aliases and functions, and import of normal alias.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@253991 91177308-0d34-0410-b5e6-96231b3b80d8
---
 lib/Transforms/IPO/FunctionImport.cpp               |  9 +++++----
 test/Transforms/FunctionImport/Inputs/funcimport.ll |  6 ------
 test/Transforms/FunctionImport/funcimport.ll        | 13 ++++++++++---
 3 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/lib/Transforms/IPO/FunctionImport.cpp b/lib/Transforms/IPO/FunctionImport.cpp
index 725796790ea..c874e4f5da7 100644
--- a/lib/Transforms/IPO/FunctionImport.cpp
+++ b/lib/Transforms/IPO/FunctionImport.cpp
@@ -142,11 +142,12 @@ bool FunctionImporter::importFunctions(Module &M) {
       llvm_unreachable("Can't load function in Module");
     }
 
-    // We cannot import weak_any functions without possibly affecting the
-    // order they are seen and selected by the linker, changing program
+    // We cannot import weak_any functions/aliases without possibly affecting
+    // the order they are seen and selected by the linker, changing program
     // semantics.
-    if (F->hasWeakAnyLinkage()) {
-      DEBUG(dbgs() << "Ignoring import request for weak-any function "
+    if (SGV->hasWeakAnyLinkage()) {
+      DEBUG(dbgs() << "Ignoring import request for weak-any "
+                   << (isa<Function>(SGV) ? "function " : "alias ")
                    << CalledFunctionName << " from " << FileName << "\n");
       continue;
     }
diff --git a/test/Transforms/FunctionImport/Inputs/funcimport.ll b/test/Transforms/FunctionImport/Inputs/funcimport.ll
index c0298b55ea4..96555892fe3 100644
--- a/test/Transforms/FunctionImport/Inputs/funcimport.ll
+++ b/test/Transforms/FunctionImport/Inputs/funcimport.ll
@@ -74,12 +74,6 @@ entry:
   ret void
 }
 
-define void @callweakfunc() #0 {
-entry:
-  call void @weakfunc()
-  ret void
-}
-
 define internal i32 @staticfunc() #0 {
 entry:
   ret i32 1
diff --git a/test/Transforms/FunctionImport/funcimport.ll b/test/Transforms/FunctionImport/funcimport.ll
index c9a376aca53..8d790520e33 100644
--- a/test/Transforms/FunctionImport/funcimport.ll
+++ b/test/Transforms/FunctionImport/funcimport.ll
@@ -15,12 +15,17 @@ entry:
   %call2 = call i32 (...) @referencecommon()
   call void (...) @setfuncptr()
   call void (...) @callfuncptr()
-  call void (...) @callweakfunc()
+  call void (...) @weakfunc()
   ret i32 0
 }
 
-; Won't import alias
+; Won't import weak alias
+; CHECK-DAG: declare extern_weak void @weakalias()
 declare void @weakalias(...) #1
+
+; Aliases import the aliasee function
+; CHECK-DAG: @analias = alias void (...), bitcast (void ()* @globalfunc2 to void (...)*)
+; CHECK-DAG: define available_externally void @globalfunc2()
 declare void @analias(...) #1
 
 ; CHECK-DAG: define available_externally i32 @referencestatics(i32 %i)
@@ -39,4 +44,6 @@ declare void @setfuncptr(...) #1
 declare void @callfuncptr(...) #1
 
 ; Won't import weak func
-declare void @callweakfunc(...) #1
+; CHECK-DAG: declare void @weakfunc(...)
+declare void @weakfunc(...) #1
+
-- 
2.34.1