Fix usage of GETPC(), variable shadowing
cover Fix usage of GETPC(), variable shadowing
Matheus' patch has previously been reviewed, but I based my -Wshadow patch on his. So I'm submitting the series for review.
Brian Cain (1):
target/hexagon: fix some occurrences of -Wshadow=local
Matheus Tavares Bernardino (1):
target/hexagon: move GETPC() calls to top level helpers
target/hexagon/imported/alu.idef | 6 +-- target/hexagon/macros.h | 19 ++++---- target/hexagon/mmvec/macros.h | 2 +- target/hexagon/op_helper.c | 84 ++++++++++++-------------------- target/hexagon/op_helper.h | 9 ---- target/hexagon/translate.c | 10 ++-- 6 files changed, 50 insertions(+), 80 deletions(-)
Signature · 1 line
2.25.1
PATCH 1/2 target/hexagon: move GETPC() calls to top level helpers
From: Matheus Tavares Bernardino <[email protected]>
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 a few 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.
- HELPER(probe_hvx_stores), when called from another helper, ends up using its own GETPC() expansion instead of the top level caller.
Message-Id: <2c74c3696946edba7cc5b2942cf296a5af532052.1689070412.git.quic_mathbern@quicinc.com>-ne
--- target/hexagon/macros.h | 19 +++++----- target/hexagon/op_helper.c | 75 +++++++++++++++----------------------- target/hexagon/op_helper.h | 9 ----- 3 files changed, 38 insertions(+), 65 deletions(-)
target/hexagon/macros.h +9 -10
@@ -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) + do { \ + check_noshuf(env, pkt_has_store_s1, slot, EA, SIZE, GETPC()); \ + DST = (size##SIZE##SIGN##_t)MEM_LOAD##SIZE(env, EA, GETPC()); \ + } while (0)#endif #define fMEMOP (NUM ,SIZE ,SIGN ,EA ,FNTYPE ,VALUE )
target/hexagon/op_helper.c +29 -46
139 lines not on this page · raw message
target/hexagon/op_helper.h +0 -9
@@ -19,15 +19,6 @@#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 log_store64 (CPUHexagonState *env,target_ulong addr,int64_t val,int width,int slot); void log_store32(CPUHexagonState *env, target_ulong addr,
Signature · 1 line
2.25.1
Re: [PATCH 1/2] target/hexagon: move GETPC() calls to top level helpers
Quoted 24 lines · Brian Cain
From: Matheus Tavares Bernardino <[email protected]>
As
docs/devel/loads-stores.rststates:``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 a few snippets where that is not currently the case:-
probe_store(), which is only called from two helpers, so it's easy to moveGETPC()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.
Quoted 6 lines
-
HELPER(probe_hvx_stores), when called from another helper, ends up using its ownGETPC()expansion instead of the top level caller.Signed-off-by Matheus Tavares Bernardino <[email protected]>Reviewed-by Taylor Simpson <[email protected]>Message-Id: <2c74c3696946edba7cc5b2942cf296a5af532052.1689070412.git.quic_mathbern@quicinc.com>-ne
Odd '-ne' trailing.
Quoted 6 lines
Reviewed-by Brian Cain <[email protected]>--- target/hexagon/macros.h | 19 +++++----- target/hexagon/op_helper.c | 75 +++++++++++++++----------------------- target/hexagon/op_helper.h | 9 ----- 3 files changed, 38 insertions(+), 65 deletions(-)
PATCH 2/2 target/hexagon: fix some occurrences of -Wshadow=local
In reply to Brian CainOf the changes in this commit, the changes in HELPER(commit_hvx_stores)() are less obvious. They are required because of some macro invocations like SCATTER_OP_WRITE_TO_MEM().
e.g.:
In file included from ../target/hexagon/op_helper.c:31:
../target/hexagon/mmvec/macros.h:205:18: error: declaration of ‘i’ shadows a previous local [-Werror=shadow=compatible-local]
205 | for (int i = 0; i < sizeof(MMVector); i += sizeof(TYPE)) { \
| ^
../target/hexagon/op_helper.c:157:17: note: in expansion of macro ‘SCATTER_OP_WRITE_TO_MEM’
157 | SCATTER_OP_WRITE_TO_MEM(uint16_t);
| ^~~~~~~~~~~~~~~~~~~~~~~
../target/hexagon/op_helper.c:135:9: note: shadowed declaration is here
135 | int i;
| ^
In file included from ../target/hexagon/op_helper.c:31:
../target/hexagon/mmvec/macros.h:204:19: error: declaration of ‘ra’ shadows a previous local [-Werror=shadow=compatible-local]
204 | uintptr_t ra = GETPC(); \
| ^~
../target/hexagon/op_helper.c:160:17: note: in expansion of macro ‘SCATTER_OP_WRITE_TO_MEM’
160 | SCATTER_OP_WRITE_TO_MEM(uint32_t);
| ^~~~~~~~~~~~~~~~~~~~~~~
../target/hexagon/op_helper.c:134:15: note: shadowed declaration is here
134 | uintptr_t ra = GETPC();
| ^~
--- target/hexagon/imported/alu.idef | 6 +++--- target/hexagon/mmvec/macros.h | 2 +- target/hexagon/op_helper.c | 9 +++------ target/hexagon/translate.c | 10 +++++----- 4 files changed, 12 insertions(+), 15 deletions(-)
target/hexagon/imported/alu.idef +3 -3
@@ -1142,9 +1142,9 @@ Q6INSN(A4_cround_rr,"Rd32=cround(Rs32,Rt32)",ATTRIBS(),"Convergent Round", {RdV tmp128 = fSHIFTR128(tmp128, SHIFT);\ DST = fCAST16S_8S(tmp128);\ } else {\ - size16s_t rndbit_128 = fCAST8S_16S((1LL << (SHIFT - 1))); \ - size16s_t src_128 = fCAST8S_16S(SRC); \ - size16s_t tmp128 = fADD128(src_128, rndbit_128);\ + rndbit_128 = fCAST8S_16S((1LL << (SHIFT - 1))); \ + src_128 = fCAST8S_16S(SRC); \ + tmp128 = fADD128(src_128, rndbit_128);\ tmp128 = fSHIFTR128(tmp128, SHIFT);\ DST = fCAST16S_8S(tmp128);\ }
target/hexagon/mmvec/macros.h +1 -1
@@ -201,7 +201,7 @@ } while (0)#define SCATTER_OP_WRITE_TO_MEM (TYPE ) \ do { \ - uintptr_t ra = GETPC(); \ + ra = GETPC(); \ for (int i = 0; i < sizeof(MMVector); i += sizeof(TYPE)) { \ if (test_bit(i, env->vtcm_log.mask)) { \ TYPE dst = 0; \
target/hexagon/op_helper.c +3 -6
@@ -132,10 +132,9 @@ void HELPER(gather_store)(CPUHexagonState *env, uint32_t addr, int slot)void HELPER (commit_hvx_stores )(CPUHexagonState *env) {uintptr_t ra =GETPC (); -int i;/* Normal (possibly masked) vector store */ -for (i =0 ; i <VSTORES_MAX ; i++) { +for (int i =0 ; i <VSTORES_MAX ; i++) {if (env->vstore_pending[i]) { env->vstore_pending[i] =0 ;target_ulong va = env->vstore[i].va;
@@ -162,7 +161,7 @@ void HELPER(commit_hvx_stores)(CPUHexagonState *env)g_assert_not_reached (); } }else { -for (i =0 ; i <sizeof (MMVector); i++) { +for (int i =0 ; i <sizeof (MMVector); i++) {if (test_bit (i, env->vtcm_log.mask)) {cpu_stb_data_ra (env, env->vtcm_log.va[i], env->vtcm_log.data.ub[i], ra);
@@ -505,10 +504,8 @@ void HELPER(probe_pkt_scalar_store_s0)(CPUHexagonState *env, int args)static void probe_hvx_stores (CPUHexagonState *env,int mmu_idx,uintptr_t retaddr) { -int i; -/* Normal (possibly masked) vector store */ -for (i =0 ; i <VSTORES_MAX ; i++) { +for (int i =0 ; i <VSTORES_MAX ; i++) {if (env->vstore_pending[i]) {target_ulong va = env->vstore[i].va;int size = env->vstore[i].size;
target/hexagon/translate.c +5 -5
@@ -553,7 +553,7 @@ static void gen_start_packet(DisasContext *ctx)/* Preload the predicated registers into get_result_gpr(ctx, i) */ if (ctx->need_commit && !bitmap_empty (ctx->predicated_regs,TOTAL_PER_THREAD_REGS )) { -int i =find_first_bit (ctx->predicated_regs,TOTAL_PER_THREAD_REGS ); + i =find_first_bit (ctx->predicated_regs,TOTAL_PER_THREAD_REGS );while (i <TOTAL_PER_THREAD_REGS ) {tcg_gen_mov_tl (get_result_gpr (ctx, i), hex_gpr[i]); i = find_next_bit(ctx->predicated_regs, TOTAL_PER_THREAD_REGS,
@@ -566,7 +566,7 @@ static void gen_start_packet(DisasContext *ctx)* Only endloop instructions conditionally write to pred registers */ if (ctx->need_commit && pkt->pkt_has_endloop) { -for (int i =0 ; i < ctx->preg_log_idx; i++) { +for (i =0 ; i < ctx->preg_log_idx; i++) {int pred_num = ctx->preg_log[i]; ctx->new_pred_value[pred_num] =tcg_temp_new ();tcg_gen_mov_tl (ctx->new_pred_value[pred_num], hex_pred[pred_num]);
@@ -575,7 +575,7 @@ static void gen_start_packet(DisasContext *ctx)/* Preload the predicated HVX registers into future_VRegs and tmp_VRegs */ if (!bitmap_empty (ctx->predicated_future_vregs,NUM_VREGS )) { -int i =find_first_bit (ctx->predicated_future_vregs,NUM_VREGS ); + i =find_first_bit (ctx->predicated_future_vregs,NUM_VREGS );while (i <NUM_VREGS ) {const intptr_t VdV_off =ctx_future_vreg_off (ctx, i,1 , true);
@@ -588,7 +588,7 @@ static void gen_start_packet(DisasContext *ctx) } }if (!bitmap_empty (ctx->predicated_tmp_vregs,NUM_VREGS )) { -int i =find_first_bit (ctx->predicated_tmp_vregs,NUM_VREGS ); + i =find_first_bit (ctx->predicated_tmp_vregs,NUM_VREGS );while (i <NUM_VREGS ) {const intptr_t VdV_off =ctx_tmp_vreg_off (ctx, i,1 , true);
@@ -1228,7 +1228,7 @@ void hexagon_translate_init(void) offsetof(CPUHexagonState, mem_log_stores[i].data64), store_val64_names[i]); } -for (int i =0 ; i <VSTORES_MAX ; i++) { +for (i =0 ; i <VSTORES_MAX ; i++) {snprintf (vstore_addr_names[i],NAME_LEN ,"vstore_addr_%d" , i); hex_vstore_addr[i] =tcg_global_mem_new (cpu_env, offsetof(CPUHexagonState, vstore[i].va),
Signature · 1 line
2.25.1
Re: [PATCH 2/2] target/hexagon: fix some occurrences of -Wshadow=local
Hi Matheus,
Quoted 41 lines · Brian Cain
Of the changes in this commit, the changes in
HELPER(commit_hvx_stores)()are less obvious. They are required because of some macro invocations likeSCATTER_OP_WRITE_TO_MEM().e.g.:
In file included from
../target/hexagon/op_helper.c:31:
../target/hexagon/mmvec/macros.h:205:18: error: declaration of ‘i’ shadows a previous local [-Werror=shadow=compatible-local]
205 | for (int i = 0; i < sizeof(MMVector); i += sizeof(TYPE)) { \
| ^
../target/hexagon/op_helper.c:157:17: note: in expansion of macro ‘SCATTER_OP_WRITE_TO_MEM’
157 |SCATTER_OP_WRITE_TO_MEM(uint16_t);
| ^~~~~~~~~~~~~~~~~~~~~~~
../target/hexagon/op_helper.c:135:9: note: shadowed declaration is here
135 | int i;
| ^
In file included from../target/hexagon/op_helper.c:31:
../target/hexagon/mmvec/macros.h:204:19: error: declaration of ‘ra’ shadows a previous local [-Werror=shadow=compatible-local]
204 |uintptr_tra =GETPC(); \
| ^~
../target/hexagon/op_helper.c:160:17: note: in expansion of macro ‘SCATTER_OP_WRITE_TO_MEM’
160 |SCATTER_OP_WRITE_TO_MEM(uint32_t);
| ^~~~~~~~~~~~~~~~~~~~~~~
../target/hexagon/op_helper.c:134:15: note: shadowed declaration is here
134 |uintptr_tra =GETPC();
| ^~Reviewed-by Matheus Tavares Bernardino <[email protected]>Signed-off-by Brian Cain <[email protected]>--- target/hexagon/imported/alu.idef | 6 +++--- target/hexagon/mmvec/macros.h | 2 +- target/hexagon/op_helper.c | 9 +++------ target/hexagon/translate.c | 10 +++++----- 4 files changed, 12 insertions(+), 15 deletions(-)target/hexagon/imported/alu.idef +0
-0
target/hexagon/imported/alu.idef Q6INSN(A4_cround_rr,"Rd32=cround(Rs32,Rt32)",ATTRIBS(),"Convergent Round", {RdV · line 1147
Quoted 9 lines
target/hexagon/imported/alu.idef +3
-3@@ -1142,9 +1142,9 @@ Q6INSN(A4_cround_rr,"Rd32=cround(Rs32,Rt32)",ATTRIBS(),"Convergent Round", {RdV tmp128 = fSHIFTR128(tmp128, SHIFT);\ DST = fCAST16S_8S(tmp128);\ } else {\ - size16s_t rndbit_128 = fCAST8S_16S((1LL << (SHIFT - 1))); \ - size16s_t src_128 = fCAST8S_16S(SRC); \ - size16s_t tmp128 = fADD128(src_128, rndbit_128);\ + rndbit_128 = fCAST8S_16S((1LL << (SHIFT - 1))); \ + src_128 = fCAST8S_16S(SRC); \ + tmp128 = fADD128(src_128, rndbit_128);\
Correct.
target/hexagon/mmvec/macros.h line 204
Quoted 13 lines
target/hexagon/imported/alu.idef +0
-0tmp128 = fSHIFTR128(tmp128, SHIFT);\ DST = fCAST16S_8S(tmp128);\ }target/hexagon/mmvec/macros.h +1
-1@@ -201,7 +201,7 @@ } while (0)#define SCATTER_OP_WRITE_TO_MEM (TYPE ) \ do { \ - uintptr_t ra = GETPC(); \ + ra = GETPC(); \
Hmm I'm not a big fan of having "public" macros (exposed in header) which depend on local variable name. I'd rather rename the local 'ra' variable.
That said, this macro is used twice, for 16/32bits, iterating on 8bits. You could probably save some cycles using cpu_lduw_le_data_ra() / cpu_ldl_be_data_ra(). If so, can be done later.
Quoted 20 lines
target/hexagon/mmvec/macros.h +0
-0for (int i =0 ; i <sizeof (MMVector); i +=sizeof (TYPE )) { \if (test_bit (i, env->vtcm_log.mask)) { \TYPE dst =0 ; \target/hexagon/op_helper.c +1
-2@@ -132,10 +132,9 @@ void HELPER(gather_store)(CPUHexagonState *env, uint32_t addr, int slot)void HELPER (commit_hvx_stores )(CPUHexagonState *env) {uintptr_t ra =GETPC (); -int i;/* Normal (possibly masked) vector store */ -for (i =0 ; i <VSTORES_MAX ; i++) { +for (int i =0 ; i <VSTORES_MAX ; i++) {if (env->vstore_pending[i]) { env->vstore_pending[i] =0 ;target_ulong va = env->vstore[i].va;
target/hexagon/op_helper.c void HELPER(commit_hvx_stores)(CPUHexagonState *env) · line 167
Quoted 8 lines
target/hexagon/op_helper.c +1
-1@@ -162,7 +161,7 @@ void HELPER(commit_hvx_stores)(CPUHexagonState *env)g_assert_not_reached (); } }else { -for (i =0 ; i <sizeof (MMVector); i++) { +for (int i =0 ; i <sizeof (MMVector); i++) {if (test_bit (i, env->vtcm_log.mask)) {cpu_stb_data_ra (env, env->vtcm_log.va[i], env->vtcm_log.data.ub[i], ra);
Correct.
target/hexagon/op_helper.c void HELPER(probe_pkt_scalar_store_s0)(CPUHexagonState *env, int args) · line 508
Quoted 9 lines
target/hexagon/op_helper.c +1
-3@@ -505,10 +504,8 @@ void HELPER(probe_pkt_scalar_store_s0)(CPUHexagonState *env, int args)static void probe_hvx_stores (CPUHexagonState *env,int mmu_idx,uintptr_t retaddr) { -int i; -/* Normal (possibly masked) vector store */ -for (i =0 ; i <VSTORES_MAX ; i++) { +for (int i =0 ; i <VSTORES_MAX ; i++) {
Correct.
target/hexagon/translate.c static void gen_start_packet(DisasContext *ctx) · line 556
Quoted 12 lines
target/hexagon/op_helper.c +0
-0if (env->vstore_pending[i]) {target_ulong va = env->vstore[i].va;int size = env->vstore[i].size;target/hexagon/translate.c +0
-1@@ -553,7 +553,7 @@ static void gen_start_packet(DisasContext *ctx)/* Preload the predicated registers into get_result_gpr(ctx, i) */ if (ctx->need_commit && !bitmap_empty (ctx->predicated_regs,TOTAL_PER_THREAD_REGS )) { -int i =find_first_bit (ctx->predicated_regs,TOTAL_PER_THREAD_REGS );
Hmmm, matter of taste, in big functions where a very generic variable is reused, reducing the scope seems safer (like you did int commit_hvx_stores).
target/hexagon/translate.c static void gen_start_packet(DisasContext *ctx) · line 572
Quoted 13 lines
target/hexagon/translate.c +2
-1+ i =find_first_bit (ctx->predicated_regs,TOTAL_PER_THREAD_REGS );while (i <TOTAL_PER_THREAD_REGS ) {tcg_gen_mov_tl (get_result_gpr (ctx, i), hex_gpr[i]); i = find_next_bit(ctx->predicated_regs, TOTAL_PER_THREAD_REGS,@@ -566,7 +566,7 @@ static void gen_start_packet(DisasContext *ctx)* Only endloop instructions conditionally write to pred registers */ if (ctx->need_commit && pkt->pkt_has_endloop) { -for (int i =0 ; i < ctx->preg_log_idx; i++) { +for (i =0 ; i < ctx->preg_log_idx; i++) {int pred_num = ctx->preg_log[i]; ctx->new_pred_value[pred_num] =tcg_temp_new ();tcg_gen_mov_tl (ctx->new_pred_value[pred_num], hex_pred[pred_num]);
[...]
Preferably reworking SCATTER_OP_WRITE_TO_MEM:
Regards,
Phil.
Quoted 9 lines · Brian Cain
Matheus' patch has previously been reviewed, but I based my -Wshadow patch on his. So I'm submitting the series for review.
Brian Cain (1):
target/hexagon: fix some occurrences of -Wshadow=localMatheus Tavares Bernardino (1):
target/hexagon: moveGETPC()calls to top level helpers
Quoted 11 lines
target/hexagon/imported/alu.idef | 6 +-- target/hexagon/macros.h | 19 ++++---- target/hexagon/mmvec/macros.h | 2 +- target/hexagon/op_helper.c | 84 ++++++++++++-------------------- target/hexagon/op_helper.h | 9 ---- target/hexagon/translate.c | 10 ++-- 6 files changed, 50 insertions(+), 80 deletions(-)Signature · 1 line
2.25.1
Both patches:
Quoted 2 lines · Brian Cain
Matheus' patch has previously been reviewed, but I based my -Wshadow patch on his. So I'm submitting the series for review.
I'm not queueing these, because it's more than just shadowing. I trust your PR will arrive in due time :)
Thanks!