Skip to contentSkip to conversation

Introduce TCGOpcode for fence instruction

RFC PATCH In review Pranith Kumar 5 participants 15 messages May 24, 2016 – May 26, 2016 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

RFC PATCH 1/3 Introduce TCGOpcode for fence instruction

This commit introduces the TCGOpcode for fence instruction.

---
 tcg/i386/tcg-target.inc.c | 4 ++++
 tcg/tcg-opc.h             | 2 ++
 tcg/tcg.c                 | 2 ++
 3 files changed, 8 insertions(+)
tcg/i386/tcg-target.inc.c +4 -0
index 8fd37f4..238fa10 100644
@@ -2114,6 +2114,8 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
         }
         break;
 
+    case INDEX_op_fence:
+        break;
     case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
     case INDEX_op_mov_i64:
     case INDEX_op_movi_i32: /* Always emitted via tcg_out_movi.  */
@@ -2179,6 +2181,8 @@ static const TCGTargetOpDef x86_op_defs[] = {
     { INDEX_op_add2_i32, { "r", "r", "0", "1", "ri", "ri" } },
     { INDEX_op_sub2_i32, { "r", "r", "0", "1", "ri", "ri" } },
 
+    { INDEX_op_fence, { } },
+
 #if TCG_TARGET_REG_BITS == 32
     { INDEX_op_brcond2_i32, { "r", "r", "ri", "ri" } },
     { INDEX_op_setcond2_i32, { "r", "r", "r", "ri", "ri" } },
tcg/tcg-opc.h +2 -0
index 6d0410c..4696cf1 100644
@@ -42,6 +42,8 @@ DEF(br, 0, 0, 1, TCG_OPF_BB_END)
 # define IMPL64  TCG_OPF_64BIT
 #endif
 
+DEF(fence, 0, 0, 0, 0)
+
 DEF(mov_i32, 1, 1, 0, TCG_OPF_NOT_PRESENT)
 DEF(movi_i32, 1, 0, 1, TCG_OPF_NOT_PRESENT)
 DEF(setcond_i32, 1, 2, 1, 0)
tcg/tcg.c +2 -0
index b46bf1a..b5a22ba 100644
@@ -2443,6 +2443,8 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb)
             tcg_reg_alloc_call(s, op->callo, op->calli, args,
                                dead_args, sync_args);
             break;
+        case INDEX_op_fence:
+            break;
         default:
             /* Sanity check that we've not introduced any unhandled opcodes. */
             if (def->flags & TCG_OPF_NOT_PRESENT) {
Signature · 1 line
2.8.3
#2 lore raw
Quoted 8 lines · Pranith Kumar

This commit introduces the TCGOpcode for fence instruction.

---
 tcg/i386/tcg-target.inc.c | 4 ++++
 tcg/tcg-opc.h             | 2 ++
 tcg/tcg.c                 | 2 ++
 3 files changed, 8 insertions(+)

Since this is a new TCG op, can we have it documented in tcg/README, please? This should describe the semantics of the operation in enough detail that:
* a new TCG backend can implement it correctly
* a TCG frontend knows when it needs to use the op

thanks
-- PMM

#3 lore raw
Quoted 7 lines · Peter Maydell

Since this is a new TCG op, can we have it documented in tcg/README, please? This should describe the semantics of the operation in enough detail that:
* a new TCG backend can implement it correctly
* a TCG frontend knows when it needs to use the op

Sure, I will add this information.

Thanks!

Signature · 1 line
Pranith
#4 lore raw

RFC PATCH 2/3 tcg: Add support for fence generation in x86 backend

In reply to a message not in the archive
---
 tcg/i386/tcg-target.h     | 1 +
 tcg/i386/tcg-target.inc.c | 9 +++++++++
 tcg/tcg-opc.h             | 2 +-
 tcg/tcg.c                 | 1 +
 4 files changed, 12 insertions(+), 1 deletion(-)
tcg/i386/tcg-target.h +1 -0
index 92be341..93ea42e 100644
@@ -100,6 +100,7 @@ extern bool have_bmi1;
 #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_fence            1
 
 #if TCG_TARGET_REG_BITS == 64
 #define TCG_TARGET_HAS_extrl_i64_i32    0
tcg/i386/tcg-target.inc.c +9 -0
index 238fa10..cf49272 100644
@@ -347,6 +347,7 @@ static inline int tcg_target_const_match(tcg_target_long val, TCGType type,
 #define OPC_SHRX        (0xf7 | P_EXT38 | P_SIMDF2)
 #define OPC_TESTL	(0x85)
 #define OPC_XCHG_ax_r32	(0x90)
+#define OPC_MFENCE      (0xAE | P_EXT)
 
 #define OPC_GRP3_Ev	(0xf7)
 #define OPC_GRP5	(0xff)
@@ -686,6 +687,14 @@ static inline void tcg_out_pushi(TCGContext *s, tcg_target_long val)
     }
 }
 
