🛡️text-to-image model API

reflection - LLVM API Documentation


reflection is a project that leverages the LLVM framework to provide advanced features for code analysis, transformation, and reflection. This documentation covers the API provided by the reflection project, which allows developers to interact with LLVM's intermediate representation (IR) and perform various transformations and analyses.


To install the reflection project, you need to have LLVM installed on your system. You can install LLVM using your package manager or by compiling it from source. Once LLVM is installed, you can install reflection using the following command:

pip install reflection-llvm

Basic Concepts

Before diving into the API, it's essential to understand some basic LLVM concepts:

  • Context: Holds LLVM global data, used to manage and isolate the state of different compilations.

  • Module: Represents a single unit of code containing functions, global variables, and symbol table information.

  • Function: Represents a function in the IR.

  • Type: Represents the type of variables and functions in the IR.

  • Builder: Provides methods to construct LLVM instructions.

  • PassManager: Manages optimization and analysis passes over the LLVM IR.

API Reference


class reflection.Context

The Context class encapsulates the global state used by LLVM.

  • Methods:

    • __init__(): Initialize a new context.

    • dispose(): Dispose of the context and free associated resources.


class reflection.Module

The Module class represents an LLVM module, which is a collection of functions and global variables.

  • Methods:

    • __init__(name: str, context: Context): Create a new module with the given name and context.

    • get_function(name: str) -> Function: Retrieve a function by its name.

    • add_function(func: Function): Add a function to the module.

    • print(): Print the IR of the module.


class reflection.Function

The Function class represents an LLVM function.

  • Methods:

    • __init__(name: str, return_type: Type, param_types: List[Type], module: Module): Create a new function with the given signature.

    • add_basic_block(name: str): Add a new basic block to the function.

    • get_basic_blocks() -> List[BasicBlock]: Get the list of basic blocks in the function.


class reflection.Type

The Type class represents an LLVM type.

  • Methods:

    • get_int_type(bits: int, context: Context) -> Type: Get an integer type with the specified number of bits.

    • get_float_type(context: Context) -> Type: Get a floating-point type.

    • get_void_type(context: Context) -> Type: Get a void type.


class reflection.Builder

The Builder class provides methods to construct LLVM instructions.

  • Methods:

    • __init__(context: Context): Create a new instruction builder.

    • set_insert_point(basic_block: BasicBlock): Set the insertion point to the end of the specified basic block.

    • create_add(lhs: Value, rhs: Value, name: str) -> Value: Create an addition instruction.

    • create_sub(lhs: Value, rhs: Value, name: str) -> Value: Create a subtraction instruction.


class reflection.PassManager

The PassManager class manages a sequence of passes over the LLVM IR.

  • Methods:

    • __init__(module: Module): Create a new pass manager for the specified module.

    • add_pass(pass: Pass): Add a pass to the manager.

    • run(): Run all the passes on the module.


Creating a Module and Function

from reflection import Context, Module, Type, Function, Builder

# Create a new context
context = Context()

# Create a new module
module = Module("my_module", context)

# Create an integer type
int32_type = Type.get_int_type(32, context)

# Create a function type (int32 -> int32)
func_type = Type.get_function_type(int32_type, [int32_type])

# Create a new function
function = Function("my_function", func_type, module)

# Create a basic block
entry = function.add_basic_block("entry")

# Create a builder and set the insertion point to the entry block
builder = Builder(context)

# Create an addition instruction
lhs = ... # Assume lhs is a Value representing a parameter or a variable
rhs = ... # Assume rhs is a Value representing a parameter or a variable
result = builder.create_add(lhs, rhs, "addtmp")

# Print the IR of the module

Running Passes

python from reflection import PassManager

# Create a pass manager for the module
pass_manager = PassManager(module)

# Add some passes

# Run the passes


Contributions to the reflection project are welcome. Please follow the standard GitHub workflow for contributing:

  1. Fork the repository.

  2. Create a new branch for your feature or bugfix.

  3. Commit your changes and push them to your branch.

  4. Create a pull request.

Ensure your code follows the project's coding standards and includes appropriate tests.


The reflection project is licensed under the MIT License. See the LICENSE file for more details.

This documentation provides an overview of the reflection API and how to use it to interact with LLVM. For more detailed information and advanced usage, please refer to the source code and additional documentation in the project's repository.

Last updated