[PATCH] D95966: [AIX][support] Implement getHostCPUName

David Tenty via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 3 12:06:04 PST 2021


daltenty created this revision.
Herald added subscribers: dexonsmith, hiraditya.
daltenty requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

We implement getHostCPUName() for AIX via exec'ing the system
`getsystype` since this is a privileged operation. We return a value
based on the  current processor implementation mode.

This fixes the cpu detection used by clang for `-mcpu=native`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D95966

Files:
  llvm/lib/Support/Host.cpp
  llvm/unittests/Support/Host.cpp


Index: llvm/unittests/Support/Host.cpp
===================================================================
--- llvm/unittests/Support/Host.cpp
+++ 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
Index: llvm/lib/Support/Host.cpp
===================================================================
--- llvm/lib/Support/Host.cpp
+++ 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 <assert.h>
@@ -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<llvm::StringRef> argv = {ExePath, "-i"};
+  const Optional<StringRef> 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<const char *>(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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D95966.321183.patch
Type: text/x-patch
Size: 2531 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210203/537d042a/attachment.bin>


More information about the llvm-commits mailing list