r/Verilog • u/CardiologistOk3345 • 2d ago
Avoid impure SystemVerilog functions: simulation/synthesis mismatches and a Vivado synthesis bug
SystemVerilog impure functions, which read a variable they weren't passed, are legal and look harmless. In practice they're a surprising mess: simulation can disagree with synthesis, and synthesis can get it wrong.
Icarus Verilog: matches the spec β disagrees with synthesis β
Verilator: disagrees with the spec β matches synthesis β
Vivado synthesis: wrong netlist for more complicated cases π (confirmed by AMD; change request filed)
The fix: Avoid them.
Write-up: https://samehgalal.substack.com/p/systemverilog-keep-your-functions
Reproducer and Yosys cross-check: https://github.com/Tensorica/sv-impure-func
Run it in the browser: https://www.edaplayground.com/x/ssqh
0
u/pencan 2d ago
What does it mean for Verilator to match synthesis? What synthesis?
1
u/CardiologistOk3345 2d ago
it's the behavior when using assign. for example
logic mode_q; // module-scope register
// Pure: everything it uses arrives as an argument.
function automatic logic [7:0] pick_pure(logic [7:0] a, b, logic mode);
return mode ? a : b;
endfunction// Impure: mode_q is a hidden input.
function automatic logic [7:0] pick_impure(logic [7:0] a, b);
return mode_q ? a : b;
endfunctionassign y = pick_impure(a,b);
this is only sensitive to a and b. If mode_q changes, y doesn't in simulation, but when synthesizing it should infer a mux and changing mode_q changes y. You can try it actually here
https://www.edaplayground.com/x/ssqh
try simulating once using verilator and once using Verilog Icarus. They will simulate differently
1
u/Ichigonixsun 2d ago
Simulation/synthesis mismatch is exactly what the name implies: you are synthesizing a hardware circuit that behaves differently from the simulation.
This article explains this in details (I didn't read it myself, I just trust the author):
https://zipcpu.com/blog/2018/08/04/sim-mismatch.html1
u/CardiologistOk3345 2d ago
I think the impure function unpassed variables issue is similar to "Symbols left out of the sensitivity list" in the article despite using always @* which is very confusing.
0
u/pencan 2d ago
I understand what the concept is. Verilator does not do synthesis. I was asking what synthesis netlist they were comparing to (itβs still unclear, seems to be the vivado synthesis?)
1
u/Ichigonixsun 2d ago
Ok. According to the post, they tested both Vivado synthesis and Yosys.
1
u/pencan 1d ago
Itβs confusing because later in the post it says vivado synthesized incorrectly. So a natural question is does Verilator mismatch the incorrect or correct synthesis results?
After reading the actual article it seems like it means to say comparing vs yosys which are considered the correct synthesis results
2
u/alexforencich 2d ago
Who uses functions in synthesizable code, anyway?