Skip to content

Commit

Permalink
Add a API to check SVE Length support on ARM CPU. (pytorch#255)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
maajidkhann authored Aug 7, 2024
1 parent ca67895 commit 16bfc16
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
10 changes: 10 additions & 0 deletions include/cpuinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -1670,6 +1670,7 @@ struct cpuinfo_arm_isa {
bool sve;
bool sve2;
bool i8mm;
uint32_t svelen;
#endif
bool rdm;
bool fp16arith;
Expand Down Expand Up @@ -2042,6 +2043,15 @@ static inline bool cpuinfo_has_arm_sve2(void) {
#endif
}

// Function to get the max SVE vector length on ARM CPU's which support SVE.
static inline uint32_t cpuinfo_get_max_arm_sve_length(void) {
#if CPUINFO_ARCH_ARM64
return cpuinfo_isa.svelen * 8; // bytes * 8 = bit length(vector length)
#else
return 0;
#endif
}

#if CPUINFO_ARCH_RISCV32 || CPUINFO_ARCH_RISCV64
/* This structure is not a part of stable API. Use cpuinfo_has_riscv_* functions
* instead. */
Expand Down
19 changes: 19 additions & 0 deletions src/arm/linux/aarch64-isa.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <arm/linux/api.h>
#include <cpuinfo/log.h>

#include <sys/prctl.h>

void cpuinfo_arm64_linux_decode_isa_from_proc_cpuinfo(
uint32_t features,
uint32_t features2,
Expand Down Expand Up @@ -151,4 +153,21 @@ void cpuinfo_arm64_linux_decode_isa_from_proc_cpuinfo(
if (features & CPUINFO_ARM_LINUX_FEATURE_ASIMDFHM) {
isa->fhm = true;
}

#ifndef PR_SVE_GET_VL
#define PR_SVE_GET_VL 51
#endif

#ifndef PR_SVE_VL_LEN_MASK
#define PR_SVE_VL_LEN_MASK 0xffff
#endif

int ret = prctl(PR_SVE_GET_VL);
if (ret < 0) {
cpuinfo_log_error("prctl(PR_SVE_GET_VL) failed");
isa->svelen = 0; // Assume no SVE support if the call fails
} else {
// Mask out the SVE vector length bits
isa->svelen = ret & PR_SVE_VL_LEN_MASK;
}
}
3 changes: 3 additions & 0 deletions tools/isa-info.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ int main(int argc, char** argv) {
printf("\tARM SVE: %s\n", cpuinfo_has_arm_sve() ? "yes" : "no");
printf("\tARM SVE 2: %s\n", cpuinfo_has_arm_sve2() ? "yes" : "no");

printf("ARM SVE Capabilities:\n");
printf("\tSVE max length: %d\n", cpuinfo_get_max_arm_sve_length());

printf("Cryptography extensions:\n");
printf("\tAES: %s\n", cpuinfo_has_arm_aes() ? "yes" : "no");
printf("\tSHA1: %s\n", cpuinfo_has_arm_sha1() ? "yes" : "no");
Expand Down

0 comments on commit 16bfc16

Please sign in to comment.