TCG optimizations for 2.10
cover TCG optimizations for 2.10
Hi all,
This series is aimed at 2.10 or beyond. Its goal is to improve TCG performance by optimizing:
1- Cross-page direct jumps (softmmu only, obviously). Patches 1-4. 2- Indirect branches (softmmu and user-mode). Patches 5-9. 3- tb_jmp_cache hashing in user-mode. Patch 10.
I decided to work on this after reading this paper [1] (code at [2]), which among other optimizations it proposes solutions for 1 and 2. I followed the same overall scheme they follow, that is to use helpers to check whether the target vaddr is valid, and if so, jump to its corresponding translated code (host address) without having to go back to the exec loop. My implementation differs from that in the paper in that it uses tb_jmp_cache instead of adding more caches, which is simpler and probably more resilient in environments where TLB invalidations are frequent (in the paper they acknowledge that they limited background processes to a minimum, which isn't realistic).
These changes require modifications on the targets and, for optimization number 2, a new TCG opcode to jump to a host address contained in a register.
For now I only implemented this for the i386 and arm targets, and the i386 TCG backend. Other targets/backends can easily opt-in.
The 3rd optimization is implemented in the last patch: it improves tb_jmp_cache hashing for user-mode by removing the requirement of being able to clear parts of the cache given a page number, since this requirement only applies to softmmu.
The series applies cleanly on top of 95b31d709ba34.
The commit logs include many measurements, performed using SPECint06 and NBench from dbt-bench[3].
Feedback welcome! Thanks,
Emilio
[1] "Optimizing Control Transfer and Memory Virtualization in Full System Emulators", Ding-Yong Hong, Chun-Chen Hsu, Cheng-Yi Chou, Wei-Chung Hsu, Pangfeng Liu, Jan-Jan Wu. ACM TACO, Jan. 2016.
http://www.iis.sinica.edu.tw/page/library/TechReport/tr2015/tr15002.pdf
[2] https://github.com/tkhsu/quick-android-emulator/tree/quick-qemu
PATCH 1/10 exec-all: add tb_from_jmp_cache
This paves the way for upcoming changes.
--- cpu-exec.c | 19 +++++++++++++++++++ include/exec/exec-all.h | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-)
cpu-exec.c +19 -0
@@ -309,6 +309,25 @@ static bool tb_cmp(const void *p, const void *d)return false; } +TranslationBlock *tb_from_jmp_cache (CPUArchState *env,target_ulong vaddr) +{ +CPUState *cpu =ENV_GET_CPU (env); +TranslationBlock *tb; +target_ulong cs_base, pc; +uint32_t flags; + +if (unlikely (atomic_read (&cpu->exit_request))) { +return NULL ; + } +cpu_get_tb_cpu_state (env, &pc, &cs_base, &flags); + tb =atomic_rcu_read (&cpu->tb_jmp_cache[tb_jmp_cache_hash_func (vaddr)]); +if (likely (tb && tb->pc == vaddr && tb->cs_base == cs_base && + tb->flags == flags)) { +return tb; + } +return NULL ; +} + static TranslationBlock *tb_htable_lookup(CPUState *cpu, target_ulong pc, target_ulong cs_base,
include/exec/exec-all.h +1 -1
@@ -56,7 +56,6 @@ TranslationBlock *tb_gen_code(CPUState *cpu, target_ulong pc, target_ulong cs_base, uint32_t flags, int cflags); - void QEMU_NORETURN cpu_loop_exit(CPUState *cpu); void QEMU_NORETURN cpu_loop_exit_restore(CPUState *cpu, uintptr_t pc); void QEMU_NORETURN cpu_loop_exit_atomic(CPUState *cpu, uintptr_t pc);
@@ -368,6 +367,7 @@ struct TranslationBlock {void tb_free (TranslationBlock *tb);void tb_flush (CPUState *cpu);void tb_phys_invalidate (TranslationBlock *tb,tb_page_addr_t page_addr); +TranslationBlock *tb_from_jmp_cache (CPUArchState *env,target_ulong vaddr);#if defined(USE_DIRECT_JUMP )
Signature · 1 line
2.7.4
PATCH 2/10 exec-all: inline tb_from_jmp_cache
In reply to Emilio G. CotaThe inline improves performance, as shown in subsequent commits' logs.
This commit is kept separate to ease review, since the inclusion of tb-hash.h might be controversial. The problem here, which was introduced before this commit, is that tb_hash_func() depends onpage_addr_t: this defeats the original purpose of tb-hash.h, which was to be self-contained and CPU-agnostic.
--- cpu-exec.c | 19 ------------------- include/exec/exec-all.h | 24 +++++++++++++++++++++++- 2 files changed, 23 insertions(+), 20 deletions(-)
cpu-exec.c +0 -19
@@ -309,25 +309,6 @@ static bool tb_cmp(const void *p, const void *d)return false; } -TranslationBlock *tb_from_jmp_cache (CPUArchState *env,target_ulong vaddr) -{ -CPUState *cpu =ENV_GET_CPU (env); -TranslationBlock *tb; -target_ulong cs_base, pc; -uint32_t flags; - -if (unlikely (atomic_read (&cpu->exit_request))) { -return NULL ; - } -cpu_get_tb_cpu_state (env, &pc, &cs_base, &flags); - tb =atomic_rcu_read (&cpu->tb_jmp_cache[tb_jmp_cache_hash_func (vaddr)]); -if (likely (tb && tb->pc == vaddr && tb->cs_base == cs_base && - tb->flags == flags)) { -return tb; - } -return NULL ; -} - static TranslationBlock *tb_htable_lookup(CPUState *cpu, target_ulong pc, target_ulong cs_base,
include/exec/exec-all.h +23 -1
@@ -367,7 +367,29 @@ struct TranslationBlock {void tb_free (TranslationBlock *tb);void tb_flush (CPUState *cpu);void tb_phys_invalidate (TranslationBlock *tb,tb_page_addr_t page_addr); -TranslationBlock *tb_from_jmp_cache (CPUArchState *env,target_ulong vaddr); + +/* tb_hash_func() in tb-hash.h needs tb_page_addr_t, defined above */ +#include "tb-hash.h" + +static inline +TranslationBlock *tb_from_jmp_cache (CPUArchState *env,target_ulong vaddr) +{ +CPUState *cpu =ENV_GET_CPU (env); +TranslationBlock *tb; +target_ulong cs_base, pc; +uint32_t flags; + +if (unlikely (atomic_read (&cpu->exit_request))) { +return NULL ; + } +cpu_get_tb_cpu_state (env, &pc, &cs_base, &flags); + tb =atomic_rcu_read (&cpu->tb_jmp_cache[tb_jmp_cache_hash_func (vaddr)]); +if (likely (tb && tb->pc == vaddr && tb->cs_base == cs_base && + tb->flags == flags)) { +return tb; + } +return NULL ; +}#if defined(USE_DIRECT_JUMP )
Signature · 1 line
2.7.4
PATCH 3/10 target/arm: optimize cross-page block chaining in softmmu
In reply to Emilio G. CotaInstead of unconditionally exiting to the exec loop, add a helper to check whether the target TB is valid. As long as the hit rate in tb_jmp_cache remains high, this improves performance.
Measurements:
- Boot time of ARM debian jessie on Intel host:
| setup | ARM debian boot+shutdown time | stddev | |--------------------+-------------------------------+--------| | master | 10.050247057 | 0.0361 | | +cross | 10.311265443 | 0.0721 |
That is a 2.58% slowdown when booting. This is reasonable given that tb_jmp_cache's hit rate when booting is expected to be low.
- NBench, arm-softmmu. Host: Intel i7-4790K @ 4.00GHz
(y axis: Speedup over 95b31d70)
1.3x+-+--------------------------------------------------------------+-+
| cross+noinline $$$ |
| cross+inline %%% |
| $$$%% |
1.2x+-+.................$.$.%.......$$$..............................+-+
| $ $ % $ $% |
| $ $ % $ $% |
1.1x+-+.................$.$.%.......$.$%.............................+-+
| $$$%% $ $ % $ $% |
| $ $ % $ $ % $ $% $$$%% $$$%% $$$%% |
| $$$%% $$$%% $ $ % $ $ % $$$%% $ $% $ $ % %%% $ $ % $ $ % |
1x+-$.$B%R$R$A%G$A$H%T$M$_%P$L$i%l$n$%.$.$.%...%.%.$$$%%.$.$.%.$.$.%-+
| $ $ % $ $ % $ $ % $ $ % $ $ % $ $% $ $ % % % $ $ % $ $ % $ $ % |
| $ $ % $ $ % $ $ % $ $ % $ $ % $ $% $ $ % % % $ $ % $ $ % $ $ % |
0.9x+-$.$.%.$.$.%.$.$.%.$.$.%.$.$.%.$.$%.$.$.%...%.%.$.$.%.$.$.%.$.$.%-+
| $ $ % $ $ % $ $ % $ $ % $ $ % $ $% $ $ % % % $ $ % $ $ % $ $ % |
| $ $ % $ $ % $ $ % $ $ % $ $ % $ $% $ $ % $$$ % $ $ % $ $ % $ $ % |
| $ $ % $ $ % $ $ % $ $ % $ $ % $ $% $ $ % $ $ % $ $ % $ $ % $ $ % |
0.8x+-$$$%%-$$$%%-$$$%%-$$$%%-$$$%%-$$$%-$$$%%-$$$%%-$$$%%-$$$%%-$$$%%-+
ASSIGNMBITFIELFOUFP_EMULATHUFFMALU_DECOMPNEURANUMERICSTRING_SOhmeanThat is, a 4.04% hmean perf improvement over master with tb_from_jmp_cache not inlined, and a 5.82% hmean perf improvement over master with tb_from_jmp_cache inlined (i.e. this commit). The largest improvement is 21% for the FP_EMULATION benchmark.
--- target/arm/helper.c | 5 +++++ target/arm/helper.h | 2 ++ target/arm/translate.c | 12 ++++++++++++ 3 files changed, 19 insertions(+)
target/arm/helper.c +5 -0
@@ -9922,3 +9922,8 @@ uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)/* Linux crc32c converts the output to one's complement. */ return crc32c (acc, buf, bytes) ^0xffffffff ; } + +uint32_t HELPER (cross_page_check )(CPUARMState *env,target_ulong vaddr) +{ +return !!tb_from_jmp_cache (env, vaddr); +}
target/arm/helper.h +2 -0
@@ -1,6 +1,8 @@ DEF_HELPER_FLAGS_1(sxtb16, TCG_CALL_NO_RWG_SE, i32, i32) DEF_HELPER_FLAGS_1(uxtb16, TCG_CALL_NO_RWG_SE, i32, i32) +DEF_HELPER_2(cross_page_check, i32, env, tl) + DEF_HELPER_3(add_setq, i32, env, i32, i32) DEF_HELPER_3(add_saturate, i32, env, i32, i32)DEF_HELPER_3 (sub_saturate, i32, env, i32, i32)
target/arm/translate.c +12 -0
@@ -4085,6 +4085,18 @@ static inline void gen_goto_tb(DisasContext *s, int n, target_ulong dest)gen_set_pc_im (s, dest);tcg_gen_exit_tb ((uintptr_t )s->tb + n); }else { +TCGv vaddr =tcg_const_tl (dest); +TCGv_i32 valid =tcg_temp_new_i32 (); +TCGLabel *label =gen_new_label (); + +gen_helper_cross_page_check (valid, cpu_env, vaddr); +tcg_temp_free (vaddr); +tcg_gen_brcondi_i32 (TCG_COND_EQ , valid,0 , label); +tcg_temp_free_i32 (valid); +tcg_gen_goto_tb (n); +gen_set_pc_im (s, dest); +tcg_gen_exit_tb ((uintptr_t )s->tb + n); +gen_set_label (label);gen_set_pc_im (s, dest);tcg_gen_exit_tb (0 ); }
Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH 03/10] target/arm: optimize cross-page block chaining in softmmu
target/arm/helper.c uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes) · line 9929
Quoted 4 lines · Emilio G. Cota
target/arm/helper.c +4
-0+uint32_t HELPER (cross_page_check )(CPUARMState *env,target_ulong vaddr) +{ +return !!tb_from_jmp_cache (env, vaddr); +}
FWIW, helpers like this that are intended to be used by more than one target should go into tcg-runtime.[ch].
That said, I don't think this is the proper abstraction. More later...
r~
PATCH 4/10 target/i386: optimize cross-page block chaining in softmmu
In reply to Emilio G. CotaInstead of unconditionally exiting to the exec loop, add a helper to check whether the target TB is valid. As long as the hit rate in tb_jmp_cache remains high, this improves performance.
Measurements:
- specINT 2006 (test set), x86_64-softmmu. Host: Intel i7-4790K @ 4.00GHz
Y axis: Speedup over 95b31d70
1.3x+-+-------------------------------------------------------------+-+
| cross $$ |
1.25x+-+.............................................................+-+
| |
1.2x+-+.............................................................+-+
| : |
| : |
1.15x+-+.............................................................+-+
| $$$$ $$$$ +++ : |
1.1x+-+.........$..$.$..$...........................................+-+
| $ $ $ $ $$$ $$$$ |
1.05x+-+.........$..$.$..$.....................$.$.$$$$......$..$....+-+
| $ $ $ $ +++ +++ +++ $+$ $++$ +++ $: $ $$$$ |
| +++ $ $ $ $ +++ $$$ : : $ $ $ $ $$$$ $: $ $++$ |
1x+-$$$$G$$$$_$EM$_$ro$s$$$..$.$.......$$$..$.$.$..$.$..$.$..$.$..$-+
| $++$ $ :$ $ $ $ $ $ $ $ $ : $+$ $ $ $ $ $++$ $: $ $ $ |
0.95x+-$..$.$..$.$..$.$..$.$.$..$.$..$$$..$.$..$.$.$..$.$..$.$..$.$..$-+
| $ $ $ $ $ $ $ $ $ $ $ $ $:$ $ $ $ $ $ $ $ $ $ $ $ $ |
0.9x+-$$$$-$$$$-$$$$-$$$$-$$$--$$$--$$$--$$$--$$$-$$$$-$$$$-$$$$-$$$$-+
astarbzip2gcc gobmh264rehmlibquantumcfomneperlbensjxalancbhmeanThat is, a hmean gain of 2.6%.
- specINT 2006 (train set), x86_64-softmmu. Host: Intel i7-4790K @ 4.00GHz
Y axis: Speedup over 95b31d70
1.25x+-+-------------------------------------------------------------+-+
| cross $$ |
| |
1.2x+-+.............................................................+-+
| : +++ |
1.15x+-+.............................................................+-+
| : $$$ $$$$ $$$$ |
| $$$$ +++ $:$ $++$ +++ $: $ |
1.1x+-+.........$..$.$$$$.....................$.$.$..$......$..$....+-+
| +++ $++$ $++$ +++ : $ $ $ $ : $++$ +++ |
1.05x+-+....$$$$.$..$.$..$......$$$............$.$.$..$.$$$$.$..$.$$$$-+
| $++$ $ $ $ $ $$$ $:$ $ $ $ $ $ :$ $ $ $ $ |
| $ $ $ $ $ $ $:$ $+$ +++ +++ $ $ $ $ $ :$ $ $ $ $ |
1x+-$$$$G$AP$_$EM$_$ro$s$i$li$e$..$$$.......$.$.$..$.$..$.$..$.$..$-+
| $++$ $ $ $ $ $ $ $+$ $ $ $:$ $$$ $ $ $ $ $ $ $ $ $ $ |
0.95x+-$..$.$..$.$..$.$..$.$.$..$.$..$.$..$.$..$.$.$..$.$..$.$..$.$..$-+
| $ $ $ $ $ $ $ $ $ $ $ $ $ $ $+$ $ $ $ $ $ $ $ $ $ $ |
| $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ |
0.9x+-$$$$-$$$$-$$$$-$$$$-$$$--$$$--$$$--$$$--$$$-$$$$-$$$$-$$$$-$$$$-+
astarbzip2gcc gobmh264rehmlibquantumcfomneperlbensjxalancbhmeanThis is the larger "train" set. We get a hmean improvement of 6.1%.
--- target/i386/helper.h | 2 ++ target/i386/misc_helper.c | 5 +++++ target/i386/translate.c | 14 +++++++++++++- 3 files changed, 20 insertions(+), 1 deletion(-)
target/i386/helper.h +2 -0
@@ -1,6 +1,8 @@ DEF_HELPER_FLAGS_4(cc_compute_all, TCG_CALL_NO_RWG_SE, tl, tl, tl, tl, int) DEF_HELPER_FLAGS_4(cc_compute_c, TCG_CALL_NO_RWG_SE, tl, tl, tl, tl, int) +DEF_HELPER_2(cross_page_check, i32, env, tl) + DEF_HELPER_3(write_eflags, void, env, tl, i32) DEF_HELPER_1(read_eflags, tl, env)DEF_HELPER_2 (divb_AL, void, env, tl)
target/i386/misc_helper.c +5 -0
@@ -637,3 +637,8 @@ void helper_wrpkru(CPUX86State *env, uint32_t ecx, uint64_t val) env->pkru = val;tlb_flush (cs); } + +uint32_t helper_cross_page_check (CPUX86State *env,target_ulong vaddr) +{ +return !!tb_from_jmp_cache (env, vaddr); +}
target/i386/translate.c +13 -1
@@ -2153,7 +2153,19 @@ static inline void gen_goto_tb(DisasContext *s, int tb_num, target_ulong eip)gen_jmp_im (eip);tcg_gen_exit_tb ((uintptr_t )s->tb + tb_num); }else { -/* jump to another page: currently not optimized */ +/* jump to another page */ +TCGv vaddr =tcg_const_tl (eip); +TCGv_i32 valid =tcg_temp_new_i32 (); +TCGLabel *label =gen_new_label (); + +gen_helper_cross_page_check (valid, cpu_env, vaddr); +tcg_temp_free (vaddr); +tcg_gen_brcondi_i32 (TCG_COND_EQ , valid,0 , label); +tcg_temp_free_i32 (valid); +tcg_gen_goto_tb (tb_num); +gen_jmp_im (eip); +tcg_gen_exit_tb ((uintptr_t )s->tb + tb_num); +gen_set_label (label);gen_jmp_im (eip);gen_eob (s); }
Signature · 1 line
2.7.4
PATCH 5/10 tcg: add jr opcode
In reply to Emilio G. CotaThis will be used by TCG targets to implement a fast path for indirect branches.
I only have implemented and tested this on an i386 host, so make this opcode optional and mark it as not implemented by other TCG backends.
--- tcg/aarch64/tcg-target.h | 1 + tcg/arm/tcg-target.h | 1 + tcg/i386/tcg-target.h | 1 + tcg/i386/tcg-target.inc.c | 7 +++++++ tcg/ia64/tcg-target.h | 1 + tcg/mips/tcg-target.h | 1 + tcg/ppc/tcg-target.h | 1 + tcg/s390/tcg-target.h | 1 + tcg/sparc/tcg-target.h | 1 + tcg/tcg-op.h | 6 ++++++ tcg/tcg-opc.h | 1 + tcg/tcg.c | 1 + tcg/tci/tcg-target.h | 1 + 13 files changed, 24 insertions(+)
tcg/aarch64/tcg-target.h +1 -0
@@ -77,6 +77,7 @@ typedef enum {#define TCG_TARGET_HAS_mulsh_i32 0#define TCG_TARGET_HAS_extrl_i64_i32 0#define TCG_TARGET_HAS_extrh_i64_i32 0 +#define TCG_TARGET_HAS_jr 0#define TCG_TARGET_HAS_div_i64 1#define TCG_TARGET_HAS_rem_i64 1
tcg/arm/tcg-target.h +1 -0
@@ -123,6 +123,7 @@ extern bool use_idiv_instructions;#define TCG_TARGET_HAS_mulsh_i32 0#define TCG_TARGET_HAS_div_i32 use_idiv_instructions#define TCG_TARGET_HAS_rem_i32 0 +#define TCG_TARGET_HAS_jr 0enum {TCG_AREG0 =TCG_REG_R6 ,
tcg/i386/tcg-target.h +1 -0
@@ -107,6 +107,7 @@ extern bool have_popcnt;#define TCG_TARGET_HAS_muls2_i32 1#define TCG_TARGET_HAS_muluh_i32 0#define TCG_TARGET_HAS_mulsh_i32 0 +#define TCG_TARGET_HAS_jr 1#if TCG_TARGET_REG_BITS ==64 #define TCG_TARGET_HAS_extrl_i64_i32 0
tcg/i386/tcg-target.inc.c +7 -0
@@ -1909,6 +1909,9 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,case INDEX_op_br:tcg_out_jxx (s,JCC_JMP ,arg_label (a0),0 );break ; +case INDEX_op_jr: +tcg_out_modrm (s,OPC_GRP5 , EXT5_JMPN_Ev, a0); +break ; OP_32_64(ld8u):/* Note that we can ignore REXW for the zero-extend to 64-bit. */ tcg_out_modrm_offset (s ,OPC_MOVZBL ,a0 ,a1 ,a2 );
@@ -2277,6 +2280,7 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,static const TCGTargetOpDef *tcg_target_op_def (TCGOpcode op) { +static const TCGTargetOpDef ri = { .args_ct_str = {"ri" } };static const TCGTargetOpDef ri_r = { .args_ct_str = {"ri" ,"r" } };static const TCGTargetOpDef re_r = { .args_ct_str = {"re" ,"r" } };static const TCGTargetOpDef qi_r = { .args_ct_str = {"qi" ,"r" } };
@@ -2324,6 +2328,9 @@ static const TCGTargetOpDef *tcg_target_op_def(TCGOpcode op)case INDEX_op_st_i64:return &re_r; +case INDEX_op_jr: +return &ri; +case INDEX_op_add_i32:case INDEX_op_add_i64:return &r_r_re;
tcg/ia64/tcg-target.h +1 -0
@@ -173,6 +173,7 @@ typedef enum {#define TCG_TARGET_HAS_mulsh_i64 0#define TCG_TARGET_HAS_extrl_i64_i32 0#define TCG_TARGET_HAS_extrh_i64_i32 0 +#define TCG_TARGET_HAS_jr 0#define TCG_TARGET_deposit_i32_valid (ofs, len) ((len) <= 16)#define TCG_TARGET_deposit_i64_valid (ofs, len) ((len) <= 16)
tcg/mips/tcg-target.h +1 -0
@@ -130,6 +130,7 @@ extern bool use_mips32r2_instructions;#define TCG_TARGET_HAS_muluh_i32 1#define TCG_TARGET_HAS_mulsh_i32 1#define TCG_TARGET_HAS_bswap32_i32 1 +#define TCG_TARGET_HAS_jr 0#if TCG_TARGET_REG_BITS ==64 #define TCG_TARGET_HAS_add2_i32 0
tcg/ppc/tcg-target.h +1 -0
@@ -82,6 +82,7 @@ extern bool have_isa_3_00;#define TCG_TARGET_HAS_muls2_i32 0#define TCG_TARGET_HAS_muluh_i32 1#define TCG_TARGET_HAS_mulsh_i32 1 +#define TCG_TARGET_HAS_jr 0#if TCG_TARGET_REG_BITS ==64 #define TCG_TARGET_HAS_add2_i32 0
tcg/s390/tcg-target.h +1 -0
@@ -92,6 +92,7 @@ extern uint64_t s390_facilities;#define TCG_TARGET_HAS_mulsh_i32 0#define TCG_TARGET_HAS_extrl_i64_i32 0#define TCG_TARGET_HAS_extrh_i64_i32 0 +#define TCG_TARGET_HAS_jr 0#define TCG_TARGET_HAS_div2_i64 1#define TCG_TARGET_HAS_rot_i64 1
tcg/sparc/tcg-target.h +1 -0
@@ -123,6 +123,7 @@ extern bool use_vis3_instructions;#define TCG_TARGET_HAS_muls2_i32 1#define TCG_TARGET_HAS_muluh_i32 0#define TCG_TARGET_HAS_mulsh_i32 0 +#define TCG_TARGET_HAS_jr 0#define TCG_TARGET_HAS_extrl_i64_i32 1#define TCG_TARGET_HAS_extrh_i64_i32 1
tcg/tcg-op.h +6 -0
@@ -261,6 +261,12 @@ static inline void tcg_gen_br(TCGLabel *l)tcg_gen_op1 (&tcg_ctx, INDEX_op_br,label_arg (l)); } +/* jump to a host address contained in a register */ +static inline void tcg_gen_jr (TCGv_ptr arg) +{ +tcg_gen_op1i (INDEX_op_jr,GET_TCGV_PTR (arg)); +} +void tcg_gen_mb (TCGBar );/* Helper calls. */
tcg/tcg-opc.h +1 -0
@@ -34,6 +34,7 @@ DEF(set_label, 0, 0, 1, TCG_OPF_BB_END | TCG_OPF_NOT_PRESENT) DEF(call, 0, 0, 3, TCG_OPF_CALL_CLOBBER | TCG_OPF_NOT_PRESENT) DEF(br, 0, 0, 1, TCG_OPF_BB_END) +DEF(jr, 0, 1, 0, TCG_OPF_BB_END)#define IMPL (X ) (__builtin_constant_p(X) && !(X) ? TCG_OPF_NOT_PRESENT : 0)#if TCG_TARGET_REG_BITS ==32
tcg/tcg.c +1 -0
@@ -1139,6 +1139,7 @@ void tcg_dump_ops(TCGContext *s)switch (c) {case INDEX_op_set_label:case INDEX_op_br: +case INDEX_op_jr:case INDEX_op_brcond_i32:case INDEX_op_brcond_i64:case INDEX_op_brcond2_i32:
tcg/tci/tcg-target.h +1 -0
@@ -85,6 +85,7 @@#define TCG_TARGET_HAS_muls2_i32 0#define TCG_TARGET_HAS_muluh_i32 0#define TCG_TARGET_HAS_mulsh_i32 0 +#define TCG_TARGET_HAS_jr 0#if TCG_TARGET_REG_BITS ==64 #define TCG_TARGET_HAS_extrl_i64_i32 0
Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH 05/10] tcg: add jr opcode
Quoted 6 lines · Emilio G. Cota
This will be used by TCG targets to implement a fast path for indirect branches.
I only have implemented and tested this on an i386 host, so make this opcode optional and mark it as not implemented by other TCG backends.
Please don't forget to document this in tcg/README.
Thanks,
Paolo
Re: [Qemu-devel] [PATCH 05/10] tcg: add jr opcode
In reply to Emilio G. CotaQuoted 6 lines · Emilio G. Cota
This will be used by TCG targets to implement a fast path for indirect branches.
I only have implemented and tested this on an i386 host, so make this opcode optional and mark it as not implemented by other TCG backends.
I don't think this is quite the right abstraction. In particular, if we can always return a valid address from the helper, we can eliminate a conditional branch.
I think this should work as follows:
(1) tb_ret_addr gets moved into TCGContext so that it's available for other code to see.
(2) Have a generic helper
void *HELPER(lookup_tb_ptr)(CPUArchState *env, target_ulong addr)
{
TranslationBlock *tb = tb_from_jmp_cache(env, addr);
return tb ? tb->tc_ptr : tcg_ctx.tb_ret_addr;
}(3) Emit TCG opcodes like
call t0,lookup_tb_ptr,env,addr
jmp_tb t0
(4) Emit code for jmp_tb like
mov %rax,%rdx // save target into new register
xor %eax,%eax // set return value a-la exit_tb
jmp *%edx // branch to tb or epilogue.
(5) There needs to be a convenience function in tcg/tcg-op.c. If the host does not support jmp_tb, we should just generate exit_tb like we do now. There should be no ifdefs inside target/*.
r~
Re: [Qemu-devel] [PATCH 05/10] tcg: add jr opcode
Quoted 8 lines · Richard Henderson
Quoted 6 lines · Emilio G. Cota
This will be used by TCG targets to implement a fast path for indirect branches.
I only have implemented and tested this on an i386 host, so make this opcode optional and mark it as not implemented by other TCG backends.
Quoted 5 lines
I don't think this is quite the right abstraction. In particular, if we can always return a valid address from the helper, we can eliminate a conditional branch.
I think this should work as follows:
(snip)
Yes that's much better. In fact in the cover letter I forgot to mention that the code by the paper authors does something similar to avoid the branch.
I went with the design with a branch because (1) I wasn't sure that exporting tb_ret_addr would get your approval and (2) my knowledge of TCG backend code is shamefully poor.
Will work on a v2. Thanks for the feedback!
Emilio
PATCH 6/10 tcg: add brcondi_ptr
In reply to Emilio G. CotaThis will be used by TCG targets to implement a fast path for indirect branches.
--- tcg/tcg-op.h | 4 ++++ 1 file changed, 4 insertions(+)
tcg/tcg-op.h +4 -0
@@ -1118,6 +1118,8 @@ void tcg_gen_atomic_xor_fetch_i64(TCGv_i64, TCGv, TCGv_i64, TCGArg, TCGMemOp); tcg_gen_addi_i32(TCGV_PTR_TO_NAT(R), TCGV_PTR_TO_NAT(A), (B))# define tcg_gen_ext_i32_ptr (R ,A ) \ tcg_gen_mov_i32(TCGV_PTR_TO_NAT(R), (A)) +# define tcg_gen_brcondi_ptr (C ,A ,I ,L ) \ + tcg_gen_brcondi_i32(C, TCGV_PTR_TO_NAT(A), (uintptr_t)I, L)#else # define tcg_gen_ld_ptr (R ,A ,O ) \ tcg_gen_ld_i64(TCGV_PTR_TO_NAT(R), (A), (O))
@@ -1129,4 +1131,6 @@ void tcg_gen_atomic_xor_fetch_i64(TCGv_i64, TCGv, TCGv_i64, TCGArg, TCGMemOp); tcg_gen_addi_i64(TCGV_PTR_TO_NAT(R), TCGV_PTR_TO_NAT(A), (B))# define tcg_gen_ext_i32_ptr (R ,A ) \ tcg_gen_ext_i32_i64(TCGV_PTR_TO_NAT(R), (A)) +# define tcg_gen_brcondi_ptr (C ,A ,I ,L ) \ + tcg_gen_brcondi_i64(C, TCGV_PTR_TO_NAT(A), (uintptr_t)I, L)#endif /* UINTPTR_MAX == UINT32_MAX */
Signature · 1 line
2.7.4
PATCH 7/10 tcg: add tcg_temp_local_new_ptr
In reply to Emilio G. CotaThis will be used by TCG targets to implement a fast path for indirect branches.
--- tcg/tcg.h | 2 ++ 1 file changed, 2 insertions(+)
tcg/tcg.h +2 -0
@@ -912,6 +912,7 @@ do {\#define tcg_global_mem_new_ptr (R ,O ,N ) \ TCGV_NAT_TO_PTR(tcg_global_mem_new_i32((R), (O), (N)))#define tcg_temp_new_ptr () TCGV_NAT_TO_PTR(tcg_temp_new_i32()) +#define tcg_temp_local_new_ptr () TCGV_NAT_TO_PTR(tcg_temp_local_new_i32())#define tcg_temp_free_ptr (T ) tcg_temp_free_i32(TCGV_PTR_TO_NAT(T))#else #define TCGV_NAT_TO_PTR (n) MAKE_TCGV_PTR(GET_TCGV_I64(n))
@@ -923,6 +924,7 @@ do {\#define tcg_global_mem_new_ptr (R ,O ,N ) \ TCGV_NAT_TO_PTR(tcg_global_mem_new_i64((R), (O), (N)))#define tcg_temp_new_ptr () TCGV_NAT_TO_PTR(tcg_temp_new_i64()) +#define tcg_temp_local_new_ptr () TCGV_NAT_TO_PTR(tcg_temp_local_new_i64())#define tcg_temp_free_ptr (T ) tcg_temp_free_i64(TCGV_PTR_TO_NAT(T))#endif
Signature · 1 line
2.7.4
PATCH 8/10 target/arm: optimize indirect branches with TCG's jr op
In reply to Emilio G. CotaSpeed up indirect branches by adding a helper to look for the TB in tb_jmp_cache. The helper returns either the corresponding host address or NULL.
Measurements:
- Impact on Boot time
| setup | ARM debian boot+shutdown time | stddev | |---------+-------------------------------+--------| | master | 10.050247057 | 0.0361 | | +cross | 10.311265443 | 0.0721 | | +jr | 10.216832579 | 0.0878 | | +inline | 10.405597879 | 0.0332 |
That is, a 3.5% slowdown. This is reasonable since booting has low hit rates in tb_jmp_cache.
- NBench, arm-linux-user. Host: Intel i7-4790K @ 4.00GHz
Y axis: speedup over 95b31d70
1.25x+-+-------------------------------------------------------------+-+
| jr $$$ |
| jr+inline %%% |
1.2x+-+..................................$$$%%......................+-+
| $ $ % |
| $ $ % |
| %%% $ $ % %% |
1.15x+-+........................%.%.......$.$.%.$$$%.................+-+
| % % $ $ % $ $% |
| $$$ % $ $ % $ $% |
1.1x+-+......................$.$.%.......$.$.%.$.$%.................+-+
| $ $ % $ $ % $ $% |
| $ $ % $$$ $ $ % $ $% %%% |
1.05x+-+......................$.$.%.$.$%%.$.$.%.$.$%.............$$$.%-+
| $$$%% $$%% $ $ % $ $ % $ $ % $ $% $ $ % |
| $$$%% $ $ % $$ % $ $ % $ $ % $ $ % $ $% $ $ % |
| $ $ % $ $ % $$ % $ $ % $ $ % $ $ % $ $% %%% $$$%% $ $ % |
1x+-$.$B%R$$$%%G$A$H%T$$P%j$+$n%i$e$.%.$.$.%.$.$%.$$$.%.$.$.%.$.$.%-+
+-$$$%%-$$$%%-$$$%%-$$%%-$$$%%-$$$%%-$$$%%-$$$%-$$$%%-$$$%%-$$$%%-+
ASSIGNMBITFIELFOFP_EMULATHUFFMANLU_DECOMPNEURNUMERICSTRING_SOhmeanThat is, a 6.65% hmean improvement with jr+inline (5.92% w/o inlining). Peak improvement is 21% for HUFFMAN.
- NBench, arm-softmmu. Host: Intel i7-4790K @ 4.00GHz
Y axis: speedup over 95b31d70
+------------------------------------------------------------------+
| |
1.3x+-+........................................ cross+noinline $$ +-+
| cross+inline %% |
| && @@&& cross+jr+noinline @@ |
| $$%@& @@ & cross+jr+inline && |
1.2x+-+.................$$%@&......$$..&&..@@.&......................+-+
| $$%@& $$%%@& @@ & @@& |
| $$%@& $$ %@& @@ & @@& |
1.1x+-+.................$$%@&...@@.$$.%@&..@@.&..@@&................&&-+
| $$%@& $$%@& @@&$$ %@& @@ & @@& @@& |
| $$%@& $$%@& @@&$$ %@&$$%@ & @@& $$%@& $$%@& |
| $$%&& $$%&& $$%@& $$%@&$$$%@&$$ %@&$$%@ & %%@& $$%@& $$%@& |
1x+-$$%@&A$$%@&A$$%@&A$$%@&$R$%@&$$T%@&$$%@s&+%%@&n$$%@&.$$%@&.$$%@&-+
| $$%@& $$%@& $$%@& $$%@&$ $%@&$$ %@&$$%@ & %%@& $$%@& $$%@& $$%@& |
| $$%@& $$%@& $$%@& $$%@&$ $%@&$$ %@&$$%@ & %%@& $$%@& $$%@& $$%@& |
0.9x+-$$%@&.$$%@&.$$%@&.$$%@&$.$%@&$$.%@&$$%@.&.%%@&.$$%@&.$$%@&.$$%@&-+
| $$%@& $$%@& $$%@& $$%@&$ $%@&$$ %@&$$%@ & %%@& $$%@& $$%@& $$%@& |
| $$%@& $$%@& $$%@& $$%@&$ $%@&$$ %@&$$%@ &$$%@& $$%@& $$%@& $$%@& |
| $$%@& $$%@& $$%@& $$%@&$ $%@&$$ %@&$$%@ &$$%@& $$%@& $$%@& $$%@& |
0.8x+-$$%@&-$$%@&-$$%@&-$$%@&$$$%@&$$%%@&$$%@&&$$%@&-$$%@&-$$%@&-$$%@&-+
ASSIGNMBITFIELFOUFP_EMULATHUFFMALU_DECOMPNEURANUMERICSTRING_SOhmeanThat is, a 9.86% hmean improvement when combining cross+jr+inline (this commit) over current master. Peak improvement is 25% for FP_EMULATION.
--- target/arm/helper.c | 11 +++++++++++ target/arm/helper.h | 1 + target/arm/translate.c | 23 +++++++++++++++++++++++ 3 files changed, 35 insertions(+)
target/arm/helper.c +11 -0
@@ -9927,3 +9927,14 @@ uint32_t HELPER(cross_page_check)(CPUARMState *env, target_ulong vaddr) {return !!tb_from_jmp_cache (env, vaddr); } + +void *HELPER (get_hostptr )(CPUARMState *env,target_ulong vaddr) +{ +TranslationBlock *tb; + + tb =tb_from_jmp_cache (env, vaddr); +if (unlikely (tb ==NULL )) { +return NULL ; + } +return tb->tc_ptr; +}
target/arm/helper.h +1 -0
@@ -2,6 +2,7 @@ DEF_HELPER_FLAGS_1(sxtb16, TCG_CALL_NO_RWG_SE, i32, i32) DEF_HELPER_FLAGS_1(uxtb16, TCG_CALL_NO_RWG_SE, i32, i32) DEF_HELPER_2(cross_page_check, i32, env, tl) +DEF_HELPER_2(get_hostptr, ptr, env, tl) DEF_HELPER_3(add_setq, i32, env, i32, i32)DEF_HELPER_3 (add_saturate, i32, env, i32, i32)
target/arm/translate.c +23 -0
@@ -65,6 +65,14 @@ static TCGv_i32 cpu_R[16];TCGv_i32 cpu_CF, cpu_NF, cpu_VF, cpu_ZF;TCGv_i64 cpu_exclusive_addr;TCGv_i64 cpu_exclusive_val; +static bool gen_jr; + +static inline void set_jr (void ) +{ +if (TCG_TARGET_HAS_jr) { + gen_jr = true; + } +}/* FIXME: These should be removed. */ static TCGv_i32 cpu_F0s, cpu_F1s;
@@ -221,6 +229,7 @@ static void store_reg(DisasContext *s, int reg, TCGv_i32 var)*/ tcg_gen_andi_i32 (var, var, s->thumb ? ~1 : ~3 ); s->is_jmp =DISAS_JUMP ; +set_jr (); }tcg_gen_mov_i32 (cpu_R[reg], var);tcg_temp_free_i32 (var);
@@ -893,6 +902,7 @@ static inline void gen_bx_im(DisasContext *s, uint32_t addr)tcg_temp_free_i32 (tmp); }tcg_gen_movi_i32 (cpu_R[15 ], addr & ~1 ); +set_jr (); }/* Set PC and Thumb state from var. var is marked as dead. */
@@ -902,6 +912,7 @@ static inline void gen_bx(DisasContext *s, TCGv_i32 var)tcg_gen_andi_i32 (cpu_R[15 ], var, ~1 );tcg_gen_andi_i32 (var, var,1 );store_cpu_field (var, thumb); +set_jr (); }/* Variant of store_reg which uses branch&exchange logic when storing
@@ -12042,6 +12053,18 @@ void gen_intermediate_code(CPUARMState *env, TranslationBlock *tb)gen_set_pc_im (dc, dc->pc);/* fall through */ case DISAS_JUMP : +if (TCG_TARGET_HAS_jr && gen_jr) { +TCGv_ptr ptr =tcg_temp_local_new_ptr (); +TCGLabel *label =gen_new_label (); + + gen_jr = false; +gen_helper_get_hostptr (ptr, cpu_env, cpu_R[15 ]); +tcg_gen_brcondi_ptr (TCG_COND_EQ , ptr,NULL , label); +tcg_gen_jr (ptr); +tcg_temp_free_ptr (ptr); +gen_set_label (label); +/* fall through */ + }default :/* indicate that the hash table must be used to find the next TB */ tcg_gen_exit_tb (0 );
Signature · 1 line
2.7.4
PATCH 9/10 target/i386: optimize indirect branches with TCG's jr op
In reply to Emilio G. CotaSpeed up indirect branches by adding a helper to look for the TB in tb_jmp_cache. The helper returns either the corresponding host address or NULL.
Measurements:
- NBench, x86_64-linux-user. Host: Intel i7-4790K @ 4.00GHz
Y axis: Speedup over 95b31d70
1.1x+-+-------------------------------------------------------------+-+
| jr $$ |
1.08x+-+...... jr+inline %% ..................................+-+
| |
| $$$ |
1.06x+-$.$............................%%%............................+-+
| $ $%% % % |
1.04x+-$.$.%..........................%.%............................+-+
| $ $ % $$$ % $$$ |
| $ $ % %%% $ $ % $ $%% |
1.02x+-$.$.%.........%%%.$$.%.......$.$.%...%%%...%%.......$.$.%.$$$%%-+
| $ $ % % % $$ % $$$ $ $ % $$$ % %% $$$%% $ $ % $ $ % |
1x+-$.$B%R$$$ARGRA%H%T$$P%j$+$%%i$e$.%.$.$.%.$$$%.$.$.%.$.$.%.$.$.%-+
| $ $ % $ $%% $$$ % $$ % $ $ % $ $ % $ $ % $ $% $ $ % $ $ % $ $ % |
0.98x+-$.$.%.$.$.%.$.$.%.$$.%.$.$.%.$.$.%.$.$.%.$.$%.$.$.%.$.$.%.$.$.%-+
| $ $ % $ $ % $ $ % $$ % $ $ % $ $ % $ $ % $ $% $ $ % $ $ % $ $ % |
| $ $ % $ $ % $ $ % $$ % $ $ % $ $ % $ $ % $ $% $ $ % $ $ % $ $ % |
0.96x+-$.$.%.$.$.%.$.$.%.$$.%.$.$.%.$.$.%.$.$.%.$.$%.$.$.%.$.$.%.$.$.%-+
+-$$$%%-$$$%%-$$$%%-$$%%-$$$%%-$$$%%-$$$%%-$$$%-$$$%%-$$$%%-$$$%%-+
ASSIGNMBITFIELFOFP_EMULATHUFFMANLU_DECOMPNEURNUMERICSTRING_SOhmeanThe fact that NBench is not very sensitive to changes here is a little surprising, especially given the significant improvements for ARM shown in the previous commit. I wonder whether the compiler is doing a better job compiling the x86_64 version (I'm using gcc 5.4.0), or I'm simply missing some i386 instructions to which the jr optimization should be applied.
specINT 2006 (test set), x86_64-linux-user. Host: Intel i7-4790K @ 4.00GHz
Y axis: Speedup over 95b31d70
1.3x+-+-------------------------------------------------------------+-+
| jr+inline $$ |
1.25x+-+.............................................................+-+
| |
1.2x+-+.............................................................+-+
| |
| +++ +++ |
1.15x+-+...................$$$.................$$$...................+-+
| $ $ $:$ |
1.1x+-+...................$.$.................$.$...........$$$$....+-+
| +++ $ $ $ $ +++ $++$ |
1.05x+-+.........$$$$......$.$.................$.$...........$..$....+-+
| $ $ $ $ $$$ $ $ $$$$ $$$$ $ $ $$$$ |
| $$$$ +++ $ $ +++ $ $ $ $ +++ $$$ $ $ $ $ $++$ $ $ $ $ |
1x+-$BA$G$$$$_$EM$_$$$$.$.$..$.$..$$$..$.$..$.$.$..$.$..$.$..$.$..$-+
| $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ |
0.95x+-$..$.$..$.$..$.$..$.$.$..$.$..$.$..$.$..$.$.$..$.$..$.$..$.$..$-+
| $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ |
0.9x+-$$$$-$$$$-$$$$-$$$$-$$$--$$$--$$$--$$$--$$$-$$$$-$$$$-$$$$-$$$$-+
astarbzip2gcc gobmh264rehmlibquantumcfomneperlbensjxalancbhmeanThat is a 4.4% hmean perf improvement.
- specINT 2006 (train set), x86_64-linux-user. Host: Intel i7-4790K @ 4.00GHz
Y axis: Speedup over 95b31d70
1.4x+-+--------------------------------------------------------------+-+
| jr $$ |
| |
1.3x+-+..............................................................+-+
| |
| |
1.2x+-+......................................................$$$$....+-+
| +++ $$$$ : $++$ |
| $$$$ $$$$ $ $ : $ $ |
1.1x+-+...................$..$................$..$.$..$.$$$$.$..$....+-+
| $ $ $ $ $ $ $: $ $ $ +++ |
| +++ +++ +++ $ $ $$$$ +++ $ $ $ $ $: $ $ $ $$$$ |
1x+-$$$$GRAPH_$$$$_$$$$.$..$.$..$.$$$$......$..$.$..$.$..$.$..$.$..$-+
| $++$ $$$$ $ $ $++$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ |
| $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ |
0.9x+-$..$.$..$.$..$.$..$.$..$.$..$.$..$......$..$.$..$.$..$.$..$.$..$-+
| $ $ $ $ $ $ $ $ $ $ $ $ $ $ $$$$ $ $ $ $ $ $ $ $ $ $ |
| $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ |
0.8x+-$$$$-$$$$-$$$$-$$$$-$$$$-$$$$-$$$$-$$$$-$$$$-$$$$-$$$$-$$$$-$$$$-+
astarbzip2 gcc gobmh264rehmlibquantmcfomneperlbensjexalancbhmeanThat is, a 4.39 % hmean improvement for jr+inline, i.e. this commit. (4.5% for noinline). Peak improvement is 20% for xalancbmk.
- specINT 2006 (test set), x86_64-softmmu. Host: Intel i7-4790K @ 4.00GHz
Y axis: Speedup over 95b31d70
1.3x+-+-------------------------------------------------------------+-+
| cross $$ |
1.25x+-+..... jr %% .........................................+-+
| cross+jr @@ : |
1.2x+-+.............................................................+-+
| : : |
| +++ : : |
1.15x+-+...........@@................................................+-+
| $$@@ $$++ +++ : @@ |
1.1x+-+.........$$@@.$$@@.....................................@@....+-+
| $$@@ $$@@ $$ : @@@ +++$$@@ |
1.05x+-+.........$$@@.$$@@...@@...............$$...$$@.@.....$$@@....+-+
| +++$$%@ $$@@ %%@+++++++++++++++$$+: $$@ @++@@ $$%@+$$@@+|
| +@@+++@@+$$%@ $$@@++%%@$$$% ::@@ ::@@$$@@@$$% @$$@@ $$%@+$$@@ |
1x+-$$%@A$$%@R$$%@R$$%@$$$%@$_$%@s%%%@$$%%@$$@.@$$%.@$$@@.$$%@.$$%@-+
|+$$%@ $$%@ $$%@ $$%@$ $%@$+$%@ %+%@$$+%@$$@+@$$% @$$@@ $$%@+$$%@ |
0.95x+-$$%@.$$%@.$$%@.$$%@$.$%@$.$%@$$.%@$$.%@$$@.@$$%.@$$%@.$$%@.$$%@-+
| $$%@ $$%@ $$%@ $$%@$ $%@$ $%@$$ %@$$ %@$$%+@$$% @$$%@ $$%@ $$%@ |
0.9x+-$$%@-$$%@-$$%@-$$%@$$$%@$$$%@$$%%@$$%%@$$%@@$$%@@$$%@-$$%@-$$%@-+
astabzip2 gcc gobmh264rehmlibquantumcfomneperlbensjexalanchmeanHere we see how jr works best when combined with cross -- jr by itself is disappointingly around baseline performance. I attribute this to the frequent page invalidations and/or TLB flushes (I'm running Ubuntu 16.04 as the guest, so there are many processes), which lowers the maximum attainable hit rate in tb_jmp_cache.
Overall the greatest hmean improvement comes from cross+jr though.
- specINT 2006 (train set), x86_64-softmmu. Host: Intel i7-4790K @ 4.00GHz
Y axis: Speedup over 95b31d70
1.25x+-+-------------------------------------------------------------+-+
| cross+inline $$ |
| cross+jr+inline %% +++ +++ |
1.2x+-+.............................................................+-+
| : : +++ |
1.15x+-+.......................................................%%....+-+
| :: +++ $$$ $$$% $$$% |
| $$%%++%%% $:$ $+$% +++ $:$% |
1.1x+-+.........$$.%.$$.%....................$.$..$.$%......$.$%....+-+
| +++ $$+%+$$ %+++++ :+++ $ $: $ $% :%% $+$% +++ |
1.05x+-+....$$...$$.%.$$.%......$$............$.$%.$.$%.$$$%.$.$%.$$%%-+
| $$%% $$ % $$ % $$%% $$: +++ $ $% $ $% $:$% $ $% $$+% |
| $$+% $$ % $$ % $$:%+$$%%+++: +++ $ $%+$ $% $:$% $ $% $$ % |
1x+-$$$AR$$A%G$$P%_$$M%_$$o%s$$r%$$$%%e....$.$%.$.$%.$.$%.$.$%.$$.%-+
| $+$% $$ % $$ % $$ %+$$+% $$:%$:$+%$$$++$ $% $ $% $ $% $ $% $$ % |
0.95x+-$.$%.$$.%.$$.%.$$.%.$$.%.$$.%$.$.%$.$..$.$%.$.$%.$.$%.$.$%.$$.%-+
| $ $% $$ % $$ % $$ % $$ % $$ %$ $ %$+$% $ $% $ $% $ $% $ $% $$ % |
| $ $% $$ % $$ % $$ % $$ % $$ %$ $ %$ $% $ $% $ $% $ $% $ $% $$ % |
0.9x+-$$$%-$$%%-$$%%-$$%%-$$%%-$$%%$$$%%$$$%-$$$%-$$$%-$$$%-$$$%-$$%%-+
astabzip2 gcc gobmh264rehmlibquantumcfomneperlbensjexalanchmeanThis is the larger "train" set of SPECint06. Here cross+jr comes slightly below cross, but it's within the noise margins (I didn't run this many times, since it takes several hours).
--- target/i386/helper.h | 1 + target/i386/misc_helper.c | 11 +++++++++++ target/i386/translate.c | 42 +++++++++++++++++++++++++++++++++--------- 3 files changed, 45 insertions(+), 9 deletions(-)
target/i386/helper.h +1 -0
@@ -2,6 +2,7 @@ DEF_HELPER_FLAGS_4(cc_compute_all, TCG_CALL_NO_RWG_SE, tl, tl, tl, tl, int) DEF_HELPER_FLAGS_4(cc_compute_c, TCG_CALL_NO_RWG_SE, tl, tl, tl, tl, int) DEF_HELPER_2(cross_page_check, i32, env, tl) +DEF_HELPER_2(get_hostptr, ptr, env, tl) DEF_HELPER_3(write_eflags, void, env, tl, i32)DEF_HELPER_1 (read_eflags, tl, env)
target/i386/misc_helper.c +11 -0
@@ -642,3 +642,14 @@ uint32_t helper_cross_page_check(CPUX86State *env, target_ulong vaddr) {return !!tb_from_jmp_cache (env, vaddr); } + +void *helper_get_hostptr (CPUX86State *env,target_ulong vaddr) +{ +TranslationBlock *tb; + + tb =tb_from_jmp_cache (env, vaddr); +if (unlikely (tb ==NULL )) { +return NULL ; + } +return tb->tc_ptr; +}
target/i386/translate.c +33 -9
101 lines not on this page · raw message
Signature · 1 line
2.7.4
Re: [PATCH 09/10] target/i386: optimize indirect branches with TCG's jr op
Quoted 7 lines · Emilio G. Cota
The fact that NBench is not very sensitive to changes here is a little surprising, especially given the significant improvements for ARM shown in the previous commit. I wonder whether the compiler is doing a better job compiling the
x86_64version (I'm using gcc 5.4.0), or I'm simply missing some i386 instructions to which the jr optimization should be applied.
Maybe it is "ret"? That would be a straightforward "bx lr" on ARM, but it is missing in your i386 patch.
Paolo
Re: [PATCH 09/10] target/i386: optimize indirect branches with TCG's jr op
Quoted 11 lines · Paolo Bonzini
Quoted 7 lines · Emilio G. Cota
The fact that NBench is not very sensitive to changes here is a little surprising, especially given the significant improvements for ARM shown in the previous commit. I wonder whether the compiler is doing a better job compiling the
x86_64version (I'm using gcc 5.4.0), or I'm simply missing some i386 instructions to which the jr optimization should be applied.
Quoted 2 lines
Maybe it is "ret"? That would be a straightforward "bx lr" on ARM, but it is missing in your i386 patch.
Yes I missed that. I added this fix-up:
target/i386/translate.c +2 -2
@@ -6430,7 +6430,7 @@ static target_ulong disas_insn(CPUX86State *env, DisasContext *s,/* Note that gen_pop_T0 uses a zero-extending load. */ gen_op_jmp_v (cpu_T0);gen_bnd_jmp (s); -gen_eob (s); +gen_jr (s, cpu_T0);break ;case 0xc3 :/* ret */ ot =gen_pop_T0 (s);
@@ -6438,7 +6438,7 @@ static target_ulong disas_insn(CPUX86State *env, DisasContext *s,/* Note that gen_pop_T0 uses a zero-extending load. */ gen_op_jmp_v (cpu_T0);gen_bnd_jmp (s); -gen_eob (s); +gen_jr (s, cpu_T0);break ;case 0xca :/* lret im */ val =cpu_ldsw_code (env, s->pc);
Any other instructions I should look into? Perhaps lret/lret im?
Anyway, nbench does not improve much with the above. The reason seems to be that it's full of direct jumps (visible with -d in_asm). Also tried softmmu to see whether these jumps are in-page or not: peak improvement is ~8%, so I guess most of them are in-page. See http://imgur.com/EKRrYUz
I'm running new tests on a server with no other users and which has frequency scaling disabled. This should help get less noisy numbers, since I'm having trouble replicating my own results :> (I used my desktop machine until now). Will post these numbers tomorrow (running overnight SPECint both train and set sizes).
Thanks,
Emilio
Re: [PATCH 09/10] target/i386: optimize indirect branches with TCG's jr op
Quoted 2 lines
Any other instructions I should look into? Perhaps lret/lret im?
Possibly (for completeness), but they are extremely rare in 32- and 64-bit code.
You also didn't cover any of syscall/sysret and sysenter/sysexit in your patch, which would be on a relatively slow path but not _that_ slow. But that probably should be a separate patch, moving the env->eip assignment from seg_helper.c to translate.c and using the resulting TCGv as the argument for jr.
Paolo
Quoted 15 lines
Anyway, nbench does not improve much with the above. The reason seems to be that it's full of direct jumps (visible with -d
in_asm). Also tried softmmu to see whether these jumps are in-page or not: peak improvement is ~8%, so I guess most of them are in-page. See http://imgur.com/EKRrYUzI'm running new tests on a server with no other users and which has frequency scaling disabled. This should help get less noisy numbers, since I'm having trouble replicating my own results :> (I used my desktop machine until now). Will post these numbers tomorrow (running overnight SPECint both train and set sizes).
Thanks,
Emilio
PATCH 10/10 tb-hash: improve tb_jmp_cache hash function in user mode
In reply to Emilio G. CotaOptimizations to cross-page chaining and indirect jumps make performance more sensitive to the hit rate of tb_jmp_cache. The constraint of reserving some bits for the page number lowers the achievable quality of the hashing function.
However, user-mode does not have this requirement. Thus, with this change we use for user-mode a hashing function that is both faster and of better quality than the previous one.
Measurements:
- specINT 2006 (test set), x86_64-linux-user. Host: Intel i7-4790K @ 4.00GHz
Y axis: Speedup over 95b31d70
1.3x+-+-------------------------------------------------------------+-+
| jr $$ |
1.25x+-+.... jr+xxhash %% ....................................+-+
| jr+hash+inline @@ +++ |
1.2x+-+.............................................................+-+
| @@@ |
| +++@@ ++@:@ +++ @@+ |
1.15x+-+..................$$$@@...............$$@.@.......@@...@@....+-+
| $ $@@ $$@ @ %%@ @@ |
1.1x+-+..................$.$@@...............$$@.@......%%@.$$@@....+-+
| +++@@+ $ $@@ $$@ @ ++%%@+$$@@ +++|
1.05x+-+.........$$@@.....$.$@@...@@..........$$@.@..@@@.%%@.$$@@...@@-+
| $$@@ $ $@@$$$@@ $$% @$$@+@$$%@ $$@@+$$@@ |
|+$$++++++++$$@@+++@@$ $@@$+$@@+++@@$$+@@$$% @$$@+@$$%@ $$%@ $$@@ |
1x+-$$@@A$$%@R$$@@R$$@@$_$%@$_$%@$$s@@$$%%@$$%.@$$%.@$$%@.$$%@.$$%@-+
| $$@@+$$%@ $$%@ $$@@$+$%@$ $%@$$%%@$$+%@$$% @$$% @$$%@ $$%@ $$%@ |
0.95x+-$$%@.$$%@.$$%@.$$%@$.$%@$.$%@$$.%@$$.%@$$%.@$$%.@$$%@.$$%@.$$%@-+
| $$%@ $$%@ $$%@ $$%@$ $%@$ $%@$$ %@$$ %@$$% @$$% @$$%@ $$%@ $$%@ |
0.9x+-$$%@-$$%@-$$%@-$$%@$$$%@$$$%@$$%%@$$%%@$$%@@$$%@@$$%@-$$%@-$$%@-+
astabzip2 gcc gobmh264rehmlibquantumcfomneperlbensjexalanchmeanThat is, a 6.45% hmean improvement for this commit. Note that this is the test set, so some benchmarks take almost no time (and therefore aren't that sensitive to changes here). See "train" results below.
Note also that hashing quality is not the only requirement: xxhash gives on average the highest hit rates. However, the time spent computing the hash negates the performance gains coming from the increase in hit rate. Given these results, I dropped xxhash from subsequent experiments.
- specINT 2006 (train set), x86_64-linux-user. Host: Intel i7-4790K @ 4.00GHz
Y axis: Speedup over 95b31d70
1.4x+-+--------------------------------------------------------------+-+
| jr $$ +++ |
| jr+hash %% : |
1.3x+-+.......................................................%%%....+-+
| +++ +++ %:% |
| +++ %%% : %+% |
1.2x+-+.....................%%......................%.%..%%%.$$.%....+-+
| ++%% %%% $$+% %:% $$+% |
| +++ $$$% $$+% $$ % %:% $$ % |
1.1x+-+...........%%......$.$%................$$.%.$$.%.$$.%.$$.%..%%%-+
| +++ %% $ $% +++ $$ % $$ % $$ % $$ % +%+% |
| ++%% +++ ++%% ++%% $ $% $$$+ +++ %%% $$ % $$ % $$ % $$ % $$+% |
1x+-$$$%RGR%%R$$$%H$$$%P$j$%h$s$%.$$%%..%.%.$$.%.$$.%.$$.%.$$.%.$$.%-+
| $+$% $$$% $ $% $+$% $ $% $ $% $$+% % % $$ % $$ % $$ % $$ % $$ % |
| $ $% $ $% $ $% $ $% $ $% $ $% $$ % % % $$ % $$ % $$ % $$ % $$ % |
0.9x+-$.$%.$.$%.$.$%.$.$%.$.$%.$.$%.$$.%..%.%.$$.%.$$.%.$$.%.$$.%.$$.%-+
| $ $% $ $% $ $% $ $% $ $% $ $% $$ % $$+% $$ % $$ % $$ % $$ % $$ % |
| $ $% $ $% $ $% $ $% $ $% $ $% $$ % $$ % $$ % $$ % $$ % $$ % $$ % |
0.8x+-$$$%-$$$%-$$$%-$$$%-$$$%-$$$%-$$%%-$$%%-$$%%-$$%%-$$%%-$$%%-$$%%-+
astarbzip2 gcc gobmh264rehlibquantumcfomneperlbensjexalancbhmeanThat is, a 10.19% hmean improvement for jr+hash (this commit).
- NBench, arm-linux-user. Host: Intel i7-4790K @ 4.00GHz
Y axis: Speedup over 95b31d70
1.35x+-+-------------------------------------------------------------+-+
| @@@ jr $$ |
1.3x+-+.............@.@. jr+inline %% ...@@@................+-+
| @ @ jr+inline+hash @@ @ @ |
| @ @ @ @ |
1.25x+-+.............@.@..........................@.@................+-+
| @ @ @@@ @ @ |
1.2x+-+.............@.@..................$$%.@...@.@................+-+
| @ @ $$% @ @ @ |
| @ @ %%@ $$% @ %% @ |
1.15x+-+.............@.@........%%@.......$$%.@$$$%.@................+-+
| @ @ %%@ $$% @$ $% @ |
1.1x+-+.............@.@......$$$%@.......$$%.@$.$%.@...............@@-+
| @ @ $ $%@ $$% @$ $% @ @@ |
| @ @ $ $%@ $$%%@ $$% @$ $% @ $$%%@ |
1.05x+-+...........$$%.@$$$%@@$.$%@.$$.%@.$$%.@$.$%.@.........@@.$$.%@-+
| $$%%@ $$% @$ $% @$ $%@ $$ %@ $$% @$ $% @ %%%@ $$ %@ |
1x+-$$.%@AR%%%@R$$%B@$G$%P@$T$%@_$$+%@l$$%+@$s$%.@$$$%@.$$.%@.$$.%@-+
+-$$%%@-$$%%@-$$%@@$$$%@@$$$%@-$$%%@-$$%@@$$$%@@$$$%@-$$%%@-$$%%@-+
ASSIGNMBITFIELFOFP_EMULATHUFFMANLU_DECOMPNEURNUMERICSTRING_SOhmeanThat is, a 11% hmean perf gain--it almost doubles the perf gain from implementing the jr optimization.
- NBench, x86_64-linux-user. Host: Intel i7-4790K @ 4.00GHz
1.1x+-+-------------------------------------------------------------+-+
| jr $$ |
1.08x+-+..... jr+inline %% ...................................+-+
| jr+inline+hash @@ |
| $$ @@ |
1.06x+-$$.@@.........................%%%.............................+-+
| $$%%@ % % |
1.04x+-$$.%@.........................%.%.............................+-+
| $$ %@ @@@ $$ % $$ |
| $$ %@ @ @ %% $$ % $$%%@ |
1.02x+-$$.%@........%%.@$$$%@@......$$.%@..%%@@..%%........$$.%@.$$%%@-+
| $$ %@ @@ %% @$ $% @$$$ $$ %@ $$% @ %%@@$$$% $$ %@ $$ %@ |
1x+-$$.%@A$$R@@RG%%B@$G$%P@$T$%P_$$T%@h$$%+@$$$%e@$.$%@.$$.%@.$$.%@-+
| $$ %@ $$%%@ $$% @$ $% @$ $% $$ %@ $$% @$ $% @$ $%@ $$ %@ $$ %@ |
0.98x+-$$.%@.$$.%@.$$%.@$.$%.@$.$%@.$$.%@.$$%.@$.$%.@$.$%@.$$.%@.$$.%@-+
| $$ %@ $$ %@ $$% @$ $% @$ $%@ $$ %@ $$% @$ $% @$ $%@ $$ %@ $$ %@ |
| $$ %@ $$ %@ $$% @$ $% @$ $%@ $$ %@ $$% @$ $% @$ $%@ $$ %@ $$ %@ |
0.96x+-$$.%@.$$.%@.$$%.@$.$%.@$.$%@.$$.%@.$$%.@$.$%.@$.$%@.$$.%@.$$.%@-+
+-$$%%@-$$%%@-$$%@@$$$%@@$$$%@-$$%%@-$$%@@$$$%@@$$$%@-$$%%@-$$%%@-+
ASSIGNMBITFIELFOFP_EMULATHUFFMANLU_DECOMPNEURNUMERICSTRING_SOhmeanThe fact that NBench is not very sensitive to changes here was mentioned in the previous commit's log. We get a very slight overall decrease in hmean performance, although some workloads improve as well. Note that there are no error bars: NBench re-runs itself until confidence on the stability of the average is >= 95%, and it doesn't report the resulting stddev.
--- include/exec/tb-hash.h | 12 ++++++++++++ 1 file changed, 12 insertions(+)
include/exec/tb-hash.h +12 -0
@@ -22,6 +22,8 @@#include "exec/tb-hash-xx.h" +#ifdef CONFIG_SOFTMMU +/* Only the bottom TB_JMP_PAGE_BITS of the jump cache hash bits vary for addresses on the same page. The top bits are the same. This allows TLB invalidation to quickly clear a subset of the hash table. */
@@ -45,6 +47,16 @@ static inline unsigned int tb_jmp_cache_hash_func(target_ulong pc) | (tmp & TB_JMP_ADDR_MASK)); } +#else + +/* In user-mode we can get better hashing because we do not have a TLB */ +static inline unsigned int tb_jmp_cache_hash_func (target_ulong pc) +{ +return (pc ^ (pc >>TB_JMP_CACHE_BITS )) & (TB_JMP_CACHE_SIZE -1 ); +} + +#endif /* CONFIG_SOFTMMU */ +static inline uint32_t tb_hash_func (tb_page_addr_t phys_pc,target_ulong pc,uint32_t flags) {
Signature · 1 line
2.7.4
Re: [PATCH 10/10] tb-hash: improve tb_jmp_cache hash function in user mode
include/exec/tb-hash.h static inline unsigned int tb_jmp_cache_hash_func(target_ulong pc) · line 56
Quoted 6 lines · Emilio G. Cota
include/exec/tb-hash.h +6
-0+ +/* In user-mode we can get better hashing because we do not have a TLB */ +static inline unsigned int tb_jmp_cache_hash_func (target_ulong pc) +{ +return (pc ^ (pc >>TB_JMP_CACHE_BITS )) & (TB_JMP_CACHE_SIZE -1 ); +}
What about multiplicative hashing?
return (uint64_t) (pc * 2654435761) >> 32;
Paolo
Re: [PATCH 10/10] tb-hash: improve tb_jmp_cache hash function in user mode
Quoted 12 lines · Paolo Bonzini
include/exec/tb-hash.h static inline unsigned int tb_jmp_cache_hash_func(target_ulong pc) · line 56
Quoted 6 lines · Emilio G. Cota
include/exec/tb-hash.h +6
-0+ +/* In user-mode we can get better hashing because we do not have a TLB */ +static inline unsigned int tb_jmp_cache_hash_func (target_ulong pc) +{ +return (pc ^ (pc >>TB_JMP_CACHE_BITS )) & (TB_JMP_CACHE_SIZE -1 ); +}What about multiplicative hashing?
Quoted 1 line
return (uint64_t) (pc * 2654435761) >> 32;
I tested this one, taking the TB_JMP_CACHE_SIZE-1 lower bits of the result:
In terms of quality it's good (I profile hit rates and they're all pretty good), but shift+xor are just so hard to beat: shift+xor take 1 cycle each; the multiplication takes on my machine 3 or 4 cycles (source: Fog's tables).
Thanks,
E.
Quoted 9 lines · Emilio G. Cota
Hi all,
This series is aimed at 2.10 or beyond. Its goal is to improve TCG performance by optimizing:
1- Cross-page direct jumps (softmmu only, obviously). Patches 1-4. 2- Indirect branches (softmmu and user-mode). Patches 5-9. 3-
tb_jmp_cachehashing in user-mode. Patch 10.
Quoted 11 lines
I decided to work on this after reading this paper [1] (code at [2]), which among other optimizations it proposes solutions for 1 and 2. I followed the same overall scheme they follow, that is to use helpers to check whether the target vaddr is valid, and if so, jump to its corresponding translated code (host address) without having to go back to the exec loop. My implementation differs from that in the paper in that it uses
tb_jmp_cacheinstead of adding more caches, which is simpler and probably more resilient in environments where TLB invalidations are frequent (in the paper they acknowledge that they limited background processes to a minimum, which isn't realistic).
Hi Emilio,
If you want to get some numbers on TLB invalidations please have a look at my WIP branch:
https://github.com/stsquad/qemu/tree/misc/tlb-flush-stats
It's mainly an experiment at how easy it is to extract number data using QEMU's trace subsystem (it turns out pretty easy). I had started looking at the execution trace but got a little bogged down with re-implementing hashes in python - it would be nice if we could just ctype dll load the C implementation (or maybe just save the computed hashes in another trace point rather than inferring via exec_tb).
Quoted 12 lines
These changes require modifications on the targets and, for optimization number 2, a new TCG opcode to jump to a host address contained in a register.
For now I only implemented this for the i386 and arm targets, and the i386 TCG backend. Other targets/backends can easily opt-in.
The 3rd optimization is implemented in the last patch: it improves
tb_jmp_cachehashing for user-mode by removing the requirement of being able to clear parts of the cache given a page number, since this requirement only applies to softmmu.
Quoted 6 lines
The series applies cleanly on top of
95b31d709ba34.The commit logs include many measurements, performed using SPECint06 and NBench from dbt-bench[3].
Feedback welcome! Thanks,
Given my notes above I think it would be worthwhile coming up with some trace-points in the helpers and hash lookups so we can analyse their behaviour as well as just looking at the performance improvement in benchmarks.
Quoted 11 lines
Emilio
[1] "Optimizing Control Transfer and Memory Virtualization in Full System Emulators", Ding-Yong Hong, Chun-Chen Hsu, Cheng-Yi Chou, Wei-Chung Hsu, Pangfeng Liu, Jan-Jan Wu. ACM TACO, Jan. 2016.
http://www.iis.sinica.edu.tw/page/library/TechReport/tr2015/tr15002.pdf[2] https://github.com/tkhsu/quick-android-emulator/tree/quick-qemu
--
Alex Bennée