### Install llvmlite Source: https://github.com/numba/llvmlite/blob/main/docs/source/admin-guide/install.md After successful validation of the build, run this command to install the llvmlite library. ```shell python setup.py install ``` -------------------------------- ### Install llvmlite with Pip Source: https://github.com/numba/llvmlite/blob/main/docs/source/admin-guide/install.md Install llvmlite using pip to get binary wheels from PyPI. Development releases are not available via this method. ```bash pip install llvmlite ``` -------------------------------- ### Example Usage of New Pass Manager API Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/binding/optimization-passes.md Demonstrates how to use the new Pass Manager API in llvmlite to optimize LLVM IR. It includes initialization, module creation, target machine setup, pipeline tuning options, and running optimization passes on both modules and functions. Comments show equivalent legacy pass manager approaches. ```Python import llvmlite.binding as llvm # Initialize LLVM llvm.initialize_native_target() llvm.initialize_native_asmprinter() # Create a module with some sample IR module = llvm.parse_assembly(""" define i32 @test_function(i32 %x, i32 %y) { entry: %z = add i32 %x, %y %w = add i32 %z, 0 ; This can be optimized away %t = mul i32 %w, 1 ; This can also be optimized away ret i32 %t } define i32 @unused_function() { entry: ret i32 42 } """) print("Original IR:") print(str(module)) # Create target machine target = llvm.Target.from_default_triple() target_machine = target.create_target_machine() # NEW PASS MANAGER API: # Create pipeline tuning options with optimization settings p_to = llvm.create_pipeline_tuning_options(speed_level=2) # LEGACY EQUIVALENT: # pmb = llvm.PassManagerBuilder() # pmb.opt_level = 2 # Optionally customize the tuning options p_to.loop_vectorization = True p_to.slp_vectorization = True p_to.loop_unrolling = True # LEGACY EQUIVALENT: # pmb.loop_vectorize = True # pmb.slp_vectorize = True # pmb.disable_unroll_loops = False # Create the pass builder pass_builder = llvm.create_pass_builder(target_machine, p_to) # LEGACY EQUIVALENT: PassManagerBuilder handles this internally # Get a populated module pass manager mpm = pass_builder.getModulePassManager() # LEGACY EQUIVALENT: # mpm = llvm.ModulePassManager() # pmb.populate(mpm) # Run the optimization passes mpm.run(module, pass_builder) # LEGACY EQUIVALENT: # changed = mpm.run(module) print("\nOptimized IR:") print(str(module)) # For function-level optimizations, you can also use: fpm = pass_builder.getFunctionPassManager() # LEGACY EQUIVALENT: # fpm = llvm.FunctionPassManager(module) # pmb.populate(fpm) # fpm.initialize() for function in module.functions: fpm.run(function, pass_builder) # LEGACY EQUIVALENT: # for function in module.functions: # fpm.run(function) # fpm.finalize() # Call after all functions are processed ``` -------------------------------- ### Install llvmlite with pip and CMAKE_PREFIX_PATH Source: https://github.com/numba/llvmlite/blob/main/docs/source/admin-guide/install.md Use this command to install llvmlite from sdist when you need to specify the LLVM CMake configuration directory. Ensure CMAKE_PREFIX_PATH is set correctly. ```bash CMAKE_PREFIX_PATH=/path/to/LLVM/cmake/directory pip3 install llvmlite ``` -------------------------------- ### Legacy Pass Manager Setup Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/binding/optimization-passes.md Initializes the PassManagerBuilder and sets optimization level and loop vectorization for the legacy API. ```python # Setup pmb = PassManagerBuilder() pmb.opt_level = 2 pmb.loop_vectorize = True ``` -------------------------------- ### Compile and Execute Simple LLVM IR Function Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/binding/examples.md This example demonstrates how to compile a simple LLVM IR function for addition and execute it using ctypes. It requires initializing the native target and assembly printer, creating an execution engine, and then compiling the IR. ```python from __future__ import print_function from ctypes import CFUNCTYPE, c_double import llvmlite.binding as llvm # All these initializations are required for code generation! llvm.initialize_native_target() lvm.initialize_native_asmprinter() # yes, even this one lvml_ir = """ ; ModuleID = "examples/ir_fpadd.py" target triple = "unknown-unknown-unknown" target datalayout = "" define double @"fpadd"(double %".1", double %".2") { entry: %"res" = fadd double %".1", %".2" ret double %"res" } """ def create_execution_engine(): """ Create an ExecutionEngine suitable for JIT code generation on the host CPU. The engine is reusable for an arbitrary number of modules. """ # Create a target machine representing the host target = llvm.Target.from_default_triple() target_machine = target.create_target_machine() # And an execution engine with an empty backing module backing_mod = llvm.parse_assembly("") engine = llvm.create_mcjit_compiler(backing_mod, target_machine) return engine def compile_ir(engine, llvm_ir): """ Compile the LLVM IR string with the given engine. The compiled module object is returned. """ # Create a LLVM module object from the IR mod = llvm.parse_assembly(llvm_ir) mod.verify() # Now add the module and make sure it is ready for execution engine.add_module(mod) engine.finalize_object() engine.run_static_constructors() return mod engine = create_execution_engine() mod = compile_ir(engine, llvm_ir) # Look up the function pointer (a Python int) func_ptr = engine.get_function_address("fpadd") # Run the function via ctypes cfunc = CFUNCTYPE(c_double, c_double, c_double)(func_ptr) res = cfunc(1.0, 3.5) print("fpadd(...) =", res) ``` -------------------------------- ### Install llvmlite with Conda Source: https://github.com/numba/llvmlite/blob/main/docs/source/admin-guide/install.md Use this command to install the stable version of llvmlite from the default Conda channel. ```bash conda install llvmlite ``` -------------------------------- ### Windows LLVM Build Script Source: https://github.com/numba/llvmlite/blob/main/docs/source/admin-guide/install.md Sets the installation prefix environment variable and executes the build script for LLVM on Windows systems. ```batch set PREFIX=desired_install_location Run the [bld.bat](https://github.com/numba/llvmlite/blob/main/conda-recipes/llvmdev/bld.bat) script in the llvmdev conda recipe from the LLVM source directory. ``` -------------------------------- ### Install llvmlite using Conda Source: https://github.com/numba/llvmlite/blob/main/README.rst This command installs llvmlite using the numba channel on the Conda package manager. It is the recommended method for obtaining pre-built binaries. ```bash conda install --channel=numba llvmlite ``` -------------------------------- ### New Pass Manager Setup Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/binding/optimization-passes.md Creates pipeline tuning options and a pass builder for the new Pass Manager API, enabling loop vectorization. ```python # Setup pto = create_pipeline_tuning_options( speed_level=2) pto.loop_vectorization = True pass_builder = create_pass_builder( target_machine, pto) ``` -------------------------------- ### Generate and View Control Flow Graph (Optimized) Source: https://github.com/numba/llvmlite/blob/main/examples/notebooks/Visualize ControlFlow.ipynb Get the CFG for the optimized function and visualize it to observe the effect of optimization. ```python dot = llvm.get_function_cfg(mod.get_function(fn.name)) llvm.view_dot_graph(dot) ``` -------------------------------- ### Install LLVMdev with Conda Source: https://github.com/numba/llvmlite/blob/main/docs/source/admin-guide/install.md Installs a prebuilt LLVM binary package using conda, allowing you to skip the manual LLVM compilation step. ```bash conda install -c defaults numba/label/llvm::llvmdev=22 ``` -------------------------------- ### Install llvmlite Development Release with Conda Source: https://github.com/numba/llvmlite/blob/main/docs/source/admin-guide/install.md Install development releases of llvmlite from the Numba development channel on Anaconda Cloud. ```bash conda install -c numba/label/dev llvmlite ``` -------------------------------- ### Install llvmlite on x86 with CXXFLAGS=-fPIC Source: https://github.com/numba/llvmlite/blob/main/docs/source/admin-guide/install.md For x86 users, this command includes the CXXFLAGS=-fPIC flag to ensure proper compilation when installing llvmlite from sdist. This is necessary to resolve specific build issues on x86 architectures. ```bash CMAKE_PREFIX_PATH=/path/to/LLVM/cmake/directory CXXFLAGS=-fPIC pip3 install llvmlite ``` -------------------------------- ### Create an LLVM Module Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/examples/notebooks/Visualize ControlFlow.ipynb Initialize an empty LLVM module to hold the IR. ```python m = ir.Module() ``` -------------------------------- ### Build llvmlite C Wrapper Source: https://github.com/numba/llvmlite/blob/main/docs/source/admin-guide/install.md Run this command from the llvmlite source directory to build the C wrapper, which embeds a statically linked copy of LLVM. ```shell python setup.py build ``` -------------------------------- ### Set CMAKE_PREFIX_PATH for Non-Standard LLVM Installation Source: https://github.com/numba/llvmlite/blob/main/docs/source/admin-guide/install.md If LLVM is installed in a non-standard location, set CMAKE_PREFIX_PATH to the LLVM installation's cmake directory. This helps CMake find the LLVM configuration files. ```shell CMAKE_PREFIX_PATH=/opt/llvm/lib/cmake ``` -------------------------------- ### Optimize LLVM Module and Visualize CFG Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/examples/notebooks/Visualize ControlFlow.ipynb Materialize the LLVM module, set up the target machine and pass manager for optimization, run the optimization pipeline, and then visualize the control flow graph of the optimized function. ```python # materialize a LLVM module mod = llvm.parse_assembly(str(m)) # create optimizer llvm.initialize_native_target() target_machine = llvm.Target.from_default_triple().create_target_machine() p_to = llvm.create_pipeline_tuning_options(speed_level=3) pb = llvm.create_pass_builder(target_machine, p_to) pm = pb.getModulePassManager() # optimize pm.run(mod, pb) print(mod) dot = llvm.get_function_cfg(mod.get_function(fn.name)) llvm.view_dot_graph(dot) ``` -------------------------------- ### llvmlite.binding.initialize_native_asmprinter Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/binding/initialization-finalization.md Initializes the native assembly printer. ```APIDOC ## llvmlite.binding.initialize_native_asmprinter ### Description Initialize the native assembly printer. ### Method APICALL ### Endpoint N/A ``` -------------------------------- ### Materialize, Optimize, and Print LLVM Module Source: https://github.com/numba/llvmlite/blob/main/examples/notebooks/Visualize ControlFlow.ipynb Parse the LLVM IR into a module, set up target machine and pass manager for optimization, run optimization, and print the optimized module. ```python # materialize a LLVM module mod = llvm.parse_assembly(str(m)) # create optimizer llvm.initialize_native_target() target_machine = llvm.Target.from_default_triple().create_target_machine() pot = llvm.create_pipeline_tuning_options(speed_level=3) pb = llvm.create_pass_builder(target_machine, pot) pm = pb.getModulePassManager() # optimize pm.run(mod, pb) print(mod) ``` -------------------------------- ### Access and Set Module Name Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/binding/modules.md Get or set the identifier string for a ModuleRef. ```python from llvmlite import binding as llvm llvm.initialize() llvm.initialize_native_target() llvm.initialize_native_asmprinter() ir_module = llvm.parse_assembly("\ndef @main(){\n ret i32 0\n}\n") ir_module.name = "my_module_id" print(f"Module name: {ir_module.name}") ``` -------------------------------- ### Build llvmlite Documentation Source: https://github.com/numba/llvmlite/blob/main/docs/source/contributing.md Commands to build the HTML documentation for llvmlite using Sphinx. Ensure you are in the docs/source/ directory. ```bash make html ``` ```bash open _build/html/index.html ``` -------------------------------- ### Access and Set Module Triple Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/binding/modules.md Get or set the platform triple string for a ModuleRef. ```python from llvmlite import binding as llvm llvm.initialize() llvm.initialize_native_target() llvm.initialize_native_asmprinter() ir_module = llvm.parse_assembly("\ndef @main(){\n ret i32 0\n}\n") ir_module.triple = "x86_64-unknown-linux-gnu" print(f"Triple: {ir_module.triple}") ``` -------------------------------- ### Access Module Data Layout Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/binding/modules.md Get or set the data layout string for a ModuleRef. ```python from llvmlite import binding as llvm llvm.initialize() llvm.initialize_native_target() llvm.initialize_native_asmprinter() ir_module = llvm.parse_assembly("\ndef @main(){\n ret i32 0\n}\n") ir_module.data_layout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n80-S128" print(f"Data layout: {ir_module.data_layout}") ``` -------------------------------- ### Get Function from ModuleRef Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/binding/modules.md Retrieve a function by its name from a ModuleRef. Raises NameError if the function is not found. ```python from llvmlite import binding as llvm llvm.initialize() llvm.initialize_native_target() llvm.initialize_native_asmprinter() ir_module = llvm.parse_assembly("\ndef @add(i32 %a, i32 %b) nounwind readnone{\n %tmp = add i32 %a, %b\n ret i32 %tmp\n}\n") func_ref = ir_module.get_function("add") print(func_ref) ``` -------------------------------- ### Access Module Source File Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/binding/modules.md Get the reported source file name for a ModuleRef. This attribute cannot be set. ```python from llvmlite import binding as llvm llvm.initialize() llvm.initialize_native_target() llvm.initialize_native_asmprinter() ir_module = llvm.parse_assembly("\ndef @main(){\n ret i32 0\n}\n") # The source file is typically set during parsing or compilation # For a manually parsed string, it might be empty or default print(f"Source file: {ir_module.source_file}") ``` -------------------------------- ### Build llvmlite wheel for x86_64 Source: https://github.com/numba/llvmlite/blob/main/buildscripts/manylinux/README.md Run this script to build the llvmlite wheel from the current source tree for x86_64 Linux, specifying the Python version. ```bash ./buildscripts/manylinux/docker_run_x64.sh build_llvmlite.sh ``` -------------------------------- ### Create TargetMachine from Default Triple Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/binding/target-information.md Instantiate a TargetMachine for code generation using the default LLVM triple for the host system. ```python from llvmlite import binding target = binding.Target.from_default_triple() target_machine = target.create_target_machine() ``` -------------------------------- ### llvmlite.binding.get_host_cpu_name() Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/binding/target-information.md Gets the name of the host's CPU. This name can be used when creating a `TargetMachine` for specific CPU targeting. ```APIDOC ## get_host_cpu_name() ### Description Get the name of the host’s CPU as a string. You can use the return value with [`Target.create_target_machine()`](#llvmlite.binding.Target.create_target_machine). ### Method GET (conceptual) ### Endpoint N/A (Function call) ### Returns - **string**: The name of the host CPU. ``` -------------------------------- ### Get Struct Type from ModuleRef Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/binding/modules.md Retrieve a struct type by its name from a ModuleRef. Raises NameError if the type is not found. ```python from llvmlite import binding as llvm llvm.initialize() llvm.initialize_native_target() llvm.initialize_native_asmprinter() ir_module = llvm.parse_assembly("%MyStruct = type { i32, float }\ndef @process_struct(%MyStruct* %s){\n ret void\n}\n") struct_type_ref = ir_module.get_struct_type("%MyStruct") print(struct_type_ref) ``` -------------------------------- ### Set up Loop Control Flow Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/examples/notebooks/Visualize ControlFlow.ipynb Define basic blocks for loop header, body, and end. Branch to the loop header and set up the loop condition (ct < N). ```python loophead = fn.append_basic_block('loop.header') loopbody = fn.append_basic_block('loop.body') loopend = fn.append_basic_block('loop.end') builder.branch(loophead) builder.position_at_end(loophead) # loop if ct < arg0 arg0 = fn.args[0] pred = builder.icmp_signed('<', builder.load(ct), arg0) builder.cbranch(pred, loopbody, loopend) print(fn) ``` -------------------------------- ### Get Global Variable from ModuleRef Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/binding/modules.md Retrieve a global variable by its name from a ModuleRef. Raises NameError if the variable is not found. ```python from llvmlite import binding as llvm llvm.initialize() llvm.initialize_native_target() llvm.initialize_native_asmprinter() ir_module = llvm.parse_assembly("@[.str] = private unnamed_addr constant [13 x i8] c\"Hello, world!\", align 1\ndef @main(){\n ret i32 0\n}\n") global_var_ref = ir_module.get_global_variable("@.str") print(global_var_ref) ``` -------------------------------- ### llvmlite.binding.initialize_all_targets Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/binding/initialization-finalization.md Initializes all targets. This must be called before targets can be looked up via the Target class. ```APIDOC ## llvmlite.binding.initialize_all_targets ### Description Initialize all targets. Must be called before targets can be looked up via the [`Target`](target-information.md#llvmlite.binding.Target) class. ### Method APICALL ### Endpoint N/A ``` -------------------------------- ### IRBuilder.landingpad Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/ir/ir-builder.md Describes which exceptions a basic block can handle. Supports catch and filter clauses, and cleanup behavior. ```APIDOC ## IRBuilder.landingpad ### Description Defines a landing pad for exception handling. It specifies the return type, personality function, and whether it's a cleanup landing pad. Clauses (catch or filter) can be added using `add_clause()`. ### Method Signature `landingpad(typ, personality, name='', cleanup=False)` ### Parameters * **typ** - The return type of the landing pad (a structure with 2 pointer-sized fields). * **personality** - An exception personality function. * **name** (Optional) - The name of the landing pad instruction. * **cleanup** (Optional) - Boolean indicating if control should always transfer here for cleanup. ``` -------------------------------- ### Build llvmlite wheel for aarch64 Source: https://github.com/numba/llvmlite/blob/main/buildscripts/manylinux/README.md Run this script to build the llvmlite wheel from the current source tree for aarch64 Linux, specifying the Python version. ```bash ./buildscripts/manylinux/docker_run_aarch64.sh build_llvmlite.sh ``` -------------------------------- ### Optimizing for Size with Function Attributes Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/binding/optimization-passes.md Demonstrates how to request size optimization for specific functions using 'optsize' or 'minsize' attributes. This approach inhibits size-increasing transformations for the targeted functions. ```Python fn = module.get_function("my_func") fn.add_function_attribute("optsize") # or "minsize" for -Oz p_to = create_pipeline_tuning_options(speed_level=2) pass_builder = create_pass_builder(target_machine, p_to) pass_builder.getModulePassManager().run(module, pass_builder) ``` -------------------------------- ### Conditional Branch with if_then Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/ir/ir-builder.md Use `if_then` to create a basic block conditioned on a predicate. The builder is positioned at the end of the conditional block upon entry and at the start of the continuation block upon exit. ```python with builder.if_then(pred): # emit instructions for when the predicate is true # emit instructions following the if-then block ``` -------------------------------- ### Create IR Builder Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/examples/notebooks/Visualize ControlFlow.ipynb Create an IR builder positioned at the entry basic block of the function. ```python builder = ir.IRBuilder(fn.append_basic_block('entry')) ``` -------------------------------- ### Visualize Unoptimized CFG Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/examples/notebooks/Visualize ControlFlow.ipynb Generate and view the control flow graph of the function before optimization using llvmlite.binding. ```python from llvmlite import binding as llvm dot = llvm.get_function_cfg(fn) llvm.view_dot_graph(dot) ``` -------------------------------- ### Initialize Variables Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/examples/notebooks/Visualize ControlFlow.ipynb Allocate memory for 'out' and 'ct' variables and initialize them to 0. ```python out = builder.alloca(ir.IntType(32), name='out') ct = builder.alloca(ir.IntType(32), name='ct') builder.store(out.type.pointee(0), out) builder.store(ct.type.pointee(0), ct) print(fn) ``` -------------------------------- ### Run llvmlite Test Suite Source: https://github.com/numba/llvmlite/blob/main/docs/source/admin-guide/install.md Validate your build by running the test suite using either the runtests.py script or the llvmlite.tests module. ```shell python runtests.py ``` ```shell python -m llvmlite.tests ``` -------------------------------- ### Import llvmlite IR module Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/examples/notebooks/Visualize ControlFlow.ipynb Import the necessary module for creating LLVM IR. ```python from llvmlite import ir ``` -------------------------------- ### ObjectFileRef Class Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/binding/object-file.md Provides methods to create and examine LLVM object files. It can be used to load symbols into the JIT. ```APIDOC ## class llvmlite.binding.ObjectFileRef A wrapper around LLVM object file. The following methods and properties are available: ### from_data(data) Create an instance of ObjectFileRef from the provided binary data. ### from_path(path) Create an instance of ObjectFileRef from the supplied filesystem path. Raises IOError if the path does not exist. ### sections Return an iterator to the sections objects consisting of the instance of [`SectionIteratorRef`](#llvmlite.binding.SectionIteratorRef) ``` -------------------------------- ### IRBuilder.resume Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/ir/ir-builder.md Resumes an exception that was caught by a landing pad. Used when a landing pad only performs cleanup. ```APIDOC ## IRBuilder.resume ### Description Resumes an exception previously caught by a landing pad. This is typically used when the landing pad's primary purpose was cleanup, and the exception needs to be re-thrown. ### Method Signature `resume(landingpad)` ### Parameters * **landingpad** - The landing pad instruction that caught the exception. ``` -------------------------------- ### TargetMachine Class Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/binding/target-information.md Holds all the settings necessary for proper code generation, including target information and compiler options. Instantiate using Target.create_target_machine(). ```APIDOC ## TargetMachine Class Holds all the settings necessary for proper code generation, including target information and compiler options. Instantiate using [`Target.create_target_machine()`](#llvmlite.binding.Target.create_target_machine). ### Methods * #### add_analysis_passes(pm) Register analysis passes for this target machine with the [`PassManager`](optimization-passes.md#llvmlite.binding.PassManager) instance *pm*. * #### emit_object(module) Represent the compiled *module*—a [`ModuleRef`](modules.md#llvmlite.binding.ModuleRef) instance—as a code object that is suitable for use with the platform’s linker. Returns a bytestring. * #### set_asm_verbosity(is_verbose) Set whether this target machine emits assembly with human-readable comments, such as those describing control flow or debug information. * #### emit_assembly(module) Return a string representing the compiled *module*’s native assembler. You must first call [`initialize_native_asmprinter()`](initialization-finalization.md#llvmlite.binding.initialize_native_asmprinter). ### Attributes * #### target_data The [`TargetData`](#llvmlite.binding.TargetData) associated with this target machine. ``` -------------------------------- ### Setting Instruction Metadata Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/ir/values.md Add instruction-specific metadata. The name is a string and the node is an MDValue. ```python instr.set_metadata(name, node) ``` -------------------------------- ### llvmlite.binding.initialize_native_target Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/binding/initialization-finalization.md Initializes the native host target. This must be called once before any code generation. ```APIDOC ## llvmlite.binding.initialize_native_target ### Description Initialize the native—host—target. Must be called once before doing any code generation. ### Method APICALL ### Endpoint N/A ``` -------------------------------- ### PipelineTuningOptions Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/binding/optimization-passes.md Manages options for tuning the optimization pipeline, such as enabling specific vectorization or inlining strategies. ```APIDOC ## llvmlite.binding.create_pipeline_tuning_options(speed_level=2) Create a [`PipelineTuningOptions`](#llvmlite.binding.PipelineTuningOptions) instance. ### *class* llvmlite.binding.PipelineTuningOptions(speed_level=2) Creates a new PipelineTuningOptions object. The following writable attributes are available, whose default values depend on the initial setting of the optimization level: * #### loop_interleaving Enable loop interleaving. * #### loop_vectorization Enable loop vectorization. * #### slp_vectorization Enable SLP vectorization, which uses a different algorithm to loop vectorization. Both may be enabled at the same time. * #### loop_unrolling Enable loop unrolling. * #### speed_level The level of optimization for speed, as an integer between 0 and 3. * #### inlining_threshold The integer threshold for inlining one function into another. The higher the number, the more likely that inlining will occur. This attribute is write-only. ``` -------------------------------- ### Linux/macOS LLVM Build Script Source: https://github.com/numba/llvmlite/blob/main/docs/source/admin-guide/install.md Sets environment variables and executes the build script for LLVM on Linux or macOS systems. ```bash export PREFIX=desired_install_location CPU_COUNT=N Run the [build.sh](https://github.com/numba/llvmlite/blob/main/conda-recipes/llvmdev/build.sh) script in the llvmdev conda recipe from the LLVM source directory. ``` -------------------------------- ### Factory Functions for Module Creation Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/binding/modules.md Functions to create ModuleRef instances by parsing LLVM IR assembly or bitcode. ```APIDOC ## llvmlite.binding.parse_assembly(llvmir, context=None) ### Description Parse the given *llvmir*, a string containing some LLVM IR code. If parsing is successful, a new [`ModuleRef`](#llvmlite.binding.ModuleRef) instance is returned. ### Parameters * **llvmir** (string) - The LLVM IR code as a string. * **context** (LLVMContextRef, optional) - An instance of [`LLVMContextRef`](context.md#LLVMContextRef). Defaults to the global context. ### Example You can obtain *llvmir* by calling `str()` on an [`llvmlite.ir.Module`](../ir/modules.md#llvmlite.ir.Module) object. ``` ```APIDOC ## llvmlite.binding.parse_bitcode(bitcode, context=None) ### Description Parse the given *bitcode*, a bytestring containing the LLVM bitcode of a module. If parsing is successful, a new [`ModuleRef`](#llvmlite.binding.ModuleRef) instance is returned. ### Parameters * **bitcode** (bytes) - The LLVM bitcode as a bytestring. * **context** (LLVMContextRef, optional) - An instance of [`LLVMContextRef`](context.md#LLVMContextRef). Defaults to the global context. ### Example You can obtain the *bitcode* by calling [`ModuleRef.as_bitcode()`](#llvmlite.binding.ModuleRef.as_bitcode). ``` -------------------------------- ### llvmlite.binding.create_target_data(data_layout) Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/binding/target-information.md Creates a `TargetData` object representing the specified data layout string. ```APIDOC ## create_target_data(data_layout) ### Description Create a [`TargetData`](#llvmlite.binding.TargetData) representing the given *data_layout* string. ### Method POST (conceptual) ### Endpoint N/A (Function call) ### Parameters #### Request Body - **data_layout** (string) - Required - The data layout string defining memory addressing and data type sizes. ### Returns - **TargetData**: An object representing the specified data layout. ``` -------------------------------- ### IRBuilder.if_else Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/ir/ir-builder.md Sets up two basic blocks for conditional execution based on a predicate. It yields a pair of context managers for the 'then' and 'otherwise' blocks, and positions the builder on a new continuation block after both are exited. ```APIDOC ## IRBuilder.if_else(pred, likely=None) ### Description Set up 2 basic blocks whose execution is conditioned on predicate *pred*, a value of type `IntType(1)`. *likely* has the same meaning as in `if_then()`. A pair of context managers is yielded. Each of them acts as an [`if_then()`](#llvmlite.ir.IRBuilder.if_then) context manager—the first for the block to be executed if pred is `True` and the second for the block to be executed if pred is `False`. When the context manager is exited, the builder is positioned on a new continuation block that both conditional blocks jump into. ### Usage Example ```Python with builder.if_else(pred) as (then, otherwise): with then: # emit instructions for when the predicate is true with otherwise: # emit instructions for when the predicate is false # emit instructions following the if-else block ``` ``` -------------------------------- ### llvmlite.binding.initialize_all_asmprinters Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/binding/initialization-finalization.md Initializes all code generators. This must be called before generating any assembly or machine code. ```APIDOC ## llvmlite.binding.initialize_all_asmprinters ### Description Initialize all code generators. Must be called before generating any assembly or machine code via the [`TargetMachine.emit_object()`](target-information.md#llvmlite.binding.TargetMachine.emit_object) and [`TargetMachine.emit_assembly()`](target-information.md#llvmlite.binding.TargetMachine.emit_assembly) methods. ### Method APICALL ### Endpoint N/A ``` -------------------------------- ### Update load and gep instructions for opaque pointers Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/deprecation.md Modify calls to `ir.load`, `ir.load_atomic`, and `ir.gep` instructions to pass in pointer types when working with opaque pointers. ```python ptr = builder.gep(func.args[0], [index]) value = builder.load(ptr) ``` ```python ptr = builder.gep(func.args[0], [index], source_etype=ll.IntType(32)) value = builder.load(ptr, typ=ll.IntType(32)) ``` -------------------------------- ### llvmlite.binding.llvm_version_info Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/binding/initialization-finalization.md A tuple representing the LLVM version number. ```APIDOC ## llvmlite.binding.llvm_version_info ### Description A 3-integer tuple representing the LLVM version number. EXAMPLE: `(3, 7, 1)` Since LLVM is statically linked into the `llvmlite` DLL, this is guaranteed to represent the true LLVM version in use. ### Type Tuple[int, int, int] ### Value Read-only ``` -------------------------------- ### ExecutionEngine Class Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/binding/execution-engine.md Provides a wrapper around an LLVM execution engine, offering methods for module management, code generation, and symbol retrieval. ```APIDOC ## class llvmlite.binding.ExecutionEngine ### Description A wrapper around an LLVM execution engine. The following methods and properties are available: ### Methods #### add_module(module) Add the *module*—a [`ModuleRef`](modules.md#llvmlite.binding.ModuleRef) instance—for code generation. When this method is called, ownership of the module is transferred to the execution engine. #### finalize_object() Make sure all modules owned by the execution engine are fully processed and usable for execution. #### get_function_address(name) Return the address of the function *name* as an integer. It’s a fatal error in LLVM if the symbol of *name* doesn’t exist. #### get_global_value_address(name) Return the address of the global value *name* as an integer. It’s a fatal error in LLVM if the symbol of *name* doesn’t exist. #### remove_module(module) Remove the *module*—a [`ModuleRef`](modules.md#llvmlite.binding.ModuleRef) instance—from the modules owned by the execution engine. This allows releasing the resources owned by the module without destroying the execution engine. #### add_object_file(object_file) Add the symbols from the specified object file to the execution engine. * *object_file* str or [`ObjectFileRef`](object-file.md#llvmlite.binding.ObjectFileRef): a path to the object file : or a object file instance. Object file instance is not usable after this call. #### set_object_cache(notify_func=None, getbuffer_func=None) Set the object cache callbacks for this engine. * *notify_func*, if given, is called whenever the engine has finished compiling a module. It is passed the `(module, buffer)` arguments: * *module* is a [`ModuleRef`](modules.md#llvmlite.binding.ModuleRef) instance. * *buffer* is a bytes object of the code generated for the module. The return value is ignored. * *getbuffer_func*, if given, is called before the engine starts compiling a module. It is passed an argument, *module*, a [`ModuleRef`](modules.md#llvmlite.binding.ModuleRef) instance of the module being compiled. * It can return `None`, in which case the module is compiled normally. * It can return a bytes object of native code for the module, which bypasses compilation entirely. ### Properties #### target_data The [`TargetData`](target-information.md#llvmlite.binding.TargetData) used by the execution engine. ``` -------------------------------- ### llvmlite.binding.initialize_native_asmparser Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/binding/initialization-finalization.md Initializes the native assembly parser. This must be called for inline assembly to work. ```APIDOC ## llvmlite.binding.initialize_native_asmparser ### Description Initialize the native assembly parser. Must be called for inline assembly to work. ### Method APICALL ### Endpoint N/A ``` -------------------------------- ### llvmlite.binding.create_mcjit_compiler Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/binding/execution-engine.md Creates an MCJIT-powered execution engine from a given LLVM module and target machine. ```APIDOC ## llvmlite.binding.create_mcjit_compiler(module, target_machine, use_lmm=None) ### Description Create a MCJIT-powered engine from the given *module* and *target_machine*. *lmm* controls whether the llvmlite memory manager is used. If not supplied, the default choice for the platform will be used (`True` on 64-bit ARM systems, `False` otherwise). * *module* does not need to contain any code. ### Returns A [`ExecutionEngine`](#llvmlite.binding.ExecutionEngine) instance. ``` -------------------------------- ### IRBuilder Positioning Methods Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/ir/ir-builder.md Methods for controlling the insertion point of new instructions within the LLVM IR. ```APIDOC ## Positioning The following [`IRBuilder`](#llvmlite.ir.IRBuilder) methods help you move the current instruction pointer: * #### IRBuilder.position_before(instruction) Position immediately before the given *instruction*. The current block is also changed to the instruction’s basic block. * #### IRBuilder.position_after(instruction) Position immediately after the given *instruction*. The current block is also changed to the instruction’s basic block. * #### IRBuilder.position_at_start(block) Position at the start of the basic *block*. * #### IRBuilder.position_at_end(block) Position at the end of the basic *block*. ``` -------------------------------- ### Comparisons Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/ir/ir-builder.md Methods for performing signed, unsigned, and floating-point comparisons. ```APIDOC ## IRBuilder.icmp_signed(cmpop, lhs, rhs, name='') ### Description Performs a signed integer comparison between two values. ### Parameters * `cmpop` (string) - The comparison operator. Can be one of `<`, `<=`, `==`, `!=`, `>=`, or `>`. * `lhs` - The left-hand side operand. * `rhs` - The right-hand side operand. * `name` (string, optional) - The name for the generated IR instruction. ``` ```APIDOC ## IRBuilder.icmp_unsigned(cmpop, lhs, rhs, name='') ### Description Performs an unsigned integer comparison between two values. ### Parameters * `cmpop` (string) - The comparison operator. Can be one of `<`, `<=`, `==`, `!=`, `>=`, or `>`. * `lhs` - The left-hand side operand. * `rhs` - The right-hand side operand. * `name` (string, optional) - The name for the generated IR instruction. ``` ```APIDOC ## IRBuilder.fcmp_ordered(cmpop, lhs, rhs, name='', flags=[]) ### Description Performs a floating-point ordered comparison between two values. ### Parameters * `cmpop` (string) - The comparison operator. Can be one of `<`, `<=`, `==`, `!=`, `>=`, `>`, `ord`, or `uno`. * `lhs` - The left-hand side operand. * `rhs` - The right-hand side operand. * `name` (string, optional) - The name for the generated IR instruction. * `flags` (list, optional) - A list of flags, which can include `nnan`, `ninf`, `nsz`, `arcp`, and `fast`. ``` ```APIDOC ## IRBuilder.fcmp_unordered(cmpop, lhs, rhs, name='', flags=[]) ### Description Performs a floating-point unordered comparison between two values. ### Parameters * `cmpop` (string) - The comparison operator. Can be one of `<`, `<=`, `==`, `!=`, `>=`, `>`, `ord`, or `uno`. * `lhs` - The left-hand side operand. * `rhs` - The right-hand side operand. * `name` (string, optional) - The name for the generated IR instruction. * `flags` (list, optional) - A list of flags, which can include `nnan`, `ninf`, `nsz`, `arcp`, and `fast`. ``` -------------------------------- ### llvmlite.binding.check_jit_execution Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/binding/execution-engine.md Ensures the system allows creation of executable memory ranges for JIT-compiled code, raising an exception if security mechanisms prevent it. ```APIDOC ## llvmlite.binding.check_jit_execution() ### Description Ensure that the system allows creation of executable memory ranges for JIT-compiled code. If some security mechanism such as SELinux prevents it, an exception is raised. Otherwise the function returns silently. Calling this function early can help diagnose system configuration issues, instead of letting JIT-compiled functions crash mysteriously. ``` -------------------------------- ### Temporarily Switch to a Basic Block Source: https://github.com/numba/llvmlite/blob/main/docs/source/user-guide/ir/ir-builder.md Use the goto_block context manager to temporarily position the builder at the end of a specified basic block. The builder returns to its previous position upon exiting the context. ```python new_block = builder.append_basic_block('foo') with builder.goto_block(new_block): # Now the builder is at the end of *new_block* # ... add instructions # Now the builder has returned to its previous position ```