13#ifndef CIRCT_SUPPORT_SATSOLVER_H
14#define CIRCT_SUPPORT_SATSOLVER_H
16#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/STLFunctionalExtras.h"
18#include "llvm/ADT/SmallVector.h"
34template <
typename T,
typename ScoreFn = HeapScore<T>>
86 unsigned top =
heap.front();
88 if (
heap.size() == 1) {
116 unsigned elem =
heap[index];
117 double elemScore =
score(elem);
119 unsigned parent = (index - 1) / 2;
125 assert(
heap[index] == elem &&
"siftUp must preserve the promoted element");
130 unsigned elem =
heap[index];
131 double elemScore =
score(elem);
132 unsigned heapSize =
heap.size();
134 unsigned child = 2 * index + 1;
135 if (child >= heapSize)
144 assert(
heap[index] == elem &&
"siftDown must preserve the demoted element");
151 llvm::SmallVector<unsigned, 0>
heap;
172 for (
int lit : assumptions)
178 virtual int val(
int v)
const = 0;
199 llvm::function_ref<
void(llvm::ArrayRef<int>)> addClause);
202void addOrClauses(
int outVar, llvm::ArrayRef<int> inputLits,
203 llvm::function_ref<
void(llvm::ArrayRef<int>)> addClause);
207 llvm::function_ref<
void(llvm::ArrayRef<int>)> addClause);
211 llvm::function_ref<
void(llvm::ArrayRef<int>)> addClause,
212 llvm::function_ref<
int()> newVar);
218 llvm::ArrayRef<int> inputLits,
219 llvm::function_ref<
void(llvm::ArrayRef<int>)> addClause,
220 llvm::function_ref<
int()> newVar);
224 llvm::ArrayRef<int> inputLits,
225 llvm::function_ref<
void(llvm::ArrayRef<int>)> addClause,
226 llvm::function_ref<
int()> newVar);
240std::unique_ptr<IncrementalSATSolver>
assert(baseType &&"element must be base type")
Abstract interface for incremental SAT solvers with an IPASIR-style API.
virtual Result solve(llvm::ArrayRef< int > assumptions)
virtual void reserveVars(int maxVar)
Reserve storage for variables in the range [1, maxVar].
virtual void addClause(llvm::ArrayRef< int > lits)
Add a complete clause in one call.
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.
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.
A max-heap of ids into caller-owned storage.
void increase(unsigned id)
Restore heap order after the score for an existing id increased.
static constexpr unsigned kInvalidIndex
llvm::SmallVector< unsigned, 0 > heap
Binary heap of ids ordered by descending score.
double score(unsigned id) const
Compute the ordering score for one heap entry.
void siftDown(unsigned index)
Push one entry toward the leaves until the heap property is restored.
void swapHeapEntries(unsigned lhs, unsigned rhs)
Swap two heap slots and keep the reverse index in sync.
llvm::SmallVectorImpl< T > & storage
The caller-owned objects indexed by heap ids.
IndexedMaxHeap(llvm::SmallVectorImpl< T > &storage)
llvm::SmallVector< unsigned, 0 > positions
Reverse map from id to heap slot, or InvalidIndex if absent.
void insert(unsigned id)
Insert id if it is not already present.
bool contains(unsigned id) const
unsigned pop()
Remove and return the highest-scoring id.
void siftUp(unsigned index)
Bubble one entry toward the root until the heap property is restored.
void clear()
Remove all heap entries while keeping the underlying storage untouched.
void resize(unsigned size)
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.
CadicalSolverConfig config