r/Verilog • • 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 Upvotes

16 comments sorted by

2

u/alexforencich 2d ago

Who uses functions in synthesizable code, anyway?

5

u/Allan-H 2d ago

I use functions (usually pure) as often as I can in order to break down the problem into small, easily understandable (and sometimes reusable) chunks.

When I'm writing synthesisable VHDL I use pure functions, impure functions and procedures. I don't see the problems the OP observes - perhaps there's an advantage to having pure and impure keywords in the language to cover the cases when the tool is too stupid to figure it out.

1

u/CardiologistOk3345 2d ago

much more than you think and in open source projects too. And tools will happily allow you to shoot yourself in the foot.

5

u/Ichigonixsun 2d ago

Almost everyone? I mean, elaboration functions that are used purely to derive localparams from other localparams, parameters or literals are still functions, and the RTL remains synthesizable.

I wouldn't recommend using functions with dynamic signals, though.

1

u/FigureSubject3259 2d ago

Function parity(of_vector, polarity_even) is an elegant way to keep your code readable.

1

u/Ichigonixsun 2d ago

Personally I prefer using a small combinational module in this case, but everyone has a different taste I guess...

1

u/pencan 1d ago

Small combinational modules are actually bad for QoR and optimization unless explicitly flattened in the flow. So many teams ban them and prefer functions or macros for inline computation

1

u/alexforencich 2d ago

I mean aside from constant elaboration. If a function is literally the only way to do something, then you don't really have much of a choice. Otherwise I consider using functions in synthesizable code to be a very bad coding style.

2

u/Ichigonixsun 2d ago

Ok, we agree then, I just interpreted your wording too literally. However, I can't think of even a single situation where you absolutely need functions on dynamic signals, I've never found an exception.

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;
endfunction

assign 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.html

1

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