chisel icon indicating copy to clipboard operation
chisel copied to clipboard

Adding custom information to Chisel for CIRCT Passes

Open liuyic00 opened this issue 1 month ago • 2 comments

Hi, I'm trying to add some optimization information in Chisel, which will eventually become the attr-dict for operators in CIRCT IR. This way, a custom pass can use this information to transform the circuit.

I found that firrtl.annotations.SingleTargetAnnotation can achieve this if I add a LowerAnnotations function in CIRCT. However, it seems that SingleTargetAnnotation is deprecated, with the message: "All APIs in the firrtl package are deprecated."

Is there a replacement for firrtl.annotations? Or a way to directly add the attribute into the CIRCT IR?

liuyic00 avatar Nov 24 '25 12:11 liuyic00

You can add arbitrary annotation and run firtool with --disable-annotation-unknown. In that case annotation remains as firrtl.annotations. If you want to convert to MLIR attribute you might want to use a (CIRCT) plugin pass to lower annotations to attribute.

Also you can add custom annotation handler through registerAnnotationRecord . By doing this you don't need --disable-annotation-unknown since LowerAnnotation can interpret the annotation.

/// Pass plugin registration mechanism.
/// Necessary symbol to register the pass plugin.
extern "C" LLVM_ATTRIBUTE_WEAK PassPluginLibraryInfo mlirGetPassPluginInfo() {
  return {MLIR_PLUGIN_API_VERSION, "CustomPassses", LLVM_VERSION_STRING,
          []() {
              registerAnnotationRecord(...);
              registerCustomPasses(...);
          }};
}

uenoku avatar Nov 24 '25 17:11 uenoku

hi @uenoku thanks for your reply, I tried to add a custom annotation to the Adder module, but it seems that the annotation is removed:

    val firrtlString =
      ChiselStage.emitFIRRTLDialect(
        gen = new Adder,
        firtoolOpts = Array(
          "-disable-annotation-unknown",
        )
      )

In the Adder module:

class Adder extends Module {
    val io = IO(new Bundle {
      val a = Input(UInt(8.W))
      val b = Input(UInt(8.W))
      val y = Output(UInt(8.W))
    })
    val r = RegInit(0.U(8.W))
    // custom annotation will be removed if not specified in firtoolOpts
    addAttribute(r, "my_custom_annotation")
    io.y := io.a + io.b + r
  }

doofin avatar Dec 02 '25 14:12 doofin