Polly 24.0.0git
LoopGenerators.h
Go to the documentation of this file.
1//===- LoopGenerators.h - IR helper to create loops -------------*- 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 to create scalar and OpenMP parallel loops
10// as LLVM-IR.
11//
12//===----------------------------------------------------------------------===//
13#ifndef POLLY_LOOP_GENERATORS_H
14#define POLLY_LOOP_GENERATORS_H
15
18#include "llvm/ADT/SetVector.h"
19
20namespace polly {
21using llvm::AllocaInst;
22using llvm::BasicBlock;
23using llvm::DataLayout;
24using llvm::DominatorTree;
25using llvm::Function;
26using llvm::ICmpInst;
27using llvm::LoopInfo;
28using llvm::Module;
29using llvm::SetVector;
30using llvm::Type;
31using llvm::Value;
32
33/// General scheduling types of parallel OpenMP for loops.
34/// Initialization values taken from OpenMP's enum in kmp.h: sched_type.
35/// Currently, only 'static' scheduling may change from chunked to non-chunked.
43
44extern int PollyNumThreads;
46extern int PollyChunkSize;
47
48/// Create a scalar do/for-style loop.
49///
50/// @param LowerBound The starting value of the induction variable.
51/// @param UpperBound The upper bound of the induction variable.
52/// @param Stride The value by which the induction variable
53/// is incremented.
54///
55/// @param Builder The builder used to create the loop.
56/// @param P A pointer to the pass that uses this function.
57/// It is used to update analysis information.
58/// @param LI The loop info we need to update
59/// @param DT The dominator tree we need to update
60/// @param ExitBlock The block the loop will exit to.
61/// @param Predicate The predicate used to generate the upper loop
62/// bound.
63/// @param Annotator This function can (optionally) take
64/// a ScopAnnotator which
65/// annotates loops and alias information in the SCoP.
66/// @param Parallel If this loop should be marked parallel in
67/// the Annotator.
68/// @param UseGuard Create a guard in front of the header to check if
69/// the loop is executed at least once, otherwise just
70/// assume it.
71/// @param LoopVectDisabled If the Loop vectorizer should be disabled for this
72/// loop.
73/// @param SkipVectorizeEnableMetadata
74/// If Polly should avoid setting
75/// llvm.loop.vectorize.enable=true for this loop.
76///
77/// @return Value* The newly created induction variable for this loop.
78Value *createLoop(Value *LowerBound, Value *UpperBound, Value *Stride,
79 PollyIRBuilder &Builder, LoopInfo &LI, DominatorTree &DT,
80 BasicBlock *&ExitBlock, ICmpInst::Predicate Predicate,
81 ScopAnnotator *Annotator = nullptr, bool Parallel = false,
82 bool UseGuard = true, bool LoopVectDisabled = false,
83 bool SkipVectorizeEnableMetadata = false);
84
85/// Create a DebugLoc representing generated instructions.
86///
87/// The IR verifier requires !dbg metadata to be set in some situations. For
88/// instance, if an (inlinable) function has debug info, all its call site must
89/// have debug info as well.
90llvm::DebugLoc createDebugLocForGeneratedCode(Function *F);
91
92/// The ParallelLoopGenerator allows to create parallelized loops
93///
94/// To parallelize a loop, we perform the following steps:
95/// o Generate a subfunction which will hold the loop body.
96/// o Create a struct to hold all outer values needed in the loop body.
97/// o Create calls to a runtime library to achieve the actual parallelism.
98/// These calls will spawn and join threads, define how the work (here the
99/// iterations) are distributed between them and make sure each has access
100/// to the struct holding all needed values.
101///
102/// At the moment we support only one parallel runtime, OpenMP.
103///
104/// If we parallelize the outer loop of the following loop nest,
105///
106/// S0;
107/// for (int i = 0; i < N; i++)
108/// for (int j = 0; j < M; j++)
109/// S1(i, j);
110/// S2;
111///
112/// we will generate the following code (with different runtime function names):
113///
114/// S0;
115/// auto *values = storeValuesIntoStruct();
116/// // Execute subfunction with multiple threads
117/// spawn_threads(subfunction, values);
118/// join_threads();
119/// S2;
120///
121/// // This function is executed in parallel by different threads
122/// void subfunction(values) {
123/// while (auto *WorkItem = getWorkItem()) {
124/// int LB = WorkItem.begin();
125/// int UB = WorkItem.end();
126/// for (int i = LB; i < UB; i++)
127/// for (int j = 0; j < M; j++)
128/// S1(i, j);
129/// }
130/// cleanup_thread();
131/// }
133public:
134 /// Create a parallel loop generator for the current function.
136 : Builder(Builder), LongType(Type::getIntNTy(Builder.getContext(),
137 DL.getPointerSizeInBits())),
138 M(Builder.GetInsertBlock()->getParent()->getParent()),
140 Builder.GetInsertBlock()->getParent())) {}
141
143
144 /// Create a parallel loop.
145 ///
146 /// This function is the main function to automatically generate a parallel
147 /// loop with all its components.
148 ///
149 /// @param LB The lower bound for the loop we parallelize.
150 /// @param UB The upper bound for the loop we parallelize.
151 /// @param Stride The stride of the loop we parallelize.
152 /// @param Values A set of LLVM-IR Values that should be available in
153 /// the new loop body.
154 /// @param VMap A map to allow outside access to the new versions of
155 /// the values in @p Values.
156 /// @param LoopBody A pointer to an iterator that is set to point to the
157 /// body of the created loop. It should be used to insert
158 /// instructions that form the actual loop body.
159 ///
160 /// @return The newly created induction variable for this loop.
161 Value *createParallelLoop(Value *LB, Value *UB, Value *Stride,
162 SetVector<Value *> &Values, ValueMapT &VMap,
163 BasicBlock::iterator *LoopBody);
164
165protected:
166 /// The IR builder we use to create instructions.
168
169 /// The loop info for the generated subfunction.
170 std::unique_ptr<LoopInfo> SubFnLI;
171
172 /// The dominance tree for the generated subfunction.
173 std::unique_ptr<DominatorTree> SubFnDT;
174
175 /// The type of a "long" on this hardware used for backend calls.
176 Type *LongType;
177
178 /// The current module
179 Module *M;
180
181 /// Debug location for generated code without direct link to any specific
182 /// line.
183 ///
184 /// We only set the DebugLoc where the IR Verifier requires us to. Otherwise,
185 /// absent debug location for optimized code should be fine.
186 llvm::DebugLoc DLGenerated;
187
188public:
189 /// Returns the DominatorTree for the generated subfunction.
190 DominatorTree *getCalleeDominatorTree() const { return SubFnDT.get(); }
191
192 /// Returns the LoopInfo for the generated subfunction.
193 LoopInfo *getCalleeLoopInfo() const { return SubFnLI.get(); }
194
195 /// Create a struct for all @p Values and store them in there.
196 ///
197 /// @param Values The values which should be stored in the struct.
198 ///
199 /// @return The created struct.
200 AllocaInst *storeValuesIntoStruct(SetVector<Value *> &Values);
201
202 /// Extract all values from the @p Struct and construct the mapping.
203 ///
204 /// @param Values The values which were stored in the struct.
205 /// @param Struct The struct holding all the values in @p Values.
206 /// @param VMap A map to associate every element of @p Values with the
207 /// new llvm value loaded from the @p Struct.
208 void extractValuesFromStruct(SetVector<Value *> Values, Type *Ty,
209 Value *Struct, ValueMapT &VMap);
210
211 /// Create the definition of the parallel subfunction.
212 ///
213 /// @return A pointer to the subfunction.
214 Function *createSubFnDefinition();
215
216 /// Create the runtime library calls for spawn and join of the worker threads.
217 /// Additionally, places a call to the specified subfunction.
218 ///
219 /// @param SubFn The subfunction which holds the loop body.
220 /// @param SubFnParam The parameter for the subfunction (basically the struct
221 /// filled with the outside values).
222 /// @param LB The lower bound for the loop we parallelize.
223 /// @param UB The upper bound for the loop we parallelize.
224 /// @param Stride The stride of the loop we parallelize.
225 virtual void deployParallelExecution(Function *SubFn, Value *SubFnParam,
226 Value *LB, Value *UB, Value *Stride) = 0;
227
228 /// Prepare the definition of the parallel subfunction.
229 /// Creates the argument list and names them (as well as the subfunction).
230 ///
231 /// @param F A pointer to the (parallel) subfunction's parent function.
232 ///
233 /// @return The pointer to the (parallel) subfunction.
234 virtual Function *prepareSubFnDefinition(Function *F) const = 0;
235
236 /// Create the parallel subfunction.
237 ///
238 /// @param Stride The induction variable increment.
239 /// @param Struct A struct holding all values in @p Values.
240 /// @param Values A set of LLVM-IR Values that should be available in
241 /// the new loop body.
242 /// @param VMap A map to allow outside access to the new versions of
243 /// the values in @p Values.
244 /// @param SubFn The newly created subfunction is returned here.
245 ///
246 /// @return The newly created induction variable.
247 virtual std::tuple<Value *, Function *>
248 createSubFn(Value *Stride, AllocaInst *Struct, SetVector<Value *> UsedValues,
249 ValueMapT &VMap) = 0;
250};
251} // end namespace polly
252#endif
LoopInfo * getCalleeLoopInfo() const
Returns the LoopInfo for the generated subfunction.
PollyIRBuilder & Builder
The IR builder we use to create instructions.
Function * createSubFnDefinition()
Create the definition of the parallel subfunction.
std::unique_ptr< DominatorTree > SubFnDT
The dominance tree for the generated subfunction.
virtual void deployParallelExecution(Function *SubFn, Value *SubFnParam, Value *LB, Value *UB, Value *Stride)=0
Create the runtime library calls for spawn and join of the worker threads.
Value * createParallelLoop(Value *LB, Value *UB, Value *Stride, SetVector< Value * > &Values, ValueMapT &VMap, BasicBlock::iterator *LoopBody)
Create a parallel loop.
virtual std::tuple< Value *, Function * > createSubFn(Value *Stride, AllocaInst *Struct, SetVector< Value * > UsedValues, ValueMapT &VMap)=0
Create the parallel subfunction.
Module * M
The current module.
DominatorTree * getCalleeDominatorTree() const
Returns the DominatorTree for the generated subfunction.
llvm::DebugLoc DLGenerated
Debug location for generated code without direct link to any specific line.
ParallelLoopGenerator(PollyIRBuilder &Builder, const DataLayout &DL)
Create a parallel loop generator for the current function.
Type * LongType
The type of a "long" on this hardware used for backend calls.
void extractValuesFromStruct(SetVector< Value * > Values, Type *Ty, Value *Struct, ValueMapT &VMap)
Extract all values from the Struct and construct the mapping.
virtual Function * prepareSubFnDefinition(Function *F) const =0
Prepare the definition of the parallel subfunction.
std::unique_ptr< LoopInfo > SubFnLI
The loop info for the generated subfunction.
AllocaInst * storeValuesIntoStruct(SetVector< Value * > &Values)
Create a struct for all Values and store them in there.
OMPGeneralSchedulingType PollyScheduling
llvm::DebugLoc createDebugLocForGeneratedCode(Function *F)
Create a DebugLoc representing generated instructions.
int PollyNumThreads
@ Value
MemoryKind::Value: Models an llvm::Value.
Definition ScopInfo.h:150
int PollyChunkSize
Value * createLoop(Value *LowerBound, Value *UpperBound, Value *Stride, PollyIRBuilder &Builder, LoopInfo &LI, DominatorTree &DT, BasicBlock *&ExitBlock, ICmpInst::Predicate Predicate, ScopAnnotator *Annotator=nullptr, bool Parallel=false, bool UseGuard=true, bool LoopVectDisabled=false, bool SkipVectorizeEnableMetadata=false)
Create a scalar do/for-style loop.
OMPGeneralSchedulingType
General scheduling types of parallel OpenMP for loops.
llvm::IRBuilder< llvm::ConstantFolder, IRInserter > PollyIRBuilder
Definition IRBuilder.h:143
llvm::DenseMap< llvm::AssertingVH< llvm::Value >, llvm::AssertingVH< llvm::Value > > ValueMapT
Type to remap values.
Definition ScopHelper.h:106