Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compare with libriscv #288

Open
jserv opened this issue Dec 9, 2023 · 3 comments
Open

Compare with libriscv #288

jserv opened this issue Dec 9, 2023 · 3 comments
Assignees
Labels
research Study certain topics

Comments

@jserv
Copy link
Contributor

jserv commented Dec 9, 2023

libriscv is a compact yet comprehensive RISC-V userspace emulator library crafted for effortless embedding and extensive adaptability. It boasts a high-performing interpreter and an experimental binary translator, driven by TinyCC. When binary translation is activated, libtcc seamlessly integrates into the RISC-V emulator, taking on the role of the compiler for binary translation.

Interestingly, both libriscv and rv32emu share some common concepts and objectives. It would be valuable to engage in a comparative analysis to explore aspects such as interpretation and binary translation performance, techniques for ensuring secure sandboxed execution, and strategies for implementing userspace RISC-V emulation.

Expected outcomes:

  • Perform a performance comparison between libriscv and rv32emu, focusing on both interpreter and binary translation modes, evaluating each separately.
  • Compile a list of techniques employed in libriscv that warrant examination and potential adoption in rv32emu. Subsequently, create corresponding GitHub issues to track this process.

Reference:

@jserv jserv added the research Study certain topics label Dec 25, 2023
@jserv
Copy link
Contributor Author

jserv commented Dec 29, 2023

Build libriscv and its command line interface.

$ git clone https://github.com/libriscv/libriscv
$ cd libriscv/emulator
$ ./build.sh

Since libriscv relies on latest C++ features, recent clang might be required. Consider the following changes:

  • clang-17
--- a/emulator/CMakeLists.txt
+++ b/emulator/CMakeLists.txt
@@ -43,7 +43,7 @@ endif()
 if (LTO)
        if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
                set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto=full")
-               set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld")
+               set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
        else()
                include(CheckIPOSupported)
                check_ipo_supported(RESULT supported OUTPUT error)
--- a/emulator/build.sh
+++ b/emulator/build.sh
@@ -3,8 +3,8 @@ set -e
 
 mkdir -p .build
 pushd .build
-cmake .. -DCMAKE_BUILD_TYPE=Release
-make -j6
+cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=clang++-17
+make -j6 VERBOSE=1
 popd
 
 if test -f ".build/rvmicro"; then

Use rvlinux to execute ELF files.

It is important to comprehend the reasons behind libriscv's superior performance compared to rv32emu in pure interpreter mode.

@henrybear327 henrybear327 removed their assignment May 19, 2024
@yangyi625
Copy link
Collaborator

I conducted experiments comparing the interpreter modes of libriscv and rv32emu.

The results are shown in the following chart, where we can clearly observe that libriscv performs better:

upload_e153bf8602297dcf697fcf91253082b9

Furthermore, I observed that the number of executed instructions was nearly identical:

Program rv32emu libriscv
Dhrystone 71500011737 71500011833
AES 4258475 4276028
Mandelbrot 3256610 3256700
Pi 13594914 13594999
Richards 359441863 359441948
N-Queens 2428587791 2428587876
Primes 7114988149 7114988162
SHA-512 7617544976 7617545069

Therefore, I began to examine libriscv's implementation.
I discovered that libriscv has designed a decoder cache, which appears to be a feature worth considering.
The workflow can be illustrated as follows:

graph TD
    A[Load ELF file] --> C[Generate Decoder Cache]
    C --> D[Main loop]
    D --> E[Fetch instruction from Decoder Cache]
    E --> F[Execute instruction]
    F --> G[Update PC]
    G --> E
Loading

After loading the ELF file, the entire executable segment is pre-decoded to generate a cache. This approach reduces repetitive decoding during execution.

@yangyi625
Copy link
Collaborator

When rv32emu has a complete MMU, perhaps we can reference some of libriscv's experimental features.

  1. read-write arena

  2. Experimental unbounded 32-bit addressing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
research Study certain topics
Projects
None yet
Development

No branches or pull requests

4 participants