Skip to main content

Mojo function

compile_info

compile_info[func_type: __TypeOfAllTypes, //, func: func_type, /, *, emission_kind: StringSlice[StaticConstantOrigin] = "asm", target: __mlir_type.!kgen.target = _current_target(), compile_options: StringSlice[StaticConstantOrigin] = CompilationTarget.default_compile_options[target]()]() -> CompiledFunctionInfo[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

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 (__TypeOfAllTypes): 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): The desired output format. Valid options are:
    • "asm": Assembly code (default).
    • "llvm": Unoptimized LLVM IR.
    • "llvm-opt": Optimized LLVM IR.
    • "object": Object code.
  • target (__mlir_type.!kgen.target``): The target architecture to compile for. Defaults to current architecture.
  • compile_options (StringSlice): Additional compiler flags and options as a string.

Returns:

CompiledFunctionInfo: A CompiledFunctionInfo 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?