Skip to content

cxxrtl: -O1 optimisation unbuffers wires too permissivelyΒ #5371

@toby-bro

Description

@toby-bro

Version

Yosys 0.57+148 (git sha1 259bd6f, g++ 15.2.1 -march=x86-64 -mtune=generic -O2 -fno-plt -fexceptions -fstack-clash-protection -fcf-protection -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -flto=auto -fPIC -O3)

On which OS did this happen?

Linux

Reproduction Steps

When running this script (which creates a systemverilog file, it's testbench then runs them),

#!/bin/bash

rm -f m_feat2_simplified.sv m_feat2_simplified.cc testbench.cpp sim_O0 sim_O1

cat > m_feat2_simplified.sv << 'EOF'
module m_feat2_simplified (
    input logic clk,
    input wire rst,
    input logic [7:0] v_in,
    output logic [7:0] v_out
);
    int counter;

    task incr(); counter++; endtask
    initial begin
        counter = 0;
    end
    always_ff @(posedge clk) begin
        incr();
        v_out <= v_in ^ counter[7:0];
    end
endmodule
EOF

cat > testbench.cpp << 'EOF'
#include "m_feat2_simplified.cc"

#include <iostream>
#include <iomanip>

int main() {
    cxxrtl_design::p_m__feat2__simplified dut;
    

    dut.p_v__in.set<uint8_t>(0xFF);  // v_in = ff (from bug report)
    dut.p_rst.set<bool>(false);      // rst = 0 (from bug report)
    dut.p_clk.set<bool>(true);       // clk = 1 (from bug report)
    

    dut.step();
    

    for (int cycle = 0; cycle < 50; cycle++) {
        dut.p_clk.set<bool>(false);
        dut.step();
        dut.p_clk.set<bool>(true); 
        dut.step();
    }
    

    for (int settle = 0; settle < 20; settle++) {
        dut.step();
    }
    

    uint8_t result = dut.p_v__out.get<uint8_t>();
    for (int i = 7; i >= 0; i--) {
        std::cout << ((result >> i) & 1);
    }
    std::cout << std::endl;
    
    return 0;
}
EOF

for opt in 0 1; do
    echo "Testing with -O$opt:"

    yosys -q -p "read_verilog -sv m_feat2_simplified.sv; prep -top m_feat2_simplified ; write_cxxrtl -O$opt m_feat2_simplified.cc"

    g++ -std=c++17 -O0 -I$(yosys-config --datdir)/include/backends/cxxrtl/runtime testbench.cpp -I. -o sim_O$opt
    ./sim_O$opt
done

Expected Behavior

The two results with optimisation level -O0 and -O1 are different

Actual Behavior

I would have expected the results of both tests to be the same...
Especially I agree with the first result with -O0 rather than with that of -O1 (from having simulated this sv design on other simulators)

Metadata

Metadata

Assignees

No one assigned

    Labels

    cxxrtlpending-verificationThis issue is pending verification and/or reproduction

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions