Skip to contentSkip to conversation

Hexagon (target/hexagon) Only pass env to generated helper when needed

PATCH In review Taylor Simpson 2 participants 7 messages Feb 14, 2024 – Feb 16, 2024

lore
Share & tools
lore
#0 lore raw

v3 cover Hexagon (target/hexagon) Only pass env to generated helper when needed

Currently, we pass env to every generated helper. When the semantics of the instruction only depend on the arguments, this is unnecessary and adds extra overhead to the helper call.

**** Changes in v3 ****
Update copyright year to 2024
Mark Reviewed-by/Tested-by: Anton Johansson <[email protected]>

**** Changes in v2 ****
- Separate patches to pass P0 and SP explicitly to helpers that need it
- Add the TCG_CALL_NO_RWG_SE flag to any non-HVX helpers that don't get ptr to env

Taylor Simpson (3):
Hexagon (target/hexagon) Pass P0 explicitly to helpers that need it Hexagon (target/hexagon) Pass SP explicitly to helpers that need it Hexagon (target/hexagon) Only pass env to generated helper when needed

 target/hexagon/gen_tcg.h            |  5 +++-
 target/hexagon/macros.h             |  6 ++--
 target/hexagon/attribs_def.h.inc    |  3 +-
 target/hexagon/gen_helper_protos.py | 12 ++++++--
 target/hexagon/hex_common.py        | 46 +++++++++++++++++++++++++----
 5 files changed, 59 insertions(+), 13 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v3 1/3 Hexagon (target/hexagon) Pass P0 explicitly to helpers that need it

Rather than reading P0 from the env, pass it explicitly

---
 target/hexagon/macros.h      |  4 ++--
 target/hexagon/hex_common.py | 12 +++++++++++-
 2 files changed, 13 insertions(+), 3 deletions(-)
