* [RFC PATCH v1 0/1] Add helper_print functions for printing intermediate results of complex instructions in RISC-V target
@ 2024-11-23 17:23 Chao Liu
2024-11-23 17:23 ` [RFC PATCH v1 1/1] riscv: Add helper_print() for printing intermediate results of IR Chao Liu
0 siblings, 1 reply; 4+ messages in thread
From: Chao Liu @ 2024-11-23 17:23 UTC (permalink / raw)
To: qemu-devel
Cc: bmeng.cn, liwei1518, peter.maydell, palmer, alistair.francis,
dbarboza, zhiwei_liu, Chao Liu
Hi all,
Recently, I have been working on functional simulation and verification of
an extended complex instruction set for an AI chip based on the RISC-V
architecture using QEMU.
Given the complexity of some instructions, I attempted to introduce a set of
helper functions that can print certain intermediate results of IR
corresponding to a single instruction, such as those from sparse matrix operations.
My implementation approach is as follows:
1. Encapsulate printf within helper functions (to avoid issues where the printf
address might exceed the ±2GB address space when called directly). Since helpers
do not support variadic arguments, I defined multiple helper_print functions with
a maximum of 7 parameters.
2. Designed a syntactic sugar using C macros and the concatenation operator ##
to encapsulate the aforementioned helper_print functions, which can support up
to 7 parameters.
However, I encountered some issues, such as having to set < #pragma GCC diagnostic
ignored "-Wformat-security"> for this piece of code, but I think there might be
some security risks involved.
Currently, these macros are temporarily added under target/risc-v, but I believe
they could be designed more generally to support more architectures.
During testing, I found that these interfaces work particularly well for debugging
simulations of complex instructions.
Therefore, I am sharing this patch with the mailing list in hopes that we can discuss
and come up with a friendlier and more universal solution. :)
Regards,
Chao
Chao Liu (1):
riscv: Add gen_helper_print() to debug IR
target/riscv/helper.h | 13 ++++++++++++
target/riscv/op_helper.c | 46 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 59 insertions(+)
--
2.40.1
^ permalink raw reply [flat|nested] 4+ messages in thread* [RFC PATCH v1 1/1] riscv: Add helper_print() for printing intermediate results of IR 2024-11-23 17:23 [RFC PATCH v1 0/1] Add helper_print functions for printing intermediate results of complex instructions in RISC-V target Chao Liu @ 2024-11-23 17:23 ` Chao Liu 2024-11-23 18:35 ` [RFC PATCH v1 0/1] Add helper_print functions for printing intermediate results of complex instructions in RISC-V target Chao Liu 0 siblings, 1 reply; 4+ messages in thread From: Chao Liu @ 2024-11-23 17:23 UTC (permalink / raw) To: qemu-devel Cc: bmeng.cn, liwei1518, peter.maydell, palmer, alistair.francis, dbarboza, zhiwei_liu, Chao Liu Signed-off-by: Chao Liu <chao.liu@yeah.net> --- target/riscv/helper.h | 13 ++++++++++++ target/riscv/op_helper.c | 46 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/target/riscv/helper.h b/target/riscv/helper.h index 451261ce5a..667da16988 100644 --- a/target/riscv/helper.h +++ b/target/riscv/helper.h @@ -1281,3 +1281,16 @@ DEF_HELPER_4(vgmul_vv, void, ptr, ptr, env, i32) DEF_HELPER_5(vsm4k_vi, void, ptr, ptr, i32, env, i32) DEF_HELPER_4(vsm4r_vv, void, ptr, ptr, env, i32) DEF_HELPER_4(vsm4r_vs, void, ptr, ptr, env, i32) + +DEF_HELPER_1(print1, void, ptr) +DEF_HELPER_2(print2, void, ptr, tl) +DEF_HELPER_3(print3, void, ptr, tl, tl) +DEF_HELPER_4(print4, void, ptr, tl, tl, tl) +DEF_HELPER_5(print5, void, ptr, tl, tl, tl, tl) +DEF_HELPER_6(print6, void, ptr, tl, tl, tl, tl, tl) +DEF_HELPER_7(print7, void, ptr, tl, tl, tl, tl, tl, tl) + +#define gen_helper_print(num, fmt, ...) do { \ + assert(num >= 1 && num <= 7); \ + gen_helper_print##num(tcg_constant_ptr((uintptr_t)fmt), __VA_ARGS__); \ +} while(0) diff --git a/target/riscv/op_helper.c b/target/riscv/op_helper.c index eddedacf4b..f45e1b9a50 100644 --- a/target/riscv/op_helper.c +++ b/target/riscv/op_helper.c @@ -564,3 +564,49 @@ target_ulong helper_hyp_hlvx_wu(CPURISCVState *env, target_ulong addr) } #endif /* !CONFIG_USER_ONLY */ + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wformat-security" + +void helper_print1(void *fmt) +{ + printf((const char *)fmt); +} + +void helper_print2(void *fmt, target_ulong arg2) +{ + printf((const char *)fmt, arg2); +} + +void helper_print3(void *fmt, target_ulong arg2, target_ulong arg3) +{ + printf((const char *)fmt, arg2, arg3); +} + +void helper_print4(void *fmt, target_ulong arg2, target_ulong arg3, + target_ulong arg4) +{ + printf((const char *)fmt, arg2, arg3, arg4); +} + +void helper_print5(void *fmt, target_ulong arg2, target_ulong arg3, + target_ulong arg4, target_ulong arg5) +{ + printf((const char *)fmt, arg2, arg3, arg4, arg5); +} + +void helper_print6(void *fmt, target_ulong arg2, target_ulong arg3, + target_ulong arg4, target_ulong arg5, target_ulong arg6) +{ + printf((const char *)fmt, arg2, arg3, arg4, arg5, arg6); +} + +void helper_print7(void *fmt, target_ulong arg2, target_ulong arg3, + target_ulong arg4, target_ulong arg5, target_ulong arg6, + target_ulong arg7) +{ + printf((const char *)fmt, arg2, arg3, arg4, arg5, arg6, arg7); +} + +#pragma GCC diagnostic pop + -- 2.40.1 ^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [RFC PATCH v1 0/1] Add helper_print functions for printing intermediate results of complex instructions in RISC-V target 2024-11-23 17:23 ` [RFC PATCH v1 1/1] riscv: Add helper_print() for printing intermediate results of IR Chao Liu @ 2024-11-23 18:35 ` Chao Liu 2024-11-23 19:08 ` Chao Liu 0 siblings, 1 reply; 4+ messages in thread From: Chao Liu @ 2024-11-23 18:35 UTC (permalink / raw) To: chao.liu Cc: alistair.francis, bmeng.cn, dbarboza, liwei1518, palmer, peter.maydell, qemu-devel, zhiwei_liu For example: static bool gen_lr(DisasContext *ctx, arg_atomic *a, MemOp mop) { TCGv src1; decode_save_opc(ctx, 0); src1 = get_address(ctx, a->rs1, 0); gen_helper_print("src1 %x\n", src1); ... return true; } When the TCG executes instructions containing this function, the terminal will print: src1 0x... However, currently the parameter type for `helper_print` is uniformly set to `tl`, so when passing parameters of other types, they need to be cast to the `TCGv` type. ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Re: [RFC PATCH v1 0/1] Add helper_print functions for printing intermediate results of complex instructions in RISC-V target 2024-11-23 18:35 ` [RFC PATCH v1 0/1] Add helper_print functions for printing intermediate results of complex instructions in RISC-V target Chao Liu @ 2024-11-23 19:08 ` Chao Liu 0 siblings, 0 replies; 4+ messages in thread From: Chao Liu @ 2024-11-23 19:08 UTC (permalink / raw) To: chao.liu Cc: alistair.francis, bmeng.cn, dbarboza, liwei1518, palmer, peter.maydell, qemu-devel, zhiwei_liu Currently, the v1-patch is not yet capable of achieving the desired effect. It still requires passing the number of variable arguments to the helper_print() function: static bool gen_lr(DisasContext *ctx, arg_atomic *a, MemOp mop) { TCGv src1; decode_save_opc(ctx, 0); src1 = get_address(ctx, a->rs1, 0); gen_helper_print(2, "src1 %x\n", src1); ... return true; } Regards, Chao ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2024-11-23 19:09 UTC | newest] Thread overview: 4+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2024-11-23 17:23 [RFC PATCH v1 0/1] Add helper_print functions for printing intermediate results of complex instructions in RISC-V target Chao Liu 2024-11-23 17:23 ` [RFC PATCH v1 1/1] riscv: Add helper_print() for printing intermediate results of IR Chao Liu 2024-11-23 18:35 ` [RFC PATCH v1 0/1] Add helper_print functions for printing intermediate results of complex instructions in RISC-V target Chao Liu 2024-11-23 19:08 ` Chao Liu
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.