Circuit IR Compilers and Tools

Using the Python Bindings

If you are mainly interested in using CIRCT from Python scripts, you need to compile both LLVM/MLIR and CIRCT with Python bindings enabled. To do this, follow the steps in “Setting this up” , adding the following options:

# in the LLVM/MLIR build directory

# in the CIRCT build directory

Afterwards, use ninja check-circt-integration to ensure that the bindings work. (This will now additionally spin up a couple of Python scripts to test that they are accessible.)

Without Installation 

If you want to try the bindings fresh from the compiler without installation, you need to ensure Python can find the generated modules:

export PYTHONPATH="$PWD/llvm/build/python:$PWD/build/python:$PYTHONPATH"

With Installation 

If you are installing CIRCT through ninja install anyway, the libraries and Python modules will be installed into the correct location automatically.

Trying things out 

Now you are able to use the CIRCT dialects and infrastructure from a Python interpreter and script:

import circt
import mlir
from import *
from circt.dialects import hw, comb

with Context() as ctx, Location.unknown():
  i42 = IntegerType.get_signless(42)
  m = Module.create()
  with InsertionPoint(m.body):
    @hw.HWModuleOp.from_py_func(i42, i42)
    def magic(a, b):
      return comb.XorOp(i42, [a, b]).result

Running this script through python3 should print the following MLIR:

module  {
  hw.module @magic(%a: i42, %b: i42) -> (%result0: i42) {
    %0 = comb.xor %a, %b : i42
    hw.output %0 : i42