+static inline void tcg_out_fence(TCGContext *s)
+{
+    /* TODO: Figure out an appropriate place for the encoding */
+    tcg_out8(s, 0x0F);
+    tcg_out8(s, 0xAE);
+    tcg_out8(s, 0xF0);
+}
+
 static inline void tcg_out_push(TCGContext *s, int reg)
 {
     tcg_out_opc(s, OPC_PUSH_r32 + LOWREGMASK(reg), 0, reg, 0);
tcg/tcg-opc.h +1 -1
index 4696cf1..b772d90 100644
@@ -42,7 +42,7 @@ DEF(br, 0, 0, 1, TCG_OPF_BB_END)
 # define IMPL64  TCG_OPF_64BIT
 #endif
 
-DEF(fence, 0, 0, 0, 0)
+DEF(fence, 0, 0, 0, TCG_OPF_SIDE_EFFECTS)
 
 DEF(mov_i32, 1, 1, 0, TCG_OPF_NOT_PRESENT)
 DEF(movi_i32, 1, 0, 1, TCG_OPF_NOT_PRESENT)
tcg/tcg.c +1 -0
index b5a22ba..461a33e 100644
@@ -2444,6 +2444,7 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb)
                                dead_args, sync_args);
             break;
         case INDEX_op_fence:
+            tcg_out_fence(s);
             break;
         default:
             /* Sanity check that we've not introduced any unhandled opcodes. */
Signature · 1 line
2.8.3
#5 lore raw

Re: [Qemu-devel] [RFC PATCH 2/3] tcg: Add support for fence generation in x86 backend

Quoted 13 lines · Pranith Kumar
---
 tcg/i386/tcg-target.h     | 1 +
 tcg/i386/tcg-target.inc.c | 9 +++++++++
 tcg/tcg-opc.h             | 2 +-
 tcg/tcg.c                 | 1 +
 4 files changed, 12 insertions(+), 1 deletion(-)
tcg/i386/tcg-target.h +0 -0
index 92be341..93ea42e 100644

tcg/i386/tcg-target.h extern bool have_bmi1; · line 103

Quoted 4 lines
tcg/i386/tcg-target.h +1 -0
@@ -100,6 +100,7 @@ extern bool have_bmi1;
 #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_fence            1

This has to be defined for all hosts.

The default implementation should be a function call into tcg-runtime.c that calls smp_mb().

Quoted 9 lines
tcg/i386/tcg-target.h +1 -0
@@ -347,6 +347,7 @@ static inline int tcg_target_const_match(tcg_target_long val, TCGType type,
 #define OPC_SHRX        (0xf7 | P_EXT38 | P_SIMDF2)
 #define OPC_TESTL	(0x85)
 #define OPC_XCHG_ax_r32	(0x90)
+#define OPC_MFENCE      (0xAE | P_EXT)
 
 #define OPC_GRP3_Ev	(0xf7)
 #define OPC_GRP5	(0xff)

tcg/i386/tcg-target.h static inline void tcg_out_pushi(TCGContext *s, tcg_target_long val) · line 696

Quoted 10 lines
tcg/i386/tcg-target.h +7 -0
@@ -686,6 +687,14 @@ static inline void tcg_out_pushi(TCGContext *s, tcg_target_long val)
     }
 }
 
+static inline void tcg_out_fence(TCGContext *s)
+{
+    /* TODO: Figure out an appropriate place for the encoding */
+    tcg_out8(s, 0x0F);
+    tcg_out8(s, 0xAE);
+    tcg_out8(s, 0xF0);
+}

Why define OPC_MFENCE if you're not going to use it? Of course, it's not exactly a complete and useful definition, so maybe just delete OPC_MFENCE.

Also, for 32-bit you need to check for sse2 before outputting this. See also the existing cpuid checks in tcg_target_init and the fallback smp_mb definition for pre-gcc-4.4.

r~

#6 lore raw

Re: [Qemu-devel] [RFC PATCH 2/3] tcg: Add support for fence generation in x86 backend

