Skip to contentSkip to conversation

Hexagon: move GETPC() calls to top level helpers

PATCH Superseded Matheus Tavares Bernardino 3 participants 5 messages Jul 5, 2023 – Jul 11, 2023

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Hexagon: move GETPC() calls to top level helpers

As docs/devel/loads-stores.rst states:

``GETPC()`` should be used with great care: calling it in other functions that are *not* the top level ``HELPER(foo)`` will cause unexpected behavior. Instead, the value of ``GETPC()`` should be read from the helper and passed if needed to the functions that the helper calls.

Let's fix the GETPC() usage in Hexagon, making sure it's always called from top level helpers and passed down to the places where it's needed. There are two snippets where that is not currently the case:

- probe_store(), which is only called from two helpers, so it's easy to move GETPC() up.

- mem_load*() functions, which are also called directly from helpers, but through the MEM_LOAD*() set of macros. Note that this are only used when compiling with --disable-hexagon-idef-parser.

In this case, we also take this opportunity to simplify the code, unifying the mem_load*() functions.

---
v1: d40fabcf9d6e92e4cd8d6a144e9b2a9acf4580dc.1688420966.git.quic_mathbern@quicinc.com

Changes in v2:
- Fixed wrong cpu_ld* unification from previous version.
- Passed retaddr down to check_noshuf() and further, as Taylor suggested.
- Reorganized macros for simplification.

 target/hexagon/macros.h    | 19 ++++++------
 target/hexagon/op_helper.h | 11 ++-----
 target/hexagon/op_helper.c | 62 +++++++++++---------------------------
 3 files changed, 29 insertions(+), 63 deletions(-)
target/hexagon/macros.h +9 -10
index 5451b061ee..e44a932434 100644
@@ -173,15 +173,6 @@
 #define MEM_STORE8(VA, DATA, SLOT) \
     MEM_STORE8_FUNC(DATA)(cpu_env, VA, DATA, SLOT)
 #else
-#define MEM_LOAD1s(VA) ((int8_t)mem_load1(env, pkt_has_store_s1, slot, VA))
-#define MEM_LOAD1u(VA) ((uint8_t)mem_load1(env, pkt_has_store_s1, slot, VA))
-#define MEM_LOAD2s(VA) ((int16_t)mem_load2(env, pkt_has_store_s1, slot, VA))
-#define MEM_LOAD2u(VA) ((uint16_t)mem_load2(env, pkt_has_store_s1, slot, VA))
-#define MEM_LOAD4s(VA) ((int32_t)mem_load4(env, pkt_has_store_s1, slot, VA))
-#define MEM_LOAD4u(VA) ((uint32_t)mem_load4(env, pkt_has_store_s1, slot, VA))
-#define MEM_LOAD8s(VA) ((int64_t)mem_load8(env, pkt_has_store_s1, slot, VA))
-#define MEM_LOAD8u(VA) ((uint64_t)mem_load8(env, pkt_has_store_s1, slot, VA))
-
 #define MEM_STORE1(VA, DATA, SLOT) log_store32(env, VA, DATA, 1, SLOT)
 #define MEM_STORE2(VA, DATA, SLOT) log_store32(env, VA, DATA, 2, SLOT)
 #define MEM_STORE4(VA, DATA, SLOT) log_store32(env, VA, DATA, 4, SLOT)
@@ -530,8 +521,16 @@ static inline TCGv gen_read_ireg(TCGv result, TCGv val, int shift)
 #ifdef QEMU_GENERATE
 #define fLOAD(NUM, SIZE, SIGN, EA, DST) MEM_LOAD##SIZE##SIGN(DST, EA)
 #else
+#define MEM_LOAD1 cpu_ldub_data_ra
+#define MEM_LOAD2 cpu_lduw_data_ra
+#define MEM_LOAD4 cpu_ldl_data_ra
+#define MEM_LOAD8 cpu_ldq_data_ra
+
 #define fLOAD(NUM, SIZE, SIGN, EA, DST) \
