Skip to contentSkip to conversation

Hexagon (target/hexagon) Improve USR handling

PATCH In review Taylor Simpson 2 participants 6 messages Nov 5, 2025 – Nov 6, 2025

lore
Share & tools
lore
#0 lore raw

v3 cover Hexagon (target/hexagon) Improve USR handling

Currently, any instruction that writes USR disables short-circuiting a packet. When the write is implicit, we can allow the short-circuit.

Changes in v2:
- Properly handle implicit USR writes when packet commit is necessary
- Added test case

Changes in v3:
- Address feedback from Brian Cain <[email protected]> Use symbolic/named operands in inline asm
- Added more packet test cases

Taylor Simpson (3):
Hexagon (target/hexagon) Add pkt_need_commit argument to arch_fpop_end Hexagon (target/hexagon) Implicit writes to USR don't force packet
commit
Hexagon (tests/tcg/hexagon) Add test for USR changes in packet

 target/hexagon/arch.h              |   2 +-
 target/hexagon/gen_tcg.h           | 114 ++++++------
 target/hexagon/helper.h            | 114 ++++++------
 target/hexagon/translate.h         |   1 +
 target/hexagon/arch.c              |   3 +-
 target/hexagon/op_helper.c         | 287 +++++++++++++++++------------
 target/hexagon/translate.c         |  35 ++--
 tests/tcg/hexagon/usr.c            |  54 ++++++
 target/hexagon/gen_helper_funcs.py |   8 +-
 target/hexagon/gen_tcg_funcs.py    |   4 +
 10 files changed, 371 insertions(+), 251 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 1/3 Hexagon (target/hexagon) Add pkt_need_commit argument to arch_fpop_end

The arch_fpop_end function converts the softfloat fp_status flags to bits set in Hexagon USR. It is hard-coded that the packet must need a commit. We add an argument to the function and update all the helper call sites.

In a subsequent commit, we will change the code that forces a commit when the packet contains an floating point instruction.

---
 target/hexagon/arch.h              |   2 +-
 target/hexagon/gen_tcg.h           | 114 ++++++------
 target/hexagon/helper.h            | 114 ++++++------
 target/hexagon/arch.c              |   3 +-
 target/hexagon/op_helper.c         | 287 +++++++++++++++++------------
 target/hexagon/gen_helper_funcs.py |   8 +-
 target/hexagon/gen_tcg_funcs.py    |   4 +
 7 files changed, 294 insertions(+), 238 deletions(-)
target/hexagon/arch.h +1 -1
index 70918065d3..c295a86600 100644
@@ -28,7 +28,7 @@ uint64_t interleave(uint32_t odd, uint32_t even);
 uint64_t deinterleave(uint64_t src);
 int32_t conv_round(int32_t a, int n);
 void arch_fpop_start(CPUHexagonState *env);
-void arch_fpop_end(CPUHexagonState *env);
+void arch_fpop_end(CPUHexagonState *env, bool pkt_need_commit);
 int arch_sf_recip_common(float32 *Rs, float32 *Rt, float32 *Rd,
                          int *adjust, float_status *fp_status);
 int arch_sf_invsqrt_common(float32 *Rs, float32 *Rd, int *adjust,
target/hexagon/gen_tcg.h +57 -57

193 lines not on this page · raw message

target/hexagon/helper.h +57 -57

129 lines not on this page · raw message

target/hexagon/arch.c +1 -2
index d053d68487..e17e714a6a 100644
@@ -222,9 +222,8 @@ void arch_fpop_start(CPUHexagonState *env)
         } \
     } while (0)
 
