Confusion on calling SV system task $size for dynamic types

Hi Moderators,

Section 5.13 of IEEE Std 1800-2023 mentions

In general, a built-in method is preferred over a system task when a particular 
functionality applies to all data types or when it applies to a specific data type.
For example: dynamic_array.size, associative_array.num, and string.len
These are all similar concepts, but they represent different things. 
A dynamic array has a size, an associative array contains a given number of
items, and a string has a given length. Using the same system task, such as 
$size, for all of them would be less clear and intuitive.

This section doesn’t explicitly say that using $size for dynamic type is illegal

Section 7.5.2 mentions

The size dynamic array method is equivalent to the $size(addr, 1) array query 
system function (see 20.7)

My interpretation is of this quote is that $size is legal for dynamic array.

Similarly it should be legal for queue as well

Later Section 20.7.1 says

int a[3][][5]; // array dimension 2 has variable size
$display( $unpacked_dimensions(a) ); // displays 3
a[2] = new[4];
a[2][2][0] = 220;        // OK, a[2][2] is a 5-element array
$display( $size(a, 1) ); // OK, displays 3
$display( $size(a, 2) ); // ERROR, dimension 2 is dynamic

[Q1] Why is $size(a,2) considered an error here ?

[Q2] Does LRM explicitly mention that $size() is illegal for dynamic array / queue type ?

Thanks

The main issue here is accessing the dimensions of a multidimensional array. SystemVerilog doesn’t inherently support these multiple dimensions everywhere; instead, it has arrays of any type, including arrays of arrays. When you try to find the size of the second dimension, each array within the first dimension might have a different size for its second dimension because it is dynamically sized. As a result, the $size method won’t work. You’ve only dynamically allocated element a[2] to 4, while the rest of the elements in the dimension still have zero elements.

Thank you Sir. Using trial and error I observe

$display($size(a[2],2)); // displays 5 due to fixed size of 5 for dimension 3
$display($size(a[2],1)); // displays 4 due to size of a[2] is 4;
$display($size(a[2]));   // displays 4 due to a[2] = new[4];

I would like to discuss a few more things

[3] Section 20.7 of LRM-2023 mentions

It is an error to use these functions directly on a dynamically sized type identifier

[Q3] Isn’t the above statement contradicting with following quote from Section 7.5.2 ?

The size dynamic array method is equivalent to the $size(addr,1) array query system function (see 20.7)

[4] Section 18.4 of LRM-2023 mentions

The size of a dynamic array or queue declared as rand or randc can also be constrained.
In that case, the array shall be resized according to the size constraint, and then all the array elements shall be randomized.The array size constraint is declared using the size method

[Q4] As per the quote from Section 7.5.2 as dyn_array.size() method is equivalent to $size(dyn_array,1), would $size(dyn_array)/$size(dyn_array,1) be legal in constraint ?

rand bit [1:0] a1[];
rand bit [1:0] a2[$];

// The below two constraints work across all tools
constraint SIZE1 { a1.size() inside{[3:6]}; }
constraint SIZE2 { a2.size() inside{[3:6]}; }

When I try writing the above two constraints using $size(arr,1)

constraint SIZE1_ { $size(a1,1) inside {[3:6]}; }
constraint SIZE2_ { $size(a2)   inside {[3:6]}; }

[Q5] As per LRM would SIZE1_ and SIZE2_ be considered legal ?

Currently on EDA I observe that only 1 tool ( VCS ) considers this as legal.

[6] Using $size in post_randomize() for dynamic types

rand bit [1:0] a1[];
rand bit [1:0] a2[$];

// The below two constraints work across all tools
constraint SIZE1 { a1.size() inside{[3:6]}; }
constraint SIZE2 { a2.size() inside{[3:6]}; }

// Both array' size are constrained with elements having random values
function post_randomize();
  $display("$size(a1,1) is %0d",$size(a1,1));
  $display("$size(a2) is %0d",$size(a2));
endfunction

[Q6] As per LRM would $size() in post_randomize() be considered legal for dynamic types ?Currently on EDA I observe that 2 tools ( VCS & Xcelium ) consider this as legal.