| 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 3 | // |
| 4 | // SPDX-License-Identifier: Apache-2.0 |
| 5 | // |
| 6 | |
| 7 | #include "test/common/cpu_info.hpp" |
| 8 | |
| 9 | #include <array> |
| 10 | #include <cctype> |
| 11 | #include <cstddef> |
| 12 | #include <cstdint> |
| 13 | #include <cstdlib> |
| 14 | #include <optional> |
| 15 | #include <string> |
| 16 | #include <string_view> |
| 17 | #include <tuple> |
| 18 | |
| 19 | #include "kai/kai_common.h" |
| 20 | #include "test/common/enum_utils.hpp" |
| 21 | |
| 22 | #if defined(__aarch64__) && defined(__linux__) |
| 23 | #include <sys/auxv.h> |
| 24 | #endif // defined(__aarch64__) && defined(__linux__) |
| 25 | |
| 26 | #if defined(__aarch64__) && defined(__APPLE__) |
| 27 | #include <sys/sysctl.h> |
| 28 | #include <sys/types.h> |
| 29 | #endif // defined(__aarch64__) && defined(__APPLE__) |
| 30 | |
| 31 | #if (defined(__aarch64__) && defined(_WIN64)) || defined(_M_ARM64) |
| 32 | #include <Windows.h> |
| 33 | #include <processthreadsapi.h> |
| 34 | #include <sysinfoapi.h> |
| 35 | #include <winnt.h> |
| 36 | #endif // (defined(__aarch64__) && defined(_WIN64)) || defined(_M_ARM64) |
| 37 | |
| 38 | namespace kai::test { |
| 39 | |
| 40 | namespace { |
| 41 | |
| 42 | enum class CpuFeature : size_t { |
| 43 | ADVSIMD = 0, // |
| 44 | DOTPROD, // |
| 45 | I8MM, // |
| 46 | FP16, // |
| 47 | BF16, // |
| 48 | SVE, // |
| 49 | SVE2, // |
| 50 | SVE2P1, // |
| 51 | SME, // |
| 52 | SME2, // |
| 53 | LAST // This should be last element, please add new CPU capabilities before it |
| 54 | }; |
| 55 | |
| 56 | constexpr std::array<std::tuple<CpuFeature, std::string_view>, n_elements<CpuFeature>()> cpu_features{{ |
| 57 | {CpuFeature::ADVSIMD, "ADVSIMD"}, // |
| 58 | {CpuFeature::DOTPROD, "DOTPROD"}, // |
| 59 | {CpuFeature::I8MM, "I8MM"}, // |
| 60 | {CpuFeature::FP16, "FP16"}, // |
| 61 | {CpuFeature::BF16, "BF16"}, // |
| 62 | {CpuFeature::SVE, "SVE"}, // |
| 63 | {CpuFeature::SVE2, "SVE2"}, // |
| 64 | {CpuFeature::SVE2P1, "SVE2P1"}, // |
| 65 | {CpuFeature::SME, "SME"}, // |
| 66 | {CpuFeature::SME2, "SME2"}, // |
| 67 | }}; |
| 68 | |
| 69 | constexpr const char* forced_cpu_features_env_name = "KAI_TEST_FORCE_CPU_FEATURES"; |
| 70 | |
| 71 | /// Return substring with trimmed leading and trailing whitespaces from input. |
| 72 | std::string_view trim_whitespace(std::string_view value) { |
| 73 | while (!value.empty() && std::isspace(static_cast<unsigned char>(value.front())) != 0) { |
| 74 | value.remove_prefix(1); |
| 75 | } |
| 76 | while (!value.empty() && std::isspace(static_cast<unsigned char>(value.back())) != 0) { |
| 77 | value.remove_suffix(1); |
| 78 | } |
| 79 | return value; |
| 80 | } |
| 81 | |
| 82 | /// Get description of available features |
| 83 | std::string available_features() { |
| 84 | std::string names; |
| 85 | |
| 86 | for (size_t i = 0; i < cpu_features.size(); ++i) { |
| 87 | if (i != 0) { |
| 88 | names += ", "; |
| 89 | } |
| 90 | names += std::get<1>(cpu_features[i]); |
| 91 | } |
| 92 | |
| 93 | return names; |
| 94 | } |
| 95 | |
| 96 | /// Read and tokenize the forced features |
| 97 | std::optional<std::array<bool, n_elements<CpuFeature>()>> parse_forced_cpu_features() { |
| 98 | const char* env = std::getenv(forced_cpu_features_env_name); |
| 99 | if (env == nullptr) { |
| 100 | return std::nullopt; |
| 101 | } |
| 102 | |
| 103 | puts("WARNING: Reading enabled CPU features from KAI_TEST_FORCE_CPU_FEATURES"); |
| 104 | std::array<bool, n_elements<CpuFeature>()> features{}; |
| 105 | std::string_view env_view{env}; |
| 106 | size_t token_start = 0; |
| 107 | |
| 108 | while (token_start <= env_view.size()) { |
| 109 | size_t token_end = env_view.find(',', token_start); |
| 110 | if (token_end == std::string_view::npos) { |
| 111 | token_end = env_view.size(); |
| 112 | } |
| 113 | |
| 114 | const std::string_view token = trim_whitespace(env_view.substr(token_start, token_end - token_start)); |
| 115 | if (!token.empty()) { |
| 116 | bool matched = false; |
| 117 | |
| 118 | for (const auto& [feature, name] : cpu_features) { |
| 119 | if (token == name) { |
| 120 | features[as_idx(feature)] = true; |
| 121 | matched = true; |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // There was an invalid token, report error |
| 127 | if (!matched) { |
| 128 | std::string error_message = "Unrecognized CPU feature provided by "; |
| 129 | error_message += forced_cpu_features_env_name; |
| 130 | error_message += ": "; |
| 131 | error_message += token; |
| 132 | error_message += ". Supported features: "; |
| 133 | error_message += available_features(); |
| 134 | KAI_ERROR(error_message.c_str()); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | if (token_end == env_view.size()) { |
| 139 | break; |
| 140 | } |
| 141 | token_start = token_end + 1; |
| 142 | } |
| 143 | |
| 144 | return features; |
| 145 | } |
| 146 | |
| 147 | const std::optional<std::array<bool, n_elements<CpuFeature>()>>& forced_cpu_features() { |
| 148 | static const std::optional<std::array<bool, n_elements<CpuFeature>()>> forced = parse_forced_cpu_features(); |
| 149 | return forced; |
| 150 | } |
| 151 | |
| 152 | #if defined(__aarch64__) && defined(__linux__) |
| 153 | /// Define CPU capabilities not available in toolchain definitions |
| 154 | #ifndef HWCAP_ASIMD |
| 155 | constexpr uint64_t HWCAP_ASIMD = 1UL << 1; |
| 156 | #endif |
| 157 | #ifndef HWCAP_FPHP |
| 158 | constexpr uint64_t HWCAP_FPHP = 1UL << 9; |
| 159 | #endif |
| 160 | #ifndef HWCAP_ASIMDHP |
| 161 | constexpr uint64_t HWCAP_ASIMDHP = 1UL << 10; |
| 162 | #endif |
| 163 | #ifndef HWCAP_ASIMDDP |
| 164 | constexpr uint64_t HWCAP_ASIMDDP = 1UL << 20; |
| 165 | #endif |
| 166 | #ifndef HWCAP_SVE |
| 167 | constexpr uint64_t HWCAP_SVE = 1UL << 22; |
| 168 | #endif |
| 169 | #ifndef HWCAP2_SVE2 |
| 170 | constexpr uint64_t HWCAP2_SVE2 = 1UL << 1; |
| 171 | #endif |
| 172 | #ifndef HWCAP2_SVE2P1 |
| 173 | constexpr uint64_t HWCAP2_SVE2P1 = 1UL << 36; |
| 174 | #endif |
| 175 | #ifndef HWCAP2_I8MM |
| 176 | constexpr uint64_t HWCAP2_I8MM = 1UL << 13; |
| 177 | #endif |
| 178 | #ifndef HWCAP2_BF16 |
| 179 | constexpr uint64_t HWCAP2_BF16 = 1UL << 14; |
| 180 | #endif |
| 181 | #ifndef HWCAP2_SME |
| 182 | constexpr uint64_t HWCAP2_SME = 1UL << 23; |
| 183 | #endif |
| 184 | #ifndef HWCAP2_SME2 |
| 185 | constexpr uint64_t HWCAP2_SME2 = 1UL << 37; |
| 186 | #endif |
| 187 | |
| 188 | const std::array<std::tuple<CpuFeature, uint64_t, uint64_t>, n_elements<CpuFeature>()> cpu_caps{{ |
| 189 | {CpuFeature::ADVSIMD, AT_HWCAP, HWCAP_ASIMD}, // |
| 190 | {CpuFeature::DOTPROD, AT_HWCAP, HWCAP_ASIMDDP}, // |
| 191 | {CpuFeature::I8MM, AT_HWCAP2, HWCAP2_I8MM}, // |
| 192 | {CpuFeature::FP16, AT_HWCAP, HWCAP_FPHP | HWCAP_ASIMDHP}, // |
| 193 | {CpuFeature::BF16, AT_HWCAP2, HWCAP2_BF16}, // |
| 194 | {CpuFeature::SVE, AT_HWCAP, HWCAP_SVE}, // |
| 195 | {CpuFeature::SVE2, AT_HWCAP2, HWCAP2_SVE2}, // |
| 196 | {CpuFeature::SVE2P1, AT_HWCAP2, HWCAP2_SVE2P1}, // |
| 197 | {CpuFeature::SME, AT_HWCAP2, HWCAP2_SME}, // |
| 198 | {CpuFeature::SME2, AT_HWCAP2, HWCAP2_SME2}, // |
| 199 | }}; |
| 200 | |
| 201 | bool get_cap_support(CpuFeature feature) { |
| 202 | const size_t feature_idx = static_cast<size_t>(as_idx(feature)); |
| 203 | KAI_ASSUME_ALWAYS(feature_idx < cpu_caps.size()); |
| 204 | |
| 205 | if (const auto& forced = forced_cpu_features(); forced.has_value()) { |
| 206 | return (*forced)[feature_idx]; |
| 207 | } |
| 208 | |
| 209 | auto [cpu_feature, cap_id, cap_bits] = cpu_caps[feature_idx]; |
| 210 | // Make sure CPU feature is correctly initialized |
| 211 | KAI_ASSERT_ALWAYS(feature == cpu_feature); |
| 212 | |
| 213 | const uint64_t hwcaps = getauxval(cap_id); |
| 214 | |
| 215 | return (hwcaps & cap_bits) == cap_bits; |
| 216 | } |
| 217 | #elif defined(__aarch64__) && defined(__APPLE__) |
| 218 | const std::array<std::tuple<CpuFeature, std::string_view>, n_elements<CpuFeature>()> cpu_caps{{ |
| 219 | {CpuFeature::ADVSIMD, "hw.optional.arm64"}, // Advanced SIMD is always present on arm64 |
| 220 | {CpuFeature::DOTPROD, "hw.optional.arm.FEAT_DotProd"}, |
| 221 | {CpuFeature::I8MM, "hw.optional.arm.FEAT_I8MM"}, |
| 222 | {CpuFeature::FP16, "hw.optional.arm.FEAT_FP16"}, |
| 223 | {CpuFeature::BF16, "hw.optional.arm.FEAT_BF16"}, |
| 224 | {CpuFeature::SVE, ""}, // not supported |
| 225 | {CpuFeature::SVE2, ""}, // not supported |
| 226 | {CpuFeature::SVE2P1, ""}, // not supported |
| 227 | {CpuFeature::SME, "hw.optional.arm.FEAT_SME"}, |
| 228 | {CpuFeature::SME2, "hw.optional.arm.FEAT_SME2"}, |
| 229 | }}; |
| 230 | |
| 231 | bool get_cap_support(CpuFeature feature) { |
| 232 | const size_t feature_idx = as_idx(feature); |
| 233 | KAI_ASSUME_ALWAYS(feature_idx < cpu_caps.size()); |
| 234 | |
| 235 | if (const auto& forced = forced_cpu_features(); forced.has_value()) { |
| 236 | return (*forced)[feature_idx]; |
| 237 | } |
| 238 | |
| 239 | auto [cpu_feature, cap_name] = cpu_caps[feature_idx]; |
| 240 | KAI_ASSERT_ALWAYS(feature == cpu_feature); |
| 241 | |
| 242 | uint32_t value{}; |
| 243 | |
| 244 | if (cap_name.length() > 0) { |
| 245 | size_t size = sizeof(value); |
| 246 | |
| 247 | KAI_ASSERT_ALWAYS(sysctlbyname(cap_name.data(), nullptr, &size, nullptr, 0) == 0); |
| 248 | KAI_ASSERT_ALWAYS(size == sizeof(value)); |
| 249 | |
| 250 | [[maybe_unused]] int status = sysctlbyname(cap_name.data(), &value, &size, nullptr, 0); |
| 251 | KAI_ASSERT_ALWAYS(status == 0); |
| 252 | } |
| 253 | |
| 254 | return value == 1; |
| 255 | } |
| 256 | #elif (defined(__aarch64__) && defined(_WIN64)) || defined(_M_ARM64) |
| 257 | // Some system registers are provided in HARDWARE\DESCRIPTION\System\CentralProcessor\* registry. |
| 258 | // |
| 259 | // The registry name is encoded as |
| 260 | // CP {op0 & 1, op1, CRn, CRm, op2} |
| 261 | // |
| 262 | // These can be used to detect architectural features that are unable to detect reliably |
| 263 | // using IsProcessorFeaturePresent. It must not be used to detect architectural features |
| 264 | // that require operating system support such as SVE and SME. |
| 265 | const char* ID_AA64PFR0_EL1 = "CP 4020"; |
| 266 | const char* ID_AA64ISAR1_EL1 = "CP 4031"; |
| 267 | |
| 268 | const std::array<std::tuple<CpuFeature, DWORD, const char*, uint64_t>, n_elements<CpuFeature>()> cpu_caps{{ |
| 269 | {CpuFeature::ADVSIMD, PF_ARM_NEON_INSTRUCTIONS_AVAILABLE, nullptr, 0}, |
| 270 | {CpuFeature::DOTPROD, PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE, nullptr, 0}, |
| 271 | {CpuFeature::I8MM, 0, ID_AA64ISAR1_EL1, 0x00f0000000000000ULL}, |
| 272 | {CpuFeature::FP16, 0, ID_AA64PFR0_EL1, 0x00000000000f0000ULL}, |
| 273 | {CpuFeature::BF16, 0, ID_AA64ISAR1_EL1, 0x0000f00000000000ULL}, |
| 274 | {CpuFeature::SVE, 46, nullptr, 0}, |
| 275 | {CpuFeature::SVE2, 47, nullptr, 0}, |
| 276 | {CpuFeature::SVE2P1, 0, nullptr, 0}, |
| 277 | {CpuFeature::SME, 0, nullptr, 0}, |
| 278 | {CpuFeature::SME2, 0, nullptr, 0}, |
| 279 | }}; |
| 280 | |
| 281 | uint64_t read_sysreg(const char* name) { |
| 282 | uint64_t value = 0; |
| 283 | DWORD size = sizeof(value); |
| 284 | |
| 285 | const LSTATUS status = RegGetValueA( |
| 286 | HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", name, RRF_RT_REG_QWORD, nullptr, |
| 287 | &value, &size); |
| 288 | |
| 289 | KAI_ASSERT_ALWAYS(status == ERROR_SUCCESS); |
| 290 | |
| 291 | return value; |
| 292 | } |
| 293 | |
| 294 | bool get_cap_support(CpuFeature feature) { |
| 295 | const size_t feature_idx = as_idx(feature); |
| 296 | KAI_ASSUME_ALWAYS(feature_idx < cpu_caps.size()); |
| 297 | |
| 298 | if (const auto& forced = forced_cpu_features(); forced.has_value()) { |
| 299 | return (*forced)[feature_idx]; |
| 300 | } |
| 301 | auto [cpu_feature, cap_id, reg_name, reg_mask] = cpu_caps[feature_idx]; |
| 302 | |
| 303 | if (cap_id != 0) { |
| 304 | return IsProcessorFeaturePresent(cap_id); |
| 305 | } |
| 306 | |
| 307 | if (reg_name != nullptr) { |
| 308 | const uint64_t value = read_sysreg(reg_name); |
| 309 | const bool is_aarch64 = IsProcessorFeaturePresent(PF_ARM_V8_INSTRUCTIONS_AVAILABLE); |
| 310 | const bool has_feature = (value & reg_mask) != 0; |
| 311 | |
| 312 | return is_aarch64 && has_feature; |
| 313 | } |
| 314 | |
| 315 | return false; |
| 316 | } |
| 317 | #elif defined(__aarch64__) |
| 318 | #error Please add a way how to check implemented CPU features |
| 319 | #else |
| 320 | bool get_cap_support(CpuFeature feature) { |
| 321 | const size_t feature_idx = as_idx(feature); |
| 322 | |
| 323 | if (const auto& forced = forced_cpu_features(); forced.has_value()) { |
| 324 | return (*forced)[feature_idx]; |
| 325 | } |
| 326 | |
| 327 | KAI_UNUSED(feature); |
| 328 | return false; |
| 329 | } |
| 330 | #endif |
| 331 | |
| 332 | /// Information about the CPU that is executing the program. |
| 333 | struct CpuInfo { |
| 334 | CpuInfo() : |
| 335 | has_advsimd(get_cap_support(CpuFeature::ADVSIMD)), |
| 336 | has_dotprod(get_cap_support(CpuFeature::DOTPROD)), |
| 337 | has_i8mm(get_cap_support(CpuFeature::I8MM)), |
| 338 | has_fp16(get_cap_support(CpuFeature::FP16)), |
| 339 | has_bf16(get_cap_support(CpuFeature::BF16)), |
| 340 | has_sve(get_cap_support(CpuFeature::SVE)), |
| 341 | has_sve2(get_cap_support(CpuFeature::SVE2)), |
| 342 | has_sve2p1(get_cap_support(CpuFeature::SVE2P1)), |
| 343 | has_sme(get_cap_support(CpuFeature::SME)), |
| 344 | has_sme2(get_cap_support(CpuFeature::SME2)) { |
| 345 | } |
| 346 | |
| 347 | /// Gets the singleton @ref CpuInfo object. |
| 348 | static const CpuInfo& current() { |
| 349 | static const CpuInfo cpu_info{}; |
| 350 | return cpu_info; |
| 351 | } |
| 352 | |
| 353 | const bool has_advsimd{}; ///< AdvSIMD is supported. |
| 354 | const bool has_dotprod{}; ///< DotProd is supported. |
| 355 | const bool has_i8mm{}; ///< I8MM is supported. |
| 356 | const bool has_fp16{}; ///< FP16 is supported. |
| 357 | const bool has_bf16{}; ///< B16 is supported. |
| 358 | const bool has_sve{}; ///< SVE is supported. |
| 359 | const bool has_sve2{}; ///< SVE2 is supported. |
| 360 | const bool has_sve2p1{}; ///< SVE2.1 is supported. |
| 361 | const bool has_sme{}; ///< SME is supported. |
| 362 | const bool has_sme2{}; ///< SME2 is supported. |
| 363 | }; |
| 364 | |
| 365 | } // namespace |
| 366 | |
| 367 | /// Helper functions |
| 368 | bool cpu_has_advsimd() { |
| 369 | return CpuInfo::current().has_advsimd; |
| 370 | } |
| 371 | |
| 372 | bool cpu_has_dotprod() { |
| 373 | return CpuInfo::current().has_dotprod; |
| 374 | } |
| 375 | |
| 376 | bool cpu_has_dotprod_and_fp16() { |
| 377 | return cpu_has_dotprod() && cpu_has_fp16(); |
| 378 | } |
| 379 | |
| 380 | bool cpu_has_i8mm() { |
| 381 | return CpuInfo::current().has_i8mm; |
| 382 | } |
| 383 | |
| 384 | bool cpu_has_i8mm_and_fp16() { |
| 385 | return cpu_has_i8mm() && cpu_has_fp16(); |
| 386 | } |
| 387 | |
| 388 | bool cpu_has_fp16() { |
| 389 | return CpuInfo::current().has_fp16; |
| 390 | } |
| 391 | |
| 392 | bool cpu_has_bf16() { |
| 393 | return CpuInfo::current().has_bf16; |
| 394 | } |
| 395 | |
| 396 | bool cpu_has_sve() { |
| 397 | return CpuInfo::current().has_sve; |
| 398 | } |
| 399 | |
| 400 | bool cpu_has_sve_vl256() { |
| 401 | if (CpuInfo::current().has_sve) { |
| 402 | return (kai_get_sve_vector_length_u8() == 32); |
| 403 | } |
| 404 | return false; |
| 405 | } |
| 406 | |
| 407 | bool cpu_has_sve2() { |
| 408 | return CpuInfo::current().has_sve2; |
| 409 | } |
| 410 | |
| 411 | bool cpu_has_sve2p1() { |
| 412 | return CpuInfo::current().has_sve2p1; |
| 413 | } |
| 414 | |
| 415 | bool cpu_has_sme() { |
| 416 | return CpuInfo::current().has_sme; |
| 417 | } |
| 418 | |
| 419 | bool cpu_has_sme2() { |
| 420 | return CpuInfo::current().has_sme2; |
| 421 | } |
| 422 | |
| 423 | bool cpu_has_dotprod_and_bf16() { |
| 424 | return cpu_has_dotprod() && cpu_has_bf16(); |
| 425 | } |
| 426 | |
| 427 | bool cpu_has_i8mm_and_bf16() { |
| 428 | return cpu_has_i8mm() && cpu_has_bf16(); |
| 429 | } |
| 430 | |
| 431 | } // namespace kai::test |