[Lldb-commits] [clang] [lldb] [llvm] [llvm][TargetParser] Return StringMap from getHostCPUFeatures (PR #97824)

David Spickett via lldb-commits lldb-commits at lists.llvm.org
Thu Jul 11 02:31:59 PDT 2024


https://github.com/DavidSpickett updated https://github.com/llvm/llvm-project/pull/97824

>From 7ebe4e487b763ff26fbab6d75aa7c8694d63e8b1 Mon Sep 17 00:00:00 2001
From: David Spickett <david.spickett at linaro.org>
Date: Fri, 5 Jul 2024 08:42:22 +0000
Subject: [PATCH 1/9] [llvm][TargetParser] Return optional from
 getHostCPUFeatures

Previously this took a reference to a map and returned a bool
to say whether it succeeded. This is an optional but with more
steps.

The only reason to keep it that way was if someone was appending
to an existing map, but all callers made a new map before calling
it.
---
 clang/lib/Driver/ToolChains/Arch/ARM.cpp      |  6 ++--
 clang/lib/Driver/ToolChains/Arch/X86.cpp      |  6 ++--
 lldb/utils/lit-cpuid/lit-cpuid.cpp            | 21 +++++++-------
 llvm/include/llvm/TargetParser/Host.h         | 14 ++++-----
 llvm/lib/CodeGen/CommandFlags.cpp             | 16 ++++------
 .../Orc/JITTargetMachineBuilder.cpp           |  8 ++---
 llvm/lib/Target/TargetMachineC.cpp            |  5 ++--
 llvm/lib/TargetParser/Host.cpp                | 29 ++++++++++++-------
 8 files changed, 54 insertions(+), 51 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Arch/ARM.cpp b/clang/lib/Driver/ToolChains/Arch/ARM.cpp
index 8ae22cc37a136..77adbf3865ab1 100644
--- a/clang/lib/Driver/ToolChains/Arch/ARM.cpp
+++ b/clang/lib/Driver/ToolChains/Arch/ARM.cpp
@@ -591,9 +591,9 @@ llvm::ARM::FPUKind arm::getARMTargetFeatures(const Driver &D,
 
   // Add CPU features for generic CPUs
   if (CPUName == "native") {
-    llvm::StringMap<bool> HostFeatures;
-    if (llvm::sys::getHostCPUFeatures(HostFeatures))
-      for (auto &F : HostFeatures)
+    if (std::optional<llvm::StringMap<bool>> HostFeatures =
+            llvm::sys::getHostCPUFeatures())
+      for (auto &F : *HostFeatures)
         Features.push_back(
             Args.MakeArgString((F.second ? "+" : "-") + F.first()));
   } else if (!CPUName.empty()) {
diff --git a/clang/lib/Driver/ToolChains/Arch/X86.cpp b/clang/lib/Driver/ToolChains/Arch/X86.cpp
index 92821b2a82dae..e4adfcac23ca0 100644
--- a/clang/lib/Driver/ToolChains/Arch/X86.cpp
+++ b/clang/lib/Driver/ToolChains/Arch/X86.cpp
@@ -131,9 +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<bool> HostFeatures;
-      if (llvm::sys::getHostCPUFeatures(HostFeatures))
-        for (auto &F : HostFeatures)
+      if (std::optional<llvm::StringMap<bool>> HostFeatures =
+              llvm::sys::getHostCPUFeatures())
+        for (auto &F : *HostFeatures)
           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..16743164e6a5d 100644
--- a/lldb/utils/lit-cpuid/lit-cpuid.cpp
+++ b/lldb/utils/lit-cpuid/lit-cpuid.cpp
@@ -15,22 +15,23 @@
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/TargetParser/Host.h"
 
+#include <optional>
+
 using namespace llvm;
 
 int main(int argc, char **argv) {
 #if defined(__i386__) || defined(_M_IX86) || \
     defined(__x86_64__) || defined(_M_X64)
-  StringMap<bool> features;
-
-  if (!sys::getHostCPUFeatures(features))
+  if (std::optional<StringMap<bool>> features =
+          sys::getHostCPUFeatures(features)) {
+    if ((*features)["sse"])
+      outs() << "sse\n";
+    if ((*features)["avx"])
+      outs() << "avx\n";
+    if ((*features)["avx512f"])
+      outs() << "avx512f\n";
+  } else
     return 1;
-
-  if (features["sse"])
-    outs() << "sse\n";
-  if (features["avx"])
-    outs() << "avx\n";
-  if (features["avx512f"])
-    outs() << "avx512f\n";
 #endif
 
   return 0;
diff --git a/llvm/include/llvm/TargetParser/Host.h b/llvm/include/llvm/TargetParser/Host.h
index af72045a8fe67..d68655835a323 100644
--- a/llvm/include/llvm/TargetParser/Host.h
+++ b/llvm/include/llvm/TargetParser/Host.h
@@ -13,6 +13,7 @@
 #ifndef LLVM_TARGETPARSER_HOST_H
 #define LLVM_TARGETPARSER_HOST_H
 
+#include <optional>
 #include <string>
 
 namespace llvm {
@@ -47,13 +48,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<bool, MallocAllocator> &Features);
+  /// \return - If feature detection succeeds, 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. If feature detection
+  /// fails, an empty optional is returned.
+  std::optional<StringMap<bool, MallocAllocator>> 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..b881f1289e9f3 100644
--- a/llvm/lib/CodeGen/CommandFlags.cpp
+++ b/llvm/lib/CodeGen/CommandFlags.cpp
@@ -624,12 +624,10 @@ 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<bool> HostFeatures;
-    if (sys::getHostCPUFeatures(HostFeatures))
-      for (const auto &[Feature, IsEnabled] : HostFeatures)
+  if (getMCPU() == "native")
+    if (std::optional<StringMap<bool>> HostFeatures = sys::getHostCPUFeatures())
+      for (const auto &[Feature, IsEnabled] : *HostFeatures)
         Features.AddFeature(Feature, IsEnabled);
-  }
 
   for (auto const &MAttr : getMAttrs())
     Features.AddFeature(MAttr);
@@ -644,12 +642,10 @@ std::vector<std::string> 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<bool> HostFeatures;
-    if (sys::getHostCPUFeatures(HostFeatures))
-      for (const auto &[Feature, IsEnabled] : HostFeatures)
+  if (getMCPU() == "native")
+    if (std::optional<StringMap<bool>> HostFeatures = sys::getHostCPUFeatures())
+      for (const auto &[Feature, IsEnabled] : *HostFeatures)
         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..5f5a8f7168998 100644
--- a/llvm/lib/ExecutionEngine/Orc/JITTargetMachineBuilder.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/JITTargetMachineBuilder.cpp
@@ -28,10 +28,10 @@ Expected<JITTargetMachineBuilder> 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<bool> FeatureMap;
-  llvm::sys::getHostCPUFeatures(FeatureMap);
-  for (auto &Feature : FeatureMap)
-    TMBuilder.getFeatures().AddFeature(Feature.first(), Feature.second);
+  if (std::optional<StringMap<bool>> FeatureMap =
+          llvm::sys::getHostCPUFeatures())
+    for (auto &Feature : *FeatureMap)
+      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..340ae50285951 100644
--- a/llvm/lib/Target/TargetMachineC.cpp
+++ b/llvm/lib/Target/TargetMachineC.cpp
@@ -363,10 +363,9 @@ char *LLVMGetHostCPUName(void) {
 
 char *LLVMGetHostCPUFeatures(void) {
   SubtargetFeatures Features;
-  StringMap<bool> HostFeatures;
 
-  if (sys::getHostCPUFeatures(HostFeatures))
-    for (const auto &[Feature, IsEnabled] : HostFeatures)
+  if (std::optional<StringMap<bool>> HostFeatures = sys::getHostCPUFeatures())
+    for (const auto &[Feature, IsEnabled] : *HostFeatures)
       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..4cf06505e902e 100644
--- a/llvm/lib/TargetParser/Host.cpp
+++ b/llvm/lib/TargetParser/Host.cpp
@@ -1710,15 +1710,17 @@ VendorSignatures getVendorSignature(unsigned *MaxLeaf) {
 
 #if defined(__i386__) || defined(_M_IX86) || \
     defined(__x86_64__) || defined(_M_X64)
-bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
+std::optional<StringMap<bool>> sys::getHostCPUFeatures() {
   unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
   unsigned MaxLevel;
 
   if (getX86CpuIDAndInfo(0, &MaxLevel, &EBX, &ECX, &EDX) || MaxLevel < 1)
-    return false;
+    return {};
 
   getX86CpuIDAndInfo(1, &EAX, &EBX, &ECX, &EDX);
 
+  StringMap<bool> Features;
+
   Features["cx8"]    = (EDX >>  8) & 1;
   Features["cmov"]   = (EDX >> 15) & 1;
   Features["mmx"]    = (EDX >> 23) & 1;
@@ -1903,13 +1905,13 @@ bool sys::getHostCPUFeatures(StringMap<bool> &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<bool> &Features) {
+std::optional<StringMap<bool>> sys::getHostCPUFeatures() {
   std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
   if (!P)
-    return false;
+    return {};
 
   SmallVector<StringRef, 32> Lines;
   P->getBuffer().split(Lines, "\n");
@@ -1929,6 +1931,7 @@ bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
   uint32_t crypto = 0;
 #endif
 
+  StringMap<bool> Features;
   for (unsigned I = 0, E = CPUFeatures.size(); I != E; ++I) {
     StringRef LLVMFeatureStr = StringSwitch<StringRef>(CPUFeatures[I])
 #if defined(__aarch64__)
@@ -1972,10 +1975,12 @@ bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
     Features["crypto"] = true;
 #endif
 
-  return true;
+  return Features;
 }
 #elif defined(_WIN32) && (defined(__aarch64__) || defined(_M_ARM64))
-bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
+std::optional<StringMap<bool>> sys::getHostCPUFeatures() {
+  std::optional<StringMap<bool>> Features;
+
   if (IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE))
     Features["neon"] = true;
   if (IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE))
@@ -1983,16 +1988,18 @@ bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
   if (IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE))
     Features["crypto"] = true;
 
-  return true;
+  return Features;
 }
 #elif defined(__linux__) && defined(__loongarch__)
 #include <sys/auxv.h>
-bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
+std::optional<StringMap<bool>> 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));
 
+  std::optional<StringMap<bool>> Features;
+
   Features["f"] = HasFPU && (cpucfg2 & (1U << 1)); // CPUCFG.2.FP_SP
   Features["d"] = HasFPU && (cpucfg2 & (1U << 2)); // CPUCFG.2.FP_DP
 
@@ -2000,10 +2007,10 @@ bool sys::getHostCPUFeatures(StringMap<bool> &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<bool> &Features) { return false; }
+std::optional<StringMap<bool>> sys::getHostCPUFeatures() { return {}; }
 #endif
 
 #if __APPLE__

>From daa180e116d9ae0d78613c109ceff5a85337f5be Mon Sep 17 00:00:00 2001
From: David Spickett <david.spickett at linaro.org>
Date: Fri, 5 Jul 2024 14:31:24 +0000
Subject: [PATCH 2/9] Use const and contains

---
 lldb/utils/lit-cpuid/lit-cpuid.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/lldb/utils/lit-cpuid/lit-cpuid.cpp b/lldb/utils/lit-cpuid/lit-cpuid.cpp
index 16743164e6a5d..5039077a5e784 100644
--- a/lldb/utils/lit-cpuid/lit-cpuid.cpp
+++ b/lldb/utils/lit-cpuid/lit-cpuid.cpp
@@ -22,13 +22,13 @@ using namespace llvm;
 int main(int argc, char **argv) {
 #if defined(__i386__) || defined(_M_IX86) || \
     defined(__x86_64__) || defined(_M_X64)
-  if (std::optional<StringMap<bool>> features =
+  if (const std::optional<StringMap<bool>> features =
           sys::getHostCPUFeatures(features)) {
-    if ((*features)["sse"])
+    if (features->contains("sse"))
       outs() << "sse\n";
-    if ((*features)["avx"])
+    if (features->contains("avx"))
       outs() << "avx\n";
-    if ((*features)["avx512f"])
+    if (features->contains("avx512f"))
       outs() << "avx512f\n";
   } else
     return 1;

>From 69e246b3d9d03bcd7cf75317fcf5016bfbc19814 Mon Sep 17 00:00:00 2001
From: David Spickett <david.spickett at linaro.org>
Date: Fri, 5 Jul 2024 15:08:00 +0000
Subject: [PATCH 3/9] contains -> lookup

---
 lldb/utils/lit-cpuid/lit-cpuid.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/lldb/utils/lit-cpuid/lit-cpuid.cpp b/lldb/utils/lit-cpuid/lit-cpuid.cpp
index 5039077a5e784..d88c2b7515fde 100644
--- a/lldb/utils/lit-cpuid/lit-cpuid.cpp
+++ b/lldb/utils/lit-cpuid/lit-cpuid.cpp
@@ -24,11 +24,11 @@ int main(int argc, char **argv) {
     defined(__x86_64__) || defined(_M_X64)
   if (const std::optional<StringMap<bool>> features =
           sys::getHostCPUFeatures(features)) {
-    if (features->contains("sse"))
+    if (features->lookup("sse"))
       outs() << "sse\n";
-    if (features->contains("avx"))
+    if (features->lookup("avx"))
       outs() << "avx\n";
-    if (features->contains("avx512f"))
+    if (features->lookup("avx512f"))
       outs() << "avx512f\n";
   } else
     return 1;

>From 2a761e591d781d85af622968f72b28c9f7540691 Mon Sep 17 00:00:00 2001
From: David Spickett <david.spickett at linaro.org>
Date: Tue, 9 Jul 2024 12:57:58 +0000
Subject: [PATCH 4/9] return stringmap only

---
 clang/lib/Driver/ToolChains/Arch/ARM.cpp      |  8 +++----
 clang/lib/Driver/ToolChains/Arch/X86.cpp      |  8 +++----
 lldb/utils/lit-cpuid/lit-cpuid.cpp            | 18 +++++++--------
 llvm/include/llvm/TargetParser/Host.h         | 12 +++++-----
 llvm/lib/CodeGen/CommandFlags.cpp             | 10 ++++----
 .../Orc/JITTargetMachineBuilder.cpp           |  6 ++---
 llvm/lib/Target/TargetMachineC.cpp            |  6 ++---
 llvm/lib/TargetParser/Host.cpp                | 23 +++++++++----------
 8 files changed, 40 insertions(+), 51 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Arch/ARM.cpp b/clang/lib/Driver/ToolChains/Arch/ARM.cpp
index 77adbf3865ab1..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") {
-    if (std::optional<llvm::StringMap<bool>> HostFeatures =
-            llvm::sys::getHostCPUFeatures())
-      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 e4adfcac23ca0..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") {
-      if (std::optional<llvm::StringMap<bool>> HostFeatures =
-              llvm::sys::getHostCPUFeatures())
-        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 d88c2b7515fde..794ff0e2af632 100644
--- a/lldb/utils/lit-cpuid/lit-cpuid.cpp
+++ b/lldb/utils/lit-cpuid/lit-cpuid.cpp
@@ -22,16 +22,16 @@ using namespace llvm;
 int main(int argc, char **argv) {
 #if defined(__i386__) || defined(_M_IX86) || \
     defined(__x86_64__) || defined(_M_X64)
-  if (const std::optional<StringMap<bool>> features =
-          sys::getHostCPUFeatures(features)) {
-    if (features->lookup("sse"))
-      outs() << "sse\n";
-    if (features->lookup("avx"))
-      outs() << "avx\n";
-    if (features->lookup("avx512f"))
-      outs() << "avx512f\n";
-  } else
+  const < StringMap<bool> features = sys::getHostCPUFeatures(features);
+  if (features.empty())
     return 1;
+
+  if (features->lookup("sse"))
+    outs() << "sse\n";
+  if (features->lookup("avx"))
+    outs() << "avx\n";
+  if (features->lookup("avx512f"))
+    outs() << "avx512f\n";
 #endif
 
   return 0;
diff --git a/llvm/include/llvm/TargetParser/Host.h b/llvm/include/llvm/TargetParser/Host.h
index d68655835a323..a43fe35b3168c 100644
--- a/llvm/include/llvm/TargetParser/Host.h
+++ b/llvm/include/llvm/TargetParser/Host.h
@@ -48,12 +48,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.
   ///
-  /// \return - If feature detection succeeds, 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. If feature detection
-  /// fails, an empty optional is returned.
-  std::optional<StringMap<bool, MallocAllocator>> getHostCPUFeatures();
+  /// \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.
+  StringMap<bool, MallocAllocator> 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 b881f1289e9f3..9e42deb94903d 100644
--- a/llvm/lib/CodeGen/CommandFlags.cpp
+++ b/llvm/lib/CodeGen/CommandFlags.cpp
@@ -625,9 +625,8 @@ std::string codegen::getFeaturesStr() {
   // features the autodetected CPU name lists in the target. For example,
   // not all Sandybridge processors support AVX.
   if (getMCPU() == "native")
-    if (std::optional<StringMap<bool>> HostFeatures = sys::getHostCPUFeatures())
-      for (const auto &[Feature, IsEnabled] : *HostFeatures)
-        Features.AddFeature(Feature, IsEnabled);
+    for (const auto &[Feature, IsEnabled] : sys::getHostCPUFeatures())
+      Features.AddFeature(Feature, IsEnabled);
 
   for (auto const &MAttr : getMAttrs())
     Features.AddFeature(MAttr);
@@ -643,9 +642,8 @@ std::vector<std::string> codegen::getFeatureList() {
   // features the autodetected CPU name lists in the target. For example,
   // not all Sandybridge processors support AVX.
   if (getMCPU() == "native")
-    if (std::optional<StringMap<bool>> HostFeatures = sys::getHostCPUFeatures())
-      for (const auto &[Feature, IsEnabled] : *HostFeatures)
-        Features.AddFeature(Feature, IsEnabled);
+    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 5f5a8f7168998..7791eb1576002 100644
--- a/llvm/lib/ExecutionEngine/Orc/JITTargetMachineBuilder.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/JITTargetMachineBuilder.cpp
@@ -28,10 +28,8 @@ Expected<JITTargetMachineBuilder> 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.
-  if (std::optional<StringMap<bool>> FeatureMap =
-          llvm::sys::getHostCPUFeatures())
-    for (auto &Feature : *FeatureMap)
-      TMBuilder.getFeatures().AddFeature(Feature.first(), Feature.second);
+  for (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 340ae50285951..d12fc65047d04 100644
--- a/llvm/lib/Target/TargetMachineC.cpp
+++ b/llvm/lib/Target/TargetMachineC.cpp
@@ -363,10 +363,8 @@ char *LLVMGetHostCPUName(void) {
 
 char *LLVMGetHostCPUFeatures(void) {
   SubtargetFeatures Features;
-
-  if (std::optional<StringMap<bool>> HostFeatures = sys::getHostCPUFeatures())
-    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 4cf06505e902e..80887b26883e4 100644
--- a/llvm/lib/TargetParser/Host.cpp
+++ b/llvm/lib/TargetParser/Host.cpp
@@ -1710,17 +1710,16 @@ VendorSignatures getVendorSignature(unsigned *MaxLeaf) {
 
 #if defined(__i386__) || defined(_M_IX86) || \
     defined(__x86_64__) || defined(_M_X64)
-std::optional<StringMap<bool>> sys::getHostCPUFeatures() {
+StringMap<bool> sys::getHostCPUFeatures() {
   unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
   unsigned MaxLevel;
+  StringMap<bool> Features;
 
   if (getX86CpuIDAndInfo(0, &MaxLevel, &EBX, &ECX, &EDX) || MaxLevel < 1)
-    return {};
+    return Features;
 
   getX86CpuIDAndInfo(1, &EAX, &EBX, &ECX, &EDX);
 
-  StringMap<bool> Features;
-
   Features["cx8"]    = (EDX >>  8) & 1;
   Features["cmov"]   = (EDX >> 15) & 1;
   Features["mmx"]    = (EDX >> 23) & 1;
@@ -1908,10 +1907,11 @@ std::optional<StringMap<bool>> sys::getHostCPUFeatures() {
   return Features;
 }
 #elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
-std::optional<StringMap<bool>> sys::getHostCPUFeatures() {
+StringMap<bool> sys::getHostCPUFeatures() {
+  StringMap<bool> Features;
   std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
   if (!P)
-    return {};
+    return Features;
 
   SmallVector<StringRef, 32> Lines;
   P->getBuffer().split(Lines, "\n");
@@ -1931,7 +1931,6 @@ std::optional<StringMap<bool>> sys::getHostCPUFeatures() {
   uint32_t crypto = 0;
 #endif
 
-  StringMap<bool> Features;
   for (unsigned I = 0, E = CPUFeatures.size(); I != E; ++I) {
     StringRef LLVMFeatureStr = StringSwitch<StringRef>(CPUFeatures[I])
 #if defined(__aarch64__)
@@ -1978,8 +1977,8 @@ std::optional<StringMap<bool>> sys::getHostCPUFeatures() {
   return Features;
 }
 #elif defined(_WIN32) && (defined(__aarch64__) || defined(_M_ARM64))
-std::optional<StringMap<bool>> sys::getHostCPUFeatures() {
-  std::optional<StringMap<bool>> Features;
+StringMap<bool> sys::getHostCPUFeatures() {
+  StringMap<bool> Features;
 
   if (IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE))
     Features["neon"] = true;
@@ -1992,13 +1991,13 @@ std::optional<StringMap<bool>> sys::getHostCPUFeatures() {
 }
 #elif defined(__linux__) && defined(__loongarch__)
 #include <sys/auxv.h>
-std::optional<StringMap<bool>> sys::getHostCPUFeatures() {
+StringMap<bool> 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));
 
-  std::optional<StringMap<bool>> Features;
+  StringMap<bool> Features;
 
   Features["f"] = HasFPU && (cpucfg2 & (1U << 1)); // CPUCFG.2.FP_SP
   Features["d"] = HasFPU && (cpucfg2 & (1U << 2)); // CPUCFG.2.FP_DP
@@ -2010,7 +2009,7 @@ std::optional<StringMap<bool>> sys::getHostCPUFeatures() {
   return Features;
 }
 #else
-std::optional<StringMap<bool>> sys::getHostCPUFeatures() { return {}; }
+StringMap<bool> sys::getHostCPUFeatures() { return {}; }
 #endif
 
 #if __APPLE__

>From c1e271e6305fbd8c63982ca6e48d8d43c09f71b7 Mon Sep 17 00:00:00 2001
From: David Spickett <david.spickett at linaro.org>
Date: Tue, 9 Jul 2024 14:28:04 +0000
Subject: [PATCH 5/9] unused includes

---
 lldb/utils/lit-cpuid/lit-cpuid.cpp    | 2 --
 llvm/include/llvm/TargetParser/Host.h | 1 -
 2 files changed, 3 deletions(-)

diff --git a/lldb/utils/lit-cpuid/lit-cpuid.cpp b/lldb/utils/lit-cpuid/lit-cpuid.cpp
index 794ff0e2af632..7ea1d788c6576 100644
--- a/lldb/utils/lit-cpuid/lit-cpuid.cpp
+++ b/lldb/utils/lit-cpuid/lit-cpuid.cpp
@@ -15,8 +15,6 @@
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/TargetParser/Host.h"
 
-#include <optional>
-
 using namespace llvm;
 
 int main(int argc, char **argv) {
diff --git a/llvm/include/llvm/TargetParser/Host.h b/llvm/include/llvm/TargetParser/Host.h
index a43fe35b3168c..a263874c946ea 100644
--- a/llvm/include/llvm/TargetParser/Host.h
+++ b/llvm/include/llvm/TargetParser/Host.h
@@ -13,7 +13,6 @@
 #ifndef LLVM_TARGETPARSER_HOST_H
 #define LLVM_TARGETPARSER_HOST_H
 
-#include <optional>
 #include <string>
 
 namespace llvm {

>From f0510783a78dfab062b287a893d0fdb08de9eeaf Mon Sep 17 00:00:00 2001
From: David Spickett <david.spickett at linaro.org>
Date: Wed, 10 Jul 2024 08:34:38 +0000
Subject: [PATCH 6/9] Fix typo

---
 lldb/utils/lit-cpuid/lit-cpuid.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lldb/utils/lit-cpuid/lit-cpuid.cpp b/lldb/utils/lit-cpuid/lit-cpuid.cpp
index 7ea1d788c6576..33308edc212e4 100644
--- a/lldb/utils/lit-cpuid/lit-cpuid.cpp
+++ b/lldb/utils/lit-cpuid/lit-cpuid.cpp
@@ -20,7 +20,7 @@ using namespace llvm;
 int main(int argc, char **argv) {
 #if defined(__i386__) || defined(_M_IX86) || \
     defined(__x86_64__) || defined(_M_X64)
-  const < StringMap<bool> features = sys::getHostCPUFeatures(features);
+  const StringMap<bool> features = sys::getHostCPUFeatures(features);
   if (features.empty())
     return 1;
 

>From 6a6b38b94932cb677a09eefe005e4fe11e47fea9 Mon Sep 17 00:00:00 2001
From: David Spickett <david.spickett at linaro.org>
Date: Wed, 10 Jul 2024 08:41:49 +0000
Subject: [PATCH 7/9] const return type

---
 llvm/include/llvm/TargetParser/Host.h                  |  2 +-
 .../ExecutionEngine/Orc/JITTargetMachineBuilder.cpp    |  2 +-
 llvm/lib/TargetParser/Host.cpp                         | 10 +++++-----
 3 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/llvm/include/llvm/TargetParser/Host.h b/llvm/include/llvm/TargetParser/Host.h
index a263874c946ea..a639b093f21bd 100644
--- a/llvm/include/llvm/TargetParser/Host.h
+++ b/llvm/include/llvm/TargetParser/Host.h
@@ -52,7 +52,7 @@ namespace sys {
   /// 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.
-  StringMap<bool, MallocAllocator> getHostCPUFeatures();
+  const StringMap<bool, MallocAllocator> 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/ExecutionEngine/Orc/JITTargetMachineBuilder.cpp b/llvm/lib/ExecutionEngine/Orc/JITTargetMachineBuilder.cpp
index 7791eb1576002..8d4e79c7d8aff 100644
--- a/llvm/lib/ExecutionEngine/Orc/JITTargetMachineBuilder.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/JITTargetMachineBuilder.cpp
@@ -28,7 +28,7 @@ Expected<JITTargetMachineBuilder> 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.
-  for (auto &Feature : llvm::sys::getHostCPUFeatures())
+  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/TargetParser/Host.cpp b/llvm/lib/TargetParser/Host.cpp
index 80887b26883e4..7e36c4867245d 100644
--- a/llvm/lib/TargetParser/Host.cpp
+++ b/llvm/lib/TargetParser/Host.cpp
@@ -1710,7 +1710,7 @@ VendorSignatures getVendorSignature(unsigned *MaxLeaf) {
 
 #if defined(__i386__) || defined(_M_IX86) || \
     defined(__x86_64__) || defined(_M_X64)
-StringMap<bool> sys::getHostCPUFeatures() {
+const StringMap<bool> sys::getHostCPUFeatures() {
   unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
   unsigned MaxLevel;
   StringMap<bool> Features;
@@ -1907,7 +1907,7 @@ StringMap<bool> sys::getHostCPUFeatures() {
   return Features;
 }
 #elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
-StringMap<bool> sys::getHostCPUFeatures() {
+const StringMap<bool> sys::getHostCPUFeatures() {
   StringMap<bool> Features;
   std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
   if (!P)
@@ -1977,7 +1977,7 @@ StringMap<bool> sys::getHostCPUFeatures() {
   return Features;
 }
 #elif defined(_WIN32) && (defined(__aarch64__) || defined(_M_ARM64))
-StringMap<bool> sys::getHostCPUFeatures() {
+const StringMap<bool> sys::getHostCPUFeatures() {
   StringMap<bool> Features;
 
   if (IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE))
@@ -1991,7 +1991,7 @@ StringMap<bool> sys::getHostCPUFeatures() {
 }
 #elif defined(__linux__) && defined(__loongarch__)
 #include <sys/auxv.h>
-StringMap<bool> sys::getHostCPUFeatures() {
+const StringMap<bool> sys::getHostCPUFeatures() {
   unsigned long hwcap = getauxval(AT_HWCAP);
   bool HasFPU = hwcap & (1UL << 3); // HWCAP_LOONGARCH_FPU
   uint32_t cpucfg2 = 0x2;
@@ -2009,7 +2009,7 @@ StringMap<bool> sys::getHostCPUFeatures() {
   return Features;
 }
 #else
-StringMap<bool> sys::getHostCPUFeatures() { return {}; }
+const StringMap<bool> sys::getHostCPUFeatures() { return {}; }
 #endif
 
 #if __APPLE__

>From 5dfc4848641c9ba2e5ee5a5b8c9978a1253879b9 Mon Sep 17 00:00:00 2001
From: David Spickett <david.spickett at linaro.org>
Date: Thu, 11 Jul 2024 09:23:37 +0000
Subject: [PATCH 8/9] correct lookup

---
 lldb/utils/lit-cpuid/lit-cpuid.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/lldb/utils/lit-cpuid/lit-cpuid.cpp b/lldb/utils/lit-cpuid/lit-cpuid.cpp
index 33308edc212e4..dcd1150dd8561 100644
--- a/lldb/utils/lit-cpuid/lit-cpuid.cpp
+++ b/lldb/utils/lit-cpuid/lit-cpuid.cpp
@@ -24,11 +24,11 @@ int main(int argc, char **argv) {
   if (features.empty())
     return 1;
 
-  if (features->lookup("sse"))
+  if (features.lookup("sse"))
     outs() << "sse\n";
-  if (features->lookup("avx"))
+  if (features.lookup("avx"))
     outs() << "avx\n";
-  if (features->lookup("avx512f"))
+  if (features.lookup("avx512f"))
     outs() << "avx512f\n";
 #endif
 

>From e59836c481726b882c8d037fb2678cceb4bfe025 Mon Sep 17 00:00:00 2001
From: David Spickett <david.spickett at linaro.org>
Date: Thu, 11 Jul 2024 09:31:20 +0000
Subject: [PATCH 9/9] Remove parameter

---
 lldb/utils/lit-cpuid/lit-cpuid.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lldb/utils/lit-cpuid/lit-cpuid.cpp b/lldb/utils/lit-cpuid/lit-cpuid.cpp
index dcd1150dd8561..55f6dfd8ea906 100644
--- a/lldb/utils/lit-cpuid/lit-cpuid.cpp
+++ b/lldb/utils/lit-cpuid/lit-cpuid.cpp
@@ -20,7 +20,7 @@ using namespace llvm;
 int main(int argc, char **argv) {
 #if defined(__i386__) || defined(_M_IX86) || \
     defined(__x86_64__) || defined(_M_X64)
-  const StringMap<bool> features = sys::getHostCPUFeatures(features);
+  const StringMap<bool> features = sys::getHostCPUFeatures();
   if (features.empty())
     return 1;
 



More information about the lldb-commits mailing list