Skip to content

Commit 18e70a4

Browse files
[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.
1 parent 55c0048 commit 18e70a4

File tree

8 files changed

+43
-54
lines changed

8 files changed

+43
-54
lines changed

clang/lib/Driver/ToolChains/Arch/ARM.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -591,11 +591,9 @@ llvm::ARM::FPUKind arm::getARMTargetFeatures(const Driver &D,
591591

592592
// Add CPU features for generic CPUs
593593
if (CPUName == "native") {
594-
llvm::StringMap<bool> HostFeatures;
595-
if (llvm::sys::getHostCPUFeatures(HostFeatures))
596-
for (auto &F : HostFeatures)
597-
Features.push_back(
598-
Args.MakeArgString((F.second ? "+" : "-") + F.first()));
594+
for (auto &F : llvm::sys::getHostCPUFeatures())
595+
Features.push_back(
596+
Args.MakeArgString((F.second ? "+" : "-") + F.first()));
599597
} else if (!CPUName.empty()) {
600598
// This sets the default features for the specified CPU. We certainly don't
601599
// want to override the features that have been explicitly specified on the

clang/lib/Driver/ToolChains/Arch/X86.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,9 @@ void x86::getX86TargetFeatures(const Driver &D, const llvm::Triple &Triple,
131131
// If -march=native, autodetect the feature list.
132132
if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_march_EQ)) {
133133
if (StringRef(A->getValue()) == "native") {
134-
llvm::StringMap<bool> HostFeatures;
135-
if (llvm::sys::getHostCPUFeatures(HostFeatures))
136-
for (auto &F : HostFeatures)
137-
Features.push_back(
138-
Args.MakeArgString((F.second ? "+" : "-") + F.first()));
134+
for (auto &F : llvm::sys::getHostCPUFeatures())
135+
Features.push_back(
136+
Args.MakeArgString((F.second ? "+" : "-") + F.first()));
139137
}
140138
}
141139

lldb/utils/lit-cpuid/lit-cpuid.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@ using namespace llvm;
2020
int main(int argc, char **argv) {
2121
#if defined(__i386__) || defined(_M_IX86) || \
2222
defined(__x86_64__) || defined(_M_X64)
23-
StringMap<bool> features;
24-
25-
if (!sys::getHostCPUFeatures(features))
23+
const StringMap<bool> features = sys::getHostCPUFeatures();
24+
if (features.empty())
2625
return 1;
2726

28-
if (features["sse"])
27+
if (features.lookup("sse"))
2928
outs() << "sse\n";
30-
if (features["avx"])
29+
if (features.lookup("avx"))
3130
outs() << "avx\n";
32-
if (features["avx512f"])
31+
if (features.lookup("avx512f"))
3332
outs() << "avx512f\n";
3433
#endif
3534

llvm/include/llvm/TargetParser/Host.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,12 @@ namespace sys {
4747
/// The particular format of the names are target dependent, and suitable for
4848
/// passing as -mattr to the target which matches the host.
4949
///
50-
/// \param Features - A string mapping feature names to either
51-
/// true (if enabled) or false (if disabled). This routine makes no guarantees
52-
/// about exactly which features may appear in this map, except that they are
53-
/// all valid LLVM feature names.
54-
///
55-
/// \return - True on success.
56-
bool getHostCPUFeatures(StringMap<bool, MallocAllocator> &Features);
50+
/// \return - A string map mapping feature names to either true (if enabled)
51+
/// or false (if disabled). This routine makes no guarantees about exactly
52+
/// which features may appear in this map, except that they are all valid LLVM
53+
/// feature names. The map can be empty, for example if feature detection
54+
/// fails.
55+
const StringMap<bool, MallocAllocator> getHostCPUFeatures();
5756

5857
/// This is a function compatible with cl::AddExtraVersionPrinter, which adds
5958
/// info about the current target triple and detected CPU.

llvm/lib/CodeGen/CommandFlags.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -624,12 +624,9 @@ std::string codegen::getFeaturesStr() {
624624
// This is necessary for x86 where the CPU might not support all the
625625
// features the autodetected CPU name lists in the target. For example,
626626
// not all Sandybridge processors support AVX.
627-
if (getMCPU() == "native") {
628-
StringMap<bool> HostFeatures;
629-
if (sys::getHostCPUFeatures(HostFeatures))
630-
for (const auto &[Feature, IsEnabled] : HostFeatures)
631-
Features.AddFeature(Feature, IsEnabled);
632-
}
627+
if (getMCPU() == "native")
628+
for (const auto &[Feature, IsEnabled] : sys::getHostCPUFeatures())
629+
Features.AddFeature(Feature, IsEnabled);
633630

634631
for (auto const &MAttr : getMAttrs())
635632
Features.AddFeature(MAttr);
@@ -644,12 +641,9 @@ std::vector<std::string> codegen::getFeatureList() {
644641
// This is necessary for x86 where the CPU might not support all the
645642
// features the autodetected CPU name lists in the target. For example,
646643
// not all Sandybridge processors support AVX.
647-
if (getMCPU() == "native") {
648-
StringMap<bool> HostFeatures;
649-
if (sys::getHostCPUFeatures(HostFeatures))
650-
for (const auto &[Feature, IsEnabled] : HostFeatures)
651-
Features.AddFeature(Feature, IsEnabled);
652-
}
644+
if (getMCPU() == "native")
645+
for (const auto &[Feature, IsEnabled] : sys::getHostCPUFeatures())
646+
Features.AddFeature(Feature, IsEnabled);
653647

654648
for (auto const &MAttr : getMAttrs())
655649
Features.AddFeature(MAttr);

llvm/lib/ExecutionEngine/Orc/JITTargetMachineBuilder.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ Expected<JITTargetMachineBuilder> JITTargetMachineBuilder::detectHost() {
2828
// Retrieve host CPU name and sub-target features and add them to builder.
2929
// Relocation model, code model and codegen opt level are kept to default
3030
// values.
31-
llvm::StringMap<bool> FeatureMap;
32-
llvm::sys::getHostCPUFeatures(FeatureMap);
33-
for (auto &Feature : FeatureMap)
31+
for (const auto &Feature : llvm::sys::getHostCPUFeatures())
3432
TMBuilder.getFeatures().AddFeature(Feature.first(), Feature.second);
3533

3634
TMBuilder.setCPU(std::string(llvm::sys::getHostCPUName()));

llvm/lib/Target/TargetMachineC.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,8 @@ char *LLVMGetHostCPUName(void) {
363363

364364
char *LLVMGetHostCPUFeatures(void) {
365365
SubtargetFeatures Features;
366-
StringMap<bool> HostFeatures;
367-
368-
if (sys::getHostCPUFeatures(HostFeatures))
369-
for (const auto &[Feature, IsEnabled] : HostFeatures)
370-
Features.AddFeature(Feature, IsEnabled);
366+
for (const auto &[Feature, IsEnabled] : sys::getHostCPUFeatures())
367+
Features.AddFeature(Feature, IsEnabled);
371368

372369
return strdup(Features.getString().c_str());
373370
}

llvm/lib/TargetParser/Host.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,12 +1708,13 @@ VendorSignatures getVendorSignature(unsigned *MaxLeaf) {
17081708

17091709
#if defined(__i386__) || defined(_M_IX86) || \
17101710
defined(__x86_64__) || defined(_M_X64)
1711-
bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
1711+
const StringMap<bool> sys::getHostCPUFeatures() {
17121712
unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
17131713
unsigned MaxLevel;
1714+
StringMap<bool> Features;
17141715

17151716
if (getX86CpuIDAndInfo(0, &MaxLevel, &EBX, &ECX, &EDX) || MaxLevel < 1)
1716-
return false;
1717+
return Features;
17171718

17181719
getX86CpuIDAndInfo(1, &EAX, &EBX, &ECX, &EDX);
17191720

@@ -1901,13 +1902,14 @@ bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
19011902
Features["avx10.1-512"] =
19021903
Features["avx10.1-256"] && HasLeaf24 && ((EBX >> 18) & 1);
19031904

1904-
return true;
1905+
return Features;
19051906
}
19061907
#elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
1907-
bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
1908+
const StringMap<bool> sys::getHostCPUFeatures() {
1909+
StringMap<bool> Features;
19081910
std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
19091911
if (!P)
1910-
return false;
1912+
return Features;
19111913

19121914
SmallVector<StringRef, 32> Lines;
19131915
P->getBuffer().split(Lines, "\n");
@@ -1970,38 +1972,42 @@ bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
19701972
Features["crypto"] = true;
19711973
#endif
19721974

1973-
return true;
1975+
return Features;
19741976
}
19751977
#elif defined(_WIN32) && (defined(__aarch64__) || defined(_M_ARM64))
1976-
bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
1978+
const StringMap<bool> sys::getHostCPUFeatures() {
1979+
StringMap<bool> Features;
1980+
19771981
if (IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE))
19781982
Features["neon"] = true;
19791983
if (IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE))
19801984
Features["crc"] = true;
19811985
if (IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE))
19821986
Features["crypto"] = true;
19831987

1984-
return true;
1988+
return Features;
19851989
}
19861990
#elif defined(__linux__) && defined(__loongarch__)
19871991
#include <sys/auxv.h>
1988-
bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
1992+
const StringMap<bool> sys::getHostCPUFeatures() {
19891993
unsigned long hwcap = getauxval(AT_HWCAP);
19901994
bool HasFPU = hwcap & (1UL << 3); // HWCAP_LOONGARCH_FPU
19911995
uint32_t cpucfg2 = 0x2;
19921996
__asm__("cpucfg %[cpucfg2], %[cpucfg2]\n\t" : [cpucfg2] "+r"(cpucfg2));
19931997

1998+
StringMap<bool> Features;
1999+
19942000
Features["f"] = HasFPU && (cpucfg2 & (1U << 1)); // CPUCFG.2.FP_SP
19952001
Features["d"] = HasFPU && (cpucfg2 & (1U << 2)); // CPUCFG.2.FP_DP
19962002

19972003
Features["lsx"] = hwcap & (1UL << 4); // HWCAP_LOONGARCH_LSX
19982004
Features["lasx"] = hwcap & (1UL << 5); // HWCAP_LOONGARCH_LASX
19992005
Features["lvz"] = hwcap & (1UL << 9); // HWCAP_LOONGARCH_LVZ
20002006

2001-
return true;
2007+
return Features;
20022008
}
20032009
#else
2004-
bool sys::getHostCPUFeatures(StringMap<bool> &Features) { return false; }
2010+
const StringMap<bool> sys::getHostCPUFeatures() { return {}; }
20052011
#endif
20062012

20072013
#if __APPLE__

0 commit comments

Comments
 (0)