Skip to content

Commit

Permalink
[apps] Extended sw build for Linux
Browse files Browse the repository at this point in the history
* Added LINUX switch, default LINUX=0
  • Loading branch information
MaistoV authored and mp-17 committed Jun 25, 2024
1 parent e385500 commit fb6985f
Show file tree
Hide file tree
Showing 14 changed files with 153 additions and 40 deletions.
2 changes: 2 additions & 0 deletions apps/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
bin
common/link.ld
*.o*
data.S*
29 changes: 20 additions & 9 deletions apps/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ APPS_DIR := $(ROOT_DIR)
COMMON_DIR := $(ROOT_DIR)/common
TESTS_DIR := $(ROOT_DIR)/riscv-tests/isa

# Build environment for Linux
LINUX ?= 0
ifeq ($(LINUX), 1)
include $(COMMON_DIR)/linux.mk
endif

# This will overwrite the ROOT_DIR variable from the included makefile
include $(COMMON_DIR)/runtime.mk
include $(COMMON_DIR)/riscv_tests.mk
Expand All @@ -33,9 +39,9 @@ BINARIES := $(filter-out bin/benchmarks, $(addprefix bin/,$(APPS)))
CVA6_EXTENSIONS := rv64ui rv64uc rv64um rv64uf rv64ud rv64si
# Atomics are messy, since there is currently no memory region capable of handling them
# CVA6_EXTENSIONS := rv64ua
CVA6_BINARIES := $(addprefix bin/, $(cva6_tests))
CVA6_BINARIES := $(addsuffix $(IS_LINUX_EXTENSION), $(addprefix bin/, $(cva6_tests)))
ARA_EXTENSIONS := rv64uv
ARA_BINARIES := $(addprefix bin/, $(ara_tests))
ARA_BINARIES := $(addsuffix $(IS_LINUX_EXTENSION), $(addprefix bin/, $(ara_tests)))

# FFT requires special treatment because of its header files
ifeq ($(ENV_DEFINES),)
Expand Down Expand Up @@ -98,14 +104,18 @@ endef
$(foreach app,$(APPS),$(eval $(call app_compile_template_spike,$(app))))

define app_compile_template
bin/$1: $1/data.S.o $(addsuffix .o, $(shell find $(1) -name "*.c" -o -name "*.S")) $(RUNTIME_LLVM) linker_script
bin/$1: $1/data.S.o$$(IS_LINUX_EXTENSION) $(addsuffix .o$$(IS_LINUX_EXTENSION), $(shell find $(1) -name "*.c" -o -name "*.S")) $(RUNTIME_LLVM) linker_script
mkdir -p bin/
$$(RISCV_CC) -Iinclude $(RISCV_CCFLAGS) -o $$@ $$(addsuffix .o, $$(shell find $(1) -name "*.c" -o -name "*.S")) $(RUNTIME_LLVM) $$(RISCV_LDFLAGS) -T$$(CURDIR)/common/link.ld
$$(RISCV_OBJDUMP) $$(RISCV_OBJDUMP_FLAGS) -D $$@ > $$@.dump
$$(RISCV_STRIP) $$@ -S --strip-unneeded
$$(RISCV_CC) $(RISCV_CCFLAGS) -o $$@$$(IS_LINUX_EXTENSION) $$(addsuffix .o$$(IS_LINUX_EXTENSION), $$(shell find $(1) -name "*.c" -o -name "*.S")) $(RUNTIME_LLVM) $$(RISCV_LDFLAGS) $$(LD_FLAGS)
$$(RISCV_OBJDUMP) $$(RISCV_OBJDUMP_FLAGS) -D $$@$$(IS_LINUX_EXTENSION) > $$@$$(IS_LINUX_EXTENSION).dump
# Don't strip symbols for Linux build since need them for debug
if [ "$$(IS_LINUX_EXTENSION)" == "" ]; then \
$$(RISCV_STRIP) $$@$$(IS_LINUX_EXTENSION) -S --strip-unneeded; \
fi
endef
$(foreach app,$(APPS),$(eval $(call app_compile_template,$(app))))


