Skip to contentSkip to conversation

Add helper_print functions for printing intermediate results of complex instructions in RISC-V target

RFC PATCH In review Chao Liu 1 participant 4 messages Nov 23, 2024 RISC-V

lore
Share & tools
lore

Chao Liu

author
#0 lore raw

cover Add helper_print functions for printing intermediate results of complex instructions in RISC-V target

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(+)
Signature · 1 line
2.40.1
author
#1 lore raw

RFC PATCH 1/1 riscv: Add helper_print() for printing intermediate results of IR

---
 target/riscv/helper.h    | 13 ++++++++++++
 target/riscv/op_helper.c | 46 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+)
target/riscv/helper.h +13 -0
index 451261ce5a..667da16988 100644
@@ -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)
target/riscv/op_helper.c +46 -0

49 lines not on this page · raw message

Signature · 1 line
2.40.1
author
#2 lore raw

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.

author
#3 lore raw

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