Quoted 21 lines · Richard Henderson
Quoted 13 lines · Pranith Kumar
---
 tcg/i386/tcg-target.h     | 1 +
 tcg/i386/tcg-target.inc.c | 9 +++++++++
 tcg/tcg-opc.h             | 2 +-
 tcg/tcg.c                 | 1 +
 4 files changed, 12 insertions(+), 1 deletion(-)
tcg/i386/tcg-target.h +0 -0
index 92be341..93ea42e 100644

tcg/i386/tcg-target.h extern bool have_bmi1; · line 103

Quoted 4 lines
tcg/i386/tcg-target.h +1 -0
@@ -100,6 +100,7 @@ extern bool have_bmi1;
 #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_fence            1

This has to be defined for all hosts.

Quoted 2 lines

The default implementation should be a function call into tcg-runtime.c that calls smp_mb().

That would solves the problem of converting the various backends piecemeal - although obviously we should move to all backends having "native" support ASAP. However by introducing expensive substitute functions we will slow down the translations as each front end is expanded to translate the target barrier ops.

Should we make the emitting of the function call/TCGop conditional on MTTCG being enabled? If we are running in round-robin mode there is no need to issue any fence operations.

Quoted 24 lines
Quoted 10 lines
tcg/i386/tcg-target.h +1 -0
@@ -347,6 +347,7 @@ static inline int tcg_target_const_match(tcg_target_long val, TCGType type,
 #define OPC_SHRX        (0xf7 | P_EXT38 | P_SIMDF2)
 #define OPC_TESTL	(0x85)
 #define OPC_XCHG_ax_r32	(0x90)
+#define OPC_MFENCE      (0xAE | P_EXT)
 
 #define OPC_GRP3_Ev	(0xf7)
 #define OPC_GRP5	(0xff)

tcg/i386/tcg-target.h static inline void tcg_out_pushi(TCGContext *s, tcg_target_long val) · line 696

Quoted 10 lines
tcg/i386/tcg-target.h +7 -0
@@ -686,6 +687,14 @@ static inline void tcg_out_pushi(TCGContext *s, tcg_target_long val)
     }
 }
 
+static inline void tcg_out_fence(TCGContext *s)
+{
+    /* TODO: Figure out an appropriate place for the encoding */
+    tcg_out8(s, 0x0F);
+    tcg_out8(s, 0xAE);
+    tcg_out8(s, 0xF0);
+}

Why define OPC_MFENCE if you're not going to use it? Of course, it's not exactly a complete and useful definition, so maybe just delete OPC_MFENCE.

Quoted 6 lines

Also, for 32-bit you need to check for sse2 before outputting this. See also the existing cpuid checks in tcg_target_init and the fallback smp_mb definition for pre-gcc-4.4.

r~

--
Alex Bennée

#7 lore raw

Re: [Qemu-devel] [RFC PATCH 2/3] tcg: Add support for fence generation in x86 backend

Quoted 21 lines · Alex Bennée
Quoted 20 lines · Richard Henderson
Quoted 13 lines · Pranith Kumar
---
 tcg/i386/tcg-target.h     | 1 +
 tcg/i386/tcg-target.inc.c | 9 +++++++++
 tcg/tcg-opc.h             | 2 +-
 tcg/tcg.c                 | 1 +
 4 files changed, 12 insertions(+), 1 deletion(-)
tcg/i386/tcg-target.h +0 -0
index 92be341..93ea42e 100644

tcg/i386/tcg-target.h extern bool have_bmi1; · line 103

Quoted 4 lines
tcg/i386/tcg-target.h +1 -0
@@ -100,6 +100,7 @@ extern bool have_bmi1;
 #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_fence            1

This has to be defined for all hosts.

Quoted 7 lines
Quoted 2 lines

The default implementation should be a function call into tcg-runtime.c that calls smp_mb().

That would solves the problem of converting the various backends piecemeal - although obviously we should move to all backends having "native" support ASAP. However by introducing expensive substitute functions we will slow down the translations as each front end is expanded to translate the target barrier ops.

I think it would better not to defer native support for the operation. It should be relatively simple instruction. Otherwise we could wind up deferring this indefinitely.

Quoted 3 lines

Should we make the emitting of the function call/TCGop conditional on MTTCG being enabled? If we are running in round-robin mode there is no need to issue any fence operations.

Good idea.

Kind regards,
Sergey

#8 lore raw

Re: [Qemu-devel] [RFC PATCH 2/3] tcg: Add support for fence generation in x86 backend

Quoted 5 lines · Sergey Fedorov

I think it would better not to defer native support for the operation. It should be relatively simple instruction. Otherwise we could wind up deferring this indefinitely.

