Skip to main content

Mojo function

compile_info

compile_info[func_type: AnyTrivialRegType, //, func: func_type, /, *, emission_kind: StringSlice[StaticConstantOrigin] = __init__[__mlir_type.!kgen.string]("asm"), compile_options: StringSlice[StaticConstantOrigin] = __init__[__mlir_type.!kgen.string](""), target: target = _current_target()]() -> Info[func_type, func, target]

Compiles a function and returns detailed compilation information.

This function takes a Mojo function and compiles it, providing access to the generated assembly code, linkage information, and other compilation artifacts. It can be used for inspection, debugging, and low-level optimization.

Example:

from compile import compile_info

fn my_func(x: Int) -> Int:
return x

info = compile_info[my_func]()
print(info) # Print assembly
from compile import compile_info

fn my_func(x: Int) -> Int:
return x

info = compile_info[my_func]()
print(info) # Print assembly

Note: The compilation is always performed, even if the function is not used. For performance-critical code, consider caching the compilation results.

Parameters:

  • func_type (AnyTrivialRegType): Type of the function to compile. Must be a trivially-copyable register type.
  • func (func_type): The function to compile. Must match the specified func_type.
  • emission_kind (StringSlice[StaticConstantOrigin]): The desired output format. Valid options are:
    • "asm": Assembly code (default).
    • "llvm": Unoptimized LLVM IR.
    • "llvm-opt": Optimized LLVM IR.
    • "object": Object code.
  • compile_options (StringSlice[StaticConstantOrigin]): Additional compiler flags and options as a string.
  • target (target): The target architecture to compile for. Defaults to current architecture.

Returns:

An Info struct containing:

  • asm: The generated code in the requested format
  • linkage_name: The mangled function name for linking
  • module_hash: A unique hash of the compiled module
  • num_captures: Number of captured variables
  • error: Any error message (empty if successful)
  • failed: Boolean indicating if compilation failed

Was this page helpful?