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
23using namespace llvm;
24
25namespace opts {
26extern cl::opt<unsigned> ThreadCount;
27extern cl::opt<bool> NoThreads;
28extern cl::opt<unsigned> TaskCount;
29} // namespace opts
30
31namespace llvm {
32namespace bolt {
33class BinaryContext;
34class BinaryFunction;
35
36namespace ParallelUtilities {
37
38using WorkFuncWithAllocTy =
39 std::function<void(BinaryFunction &BF, MCPlusBuilder::AllocatorIdTy)>;
40using WorkFuncTy = std::function<void(BinaryFunction &BF)>;
41using PredicateTy = std::function<bool(const BinaryFunction &BF)>;
42
43enum 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.
53ThreadPoolInterface &getThreadPool();
54
55/// Perform the work on each BinaryFunction except those that are accepted
56/// by SkipPredicate, scheduling heuristic is based on SchedPolicy.
57/// ForceSequential will selectively disable parallel execution and perform the
58/// work sequentially.
59void runOnEachFunction(BinaryContext &BC, SchedulingPolicy SchedPolicy,
60 WorkFuncTy WorkFunction,
61 PredicateTy SkipPredicate = PredicateTy(),
62 std::string LogName = "", bool ForceSequential = false,
63 unsigned TasksPerThread = opts::TaskCount);
64
65/// Perform the work on each BinaryFunction except those that are rejected
66/// by SkipPredicate, and create a unique annotation allocator for each
67/// task. This should be used whenever the work function creates annotations to
68/// allow thread-safe annotation creation.
69/// ForceSequential will selectively disable parallel execution and perform the
70/// work sequentially.
71void runOnEachFunctionWithUniqueAllocId(
72 BinaryContext &BC, SchedulingPolicy SchedPolicy,
73 WorkFuncWithAllocTy WorkFunction, PredicateTy SkipPredicate,
74 std::string LogName = "", bool ForceSequential = false,
75 unsigned TasksPerThread = opts::TaskCount);
76
77} // namespace ParallelUtilities
78} // namespace bolt
79} // namespace llvm
80#endif
81

source code of bolt/include/bolt/Core/ParallelUtilities.h