:@code_llvm

..  @code_llvm

Evaluates the arguments to the function call, determines their types, and calls :func:`code_llvm` on the resulting expression.

Examples

The @code_llvm macro in Julia is used to evaluate the arguments to a function call, determine their types, and then call the code_llvm function on the resulting expression. It provides the LLVM Intermediate Representation (IR) for the function call.

Here are some examples of how to use @code_llvm:

  1. Inspecting the LLVM IR for a simple function call:

    julia> function add_numbers(a, b)
              return a + b
           end
    julia> @code_llvm add_numbers(2, 3)
    ; Function add_numbers
    ; Location: REPL[1]:2
    define i64 @julia_add_numbers_17947(i64, i64) {
    top:
     ; Location: REPL[1]:3
     %2 = add i64 %0, %1
     ret i64 %2
    }

    This example shows the LLVM IR generated for the add_numbers function call with arguments 2 and 3.

  2. Inspecting the LLVM IR for a more complex function call:

    julia> function factorial(n)
              if n == 0
                  return 1
              else
                  return n * factorial(n - 1)
              end
           end
    julia> @code_llvm factorial(5)
    ; Function factorial
    ; Location: REPL[1]:2
    define i64 @julia_factorial_17948(i64) {
    top:
     ; Location: REPL[1]:3
     %1 = icmp eq i64 %0, 0
     br i1 %1, label %L7, label %L3
    
    L3:                                               ; preds = %top
     ; Location: REPL[1]:4
     %2 = add i64 %0, -1
     %3 = call i64 @julia_factorial_17948(i64 %2)
     %4 = mul i64 %0, %3
     br label %L7
    
    L7:                                               ; preds = %L3, %top
     ; Location: REPL[1]:6
     %5 = phi i64 [ 1, %top ], [ %4, %L3 ]
     ret i64 %5
    }

    This example shows the LLVM IR generated for the factorial function call with argument 5.

Common mistake example:

julia> @code_llvm add_numbers("2", 3)
ERROR: MethodError: no method matching add_numbers(::String, ::Int64)

In this example, the @code_llvm macro throws an error because the arguments provided do not match the expected types for the function add_numbers. Ensure that the arguments passed to the function are of the correct types to avoid such errors.

See Also

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: