Skip to content

Commit af81b88

Browse files
mkannwischerhanno-becker
authored andcommitted
auto.mk: Add RVV feature detection for RISC-V 64-bit
Previously, -march=rv64gcv was unconditionally added for all riscv64 targets, causing compilation failures on RISC-V systems without the vector extension. Add host CPU and compiler feature detection for RVV, mirroring the existing pattern for x86_64 (AVX2/BMI2) and AArch64 (SHA3). The host detection parses the ISA string from /proc/cpuinfo to check for the 'v' extension. Also add riscv64 to the host_info make target. Signed-off-by: Matthias J. Kannwischer <matthias@zerorisc.com>
1 parent 11b58a7 commit af81b88

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ ifeq ($(ARCH),x86_64)
270270
else ifeq ($(ARCH),aarch64)
271271
@echo "=== AArch64 Feature Support ==="
272272
@echo "SHA3: Host $(if $(filter 1,$(MK_HOST_SUPPORTS_SHA3)),✅,❌) Compiler $(if $(filter 1,$(MK_COMPILER_SUPPORTS_SHA3)),✅,❌)"
273+
else ifeq ($(ARCH),riscv64)
274+
@echo "=== RISC-V 64-bit Feature Support ==="
275+
@echo "RVV: Host $(if $(filter 1,$(MK_HOST_SUPPORTS_RVV)),✅,❌) Compiler $(if $(filter 1,$(MK_COMPILER_SUPPORTS_RVV)),✅,❌)"
273276
else
274277
@echo "=== Architecture Not Supported ==="
275278
@echo "No specific feature detection available for $(ARCH)"

test/mk/auto.mk

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ MK_COMPILER_SUPPORTS_SHA3 ?= $(shell echo 'int main() { __asm__("eor3 v0.16b, v1
6767

6868
endif # aarch64 compiler detection
6969

70+
# RISC-V 64-bit compiler feature detection
71+
ifeq ($(ARCH),riscv64)
72+
73+
# Test RVV support using C with inline assembly
74+
MK_COMPILER_SUPPORTS_RVV ?= $(shell echo 'int main() { __asm__("vadd.vv v0, v1, v2"); return 0; }' | $(CC) -march=rv64gcv -x c - -c -o /dev/null 2>/dev/null && echo 1 || echo 0)
75+
76+
endif # riscv64 compiler detection
77+
7078
# Define HOST_PLATFORM if not already defined
7179
HOST_PLATFORM ?= $(shell uname -s)-$(shell uname -m)
7280

@@ -124,6 +132,25 @@ endif # HOST_PLATFORM aarch64
124132

125133
endif # aarch64
126134

135+
# RISC-V 64-bit architecture detection
136+
ifeq ($(ARCH),riscv64)
137+
138+
# Host CPU feature detection for RISC-V 64-bit
139+
ifeq ($(HOST_PLATFORM),Linux-riscv64)
140+
# Linux: Parse ISA string from /proc/cpuinfo
141+
# Format: rv64imafdcv_sscofpmf_... -- extract single-letter extensions
142+
# (before first '_') and check for 'v'
143+
MK_HOST_SUPPORTS_RVV := $(shell sed -n '/^isa/{s/.*rv64//;s/_.*//;p;q}' /proc/cpuinfo 2>/dev/null | grep -q v && echo 1 || echo 0)
144+
else ifneq ($(CROSS_PREFIX),)
145+
# Cross-compilation: assume all features are supported
146+
MK_HOST_SUPPORTS_RVV := 1
147+
else
148+
# Other platforms: assume no support
149+
MK_HOST_SUPPORTS_RVV := 0
150+
endif # HOST_PLATFORM riscv64
151+
152+
endif # riscv64
153+
127154
# Only apply CFLAGS modifications if AUTO=1
128155
ifeq ($(AUTO),1)
129156

@@ -159,7 +186,11 @@ endif # aarch64_be
159186
# RISC-V 64-bit CFLAGS configuration
160187
ifeq ($(ARCH),riscv64)
161188
CFLAGS += -DMLK_FORCE_RISCV64
189+
190+
# Add RVV flags only if both compiler and host support it
191+
ifeq ($(MK_COMPILER_SUPPORTS_RVV)$(MK_HOST_SUPPORTS_RVV),11)
162192
CFLAGS += -march=rv64gcv
193+
endif
163194
endif # riscv64
164195

165196
# RISC-V 32-bit CFLAGS configuration

0 commit comments

Comments
 (0)