# Make the RISC-V tests
riscv_tests: $(CVA6_BINARIES) $(ARA_BINARIES)

Expand All @@ -114,7 +124,7 @@ TESTS_$(1) := $(addprefix bin/, $($(addsuffix _ara_tests, $1)))

bin/$(1)-ara-%: $(TESTS_DIR)/$(1)/%.$(2) $(RUNTIME_GCC) linker_script
mkdir -p bin/
$$(RISCV_CC_GCC) -Iinclude -I$$(TESTS_DIR)/macros/scalar -I$$(TESTS_DIR)/macros/vector $$(RISCV_CCFLAGS_GCC) $$(RISCV_LDFLAGS_GCC) -o $$@ $$< $(RUNTIME_GCC) -T$$(CURDIR)/common/link.ld
$$(RISCV_CC_GCC) -Iinclude -I$$(TESTS_DIR)/macros/scalar -I$$(TESTS_DIR)/macros/vector $$(RISCV_CCFLAGS_GCC) $$(RISCV_LDFLAGS_GCC) -o $$@ $$< $(RUNTIME_GCC) $$(LD_FLAGS)
$$(RISCV_OBJDUMP) $$(RISCV_OBJDUMP_FLAGS) -D $$@ > $$@.dump
$$(RISCV_STRIP) $$@ -S --strip-unneeded
endef
Expand All @@ -124,7 +134,7 @@ TESTS_$(1) := $(addprefix bin/, $($(addsuffix _ara_tests, $1)))

bin/$(1)-ara-%: $(TESTS_DIR)/$(1)/%.$(2) $(RUNTIME_LLVM) linker_script
mkdir -p bin/
$$(RISCV_CC) -Iinclude -I$$(TESTS_DIR)/macros/scalar -I$$(TESTS_DIR)/macros/vector $$(RISCV_CCFLAGS) $$(RISCV_LDFLAGS) -o $$@ $$< $(RUNTIME_LLVM) -T$$(CURDIR)/common/link.ld
$$(RISCV_CC) -Iinclude -I$$(TESTS_DIR)/macros/scalar -I$$(TESTS_DIR)/macros/vector $$(RISCV_CCFLAGS) $$(RISCV_LDFLAGS) -o $$@ $$< $(RUNTIME_LLVM) $$(LD_FLAGS)
$$(RISCV_OBJDUMP) $$(RISCV_OBJDUMP_FLAGS) -D $$@ > $$@.dump
$$(RISCV_STRIP) $$@ -S --strip-unneeded
endef
Expand Down Expand Up @@ -172,13 +182,14 @@ benchmarks_clean:

