Skip to content

Commit c39da6f

Browse files
committed
Refine the description about block prediction
1 parent df6731f commit c39da6f

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

src/emulate.c

+24-13
Original file line numberDiff line numberDiff line change
@@ -1202,10 +1202,10 @@ static void block_insert(block_map_t *map, const block_t *block)
12021202
}
12031203

12041204
/* try to locate an already translated block in the block map */
1205-
static block_t *block_find(const block_map_t *map, const uint32_t pc)
1205+
static block_t *block_find(const block_map_t *map, const uint32_t addr)
12061206
{
12071207
assert(map);
1208-
uint32_t index = hash(pc);
1208+
uint32_t index = hash(addr);
12091209
const uint32_t mask = map->block_capacity - 1;
12101210

12111211
/* find block in block map */
@@ -1214,7 +1214,7 @@ static block_t *block_find(const block_map_t *map, const uint32_t pc)
12141214
if (!block)
12151215
return NULL;
12161216

1217-
if (block->pc_start == pc)
1217+
if (block->pc_start == addr)
12181218
return block;
12191219
}
12201220
return NULL;
@@ -1240,8 +1240,7 @@ static bool block_emulate(riscv_t *rv, const block_t *block)
12401240

12411241
static void block_translate(riscv_t *rv, block_t *block)
12421242
{
1243-
block->pc_start = rv->PC;
1244-
block->pc_end = rv->PC;
1243+
block->pc_start = block->pc_end = rv->PC;
12451244

12461245
/* translate the basic block */
12471246
while (block->n_insn < block->insn_capacity) {
@@ -1288,7 +1287,11 @@ static block_t *block_find_or_translate(riscv_t *rv, block_t *prev)
12881287
/* insert the block into block map */
12891288
block_insert(&rv->block_map, next);
12901289

1291-
/* update the block prediction */
1290+
/* update the block prediction
1291+
* When we translate a new block, the block predictor may benefit,
1292+
* but when it is updated after we find a particular block, it may
1293+
* penalize us significantly.
1294+
*/
12921295
if (prev)
12931296
prev->predict = next;
12941297
}
@@ -1301,24 +1304,32 @@ void rv_step(riscv_t *rv, int32_t cycles)
13011304
assert(rv);
13021305

13031306
/* find or translate a block for starting PC */
1304-
block_t *prev = NULL, *next = NULL;
1307+
block_t *prev = NULL;
1308+
13051309
const uint64_t cycles_target = rv->csr_cycle + cycles;
13061310

1311+
/* loop until we hit out cycle target */
13071312
while (rv->csr_cycle < cycles_target && !rv->halt) {
1308-
/* check the block prediction first */
1313+
block_t *block;
1314+
1315+
/* try to predict the next block */
13091316
if (prev && prev->predict && prev->predict->pc_start == rv->PC) {
1310-
next = prev->predict;
1317+
block = prev->predict;
13111318
} else {
1312-
next = block_find_or_translate(rv, prev);
1319+
/* lookup the next block in block map or translate a new block,
1320+
* and move onto the next block.
1321+
*/
1322+
block = block_find_or_translate(rv, prev);
13131323
}
13141324

1315-
assert(next);
1325+
/* we should have a block by now */
1326+
assert(block);
13161327

13171328
/* execute the block */
1318-
if (!block_emulate(rv, next))
1329+
if (!block_emulate(rv, block))
13191330
break;
13201331

1321-
prev = next;
1332+
prev = block;
13221333
}
13231334
}
13241335

0 commit comments

Comments
 (0)