rv32emu icon indicating copy to clipboard operation
rv32emu copied to clipboard

Implement tier-2 JIT compiler to accelerate ISS

Open qwe661234 opened this issue 9 months ago • 2 comments

The T2C, designed for flexibility, efficiently transforms custom IR within a chained block into LLVM IR. After the translation, the built LLVM IR is offloaded to the LLVM backend, where it undergoes optimization through several selected LLVM passes. Subsequently, the optimized LLVM IR is passed to the LLVM execution engine, which compiles the optimized LLVM IR and returns a function pointer to the generated machine code. Because our design for T2C opts for a function pointer of the generated machine code instead of storing it in the code cache, we need not account for the binary format discrepancies among different compilers. This process allows for the possibility of substituting LLVM with alternative optimization frameworks if required.

Figure below shows the performance analysis of QEMU and rv32emu tiered-JIT mode. image

qwe661234 avatar May 04 '24 16:05 qwe661234

LLVM-17 is the preferable version for T2C's LLVM integration. Check Linux_Build.yml for the usage. Alternatively, xPack built LLVM packages are available for several host environments.

jserv avatar May 13 '24 04:05 jserv

Consider the following quick and dirty fix for macOS enablement:

--- a/Makefile
+++ b/Makefile
@@ -125,16 +125,28 @@ endif
 ENABLE_JIT ?= 0
 $(call set-feature, JIT)
 ifeq ($(call has, JIT), 1)
-OBJS_EXT += jit.o 
-ifneq ("$(findstring -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS, "$(shell llvm-config-17 --cflags)")", "")
+OBJS_EXT += jit.o
+LLVM_CONFIG = llvm-config-17
+LLVM_CONFIG := $(shell which $(LLVM_CONFIG))
+ifndef LLVM_CONFIG
+    # Try Homebrew on macOS
+    LLVM_CONFIG = /opt/homebrew/opt/llvm@17/bin/llvm-config
+    LLVM_CONFIG := $(shell which $(LLVM_CONFIG))
+    ifdef LLVM_CONFIG
+    LDFLAGS += -L/opt/homebrew/opt/llvm@17/lib
+    endif
+endif
+ifneq ("$(LLVM_CONFIG)", "")
+ifneq ("$(findstring -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS, "$(shell $(LLVM_CONFIG) --cflags)")", "")
 ENABLE_T2C := 1
 OBJS_EXT += t2c.o
-CFLAGS += -g `llvm-config-17 --cflags`
-LDFLAGS += `llvm-config-17 --libs core executionengine mcjit interpreter analysis native --system-libs`
+CFLAGS += -g $(shell $(LLVM_CONFIG) --cflags)
+LDFLAGS += $(shell $(LLVM_CONFIG) --libs core executionengine mcjit interpreter analysis native --system-libs)
 else
 ENABLE_T2C := 0
 $(warning No llvm-config-17 installed. Check llvm-config-17 installation in advance)
 endif
+endif
 ifneq ($(processor),$(filter $(processor),x86_64 aarch64 arm64))
 $(error JIT mode only supports for x64 and arm64 target currently.)
 endif

jserv avatar May 13 '24 16:05 jserv

I am a bit worried about the lengthy file src/t2c_template.c, which could lead to potential maintenance issues. Can you suggest a more advanced way to reduce the code size for T2C?

jserv avatar Jun 02 '24 15:06 jserv

I am a bit worried about the lengthy file src/t2c_template.c, which could lead to potential maintenance issues. Can you suggest a more advanced way to reduce the code size for T2C?

I reduce the code size through inline function and MARCO, this modification also provides more information about the gnerated machine code and make it more readable.

qwe661234 avatar Jun 03 '24 07:06 qwe661234

I defer to @vacantron for reviewing.

jserv avatar Jun 05 '24 13:06 jserv