Agreed. I will go with the native generation for now.

Thanks,

Signature · 1 line
Pranith
#9 lore raw

Re: [Qemu-devel] [RFC PATCH 2/3] tcg: Add support for fence generation in x86 backend

Quoted 6 lines · Pranith Kumar
Quoted 4 lines · Sergey Fedorov

I think it would better not to defer native support for the operation. It should be relatively simple instruction. Otherwise we could wind up deferring this indefinitely.

Agreed. I will go with the native generation for now.

I mean we'd better implement native support for all the supported host architectures right away.

Kind regards,
Sergey

#10 lore raw

Re: [Qemu-devel] [RFC PATCH 2/3] tcg: Add support for fence generation in x86 backend

In reply to Alex Bennée
Quoted 5 lines · Alex Bennée

That would solves the problem of converting the various backends piecemeal - although obviously we should move to all backends having "native" support ASAP. However by introducing expensive substitute functions we will slow down the translations as each front end is expanded to translate the target barrier ops.

Obviously. We could in fact do that all up front if desired. It doesn't take long to look up the barrier instructions for each isa.

Quoted 3 lines

Should we make the emitting of the function call/TCGop conditional on MTTCG being enabled? If we are running in round-robin mode there is no need to issue any fence operations.

Probably. But to keep the translators clean we should probably hide that within tcg_gen_fence().

r~

#11 lore raw

Re: [Qemu-devel] [RFC PATCH 2/3] tcg: Add support for fence generation in x86 backend

In reply to Alex Bennée
Quoted 4 lines · Alex Bennée

Should we make the emitting of the function call/TCGop conditional on MTTCG being enabled? If we are running in round-robin mode there is no need to issue any fence operations.

Also, we should check if SMP(> 1 processors) is enabled since fences are not necessary on UP systems.

Signature · 1 line
Pranith
#12 lore raw

Re: [Qemu-devel] [RFC PATCH 2/3] tcg: Add support for fence generation in x86 backend

In reply to Richard Henderson

Hi Richard,

Thank you for the helpful comments.

Quoted 13 lines · Richard Henderson

tcg/i386/tcg-target.h extern bool have_bmi1; · line 103

Quoted 9 lines · Pranith Kumar
tcg/i386/tcg-target.h +1 -0
index 92be341..93ea42e 100644
@@ -100,6 +100,7 @@ extern bool have_bmi1;
 #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_fence            1

This has to be defined for all hosts.

OK. I will add an entry in tcg.h with default 0 and override in individual architecture once it is implemented.

Quoted 9 lines

tcg/i386/tcg-target.h static inline int · line 351

