[SandboxIR][Doc] Add Quick start notes (#123992)
[llvm-project.git] / polly / lib / Analysis / DependenceInfo.cpp
bloba530fa7b58fa6ea6b34d454d231783261f3d3f92
1 //===- DependenceInfo.cpp - Calculate dependency information for a Scop. --===//
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 // Calculate the data dependency relations for a Scop using ISL.
11 // The integer set library (ISL) from Sven, has a integrated dependency analysis
12 // to calculate data dependences. This pass takes advantage of this and
13 // calculate those dependences a Scop.
15 // The dependences in this pass are exact in terms that for a specific read
16 // statement instance only the last write statement instance is returned. In
17 // case of may writes a set of possible write instances is returned. This
18 // analysis will never produce redundant dependences.
20 //===----------------------------------------------------------------------===//
22 #include "polly/DependenceInfo.h"
23 #include "polly/LinkAllPasses.h"
24 #include "polly/Options.h"
25 #include "polly/ScopInfo.h"
26 #include "polly/Support/GICHelper.h"
27 #include "polly/Support/ISLTools.h"
28 #include "llvm/ADT/Sequence.h"
29 #include "llvm/Support/Debug.h"
30 #include "isl/aff.h"
31 #include "isl/ctx.h"
32 #include "isl/flow.h"
33 #include "isl/map.h"
34 #include "isl/schedule.h"
35 #include "isl/set.h"
36 #include "isl/union_map.h"
37 #include "isl/union_set.h"
39 using namespace polly;
40 using namespace llvm;
42 #include "polly/Support/PollyDebug.h"
43 #define DEBUG_TYPE "polly-dependence"
45 static cl::opt<int> OptComputeOut(
46 "polly-dependences-computeout",
47 cl::desc("Bound the dependence analysis by a maximal amount of "
48 "computational steps (0 means no bound)"),
49 cl::Hidden, cl::init(500000), cl::cat(PollyCategory));
51 static cl::opt<bool>
52 LegalityCheckDisabled("disable-polly-legality",
53 cl::desc("Disable polly legality check"), cl::Hidden,
54 cl::cat(PollyCategory));
56 static cl::opt<bool>
57 UseReductions("polly-dependences-use-reductions",
58 cl::desc("Exploit reductions in dependence analysis"),
59 cl::Hidden, cl::init(true), cl::cat(PollyCategory));
61 enum AnalysisType { VALUE_BASED_ANALYSIS, MEMORY_BASED_ANALYSIS };
63 static cl::opt<enum AnalysisType> OptAnalysisType(
64 "polly-dependences-analysis-type",
65 cl::desc("The kind of dependence analysis to use"),
66 cl::values(clEnumValN(VALUE_BASED_ANALYSIS, "value-based",
67 "Exact dependences without transitive dependences"),
68 clEnumValN(MEMORY_BASED_ANALYSIS, "memory-based",
69 "Overapproximation of dependences")),
70 cl::Hidden, cl::init(VALUE_BASED_ANALYSIS), cl::cat(PollyCategory));
72 static cl::opt<Dependences::AnalysisLevel> OptAnalysisLevel(
73 "polly-dependences-analysis-level",
74 cl::desc("The level of dependence analysis"),
75 cl::values(clEnumValN(Dependences::AL_Statement, "statement-wise",
76 "Statement-level analysis"),
77 clEnumValN(Dependences::AL_Reference, "reference-wise",
78 "Memory reference level analysis that distinguish"
79 " accessed references in the same statement"),
80 clEnumValN(Dependences::AL_Access, "access-wise",
81 "Memory reference level analysis that distinguish"
82 " access instructions in the same statement")),
83 cl::Hidden, cl::init(Dependences::AL_Statement), cl::cat(PollyCategory));
85 //===----------------------------------------------------------------------===//
87 /// Tag the @p Relation domain with @p TagId
88 static __isl_give isl_map *tag(__isl_take isl_map *Relation,
89 __isl_take isl_id *TagId) {
90 isl_space *Space = isl_map_get_space(Relation);
91 Space = isl_space_drop_dims(Space, isl_dim_out, 0,
92 isl_map_dim(Relation, isl_dim_out));
93 Space = isl_space_set_tuple_id(Space, isl_dim_out, TagId);
94 isl_multi_aff *Tag = isl_multi_aff_domain_map(Space);
95 Relation = isl_map_preimage_domain_multi_aff(Relation, Tag);
96 return Relation;
99 /// Tag the @p Relation domain with either MA->getArrayId() or
100 /// MA->getId() based on @p TagLevel
101 static __isl_give isl_map *tag(__isl_take isl_map *Relation, MemoryAccess *MA,
102 Dependences::AnalysisLevel TagLevel) {
103 if (TagLevel == Dependences::AL_Reference)
104 return tag(Relation, MA->getArrayId().release());
106 if (TagLevel == Dependences::AL_Access)
107 return tag(Relation, MA->getId().release());
109 // No need to tag at the statement level.
110 return Relation;
113 /// Collect information about the SCoP @p S.
114 static void collectInfo(Scop &S, isl_union_map *&Read,
115 isl_union_map *&MustWrite, isl_union_map *&MayWrite,
116 isl_union_map *&ReductionTagMap,
117 isl_union_set *&TaggedStmtDomain,
118 Dependences::AnalysisLevel Level) {
119 isl_space *Space = S.getParamSpace().release();
120 Read = isl_union_map_empty(isl_space_copy(Space));
121 MustWrite = isl_union_map_empty(isl_space_copy(Space));
122 MayWrite = isl_union_map_empty(isl_space_copy(Space));
123 ReductionTagMap = isl_union_map_empty(isl_space_copy(Space));
124 isl_union_map *StmtSchedule = isl_union_map_empty(Space);
126 SmallPtrSet<const ScopArrayInfo *, 8> ReductionArrays;
127 if (UseReductions)
128 for (ScopStmt &Stmt : S)
129 for (MemoryAccess *MA : Stmt)
130 if (MA->isReductionLike())
131 ReductionArrays.insert(MA->getScopArrayInfo());
133 for (ScopStmt &Stmt : S) {
134 for (MemoryAccess *MA : Stmt) {
135 isl_set *domcp = Stmt.getDomain().release();
136 isl_map *accdom = MA->getAccessRelation().release();
138 accdom = isl_map_intersect_domain(accdom, domcp);
140 if (ReductionArrays.count(MA->getScopArrayInfo())) {
141 // Wrap the access domain and adjust the schedule accordingly.
143 // An access domain like
144 // Stmt[i0, i1] -> MemAcc_A[i0 + i1]
145 // will be transformed into
146 // [Stmt[i0, i1] -> MemAcc_A[i0 + i1]] -> MemAcc_A[i0 + i1]
148 // We collect all the access domains in the ReductionTagMap.
149 // This is used in Dependences::calculateDependences to create
150 // a tagged Schedule tree.
152 ReductionTagMap =
153 isl_union_map_add_map(ReductionTagMap, isl_map_copy(accdom));
154 accdom = isl_map_range_map(accdom);
155 } else {
156 accdom = tag(accdom, MA, Level);
157 if (Level > Dependences::AL_Statement) {
158 isl_map *StmtScheduleMap = Stmt.getSchedule().release();
159 assert(StmtScheduleMap &&
160 "Schedules that contain extension nodes require special "
161 "handling.");
162 isl_map *Schedule = tag(StmtScheduleMap, MA, Level);
163 StmtSchedule = isl_union_map_add_map(StmtSchedule, Schedule);
167 if (MA->isRead())
168 Read = isl_union_map_add_map(Read, accdom);
169 else if (MA->isMayWrite())
170 MayWrite = isl_union_map_add_map(MayWrite, accdom);
171 else
172 MustWrite = isl_union_map_add_map(MustWrite, accdom);
175 if (!ReductionArrays.empty() && Level == Dependences::AL_Statement)
176 StmtSchedule =
177 isl_union_map_add_map(StmtSchedule, Stmt.getSchedule().release());
180 StmtSchedule = isl_union_map_intersect_params(
181 StmtSchedule, S.getAssumedContext().release());
182 TaggedStmtDomain = isl_union_map_domain(StmtSchedule);
184 ReductionTagMap = isl_union_map_coalesce(ReductionTagMap);
185 Read = isl_union_map_coalesce(Read);
186 MustWrite = isl_union_map_coalesce(MustWrite);
187 MayWrite = isl_union_map_coalesce(MayWrite);
190 /// Fix all dimension of @p Zero to 0 and add it to @p user
191 static void fixSetToZero(isl::set Zero, isl::union_set *User) {
192 for (auto i : rangeIslSize(0, Zero.tuple_dim()))
193 Zero = Zero.fix_si(isl::dim::set, i, 0);
194 *User = User->unite(Zero);
197 /// Compute the privatization dependences for a given dependency @p Map
199 /// Privatization dependences are widened original dependences which originate
200 /// or end in a reduction access. To compute them we apply the transitive close
201 /// of the reduction dependences (which maps each iteration of a reduction
202 /// statement to all following ones) on the RAW/WAR/WAW dependences. The
203 /// dependences which start or end at a reduction statement will be extended to
204 /// depend on all following reduction statement iterations as well.
205 /// Note: "Following" here means according to the reduction dependences.
207 /// For the input:
209 /// S0: *sum = 0;
210 /// for (int i = 0; i < 1024; i++)
211 /// S1: *sum += i;
212 /// S2: *sum = *sum * 3;
214 /// we have the following dependences before we add privatization dependences:
216 /// RAW:
217 /// { S0[] -> S1[0]; S1[1023] -> S2[] }
218 /// WAR:
219 /// { }
220 /// WAW:
221 /// { S0[] -> S1[0]; S1[1024] -> S2[] }
222 /// RED:
223 /// { S1[i0] -> S1[1 + i0] : i0 >= 0 and i0 <= 1022 }
225 /// and afterwards:
227 /// RAW:
228 /// { S0[] -> S1[i0] : i0 >= 0 and i0 <= 1023;
229 /// S1[i0] -> S2[] : i0 >= 0 and i0 <= 1023}
230 /// WAR:
231 /// { }
232 /// WAW:
233 /// { S0[] -> S1[i0] : i0 >= 0 and i0 <= 1023;
234 /// S1[i0] -> S2[] : i0 >= 0 and i0 <= 1023}
235 /// RED:
236 /// { S1[i0] -> S1[1 + i0] : i0 >= 0 and i0 <= 1022 }
238 /// Note: This function also computes the (reverse) transitive closure of the
239 /// reduction dependences.
240 void Dependences::addPrivatizationDependences() {
241 isl_union_map *PrivRAW, *PrivWAW, *PrivWAR;
243 // The transitive closure might be over approximated, thus could lead to
244 // dependency cycles in the privatization dependences. To make sure this
245 // will not happen we remove all negative dependences after we computed
246 // the transitive closure.
247 TC_RED = isl_union_map_transitive_closure(isl_union_map_copy(RED), nullptr);
249 // FIXME: Apply the current schedule instead of assuming the identity schedule
250 // here. The current approach is only valid as long as we compute the
251 // dependences only with the initial (identity schedule). Any other
252 // schedule could change "the direction of the backward dependences" we
253 // want to eliminate here.
254 isl_union_set *UDeltas = isl_union_map_deltas(isl_union_map_copy(TC_RED));
255 isl_union_set *Universe = isl_union_set_universe(isl_union_set_copy(UDeltas));
256 isl::union_set Zero =
257 isl::manage(isl_union_set_empty(isl_union_set_get_space(Universe)));
259 for (isl::set Set : isl::manage_copy(Universe).get_set_list())
260 fixSetToZero(Set, &Zero);
262 isl_union_map *NonPositive =
263 isl_union_set_lex_le_union_set(UDeltas, Zero.release());
265 TC_RED = isl_union_map_subtract(TC_RED, NonPositive);
267 TC_RED = isl_union_map_union(
268 TC_RED, isl_union_map_reverse(isl_union_map_copy(TC_RED)));
269 TC_RED = isl_union_map_coalesce(TC_RED);
271 isl_union_map **Maps[] = {&RAW, &WAW, &WAR};
272 isl_union_map **PrivMaps[] = {&PrivRAW, &PrivWAW, &PrivWAR};
273 for (unsigned u = 0; u < 3; u++) {
274 isl_union_map **Map = Maps[u], **PrivMap = PrivMaps[u];
276 *PrivMap = isl_union_map_apply_range(isl_union_map_copy(*Map),
277 isl_union_map_copy(TC_RED));
278 *PrivMap = isl_union_map_union(
279 *PrivMap, isl_union_map_apply_range(isl_union_map_copy(TC_RED),
280 isl_union_map_copy(*Map)));
282 *Map = isl_union_map_union(*Map, *PrivMap);
285 isl_union_set_free(Universe);
288 static __isl_give isl_union_flow *buildFlow(__isl_keep isl_union_map *Snk,
289 __isl_keep isl_union_map *Src,
290 __isl_keep isl_union_map *MaySrc,
291 __isl_keep isl_union_map *Kill,
292 __isl_keep isl_schedule *Schedule) {
293 isl_union_access_info *AI;
295 AI = isl_union_access_info_from_sink(isl_union_map_copy(Snk));
296 if (MaySrc)
297 AI = isl_union_access_info_set_may_source(AI, isl_union_map_copy(MaySrc));
298 if (Src)
299 AI = isl_union_access_info_set_must_source(AI, isl_union_map_copy(Src));
300 if (Kill)
301 AI = isl_union_access_info_set_kill(AI, isl_union_map_copy(Kill));
302 AI = isl_union_access_info_set_schedule(AI, isl_schedule_copy(Schedule));
303 auto Flow = isl_union_access_info_compute_flow(AI);
304 POLLY_DEBUG(if (!Flow) dbgs()
305 << "last error: "
306 << isl_ctx_last_error(isl_schedule_get_ctx(Schedule))
307 << '\n';);
308 return Flow;
311 void Dependences::calculateDependences(Scop &S) {
312 isl_union_map *Read, *MustWrite, *MayWrite, *ReductionTagMap;
313 isl_schedule *Schedule;
314 isl_union_set *TaggedStmtDomain;
316 POLLY_DEBUG(dbgs() << "Scop: \n" << S << "\n");
318 collectInfo(S, Read, MustWrite, MayWrite, ReductionTagMap, TaggedStmtDomain,
319 Level);
321 bool HasReductions = !isl_union_map_is_empty(ReductionTagMap);
323 POLLY_DEBUG(dbgs() << "Read: " << Read << '\n';
324 dbgs() << "MustWrite: " << MustWrite << '\n';
325 dbgs() << "MayWrite: " << MayWrite << '\n';
326 dbgs() << "ReductionTagMap: " << ReductionTagMap << '\n';
327 dbgs() << "TaggedStmtDomain: " << TaggedStmtDomain << '\n';);
329 Schedule = S.getScheduleTree().release();
331 if (!HasReductions) {
332 isl_union_map_free(ReductionTagMap);
333 // Tag the schedule tree if we want fine-grain dependence info
334 if (Level > AL_Statement) {
335 auto TaggedMap =
336 isl_union_set_unwrap(isl_union_set_copy(TaggedStmtDomain));
337 auto Tags = isl_union_map_domain_map_union_pw_multi_aff(TaggedMap);
338 Schedule = isl_schedule_pullback_union_pw_multi_aff(Schedule, Tags);
340 } else {
341 isl_union_map *IdentityMap;
342 isl_union_pw_multi_aff *ReductionTags, *IdentityTags, *Tags;
344 // Extract Reduction tags from the combined access domains in the given
345 // SCoP. The result is a map that maps each tagged element in the domain to
346 // the memory location it accesses. ReductionTags = {[Stmt[i] ->
347 // Array[f(i)]] -> Stmt[i] }
348 ReductionTags =
349 isl_union_map_domain_map_union_pw_multi_aff(ReductionTagMap);
351 // Compute an identity map from each statement in domain to itself.
352 // IdentityTags = { [Stmt[i] -> Stmt[i] }
353 IdentityMap = isl_union_set_identity(isl_union_set_copy(TaggedStmtDomain));
354 IdentityTags = isl_union_pw_multi_aff_from_union_map(IdentityMap);
356 Tags = isl_union_pw_multi_aff_union_add(ReductionTags, IdentityTags);
358 // By pulling back Tags from Schedule, we have a schedule tree that can
359 // be used to compute normal dependences, as well as 'tagged' reduction
360 // dependences.
361 Schedule = isl_schedule_pullback_union_pw_multi_aff(Schedule, Tags);
364 POLLY_DEBUG(dbgs() << "Read: " << Read << "\n";
365 dbgs() << "MustWrite: " << MustWrite << "\n";
366 dbgs() << "MayWrite: " << MayWrite << "\n";
367 dbgs() << "Schedule: " << Schedule << "\n");
369 isl_union_map *StrictWAW = nullptr;
371 IslMaxOperationsGuard MaxOpGuard(IslCtx.get(), OptComputeOut);
373 RAW = WAW = WAR = RED = nullptr;
374 isl_union_map *Write = isl_union_map_union(isl_union_map_copy(MustWrite),
375 isl_union_map_copy(MayWrite));
377 // We are interested in detecting reductions that do not have intermediate
378 // computations that are captured by other statements.
380 // Example:
381 // void f(int *A, int *B) {
382 // for(int i = 0; i <= 100; i++) {
384 // *-WAR (S0[i] -> S0[i + 1] 0 <= i <= 100)------------*
385 // | |
386 // *-WAW (S0[i] -> S0[i + 1] 0 <= i <= 100)------------*
387 // | |
388 // v |
389 // S0: *A += i; >------------------*-----------------------*
390 // |
391 // if (i >= 98) { WAR (S0[i] -> S1[i]) 98 <= i <= 100
392 // |
393 // S1: *B = *A; <--------------*
394 // }
395 // }
396 // }
398 // S0[0 <= i <= 100] has a reduction. However, the values in
399 // S0[98 <= i <= 100] is captured in S1[98 <= i <= 100].
400 // Since we allow free reordering on our reduction dependences, we need to
401 // remove all instances of a reduction statement that have data dependences
402 // originating from them.
403 // In the case of the example, we need to remove S0[98 <= i <= 100] from
404 // our reduction dependences.
406 // When we build up the WAW dependences that are used to detect reductions,
407 // we consider only **Writes that have no intermediate Reads**.
409 // `isl_union_flow_get_must_dependence` gives us dependences of the form:
410 // (sink <- must_source).
412 // It *will not give* dependences of the form:
413 // 1. (sink <- ... <- may_source <- ... <- must_source)
414 // 2. (sink <- ... <- must_source <- ... <- must_source)
416 // For a detailed reference on ISL's flow analysis, see:
417 // "Presburger Formulas and Polyhedral Compilation" - Approximate Dataflow
418 // Analysis.
420 // Since we set "Write" as a must-source, "Read" as a may-source, and ask
421 // for must dependences, we get all Writes to Writes that **do not flow
422 // through a Read**.
424 // ScopInfo::checkForReductions makes sure that if something captures
425 // the reduction variable in the same basic block, then it is rejected
426 // before it is even handed here. This makes sure that there is exactly
427 // one read and one write to a reduction variable in a Statement.
428 // Example:
429 // void f(int *sum, int A[N], int B[N]) {
430 // for (int i = 0; i < N; i++) {
431 // *sum += A[i]; < the store and the load is not tagged as a
432 // B[i] = *sum; < reduction-like access due to the overlap.
433 // }
434 // }
436 isl_union_flow *Flow = buildFlow(Write, Write, Read, nullptr, Schedule);
437 StrictWAW = isl_union_flow_get_must_dependence(Flow);
438 isl_union_flow_free(Flow);
440 if (OptAnalysisType == VALUE_BASED_ANALYSIS) {
441 Flow = buildFlow(Read, MustWrite, MayWrite, nullptr, Schedule);
442 RAW = isl_union_flow_get_may_dependence(Flow);
443 isl_union_flow_free(Flow);
445 Flow = buildFlow(Write, MustWrite, MayWrite, nullptr, Schedule);
446 WAW = isl_union_flow_get_may_dependence(Flow);
447 isl_union_flow_free(Flow);
449 // ISL now supports "kills" in approximate dataflow analysis, we can
450 // specify the MustWrite as kills, Read as source and Write as sink.
451 Flow = buildFlow(Write, nullptr, Read, MustWrite, Schedule);
452 WAR = isl_union_flow_get_may_dependence(Flow);
453 isl_union_flow_free(Flow);
454 } else {
455 Flow = buildFlow(Read, nullptr, Write, nullptr, Schedule);
456 RAW = isl_union_flow_get_may_dependence(Flow);
457 isl_union_flow_free(Flow);
459 Flow = buildFlow(Write, nullptr, Read, nullptr, Schedule);
460 WAR = isl_union_flow_get_may_dependence(Flow);
461 isl_union_flow_free(Flow);
463 Flow = buildFlow(Write, nullptr, Write, nullptr, Schedule);
464 WAW = isl_union_flow_get_may_dependence(Flow);
465 isl_union_flow_free(Flow);
468 isl_union_map_free(Write);
469 isl_union_map_free(MustWrite);
470 isl_union_map_free(MayWrite);
471 isl_union_map_free(Read);
472 isl_schedule_free(Schedule);
474 RAW = isl_union_map_coalesce(RAW);
475 WAW = isl_union_map_coalesce(WAW);
476 WAR = isl_union_map_coalesce(WAR);
478 // End of max_operations scope.
481 if (isl_ctx_last_error(IslCtx.get()) == isl_error_quota) {
482 isl_union_map_free(RAW);
483 isl_union_map_free(WAW);
484 isl_union_map_free(WAR);
485 isl_union_map_free(StrictWAW);
486 RAW = WAW = WAR = StrictWAW = nullptr;
487 isl_ctx_reset_error(IslCtx.get());
490 // Drop out early, as the remaining computations are only needed for
491 // reduction dependences or dependences that are finer than statement
492 // level dependences.
493 if (!HasReductions && Level == AL_Statement) {
494 RED = isl_union_map_empty(isl_union_map_get_space(RAW));
495 TC_RED = isl_union_map_empty(isl_union_set_get_space(TaggedStmtDomain));
496 isl_union_set_free(TaggedStmtDomain);
497 isl_union_map_free(StrictWAW);
498 return;
501 isl_union_map *STMT_RAW, *STMT_WAW, *STMT_WAR;
502 STMT_RAW = isl_union_map_intersect_domain(
503 isl_union_map_copy(RAW), isl_union_set_copy(TaggedStmtDomain));
504 STMT_WAW = isl_union_map_intersect_domain(
505 isl_union_map_copy(WAW), isl_union_set_copy(TaggedStmtDomain));
506 STMT_WAR =
507 isl_union_map_intersect_domain(isl_union_map_copy(WAR), TaggedStmtDomain);
508 POLLY_DEBUG({
509 dbgs() << "Wrapped Dependences:\n";
510 dump();
511 dbgs() << "\n";
514 // To handle reduction dependences we proceed as follows:
515 // 1) Aggregate all possible reduction dependences, namely all self
516 // dependences on reduction like statements.
517 // 2) Intersect them with the actual RAW & WAW dependences to the get the
518 // actual reduction dependences. This will ensure the load/store memory
519 // addresses were __identical__ in the two iterations of the statement.
520 // 3) Relax the original RAW, WAW and WAR dependences by subtracting the
521 // actual reduction dependences. Binary reductions (sum += A[i]) cause
522 // the same, RAW, WAW and WAR dependences.
523 // 4) Add the privatization dependences which are widened versions of
524 // already present dependences. They model the effect of manual
525 // privatization at the outermost possible place (namely after the last
526 // write and before the first access to a reduction location).
528 // Step 1)
529 RED = isl_union_map_empty(isl_union_map_get_space(RAW));
530 for (ScopStmt &Stmt : S) {
531 for (MemoryAccess *MA : Stmt) {
532 if (!MA->isReductionLike())
533 continue;
534 isl_set *AccDomW = isl_map_wrap(MA->getAccessRelation().release());
535 isl_map *Identity =
536 isl_map_from_domain_and_range(isl_set_copy(AccDomW), AccDomW);
537 RED = isl_union_map_add_map(RED, Identity);
541 // Step 2)
542 RED = isl_union_map_intersect(RED, isl_union_map_copy(RAW));
543 RED = isl_union_map_intersect(RED, StrictWAW);
545 if (!isl_union_map_is_empty(RED)) {
547 // Step 3)
548 RAW = isl_union_map_subtract(RAW, isl_union_map_copy(RED));
549 WAW = isl_union_map_subtract(WAW, isl_union_map_copy(RED));
550 WAR = isl_union_map_subtract(WAR, isl_union_map_copy(RED));
552 // Step 4)
553 addPrivatizationDependences();
554 } else
555 TC_RED = isl_union_map_empty(isl_union_map_get_space(RED));
557 POLLY_DEBUG({
558 dbgs() << "Final Wrapped Dependences:\n";
559 dump();
560 dbgs() << "\n";
563 // RED_SIN is used to collect all reduction dependences again after we
564 // split them according to the causing memory accesses. The current assumption
565 // is that our method of splitting will not have any leftovers. In the end
566 // we validate this assumption until we have more confidence in this method.
567 isl_union_map *RED_SIN = isl_union_map_empty(isl_union_map_get_space(RAW));
569 // For each reduction like memory access, check if there are reduction
570 // dependences with the access relation of the memory access as a domain
571 // (wrapped space!). If so these dependences are caused by this memory access.
572 // We then move this portion of reduction dependences back to the statement ->
573 // statement space and add a mapping from the memory access to these
574 // dependences.
575 for (ScopStmt &Stmt : S) {
576 for (MemoryAccess *MA : Stmt) {
577 if (!MA->isReductionLike())
578 continue;
580 isl_set *AccDomW = isl_map_wrap(MA->getAccessRelation().release());
581 isl_union_map *AccRedDepU = isl_union_map_intersect_domain(
582 isl_union_map_copy(TC_RED), isl_union_set_from_set(AccDomW));
583 if (isl_union_map_is_empty(AccRedDepU)) {
584 isl_union_map_free(AccRedDepU);
585 continue;
588 isl_map *AccRedDep = isl_map_from_union_map(AccRedDepU);
589 RED_SIN = isl_union_map_add_map(RED_SIN, isl_map_copy(AccRedDep));
590 AccRedDep = isl_map_zip(AccRedDep);
591 AccRedDep = isl_set_unwrap(isl_map_domain(AccRedDep));
592 setReductionDependences(MA, AccRedDep);
596 assert(isl_union_map_is_equal(RED_SIN, TC_RED) &&
597 "Intersecting the reduction dependence domain with the wrapped access "
598 "relation is not enough, we need to loosen the access relation also");
599 isl_union_map_free(RED_SIN);
601 RAW = isl_union_map_zip(RAW);
602 WAW = isl_union_map_zip(WAW);
603 WAR = isl_union_map_zip(WAR);
604 RED = isl_union_map_zip(RED);
605 TC_RED = isl_union_map_zip(TC_RED);
607 POLLY_DEBUG({
608 dbgs() << "Zipped Dependences:\n";
609 dump();
610 dbgs() << "\n";
613 RAW = isl_union_set_unwrap(isl_union_map_domain(RAW));
614 WAW = isl_union_set_unwrap(isl_union_map_domain(WAW));
615 WAR = isl_union_set_unwrap(isl_union_map_domain(WAR));
616 RED = isl_union_set_unwrap(isl_union_map_domain(RED));
617 TC_RED = isl_union_set_unwrap(isl_union_map_domain(TC_RED));
619 POLLY_DEBUG({
620 dbgs() << "Unwrapped Dependences:\n";
621 dump();
622 dbgs() << "\n";
625 RAW = isl_union_map_union(RAW, STMT_RAW);
626 WAW = isl_union_map_union(WAW, STMT_WAW);
627 WAR = isl_union_map_union(WAR, STMT_WAR);
629 RAW = isl_union_map_coalesce(RAW);
630 WAW = isl_union_map_coalesce(WAW);
631 WAR = isl_union_map_coalesce(WAR);
632 RED = isl_union_map_coalesce(RED);
633 TC_RED = isl_union_map_coalesce(TC_RED);
635 POLLY_DEBUG(dump());
638 bool Dependences::isValidSchedule(Scop &S, isl::schedule NewSched) const {
639 // TODO: Also check permutable/coincident flags as well.
641 StatementToIslMapTy NewSchedules;
642 for (auto NewMap : NewSched.get_map().get_map_list()) {
643 auto Stmt = reinterpret_cast<ScopStmt *>(
644 NewMap.get_tuple_id(isl::dim::in).get_user());
645 NewSchedules[Stmt] = NewMap;
648 return isValidSchedule(S, NewSchedules);
651 bool Dependences::isValidSchedule(
652 Scop &S, const StatementToIslMapTy &NewSchedule) const {
653 if (LegalityCheckDisabled)
654 return true;
656 isl::union_map Dependences = getDependences(TYPE_RAW | TYPE_WAW | TYPE_WAR);
657 isl::union_map Schedule = isl::union_map::empty(S.getIslCtx());
659 isl::space ScheduleSpace;
661 for (ScopStmt &Stmt : S) {
662 isl::map StmtScat;
664 auto Lookup = NewSchedule.find(&Stmt);
665 if (Lookup == NewSchedule.end())
666 StmtScat = Stmt.getSchedule();
667 else
668 StmtScat = Lookup->second;
669 assert(!StmtScat.is_null() &&
670 "Schedules that contain extension nodes require special handling.");
672 if (ScheduleSpace.is_null())
673 ScheduleSpace = StmtScat.get_space().range();
675 Schedule = Schedule.unite(StmtScat);
678 Dependences = Dependences.apply_domain(Schedule);
679 Dependences = Dependences.apply_range(Schedule);
681 isl::set Zero = isl::set::universe(ScheduleSpace);
682 for (auto i : rangeIslSize(0, Zero.tuple_dim()))
683 Zero = Zero.fix_si(isl::dim::set, i, 0);
685 isl::union_set UDeltas = Dependences.deltas();
686 isl::set Deltas = singleton(UDeltas, ScheduleSpace);
688 isl::space Space = Deltas.get_space();
689 isl::map NonPositive = isl::map::universe(Space.map_from_set());
690 NonPositive =
691 NonPositive.lex_le_at(isl::multi_pw_aff::identity_on_domain(Space));
692 NonPositive = NonPositive.intersect_domain(Deltas);
693 NonPositive = NonPositive.intersect_range(Zero);
695 return NonPositive.is_empty();
698 // Check if the current scheduling dimension is parallel.
700 // We check for parallelism by verifying that the loop does not carry any
701 // dependences.
703 // Parallelism test: if the distance is zero in all outer dimensions, then it
704 // has to be zero in the current dimension as well.
706 // Implementation: first, translate dependences into time space, then force
707 // outer dimensions to be equal. If the distance is zero in the current
708 // dimension, then the loop is parallel. The distance is zero in the current
709 // dimension if it is a subset of a map with equal values for the current
710 // dimension.
711 bool Dependences::isParallel(__isl_keep isl_union_map *Schedule,
712 __isl_take isl_union_map *Deps,
713 __isl_give isl_pw_aff **MinDistancePtr) const {
714 isl_set *Deltas, *Distance;
715 isl_map *ScheduleDeps;
716 unsigned Dimension;
717 bool IsParallel;
719 Deps = isl_union_map_apply_range(Deps, isl_union_map_copy(Schedule));
720 Deps = isl_union_map_apply_domain(Deps, isl_union_map_copy(Schedule));
722 if (isl_union_map_is_empty(Deps)) {
723 isl_union_map_free(Deps);
724 return true;
727 ScheduleDeps = isl_map_from_union_map(Deps);
728 Dimension = isl_map_dim(ScheduleDeps, isl_dim_out) - 1;
730 for (unsigned i = 0; i < Dimension; i++)
731 ScheduleDeps = isl_map_equate(ScheduleDeps, isl_dim_out, i, isl_dim_in, i);
733 Deltas = isl_map_deltas(ScheduleDeps);
734 Distance = isl_set_universe(isl_set_get_space(Deltas));
736 // [0, ..., 0, +] - All zeros and last dimension larger than zero
737 for (unsigned i = 0; i < Dimension; i++)
738 Distance = isl_set_fix_si(Distance, isl_dim_set, i, 0);
740 Distance = isl_set_lower_bound_si(Distance, isl_dim_set, Dimension, 1);
741 Distance = isl_set_intersect(Distance, Deltas);
743 IsParallel = isl_set_is_empty(Distance);
744 if (IsParallel || !MinDistancePtr) {
745 isl_set_free(Distance);
746 return IsParallel;
749 Distance = isl_set_project_out(Distance, isl_dim_set, 0, Dimension);
750 Distance = isl_set_coalesce(Distance);
752 // This last step will compute a expression for the minimal value in the
753 // distance polyhedron Distance with regards to the first (outer most)
754 // dimension.
755 *MinDistancePtr = isl_pw_aff_coalesce(isl_set_dim_min(Distance, 0));
757 return false;
760 static void printDependencyMap(raw_ostream &OS, __isl_keep isl_union_map *DM) {
761 if (DM)
762 OS << DM << "\n";
763 else
764 OS << "n/a\n";
767 void Dependences::print(raw_ostream &OS) const {
768 OS << "\tRAW dependences:\n\t\t";
769 printDependencyMap(OS, RAW);
770 OS << "\tWAR dependences:\n\t\t";
771 printDependencyMap(OS, WAR);
772 OS << "\tWAW dependences:\n\t\t";
773 printDependencyMap(OS, WAW);
774 OS << "\tReduction dependences:\n\t\t";
775 printDependencyMap(OS, RED);
776 OS << "\tTransitive closure of reduction dependences:\n\t\t";
777 printDependencyMap(OS, TC_RED);
780 void Dependences::dump() const { print(dbgs()); }
782 void Dependences::releaseMemory() {
783 isl_union_map_free(RAW);
784 isl_union_map_free(WAR);
785 isl_union_map_free(WAW);
786 isl_union_map_free(RED);
787 isl_union_map_free(TC_RED);
789 RED = RAW = WAR = WAW = TC_RED = nullptr;
791 for (auto &ReductionDeps : ReductionDependences)
792 isl_map_free(ReductionDeps.second);
793 ReductionDependences.clear();
796 isl::union_map Dependences::getDependences(int Kinds) const {
797 assert(hasValidDependences() && "No valid dependences available");
798 isl::space Space = isl::manage_copy(RAW).get_space();
799 isl::union_map Deps = Deps.empty(Space.ctx());
801 if (Kinds & TYPE_RAW)
802 Deps = Deps.unite(isl::manage_copy(RAW));
804 if (Kinds & TYPE_WAR)
805 Deps = Deps.unite(isl::manage_copy(WAR));
807 if (Kinds & TYPE_WAW)
808 Deps = Deps.unite(isl::manage_copy(WAW));
810 if (Kinds & TYPE_RED)
811 Deps = Deps.unite(isl::manage_copy(RED));
813 if (Kinds & TYPE_TC_RED)
814 Deps = Deps.unite(isl::manage_copy(TC_RED));
816 Deps = Deps.coalesce();
817 Deps = Deps.detect_equalities();
818 return Deps;
821 bool Dependences::hasValidDependences() const {
822 return (RAW != nullptr) && (WAR != nullptr) && (WAW != nullptr);
825 __isl_give isl_map *
826 Dependences::getReductionDependences(MemoryAccess *MA) const {
827 return isl_map_copy(ReductionDependences.lookup(MA));
830 void Dependences::setReductionDependences(MemoryAccess *MA,
831 __isl_take isl_map *D) {
832 assert(ReductionDependences.count(MA) == 0 &&
833 "Reduction dependences set twice!");
834 ReductionDependences[MA] = D;
837 const Dependences &
838 DependenceAnalysis::Result::getDependences(Dependences::AnalysisLevel Level) {
839 if (Dependences *d = D[Level].get())
840 return *d;
842 return recomputeDependences(Level);
845 const Dependences &DependenceAnalysis::Result::recomputeDependences(
846 Dependences::AnalysisLevel Level) {
847 D[Level].reset(new Dependences(S.getSharedIslCtx(), Level));
848 D[Level]->calculateDependences(S);
849 return *D[Level];
852 void DependenceAnalysis::Result::abandonDependences() {
853 for (std::unique_ptr<Dependences> &Deps : D)
854 Deps.release();
857 DependenceAnalysis::Result
858 DependenceAnalysis::run(Scop &S, ScopAnalysisManager &SAM,
859 ScopStandardAnalysisResults &SAR) {
860 return {S, {}};
863 AnalysisKey DependenceAnalysis::Key;
865 PreservedAnalyses
866 DependenceInfoPrinterPass::run(Scop &S, ScopAnalysisManager &SAM,
867 ScopStandardAnalysisResults &SAR,
868 SPMUpdater &U) {
869 auto &DI = SAM.getResult<DependenceAnalysis>(S, SAR);
871 if (auto d = DI.D[OptAnalysisLevel].get()) {
872 d->print(OS);
873 return PreservedAnalyses::all();
876 // Otherwise create the dependences on-the-fly and print them
877 Dependences D(S.getSharedIslCtx(), OptAnalysisLevel);
878 D.calculateDependences(S);
879 D.print(OS);
881 return PreservedAnalyses::all();
884 const Dependences &
885 DependenceInfo::getDependences(Dependences::AnalysisLevel Level) {
886 if (Dependences *d = D[Level].get())
887 return *d;
889 return recomputeDependences(Level);
892 const Dependences &
893 DependenceInfo::recomputeDependences(Dependences::AnalysisLevel Level) {
894 D[Level].reset(new Dependences(S->getSharedIslCtx(), Level));
895 D[Level]->calculateDependences(*S);
896 return *D[Level];
899 void DependenceInfo::abandonDependences() {
900 for (std::unique_ptr<Dependences> &Deps : D)
901 Deps.release();
904 bool DependenceInfo::runOnScop(Scop &ScopVar) {
905 S = &ScopVar;
906 return false;
909 /// Print the dependences for the given SCoP to @p OS.
911 void polly::DependenceInfo::printScop(raw_ostream &OS, Scop &S) const {
912 if (auto d = D[OptAnalysisLevel].get()) {
913 d->print(OS);
914 return;
917 // Otherwise create the dependences on-the-fly and print it
918 Dependences D(S.getSharedIslCtx(), OptAnalysisLevel);
919 D.calculateDependences(S);
920 D.print(OS);
923 void DependenceInfo::getAnalysisUsage(AnalysisUsage &AU) const {
924 AU.addRequiredTransitive<ScopInfoRegionPass>();
925 AU.setPreservesAll();
928 char DependenceInfo::ID = 0;
930 Pass *polly::createDependenceInfoPass() { return new DependenceInfo(); }
932 INITIALIZE_PASS_BEGIN(DependenceInfo, "polly-dependences",
933 "Polly - Calculate dependences", false, false);
934 INITIALIZE_PASS_DEPENDENCY(ScopInfoRegionPass);
935 INITIALIZE_PASS_END(DependenceInfo, "polly-dependences",
936 "Polly - Calculate dependences", false, false)
938 //===----------------------------------------------------------------------===//
940 namespace {
941 /// Print result from DependenceAnalysis.
942 class DependenceInfoPrinterLegacyPass final : public ScopPass {
943 public:
944 static char ID;
946 DependenceInfoPrinterLegacyPass() : DependenceInfoPrinterLegacyPass(outs()) {}
948 explicit DependenceInfoPrinterLegacyPass(llvm::raw_ostream &OS)
949 : ScopPass(ID), OS(OS) {}
951 bool runOnScop(Scop &S) override {
952 DependenceInfo &P = getAnalysis<DependenceInfo>();
954 OS << "Printing analysis '" << P.getPassName() << "' for "
955 << "region: '" << S.getRegion().getNameStr() << "' in function '"
956 << S.getFunction().getName() << "':\n";
957 P.printScop(OS, S);
959 return false;
962 void getAnalysisUsage(AnalysisUsage &AU) const override {
963 ScopPass::getAnalysisUsage(AU);
964 AU.addRequired<DependenceInfo>();
965 AU.setPreservesAll();
968 private:
969 llvm::raw_ostream &OS;
972 char DependenceInfoPrinterLegacyPass::ID = 0;
973 } // namespace
975 Pass *polly::createDependenceInfoPrinterLegacyPass(raw_ostream &OS) {
976 return new DependenceInfoPrinterLegacyPass(OS);
979 INITIALIZE_PASS_BEGIN(DependenceInfoPrinterLegacyPass,
980 "polly-print-dependences", "Polly - Print dependences",
981 false, false);
982 INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
983 INITIALIZE_PASS_END(DependenceInfoPrinterLegacyPass, "polly-print-dependences",
984 "Polly - Print dependences", false, false)
986 //===----------------------------------------------------------------------===//
988 const Dependences &
989 DependenceInfoWrapperPass::getDependences(Scop *S,
990 Dependences::AnalysisLevel Level) {
991 auto It = ScopToDepsMap.find(S);
992 if (It != ScopToDepsMap.end())
993 if (It->second) {
994 if (It->second->getDependenceLevel() == Level)
995 return *It->second.get();
997 return recomputeDependences(S, Level);
1000 const Dependences &DependenceInfoWrapperPass::recomputeDependences(
1001 Scop *S, Dependences::AnalysisLevel Level) {
1002 std::unique_ptr<Dependences> D(new Dependences(S->getSharedIslCtx(), Level));
1003 D->calculateDependences(*S);
1004 auto Inserted = ScopToDepsMap.insert(std::make_pair(S, std::move(D)));
1005 return *Inserted.first->second;
1008 bool DependenceInfoWrapperPass::runOnFunction(Function &F) {
1009 auto &SI = *getAnalysis<ScopInfoWrapperPass>().getSI();
1010 for (auto &It : SI) {
1011 assert(It.second && "Invalid SCoP object!");
1012 recomputeDependences(It.second.get(), Dependences::AL_Access);
1014 return false;
1017 void DependenceInfoWrapperPass::print(raw_ostream &OS, const Module *M) const {
1018 for (auto &It : ScopToDepsMap) {
1019 assert((It.first && It.second) && "Invalid Scop or Dependence object!\n");
1020 It.second->print(OS);
1024 void DependenceInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
1025 AU.addRequiredTransitive<ScopInfoWrapperPass>();
1026 AU.setPreservesAll();
1029 char DependenceInfoWrapperPass::ID = 0;
1031 Pass *polly::createDependenceInfoWrapperPassPass() {
1032 return new DependenceInfoWrapperPass();
1035 INITIALIZE_PASS_BEGIN(
1036 DependenceInfoWrapperPass, "polly-function-dependences",
1037 "Polly - Calculate dependences for all the SCoPs of a function", false,
1038 false)
1039 INITIALIZE_PASS_DEPENDENCY(ScopInfoWrapperPass);
1040 INITIALIZE_PASS_END(
1041 DependenceInfoWrapperPass, "polly-function-dependences",
1042 "Polly - Calculate dependences for all the SCoPs of a function", false,
1043 false)
1045 //===----------------------------------------------------------------------===//
1047 namespace {
1048 /// Print result from DependenceInfoWrapperPass.
1049 class DependenceInfoPrinterLegacyFunctionPass final : public FunctionPass {
1050 public:
1051 static char ID;
1053 DependenceInfoPrinterLegacyFunctionPass()
1054 : DependenceInfoPrinterLegacyFunctionPass(outs()) {}
1056 explicit DependenceInfoPrinterLegacyFunctionPass(llvm::raw_ostream &OS)
1057 : FunctionPass(ID), OS(OS) {}
1059 bool runOnFunction(Function &F) override {
1060 DependenceInfoWrapperPass &P = getAnalysis<DependenceInfoWrapperPass>();
1062 OS << "Printing analysis '" << P.getPassName() << "' for function '"
1063 << F.getName() << "':\n";
1064 P.print(OS);
1066 return false;
1069 void getAnalysisUsage(AnalysisUsage &AU) const override {
1070 FunctionPass::getAnalysisUsage(AU);
1071 AU.addRequired<DependenceInfoWrapperPass>();
1072 AU.setPreservesAll();
1075 private:
1076 llvm::raw_ostream &OS;
1079 char DependenceInfoPrinterLegacyFunctionPass::ID = 0;
1080 } // namespace
1082 Pass *polly::createDependenceInfoPrinterLegacyFunctionPass(raw_ostream &OS) {
1083 return new DependenceInfoPrinterLegacyFunctionPass(OS);
1086 INITIALIZE_PASS_BEGIN(
1087 DependenceInfoPrinterLegacyFunctionPass, "polly-print-function-dependences",
1088 "Polly - Print dependences for all the SCoPs of a function", false, false);
1089 INITIALIZE_PASS_DEPENDENCY(DependenceInfoWrapperPass);
1090 INITIALIZE_PASS_END(DependenceInfoPrinterLegacyFunctionPass,
1091 "polly-print-function-dependences",
1092 "Polly - Print dependences for all the SCoPs of a function",
1093 false, false)