[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.
This commit is contained in:
David Spickett 2024-07-11 10:32:43 +01:00 committed by GitHub
parent 55c00485eb
commit 18e70a4d50
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 43 additions and 54 deletions

View File

@ -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

View File

@ -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()));
}
}

View File

@ -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

View File

@ -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.

View File

@ -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);

View File

@ -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()));

View File

@ -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());
}

View File

@ -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__