New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] constrained randomization with popen and external solvers #4947
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we'll need to integrate a solver directly eventually because:
- It will be faster
- It'll consume less effort than supporting multiple solvers
- We'll find bugs in the solver we need to fix, and the fixes couple with Verilator
However I agree this decoupled approach is an acceptable way to make progress in the meantime.
It's not clear from the patch when the solver is needed. We should try to write internal code to remove whatever we can from the solver and continue to skip as needed - I think pretty much every variable that has "easy" constraints without coupling to other variables should be able to be done by Verilator without a solver. (And even after we have a real integrated solver to help runtime we should simplify what we can at compile time.)
As to the patch itself:
-
The current patch is spawning a solver process for every solution. This is untenable from a performance standpoint. Please run it with --in, and feed commands to the "same" session as needed - perhaps you can also load up definitions at init time to make the interactive part faster. The pipe-filter option in Verilator is example code of how to do something similar.
-
What that's currently in verilated_random can instead be moved to compile time? (In addition to what can be done at z3 startup per above)
-
Need some internals.rst documentation on the sort of commands we send to/from the solver.
-
Check for missing const's.
This will help introducing constrained randomization, as SMT solvers treat booleans and 1-bit vectors as incompatible types. Split from verilator#4947 Signed-off-by: Arkadiusz Kozdra <akozdra@antmicro.com>
This will help introducing constrained randomization, as SMT solvers treat booleans and 1-bit vectors as incompatible types. Split from verilator#4947 Signed-off-by: Arkadiusz Kozdra <akozdra@antmicro.com>
13cc623
to
23dbea2
Compare
33c5684
to
11295ae
Compare
261847c
to
df5969b
Compare
Signed-off-by: Ryszard Rozak <rrozak@antmicro.com>
Signed-off-by: Ryszard Rozak <rrozak@antmicro.com>
Signed-off-by: Arkadiusz Kozdra <akozdra@antmicro.com>
Signed-off-by: Arkadiusz Kozdra <akozdra@antmicro.com>
Signed-off-by: Ryszard Rozak <rrozak@antmicro.com>
The extra constraints force xors of random bits of variables to random values. Signed-off-by: Arkadiusz Kozdra <akozdra@antmicro.com>
Signed-off-by: Arkadiusz Kozdra <akozdra@antmicro.com>
Signed-off-by: Arkadiusz Kozdra <akozdra@antmicro.com>
Signed-off-by: Arkadiusz Kozdra <akozdra@antmicro.com>
Signed-off-by: Arkadiusz Kozdra <akozdra@antmicro.com>
Signed-off-by: Arkadiusz Kozdra <akozdra@antmicro.com>
Signed-off-by: Arkadiusz Kozdra <akozdra@antmicro.com>
Signed-off-by: Arkadiusz Kozdra <akozdra@antmicro.com>
Signed-off-by: Arkadiusz Kozdra <akozdra@antmicro.com>
Signed-off-by: Arkadiusz Kozdra <akozdra@antmicro.com>
Signed-off-by: Arkadiusz Kozdra <akozdra@antmicro.com>
Signed-off-by: Arkadiusz Kozdra <akozdra@antmicro.com>
Signed-off-by: Arkadiusz Kozdra <akozdra@antmicro.com>
Signed-off-by: Arkadiusz Kozdra <akozdra@antmicro.com>
Signed-off-by: Arkadiusz Kozdra <akozdra@antmicro.com>
df5969b
to
240ee9a
Compare
Verilator currently has support for basic randomization, but the constraints are ignored. Our previous approach involved an exteral library called CRAVE, which was versatile, but quite heavyweight for our limited needs, not yet packaged on any major distro and had a quite cumbersome build process (multiple nested dependencies). The high-level overview of the changes is that the constraints get expressed in SMT-LIB2 format (textual Lisp), written at runtime into a temporary file, and then solvers like z3, cvc4 or cvc5 are popen()ed and their output parsed.
The advantage of the approach over using the API of an external library are as follows:
(this is effectively using a well-defined external batch processing interface),
obj.randomize()
warns and returns 0,doing no randomization, but it could equally easily randomize without looking at the constraints)
instead of hindering the whole compilation process.
Additionally, satisfiability modulo theories (SMT) solvers are typically faster than binary decision diagram (BDD) solvers, which could be considered as an alternative approach.
One nice example class that illustrates how the generated expression looks like:
The class gets a method that could be expressed in a pseudo-SystemVerilog:
The main challenge was that the solvers are deterministic and do not typically have many possibilities to influence the results. The randomization is therefore achieved by adding an extra artificial constraint, choosing random bits of the variables being randomized, and forcing their xor to a random value, inspired by our understanding of CRAVE's randomization mechanisms. This forces an extra random narrowing of solution space, and hopefully introduces enough noise to the solver's internal state that the solutions start behaving more random.
Things done here:
Things still to consider:
solve X before Y
(does not change the solution space itself, only the probability distribution)obj.randomize() with {...}
(still unsupported)The changes are split into several commits for easier review.
Related issues: #2879 #3901