CIRCT  18.0.0git
Solver.h
Go to the documentation of this file.
1 //===-- Solver.h - SMT solver interface -------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// This file defines a SMT solver interface for the `circt-lec` tool.
10 ///
11 //===----------------------------------------------------------------------===//
12 
13 // NOLINTNEXTLINE
14 #ifndef TOOLS_CIRCT_LEC_SOLVER_H
15 #define TOOLS_CIRCT_LEC_SOLVER_H
16 
17 #include "circt/Support/LLVM.h"
18 #include "mlir/IR/MLIRContext.h"
19 #include "mlir/IR/Value.h"
20 #include <z3++.h>
21 
22 namespace circt {
23 
24 /// A satisfiability checker for circuit equivalence
25 ///
26 /// This class interfaces with an external SMT solver acting as a logical
27 /// engine. First spawn two circuits through `addCircuit`; after collecting
28 /// their logical constraints, the `solve` method will compare them and report
29 /// whether they result to be equivalent or, when not, also printing a model
30 /// acting as a counterexample.
31 class Solver {
32 public:
33  Solver(mlir::MLIRContext *mlirCtx, bool statisticsOpt);
34  ~Solver() = default;
35 
36  /// Solve the equivalence problem between the two circuits, then present the
37  /// results to the user.
38  mlir::LogicalResult solve();
39 
40  class Circuit;
41  /// Create a new circuit to be compared and return it.
42  Circuit *addCircuit(llvm::StringRef name);
43 
44 private:
45  /// Prints a model satisfying the solved constraints.
46  void printModel();
47  /// Prints the constraints which were added to the solver.
48  /// Compared to solver.assertions().to_string() this method exposes each
49  /// assertion as a z3::expression for eventual in-depth debugging.
50  void printAssertions();
51  /// Prints the internal statistics of the SMT solver for benchmarking purposes
52  /// and operational insight.
53  void printStatistics();
54 
55  /// Formulates additional constraints which are satisfiable if only if the
56  /// two circuits which are being compared are NOT equivalent, in which case
57  /// there would be a model acting as a counterexample.
58  /// The procedure fails when detecting a mismatch of arity or type between
59  /// the inputs and outputs of the circuits.
60  mlir::LogicalResult constrainCircuits();
61 
62  /// A map from internal solver symbols to the IR values they represent.
63  llvm::DenseMap<mlir::StringAttr, mlir::Value> symbolTable;
64  /// The two circuits to be compared.
66  /// The MLIR context of reference, owning all the MLIR entities.
67  mlir::MLIRContext *mlirCtx;
68  /// The Z3 context of reference, owning all the declared values, constants
69  /// and expressions.
70  z3::context context;
71  /// The Z3 solver acting as the logical engine backend.
72  z3::solver solver;
73  /// The value of the `statistics` command-line option.
75 };
76 
77 } // namespace circt
78 
79 #endif // TOOLS_CIRCT_LEC_SOLVER_H
The representation of a circuit within a logical engine.
Definition: Circuit.h:35
A satisfiability checker for circuit equivalence.
Definition: Solver.h:31
mlir::LogicalResult constrainCircuits()
Formulates additional constraints which are satisfiable if only if the two circuits which are being c...
Definition: Solver.cpp:139
void printAssertions()
Prints the constraints which were added to the solver.
Definition: Solver.cpp:115
Circuit * circuits[2]
The two circuits to be compared.
Definition: Solver.h:65
z3::context context
The Z3 context of reference, owning all the declared values, constants and expressions.
Definition: Solver.h:70
void printModel()
Prints a model satisfying the solved constraints.
Definition: Solver.cpp:79
llvm::DenseMap< mlir::StringAttr, mlir::Value > symbolTable
A map from internal solver symbols to the IR values they represent.
Definition: Solver.h:63
mlir::LogicalResult solve()
Solve the equivalence problem between the two circuits, then present the results to the user.
Definition: Solver.cpp:32
Circuit * addCircuit(llvm::StringRef name)
Create a new circuit to be compared and return it.
Definition: Solver.cpp:65
mlir::MLIRContext * mlirCtx
The MLIR context of reference, owning all the MLIR entities.
Definition: Solver.h:67
bool statisticsOpt
The value of the statistics command-line option.
Definition: Solver.h:74
void printStatistics()
Prints the internal statistics of the SMT solver for benchmarking purposes and operational insight.
Definition: Solver.cpp:125
~Solver()=default
Solver(mlir::MLIRContext *mlirCtx, bool statisticsOpt)
Definition: Solver.cpp:26
z3::solver solver
The Z3 solver acting as the logical engine backend.
Definition: Solver.h:72
This file defines an intermediate representation for circuits acting as an abstraction for constraint...
Definition: DebugAnalysis.h:21