1bf612458SYaxun (Sam) Liu; REQUIRES: x86-registered-target
2bf612458SYaxun (Sam) Liu
3cee313d2SEric Christopher; Do setup work for all below tests: generate bitcode and combined index
4cee313d2SEric Christopher; RUN: opt -module-summary %s -o %t.bc
5cee313d2SEric Christopher; RUN: opt -module-summary %p/Inputs/funcimport.ll -o %t2.bc
6cee313d2SEric Christopher; RUN: llvm-lto -thinlto -print-summary-global-ids -o %t3 %t.bc %t2.bc 2>&1 | FileCheck %s --check-prefix=GUID
7cee313d2SEric Christopher
8cee313d2SEric Christopher; Do the import now
9*a9af1b9aSFangrui Song; RUN: opt -passes=function-import -stats -print-imports -enable-import-metadata -summary-file %t3.thinlto.bc %t.bc -S 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=INSTLIMDEF
10cee313d2SEric Christopher; Try again with new pass manager
11cee313d2SEric Christopher; RUN: opt -passes='function-import' -stats -print-imports -enable-import-metadata -summary-file %t3.thinlto.bc %t.bc -S 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=INSTLIMDEF
12cee313d2SEric Christopher; RUN: opt -passes='function-import' -debug-only=function-import -enable-import-metadata -summary-file %t3.thinlto.bc %t.bc -S 2>&1 | FileCheck %s --check-prefix=DUMP
13cee313d2SEric Christopher; "-stats" and "-debug-only" require +Asserts.
14cee313d2SEric Christopher; REQUIRES: asserts
15cee313d2SEric Christopher
16cee313d2SEric Christopher; Test import with smaller instruction limit
17*a9af1b9aSFangrui Song; RUN: opt -passes=function-import -enable-import-metadata  -summary-file %t3.thinlto.bc %t.bc -import-instr-limit=5 -S | FileCheck %s --check-prefix=CHECK --check-prefix=INSTLIM5
18cee313d2SEric Christopher; INSTLIM5-NOT: @staticfunc.llvm.
19cee313d2SEric Christopher
20bf612458SYaxun (Sam) Liu; Test force import all
21bf612458SYaxun (Sam) Liu; RUN: llvm-lto -thinlto-action=run -force-import-all %t.bc %t2.bc 2>&1 \
22bf612458SYaxun (Sam) Liu; RUN:  | FileCheck %s --check-prefix=IMPORTALL
23bf612458SYaxun (Sam) Liu; IMPORTALL-DAG: Error importing module: Failed to import function weakalias due to InterposableLinkage
24bf612458SYaxun (Sam) Liu
25bf612458SYaxun (Sam) Liutarget datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
26bf612458SYaxun (Sam) Liutarget triple = "x86_64-apple-macosx10.11.0"
27cee313d2SEric Christopher
28cee313d2SEric Christopherdefine i32 @main() #0 {
29cee313d2SEric Christopherentry:
30cee313d2SEric Christopher  call void (...) @weakalias()
31cee313d2SEric Christopher  call void (...) @analias()
32cee313d2SEric Christopher  call void (...) @linkoncealias()
33cee313d2SEric Christopher  %call = call i32 (...) @referencestatics()
34cee313d2SEric Christopher  %call1 = call i32 (...) @referenceglobals()
35cee313d2SEric Christopher  %call2 = call i32 (...) @referencecommon()
36cee313d2SEric Christopher  call void (...) @setfuncptr()
37cee313d2SEric Christopher  call void (...) @callfuncptr()
38cee313d2SEric Christopher  call void (...) @weakfunc()
39cee313d2SEric Christopher  call void (...) @linkoncefunc2()
40cee313d2SEric Christopher  call void (...) @referencelargelinkonce()
41cee313d2SEric Christopher  call void (...) @variadic_no_va_start()
42cee313d2SEric Christopher  call void (...) @variadic_va_start()
43cee313d2SEric Christopher  ret i32 0
44cee313d2SEric Christopher}
45cee313d2SEric Christopher
46cee313d2SEric Christopher; Won't import weak alias
47cee313d2SEric Christopher; CHECK-DAG: declare void @weakalias
48cee313d2SEric Christopherdeclare void @weakalias(...) #1
49cee313d2SEric Christopher
50cee313d2SEric Christopher; External alias imported as available_externally copy of aliasee
51cee313d2SEric Christopher; CHECK-DAG: define available_externally void @analias
52cee313d2SEric Christopherdeclare void @analias(...) #1
53cee313d2SEric Christopher
54cee313d2SEric Christopher; External alias imported as available_externally copy of aliasee
55cee313d2SEric Christopher; (linkoncealias is an external alias to a linkonce_odr)
56cee313d2SEric Christopherdeclare void @linkoncealias(...) #1
57cee313d2SEric Christopher; CHECK-DAG: define available_externally void @linkoncealias()
58cee313d2SEric Christopher
59cee313d2SEric Christopher; INSTLIMDEF-DAG: Import referencestatics
60cee313d2SEric Christopher; INSTLIMDEF-DAG: define available_externally i32 @referencestatics(i32 %i) !thinlto_src_module !0 {
61cee313d2SEric Christopher; INSTLIM5-DAG: declare i32 @referencestatics(...)
62cee313d2SEric Christopherdeclare i32 @referencestatics(...) #1
63cee313d2SEric Christopher
64cee313d2SEric Christopher; The import of referencestatics will expose call to staticfunc that
65cee313d2SEric Christopher; should in turn be imported as a promoted/renamed and hidden function.
66cee313d2SEric Christopher; Ensure that the call is to the properly-renamed function.
67cee313d2SEric Christopher; INSTLIMDEF-DAG: Import staticfunc
68cee313d2SEric Christopher; INSTLIMDEF-DAG: %call = call i32 @staticfunc.llvm.
69cee313d2SEric Christopher; INSTLIMDEF-DAG: define available_externally hidden i32 @staticfunc.llvm.{{.*}} !thinlto_src_module !0 {
70cee313d2SEric Christopher
71cee313d2SEric Christopher; INSTLIMDEF-DAG: Import referenceglobals
72cee313d2SEric Christopher; CHECK-DAG: define available_externally i32 @referenceglobals(i32 %i) !thinlto_src_module !0 {
73cee313d2SEric Christopherdeclare i32 @referenceglobals(...) #1
74cee313d2SEric Christopher
75cee313d2SEric Christopher; The import of referenceglobals will expose call to globalfunc1 that
76cee313d2SEric Christopher; should in turn be imported.
77cee313d2SEric Christopher; INSTLIMDEF-DAG: Import globalfunc1
78cee313d2SEric Christopher; CHECK-DAG: define available_externally void @globalfunc1() !thinlto_src_module !0
79cee313d2SEric Christopher
80cee313d2SEric Christopher; INSTLIMDEF-DAG: Import referencecommon
81cee313d2SEric Christopher; CHECK-DAG: define available_externally i32 @referencecommon(i32 %i) !thinlto_src_module !0 {
82cee313d2SEric Christopherdeclare i32 @referencecommon(...) #1
83cee313d2SEric Christopher
84cee313d2SEric Christopher; INSTLIMDEF-DAG: Import setfuncptr
85cee313d2SEric Christopher; CHECK-DAG: define available_externally void @setfuncptr() !thinlto_src_module !0 {
86cee313d2SEric Christopherdeclare void @setfuncptr(...) #1
87cee313d2SEric Christopher
88cee313d2SEric Christopher; INSTLIMDEF-DAG: Import callfuncptr
89cee313d2SEric Christopher; CHECK-DAG: define available_externally void @callfuncptr() !thinlto_src_module !0 {
90cee313d2SEric Christopherdeclare void @callfuncptr(...) #1
91cee313d2SEric Christopher
92cee313d2SEric Christopher; Ensure that all uses of local variable @P which has used in setfuncptr
93cee313d2SEric Christopher; and callfuncptr are to the same promoted/renamed global.
9441d5033eSNikita Popov; CHECK-DAG: @P.llvm.{{.*}} = available_externally hidden global ptr null
9541d5033eSNikita Popov; CHECK-DAG: %0 = load ptr, ptr @P.llvm.
9641d5033eSNikita Popov; CHECK-DAG: store ptr @staticfunc2.llvm.{{.*}}, ptr @P.llvm.
97cee313d2SEric Christopher
98cee313d2SEric Christopher; Ensure that @referencelargelinkonce definition is pulled in, but later we
99cee313d2SEric Christopher; also check that the linkonceodr function is not.
100cee313d2SEric Christopher; CHECK-DAG: define available_externally void @referencelargelinkonce() !thinlto_src_module !0 {
101cee313d2SEric Christopher; INSTLIM5-DAG: declare void @linkonceodr()
102cee313d2SEric Christopherdeclare void @referencelargelinkonce(...)
103cee313d2SEric Christopher
104cee313d2SEric Christopher; Won't import weak func
105cee313d2SEric Christopher; CHECK-DAG: declare void @weakfunc(...)
106cee313d2SEric Christopherdeclare void @weakfunc(...) #1
107cee313d2SEric Christopher
108cee313d2SEric Christopher; Won't import linkonce func
109cee313d2SEric Christopher; CHECK-DAG: declare void @linkoncefunc2(...)
110cee313d2SEric Christopherdeclare void @linkoncefunc2(...) #1
111cee313d2SEric Christopher
112cee313d2SEric Christopher; INSTLIMDEF-DAG: Import funcwithpersonality
11341d5033eSNikita Popov; INSTLIMDEF-DAG: define available_externally hidden void @funcwithpersonality.llvm.{{.*}}() personality ptr @__gxx_personality_v0 !thinlto_src_module !0 {
114cee313d2SEric Christopher; INSTLIM5-DAG: declare hidden void @funcwithpersonality.llvm.{{.*}}()
115cee313d2SEric Christopher
116cee313d2SEric Christopher; We can import variadic functions without a va_start, since the inliner
117cee313d2SEric Christopher; can handle them.
118cee313d2SEric Christopher; INSTLIMDEF-DAG: Import variadic_no_va_start
119cee313d2SEric Christopher; CHECK-DAG: define available_externally void @variadic_no_va_start(...) !thinlto_src_module !0 {
120cee313d2SEric Christopherdeclare void @variadic_no_va_start(...)
121cee313d2SEric Christopher
122cee313d2SEric Christopher; We can import variadic functions with a va_start, since the inliner
123cee313d2SEric Christopher; can sometimes handle them.
124cee313d2SEric Christopher; CHECK-DAG: define available_externally void @variadic_va_start(...)
125cee313d2SEric Christopherdeclare void @variadic_va_start(...)
126cee313d2SEric Christopher
127cee313d2SEric Christopher; INSTLIMDEF-DAG: Import globalfunc2
128cee313d2SEric Christopher; INSTLIMDEF-DAG: 15 function-import - Number of functions imported
129cee313d2SEric Christopher; INSTLIMDEF-DAG: 4 function-import - Number of global variables imported
130cee313d2SEric Christopher
131cee313d2SEric Christopher; CHECK-DAG: !0 = !{!"{{.*}}/Inputs/funcimport.ll"}
132cee313d2SEric Christopher
133cee313d2SEric Christopher; The actual GUID values will depend on path to test.
134cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is weakalias
135cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is referenceglobals
136cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is weakfunc
137cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is main
138cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is referencecommon
139cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is analias
140cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is referencestatics
141cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is linkoncealias
142cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is setfuncptr
143cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is callfuncptr
144cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is funcwithpersonality
145cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is setfuncptr
146cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is staticfunc2
147cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is __gxx_personality_v0
148cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is referencestatics
149cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is globalfunc1
150cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is globalfunc2
151cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is P
152cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is staticvar
153cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is commonvar
154cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is weakalias
155cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is staticfunc
156cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is weakfunc
157cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is referenceglobals
158cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is weakvar
159cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is staticconstvar
160cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is analias
161cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is globalvar
162cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is referencecommon
163cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is linkoncealias
164cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is callfuncptr
165cee313d2SEric Christopher; GUID-DAG: GUID {{.*}} is linkoncefunc
166cee313d2SEric Christopher
167cee313d2SEric Christopher; DUMP:       Module [[M1:.*]] imports from 1 module
168cee313d2SEric Christopher; DUMP-NEXT:  15 functions imported from [[M2:.*]]
169cee313d2SEric Christopher; DUMP-NEXT:  4 vars imported from [[M2]]
170cee313d2SEric Christopher; DUMP:       Imported 15 functions for Module [[M1]]
171cee313d2SEric Christopher; DUMP-NEXT:  Imported 4 global variables for Module [[M1]]
172