Skip to content

Commit 16bfc16

Browse files
authored
Add a API to check SVE Length support on ARM CPU. (#255)
This pull request introduces a new feature to the cpuinfo library that adds an API to return the maximum supported Scalable Vector Extension (SVE) vector length on the given ARM CPU. This enhancement will allow users to query and determine the maximum SVE vector lengths on a given ARM CPU, providing better insights and flexibility for optimizing applications that utilize SVE. **Key Features:** **New API Function:** Introduces a single API function - cpuinfo_get_max_arm_sve_length() that returns the maximum SVE Vector Length supported on the given ARM CPU. The function is designed to be easy to integrate with existing code in other projects like PyTorch (pytorch/pytorch#119571) and provides a straightforward interface for querying SVE VL. **Here's the sample output on SVE supported instance:** **Query:** ![test_cpp_aug7](https://github.com/user-attachments/assets/0f55fa37-cf54-4fbf-b1bc-a34a27139869) **Output on SVE256 supported Hardware - Graviton3:** ![test_cpp_output](https://github.com/user-attachments/assets/bef72357-dadd-43e9-8983-33248205782f) Signed-off-by: maajidkhann <[email protected]>
1 parent ca67895 commit 16bfc16

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

include/cpuinfo.h

+10
Original file line numberDiff line numberDiff line change
@@ -1670,6 +1670,7 @@ struct cpuinfo_arm_isa {
16701670
bool sve;
16711671
bool sve2;
16721672
bool i8mm;
1673+
uint32_t svelen;
16731674
#endif
16741675
bool rdm;
16751676
bool fp16arith;
@@ -2042,6 +2043,15 @@ static inline bool cpuinfo_has_arm_sve2(void) {
20422043
#endif
20432044
}
20442045

2046+
// Function to get the max SVE vector length on ARM CPU's which support SVE.
2047+
static inline uint32_t cpuinfo_get_max_arm_sve_length(void) {
2048+
#if CPUINFO_ARCH_ARM64
2049+
return cpuinfo_isa.svelen * 8; // bytes * 8 = bit length(vector length)
2050+
#else
2051+
return 0;
2052+
#endif
2053+
}
2054+
20452055
#if CPUINFO_ARCH_RISCV32 || CPUINFO_ARCH_RISCV64
20462056
/* This structure is not a part of stable API. Use cpuinfo_has_riscv_* functions
20472057
* instead. */

src/arm/linux/aarch64-isa.c

+19
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <arm/linux/api.h>
44
#include <cpuinfo/log.h>
55

6+
#include <sys/prctl.h>
7+
68
void cpuinfo_arm64_linux_decode_isa_from_proc_cpuinfo(
79
uint32_t features,
810
uint32_t features2,
@@ -151,4 +153,21 @@ void cpuinfo_arm64_linux_decode_isa_from_proc_cpuinfo(
151153
if (features & CPUINFO_ARM_LINUX_FEATURE_ASIMDFHM) {
152154
isa->fhm = true;
153155
}
156+
157+
#ifndef PR_SVE_GET_VL
158+
#define PR_SVE_GET_VL 51
159+
#endif
160+
161+
#ifndef PR_SVE_VL_LEN_MASK
162+
#define PR_SVE_VL_LEN_MASK 0xffff
163+
#endif
164+
165+
int ret = prctl(PR_SVE_GET_VL);
166+
if (ret < 0) {
167+
cpuinfo_log_error("prctl(PR_SVE_GET_VL) failed");
168+
isa->svelen = 0; // Assume no SVE support if the call fails
169+
} else {
170+
// Mask out the SVE vector length bits
171+
isa->svelen = ret & PR_SVE_VL_LEN_MASK;
172+
}
154173
}

tools/isa-info.c

+3
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ int main(int argc, char** argv) {
172172
printf("\tARM SVE: %s\n", cpuinfo_has_arm_sve() ? "yes" : "no");
173173
printf("\tARM SVE 2: %s\n", cpuinfo_has_arm_sve2() ? "yes" : "no");
174174

175+
printf("ARM SVE Capabilities:\n");
176+
printf("\tSVE max length: %d\n", cpuinfo_get_max_arm_sve_length());
177+
175178
printf("Cryptography extensions:\n");
176179
printf("\tAES: %s\n", cpuinfo_has_arm_aes() ? "yes" : "no");
177180
printf("\tSHA1: %s\n", cpuinfo_has_arm_sha1() ? "yes" : "no");

0 commit comments

Comments
 (0)