Skip to content

Commit 20ed2b6

Browse files
authored
Fix Julia JIT integration on Windows x86_64 (#394)
1 parent aeb1417 commit 20ed2b6

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-8
lines changed

src/core/module.jl

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Modules represent the top-level structure in an LLVM program.
22

3-
export dispose,
3+
export dispose, context,
44
name, name!,
55
triple, triple!,
66
datalayout, datalayout!,
7-
context, inline_asm!,
7+
inline_asm, inline_asm!,
88
set_used!, set_compiler_used!
99

1010
# forward definition of Module in src/core/value/constant.jl
@@ -64,8 +64,19 @@ datalayout!(mod::Module, layout::String) = API.LLVMSetDataLayout(mod, layout)
6464
datalayout!(mod::Module, layout::DataLayout) =
6565
API.LLVMSetModuleDataLayout(mod, layout)
6666

67-
inline_asm!(mod::Module, asm::String) =
68-
API.LLVMSetModuleInlineAsm(mod, asm)
67+
function inline_asm!(mod::Module, asm::String; overwrite::Bool=false)
68+
if overwrite
69+
API.LLVMSetModuleInlineAsm2(mod, asm, length(asm))
70+
else
71+
API.LLVMAppendModuleInlineAsm(mod, asm, length(asm))
72+
end
73+
end
74+
75+
function inline_asm(mod::Module)
76+
out_len = Ref{Csize_t}()
77+
ptr = convert(Ptr{UInt8}, API.LLVMGetModuleInlineAsm(mod, out_len))
78+
return unsafe_string(ptr, out_len[])
79+
end
6980

7081
context(mod::Module) = Context(API.LLVMGetModuleContext(mod))
7182

src/orc.jl

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,32 @@ function add!(jljit::JuliaOJIT, jd::JITDylib, obj::MemoryBuffer)
400400
return nothing
401401
end
402402

403-
function add!(jljit::JuliaOJIT, jd::JITDylib, mod::ThreadSafeModule)
404-
@check API.JLJITAddLLVMIRModule(jljit, jd, mod)
403+
function decorate_module(mod)
404+
# TODO: check the triple, not the system
405+
if Sys.iswindows() && Sys.ARCH == :x86_64 &&
406+
!contains(inline_asm(mod), "__UnwindData")
407+
inline_asm!(mod, """
408+
.section .text
409+
.type __UnwindData,@object
410+
.p2align 2, 0x90
411+
__UnwindData:
412+
.zero 12
413+
.size __UnwindData, 12
414+
415+
.type __catchjmp,@object
416+
.p2align 2, 0x90
417+
__catchjmp:
418+
.zero 12
419+
.size __catchjmp, 12""")
420+
end
421+
end
422+
423+
function add!(jljit::JuliaOJIT, jd::JITDylib, tsm::ThreadSafeModule)
424+
# Julia's debug info expects certain symbols to be present
425+
tsm() do mod
426+
decorate_module(mod)
427+
end
428+
@check API.JLJITAddLLVMIRModule(jljit, jd, tsm)
405429
return nothing
406430
end
407431

@@ -413,17 +437,24 @@ end
413437

414438
@checked struct IRCompileLayer
415439
ref::API.LLVMOrcIRCompileLayerRef
440+
jit
416441
end
417442

418443
Base.unsafe_convert(::Type{API.LLVMOrcIRCompileLayerRef}, il::IRCompileLayer) = il.ref
419444

420445
function emit(il::IRCompileLayer, mr::MaterializationResponsibility, tsm::ThreadSafeModule)
446+
if il.jit isa JuliaOJIT
447+
# Julia's debug info expects certain symbols to be present
448+
tsm() do mod
449+
decorate_module(mod)
450+
end
451+
end
421452
API.LLVMOrcIRCompileLayerEmit(il, mr, tsm)
422453
end
423454

424455
function IRCompileLayer(jljit::JuliaOJIT)
425456
ref = API.JLJITGetIRCompileLayer(jljit)
426-
IRCompileLayer(ref)
457+
IRCompileLayer(ref, jljit)
427458
end
428459

429460
export JuliaOJIT

test/core_tests.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,11 @@ end
10321032
show(devnull, mod)
10331033

10341034
inline_asm!(mod, "nop")
1035-
@test occursin("module asm", string(mod))
1035+
@test split(inline_asm(mod)) == ["nop"]
1036+
inline_asm!(mod, "nop")
1037+
@test split(inline_asm(mod)) == ["nop", "nop"]
1038+
inline_asm!(mod, "nop"; overwrite=true)
1039+
@test split(inline_asm(mod)) == ["nop"]
10361040

10371041
dummyTriple = "SomeTriple"
10381042
triple!(mod, dummyTriple)

0 commit comments

Comments
 (0)