Constant vs Variable in Constraint
Context
The comparison between using a constant versus a variable inside a SystemVerilog constraint is examined as OOP optimization 2 in the paper Performance of a SystemVerilog Sudoku Solver with VCS. The motivating question (Question 3 in the paper) is: Are constants more efficient than variables? [1][2]
The Problem With Variables in Constraints
In the baseline OOP test bench, the range constraint in the scell class sets the maximum range using a class variable:
val >= 0 && val <= max
Because Sudoku and Latin-square rules dictate that the maximum value cannot change for a single game board, this value is in practice invariant. However, depending on how the solver operates, each time the clause val <= max is evaluated, a memory access may be required on max. [2]
SystemVerilog Options for Constants
SystemVerilog provides two mechanisms for expressing this constant value inside a constraint:
- Constant class variable — declared with the
constkeyword, e.g.const int max. This form is compared against the Basic OOP test bench in Optimization 2 (constant) and is illustrated in Fig. 5 of the paper. [1][2] - Compile-time macro / parameter — used to force the immediate-type encoding by writing the bound as a macro, e.g.
val >= 0 && val <= `NUM_ROWS. This form is Optimization 2 (parameter) and is illustrated in Fig. 6. [1]
The const Keyword Caveat
The paper notes that the const keyword may or may not imply an immediate-type CPU instruction (one where the value is encoded directly within the instruction). Whether it does is simulator-dependent, so using const does not guarantee an immediate-type access. [1]
If an immediate-type encoding is desired, the paper recommends forcing it explicitly with a compile-time macro (e.g., `NUM_ROWS) rather than relying on const. [1]
Performance Comparison in VCS
The three storage types evaluated in the constraints are:
- Local variable
constclass variable (Fig. 5)- Parameter (Fig. 6)
These are compared in Table IV of the paper. While Fig. 6 shows that the parameter variant varies wildly on larger board sizes, comparing the metrics for Optimization 2 across both variants shows that the overall performance is very similar. The paper's conclusion is that the VCS constraint solver is about as efficient in either variable, constant, or parameter accesses in constraints. [1][2]
Relationship to Other Optimizations
The constant-vs-variable study sits alongside other OOP optimizations in the paper:
- Optimization 1:
if-elsevs implication — showed very little difference in solving time. [1][2] - Optimization 3: reduction of array duplicates — compared across all three storage types (local variable, const variable, parameter); results reported in Table IV. [1]
- Flat Game Board (FLAT): a separate theoretical exercise with a flat constant game board. [1]
Taken together, all OOP optimizations proved largely ineffectual for VCS. [1]
Key Takeaways
- Using a
constclass member or a parameter inside a constraint is conceptually equivalent to using a regular variable for the purposes of VCS solving performance. - Do not assume
constwill produce an immediate-type CPU instruction; this behavior is simulator-dependent. - To guarantee an immediate-type access in constraints, prefer a compile-time macro (
`NUM_ROWS) over relying on theconstqualifier.