Skip to contentSkip to conversation

target-sparc register window handling

PATCH In review Richard Henderson 2 participants 6 messages Oct 6, 2012 – Oct 9, 2012

lore
Share & tools
lore
#1 lore raw

PATCH 1/1 target-sparc: Use TCG registers for windowed registers

In reply to Richard Henderson

The use of direct loads/stores from regwptr meant that the optimizer could not be effective with normal computation. Instead use memcpy on register window changes to move data into and out of "tcg space".

---
 gdbstub.c                          |   4 +-
 linux-user/main.c                  |  17 ++--
 linux-user/signal.c                |  78 +++++++--------
 linux-user/sparc/target_signal.h   |   2 +-
 linux-user/sparc64/target_signal.h |   2 +-
 monitor.c                          |   2 +-
 target-sparc/cpu.c                 |   3 +-
 target-sparc/cpu.h                 |  20 ++--
 target-sparc/int32_helper.c        |   4 +-
 target-sparc/ldst_helper.c         |  12 +--
 target-sparc/translate.c           | 188 +++++++++++++++++--------------------
 target-sparc/win_helper.c          |  56 ++++++-----
 12 files changed, 190 insertions(+), 198 deletions(-)
gdbstub.c +2 -2
index d02ec75..349faeb 100644
@@ -808,7 +808,7 @@ static int cpu_gdb_read_register(CPUSPARCState *env, uint8_t *mem_buf, int n)
     }
     if (n < 32) {
         /* register window */
-        GET_REGA(env->regwptr[n - 8]);
+        GET_REGA(env->wregs[n - 8]);
     }
 #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
     if (n < 64) {
@@ -876,7 +876,7 @@ static int cpu_gdb_write_register(CPUSPARCState *env, uint8_t *mem_buf, int n)
         env->gregs[n] = tmp;
     } else if (n < 32) {
         /* register window */
-        env->regwptr[n - 8] = tmp;
+        env->wregs[n - 8] = tmp;
     }
 #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
     else if (n < 64) {
linux-user/main.c +6 -11
index 9f3476b..57c9805 100644
@@ -991,12 +991,7 @@ error:
    can be found at http://www.sics.se/~psm/sparcstack.html */
 static inline int get_reg_index(CPUSPARCState *env, int cwp, int index)
 {
-    index = (index + cwp * 16) % (16 * env->nwindows);
-    /* wrap handling : if cwp is on the last window, then we use the
-       registers 'after' the end */
-    if (index < 8 && env->cwp == env->nwindows - 1)
-        index += 16 * env->nwindows;
-    return index;
+    return (index + cwp * 16) % (16 * env->nwindows);
 }
 
 /* save the register window 'cwp1' */
@@ -1123,9 +1118,9 @@ void cpu_loop (CPUSPARCState *env)
         case 0x16d:
 #endif
             ret = do_syscall (env, env->gregs[1],
-                              env->regwptr[0], env->regwptr[1],
-                              env->regwptr[2], env->regwptr[3],
-                              env->regwptr[4], env->regwptr[5],
+                              env->wregs[0], env->wregs[1],
+                              env->wregs[2], env->wregs[3],
+                              env->wregs[4], env->wregs[5],
                               0, 0);
             if ((abi_ulong)ret >= (abi_ulong)(-515)) {
 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
@@ -1141,7 +1136,7 @@ void cpu_loop (CPUSPARCState *env)
                 env->psr &= ~PSR_CARRY;
 #endif
             }
-            env->regwptr[0] = ret;
+            env->wregs[0] = ret;
             /* next instruction */
             env->pc = env->npc;
             env->npc = env->npc + 4;
@@ -3755,7 +3750,7 @@ int main(int argc, char **argv, char **envp)
         for(i = 0; i < 8; i++)
             env->gregs[i] = regs->u_regs[i];
         for(i = 0; i < 8; i++)
-            env->regwptr[i] = regs->u_regs[i + 8];
+            env->wregs[i] = regs->u_regs[i + 8];
     }
 #elif defined(TARGET_PPC)
     {
linux-user/signal.c +39 -39

185 lines not on this page · raw message

linux-user/sparc/target_signal.h +1 -1
index c7de300..10bf508 100644
@@ -30,7 +30,7 @@ typedef struct target_sigaltstack {
 
 static inline abi_ulong get_sp_from_cpustate(CPUSPARCState *state)
 {
-    return state->regwptr[UREG_FP];
+    return state->wregs[UREG_FP];
 }
 
 #endif /* TARGET_SIGNAL_H */
linux-user/sparc64/target_signal.h +1 -1
index c7de300..10bf508 100644
@@ -30,7 +30,7 @@ typedef struct target_sigaltstack {
 
 static inline abi_ulong get_sp_from_cpustate(CPUSPARCState *state)
 {
-    return state->regwptr[UREG_FP];
+    return state->wregs[UREG_FP];
 }
 
 #endif /* TARGET_SIGNAL_H */
monitor.c +1 -1
index a0e3ffb..2fbc3ce 100644
@@ -2795,7 +2795,7 @@ static target_long monitor_get_psr (const struct MonitorDef *md, int val)
 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
 {
     CPUArchState *env = mon_get_cpu();
-    return env->regwptr[val];
+    return env->wregs[val];
 }
 #endif
 
target-sparc/cpu.c +1 -2
index 71cc9e8..2263904 100644
@@ -43,7 +43,6 @@ static void sparc_cpu_reset(CPUState *s)
 #ifndef TARGET_SPARC64
     env->wim = 1;
 #endif
-    env->regwptr = env->regbase + (env->cwp * 16);
     CC_OP = CC_OP_FLAGS;
 #if defined(CONFIG_USER_ONLY)
 #ifdef TARGET_SPARC64
@@ -809,7 +808,7 @@ void cpu_dump_state(CPUSPARCState *env, FILE *f, fprintf_function cpu_fprintf,
                             x == 0 ? 'o' : (x == 1 ? 'l' : 'i'),
                             i, i + REGS_PER_LINE - 1);
             }
-            cpu_fprintf(f, TARGET_FMT_lx " ", env->regwptr[i + x * 8]);
+            cpu_fprintf(f, TARGET_FMT_lx " ", env->wregs[i + x * 8]);
             if (i % REGS_PER_LINE == REGS_PER_LINE - 1) {
                 cpu_fprintf(f, "\n");
             }
target-sparc/cpu.h +10 -10
index 214d01d..4572c4d 100644
@@ -381,11 +381,11 @@ void cpu_get_timer(struct QEMUFile *f, CPUTimer *s);
 typedef struct CPUSPARCState CPUSPARCState;
 
 struct CPUSPARCState {
-    target_ulong gregs[8]; /* general registers */
-    target_ulong *regwptr; /* pointer to current register window */
-    target_ulong pc;       /* program counter */
-    target_ulong npc;      /* next program counter */
-    target_ulong y;        /* multiply/divide register */
+    target_ulong gregs[8];  /* general registers */
+    target_ulong wregs[24]; /* current register window */
+    target_ulong pc;        /* program counter */
+    target_ulong npc;       /* next program counter */
+    target_ulong y;         /* multiply/divide register */
 
     /* emulator internal flags handling */
     target_ulong cc_src, cc_src2;
@@ -416,8 +416,7 @@ struct CPUSPARCState {
     int      psref;    /* enable fpu */
 #endif
     int interrupt_index;
-    /* NOTE: we allow 8 more registers to handle wrapping */
-    target_ulong regbase[MAX_NWINDOWS * 16 + 8];
+    target_ulong regbase[MAX_NWINDOWS * 16];
 
     CPU_COMMON
 
@@ -693,9 +692,10 @@ static inline int cpu_pil_allowed(CPUSPARCState *env1, int pil)
 #if defined(CONFIG_USER_ONLY)
 static inline void cpu_clone_regs(CPUSPARCState *env, target_ulong newsp)
 {
-    if (newsp)
-        env->regwptr[22] = newsp;
-    env->regwptr[0] = 0;
+    if (newsp) {
+        env->wregs[22] = newsp;
+    }
+    env->wregs[0] = 0;
     /* FIXME: Do we also need to clear CF?  */
     /* XXXXX */
     printf ("HELPME: %s:%d\n", __FILE__, __LINE__);
target-sparc/int32_helper.c +2 -2
index 9ac5aac..6fd43d8 100644
@@ -111,8 +111,8 @@ void do_interrupt(CPUSPARCState *env)
     env->psret = 0;
     cwp = cpu_cwp_dec(env, env->cwp - 1);
     cpu_set_cwp(env, cwp);
-    env->regwptr[9] = env->pc;
-    env->regwptr[10] = env->npc;
+    env->wregs[9] = env->pc;
+    env->wregs[10] = env->npc;
     env->psrps = env->psrs;
     env->psrs = 1;
     env->tbr = (env->tbr & TBR_BASE_MASK) | (intno << 4);
target-sparc/ldst_helper.c +6 -6
index 2ca9a5c..e80bd3a 100644
@@ -2053,11 +2053,11 @@ void helper_ldda_asi(CPUSPARCState *env, target_ulong addr, int asi, int rd)
                 bswap64s(&env->gregs[rd + 1]);
             }
         } else {
-            env->regwptr[rd] = cpu_ldq_nucleus(env, addr);
-            env->regwptr[rd + 1] = cpu_ldq_nucleus(env, addr + 8);
+            env->wregs[rd - 8] = cpu_ldq_nucleus(env, addr);
+            env->wregs[rd - 8 + 1] = cpu_ldq_nucleus(env, addr + 8);
             if (asi == 0x2c) {
-                bswap64s(&env->regwptr[rd]);
-                bswap64s(&env->regwptr[rd + 1]);
+                bswap64s(&env->wregs[rd - 8]);
+                bswap64s(&env->wregs[rd - 8 + 1]);
             }
         }
         break;
@@ -2070,8 +2070,8 @@ void helper_ldda_asi(CPUSPARCState *env, target_ulong addr, int asi, int rd)
             env->gregs[rd] = helper_ld_asi(env, addr, asi, 4, 0);
             env->gregs[rd + 1] = helper_ld_asi(env, addr + 4, asi, 4, 0);
         } else {
-            env->regwptr[rd] = helper_ld_asi(env, addr, asi, 4, 0);
-            env->regwptr[rd + 1] = helper_ld_asi(env, addr + 4, asi, 4, 0);
+            env->wregs[rd - 8] = helper_ld_asi(env, addr, asi, 4, 0);
+            env->wregs[rd - 8 + 1] = helper_ld_asi(env, addr + 4, asi, 4, 0);
         }
         break;
     }
target-sparc/translate.c +88 -100

247 lines not on this page · raw message

target-sparc/win_helper.c +33 -23

88 lines not on this page · raw message

Signature · 1 line
1.7.11.4
#0 lore raw

PATCH 1/1 target-sparc register window handling

This applies with or without the sparc-compare patch set I recently sent, and it works with the same set of tests.

I've not had time to do true benchmarking on this, but it does reduce the size of the generated code:

BEFORE
Translation buffer state:

gen code size       509344/33431552
TB count            2196/262144
TB avg target size  22 max=252 bytes
TB avg host size    231 bytes (expansion ratio: 10.3)

cross page TB count 0 (0%)
direct jump count 1153 (52%) (2 jumps=628 28%)

AFTER
Translation buffer state:

gen code size       418064/33431552
TB count            2196/262144
TB avg target size  22 max=252 bytes
TB avg host size    190 bytes (expansion ratio: 8.4)

cross page TB count 0 (0%)
direct jump count 1153 (52%) (2 jumps=628 28%)

r~

Richard Henderson (1):
target-sparc: Use TCG registers for windowed registers

 gdbstub.c                          |   4 +-
 linux-user/main.c                  |  17 ++--
 linux-user/signal.c                |  78 +++++++--------
 linux-user/sparc/target_signal.h   |   2 +-
 linux-user/sparc64/target_signal.h |   2 +-
 monitor.c                          |   2 +-
 target-sparc/cpu.c                 |   3 +-
 target-sparc/cpu.h                 |  20 ++--
 target-sparc/int32_helper.c        |   4 +-
 target-sparc/ldst_helper.c         |  12 +--
 target-sparc/translate.c           | 188 +++++++++++++++++--------------------
 target-sparc/win_helper.c          |  56 ++++++-----
 12 files changed, 190 insertions(+), 198 deletions(-)
Signature · 1 line
1.7.11.4
#2 lore raw
Quoted 5 lines · Richard Henderson

This applies with or without the sparc-compare patch set I recently sent, and it works with the same set of tests.

I've not had time to do true benchmarking on this, but it does reduce the size of the generated code:

Experience proves that there is not a direct relation between size of the generated code and the resulting emulation speed, sometimes smaller code means slower emulation (when the code generation/optimization takes too much time), and sometimes bigger code might means faster emulation (think about replacing some helpers by TCG code).

As from the user point of view what is important is the emulation speed, I think benchmarks (even simple ones like measuring the boot time of a guest) are essential for this kind of patch.

Quoted 44 lines

BEFORE
Translation buffer state:

gen code size       509344/33431552
TB count            2196/262144
TB avg target size  22 max=252 bytes
TB avg host size    231 bytes (expansion ratio: 10.3)

cross page TB count 0 (0%)
direct jump count 1153 (52%) (2 jumps=628 28%)

AFTER
Translation buffer state:

gen code size       418064/33431552
TB count            2196/262144
TB avg target size  22 max=252 bytes
TB avg host size    190 bytes (expansion ratio: 8.4)

cross page TB count 0 (0%)
direct jump count 1153 (52%) (2 jumps=628 28%)

r~

Richard Henderson (1):
target-sparc: Use TCG registers for windowed registers

 gdbstub.c                          |   4 +-
 linux-user/main.c                  |  17 ++--
 linux-user/signal.c                |  78 +++++++--------
 linux-user/sparc/target_signal.h   |   2 +-
 linux-user/sparc64/target_signal.h |   2 +-
 monitor.c                          |   2 +-
 target-sparc/cpu.c                 |   3 +-
 target-sparc/cpu.h                 |  20 ++--
 target-sparc/int32_helper.c        |   4 +-
 target-sparc/ldst_helper.c         |  12 +--
 target-sparc/translate.c           | 188 +++++++++++++++++--------------------
 target-sparc/win_helper.c          |  56 ++++++-----
 12 files changed, 190 insertions(+), 198 deletions(-)
Signature · 1 line
1.7.11.4
Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
#3 lore raw
Quoted 13 lines · Aurelien Jarno
Quoted 5 lines · Richard Henderson

This applies with or without the sparc-compare patch set I recently sent, and it works with the same set of tests.

I've not had time to do true benchmarking on this, but it does reduce the size of the generated code:

Experience proves that there is not a direct relation between size of the generated code and the resulting emulation speed, sometimes smaller code means slower emulation (when the code generation/optimization takes too much time), and sometimes bigger code might means faster emulation (think about replacing some helpers by TCG code).

Quoted 4 lines

As from the user point of view what is important is the emulation speed, I think benchmarks (even simple ones like measuring the boot time of a guest) are essential for this kind of patch.

For what it worth, I measure a 4% slow down booting a sparc64 guest on a Core-i5 2500 machine. I guess the memcpy() of the register windows is more expensive that the gain on the TCG side, though it should be probably be confirmed using some profiling tools.

Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
#4 lore raw
Quoted 4 lines · Aurelien Jarno

For what it worth, I measure a 4% slow down booting a sparc64 guest on a Core-i5 2500 machine. I guess the memcpy() of the register windows is more expensive that the gain on the TCG side, though it should be probably be confirmed using some profiling tools.

Confirmed with userland testing (running cc1plus on large input), and I see much more than 4% slow down -- more like 15%. I can reduce this by adding save/restore window inc/dec optimization within the cpu_set_cwp routine, but that only gets me back to 5% slow down.

A bit disappointing; I guess other avenues will want exploring.

Patch withdrawn.

r~

#5 lore raw
Quoted 8 lines · Richard Henderson
Quoted 4 lines · Aurelien Jarno

For what it worth, I measure a 4% slow down booting a sparc64 guest on a Core-i5 2500 machine. I guess the memcpy() of the register windows is more expensive that the gain on the TCG side, though it should be probably be confirmed using some profiling tools.

Confirmed with userland testing (running cc1plus on large input), and I see much more than 4% slow down -- more like 15%. I can reduce

Not surprising. System mode spends a lot of time outside of the TCG code, and when doing TCG code, it spends most of its time in the qemu_ld/st code. User mode has a very simple qemu_ld/st code.

Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net