Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Excerpt:

"

julia> a = function (x); x += 1; x += 1; return x; end

#13 (generic function with 1 method)

julia> @code_llvm a(3)

; @ none:1 within `#13'

define i64 @"julia_#13_17607"(i64) {

top:

; ┌ @ int.jl:53 within `+'

   %1 = add i64 %0, 2 <-- <one instruction, not two>
; └

  ret i64 %1
}"

Prior to reading this article, I did not know that Julia could compile LLVM at the function level... very cool!



It can also show you assembly at the function level:

   julia> a=1+2im; b=2+4im; @code_native a+b
           .text
   ; ┌ @ complex.jl:271 within `+'
           pushq   %rbp
           movq    %rsp, %rbp
   ; │ @ complex.jl:271 within `+' @ int.jl:53
           vmovdqu (%r8), %xmm0
           vpaddq  (%rdx), %xmm0, %xmm0
   ; │ @ complex.jl:271 within `+'
           vmovdqu %xmm0, (%rcx)
           movq    %rcx, %rax
           popq    %rbp
           retq
           nopw    %cs:(%rax,%rax)
   ; └


You can also use @code_native to look at the assembly code of any function, @code_lowered/@code_typed to see the Julia IR (which you can also modify before fully compiling, which is how source-to-source differentiation with Zygote works) and @code_warntype to evaluate the type inference result. This is a nice diagram of Julia's multi-stage compilation:

https://blog.rogerluo.me/images/julia-compile-diagram.png




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: