xref: /aosp_15_r20/external/llvm/test/Linker/funcimport.ll (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker; First ensure that the ThinLTO handling in llvm-link and llvm-lto handles
2*9880d681SAndroid Build Coastguard Worker; bitcode without summary sections gracefully.
3*9880d681SAndroid Build Coastguard Worker; RUN: opt %s -o %t.bc
4*9880d681SAndroid Build Coastguard Worker; RUN: opt %p/Inputs/funcimport.ll -o %t2.bc
5*9880d681SAndroid Build Coastguard Worker; RUN: llvm-link %t.bc -summary-index=%t.bc -S
6*9880d681SAndroid Build Coastguard Worker; RUN: llvm-lto -thinlto -o %t3 %t.bc %t2.bc
7*9880d681SAndroid Build Coastguard Worker
8*9880d681SAndroid Build Coastguard Worker; Do setup work for all below tests: generate bitcode and combined index
9*9880d681SAndroid Build Coastguard Worker; RUN: opt -module-summary %s -o %t.bc
10*9880d681SAndroid Build Coastguard Worker; RUN: opt -module-summary %p/Inputs/funcimport.ll -o %t2.bc
11*9880d681SAndroid Build Coastguard Worker; RUN: llvm-lto -thinlto -o %t3 %t.bc %t2.bc
12*9880d681SAndroid Build Coastguard Worker
13*9880d681SAndroid Build Coastguard Worker; Ensure statics are promoted/renamed correctly from this file (all but
14*9880d681SAndroid Build Coastguard Worker; constant variable need promotion).
15*9880d681SAndroid Build Coastguard Worker; RUN: llvm-link %t.bc -summary-index=%t3.thinlto.bc -S | FileCheck %s --check-prefix=EXPORTSTATIC
16*9880d681SAndroid Build Coastguard Worker; EXPORTSTATIC-DAG: @staticvar.llvm.{{.*}} = hidden global
17*9880d681SAndroid Build Coastguard Worker; EXPORTSTATIC-DAG: @staticconstvar = internal unnamed_addr constant
18*9880d681SAndroid Build Coastguard Worker; EXPORTSTATIC-DAG: @P.llvm.{{.*}} = hidden global void ()* null
19*9880d681SAndroid Build Coastguard Worker; EXPORTSTATIC-DAG: define hidden i32 @staticfunc.llvm.
20*9880d681SAndroid Build Coastguard Worker; EXPORTSTATIC-DAG: define hidden void @staticfunc2.llvm.
21*9880d681SAndroid Build Coastguard Worker
22*9880d681SAndroid Build Coastguard Worker; Ensure that both weak alias to an imported function and strong alias to a
23*9880d681SAndroid Build Coastguard Worker; non-imported function are correctly turned into declarations.
24*9880d681SAndroid Build Coastguard Worker; Also ensures that alias to a linkonce function is turned into a declaration
25*9880d681SAndroid Build Coastguard Worker; and that the associated linkonce function is not in the output, as it is
26*9880d681SAndroid Build Coastguard Worker; lazily linked and never referenced/materialized.
27*9880d681SAndroid Build Coastguard Worker; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=globalfunc1:%t.bc -S | FileCheck %s --check-prefix=IMPORTGLOB1
28*9880d681SAndroid Build Coastguard Worker; IMPORTGLOB1-DAG: define available_externally void @globalfunc1
29*9880d681SAndroid Build Coastguard Worker; IMPORTGLOB1-DAG: declare void @weakalias
30*9880d681SAndroid Build Coastguard Worker; IMPORTGLOB1-DAG: declare void @analias
31*9880d681SAndroid Build Coastguard Worker; IMPORTGLOB1-NOT: @linkoncealias
32*9880d681SAndroid Build Coastguard Worker; IMPORTGLOB1-NOT: @linkoncefunc
33*9880d681SAndroid Build Coastguard Worker; IMPORTGLOB1-NOT: declare void @globalfunc2
34*9880d681SAndroid Build Coastguard Worker
35*9880d681SAndroid Build Coastguard Worker; Ensure that weak alias to a non-imported function is correctly
36*9880d681SAndroid Build Coastguard Worker; turned into a declaration, but that strong alias to an imported function
37*9880d681SAndroid Build Coastguard Worker; is imported as alias.
38*9880d681SAndroid Build Coastguard Worker; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=globalfunc2:%t.bc -S | FileCheck %s --check-prefix=IMPORTGLOB2
39*9880d681SAndroid Build Coastguard Worker; IMPORTGLOB2-DAG: declare void @analias
40*9880d681SAndroid Build Coastguard Worker; IMPORTGLOB2-DAG: define available_externally void @globalfunc2
41*9880d681SAndroid Build Coastguard Worker; IMPORTGLOB2-DAG: declare void @weakalias
42*9880d681SAndroid Build Coastguard Worker; IMPORTGLOB2-NOT: declare void @globalfunc1
43*9880d681SAndroid Build Coastguard Worker
44*9880d681SAndroid Build Coastguard Worker; Ensure that strong alias imported in second pass of importing ends up
45*9880d681SAndroid Build Coastguard Worker; as an alias.
46*9880d681SAndroid Build Coastguard Worker; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=globalfunc1:%t.bc -import=globalfunc2:%t.bc -S | FileCheck %s --check-prefix=IMPORTGLOB3
47*9880d681SAndroid Build Coastguard Worker; IMPORTGLOB3-DAG: declare void @analias
48*9880d681SAndroid Build Coastguard Worker; IMPORTGLOB3-DAG: define available_externally void @globalfunc1
49*9880d681SAndroid Build Coastguard Worker; IMPORTGLOB3-DAG: define available_externally void @globalfunc2
50*9880d681SAndroid Build Coastguard Worker; IMPORTGLOB3-DAG: declare void @weakalias
51*9880d681SAndroid Build Coastguard Worker
52*9880d681SAndroid Build Coastguard Worker; Ensure that strong alias imported in first pass of importing ends up
53*9880d681SAndroid Build Coastguard Worker; as an alias, and that seeing the alias definition during a second inlining
54*9880d681SAndroid Build Coastguard Worker; pass is handled correctly.
55*9880d681SAndroid Build Coastguard Worker; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=globalfunc2:%t.bc -import=globalfunc1:%t.bc -S | FileCheck %s --check-prefix=IMPORTGLOB4
56*9880d681SAndroid Build Coastguard Worker; IMPORTGLOB4-DAG: declare void @analias
57*9880d681SAndroid Build Coastguard Worker; IMPORTGLOB4-DAG: define available_externally void @globalfunc2
58*9880d681SAndroid Build Coastguard Worker; IMPORTGLOB4-DAG: define available_externally void @globalfunc1
59*9880d681SAndroid Build Coastguard Worker; IMPORTGLOB4-DAG: declare void @weakalias
60*9880d681SAndroid Build Coastguard Worker
61*9880d681SAndroid Build Coastguard Worker; An alias to an imported function is imported as alias if the function is not
62*9880d681SAndroid Build Coastguard Worker; available_externally.
63*9880d681SAndroid Build Coastguard Worker; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=linkoncefunc:%t.bc -S | FileCheck %s --check-prefix=IMPORTGLOB5
64*9880d681SAndroid Build Coastguard Worker; IMPORTGLOB5-DAG: linkoncealias = alias void (...), bitcast (void ()* @linkoncefunc to void (...)*)
65*9880d681SAndroid Build Coastguard Worker; IMPORTGLOB5-DAG: define linkonce_odr void @linkoncefunc()
66*9880d681SAndroid Build Coastguard Worker
67*9880d681SAndroid Build Coastguard Worker; Ensure that imported static variable and function references are correctly
68*9880d681SAndroid Build Coastguard Worker; promoted and renamed (including static constant variable).
69*9880d681SAndroid Build Coastguard Worker; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=referencestatics:%t.bc -S | FileCheck %s --check-prefix=IMPORTSTATIC
70*9880d681SAndroid Build Coastguard Worker; IMPORTSTATIC-DAG: @staticvar.llvm.{{.*}} = external hidden global
71*9880d681SAndroid Build Coastguard Worker; IMPORTSTATIC-DAG: @staticconstvar.llvm.{{.*}} = internal unnamed_addr constant
72*9880d681SAndroid Build Coastguard Worker; IMPORTSTATIC-DAG: define available_externally i32 @referencestatics
73*9880d681SAndroid Build Coastguard Worker; IMPORTSTATIC-DAG: %call = call i32 @staticfunc.llvm.
74*9880d681SAndroid Build Coastguard Worker; IMPORTSTATIC-DAG: %0 = load i32, i32* @staticvar.llvm.
75*9880d681SAndroid Build Coastguard Worker; IMPORTSTATIC-DAG: declare hidden i32 @staticfunc.llvm.
76*9880d681SAndroid Build Coastguard Worker
77*9880d681SAndroid Build Coastguard Worker; Ensure that imported global (external) function and variable references
78*9880d681SAndroid Build Coastguard Worker; are handled correctly (including referenced variable imported as
79*9880d681SAndroid Build Coastguard Worker; available_externally definition)
80*9880d681SAndroid Build Coastguard Worker; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=referenceglobals:%t.bc -S | FileCheck %s --check-prefix=IMPORTGLOBALS
81*9880d681SAndroid Build Coastguard Worker; IMPORTGLOBALS-DAG: @globalvar = external global
82*9880d681SAndroid Build Coastguard Worker; IMPORTGLOBALS-DAG: declare void @globalfunc1()
83*9880d681SAndroid Build Coastguard Worker; IMPORTGLOBALS-DAG: define available_externally i32 @referenceglobals
84*9880d681SAndroid Build Coastguard Worker
85*9880d681SAndroid Build Coastguard Worker; Ensure that common variable correctly imported as common defition.
86*9880d681SAndroid Build Coastguard Worker; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=referencecommon:%t.bc -S | FileCheck %s --check-prefix=IMPORTCOMMON
87*9880d681SAndroid Build Coastguard Worker; IMPORTCOMMON-DAG: @commonvar = external global
88*9880d681SAndroid Build Coastguard Worker; IMPORTCOMMON-DAG: define available_externally i32 @referencecommon
89*9880d681SAndroid Build Coastguard Worker
90*9880d681SAndroid Build Coastguard Worker; Ensure that imported static function pointer correctly promoted and renamed.
91*9880d681SAndroid Build Coastguard Worker; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=callfuncptr:%t.bc -S | FileCheck %s --check-prefix=IMPORTFUNCPTR
92*9880d681SAndroid Build Coastguard Worker; IMPORTFUNCPTR-DAG: @P.llvm.{{.*}} = external hidden global void ()*
93*9880d681SAndroid Build Coastguard Worker; IMPORTFUNCPTR-DAG: define available_externally void @callfuncptr
94*9880d681SAndroid Build Coastguard Worker; IMPORTFUNCPTR-DAG: %0 = load void ()*, void ()** @P.llvm.
95*9880d681SAndroid Build Coastguard Worker
96*9880d681SAndroid Build Coastguard Worker; Ensure that imported weak function reference/definition handled properly.
97*9880d681SAndroid Build Coastguard Worker; Imported weak_any definition should be skipped with warning, and imported
98*9880d681SAndroid Build Coastguard Worker; reference should turned into an external_weak declaration.
99*9880d681SAndroid Build Coastguard Worker; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=callweakfunc:%t.bc -import=weakfunc:%t.bc -S 2>&1 | FileCheck %s --check-prefix=IMPORTWEAKFUNC
100*9880d681SAndroid Build Coastguard Worker; IMPORTWEAKFUNC-DAG: Ignoring import request for weak-any function weakfunc
101*9880d681SAndroid Build Coastguard Worker; IMPORTWEAKFUNC-DAG: declare void @weakfunc
102*9880d681SAndroid Build Coastguard Worker; IMPORTWEAKFUNC-DAG: define available_externally void @callweakfunc
103*9880d681SAndroid Build Coastguard Worker; IMPORTWEAKFUNC-NOT: @weakvar = extern_weak global i32, align 4
104*9880d681SAndroid Build Coastguard Worker
105*9880d681SAndroid Build Coastguard Worker@globalvar = global i32 1, align 4
106*9880d681SAndroid Build Coastguard Worker@staticvar = internal global i32 1, align 4
107*9880d681SAndroid Build Coastguard Worker@staticconstvar = internal unnamed_addr constant [2 x i32] [i32 10, i32 20], align 4
108*9880d681SAndroid Build Coastguard Worker@commonvar = common global i32 0, align 4
109*9880d681SAndroid Build Coastguard Worker@P = internal global void ()* null, align 8
110*9880d681SAndroid Build Coastguard Worker
111*9880d681SAndroid Build Coastguard Worker@weakalias = weak alias void (...), bitcast (void ()* @globalfunc1 to void (...)*)
112*9880d681SAndroid Build Coastguard Worker@analias = alias void (...), bitcast (void ()* @globalfunc2 to void (...)*)
113*9880d681SAndroid Build Coastguard Worker@linkoncealias = alias void (...), bitcast (void ()* @linkoncefunc to void (...)*)
114*9880d681SAndroid Build Coastguard Worker
115*9880d681SAndroid Build Coastguard Workerdefine void @globalfunc1() #0 {
116*9880d681SAndroid Build Coastguard Workerentry:
117*9880d681SAndroid Build Coastguard Worker  ret void
118*9880d681SAndroid Build Coastguard Worker}
119*9880d681SAndroid Build Coastguard Worker
120*9880d681SAndroid Build Coastguard Workerdefine void @globalfunc2() #0 {
121*9880d681SAndroid Build Coastguard Workerentry:
122*9880d681SAndroid Build Coastguard Worker  ret void
123*9880d681SAndroid Build Coastguard Worker}
124*9880d681SAndroid Build Coastguard Worker
125*9880d681SAndroid Build Coastguard Workerdefine linkonce_odr void @linkoncefunc() #0 {
126*9880d681SAndroid Build Coastguard Workerentry:
127*9880d681SAndroid Build Coastguard Worker  ret void
128*9880d681SAndroid Build Coastguard Worker}
129*9880d681SAndroid Build Coastguard Worker
130*9880d681SAndroid Build Coastguard Workerdefine i32 @referencestatics(i32 %i) #0 {
131*9880d681SAndroid Build Coastguard Workerentry:
132*9880d681SAndroid Build Coastguard Worker  %i.addr = alloca i32, align 4
133*9880d681SAndroid Build Coastguard Worker  store i32 %i, i32* %i.addr, align 4
134*9880d681SAndroid Build Coastguard Worker  %call = call i32 @staticfunc()
135*9880d681SAndroid Build Coastguard Worker  %0 = load i32, i32* @staticvar, align 4
136*9880d681SAndroid Build Coastguard Worker  %add = add nsw i32 %call, %0
137*9880d681SAndroid Build Coastguard Worker  %1 = load i32, i32* %i.addr, align 4
138*9880d681SAndroid Build Coastguard Worker  %idxprom = sext i32 %1 to i64
139*9880d681SAndroid Build Coastguard Worker  %arrayidx = getelementptr inbounds [2 x i32], [2 x i32]* @staticconstvar, i64 0, i64 %idxprom
140*9880d681SAndroid Build Coastguard Worker  %2 = load i32, i32* %arrayidx, align 4
141*9880d681SAndroid Build Coastguard Worker  %add1 = add nsw i32 %add, %2
142*9880d681SAndroid Build Coastguard Worker  ret i32 %add1
143*9880d681SAndroid Build Coastguard Worker}
144*9880d681SAndroid Build Coastguard Worker
145*9880d681SAndroid Build Coastguard Workerdefine i32 @referenceglobals(i32 %i) #0 {
146*9880d681SAndroid Build Coastguard Workerentry:
147*9880d681SAndroid Build Coastguard Worker  %i.addr = alloca i32, align 4
148*9880d681SAndroid Build Coastguard Worker  store i32 %i, i32* %i.addr, align 4
149*9880d681SAndroid Build Coastguard Worker  call void @globalfunc1()
150*9880d681SAndroid Build Coastguard Worker  %0 = load i32, i32* @globalvar, align 4
151*9880d681SAndroid Build Coastguard Worker  ret i32 %0
152*9880d681SAndroid Build Coastguard Worker}
153*9880d681SAndroid Build Coastguard Worker
154*9880d681SAndroid Build Coastguard Workerdefine i32 @referencecommon(i32 %i) #0 {
155*9880d681SAndroid Build Coastguard Workerentry:
156*9880d681SAndroid Build Coastguard Worker  %i.addr = alloca i32, align 4
157*9880d681SAndroid Build Coastguard Worker  store i32 %i, i32* %i.addr, align 4
158*9880d681SAndroid Build Coastguard Worker  %0 = load i32, i32* @commonvar, align 4
159*9880d681SAndroid Build Coastguard Worker  ret i32 %0
160*9880d681SAndroid Build Coastguard Worker}
161*9880d681SAndroid Build Coastguard Worker
162*9880d681SAndroid Build Coastguard Workerdefine void @setfuncptr() #0 {
163*9880d681SAndroid Build Coastguard Workerentry:
164*9880d681SAndroid Build Coastguard Worker  store void ()* @staticfunc2, void ()** @P, align 8
165*9880d681SAndroid Build Coastguard Worker  ret void
166*9880d681SAndroid Build Coastguard Worker}
167*9880d681SAndroid Build Coastguard Worker
168*9880d681SAndroid Build Coastguard Workerdefine void @callfuncptr() #0 {
169*9880d681SAndroid Build Coastguard Workerentry:
170*9880d681SAndroid Build Coastguard Worker  %0 = load void ()*, void ()** @P, align 8
171*9880d681SAndroid Build Coastguard Worker  call void %0()
172*9880d681SAndroid Build Coastguard Worker  ret void
173*9880d681SAndroid Build Coastguard Worker}
174*9880d681SAndroid Build Coastguard Worker
175*9880d681SAndroid Build Coastguard Worker@weakvar = weak global i32 1, align 4
176*9880d681SAndroid Build Coastguard Workerdefine weak void @weakfunc() #0 {
177*9880d681SAndroid Build Coastguard Workerentry:
178*9880d681SAndroid Build Coastguard Worker  ret void
179*9880d681SAndroid Build Coastguard Worker}
180*9880d681SAndroid Build Coastguard Worker
181*9880d681SAndroid Build Coastguard Workerdefine void @callweakfunc() #0 {
182*9880d681SAndroid Build Coastguard Workerentry:
183*9880d681SAndroid Build Coastguard Worker  call void @weakfunc()
184*9880d681SAndroid Build Coastguard Worker  ret void
185*9880d681SAndroid Build Coastguard Worker}
186*9880d681SAndroid Build Coastguard Worker
187*9880d681SAndroid Build Coastguard Workerdefine internal i32 @staticfunc() #0 {
188*9880d681SAndroid Build Coastguard Workerentry:
189*9880d681SAndroid Build Coastguard Worker  ret i32 1
190*9880d681SAndroid Build Coastguard Worker}
191*9880d681SAndroid Build Coastguard Worker
192*9880d681SAndroid Build Coastguard Workerdefine internal void @staticfunc2() #0 {
193*9880d681SAndroid Build Coastguard Workerentry:
194*9880d681SAndroid Build Coastguard Worker  ret void
195*9880d681SAndroid Build Coastguard Worker}
196