[llvm] r284180 - Add interface for querying physical hardware concurrency

Teresa Johnson via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 13 17:14:00 PDT 2016


Author: tejohnson
Date: Thu Oct 13 19:13:59 2016
New Revision: 284180

URL: http://llvm.org/viewvc/llvm-project?rev=284180&view=rev
Log:
Add interface for querying physical hardware concurrency

Summary:
This will be used by ThinLTO to set the amount of backend
parallelism, which performs better when restricted to the number
of physical cores (on X86 at least, where getHostNumPhysicalCores is
currently defined). If not available this falls back to
thread::hardware_concurrency.

Note I didn't add to the thread class since that is a typedef to
std::thread where available.

Reviewers: mehdi_amini

Subscribers: beanz, llvm-commits, mgorny

Differential Revision: https://reviews.llvm.org/D25585

Added:
    llvm/trunk/unittests/Support/Threading.cpp
Modified:
    llvm/trunk/include/llvm/Support/Threading.h
    llvm/trunk/lib/Support/Threading.cpp
    llvm/trunk/unittests/Support/CMakeLists.txt

Modified: llvm/trunk/include/llvm/Support/Threading.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Threading.h?rev=284180&r1=284179&r2=284180&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Threading.h (original)
+++ llvm/trunk/include/llvm/Support/Threading.h Thu Oct 13 19:13:59 2016
@@ -115,6 +115,10 @@ namespace llvm {
     TsanHappensAfter(&flag);
 #endif
   }
+
+  /// Get the amount of currency based on physical cores, if available for the
+  /// host system, otherwise falls back to thread::hardware_concurrency().
+  unsigned hardware_physical_concurrency();
 }
 
 #endif

Modified: llvm/trunk/lib/Support/Threading.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Threading.cpp?rev=284180&r1=284179&r2=284180&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Threading.cpp (original)
+++ llvm/trunk/lib/Support/Threading.cpp Thu Oct 13 19:13:59 2016
@@ -15,6 +15,7 @@
 #include "llvm/Support/Threading.h"
 #include "llvm/Config/config.h"
 #include "llvm/Support/Atomic.h"
+#include "llvm/Support/Host.h"
 #include "llvm/Support/Mutex.h"
 #include "llvm/Support/thread.h"
 #include <cassert>
@@ -116,3 +117,10 @@ void llvm::llvm_execute_on_thread(void (
 }
 
 #endif
+
+unsigned llvm::hardware_physical_concurrency() {
+  int NumPhysical = sys::getHostNumPhysicalCores();
+  if (NumPhysical == -1)
+    return thread::hardware_concurrency();
+  return NumPhysical;
+}

Modified: llvm/trunk/unittests/Support/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/CMakeLists.txt?rev=284180&r1=284179&r2=284180&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/CMakeLists.txt (original)
+++ llvm/trunk/unittests/Support/CMakeLists.txt Thu Oct 13 19:13:59 2016
@@ -40,6 +40,7 @@ add_llvm_unittest(SupportTests
   StringPool.cpp
   SwapByteOrderTest.cpp
   TargetParserTest.cpp
+  Threading.cpp
   ThreadLocalTest.cpp
   ThreadPool.cpp
   TimerTest.cpp

Added: llvm/trunk/unittests/Support/Threading.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/Threading.cpp?rev=284180&view=auto
==============================================================================
--- llvm/trunk/unittests/Support/Threading.cpp (added)
+++ llvm/trunk/unittests/Support/Threading.cpp Thu Oct 13 19:13:59 2016
@@ -0,0 +1,25 @@
+//===- unittests/Threading.cpp - Thread tests -----------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Support/Threading.h"
+#include "llvm/Support/thread.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+namespace {
+
+TEST(Threading, PhysicalConcurrency) {
+  auto Num = hardware_physical_concurrency();
+  // Since Num is unsigned this will also catch us trying to
+  // return -1.
+  ASSERT_LE(Num, thread::hardware_concurrency());
+}
+
+} // end anon namespace




More information about the llvm-commits mailing list