-void arch_fpop_end(CPUHexagonState *env)
+void arch_fpop_end(CPUHexagonState *env, bool pkt_need_commit)
 {
-    const bool pkt_need_commit = true;
     int flags = get_float_exception_flags(&env->fp_status);
     if (flags != 0) {
         SOFTFLOAT_TEST_FLAG(float_flag_inexact, FPINPF, FPINPE);
target/hexagon/op_helper.c +171 -116

755 lines not on this page · raw message

target/hexagon/gen_helper_funcs.py +3 -5
index a9c0e27a80..6206104424 100755
@@ -84,11 +84,6 @@ def gen_helper_function(f, tag, tagregs, tagimms):
         {hex_common.semdict[tag]}
     """))
 
-    if "A_FPOP" in hex_common.attribdict[tag]:
-        f.write(hex_common.code_fmt(f"""\
-            arch_fpop_end(env);
-        """))
-
     ## Return the scalar result
     for regtype, regid in regs:
         reg = hex_common.get_register(tag, regtype, regid)
@@ -116,6 +111,9 @@ def main():
             ## Skip the guest instructions
             if "A_GUEST" in hex_common.attribdict[tag]:
                 continue
+            ## Skip the floating point instructions
+            if "A_FPOP" in hex_common.attribdict[tag]:
+                continue
             ## Skip the diag instructions
             if tag == "Y6_diag":
                 continue
target/hexagon/gen_tcg_funcs.py +4 -0
index c2ba91ddc0..91b6a1e9d0 100755
@@ -77,6 +77,10 @@ def gen_tcg_func(f, tag, regs, imms):
         f.write(f"    emit_{tag}({arguments});\n")
 
     elif hex_common.skip_qemu_helper(tag):
+        if "A_FPOP" in hex_common.attribdict[tag]:
+            f.write("    TCGv pkt_need_commit = ")
+            f.write("tcg_constant_tl(ctx->need_commit);\n")
+
         f.write(f"    fGEN_TCG_{tag}({hex_common.semdict[tag]});\n")
     else:
         ## Generate the call to the helper
Signature · 1 line
2.43.0
#2 lore raw

PATCH v3 2/3 Hexagon (target/hexagon) Implicit writes to USR don't force packet commit

In reply to Taylor Simpson

Implicit writes to USR are only to specific fields of USR to indicate side effects (e.g., saturation overflow, floating point status). In these cases, we don't force a packet commit. This will allow more packets to be short-circuited (avoid writing the results to temporaries).

When there is a packet commit with an implicit write to USR, we initialize new_value_usr during gen_start_packet and write to USR in gen_reg_writes.

---
 target/hexagon/translate.h |  1 +
 target/hexagon/translate.c | 35 ++++++++++++++++++++++-------------
 2 files changed, 23 insertions(+), 13 deletions(-)
target/hexagon/translate.h +1 -0
index d251e2233f..a0102b6cbd 100644
@@ -39,6 +39,7 @@ typedef struct DisasContext {
     int reg_log_idx;
     DECLARE_BITMAP(regs_written, TOTAL_PER_THREAD_REGS);
     DECLARE_BITMAP(predicated_regs, TOTAL_PER_THREAD_REGS);
+    bool implicit_usr_write;
     int preg_log[PRED_WRITES_MAX];
     int preg_log_idx;
     DECLARE_BITMAP(pregs_written, NUM_PREGS);
target/hexagon/translate.c +22 -13
index 8fce219c0d..f3240953b5 100644
@@ -272,12 +272,7 @@ static void mark_implicit_reg_write(DisasContext *ctx, int attrib, int rnum)
 {
     uint16_t opcode = ctx->insn->opcode;
     if (GET_ATTRIB(opcode, attrib)) {
-        /*
-         * USR is used to set overflow and FP exceptions,
-         * so treat it as conditional
-         */
-        bool is_predicated = GET_ATTRIB(opcode, A_CONDEXEC) ||
-                             rnum == HEX_REG_USR;
+        bool is_predicated = GET_ATTRIB(opcode, A_CONDEXEC);
 
         /* LC0/LC1 is conditionally written by endloop instructions */
         if ((rnum == HEX_REG_LC0 || rnum == HEX_REG_LC1) &&
@@ -291,6 +286,14 @@ static void mark_implicit_reg_write(DisasContext *ctx, int attrib, int rnum)
     }
 }
 
+static void mark_implicit_usr_write(DisasContext *ctx, int attrib)
+{
+    uint16_t opcode = ctx->insn->opcode;
+    if (GET_ATTRIB(opcode, attrib)) {
+        ctx->implicit_usr_write = true;
+    }
+}
+
 static void mark_implicit_reg_writes(DisasContext *ctx)
 {
     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_FP,  HEX_REG_FP);
@@ -300,8 +303,9 @@ static void mark_implicit_reg_writes(DisasContext *ctx)
     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_SA0, HEX_REG_SA0);
     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_LC1, HEX_REG_LC1);
     mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_SA1, HEX_REG_SA1);
-    mark_implicit_reg_write(ctx, A_IMPLICIT_WRITES_USR, HEX_REG_USR);
-    mark_implicit_reg_write(ctx, A_FPOP, HEX_REG_USR);
+
+    mark_implicit_usr_write(ctx, A_IMPLICIT_WRITES_USR);
+    mark_implicit_usr_write(ctx, A_FPOP);
 }
 
 static void mark_implicit_pred_write(DisasContext *ctx, int attrib, int pnum)
@@ -351,11 +355,6 @@ static bool need_commit(DisasContext *ctx)
         }
     }
 
-    /* Floating point instructions are hard-coded to use new_value */
-    if (check_for_attrib(pkt, A_FPOP)) {
-        return true;
-    }
-
     if (ctx->read_after_write || ctx->has_hvx_overlap) {
         return true;
     }
@@ -467,6 +466,12 @@ static void gen_start_packet(DisasContext *ctx)
         }
     }
 
+    /* Preload usr to new_value_usr */
+    if (ctx->need_commit && ctx->implicit_usr_write &&
+        !test_bit(HEX_REG_USR, ctx->regs_written)) {
+        tcg_gen_mov_tl(hex_new_value_usr, hex_gpr[HEX_REG_USR]);
+    }
+
     /*
      * Preload the predicated pred registers into ctx->new_pred_value[pred_num]
      * Only endloop instructions conditionally write to pred registers
@@ -587,6 +592,10 @@ static void gen_reg_writes(DisasContext *ctx)
             ctx->is_tight_loop = false;
         }
     }
+
+    if (ctx->implicit_usr_write && !test_bit(HEX_REG_USR, ctx->regs_written)) {
+        tcg_gen_mov_tl(hex_gpr[HEX_REG_USR], hex_new_value_usr);
+    }
 }
 
 static void gen_pred_writes(DisasContext *ctx)
Signature · 1 line
2.43.0
#3 lore raw

PATCH v3 3/3 Hexagon (tests/tcg/hexagon) Add test for USR changes in packet

In reply to Taylor Simpson
---
 tests/tcg/hexagon/usr.c | 54 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
tests/tcg/hexagon/usr.c +54 -0
index f0b23d312b..ef1787c64c 100644
@@ -608,6 +608,58 @@ TEST_CMP_xx(uint32_t, uint32_t, FUNC, SRC1, SRC2, RES, USR_RES)
 #define TEST_CMP_PP(FUNC, SRC1, SRC2, RES, USR_RES) \
 TEST_CMP_xx(uint64_t, uint64_t, FUNC, SRC1, SRC2, RES, USR_RES)
 
+static void test_usr_packets(void)
+{
+    uint32_t usr_out;
+    /* Test setting USR bits inside and outside packets */
+    asm(CLEAR_USRBITS \
+        "r10 = satub(%[val_0xfff])                   /* Set usr.OVF */\n\t"
+        "{\n\t"
+        "    r11 = convert_uw2sf(%[val_0x010020a5])  /* Set usr.FPINPF */\n\t"
+        "    r10 = memw(%[err])                      /* Force pkt commit */\n\t"
+        "}\n\t"
+        "{\n\t"
+        "    r11 = sfadd(%[SF_one], %[SF_SNaN])      /* Set usr.FPINVF */\n\t"
+        "    r10 = add(r10, #1)                      /* No pkt commit */\n\t"
+        "}\n\t"
+        "%[usr_out] = usr\n\t"
+        : [usr_out]"=r"(usr_out)
+        : [val_0xfff]"r"(0xfff),
+          [SF_one]"r"(SF_one), [SF_SNaN]"r"(SF_SNaN),
+          [val_0x010020a5]"r"(0x010020a5),
+          [err]"m"(err)
+        : "r2", "r10", "r11", "usr");
+    check32(usr_out & 0x3f, USR_OVF | USR_FPINVF | USR_FPINPF);
+
+    /* Test setting several USR bits in the same packet (no pkt commit) */
+    asm(CLEAR_USRBITS \
+        "{\n\t"
+        "    r10 = satub(%[val_0xfff])               /* Set usr.OVF */\n\t"
+        "    r12 = sfadd(%[SF_one], %[SF_SNaN])      /* Set usr.FPINVF */\n\t"
+        "}\n\t"
+        "%[usr_out] = usr\n\t"
+        : [usr_out]"=r"(usr_out)
+        : [val_0xfff]"r"(0xfff),
+          [SF_one]"r"(SF_one), [SF_SNaN]"r"(SF_SNaN)
+        : "r2", "r10", "r11", "r12", "usr");
+    check32(usr_out & 0x3f, USR_OVF | USR_FPINVF);
+
+    /* Test setting several USR bits in the same packet (with pkt commit) */
+    asm(CLEAR_USRBITS \
+        "{\n\t"
+        "    r10 = satub(%[val_0xfff])               /* Set usr.OVF */\n\t"
+        "    r11 = convert_uw2sf(%[val_0x010020a5])  /* Set usr.FPINPF */\n\t"
+        "    r12 = memw(%[err])                      /* Force pkt commit */\n\t"
+        "}\n\t"
+        "%[usr_out] = usr\n\t"
+        : [usr_out]"=r"(usr_out)
+        : [val_0xfff]"r"(0xfff),
+          [val_0x010020a5]"r"(0x010020a5),
+          [err]"m"(err)
+        : "r2", "r10", "r11", "r12", "usr");
+    check32(usr_out & 0x3f, USR_OVF | USR_FPINPF);
+}
+
 int main()
 {
     TEST_R_OP_R(satub,       0,         0,         USR_CLEAR);
@@ -1097,6 +1149,8 @@ int main()
     TEST_Rp_OP_R(sfinvsqrta, SF_small_neg,  SF_HEX_NaN,       0x00, USR_FPINVF);
     TEST_Rp_OP_R(sfinvsqrta, SF_SNaN,       SF_HEX_NaN,       0x00, USR_FPINVF);
 
+    test_usr_packets();
+
     puts(err ? "FAIL" : "PASS");
     return err;
 }
Signature · 1 line
2.43.0
Reviewed-by on v3 3/3
#4 lore raw

Re: [PATCH v3 3/3] Hexagon (tests/tcg/hexagon) Add test for USR changes in packet

Quoted 3 lines · Taylor Simpson

---

Quoted 93 lines
 tests/tcg/hexagon/usr.c | 54 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
tests/tcg/hexagon/usr.c +9 -0
index f0b23d312b..ef1787c64c 100644
@@ -608,6 +608,58 @@ TEST_CMP_xx(uint32_t, uint32_t, FUNC, SRC1, SRC2,
 RES, USR_RES)
 #define TEST_CMP_PP(FUNC, SRC1, SRC2, RES, USR_RES) \
 TEST_CMP_xx(uint64_t, uint64_t, FUNC, SRC1, SRC2, RES, USR_RES)
 
+static void test_usr_packets(void)
+{
+    uint32_t usr_out;
+    /* Test setting USR bits inside and outside packets */
+    asm(CLEAR_USRBITS \
+        "r10 = satub(%[val_0xfff])                   /* Set usr.OVF
 */\n\t"
+        "{\n\t"
+        "    r11 = convert_uw2sf(%[val_0x010020a5])  /* Set usr.FPINPF
 */\n\t"
+        "    r10 = memw(%[err])                      /* Force pkt commit

*/\n\t"

tests/tcg/hexagon/usr.c +3 -0
+        "}\n\t"
+        "{\n\t"
+        "    r11 = sfadd(%[SF_one], %[SF_SNaN])      /* Set usr.FPINVF

*/\n\t"
+ " r10 = add(r10, #1) /* No pkt commit
*/\n\t"

tests/tcg/hexagon/usr.c +14 -0
+        "}\n\t"
+        "%[usr_out] = usr\n\t"
+        : [usr_out]"=r"(usr_out)
+        : [val_0xfff]"r"(0xfff),
+          [SF_one]"r"(SF_one), [SF_SNaN]"r"(SF_SNaN),
+          [val_0x010020a5]"r"(0x010020a5),
+          [err]"m"(err)
+        : "r2", "r10", "r11", "usr");
+    check32(usr_out & 0x3f, USR_OVF | USR_FPINVF | USR_FPINPF);
+
+    /* Test setting several USR bits in the same packet (no pkt commit) */
+    asm(CLEAR_USRBITS \
+        "{\n\t"
+        "    r10 = satub(%[val_0xfff])               /* Set usr.OVF

*/\n\t"
+ " r12 = sfadd(%[SF_one], %[SF_SNaN]) /* Set usr.FPINVF
*/\n\t"

tests/tcg/hexagon/usr.c +9 -0
+        "}\n\t"
+        "%[usr_out] = usr\n\t"
+        : [usr_out]"=r"(usr_out)
+        : [val_0xfff]"r"(0xfff),
+          [SF_one]"r"(SF_one), [SF_SNaN]"r"(SF_SNaN)
+        : "r2", "r10", "r11", "r12", "usr");
+    check32(usr_out & 0x3f, USR_OVF | USR_FPINVF);
+
+    /* Test setting several USR bits in the same packet (with pkt commit)

*/

tests/tcg/hexagon/usr.c +3 -0
+    asm(CLEAR_USRBITS \
+        "{\n\t"
+        "    r10 = satub(%[val_0xfff])               /* Set usr.OVF

*/\n\t"
+ " r11 = convert_uw2sf(%[val_0x010020a5]) /* Set usr.FPINPF
*/\n\t"
+ " r12 = memw(%[err]) /* Force pkt commit
*/\n\t"

tests/tcg/hexagon/usr.c +12 -0
+        "}\n\t"
+        "%[usr_out] = usr\n\t"
+        : [usr_out]"=r"(usr_out)
+        : [val_0xfff]"r"(0xfff),
+          [val_0x010020a5]"r"(0x010020a5),
+          [err]"m"(err)
+        : "r2", "r10", "r11", "r12", "usr");
+    check32(usr_out & 0x3f, USR_OVF | USR_FPINPF);
+}
+
 int main()
 {
     TEST_R_OP_R(satub,       0,         0,         USR_CLEAR);
@@ -1097,6 +1149,8 @@ int main()
     TEST_Rp_OP_R(sfinvsqrta, SF_small_neg,  SF_HEX_NaN,       0x00,
 USR_FPINVF);
     TEST_Rp_OP_R(sfinvsqrta, SF_SNaN,       SF_HEX_NaN,       0x00,
 USR_FPINVF);
 
+    test_usr_packets();
+
     puts(err ? "FAIL" : "PASS");
     return err;

}
--
2.43.0

#5 lore raw
In reply to Taylor Simpson
Quoted 3 lines · Taylor Simpson

Currently, any instruction that writes USR disables short-circuiting a packet. When the write is implicit, we can allow the short-circuit.

This series is now queued at https://github.com/quic/qemu branch hex-next-express

Quoted 33 lines

Changes in v2:
- Properly handle implicit USR writes when packet commit is necessary
- Added test case

Changes in v3:
- Address feedback from Brian Cain <[email protected]> Use symbolic/named operands in inline asm
- Added more packet test cases

Taylor Simpson (3):
Hexagon (target/hexagon) Add pkt_need_commit argument to arch_fpop_end Hexagon (target/hexagon) Implicit writes to USR don't force packet
commit
Hexagon (tests/tcg/hexagon) Add test for USR changes in packet

 target/hexagon/arch.h              |   2 +-
 target/hexagon/gen_tcg.h           | 114 ++++++------
 target/hexagon/helper.h            | 114 ++++++------
 target/hexagon/translate.h         |   1 +
 target/hexagon/arch.c              |   3 +-
 target/hexagon/op_helper.c         | 287 +++++++++++++++++------------
 target/hexagon/translate.c         |  35 ++--
 tests/tcg/hexagon/usr.c            |  54 ++++++
 target/hexagon/gen_helper_funcs.py |   8 +-
 target/hexagon/gen_tcg_funcs.py    |   4 +
 10 files changed, 371 insertions(+), 251 deletions(-)

--
2.43.0