summaryrefslogtreecommitdiff
path: root/llvm/lib/Support/ThreadPool.cpp
AgeCommit message (Collapse)Author
2025-05-20[llvm] Use llvm::is_contained (NFC) (#140742)Kazu Hirata
2024-03-02Split the llvm::ThreadPool into an abstract base class and an implementation ↵Mehdi Amini
(#82094) This decouples the public API used to enqueue tasks and wait for completion from the actual implementation, and opens up the possibility for clients to set their own thread pool implementation for the pool. https://discourse.llvm.org/t/construct-threadpool-from-vector-of-existing-threads/76883
2023-02-23Name threadpool threads.Benoit Jacob
This gives our worker threads some names that allow easily identifying them in tools such as profilers and debuggers. Differential Revision: https://reviews.llvm.org/D144297
2022-08-28Revert "[llvm] Use llvm::is_contained (NFC)"Kazu Hirata
This reverts commit ebf574f59a80ca00e234eee0b047e5f0df99587d. This patch seems to cause build failures on Windows.
2022-08-28[llvm] Use llvm::is_contained (NFC)Kazu Hirata
2022-05-04[ThreadPool] delete debug global variable if not neededLuboš Luňák
https://lab.llvm.org/buildbot/#/builders/5/builds/23099
2022-05-04[ThreadPool] add ability to group tasks into separate groupsLuboš Luňák
This is needed for parallelizing of loading modules symbols in LLDB (D122975). Currently LLDB can parallelize indexing symbols when loading a module, but modules are loaded sequentially. If LLDB index cache is enabled, this means that the cache loading is not parallelized, even though it could. However doing that creates a threadpool-within-threadpool situation, so the number of threads would not be properly limited. This change adds ThreadPoolTaskGroup as a simple type that can be used with ThreadPool calls to put tasks into groups that can be independently waited for (even recursively from within a task) but still run in the same thread pool. Differential Revision: https://reviews.llvm.org/D123225
2022-01-26Fix conditional include in ThreadPoolserge-sans-paille
Should fix https://lab.llvm.org/buildbot#builders/37/builds/10259
2022-01-26[NFC] Additional header dependency cleanup LLVMSupportserge-sans-paille
A few more forward-declarations, a few less headers. the impact on number of preprocessed lines for LLVMSupport is negligible (-3K lines) but it's always good to remove dependencies. Related discourse thread: https://llvm.discourse.group/t/include-what-you-use-include-cleanup
2022-01-21NFC (build fix): Add header for llvm::errs().Mitch Phillips
Looks like e9211e039377 unfortunately broke the sanitizer build bots, because those bots compile the symbolizer with DLLVM_ENABLE_THREADS=Off. Likely, before the patch, this header was transitively included.
2022-01-21[llvm] Cleanup header dependencies in ADT and Supportserge-sans-paille
The cleanup was manual, but assisted by "include-what-you-use". It consists in 1. Removing unused forward declaration. No impact expected. 2. Removing unused headers in .cpp files. No impact expected. 3. Removing unused headers in .h files. This removes implicit dependencies and is generally considered a good thing, but this may break downstream builds. I've updated llvm, clang, lld, lldb and mlir deps, and included a list of the modification in the second part of the commit. 4. Replacing header inclusion by forward declaration. This has the same impact as 3. Notable changes: - llvm/Support/TargetParser.h no longer includes llvm/Support/AArch64TargetParser.h nor llvm/Support/ARMTargetParser.h - llvm/Support/TypeSize.h no longer includes llvm/Support/WithColor.h - llvm/Support/YAMLTraits.h no longer includes llvm/Support/Regex.h - llvm/ADT/SmallVector.h no longer includes llvm/Support/MemAlloc.h nor llvm/Support/ErrorHandling.h You may need to add some of these headers in your compilation units, if needs be. As an hint to the impact of the cleanup, running clang++ -E -Iinclude -I../llvm/include ../llvm/lib/Support/*.cpp -std=c++14 -fno-rtti -fno-exceptions | wc -l before: 8000919 lines after: 7917500 lines Reduced dependencies also helps incremental rebuilds and is more ccache friendly, something not shown by the above metric :-) Discourse thread on the topic: https://llvm.discourse.group/t/include-what-you-use-include-cleanup/5831
2022-01-17[Support] Fix -Wreturn-type in LLVM_ENABLE_THREADS=OFF build after D116846Fangrui Song
2022-01-08[MLIR] Fix compilation with LLVM_ENABLE_THREADS=OFFJohn Demme
Currently, compiles with LLVM_ENABLE_THREADS=OFF fail due to this symbol missing. Add it but assert as calling code is (and should be) checking that threading is enabled. Differential Revision: https://reviews.llvm.org/D116846
2021-12-04Split the locking of the queue and the threads vector in the ThreadPool ↵Mehdi Amini
implementation This allows to release the QueueLock early and create Thread independently of the queue processing. Differential Revision: https://reviews.llvm.org/D115078
2021-12-04Fix build for ThreadPool when using -DLLVM_ENABLE_THREADS=OFFMehdi Amini
Differential Revision: https://reviews.llvm.org/D115019
2021-12-03ThreadPool: grow the pool only as neededBenoit Jacob
On my 96-core cloudtop 'machine', it seems unnecessary to always start 96 threads upfront... particularly as the ThreadPool is created even with -mlir-disable-threading. Things like the resuling spew in GDB and the obfuscated output of `(gdb) info threads` are my motivation here, but it probably also doesn't hurt for at least some efficiency metrics to avoid creating many threads upfront. Reviewed By: mehdi_amini Differential Revision: https://reviews.llvm.org/D115019
2021-11-25Recommit [ThreadPool] Support returning futures with results.Florian Hahn
This reverts commit 71a7c55f0f021b04b9a7303d0cd391b9161cf303. The revert broken building llvm-reduce and it is not clear it fixes an issue with LLVM_ENABLE_THREADS=OFF. See discussion in https://reviews.llvm.org/D114183 for more details.
2021-11-25Revert "[ThreadPool] Support returning futures with results."Daniel McIntosh
This reverts commit 6149e57dc1313d32c85524f8009a1249e0b8f4d1. The offending commit broke building with LLVM_ENABLE_THREADS=OFF.
2021-11-22[ThreadPool] Support returning futures with results.Florian Hahn
This patch adjusts ThreadPool::async to return futures that wrap the result type of the passed in callable. To do so, ThreadPool::asyncImpl first creates a shared promise. The result of the promise is set in a new callable that first executes the task. The callable is added to the task queue. Reviewed By: mehdi_amini Differential Revision: https://reviews.llvm.org/D114183
2021-07-08Recommit: Support: add llvm::thread class that supports specifying stack size.Tim Northover
This adds a new llvm::thread class with the same interface as std::thread except there is an extra constructor that allows us to set the new thread's stack size. On Darwin even the default size is boosted to 8MB to match the main thread. It also switches all users of the older C-style `llvm_execute_on_thread` API family over to `llvm::thread` followed by either a `detach` or `join` call and removes the old API. Moved definition of DefaultStackSize into the .cpp file to hopefully fix the build on some (GCC-6?) machines.
2021-07-08Revert "Support: add llvm::thread class that supports specifying stack size."Tim Northover
It's causing build failures because DefaultStackSize isn't defined everywhere it should be and I need time to investigate.
2021-07-08Support: add llvm::thread class that supports specifying stack size.Tim Northover
This adds a new llvm::thread class with the same interface as std::thread except there is an extra constructor that allows us to set the new thread's stack size. On Darwin even the default size is boosted to 8MB to match the main thread. It also switches all users of the older C-style `llvm_execute_on_thread` API family over to `llvm::thread` followed by either a `detach` or `join` call and removes the old API.
2021-06-23[mlir] Add a ThreadPool to MLIRContext and refactor MLIR threading usageRiver Riddle
This revision refactors the usage of multithreaded utilities in MLIR to use a common thread pool within the MLIR context, in addition to a new utility that makes writing multi-threaded code in MLIR less error prone. Using a unified thread pool brings about several advantages: * Better thread usage and more control We currently use the static llvm threading utilities, which do not allow multiple levels of asynchronous scheduling (even if there are open threads). This is due to how the current TaskGroup structure works, which only allows one truly multithreaded instance at a time. By having our own ThreadPool we gain more control and flexibility over our job/thread scheduling, and in a followup can enable threading more parts of the compiler. * The static nature of TaskGroup causes issues in certain configurations Due to the static nature of TaskGroup, there have been quite a few problems related to destruction that have caused several downstream projects to disable threading. See D104207 for discussion on some related fallout. By having a ThreadPool scoped to the context, we don't have to worry about destruction and can ensure that any additional MLIR thread usage ends when the context is destroyed. Differential Revision: https://reviews.llvm.org/D104516
2020-04-28[Support] Simplify and optimize ThreadPoolFangrui Song
* Merge QueueLock and CompletionLock. * Avoid spurious CompletionCondition.notify_all() when ActiveThreads is greater than 0. * Use default member initializers. Reviewed By: mehdi_amini Differential Revision: https://reviews.llvm.org/D78856
2020-02-14[Support] On Windows, ensure hardware_concurrency() extends to all CPU ↵Alexandre Ganea
sockets and all NUMA groups The goal of this patch is to maximize CPU utilization on multi-socket or high core count systems, so that parallel computations such as LLD/ThinLTO can use all hardware threads in the system. Before this patch, on Windows, a maximum of 64 hardware threads could be used at most, in some cases dispatched only on one CPU socket. == Background == Windows doesn't have a flat cpu_set_t like Linux. Instead, it projects hardware CPUs (or NUMA nodes) to applications through a concept of "processor groups". A "processor" is the smallest unit of execution on a CPU, that is, an hyper-thread if SMT is active; a core otherwise. There's a limit of 32-bit processors on older 32-bit versions of Windows, which later was raised to 64-processors with 64-bit versions of Windows. This limit comes from the affinity mask, which historically is represented by the sizeof(void*). Consequently, the concept of "processor groups" was introduced for dealing with systems with more than 64 hyper-threads. By default, the Windows OS assigns only one "processor group" to each starting application, in a round-robin manner. If the application wants to use more processors, it needs to programmatically enable it, by assigning threads to other "processor groups". This also means that affinity cannot cross "processor group" boundaries; one can only specify a "preferred" group on start-up, but the application is free to allocate more groups if it wants to. This creates a peculiar situation, where newer CPUs like the AMD EPYC 7702P (64-cores, 128-hyperthreads) are projected by the OS as two (2) "processor groups". This means that by default, an application can only use half of the cores. This situation could only get worse in the years to come, as dies with more cores will appear on the market. == The problem == The heavyweight_hardware_concurrency() API was introduced so that only *one hardware thread per core* was used. Once that API returns, that original intention is lost, only the number of threads is retained. Consider a situation, on Windows, where the system has 2 CPU sockets, 18 cores each, each core having 2 hyper-threads, for a total of 72 hyper-threads. Both heavyweight_hardware_concurrency() and hardware_concurrency() currently return 36, because on Windows they are simply wrappers over std::thread::hardware_concurrency() -- which can only return processors from the current "processor group". == The changes in this patch == To solve this situation, we capture (and retain) the initial intention until the point of usage, through a new ThreadPoolStrategy class. The number of threads to use is deferred as late as possible, until the moment where the std::threads are created (ThreadPool in the case of ThinLTO). When using hardware_concurrency(), setting ThreadCount to 0 now means to use all the possible hardware CPU (SMT) threads. Providing a ThreadCount above to the maximum number of threads will have no effect, the maximum will be used instead. The heavyweight_hardware_concurrency() is similar to hardware_concurrency(), except that only one thread per hardware *core* will be used. When LLVM_ENABLE_THREADS is OFF, the threading APIs will always return 1, to ensure any caller loops will be exercised at least once. Differential Revision: https://reviews.llvm.org/D71775
2019-01-19Update the file headers across all of the LLVM projects in the monorepoChandler Carruth
to reflect the new license. We understand that people may be surprised that we're moving the header entirely to discuss the new license. We checked this carefully with the Foundation's lawyer and we believe this is the correct approach. Essentially, all code in the project is now made available by the LLVM project under our new license, so you will see that the license headers include that license only. Some of our contributors have contributed code under our old license, and accordingly, we have retained a copy of our old license notice in the top-level files in each project and repository. llvm-svn: 351636
2018-06-13Revert "Enable ThreadPool to queue tasks that return values."Zachary Turner
This is failing to compile when LLVM_ENABLE_THREADS is false, and the fix is not immediately obvious, so reverting while I look into it. llvm-svn: 334658
2018-06-13Enable ThreadPool to support tasks that return values.Zachary Turner
Previously ThreadPool could only queue async "jobs", i.e. work that was done for its side effects and not for its result. It's useful occasionally to queue async work that returns a value. From an API perspective, this is very intuitive. The previous API just returned a shared_future<void>, so all we need to do is make it return a shared_future<T>, where T is the type of value that the operation returns. Making this work required a little magic, but ultimately it's not too bad. Instead of keeping a shared queue<packaged_task<void()>> we just keep a shared queue<unique_ptr<TaskBase>>, where TaskBase is a class with a pure virtual execute() method, then have a templated derived class that stores a packaged_task<T()>. Everything else works out pretty cleanly. Differential Revision: https://reviews.llvm.org/D48115 llvm-svn: 334643
2017-12-13Speculative build fix for lld on Linux after Michael's #include removalsHans Wennborg
llvm-svn: 320645
2017-12-13Remove redundant includes from lib/Support.Michael Zolotukhin
llvm-svn: 320627
2017-11-27[Support] Fix locking of shared variable in threadpoolJan Korous
llvm-svn: 319027
2017-10-04Bring r314809 back.Rafael Espindola
But now include a check for CPU_COUNT so we still build on 10 year old versions of glibc. Original message: Use sched_getaffinity instead of std::thread::hardware_concurrency. The issue with std::thread::hardware_concurrency is that it forwards to libc and some implementations (like glibc) don't take thread affinity into consideration. With this change a llvm program that can execute in only 2 cores will use 2 threads, even if the machine has 32 cores. This makes benchmarking a lot easier, but should also help if someone doesn't want to use all cores for compilation for example. llvm-svn: 314931
2017-10-04Revert D38481 due to missing cmake check for CPU_COUNTDaniel Neilson
Summary: This reverts D38481. The change breaks systems with older versions of glibc. It injects a use of CPU_COUNT() from sched.h without checking to ensure that the function exists first. Reviewers: Subscribers: llvm-svn: 314922
2017-10-03Use sched_getaffinity instead of std::thread::hardware_concurrency.Rafael Espindola
The issue with std::thread::hardware_concurrency is that it forwards to libc and some implementations (like glibc) don't take thread affinity into consideration. With this change a llvm program that can execute in only 2 cores will use 2 threads, even if the machine has 32 cores. This makes benchmarking a lot easier, but should also help if someone doesn't want to use all cores for compilation for example. llvm-svn: 314809
2017-06-14Support: Remove MSVC 2013 workarounds in ThreadPool class.Peter Collingbourne
I have confirmed that these are no longer needed with MSVC 2015. Differential Revision: https://reviews.llvm.org/D34187 llvm-svn: 305347
2016-11-28[ThreadPool] Rollback recent changes until I figure out the breakage.Davide Italiano
llvm-svn: 288018
2016-11-28[ThreadPool] Simplify the interface. NFCI.Davide Italiano
The callers don't use the return value. Found by Michael Spencer. llvm-svn: 288016
2016-06-22Removing whitespace from test commit rL273447Jason Henline
Undoing the trivial change I introduced in rL273447. llvm-svn: 273449
2016-06-22Add whitespace to check commit accessJason Henline
No functional changes. Just adding whitespace in a comment in order to check that I am able to push a commit to the repo. llvm-svn: 273447
2016-04-06Fix a race condition in support library ThreadPool.Justin Lebar
By running TSAN on the ThreadPool unit tests it was discovered that the threads in the pool can pop tasks off the queue at the same time the "wait" routine is trying to check if the task queue is empty. This patch fixes this problem by checking for active threads in the waiter before checking whether the queue is empty. Patch by Jason Henline. Differential Revision: http://reviews.llvm.org/D18811 Reviewers: joker.eph, jlebar llvm-svn: 265618
2015-12-15Fix MSVC build with LLVM_ENABLE_THREADS=OFFMehdi Amini
Follow-up to the ThreadPool implementation. From: Mehdi Amini <mehdi.amini@apple.com> llvm-svn: 255621
2015-12-15Add a C++11 ThreadPool implementation in LLVMMehdi Amini
This is a very simple implementation of a thread pool using C++11 thread. It accepts any std::function<void()> for asynchronous execution. Individual task can be synchronize using the returned future, or the client can block on the full queue completion. In case LLVM is configured with Threading disabled, it falls back to sequential execution using std::async with launch:deferred. This is intended to support parallelism for ThinLTO processing in linker plugin, but is generic enough for any other uses. This is a recommit of r255444 ; trying to workaround a bug in the MSVC 2013 standard library. I think I was hit by: http://connect.microsoft.com/VisualStudio/feedbackdetail/view/791185/std-packaged-task-t-where-t-is-void-or-a-reference-class-are-not-movable Recommit of r255589, trying to please g++ as well. Differential Revision: http://reviews.llvm.org/D15464 From: mehdi_amini <mehdi_amini@91177308-0d34-0410-b5e6-96231b3b80d8> llvm-svn: 255593
2015-12-15Revert "Add a C++11 ThreadPool implementation in LLVM"Mehdi Amini
This reverts commit r255589. Breaks g++ From: Mehdi Amini <mehdi.amini@apple.com> llvm-svn: 255591
2015-12-15Add a C++11 ThreadPool implementation in LLVMMehdi Amini
This is a very simple implementation of a thread pool using C++11 thread. It accepts any std::function<void()> for asynchronous execution. Individual task can be synchronize using the returned future, or the client can block on the full queue completion. In case LLVM is configured with Threading disabled, it falls back to sequential execution using std::async with launch:deferred. This is intended to support parallelism for ThinLTO processing in linker plugin, but is generic enough for any other uses. This is a recommit of r255444 ; trying to workaround a bug in the MSVC 2013 standard library. I think I was hit by: http://connect.microsoft.com/VisualStudio/feedbackdetail/view/791185/std-packaged-task-t-where-t-is-void-or-a-reference-class-are-not-movable Differential Revision: http://reviews.llvm.org/D15464 From: Mehdi Amini <mehdi.amini@apple.com> llvm-svn: 255589
2015-12-13Revert r255444.Nico Weber
It doesn't build on Windows and broke the Windows LLD and LLDB bots: http://lab.llvm.org:8011/builders/lld-x86_64-win7/builds/27693/steps/build_Lld/logs/stdio http://lab.llvm.org:8011/builders/lldb-x86-windows-msvc/builds/13468/steps/build/logs/stdio llvm-svn: 255446
2015-12-12Add a C++11 ThreadPool implementation in LLVMMehdi Amini
This is a very simple implementation of a thread pool using C++11 thread. It accepts any std::function<void()> for asynchronous execution. Individual task can be synchronize using the returned future, or the client can block on the full queue completion. In case LLVM is configured with Threading disabled, it falls back to sequential execution using std::async with launch:deferred. This is intended to support parallelism for ThinLTO processing in linker plugin, but is generic enough for any other uses. Differential Revision: http://reviews.llvm.org/D15464 From: Mehdi Amini <mehdi.amini@apple.com> llvm-svn: 255444