Unexpected Behaviour From Xilinx Vivado?

Hi,

I was trying to learn some array methods when I encountered a strange phenomena when trying calculate an array’s sum based on a iterator condition using a “with” clause

To give context, here’s the code

module tb_array_methods;
    function void disp_msg(input string tag, input string msg);
        $display($sformatf("[%0t][%0s]: %0s", $time, tag, msg));
    endfunction
    int arr[$] = {21,33,42,20,20,42,11,24,33};
    longint res;
    int arr_loc[$];
    int res_2;


    bit b_array[] = '{1,0,0,1,0,1};



    // ====================================== //
    // Array Locator Methods
    // All array locator methods return the results as a queue of type "int" and not "integer"
    initial begin



        // How about we combine some boolean functions with locators?
        //int arr[$] = {21,33,42,20,20,42,11,24,33};
        res_2 = arr.sum() with (item>34); 
        // Returns sum({0,0,1,0,0,1,0,0,0}) = 2; 
        // This can be used to find the count of certain values        
        disp_msg("Block 3", $sformatf("res_2 = %0d", res_2));


        res_2 = arr.sum() with (item * (item>34)); 
        // Returns sum({0,0,42,0,0,42,0,0,0}) = 84;


        res_2 = arr.sum() with (item > 34? item:0);
        // Returns 84
        disp_msg("Block 3", $sformatf("res_2 = %0d", res_2));
        
    end
endmodule

When I ran this code, I got this result

image

But I was expecting a sum of 2. Instead I got a 565641244.

Could anyone please tell me what’s wrong here?

Also when i use

res_2 = arr.sum(x) with (x>34); 

I get

res_2 = 111067408

PS: I also noticed that Xilinx says compile issues when i use queues with product() and and(). Is that a xilinx issue? Because in chris spear, he uses them with queues

The code you have shown does not match the output you’re getting. Your code only calls disp_msg() twice, yet your output looks like it was called five times. And we can’t help with your compile issues unless you show us the error messages.

res_2 = arr.sum(x) with (x>34); 

Has a problem: the expression inside the with clause is a relational operator whose result is a single bit. So its output can only be 0 or 1. If you want a count of elements greater than 34, you would write:

