File: | polly/lib/CodeGen/IslNodeBuilder.cpp |
Warning: | line 1165, column 10 Value stored to 'size' during its initialization is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- IslNodeBuilder.cpp - Translate an isl AST into a LLVM-IR AST -------===// |
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 | #include "polly/CodeGen/IslNodeBuilder.h" |
15 | #include "polly/CodeGen/BlockGenerators.h" |
16 | #include "polly/CodeGen/CodeGeneration.h" |
17 | #include "polly/CodeGen/IslAst.h" |
18 | #include "polly/CodeGen/IslExprBuilder.h" |
19 | #include "polly/CodeGen/LoopGeneratorsGOMP.h" |
20 | #include "polly/CodeGen/LoopGeneratorsKMP.h" |
21 | #include "polly/CodeGen/RuntimeDebugBuilder.h" |
22 | #include "polly/Options.h" |
23 | #include "polly/ScopInfo.h" |
24 | #include "polly/Support/ISLTools.h" |
25 | #include "polly/Support/SCEVValidator.h" |
26 | #include "polly/Support/ScopHelper.h" |
27 | #include "llvm/ADT/APInt.h" |
28 | #include "llvm/ADT/PostOrderIterator.h" |
29 | #include "llvm/ADT/SetVector.h" |
30 | #include "llvm/ADT/SmallPtrSet.h" |
31 | #include "llvm/ADT/Statistic.h" |
32 | #include "llvm/Analysis/LoopInfo.h" |
33 | #include "llvm/Analysis/RegionInfo.h" |
34 | #include "llvm/Analysis/ScalarEvolution.h" |
35 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
36 | #include "llvm/IR/BasicBlock.h" |
37 | #include "llvm/IR/Constant.h" |
38 | #include "llvm/IR/Constants.h" |
39 | #include "llvm/IR/DataLayout.h" |
40 | #include "llvm/IR/DerivedTypes.h" |
41 | #include "llvm/IR/Dominators.h" |
42 | #include "llvm/IR/Function.h" |
43 | #include "llvm/IR/InstrTypes.h" |
44 | #include "llvm/IR/Instruction.h" |
45 | #include "llvm/IR/Instructions.h" |
46 | #include "llvm/IR/Type.h" |
47 | #include "llvm/IR/Value.h" |
48 | #include "llvm/Support/Casting.h" |
49 | #include "llvm/Support/CommandLine.h" |
50 | #include "llvm/Support/ErrorHandling.h" |
51 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
52 | #include "isl/aff.h" |
53 | #include "isl/aff_type.h" |
54 | #include "isl/ast.h" |
55 | #include "isl/ast_build.h" |
56 | #include "isl/isl-noexceptions.h" |
57 | #include "isl/map.h" |
58 | #include "isl/set.h" |
59 | #include "isl/union_map.h" |
60 | #include "isl/union_set.h" |
61 | #include "isl/val.h" |
62 | #include <algorithm> |
63 | #include <cassert> |
64 | #include <cstdint> |
65 | #include <cstring> |
66 | #include <string> |
67 | #include <utility> |
68 | #include <vector> |
69 | |
70 | using namespace llvm; |
71 | using namespace polly; |
72 | |
73 | #define DEBUG_TYPE"polly-codegen" "polly-codegen" |
74 | |
75 | STATISTIC(VersionedScops, "Number of SCoPs that required versioning.")static llvm::Statistic VersionedScops = {"polly-codegen", "VersionedScops" , "Number of SCoPs that required versioning."}; |
76 | |
77 | STATISTIC(SequentialLoops, "Number of generated sequential for-loops")static llvm::Statistic SequentialLoops = {"polly-codegen", "SequentialLoops" , "Number of generated sequential for-loops"}; |
78 | STATISTIC(ParallelLoops, "Number of generated parallel for-loops")static llvm::Statistic ParallelLoops = {"polly-codegen", "ParallelLoops" , "Number of generated parallel for-loops"}; |
79 | STATISTIC(VectorLoops, "Number of generated vector for-loops")static llvm::Statistic VectorLoops = {"polly-codegen", "VectorLoops" , "Number of generated vector for-loops"}; |
80 | STATISTIC(IfConditions, "Number of generated if-conditions")static llvm::Statistic IfConditions = {"polly-codegen", "IfConditions" , "Number of generated if-conditions"}; |
81 | |
82 | /// OpenMP backend options |
83 | enum class OpenMPBackend { GNU, LLVM }; |
84 | |
85 | static cl::opt<bool> PollyGenerateRTCPrint( |
86 | "polly-codegen-emit-rtc-print", |
87 | cl::desc("Emit code that prints the runtime check result dynamically."), |
88 | cl::Hidden, cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); |
89 | |
90 | // If this option is set we always use the isl AST generator to regenerate |
91 | // memory accesses. Without this option set we regenerate expressions using the |
92 | // original SCEV expressions and only generate new expressions in case the |
93 | // access relation has been changed and consequently must be regenerated. |
94 | static cl::opt<bool> PollyGenerateExpressions( |
95 | "polly-codegen-generate-expressions", |
96 | cl::desc("Generate AST expressions for unmodified and modified accesses"), |
97 | cl::Hidden, cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); |
98 | |
99 | static cl::opt<int> PollyTargetFirstLevelCacheLineSize( |
100 | "polly-target-first-level-cache-line-size", |
101 | cl::desc("The size of the first level cache line size specified in bytes."), |
102 | cl::Hidden, cl::init(64), cl::ZeroOrMore, cl::cat(PollyCategory)); |
103 | |
104 | static cl::opt<OpenMPBackend> PollyOmpBackend( |
105 | "polly-omp-backend", cl::desc("Choose the OpenMP library to use:"), |
106 | cl::values(clEnumValN(OpenMPBackend::GNU, "GNU", "GNU OpenMP")llvm::cl::OptionEnumValue { "GNU", int(OpenMPBackend::GNU), "GNU OpenMP" }, |
107 | clEnumValN(OpenMPBackend::LLVM, "LLVM", "LLVM OpenMP")llvm::cl::OptionEnumValue { "LLVM", int(OpenMPBackend::LLVM), "LLVM OpenMP" }), |
108 | cl::Hidden, cl::init(OpenMPBackend::GNU), cl::cat(PollyCategory)); |
109 | |
110 | isl::ast_expr IslNodeBuilder::getUpperBound(isl::ast_node For, |
111 | ICmpInst::Predicate &Predicate) { |
112 | isl::ast_expr Cond = For.for_get_cond(); |
113 | isl::ast_expr Iterator = For.for_get_iterator(); |
114 | assert(isl_ast_expr_get_type(Cond.get()) == isl_ast_expr_op &&(static_cast <bool> (isl_ast_expr_get_type(Cond.get()) == isl_ast_expr_op && "conditional expression is not an atomic upper bound" ) ? void (0) : __assert_fail ("isl_ast_expr_get_type(Cond.get()) == isl_ast_expr_op && \"conditional expression is not an atomic upper bound\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 115, __extension__ __PRETTY_FUNCTION__)) |
115 | "conditional expression is not an atomic upper bound")(static_cast <bool> (isl_ast_expr_get_type(Cond.get()) == isl_ast_expr_op && "conditional expression is not an atomic upper bound" ) ? void (0) : __assert_fail ("isl_ast_expr_get_type(Cond.get()) == isl_ast_expr_op && \"conditional expression is not an atomic upper bound\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 115, __extension__ __PRETTY_FUNCTION__)); |
116 | |
117 | isl_ast_op_typeisl_ast_expr_op_type OpType = isl_ast_expr_get_op_type(Cond.get()); |
118 | |
119 | switch (OpType) { |
120 | case isl_ast_op_leisl_ast_expr_op_le: |
121 | Predicate = ICmpInst::ICMP_SLE; |
122 | break; |
123 | case isl_ast_op_ltisl_ast_expr_op_lt: |
124 | Predicate = ICmpInst::ICMP_SLT; |
125 | break; |
126 | default: |
127 | llvm_unreachable("Unexpected comparison type in loop condition")::llvm::llvm_unreachable_internal("Unexpected comparison type in loop condition" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 127); |
128 | } |
129 | |
130 | isl::ast_expr Arg0 = Cond.get_op_arg(0); |
131 | |
132 | assert(isl_ast_expr_get_type(Arg0.get()) == isl_ast_expr_id &&(static_cast <bool> (isl_ast_expr_get_type(Arg0.get()) == isl_ast_expr_id && "conditional expression is not an atomic upper bound" ) ? void (0) : __assert_fail ("isl_ast_expr_get_type(Arg0.get()) == isl_ast_expr_id && \"conditional expression is not an atomic upper bound\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 133, __extension__ __PRETTY_FUNCTION__)) |
133 | "conditional expression is not an atomic upper bound")(static_cast <bool> (isl_ast_expr_get_type(Arg0.get()) == isl_ast_expr_id && "conditional expression is not an atomic upper bound" ) ? void (0) : __assert_fail ("isl_ast_expr_get_type(Arg0.get()) == isl_ast_expr_id && \"conditional expression is not an atomic upper bound\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 133, __extension__ __PRETTY_FUNCTION__)); |
134 | |
135 | isl::id UBID = Arg0.get_id(); |
136 | |
137 | assert(isl_ast_expr_get_type(Iterator.get()) == isl_ast_expr_id &&(static_cast <bool> (isl_ast_expr_get_type(Iterator.get ()) == isl_ast_expr_id && "Could not get the iterator" ) ? void (0) : __assert_fail ("isl_ast_expr_get_type(Iterator.get()) == isl_ast_expr_id && \"Could not get the iterator\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 138, __extension__ __PRETTY_FUNCTION__)) |
138 | "Could not get the iterator")(static_cast <bool> (isl_ast_expr_get_type(Iterator.get ()) == isl_ast_expr_id && "Could not get the iterator" ) ? void (0) : __assert_fail ("isl_ast_expr_get_type(Iterator.get()) == isl_ast_expr_id && \"Could not get the iterator\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 138, __extension__ __PRETTY_FUNCTION__)); |
139 | |
140 | isl::id IteratorID = Iterator.get_id(); |
141 | |
142 | assert(UBID.get() == IteratorID.get() &&(static_cast <bool> (UBID.get() == IteratorID.get() && "conditional expression is not an atomic upper bound") ? void (0) : __assert_fail ("UBID.get() == IteratorID.get() && \"conditional expression is not an atomic upper bound\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 143, __extension__ __PRETTY_FUNCTION__)) |
143 | "conditional expression is not an atomic upper bound")(static_cast <bool> (UBID.get() == IteratorID.get() && "conditional expression is not an atomic upper bound") ? void (0) : __assert_fail ("UBID.get() == IteratorID.get() && \"conditional expression is not an atomic upper bound\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 143, __extension__ __PRETTY_FUNCTION__)); |
144 | |
145 | return Cond.get_op_arg(1); |
146 | } |
147 | |
148 | /// Return true if a return value of Predicate is true for the value represented |
149 | /// by passed isl_ast_expr_int. |
150 | static bool checkIslAstExprInt(__isl_take isl_ast_expr *Expr, |
151 | isl_bool (*Predicate)(__isl_keep isl_val *)) { |
152 | if (isl_ast_expr_get_type(Expr) != isl_ast_expr_int) { |
153 | isl_ast_expr_free(Expr); |
154 | return false; |
155 | } |
156 | auto ExprVal = isl_ast_expr_get_val(Expr); |
157 | isl_ast_expr_free(Expr); |
158 | if (Predicate(ExprVal) != isl_bool_true) { |
159 | isl_val_free(ExprVal); |
160 | return false; |
161 | } |
162 | isl_val_free(ExprVal); |
163 | return true; |
164 | } |
165 | |
166 | int IslNodeBuilder::getNumberOfIterations(isl::ast_node For) { |
167 | assert(isl_ast_node_get_type(For.get()) == isl_ast_node_for)(static_cast <bool> (isl_ast_node_get_type(For.get()) == isl_ast_node_for) ? void (0) : __assert_fail ("isl_ast_node_get_type(For.get()) == isl_ast_node_for" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 167, __extension__ __PRETTY_FUNCTION__)); |
168 | isl::ast_node Body = For.for_get_body(); |
169 | |
170 | // First, check if we can actually handle this code. |
171 | switch (isl_ast_node_get_type(Body.get())) { |
172 | case isl_ast_node_user: |
173 | break; |
174 | case isl_ast_node_block: { |
175 | isl::ast_node_list List = Body.block_get_children(); |
176 | for (isl::ast_node Node : List) { |
177 | isl_ast_node_type NodeType = isl_ast_node_get_type(Node.get()); |
178 | if (NodeType != isl_ast_node_user) |
179 | return -1; |
180 | } |
181 | break; |
182 | } |
183 | default: |
184 | return -1; |
185 | } |
186 | |
187 | isl::ast_expr Init = For.for_get_init(); |
188 | if (!checkIslAstExprInt(Init.release(), isl_val_is_zero)) |
189 | return -1; |
190 | isl::ast_expr Inc = For.for_get_inc(); |
191 | if (!checkIslAstExprInt(Inc.release(), isl_val_is_one)) |
192 | return -1; |
193 | CmpInst::Predicate Predicate; |
194 | isl::ast_expr UB = getUpperBound(For, Predicate); |
195 | if (isl_ast_expr_get_type(UB.get()) != isl_ast_expr_int) |
196 | return -1; |
197 | isl::val UpVal = UB.get_val(); |
198 | int NumberIterations = UpVal.get_num_si(); |
199 | if (NumberIterations < 0) |
200 | return -1; |
201 | if (Predicate == CmpInst::ICMP_SLT) |
202 | return NumberIterations; |
203 | else |
204 | return NumberIterations + 1; |
205 | } |
206 | |
207 | /// Extract the values and SCEVs needed to generate code for a block. |
208 | static int findReferencesInBlock(struct SubtreeReferences &References, |
209 | const ScopStmt *Stmt, BasicBlock *BB) { |
210 | for (Instruction &Inst : *BB) { |
211 | // Include invariant loads |
212 | if (isa<LoadInst>(Inst)) |
213 | if (Value *InvariantLoad = References.GlobalMap.lookup(&Inst)) |
214 | References.Values.insert(InvariantLoad); |
215 | |
216 | for (Value *SrcVal : Inst.operands()) { |
217 | auto *Scope = References.LI.getLoopFor(BB); |
218 | if (canSynthesize(SrcVal, References.S, &References.SE, Scope)) { |
219 | References.SCEVs.insert(References.SE.getSCEVAtScope(SrcVal, Scope)); |
220 | continue; |
221 | } else if (Value *NewVal = References.GlobalMap.lookup(SrcVal)) |
222 | References.Values.insert(NewVal); |
223 | } |
224 | } |
225 | return 0; |
226 | } |
227 | |
228 | void polly::addReferencesFromStmt(const ScopStmt *Stmt, void *UserPtr, |
229 | bool CreateScalarRefs) { |
230 | auto &References = *static_cast<struct SubtreeReferences *>(UserPtr); |
231 | |
232 | if (Stmt->isBlockStmt()) |
233 | findReferencesInBlock(References, Stmt, Stmt->getBasicBlock()); |
234 | else if (Stmt->isRegionStmt()) { |
235 | for (BasicBlock *BB : Stmt->getRegion()->blocks()) |
236 | findReferencesInBlock(References, Stmt, BB); |
237 | } else { |
238 | assert(Stmt->isCopyStmt())(static_cast <bool> (Stmt->isCopyStmt()) ? void (0) : __assert_fail ("Stmt->isCopyStmt()", "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 238, __extension__ __PRETTY_FUNCTION__)); |
239 | // Copy Stmts have no instructions that we need to consider. |
240 | } |
241 | |
242 | for (auto &Access : *Stmt) { |
243 | if (References.ParamSpace) { |
244 | isl::space ParamSpace = Access->getLatestAccessRelation().get_space(); |
245 | (*References.ParamSpace) = |
246 | References.ParamSpace->align_params(ParamSpace); |
247 | } |
248 | |
249 | if (Access->isLatestArrayKind()) { |
250 | auto *BasePtr = Access->getLatestScopArrayInfo()->getBasePtr(); |
251 | if (Instruction *OpInst = dyn_cast<Instruction>(BasePtr)) |
252 | if (Stmt->getParent()->contains(OpInst)) |
253 | continue; |
254 | |
255 | References.Values.insert(BasePtr); |
256 | continue; |
257 | } |
258 | |
259 | if (CreateScalarRefs) |
260 | References.Values.insert(References.BlockGen.getOrCreateAlloca(*Access)); |
261 | } |
262 | } |
263 | |
264 | /// Extract the out-of-scop values and SCEVs referenced from a set describing |
265 | /// a ScopStmt. |
266 | /// |
267 | /// This includes the SCEVUnknowns referenced by the SCEVs used in the |
268 | /// statement and the base pointers of the memory accesses. For scalar |
269 | /// statements we force the generation of alloca memory locations and list |
270 | /// these locations in the set of out-of-scop values as well. |
271 | /// |
272 | /// @param Set A set which references the ScopStmt we are interested in. |
273 | /// @param UserPtr A void pointer that can be casted to a SubtreeReferences |
274 | /// structure. |
275 | static void addReferencesFromStmtSet(isl::set Set, |
276 | struct SubtreeReferences *UserPtr) { |
277 | isl::id Id = Set.get_tuple_id(); |
278 | auto *Stmt = static_cast<const ScopStmt *>(Id.get_user()); |
279 | return addReferencesFromStmt(Stmt, UserPtr); |
280 | } |
281 | |
282 | /// Extract the out-of-scop values and SCEVs referenced from a union set |
283 | /// referencing multiple ScopStmts. |
284 | /// |
285 | /// This includes the SCEVUnknowns referenced by the SCEVs used in the |
286 | /// statement and the base pointers of the memory accesses. For scalar |
287 | /// statements we force the generation of alloca memory locations and list |
288 | /// these locations in the set of out-of-scop values as well. |
289 | /// |
290 | /// @param USet A union set referencing the ScopStmts we are interested |
291 | /// in. |
292 | /// @param References The SubtreeReferences data structure through which |
293 | /// results are returned and further information is |
294 | /// provided. |
295 | static void |
296 | addReferencesFromStmtUnionSet(isl::union_set USet, |
297 | struct SubtreeReferences &References) { |
298 | |
299 | for (isl::set Set : USet.get_set_list()) |
300 | addReferencesFromStmtSet(Set, &References); |
301 | } |
302 | |
303 | isl::union_map |
304 | IslNodeBuilder::getScheduleForAstNode(const isl::ast_node &Node) { |
305 | return IslAstInfo::getSchedule(Node); |
306 | } |
307 | |
308 | void IslNodeBuilder::getReferencesInSubtree(const isl::ast_node &For, |
309 | SetVector<Value *> &Values, |
310 | SetVector<const Loop *> &Loops) { |
311 | SetVector<const SCEV *> SCEVs; |
312 | struct SubtreeReferences References = { |
313 | LI, SE, S, ValueMap, Values, SCEVs, getBlockGenerator(), nullptr}; |
314 | |
315 | for (const auto &I : IDToValue) |
316 | Values.insert(I.second); |
317 | |
318 | // NOTE: this is populated in IslNodeBuilder::addParameters |
319 | for (const auto &I : OutsideLoopIterations) |
320 | Values.insert(cast<SCEVUnknown>(I.second)->getValue()); |
321 | |
322 | isl::union_set Schedule = getScheduleForAstNode(For).domain(); |
323 | addReferencesFromStmtUnionSet(Schedule, References); |
324 | |
325 | for (const SCEV *Expr : SCEVs) { |
326 | findValues(Expr, SE, Values); |
327 | findLoops(Expr, Loops); |
328 | } |
329 | |
330 | Values.remove_if([](const Value *V) { return isa<GlobalValue>(V); }); |
331 | |
332 | /// Note: Code generation of induction variables of loops outside Scops |
333 | /// |
334 | /// Remove loops that contain the scop or that are part of the scop, as they |
335 | /// are considered local. This leaves only loops that are before the scop, but |
336 | /// do not contain the scop itself. |
337 | /// We ignore loops perfectly contained in the Scop because these are already |
338 | /// generated at `IslNodeBuilder::addParameters`. These `Loops` are loops |
339 | /// whose induction variables are referred to by the Scop, but the Scop is not |
340 | /// fully contained in these Loops. Since there can be many of these, |
341 | /// we choose to codegen these on-demand. |
342 | /// @see IslNodeBuilder::materializeNonScopLoopInductionVariable. |
343 | Loops.remove_if([this](const Loop *L) { |
344 | return S.contains(L) || L->contains(S.getEntry()); |
345 | }); |
346 | |
347 | // Contains Values that may need to be replaced with other values |
348 | // due to replacements from the ValueMap. We should make sure |
349 | // that we return correctly remapped values. |
350 | // NOTE: this code path is tested by: |
351 | // 1. test/Isl/CodeGen/OpenMP/single_loop_with_loop_invariant_baseptr.ll |
352 | // 2. test/Isl/CodeGen/OpenMP/loop-body-references-outer-values-3.ll |
353 | SetVector<Value *> ReplacedValues; |
354 | for (Value *V : Values) { |
355 | ReplacedValues.insert(getLatestValue(V)); |
356 | } |
357 | Values = ReplacedValues; |
358 | } |
359 | |
360 | void IslNodeBuilder::updateValues(ValueMapT &NewValues) { |
361 | SmallPtrSet<Value *, 5> Inserted; |
362 | |
363 | for (const auto &I : IDToValue) { |
364 | IDToValue[I.first] = NewValues[I.second]; |
365 | Inserted.insert(I.second); |
366 | } |
367 | |
368 | for (const auto &I : NewValues) { |
369 | if (Inserted.count(I.first)) |
370 | continue; |
371 | |
372 | ValueMap[I.first] = I.second; |
373 | } |
374 | } |
375 | |
376 | Value *IslNodeBuilder::getLatestValue(Value *Original) const { |
377 | auto It = ValueMap.find(Original); |
378 | if (It == ValueMap.end()) |
379 | return Original; |
380 | return It->second; |
381 | } |
382 | |
383 | void IslNodeBuilder::createUserVector(__isl_take isl_ast_node *User, |
384 | std::vector<Value *> &IVS, |
385 | __isl_take isl_id *IteratorID, |
386 | __isl_take isl_union_map *Schedule) { |
387 | isl_ast_expr *Expr = isl_ast_node_user_get_expr(User); |
388 | isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0); |
389 | isl_id *Id = isl_ast_expr_get_id(StmtExpr); |
390 | isl_ast_expr_free(StmtExpr); |
391 | ScopStmt *Stmt = (ScopStmt *)isl_id_get_user(Id); |
392 | std::vector<LoopToScevMapT> VLTS(IVS.size()); |
393 | |
394 | isl_union_set *Domain = isl_union_set_from_set(Stmt->getDomain().release()); |
395 | Schedule = isl_union_map_intersect_domain(Schedule, Domain); |
396 | isl_map *S = isl_map_from_union_map(Schedule); |
397 | |
398 | auto *NewAccesses = createNewAccesses(Stmt, User); |
399 | createSubstitutionsVector(Expr, Stmt, VLTS, IVS, IteratorID); |
400 | VectorBlockGenerator::generate(BlockGen, *Stmt, VLTS, S, NewAccesses); |
401 | isl_id_to_ast_expr_free(NewAccesses); |
402 | isl_map_free(S); |
403 | isl_id_free(Id); |
404 | isl_ast_node_free(User); |
405 | } |
406 | |
407 | void IslNodeBuilder::createMark(__isl_take isl_ast_node *Node) { |
408 | auto *Id = isl_ast_node_mark_get_id(Node); |
409 | auto Child = isl_ast_node_mark_get_node(Node); |
410 | isl_ast_node_free(Node); |
411 | // If a child node of a 'SIMD mark' is a loop that has a single iteration, |
412 | // it will be optimized away and we should skip it. |
413 | if (strcmp(isl_id_get_name(Id), "SIMD") == 0 && |
414 | isl_ast_node_get_type(Child) == isl_ast_node_for) { |
415 | bool Vector = PollyVectorizerChoice == VECTORIZER_POLLY; |
416 | int VectorWidth = getNumberOfIterations(isl::manage_copy(Child)); |
417 | if (Vector && 1 < VectorWidth && VectorWidth <= 16) |
418 | createForVector(Child, VectorWidth); |
419 | else |
420 | createForSequential(isl::manage(Child), true); |
421 | isl_id_free(Id); |
422 | return; |
423 | } |
424 | if (strcmp(isl_id_get_name(Id), "Inter iteration alias-free") == 0) { |
425 | auto *BasePtr = static_cast<Value *>(isl_id_get_user(Id)); |
426 | Annotator.addInterIterationAliasFreeBasePtr(BasePtr); |
427 | } |
428 | |
429 | BandAttr *ChildLoopAttr = getLoopAttr(isl::manage_copy(Id)); |
430 | BandAttr *AncestorLoopAttr; |
431 | if (ChildLoopAttr) { |
432 | // Save current LoopAttr environment to restore again when leaving this |
433 | // subtree. This means there was no loop between the ancestor LoopAttr and |
434 | // this mark, i.e. the ancestor LoopAttr did not directly mark a loop. This |
435 | // can happen e.g. if the AST build peeled or unrolled the loop. |
436 | AncestorLoopAttr = Annotator.getStagingAttrEnv(); |
437 | |
438 | Annotator.getStagingAttrEnv() = ChildLoopAttr; |
439 | } |
440 | |
441 | create(Child); |
442 | |
443 | if (ChildLoopAttr) { |
444 | assert(Annotator.getStagingAttrEnv() == ChildLoopAttr &&(static_cast <bool> (Annotator.getStagingAttrEnv() == ChildLoopAttr && "Nest must not overwrite loop attr environment") ? void (0) : __assert_fail ("Annotator.getStagingAttrEnv() == ChildLoopAttr && \"Nest must not overwrite loop attr environment\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 445, __extension__ __PRETTY_FUNCTION__)) |
445 | "Nest must not overwrite loop attr environment")(static_cast <bool> (Annotator.getStagingAttrEnv() == ChildLoopAttr && "Nest must not overwrite loop attr environment") ? void (0) : __assert_fail ("Annotator.getStagingAttrEnv() == ChildLoopAttr && \"Nest must not overwrite loop attr environment\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 445, __extension__ __PRETTY_FUNCTION__)); |
446 | Annotator.getStagingAttrEnv() = AncestorLoopAttr; |
447 | } |
448 | |
449 | isl_id_free(Id); |
450 | } |
451 | |
452 | void IslNodeBuilder::createForVector(__isl_take isl_ast_node *For, |
453 | int VectorWidth) { |
454 | isl_ast_node *Body = isl_ast_node_for_get_body(For); |
455 | isl_ast_expr *Init = isl_ast_node_for_get_init(For); |
456 | isl_ast_expr *Inc = isl_ast_node_for_get_inc(For); |
457 | isl_ast_expr *Iterator = isl_ast_node_for_get_iterator(For); |
458 | isl_id *IteratorID = isl_ast_expr_get_id(Iterator); |
459 | |
460 | Value *ValueLB = ExprBuilder.create(Init); |
461 | Value *ValueInc = ExprBuilder.create(Inc); |
462 | |
463 | Type *MaxType = ExprBuilder.getType(Iterator); |
464 | MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType()); |
465 | MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType()); |
466 | |
467 | if (MaxType != ValueLB->getType()) |
468 | ValueLB = Builder.CreateSExt(ValueLB, MaxType); |
469 | if (MaxType != ValueInc->getType()) |
470 | ValueInc = Builder.CreateSExt(ValueInc, MaxType); |
471 | |
472 | std::vector<Value *> IVS(VectorWidth); |
473 | IVS[0] = ValueLB; |
474 | |
475 | for (int i = 1; i < VectorWidth; i++) |
476 | IVS[i] = Builder.CreateAdd(IVS[i - 1], ValueInc, "p_vector_iv"); |
477 | |
478 | isl::union_map Schedule = getScheduleForAstNode(isl::manage_copy(For)); |
479 | assert(!Schedule.is_null() &&(static_cast <bool> (!Schedule.is_null() && "For statement annotation does not contain its schedule" ) ? void (0) : __assert_fail ("!Schedule.is_null() && \"For statement annotation does not contain its schedule\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 480, __extension__ __PRETTY_FUNCTION__)) |
480 | "For statement annotation does not contain its schedule")(static_cast <bool> (!Schedule.is_null() && "For statement annotation does not contain its schedule" ) ? void (0) : __assert_fail ("!Schedule.is_null() && \"For statement annotation does not contain its schedule\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 480, __extension__ __PRETTY_FUNCTION__)); |
481 | |
482 | IDToValue[IteratorID] = ValueLB; |
483 | |
484 | switch (isl_ast_node_get_type(Body)) { |
485 | case isl_ast_node_user: |
486 | createUserVector(Body, IVS, isl_id_copy(IteratorID), Schedule.copy()); |
487 | break; |
488 | case isl_ast_node_block: { |
489 | isl_ast_node_list *List = isl_ast_node_block_get_children(Body); |
490 | |
491 | for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i) |
492 | createUserVector(isl_ast_node_list_get_ast_node(List, i), IVS, |
493 | isl_id_copy(IteratorID), Schedule.copy()); |
494 | |
495 | isl_ast_node_free(Body); |
496 | isl_ast_node_list_free(List); |
497 | break; |
498 | } |
499 | default: |
500 | isl_ast_node_dump(Body); |
501 | llvm_unreachable("Unhandled isl_ast_node in vectorizer")::llvm::llvm_unreachable_internal("Unhandled isl_ast_node in vectorizer" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 501); |
502 | } |
503 | |
504 | IDToValue.erase(IDToValue.find(IteratorID)); |
505 | isl_id_free(IteratorID); |
506 | |
507 | isl_ast_node_free(For); |
508 | isl_ast_expr_free(Iterator); |
509 | |
510 | VectorLoops++; |
511 | } |
512 | |
513 | /// Restore the initial ordering of dimensions of the band node |
514 | /// |
515 | /// In case the band node represents all the dimensions of the iteration |
516 | /// domain, recreate the band node to restore the initial ordering of the |
517 | /// dimensions. |
518 | /// |
519 | /// @param Node The band node to be modified. |
520 | /// @return The modified schedule node. |
521 | static bool IsLoopVectorizerDisabled(isl::ast_node Node) { |
522 | assert(isl_ast_node_get_type(Node.get()) == isl_ast_node_for)(static_cast <bool> (isl_ast_node_get_type(Node.get()) == isl_ast_node_for) ? void (0) : __assert_fail ("isl_ast_node_get_type(Node.get()) == isl_ast_node_for" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 522, __extension__ __PRETTY_FUNCTION__)); |
523 | auto Body = Node.for_get_body(); |
524 | if (isl_ast_node_get_type(Body.get()) != isl_ast_node_mark) |
525 | return false; |
526 | auto Id = Body.mark_get_id(); |
527 | if (strcmp(Id.get_name().c_str(), "Loop Vectorizer Disabled") == 0) |
528 | return true; |
529 | return false; |
530 | } |
531 | |
532 | void IslNodeBuilder::createForSequential(isl::ast_node For, bool MarkParallel) { |
533 | Value *ValueLB, *ValueUB, *ValueInc; |
534 | Type *MaxType; |
535 | BasicBlock *ExitBlock; |
536 | Value *IV; |
537 | CmpInst::Predicate Predicate; |
538 | |
539 | bool LoopVectorizerDisabled = IsLoopVectorizerDisabled(For); |
540 | |
541 | isl::ast_node Body = For.for_get_body(); |
542 | |
543 | // isl_ast_node_for_is_degenerate(For) |
544 | // |
545 | // TODO: For degenerated loops we could generate a plain assignment. |
546 | // However, for now we just reuse the logic for normal loops, which will |
547 | // create a loop with a single iteration. |
548 | |
549 | isl::ast_expr Init = For.for_get_init(); |
550 | isl::ast_expr Inc = For.for_get_inc(); |
551 | isl::ast_expr Iterator = For.for_get_iterator(); |
552 | isl::id IteratorID = Iterator.get_id(); |
553 | isl::ast_expr UB = getUpperBound(For, Predicate); |
554 | |
555 | ValueLB = ExprBuilder.create(Init.release()); |
556 | ValueUB = ExprBuilder.create(UB.release()); |
557 | ValueInc = ExprBuilder.create(Inc.release()); |
558 | |
559 | MaxType = ExprBuilder.getType(Iterator.get()); |
560 | MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType()); |
561 | MaxType = ExprBuilder.getWidestType(MaxType, ValueUB->getType()); |
562 | MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType()); |
563 | |
564 | if (MaxType != ValueLB->getType()) |
565 | ValueLB = Builder.CreateSExt(ValueLB, MaxType); |
566 | if (MaxType != ValueUB->getType()) |
567 | ValueUB = Builder.CreateSExt(ValueUB, MaxType); |
568 | if (MaxType != ValueInc->getType()) |
569 | ValueInc = Builder.CreateSExt(ValueInc, MaxType); |
570 | |
571 | // If we can show that LB <Predicate> UB holds at least once, we can |
572 | // omit the GuardBB in front of the loop. |
573 | bool UseGuardBB = |
574 | !SE.isKnownPredicate(Predicate, SE.getSCEV(ValueLB), SE.getSCEV(ValueUB)); |
575 | IV = createLoop(ValueLB, ValueUB, ValueInc, Builder, LI, DT, ExitBlock, |
576 | Predicate, &Annotator, MarkParallel, UseGuardBB, |
577 | LoopVectorizerDisabled); |
578 | IDToValue[IteratorID.get()] = IV; |
579 | |
580 | create(Body.release()); |
581 | |
582 | Annotator.popLoop(MarkParallel); |
583 | |
584 | IDToValue.erase(IDToValue.find(IteratorID.get())); |
585 | |
586 | Builder.SetInsertPoint(&ExitBlock->front()); |
587 | |
588 | SequentialLoops++; |
589 | } |
590 | |
591 | /// Remove the BBs contained in a (sub)function from the dominator tree. |
592 | /// |
593 | /// This function removes the basic blocks that are part of a subfunction from |
594 | /// the dominator tree. Specifically, when generating code it may happen that at |
595 | /// some point the code generation continues in a new sub-function (e.g., when |
596 | /// generating OpenMP code). The basic blocks that are created in this |
597 | /// sub-function are then still part of the dominator tree of the original |
598 | /// function, such that the dominator tree reaches over function boundaries. |
599 | /// This is not only incorrect, but also causes crashes. This function now |
600 | /// removes from the dominator tree all basic blocks that are dominated (and |
601 | /// consequently reachable) from the entry block of this (sub)function. |
602 | /// |
603 | /// FIXME: A LLVM (function or region) pass should not touch anything outside of |
604 | /// the function/region it runs on. Hence, the pure need for this function shows |
605 | /// that we do not comply to this rule. At the moment, this does not cause any |
606 | /// issues, but we should be aware that such issues may appear. Unfortunately |
607 | /// the current LLVM pass infrastructure does not allow to make Polly a module |
608 | /// or call-graph pass to solve this issue, as such a pass would not have access |
609 | /// to the per-function analyses passes needed by Polly. A future pass manager |
610 | /// infrastructure is supposed to enable such kind of access possibly allowing |
611 | /// us to create a cleaner solution here. |
612 | /// |
613 | /// FIXME: Instead of adding the dominance information and then dropping it |
614 | /// later on, we should try to just not add it in the first place. This requires |
615 | /// some careful testing to make sure this does not break in interaction with |
616 | /// the SCEVBuilder and SplitBlock which may rely on the dominator tree or |
617 | /// which may try to update it. |
618 | /// |
619 | /// @param F The function which contains the BBs to removed. |
620 | /// @param DT The dominator tree from which to remove the BBs. |
621 | static void removeSubFuncFromDomTree(Function *F, DominatorTree &DT) { |
622 | DomTreeNode *N = DT.getNode(&F->getEntryBlock()); |
623 | std::vector<BasicBlock *> Nodes; |
624 | |
625 | // We can only remove an element from the dominator tree, if all its children |
626 | // have been removed. To ensure this we obtain the list of nodes to remove |
627 | // using a post-order tree traversal. |
628 | for (po_iterator<DomTreeNode *> I = po_begin(N), E = po_end(N); I != E; ++I) |
629 | Nodes.push_back(I->getBlock()); |
630 | |
631 | for (BasicBlock *BB : Nodes) |
632 | DT.eraseNode(BB); |
633 | } |
634 | |
635 | void IslNodeBuilder::createForParallel(__isl_take isl_ast_node *For) { |
636 | isl_ast_node *Body; |
637 | isl_ast_expr *Init, *Inc, *Iterator, *UB; |
638 | isl_id *IteratorID; |
639 | Value *ValueLB, *ValueUB, *ValueInc; |
640 | Type *MaxType; |
641 | Value *IV; |
642 | CmpInst::Predicate Predicate; |
643 | |
644 | // The preamble of parallel code interacts different than normal code with |
645 | // e.g., scalar initialization. Therefore, we ensure the parallel code is |
646 | // separated from the last basic block. |
647 | BasicBlock *ParBB = SplitBlock(Builder.GetInsertBlock(), |
648 | &*Builder.GetInsertPoint(), &DT, &LI); |
649 | ParBB->setName("polly.parallel.for"); |
650 | Builder.SetInsertPoint(&ParBB->front()); |
651 | |
652 | Body = isl_ast_node_for_get_body(For); |
653 | Init = isl_ast_node_for_get_init(For); |
654 | Inc = isl_ast_node_for_get_inc(For); |
655 | Iterator = isl_ast_node_for_get_iterator(For); |
656 | IteratorID = isl_ast_expr_get_id(Iterator); |
657 | UB = getUpperBound(isl::manage_copy(For), Predicate).release(); |
658 | |
659 | ValueLB = ExprBuilder.create(Init); |
660 | ValueUB = ExprBuilder.create(UB); |
661 | ValueInc = ExprBuilder.create(Inc); |
662 | |
663 | // OpenMP always uses SLE. In case the isl generated AST uses a SLT |
664 | // expression, we need to adjust the loop bound by one. |
665 | if (Predicate == CmpInst::ICMP_SLT) |
666 | ValueUB = Builder.CreateAdd( |
667 | ValueUB, Builder.CreateSExt(Builder.getTrue(), ValueUB->getType())); |
668 | |
669 | MaxType = ExprBuilder.getType(Iterator); |
670 | MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType()); |
671 | MaxType = ExprBuilder.getWidestType(MaxType, ValueUB->getType()); |
672 | MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType()); |
673 | |
674 | if (MaxType != ValueLB->getType()) |
675 | ValueLB = Builder.CreateSExt(ValueLB, MaxType); |
676 | if (MaxType != ValueUB->getType()) |
677 | ValueUB = Builder.CreateSExt(ValueUB, MaxType); |
678 | if (MaxType != ValueInc->getType()) |
679 | ValueInc = Builder.CreateSExt(ValueInc, MaxType); |
680 | |
681 | BasicBlock::iterator LoopBody; |
682 | |
683 | SetVector<Value *> SubtreeValues; |
684 | SetVector<const Loop *> Loops; |
685 | |
686 | getReferencesInSubtree(isl::manage_copy(For), SubtreeValues, Loops); |
687 | |
688 | // Create for all loops we depend on values that contain the current loop |
689 | // iteration. These values are necessary to generate code for SCEVs that |
690 | // depend on such loops. As a result we need to pass them to the subfunction. |
691 | // See [Code generation of induction variables of loops outside Scops] |
692 | for (const Loop *L : Loops) { |
693 | Value *LoopInductionVar = materializeNonScopLoopInductionVariable(L); |
694 | SubtreeValues.insert(LoopInductionVar); |
695 | } |
696 | |
697 | ValueMapT NewValues; |
698 | |
699 | std::unique_ptr<ParallelLoopGenerator> ParallelLoopGenPtr; |
700 | |
701 | switch (PollyOmpBackend) { |
702 | case OpenMPBackend::GNU: |
703 | ParallelLoopGenPtr.reset( |
704 | new ParallelLoopGeneratorGOMP(Builder, LI, DT, DL)); |
705 | break; |
706 | case OpenMPBackend::LLVM: |
707 | ParallelLoopGenPtr.reset(new ParallelLoopGeneratorKMP(Builder, LI, DT, DL)); |
708 | break; |
709 | } |
710 | |
711 | IV = ParallelLoopGenPtr->createParallelLoop( |
712 | ValueLB, ValueUB, ValueInc, SubtreeValues, NewValues, &LoopBody); |
713 | BasicBlock::iterator AfterLoop = Builder.GetInsertPoint(); |
714 | Builder.SetInsertPoint(&*LoopBody); |
715 | |
716 | // Remember the parallel subfunction |
717 | ParallelSubfunctions.push_back(LoopBody->getFunction()); |
718 | |
719 | // Save the current values. |
720 | auto ValueMapCopy = ValueMap; |
721 | IslExprBuilder::IDToValueTy IDToValueCopy = IDToValue; |
722 | |
723 | updateValues(NewValues); |
724 | IDToValue[IteratorID] = IV; |
725 | |
726 | ValueMapT NewValuesReverse; |
727 | |
728 | for (auto P : NewValues) |
729 | NewValuesReverse[P.second] = P.first; |
730 | |
731 | Annotator.addAlternativeAliasBases(NewValuesReverse); |
732 | |
733 | create(Body); |
734 | |
735 | Annotator.resetAlternativeAliasBases(); |
736 | // Restore the original values. |
737 | ValueMap = ValueMapCopy; |
738 | IDToValue = IDToValueCopy; |
739 | |
740 | Builder.SetInsertPoint(&*AfterLoop); |
741 | removeSubFuncFromDomTree((*LoopBody).getParent()->getParent(), DT); |
742 | |
743 | for (const Loop *L : Loops) |
744 | OutsideLoopIterations.erase(L); |
745 | |
746 | isl_ast_node_free(For); |
747 | isl_ast_expr_free(Iterator); |
748 | isl_id_free(IteratorID); |
749 | |
750 | ParallelLoops++; |
751 | } |
752 | |
753 | /// Return whether any of @p Node's statements contain partial accesses. |
754 | /// |
755 | /// Partial accesses are not supported by Polly's vector code generator. |
756 | static bool hasPartialAccesses(__isl_take isl_ast_node *Node) { |
757 | return isl_ast_node_foreach_descendant_top_down( |
758 | Node, |
759 | [](isl_ast_node *Node, void *User) -> isl_bool { |
760 | if (isl_ast_node_get_type(Node) != isl_ast_node_user) |
761 | return isl_bool_true; |
762 | |
763 | isl::ast_expr Expr = |
764 | isl::manage(isl_ast_node_user_get_expr(Node)); |
765 | isl::ast_expr StmtExpr = Expr.get_op_arg(0); |
766 | isl::id Id = StmtExpr.get_id(); |
767 | |
768 | ScopStmt *Stmt = |
769 | static_cast<ScopStmt *>(isl_id_get_user(Id.get())); |
770 | isl::set StmtDom = Stmt->getDomain(); |
771 | for (auto *MA : *Stmt) { |
772 | if (MA->isLatestPartialAccess()) |
773 | return isl_bool_error; |
774 | } |
775 | return isl_bool_true; |
776 | }, |
777 | nullptr) == isl_stat_error; |
778 | } |
779 | |
780 | void IslNodeBuilder::createFor(__isl_take isl_ast_node *For) { |
781 | bool Vector = PollyVectorizerChoice == VECTORIZER_POLLY; |
782 | |
783 | if (Vector && IslAstInfo::isInnermostParallel(isl::manage_copy(For)) && |
784 | !IslAstInfo::isReductionParallel(isl::manage_copy(For))) { |
785 | int VectorWidth = getNumberOfIterations(isl::manage_copy(For)); |
786 | if (1 < VectorWidth && VectorWidth <= 16 && !hasPartialAccesses(For)) { |
787 | createForVector(For, VectorWidth); |
788 | return; |
789 | } |
790 | } |
791 | |
792 | if (IslAstInfo::isExecutedInParallel(isl::manage_copy(For))) { |
793 | createForParallel(For); |
794 | return; |
795 | } |
796 | bool Parallel = (IslAstInfo::isParallel(isl::manage_copy(For)) && |
797 | !IslAstInfo::isReductionParallel(isl::manage_copy(For))); |
798 | createForSequential(isl::manage(For), Parallel); |
799 | } |
800 | |
801 | void IslNodeBuilder::createIf(__isl_take isl_ast_node *If) { |
802 | isl_ast_expr *Cond = isl_ast_node_if_get_cond(If); |
803 | |
804 | Function *F = Builder.GetInsertBlock()->getParent(); |
805 | LLVMContext &Context = F->getContext(); |
806 | |
807 | BasicBlock *CondBB = SplitBlock(Builder.GetInsertBlock(), |
808 | &*Builder.GetInsertPoint(), &DT, &LI); |
809 | CondBB->setName("polly.cond"); |
810 | BasicBlock *MergeBB = SplitBlock(CondBB, &CondBB->front(), &DT, &LI); |
811 | MergeBB->setName("polly.merge"); |
812 | BasicBlock *ThenBB = BasicBlock::Create(Context, "polly.then", F); |
813 | BasicBlock *ElseBB = BasicBlock::Create(Context, "polly.else", F); |
814 | |
815 | DT.addNewBlock(ThenBB, CondBB); |
816 | DT.addNewBlock(ElseBB, CondBB); |
817 | DT.changeImmediateDominator(MergeBB, CondBB); |
818 | |
819 | Loop *L = LI.getLoopFor(CondBB); |
820 | if (L) { |
821 | L->addBasicBlockToLoop(ThenBB, LI); |
822 | L->addBasicBlockToLoop(ElseBB, LI); |
823 | } |
824 | |
825 | CondBB->getTerminator()->eraseFromParent(); |
826 | |
827 | Builder.SetInsertPoint(CondBB); |
828 | Value *Predicate = ExprBuilder.create(Cond); |
829 | Builder.CreateCondBr(Predicate, ThenBB, ElseBB); |
830 | Builder.SetInsertPoint(ThenBB); |
831 | Builder.CreateBr(MergeBB); |
832 | Builder.SetInsertPoint(ElseBB); |
833 | Builder.CreateBr(MergeBB); |
834 | Builder.SetInsertPoint(&ThenBB->front()); |
835 | |
836 | create(isl_ast_node_if_get_then(If)); |
837 | |
838 | Builder.SetInsertPoint(&ElseBB->front()); |
839 | |
840 | if (isl_ast_node_if_has_else(If)) |
841 | create(isl_ast_node_if_get_else(If)); |
842 | |
843 | Builder.SetInsertPoint(&MergeBB->front()); |
844 | |
845 | isl_ast_node_free(If); |
846 | |
847 | IfConditions++; |
848 | } |
849 | |
850 | __isl_give isl_id_to_ast_expr * |
851 | IslNodeBuilder::createNewAccesses(ScopStmt *Stmt, |
852 | __isl_keep isl_ast_node *Node) { |
853 | isl::id_to_ast_expr NewAccesses = |
854 | isl::id_to_ast_expr::alloc(Stmt->getParent()->getIslCtx(), 0); |
855 | |
856 | isl::ast_build Build = IslAstInfo::getBuild(isl::manage_copy(Node)); |
857 | assert(!Build.is_null() && "Could not obtain isl_ast_build from user node")(static_cast <bool> (!Build.is_null() && "Could not obtain isl_ast_build from user node" ) ? void (0) : __assert_fail ("!Build.is_null() && \"Could not obtain isl_ast_build from user node\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 857, __extension__ __PRETTY_FUNCTION__)); |
858 | Stmt->setAstBuild(Build); |
859 | |
860 | for (auto *MA : *Stmt) { |
861 | if (!MA->hasNewAccessRelation()) { |
862 | if (PollyGenerateExpressions) { |
863 | if (!MA->isAffine()) |
864 | continue; |
865 | if (MA->getLatestScopArrayInfo()->getBasePtrOriginSAI()) |
866 | continue; |
867 | |
868 | auto *BasePtr = |
869 | dyn_cast<Instruction>(MA->getLatestScopArrayInfo()->getBasePtr()); |
870 | if (BasePtr && Stmt->getParent()->getRegion().contains(BasePtr)) |
871 | continue; |
872 | } else { |
873 | continue; |
874 | } |
875 | } |
876 | assert(MA->isAffine() &&(static_cast <bool> (MA->isAffine() && "Only affine memory accesses can be code generated" ) ? void (0) : __assert_fail ("MA->isAffine() && \"Only affine memory accesses can be code generated\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 877, __extension__ __PRETTY_FUNCTION__)) |
877 | "Only affine memory accesses can be code generated")(static_cast <bool> (MA->isAffine() && "Only affine memory accesses can be code generated" ) ? void (0) : __assert_fail ("MA->isAffine() && \"Only affine memory accesses can be code generated\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 877, __extension__ __PRETTY_FUNCTION__)); |
878 | |
879 | isl::union_map Schedule = Build.get_schedule(); |
880 | |
881 | #ifndef NDEBUG |
882 | if (MA->isRead()) { |
883 | auto Dom = Stmt->getDomain().release(); |
884 | auto SchedDom = isl_set_from_union_set(Schedule.domain().release()); |
885 | auto AccDom = isl_map_domain(MA->getAccessRelation().release()); |
886 | Dom = isl_set_intersect_params(Dom, |
887 | Stmt->getParent()->getContext().release()); |
888 | SchedDom = isl_set_intersect_params( |
889 | SchedDom, Stmt->getParent()->getContext().release()); |
890 | assert(isl_set_is_subset(SchedDom, AccDom) &&(static_cast <bool> (isl_set_is_subset(SchedDom, AccDom ) && "Access relation not defined on full schedule domain" ) ? void (0) : __assert_fail ("isl_set_is_subset(SchedDom, AccDom) && \"Access relation not defined on full schedule domain\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 891, __extension__ __PRETTY_FUNCTION__)) |
891 | "Access relation not defined on full schedule domain")(static_cast <bool> (isl_set_is_subset(SchedDom, AccDom ) && "Access relation not defined on full schedule domain" ) ? void (0) : __assert_fail ("isl_set_is_subset(SchedDom, AccDom) && \"Access relation not defined on full schedule domain\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 891, __extension__ __PRETTY_FUNCTION__)); |
892 | assert(isl_set_is_subset(Dom, AccDom) &&(static_cast <bool> (isl_set_is_subset(Dom, AccDom) && "Access relation not defined on full domain") ? void (0) : __assert_fail ("isl_set_is_subset(Dom, AccDom) && \"Access relation not defined on full domain\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 893, __extension__ __PRETTY_FUNCTION__)) |
893 | "Access relation not defined on full domain")(static_cast <bool> (isl_set_is_subset(Dom, AccDom) && "Access relation not defined on full domain") ? void (0) : __assert_fail ("isl_set_is_subset(Dom, AccDom) && \"Access relation not defined on full domain\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 893, __extension__ __PRETTY_FUNCTION__)); |
894 | isl_set_free(AccDom); |
895 | isl_set_free(SchedDom); |
896 | isl_set_free(Dom); |
897 | } |
898 | #endif |
899 | |
900 | isl::pw_multi_aff PWAccRel = MA->applyScheduleToAccessRelation(Schedule); |
901 | |
902 | // isl cannot generate an index expression for access-nothing accesses. |
903 | isl::set AccDomain = PWAccRel.domain(); |
904 | isl::set Context = S.getContext(); |
905 | AccDomain = AccDomain.intersect_params(Context); |
906 | if (AccDomain.is_empty()) |
907 | continue; |
908 | |
909 | isl::ast_expr AccessExpr = Build.access_from(PWAccRel); |
910 | NewAccesses = NewAccesses.set(MA->getId(), AccessExpr); |
911 | } |
912 | |
913 | return NewAccesses.release(); |
914 | } |
915 | |
916 | void IslNodeBuilder::createSubstitutions(__isl_take isl_ast_expr *Expr, |
917 | ScopStmt *Stmt, LoopToScevMapT <S) { |
918 | assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&(static_cast <bool> (isl_ast_expr_get_type(Expr) == isl_ast_expr_op && "Expression of type 'op' expected") ? void (0) : __assert_fail ("isl_ast_expr_get_type(Expr) == isl_ast_expr_op && \"Expression of type 'op' expected\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 919, __extension__ __PRETTY_FUNCTION__)) |
919 | "Expression of type 'op' expected")(static_cast <bool> (isl_ast_expr_get_type(Expr) == isl_ast_expr_op && "Expression of type 'op' expected") ? void (0) : __assert_fail ("isl_ast_expr_get_type(Expr) == isl_ast_expr_op && \"Expression of type 'op' expected\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 919, __extension__ __PRETTY_FUNCTION__)); |
920 | assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_call &&(static_cast <bool> (isl_ast_expr_get_op_type(Expr) == isl_ast_expr_op_call && "Operation of type 'call' expected") ? void (0) : __assert_fail ("isl_ast_expr_get_op_type(Expr) == isl_ast_op_call && \"Operation of type 'call' expected\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 921, __extension__ __PRETTY_FUNCTION__)) |
921 | "Operation of type 'call' expected")(static_cast <bool> (isl_ast_expr_get_op_type(Expr) == isl_ast_expr_op_call && "Operation of type 'call' expected") ? void (0) : __assert_fail ("isl_ast_expr_get_op_type(Expr) == isl_ast_op_call && \"Operation of type 'call' expected\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 921, __extension__ __PRETTY_FUNCTION__)); |
922 | for (int i = 0; i < isl_ast_expr_get_op_n_arg(Expr) - 1; ++i) { |
923 | isl_ast_expr *SubExpr; |
924 | Value *V; |
925 | |
926 | SubExpr = isl_ast_expr_get_op_arg(Expr, i + 1); |
927 | V = ExprBuilder.create(SubExpr); |
928 | ScalarEvolution *SE = Stmt->getParent()->getSE(); |
929 | LTS[Stmt->getLoopForDimension(i)] = SE->getUnknown(V); |
930 | } |
931 | |
932 | isl_ast_expr_free(Expr); |
933 | } |
934 | |
935 | void IslNodeBuilder::createSubstitutionsVector( |
936 | __isl_take isl_ast_expr *Expr, ScopStmt *Stmt, |
937 | std::vector<LoopToScevMapT> &VLTS, std::vector<Value *> &IVS, |
938 | __isl_take isl_id *IteratorID) { |
939 | int i = 0; |
940 | |
941 | Value *OldValue = IDToValue[IteratorID]; |
942 | for (Value *IV : IVS) { |
943 | IDToValue[IteratorID] = IV; |
944 | createSubstitutions(isl_ast_expr_copy(Expr), Stmt, VLTS[i]); |
945 | i++; |
946 | } |
947 | |
948 | IDToValue[IteratorID] = OldValue; |
949 | isl_id_free(IteratorID); |
950 | isl_ast_expr_free(Expr); |
951 | } |
952 | |
953 | void IslNodeBuilder::generateCopyStmt( |
954 | ScopStmt *Stmt, __isl_keep isl_id_to_ast_expr *NewAccesses) { |
955 | assert(Stmt->size() == 2)(static_cast <bool> (Stmt->size() == 2) ? void (0) : __assert_fail ("Stmt->size() == 2", "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 955, __extension__ __PRETTY_FUNCTION__)); |
956 | auto ReadAccess = Stmt->begin(); |
957 | auto WriteAccess = ReadAccess++; |
958 | assert((*ReadAccess)->isRead() && (*WriteAccess)->isMustWrite())(static_cast <bool> ((*ReadAccess)->isRead() && (*WriteAccess)->isMustWrite()) ? void (0) : __assert_fail ("(*ReadAccess)->isRead() && (*WriteAccess)->isMustWrite()" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 958, __extension__ __PRETTY_FUNCTION__)); |
959 | assert((*ReadAccess)->getElementType() == (*WriteAccess)->getElementType() &&(static_cast <bool> ((*ReadAccess)->getElementType() == (*WriteAccess)->getElementType() && "Accesses use the same data type" ) ? void (0) : __assert_fail ("(*ReadAccess)->getElementType() == (*WriteAccess)->getElementType() && \"Accesses use the same data type\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 960, __extension__ __PRETTY_FUNCTION__)) |
960 | "Accesses use the same data type")(static_cast <bool> ((*ReadAccess)->getElementType() == (*WriteAccess)->getElementType() && "Accesses use the same data type" ) ? void (0) : __assert_fail ("(*ReadAccess)->getElementType() == (*WriteAccess)->getElementType() && \"Accesses use the same data type\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 960, __extension__ __PRETTY_FUNCTION__)); |
961 | assert((*ReadAccess)->isArrayKind() && (*WriteAccess)->isArrayKind())(static_cast <bool> ((*ReadAccess)->isArrayKind() && (*WriteAccess)->isArrayKind()) ? void (0) : __assert_fail ("(*ReadAccess)->isArrayKind() && (*WriteAccess)->isArrayKind()" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 961, __extension__ __PRETTY_FUNCTION__)); |
962 | auto *AccessExpr = |
963 | isl_id_to_ast_expr_get(NewAccesses, (*ReadAccess)->getId().release()); |
964 | auto *LoadValue = ExprBuilder.create(AccessExpr); |
965 | AccessExpr = |
966 | isl_id_to_ast_expr_get(NewAccesses, (*WriteAccess)->getId().release()); |
967 | auto *StoreAddr = ExprBuilder.createAccessAddress(AccessExpr).first; |
968 | Builder.CreateStore(LoadValue, StoreAddr); |
969 | } |
970 | |
971 | Value *IslNodeBuilder::materializeNonScopLoopInductionVariable(const Loop *L) { |
972 | assert(OutsideLoopIterations.find(L) == OutsideLoopIterations.end() &&(static_cast <bool> (OutsideLoopIterations.find(L) == OutsideLoopIterations .end() && "trying to materialize loop induction variable twice" ) ? void (0) : __assert_fail ("OutsideLoopIterations.find(L) == OutsideLoopIterations.end() && \"trying to materialize loop induction variable twice\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 973, __extension__ __PRETTY_FUNCTION__)) |
973 | "trying to materialize loop induction variable twice")(static_cast <bool> (OutsideLoopIterations.find(L) == OutsideLoopIterations .end() && "trying to materialize loop induction variable twice" ) ? void (0) : __assert_fail ("OutsideLoopIterations.find(L) == OutsideLoopIterations.end() && \"trying to materialize loop induction variable twice\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 973, __extension__ __PRETTY_FUNCTION__)); |
974 | const SCEV *OuterLIV = SE.getAddRecExpr(SE.getUnknown(Builder.getInt64(0)), |
975 | SE.getUnknown(Builder.getInt64(1)), L, |
976 | SCEV::FlagAnyWrap); |
977 | Value *V = generateSCEV(OuterLIV); |
978 | OutsideLoopIterations[L] = SE.getUnknown(V); |
979 | return V; |
980 | } |
981 | |
982 | void IslNodeBuilder::createUser(__isl_take isl_ast_node *User) { |
983 | LoopToScevMapT LTS; |
984 | isl_id *Id; |
985 | ScopStmt *Stmt; |
986 | |
987 | isl_ast_expr *Expr = isl_ast_node_user_get_expr(User); |
988 | isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0); |
989 | Id = isl_ast_expr_get_id(StmtExpr); |
990 | isl_ast_expr_free(StmtExpr); |
991 | |
992 | LTS.insert(OutsideLoopIterations.begin(), OutsideLoopIterations.end()); |
993 | |
994 | Stmt = (ScopStmt *)isl_id_get_user(Id); |
995 | auto *NewAccesses = createNewAccesses(Stmt, User); |
996 | if (Stmt->isCopyStmt()) { |
997 | generateCopyStmt(Stmt, NewAccesses); |
998 | isl_ast_expr_free(Expr); |
999 | } else { |
1000 | createSubstitutions(Expr, Stmt, LTS); |
1001 | |
1002 | if (Stmt->isBlockStmt()) |
1003 | BlockGen.copyStmt(*Stmt, LTS, NewAccesses); |
1004 | else |
1005 | RegionGen.copyStmt(*Stmt, LTS, NewAccesses); |
1006 | } |
1007 | |
1008 | isl_id_to_ast_expr_free(NewAccesses); |
1009 | isl_ast_node_free(User); |
1010 | isl_id_free(Id); |
1011 | } |
1012 | |
1013 | void IslNodeBuilder::createBlock(__isl_take isl_ast_node *Block) { |
1014 | isl_ast_node_list *List = isl_ast_node_block_get_children(Block); |
1015 | |
1016 | for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i) |
1017 | create(isl_ast_node_list_get_ast_node(List, i)); |
1018 | |
1019 | isl_ast_node_free(Block); |
1020 | isl_ast_node_list_free(List); |
1021 | } |
1022 | |
1023 | void IslNodeBuilder::create(__isl_take isl_ast_node *Node) { |
1024 | switch (isl_ast_node_get_type(Node)) { |
1025 | case isl_ast_node_error: |
1026 | llvm_unreachable("code generation error")::llvm::llvm_unreachable_internal("code generation error", "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 1026); |
1027 | case isl_ast_node_mark: |
1028 | createMark(Node); |
1029 | return; |
1030 | case isl_ast_node_for: |
1031 | createFor(Node); |
1032 | return; |
1033 | case isl_ast_node_if: |
1034 | createIf(Node); |
1035 | return; |
1036 | case isl_ast_node_user: |
1037 | createUser(Node); |
1038 | return; |
1039 | case isl_ast_node_block: |
1040 | createBlock(Node); |
1041 | return; |
1042 | } |
1043 | |
1044 | llvm_unreachable("Unknown isl_ast_node type")::llvm::llvm_unreachable_internal("Unknown isl_ast_node type" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 1044); |
1045 | } |
1046 | |
1047 | bool IslNodeBuilder::materializeValue(isl_id *Id) { |
1048 | // If the Id is already mapped, skip it. |
1049 | if (!IDToValue.count(Id)) { |
1050 | auto *ParamSCEV = (const SCEV *)isl_id_get_user(Id); |
1051 | Value *V = nullptr; |
1052 | |
1053 | // Parameters could refer to invariant loads that need to be |
1054 | // preloaded before we can generate code for the parameter. Thus, |
1055 | // check if any value referred to in ParamSCEV is an invariant load |
1056 | // and if so make sure its equivalence class is preloaded. |
1057 | SetVector<Value *> Values; |
1058 | findValues(ParamSCEV, SE, Values); |
1059 | for (auto *Val : Values) { |
1060 | // Check if the value is an instruction in a dead block within the SCoP |
1061 | // and if so do not code generate it. |
1062 | if (auto *Inst = dyn_cast<Instruction>(Val)) { |
1063 | if (S.contains(Inst)) { |
1064 | bool IsDead = true; |
1065 | |
1066 | // Check for "undef" loads first, then if there is a statement for |
1067 | // the parent of Inst and lastly if the parent of Inst has an empty |
1068 | // domain. In the first and last case the instruction is dead but if |
1069 | // there is a statement or the domain is not empty Inst is not dead. |
1070 | auto MemInst = MemAccInst::dyn_cast(Inst); |
1071 | auto Address = MemInst ? MemInst.getPointerOperand() : nullptr; |
1072 | if (Address && SE.getUnknown(UndefValue::get(Address->getType())) == |
1073 | SE.getPointerBase(SE.getSCEV(Address))) { |
1074 | } else if (S.getStmtFor(Inst)) { |
1075 | IsDead = false; |
1076 | } else { |
1077 | auto *Domain = S.getDomainConditions(Inst->getParent()).release(); |
1078 | IsDead = isl_set_is_empty(Domain); |
1079 | isl_set_free(Domain); |
1080 | } |
1081 | |
1082 | if (IsDead) { |
1083 | V = UndefValue::get(ParamSCEV->getType()); |
1084 | break; |
1085 | } |
1086 | } |
1087 | } |
1088 | |
1089 | if (auto *IAClass = S.lookupInvariantEquivClass(Val)) { |
1090 | // Check if this invariant access class is empty, hence if we never |
1091 | // actually added a loads instruction to it. In that case it has no |
1092 | // (meaningful) users and we should not try to code generate it. |
1093 | if (IAClass->InvariantAccesses.empty()) |
1094 | V = UndefValue::get(ParamSCEV->getType()); |
1095 | |
1096 | if (!preloadInvariantEquivClass(*IAClass)) { |
1097 | isl_id_free(Id); |
1098 | return false; |
1099 | } |
1100 | } |
1101 | } |
1102 | |
1103 | V = V ? V : generateSCEV(ParamSCEV); |
1104 | IDToValue[Id] = V; |
1105 | } |
1106 | |
1107 | isl_id_free(Id); |
1108 | return true; |
1109 | } |
1110 | |
1111 | bool IslNodeBuilder::materializeParameters(isl_set *Set) { |
1112 | for (unsigned i = 0, e = isl_set_dim(Set, isl_dim_param); i < e; ++i) { |
1113 | if (!isl_set_involves_dims(Set, isl_dim_param, i, 1)) |
1114 | continue; |
1115 | isl_id *Id = isl_set_get_dim_id(Set, isl_dim_param, i); |
1116 | if (!materializeValue(Id)) |
1117 | return false; |
1118 | } |
1119 | return true; |
1120 | } |
1121 | |
1122 | bool IslNodeBuilder::materializeParameters() { |
1123 | for (const SCEV *Param : S.parameters()) { |
1124 | isl_id *Id = S.getIdForParam(Param).release(); |
1125 | if (!materializeValue(Id)) |
1126 | return false; |
1127 | } |
1128 | return true; |
1129 | } |
1130 | |
1131 | /// Generate the computation of the size of the outermost dimension from the |
1132 | /// Fortran array descriptor (in this case, `@g_arr`). The final `%size` |
1133 | /// contains the size of the array. |
1134 | /// |
1135 | /// %arrty = type { i8*, i64, i64, [3 x %desc.dimensionty] } |
1136 | /// %desc.dimensionty = type { i64, i64, i64 } |
1137 | /// @g_arr = global %arrty zeroinitializer, align 32 |
1138 | /// ... |
1139 | /// %0 = load i64, i64* getelementptr inbounds |
1140 | /// (%arrty, %arrty* @g_arr, i64 0, i32 3, i64 0, i32 2) |
1141 | /// %1 = load i64, i64* getelementptr inbounds |
1142 | /// (%arrty, %arrty* @g_arr, i64 0, i32 3, i64 0, i32 1) |
1143 | /// %2 = sub nsw i64 %0, %1 |
1144 | /// %size = add nsw i64 %2, 1 |
1145 | static Value *buildFADOutermostDimensionLoad(Value *GlobalDescriptor, |
1146 | PollyIRBuilder &Builder, |
1147 | std::string ArrayName) { |
1148 | assert(GlobalDescriptor && "invalid global descriptor given")(static_cast <bool> (GlobalDescriptor && "invalid global descriptor given" ) ? void (0) : __assert_fail ("GlobalDescriptor && \"invalid global descriptor given\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 1148, __extension__ __PRETTY_FUNCTION__)); |
1149 | |
1150 | Value *endIdx[4] = {Builder.getInt64(0), Builder.getInt32(3), |
1151 | Builder.getInt64(0), Builder.getInt32(2)}; |
1152 | Value *endPtr = Builder.CreateInBoundsGEP(GlobalDescriptor, endIdx, |
1153 | ArrayName + "_end_ptr"); |
1154 | Type *type = cast<GEPOperator>(endPtr)->getResultElementType(); |
1155 | assert(isa<IntegerType>(type) && "expected type of end to be integral")(static_cast <bool> (isa<IntegerType>(type) && "expected type of end to be integral") ? void (0) : __assert_fail ("isa<IntegerType>(type) && \"expected type of end to be integral\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 1155, __extension__ __PRETTY_FUNCTION__)); |
1156 | |
1157 | Value *end = Builder.CreateLoad(type, endPtr, ArrayName + "_end"); |
1158 | |
1159 | Value *beginIdx[4] = {Builder.getInt64(0), Builder.getInt32(3), |
1160 | Builder.getInt64(0), Builder.getInt32(1)}; |
1161 | Value *beginPtr = Builder.CreateInBoundsGEP(GlobalDescriptor, beginIdx, |
1162 | ArrayName + "_begin_ptr"); |
1163 | Value *begin = Builder.CreateLoad(type, beginPtr, ArrayName + "_begin"); |
1164 | |
1165 | Value *size = |
Value stored to 'size' during its initialization is never read | |
1166 | Builder.CreateNSWSub(end, begin, ArrayName + "_end_begin_delta"); |
1167 | |
1168 | size = Builder.CreateNSWAdd( |
1169 | end, ConstantInt::get(type, 1, /* signed = */ true), ArrayName + "_size"); |
1170 | |
1171 | return size; |
1172 | } |
1173 | |
1174 | bool IslNodeBuilder::materializeFortranArrayOutermostDimension() { |
1175 | for (ScopArrayInfo *Array : S.arrays()) { |
1176 | if (Array->getNumberOfDimensions() == 0) |
1177 | continue; |
1178 | |
1179 | Value *FAD = Array->getFortranArrayDescriptor(); |
1180 | if (!FAD) |
1181 | continue; |
1182 | |
1183 | isl_pw_aff *ParametricPwAff = Array->getDimensionSizePw(0).release(); |
1184 | assert(ParametricPwAff && "parametric pw_aff corresponding "(static_cast <bool> (ParametricPwAff && "parametric pw_aff corresponding " "to outermost dimension does not " "exist") ? void (0) : __assert_fail ("ParametricPwAff && \"parametric pw_aff corresponding \" \"to outermost dimension does not \" \"exist\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 1186, __extension__ __PRETTY_FUNCTION__)) |
1185 | "to outermost dimension does not "(static_cast <bool> (ParametricPwAff && "parametric pw_aff corresponding " "to outermost dimension does not " "exist") ? void (0) : __assert_fail ("ParametricPwAff && \"parametric pw_aff corresponding \" \"to outermost dimension does not \" \"exist\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 1186, __extension__ __PRETTY_FUNCTION__)) |
1186 | "exist")(static_cast <bool> (ParametricPwAff && "parametric pw_aff corresponding " "to outermost dimension does not " "exist") ? void (0) : __assert_fail ("ParametricPwAff && \"parametric pw_aff corresponding \" \"to outermost dimension does not \" \"exist\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 1186, __extension__ __PRETTY_FUNCTION__)); |
1187 | |
1188 | isl_id *Id = isl_pw_aff_get_dim_id(ParametricPwAff, isl_dim_param, 0); |
1189 | isl_pw_aff_free(ParametricPwAff); |
1190 | |
1191 | assert(Id && "pw_aff is not parametric")(static_cast <bool> (Id && "pw_aff is not parametric" ) ? void (0) : __assert_fail ("Id && \"pw_aff is not parametric\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 1191, __extension__ __PRETTY_FUNCTION__)); |
1192 | |
1193 | if (IDToValue.count(Id)) { |
1194 | isl_id_free(Id); |
1195 | continue; |
1196 | } |
1197 | |
1198 | Value *FinalValue = |
1199 | buildFADOutermostDimensionLoad(FAD, Builder, Array->getName()); |
1200 | assert(FinalValue && "unable to build Fortran array "(static_cast <bool> (FinalValue && "unable to build Fortran array " "descriptor load of outermost dimension") ? void (0) : __assert_fail ("FinalValue && \"unable to build Fortran array \" \"descriptor load of outermost dimension\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 1201, __extension__ __PRETTY_FUNCTION__)) |
1201 | "descriptor load of outermost dimension")(static_cast <bool> (FinalValue && "unable to build Fortran array " "descriptor load of outermost dimension") ? void (0) : __assert_fail ("FinalValue && \"unable to build Fortran array \" \"descriptor load of outermost dimension\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 1201, __extension__ __PRETTY_FUNCTION__)); |
1202 | IDToValue[Id] = FinalValue; |
1203 | isl_id_free(Id); |
1204 | } |
1205 | return true; |
1206 | } |
1207 | |
1208 | Value *IslNodeBuilder::preloadUnconditionally(isl_set *AccessRange, |
1209 | isl_ast_build *Build, |
1210 | Instruction *AccInst) { |
1211 | isl_pw_multi_aff *PWAccRel = isl_pw_multi_aff_from_set(AccessRange); |
1212 | isl_ast_expr *Access = |
1213 | isl_ast_build_access_from_pw_multi_aff(Build, PWAccRel); |
1214 | auto *Address = isl_ast_expr_address_of(Access); |
1215 | auto *AddressValue = ExprBuilder.create(Address); |
1216 | Value *PreloadVal; |
1217 | |
1218 | // Correct the type as the SAI might have a different type than the user |
1219 | // expects, especially if the base pointer is a struct. |
1220 | Type *Ty = AccInst->getType(); |
1221 | |
1222 | auto *Ptr = AddressValue; |
1223 | auto Name = Ptr->getName(); |
1224 | auto AS = Ptr->getType()->getPointerAddressSpace(); |
1225 | Ptr = Builder.CreatePointerCast(Ptr, Ty->getPointerTo(AS), Name + ".cast"); |
1226 | PreloadVal = Builder.CreateLoad(Ty, Ptr, Name + ".load"); |
1227 | if (LoadInst *PreloadInst = dyn_cast<LoadInst>(PreloadVal)) |
1228 | PreloadInst->setAlignment(cast<LoadInst>(AccInst)->getAlign()); |
1229 | |
1230 | // TODO: This is only a hot fix for SCoP sequences that use the same load |
1231 | // instruction contained and hoisted by one of the SCoPs. |
1232 | if (SE.isSCEVable(Ty)) |
1233 | SE.forgetValue(AccInst); |
1234 | |
1235 | return PreloadVal; |
1236 | } |
1237 | |
1238 | Value *IslNodeBuilder::preloadInvariantLoad(const MemoryAccess &MA, |
1239 | isl_set *Domain) { |
1240 | isl_set *AccessRange = isl_map_range(MA.getAddressFunction().release()); |
1241 | AccessRange = isl_set_gist_params(AccessRange, S.getContext().release()); |
1242 | |
1243 | if (!materializeParameters(AccessRange)) { |
1244 | isl_set_free(AccessRange); |
1245 | isl_set_free(Domain); |
1246 | return nullptr; |
1247 | } |
1248 | |
1249 | auto *Build = |
1250 | isl_ast_build_from_context(isl_set_universe(S.getParamSpace().release())); |
1251 | isl_set *Universe = isl_set_universe(isl_set_get_space(Domain)); |
1252 | bool AlwaysExecuted = isl_set_is_equal(Domain, Universe); |
1253 | isl_set_free(Universe); |
1254 | |
1255 | Instruction *AccInst = MA.getAccessInstruction(); |
1256 | Type *AccInstTy = AccInst->getType(); |
1257 | |
1258 | Value *PreloadVal = nullptr; |
1259 | if (AlwaysExecuted) { |
1260 | PreloadVal = preloadUnconditionally(AccessRange, Build, AccInst); |
1261 | isl_ast_build_free(Build); |
1262 | isl_set_free(Domain); |
1263 | return PreloadVal; |
1264 | } |
1265 | |
1266 | if (!materializeParameters(Domain)) { |
1267 | isl_ast_build_free(Build); |
1268 | isl_set_free(AccessRange); |
1269 | isl_set_free(Domain); |
1270 | return nullptr; |
1271 | } |
1272 | |
1273 | isl_ast_expr *DomainCond = isl_ast_build_expr_from_set(Build, Domain); |
1274 | Domain = nullptr; |
1275 | |
1276 | ExprBuilder.setTrackOverflow(true); |
1277 | Value *Cond = ExprBuilder.create(DomainCond); |
1278 | Value *OverflowHappened = Builder.CreateNot(ExprBuilder.getOverflowState(), |
1279 | "polly.preload.cond.overflown"); |
1280 | Cond = Builder.CreateAnd(Cond, OverflowHappened, "polly.preload.cond.result"); |
1281 | ExprBuilder.setTrackOverflow(false); |
1282 | |
1283 | if (!Cond->getType()->isIntegerTy(1)) |
1284 | Cond = Builder.CreateIsNotNull(Cond); |
1285 | |
1286 | BasicBlock *CondBB = SplitBlock(Builder.GetInsertBlock(), |
1287 | &*Builder.GetInsertPoint(), &DT, &LI); |
1288 | CondBB->setName("polly.preload.cond"); |
1289 | |
1290 | BasicBlock *MergeBB = SplitBlock(CondBB, &CondBB->front(), &DT, &LI); |
1291 | MergeBB->setName("polly.preload.merge"); |
1292 | |
1293 | Function *F = Builder.GetInsertBlock()->getParent(); |
1294 | LLVMContext &Context = F->getContext(); |
1295 | BasicBlock *ExecBB = BasicBlock::Create(Context, "polly.preload.exec", F); |
1296 | |
1297 | DT.addNewBlock(ExecBB, CondBB); |
1298 | if (Loop *L = LI.getLoopFor(CondBB)) |
1299 | L->addBasicBlockToLoop(ExecBB, LI); |
1300 | |
1301 | auto *CondBBTerminator = CondBB->getTerminator(); |
1302 | Builder.SetInsertPoint(CondBBTerminator); |
1303 | Builder.CreateCondBr(Cond, ExecBB, MergeBB); |
1304 | CondBBTerminator->eraseFromParent(); |
1305 | |
1306 | Builder.SetInsertPoint(ExecBB); |
1307 | Builder.CreateBr(MergeBB); |
1308 | |
1309 | Builder.SetInsertPoint(ExecBB->getTerminator()); |
1310 | Value *PreAccInst = preloadUnconditionally(AccessRange, Build, AccInst); |
1311 | Builder.SetInsertPoint(MergeBB->getTerminator()); |
1312 | auto *MergePHI = Builder.CreatePHI( |
1313 | AccInstTy, 2, "polly.preload." + AccInst->getName() + ".merge"); |
1314 | PreloadVal = MergePHI; |
1315 | |
1316 | if (!PreAccInst) { |
1317 | PreloadVal = nullptr; |
1318 | PreAccInst = UndefValue::get(AccInstTy); |
1319 | } |
1320 | |
1321 | MergePHI->addIncoming(PreAccInst, ExecBB); |
1322 | MergePHI->addIncoming(Constant::getNullValue(AccInstTy), CondBB); |
1323 | |
1324 | isl_ast_build_free(Build); |
1325 | return PreloadVal; |
1326 | } |
1327 | |
1328 | bool IslNodeBuilder::preloadInvariantEquivClass( |
1329 | InvariantEquivClassTy &IAClass) { |
1330 | // For an equivalence class of invariant loads we pre-load the representing |
1331 | // element with the unified execution context. However, we have to map all |
1332 | // elements of the class to the one preloaded load as they are referenced |
1333 | // during the code generation and therefor need to be mapped. |
1334 | const MemoryAccessList &MAs = IAClass.InvariantAccesses; |
1335 | if (MAs.empty()) |
1336 | return true; |
1337 | |
1338 | MemoryAccess *MA = MAs.front(); |
1339 | assert(MA->isArrayKind() && MA->isRead())(static_cast <bool> (MA->isArrayKind() && MA ->isRead()) ? void (0) : __assert_fail ("MA->isArrayKind() && MA->isRead()" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 1339, __extension__ __PRETTY_FUNCTION__)); |
1340 | |
1341 | // If the access function was already mapped, the preload of this equivalence |
1342 | // class was triggered earlier already and doesn't need to be done again. |
1343 | if (ValueMap.count(MA->getAccessInstruction())) |
1344 | return true; |
1345 | |
1346 | // Check for recursion which can be caused by additional constraints, e.g., |
1347 | // non-finite loop constraints. In such a case we have to bail out and insert |
1348 | // a "false" runtime check that will cause the original code to be executed. |
1349 | auto PtrId = std::make_pair(IAClass.IdentifyingPointer, IAClass.AccessType); |
1350 | if (!PreloadedPtrs.insert(PtrId).second) |
1351 | return false; |
1352 | |
1353 | // The execution context of the IAClass. |
1354 | isl::set &ExecutionCtx = IAClass.ExecutionContext; |
1355 | |
1356 | // If the base pointer of this class is dependent on another one we have to |
1357 | // make sure it was preloaded already. |
1358 | auto *SAI = MA->getScopArrayInfo(); |
1359 | if (auto *BaseIAClass = S.lookupInvariantEquivClass(SAI->getBasePtr())) { |
1360 | if (!preloadInvariantEquivClass(*BaseIAClass)) |
1361 | return false; |
1362 | |
1363 | // After we preloaded the BaseIAClass we adjusted the BaseExecutionCtx and |
1364 | // we need to refine the ExecutionCtx. |
1365 | isl::set BaseExecutionCtx = BaseIAClass->ExecutionContext; |
1366 | ExecutionCtx = ExecutionCtx.intersect(BaseExecutionCtx); |
1367 | } |
1368 | |
1369 | // If the size of a dimension is dependent on another class, make sure it is |
1370 | // preloaded. |
1371 | for (unsigned i = 1, e = SAI->getNumberOfDimensions(); i < e; ++i) { |
1372 | const SCEV *Dim = SAI->getDimensionSize(i); |
1373 | SetVector<Value *> Values; |
1374 | findValues(Dim, SE, Values); |
1375 | for (auto *Val : Values) { |
1376 | if (auto *BaseIAClass = S.lookupInvariantEquivClass(Val)) { |
1377 | if (!preloadInvariantEquivClass(*BaseIAClass)) |
1378 | return false; |
1379 | |
1380 | // After we preloaded the BaseIAClass we adjusted the BaseExecutionCtx |
1381 | // and we need to refine the ExecutionCtx. |
1382 | isl::set BaseExecutionCtx = BaseIAClass->ExecutionContext; |
1383 | ExecutionCtx = ExecutionCtx.intersect(BaseExecutionCtx); |
1384 | } |
1385 | } |
1386 | } |
1387 | |
1388 | Instruction *AccInst = MA->getAccessInstruction(); |
1389 | Type *AccInstTy = AccInst->getType(); |
1390 | |
1391 | Value *PreloadVal = preloadInvariantLoad(*MA, ExecutionCtx.copy()); |
1392 | if (!PreloadVal) |
1393 | return false; |
1394 | |
1395 | for (const MemoryAccess *MA : MAs) { |
1396 | Instruction *MAAccInst = MA->getAccessInstruction(); |
1397 | assert(PreloadVal->getType() == MAAccInst->getType())(static_cast <bool> (PreloadVal->getType() == MAAccInst ->getType()) ? void (0) : __assert_fail ("PreloadVal->getType() == MAAccInst->getType()" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 1397, __extension__ __PRETTY_FUNCTION__)); |
1398 | ValueMap[MAAccInst] = PreloadVal; |
1399 | } |
1400 | |
1401 | if (SE.isSCEVable(AccInstTy)) { |
1402 | isl_id *ParamId = S.getIdForParam(SE.getSCEV(AccInst)).release(); |
1403 | if (ParamId) |
1404 | IDToValue[ParamId] = PreloadVal; |
1405 | isl_id_free(ParamId); |
1406 | } |
1407 | |
1408 | BasicBlock *EntryBB = &Builder.GetInsertBlock()->getParent()->getEntryBlock(); |
1409 | auto *Alloca = new AllocaInst(AccInstTy, DL.getAllocaAddrSpace(), |
1410 | AccInst->getName() + ".preload.s2a", |
1411 | &*EntryBB->getFirstInsertionPt()); |
1412 | Builder.CreateStore(PreloadVal, Alloca); |
1413 | ValueMapT PreloadedPointer; |
1414 | PreloadedPointer[PreloadVal] = AccInst; |
1415 | Annotator.addAlternativeAliasBases(PreloadedPointer); |
1416 | |
1417 | for (auto *DerivedSAI : SAI->getDerivedSAIs()) { |
1418 | Value *BasePtr = DerivedSAI->getBasePtr(); |
1419 | |
1420 | for (const MemoryAccess *MA : MAs) { |
1421 | // As the derived SAI information is quite coarse, any load from the |
1422 | // current SAI could be the base pointer of the derived SAI, however we |
1423 | // should only change the base pointer of the derived SAI if we actually |
1424 | // preloaded it. |
1425 | if (BasePtr == MA->getOriginalBaseAddr()) { |
1426 | assert(BasePtr->getType() == PreloadVal->getType())(static_cast <bool> (BasePtr->getType() == PreloadVal ->getType()) ? void (0) : __assert_fail ("BasePtr->getType() == PreloadVal->getType()" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 1426, __extension__ __PRETTY_FUNCTION__)); |
1427 | DerivedSAI->setBasePtr(PreloadVal); |
1428 | } |
1429 | |
1430 | // For scalar derived SAIs we remap the alloca used for the derived value. |
1431 | if (BasePtr == MA->getAccessInstruction()) |
1432 | ScalarMap[DerivedSAI] = Alloca; |
1433 | } |
1434 | } |
1435 | |
1436 | for (const MemoryAccess *MA : MAs) { |
1437 | Instruction *MAAccInst = MA->getAccessInstruction(); |
1438 | // Use the escape system to get the correct value to users outside the SCoP. |
1439 | BlockGenerator::EscapeUserVectorTy EscapeUsers; |
1440 | for (auto *U : MAAccInst->users()) |
1441 | if (Instruction *UI = dyn_cast<Instruction>(U)) |
1442 | if (!S.contains(UI)) |
1443 | EscapeUsers.push_back(UI); |
1444 | |
1445 | if (EscapeUsers.empty()) |
1446 | continue; |
1447 | |
1448 | EscapeMap[MA->getAccessInstruction()] = |
1449 | std::make_pair(Alloca, std::move(EscapeUsers)); |
1450 | } |
1451 | |
1452 | return true; |
1453 | } |
1454 | |
1455 | void IslNodeBuilder::allocateNewArrays(BBPair StartExitBlocks) { |
1456 | for (auto &SAI : S.arrays()) { |
1457 | if (SAI->getBasePtr()) |
1458 | continue; |
1459 | |
1460 | assert(SAI->getNumberOfDimensions() > 0 && SAI->getDimensionSize(0) &&(static_cast <bool> (SAI->getNumberOfDimensions() > 0 && SAI->getDimensionSize(0) && "The size of the outermost dimension is used to declare newly " "created arrays that require memory allocation.") ? void (0) : __assert_fail ("SAI->getNumberOfDimensions() > 0 && SAI->getDimensionSize(0) && \"The size of the outermost dimension is used to declare newly \" \"created arrays that require memory allocation.\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 1462, __extension__ __PRETTY_FUNCTION__)) |
1461 | "The size of the outermost dimension is used to declare newly "(static_cast <bool> (SAI->getNumberOfDimensions() > 0 && SAI->getDimensionSize(0) && "The size of the outermost dimension is used to declare newly " "created arrays that require memory allocation.") ? void (0) : __assert_fail ("SAI->getNumberOfDimensions() > 0 && SAI->getDimensionSize(0) && \"The size of the outermost dimension is used to declare newly \" \"created arrays that require memory allocation.\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 1462, __extension__ __PRETTY_FUNCTION__)) |
1462 | "created arrays that require memory allocation.")(static_cast <bool> (SAI->getNumberOfDimensions() > 0 && SAI->getDimensionSize(0) && "The size of the outermost dimension is used to declare newly " "created arrays that require memory allocation.") ? void (0) : __assert_fail ("SAI->getNumberOfDimensions() > 0 && SAI->getDimensionSize(0) && \"The size of the outermost dimension is used to declare newly \" \"created arrays that require memory allocation.\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 1462, __extension__ __PRETTY_FUNCTION__)); |
1463 | |
1464 | Type *NewArrayType = nullptr; |
1465 | |
1466 | // Get the size of the array = size(dim_1)*...*size(dim_n) |
1467 | uint64_t ArraySizeInt = 1; |
1468 | for (int i = SAI->getNumberOfDimensions() - 1; i >= 0; i--) { |
1469 | auto *DimSize = SAI->getDimensionSize(i); |
1470 | unsigned UnsignedDimSize = static_cast<const SCEVConstant *>(DimSize) |
1471 | ->getAPInt() |
1472 | .getLimitedValue(); |
1473 | |
1474 | if (!NewArrayType) |
1475 | NewArrayType = SAI->getElementType(); |
1476 | |
1477 | NewArrayType = ArrayType::get(NewArrayType, UnsignedDimSize); |
1478 | ArraySizeInt *= UnsignedDimSize; |
1479 | } |
1480 | |
1481 | if (SAI->isOnHeap()) { |
1482 | LLVMContext &Ctx = NewArrayType->getContext(); |
1483 | |
1484 | // Get the IntPtrTy from the Datalayout |
1485 | auto IntPtrTy = DL.getIntPtrType(Ctx); |
1486 | |
1487 | // Get the size of the element type in bits |
1488 | unsigned Size = SAI->getElemSizeInBytes(); |
1489 | |
1490 | // Insert the malloc call at polly.start |
1491 | auto InstIt = std::get<0>(StartExitBlocks)->getTerminator(); |
1492 | auto *CreatedArray = CallInst::CreateMalloc( |
1493 | &*InstIt, IntPtrTy, SAI->getElementType(), |
1494 | ConstantInt::get(Type::getInt64Ty(Ctx), Size), |
1495 | ConstantInt::get(Type::getInt64Ty(Ctx), ArraySizeInt), nullptr, |
1496 | SAI->getName()); |
1497 | |
1498 | SAI->setBasePtr(CreatedArray); |
1499 | |
1500 | // Insert the free call at polly.exiting |
1501 | CallInst::CreateFree(CreatedArray, |
1502 | std::get<1>(StartExitBlocks)->getTerminator()); |
1503 | } else { |
1504 | auto InstIt = Builder.GetInsertBlock() |
1505 | ->getParent() |
1506 | ->getEntryBlock() |
1507 | .getTerminator(); |
1508 | |
1509 | auto *CreatedArray = new AllocaInst(NewArrayType, DL.getAllocaAddrSpace(), |
1510 | SAI->getName(), &*InstIt); |
1511 | if (PollyTargetFirstLevelCacheLineSize) |
1512 | CreatedArray->setAlignment(Align(PollyTargetFirstLevelCacheLineSize)); |
1513 | SAI->setBasePtr(CreatedArray); |
1514 | } |
1515 | } |
1516 | } |
1517 | |
1518 | bool IslNodeBuilder::preloadInvariantLoads() { |
1519 | auto &InvariantEquivClasses = S.getInvariantAccesses(); |
1520 | if (InvariantEquivClasses.empty()) |
1521 | return true; |
1522 | |
1523 | BasicBlock *PreLoadBB = SplitBlock(Builder.GetInsertBlock(), |
1524 | &*Builder.GetInsertPoint(), &DT, &LI); |
1525 | PreLoadBB->setName("polly.preload.begin"); |
1526 | Builder.SetInsertPoint(&PreLoadBB->front()); |
1527 | |
1528 | for (auto &IAClass : InvariantEquivClasses) |
1529 | if (!preloadInvariantEquivClass(IAClass)) |
1530 | return false; |
1531 | |
1532 | return true; |
1533 | } |
1534 | |
1535 | void IslNodeBuilder::addParameters(__isl_take isl_set *Context) { |
1536 | // Materialize values for the parameters of the SCoP. |
1537 | materializeParameters(); |
1538 | |
1539 | // materialize the outermost dimension parameters for a Fortran array. |
1540 | // NOTE: materializeParameters() does not work since it looks through |
1541 | // the SCEVs. We don't have a corresponding SCEV for the array size |
1542 | // parameter |
1543 | materializeFortranArrayOutermostDimension(); |
1544 | |
1545 | // Generate values for the current loop iteration for all surrounding loops. |
1546 | // |
1547 | // We may also reference loops outside of the scop which do not contain the |
1548 | // scop itself, but as the number of such scops may be arbitrarily large we do |
1549 | // not generate code for them here, but only at the point of code generation |
1550 | // where these values are needed. |
1551 | Loop *L = LI.getLoopFor(S.getEntry()); |
1552 | |
1553 | while (L != nullptr && S.contains(L)) |
1554 | L = L->getParentLoop(); |
1555 | |
1556 | while (L != nullptr) { |
1557 | materializeNonScopLoopInductionVariable(L); |
1558 | L = L->getParentLoop(); |
1559 | } |
1560 | |
1561 | isl_set_free(Context); |
1562 | } |
1563 | |
1564 | Value *IslNodeBuilder::generateSCEV(const SCEV *Expr) { |
1565 | /// We pass the insert location of our Builder, as Polly ensures during IR |
1566 | /// generation that there is always a valid CFG into which instructions are |
1567 | /// inserted. As a result, the insertpoint is known to be always followed by a |
1568 | /// terminator instruction. This means the insert point may be specified by a |
1569 | /// terminator instruction, but it can never point to an ->end() iterator |
1570 | /// which does not have a corresponding instruction. Hence, dereferencing |
1571 | /// the insertpoint to obtain an instruction is known to be save. |
1572 | /// |
1573 | /// We also do not need to update the Builder here, as new instructions are |
1574 | /// always inserted _before_ the given InsertLocation. As a result, the |
1575 | /// insert location remains valid. |
1576 | assert(Builder.GetInsertBlock()->end() != Builder.GetInsertPoint() &&(static_cast <bool> (Builder.GetInsertBlock()->end() != Builder.GetInsertPoint() && "Insert location points after last valid instruction" ) ? void (0) : __assert_fail ("Builder.GetInsertBlock()->end() != Builder.GetInsertPoint() && \"Insert location points after last valid instruction\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 1577, __extension__ __PRETTY_FUNCTION__)) |
1577 | "Insert location points after last valid instruction")(static_cast <bool> (Builder.GetInsertBlock()->end() != Builder.GetInsertPoint() && "Insert location points after last valid instruction" ) ? void (0) : __assert_fail ("Builder.GetInsertBlock()->end() != Builder.GetInsertPoint() && \"Insert location points after last valid instruction\"" , "/build/llvm-toolchain-snapshot-13~++20210717111130+173332d17561/polly/lib/CodeGen/IslNodeBuilder.cpp" , 1577, __extension__ __PRETTY_FUNCTION__)); |
1578 | Instruction *InsertLocation = &*Builder.GetInsertPoint(); |
1579 | return expandCodeFor(S, SE, DL, "polly", Expr, Expr->getType(), |
1580 | InsertLocation, &ValueMap, |
1581 | StartBlock->getSinglePredecessor()); |
1582 | } |
1583 | |
1584 | /// The AST expression we generate to perform the run-time check assumes |
1585 | /// computations on integer types of infinite size. As we only use 64-bit |
1586 | /// arithmetic we check for overflows, in case of which we set the result |
1587 | /// of this run-time check to false to be conservatively correct, |
1588 | Value *IslNodeBuilder::createRTC(isl_ast_expr *Condition) { |
1589 | auto ExprBuilder = getExprBuilder(); |
1590 | |
1591 | // In case the AST expression has integers larger than 64 bit, bail out. The |
1592 | // resulting LLVM-IR will contain operations on types that use more than 64 |
1593 | // bits. These are -- in case wrapping intrinsics are used -- translated to |
1594 | // runtime library calls that are not available on all systems (e.g., Android) |
1595 | // and consequently will result in linker errors. |
1596 | if (ExprBuilder.hasLargeInts(isl::manage_copy(Condition))) { |
1597 | isl_ast_expr_free(Condition); |
1598 | return Builder.getFalse(); |
1599 | } |
1600 | |
1601 | ExprBuilder.setTrackOverflow(true); |
1602 | Value *RTC = ExprBuilder.create(Condition); |
1603 | if (!RTC->getType()->isIntegerTy(1)) |
1604 | RTC = Builder.CreateIsNotNull(RTC); |
1605 | Value *OverflowHappened = |
1606 | Builder.CreateNot(ExprBuilder.getOverflowState(), "polly.rtc.overflown"); |
1607 | |
1608 | if (PollyGenerateRTCPrint) { |
1609 | auto *F = Builder.GetInsertBlock()->getParent(); |
1610 | RuntimeDebugBuilder::createCPUPrinter( |
1611 | Builder, |
1612 | "F: " + F->getName().str() + " R: " + S.getRegion().getNameStr() + |
1613 | "RTC: ", |
1614 | RTC, " Overflow: ", OverflowHappened, |
1615 | "\n" |
1616 | " (0 failed, -1 succeeded)\n" |
1617 | " (if one or both are 0 falling back to original code, if both are -1 " |
1618 | "executing Polly code)\n"); |
1619 | } |
1620 | |
1621 | RTC = Builder.CreateAnd(RTC, OverflowHappened, "polly.rtc.result"); |
1622 | ExprBuilder.setTrackOverflow(false); |
1623 | |
1624 | if (!isa<ConstantInt>(RTC)) |
1625 | VersionedScops++; |
1626 | |
1627 | return RTC; |
1628 | } |