How to Save and Restore `ymm` register in `mcontext` before and after build_bb_ilist in DR internal

60 views
Skip to first unread message

TOMYUE

unread,
Jan 31, 2024, 11:44:22 AMJan 31
to DynamoRIO Users
Hi all, sorry to bother🙏🏻, I have questions as below:
      I use `drrun -debug -loglevel 3 -- <avx512/avx2 binary>` as workload to run the dynamorio. Some basic blocks in this workload contain instructions that manipulate both ymm and zmm registers. I want to save only the `ymm` register value in `mcontext` before building the `instr` one by one in a basic block in the /core/arch/interp.c: build_bb_ilist() and restore all 15  `ymm` register back to `mcontext` when all `instr`s in this basic block have been build.  

Q1: I wonder if there exists any mechanisms or routines in DR internal to do this store and restore ymm register? Or any examples I can learn from?

Q2: When I debugging,  I'm confusing about how DynamoRIO handles a basic block that includes both zmm and ymm register instructions utilizing the `dr_zmm_t simd[MCXT_NUM_SIMD_SLOTS]` array in `mcontext` struct definition. Since the `mcontext` struct has only this array to store the values of vector registers, how does the `simd[MCXT_NUM_SIMD_SLOTS]` array in `mcontext` can be capable of simultaneously storing the values of both zmm and ymm registers when a basic block contains instructions operating on both types of registers?

I'm currently doing this store and restore in an ugly way, and I'm not sure if it's right (intuitively this has a high probability of being wrong.), by saving the `ymm` registers in the `mcontext` directly into a global array: `dr_zmm_t rewrite_reserved_ymm[MCXT_NUM_SIMD_SLOTS];` I added myself. Screenshot 2024-02-01 at 00.39.51.png

Abhinav Sharma

unread,
Feb 1, 2024, 5:00:48 PMFeb 1
to DynamoRIO Users
Hi,

> how does the `simd[MCXT_NUM_SIMD_SLOTS]` array in `mcontext` can be capable of simultaneously storing the values of both zmm and ymm registers when a basic block contains instructions operating on both types of registers

Note that the ymm registers are the lower half of the zmm registers (https://www.intel.com/content/www/us/en/developer/articles/technical/intel-avx-512-instructions.html). So it is sufficient for mcontext to store only the zmm registers.

> I want to save only the `ymm` register value in `mcontext` before building the `instr` one by one in a basic block in the /core/arch/interp.c: build_bb_ilist() and restore all 15  `ymm` register back to `mcontext` when all `instr`s in this basic block have been build.  

If I understand correctly, you're trying to modify internal DynamoRIO to also preserve the ymm regs?

Like I said above, ymm regs are a part of zmm regs. Our append_save_simd_reg (https://github.com/DynamoRIO/dynamorio/blob/60acc5871c4200a2f4cc10d416c5e8ef146db447/core/arch/x86/emit_utils.c#L1617) already preserves the zmm regs; correspondingly append_restore_simd_reg already restores them. So things should be working as intended already.

Did you see any error/crash that led you to investigate this? If yes, can you share more details? Would be happy to help.

Abhinav

TOMYUE

unread,
Feb 3, 2024, 8:00:54 PMFeb 3
to DynamoRIO Users
>Note that the ymm registers are the lower half of the zmm registers. So it is sufficient for mcontext to store only the zmm registers.
Thank you extremely much for reminding me that the ymm register is the lower 256bit of the zmm register on machines that support avx512, and for telling me about the save,restore simd register routines in emit_utils.c. 

>Did you see any error/crash that led you to investigate this? If yes, can you share more details? Would be happy to help.
I didn't see any error/crash when running true workloads. I'm trying to play around with rewriting instructions inside the DynamoRIO internal to help me better understand the internal mechanism of DynamoRIO, such as rewriting vector instructions as equivalent scalar instructions, or turning instructions that use zmm into instructions that use only ymm or xmm, so I'm thinking about how to ensure the consistency of the context before and after a bb when rewriting it, as a dynamic recompiler does. 
Reply all
Reply to author
Forward
0 new messages