Coverage Report

Created: 2017-06-28 17:40

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/polly/include/polly/CodeGen/LoopGenerators.h
Line
Count
Source
1
//===- LoopGenerators.h - IR helper to create loops -------------*- C++ -*-===//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
// This file contains functions to create scalar and OpenMP parallel loops
11
// as LLVM-IR.
12
//
13
//===----------------------------------------------------------------------===//
14
#ifndef POLLY_LOOP_GENERATORS_H
15
#define POLLY_LOOP_GENERATORS_H
16
17
#include "polly/CodeGen/IRBuilder.h"
18
#include "polly/Support/ScopHelper.h"
19
20
#include "llvm/ADT/SetVector.h"
21
#include "llvm/IR/ValueMap.h"
22
23
namespace llvm {
24
class Value;
25
class Pass;
26
class BasicBlock;
27
} // namespace llvm
28
29
namespace polly {
30
using namespace llvm;
31
32
/// Create a scalar do/for-style loop.
33
///
34
/// @param LowerBound The starting value of the induction variable.
35
/// @param UpperBound The upper bound of the induction variable.
36
/// @param Stride     The value by which the induction variable is incremented.
37
///
38
/// @param Builder    The builder used to create the loop.
39
/// @param P          A pointer to the pass that uses this function. It is used
40
///                   to update analysis information.
41
/// @param LI         The loop info for the current function
42
/// @param DT         The dominator tree we need to update
43
/// @param ExitBlock  The block the loop will exit to.
44
/// @param Predicate  The predicate used to generate the upper loop bound.
45
/// @param Annotator  This function can (optionally) take a ScopAnnotator which
46
///                   annotates loops and alias information in the SCoP.
47
/// @param Parallel   If this loop should be marked parallel in the Annotator.
48
/// @param UseGuard   Create a guard in front of the header to check if the
49
///                   loop is executed at least once, otherwise just assume it.
50
///
51
/// @return Value*    The newly created induction variable for this loop.
52
Value *createLoop(Value *LowerBound, Value *UpperBound, Value *Stride,
53
                  PollyIRBuilder &Builder, LoopInfo &LI, DominatorTree &DT,
54
                  BasicBlock *&ExitBlock, ICmpInst::Predicate Predicate,
55
                  ScopAnnotator *Annotator = NULL, bool Parallel = false,
56
                  bool UseGuard = true);
57
58
/// The ParallelLoopGenerator allows to create parallelized loops
59
///
60
/// To parallelize a loop, we perform the following steps:
61
///   o  Generate a subfunction which will hold the loop body.
62
///   o  Create a struct to hold all outer values needed in the loop body.
63
///   o  Create calls to a runtime library to achieve the actual parallelism.
64
///      These calls will spawn and join threads, define how the work (here the
65
///      iterations) are distributed between them and make sure each has access
66
///      to the struct holding all needed values.
67
///
68
/// At the moment we support only one parallel runtime, OpenMP.
69
///
70
/// If we parallelize the outer loop of the following loop nest,
71
///
72
///   S0;
73
///   for (int i = 0; i < N; i++)
74
///     for (int j = 0; j < M; j++)
75
///       S1(i, j);
76
///   S2;
77
///
78
/// we will generate the following code (with different runtime function names):
79
///
80
///   S0;
81
///   auto *values = storeValuesIntoStruct();
82
///   // Execute subfunction with multiple threads
83
///   spawn_threads(subfunction, values);
84
///   join_threads();
85
///   S2;
86
///
87
///  // This function is executed in parallel by different threads
88
///   void subfunction(values) {
89
///     while (auto *WorkItem = getWorkItem()) {
90
///       int LB = WorkItem.begin();
91
///       int UB = WorkItem.end();
92
///       for (int i = LB; i < UB; i++)
93
///         for (int j = 0; j < M; j++)
94
///           S1(i, j);
95
///     }
96
///     cleanup_thread();
97
///   }
98
class ParallelLoopGenerator {
99
public:
100
  /// Create a parallel loop generator for the current function.
101
  ParallelLoopGenerator(PollyIRBuilder &Builder, LoopInfo &LI,
102
                        DominatorTree &DT, const DataLayout &DL)
103
      : Builder(Builder), LI(LI), DT(DT),
104
        LongType(
105
            Type::getIntNTy(Builder.getContext(), DL.getPointerSizeInBits())),
106
26
        M(Builder.GetInsertBlock()->getParent()->getParent()) {}
107
108
  /// Create a parallel loop.
109
  ///
110
  /// This function is the main function to automatically generate a parallel
111
  /// loop with all its components.
112
  ///
113
  /// @param LB        The lower bound for the loop we parallelize.
114
  /// @param UB        The upper bound for the loop we parallelize.
115
  /// @param Stride    The stride of the loop we parallelize.
116
  /// @param Values    A set of LLVM-IR Values that should be available in
117
  ///                  the new loop body.
118
  /// @param VMap      A map to allow outside access to the new versions of
119
  ///                  the values in @p Values.
120
  /// @param LoopBody  A pointer to an iterator that is set to point to the
121
  ///                  body of the created loop. It should be used to insert
122
  ///                  instructions that form the actual loop body.
123
  ///
124
  /// @return The newly created induction variable for this loop.
125
  Value *createParallelLoop(Value *LB, Value *UB, Value *Stride,
126
                            SetVector<Value *> &Values, ValueMapT &VMap,
127
                            BasicBlock::iterator *LoopBody);
128
129
private:
130
  /// The IR builder we use to create instructions.
131
  PollyIRBuilder &Builder;
132
133
  /// The loop info of the current function we need to update.
134
  LoopInfo &LI;
135
136
  /// The dominance tree of the current function we need to update.
137
  DominatorTree &DT;
138
139
  /// The type of a "long" on this hardware used for backend calls.
140
  Type *LongType;
141
142
  /// The current module
143
  Module *M;
144
145
public:
146
  /// The functions below can be used if one does not want to generate a
147
  /// specific OpenMP parallel loop, but generate individual parts of it
148
  /// (e.g., the subfunction definition).
149
150
  /// Create a runtime library call to spawn the worker threads.
151
  ///
152
  /// @param SubFn      The subfunction which holds the loop body.
153
  /// @param SubFnParam The parameter for the subfunction (basically the struct
154
  ///                   filled with the outside values).
155
  /// @param LB         The lower bound for the loop we parallelize.
156
  /// @param UB         The upper bound for the loop we parallelize.
157
  /// @param Stride     The stride of the loop we parallelize.
158
  void createCallSpawnThreads(Value *SubFn, Value *SubFnParam, Value *LB,
159
                              Value *UB, Value *Stride);
160
161
  /// Create a runtime library call to join the worker threads.
162
  void createCallJoinThreads();
163
164
  /// Create a runtime library call to get the next work item.
165
  ///
166
  /// @param LBPtr A pointer value to store the work item begin in.
167
  /// @param UBPtr A pointer value to store the work item end in.
168
  ///
169
  /// @returns A true value if the work item is not empty.
170
  Value *createCallGetWorkItem(Value *LBPtr, Value *UBPtr);
171
172
  /// Create a runtime library call to allow cleanup of the thread.
173
  ///
174
  /// @note This function is called right before the thread will exit the
175
  ///       subfunction and only if the runtime system depends on it.
176
  void createCallCleanupThread();
177
178
  /// Create a struct for all @p Values and store them in there.
179
  ///
180
  /// @param Values The values which should be stored in the struct.
181
  ///
182
  /// @return The created struct.
183
  AllocaInst *storeValuesIntoStruct(SetVector<Value *> &Values);
184
185
  /// Extract all values from the @p Struct and construct the mapping.
186
  ///
187
  /// @param Values The values which were stored in the struct.
188
  /// @param Struct The struct holding all the values in @p Values.
189
  /// @param VMap   A map to associate every element of @p Values with the
190
  ///               new llvm value loaded from the @p Struct.
191
  void extractValuesFromStruct(SetVector<Value *> Values, Type *Ty,
192
                               Value *Struct, ValueMapT &VMap);
193
194
  /// Create the definition of the parallel subfunction.
195
  Function *createSubFnDefinition();
196
197
  /// Create the parallel subfunction.
198
  ///
199
  /// @param Stride The induction variable increment.
200
  /// @param Struct A struct holding all values in @p Values.
201
  /// @param Values A set of LLVM-IR Values that should be available in
202
  ///               the new loop body.
203
  /// @param VMap   A map to allow outside access to the new versions of
204
  ///               the values in @p Values.
205
  /// @param SubFn  The newly created subfunction is returned here.
206
  ///
207
  /// @return The newly created induction variable.
208
  Value *createSubFn(Value *Stride, AllocaInst *Struct,
209
                     SetVector<Value *> UsedValues, ValueMapT &VMap,
210
                     Function **SubFn);
211
};
212
} // end namespace polly
213
#endif