Skip to content

Commit

Permalink
Reworking expression cache method
Browse files Browse the repository at this point in the history
  • Loading branch information
dim8art authored and S1eGa committed Mar 6, 2023
1 parent 4d057e3 commit 6e7a91f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 34 deletions.
24 changes: 9 additions & 15 deletions include/klee/Expr/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,30 +106,24 @@ class Expr {
}
};

class ExprCacheSet {
public:
typedef std::unordered_set<Expr *, ExprHash, ExprCmp> CacheType;
CacheType cache;
typedef std::unordered_set<Expr *, ExprHash, ExprCmp> CacheType;

struct ExprCacheSet {
CacheType cache;
~ExprCacheSet() {
for (auto &p : cache) {
p->cached = false;
while (cache.size() != 0) {
ref<Expr> tmp = *cache.begin();
tmp->isCached = false;
cache.erase(cache.begin());
}
}
};

static std::unique_ptr<Expr::ExprCacheSet> cachedExpressions;
static ExprCacheSet cachedExpressions;
static ref<Expr> createCachedExpr(const ref<Expr> &e);
bool cached = false;
bool isCached = false;
bool toBeCleared = false;

public:
static void enableCache() {
cachedExpressions =
std::unique_ptr<Expr::ExprCacheSet>(new Expr::ExprCacheSet());
}
bool isCached() const { return cached; }

public:
static unsigned count;
static const unsigned MAGIC_HASH_CONSTANT = 39;
Expand Down
2 changes: 0 additions & 2 deletions lib/Core/Executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,6 @@ Executor::Executor(LLVMContext &ctx, const InterpreterOptions &opts,

initializeSearchOptions();

Expr::enableCache();

if (OnlyOutputStatesCoveringNew && !StatsTracker::useIStats())
klee_error("To use --only-output-states-covering-new, you need to enable "
"--output-istats.");
Expand Down
32 changes: 15 additions & 17 deletions lib/Expr/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ int Expr::compare(const Expr &b, ExprEquivSet &equivs) const {
if (hashValue != b.hashValue)
return (hashValue < b.hashValue) ? -1 : 1;

if (isCached() && b.isCached())
if (isCached && b.isCached)
return (this < &b) ? -1 : 1;

const Expr *ap, *bp;
Expand Down Expand Up @@ -501,30 +501,28 @@ std::string Expr::toString() const {

/***/

std::unique_ptr<Expr::ExprCacheSet> Expr::cachedExpressions;
Expr::ExprCacheSet Expr::cachedExpressions;

Expr::~Expr() {
Expr::count--;
toBeCleared = true;
if (cachedExpressions) {
cachedExpressions->cache.erase(this);
if (isCached) {
toBeCleared = true;
cachedExpressions.cache.erase(this);
}
}

ref<Expr> Expr::createCachedExpr(const ref<Expr> &e) {
if (cachedExpressions) {
std::pair<ExprCacheSet::CacheType::const_iterator, bool> success =
cachedExpressions->cache.insert(e.get());

if (success.second) {
// Cache miss
e->cached = true;
return e;
}
// Cache hit
return (ref<Expr>)*(success.first);

std::pair<CacheType::const_iterator, bool> success =
cachedExpressions.cache.insert(e.get());

if (success.second) {
// Cache miss
e->isCached = true;
return e;
}
return e;
// Cache hit
return (ref<Expr>)*(success.first);
}
/***/

Expand Down

0 comments on commit 6e7a91f

Please sign in to comment.