From 639f0f665b4346d287014adc99e14a8497aadf18 Mon Sep 17 00:00:00 2001 From: Pablo Marquez Tello Date: Fri, 21 Jan 2022 15:25:27 +0000 Subject: Fix CPU detection code on Macos * Query the number of cpus on the system via sysctl and set CpuInfo accordingly * Query support for fp16 via sysctlbyname and use this to update isainfo * Resolves MLCE-746 Change-Id: I4ad802435c21ee238bff0344af1d7a1ce2479ac5 Signed-off-by: Pablo Marquez Tello Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/6989 Reviewed-by: Gunes Bayir Reviewed-by: SiCong Li Tested-by: Arm Jenkins Comments-Addressed: Arm Jenkins --- src/common/cpuinfo/CpuInfo.cpp | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/common/cpuinfo/CpuInfo.cpp b/src/common/cpuinfo/CpuInfo.cpp index 32504acc44..b96e108af5 100644 --- a/src/common/cpuinfo/CpuInfo.cpp +++ b/src/common/cpuinfo/CpuInfo.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Arm Limited. + * Copyright (c) 2021-2022 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -43,6 +43,9 @@ #if !defined(BARE_METAL) && !defined(__APPLE__) && (defined(__arm__) || defined(__aarch64__)) #include /* Get HWCAP bits from asm/hwcap.h */ #include +#elif defined(__APPLE__) && defined(__aarch64__) +#include +#include #endif /* !defined(BARE_METAL) && !defined(__APPLE__) && (defined(__arm__) || defined(__aarch64__)) */ #define ARM_COMPUTE_CPU_FEATURE_HWCAP_CPUID (1 << 11) @@ -258,7 +261,19 @@ int get_max_cpus() } return max_cpus; } -#endif /* !defined(BARE_METAL) && !defined(__APPLE__) && (defined(__arm__) || defined(__aarch64__)) */ +#elif defined(__aarch64__) && defined(__APPLE__) /* !defined(BARE_METAL) && !defined(__APPLE__) && (defined(__arm__) || defined(__aarch64__)) */ +/** Query features through sysctlbyname + * + * @return int value queried + */ +int get_hw_capability(const std::string& cap) +{ + int64_t result(0); + size_t size = sizeof(result); + sysctlbyname(cap.c_str(), &result, &size, NULL, 0); + return result; +} +#endif /* defined(__aarch64__) && defined(__APPLE__) */ #if defined(BARE_METAL) && defined(__aarch64__) uint64_t get_sve_feature_reg() @@ -330,10 +345,19 @@ CpuInfo CpuInfo::build() std::vector cpus_model(1, midr_to_model(midr)); CpuInfo info(isa, cpus_model); return info; -#else /* #elif(BARE_METAL) && defined(__aarch64__) */ +#elif defined(__aarch64__) && defined(__APPLE__) /* #elif(BARE_METAL) && defined(__aarch64__) */ + int ncpus = get_hw_capability("hw.logicalcpu"); + CpuIsaInfo isainfo; + std::vector cpus_model(ncpus); + isainfo.neon = get_hw_capability("hw.optional.neon"); + isainfo.fp16 = get_hw_capability("hw.optional.neon_fp16"); + isainfo.dot = get_hw_capability("hw.optional.arm.FEAT_DotProd"); + CpuInfo info(isainfo,cpus_model); + return info; +#else /* #elif defined(__aarch64__) && defined(__APPLE__) */ CpuInfo info(CpuIsaInfo(), { CpuModel::GENERIC }); return info; -#endif /* !defined(BARE_METAL) && !defined(__APPLE__) && (defined(__arm__) || defined(__aarch64__)) */ +#endif /* !defined(BARE_METAL) && !defined(__APPLE__) && (defined(__arm__) || defined(__aarch64__)) */ } CpuModel CpuInfo::cpu_model(uint32_t cpuid) const @@ -415,4 +439,4 @@ uint32_t num_threads_hint() return num_threads_hint; } } // namespace cpuinfo -} // namespace arm_compute \ No newline at end of file +} // namespace arm_compute -- cgit v1.2.1