Quoted 6 lines
tcg/i386/tcg-target.h +1 -0
@@ -347,6 +347,7 @@ static inline int
 tcg_target_const_match(tcg_target_long val, TCGType type,
 #define OPC_SHRX        (0xf7 | P_EXT38 | P_SIMDF2)
 #define OPC_TESTL      (0x85)
 #define OPC_XCHG_ax_r32        (0x90)
+#define OPC_MFENCE      (0xAE | P_EXT)

Why define OPC_MFENCE if you're not going to use it? Of course, it's not exactly a complete and useful definition, so maybe just delete OPC_MFENCE.

I want to use OPC_MFENCE instead of hard-coding the value in tcg_out_fence(), but as you said the definition is not complete(it currently generates only 0x0FAE). I am trying to figure out how to generate 0x0FAEF0 using the definition.

Quoted 4 lines

Also, for 32-bit you need to check for sse2 before outputting this. See also the existing cpuid checks in tcg_target_init and the fallback smp_mb definition for pre-gcc-4.4.

OK, I'll check the current code and do something similar.

Thanks,

Signature · 1 line
Pranith
#13 lore raw

Re: [Qemu-devel] [RFC PATCH 2/3] tcg: Add support for fence generation in x86 backend

Quoted 32 lines · Pranith Kumar

Hi Richard,

Thank you for the helpful comments.

Quoted 13 lines · Richard Henderson

tcg/i386/tcg-target.h extern bool have_bmi1; · line 103

Quoted 9 lines · Pranith Kumar
tcg/i386/tcg-target.h +1 -0
index 92be341..93ea42e 100644
@@ -100,6 +100,7 @@ extern bool have_bmi1;
 #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_fence            1

This has to be defined for all hosts.

OK. I will add an entry in tcg.h with default 0 and override in individual architecture once it is implemented.

Quoted 9 lines

tcg/i386/tcg-target.h static inline int · line 351

Quoted 6 lines
tcg/i386/tcg-target.h +1 -0
@@ -347,6 +347,7 @@ static inline int
 tcg_target_const_match(tcg_target_long val, TCGType type,
 #define OPC_SHRX        (0xf7 | P_EXT38 | P_SIMDF2)
 #define OPC_TESTL      (0x85)
 #define OPC_XCHG_ax_r32        (0x90)
+#define OPC_MFENCE      (0xAE | P_EXT)

Why define OPC_MFENCE if you're not going to use it? Of course, it's not exactly a complete and useful definition, so maybe just delete OPC_MFENCE.

Quoted 4 lines

I want to use OPC_MFENCE instead of hard-coding the value in tcg_out_fence(), but as you said the definition is not complete(it currently generates only 0x0FAE). I am trying to figure out how to generate 0x0FAEF0 using the definition.

I think your going to have to just use tcg_out_fence() and the tcg_out_opc() does black magic with extra flag bits and assumes it is encoding rx registers (at least in 64 bit mode).

However I would suggest a comment and maybe breakdown of the different fence types you can emit. Will pre-P4 processors never need mfences?

Quoted 9 lines
Quoted 5 lines

Also, for 32-bit you need to check for sse2 before outputting this. See also the existing cpuid checks in tcg_target_init and the fallback smp_mb definition for pre-gcc-4.4.

OK, I'll check the current code and do something similar.

Thanks,

--
Alex Bennée

#14 lore raw

RFC PATCH 3/3 tcg: Add frontend support for fence gen in ARMv7

In reply to a message not in the archive
---
 target-arm/translate.c | 11 +++++++++--
 tcg/tcg-op.h           |  5 +++++
 tcg/tcg-opc.h          |  2 +-
 3 files changed, 15 insertions(+), 3 deletions(-)
target-arm/translate.c +9 -2
index c946c0e..3407176 100644
@@ -7980,9 +7980,13 @@ static void disas_arm_insn(DisasContext *s, unsigned int insn)
                 gen_clrex(s);
                 return;
             case 4: /* dsb */
+                ARCH(7);
+                return;
             case 5: /* dmb */
                 ARCH(7);
-                /* We don't emulate caches so these are a no-op.  */
+                if (TCG_TARGET_HAS_fence) {
+                    tcg_gen_fence();
+                }
                 return;
             case 6: /* isb */
                 /* We need to break the TB after this insn to execute
@@ -10330,8 +10334,11 @@ static int disas_thumb2_insn(CPUARMState *env, DisasContext *s, uint16_t insn_hw
                             gen_clrex(s);
                             break;
                         case 4: /* dsb */
+                            break;
                         case 5: /* dmb */
-                            /* These execute as NOPs.  */
+                            if (TCG_TARGET_HAS_fence) {
+                                tcg_gen_fence();
+                            }
                             break;
                         case 6: /* isb */
                             /* We need to break the TB after this insn
tcg/tcg-op.h +5 -0
index c446d3d..1c7e8fc 100644
@@ -261,6 +261,11 @@ static inline void tcg_gen_br(TCGLabel *l)
     tcg_gen_op1(&tcg_ctx, INDEX_op_br, label_arg(l));
 }
 
+static inline void tcg_gen_fence(void)
+{
+    tcg_gen_op1(&tcg_ctx, INDEX_op_fence, 0);
+}
+
 /* Helper calls. */
 
 /* 32 bit ops */
tcg/tcg-opc.h +1 -1
index b772d90..4696cf1 100644
@@ -42,7 +42,7 @@ DEF(br, 0, 0, 1, TCG_OPF_BB_END)
 # define IMPL64  TCG_OPF_64BIT
 #endif
 
-DEF(fence, 0, 0, 0, TCG_OPF_SIDE_EFFECTS)
+DEF(fence, 0, 0, 0, 0)
 
 DEF(mov_i32, 1, 1, 0, TCG_OPF_NOT_PRESENT)
 DEF(movi_i32, 1, 0, 1, TCG_OPF_NOT_PRESENT)
Signature · 1 line
2.8.3
#15 lore raw

Re: [RFC PATCH 3/3] tcg: Add frontend support for fence gen in ARMv7

target-arm/translate.c static void disas_arm_insn(DisasContext *s, unsigned int insn) · line 7989

Quoted 4 lines · Pranith Kumar
target-arm/translate.c +3 -1
-                /* We don't emulate caches so these are a no-op.  */
+                if (TCG_TARGET_HAS_fence) {
+                    tcg_gen_fence();
+                }

This should then be unconditional.

r~