-    DST = (size##SIZE##SIGN##_t)MEM_LOAD##SIZE##SIGN(EA)
+    DST =  (size##SIZE##SIGN##_t)({ \
+        check_noshuf(env, pkt_has_store_s1, slot, EA, SIZE, GETPC()); \
+        MEM_LOAD##SIZE(env, EA, GETPC()); \
+    })
 #endif
 
 #define fMEMOP(NUM, SIZE, SIGN, EA, FNTYPE, VALUE)
target/hexagon/op_helper.h +2 -9
index 8f3764d15e..7744e819ef 100644
@@ -19,15 +19,8 @@
 #define HEXAGON_OP_HELPER_H
 
 /* Misc functions */
-uint8_t mem_load1(CPUHexagonState *env, bool pkt_has_store_s1,
-                  uint32_t slot, target_ulong vaddr);
-uint16_t mem_load2(CPUHexagonState *env, bool pkt_has_store_s1,
-                   uint32_t slot, target_ulong vaddr);
-uint32_t mem_load4(CPUHexagonState *env, bool pkt_has_store_s1,
-                   uint32_t slot, target_ulong vaddr);
-uint64_t mem_load8(CPUHexagonState *env, bool pkt_has_store_s1,
-                   uint32_t slot, target_ulong vaddr);
-
+void check_noshuf(CPUHexagonState *env, bool pkt_has_store_s1,
+                  uint32_t slot, target_ulong vaddr, int size, uintptr_t ra);
 void log_store64(CPUHexagonState *env, target_ulong addr,
                  int64_t val, int width, int slot);
 void log_store32(CPUHexagonState *env, target_ulong addr,
target/hexagon/op_helper.c +18 -44

113 lines not on this page · raw message

Signature · 1 line
2.37.2
Reviewed-by on v2 1/1
#1 lore raw
Quoted 13 lines

-----Original Message-----
From: Matheus Tavares Bernardino <[email protected]>
Sent: Wednesday, July 5, 2023 12:35 PM
To: [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [PATCH v2] Hexagon: move GETPC() calls to top level helpers

As docs/devel/loads-stores.rst states:

Quoted 7 lines

``GETPC()`` should be used with great care: calling it in other functions that are *not* the top level ``HELPER(foo)`` will cause unexpected behavior. Instead, the value of ``GETPC()`` should be read from the helper and passed if needed to the functions that the helper calls.

Let's fix the GETPC() usage in Hexagon, making sure it's always called

from

Quoted 35 lines

top level helpers and passed down to the places where it's needed. There are two snippets where that is not currently the case:

- probe_store(), which is only called from two helpers, so it's easy to move GETPC() up.

- mem_load*() functions, which are also called directly from helpers, but through the MEM_LOAD*() set of macros. Note that this are only used when compiling with --disable-hexagon-idef-parser.

In this case, we also take this opportunity to simplify the code, unifying the mem_load*() functions.

---
v1:
d40fabcf9d6e92e4cd8d6a144e9b2a9acf4580dc.1688420966.git.quic_mathber [email protected]

Changes in v2:
- Fixed wrong cpu_ld* unification from previous version.
- Passed retaddr down to check_noshuf() and further, as Taylor suggested.
- Reorganized macros for simplification.

 target/hexagon/macros.h    | 19 ++++++------
 target/hexagon/op_helper.h | 11 ++-----  target/hexagon/op_helper.c | 62
+++++++++++---------------------------
 3 files changed, 29 insertions(+), 63 deletions(-)
target/hexagon/macros.h index renamed from target/hexagon/macros.h +0 -0

5451b061ee..e44a932434 100644

target/hexagon/macros.h line 178

Quoted 7 lines
target/hexagon/macros.h +4 -1
@@ -173,15 +173,6 @@
 #define fLOAD(NUM, SIZE, SIGN, EA, DST) \
-    DST = (size##SIZE##SIGN##_t)MEM_LOAD##SIZE##SIGN(EA)
+    DST =  (size##SIZE##SIGN##_t)({ \
+        check_noshuf(env, pkt_has_store_s1, slot, EA, SIZE, GETPC()); \
+        MEM_LOAD##SIZE(env, EA, GETPC()); \
+    })
 #endif

This should be formatted as

#define fLOAD(...) \
    do { \
        check_noshuf(...); \
        DST = ...; \
    } while (0)

target/hexagon/op_helper.h

Quoted 7 lines

a/target/hexagon/op_helper.h b/target/hexagon/op_helper.h index 8f3764d15e..7744e819ef 100644

target/hexagon/op_helper.h +3 -0
+void check_noshuf(CPUHexagonState *env, bool pkt_has_store_s1,
+                  uint32_t slot, target_ulong vaddr, int size,
+uintptr_t ra);

Are you sure this needs to be non-static?

Othersiwe

#2 lore raw
Quoted 65 lines · On 7/5/23 23:17, [email protected] wrote:
Quoted 13 lines

-----Original Message-----
From: Matheus Tavares Bernardino <[email protected]>
Sent: Wednesday, July 5, 2023 12:35 PM
To: [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [PATCH v2] Hexagon: move GETPC() calls to top level helpers

As docs/devel/loads-stores.rst states:

Quoted 7 lines

``GETPC()`` should be used with great care: calling it in other functions that are *not* the top level ``HELPER(foo)`` will cause unexpected behavior. Instead, the value of ``GETPC()`` should be read from the helper and passed if needed to the functions that the helper calls.

Let's fix the GETPC() usage in Hexagon, making sure it's always called

from

Quoted 35 lines

top level helpers and passed down to the places where it's needed. There are two snippets where that is not currently the case:

- probe_store(), which is only called from two helpers, so it's easy to move GETPC() up.

- mem_load*() functions, which are also called directly from helpers, but through the MEM_LOAD*() set of macros. Note that this are only used when compiling with --disable-hexagon-idef-parser.

In this case, we also take this opportunity to simplify the code, unifying the mem_load*() functions.

---
v1:
d40fabcf9d6e92e4cd8d6a144e9b2a9acf4580dc.1688420966.git.quic_mathber [email protected]

Changes in v2:
- Fixed wrong cpu_ld* unification from previous version.
- Passed retaddr down to check_noshuf() and further, as Taylor suggested.
- Reorganized macros for simplification.

  target/hexagon/macros.h    | 19 ++++++------
  target/hexagon/op_helper.h | 11 ++-----  target/hexagon/op_helper.c | 62
+++++++++++---------------------------
  3 files changed, 29 insertions(+), 63 deletions(-)
target/hexagon/macros.h index renamed from target/hexagon/macros.h +0 -0

5451b061ee..e44a932434 100644

target/hexagon/macros.h line 178

Quoted 7 lines
target/hexagon/macros.h +4 -1
@@ -173,15 +173,6 @@
  #define fLOAD(NUM, SIZE, SIGN, EA, DST) \
-    DST = (size##SIZE##SIGN##_t)MEM_LOAD##SIZE##SIGN(EA)
+    DST =  (size##SIZE##SIGN##_t)({ \
+        check_noshuf(env, pkt_has_store_s1, slot, EA, SIZE, GETPC()); \
+        MEM_LOAD##SIZE(env, EA, GETPC()); \
+    })
  #endif

This should be formatted as

Quoted 5 lines
#define fLOAD(...) \
     do { \
         check_noshuf(...); \
         DST = ...; \
     } while (0)

Alternately,

    DST = (size##SIZE##SIGN##_t)mem_load##SIZE(env, pkt_has_store_s1, slot, EA, GETPC())

and retain the mem_loadN functions with an extra argument.

Quoted 5 lines

target/hexagon/macros.h

Quoted 3 lines
target/hexagon/macros.h +3 -0
+void check_noshuf(CPUHexagonState *env, bool pkt_has_store_s1,
+                  uint32_t slot, target_ulong vaddr, int size,
+uintptr_t ra);

Are you sure this needs to be non-static?

I think only if the mem_loadN functions are retained may it be static. But perhaps the macro magic is actually limited to the same compilation unit?

r~

#3 lore raw
In reply to [email protected]
Quoted 26 lines
Quoted 14 lines · [email protected] wrote:

-----Original Message-----
From: Matheus Tavares Bernardino <[email protected]>
Sent: Wednesday, July 5, 2023 12:35 PM
To: [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [PATCH v2] Hexagon: move GETPC() calls to top level helpers

target/hexagon/macros.h index renamed from target/hexagon/macros.h +0 -0

5451b061ee..e44a932434 100644

target/hexagon/macros.h line 178

Quoted 7 lines
target/hexagon/macros.h +4 -1
@@ -173,15 +173,6 @@
 #define fLOAD(NUM, SIZE, SIGN, EA, DST) \
-    DST = (size##SIZE##SIGN##_t)MEM_LOAD##SIZE##SIGN(EA)
+    DST =  (size##SIZE##SIGN##_t)({ \
+        check_noshuf(env, pkt_has_store_s1, slot, EA, SIZE, GETPC()); \
+        MEM_LOAD##SIZE(env, EA, GETPC()); \
+    })
 #endif

This should be formatted as

Quoted 5 lines
#define fLOAD(...) \
    do { \
        check_noshuf(...); \
        DST = ...; \
    } while (0)

Ah, indeed, thanks!

Quoted 9 lines

target/hexagon/op_helper.h

Quoted 7 lines

a/target/hexagon/op_helper.h b/target/hexagon/op_helper.h index 8f3764d15e..7744e819ef 100644

target/hexagon/op_helper.h +3 -0
+void check_noshuf(CPUHexagonState *env, bool pkt_has_store_s1,
+                  uint32_t slot, target_ulong vaddr, int size,
+uintptr_t ra);

Are you sure this needs to be non-static?

Yeah, since we remove the mem_load*() functions, check_noshuf() must now be visible to the other compilation units that include macros.h, as we will expand the fLOAD() macro to call it.

#4 lore raw
Quoted 33 lines

-----Original Message-----
From: Matheus Tavares Bernardino <[email protected]>
Sent: Thursday, July 6, 2023 5:23 AM
To: [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v2] Hexagon: move GETPC() calls to top level helpers

Quoted 11 lines
Quoted 9 lines · [email protected] wrote:

-----Original Message-----
From: Matheus Tavares Bernardino <[email protected]>
Sent: Wednesday, July 5, 2023 12:35 PM
To: [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [PATCH v2] Hexagon: move GETPC() calls to top level helpers

Quoted 9 lines

target/hexagon/op_helper.h

Quoted 7 lines

a/target/hexagon/op_helper.h b/target/hexagon/op_helper.h index 8f3764d15e..7744e819ef 100644

target/hexagon/op_helper.h +3 -0
+void check_noshuf(CPUHexagonState *env, bool pkt_has_store_s1,
+                  uint32_t slot, target_ulong vaddr, int size,
+uintptr_t ra);

Are you sure this needs to be non-static?

Quoted 2 lines

Yeah, since we remove the mem_load*() functions, check_noshuf() must now be visible to the other compilation units that include macros.h, as we

will

Quoted 1 line

expand the fLOAD() macro to call it.

Since the generated helper functions are included at the bottom of op_helper.c

    #include "helper_funcs_generated.c.inc"

it can be static.

It needs to be guarded with

    #ifndef CONFIG_HEXAGON_IDEF_PARSER

because it is not used when by any of the idef-parser functions.

Thanks,
Taylor