Constraint Override

Hi all ,

[1] I have subclass that overrides base class constraint .
However I want that whenever the Overridden Constraint is disabled the parent
Constraint should take effect .

Hence I have following code ::


     class Base ;

  rand bit [4:0] b ;

  constraint VAL {   b == 15 ;  }

endclass

class Ext extends Base ;
 
 `ifdef M1
   bit val_cg ; // Holds Constraint Mode of Constraint VAL

  function  void pre_randomize() ;

      val_cg = VAL.constraint_mode() ;

  endfunction

`endif  

  constraint VAL {   b == 10 ;  } // Overridden Constraint

  constraint BASE_VAL {    
                        `ifdef M1

                           if ( val_cg == 0 )
		  	    {
                               b == 15 ;
			    }
                         
			`elsif M2 

                           if ( VAL_CG() == 0  )
		  	    {
                               b == 15 ;
			    }
			 
			`else

                           if ( VAL.constraint_mode() == 0 )
		  	    {
                               b == 15 ;
			    }

                        `endif

	              }		    


`ifdef M2
  function bit VAL_CG() ; //  No input arg !!

    return ( VAL.constraint_mode() ) ;  
  //  Is it Valid to access random property without passing it as Input ??  

  endfunction


`endif

endclass

 Ext e1 ;

 initial begin

   e1 = new() ;
   
   if ( e1.randomize() )
    $display("Success with %p",e1) ; 
   else
    $display(" Fails") ;     
      
    e1.VAL.constraint_mode(0) ; // Disables Overridden Constraint !!

   if ( e1.randomize() )
    $display("Success with %p",e1) ; 
   else
    $display(" Fails") ; 

 end


Across 3 Licensed Simulators I Observed

With +define+M1 all 3 Simulators ::

Success with '{b:10, val_cg:1}
Success with '{b:15, val_cg:0}

With +define+M2 only 2 Simulators give following Output ::

Success with '{b:10}
Success with '{b:15}

The 3rd Simulator flashed an error :: “VAL_CG contains a construct not supported in constraint functions.” ( Compilation Error )

I believe since we access a random variable within a function without passing it as an input , we get the error.
(Considers it as side-effect maybe)

It doesn’t treat code as constraint guard rather a function called in a Constraint .

[ Q1] I am keen to know if Code with +define+M2 has any LRM violations ?

Without ANY +define ONLY 1 Simulator gave following Output ::

Success with '{b:10}
Success with '{b:15}

**[ Q2] LRM isn't clear about constraint_mode() as a Constraint Guard . So I am confused whether the Code without ANY +define is Valid ?**

Out of the 3 solution I was keen on the 3rd Code ( without ANY +define ) since it doesn’t
have overhead of any extra variables .

[2] I was trying out another Code ( Unrelated to [1] above ) .
Normally in constraint Override we Constraint the Inherited Property .
What if rather than the Inherited Property we Constraint the extended property instead ??


    class Base ;

  rand bit [4:0] b ;

  constraint VAL {   b == 15 ;  }

endclass

class Ext extends Base ;
 
  rand bit [1:0] a ;

  constraint VAL {   a == 1 ;  } // Overriden Constraint but Constraining the Extended Property instead !!

endclass

 Ext e1 ;

 initial begin

   e1 = new() ;
   
   if ( e1.randomize() )
    $display("Success with %p",e1) ; 
   else
    $display(" Fails") ; 

 end

   

I Observe Output :: Success with '{b:24, a:1}

The Inherited Property isn’t Constrained !!

[Q3] Is this Valid based on LRM ?? As in Constraining Extended property instead
of Inherited property within a Constraint Override

In reply to TC_2017:

A1: Accessing a value outside the scope of the function is a side-effect, or impure function, which is not legal. Unfortunately these terms do not have uniform definitions across all languages. (See 0002928: ambiguous restriction on function calls in constraint expressions - Accellera Mantis)

A2: Not sure what you are asking about. A “guard” expression is simply a short-circuit expression preventing a fatal evaluation.

A3: When you override a constraint, it is if the base constraint was never written. It does not matter which variables the overridden constraint references.