Trying gem5 with RVV 1.0 Support (1. Building and Debugging)

gem5 is an architecture simulator, a tool that can simulate subsystems including CPUs. It supports architectures such as RISC-V and is also capable of cycle-accurate simulation.

I wanted to check the state of gem5's support for simulating RISC-V Vector, so I decided to look into how to build it.

  1. Use Rivos's working repository for gem5. Clone the following Git repository.
git clone https://github.com/rivosinc/gem5.git gem5-rvv -b rivos/dev/joy/initial_RVV_support
  1. The build method is as follows. It takes quite a long time.
scons build/RISCV/gem5.opt -j8

Cycle-Accurate Simulation with gem5: Running the Scalar Benchmark

For testing, let's use the RISC-V Vectorized Benchmark. Download it from the following.

git clone https://github.com/RALC88/riscv-vectorized-benchmark-suite.git -b rvv-1.0

It comes with prebuilt binaries, so let's just run one as is.

./gem5-rvv/build/RISCV/gem5.opt \
    --debug-flags=O3PipeView \
    --debug-file=trace.out \
    gem5-rvv/configs/example/se.py \
    --cpu-type=DerivO3CPU \
    --caches \
    --cmd=riscv-vectorized-benchmark-suite/_blackscholes/bin/blackscholes_serial.exe \
    -o "1 riscv-vectorized-benchmark-suite/_blackscholes/input/in_256.input output_serial.txt"

I'll skip explaining the options since they're easy to look up. trace.out is generated in m5out/.

Let's view it in Konata. The file is about 5 GB, so it takes a little while.

It looks about right.

Cycle-Accurate Simulation with gem5: Running the Vector Benchmark

Next, I want to run the vectorized benchmark. I change the command as follows.

./gem5-rvv/build/RISCV/gem5.opt \
    --debug-flags=O3PipeView \
    --debug-file=trace_v.out \
    gem5-rvv/configs/example/se.py \
    --cpu-type=DerivO3CPU \
    --caches \
    --cmd=riscv-vectorized-benchmark-suite/_blackscholes/bin/blackscholes_vector.exe \
    -o "1 riscv-vectorized-benchmark-suite/_blackscholes/input/in_256.input output_parallel.txt"
gem5 Simulator System.  http://gem5.org
gem5 is copyrighted software; use the --copyright option for details.

gem5 version 21.2.1.0
gem5 compiled May  9 2023 10:19:44
gem5 started May  9 2023 14:39:50
gem5 executing on msyksphinz-tower, pid 1488812
command line: ./gem5-rvv/build/RISCV/gem5.opt --debug-flags=O3PipeView --debug-file=trace_v.out gem5-rvv/configs/example/se.py --cpu-type=DerivO3CPU --caches --cmd=riscv-vectorized-benchmark-suite/_blackscholes/bin/blackscholes_vector.exe -o '1 riscv-vectorized-benchmark-suite/_blackscholes/input/in_256.input output_parallel.txt'
Global frequency set at 1000000000000 ticks per second
warn: No dot file generated. Please install pydot to generate the dot file and pdf.
build/RISCV/mem/mem_interface.cc:791: warn: DRAM device capacity (8192 Mbytes) does not match the address range assigned (512 Mbytes)
build/RISCV/arch/riscv/linux/se_workload.cc:60: warn: Unknown operating system; assuming Linux.
0: system.remote_gdb: listening for remote gdb on port 7000
**** REAL SIMULATION ****
build/RISCV/sim/simulate.cc:194: info: Entering event queue @ 0.  Starting simulation...
zsh: floating point exception (core dumped)  ./gem5-rvv/build/RISCV/gem5.opt --debug-flags=O3PipeView    --caches  -o

Oh, it crashed. That's a bit of a problem. There's no record of where it crashed, either. I may have to debug it with GDB...

Update: When I tried to attach GDB, gem5 itself crashed.

# GDB side
target remote localhost:7000
build/RISCV/sim/workload.cc:100: info: system.workload: Waiting for a remote GDB connection on port 7000.
build/RISCV/base/remote_gdb.cc:708: warn: Couldn't read data from debugger.
build/RISCV/sim/simulate.cc:194: info: Entering event queue @ 0.  Starting simulation...
zsh: floating point exception (core dumped)  ./gem5-rvv/build/RISCV/gem5.opt --debug-flags=O3PipeView   --wait-gdb    -o

Building in Debug Mode

scons build/RISCV/gem5.debug -j$(nproc)

After debugging in various ways with GDB, I found a few points. This branch still seems to have problems, and a few fixes are needed.

  • alignToPowerOfTwo() needs to be fixed. When the value is 0, it should return 1.
diff --git a/src/base/bitfield.hh b/src/base/bitfield.hh
index 1b585da09..8ac3eb813 100644
--- a/src/base/bitfield.hh
+++ b/src/base/bitfield.hh
@@ -491,7 +491,10 @@ popCount(uint64_t val)
 constexpr uint64_t
 alignToPowerOfTwo(uint64_t val)
 {
-    val--;
+  if (val == 0) {
+    return 1;
+  }
+  val--;
     val |= val >> 1;
     val |= val >> 2;
     val |= val >> 4;
  • For now, allow accesses to undefined CSRs. Accessing hpmcounter doesn't seem to be supported, so this is just a stopgap.
diff --git a/src/arch/riscv/faults.cc b/src/arch/riscv/faults.cc
index eea42fe27..d5100c665 100644
--- a/src/arch/riscv/faults.cc
+++ b/src/arch/riscv/faults.cc
@@ -192,9 +192,9 @@ UnknownInstFault::invokeSE(ThreadContext *tc, const StaticInstPtr &inst)
 void
 IllegalInstFault::invokeSE(ThreadContext *tc, const StaticInstPtr &inst)
 {
-    auto *rsi = static_cast<RiscvStaticInst *>(inst.get());
-    panic("Illegal instruction 0x%08x at pc %s: %s", rsi->machInst,
-        tc->pcState(), reason.c_str());
+    // auto *rsi = static_cast<RiscvStaticInst *>(inst.get());
+    // panic("Illegal instruction 0x%08x at pc %s: %s", rsi->machInst,
+    //     tc->pcState(), reason.c_str());
 }

I ran the benchmark again with these changes, but it complained that initiateAcc() is not defined. Why is that? It's a close call between debugging a bit more and giving up on Rivos's gem5 once and for all.

Global frequency set at 1000000000000 ticks per second
warn: No dot file generated. Please install pydot to generate the dot file and pdf.
build/RISCV/mem/mem_interface.cc:791: warn: DRAM device capacity (8192 Mbytes) does not match the address range assigned (512 Mbytes)
build/RISCV/arch/riscv/linux/se_workload.cc:60: warn: Unknown operating system; assuming Linux.
0: system.remote_gdb: listening for remote gdb on port 7000
**** REAL SIMULATION ****
build/RISCV/sim/simulate.cc:194: info: Entering event queue @ 0.  Starting simulation...
PARSEC Benchmark Suite
build/RISCV/sim/mem_state.cc:443: info: Increasing stack size by one page.
Num of Options: 256
Num of Runs: 100
Size of data: 10240

BlackScholes Initialization took 0.00000000 secs
build/RISCV/cpu/static_inst.hh:315: panic: initiateAcc not defined!
Memory Usage: 625724 KBytes
Program aborted at tick 682762500

This is an English translation of the original Japanese post: RVV-1.0をサポートしたGem5を試す (1. ビルドとデバッグ)