Will temp variable in always_comb create latch
Will temp variable in always_comb create latch
I have following code snippet where a temp variable is used to count number of 1s in an array:
// count the number 1s in array
logic [5:0] count_v; //temp
always_comb begin
count_v = arr[0];
if (valid) begin
for (int i=1; i<=31; i++) begin
count_v = arr[i] + count_v;
end
end
final_count = count_v;
end
Will this logic create a latch for count_v ? Is synthesis tool smart enough to properly synthesize this logic? I am struggling to find any coding recommendation for these kind of scenarios.
Another example:
logic temp; // temp variable
always_comb begin
temp = 0;
for (int i=0; i<32; i++) begin
if (i>=start) begin
out_data[temp*8 +: 8] = in_data[i*8 +: 8];
temp = temp + 1'b1;
end
end
end
3 Answers
3
General rule: if you read a variable before writing to it, then your code implies memory of some sort. In this case, both the simulator and synthesiser have to implement storage of a previous value, so a synthesiser will give you a register or latch. Both your examples write to the temporary before reading it, so no storage is implied.
Does it synthesisie? Try it and see. I've seen lots of this sort of thing in production code, and it works (with the synths I've used), but I don't do it myself. I would try it, see what logic is created, and use that to decide whether you need to think more about it. Counting set bits is easy without a loop, but the count loop will almost certainly work with your synth. The second example may be more problematical.
You are right, EML: if you read a variable before writing to it, you will get a latch (unless the always block is clocked, then you get a flip-flop).
– Matthew Taylor
Jul 2 at 7:51
Sorry. @Oldfart. EML is right. Yes, you will get a latch if there is one conditional path where no value is assigned, but you will also get a latch if you read a variable before writing to it.
– Matthew Taylor
Jul 2 at 7:52
@MatthewTaylor: ah, I detect someone else who has taught VHDL :)
– EML
Jul 2 at 8:11
@MatthewTaylor I just tried reading a variable before assigning it in Vivado and I do NOT get a latch. I assume the construct you are referring to must be more complex then this:
nb = temp; temp = par_tx[0]; for (i=1; i<DW; i=i+1) temp = temp + par_tx[i]; temp disappears and nb is a combinatorial path.– Oldfart
Jul 2 at 8:51
nb = temp; temp = par_tx[0]; for (i=1; i<DW; i=i+1) temp = temp + par_tx[i];
For any always block with deterministic initial assignment, it will not generate latch except logic loop.
Sorry Eddy Yau, we seem to have some discussions going on regarding your post.
Here is some example code:
module latch_or_not (
input cond,
input [3:0] v_in,
output reg latch,
output reg [2:0] comb1,
output reg [2:0] comb2
);
reg [2:0] temp;
reg [2:0] comb_loop;
// Make a latch
always @( * )
if (cond)
latch = v_in[0];
always @( * )
begin : aw1
integer i;
for (i=0; i<4; i=i+1)
comb_loop = comb_loop + v_in[i];
comb2 = comb_loop;
end
always @( * )
begin : aw2
integer i;
temp = 7;
for (i=0; i<4; i=i+1)
temp = temp - v_in[i];
comb1 = temp;
end
endmodule
This is what came out if it according to the Xilinx Vivado tool after elaboration:
The 'latch' output is obvious. You will also notice that temp is not present in the end result.
The 'comb_loop' is not a latch but even worse: it is a combinatorial loop. The output of the logic goes back to the input. A definitely NO-NO!

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Not true, You will not get a latch as the variable always gets a value. You get a latch if there is one conditional path where no value is assigned.
– Oldfart
Jul 2 at 7:18