Skip to content

Verilog reader imports NOR and XNOR assignments as constant zero #712

Closed

Description

@vera-rubin

Describe the bug

The Verilog reader accepts assign ynor = ~(a | b); and assign yxnor = ~(a ^ b); but imports both outputs as constant zero. The mockturtle adapter lacks the on_nor and on_xnor callbacks that lorina correctly dispatches.

To Reproduce

Tested commit: lsils/mockturtle@47d1e70fdf775e1a295016c3c17a1ad206db24c0, including its bundled lorina. Upstream master still resolves to this commit on 2026-10-11. The execution results below are from the independent verification on that date.

Run this in Bash on Linux with Git, CMake, a C++17 compiler, and Icarus Verilog installed. It creates the complete C++ driver, CMake file, three-gate circuit, and reference testbench.

export JOB_WORK="$(mktemp -d)"
mkdir "$JOB_WORK/repro-src"
git clone https://github.com/lsils/mockturtle.git "$JOB_WORK/mockturtle"
git -C "$JOB_WORK/mockturtle" checkout --detach 47d1e70fdf775e1a295016c3c17a1ad206db24c0
cat > "$JOB_WORK/repro-src/CMakeLists.txt" <<'EOF'
cmake_minimum_required(VERSION 3.16)
project(mockturtle_reproducer LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(NOT MOCKTURTLE_DIR)
  message(FATAL_ERROR "Pass -DMOCKTURTLE_DIR=/path/to/pinned/mockturtle")
endif()
execute_process(COMMAND git -C "${MOCKTURTLE_DIR}" rev-parse HEAD OUTPUT_VARIABLE revision OUTPUT_STRIP_TRAILING_WHITESPACE)
if(NOT revision STREQUAL "47d1e70fdf775e1a295016c3c17a1ad206db24c0")
  message(FATAL_ERROR "Wrong mockturtle commit: ${revision}")
endif()
set(MOCKTURTLE_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(MOCKTURTLE_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(MOCKTURTLE_BUILD_EXPERIMENTS OFF CACHE BOOL "" FORCE)
add_subdirectory("${MOCKTURTLE_DIR}" upstream EXCLUDE_FROM_ALL)
add_executable(repro repro.cpp)
target_link_libraries(repro PRIVATE mockturtle)
add_executable(repro_ndebug repro.cpp)
target_compile_definitions(repro_ndebug PRIVATE NDEBUG)
target_link_libraries(repro_ndebug PRIVATE mockturtle)
EOF

cat > "$JOB_WORK/repro-src/repro.cpp" <<'EOF'
#include <mockturtle/io/verilog_reader.hpp>
#include <mockturtle/networks/aig.hpp>
#include <functional>
#include <iostream>
#include <string>
#include <vector>

// Evaluate AIG structure directly, without mockturtle compute/simulate.
std::vector<bool> evaluate(mockturtle::aig_network const& n, unsigned m) {
  std::vector<int> values(n.size(), -1);
  n.foreach_pi([&](auto v, unsigned i) { values[v] = (m >> i) & 1; });
  std::function<bool(mockturtle::aig_network::node)> node = [&](auto v) -> bool {
    if (values[v] >= 0) return values[v];
    if (n.is_constant(v)) return values[v] = n.constant_value(v);
    bool result = true;
    n.foreach_fanin(v, [&](auto f) { result &= node(n.get_node(f)) ^ n.is_complemented(f); });
    return values[v] = result;
  };
  std::vector<bool> outputs;
  n.foreach_po([&](auto f) { outputs.push_back(node(n.get_node(f)) ^ n.is_complemented(f)); });
  return outputs;
}

int main(int argc, char** argv) {
  if (argc != 2) return 2;
  mockturtle::aig_network n;
  auto rc = lorina::read_verilog(std::string(argv[1]), mockturtle::verilog_reader(n));
  std::cout << "parser success=" << (rc == lorina::return_code::success)
            << " inputs=" << n.num_pis() << " outputs=" << n.num_pos() << '\n';
  if (rc != lorina::return_code::success || n.num_pis() != 2 || n.num_pos() != 3) return 2;
  unsigned failures = 0;
  for (unsigned m = 0; m < 4; ++m) {
    bool a = m & 1, b = m & 2;
    std::vector<bool> expected{!(a || b), a == b, !(a && b)};
    auto got = evaluate(n, m);
    for (unsigned o = 0; o < expected.size(); ++o) {
      if (got[o] != expected[o]) {
        std::cout << "assignment=" << m << " output=" << o
                  << " expected=" << expected[o] << " actual=" << got[o] << '\n';
        ++failures;
      }
    }
  }
  std::cout << "mismatches=" << failures << '\n';
  return failures ? 1 : 0;
}
EOF

cat > "$JOB_WORK/repro-src/gates.v" <<'EOF'
module top(a, b, ynor, yxnor, ynand);
  input a, b;
  output ynor, yxnor, ynand;
  assign ynor = ~(a | b);
  assign yxnor = ~(a ^ b);
  assign ynand = ~(a & b);
endmodule
EOF

cat > "$JOB_WORK/repro-src/reference_tb.v" <<'EOF'
module reference_tb;
  reg a, b;
  wire ynor, yxnor, ynand;
  integer m;
  top dut(a, b, ynor, yxnor, ynand);
  initial begin
    for (m = 0; m < 4; m = m + 1) begin
      {b, a} = m;
      #1;
      if (ynor !== !(a | b) || yxnor !== (a == b) || ynand !== !(a & b))
        $fatal(1, "reference mismatch");
      $display("assignment=%0d outputs=%b%b%b", m, ynor, yxnor, ynand);
    end
    $display("Icarus: all 4 assignments agree with the independent Boolean formulas");
    $finish;
  end
endmodule
EOF

cmake -S "$JOB_WORK/repro-src" -B "$JOB_WORK/repro-verilog" -DMOCKTURTLE_DIR="$JOB_WORK/mockturtle" -DCMAKE_BUILD_TYPE=Debug
cmake --build "$JOB_WORK/repro-verilog" -j3
"$JOB_WORK/repro-verilog/repro" "$JOB_WORK/repro-src/gates.v"
"$JOB_WORK/repro-verilog/repro_ndebug" "$JOB_WORK/repro-src/gates.v"
iverilog -g2012 -s reference_tb -o "$JOB_WORK/verilog-reference.vvp" "$JOB_WORK/repro-src/gates.v" "$JOB_WORK/repro-src/reference_tb.v"
vvp "$JOB_WORK/verilog-reference.vvp"

Expected: NOR is ~(a | b), XNOR is ~(a ^ b), and NAND is ~(a & b). These exact NOR and XNOR forms appear in the bundled parser's NOR callback documentation and XNOR callback documentation. The mockturtle reader documentation exposes this adapter. Each assignment contains one elementary gate, as described in the maintainer's subset explanation.

Observed: both C++ executables exit 1 and print:

parser success=1 inputs=2 outputs=3
assignment=0 output=0 expected=1 actual=0
assignment=0 output=1 expected=1 actual=0
assignment=3 output=1 expected=1 actual=0
mismatches=3

Assignment numbers encode a in bit 0 and b in bit 1. Output indices are NOR, XNOR, NAND.

a b Expected NOR, XNOR, NAND Imported NOR, XNOR, NAND
0 0 111 001
1 0 001 001
0 1 001 001
1 1 010 000

The reference testbench exits 0 and prints 111, 001, 001, 010. Independent verification also exported the imported AIG and MIG networks with write_verilog and ran Icarus on them. All four exports, covering both network types with assertions enabled and disabled, had the same three wrong output bits.

Environment

  • OS: Linux 6.17.0-1020-oracle, x86_64 GNU/Linux.
  • Compiler: GCC 13.3.0, Ubuntu 13.3.0-6ubuntu2~24.04.1, GNU C++17.
  • Build: CMake 3.28.3, Unix Makefiles, Debug. The second executable adds NDEBUG to the Debug build.
  • Simulator: Icarus Verilog and vvp 14.0 development, s20260301-531-g633cc63a1-dirty.

Additional context

mockturtle's adapter implements NAND but has no NOR or XNOR callback overrides. Lorina's dispatch calls the correct callbacks with the correct operands. Lorina itself is correct for this fixture. The inherited callback bodies do nothing. During output creation, signals_[o] inserts a default signal for each missing output, which is constant zero in AIG and MIG.

A local adapter subclass implementing only these two callbacks made the exported truth tables pass. Splitting OR/XOR and inversion into separate assignments also passed. The NAND control passed every input row. Both forms are included here because they share the same missing-callback cause.

Tested scope: scalar, combinational assignments in the default top module, default reader settings, all four input combinations, and AIG/MIG networks. The focused upstream [verilog_reader] tests passed 44 assertions in seven cases. No full test suite, formal check, optimized Release build, other network type, hierarchy, or arbitrary behavioral Verilog was tested.

The reproducer and testbench above are provided under CC0.

Check list

  • The recorded Debug run had no assertion failure.
  • The provided code compiled and the testcase reproduced the error during independent verification.
  • The testcase is limited to NOR, XNOR, and a NAND control.

ChipLab ref: CLAB-2026-353

Found by ChipLab's AI verification agents and independently reproduced.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions