Skip to contentSkip to conversation

tcg/ppc: Enable direct branching tcg_out_goto_tb with TCG_REG_TB

RFC PATCH In review Jordan Niethe 2 participants 3 messages Aug 15, 2023 – Aug 16, 2023

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 tcg/ppc: Enable direct branching tcg_out_goto_tb with TCG_REG_TB

Direct branch patching was disabled when using TCG_REG_TB in commit 736a1588c1 ("tcg/ppc: Fix race in goto_tb implementation"). Commit 7502f89c74 ("tcg/ppc: Use prefixed instructions for tcg_out_goto_tb") used the support for pc relative ISAv3.1 instructions to re-enable direct branch patching on POWER10.

The issue with direct branch patching with TCG_REG_TB is the lack of synchronization between the new TCG_REG_TB being established and the direct branch being patched in.

If each translation block is responsible for establishing its own TCG_REG_TB then there can be no synchronization issue.

Make each translation block begin by setting up its own TCG_REG_TB. ISA v3.0 has addpcis so use that for getting the pc at the beginning of a translation block (plus offset). For systems without addpcis, use the preferred 'bcl 20,31,$+4' sequence.

When branching indirectly to a translation block the setup sequence can be skipped if the caller sets up TCG_REG_TB as there is no possible race in this case.

---
This is just a proof of concept, not sure that this is the correct way to do this or even if it is something we'd like to do.

Applies on top of Richard's series [1].

[1] https://lore.kernel.org/qemu-devel/[email protected]/

---
 include/tcg/tcg.h        |  1 +
 tcg/ppc/tcg-target.c.inc | 59 ++++++++++++++++++++++++++--------------
 tcg/tcg.c                |  3 ++
 3 files changed, 42 insertions(+), 21 deletions(-)
include/tcg/tcg.h +1 -0
index 0875971719..337506fea0 100644
@@ -518,6 +518,7 @@ struct TCGContext {
        extension that allows arithmetic on void*.  */
     void *code_gen_buffer;
     size_t code_gen_buffer_size;
+    size_t code_gen_entry_size;
     void *code_gen_ptr;
     void *data_gen_ptr;
 
tcg/ppc/tcg-target.c.inc +38 -21

95 lines not on this page · raw message

tcg/tcg.c +3 -0
index ddfe9a96cb..20698131c2 100644
@@ -6010,6 +6010,9 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb, uint64_t pc_start)
         tcg_malloc(sizeof(uint64_t) * s->gen_tb->icount * start_words);
 
     num_insns = -1;
+#ifdef TCG_TARGET_NEED_ENTER_TB
+    tcg_out_enter_tb(s);
+#endif
     QTAILQ_FOREACH(op, &s->ops, link) {
         TCGOpcode opc = op->opc;
 
Signature · 1 line
2.39.3
#1 lore raw
Quoted 6 lines · Jordan Niethe

Direct branch patching was disabled when using TCG_REG_TB in commit 736a1588c1 ("tcg/ppc: Fix race in goto_tb implementation"). Commit 7502f89c74 ("tcg/ppc: Use prefixed instructions for tcg_out_goto_tb") used the support for pc relative ISAv3.1 instructions to re-enable direct branch patching on POWER10.

Quoted 6 lines

The issue with direct branch patching with TCG_REG_TB is the lack of synchronization between the new TCG_REG_TB being established and the direct branch being patched in.

If each translation block is responsible for establishing its own TCG_REG_TB then there can be no synchronization issue.

That's a good idea, and can be used for other things...

It also begs the question of whether power10 should continue to use TCG_REG_TB, loading the address with PADDI. Or whether power9 should, like power10, disable USE_REG_TB and use ADDPCIS throughout.

I imagine it depends on usage frequency, whether use of TCG_REG_TB allows 1 insn, where addpcis requires 2 insns and prefixed insns require 2 or 3 insn slots (depending on alignment).

tcg/ppc/tcg-target.c.inc static void tcg_target_qemu_prologue(TCGContext *s) · line 2657

Quoted 6 lines
tcg/ppc/tcg-target.c.inc +6 -0
+        tcg_out32(s, MFSPR | RT(TCG_REG_TMP1) | LR);
+        /* bcl 20,31,$+4 (Preferred form for getting nia.) */
+        tcg_out32(s, BC | BO_ALWAYS | BI(7, CR_SO) | 0x4 | LK);
+        tcg_out32(s, MFSPR | RT(TCG_REG_TB) | LR);
+        tcg_out32(s, ADDI | TAI(TCG_REG_TB, TCG_REG_TB, -8));
+        tcg_out32(s, MTSPR | RS(TCG_REG_TMP1) | LR);

Don't need to save/restore LR. It is saved in the prologue and may be clobbered within the tb itself (as we do for calls).

tcg/ppc/tcg-target.c.inc static void tcg_out_goto_tb(TCGContext *s, int which) · line 2702

Quoted 11 lines
tcg/ppc/tcg-target.c.inc +6 -0
@@ -2678,6 +2693,12 @@ static void tcg_out_goto_tb(TCGContext *s, int which)
      tcg_out32(s, MTSPR | RS(TCG_REG_TMP1) | CTR);
      tcg_out32(s, BCCTR | BO_ALWAYS);
      set_jmp_reset_offset(s, which);
+
+    /* For the unlinked case, need to reset TCG_REG_TB.  */
+    if (USE_REG_TB) {
+        tcg_out_movi_int(s, TCG_TYPE_I64, TCG_REG_TB,
+                         (tcg_target_long)s->code_buf, true);
+    }
  }

