Function in Constraint

Hi all,
I was trying to generate this sequence by using constraint → 021346578. This is my code -

module test;
  class generate_pattern;
    rand int array[];
    int j=0,k=0,num1 = 0,num2 = 2;
    constraint size{array.size == 9;}
    constraint condition{foreach(array[i])
      array[i]==func(i);}

    function int func (int i);
      if(!(i%2))
        begin
          func = num1;
          if(j==0)
             begin
               num1++;
               j++;
             end
          else
             begin
                j = 0;
                num1+=3;
             end
        end
      
      
      else
        begin
          func = num2;
          if(k==0)
              begin
                num2++;
                k++;
              end
          else 
              begin
                num2+=3;
                k=0;
              end
        end
    endfunction
  
    function void post_randomize();
      $display("%0p",array);
    endfunction
  endclass
  
  generate_pattern pkt = new;
  
  initial
    begin
      assert(pkt.randomize());
    end
endmodule
  • This should have given the required output. But it’s giving the following error message .

Error-[CSTR-UMC] Constraint: unsupported method call
testbench.sv, 176
test, “this.func(i)”
Method generate_pattern::func contains a construct not supported in
constraint functions: driving a non-local variable ‘num1’ at line 185 of
testbench.sv.
Rewrite the method to avoid unsupported constructs.

Error-[CSTR-UMC] Constraint: unsupported method call
testbench.sv, 176
test, “this.func(i)”
Method generate_pattern::func contains a construct not supported in
constraint functions: driving a non-local variable ‘j’ at line 186 of
testbench.sv.
Rewrite the method to avoid unsupported constructs.

Error-[CSTR-UMC] Constraint: unsupported method call
testbench.sv, 176
test, “this.func(i)”
Method generate_pattern::func contains a construct not supported in
constraint functions: driving a non-local variable ‘j’ at line 190 of
testbench.sv.
Rewrite the method to avoid unsupported constructs.

Error-[CSTR-UMC] Constraint: unsupported method call
testbench.sv, 176
test, “this.func(i)”
Method generate_pattern::func contains a construct not supported in
constraint functions: driving a non-local variable ‘num1’ at line 191 of
testbench.sv.
Rewrite the method to avoid unsupported constructs.

Error-[CSTR-UMC] Constraint: unsupported method call
testbench.sv, 176
test, “this.func(i)”
Method generate_pattern::func contains a construct not supported in
constraint functions: driving a non-local variable ‘num2’ at line 202 of
testbench.sv.
Rewrite the method to avoid unsupported constructs.

Error-[CSTR-UMC] Constraint: unsupported method call
testbench.sv, 176
test, “this.func(i)”
Method generate_pattern::func contains a construct not supported in
constraint functions: driving a non-local variable ‘k’ at line 203 of
testbench.sv.
Rewrite the method to avoid unsupported constructs.

Error-[CSTR-UMC] Constraint: unsupported method call
testbench.sv, 176
test, “this.func(i)”
Method generate_pattern::func contains a construct not supported in
constraint functions: driving a non-local variable ‘num2’ at line 207 of
testbench.sv.
Rewrite the method to avoid unsupported constructs.

Error-[CSTR-UMC] Constraint: unsupported method call
testbench.sv, 176
test, “this.func(i)”
Method generate_pattern::func contains a construct not supported in
constraint functions: driving a non-local variable ‘k’ at line 208 of
testbench.sv.
Rewrite the method to avoid unsupported constructs.

In reply to Shubhabrata:

If you look the error is telling the exact problem, when using functions in constraints they cannot have side effects in this case a side effect is altering the value of num1, you can find more information in the 1800-2017 LRM section 18.5.12 Functions in constraints

“— Functions that appear in constraint expressions should be automatic (or preserve no state information) and have no side effects.”

HTH,

-R

In reply to rgarcia07:

Hi. Thanks for the help . Actually, I was using the eda-playground platform and I was using Synopsys VCS. It was giving the above-mentioned error messages. When I changed the tool and used Mentor Questa, I got the expected answer . Didn’t understand why. I mean, is there anything called tool depending-attributes in SV? Something didn’t work on one tool but gave required ramifications on another tool.
Do u know anything about it ?

In reply to Shubhabrata:

The problem here is there is no defined ordering, or even defined number of times your function gets called within a constraint. These constraints are not procedural code. By having side effects in the function, you are relying on indeterminate evaluations. The foreach loop gets unrolled into 9 constraints and there is no guarantee that the func(i) gets called in the order from 0…8.

class seq;

	rand bit [4:0] array [];

	constraint size { array.size() == 16;}
	constraint  element { foreach(array[i]) {
					if(i == 0) {
						array[i] == 0;
					}
					else if (i %2 == 1) {
						array[i] == array[i-1]+2;
					}
					else if ( i % 4 == 0) {
						array[i] == array[i-1] + 1;
					}
					else if(i %2 ==0) {
						array[i] == array[i-1] -1;
					}
				}
				}
endclass
module check;

	seq S1;

	initial 
		 begin
			S1 = new();
			S1.randomize();	
			$display("%0p", S1.array);
		end
endmodule

Incase this is not only about use of function inside constraint. can change the size to required no.