CIRCT 23.0.0git
Loading...
Searching...
No Matches
SATSolver.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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 header defines an abstract incremental SAT interface
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef CIRCT_SUPPORT_SATSOLVER_H
14#define CIRCT_SUPPORT_SATSOLVER_H
15
16#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/STLFunctionalExtras.h"
18#include "llvm/ADT/StringRef.h"
19#include <memory>
20
21namespace circt {
22/// Abstract interface for incremental SAT solvers with an IPASIR-style API.
24public:
25 enum Result : int { kSAT = 10, kUNSAT = 20, kUNKNOWN = 0 };
26
27 virtual ~IncrementalSATSolver() = default;
28
29 /// Add one literal to the clause currently under construction. A `0`
30 /// literal terminates the clause and submits it to the solver.
31 virtual void add(int lit) = 0;
32 /// Add an assumption literal for the next `solve()` call only.
33 virtual void assume(int lit) = 0;
34 /// Solve under the previously added clauses and current assumptions.
35 virtual Result solve() = 0;
36 virtual Result solve(llvm::ArrayRef<int> assumptions) {
37 for (int lit : assumptions)
38 assume(lit);
39 return solve();
40 };
41 /// Return the satisfying assignment for variable `v` from the last SAT
42 /// result. The sign of the returned literal encodes the Boolean value.
43 virtual int val(int v) const = 0;
44
45 // These helpers are not part of the standard IPASIR interface.
46 /// Set the per-`solve()` conflict budget. Negative values restore the
47 /// backend default of no explicit conflict limit. The backend may choose to
48 /// ignore this if it does not support conflict limits.
49 virtual void setConflictLimit(int limit) {}
50 /// Reserve storage for variables in the range `[1, maxVar]`.
51 virtual void reserveVars(int maxVar) {}
52 /// Add a complete clause in one call.
53 virtual void addClause(llvm::ArrayRef<int> lits) {
54 for (int lit : lits)
55 add(lit);
56 add(0);
57 }
58 /// Add a fresh variable for safe incremental SAT solving.
59 virtual int newVar() = 0;
60};
61
62/// Emit clauses encoding `outVar <=> and(inputLits)`.
63void addAndClauses(int outVar, llvm::ArrayRef<int> inputLits,
64 llvm::function_ref<void(llvm::ArrayRef<int>)> addClause);
65
66/// Emit clauses encoding `outVar <=> or(inputLits)`.
67void addOrClauses(int outVar, llvm::ArrayRef<int> inputLits,
68 llvm::function_ref<void(llvm::ArrayRef<int>)> addClause);
69
70/// Emit clauses encoding `outVar <=> (lhsLit xor rhsLit)`.
71void addXorClauses(int outVar, int lhsLit, int rhsLit,
72 llvm::function_ref<void(llvm::ArrayRef<int>)> addClause);
73
74/// Emit clauses encoding `outVar <=> parity(inputLits)`.
75void addParityClauses(int outVar, llvm::ArrayRef<int> inputLits,
76 llvm::function_ref<void(llvm::ArrayRef<int>)> addClause,
77 llvm::function_ref<int()> newVar);
78
79/// Emit clauses encoding that at most one literal in `inputLits` can be true.
80/// Unlike the Tseitin-style gate helpers above, this helper does not
81/// take an `outVar`; it only emits the cardinality constraint itself.
83 llvm::ArrayRef<int> inputLits,
84 llvm::function_ref<void(llvm::ArrayRef<int>)> addClause,
85 llvm::function_ref<int()> newVar);
86
87/// Emit clauses encoding that exactly one literal in `inputLits` is true.
89 llvm::ArrayRef<int> inputLits,
90 llvm::function_ref<void(llvm::ArrayRef<int>)> addClause,
91 llvm::function_ref<int()> newVar);
92
93/// Construct a Z3-backed incremental IPASIR-style SAT solver.
94std::unique_ptr<IncrementalSATSolver> createZ3SATSolver();
97 Default, // Default.
98 Plain, // Disable preprocessing.
99 Sat, // Target satisfiable instances.
100 Unsat, // Target unsatisfiable instances.
101 };
103};
104/// Construct a CaDiCaL-backed incremental IPASIR-style SAT solver.
105std::unique_ptr<IncrementalSATSolver>
107/// Return true when at least one incremental SAT backend is available.
109
110/// Construct an incremental SAT solver using the requested backend. The
111/// `auto` backend prefers CaDiCaL and falls back to Z3.
112std::unique_ptr<IncrementalSATSolver>
113createSATSolver(llvm::StringRef backend = "auto");
114
115} // namespace circt
116
117#endif // CIRCT_SUPPORT_SATSOLVER_H
Abstract interface for incremental SAT solvers with an IPASIR-style API.
Definition SATSolver.h:23
virtual Result solve(llvm::ArrayRef< int > assumptions)
Definition SATSolver.h:36
virtual void reserveVars(int maxVar)
Reserve storage for variables in the range [1, maxVar].
Definition SATSolver.h:51
virtual void addClause(llvm::ArrayRef< int > lits)
Add a complete clause in one call.
Definition SATSolver.h:53
virtual ~IncrementalSATSolver()=default
virtual void add(int lit)=0
Add one literal to the clause currently under construction.
virtual void setConflictLimit(int limit)
Set the per-solve() conflict budget.
Definition SATSolver.h:49
virtual int newVar()=0
Add a fresh variable for safe incremental SAT solving.
virtual int val(int v) const =0
Return the satisfying assignment for variable v from the last SAT result.
virtual Result solve()=0
Solve under the previously added clauses and current assumptions.
virtual void assume(int lit)=0
Add an assumption literal for the next solve() call only.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
bool hasIncrementalSATSolverBackend()
Return true when at least one incremental SAT backend is available.
void addExactlyOneClauses(llvm::ArrayRef< int > inputLits, llvm::function_ref< void(llvm::ArrayRef< int >)> addClause, llvm::function_ref< int()> newVar)
Emit clauses encoding that exactly one literal in inputLits is true.
void addAndClauses(int outVar, llvm::ArrayRef< int > inputLits, llvm::function_ref< void(llvm::ArrayRef< int >)> addClause)
Emit clauses encoding outVar <=> and(inputLits).
void addXorClauses(int outVar, int lhsLit, int rhsLit, llvm::function_ref< void(llvm::ArrayRef< int >)> addClause)
Emit clauses encoding outVar <=> (lhsLit xor rhsLit).
std::unique_ptr< IncrementalSATSolver > createZ3SATSolver()
Construct a Z3-backed incremental IPASIR-style SAT solver.
std::unique_ptr< IncrementalSATSolver > createCadicalSATSolver(const CadicalSATSolverOptions &options={})
Construct a CaDiCaL-backed incremental IPASIR-style SAT solver.
void addOrClauses(int outVar, llvm::ArrayRef< int > inputLits, llvm::function_ref< void(llvm::ArrayRef< int >)> addClause)
Emit clauses encoding outVar <=> or(inputLits).
void addParityClauses(int outVar, llvm::ArrayRef< int > inputLits, llvm::function_ref< void(llvm::ArrayRef< int >)> addClause, llvm::function_ref< int()> newVar)
Emit clauses encoding outVar <=> parity(inputLits).
void addAtMostOneClauses(llvm::ArrayRef< int > inputLits, llvm::function_ref< void(llvm::ArrayRef< int >)> addClause, llvm::function_ref< int()> newVar)
Emit clauses encoding that at most one literal in inputLits can be true.
std::unique_ptr< IncrementalSATSolver > createSATSolver(llvm::StringRef backend="auto")
Construct an incremental SAT solver using the requested backend.
CadicalSolverConfig config
Definition SATSolver.h:102