Actually, we don't. The only time we arrive here is when an unlinked TB branches to itself. TCG_REG_TB is still valid.

tcg/tcg.c int tcg_gen_code(TCGContext *s, TranslationBlock *tb, uint64_t pc_start) · line 6015

Quoted 11 lines
tcg/tcg.c +3 -0
index ddfe9a96cb..20698131c2 100644
@@ -6010,6 +6010,9 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb, uint64_t pc_start)
          tcg_malloc(sizeof(uint64_t) * s->gen_tb->icount * start_words);
 
      num_insns = -1;
+#ifdef TCG_TARGET_NEED_ENTER_TB
+    tcg_out_enter_tb(s);
+#endif

Better would be to not have the ifdef, and add this symbol as an empty function in all other tcg backends.

I might play around with this a bit.

r~

#2 lore raw
Quoted 11 lines · Richard Henderson
Quoted 10 lines · Jordan Niethe

Direct branch patching was disabled when using TCG_REG_TB in commit 736a1588c1 ("tcg/ppc: Fix race in goto_tb implementation"). Commit 7502f89c74 ("tcg/ppc: Use prefixed instructions for tcg_out_goto_tb") used the support for pc relative ISAv3.1 instructions to re-enable direct branch patching on POWER10.

The issue with direct branch patching with TCG_REG_TB is the lack of synchronization between the new TCG_REG_TB being established and the direct branch being patched in.

Quoted 8 lines
Quoted 2 lines

If each translation block is responsible for establishing its own TCG_REG_TB then there can be no synchronization issue.

That's a good idea, and can be used for other things...

It also begs the question of whether power10 should continue to use TCG_REG_TB, loading the address with PADDI.  Or whether power9 should, like power10, disable USE_REG_TB and use ADDPCIS throughout.

I imagine it depends on usage frequency, whether use of TCG_REG_TB allows 1 insn, where addpcis requires 2 insns and prefixed insns require 2 or 3 insn slots (depending on alignment).

Yes, I agree. Your v3 series looks good, I'll try and get some performance numbers with it so we can make a decision about which way to go on power10 and power9.

Quoted 10 lines
Quoted 8 lines

+        tcg_out32(s, MFSPR | RT(TCG_REG_TMP1) | LR); +        /* bcl 20,31,$+4 (Preferred form for getting nia.) */ +        tcg_out32(s, BC | BO_ALWAYS | BI(7, CR_SO) | 0x4 | LK); +        tcg_out32(s, MFSPR | RT(TCG_REG_TB) | LR);
+        tcg_out32(s, ADDI | TAI(TCG_REG_TB, TCG_REG_TB, -8)); +        tcg_out32(s, MTSPR | RS(TCG_REG_TMP1) | LR);

Don't need to save/restore LR.  It is saved in the prologue and may be clobbered within the tb itself (as we do for calls >

Quoted 14 lines

tcg/ppc/tcg-target.c.inc static void tcg_out_goto_tb(TCGContext *s, int · line 2703

Quoted 12 lines
tcg/ppc/tcg-target.c.inc +6 -0
@@ -2678,6 +2693,12 @@ static void tcg_out_goto_tb(TCGContext *s, int 
 which)
       tcg_out32(s, MTSPR | RS(TCG_REG_TMP1) | CTR);
       tcg_out32(s, BCCTR | BO_ALWAYS);
       set_jmp_reset_offset(s, which);
+
+    /* For the unlinked case, need to reset TCG_REG_TB.  */
+    if (USE_REG_TB) {
+        tcg_out_movi_int(s, TCG_TYPE_I64, TCG_REG_TB,
+                         (tcg_target_long)s->code_buf, true);
+    }
   }

Actually, we don't.  The only time we arrive here is when an unlinked TB branches to itself.  TCG_REG_TB is still valid.

Ok, I was not sure how that was meant to work.

Quoted 15 lines

tcg/tcg.c int tcg_gen_code(TCGContext *s, TranslationBlock · line 6015

Quoted 12 lines
tcg/tcg.c +3 -0
index ddfe9a96cb..20698131c2 100644
@@ -6010,6 +6010,9 @@ int tcg_gen_code(TCGContext *s, TranslationBlock 
 *tb, uint64_t pc_start)
           tcg_malloc(sizeof(uint64_t) * s->gen_tb->icount * start_words);
       num_insns = -1;
+#ifdef TCG_TARGET_NEED_ENTER_TB
+    tcg_out_enter_tb(s);
+#endif

Better would be to not have the ifdef, and add this symbol as an empty function in all other tcg backends.

Quoted 1 line

I might play around with this a bit.

Thank you for that, adding pcrel support on POWER9 too is really cool.

Quoted 4 lines

r~