This repository has been archived by the owner on Aug 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ChiselEnum: implement peek and better error message (#542)
- Loading branch information
Showing
14 changed files
with
441 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,40 +132,47 @@ jobs: | |
|
||
formal: | ||
name: formal verification tests | ||
runs-on: ${{ matrix.os }} | ||
runs-on: ubuntu-20.04 | ||
strategy: | ||
matrix: | ||
os: [ubuntu-20.04, macos-latest] | ||
backend: [z3, cvc4, btormc] | ||
backend: [z3, cvc4, btormc, yices2, boolector, bitwuzla] | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
- name: Install Z3 and CVC4 for Ubuntu | ||
- name: Install Z3 and CVC4 | ||
if: runner.os == 'Linux' | ||
run: | | ||
sudo apt-get install -y z3 cvc4 | ||
z3 --version | ||
cvc4 --version | ||
- name: Install Z3 and CVC4 for MacOS | ||
if: runner.os == 'macOS' | ||
run: | | ||
brew install z3 | ||
brew tap cvc4/cvc4 | ||
brew install cvc4/cvc4/cvc4 | ||
z3 --version | ||
cvc4 --version | ||
- name: Install Tabby OSS Cad Suite (from YosysHQ) | ||
if: matrix.backend == 'btormc' | ||
uses: YosysHQ/setup-oss-cad-suite@v1 | ||
with: | ||
osscadsuite-version: '2021-11-09' | ||
osscadsuite-version: '2022-08-02' | ||
- name: Setup Scala | ||
uses: olafurpg/setup-scala@v10 | ||
with: | ||
java-version: [email protected] | ||
- name: Test | ||
run: sbt "testOnly -- -n Formal -Dformal_engine=${{ matrix.backend }}" | ||
|
||
formal-mac: | ||
name: formal verification tests on mac | ||
runs-on: macos-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
- name: Install Z3 for MacOS | ||
run: | | ||
brew install z3 | ||
z3 --version | ||
- name: Setup Scala | ||
uses: olafurpg/setup-scala@v10 | ||
with: | ||
java-version: [email protected] | ||
- name: Test | ||
run: sbt "testOnly -- -n Formal -Dformal_engine=z3" | ||
|
||
doc: | ||
name: Documentation and Formatting | ||
runs-on: ubuntu-20.04 | ||
|
@@ -186,7 +193,7 @@ jobs: | |
# When adding new jobs, please add them to `needs` below | ||
all_tests_passed: | ||
name: "all tests passed" | ||
needs: [test, doc, verilator, verilator-mac, formal, icarus, test-mac] | ||
needs: [test, doc, verilator, verilator-mac, formal, formal-mac, icarus, test-mac] | ||
runs-on: ubuntu-latest | ||
steps: | ||
- run: echo Success! | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package chisel3.internaltest | ||
|
||
import chisel3.experimental.EnumType | ||
|
||
/** Helper functions to allow for peeks and better debugging of ChiselEnums. | ||
* This needs to be in a `chisel3` package in order to access the package private | ||
* `factory` field of EnumType. | ||
*/ | ||
object EnumHelpers { | ||
def fromBits[T <: EnumType](tpe: T, bits: BigInt): T = { | ||
val all = tpe.factory.all | ||
all.find(_.litValue == bits).get.asInstanceOf[T] | ||
} | ||
def valueToName(tpe: EnumType, bits: BigInt): Option[String] = { | ||
val name = tpe.factory.nameOfValue(bits) | ||
val tpeName = tpe.factory.enumTypeName | ||
name.map(n => s"$tpeName.$n") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/scala/chiseltest/formal/backends/smt/CompactSmtEncoding.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Author: Kevin Laeufer <[email protected]> | ||
|
||
package chiseltest.formal.backends.smt | ||
|
||
import firrtl.backends.experimental.smt._ | ||
import scala.collection.mutable | ||
|
||
class CompactSmtEncoding(sys: TransitionSystem) extends TransitionSystemSmtEncoding { | ||
import SMTTransitionSystemEncoder._ | ||
private def id(s: String): String = SMTLibSerializer.escapeIdentifier(s) | ||
private val stateType = id(sys.name + "_s") | ||
private val stateInitFun = id(sys.name + "_i") | ||
private val stateTransitionFun = id(sys.name + "_t") | ||
|
||
private val states = mutable.ArrayBuffer[UTSymbol]() | ||
|
||
def defineHeader(ctx: SolverContext): Unit = encode(sys).foreach(ctx.runCommand) | ||
|
||
private def appendState(ctx: SolverContext): UTSymbol = { | ||
val s = UTSymbol(s"s${states.length}", stateType) | ||
ctx.runCommand(DeclareUninterpretedSymbol(s.name, s.tpe)) | ||
states.append(s) | ||
s | ||
} | ||
|
||
def init(ctx: SolverContext): Unit = { | ||
assert(states.isEmpty) | ||
val s0 = appendState(ctx) | ||
ctx.assert(BVFunctionCall(stateInitFun, List(s0), 1)) | ||
} | ||
|
||
def unroll(ctx: SolverContext): Unit = { | ||
assert(states.nonEmpty) | ||
appendState(ctx) | ||
val tStates = states.takeRight(2).toList | ||
ctx.assert(BVFunctionCall(stateTransitionFun, tStates, 1)) | ||
} | ||
|
||
/** returns an expression representing the constraint in the current state */ | ||
def getConstraint(name: String): BVExpr = { | ||
assert(states.nonEmpty) | ||
val foo = id(name + "_f") | ||
BVFunctionCall(foo, List(states.last), 1) | ||
} | ||
|
||
/** returns an expression representing the assertion in the current state */ | ||
def getAssertion(name: String): BVExpr = { | ||
assert(states.nonEmpty) | ||
val foo = id(name + "_f") | ||
BVFunctionCall(foo, List(states.last), 1) | ||
} | ||
|
||
def getSignalAt(sym: BVSymbol, k: Int): BVExpr = { | ||
assert(states.length > k, s"no state s$k") | ||
val state = states(k) | ||
val foo = id(sym.name + "_f") | ||
BVFunctionCall(foo, List(state), sym.width) | ||
} | ||
|
||
def getSignalAt(sym: ArraySymbol, k: Int): ArrayExpr = { | ||
assert(states.length > k, s"no state s$k") | ||
val state = states(k) | ||
val foo = id(sym.name + "_f") | ||
ArrayFunctionCall(foo, List(state), sym.indexWidth, sym.dataWidth) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.