Polly 20.0.0git
IslNodeBuilder.h
Go to the documentation of this file.
1//=- IslNodeBuilder.cpp - Translate an isl AST into a LLVM-IR AST -*- 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 the IslNodeBuilder, a class to translate an isl AST into
10// a LLVM-IR AST.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef POLLY_ISLNODEBUILDER_H
15#define POLLY_ISLNODEBUILDER_H
16
20#include "llvm/ADT/ArrayRef.h"
21#include "llvm/ADT/SmallSet.h"
22#include "llvm/IR/InstrTypes.h"
23#include "isl/ctx.h"
25
26namespace polly {
27using llvm::LoopInfo;
28using llvm::SmallSet;
29
30struct InvariantEquivClassTy;
31
33 LoopInfo &LI;
34 ScalarEvolution &SE;
37 SetVector<Value *> &Values;
38 SetVector<const SCEV *> &SCEVs;
40 // In case an (optional) parameter space location is provided, parameter space
41 // information is collected as well.
43};
44
45/// Extract the out-of-scop values and SCEVs referenced from a ScopStmt.
46///
47/// This includes the SCEVUnknowns referenced by the SCEVs used in the
48/// statement and the base pointers of the memory accesses. For scalar
49/// statements we force the generation of alloca memory locations and list
50/// these locations in the set of out-of-scop values as well.
51///
52/// We also collect an isl::space that includes all parameter dimensions
53/// used in the statement's memory accesses, in case the ParamSpace pointer
54/// is non-null.
55///
56/// @param Stmt The statement for which to extract the information.
57/// @param UserPtr A void pointer that can be casted to a
58/// SubtreeReferences structure.
59/// @param CreateScalarRefs Should the result include allocas of scalar
60/// references?
61void addReferencesFromStmt(ScopStmt *Stmt, void *UserPtr,
62 bool CreateScalarRefs = true);
63
65public:
67 const DataLayout &DL, LoopInfo &LI, ScalarEvolution &SE,
68 DominatorTree &DT, Scop &S, BasicBlock *StartBlock)
74 RegionGen(BlockGen), DL(DL), LI(LI), SE(SE), DT(DT),
76
77 virtual ~IslNodeBuilder() = default;
78
79 void addParameters(__isl_take isl_set *Context);
80
81 /// Generate code that evaluates @p Condition at run-time.
82 ///
83 /// This function is typically called to generate the LLVM-IR for the
84 /// run-time condition of the scop, that verifies that all the optimistic
85 /// assumptions we have taken during scop modeling and transformation
86 /// hold at run-time.
87 ///
88 /// @param Condition The condition to evaluate
89 ///
90 /// @result An llvm::Value that is true if the condition holds and false
91 /// otherwise.
92 Value *createRTC(isl_ast_expr *Condition);
93
94 void create(__isl_take isl_ast_node *Node);
95
96 /// Allocate memory for all new arrays created by Polly.
97 void allocateNewArrays(BBPair StartExitBlocks);
98
99 /// Preload all memory loads that are invariant.
101
102 /// Finalize code generation.
103 ///
104 /// @see BlockGenerator::finalizeSCoP(Scop &S)
105 virtual void finalize() { BlockGen.finalizeSCoP(S); }
106
108
109 /// Get the associated block generator.
110 ///
111 /// @return A reference to the associated block generator.
113
114 /// Return the parallel subfunctions that have been created.
115 const ArrayRef<Function *> getParallelSubfunctions() const {
117 }
118
119protected:
123
125
126 /// Maps used by the block and region generator to demote scalars.
127 ///
128 ///@{
129
130 /// See BlockGenerator::ScalarMap.
132
133 /// See BlockGenerator::EscapeMap.
135
136 ///@}
137
138 /// The generator used to copy a basic block.
140
141 /// The generator used to copy a non-affine region.
143
144 const DataLayout &DL;
145 LoopInfo &LI;
146 ScalarEvolution &SE;
147 DominatorTree &DT;
148 BasicBlock *StartBlock;
149
150 /// Relates to the region where the code is emitted into.
151 /// @{
152 DominatorTree *GenDT;
153 LoopInfo *GenLI;
154 ScalarEvolution *GenSE;
155 /// @}
156
157 /// The current iteration of out-of-scop loops
158 ///
159 /// This map provides for a given loop a llvm::Value that contains the current
160 /// loop iteration.
161 MapVector<const Loop *, const SCEV *> OutsideLoopIterations;
162
163 // This maps an isl_id* to the Value* it has in the generated program. For now
164 // on, the only isl_ids that are stored here are the newly calculated loop
165 // ivs.
167
168 /// A collection of all parallel subfunctions that have been created.
169 SmallVector<Function *, 8> ParallelSubfunctions;
170
171 /// Generate code for a given SCEV*
172 ///
173 /// This function generates code for a given SCEV expression. It generated
174 /// code is emitted at the end of the basic block our Builder currently
175 /// points to and the resulting value is returned.
176 ///
177 /// @param Expr The expression to code generate.
178 Value *generateSCEV(const SCEV *Expr);
179
180 /// A set of Value -> Value remappings to apply when generating new code.
181 ///
182 /// When generating new code for a ScopStmt this map is used to map certain
183 /// llvm::Values to new llvm::Values.
185
186 /// Materialize code for @p Id if it was not done before.
187 ///
188 /// @returns False, iff a problem occurred and the value was not materialized.
190
191 /// Materialize parameters of @p Set.
192 ///
193 /// @returns False, iff a problem occurred and the value was not materialized.
195
196 /// Materialize all parameters in the current scop.
197 ///
198 /// @returns False, iff a problem occurred and the value was not materialized.
200
201 // Extract the upper bound of this loop
202 //
203 // The isl code generation can generate arbitrary expressions to check if the
204 // upper bound of a loop is reached, but it provides an option to enforce
205 // 'atomic' upper bounds. An 'atomic upper bound is always of the form
206 // iv <= expr, where expr is an (arbitrary) expression not containing iv.
207 //
208 // This function extracts 'atomic' upper bounds. Polly, in general, requires
209 // atomic upper bounds for the following reasons:
210 //
211 // 1. An atomic upper bound is loop invariant
212 //
213 // It must not be calculated at each loop iteration and can often even be
214 // hoisted out further by the loop invariant code motion.
215 //
216 // 2. OpenMP needs a loop invariant upper bound to calculate the number
217 // of loop iterations.
218 //
219 // 3. With the existing code, upper bounds have been easier to implement.
221 CmpInst::Predicate &Predicate);
222
223 /// Return non-negative number of iterations in case of the following form
224 /// of a loop and -1 otherwise.
225 ///
226 /// for (i = 0; i <= NumIter; i++) {
227 /// loop body;
228 /// }
229 ///
230 /// NumIter is a non-negative integer value. Condition can have
231 /// isl_ast_op_lt type.
233
234 /// Compute the values and loops referenced in this subtree.
235 ///
236 /// This function looks at all ScopStmts scheduled below the provided For node
237 /// and finds the llvm::Value[s] and llvm::Loops[s] which are referenced but
238 /// not locally defined.
239 ///
240 /// Values that can be synthesized or that are available as globals are
241 /// considered locally defined.
242 ///
243 /// Loops that contain the scop or that are part of the scop are considered
244 /// locally defined. Loops that are before the scop, but do not contain the
245 /// scop itself are considered not locally defined.
246 ///
247 /// @param For The node defining the subtree.
248 /// @param Values A vector that will be filled with the Values referenced in
249 /// this subtree.
250 /// @param Loops A vector that will be filled with the Loops referenced in
251 /// this subtree.
253 SetVector<Value *> &Values,
254 SetVector<const Loop *> &Loops);
255
256 /// Return the most up-to-date version of the llvm::Value for code generation.
257 /// @param Original The Value to check for an up to date version.
258 /// @returns A remapped `Value` from ValueMap, or `Original` if no mapping
259 /// exists.
260 /// @see IslNodeBuilder::updateValues
261 /// @see IslNodeBuilder::ValueMap
262 Value *getLatestValue(Value *Original) const;
263
264 /// Generate code for a marker now.
265 ///
266 /// For mark nodes with an unknown name, we just forward the code generation
267 /// to its child. This is currently the only behavior implemented, as there is
268 /// currently not special handling for marker nodes implemented.
269 ///
270 /// @param Mark The node we generate code for.
271 virtual void createMark(__isl_take isl_ast_node *Marker);
272
273 virtual void createFor(__isl_take isl_ast_node *For);
274
275 /// Set to remember materialized invariant loads.
276 ///
277 /// An invariant load is identified by its pointer (the SCEV) and its type.
278 SmallSet<std::pair<const SCEV *, Type *>, 16> PreloadedPtrs;
279
280 /// Preload the memory access at @p AccessRange with @p Build.
281 ///
282 /// @returns The preloaded value casted to type @p Ty
284 isl_ast_build *Build, Instruction *AccInst);
285
286 /// Preload the memory load access @p MA.
287 ///
288 /// If @p MA is not always executed it will be conditionally loaded and
289 /// merged with undef from the same type. Hence, if @p MA is executed only
290 /// under condition C then the preload code will look like this:
291 ///
292 /// MA_preload = undef;
293 /// if (C)
294 /// MA_preload = load MA;
295 /// use MA_preload
298
299 /// Preload the invariant access equivalence class @p IAClass
300 ///
301 /// This function will preload the representing load from @p IAClass and
302 /// map all members of @p IAClass to that preloaded value, potentially casted
303 /// to the required type.
304 ///
305 /// @returns False, iff a problem occurred and the load was not preloaded.
307
308 void createForSequential(isl::ast_node_for For, bool MarkParallel);
309
310 /// Create LLVM-IR that executes a for node thread parallel.
311 ///
312 /// @param For The FOR isl_ast_node for which code is generated.
314
315 /// Create new access functions for modified memory accesses.
316 ///
317 /// In case the access function of one of the memory references in the Stmt
318 /// has been modified, we generate a new isl_ast_expr that reflects the
319 /// newly modified access function and return a map that maps from the
320 /// individual memory references in the statement (identified by their id)
321 /// to these newly generated ast expressions.
322 ///
323 /// @param Stmt The statement for which to (possibly) generate new access
324 /// functions.
325 /// @param Node The ast node corresponding to the statement for us to extract
326 /// the local schedule from.
327 /// @return A new hash table that contains remappings from memory ids to new
328 /// access expressions.
329 __isl_give isl_id_to_ast_expr *
331
332 /// Generate LLVM-IR that computes the values of the original induction
333 /// variables in function of the newly generated loop induction variables.
334 ///
335 /// Example:
336 ///
337 /// // Original
338 /// for i
339 /// for j
340 /// S(i)
341 ///
342 /// Schedule: [i,j] -> [i+j, j]
343 ///
344 /// // New
345 /// for c0
346 /// for c1
347 /// S(c0 - c1, c1)
348 ///
349 /// Assuming the original code consists of two loops which are
350 /// transformed according to a schedule [i,j] -> [c0=i+j,c1=j]. The resulting
351 /// ast models the original statement as a call expression where each argument
352 /// is an expression that computes the old induction variables from the new
353 /// ones, ordered such that the first argument computes the value of induction
354 /// variable that was outermost in the original code.
355 ///
356 /// @param Expr The call expression that represents the statement.
357 /// @param Stmt The statement that is called.
358 /// @param LTS The loop to SCEV map in which the mapping from the original
359 /// loop to a SCEV representing the new loop iv is added. This
360 /// mapping does not require an explicit induction variable.
361 /// Instead, we think in terms of an implicit induction variable
362 /// that counts the number of times a loop is executed. For each
363 /// original loop this count, expressed in function of the new
364 /// induction variables, is added to the LTS map.
366 LoopToScevMapT &LTS);
368 std::vector<LoopToScevMapT> &VLTS,
369 std::vector<Value *> &IVS,
370 __isl_take isl_id *IteratorID);
371 virtual void createIf(__isl_take isl_ast_node *If);
372 virtual void createUser(__isl_take isl_ast_node *User);
373 virtual void createBlock(__isl_take isl_ast_node *Block);
374
375 /// Get the schedule for a given AST node.
376 ///
377 /// This information is used to reason about parallelism of loops or the
378 /// locality of memory accesses under a given schedule.
379 ///
380 /// @param Node The node we want to obtain the schedule for.
381 /// @return Return an isl_union_map that maps from the statements executed
382 /// below this ast node to the scheduling vectors used to enumerate
383 /// them.
384 ///
386
387private:
388 /// Create code for a copy statement.
389 ///
390 /// A copy statement is expected to have one read memory access and one write
391 /// memory access (in this very order). Data is loaded from the location
392 /// described by the read memory access and written to the location described
393 /// by the write memory access. @p NewAccesses contains for each access
394 /// the isl ast expression that describes the location accessed.
395 ///
396 /// @param Stmt The copy statement that contains the accesses.
397 /// @param NewAccesses The hash table that contains remappings from memory
398 /// ids to new access expressions.
399 void generateCopyStmt(ScopStmt *Stmt,
400 __isl_keep isl_id_to_ast_expr *NewAccesses);
401
402 /// Materialize a canonical loop induction variable for `L`, which is a loop
403 /// that is *not* present in the Scop.
404 ///
405 /// Note that this is materialized at the point where the `Builder` is
406 /// currently pointing.
407 /// We also populate the `OutsideLoopIterations` map with `L`s SCEV to keep
408 /// track of the induction variable.
409 /// See [Code generation of induction variables of loops outside Scops]
411};
412
413} // namespace polly
414
415#endif // POLLY_ISLNODEBUILDER_H
Generate a new basic block for a polyhedral statement.
DenseMap< const ScopArrayInfo *, AssertingVH< AllocaInst > > AllocaMapTy
Map types to resolve scalar dependences.
void finalizeSCoP(Scop &S)
Finalize the code generation for the SCoP S.
MapVector< Instruction *, std::pair< AssertingVH< Value >, EscapeUserVectorTy > > EscapeUsersAllocaMapTy
Map type to resolve escaping users for scalar instructions.
LLVM-IR generator for isl_ast_expr[essions].
llvm::MapVector< isl_id *, llvm::AssertingVH< llvm::Value > > IDToValueTy
A map from isl_ids to llvm::Values.
Value * preloadUnconditionally(__isl_take isl_set *AccessRange, isl_ast_build *Build, Instruction *AccInst)
Preload the memory access at AccessRange with Build.
void addParameters(__isl_take isl_set *Context)
Value * preloadInvariantLoad(const MemoryAccess &MA, __isl_take isl_set *Domain)
Preload the memory load access MA.
Value * getLatestValue(Value *Original) const
Return the most up-to-date version of the llvm::Value for code generation.
void create(__isl_take isl_ast_node *Node)
RegionGenerator RegionGen
The generator used to copy a non-affine region.
ScopAnnotator & Annotator
BlockGenerator::AllocaMapTy ScalarMap
Maps used by the block and region generator to demote scalars.
SmallVector< Function *, 8 > ParallelSubfunctions
A collection of all parallel subfunctions that have been created.
DominatorTree & DT
IslExprBuilder::IDToValueTy IDToValue
bool preloadInvariantEquivClass(InvariantEquivClassTy &IAClass)
Preload the invariant access equivalence class IAClass.
IslExprBuilder ExprBuilder
void createForSequential(isl::ast_node_for For, bool MarkParallel)
__isl_give isl_id_to_ast_expr * createNewAccesses(ScopStmt *Stmt, __isl_keep isl_ast_node *Node)
Create new access functions for modified memory accesses.
virtual void finalize()
Finalize code generation.
virtual ~IslNodeBuilder()=default
void createForParallel(__isl_take isl_ast_node *For)
Create LLVM-IR that executes a for node thread parallel.
IslNodeBuilder(PollyIRBuilder &Builder, ScopAnnotator &Annotator, const DataLayout &DL, LoopInfo &LI, ScalarEvolution &SE, DominatorTree &DT, Scop &S, BasicBlock *StartBlock)
bool preloadInvariantLoads()
Preload all memory loads that are invariant.
Value * generateSCEV(const SCEV *Expr)
Generate code for a given SCEV*.
bool materializeParameters()
Materialize all parameters in the current scop.
const DataLayout & DL
const ArrayRef< Function * > getParallelSubfunctions() const
Return the parallel subfunctions that have been created.
ValueMapT ValueMap
A set of Value -> Value remappings to apply when generating new code.
bool materializeValue(__isl_take isl_id *Id)
Materialize code for Id if it was not done before.
SmallSet< std::pair< const SCEV *, Type * >, 16 > PreloadedPtrs
Set to remember materialized invariant loads.
Value * materializeNonScopLoopInductionVariable(const Loop *L)
Materialize a canonical loop induction variable for L, which is a loop that is not present in the Sco...
virtual void createBlock(__isl_take isl_ast_node *Block)
virtual void createUser(__isl_take isl_ast_node *User)
ScalarEvolution & SE
void createSubstitutionsVector(__isl_take isl_ast_expr *Expr, ScopStmt *Stmt, std::vector< LoopToScevMapT > &VLTS, std::vector< Value * > &IVS, __isl_take isl_id *IteratorID)
DominatorTree * GenDT
Relates to the region where the code is emitted into.
virtual void createFor(__isl_take isl_ast_node *For)
virtual void createMark(__isl_take isl_ast_node *Marker)
Generate code for a marker now.
void allocateNewArrays(BBPair StartExitBlocks)
Allocate memory for all new arrays created by Polly.
virtual isl::union_map getScheduleForAstNode(const isl::ast_node &Node)
Get the schedule for a given AST node.
PollyIRBuilder & Builder
void getReferencesInSubtree(const isl::ast_node &For, SetVector< Value * > &Values, SetVector< const Loop * > &Loops)
Compute the values and loops referenced in this subtree.
ScalarEvolution * GenSE
void generateCopyStmt(ScopStmt *Stmt, __isl_keep isl_id_to_ast_expr *NewAccesses)
Create code for a copy statement.
virtual void createIf(__isl_take isl_ast_node *If)
void createSubstitutions(__isl_take isl_ast_expr *Expr, ScopStmt *Stmt, LoopToScevMapT &LTS)
Generate LLVM-IR that computes the values of the original induction variables in function of the newl...
isl::ast_expr getUpperBound(isl::ast_node_for For, CmpInst::Predicate &Predicate)
BlockGenerator::EscapeUsersAllocaMapTy EscapeMap
See BlockGenerator::EscapeMap.
BlockGenerator BlockGen
The generator used to copy a basic block.
BlockGenerator & getBlockGenerator()
Get the associated block generator.
int getNumberOfIterations(isl::ast_node_for For)
Return non-negative number of iterations in case of the following form of a loop and -1 otherwise.
Value * createRTC(isl_ast_expr *Condition)
Generate code that evaluates Condition at run-time.
IslExprBuilder & getExprBuilder()
MapVector< const Loop *, const SCEV * > OutsideLoopIterations
The current iteration of out-of-scop loops.
Represent memory accesses in statements.
Definition: ScopInfo.h:431
Generator for new versions of polyhedral region statements.
Helper class to annotate newly generated SCoPs with metadata.
Definition: IRBuilder.h:44
Statement of the Scop.
Definition: ScopInfo.h:1140
Static Control Part.
Definition: ScopInfo.h:1630
#define __isl_take
Definition: ctx.h:22
#define __isl_give
Definition: ctx.h:19
#define __isl_keep
Definition: ctx.h:25
struct isl_set isl_set
Definition: map_type.h:26
std::pair< llvm::BasicBlock *, llvm::BasicBlock * > BBPair
Type to hold region delimiters (entry & exit block).
Definition: Utils.h:31
@ Value
MemoryKind::Value: Models an llvm::Value.
void addReferencesFromStmt(ScopStmt *Stmt, void *UserPtr, bool CreateScalarRefs=true)
Extract the out-of-scop values and SCEVs referenced from a ScopStmt.
llvm::IRBuilder< llvm::ConstantFolder, IRInserter > PollyIRBuilder
Definition: IRBuilder.h:140
llvm::DenseMap< llvm::AssertingVH< llvm::Value >, llvm::AssertingVH< llvm::Value > > ValueMapT
Type to remap values.
Definition: ScopHelper.h:106
llvm::DenseMap< const llvm::Loop *, const llvm::SCEV * > LoopToScevMapT
Same as llvm/Analysis/ScalarEvolutionExpressions.h.
Definition: ScopHelper.h:40
Type for equivalent invariant accesses and their domain context.
Definition: ScopInfo.h:1106
ScalarEvolution & SE
SetVector< Value * > & Values
SetVector< const SCEV * > & SCEVs
BlockGenerator & BlockGen
static TupleKindPtr Domain("Domain")