[PATCH] D156193: Fix thinLTO long compile time problem

Zhuohang Li via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 24 18:15:27 PDT 2023


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

ThinLTO compile time is longer on aarch64 system than on x86 system. The reason is that on aarch64 the function of getting physical core number return is 1, so thinLTO cannot work parallel.


https://reviews.llvm.org/D156193

Files:
  llvm/lib/Support/Unix/Threading.inc
  llvm/unittests/Support/Threading.cpp


Index: llvm/unittests/Support/Threading.cpp
===================================================================
--- llvm/unittests/Support/Threading.cpp
+++ llvm/unittests/Support/Threading.cpp
@@ -28,6 +28,7 @@
   // some systems.
   return (Host.isOSWindows() && llvm_is_multithreaded()) || Host.isOSDarwin() ||
          (Host.isX86() && Host.isOSLinux()) ||
+         (Host.isAArch64() && Host.isOSLinux()) ||
          (Host.isOSLinux() && !Host.isAndroid()) ||
          (Host.isSystemZ() && Host.isOSzOS());
 #else
Index: llvm/lib/Support/Unix/Threading.inc
===================================================================
--- llvm/lib/Support/Unix/Threading.inc
+++ llvm/lib/Support/Unix/Threading.inc
@@ -371,6 +371,49 @@
   }
   return CPU_COUNT(&Enabled);
 }
+#elif defined(__linux__) && defined(__aarch64__)
+static int computeHostNumPhysicalCores() {
+  cpu_set_t Affinity, Enabled;
+  if (sched_getaffinity(0, sizeof(Affinity), &Affinity) == 0 &&
+      CPU_COUNT(&Affinity) != 0)
+    return CPU_COUNT(&Affinity);
+
+  // Get affinity failed, check the core number.
+  // Read /proc/cpuinfo as a stream (until EOF reached). It cannot be
+  // mmapped because it appears to have 0 size.
+  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
+      llvm::MemoryBuffer::getFileAsStream("/proc/cpuinfo");
+  if (std::error_code EC = Text.getError()) {
+    llvm::errs() << "Can't read "
+                 << "/proc/cpuinfo: " << EC.message() << "\n";
+    return -1;
+  }
+  SmallVector<StringRef, 8> strs;
+  (*Text)->getBuffer().split(strs, "\n", -1, false);
+  int CurProcessor = -1;
+  int CurPhysicalId = -1;
+  int CurSiblings = -1;
+  int CurCoreId = -1;
+  for (StringRef Line : strs) {
+    std::pair<StringRef, StringRef> Data = Line.split(':');
+    auto Name = Data.first.trim();
+    auto Val = Data.second.trim();
+    // These fields are available if the kernel is configured with CONFIG_SMP.
+    if (Name == "processor")
+      Val.getAsInteger(10, CurProcessor);
+    else if (Name == "physical id")
+      Val.getAsInteger(10, CurPhysicalId);
+    else if (Name == "siblings")
+      Val.getAsInteger(10, CurSiblings);
+    else if (Name == "core id") {
+      Val.getAsInteger(10, CurCoreId);
+      // The processor id corresponds to an index into cpu_set_t.
+      if (CPU_ISSET(CurProcessor, &Affinity))
+        CPU_SET(CurPhysicalId * CurSiblings + CurCoreId, &Enabled);
+    }
+  }
+  return CPU_COUNT(&Enabled);
+}
 #elif defined(__linux__) && defined(__s390x__)
 static int computeHostNumPhysicalCores() {
   return sysconf(_SC_NPROCESSORS_ONLN);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156193.543770.patch
Type: text/x-patch
Size: 2596 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230725/90bf321b/attachment.bin>


More information about the llvm-commits mailing list