Coverage Report

Created: 2017-10-03 07:32

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