llvm / llvm/circt

[BUG] Wrong propagation of reset signal and clock

Open
#9,069 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Calyx
Dominant language
C++
Stars
2.2k
Forks
524
Avg merge
3d 2h
Merged PRs (30d)
46

Description

Hi there,
I have found a bug in the code generation with the Calyx backend in the latest version of CIRCT. In the generated Verilog the reset signal are not used directly but they are renamed with a couple of wires:

  wire        ret_arg0_reg_clk = 1'bz;	// adder.mlir:1:1
  wire        ret_arg0_reg_reset = 1'bz;

However, the assignment is not correct and this causes the control FSM to hang. The bug is fixed by fixing the assignment:

  wire        ret_arg0_reg_clk = clk;	// adder.mlir:1:1
  wire        ret_arg0_reg_reset = reset;

To reproduce the issue use the following adder.mlir:

func.func @adder(%a: i32, %b: i32) -> i32 {
  %sum = arith.addi %a, %b : i32
  return %sum : i32
}

And the command hlstool --calyx-hw adder.mlir --split-verilog -o=out. The issue is in out/adder.sv.
I can also provide a Verilator testbench if needed.

Edit:
I have managed to isolate the faulty pass. The problem happens at the canonicalization step at https://github.com/llvm/circt/blob/407c3d2de3a1f2814d317108e979115ddc9529f5/tools/hlstool/hlstool.cpp#L454

You can see this that here the clock and reset signals are passed to the FSM from the top module.

hlstool --calyx-hw adder.mlir --output-level=post-compile --ir

Whilst here two Z-constants are created:

hlstool --calyx-hw adder.mlir --output-level=rtl --ir

You can isolate the problematic pass with:

hlstool --calyx-hw adder.mlir --output-level=post-compile --ir | circt-opt --lower-calyx-to-hw --canonicalize

(Simply remove --canonicalize to see how the clock and reset signals are still used before that).

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with adder.mlir using hlstool --calyx-hw adder.mlir --output-level=post-compile --ir, then compare it with the rtl output. Inspect the canonicalization step in tools/hlstool/hlstool.cpp around line 454 and isolate it with circt-opt --lower-calyx-to-hw --canonicalize. Done means the generated out/adder.sv propagates clk and reset instead of creating z-valued wires.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.