target/xtensa: rework zero overhead loops implementation
PATCH 1/1 target/xtensa: rework zero overhead loops implementation
Don't invalidate TB with the end of zero overhead loop when LBEG or LEND change. Instead encode the distance from the start of the page where the TB starts to the LEND in the TB cs_base and generate loopback code when the next PC matches encoded LEND. Distance to a destination within the same page and up to a maximum instruction length into the next page is encoded literally, otherwise it's zero. The distance from LEND to LBEG is also encoded in the cs_base: it's encoded literally when less than 256 or as 0 otherwise. This allows for TB chaining for the loopback branch at the end of a loop for the most common loop sizes.
With this change the resulting emulation speed is about 10% higher in softmmu mode on uClibc-ng and LTP tests. Emulation speed in linux user mode is a few percent lower because there's no direct TB chaining between different memory pages. Testing with lower limit on direct TB chainig range shows gradual slowdown to ~15% for the block size of 64 bytes and ~50% for the block size of 32 bytes.
--- target/xtensa/cpu.h | 21 +++++++++++++++ target/xtensa/helper.h | 2 -- target/xtensa/op_helper.c | 24 ----------------- target/xtensa/overlay_tool.h | 1 + target/xtensa/translate.c | 61 +++++++++++++++----------------------------- 5 files changed, 42 insertions(+), 67 deletions(-)
target/xtensa/cpu.h +21 -0
@@ -400,6 +400,7 @@ struct XtensaConfig {int excm_level;int ndepc;unsigned inst_fetch_width; +unsigned max_insn_size;uint32_t vecbase;uint32_t exception_vector[EXC_MAX ];unsigned ninterrupt;
@@ -695,6 +696,15 @@ static inline int cpu_mmu_index(CPUXtensaState *env, bool ifetch)#define XTENSA_TBFLAG_CALLINC_MASK 0x180000#define XTENSA_TBFLAG_CALLINC_SHIFT 19 +#define XTENSA_CSBASE_LEND_MASK 0x0000ffff +#define XTENSA_CSBASE_LEND_SHIFT 0 +#define XTENSA_CSBASE_LBEG_OFF_MASK 0x00ff0000 +#define XTENSA_CSBASE_LBEG_OFF_SHIFT 16 + +#define LINKABLE_BITS 12 +#define LINKABLE_SIZE (1u << LINKABLE_BITS) +#define LINKABLE_MASK (-LINKABLE_SIZE) +static inline void cpu_get_tb_cpu_state (CPUXtensaState *env,target_ulong *pc,target_ulong *cs_base,uint32_t *flags) {
@@ -706,6 +716,17 @@ static inline void cpu_get_tb_cpu_state(CPUXtensaState *env, target_ulong *pc, *flags |=xtensa_get_ring (env);if (env->sregs[PS ] &PS_EXCM ) { *flags |=XTENSA_TBFLAG_EXCM ; + }else if (xtensa_option_enabled (env->config,XTENSA_OPTION_LOOP )) { +target_ulong lend_dist = env->sregs[LEND ] - (env->pc &LINKABLE_MASK ); + +if (lend_dist <LINKABLE_SIZE + env->config->max_insn_size) { +target_ulong lbeg_off = env->sregs[LEND ] - env->sregs[LBEG ]; + + *cs_base = lend_dist; +if (lbeg_off <256 ) { + *cs_base |= lbeg_off <<XTENSA_CSBASE_LBEG_OFF_SHIFT ; + } + } }if (xtensa_option_enabled (env->config,XTENSA_OPTION_EXTENDED_L32R ) && (env->sregs[LITBASE ] &1 )) {
target/xtensa/helper.h +0 -2
@@ -12,8 +12,6 @@ DEF_HELPER_2(rotw, void, env, i32) DEF_HELPER_3(window_check, noreturn, env, i32, i32) DEF_HELPER_1(restore_owb, void, env) DEF_HELPER_2(movsp, void, env, i32) -DEF_HELPER_2(wsr_lbeg, void, env, i32) -DEF_HELPER_2(wsr_lend, void, env, i32)#ifndef CONFIG_USER_ONLY DEF_HELPER_1(simcall, void, env)#endif
target/xtensa/op_helper.c +0 -24
@@ -107,13 +107,6 @@ static void tb_invalidate_virtual_addr(CPUXtensaState *env, uint32_t vaddr) } } -#else - -static void tb_invalidate_virtual_addr (CPUXtensaState *env,uint32_t vaddr) -{ -tb_invalidate_phys_addr (vaddr); -} -#endif void HELPER (exception )(CPUXtensaState *env,uint32_t excp)
@@ -370,23 +363,6 @@ void HELPER(movsp)(CPUXtensaState *env, uint32_t pc) } } -void HELPER (wsr_lbeg )(CPUXtensaState *env,uint32_t v) -{ -if (env->sregs[LBEG ] != v) { -tb_invalidate_virtual_addr (env, env->sregs[LEND ] -1 ); - env->sregs[LBEG ] = v; - } -} - -void HELPER (wsr_lend )(CPUXtensaState *env,uint32_t v) -{ -if (env->sregs[LEND ] != v) { -tb_invalidate_virtual_addr (env, env->sregs[LEND ] -1 ); - env->sregs[LEND ] = v; -tb_invalidate_virtual_addr (env, env->sregs[LEND ] -1 ); - } -} -void HELPER (dump_state )(CPUXtensaState *env) {XtensaCPU *cpu =xtensa_env_get_cpu (env);
target/xtensa/overlay_tool.h +1 -0
@@ -457,6 +457,7 @@ .nareg = XCHAL_NUM_AREGS, \ .ndepc = (XCHAL_XEA_VERSION >= 2), \ .inst_fetch_width = XCHAL_INST_FETCH_WIDTH, \ + .max_insn_size =XCHAL_MAX_INSTRUCTION_SIZE , \EXCEPTIONS_SECTION , \INTERRUPTS_SECTION , \ TLB_SECTION, \
target/xtensa/translate.c +20 -41
158 lines not on this page · raw message
Signature · 1 line
2.11.0
target/xtensa/cpu.h static inline int cpu_mmu_index(CPUXtensaState *env, bool ifetch) · line 706
Quoted 3 lines · Max Filippov
target/xtensa/cpu.h +3
-0+#define LINKABLE_BITS 12 +#define LINKABLE_SIZE (1u << LINKABLE_BITS) +#define LINKABLE_MASK (-LINKABLE_SIZE)
What is this for? It seems to be replicating TARGET_PAGE_BITS.
r~
Quoted 7 lines · Richard Henderson
target/xtensa/cpu.h static inline int cpu_mmu_index(CPUXtensaState *env, bool ifetch) · line 706
Quoted 3 lines · Max Filippov
target/xtensa/cpu.h +3
-0+#define LINKABLE_BITS 12 +#define LINKABLE_SIZE (1u << LINKABLE_BITS) +#define LINKABLE_MASK (-LINKABLE_SIZE)What is this for? It seems to be replicating
TARGET_PAGE_BITS.
I used it to play with different sizes of regions where direct TB chaining is allowed. In linux-user it is possible to have it bigger than the page size. It looks like the optimum is somewhere in the range 10..12.
I left it there because it looks logically independent from the page size, but I sure can drop it.
Signature · 2 lines
Thanks. -- Max
Quoted 8 lines · Max Filippov
target/xtensa/cpu.h +4
-0@@ -706,6 +716,17 @@ static inline void cpu_get_tb_cpu_state(CPUXtensaState *env, target_ulong *pc, *flags |=xtensa_get_ring (env);if (env->sregs[PS ] &PS_EXCM ) { *flags |=XTENSA_TBFLAG_EXCM ; + }else if (xtensa_option_enabled (env->config,XTENSA_OPTION_LOOP )) { +target_ulong lend_dist = env->sregs[LEND ] - (env->pc &LINKABLE_MASK ); + +if (lend_dist <LINKABLE_SIZE + env->config->max_insn_size) {
target/xtensa/cpu.h static inline void cpu_get_tb_cpu_state(CPUXtensaState *env, target_ulong *pc, · line 732
Quoted 10 lines
target/xtensa/cpu.h +7
-0+target_ulong lbeg_off = env->sregs[LEND ] - env->sregs[LBEG ]; + + *cs_base = lend_dist; +if (lbeg_off <256 ) { + *cs_base |= lbeg_off <<XTENSA_CSBASE_LBEG_OFF_SHIFT ; + } + } }if (xtensa_option_enabled (env->config,XTENSA_OPTION_EXTENDED_L32R ) && (env->sregs[LITBASE ] &1 )) {
I think the only other thing that would be nice is some comment that describes the loop scheme. I can follow it now, but it took a while of re-reading. In particular, the fact that 0 means "disabled", and happens to work because we (correctly) only check for LEND at the end of an instruction. Thus the offset from pc_first will always be non-zero when we check.
r~