diff --git a/clang/lib/Driver/ToolChains/Arch/ARM.cpp b/clang/lib/Driver/ToolChains/Arch/ARM.cpp index 8ae22cc37a136..a6041b809b80b 100644 --- a/clang/lib/Driver/ToolChains/Arch/ARM.cpp +++ b/clang/lib/Driver/ToolChains/Arch/ARM.cpp @@ -591,11 +591,9 @@ llvm::ARM::FPUKind arm::getARMTargetFeatures(const Driver &D, // Add CPU features for generic CPUs if (CPUName == "native") { - llvm::StringMap HostFeatures; - if (llvm::sys::getHostCPUFeatures(HostFeatures)) - for (auto &F : HostFeatures) - Features.push_back( - Args.MakeArgString((F.second ? "+" : "-") + F.first())); + for (auto &F : llvm::sys::getHostCPUFeatures()) + Features.push_back( + Args.MakeArgString((F.second ? "+" : "-") + F.first())); } else if (!CPUName.empty()) { // This sets the default features for the specified CPU. We certainly don't // want to override the features that have been explicitly specified on the diff --git a/clang/lib/Driver/ToolChains/Arch/X86.cpp b/clang/lib/Driver/ToolChains/Arch/X86.cpp index 92821b2a82dae..9fca7864b2546 100644 --- a/clang/lib/Driver/ToolChains/Arch/X86.cpp +++ b/clang/lib/Driver/ToolChains/Arch/X86.cpp @@ -131,11 +131,9 @@ void x86::getX86TargetFeatures(const Driver &D, const llvm::Triple &Triple, // If -march=native, autodetect the feature list. if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_march_EQ)) { if (StringRef(A->getValue()) == "native") { - llvm::StringMap HostFeatures; - if (llvm::sys::getHostCPUFeatures(HostFeatures)) - for (auto &F : HostFeatures) - Features.push_back( - Args.MakeArgString((F.second ? "+" : "-") + F.first())); + for (auto &F : llvm::sys::getHostCPUFeatures()) + Features.push_back( + Args.MakeArgString((F.second ? "+" : "-") + F.first())); } } diff --git a/lldb/utils/lit-cpuid/lit-cpuid.cpp b/lldb/utils/lit-cpuid/lit-cpuid.cpp index be322cb6aa42a..55f6dfd8ea906 100644 --- a/lldb/utils/lit-cpuid/lit-cpuid.cpp +++ b/lldb/utils/lit-cpuid/lit-cpuid.cpp @@ -20,16 +20,15 @@ using namespace llvm; int main(int argc, char **argv) { #if defined(__i386__) || defined(_M_IX86) || \ defined(__x86_64__) || defined(_M_X64) - StringMap features; - - if (!sys::getHostCPUFeatures(features)) + const StringMap features = sys::getHostCPUFeatures(); + if (features.empty()) return 1; - if (features["sse"]) + if (features.lookup("sse")) outs() << "sse\n"; - if (features["avx"]) + if (features.lookup("avx")) outs() << "avx\n"; - if (features["avx512f"]) + if (features.lookup("avx512f")) outs() << "avx512f\n"; #endif diff --git a/llvm/include/llvm/TargetParser/Host.h b/llvm/include/llvm/TargetParser/Host.h index af72045a8fe67..a639b093f21bd 100644 --- a/llvm/include/llvm/TargetParser/Host.h +++ b/llvm/include/llvm/TargetParser/Host.h @@ -47,13 +47,12 @@ namespace sys { /// The particular format of the names are target dependent, and suitable for /// passing as -mattr to the target which matches the host. /// - /// \param Features - A string mapping feature names to either - /// true (if enabled) or false (if disabled). This routine makes no guarantees - /// about exactly which features may appear in this map, except that they are - /// all valid LLVM feature names. - /// - /// \return - True on success. - bool getHostCPUFeatures(StringMap &Features); + /// \return - A string map mapping feature names to either true (if enabled) + /// or false (if disabled). This routine makes no guarantees about exactly + /// which features may appear in this map, except that they are all valid LLVM + /// feature names. The map can be empty, for example if feature detection + /// fails. + const StringMap getHostCPUFeatures(); /// This is a function compatible with cl::AddExtraVersionPrinter, which adds /// info about the current target triple and detected CPU. diff --git a/llvm/lib/CodeGen/CommandFlags.cpp b/llvm/lib/CodeGen/CommandFlags.cpp index 8fc65d78ff2c9..9e42deb94903d 100644 --- a/llvm/lib/CodeGen/CommandFlags.cpp +++ b/llvm/lib/CodeGen/CommandFlags.cpp @@ -624,12 +624,9 @@ std::string codegen::getFeaturesStr() { // This is necessary for x86 where the CPU might not support all the // features the autodetected CPU name lists in the target. For example, // not all Sandybridge processors support AVX. - if (getMCPU() == "native") { - StringMap HostFeatures; - if (sys::getHostCPUFeatures(HostFeatures)) - for (const auto &[Feature, IsEnabled] : HostFeatures) - Features.AddFeature(Feature, IsEnabled); - } + if (getMCPU() == "native") + for (const auto &[Feature, IsEnabled] : sys::getHostCPUFeatures()) + Features.AddFeature(Feature, IsEnabled); for (auto const &MAttr : getMAttrs()) Features.AddFeature(MAttr); @@ -644,12 +641,9 @@ std::vector codegen::getFeatureList() { // This is necessary for x86 where the CPU might not support all the // features the autodetected CPU name lists in the target. For example, // not all Sandybridge processors support AVX. - if (getMCPU() == "native") { - StringMap HostFeatures; - if (sys::getHostCPUFeatures(HostFeatures)) - for (const auto &[Feature, IsEnabled] : HostFeatures) - Features.AddFeature(Feature, IsEnabled); - } + if (getMCPU() == "native") + for (const auto &[Feature, IsEnabled] : sys::getHostCPUFeatures()) + Features.AddFeature(Feature, IsEnabled); for (auto const &MAttr : getMAttrs()) Features.AddFeature(MAttr); diff --git a/llvm/lib/ExecutionEngine/Orc/JITTargetMachineBuilder.cpp b/llvm/lib/ExecutionEngine/Orc/JITTargetMachineBuilder.cpp index 540728ea0fd99..8d4e79c7d8aff 100644 --- a/llvm/lib/ExecutionEngine/Orc/JITTargetMachineBuilder.cpp +++ b/llvm/lib/ExecutionEngine/Orc/JITTargetMachineBuilder.cpp @@ -28,9 +28,7 @@ Expected JITTargetMachineBuilder::detectHost() { // Retrieve host CPU name and sub-target features and add them to builder. // Relocation model, code model and codegen opt level are kept to default // values. - llvm::StringMap FeatureMap; - llvm::sys::getHostCPUFeatures(FeatureMap); - for (auto &Feature : FeatureMap) + for (const auto &Feature : llvm::sys::getHostCPUFeatures()) TMBuilder.getFeatures().AddFeature(Feature.first(), Feature.second); TMBuilder.setCPU(std::string(llvm::sys::getHostCPUName())); diff --git a/llvm/lib/Target/TargetMachineC.cpp b/llvm/lib/Target/TargetMachineC.cpp index 80024f9a6d5df..d12fc65047d04 100644 --- a/llvm/lib/Target/TargetMachineC.cpp +++ b/llvm/lib/Target/TargetMachineC.cpp @@ -363,11 +363,8 @@ char *LLVMGetHostCPUName(void) { char *LLVMGetHostCPUFeatures(void) { SubtargetFeatures Features; - StringMap HostFeatures; - - if (sys::getHostCPUFeatures(HostFeatures)) - for (const auto &[Feature, IsEnabled] : HostFeatures) - Features.AddFeature(Feature, IsEnabled); + for (const auto &[Feature, IsEnabled] : sys::getHostCPUFeatures()) + Features.AddFeature(Feature, IsEnabled); return strdup(Features.getString().c_str()); } diff --git a/llvm/lib/TargetParser/Host.cpp b/llvm/lib/TargetParser/Host.cpp index 2ea56746aff24..7e36c4867245d 100644 --- a/llvm/lib/TargetParser/Host.cpp +++ b/llvm/lib/TargetParser/Host.cpp @@ -1710,12 +1710,13 @@ VendorSignatures getVendorSignature(unsigned *MaxLeaf) { #if defined(__i386__) || defined(_M_IX86) || \ defined(__x86_64__) || defined(_M_X64) -bool sys::getHostCPUFeatures(StringMap &Features) { +const StringMap sys::getHostCPUFeatures() { unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0; unsigned MaxLevel; + StringMap Features; if (getX86CpuIDAndInfo(0, &MaxLevel, &EBX, &ECX, &EDX) || MaxLevel < 1) - return false; + return Features; getX86CpuIDAndInfo(1, &EAX, &EBX, &ECX, &EDX); @@ -1903,13 +1904,14 @@ bool sys::getHostCPUFeatures(StringMap &Features) { Features["avx10.1-512"] = Features["avx10.1-256"] && HasLeaf24 && ((EBX >> 18) & 1); - return true; + return Features; } #elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__)) -bool sys::getHostCPUFeatures(StringMap &Features) { +const StringMap sys::getHostCPUFeatures() { + StringMap Features; std::unique_ptr P = getProcCpuinfoContent(); if (!P) - return false; + return Features; SmallVector Lines; P->getBuffer().split(Lines, "\n"); @@ -1972,10 +1974,12 @@ bool sys::getHostCPUFeatures(StringMap &Features) { Features["crypto"] = true; #endif - return true; + return Features; } #elif defined(_WIN32) && (defined(__aarch64__) || defined(_M_ARM64)) -bool sys::getHostCPUFeatures(StringMap &Features) { +const StringMap sys::getHostCPUFeatures() { + StringMap Features; + if (IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE)) Features["neon"] = true; if (IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE)) @@ -1983,16 +1987,18 @@ bool sys::getHostCPUFeatures(StringMap &Features) { if (IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE)) Features["crypto"] = true; - return true; + return Features; } #elif defined(__linux__) && defined(__loongarch__) #include -bool sys::getHostCPUFeatures(StringMap &Features) { +const StringMap sys::getHostCPUFeatures() { unsigned long hwcap = getauxval(AT_HWCAP); bool HasFPU = hwcap & (1UL << 3); // HWCAP_LOONGARCH_FPU uint32_t cpucfg2 = 0x2; __asm__("cpucfg %[cpucfg2], %[cpucfg2]\n\t" : [cpucfg2] "+r"(cpucfg2)); + StringMap Features; + Features["f"] = HasFPU && (cpucfg2 & (1U << 1)); // CPUCFG.2.FP_SP Features["d"] = HasFPU && (cpucfg2 & (1U << 2)); // CPUCFG.2.FP_DP @@ -2000,10 +2006,10 @@ bool sys::getHostCPUFeatures(StringMap &Features) { Features["lasx"] = hwcap & (1UL << 5); // HWCAP_LOONGARCH_LASX Features["lvz"] = hwcap & (1UL << 9); // HWCAP_LOONGARCH_LVZ - return true; + return Features; } #else -bool sys::getHostCPUFeatures(StringMap &Features) { return false; } +const StringMap sys::getHostCPUFeatures() { return {}; } #endif #if __APPLE__