Skip to content

Commit 5ffcaa9

Browse files
committed
[Orc] fix and test callback!
1 parent ea9bb77 commit 5ffcaa9

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

src/orc.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ end
150150

151151
function callback!(orc::OrcJIT, callback, ctx)
152152
r_address = Ref{API.LLVMOrcTargetAddress}()
153-
LLVM.API.LLVMOrcLazyCompileCallback(orc, r_address, callback, ctx)
153+
API.LLVMOrcCreateLazyCompileCallback(orc, r_address, callback, ctx)
154154
return OrcTargetAddress(r_address[])
155155
end
156156

test/orc.jl

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,62 @@ end
272272
end
273273
end
274274

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
277332

278333
end

0 commit comments

Comments
 (0)