13#ifndef CIRCT_SUPPORT_SATSOLVER_H
14#define CIRCT_SUPPORT_SATSOLVER_H
16#include "llvm/ADT/ArrayRef.h"
32template <
typename T,
typename ScoreFn = HeapScore<T>>
84 unsigned top =
heap.front();
86 if (
heap.size() == 1) {
114 unsigned elem =
heap[index];
115 double elemScore =
score(elem);
117 unsigned parent = (index - 1) / 2;
123 assert(
heap[index] == elem &&
"siftUp must preserve the promoted element");
128 unsigned elem =
heap[index];
129 double elemScore =
score(elem);
130 unsigned heapSize =
heap.size();
132 unsigned child = 2 * index + 1;
133 if (child >= heapSize)
142 assert(
heap[index] == elem &&
"siftDown must preserve the demoted element");
149 llvm::SmallVector<unsigned, 0>
heap;
170 for (
int lit : assumptions)
176 virtual int val(
int v)
const = 0;
205std::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 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.
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.
CadicalSolverConfig config