Skip to content

Commit 027ecb8

Browse files
committed
JIT compiler for RISC-V
1 parent 1c603a2 commit 027ecb8

9 files changed

+2633
-2
lines changed

CMakeLists.txt

+36
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,42 @@ if(ARM_ID STREQUAL "aarch64" OR ARM_ID STREQUAL "arm64" OR ARM_ID STREQUAL "armv
173173
endif()
174174
endif()
175175

176+
# RISC-V
177+
if(ARCH_ID STREQUAL "riscv64")
178+
list(APPEND randomx_sources
179+
src/jit_compiler_rv64_static.S
180+
src/jit_compiler_rv64.cpp)
181+
# cheat because cmake and ccache hate each other
182+
set_property(SOURCE src/jit_compiler_rv64_static.S PROPERTY LANGUAGE C)
183+
set_property(SOURCE src/jit_compiler_rv64_static.S PROPERTY XCODE_EXPLICIT_FILE_TYPE sourcecode.asm)
184+
185+
# default build uses the RV64GC baseline
186+
set(RVARCH "rv64gc")
187+
188+
# for native builds, enable Zba and Zbb if supported by the CPU
189+
if(ARCH STREQUAL "native")
190+
enable_language(ASM)
191+
try_run(RANDOMX_ZBA_RUN_FAIL
192+
RANDOMX_ZBA_COMPILE_OK
193+
${CMAKE_CURRENT_BINARY_DIR}/
194+
${CMAKE_CURRENT_SOURCE_DIR}/src/tests/riscv64_zba.s
195+
COMPILE_DEFINITIONS "-march=rv64gc_zba")
196+
if (RANDOMX_ZBA_COMPILE_OK AND NOT RANDOMX_ZBA_RUN_FAIL)
197+
set(RVARCH "${RVARCH}_zba")
198+
endif()
199+
try_run(RANDOMX_ZBB_RUN_FAIL
200+
RANDOMX_ZBB_COMPILE_OK
201+
${CMAKE_CURRENT_BINARY_DIR}/
202+
${CMAKE_CURRENT_SOURCE_DIR}/src/tests/riscv64_zbb.s
203+
COMPILE_DEFINITIONS "-march=rv64gc_zbb")
204+
if (RANDOMX_ZBB_COMPILE_OK AND NOT RANDOMX_ZBB_RUN_FAIL)
205+
set(RVARCH "${RVARCH}_zbb")
206+
endif()
207+
endif()
208+
209+
add_flag("-march=${RVARCH}")
210+
endif()
211+
176212
set(RANDOMX_INCLUDE "${CMAKE_CURRENT_SOURCE_DIR}/src" CACHE STRING "RandomX Include path")
177213

178214
add_library(randomx ${randomx_sources})

src/common.hpp

+7
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,19 @@ namespace randomx {
116116

117117
#if defined(_M_X64) || defined(__x86_64__)
118118
#define RANDOMX_HAVE_COMPILER 1
119+
#define RANDOMX_COMPILER_X86
119120
class JitCompilerX86;
120121
using JitCompiler = JitCompilerX86;
121122
#elif defined(__aarch64__)
122123
#define RANDOMX_HAVE_COMPILER 1
124+
#define RANDOMX_COMPILER_A64
123125
class JitCompilerA64;
124126
using JitCompiler = JitCompilerA64;
127+
#elif defined(__riscv) && __riscv_xlen == 64
128+
#define RANDOMX_HAVE_COMPILER 1
129+
#define RANDOMX_COMPILER_RV64
130+
class JitCompilerRV64;
131+
using JitCompiler = JitCompilerRV64;
125132
#else
126133
#define RANDOMX_HAVE_COMPILER 0
127134
class JitCompilerFallback;

src/jit_compiler.hpp

+40-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,48 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828

2929
#pragma once
3030

31-
#if defined(_M_X64) || defined(__x86_64__)
31+
#include "common.hpp"
32+
33+
namespace randomx {
34+
35+
struct CodeBuffer {
36+
uint8_t* code;
37+
int32_t codePos;
38+
int32_t rcpCount;
39+
40+
void emit(const uint8_t* src, int32_t len) {
41+
memcpy(&code[codePos], src, len);
42+
codePos += len;
43+
}
44+
45+
template<typename T>
46+
void emit(T src) {
47+
memcpy(&code[codePos], &src, sizeof(src));
48+
codePos += sizeof(src);
49+
}
50+
51+
void emitAt(int32_t codePos, const uint8_t* src, int32_t len) {
52+
memcpy(&code[codePos], src, len);
53+
}
54+
55+
template<typename T>
56+
void emitAt(int32_t codePos, T src) {
57+
memcpy(&code[codePos], &src, sizeof(src));
58+
}
59+
};
60+
61+
struct CompilerState : public CodeBuffer {
62+
int32_t instructionOffsets[RANDOMX_PROGRAM_SIZE];
63+
int registerUsage[RegistersCount];
64+
};
65+
}
66+
67+
#if defined(RANDOMX_COMPILER_X86)
3268
#include "jit_compiler_x86.hpp"
33-
#elif defined(__aarch64__)
69+
#elif defined(RANDOMX_COMPILER_A64)
3470
#include "jit_compiler_a64.hpp"
71+
#elif defined(RANDOMX_COMPILER_RV64)
72+
#include "jit_compiler_rv64.hpp"
3573
#else
3674
#include "jit_compiler_fallback.hpp"
3775
#endif

0 commit comments

Comments
 (0)