Skip to content

Commit 844e9fc

Browse files
committed
Correctly update the used frequency of BB
1 parent 5bf9063 commit 844e9fc

File tree

6 files changed

+26
-25
lines changed

6 files changed

+26
-25
lines changed

src/cache.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ cache_t *cache_create(int size_bits)
166166
return NULL;
167167
}
168168

169-
void *cache_get(const cache_t *cache, uint32_t key)
169+
void *cache_get(const cache_t *cache, uint32_t key, bool update)
170170
{
171171
if (!cache->capacity ||
172172
hlist_empty(&cache->map->ht_list_head[cache_hash(key)]))
@@ -192,7 +192,7 @@ void *cache_get(const cache_t *cache, uint32_t key)
192192
* code. The generated C code is then compiled into machine code by the
193193
* target compiler.
194194
*/
195-
if (entry->frequency < THRESHOLD) {
195+
if (update && entry->frequency < THRESHOLD) {
196196
list_del_init(&entry->list);
197197
list_add(&entry->list, cache->lists[entry->frequency++]);
198198
}
@@ -242,7 +242,7 @@ void cache_free(cache_t *cache)
242242
free(cache);
243243
}
244244

245-
uint32_t cache_freq(struct cache *cache, uint32_t key)
245+
uint32_t cache_freq(const struct cache *cache, uint32_t key)
246246
{
247247
if (!cache->capacity ||
248248
hlist_empty(&cache->map->ht_list_head[cache_hash(key)]))

src/cache.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ struct cache *cache_create(int size_bits);
2020
* cache_get - retrieve the specified entry from the cache
2121
* @cache: a pointer points to target cache
2222
* @key: the key of the specified entry
23+
* @update: update frequency or not
2324
* @return: the specified entry or NULL
2425
*/
25-
void *cache_get(const struct cache *cache, uint32_t key);
26+
void *cache_get(const struct cache *cache, uint32_t key, bool update);
2627

2728
/**
2829
* cache_put - insert a new entry into the cache
@@ -50,4 +51,4 @@ void cache_free(struct cache *cache);
5051
bool cache_hot(const struct cache *cache, uint32_t key);
5152
#endif
5253

53-
uint32_t cache_freq(struct cache *cache, uint32_t key);
54+
uint32_t cache_freq(const struct cache *cache, uint32_t key);

src/emulate.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ static block_t *block_find_or_translate(riscv_t *rv)
950950
block_t *next = block_find(map, rv->PC);
951951
#else
952952
/* lookup the next block in the block cache */
953-
block_t *next = (block_t *) cache_get(rv->block_cache, rv->PC);
953+
block_t *next = (block_t *) cache_get(rv->block_cache, rv->PC, true);
954954
#endif
955955

956956
if (!next) {
@@ -986,7 +986,7 @@ static block_t *block_find_or_translate(riscv_t *rv)
986986
rv_insn_t *taken = delete_target->ir_tail->branch_taken,
987987
*untaken = delete_target->ir_tail->branch_untaken;
988988
if (taken && taken->pc != delete_target->pc_start) {
989-
block_t *target = cache_get(rv->block_cache, taken->pc);
989+
block_t *target = cache_get(rv->block_cache, taken->pc, false);
990990
bool flag = false;
991991
list_for_each_entry_safe (entry, safe, &target->list, list) {
992992
if (entry->block == delete_target) {
@@ -998,7 +998,8 @@ static block_t *block_find_or_translate(riscv_t *rv)
998998
assert(flag);
999999
}
10001000
if (untaken && untaken->pc != delete_target->pc_start) {
1001-
block_t *target = cache_get(rv->block_cache, untaken->pc);
1001+
block_t *target =
1002+
cache_get(rv->block_cache, untaken->pc, false);
10021003
assert(target);
10031004
bool flag = false;
10041005
list_for_each_entry_safe (entry, safe, &target->list, list) {
@@ -1056,7 +1057,7 @@ void rv_step(riscv_t *rv, int32_t cycles)
10561057
#if !RV32_HAS(JIT)
10571058
prev = block_find(&rv->block_map, last_pc);
10581059
#else
1059-
prev = cache_get(rv->block_cache, last_pc);
1060+
prev = cache_get(rv->block_cache, last_pc, false);
10601061
#endif
10611062
}
10621063
/* lookup the next block in block map or translate a new block,

src/jit.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,12 +1497,14 @@ static void translate_chained_block(struct jit_state *state,
14971497
translate(state, rv, block);
14981498
rv_insn_t *ir = block->ir_tail;
14991499
if (ir->branch_untaken && !set_has(set, ir->branch_untaken->pc)) {
1500-
block_t *block1 = cache_get(rv->block_cache, ir->branch_untaken->pc);
1500+
block_t *block1 =
1501+
cache_get(rv->block_cache, ir->branch_untaken->pc, false);
15011502
if (block1->translatable)
15021503
translate_chained_block(state, rv, block1, set);
15031504
}
15041505
if (ir->branch_taken && !set_has(set, ir->branch_taken->pc)) {
1505-
block_t *block1 = cache_get(rv->block_cache, ir->branch_taken->pc);
1506+
block_t *block1 =
1507+
cache_get(rv->block_cache, ir->branch_taken->pc, false);
15061508
if (block1->translatable)
15071509
translate_chained_block(state, rv, block1, set);
15081510
}

src/rv32_template.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ RVOP(
148148
struct rv_insn *taken = ir->branch_taken;
149149
if (taken) {
150150
#if RV32_HAS(JIT)
151+
cache_get(rv->block_cache, PC, true);
151152
if (cache_hot(rv->block_cache, PC))
152153
goto end_insn;
153154
#endif
@@ -246,12 +247,7 @@ RVOP(
246247
goto nextop; \
247248
IIF(RV32_HAS(JIT)) \
248249
({ \
249-
block_t *block = cache_get(rv->block_cache, PC + 4); \
250-
if (!block) { \
251-
ir->branch_untaken = NULL; \
252-
goto nextop; \
253-
} \
254-
untaken = ir->branch_untaken = block->ir_head; \
250+
cache_get(rv->block_cache, PC + 4, true); \
255251
if (cache_hot(rv->block_cache, PC + 4)) \
256252
goto nextop; \
257253
}, ); \
@@ -268,12 +264,7 @@ RVOP(
268264
if (taken) { \
269265
IIF(RV32_HAS(JIT)) \
270266
({ \
271-
block_t *block = cache_get(rv->block_cache, PC); \
272-
if (!block) { \
273-
ir->branch_taken = NULL; \
274-
goto end_insn; \
275-
} \
276-
taken = ir->branch_taken = block->ir_head; \
267+
cache_get(rv->block_cache, PC, true); \
277268
if (cache_hot(rv->block_cache, PC)) \
278269
goto end_insn; \
279270
}, ); \
@@ -1858,6 +1849,7 @@ RVOP(
18581849
struct rv_insn *taken = ir->branch_taken;
18591850
if (taken) {
18601851
#if RV32_HAS(JIT)
1852+
cache_get(rv->block_cache, PC, true);
18611853
if (cache_hot(rv->block_cache, PC))
18621854
goto end_insn;
18631855
#endif
@@ -2018,6 +2010,7 @@ RVOP(
20182010
struct rv_insn *taken = ir->branch_taken;
20192011
if (taken) {
20202012
#if RV32_HAS(JIT)
2013+
cache_get(rv->block_cache, PC, true);
20212014
if (cache_hot(rv->block_cache, PC))
20222015
goto end_insn;
20232016
#endif
@@ -2050,6 +2043,7 @@ RVOP(
20502043
if (!untaken)
20512044
goto nextop;
20522045
#if RV32_HAS(JIT)
2046+
cache_get(rv->block_cache, PC + 2, true);
20532047
if (cache_hot(rv->block_cache, PC + 2))
20542048
goto nextop;
20552049
#endif
@@ -2062,6 +2056,7 @@ RVOP(
20622056
struct rv_insn *taken = ir->branch_taken;
20632057
if (taken) {
20642058
#if RV32_HAS(JIT)
2059+
cache_get(rv->block_cache, PC, true);
20652060
if (cache_hot(rv->block_cache, PC))
20662061
goto end_insn;
20672062
#endif
@@ -2103,6 +2098,7 @@ RVOP(
21032098
if (!untaken)
21042099
goto nextop;
21052100
#if RV32_HAS(JIT)
2101+
cache_get(rv->block_cache, PC + 2, true);
21062102
if (cache_hot(rv->block_cache, PC + 2))
21072103
goto nextop;
21082104
#endif
@@ -2115,6 +2111,7 @@ RVOP(
21152111
struct rv_insn *taken = ir->branch_taken;
21162112
if (taken) {
21172113
#if RV32_HAS(JIT)
2114+
cache_get(rv->block_cache, PC, true);
21182115
if (cache_hot(rv->block_cache, PC))
21192116
goto end_insn;
21202117
#endif

tests/cache/test-cache.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static void split(char **arr, char *str, const char *del)
2525

2626
/* Commands of test-cache
2727
* 1. NEW: cache_create(8), the cache size is set to 256.
28-
* 2. GET key: cache_get(cache, key)
28+
* 2. GET key: cache_get(cache, key, true)
2929
* 3. PUT key val: cache_put(cache, key, val)
3030
* 4. FREE: cache_free(cache, free)
3131
*/
@@ -46,7 +46,7 @@ int main(int argc, char *argv[])
4646
split(arr, line, " ");
4747
if (!strcmp(arr[0], "GET")) {
4848
key = (int) strtol(arr[1], &ptr, 10);
49-
ans = cache_get(cache, key);
49+
ans = cache_get(cache, key, true);
5050
print_value(ans);
5151
} else if (!strcmp(arr[0], "PUT")) {
5252
key = (int) strtol(arr[1], &ptr, 10);

0 commit comments

Comments
 (0)