[clang] 18e70a4 - [llvm][TargetParser] Return StringMap from getHostCPUFeatures (#97824)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 11 02:32:47 PDT 2024
Author: David Spickett
Date: 2024-07-11T10:32:43+01:00
New Revision: 18e70a4d5042299054dae7d3995f6ccd8f4112b3
URL: https://github.com/llvm/llvm-project/commit/18e70a4d5042299054dae7d3995f6ccd8f4112b3
DIFF: https://github.com/llvm/llvm-project/commit/18e70a4d5042299054dae7d3995f6ccd8f4112b3.diff
LOG: [llvm][TargetParser] Return StringMap from getHostCPUFeatures (#97824)
Previously this took a reference to a map and returned a bool to say
whether it succeeded. We can return a StringMap instead, as all callers
but 1 simply iterated the map if the bool was true, and passed in empty
maps as the starting point.
lldb's lit-cpuid did specifically check whether the call failed, but due
to the way the x86 routines work this works out the same as checking if
the returned map is empty.
Added:
Modified:
clang/lib/Driver/ToolChains/Arch/ARM.cpp
clang/lib/Driver/ToolChains/Arch/X86.cpp
lldb/utils/lit-cpuid/lit-cpuid.cpp
llvm/include/llvm/TargetParser/Host.h
llvm/lib/CodeGen/CommandFlags.cpp
llvm/lib/ExecutionEngine/Orc/JITTargetMachineBuilder.cpp
llvm/lib/Target/TargetMachineC.cpp
llvm/lib/TargetParser/Host.cpp
Removed:
################################################################################
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<bool> 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<bool> 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<bool> features;
-
- if (!sys::getHostCPUFeatures(features))
+ const StringMap<bool> 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<bool, MallocAllocator> &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<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..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<bool> 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<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)
- 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> 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)
+ 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<bool> 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 c43bf370d1966..8d5ad91839bc4 100644
--- a/llvm/lib/TargetParser/Host.cpp
+++ b/llvm/lib/TargetParser/Host.cpp
@@ -1708,12 +1708,13 @@ VendorSignatures getVendorSignature(unsigned *MaxLeaf) {
#if defined(__i386__) || defined(_M_IX86) || \
defined(__x86_64__) || defined(_M_X64)
-bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
+const 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 false;
+ return Features;
getX86CpuIDAndInfo(1, &EAX, &EBX, &ECX, &EDX);
@@ -1901,13 +1902,14 @@ 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) {
+const StringMap<bool> sys::getHostCPUFeatures() {
+ StringMap<bool> Features;
std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
if (!P)
- return false;
+ return Features;
SmallVector<StringRef, 32> Lines;
P->getBuffer().split(Lines, "\n");
@@ -1970,10 +1972,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) {
+const StringMap<bool> sys::getHostCPUFeatures() {
+ StringMap<bool> Features;
+
if (IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE))
Features["neon"] = true;
if (IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE))
@@ -1981,16 +1985,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) {
+const 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));
+ StringMap<bool> Features;
+
Features["f"] = HasFPU && (cpucfg2 & (1U << 1)); // CPUCFG.2.FP_SP
Features["d"] = HasFPU && (cpucfg2 & (1U << 2)); // CPUCFG.2.FP_DP
@@ -1998,10 +2004,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; }
+const StringMap<bool> sys::getHostCPUFeatures() { return {}; }
#endif
#if __APPLE__
More information about the cfe-commits
mailing list