Engineered SystemVerilog Constraints - WordPress.com · 2018. 12. 4. · Engineered Formula...

26
Engineered SystemVerilog Constraints Jeremy Ridgeway Avago Technologies, Inc.

Transcript of Engineered SystemVerilog Constraints - WordPress.com · 2018. 12. 4. · Engineered Formula...

Page 1: Engineered SystemVerilog Constraints - WordPress.com · 2018. 12. 4. · Engineered Formula •Consider 1st random, len == 0 •Instantiate a new constraint in the formula: len !=

Engineered SystemVerilog

Constraints

Jeremy Ridgeway

Avago Technologies, Inc.

Page 2: Engineered SystemVerilog Constraints - WordPress.com · 2018. 12. 4. · Engineered Formula •Consider 1st random, len == 0 •Instantiate a new constraint in the formula: len !=

Agenda

• Example Constraints

• Constraint Solver

• Engineering Constraints

12/4/2018 Jeremy Ridgeway, Avago Technologies, Inc. 2

Page 3: Engineered SystemVerilog Constraints - WordPress.com · 2018. 12. 4. · Engineered Formula •Consider 1st random, len == 0 •Instantiate a new constraint in the formula: len !=

Example Testbench

• Packet based device

– Start of Packet

– Header fields

– Data to send

– CRC

– End of Packet

• Length 0 – 512 bytes

12/4/2018 Jeremy Ridgeway, Avago Technologies, Inc. 3

ENV DUT pkt

pkt

pkt

sop

HDR DATA CRC

eop

Page 4: Engineered SystemVerilog Constraints - WordPress.com · 2018. 12. 4. · Engineered Formula •Consider 1st random, len == 0 •Instantiate a new constraint in the formula: len !=

Stimulus Packet Class

• Encapsulate the packet

in a class

• Focus only on length

12/4/2018 Jeremy Ridgeway, Avago Technologies, Inc. 4

class base_packet;

rand sop_t sop;

rand header_t hdr;

rand byte data[];

rand int len;

crc_t crc;

rand eop_t eop;

endclass

Page 5: Engineered SystemVerilog Constraints - WordPress.com · 2018. 12. 4. · Engineered Formula •Consider 1st random, len == 0 •Instantiate a new constraint in the formula: len !=

Valid-type Constraint

• Length 0 – 512 bytes

• Use class inheritance to

focus randomization

12/4/2018 Jeremy Ridgeway, Avago Technologies, Inc. 5

class base_packet;

rand byte data[];

rand int len;

