From e41b28ef78ca5f1f7e8ee08f281c6f5adb638c5c Mon Sep 17 00:00:00 2001 From: SChernykh <15806605+SChernykh@users.noreply.github.com> Date: Thu, 15 Jan 2026 12:48:55 +0100 Subject: [PATCH] Improved RISC-V code --- .../randomx/jit_compiler_rv64_vector.cpp | 48 ------------------- .../randomx/jit_compiler_rv64_vector_static.S | 45 ++++++++--------- .../randomx/jit_compiler_rv64_vector_static.h | 1 - 3 files changed, 20 insertions(+), 74 deletions(-) diff --git a/src/crypto/randomx/jit_compiler_rv64_vector.cpp b/src/crypto/randomx/jit_compiler_rv64_vector.cpp index 536d9a9df..5725779f8 100644 --- a/src/crypto/randomx/jit_compiler_rv64_vector.cpp +++ b/src/crypto/randomx/jit_compiler_rv64_vector.cpp @@ -360,44 +360,6 @@ void* generateProgramVectorRV64(uint8_t* buf, Program& prog, ProgramConfiguratio uint8_t* last_modified[RegistersCount] = { p, p, p, p, p, p, p, p }; - uint8_t readReg01[RegistersCount] = {}; - - readReg01[pcfg.readReg0] = 1; - readReg01[pcfg.readReg1] = 1; - - uint32_t scratchpad_prefetch_pos = 0; - - for (int32_t i = static_cast(prog.getSize()) - 1; i >= 0; --i) { - Instruction instr = prog(i); - - const InstructionType inst_type = static_cast(inst_map[instr.opcode]); - - if (inst_type == InstructionType::CBRANCH) { - scratchpad_prefetch_pos = i; - break; - } - - if (inst_type < InstructionType::FSWAP_R) { - const uint32_t src = instr.src % RegistersCount; - const uint32_t dst = instr.dst % RegistersCount; - - if ((inst_type == InstructionType::ISWAP_R) && (src != dst) && (readReg01[src] || readReg01[dst])) { - scratchpad_prefetch_pos = i; - break; - } - - if ((inst_type == InstructionType::IMUL_RCP) && readReg01[dst] && !isZeroOrPowerOf2(instr.getImm32())) { - scratchpad_prefetch_pos = i; - break; - } - - if (readReg01[dst]) { - scratchpad_prefetch_pos = i; - break; - } - } - } - for (uint32_t i = 0, n = prog.getSize(); i < n; ++i) { Instruction instr = prog(i); @@ -854,16 +816,6 @@ void* generateProgramVectorRV64(uint8_t* buf, Program& prog, ProgramConfiguratio default: UNREACHABLE; } - - // Prefetch scratchpad lines for the next main loop iteration - // scratchpad_prefetch_pos is a conservative estimate of the earliest place in the code where we can do it - if (i == scratchpad_prefetch_pos) { - uint8_t* e = (uint8_t*)(buf + DIST(randomx_riscv64_vector_code_begin, randomx_riscv64_vector_program_scratchpad_prefetch_end)); - const size_t n = e - ((uint8_t*)spaddr_xor2); - - memcpy(p, spaddr_xor2, n); - p += n; - } } const uint8_t* e; diff --git a/src/crypto/randomx/jit_compiler_rv64_vector_static.S b/src/crypto/randomx/jit_compiler_rv64_vector_static.S index 26409724e..f8c0a7883 100644 --- a/src/crypto/randomx/jit_compiler_rv64_vector_static.S +++ b/src/crypto/randomx/jit_compiler_rv64_vector_static.S @@ -75,7 +75,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. .global DECL(randomx_riscv64_vector_program_main_loop_instructions_end_light_mode) .global DECL(randomx_riscv64_vector_program_main_loop_mx_xor_light_mode) .global DECL(randomx_riscv64_vector_program_scratchpad_prefetch) -.global DECL(randomx_riscv64_vector_program_scratchpad_prefetch_end) .global DECL(randomx_riscv64_vector_program_end) @@ -614,8 +613,7 @@ DECL(randomx_riscv64_vector_program_main_loop_mx_xor): and x5, x5, x19 // x5 = (readReg2 ^ readReg3) & dataset mask xor x14, x14, x5 // mx ^= (readReg2 ^ readReg3) & dataset mask - and x5, x14, x19 // x5 = mx & dataset mask - add x5, x5, x11 // x5 = &dataset[mx & dataset mask] + add x5, x14, x11 // x5 = &dataset[mx & dataset mask] #ifdef __riscv_zicbop prefetch.r (x5) @@ -643,7 +641,24 @@ DECL(randomx_riscv64_vector_program_main_loop_mx_xor): ld x6, 56(x5) xor x27, x27, x6 -randomx_riscv64_vector_program_main_loop_swap_mx_ma: +DECL(randomx_riscv64_vector_program_scratchpad_prefetch): + xor x5, x20, x22 // spAddr0-spAddr1 = readReg0 ^ readReg1 (JIT compiler will substitute the actual registers) + srli x6, x5, 32 // x6 = spAddr1 + + and x5, x5, x9 // x5 = spAddr0 & 64-byte aligned L3 mask + and x6, x6, x9 // x6 = spAddr1 & 64-byte aligned L3 mask + + c.add x5, x12 // x5 = &scratchpad[spAddr0 & 64-byte aligned L3 mask] + c.add x6, x12 // x6 = &scratchpad[spAddr1 & 64-byte aligned L3 mask] + +#ifdef __riscv_zicbop + prefetch.r (x5) + prefetch.r (x6) +#else + ld x5, (x5) + ld x6, (x6) +#endif + // swap mx <-> ma #ifdef __riscv_zbb rori x14, x14, 32 @@ -847,27 +862,7 @@ DECL(randomx_riscv64_vector_program_main_loop_mx_xor_light_mode): addi sp, sp, 192 - j randomx_riscv64_vector_program_main_loop_swap_mx_ma - -DECL(randomx_riscv64_vector_program_scratchpad_prefetch): - xor x5, x20, x22 // spAddr0-spAddr1 = readReg0 ^ readReg1 (JIT compiler will substitute the actual registers) - srli x6, x5, 32 // x6 = spAddr1 - - and x5, x5, x9 // x5 = spAddr0 & 64-byte aligned L3 mask - and x6, x6, x9 // x6 = spAddr1 & 64-byte aligned L3 mask - - c.add x5, x12 // x5 = &scratchpad[spAddr0 & 64-byte aligned L3 mask] - c.add x6, x12 // x6 = &scratchpad[spAddr1 & 64-byte aligned L3 mask] - -#ifdef __riscv_zicbop - prefetch.r (x5) - prefetch.r (x6) -#else - ld x5, (x5) - ld x6, (x6) -#endif - -DECL(randomx_riscv64_vector_program_scratchpad_prefetch_end): + j randomx_riscv64_vector_program_scratchpad_prefetch DECL(randomx_riscv64_vector_program_end): diff --git a/src/crypto/randomx/jit_compiler_rv64_vector_static.h b/src/crypto/randomx/jit_compiler_rv64_vector_static.h index a616f27f9..0624cc83a 100644 --- a/src/crypto/randomx/jit_compiler_rv64_vector_static.h +++ b/src/crypto/randomx/jit_compiler_rv64_vector_static.h @@ -66,7 +66,6 @@ void randomx_riscv64_vector_program_main_loop_instructions_end_light_mode(); void randomx_riscv64_vector_program_main_loop_mx_xor_light_mode(); void randomx_riscv64_vector_program_end(); void randomx_riscv64_vector_program_scratchpad_prefetch(); -void randomx_riscv64_vector_program_scratchpad_prefetch_end(); void randomx_riscv64_vector_code_end();