res_2 = arr.sum(x) with (int'(x>34)); 

Here’s my full code.
I had have commented the queue reduction at lines 14 and 16.
Also the print issue I mentioned regarding the sum operation is at line

1. module tb_array_methods;
2.     function void disp_msg(input string tag, input string msg);
3.         $display($sformatf("[%0t][%0s]: %0s", $time, tag, msg));
4.     endfunction
5.     int arr[$] = {21,33,42,20,20,42,11,24,33};
6.     longint res;
7.     int arr_loc[$];
8.     int res_2;

9.     bit b_array[] = '{1,0,0,1,0,1};

10.     ///// Array Reduction Methods
11.     initial begin
12.         res = arr.sum();
13.         disp_msg("Block 1", $sformatf("res_sum = %0d", res));
14.         // res = arr.product();
15.         // disp_msg("Block 1", $sformatf("res_product = %0d", res));
16.         // res = arr.and();
17.         // disp_msg("Block 1", $sformatf("res_and = %0d", res));
18.     end

19.     // ====================================== //
20.     // Array Locator Methods
21.     // All array locator methods return the results as a queue of type "int" and not "integer"
22.     initial begin
23.         arr_loc = arr.min(); // Returns {11}
24.         disp_msg("Block 2", $sformatf("arr_loc = %0p", arr_loc));
25.         arr_loc = arr.max(); // Returns {42}
26.         disp_msg("Block 2", $sformatf("arr_loc = %0p", arr_loc)); 
27.         // Min and Max function also work for associative arrays

28.         arr_loc = arr.unique();
29.         disp_msg("Block 2", $sformatf("arr_loc = %0p", arr_loc));

30.         // We require an queue variable to store these results. Why?
31.         // Because in case of empty queues, the functions will return empty queue.

32.         //////////////////////////
33.         // "with" operator
34.         arr_loc = arr.find() with (item > 34); // Returns a queue of type int
35.         disp_msg("Block 2", $sformatf("arr_loc = %0p", arr_loc));

36.         arr_loc = arr.find(x) with (x <42); 
37.         disp_msg("Block 2", $sformatf("arr_loc = %0p", arr_loc));

38.         arr_loc = arr.find_index() with (item == 33);
39.         disp_msg("Block 2", $sformatf("arr_loc = %0p", arr_loc));

40.         arr_loc = arr.find_first_index() with (item == 32); // Returns an empty string
41.         disp_msg("Block 2", $sformatf("arr_loc = %0p", arr_loc));

42.         arr_loc = arr.find_last_index with (item == 20);
43.         disp_msg("Block 2", $sformatf("arr_loc = %0p", arr_loc));


44.         // How about we combine some boolean functions with locators?
45.         //int arr[$] = {21,33,42,20,20,42,11,24,33};
46.         res_2 = arr.sum() with (item>34); 
47.         // Returns sum({0,0,1,0,0,1,0,0,0}) = 2; 
48.         // This can be used to find the count of certain values       
49.         disp_msg("Block 3", $sformatf("res_2 = %0d", res_2));

50.         res_2 = arr.sum() with (item * (item>34)); 
51.         // Returns sum({0,0,42,0,0,42,0,0,0}) = 84;

52.         res_2 = arr.sum() with (item > 34? item:0);
53.         // Returns 84
54.         disp_msg("Block 3", $sformatf("res_2 = %0d", res_2));
55.         

56.         // One has to be careful of bit widths!
57.         // sum() function uses the width of the array's data
58.         // So if we add values of a 1-bit array, using sum(), we will will get a single bit result of either 0 or 1
59.         // So we need to type cast values before.

60.         res_2 = b_array.sum(); // Total = (1 + 1 + 1) = 1 (in 1-bit addition)
61.         disp_msg("Block 3", $sformatf("res_2 = %0d", res_2));

62.         res_2 = b_array.sum() with (int'(item)); // Total = 1 + 1 + 1 = 3
63.         disp_msg("Block 3", $sformatf("res_2 = %0d", res_2));
64.     end

65.     //===================================//
66.     // Array Sorting and Ordering
67.     // Unlike array locators and reduction operators, these change the original array!!
68.     int d [] = '{2,1,2,42,3,5};

69.     struct packed {bit [7:0] r,g,b;} pix[];
70.   
71.     initial begin
72.         #40;
73.         pix = '{'{r:7, g:34, b: 65}, '{r:70, g:49, b: 6},'{r:46, g:32, b: 63}}; 
74.         //Structures also use '{} instead of {} for full structure assignment
75.         d.reverse();
76.         disp_msg("Block 4", $sformatf("Reverse = %0p", d));

77.         d.sort();
78.         disp_msg("Block 4", $sformatf("Sorted = %0p", d));

79.         d.shuffle();
80.         disp_msg("Block 4", $sformatf("Shuffled = %0p", d));

81.         d.rsort();
82.         disp_msg("Block 4", $sformatf("Reverse Sorted = %0p", d));  

83.         // reverse() and shuffle() don't have a "with" clause. But others have...
84.         pix.sort() with (item.r); // Sort using r only
85.         disp_msg("Block 5", $sformatf("Structure with sorting based on Red= %0p", pix));
86.         // pix = '{'{r:7, g:34, b: 65}, '{r:46, g:32, b: 63}, '{r:70, g:49, b: 6}};

87.         pix.sort() with ({item.g, item.b}); // Sort first based on g then b
88.         disp_msg("Block 5", $sformatf("Structure with sorting based on Red= %0p", pix));
89.         // pix = '{'{r:70, g:49, b: 6}, '{r:46, g:32, b: 63},'{r:7, g:34, b: 65}};

90.         // Only fixed & dynamic arrays and queues can be sorted, reversed or shuffled
91.         // Associative arrays cannot be re-ordered because it's a key based storage. 
92.         // We can't just say move this key before that key. 

93.         
94.     end
95. endmodule

Also the print issue I mentioned regarding the sum operation is at line 46

Here’s the complete output I get when running your code on all the simulators available on EDAPlayground.com. Perhaps there is a problem with the Vivado version you are using.

[0][Block 1]: res_sum = 246
[0][Block 2]: arr_loc = '{11} 
[0][Block 2]: arr_loc = '{42} 
[0][Block 2]: arr_loc = '{21, 33, 42, 20, 11, 24} 
[0][Block 2]: arr_loc = '{42, 42} 
[0][Block 2]: arr_loc = '{21, 33, 20, 20, 11, 24, 33} 
[0][Block 2]: arr_loc = '{1, 8} 
[0][Block 2]: arr_loc = '{}
[0][Block 2]: arr_loc = '{4} 
[0][Block 3]: res_2 = 0
[0][Block 3]: res_2 = 84
[0][Block 3]: res_2 = 1
[0][Block 3]: res_2 = 3
[40][Block 4]: Reverse = '{5, 3, 42, 2, 1, 2} 
[40][Block 4]: Sorted = '{1, 2, 2, 3, 5, 42} 
[40][Block 4]: Shuffled = '{42, 1, 5, 3, 2, 2} 
[40][Block 4]: Reverse Sorted = '{42, 5, 3, 2, 2, 1} 
[40][Block 5]: Structure with sorting based on Red= '{'{r:'h7, g:'h22, b:'h41}, '{r:'h2e, g:'h20, b:'h3f}, '{r:'h46, g:'h31, b:'h6}} 
[40][Block 5]: Structure with sorting based on Red= '{'{r:'h2e, g:'h20, b:'h3f}, '{r:'h7, g:'h22, b:'h41}, '{r:'h46, g:'h31, b:'h6}}