Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[llvm][TargetParser] Return StringMap from getHostCPUFeatures #97824

Merged
merged 9 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions clang/lib/Driver/ToolChains/Arch/ARM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Driver/ToolChains/Arch/X86.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand Down
21 changes: 11 additions & 10 deletions lldb/utils/lit-cpuid/lit-cpuid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 (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
return 1;

if (features["sse"])
outs() << "sse\n";
if (features["avx"])
outs() << "avx\n";
if (features["avx512f"])
outs() << "avx512f\n";
#endif

return 0;
Expand Down
14 changes: 7 additions & 7 deletions llvm/include/llvm/TargetParser/Host.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef LLVM_TARGETPARSER_HOST_H
#define LLVM_TARGETPARSER_HOST_H

#include <optional>
#include <string>

namespace llvm {
Expand Down Expand Up @@ -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.
Expand Down
16 changes: 6 additions & 10 deletions llvm/lib/CodeGen/CommandFlags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/ExecutionEngine/Orc/JITTargetMachineBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()));

Expand Down
5 changes: 2 additions & 3 deletions llvm/lib/Target/TargetMachineC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
29 changes: 18 additions & 11 deletions llvm/lib/TargetParser/Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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__)
Expand Down Expand Up @@ -1972,38 +1975,42 @@ 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))
Features["crc"] = true;
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

Features["lsx"] = hwcap & (1UL << 4); // HWCAP_LOONGARCH_LSX
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__
Expand Down
Loading