Skip to content

Commit 6ecc87d

Browse files
committed
End the indirect jump when potential hotspot is detected
Currently, the indirect jump does not switch to T1C when potential hotspot is detected, it would make execution stuck in interpreter mode. This revision ensures the execution halts upon identifying a potential hotspot. It aims to make the execution process more efficient by stopping when a significant area of code usage is detected, allowing for targeted optimization.
1 parent 2d89b32 commit 6ecc87d

File tree

2 files changed

+24
-44
lines changed

2 files changed

+24
-44
lines changed

src/emulate.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,12 +399,14 @@ static bool has_loops = false;
399399
nextop: \
400400
PC += __rv_insn_##inst##_len; \
401401
if (unlikely(RVOP_NO_NEXT(ir))) { \
402-
rv->csr_cycle = cycle; \
403-
rv->PC = PC; \
404-
return true; \
402+
goto end_op; \
405403
} \
406404
const rv_insn_t *next = ir->next; \
407405
MUST_TAIL return next->impl(rv, next, cycle, PC); \
406+
end_op: \
407+
rv->csr_cycle = cycle; \
408+
rv->PC = PC; \
409+
return true; \
408410
}
409411

410412
#include "rv32_template.c"

src/rv32_template.c

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,12 @@ RVOP(
174174
if (!set_add(&pc_set, PC))
175175
has_loops = true;
176176
if (cache_hot(rv->block_cache, PC))
177-
goto end_insn;
177+
goto end_op;
178178
#endif
179179
last_pc = PC;
180180
MUST_TAIL return taken->impl(rv, taken, cycle, PC);
181181
}
182-
end_insn:
183-
rv->csr_cycle = cycle;
184-
rv->PC = PC;
185-
return true;
182+
goto end_op;
186183
},
187184
GEN({
188185
cond, rd;
@@ -226,8 +223,8 @@ RVOP(
226223
for (int i = 0; i < HISTORY_SIZE; i++) { \
227224
if (ir->branch_table->PC[i] == PC) { \
228225
ir->branch_table->times[i]++; \
229-
MUST_TAIL return block->ir_head->impl(rv, block->ir_head, \
230-
cycle, PC); \
226+
if (cache_hot(rv->block_cache, PC)) \
227+
goto end_op; \
231228
} \
232229
} \
233230
/* update branch history table */ \
@@ -243,6 +240,8 @@ RVOP(
243240
} \
244241
ir->branch_table->times[min_idx] = 1; \
245242
ir->branch_table->PC[min_idx] = PC; \
243+
if (cache_hot(rv->block_cache, PC)) \
244+
goto end_op; \
246245
MUST_TAIL return block->ir_head->impl(rv, block->ir_head, cycle, PC); \
247246
}
248247
#endif
@@ -268,9 +267,7 @@ RVOP(
268267
RV_EXC_MISALIGN_HANDLER(pc, insn, false, 0);
269268
#endif
270269
LOOKUP_OR_UPDATE_BRANCH_HISTORY_TABLE();
271-
rv->csr_cycle = cycle;
272-
rv->PC = PC;
273-
return true;
270+
goto end_op;
274271
},
275272
GEN({
276273
cond, rd;
@@ -324,15 +321,12 @@ RVOP(
324321
if (!set_add(&pc_set, PC)) \
325322
has_loops = true; \
326323
if (cache_hot(rv->block_cache, PC)) \
327-
goto end_insn; \
324+
goto end_op; \
328325
}, ); \
329326
last_pc = PC; \
330327
MUST_TAIL return taken->impl(rv, taken, cycle, PC); \
331328
} \
332-
end_insn: \
333-
rv->csr_cycle = cycle; \
334-
rv->PC = PC; \
335-
return true;
329+
goto end_op;
336330

337331
/* In RV32I and RV64I, if the branch is taken, set pc = pc + offset, where
338332
* offset is a multiple of two; else do nothing. The offset is 13 bits long.
@@ -1956,15 +1950,12 @@ RVOP(
19561950
if (!set_add(&pc_set, PC))
19571951
has_loops = true;
19581952
if (cache_hot(rv->block_cache, PC))
1959-
goto end_insn;
1953+
goto end_op;
19601954
#endif
19611955
last_pc = PC;
19621956
MUST_TAIL return taken->impl(rv, taken, cycle, PC);
19631957
}
1964-
end_insn:
1965-
rv->csr_cycle = cycle;
1966-
rv->PC = PC;
1967-
return true;
1958+
goto end_op;
19681959
},
19691960
GEN({
19701961
map, VR0, rv_reg_ra;
@@ -2120,15 +2111,12 @@ RVOP(
21202111
if (!set_add(&pc_set, PC))
21212112
has_loops = true;
21222113
if (cache_hot(rv->block_cache, PC))
2123-
goto end_insn;
2114+
goto end_op;
21242115
#endif
21252116
last_pc = PC;
21262117
MUST_TAIL return taken->impl(rv, taken, cycle, PC);
21272118
}
2128-
end_insn:
2129-
rv->csr_cycle = cycle;
2130-
rv->PC = PC;
2131-
return true;
2119+
goto end_op;
21322120
},
21332121
GEN({
21342122
break;
@@ -2171,15 +2159,12 @@ RVOP(
21712159
if (!set_add(&pc_set, PC))
21722160
has_loops = true;
21732161
if (cache_hot(rv->block_cache, PC))
2174-
goto end_insn;
2162+
goto end_op;
21752163
#endif
21762164
last_pc = PC;
21772165
MUST_TAIL return taken->impl(rv, taken, cycle, PC);
21782166
}
2179-
end_insn:
2180-
rv->csr_cycle = cycle;
2181-
rv->PC = PC;
2182-
return true;
2167+
goto end_op;
21832168
},
21842169
GEN({
21852170
rald, VR0, rs1;
@@ -2231,15 +2216,12 @@ RVOP(
22312216
if (!set_add(&pc_set, PC))
22322217
has_loops = true;
22332218
if (cache_hot(rv->block_cache, PC))
2234-
goto end_insn;
2219+
goto end_op;
22352220
#endif
22362221
last_pc = PC;
22372222
MUST_TAIL return taken->impl(rv, taken, cycle, PC);
22382223
}
2239-
end_insn:
2240-
rv->csr_cycle = cycle;
2241-
rv->PC = PC;
2242-
return true;
2224+
goto end_op;
22432225
},
22442226
GEN({
22452227
rald, VR0, rs1;
@@ -2297,9 +2279,7 @@ RVOP(
22972279
{
22982280
PC = rv->X[ir->rs1];
22992281
LOOKUP_OR_UPDATE_BRANCH_HISTORY_TABLE();
2300-
rv->csr_cycle = cycle;
2301-
rv->PC = PC;
2302-
return true;
2282+
goto end_op;
23032283
},
23042284
GEN({
23052285
rald, VR0, rs1;
@@ -2349,9 +2329,7 @@ RVOP(
23492329
rv->X[rv_reg_ra] = PC + 2;
23502330
PC = jump_to;
23512331
LOOKUP_OR_UPDATE_BRANCH_HISTORY_TABLE();
2352-
rv->csr_cycle = cycle;
2353-
rv->PC = PC;
2354-
return true;
2332+
goto end_op;
23552333
},
23562334
GEN({
23572335
map, VR0, rv_reg_ra;

0 commit comments

Comments
 (0)