xref: /llvm-project/bolt/include/bolt/Core/ParallelUtilities.h (revision 6aad62cf5b7f91f4b02266cf72469e2c8e28dbef)
1 //===- bolt/Core/ParallelUtilities.h - Parallel utilities -------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains functions for assisting parallel processing of binary
10 // functions. Several scheduling criteria are supported using SchedulingPolicy,
11 // and are defined by how the runtime cost should be estimated. If the NoThreads
12 // flags is passed, all jobs will execute sequentially.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef BOLT_CORE_PARALLEL_UTILITIES_H
17 #define BOLT_CORE_PARALLEL_UTILITIES_H
18 
19 #include "bolt/Core/MCPlusBuilder.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/ThreadPool.h"
22 
23 using namespace llvm;
24 
25 namespace opts {
26 extern cl::opt<unsigned> ThreadCount;
27 extern cl::opt<bool> NoThreads;
28 extern cl::opt<unsigned> TaskCount;
29 } // namespace opts
30 
31 namespace llvm {
32 namespace bolt {
33 class BinaryContext;
34 class BinaryFunction;
35 
36 namespace ParallelUtilities {
37 
38 using WorkFuncWithAllocTy =
39     std::function<void(BinaryFunction &BF, MCPlusBuilder::AllocatorIdTy)>;
40 using WorkFuncTy = std::function<void(BinaryFunction &BF)>;
41 using PredicateTy = std::function<bool(const BinaryFunction &BF)>;
42 
43 enum SchedulingPolicy {
44   SP_TRIVIAL,     /// cost is estimated by the number of functions
45   SP_CONSTANT,    /// cost is estimated by the number of non-skipped functions
46   SP_INST_LINEAR, /// cost is estimated by inst count
47   SP_INST_QUADRATIC, /// cost is estimated by the square of the inst count
48   SP_BB_LINEAR,      /// cost is estimated by BB count
49   SP_BB_QUADRATIC,   /// cost is estimated by the square of the BB count
50 };
51 
52 /// Return the managed thread pool and initialize it if not initialized.
53 ThreadPoolInterface &
54 getThreadPool(const unsigned ThreadsCount = opts::ThreadCount);
55 
56 /// Perform the work on each BinaryFunction except those that are accepted
57 /// by SkipPredicate, scheduling heuristic is based on SchedPolicy.
58 /// ForceSequential will selectively disable parallel execution and perform the
59 /// work sequentially.
60 void runOnEachFunction(BinaryContext &BC, SchedulingPolicy SchedPolicy,
61                        WorkFuncTy WorkFunction,
62                        PredicateTy SkipPredicate = PredicateTy(),
63                        std::string LogName = "", bool ForceSequential = false,
64                        unsigned TasksPerThread = opts::TaskCount);
65 
66 /// Perform the work on each BinaryFunction except those that are rejected
67 /// by SkipPredicate, and create a unique annotation allocator for each
68 /// task. This should be used whenever the work function creates annotations to
69 /// allow thread-safe annotation creation.
70 /// ForceSequential will selectively disable parallel execution and perform the
71 /// work sequentially.
72 void runOnEachFunctionWithUniqueAllocId(
73     BinaryContext &BC, SchedulingPolicy SchedPolicy,
74     WorkFuncWithAllocTy WorkFunction, PredicateTy SkipPredicate,
75     std::string LogName = "", bool ForceSequential = false,
76     unsigned TasksPerThread = opts::TaskCount);
77 
78 } // namespace ParallelUtilities
79 } // namespace bolt
80 } // namespace llvm
81 #endif
82