[PATCH] [lld][Driver] Add maximum concurrency option.

Michael Spencer bigcheesegs at gmail.com
Fri Apr 19 17:32:48 PDT 2013


Hi shankarke, kledzik,

http://llvm-reviews.chandlerc.com/D698

Files:
  include/lld/Core/Parallel.h
  include/lld/Core/TargetInfo.h
  lib/Core/TargetInfo.cpp
  lib/Driver/Driver.cpp
  lib/Driver/GnuLdDriver.cpp
  lib/Driver/LDOptions.td

Index: include/lld/Core/Parallel.h
===================================================================
--- include/lld/Core/Parallel.h
+++ include/lld/Core/Parallel.h
@@ -73,12 +73,27 @@
   virtual void add(std::function<void()> func) = 0;
 };
 
+namespace detail {
+  inline unsigned &getDefaultExecutorMaxConcurrency() {
+    static unsigned maxCur = std::thread::hardware_concurrency();
+    return maxCur;
+  }
+}
+
+inline void setDefaultExecutorMaxConcurrency(unsigned maxCur) {
+  detail::getDefaultExecutorMaxConcurrency() = maxCur;
+}
+
+inline unsigned getDefaultExecutorMaxConcurrency() {
+  return detail::getDefaultExecutorMaxConcurrency();
+}
+
 /// \brief An implementation of an Executor that runs closures on a thread pool
 ///   in filo order.
 class ThreadPoolExecutor : public Executor {
 public:
   ThreadPoolExecutor(unsigned threadCount =
-                         std::thread::hardware_concurrency())
+                         getDefaultExecutorMaxConcurrency())
       : _stop(false), _done(threadCount) {
     // Spawn all but one of the threads in another thread as spawning threads
     // can take a while.
Index: include/lld/Core/TargetInfo.h
===================================================================
--- include/lld/Core/TargetInfo.h
+++ include/lld/Core/TargetInfo.h
@@ -174,6 +174,11 @@
     return _logInputFiles;
   }
 
+  /// \brief Maximum number of concurrent tasks.
+  unsigned maxConcurrency() const {
+    return _maxConcurrency;
+  }
+
   /// Parts of LLVM use global variables which are bound to command line
   /// options (see llvm::cl::Options). This method returns "command line"
   /// options which are used to configure LLVM's command line settings.
@@ -227,6 +232,9 @@
   void setLogInputFiles(bool log) {
     _logInputFiles = log;
   }
+  void setMaxConcurrency(unsigned c) {
+    _maxConcurrency = c;
+  }
   void appendInputFile(StringRef path) {
     _inputFiles.emplace_back(LinkerInput(path));
   }
@@ -336,6 +344,7 @@
   bool                     _allowRemainingUndefines;
   bool                     _logInputFiles;
   bool                     _allowShlibUndefines;
+  unsigned                 _maxConcurrency;
   std::vector<StringRef>   _deadStripRoots;
   std::vector<LinkerInput> _inputFiles;
   std::vector<const char*> _llvmOptions;
Index: lib/Core/TargetInfo.cpp
===================================================================
--- lib/Core/TargetInfo.cpp
+++ lib/Core/TargetInfo.cpp
@@ -22,7 +22,7 @@
       _warnIfCoalesableAtomsHaveDifferentLoadName(false),
       _forceLoadAllArchives(false), _printRemainingUndefines(true),
       _allowRemainingUndefines(false), _logInputFiles(false),
-      _allowShlibUndefines(false) {}
+      _allowShlibUndefines(false), _maxConcurrency(0) {}
 
 TargetInfo::~TargetInfo() {}
 
Index: lib/Driver/Driver.cpp
===================================================================
--- lib/Driver/Driver.cpp
+++ lib/Driver/Driver.cpp
@@ -40,6 +40,9 @@
     llvm::cl::ParseCommandLineOptions(numArgs + 1, args);
   }
 
+  if (targetInfo.maxConcurrency())
+    setDefaultExecutorMaxConcurrency(targetInfo.maxConcurrency());
+
   // Read inputs
   std::vector<std::vector<std::unique_ptr<File>>> files(
       targetInfo.inputFiles().size());
Index: lib/Driver/GnuLdDriver.cpp
===================================================================
--- lib/Driver/GnuLdDriver.cpp
+++ lib/Driver/GnuLdDriver.cpp
@@ -193,6 +193,13 @@
   if (parsedArgs->getLastArg(OPT_use_shlib_undefs))
     options->setUseShlibUndefines(true);
 
+  // --thread-count
+  if (auto Arg = parsedArgs->getLastArg(OPT_thread_count)) {
+    unsigned maxConcurrency;
+    StringRef(Arg->getValue()).getAsInteger(10, maxConcurrency);
+    options->setMaxConcurrency(maxConcurrency);
+  }
+
   // Handle -Lxxx
   for (llvm::opt::arg_iterator it = parsedArgs->filtered_begin(OPT_L),
                                ie = parsedArgs->filtered_end();
Index: lib/Driver/LDOptions.td
===================================================================
--- lib/Driver/LDOptions.td
+++ lib/Driver/LDOptions.td
@@ -65,3 +65,5 @@
     HelpText<"Write YAML instead of ELF">;
 def force_load : Flag<["--"], "force-load">,
     HelpText<"Force load of all members in all static libraries">;
+def thread_count : Separate<["--"], "thread-count">,
+    HelpText<"Maximum number of concurrently running tasks">;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D698.1.patch
Type: text/x-patch
Size: 4378 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130419/ea2cfbee/attachment.bin>


More information about the llvm-commits mailing list