CoreIR Python Bindings

2.0.156 · active · verified Fri Apr 17

Python bindings for CoreIR, an intermediate representation for hardware. This library provides a programmatic interface to build, manipulate, and analyze hardware circuits using Python. Currently at version 2.0.156, it maintains an active development pace with frequent updates, primarily leveraging a Rust-based CoreIR backend.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a CoreIR context, define a module's interface, create a module definition, add an instance of a built-in generator (an adder), and connect the ports, resulting in a simple 16-bit adder module.

import coreir

# Create a CoreIR context
context = coreir.Context()

# Define a simple module type (e.g., a 16-bit input and output)
coreir_type = context.Record({
    "in": context.BitIn().Arr(16),
    "out": context.Bit().Arr(16)
})

# Create a new module named 'my_adder' with the defined type
module = context.Gettop().new_module("my_adder", coreir_type)

# Create a definition for the module, where instances and connections will be placed
module_def = module.new_definition()

# Get the 'add' generator and create an instance of a 16-bit adder
add16 = context.get_generator("coreir.add").get_instances({"width": 16})[0]
module_def.add_instance("add_inst", add16)

# Connect the module's input port to the adder instance's input
module_def.connect(module_def.get_port("in"), "add_inst.in")

# Connect the adder instance's output to the module's output port
module_def.connect("add_inst.out", module_def.get_port("out"))

# Set the definition for the module
module.set_definition(module_def)

# Verify the module's type system (optional, but good for validation)
module.type.verify()

print(f"Successfully created CoreIR module: {module.name}")
# print(f"Module definition:
{module_def.str()}") # Uncomment to see the generated IR

view raw JSON →