272
272
end
273
273
end
274
274
275
- # TODO :
276
- # Test for `callback!`, currently unsure how to trigger that.
275
+ @testset " callback!" begin
276
+ tm = JITTargetMachine ()
277
+ OrcJIT (tm) do orc
278
+ triggered = Ref {Bool} (false )
279
+
280
+ # Setup the lazy callback for creating a module
281
+ function callback (orc_ref:: LLVM.API.LLVMOrcJITStackRef , callback_ctx:: Ptr{Cvoid} )
282
+ orc = OrcJIT (orc_ref)
283
+ sym = mangle (orc, " SomeFunction" )
284
+
285
+ # 1. IRGen & Optimize the module
286
+ orc_mod = Context () do ctx
287
+ mod = LLVM. Module (" jit" , ctx)
288
+ ft = LLVM. FunctionType (LLVM. VoidType (ctx))
289
+ fn = LLVM. Function (mod, sym, ft)
290
+
291
+ Builder (ctx) do builder
292
+ entry = BasicBlock (fn, " entry" )
293
+ position! (builder, entry)
294
+ ret! (builder)
295
+ end
296
+ verify (mod)
297
+
298
+ triple! (mod, triple (tm))
299
+ ModulePassManager () do pm
300
+ add_library_info! (pm, triple (mod))
301
+ add_transform_info! (pm, tm)
302
+ run! (pm, mod)
303
+ end
304
+ verify (mod)
305
+
306
+ # 2. Add the IR module to the JIT
307
+ return compile! (orc, mod)
308
+ end
309
+
310
+ # 3. Obtain address of compiled module
311
+ addr = addressin (orc, orc_mod, sym)
312
+
313
+ # 4. Update the stub pointer to point to the recently compiled module
314
+ set_stub! (orc, " lazystub" , addr)
315
+
316
+ # 5. Return the address of tie implementation, since we are going to call it now
317
+ triggered[] = true
318
+ return addr. ptr
319
+ end
320
+ c_callback = @cfunction ($ callback, UInt64, (LLVM. API. LLVMOrcJITStackRef, Ptr{Cvoid}))
321
+
322
+ GC. @preserve c_callback begin
323
+ initial_addr = callback! (orc, c_callback, C_NULL )
324
+ create_stub! (orc, " lazystub" , initial_addr)
325
+ addr = address (orc, " lazystub" )
326
+
327
+ ccall (pointer (addr), Cvoid, ()) # Triggers compilation
328
+ end
329
+ @test triggered[]
330
+ end
331
+ end
277
332
278
333
end
0 commit comments