LLVM Toolchain Test for RISC-V

LLVM Toolchain Test for RISC-V

This notebook tests the LLVM toolchain by creating, compiling, and running a C program that outputs “Hello risc-v world, courtesy of clang” natively on the Megrez board

Important Note: This notebook requires LLVM/clang with RISC-V support to be installed. If you don’t have it installed, you may need to build LLVM from source. Building LLVM can be time-consuming - for reference, a build on the Megrez board took approximately 8.5 hours to complete. See my other post on building an LLVM toolchain for detailed instructions.

Step 1: Create the C source file

%%file hello_riscv.c
#include <stdio.h>

int main() {
    printf("Hello risc-v world, courtesy of clang\n");
    return 0;
}

Writing hello_riscv.c

Step 2: Check if LLVM/clang is available

%%bash
echo "Checking for clang on Megrez board..."
clang --version | head -1
echo ""
echo "System architecture:"
uname -m

Checking for clang on Megrez board...
clang version 15.0.7 (https://github.com/llvm/llvm-project.git 8dfdcc7b7bf66834a761bd8de445840ef68e4d1a)

System architecture:
riscv64

Step 3: Compile the program natively for RISC-V

%%bash
# Compile natively for RISC-V architecture
echo "Compiling natively for RISC-V..."
clang -O2 -o hello_riscv hello_riscv.c

# Check if compilation was successful
if [ $? -eq 0 ]; then
    echo "✓ Compilation successful!"
    echo "Generated executable: hello_riscv"
    file hello_riscv
else
    echo "✗ Compilation failed!"
    echo "This may indicate that clang is not properly installed."
    echo "Refer to the build instructions for compiling LLVM from source."
    exit 1
fi

Compiling natively for RISC-V...
✓ Compilation successful!
Generated executable: hello_riscv
hello_riscv: ELF 64-bit LSB pie executable, UCB RISC-V, RVC, double-float ABI, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-riscv64-lp64d.so.1, for GNU/Linux 4.15.0, not stripped

Step 4: Run the compiled program natively on Megrez

%%bash
echo "Running natively on Megrez RISC-V board:"
./hello_riscv
echo ""
echo "Exit code: $?"

Running natively on Megrez RISC-V board:
Hello risc-v world, courtesy of clang

Exit code: 0

Step 5: Verify the file properties and clean up

%%bash
echo "Generated files:"
ls -la hello_riscv*
echo ""
echo "File type information:"
file hello_riscv
echo ""
echo "Binary architecture:"
readelf -h hello_riscv | grep "Machine:" || echo "readelf not available"
echo ""
echo "Test completed successfully! LLVM toolchain is working on Megrez board."

Generated files:
-rwxr-xr-x 1 debian debian 8616 Aug 25 15:17 hello_riscv
-rw-r--r-- 1 debian debian  104 Aug 25 15:16 hello_riscv.c

File type information:
hello_riscv: ELF 64-bit LSB pie executable, UCB RISC-V, RVC, double-float ABI, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-riscv64-lp64d.so.1, for GNU/Linux 4.15.0, not stripped

Binary architecture:
  Machine:                           RISC-V

Test completed successfully! LLVM toolchain is working on Megrez board.

 
1 Like