Skip to content

Commit

Permalink
Add clang
Browse files Browse the repository at this point in the history
  • Loading branch information
Yen-Fu Chen committed Sep 12, 2023
1 parent a463dd4 commit 187ccce
Show file tree
Hide file tree
Showing 9 changed files with 382 additions and 27 deletions.
105 changes: 104 additions & 1 deletion src/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>

#include "cache.h"
#include "mpool.h"
Expand All @@ -22,10 +23,14 @@
*/
#define THRESHOLD 32768


#if RV32_HAS(JIT)
#ifndef MIR
#define CODE_CACHE_SIZE (64 * 1024 * 1024)
#define sys_icache_invalidate(addr, size) \
__builtin___clear_cache((char *) (addr), (char *) (addr) + (size));
#endif
#endif

static uint32_t cache_size, cache_size_bits;
static struct mpool *cache_mp;
Expand Down Expand Up @@ -75,6 +80,7 @@ typedef struct {
uint32_t key;
uint32_t frequency;
cache_list_t type;
uint64_t offset;
struct list_head list;
struct hlist_node ht_list;
} arc_entry_t;
Expand All @@ -83,6 +89,7 @@ typedef struct {
void *value;
uint32_t key;
uint32_t frequency;
uint64_t offset;
struct list_head list;
struct hlist_node ht_list;
} lfu_entry_t;
Expand All @@ -103,6 +110,12 @@ typedef struct cache {
#endif
hashtable_t *map;
uint32_t capacity;
#if RV32_HAS(JIT)
#ifndef MIR
uint8_t *jitcode;
uint64_t offset;
#endif
#endif
} cache_t;

static inline void INIT_LIST_HEAD(struct list_head *head)
Expand Down Expand Up @@ -294,6 +307,14 @@ cache_t *cache_create(int size_bits)
mpool_create(cache_size * sizeof(lfu_entry_t), sizeof(lfu_entry_t));
#endif
cache->capacity = cache_size;
#if RV32_HAS(JIT)
#ifndef MIR
cache->jitcode = (uint8_t *) mmap(NULL, CODE_CACHE_SIZE,
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
cache->offset = 0;
#endif
#endif
return cache;
}

Expand Down Expand Up @@ -603,10 +624,92 @@ bool cache_hot(struct cache *cache, uint32_t key)
lfu_entry_t)
#endif
{
if (entry->key == key && entry->frequency == THRESHOLD)
if (entry->key == key && entry->frequency == THRESHOLD - 1)
return true;
}
#endif
return false;
}

#ifndef MIR
uint8_t *code_cache_lookup(cache_t *cache, uint32_t key)
{
if (!cache->capacity || hlist_empty(&cache->map->ht_list_head[HASH(key)]))
return NULL;
#if RV32_HAS(ARC)
arc_entry_t *entry = NULL;
#ifdef __HAVE_TYPEOF
hlist_for_each_entry (entry, &cache->map->ht_list_head[HASH(key)], ht_list)
#else
hlist_for_each_entry (entry, &cache->map->ht_list_head[HASH(key)], ht_list,
arc_entry_t)
#endif
{
if (entry->key == key)
return cache->jitcode + entry->offset;
}
#else
lfu_entry_t *entry = NULL;
#ifdef __HAVE_TYPEOF
hlist_for_each_entry (entry, &cache->map->ht_list_head[HASH(key)], ht_list)
#else
hlist_for_each_entry (entry, &cache->map->ht_list_head[HASH(key)], ht_list,
lfu_entry_t)
#endif
{
if (entry->key == key)
return cache->jitcode + entry->offset;
}
#endif
return NULL;
}

static inline uint64_t align_to(uint64_t val, uint64_t align)
{
if (!align)
return val;
return (val + align - 1) & ~(align - 1);
}

uint8_t *code_cache_add(cache_t *cache,
uint64_t key,
uint8_t *code,
size_t sz,
uint64_t align)
{
cache->offset = align_to(cache->offset, align);
if (!cache->capacity || hlist_empty(&cache->map->ht_list_head[HASH(key)]))
return NULL;
#if RV32_HAS(ARC)
arc_entry_t *entry = NULL;
#ifdef __HAVE_TYPEOF
hlist_for_each_entry (entry, &cache->map->ht_list_head[HASH(key)], ht_list)
#else
hlist_for_each_entry (entry, &cache->map->ht_list_head[HASH(key)], ht_list,
arc_entry_t)
#endif
{
if (entry->key == key)
break;
}
#else
lfu_entry_t *entry = NULL;
#ifdef __HAVE_TYPEOF
hlist_for_each_entry (entry, &cache->map->ht_list_head[HASH(key)], ht_list)
#else
hlist_for_each_entry (entry, &cache->map->ht_list_head[HASH(key)], ht_list,
lfu_entry_t)
#endif
{
if (entry->key == key)
break;
}
#endif
memcpy(cache->jitcode + cache->offset, code, sz);
entry->offset = cache->offset;
cache->offset += sz;
sys_icache_invalidate(cache->jitcode + entry->offset, sz);
return cache->jitcode + entry->offset;
}
#endif
#endif
10 changes: 10 additions & 0 deletions src/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,14 @@ void cache_free(struct cache *cache, void (*callback)(void *));
* @key: the key of the specified entry
*/
bool cache_hot(struct cache *cache, uint32_t key);

#ifndef MIR
uint8_t *code_cache_lookup(struct cache *cache, uint32_t key);

uint8_t *code_cache_add(struct cache *cache,
uint64_t key,
uint8_t *code,
size_t sz,
uint64_t align);
#endif
#endif
1 change: 0 additions & 1 deletion src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#pragma once

#include "feature.h"

#if defined(__GNUC__) || defined(__clang__)
#define UNUSED __attribute__((unused))
#define likely(x) __builtin_expect(!!(x), 1)
Expand Down
Loading

0 comments on commit 187ccce

Please sign in to comment.