[PATCH] D140806: Change getProcessTriple to return different archs in universal binary

Jacques Pienaar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 31 08:13:35 PST 2022


jpienaar created this revision.
Herald added subscribers: pengfei, hiraditya, kristof.beyls.
Herald added a project: All.
jpienaar requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

With the current approach that cmake makes uses for universal binaries
getProcessTriple was returning the same value whether or not being run on
x86_64 or arm. This resulted in failures if one ran the tools/tests with `arch
-x86_64` or `arch -arm64` (depending on LLVM_HOST_TRIPLE that was set at build
time). Change getProcessTriple to consider the architecture being compiled when
populating triple.

I'm not sure if this is best approach but LLVM_HOST_TRIPLE is fixed per
compilation given current universal binary approach and this is noninvasive
while enabling usage inside universal binary so easy to change.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D140806

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
@@ -465,6 +465,25 @@
   }
 }
 
+TEST_F(HostTest, getMacOSHostArch) {
+  llvm::Triple HostTriple(llvm::sys::getProcessTriple());
+  if (!HostTriple.isMacOSX())
+    GTEST_SKIP();
+  std::string HostTripleStr = HostTriple.normalize();
+
+  const char *SwVersPath = "/usr/bin/arch";
+  StringRef argv[] = {SwVersPath};
+  std::unique_ptr<char[]> Buffer;
+  off_t Size;
+  ASSERT_EQ(runAndGetCommandOutput(SwVersPath, argv, Buffer, Size), true);
+  StringRef ArchStr = StringRef(Buffer.get(), Size).rtrim();
+
+  if (ArchStr == "i386")
+    ASSERT_EQ(StringRef(HostTripleStr).starts_with("x86"), true);
+  if (ArchStr == "arm64")
+    ASSERT_EQ(StringRef(HostTripleStr).starts_with("arm"), true);
+}
+
 // Helper to return AIX system version. Must return void to use ASSERT_*.
 static void getAIXSystemVersion(VersionTuple &SystemVersion) {
   const char *ExePath = "/usr/bin/oslevel";
Index: llvm/lib/Support/Host.cpp
===================================================================
--- llvm/lib/Support/Host.cpp
+++ llvm/lib/Support/Host.cpp
@@ -1927,5 +1927,16 @@
   if (sizeof(void *) == 4 && PT.isArch64Bit())
     PT = PT.get32BitArchVariant();
 
+#if defined(__APPLE__)
+    // Handle current CMake universal binary compilation.
+#if defined(__aarch64__)
+  if (PT.getArch() != llvm::Triple::aarch64)
+    PT.setArch(llvm::Triple::aarch64);
+#elif defined(__x86_64__) || defined(_M_X64)
+  if (PT.getArch() != llvm::Triple::x86_64)
+    PT.setArch(llvm::Triple::x86_64);
+#endif
+#endif
+
   return PT.str();
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140806.485778.patch
Type: text/x-patch
Size: 1697 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221231/2d3243b5/attachment.bin>


More information about the llvm-commits mailing list