.PHONY: clean
clean: riscv_tests_spike_clean benchmarks_clean
rm -vf bin/*
rm -vf $(BINARIES)
rm -vf $(CVA6_BINARIES)
rm -vf $(ARA_BINARIES)
rm -vf $(addsuffix .dump,$(BINARIES))
rm -vf $(addsuffix .dump,$(CVA6_BINARIES))
rm -vf $(addsuffix .dump,$(ARA_BINARIES))
rm -vf $(addsuffix /main.c.o,$(APPS))
rm -vf $(addsuffix /main.c.o$(IS_LINUX_EXTENSION),$(APPS))
rm -vf $(RUNTIME_GCC)
rm -vf $(RUNTIME_LLVM)
rm -vf $(RUNTIME_SPIKE)
Expand Down
47 changes: 47 additions & 0 deletions apps/common/linux.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
IS_LINUX_EXTENSION := .linux

CVA6_SDK ?= /usr/scratch/fenga3/vmaisto/cva6-sdk_fork_backup
ROOTFS_DEST ?= $(CVA6_SDK)/rootfs/ara/apps/bin
cp_to_rootfs:
mkdir -p $(ROOTFS_DEST)
@echo "[Copying binaries to rootfs directory $(ROOTFS_DEST)]"
cp -v bin/*.linux $(ROOTFS_DEST)

# Set the runtime variables to empty, the Linux libs will takcare of that
LD_FLAGS :=
RUNTIME_GCC ?= common/util-gcc.c.o
RUNTIME_LLVM ?= common/util-llvm.c.o


# Override
INSTALL_DIR ?= $(ARA_DIR)/install
GCC_INSTALL_DIR ?= $(CVA6_SDK)/buildroot/output/host/
LLVM_INSTALL_DIR ?= $(INSTALL_DIR)/riscv-llvm

RISCV_XLEN ?= 64
RISCV_ARCH ?= rv$(RISCV_XLEN)gcv
RISCV_ABI ?= lp64d
RISCV_TARGET ?= riscv$(RISCV_XLEN)-buildroot-linux-gnu-

# Don't use LLVM
RISCV_PREFIX ?= $(GCC_INSTALL_DIR)/bin/$(RISCV_TARGET)
RISCV_CC ?= $(RISCV_PREFIX)gcc
RISCV_CXX ?= $(RISCV_PREFIX)g++
RISCV_OBJDUMP ?= $(RISCV_PREFIX)objdump
RISCV_OBJCOPY ?= $(RISCV_PREFIX)objcopy
RISCV_AS ?= $(RISCV_PREFIX)as
RISCV_AR ?= $(RISCV_PREFIX)ar
RISCV_LD ?= $(RISCV_PREFIX)ld
RISCV_STRIP ?= $(RISCV_PREFIX)strip

# Override flags
# LLVM_FLAGS ?= -march=rv64gcv_zfh_zvfh0p1 -mabi=$(RISCV_ABI) -mno-relax -fuse-ld=lld
LLVM_FLAGS ?= -march=rv64gcv -mabi=$(RISCV_ABI)
LLVM_V_FLAGS ?= #+no-optimized-zero-stride-load
# RISCV_FLAGS ?= $(LLVM_FLAGS) $(LLVM_V_FLAGS) -mcmodel=medany -I$(CURDIR)/common -std=gnu99 -O3 -ffast-math -fno-common -fno-builtin-printf $(DEFINES) $(RISCV_WARNINGS)
RISCV_FLAGS ?= -g $(LLVM_FLAGS) $(LLVM_V_FLAGS) -I$(CURDIR)/common -std=gnu99 -O0 $(DEFINES) $(RISCV_WARNINGS)
RISCV_CCFLAGS ?= $(RISCV_FLAGS) #-ffunction-sections -fdata-sections
RISCV_CXXFLAGS ?= $(RISCV_FLAGS) -ffunction-sections -fdata-sections
RISCV_LDFLAGS ?= #-static -nostartfiles -lm -Wl,--gc-sections

RISCV_OBJDUMP_FLAGS ?= -S
5 changes: 5 additions & 0 deletions apps/common/printf.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
//
///////////////////////////////////////////////////////////////////////////////

#ifdef __linux__
#include <stdio.h>
#else // ! __linux__

#ifndef _PRINTF_H_
#define _PRINTF_H_

Expand Down Expand Up @@ -100,4 +104,5 @@ int fctprintf(void (*out)(char character, void *arg), void *arg,
}
#endif

#endif // __linux__
#endif // _PRINTF_H_
22 changes: 14 additions & 8 deletions apps/common/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@
asm volatile( \
"csrs mstatus, %[bits];" ::[bits] "r"(0x00000600 & (0x00000600 >> 1)))

// SoC-level CSR, put in memory for Linux build
#ifdef __linux__
int64_t event_trigger;
int64_t timer;
uint64_t hw_cnt_en_reg;
#else // ! __linux__
extern int64_t event_trigger;
extern int64_t timer;
// SoC-level CSR
extern uint64_t hw_cnt_en_reg;
#endif // __linux__

// Return the current value of the cycle counter
inline int64_t get_cycle_count() {
int64_t get_cycle_count() {
int64_t cycle_count;
// The fence is needed to be sure that Ara is idle, and it is not performing
// the last vector stores when we read mcycle with stop_timer()
Expand All @@ -31,26 +37,26 @@ inline int64_t get_cycle_count() {
#define HW_CNT_READY hw_cnt_en_reg = 1;
#define HW_CNT_NOT_READY hw_cnt_en_reg = 0;
// Start and stop the counter
inline void start_timer() { timer = -get_cycle_count(); }
inline void stop_timer() { timer += get_cycle_count(); }
void start_timer() { timer = -get_cycle_count(); }
void stop_timer() { timer += get_cycle_count(); }

// Get the value of the timer
inline int64_t get_timer() { return timer; }
int64_t get_timer() { return timer; }
#else
#define HW_CNT_READY ;
#define HW_CNT_NOT_READY ;
// Start and stop the counter
inline void start_timer() {
void start_timer() {
while (0)
;
}
inline void stop_timer() {
void stop_timer() {
while (0)
;
}

// Get the value of the timer
inline int64_t get_timer() { return 0; }
int64_t get_timer() { return 0; }
#endif

#endif // _RUNTIME_H_
12 changes: 8 additions & 4 deletions apps/common/runtime.mk
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ ISA_SIM_MOD_INSTALL_DIR ?= $(INSTALL_DIR)/riscv-isa-sim-mod
RISCV_XLEN ?= 64
RISCV_ARCH ?= rv$(RISCV_XLEN)gcv
RISCV_ABI ?= lp64d
RISCV_TARGET ?= riscv$(RISCV_XLEN)-unknown-elf
RISCV_TARGET ?= riscv$(RISCV_XLEN)-unknown-elf-

# Use LLVM
RISCV_PREFIX ?= $(LLVM_INSTALL_DIR)/bin/
Expand All @@ -56,7 +56,9 @@ RISCV_LD ?= $(RISCV_PREFIX)ld.lld
RISCV_STRIP ?= $(RISCV_PREFIX)llvm-strip

# Use gcc to compile scalar riscv-tests
RISCV_CC_GCC ?= $(GCC_INSTALL_DIR)/bin/$(RISCV_TARGET)-gcc
RISCV_CC_GCC ?= $(GCC_INSTALL_DIR)/bin/$(RISCV_TARGET)gcc
RISCV_OBJCOPY_GCC ?= $(GCC_INSTALL_DIR)/bin/$(RISCV_TARGET)objcopy
RISCV_OBJDUMP_GCC ?= $(GCC_INSTALL_DIR)/bin/$(RISCV_TARGET)objdump

# Benchmark with spike
spike_env_dir ?= $(ARA_DIR)/apps/riscv-tests
Expand Down Expand Up @@ -111,6 +113,8 @@ RUNTIME_GCC ?= common/crt0-gcc.S.o common/printf-gcc.c.o common/string-gcc.c.o
RUNTIME_LLVM ?= common/crt0-llvm.S.o common/printf-llvm.c.o common/string-llvm.c.o common/serial-llvm.c.o common/util-llvm.c.o
RUNTIME_SPIKE ?= $(spike_env_dir)/benchmarks/common/crt.S.o.spike $(spike_env_dir)/benchmarks/common/syscalls.c.o.spike common/util.c.o.spike

LD_FLAGS ?= -T$(CURDIR)/common/link.ld

.INTERMEDIATE: $(RUNTIME_GCC) $(RUNTIME_LLVM)

%-gcc.S.o: %.S
Expand All @@ -125,10 +129,10 @@ RUNTIME_SPIKE ?= $(spike_env_dir)/benchmarks/common/crt.S.o.spike $(spike_env_di
%-llvm.c.o: %.c
$(RISCV_CC) $(RISCV_CCFLAGS) -c $< -o $@

%.S.o: %.S
%.S.o$(IS_LINUX_EXTENSION): %.S
$(RISCV_CC) $(RISCV_CCFLAGS) -c $< -o $@

%.c.o: %.c
%.c.o$(IS_LINUX_EXTENSION): %.c
$(RISCV_CC) $(RISCV_CCFLAGS) -c $< -o $@

%.S.o.spike: %.S patch-spike-crt0
Expand Down
2 changes: 0 additions & 2 deletions apps/dropout/kernel/dropout.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
// Use asm, by default
#undef INTRINSICS

#include "runtime.h"

#ifndef SPIKE
#include "printf.h"
#endif
Expand Down
11 changes: 11 additions & 0 deletions apps/dropout/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@

#include "kernel/dropout.h"

#include <stdint.h>
#include <string.h>

#include "runtime.h"

#ifndef SPIKE
#include "printf.h"
#else
#include <stdio.h>
#endif

extern const unsigned int N;
extern const float SCALE;
extern const float I[] __attribute__((aligned(4 * NR_LANES)));
Expand Down
1 change: 0 additions & 1 deletion apps/gemv/kernel/gemv.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
// Author: Chi Zhang, ETH Zurich <[email protected]>

#include "gemv.h"
#include "runtime.h"
#include "util.h"
#include <math.h>
#include <stdbool.h>
Expand Down
4 changes: 1 addition & 3 deletions apps/jacobi2d/kernel/jacobi2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,13 @@ WITH ACCESS OR USE OF THE SOFTWARE.

#define _JACOBI2D_H_

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <riscv_vector.h>

#include "runtime.h"
#include "util.h"

// The vector algorithm seems not to be parametrized on the data type
// So, don't change this parameter if also the vector implementation is used
#define DATA_TYPE double
Expand Down
12 changes: 6 additions & 6 deletions apps/riscv-tests/benchmarks/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@ bmarks = \
# Build rules
#--------------------------------------------------------------------

RISCV_PREFIX ?= riscv$(XLEN)-unknown-elf-
RISCV_PREFIX ?= /usr/scratch/fenga3/vmaisto/cva6-sdk_fork/buildroot/output/host/bin/riscv64-buildroot-linux-gnu-
RISCV_GCC ?= $(RISCV_PREFIX)gcc
RISCV_GCC_OPTS ?= -DPREALLOCATE=1 -mcmodel=medany -static -std=gnu99 -O2 -ffast-math -fno-common -fno-builtin-printf
RISCV_LINK ?= $(RISCV_GCC) -T $(src_dir)/common/test.ld $(incs)
RISCV_LINK_OPTS ?= -static -nostdlib -nostartfiles -lm -lgcc -T $(src_dir)/common/test.ld
RISCV_OBJDUMP ?= $(RISCV_PREFIX)objdump --disassemble-all --disassemble-zeroes --section=.text --section=.text.startup --section=.text.init --section=.data
RISCV_GCC_OPTS ?= -DPREALLOCATE=1 -mcmodel=medany -std=gnu99 -O2 -ffast-math -fPIC
RISCV_LINK ?= $(RISCV_GCC) $(incs)
RISCV_LINK_OPTS ?=
RISCV_OBJDUMP ?= $(RISCV_PREFIX)objdump --disassemble-all --disassemble-zeroes -S
RISCV_SIM ?= spike --isa=rv$(XLEN)gc

incs += -I$(src_dir)/../env -I$(src_dir)/common $(addprefix -I$(src_dir)/, $(bmarks))
objs :=

define compile_template
$(1).riscv: $(wildcard $(src_dir)/$(1)/*) $(wildcard $(src_dir)/common/*)
$$(RISCV_GCC) $$(incs) $$(RISCV_GCC_OPTS) -o $$@ $(wildcard $(src_dir)/$(1)/*.c) $(wildcard $(src_dir)/common/*.c) $(wildcard $(src_dir)/common/*.S) $$(RISCV_LINK_OPTS)
$$(RISCV_GCC) $$(incs) $$(RISCV_GCC_OPTS) -o $$@ $(wildcard $(src_dir)/$(1)/*.c) $(wildcard $(src_dir)/common/*.c) $$(RISCV_LINK_OPTS)
endef

$(foreach bmark,$(bmarks),$(eval $(call compile_template,$(bmark))))
Expand Down
Loading

0 comments on commit fb6985f

Please sign in to comment.