Skip to content

Commit

Permalink
MUSL: Adding a new lightweight math library to Snitch (#25)
Browse files Browse the repository at this point in the history
* musl: add library as new submodule with README

* musl: add musl library to toolchain

* ci: add musl build

---------

Co-authored-by: Viviane Potocnik <[email protected]>
  • Loading branch information
paulsc96 and Viviane Potocnik authored Jul 18, 2023
1 parent 487521d commit 090ab26
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 3 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ jobs:
submodules: 'recursive'
- name: Build Software
run: |
cd sw/deps
mkdir install
cd musl
CC=$LLVM_BINROOT/clang ./configure --disable-shared \
--prefix=../install/ --enable-wrapper=all \
CFLAGS="-mcpu=snitch -menable-experimental-extensions"
make -j4
make install
cd ../../../
make -C target/snitch_cluster sw
- name: Build Hardware
run: |
Expand All @@ -61,4 +70,13 @@ jobs:
submodules: 'recursive'
- name: Build Software
run: |
cd sw/deps
mkdir install
cd musl
CC=$LLVM_BINROOT/clang ./configure --disable-shared \
--prefix=../install/ --enable-wrapper=all \
CFLAGS="-mcpu=snitch -menable-experimental-extensions"
make -j4
make install
cd ../../../
make -C target/snitch_cluster SELECT_RUNTIME=banshee sw
16 changes: 13 additions & 3 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ variables:
VERILATOR: verilator-4.110
QUESTA: questa-2022.3
LLVM_BINROOT: /usr/pack/riscv-1.0-kgf/pulp-llvm-0.12.0/bin
CLANG: /usr/pack/riscv-1.0-kgf/pulp-llvm-0.12.0/bin/clang

before_script:
- $PYTHON -m venv .venv
Expand All @@ -33,6 +34,15 @@ docs:

snitch-cluster-sw:
script:
- cd sw/deps
- mkdir install
- cd musl
- CC=$CLANG ./configure --disable-shared --prefix=../install/
--enable-wrapper=all
CFLAGS="-mcpu=snitch -menable-experimental-extensions"
- make -j4
- make install
- cd ../../../
- cd target/snitch_cluster
- make sw
artifacts:
Expand Down Expand Up @@ -78,7 +88,7 @@ snitch-cluster-vlt:
script:
- cd target/snitch_cluster
- $VERILATOR make bin/snitch_cluster.vlt
- $VERILATOR ./sw/tests/run.py sw/tests/passing-apps.list --simulator verilator
- $VERILATOR ./sw/test/run.py sw/tests/passing-apps.list --simulator verilator
- $VERILATOR ./sw/apps/run.py sw/apps/passing-apps.list --simulator verilator
# yamllint enable rule:line-length

Expand All @@ -88,7 +98,7 @@ snitch-cluster-vcs:
script:
- cd target/snitch_cluster
- $VCS make bin/snitch_cluster.vcs
- $VCS ./sw/tests/run.py sw/tests/passing-apps.list --simulator vcs
- $VCS ./sw/test/run.py sw/tests/passing-apps.list --simulator vcs
- $VCS ./sw/apps/run.py sw/apps/passing-apps.list --simulator vcs

# Questa
Expand All @@ -97,5 +107,5 @@ snitch-cluster-vsim:
script:
- cd target/snitch_cluster
- $QUESTA make bin/snitch_cluster.vsim
- $QUESTA ./sw/tests/run.py sw/tests/passing-apps.list --simulator vsim
- $QUESTA ./sw/test/run.py sw/tests/passing-apps.list --simulator vsim
- $QUESTA ./sw/apps/run.py sw/apps/passing-apps.list --simulator vsim
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "sw/deps/printf"]
path = sw/deps/printf
url = https://github.com/mpaland/printf.git
[submodule "sw/deps/musl"]
path = sw/deps/musl
url = [email protected]:kraj/musl.git
42 changes: 42 additions & 0 deletions sw/deps/MUSL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# musl Guide

In this guide, we show you how to set up `Snitch` with the `musl` library.

