soft-constraints-in-systemverilog-semantics-and-challenges
soft-constraints-in-systemverilog-semantics-and-challenges
Mark Strickland, Joseph Hanli Zhang Jason Chen, Dhiraj Goswami, Alex Wakefield
Cisco Systems Synopsys Inc.
{mastrick, jhzhang}@cisco.com {jasonc, dhiraj, alexw}@synopsys.com
Abstract—This pape r introduces SystemVerilog soft constraints, examines all the active constraints to generate a legal solution
de scribing their syntax and semantics. We then examine how soft where all constraints are satisfied. Sometimes it would be
constraints can be applied to verification, how they can be used to desirable to have the constraint solver disregard certain
manage constraint complexity and used with VIP. Lastly we constraints if (and only if) no solution is possible when they
discuss tool requirements including debug features.
are included.
Keywords-SystemVerilo g, constraint solver, so ft constraint
This requirement is more pronounced when VIP is used, as
I. INTRODUCTION is typical in today's SoC environment. Here the test writer is
often not familiar with all the constraint details of the VIP and
Constrained random verification is the leading
methodology for verification of chip designs today. As this simply wants to override some of the constraints to generate
methodology has grown in popularity, so has the size and an error condition or direct the distribution of specific
variables. Soft constraints allow the VIP creator to specify
complexity of the environments used to verify increasingly
complex System-on-Chip (SoC) designs. One of the rules that can be easily overridden by the test writer. In other
words, soft constraints provide more scope for relaxing these
challenges facing verification engineers is the number of
constraints present in the verification environment, and how rules.
these constraints are reused between projects or can be
leveraged from existing Verification IP (VIP) components. Soft constraints are the constraints that need to be satisfied
if possible; otherwise, they are disregarded. If no solution is
The IEEE P1800 (SystemVerilog) [1] standard provides possible when all the hard and soft constraints are considered,
individual soft constraints are iteratively disregarded based on
two mechanisms to modify constraints – (1) overriding by
defining a constraint block with the same name, and (2) a priority scheme (described in section III.A). Soft is applied
to individual constraint expressions, not the entire constraint
constraint mode control to enable and disable an existing
constraint block. block, providing fine grain control for each constraint
expression.
The IEEE P1647 (e) standard [2] defines soft constraints as
a mechanism to allow constraints to be overridden by other B. Soft Constraint Examples
constraints. A proposal (Mantis 2987 [3]) was recently
approved by the P1800 working group to add soft constrains to Figure 1 shows a simple example of a soft constraint being
the 2012 revision of the SystemVerilog standard. Later in this honored.
paper we will details the similarities and differences between
e and SystemVerilog soft constraint semantics.
class A;
rand int x;
We believe both of these concepts provide complementary constraint A1 {
ways of managing constraints in a large verification soft x == 10 ;
environment. This paper will describe the semantics for soft }
constraints, discuss methodology for using soft constraints endclass
effectively, and investigate some additional SystemVerilog
specific issues. A obj = new ();
obj.randomize() with {x inside {[8:12]}; };
II. SOFT CONS TRAINT => Result: solver generates x == 10
A. What Are Soft Constraints?
Figure 1 – Soft Constraint Honored Example
SystemVerilog and e simulators both use a constraint
solver to create random stimulus. The constraint solver
There is a soft constraint (x == 10) and a hard constraint ( However there is one area where we must extend the
x inside { [8:12] } ). The solver will apply maximum semantics for SystemVerilog. e always uses the file order to
satisfiability on constraints and since the soft constraint does determine priority when multiple soft constraints conflict. This
not cause a constraint conflict in the presence of the hard is not possible in SystemVerilog. While SystemVerilog
constraint (i.e. 10 is inside the range between 8 and 12), the requires a limited file order dependency (base class must be
soft constraint is honored. The solver generates a value of 10 compiled before a derived class), it does allow packages and
for the random variable x. units to be compiled in an arbitrary order to a certain extent.
For this reason, SystemVerilog soft constraints must have a
Figure 2 shows a slightly different example with a dropped well-defined priority scheme for determining which
soft constraint. constraints are disabled if there are conflicts between two or
more expressions.
class A;
rand int x; A. Soft Constraint Priorities
constraint A1 { Soft constraints are assigned priorities and this is an
soft x == 10 ; important concept to understand when and how a soft
} constraint is honored or dropped. Obviously, the hard
endclass
constraints must always be satisfied. If there are two soft
A obj = new (); constraints, SC1 and SC2, and SC2 is a higher priority
obj.randomize() with {x inside {[5:9]};}; constraint than SC1, and if both constraints SC1 and SC2 can
be satisfied, then they will both be honored. Otherwise, if SC2
=> Result: solver generates x==5,6,7,8 or 9 can be satisfied, SC1 will be dropped. If the presence of SC2
will cause a constraint conflict, SC2 will be dropped and SC1
Figure 2 – Soft Constraint Dropped Example be honored, if possible. Otherwise both SC1 and SC2 will be
dropped.
If the constraint expression inside the constraint block
“A::A1” were not declared as soft, the call to randomize Mantis 2987 proposal [3] to SV-EC IEEE 1800 committee
would have failed as there would be no solution that would defines the priorities of soft constraints:
satisfy both x == 10 and x inside the range of [5:9]. To - Constraint expressions that appear later in the same
resolve the solver failure, the user would need to introduce construct (constraint block, class, or struct) have
some additional procedural code to turn off the constraint higher priority.
block “A1” or extend class A to override the constraint block - Constraint expressions in out-of-body constraint
“A1” with additional constraints. Either way, it makes the test blocks whose prototypes appear later in the class have
more complicated. higher priority.
- Constraints in contained objects (rand class handles)
With the constraint expression ( x == 10 ) declared as a have lower priority than all constraints in the container
soft constraint, this soft constraint automatically gets turned object (class or struct).
off by the solver because if it were honored, it would have - Constraints in objects whose handles appear later in
been in conflict with the hard constraint ( x inside { [5:9] } ). the container object have higher priority
The outcome is a simpler test. - Constraints in derived classes have higher priority
than all constraints in their super classes
- Constraints within inline constraint blocks have higher
III. SOFT CONS TRAINT SEMANTICS priority than constraints in the class being randomized.
- Latter iterations within a foreach constraint have
Similar to the soft constraint definition in the e verification higher priority than former iterations
language, SystemVerilog soft constraints are often used to
specify default values and relations that can be disregarded in Consider the following example in Figure 3:
the presence of conflicting hard or other soft constraints.
class M;
rand int x;
constraint a { soft x > 2; soft x < 10; class M;
} rand int x;
endclass constraint a { soft x > 2; soft x < 10; }
endclass
class N extends M;
constraint b; M obj = new();
constraint c { soft x == 5; }
endclass obj.randomize() with { x inside {[0:20]};};
=> Result: x == 3...9
constraint N::b { soft x == 9; }
// disable soft constraints on ‘x’
class Q ; obj.randomize() with {
rand N n; disable soft x;
constraint d { soft n.x inside {[5:8]}; x inside {[0:20]};
} };
endclass => Result: x == 0...20
Q obj = new();
obj.randomize() with { soft n.x >= 7; }; Figure 4 – Disable Soft Example
=> Result: solver generates x = 7 or 8
b) The VIP code typically contains a set of “exercisable ii. Inheritance with the same constraint block name.
space” constraints, which define the stimulus where a
If the constraint block name in the derived class is the same as
predictable response from the DUT is defined. This range
a block name in a base class, then the constraints in the base
will contain many cases where the stimulus violates the
block are replaced by those in the derived, as shown in the
protocol, yet a well-defined error response is produced. error_packet class in Figure 6. To use this strategy to
Using these constraints the testbench will create the legal
accomplish overriding, one must know the name of the
and error stimulus that is possible for the protocol.
original constraint block, and that original block must not
contain other constraints that are not intended to be
c) A set of “legal space” constraints will generate valid
overridden.
stimulus, without any error injection. A default test with
only these constraints would eventually generate all legal class packet;
input stimuli. rand bit [15:0] len;
constraint valid_len {
len inside {[0:1500]};
d) The VIP code also constrains a set of “typical space” }
constraints. These will cause the stimulus to be more endclass
useful and coverage for the protocol or the design will be
hit more quickly using these constraints. class long_packet extends packet;
constraint long_len {
len > 1000; // used with valid_len
e) Variables in the constraint space are often related to other }
variables via implication. This is shown in the diagram endclass;
by a bidirectional arrow linking the two variable range
spaces. class error_packet extends packet;
constraint valid_len { //overrides
The test writer typically defines two sets of constraints at len inside {[1501:1600]};
}
the test-level.
endclass
f) Test specific constraints that further restrict the range of
values generated. These are biases or directives for a Figure 6 – Parent/Child Class Constraints
specific test to increase the chance of hitting a coverage
point or corner case. These are the typical constrained- iii. Procedural calls to constraint_mode()
random tests that are part of a level-1 or level-2 test suite. As constraint blocks are named in SystemVerilog, each block
can be enabled or disabled using procedural code. The test
g) Test specific constraints that change or expand the range writer can simply disable any constraint block explicitly in the
of values generated to some range outside the “typical test, as shown in Figure 7. To use this strategy, one must
space” to hit a corner case. These are typically know the name of the original constraint block and that the
constrained-random tests created during the coverage original block must not contain other constraints that are not
closure process, where corner cases are targeted by adding intended to be overridden, and because constraint_mode() is
constraints. set on an instance, it must be repeated for all instances where
the override is required.
class packet;
rand bit [15:0] len; The requirements for (g) are quite different – here we want
constraint valid_len { to disable one or more VIP constraints to allow the test
len inside {[0:1500]}; constraints to define the space. We are effectively enlarging
} the space in some area above and beyond what was specified
endclass as typical by the VIP developer.
// Test invalid length 1500-1600.
// Disable valid_len cst (0-1500) There are several problems with the existing solutions:
// Gen packets with len 1501-1600
a) The user must determine which constraints need to be
pkt.valid_len.constraint_mode(0); overridden or disabled, which is often not easy to
pkt.randomize() with { calculate or intuitive to code. In many cases the
len inside {[1501:1600]}; constraints dropped will depend on state variables so
}; cannot be calculated statically and coded into the test.
Figure 7 – constraint_mode() b) The user must know the name of the constraint block
in order to override it or disable it. That puts a
burden on documentation, especially if the block is in
iv. Dist to approximate soft constraints. encrypted code.
A distribution constraint with extreme bias will be honored
if possible yet will not cause a conflict even if as few as one of c) If a constraint block is overridden or disabled to
the distribution alternatives overlaps with other constraints. If remove a particular constraint, any other constraints
the desired typical space is given a very large weight and the in that block need to be re-specified. This suggests a
exercisable space is given a very small weight, then the test methodology of always putting constraints in their
writer can in effect override the constraint specified as dist own block if they are known to be subject to being
without disabling or re-defining the constraint block, as shown disabled. Such a methodology can be hard to enforce
in Figure 8. There is, of course, a small chance that the typical and maintain (a later decision that a constraint should
space would not be chosen. be possible to disable means blocks would be
subsequently partitioned).
class packet;
rand bit [15:0] len; d) For the override or disable solutions, even though the
constraint valid_len { original intent may have been to allow the test to not
len dist {[0:1500] := 10000, use the constraint, the test still has to write extra code
[0:1600] := 1}; to make that happen.
};
endclass e) If a conflicting constraint is presented in the “with”
packet pkt = new();
clause of a randomize call, there is no derived class
pkt.randomize(); within which to apply the override.
=> Results: len very likely in 0:1500
f) The solution of using a skewed dist constraint can fail
// overlapping range with a finite probability. Distribution issues can arise
pkt.randomize() with { if a second dist is specified (e.g. in the test) for the
len inside {[1400:1600]}; same variable. The constraint solver will determine
}; some dist variables to relax, however there is no
=> Results: len very likely in 1400:1500
control over the priority and no way to specify which
// conflict range dist variables should be relaxed in the test.
pkt.randomize() with {
len inside {[1501:1600]};
C. Soft Constraint Methodology/Solution
};
=> Results: len in 1501:1600
Soft constraints solve all of the issues listed above when a
Figure 8 – dist Approximating Soft Constraint Example test writer needs to expand the solution space.