Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A PoC-ish Trick: LLVM IR Generation with Ruby

remore
November 11, 2016

A PoC-ish Trick: LLVM IR Generation with Ruby

A Lightning Talk at RubyConf 2016

remore

November 11, 2016
Tweet

More Decks by remore

Other Decks in Technology

Transcript

  1. A PoC-ish Trick: LLVM IR
    Generation with Ruby
    A Lightning Talk at RubyConf 2016
    Kei Sawada (@remore)

    View Slide

  2. View Slide

  3. a modern, fast, and open source language for
    data science and technical computing

    View Slide

  4. Julia is
    4 years old young
    Good performance, as fast as C
    Dynamic typing
    Julia has
    Capability to call C functions directly without
    wrappers or special APIs
    a compiler that used LLVM to translate Julia
    programs into ef cient executable code

    View Slide

  5. In short,

    View Slide

  6. Write some Julia code then you
    will get LLVM IR without hassle

    View Slide

  7. debussy:~ guest$ julia
    _
    _ _ _(_)_ | A fresh approach to technical computin
    (_) | (_) (_) | Documentation: http://docs.julialang.o
    _ _ _| |_ __ _ | Type "?help" for help.
    | | | | | | |/ _` | |
    | | |_| | | | (_| | | Version 0.4.6 (2016-06-19 17:16 UTC)
    _/ |\__'_|_|_|\__'_| |
    |__/ | x86_64-apple-darwin15.5.0
    julia>

    View Slide

  8. debussy:~ guest$ julia
    _
    _ _ _(_)_ | A fresh approach to technical computin
    (_) | (_) (_) | Documentation: http://docs.julialang.o
    _ _ _| |_ __ _ | Type "?help" for help.
    | | | | | | |/ _` | |
    | | |_| | | | (_| | | Version 0.4.6 (2016-06-19 17:16 UTC)
    _/ |\__'_|_|_|\__'_| |
    |__/ | x86_64-apple-darwin15.5.0
    julia> function hello()
    "rubyconf"
    end
    hello (generic function with 1 method)
    julia>

    View Slide

  9. debussy:~ guest$ julia
    _
    _ _ _(_)_ | A fresh approach to technical computin
    (_) | (_) (_) | Documentation: http://docs.julialang.o
    _ _ _| |_ __ _ | Type "?help" for help.
    | | | | | | |/ _` | |
    | | |_| | | | (_| | | Version 0.4.6 (2016-06-19 17:16 UTC)
    _/ |\__'_|_|_|\__'_| |
    |__/ | x86_64-apple-darwin15.5.0
    julia> function hello()
    "rubyconf"
    end
    hello (generic function with 1 method)
    julia> hello()
    "rubyconf"
    julia>

    View Slide

  10. julia> code_llvm(hello, ())
    define %jl_value_t* @julia_hello_21549() {
    top:
    ret %jl_value_t* inttoptr (i64 4447575696 to %jl_value_t*)
    }
    julia>

    View Slide

  11. julia> code_llvm(hello, ())
    define %jl_value_t* @julia_hello_21549() {
    top:
    ret %jl_value_t* inttoptr (i64 4447575696 to %jl_value_t*)
    }
    julia> code_native(hello, ())
    .section __TEXT,__text,regular,pure_instructions
    Filename: none
    Source line: 2
    pushq %rbp
    movq %rsp, %rbp
    movabsq $4447575696, %rax ## imm = 0x109189E90
    Source line: 2
    popq %rbp
    ret
    julia>

    View Slide

  12. OK Julia is a thing
    But we are in

    View Slide

  13. View Slide

  14. What if

    View Slide

  15. You can write some Ruby code
    which can be translated into Julia
    where you can get LLVM IR easily?

    View Slide

  16. Ruby -> Julia -> LLVM IR

    View Slide

  17. View Slide

  18. Julializer
    Allow you to convert your arbitrary
    Ruby code into Julia code

    View Slide

  19. Time to DEMO!

    View Slide

  20. For more examples and attempts, check out:
    github.com/remore/julializer
    github.com/remore/virtual_module
    Thanks!

    View Slide