constraint valid {

len inside { [0:512] };

endclass

Page 6: Engineered SystemVerilog Constraints - WordPress.com · 2018. 12. 4. · Engineered Formula •Consider 1st random, len == 0 •Instantiate a new constraint in the formula: len !=

Coverage-based Constraints

• Length 0 – 512 bytes

• Determine what values

are important to cover

• Bin important values

• Regress & Rely on

random

12/4/2018 Jeremy Ridgeway, Avago Technologies, Inc. 6

class base_packet;

rand byte data[];

rand int len;

constraint imp {

len inside {

0, [1:511], 512 };

};

covergroup imp_cg;

coverpoint len {

bins a = { 0 };

bins b = { [1:511] };

bins c = { 512 };

}

endgroup

endclass

Page 7: Engineered SystemVerilog Constraints - WordPress.com · 2018. 12. 4. · Engineered Formula •Consider 1st random, len == 0 •Instantiate a new constraint in the formula: len !=

Regress & Rely on Random

• How many tests must execute to hit coverage goals?

– 10? 1,000? 10,000?

• Does the number change between regressions?

– Today 1,321. Tomorrow 1,999?

• Knowing what we need to cover, can we do better?

len inside { 0, [1:511], 512 }

len != 0

12/4/2018 Jeremy Ridgeway, Avago Technologies, Inc. 7

Page 8: Engineered SystemVerilog Constraints - WordPress.com · 2018. 12. 4. · Engineered Formula •Consider 1st random, len == 0 •Instantiate a new constraint in the formula: len !=

Declarative Constraints

• Cannot generate constraints on-the-fly:

constraint c { len inside { 0, [1:511], 512 }; }

constraint d { len != 0; }

• d can not be instantiated on-the-fly .

• d can be enabled on-the-fly:

12/4/2018 Jeremy Ridgeway, Avago Technologies, Inc. 8

task update_constraints();

if(len == 0) d.constraint_mode(1);

endtask

kind of …

Page 9: Engineered SystemVerilog Constraints - WordPress.com · 2018. 12. 4. · Engineered Formula •Consider 1st random, len == 0 •Instantiate a new constraint in the formula: len !=

Agenda

• Example Constraints

• Constraint Solver

• Engineering Constraints

12/4/2018 Jeremy Ridgeway, Avago Technologies, Inc. 9

Page 10: Engineered SystemVerilog Constraints - WordPress.com · 2018. 12. 4. · Engineered Formula •Consider 1st random, len == 0 •Instantiate a new constraint in the formula: len !=

Constraint Formula

constraint c { len inside { 0, [1:511], 512 }; }

• Convert to Boolean:

(len == 0) || (len >= 1 && len <= 511) || (len == 512)

• Identify Unique Components:

– Variable: len

– Predicate: a Boolean quantity (arg1 op arg2)

– Literal: a predicate or its negation

– Clause: logical OR of literals

– Formula (in CNF): logical AND of clauses 12/4/2018 Jeremy Ridgeway, Avago Technologies, Inc. 10

Conjunctive Normal Form:

Logial AND of all Logical ORs

(len <= 511) == !(len > 511)

Predicate: p == (len > 511)

(Negative) Literal: !p

Page 11: Engineered SystemVerilog Constraints - WordPress.com · 2018. 12. 4. · Engineered Formula •Consider 1st random, len == 0 •Instantiate a new constraint in the formula: len !=

Conversion to CNF

• Original Boolean Formula

(len == 0) || (len >= 1 && len <= 511) || (len == 512)

• Distribute predicates over the logical AND:

(len == 0) || (len >= 1) || (len == 512) &&

(len == 0) || (len <= 511) || (len == 512)

• For this presentation we will use the

Original Boolean Formula

12/4/2018 Jeremy Ridgeway, Avago Technologies, Inc. 11

Page 12: Engineered SystemVerilog Constraints - WordPress.com · 2018. 12. 4. · Engineered Formula •Consider 1st random, len == 0 •Instantiate a new constraint in the formula: len !=

Constraint Solver

• Determines if a formula is satisfiable

• Returns an assignment on variables otherwise

• Layered and incremental solver

12/4/2018 Jeremy Ridgeway, Avago Technologies, Inc. 12

sat == true

unsat

Boolean

Linear Arithmetic Equality and

Uninterpreted Functions

Bit-vectors Theory

SAT

( (len == 0) || (len >= 1 && len <= 511) || (len == 512) ) ↔ false

Page 13: Engineered SystemVerilog Constraints - WordPress.com · 2018. 12. 4. · Engineered Formula •Consider 1st random, len == 0 •Instantiate a new constraint in the formula: len !=

Incremental Solving

(len == 0) || (len >= 1 && len <= 511) || (len == 512)

A || (B && C) || D

• SAT Solver assignment: A==1, D==1

– len cannot be both 0 and 512

– Must be rectified by theory solver

12/4/2018 Jeremy Ridgeway, Avago Technologies, Inc. 13

A B C D

Boolean

LA EUF BV Theory

SAT

a b c d

Page 14: Engineered SystemVerilog Constraints - WordPress.com · 2018. 12. 4. · Engineered Formula •Consider 1st random, len == 0 •Instantiate a new constraint in the formula: len !=

T(LA) Adds Clauses

(len == 0) || (len >= 1 && len <= 511) || (len == 512)

A || (B && C) || D

• Instantiate new constraints:

A ↔ !D

• Simplify to CNF Clauses:

(!A || !D) && (D || A)

• Add to the formula and solve again

12/4/2018 Jeremy Ridgeway, Avago Technologies, Inc. 14

Page 15: Engineered SystemVerilog Constraints - WordPress.com · 2018. 12. 4. · Engineered Formula •Consider 1st random, len == 0 •Instantiate a new constraint in the formula: len !=

Backtrack and Solve Again

((len == 0) || (len >= 1 && len <= 511) || (len == 512))

&&

!(len == 0) || !(len == 512) && (len == 0) || (len == 512)

• Re-used predicates and literals

(A || (B && C) || D) && (!A || !D) && (A || D)

• Formula cannot satisfy when A == 1, D == 1

• Let’s stop right here!

12/4/2018 Jeremy Ridgeway, Avago Technologies, Inc. 15

A B C D

!A !D A D

Page 16: Engineered SystemVerilog Constraints - WordPress.com · 2018. 12. 4. · Engineered Formula •Consider 1st random, len == 0 •Instantiate a new constraint in the formula: len !=

Agenda

• Example Constraints

• Constraint Solver

• Engineering Constraints

12/4/2018 Jeremy Ridgeway, Avago Technologies, Inc. 16

Page 17: Engineered SystemVerilog Constraints - WordPress.com · 2018. 12. 4. · Engineered Formula •Consider 1st random, len == 0 •Instantiate a new constraint in the formula: len !=

Return to the Ideal

• Want to generate constraint d on-the-fly:

constraint c { len inside { 0, [1:511], 512 }; }

constraint d { len != 0; }

• Represent constraint as CNF formula

– Clauses

• Positive or Negative Literals

– Predicates

• Decompose constraint into predicates and literals

12/4/2018 Jeremy Ridgeway, Avago Technologies, Inc. 17

Assume everything is

an int data type

Page 18: Engineered SystemVerilog Constraints - WordPress.com · 2018. 12. 4. · Engineered Formula •Consider 1st random, len == 0 •Instantiate a new constraint in the formula: len !=

Engineered Predicates

• Boolean Quantity

• Result flag

• Unique instances in

formula

• Extend to implement

quantity type

12/4/2018 Jeremy Ridgeway, Avago Technologies, Inc. 18

class constraint_base;

class constraint_pred;

rand bit result;

endclass

class pred_equality;

constraint valid {

if(result) lhs.val == rhs.val;

else lhs.val != rhs.val;

}

endclass

Assume there’s

a constraint value

container class

class constraint_binary_op;

rand constraint_val lhs;

rand constraint_val rhs;

endclass

len == 0

Page 19: Engineered SystemVerilog Constraints - WordPress.com · 2018. 12. 4. · Engineered Formula •Consider 1st random, len == 0 •Instantiate a new constraint in the formula: len !=

Engineered Literals

• Result flag

• Negation flag

• Predicate

• At most 2 literals need

be instantiated for

a predicate

12/4/2018 Jeremy Ridgeway, Avago Technologies, Inc. 19

class constraint_pred;

rand bit result;

endclass

class constraint_literal;

const bit neg;

rand constraint_pred pred;

constraint valid {

result -> pred.result == !neg;

}

endclass

class constraint_base;

Implication allows

literals to be ignored

(len <= 511) == !(len > 511)

Predicate: p == (len > 511)

(Negative) Literal: !p

Page 20: Engineered SystemVerilog Constraints - WordPress.com · 2018. 12. 4. · Engineered Formula •Consider 1st random, len == 0 •Instantiate a new constraint in the formula: len !=

Engineered Clauses

• Queue of Literals

• Array reduction

implements the

logical OR

12/4/2018 Jeremy Ridgeway, Avago Technologies, Inc. 20

class constraint_pred;

rand bit result;

endclass

class constraint_clause;

rand constraint_literal lit_or[$];

constraint valid {

result -> lit_or.or() with

(member.result == 1);

}

endclass

class constraint_base;

A || (B && C) || D

Page 21: Engineered SystemVerilog Constraints - WordPress.com · 2018. 12. 4. · Engineered Formula •Consider 1st random, len == 0 •Instantiate a new constraint in the formula: len !=

Engineered Formula

• Queue of Clauses

• Array reduction

implements the

logical AND

12/4/2018 Jeremy Ridgeway, Avago Technologies, Inc. 21

class constraint_pred;

rand bit result;

endclass

class constraint_formula;

rand constraint_clause cls_and[$];

constraint valid {

result -> cls_and.and() with

(member.result == 1);

}

endclass

class constraint_base;

(A || (B && C) || D)

&&

(!A || !D)

&&

(A || D)

Page 22: Engineered SystemVerilog Constraints - WordPress.com · 2018. 12. 4. · Engineered Formula •Consider 1st random, len == 0 •Instantiate a new constraint in the formula: len !=

Engineered Values

• Contain a single value

• Can nest constraints

12/4/2018 Jeremy Ridgeway, Avago Technologies, Inc. 22

Refer to the paper!

Type Example

Value len

Inside len inside {0, [1:511], 512}

Distribution len dist { 0 := 10, [1:511] := 20, 512 := 10 }

Sequence len seq [ 0, 512, dist {0 := 10, [1:512] := 50} ]

Constant 512

Page 23: Engineered SystemVerilog Constraints - WordPress.com · 2018. 12. 4. · Engineered Formula •Consider 1st random, len == 0 •Instantiate a new constraint in the formula: len !=

Engineered Formula

• Consider 1st random, len == 0

• Instantiate a new constraint in the formula: len != 0

12/4/2018 Jeremy Ridgeway, Avago Technologies, Inc. 23

Formula

Clause Inside Literal

neg = 0

Const 0

Range Min Max

Const 512

Const 1

Const 511

constraint c { len inside { 0, [1:511], 512 }; }

Clause Literal

neg = 1

Equality lhs rhs

Page 24: Engineered SystemVerilog Constraints - WordPress.com · 2018. 12. 4. · Engineered Formula •Consider 1st random, len == 0 •Instantiate a new constraint in the formula: len !=

Engineered Random Variable

• Random value

• Constrained value

• Formula

• Constraint

Factory

12/4/2018 Jeremy Ridgeway, Avago Technologies, Inc. 24

class loc_rand_param#(T);

class loc_rand_static#(T);

rand T value;

constraint_pred#(T) val;

constraint_pred#(T) formula;

static constraint_factory#(T) factory;

constraint valid {

solve formula.result before value;

formula.result == 1;

value == val.value; }

endclass

class loc_rand_base;

(Almost) all classes are

type-parameterized class loc_rand#(T);

Page 25: Engineered SystemVerilog Constraints - WordPress.com · 2018. 12. 4. · Engineered Formula •Consider 1st random, len == 0 •Instantiate a new constraint in the formula: len !=

Agenda

• Example Constraints

• Constraint Solver

• Engineering Constraints

12/4/2018 Jeremy Ridgeway, Avago Technologies, Inc. 25

Page 26: Engineered SystemVerilog Constraints - WordPress.com · 2018. 12. 4. · Engineered Formula •Consider 1st random, len == 0 •Instantiate a new constraint in the formula: len !=

Thank You!

Questions?

12/4/2018 Jeremy Ridgeway, Avago Technologies, Inc. 26