## Prerequisites
Make sure that you update this `musl` submodule to the latest version.
```
git submodule update --init --recursive
```
Afterward, we need to set the correct compiler so that `Snitch` can compile the `musl` library.
```
export CC=clang
```
Where `clang` is the compiler that you want to use. At IIS, please use `/usr/pack/riscv-1.0-kgf/pulp-llvm-0.12.0/bin/riscv32-unknown-elf-clang`.
Next, we create the installation directory and configure the build and installation process.
```
mkdir install
cd musl
./configure --disable-shared --prefix=../install/ --with-boost-libdir=<path_to_boost_dir> --enable-wrapper=all CFLAGS="-mcpu=snitch -menable-experimental-extensions"
```
Where `<path_to_boost_dir>` is the path to the `boost` library. At IIS, if you are using Alma Linux you might have to install the `boost-devel` package.
You can do this by retrieviing the `rpm` package and updating your `LD_LIBRARY_PATH` variable.
```
wget https://repo.almalinux.org/almalinux/8/AppStream/aarch64/os/Packages/boost-regex-1.66.0-13.el8.aarch64.rpm
rpm2cpio boost-regex-1.66.0-13.el8.aarch64.rpm | cpio -idmv
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/.local/lib64
```
Afterward, we can build and install the `musl` library.
```
make -j$(nproc)
make install
```
You might have to change the permissions of the `./tools/install.sh` script. This can be done by running `chmod +x ./tools/install.sh`.
Finally, we need to set the `LD_LIBRARY_PATH` variable so that `Snitch` can find the `musl` library.
```
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<home>/snitch_cluster/sw/deps/install/lib
```
where `<home>` is the path to the location where you cloned the `snitch_cluster` repository.

**Important** : Whenever you change the source code of the `musl` library, you need to recompile it.


1 change: 1 addition & 0 deletions sw/deps/musl
Submodule musl added at 4d561a
1 change: 1 addition & 0 deletions sw/snRuntime/src/omp/kmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "kmp.h"

#include <inttypes.h> // for PRIx##
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>

Expand Down
21 changes: 21 additions & 0 deletions target/snitch_cluster/sw/toolchain.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-License-Identifier: Apache-2.0
#
# Luca Colagrande <[email protected]>
# Viviane Potocnik <[email protected]>

######################
# Invocation options #
Expand All @@ -14,6 +15,10 @@ DEBUG ?= OFF # ON to turn on debugging symbols
# Build variables #
###################

# Directory to the MUSL installation
MUSL_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST))))
SW_INSTALL_DIR ?= $(abspath $(MUSL_DIR)/../../../sw/deps/install/)

# Compiler toolchain
LLVM_BINROOT ?= $(dir $(shell which riscv32-unknown-elf-clang))
RISCV_CC ?= $(LLVM_BINROOT)/clang
Expand All @@ -25,6 +30,11 @@ RISCV_DWARFDUMP ?= $(LLVM_BINROOT)/llvm-dwarfdump

# Compiler flags
RISCV_CFLAGS += $(addprefix -I,$(INCDIRS))

# musl specific flags
RISCV_CFLAGS += -I$(SW_INSTALL_DIR)/include
RISCV_CFLAGS += -B$(SW_INSTALL_DIR)/bin

RISCV_CFLAGS += -mcpu=snitch
RISCV_CFLAGS += -menable-experimental-extensions
RISCV_CFLAGS += -mabi=ilp32d
Expand All @@ -33,6 +43,13 @@ RISCV_CFLAGS += -ffast-math
RISCV_CFLAGS += -fno-builtin-printf
RISCV_CFLAGS += -fno-common
RISCV_CFLAGS += -fopenmp

# TODO: check why the LTO flags optimizes the CBSS section away
# RISCV_CFLAGS += -flto=thin

RISCV_CFLAGS += -ftls-model=local-exec
RISCV_CFLAGS += -nostdinc

RISCV_CFLAGS += -O3
ifeq ($(DEBUG), ON)
RISCV_CFLAGS += -g
Expand All @@ -42,6 +59,10 @@ endif
RISCV_LDFLAGS += -fuse-ld=$(RISCV_LD)
RISCV_LDFLAGS += -nostartfiles
RISCV_LDFLAGS += -lm
# musl specific flags
RISCV_LDFLAGS += -L$(SW_INSTALL_DIR)/lib
RISCV_LDFLAGS += -nostdinc
# RISCV_LDFLAGS += -flto=thin

# Archiver flags
RISCV_ARFLAGS = rcs

0 comments on commit 090ab26

Please sign in to comment.