1
1
@testset " orc" begin
2
2
3
- let ctx = Context ()
3
+ @testset " Undefined Symbol" begin
4
+ ctx = Context ()
5
+ tm = JITTargetMachine ()
6
+ orc = OrcJIT (tm)
7
+
8
+ mod = LLVM. Module (" jit" , ctx)
9
+ T_Int32 = LLVM. Int32Type (ctx)
10
+ ft = LLVM. FunctionType (T_Int32, [T_Int32, T_Int32])
11
+ fn = LLVM. Function (mod, " mysum" , ft)
12
+ linkage! (fn, LLVM. API. LLVMExternalLinkage)
13
+
14
+ fname = mangle (orc, " wrapper" )
15
+ wrapper = LLVM. Function (mod, fname, ft)
16
+ # generate IR
17
+ Builder (ctx) do builder
18
+ entry = BasicBlock (wrapper, " entry" , ctx)
19
+ position! (builder, entry)
20
+
21
+ tmp = call! (builder, fn, [parameters (wrapper)... ])
22
+ ret! (builder, tmp)
23
+ end
24
+
25
+ triple! (mod, triple (tm))
26
+ ModulePassManager () do pm
27
+ add_library_info! (pm, triple (mod))
28
+ add_transform_info! (pm, tm)
29
+ run! (pm, mod)
30
+ end
31
+ verify (mod)
32
+
33
+ orc_mod = compile! (orc, mod)
34
+ @test_throws ErrorException address (orc, fname)
35
+
36
+ delete! (orc, orc_mod)
37
+ dispose (orc)
38
+ end
39
+
40
+ @testset " Custom Resolver" begin
41
+ ctx = Context ()
4
42
tm = JITTargetMachine ()
5
43
orc = OrcJIT (tm)
6
44
@@ -14,10 +52,10 @@ let ctx = Context()
14
52
end
15
53
fnames[name] += 1
16
54
17
- return get ( known_functions, name, OrcTargetAddress ( C_NULL )) . ptr
55
+ return known_functions[ name] . ptr
18
56
catch ex
19
- @error " Exception during lookup" exception= (ex, catch_backtrace ())
20
- return UInt64 ( 0 )
57
+ @error " Exception during lookup" name exception= (ex, catch_backtrace ())
58
+ error ( " OrcJIT: Could not find symbol " )
21
59
end
22
60
end
23
61
@@ -51,7 +89,7 @@ let ctx = Context()
51
89
52
90
f_lookup = @cfunction ($ lookup, UInt64, (Cstring, Ptr{Cvoid}))
53
91
GC. @preserve f_lookup begin
54
- orc_mod = compile! (orc, mod, f_lookup, lazy= true ) # will capture f_lookup
92
+ orc_mod = compile! (orc, mod, f_lookup, C_NULL , lazy= true ) # will capture f_lookup
55
93
56
94
addr = address (orc, fname)
57
95
@test errormsg (orc) == " "
@@ -73,7 +111,101 @@ let ctx = Context()
73
111
dispose (orc)
74
112
end
75
113
76
- let ctx = Context ()
114
+ @testset " Default Resolver + Stub" begin
115
+ ctx = Context ()
116
+ tm = JITTargetMachine ()
117
+ orc = OrcJIT (tm)
118
+
119
+ mod = LLVM. Module (" jit" , ctx)
120
+ T_Int32 = LLVM. Int32Type (ctx)
121
+ ft = LLVM. FunctionType (T_Int32, [T_Int32, T_Int32])
122
+ fn = LLVM. Function (mod, " mysum" , ft)
123
+ linkage! (fn, LLVM. API. LLVMExternalLinkage)
124
+
125
+ fname = mangle (orc, " wrapper" )
126
+ wrapper = LLVM. Function (mod, fname, ft)
127
+ # generate IR
128
+ Builder (ctx) do builder
129
+ entry = BasicBlock (wrapper, " entry" , ctx)
130
+ position! (builder, entry)
131
+
132
+ tmp = call! (builder, fn, [parameters (wrapper)... ])
133
+ ret! (builder, tmp)
134
+ end
135
+
136
+ triple! (mod, triple (tm))
137
+ ModulePassManager () do pm
138
+ add_library_info! (pm, triple (mod))
139
+ add_transform_info! (pm, tm)
140
+ run! (pm, mod)
141
+ end
142
+ verify (mod)
143
+
144
+ create_stub! (orc, mangle (orc, " mysum" ), OrcTargetAddress (@cfunction (+ , Int32, (Int32, Int32))))
145
+
146
+ orc_mod = compile! (orc, mod)
147
+
148
+ addr = address (orc, fname)
149
+ @test errormsg (orc) == " "
150
+
151
+ r = ccall (pointer (addr), Int32, (Int32, Int32), 1 , 2 )
152
+ @test r == 3
153
+
154
+ delete! (orc, orc_mod)
155
+ dispose (orc)
156
+ end
157
+
158
+ @testset " Default Resolver + Global Symbol" begin
159
+ ctx = Context ()
160
+ tm = JITTargetMachine ()
161
+ orc = OrcJIT (tm)
162
+
163
+ mod = LLVM. Module (" jit" , ctx)
164
+ T_Int32 = LLVM. Int32Type (ctx)
165
+ ft = LLVM. FunctionType (T_Int32, [T_Int32, T_Int32])
166
+ mysum = mangle (orc, " mysum" )
167
+ fn = LLVM. Function (mod, mysum, ft)
168
+ linkage! (fn, LLVM. API. LLVMExternalLinkage)
169
+
170
+ fname = mangle (orc, " wrapper" )
171
+ wrapper = LLVM. Function (mod, fname, ft)
172
+ # generate IR
173
+ Builder (ctx) do builder
174
+ entry = BasicBlock (wrapper, " entry" , ctx)
175
+ position! (builder, entry)
176
+
177
+ tmp = call! (builder, fn, [parameters (wrapper)... ])
178
+ ret! (builder, tmp)
179
+ end
180
+
181
+ triple! (mod, triple (tm))
182
+ ModulePassManager () do pm
183
+ add_library_info! (pm, triple (mod))
184
+ add_transform_info! (pm, tm)
185
+ run! (pm, mod)
186
+ end
187
+ verify (mod)
188
+
189
+ # Should do pretty much the same as `@ccallable`
190
+ LLVM. add_symbol (mysum, @cfunction (+ , Int32, (Int32, Int32)))
191
+ ptr = LLVM. find_symbol (mysum)
192
+ @test ptr != = C_NULL
193
+ @test ccall (ptr, Int32, (Int32, Int32), 1 , 2 ) == 3
194
+
195
+ orc_mod = compile! (orc, mod, lazy= true )
196
+
197
+ addr = address (orc, fname)
198
+ @test errormsg (orc) == " "
199
+
200
+ r = ccall (pointer (addr), Int32, (Int32, Int32), 1 , 2 )
201
+ @test r == 3
202
+
203
+ delete! (orc, orc_mod)
204
+ dispose (orc)
205
+ end
206
+
207
+ @testset " Loading ObjectFile" begin
208
+ ctx = Context ()
77
209
tm = JITTargetMachine ()
78
210
orc = OrcJIT (tm)
79
211
sym = mangle (orc, " SomeFunction" )
@@ -96,6 +228,12 @@ let ctx = Context()
96
228
97
229
@test addr. ptr != 0
98
230
delete! (orc, orc_m)
231
+ end
232
+
233
+ @testset " Stubs" begin
234
+ ctx = Context ()
235
+ tm = JITTargetMachine ()
236
+ orc = OrcJIT (tm)
99
237
100
238
toggle = Ref {Bool} (false )
101
239
on () = (toggle[] = true ; nothing )
0 commit comments