code_native

code_native(f, types)

Prints the native assembly instructions generated for running the method matching the given generic function and type signature to STDOUT.

Examples

julia> mysin(x) = ccall((:sin,"libm"), Cdouble, (Cdouble,), x)
    @vectorize_1arg Real mysin
    mysin([1,2,3,4])
    code_native(mysin, (Float64,))
.text
Filename: In[11]
Source line: 1
    push    RBP
    mov RBP, RSP
    movabs  RAX, 140193336993536
Source line: 1
    call    RAX
    pop RBP
    ret
  1. Print native assembly instructions for a function with specific types:

    julia> function add_numbers(a::Int, b::Int)
              return a + b
           end
    julia> code_native(add_numbers, (Int, Int))
    
           .text
    ; ┌ @ In[1]:2 within `add_numbers`
           push    RBP
           mov     RBP, RSP
           add     RDI, RSI
           mov     RAX, RDI
           pop     RBP
           ret
           nop
           nop
    ; └

    This example shows the native assembly instructions generated for the add_numbers function when called with two Int arguments.

  2. Inspect native assembly instructions for a generic function:

    julia> function multiply_numbers(a, b)
              return a * b
           end
    julia> code_native(multiply_numbers, (Any, Any))
    
           .text
    ; ┌ @ In[2]:2 within `multiply_numbers`
           push    RBP
           mov     RBP, RSP
           push    RSI
           push    RDI
           push    RAX
           mov     RAX, RSI
           mul     RDI
           pop     RDX
           pop     RDX
           pop     RDX
           mov     RDI, RAX
           pop     RBP
           ret
           nop
           nop
    ; └

    This example demonstrates the native assembly instructions generated for the multiply_numbers generic function when called with any argument types.

Common mistake example:

julia> code_native(println, (String,))
ERROR: MethodError: no method matching println(::Type{String})
Closest candidates are:
  println(::Any, ::Any, ::Any, ::Any...) at io.jl:104
  println(::Any, ::Any, ::Any...) at io.jl:105
  println(::IJulia.IJuliaStdio{Base.PipeEndpoint}, ::Any, ::Any, ::Any...) at /Users/runner/.julia/packages/IJulia/DrVMH/src/stdio.jl:273
  ...

In this example, the println function is not defined for the specific type String. Make sure to provide valid function and type arguments to code_native to avoid such errors.

See Also

assert, backtrace, code_llvm, code_lowered, code_native, code_typed, code_warntype, :@which, compilecache, current_module, eval, finalize, finalizer, fullname, function_module, function_name, include_dependency, InterruptException, invoke, isconst, isdefined, isgeneric, methodswith, method_exists, module_name, module_parent, require, subtypes, unsafe_load, workspace, __precompile__,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: