1#ifndef HALIDE_RUNTIME_HASHMAP_H
2#define HALIDE_RUNTIME_HASHMAP_H
15#define hashmap_malloc(user_context, size) halide_malloc(user_context, size)
19#define hashmap_free(user_context, memory) halide_free(user_context, memory)
27 return memcmp(key1, key2, key_size) == 0;
32 for (
size_t i = 0; i < key_size; i++) {
33 h = (h << 5) + h + key[i];
56 const uint8_t *cache_key,
size_t cache_key_size,
58 const uint8_t *cache_value,
size_t cache_value_size,
65 const uint8_t *cache_key,
size_t cache_key_size,
67 const uint8_t *cache_value,
size_t cache_value_size,
77 size_t storage_bytes = 0;
80 storage_bytes += cache_value_size;
83 const size_t alignment = 8;
84 storage_bytes += (alignment - 1);
85 storage_bytes /= alignment;
86 storage_bytes *= alignment;
89 const size_t key_offset = storage_bytes;
103 for (
size_t i = 0; i <
key_size; i++) {
104 key[i] = cache_key[i];
108 copy_value(
value, cache_value, cache_value_size);
159 cache_entry_ref =
nullptr;
176 prune_candidate !=
nullptr) {
185 if (prev_hash_entry == prune_candidate) {
188 while (prev_hash_entry !=
nullptr && prev_hash_entry->
next != prune_candidate) {
189 prev_hash_entry = prev_hash_entry->
next;
192 prev_hash_entry->
next = prune_candidate->
next;
199 if (more_recent !=
nullptr) {
219 prune_candidate = more_recent;
239 uint8_t *cache_value,
size_t cache_value_size) {
246 debug_print_key(
user_context,
"halide_memoization_cache_lookup", cache_key, size);
248 debug_print_buffer(
user_context,
"computed_bounds", *computed_bounds);
251 for (
int32_t i = 0; i < tuple_count; i++) {
253 debug_print_buffer(
user_context,
"Allocation bounds", *buf);
259 while (entry !=
nullptr) {
260 if (entry->
hash == h && entry->
key_size == (
size_t)size &&
301 const uint8_t *cache_value,
size_t cache_value_size) {
302 debug(
user_context) <<
"halide_memoization_cache_store\n";
310 debug_print_key(
user_context,
"halide_memoization_cache_store", cache_key, size);
312 debug_print_buffer(
user_context,
"computed_bounds", *computed_bounds);
315 for (
int32_t i = 0; i < tuple_count; i++) {
317 debug_print_buffer(
user_context,
"Allocation bounds", *buf);
325 entry = entry->
next) {
326 if (entry->hash == h && entry->key_size == (
size_t)size &&
330 copy_value(entry->value, cache_value, entry->value_size);
341 uint64_t added_size = cache_value_size;
361 debug(
user_context) <<
"Exiting halide_memoization_cache_store\n";
365 debug(
user_context) <<
"halide_memoization_cache_release\n";
368 debug(
user_context) <<
"Exited halide_memoization_cache_release.\n";
372 debug(
nullptr) <<
"halide_memoization_cache_cleanup\n";
375 cache_entry_ref =
nullptr;
376 while (entry !=
nullptr) {
389template<
typename KeyType,
typename ValueType>
399 ValueType *D =
reinterpret_cast<ValueType *
>(dst);
400 const ValueType *S =
reinterpret_cast<const ValueType *
>(src);
406 ValueType *V =
reinterpret_cast<ValueType *
>(value);
#define hashmap_malloc(user_context, size)
#define hashmap_free(user_context, memory)
bool keys_equal(const uint8_t *key1, const uint8_t *key2, size_t key_size)
void(* copy_value_func)(uint8_t *dst, const uint8_t *src, size_t size)
uint32_t djb_hash(const uint8_t *key, size_t key_size)
void(* destroy_value_func)(uint8_t *value, size_t size)
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
unsigned __INT64_TYPE__ uint64_t
signed __INT64_TYPE__ int64_t
#define halide_debug_assert(user_context, cond)
halide_debug_assert() is like halide_assert(), but only expands into a check when DEBUG_RUNTIME is de...
signed __INT32_TYPE__ int32_t
unsigned __INT8_TYPE__ uint8_t
int memcmp(const void *s1, const void *s2, size_t n)
void * memset(void *s, int val, size_t n)
unsigned __INT32_TYPE__ uint32_t
bool init(void *user_context, const uint8_t *cache_key, size_t cache_key_size, uint32_t key_hash, const uint8_t *cache_value, size_t cache_value_size, copy_value_func copy_value)
uint8_t * metadata_storage
void destroy(void *user_context, destroy_value_func destroy_value)
void set_size(int64_t size)
destroy_value_func destroy_value
CacheEntry * cache_entries[kHashTableSize]
halide_mutex memoization_lock
CacheEntry * most_recently_used
CacheEntry * least_recently_used
void release(void *user_context, void *host)
int64_t current_cache_size
bool init(void *user_context, copy_value_func copy_value, destroy_value_func destroy_value)
copy_value_func copy_value
static const size_t kHashTableSize
uint64_t kDefaultCacheSize
void store(void *user_context, const uint8_t *cache_key, int32_t size, const uint8_t *cache_value, size_t cache_value_size)
int lookup(void *user_context, const uint8_t *cache_key, int32_t size, uint8_t *cache_value, size_t cache_value_size)
static void destroy_value_func(uint8_t *value, size_t size)
bool init(void *user_context)
static void copy_value_func(uint8_t *dst, const uint8_t *src, size_t size)
int lookup(void *user_context, const uint8_t *cache_key, int32_t key_size, ValueType *cache_value)
void store(void *user_context, const uint8_t *cache_key, int32_t key_size, const ValueType *cache_value)
The raw representation of an image passed around by generated Halide code.