Doubt on Scope Randomize Function


LRM  18.12  says :: 

The scope randomize function, std::randomize() , enables users to randomize data in the current
scope without the need to define a class or instantiate a class object


But the LRM isn’t clear if an object with random properties can be used as argument to it

Example ::


class seq ;

     rand bit [1:0] b ;
          
	  bit [1:0] a ;

  endclass

  seq  obj ;

  initial begin

      obj = new();

    if ( std::randomize(obj.a) with { obj.a == 3 ; } ) //  Valid ??   
      begin
         $display("Success with a == %0d",obj.a);
      end
    else
     begin
        $display("Fails");
     end

    if ( std::randomize(obj.b) with { obj.b == 2 ; } ) //  Valid ??
      begin
         $display("Success with b == %0d",obj.b);
      end
    else
     begin
        $display("Fails");
     end
  end



I see warning message on some Simulators ::

Warning: (vopt-2961) Argument #1 for std::randomize() function is non-LRM compliant.

While some throw Compilation Error ::

**if ( std::randomize(obj.a) with { obj.a == 3 ; } )

xmvlog: *E,RNDRARG : The argument to class or scope randomize must be a simple identifier of integral type.
**

[Q] Is this a violation based on LRM ??

In reply to TC_2017:

This is an open issue in the LRM. 0002967: define explicitly legal set of data types for randomize variables list - Accellera Mantis

Hopefully the IEEE P1800 committee can address this in the next revision of the standard.

In reply to dave_59:

It would also be nice to randomize an element of a struct without having the compiler yell at you.


struct
{
  int a;
  int b;
} some_struct;
  
initial begin
   void'(std::randomize (some_struct.a));
end

produces

** Warning: testbench.sv(12): (vlog-2961) Argument #1 for std::randomize() function is non-LRM compliant.

To work around you need to introduce a dummy variable.


initial begin
   int dummy_var;
    
   void'(std::randomize (dummy_var));
   some_struct.a = dummy_var;
end

So much typing!

In reply to sbellock:

https://accellera.mantishub.io/view.php?id=2970

Hi Dave,

Was trying the above code using the latest edatools, I observe that 2 of 3 tools now treat the code as legal

(1) Does 1800-2023 make any new mention about scope of std::randomize ?

Section 18.2 of 1800-2023 says

The scope randomize function behaves exactly the same as a class randomize method except that it operates on the variables of the current scope instead of class member variables

(2) So why is hierarchical referencing discussed in thread illegal for class randomize method whereas it’s legal for std::randomize ?

(3) In a module is the prefix std:: when calling the randomize function optional ?

(1)(2) This is an oversight in the BNF syntax description, which resulted in unintended consequences. We didn’t get around to correcting it in the 1800-2023 standard, even though some simulators had supported it as intended for a while.

You don’t need to use the prefix std:: since the std package is automatically imported. The only place it’s needed is within a class method to distinguish it from the built-in randomize() class method.

Hi Dave,

Could you please elaborate ?

I am not clear which one of the two randomize functions needs an update

scope_randomize ::=
[ std :: ] randomize ( [ variable_identifier_list ] ) [ with constraint_block ]

Needs to use a term less restrictive than variable_identifier_list which does not currently exist in the BNF.

Probably need to create something like variable_lvalue_list.

Hi Sir,

Just wanted to confirm if I have been able to follow the above conversation

Section 18.7 uses variable_identifier_list for randomize()

(A) Referencing variables hierarchically is illegal for variable_identifier_list whereas variable_lvalue_list could reference variables hierarchically

Is my understanding correct ?

(B) Would this also mean that there should be an update in following quote from 18.12 ?

The scope randomize function behaves exactly the same as a class randomize method,

except that it operates on the variables of the current scope instead of class member variables

Unlike class randomize function, scope randomize function allows hierarchical referencing.

So they are not exactly the same in terms of the argument

(A) Correct.

(B) It’s also correct, but I would reverse the restrictions. The arguments to the class’s randomize() method are limited to its member class variables, while the scope std::randomize() method can include any variables.