diff --git a/llvm/lib/Support/Host.cpp b/llvm/lib/Support/Host.cpp --- a/llvm/lib/Support/Host.cpp +++ b/llvm/lib/Support/Host.cpp @@ -21,6 +21,7 @@ #include "llvm/Support/Debug.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/Program.h" #include "llvm/Support/X86TargetParser.h" #include "llvm/Support/raw_ostream.h" #include @@ -1217,6 +1218,50 @@ return "generic"; } +#elif defined(_AIX) +StringRef sys::getHostCPUName() { + SmallString<128> OutputFile; + std::error_code EC = fs::createTemporaryFile("getsystype", "", OutputFile); + if (EC) { + llvm::errs() << "Can't create temporary file for getsystype output:" + << EC.message() << "\n"; + return "generic"; + } + StringRef OutputPath = OutputFile.str(); + + // We return a value base on the current processor implementation mode. + const char *ExePath = "/usr/sbin/getsystype"; + ArrayRef argv = {ExePath, "-i"}; + const Optional Redirects[] = { + /*STDIN=*/None, /*STDOUT=*/OutputPath, /*STDERR=*/None}; + + int RetCode = + sys::ExecuteAndWait(ExePath, argv, /*env=*/llvm::None, Redirects); + if (RetCode) { + llvm::errs() << "getsystype exited non-zero \n"; + return "generic"; + } + + auto ErrOrBuf = MemoryBuffer::getFile(OutputFile); + if (EC = ErrOrBuf.getError()) { + llvm::errs() << "Can't read getsystype output:" << EC.message() << "\n"; + return "generic"; + } + + if (EC = fs::remove(OutputPath)) { + llvm::errs() << "Can't cleanup temporary file for getsystype output:" + << EC.message() << "\n"; + } + return StringSwitch(ErrOrBuf.get()->getBuffer()) + .Case("POWER 4\n", "pwr4") + .Case("POWER 5\n", "pwr5") + .Case("POWER 6\n", "pwr6") + .Case("POWER 7\n", "pwr7") + .Case("POWER 8\n", "pwr8") + .Case("POWER 9\n", "pwr9") + .Case("POWER 10\n", "pwr10") + .Default("generic"); +} #else StringRef sys::getHostCPUName() { return "generic"; } #endif diff --git a/llvm/unittests/Support/Host.cpp b/llvm/unittests/Support/Host.cpp --- a/llvm/unittests/Support/Host.cpp +++ b/llvm/unittests/Support/Host.cpp @@ -431,4 +431,10 @@ ASSERT_EQ(std::tie(SystemMajor, SystemMinor), std::tie(TargetMajor, TargetMinor)); } + +TEST_F(HostTest, AIXHostCPUDetect) { + using namespace llvm::sys; + + EXPECT_EQ(getHostCPUName().slice(0, 3), "pwr"); +} #endif