@@ -8,10 +8,11 @@ using Libdl: Libdl, dlsym, dlopen
88using Base: RefValue
99using Serialization: serialize, deserialize
1010using Clang_jll: clang
11+ using LLD_jll: lld
1112using StaticTools
1213using StaticTools: @symbolcall , @c_str , println
1314
14- export compile, load_function, compile_shlib, compile_executable
15+ export compile, load_function, compile_shlib, compile_executable, compile_wasm
1516export native_code_llvm, native_code_typed, native_llvm_module, native_code_native
1617export @device_override , @print_and_throw
1718
@@ -154,7 +155,8 @@ function generate_obj(f, tt, external = true, path::String = tempname(), name =
154155 mixtape = NoContext (),
155156 strip_llvm = false ,
156157 strip_asm = true ,
157- opt_level= 3 ,
158+ opt_level = 3 ,
159+ remove_julia_addrspaces = false ,
158160 target = (),
159161 kwargs... )
160162 mkpath (path)
@@ -180,7 +182,7 @@ function generate_obj(f, tt, external = true, path::String = tempname(), name =
180182
181183 # Now that we've removed all the pointers from the code, we can (hopefully) safely lower all the instrinsics
182184 # (again, using Enzyme's pipeline)
183- post_optimize! (mod, tm)
185+ post_optimize! (mod, tm; remove_julia_addrspaces )
184186
185187 # Make sure we didn't make any glaring errors
186188 LLVM. verify (mod)
@@ -355,6 +357,40 @@ function compile_shlib(funcs::Array, path::String="./";
355357 joinpath (abspath (path), filename * " ." * Libdl. dlext)
356358end
357359
360+ """
361+ ```julia
362+ compile_wasm(f::Function, types::Tuple, [path::String="./"], [name::String=repr(f)]; filename::String=name, flags=``, kwargs...)
363+ compile_wasm(funcs::Array, [path::String="./"]; filename="libfoo", demangle=false, flags=``, kwargs...)
364+ ```
365+ As `compile_shlib`, but compiling to a WebAssembly library.
366+
367+ The compiled function is by default given the symbol name `julia_$(name) `, i.e.,
368+ the function `test` in the example below is called `julia_test` in the shared library.
369+ The keword argument `demangle=true` will remove this prefix, but is currently only
370+ supported the second (multi-function-shlib) method.
371+ ```
372+ """
373+ function compile_wasm (f:: Function , types= ();
374+ path:: String = " ./" ,
375+ filename= fix_name (repr (f)),
376+ flags= ` ` ,
377+ kwargs...
378+ )
379+ tt = Base. to_tuple_type (types)
380+ obj_path, name = generate_obj (f, tt, true , path, filename; target = (triple = " wasm32-unknown-wasi" , cpu = " " , features = " " ), remove_julia_addrspaces = true , kwargs... )
381+ run (` $(lld ()) -flavor wasm --no-entry --export-all $flags $obj_path /obj.o -o $path /$name .wasm` )
382+ joinpath (abspath (path), filename * " .wasm" )
383+ end
384+ function compile_wasm (funcs:: Array ;
385+ path:: String = " ./" ,
386+ filename= " libfoo" ,
387+ flags= ` ` ,
388+ kwargs...
389+ )
390+ obj_path, name = generate_obj (funcs, true ; target = (triple = " wasm32-unknown-wasi" , cpu = " " , features = " " ), remove_julia_addrspaces = true , kwargs... )
391+ run (` $(lld ()) -flavor wasm --no-entry --export-all $flags $obj_path /obj.o -o $path /$filename .wasm` )
392+ joinpath (abspath (path), filename * " .wasm" )
393+ end
358394
359395"""
360396```julia
@@ -606,7 +642,7 @@ function native_llvm_module(funcs::Array; demangle = false, kwargs...)
606642 return mod
607643end
608644
609- function generate_obj (funcs:: Array , external, path:: String = tempname (), filenamebase:: String = " obj" ;
645+ function generate_obj (funcs:: Array , external:: Bool , path:: String = tempname (), filenamebase:: String = " obj" ;
610646 demangle = false ,
611647 strip_llvm = false ,
612648 strip_asm = true ,
@@ -615,7 +651,7 @@ function generate_obj(funcs::Array, external, path::String = tempname(), filenam
615651 f, tt = funcs[1 ]
616652 mkpath (path)
617653 obj_path = joinpath (path, " $filenamebase .o" )
618- fakejob, kwargs = native_job (f, tt, external, kwargs... )
654+ fakejob, kwargs = native_job (f, tt, external; kwargs... )
619655 mod = native_llvm_module (funcs; demangle = demangle, kwargs... )
620656 obj, _ = GPUCompiler. emit_asm (fakejob, mod; strip= strip_asm, validate= false , format= LLVM. API. LLVMObjectFile)
621657 open (obj_path, " w" ) do io
0 commit comments