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 
22 using namespace llvm;
23 
24 namespace opts {
25 extern cl::opt<unsigned> ThreadCount;
26 extern cl::opt<bool> NoThreads;
27 extern cl::opt<unsigned> TaskCount;
28 } // namespace opts
29 
30 namespace llvm {
31 class ThreadPool;
32 
33 namespace bolt {
34 class BinaryContext;
35 class BinaryFunction;
36 
37 namespace ParallelUtilities {
38 
39 using WorkFuncWithAllocTy =
40     std::function<void(BinaryFunction &BF, MCPlusBuilder::AllocatorIdTy)>;
41 using WorkFuncTy = std::function<void(BinaryFunction &BF)>;
42 using PredicateTy = std::function<bool(const BinaryFunction &BF)>;
43 
44 enum SchedulingPolicy {
45   SP_TRIVIAL,     /// cost is estimated by the number of functions
46   SP_CONSTANT,    /// cost is estimated by the number of non-skipped functions
47   SP_INST_LINEAR, /// cost is estimated by inst count
48   SP_INST_QUADRATIC, /// cost is estimated by the square of the inst count
49   SP_BB_LINEAR,      /// cost is estimated by BB count
50   SP_BB_QUADRATIC,   /// cost is estimated by the square of the BB count
51 };
52 
53 /// Return the managed thread pool and initialize it if not initiliazed.
54 ThreadPool &getThreadPool();
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