Skip to content

Support compiling to JavaScript with Emscripten #9430

Closed

Description

@tshort

With Julia's improved static compilation, compilation to JavaScript is getting closer. This feature would allow Julia code to be run from static web pages. Eventually, it may be possible to compile all of Julia, REPL and all.

Activity

  1. tshort commented on Dec 21, 2014

    @tshort
    ContributorAuthor

    I've tried and given up on this a couple of times, so I'm describing my progress to date in case anyone has advice or wants to try it. It's easy to generate a large LLVM bitcode file containing the julia/base library. Run the following from within the julia/base directory:

    ../julia --build XX --dump-bitcode=yes --compile=all -J ../usr/lib/julia/sys0.ji -f sysimg.jl
    

    Now, you should have an XX.bc file in the base directory. To identify functions in the bc file, use the following:

    llvm-nm XX.bc | grep _abs_
    

    That'll give something like:

    T jlcall_abs_24823
    T jlcall_abs_57792
    T jlcall_abs_57794
    T jlcall_abs_57795
    T jlcall_abs_57796
    T julia_abs_24823
    T julia_abs_42171
    T julia_abs_42172
    T julia_abs_42173
    T julia_abs_42174
    T julia_abs_42175
    T julia_abs_57792
    T julia_abs_57793
    T julia_abs_57794
    T julia_abs_57795
    T julia_abs_57796
    T julia_abs_57797
    T julia_abs_57798
    T julia_abs_57799
    T julia_abs_57800
    T julia_abs_57801
    T julia_abs_57802
    T julia_abs_57803
    T julia_abs_57804
    T julia_abs_57805
    T julia_abs_57806
    T julia_abs_57807
    

    To compile one of these functions to JavaScript, use emcc:

    emcc -v XX.bc -o XX.js -s EXPORTED_FUNCTIONS="['_julia_abs_42171]"
    

    A few of the simplest functions will compile to working JavaScript code. Several others will error as described below.

    Here are current issues:

    • 32-bit bitcode -- The most common error from the compilation step above is "i64 thing we can't legalize yet". Emscripten expects 32-bit bitcode. On a 64-bit platform, it can be difficult to generate 32-bit binaries. I tried modifying the contrib/build_sysimg.jl script to try to get 32-bit results by compiling with the i686 target. I wasn unsuccessful with that.

    • Struct issues -- Sometimes, we get the following error: Here's a bug report on this:

      https://code.google.com/p/nativeclient/issues/detail?id=3932

      Someone has compiled Rust with Emscripten and may have overcome similar issues:

      https://www.reddit.com/r/rust_gamedev/comments/2n0x08/emscripten_experiments/cmcakwr

      Also, here's a thread on using Emscripten with Rust that has interesting info:

      Make Rust work with emscripten rust-lang/rust#2235

    • Symbol names -- Julia creates several symbol names with # in them. When converted to JavaScript, it causes errors. This one's probably not too bad to fix, either in Julia or in Emscripten.

    Another issue is compiling libjulia to bitcode. I've gotten about 80% or more of it to compile with emcc. I had to strip out the libuv stuff. I haven't tried building any of the third-party libraries.

  2. nolta commented on Dec 29, 2014

    @nolta
    Member

    Dup of #2418.

  3. ihnorton commented on Dec 30, 2014

    @ihnorton
    Member

    If anything, I would close the other one because it is fairly speculative whereas this issue is substantive (if only partially actionable in base). Emscripten keeps coming up, so we might as well have a central place to point people to.

  4. StefanKarpinski commented on Dec 30, 2014

    @StefanKarpinski
    SponsorMember

    I'd favor that – #2418 was pretty vague.

  5. EricForgy commented on Jan 24, 2015

    @EricForgy

    This sounds awesome.

    I am having some difficulty getting Emscripten installed, but once I do, I can try to play around with this.

    Getting all of Julia including REPL compiled sounds ambitious, but it looks like bits and pieces already can be compiled. Would it makes sense to try to compile a small version of Julia (Juliet?) into Javascript as a start? I think it would already be super cool if we could do basic matrix algebra and maybe an svd or eig here and there and lean on JS proper for a lot of the other stuff.

    For this smaller scope, we could probably even write a parser in JS if that helps.

    Just so I understand, if we were able to do this, would we be able to write code like this in the JS console:

    var a = rand(5,5);
    var b = rand(5,5);
    var c = .5*((a+b)+(a+b)');
    var out = svd(c);
    var u = out[0],
        v = out[1],
        w = out[2];
    console.log(u);
    

    One can dream :D

    What would the usage look like?

    By the way, Tom, I noticed it appears you managed to compile CLAPACK to JS. How is that? I'd love to learn how to use that.

  6. ViralBShah commented on Jan 26, 2015

    @ViralBShah
    SponsorMember

    Or just translate LAPACK to Julia. However there are too many other libraries we depend on.

  7. EricForgy commented on Jan 26, 2015

    @EricForgy

    Baby steps...

    Running a Julia function from the JS console in Chrome...

    image

    The JS code:

    function _julia_abs_49197($0) {
    $0 = +$0;
    var $1 = 0, $2 = 0, $3 = 0.0, label = 0, sp = 0;
    sp = STACKTOP;
    $1 = (HEAPF32[tempDoublePtr>>2]=$0,HEAP32[tempDoublePtr>>2]|0);
    $2 = $1 & 2147483647;
    $3 = (HEAP32[tempDoublePtr>>2]=$2,+HEAPF32[tempDoublePtr>>2]);
    STACKTOP = sp;return (+$3);
    }
    
  8. tshort commented on Feb 18, 2015

    @tshort
    ContributorAuthor

    Someone is also looking to make this work with Numba (Python/NumPy that uses LLVM):

    http://cyrille.rossant.net/numpy-browser-llvm/

  9. jskDr commented on Feb 19, 2015

    @jskDr

    It is interesting to know it. Everything go on Web now. However, still
    Javascript is too realistic while desktop script languages become more and
    more being abstract.

    On Wed, Feb 18, 2015 at 4:56 PM, Tom Short notifications@github.com wrote:

    Someone is also looking to make this work with Numba (Python/NumPy that
    uses LLVM):

    http://cyrille.rossant.net/numpy-browser-llvm/

    —
    Reply to this email directly or view it on GitHub
    #9430 (comment).

    Best regards,
    James Sungjin Kim, Ph.D.
    Samsung Advanced Institute of Technology (SAIT)
    Samsung. Electronics Co., LTD

  10. tshort commented on Jun 19, 2015

    @tshort
    ContributorAuthor

    Given the recent announcements about WebAssembly, the best path may be to wait until that is ready. Their development plan is to build an official LLVM backend. Emscripten uses an out-of-tree fork of LLVM, so version issues can be a problem. With a built-in backend for WebAssembly, compilation for the web might be an easier path.

  11. StefanKarpinski commented on Jun 19, 2015

    @StefanKarpinski
    SponsorMember

    WebAssembly is pretty exciting – it makes running Julia code in the browser a real prospect.

  12. 11 remaining items

  13. fasiha commented on Jan 27, 2016

    @fasiha

    OpenLibm and BLAS have inline assembly – not inline LLVM assembly.

    I'm curious: do the functions containing assembly in these libraries lack
    portable C/Fortran, non-assembly, versions?

  14. vtjnash commented on Jan 28, 2016

    @vtjnash
    SponsorMember

    yes, there's a reference implementation in C. lapack is a bit harder, since it is in fortran, but it turns out there is an SO post on the topic: http://stackoverflow.com/questions/21990243/use-emscripten-with-fortran-lapack-binding

  15. tkelman commented on Jan 28, 2016

    @tkelman
    Contributor

    Dragonegg is basically dead, unfortunately. End of this year the Nvidia/PGI Fortran LLVM front-end should get released.

  16. Ismael-VC commented on Jan 28, 2016

    @Ismael-VC
    Contributor

    Wouldn't compiled binary emitted by a hypothetical slimmed down non-numerical Julia work with Web assembly?

    @datnamer I think that is also the way we are going to, see:

    Also currently @ScottPJones is working on a julia-lite version, that we are testing on RaspberryPi2, see:

    Discussion at Gitter chat room:

  17. kripken commented on Jan 29, 2016

    @kripken

    It might be worth trying again to get this working with emscripten - around a year passed since the last attempt based on the history here. Fixes since then might have helped. In particular, the Rust work mentioned above has matured, including struct fixes and so forth.

    There is also a current PR for another possibly relevant issue.

  18. ylluminate commented on Feb 2, 2016

    @ylluminate
  19. datnamer commented on Mar 13, 2016

    @datnamer

    Maybe with the revamped Julia -> c transpiler, the c code can be compiled to web assembly.

  20. vtjnash commented on Mar 13, 2016

    @vtjnash
    SponsorMember

    this will be cool when it is possible, and we intend to continue working towards this (going through the Julia2C transpiler is not the best path forward), but I'm going to close the issue since it's not particularly actionable as a bug report. (although discussion here and on julia-dev is still encouraged to continue)

  21. StefanKarpinski commented on Mar 14, 2016

    @StefanKarpinski
    SponsorMember

    @vtjnash: The issue tracker is not just for bug reports, but also for feature requests. I'll leave this one closed since we don't really need an issue to track it, but please don't close everything that's not a bug.

  22. vtjnash commented on Mar 14, 2016

    @vtjnash
    SponsorMember

    i'm not attempting to close all feature requests, only ones (like this one) that aren't specifically actionable and thus don't need an issue to track it

  23. datnamer commented on Mar 15, 2016

    @datnamer

    So what is being thought of as the most potentially successful path forward if not the c transpilation?

  24. StefanKarpinski commented on Mar 15, 2016

    @StefanKarpinski
    SponsorMember

    Going through LLVM without C would be simpler.

  25. ylluminate commented on Mar 15, 2016

    @ylluminate

    Has anyone evaluated @kripken's remarks wrt emscripten?

  26. PallHaraldsson commented on Aug 24, 2016

    @PallHaraldsson
    Contributor

    Is this "actionable" now (at least for @ScottPJones's Julia-lite branch, that is available), with or without Julia2C? The PR that @kripken mentioned is merged.

    "Maybe with the revamped Julia -> c transpiler, the c code can be compiled to web assembly."

    [or asm.js], anyway is Julia2C broken (or only for 0.3?), would if be much work to get it to work if it is, maybe not for this.

    [Was this not actionable, only because of assembly/BLAS/LAPACK: "yes, there's a reference implementation in C. lapack is a bit harder, since it is in fortran"? I would be ok without BLAS/LAPACK. There are many functions besides matrix multiply, but how far would e.g. pure-Julia native [naive] matrix multiply get you (ans say pow)?]

    [Also more discussion at https://github.com//issues/2418#issuecomment-168565188 (that was closed in favor of this one), since, I'm just not sure what "Biwa" is.]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions