verilator icon indicating copy to clipboard operation
verilator copied to clipboard

Support `array.sum() with (...)` constraints

Open raulbehl opened this issue 4 months ago • 1 comments

Thanks for taking the time to report this.

Can you please attach an example that shows the issue or missing feature? (Must be openly licensed, completely self-contained so can directly run what you provide. Ideally use test_regress format, see https://veripool.org/guide/latest/contributing.html?highlight=test_regress#reporting-bugs)

Here's the code which fails to compile on verilator (error message listed below):

// Minimal reproducer for Verilator bug report:
// - Shows array constraints for repeated values
// - Should randomize array with repeated_value exactly three times and others once

class three_duplicates_only;
  rand byte array[5];
  rand byte repeated_value;
  constraint three_duplicates_only_c {
    array.sum() with (int'(item==repeated_value)) == 3;
    foreach(array[i]) {
      array[i] != repeated_value -> array.sum() with (int'(item==array[i]))== 1;
    }
  }
  function void display();
    foreach (array[i]) begin
      $display("Array[%0d]: %d", i, array[i]);
    end
  endfunction
endclass

module three_duplicates_only_top;
  three_duplicates_only inst;
  initial begin
    inst = new();
    if (inst.randomize()==0) begin
      $fatal(1, "Failed to randomize array.");
    end
    inst.display();
  end
endmodule

What output from that test indicates it is wrong, and what is the correct or expected output? (Or, please make test self-checking if possible.)

%Warning-CONSTRAINTIGN: top.sv:33:11: Unsupported: randomizing this expression, treating as state
   33 |     array.sum() with (int'(item == array[i])) == 1;
      |           ^~~
                        top.sv:13:1:
                        ... For warning description see https://verilator.org/warn/CONSTRAINTIGN?v=5.041
                        ... Use "/* verilator lint_off CONSTRAINTIGN */" and lint_on around source to disable this message.
%Error: Exiting due to 1 warning(s)

What 'verilator' command line do we use to run your example?

verilator -cc --exe --binary --timing --top-module three_duplicates_only_top three_duplicates_only.sv

What 'verilator --version' are you using? Did you try it with the git master version? Did you try it with other simulators?

Verilator 5.041 devel rev v5.040-121-gdca555b6d

Expected output from other simulators:

Array[0]:   90
Array[1]:   90
Array[2]:    7
Array[3]:  123
Array[4]:   90

What OS and distribution are you using?

macOS

May we assist you in trying to fix this in Verilator yourself?

Would be happy to help but unfortunately I still haven't been able to find my way around the verilator source code.

raulbehl avatar Sep 18 '25 16:09 raulbehl

@YilouWang any interest in fixing?

wsnyder avatar Sep 18 '25 22:09 wsnyder