target/hexagon/macros.h +2 -2
index 1376d6ccc1..aedc863fab 100644
@@ -1,5 +1,5 @@
 /*
- *  Copyright(c) 2019-2023 Qualcomm Innovation Center, Inc. All Rights Reserved.
+ *  Copyright(c) 2019-2024 Qualcomm Innovation Center, Inc. All Rights Reserved.
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -358,7 +358,7 @@ static inline TCGv gen_read_ireg(TCGv result, TCGv val, int shift)
 #endif
 #define fREAD_PC() (PC)
 
-#define fREAD_P0() (env->pred[0])
+#define fREAD_P0() (P0)
 
 #define fCHECK_PCALIGN(A)
 
target/hexagon/hex_common.py +11 -1
index 195620c7ec..14dcf261b4 100755
@@ -1,7 +1,7 @@
 #!/usr/bin/env python3
 
 ##
-##  Copyright(c) 2019-2023 Qualcomm Innovation Center, Inc. All Rights Reserved.
+##  Copyright(c) 2019-2024 Qualcomm Innovation Center, Inc. All Rights Reserved.
 ##
 ##  This program is free software; you can redistribute it and/or modify
 ##  it under the terms of the GNU General Public License as published by
@@ -197,6 +197,10 @@ def get_tagimms():
     return dict(zip(tags, list(map(compute_tag_immediates, tags))))
 
 
+def need_p0(tag):
+    return "A_IMPLICIT_READS_P0" in attribdict[tag]
+
+
 def need_slot(tag):
     if (
         "A_CVI_SCATTER" not in attribdict[tag]
@@ -1118,6 +1122,12 @@ def helper_args(tag, regs, imms):
             "tcg_constant_tl(ctx->next_PC)",
             "target_ulong next_PC"
         ))
+    if need_p0(tag):
+        args.append(HelperArg(
+            "i32",
+            "hex_pred[0]",
+            "uint32_t P0"
+        ))
     if need_slot(tag):
         args.append(HelperArg(
             "i32",
Signature · 1 line
2.34.1
Reviewed-by on v3 1/3
#2 lore raw

RE: [PATCH v3 1/3] Hexagon (target/hexagon) Pass P0 explicitly to helpers that need it

Quoted 16 lines

-----Original Message-----
From: Taylor Simpson <[email protected]>
Sent: Tuesday, February 13, 2024 10:27 PM
To: [email protected]
Cc: Brian Cain <[email protected]>; Matheus Bernardino (QUIC) <[email protected]>; Sid Manning <[email protected]>; Marco Liebel (QUIC) <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [PATCH v3 1/3] Hexagon (target/hexagon) Pass P0 explicitly to helpers that need it

WARNING: This email originated from outside of Qualcomm. Please be wary of any links or attachments, and do not enable macros.

Quoted 6 lines

Rather than reading P0 from the env, pass it explicitly

---

Quoted 68 lines
 target/hexagon/macros.h      |  4 ++--
 target/hexagon/hex_common.py | 12 +++++++++++-
 2 files changed, 13 insertions(+), 3 deletions(-)
target/hexagon/macros.h +0 -1
index 1376d6ccc1..aedc863fab 100644
@@ -1,5 +1,5 @@
 /*
- *  Copyright(c) 2019-2023 Qualcomm Innovation Center, Inc. All Rights

Reserved.
+ * Copyright(c) 2019-2024 Qualcomm Innovation Center, Inc. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by

target/hexagon/macros.h +1 -1
@@ -358,7 +358,7 @@ static inline TCGv gen_read_ireg(TCGv result, TCGv val,
 int shift)
 #endif
 #define fREAD_PC() (PC)
 
-#define fREAD_P0() (env->pred[0])
+#define fREAD_P0() (P0)
 
 #define fCHECK_PCALIGN(A)
target/hexagon/hex_common.py +0 -1
index 195620c7ec..14dcf261b4 100755
@@ -1,7 +1,7 @@
 #!/usr/bin/env python3
 
 ##
-##  Copyright(c) 2019-2023 Qualcomm Innovation Center, Inc. All Rights

Reserved.
+## Copyright(c) 2019-2024 Qualcomm Innovation Center, Inc. All Rights Reserved.
##
## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by

target/hexagon/hex_common.py +10 -1
@@ -197,6 +197,10 @@ def get_tagimms():
     return dict(zip(tags, list(map(compute_tag_immediates, tags))))
 
 
+def need_p0(tag):
+    return "A_IMPLICIT_READS_P0" in attribdict[tag]
+
+
 def need_slot(tag):
     if (
         "A_CVI_SCATTER" not in attribdict[tag]
@@ -1118,6 +1122,12 @@ def helper_args(tag, regs, imms):
             "tcg_constant_tl(ctx->next_PC)",
             "target_ulong next_PC"
         ))
+    if need_p0(tag):
+        args.append(HelperArg(
+            "i32",
+            "hex_pred[0]",
+            "uint32_t P0"
+        ))
     if need_slot(tag):
         args.append(HelperArg(
             "i32",
--

2.34.1

#3 lore raw

PATCH v3 2/3 Hexagon (target/hexagon) Pass SP explicitly to helpers that need it

In reply to Taylor Simpson

Rather than reading SP from the env, pass it explicitly

---
 target/hexagon/macros.h          |  2 +-
 target/hexagon/attribs_def.h.inc |  3 ++-
 target/hexagon/hex_common.py     | 11 +++++++++++
 3 files changed, 14 insertions(+), 2 deletions(-)
target/hexagon/macros.h +1 -1
index aedc863fab..feb798c6c0 100644
@@ -343,7 +343,7 @@ static inline TCGv gen_read_ireg(TCGv result, TCGv val, int shift)
 
 #define fREAD_LR() (env->gpr[HEX_REG_LR])
 
-#define fREAD_SP() (env->gpr[HEX_REG_SP])
+#define fREAD_SP() (SP)
 #define fREAD_LC0 (env->gpr[HEX_REG_LC0])
 #define fREAD_LC1 (env->gpr[HEX_REG_LC1])
 #define fREAD_SA0 (env->gpr[HEX_REG_SA0])
target/hexagon/attribs_def.h.inc +2 -1
index 87942d46f4..9e3a05f882 100644
@@ -1,5 +1,5 @@
 /*
- *  Copyright(c) 2019-2023 Qualcomm Innovation Center, Inc. All Rights Reserved.
+ *  Copyright(c) 2019-2024 Qualcomm Innovation Center, Inc. All Rights Reserved.
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -117,6 +117,7 @@ DEF_ATTRIB(IMPLICIT_READS_P1, "Reads the P1 register", "", "")
 DEF_ATTRIB(IMPLICIT_READS_P2, "Reads the P2 register", "", "")
 DEF_ATTRIB(IMPLICIT_READS_P3, "Reads the P3 register", "", "")
 DEF_ATTRIB(IMPLICIT_WRITES_USR, "May write USR", "", "")
+DEF_ATTRIB(IMPLICIT_READS_SP, "Reads the SP register", "", "")
 DEF_ATTRIB(COMMUTES, "The operation is communitive", "", "")
 DEF_ATTRIB(DEALLOCRET, "dealloc_return", "", "")
 DEF_ATTRIB(DEALLOCFRAME, "deallocframe", "", "")
target/hexagon/hex_common.py +11 -0
index 14dcf261b4..b96f67972d 100755
@@ -101,6 +101,7 @@ def calculate_attribs():
     add_qemu_macro_attrib('fLSBNEW1', 'A_IMPLICIT_READS_P1')
     add_qemu_macro_attrib('fLSBNEW1NOT', 'A_IMPLICIT_READS_P1')
     add_qemu_macro_attrib('fREAD_P3', 'A_IMPLICIT_READS_P3')
+    add_qemu_macro_attrib('fREAD_SP', 'A_IMPLICIT_READS_SP')
 
     # Recurse down macros, find attributes from sub-macros
     macroValues = list(macros.values())
@@ -201,6 +202,10 @@ def need_p0(tag):
     return "A_IMPLICIT_READS_P0" in attribdict[tag]
 
 
+def need_sp(tag):
+    return "A_IMPLICIT_READS_SP" in attribdict[tag]
+
+
 def need_slot(tag):
     if (
         "A_CVI_SCATTER" not in attribdict[tag]
@@ -1128,6 +1133,12 @@ def helper_args(tag, regs, imms):
             "hex_pred[0]",
             "uint32_t P0"
         ))
+    if need_sp(tag):
+        args.append(HelperArg(
+            "i32",
+            "hex_gpr[HEX_REG_SP]",
+            "uint32_t SP"
+        ))
     if need_slot(tag):
         args.append(HelperArg(
             "i32",
Signature · 1 line
2.34.1
Reviewed-by on v3 2/3
#4 lore raw

RE: [PATCH v3 2/3] Hexagon (target/hexagon) Pass SP explicitly to helpers that need it

Quoted 16 lines

-----Original Message-----
From: Taylor Simpson <[email protected]>
Sent: Tuesday, February 13, 2024 10:27 PM
To: [email protected]
Cc: Brian Cain <[email protected]>; Matheus Bernardino (QUIC) <[email protected]>; Sid Manning <[email protected]>; Marco Liebel (QUIC) <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [PATCH v3 2/3] Hexagon (target/hexagon) Pass SP explicitly to helpers that need it

WARNING: This email originated from outside of Qualcomm. Please be wary of any links or attachments, and do not enable macros.

Quoted 6 lines

Rather than reading SP from the env, pass it explicitly

---

Quoted 79 lines
 target/hexagon/macros.h          |  2 +-
 target/hexagon/attribs_def.h.inc |  3 ++-
 target/hexagon/hex_common.py     | 11 +++++++++++
 3 files changed, 14 insertions(+), 2 deletions(-)
target/hexagon/macros.h +1 -1
index aedc863fab..feb798c6c0 100644
@@ -343,7 +343,7 @@ static inline TCGv gen_read_ireg(TCGv result, TCGv val,
 int shift)
 
 #define fREAD_LR() (env->gpr[HEX_REG_LR])
 
-#define fREAD_SP() (env->gpr[HEX_REG_SP])
+#define fREAD_SP() (SP)
 #define fREAD_LC0 (env->gpr[HEX_REG_LC0])
 #define fREAD_LC1 (env->gpr[HEX_REG_LC1])

#define fREAD_SA0 (env->gpr[HEX_REG_SA0])

target/hexagon/attribs_def.h.inc +0 -1
index 87942d46f4..9e3a05f882 100644
@@ -1,5 +1,5 @@
 /*
- *  Copyright(c) 2019-2023 Qualcomm Innovation Center, Inc. All Rights

Reserved.
+ * Copyright(c) 2019-2024 Qualcomm Innovation Center, Inc. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by

target/hexagon/attribs_def.h.inc +1 -0
@@ -117,6 +117,7 @@ DEF_ATTRIB(IMPLICIT_READS_P1, "Reads the P1
 register", "", "")
 DEF_ATTRIB(IMPLICIT_READS_P2, "Reads the P2 register", "", "")
 DEF_ATTRIB(IMPLICIT_READS_P3, "Reads the P3 register", "", "")
 DEF_ATTRIB(IMPLICIT_WRITES_USR, "May write USR", "", "")
+DEF_ATTRIB(IMPLICIT_READS_SP, "Reads the SP register", "", "")
 DEF_ATTRIB(COMMUTES, "The operation is communitive", "", "")
 DEF_ATTRIB(DEALLOCRET, "dealloc_return", "", "")

DEF_ATTRIB(DEALLOCFRAME, "deallocframe", "", "")

target/hexagon/hex_common.py +11 -1
index 14dcf261b4..b96f67972d 100755
@@ -101,6 +101,7 @@ def calculate_attribs():
     add_qemu_macro_attrib('fLSBNEW1', 'A_IMPLICIT_READS_P1')
     add_qemu_macro_attrib('fLSBNEW1NOT', 'A_IMPLICIT_READS_P1')
     add_qemu_macro_attrib('fREAD_P3', 'A_IMPLICIT_READS_P3')
+    add_qemu_macro_attrib('fREAD_SP', 'A_IMPLICIT_READS_SP')
 
     # Recurse down macros, find attributes from sub-macros
     macroValues = list(macros.values())
@@ -201,6 +202,10 @@ def need_p0(tag):
     return "A_IMPLICIT_READS_P0" in attribdict[tag]
 
 
+def need_sp(tag):
+    return "A_IMPLICIT_READS_SP" in attribdict[tag]
+
+
 def need_slot(tag):
     if (
         "A_CVI_SCATTER" not in attribdict[tag]
@@ -1128,6 +1133,12 @@ def helper_args(tag, regs, imms):
             "hex_pred[0]",
             "uint32_t P0"
         ))
+    if need_sp(tag):
+        args.append(HelperArg(
+            "i32",
+            "hex_gpr[HEX_REG_SP]",
+            "uint32_t SP"
+        ))
     if need_slot(tag):
         args.append(HelperArg(
             "i32",
--

2.34.1

Reviewed-by on v3 2/3
#5 lore raw

RE: [PATCH v3 2/3] Hexagon (target/hexagon) Pass SP explicitly to helpers that need it

In reply to Taylor Simpson
Quoted 16 lines

-----Original Message-----
From: Taylor Simpson <[email protected]>
Sent: Tuesday, February 13, 2024 10:27 PM
To: [email protected]
Cc: Brian Cain <[email protected]>; Matheus Bernardino (QUIC) <[email protected]>; Sid Manning <[email protected]>; Marco Liebel (QUIC) <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [PATCH v3 2/3] Hexagon (target/hexagon) Pass SP explicitly to helpers that need it

WARNING: This email originated from outside of Qualcomm. Please be wary of any links or attachments, and do not enable macros.

Quoted 6 lines

Rather than reading SP from the env, pass it explicitly

---

Quoted 79 lines
 target/hexagon/macros.h          |  2 +-
 target/hexagon/attribs_def.h.inc |  3 ++-
 target/hexagon/hex_common.py     | 11 +++++++++++
 3 files changed, 14 insertions(+), 2 deletions(-)
target/hexagon/macros.h +1 -1
index aedc863fab..feb798c6c0 100644
@@ -343,7 +343,7 @@ static inline TCGv gen_read_ireg(TCGv result, TCGv val,
 int shift)
 
 #define fREAD_LR() (env->gpr[HEX_REG_LR])
 
-#define fREAD_SP() (env->gpr[HEX_REG_SP])
+#define fREAD_SP() (SP)
 #define fREAD_LC0 (env->gpr[HEX_REG_LC0])
 #define fREAD_LC1 (env->gpr[HEX_REG_LC1])

#define fREAD_SA0 (env->gpr[HEX_REG_SA0])

target/hexagon/attribs_def.h.inc +0 -1
index 87942d46f4..9e3a05f882 100644
@@ -1,5 +1,5 @@
 /*
- *  Copyright(c) 2019-2023 Qualcomm Innovation Center, Inc. All Rights

Reserved.
+ * Copyright(c) 2019-2024 Qualcomm Innovation Center, Inc. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by

target/hexagon/attribs_def.h.inc +1 -0
@@ -117,6 +117,7 @@ DEF_ATTRIB(IMPLICIT_READS_P1, "Reads the P1
 register", "", "")
 DEF_ATTRIB(IMPLICIT_READS_P2, "Reads the P2 register", "", "")
 DEF_ATTRIB(IMPLICIT_READS_P3, "Reads the P3 register", "", "")
 DEF_ATTRIB(IMPLICIT_WRITES_USR, "May write USR", "", "")
+DEF_ATTRIB(IMPLICIT_READS_SP, "Reads the SP register", "", "")
 DEF_ATTRIB(COMMUTES, "The operation is communitive", "", "")
 DEF_ATTRIB(DEALLOCRET, "dealloc_return", "", "")

DEF_ATTRIB(DEALLOCFRAME, "deallocframe", "", "")

target/hexagon/hex_common.py +11 -1
index 14dcf261b4..b96f67972d 100755
@@ -101,6 +101,7 @@ def calculate_attribs():
     add_qemu_macro_attrib('fLSBNEW1', 'A_IMPLICIT_READS_P1')
     add_qemu_macro_attrib('fLSBNEW1NOT', 'A_IMPLICIT_READS_P1')
     add_qemu_macro_attrib('fREAD_P3', 'A_IMPLICIT_READS_P3')
+    add_qemu_macro_attrib('fREAD_SP', 'A_IMPLICIT_READS_SP')
 
     # Recurse down macros, find attributes from sub-macros
     macroValues = list(macros.values())
@@ -201,6 +202,10 @@ def need_p0(tag):
     return "A_IMPLICIT_READS_P0" in attribdict[tag]
 
 
+def need_sp(tag):
+    return "A_IMPLICIT_READS_SP" in attribdict[tag]
+
+
 def need_slot(tag):
     if (
         "A_CVI_SCATTER" not in attribdict[tag]
@@ -1128,6 +1133,12 @@ def helper_args(tag, regs, imms):
             "hex_pred[0]",
             "uint32_t P0"
         ))
+    if need_sp(tag):
+        args.append(HelperArg(
+            "i32",
+            "hex_gpr[HEX_REG_SP]",
+            "uint32_t SP"
+        ))
     if need_slot(tag):
         args.append(HelperArg(
             "i32",
--

2.34.1

#6 lore raw

PATCH v3 3/3 Hexagon (target/hexagon) Only pass env to generated helper when needed

In reply to Taylor Simpson

Currently, we pass env to every generated helper. When the semantics of the instruction only depend on the arguments, this is unnecessary and adds extra overhead to the helper call.

We add the TCG_CALL_NO_RWG_SE flag to any non-HVX helpers that don't get the ptr to env.

The A2_nop and SA1_setin1 instructions end up with no arguments. This results in a "old-style function definition" error from the compiler, so we write overrides for them.

With this change, the number of helpers with env argument is
idef-parser enabled: 329 total, 23 with env
idef-parser disabled: 1543 total, 550 with env

---
 target/hexagon/gen_tcg.h            |  5 ++++-
 target/hexagon/gen_helper_protos.py | 12 ++++++++++--
 target/hexagon/hex_common.py        | 23 ++++++++++++++++++-----
 3 files changed, 32 insertions(+), 8 deletions(-)
target/hexagon/gen_tcg.h +4 -1
index 1c4391b415..3fc1f4e281 100644
@@ -1,5 +1,5 @@
 /*
- *  Copyright(c) 2019-2023 Qualcomm Innovation Center, Inc. All Rights Reserved.
+ *  Copyright(c) 2019-2024 Qualcomm Innovation Center, Inc. All Rights Reserved.
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -1369,3 +1369,6 @@
         gen_helper_raise_exception(tcg_env, excp); \
     } while (0)
 #endif
+
+#define fGEN_TCG_A2_nop(SHORTCODE) do { } while (0)
+#define fGEN_TCG_SA1_setin1(SHORTCODE) tcg_gen_movi_tl(RdV, -1)
target/hexagon/gen_helper_protos.py +10 -2
index c82b0f54e4..f8578d5033 100755
@@ -1,7 +1,7 @@
 #!/usr/bin/env python3
 
 ##
-##  Copyright(c) 2019-2023 Qualcomm Innovation Center, Inc. All Rights Reserved.
+##  Copyright(c) 2019-2024 Qualcomm Innovation Center, Inc. All Rights Reserved.
 ##
 ##  This program is free software; you can redistribute it and/or modify
 ##  it under the terms of the GNU General Public License as published by
@@ -40,7 +40,15 @@ def gen_helper_prototype(f, tag, tagregs, tagimms):
         declared.append(arg.proto_arg)
 
     arguments = ", ".join(declared)
-    f.write(f"DEF_HELPER_{len(declared) - 1}({tag}, {arguments})\n")
+
+    ## Add the TCG_CALL_NO_RWG_SE flag to helpers that don't take the env
+    ## argument and aren't HVX instructions.  Since HVX instructions take
+    ## pointers to their arguments, they will have side effects.
+    if hex_common.need_env(tag) or hex_common.is_hvx_insn(tag):
+        f.write(f"DEF_HELPER_{len(declared) - 1}({tag}, {arguments})\n")
+    else:
+        f.write(f"DEF_HELPER_FLAGS_{len(declared) - 1}({tag}, "
+                f"TCG_CALL_NO_RWG_SE, {arguments})\n")
 
 
 def main():
target/hexagon/hex_common.py +18 -5
index b96f67972d..d3d8560fcf 100755
@@ -206,6 +206,18 @@ def need_sp(tag):
     return "A_IMPLICIT_READS_SP" in attribdict[tag]
 
 
+def is_hvx_insn(tag):
+    return "A_CVI" in attribdict[tag]
+
+
+def need_env(tag):
+    return ("A_STORE" in attribdict[tag] or
+            "A_LOAD" in attribdict[tag] or
+            "A_CVI_GATHER" in attribdict[tag] or
+            "A_CVI_SCATTER" in attribdict[tag] or
+            "A_IMPLICIT_WRITES_USR" in attribdict[tag])
+
+
 def need_slot(tag):
     if (
         "A_CVI_SCATTER" not in attribdict[tag]
@@ -1069,11 +1081,12 @@ def helper_args(tag, regs, imms):
     args = []
 
     ## First argument is the CPU state
-    args.append(HelperArg(
-        "env",
-        "tcg_env",
-        "CPUHexagonState *env"
-    ))
+    if need_env(tag):
+        args.append(HelperArg(
+            "env",
+            "tcg_env",
+            "CPUHexagonState *env"
+        ))
 
     ## For predicated instructions, we pass in the destination register
     if is_predicated(tag):
Signature · 1 line
2.34.1