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.
10 //
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.
14 //
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.
19 //
20 //===----------------------------------------------------------------------===//
21 //
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/Support/Debug.h"
29 #include "isl/aff.h"
30 #include "isl/ctx.h"
31 #include "isl/flow.h"
32 #include "isl/map.h"
33 #include "isl/schedule.h"
34 #include "isl/set.h"
35 #include "isl/union_map.h"
36 #include "isl/union_set.h"
37
38 using namespace polly;
39 using namespace llvm;
40
41 #define DEBUG_TYPE "polly-dependence"
42
43 static cl::opt<int> OptComputeOut(
44 "polly-dependences-computeout",
45 cl::desc("Bound the dependence analysis by a maximal amount of "
46 "computational steps (0 means no bound)"),
47 cl::Hidden, cl::init(500000), cl::ZeroOrMore, cl::cat(PollyCategory));
48
49 static cl::opt<bool> LegalityCheckDisabled(
50 "disable-polly-legality", cl::desc("Disable polly legality check"),
51 cl::Hidden, cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
52
53 static cl::opt<bool>
54 UseReductions("polly-dependences-use-reductions",
55 cl::desc("Exploit reductions in dependence analysis"),
56 cl::Hidden, cl::init(true), cl::ZeroOrMore,
57 cl::cat(PollyCategory));
58
59 enum AnalysisType { VALUE_BASED_ANALYSIS, MEMORY_BASED_ANALYSIS };
60
61 static cl::opt<enum AnalysisType> OptAnalysisType(
62 "polly-dependences-analysis-type",
63 cl::desc("The kind of dependence analysis to use"),
64 cl::values(clEnumValN(VALUE_BASED_ANALYSIS, "value-based",
65 "Exact dependences without transitive dependences"),
66 clEnumValN(MEMORY_BASED_ANALYSIS, "memory-based",
67 "Overapproximation of dependences")),
68 cl::Hidden, cl::init(VALUE_BASED_ANALYSIS), cl::ZeroOrMore,
69 cl::cat(PollyCategory));
70
71 static cl::opt<Dependences::AnalysisLevel> OptAnalysisLevel(
72 "polly-dependences-analysis-level",
73 cl::desc("The level of dependence analysis"),
74 cl::values(clEnumValN(Dependences::AL_Statement, "statement-wise",
75 "Statement-level analysis"),
76 clEnumValN(Dependences::AL_Reference, "reference-wise",
77 "Memory reference level analysis that distinguish"
78 " accessed references in the same statement"),
79 clEnumValN(Dependences::AL_Access, "access-wise",
80 "Memory reference level analysis that distinguish"
81 " access instructions in the same statement")),
82 cl::Hidden, cl::init(Dependences::AL_Statement), cl::ZeroOrMore,
83 cl::cat(PollyCategory));
84
85 //===----------------------------------------------------------------------===//
86
87 /// Tag the @p Relation domain with @p TagId
tag(__isl_take isl_map * Relation,__isl_take isl_id * 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;
97 }
98
99 /// Tag the @p Relation domain with either MA->getArrayId() or
100 /// MA->getId() based on @p TagLevel
tag(__isl_take isl_map * Relation,MemoryAccess * MA,Dependences::AnalysisLevel 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());
105
106 if (TagLevel == Dependences::AL_Access)
107 return tag(Relation, MA->getId().release());
108
109 // No need to tag at the statement level.
110 return Relation;
111 }
112
113 /// Collect information about the SCoP @p S.
collectInfo(Scop & S,isl_union_map * & Read,isl_union_map * & MustWrite,isl_union_map * & MayWrite,isl_union_map * & ReductionTagMap,isl_union_set * & TaggedStmtDomain,Dependences::AnalysisLevel Level)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);
125
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());
132
133 for (ScopStmt &Stmt : S) {
134 for (MemoryAccess *MA : Stmt) {
135 isl_set *domcp = Stmt.getDomain().release();
136 isl_map *accdom = MA->getAccessRelation().release();
137
138 accdom = isl_map_intersect_domain(accdom, domcp);
139
140 if (ReductionArrays.count(MA->getScopArrayInfo())) {
141 // Wrap the access domain and adjust the schedule accordingly.
142 //
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]
147 //
148 // We collect all the access domains in the ReductionTagMap.
149 // This is used in Dependences::calculateDependences to create
150 // a tagged Schedule tree.
151
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);
164 }
165 }
166
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);
173 }
174
175 if (!ReductionArrays.empty() && Level == Dependences::AL_Statement)
176 StmtSchedule =
177 isl_union_map_add_map(StmtSchedule, Stmt.getSchedule().release());
178 }
179
180 StmtSchedule = isl_union_map_intersect_params(
181 StmtSchedule, S.getAssumedContext().release());
182 TaggedStmtDomain = isl_union_map_domain(StmtSchedule);
183
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);
188 }
189
190 /// Fix all dimension of @p Zero to 0 and add it to @p user
fixSetToZero(isl::set Zero,isl::union_set * User)191 static void fixSetToZero(isl::set Zero, isl::union_set *User) {
192 for (unsigned i = 0; i < Zero.dim(isl::dim::set); i++)
193 Zero = Zero.fix_si(isl::dim::set, i, 0);
194 *User = User->add_set(Zero);
195 }
196
197 /// Compute the privatization dependences for a given dependency @p Map
198 ///
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.
206 ///
207 /// For the input:
208 ///
209 /// S0: *sum = 0;
210 /// for (int i = 0; i < 1024; i++)
211 /// S1: *sum += i;
212 /// S2: *sum = *sum * 3;
213 ///
214 /// we have the following dependences before we add privatization dependences:
215 ///
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 }
224 ///
225 /// and afterwards:
226 ///
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 }
237 ///
238 /// Note: This function also computes the (reverse) transitive closure of the
239 /// reduction dependences.
addPrivatizationDependences()240 void Dependences::addPrivatizationDependences() {
241 isl_union_map *PrivRAW, *PrivWAW, *PrivWAR;
242
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);
248
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)));
258
259 for (isl::set Set : isl::manage_copy(Universe).get_set_list())
260 fixSetToZero(Set, &Zero);
261
262 isl_union_map *NonPositive =
263 isl_union_set_lex_le_union_set(UDeltas, Zero.release());
264
265 TC_RED = isl_union_map_subtract(TC_RED, NonPositive);
266
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);
270
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];
275
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)));
281
282 *Map = isl_union_map_union(*Map, *PrivMap);
283 }
284
285 isl_union_set_free(Universe);
286 }
287
buildFlow(__isl_keep isl_union_map * Snk,__isl_keep isl_union_map * Src,__isl_keep isl_union_map * MaySrc,__isl_keep isl_union_map * Kill,__isl_keep isl_schedule * Schedule)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;
294
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 LLVM_DEBUG(if (!Flow) dbgs()
305 << "last error: "
306 << isl_ctx_last_error(isl_schedule_get_ctx(Schedule))
307 << '\n';);
308 return Flow;
309 }
310
calculateDependences(Scop & S)311 void Dependences::calculateDependences(Scop &S) {
312 isl_union_map *Read, *MustWrite, *MayWrite, *ReductionTagMap;
313 isl_schedule *Schedule;
314 isl_union_set *TaggedStmtDomain;
315
316 LLVM_DEBUG(dbgs() << "Scop: \n" << S << "\n");
317
318 collectInfo(S, Read, MustWrite, MayWrite, ReductionTagMap, TaggedStmtDomain,
319 Level);
320
321 bool HasReductions = !isl_union_map_is_empty(ReductionTagMap);
322
323 LLVM_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';);
328
329 Schedule = S.getScheduleTree().release();
330
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);
339 }
340 } else {
341 isl_union_map *IdentityMap;
342 isl_union_pw_multi_aff *ReductionTags, *IdentityTags, *Tags;
343
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);
350
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);
355
356 Tags = isl_union_pw_multi_aff_union_add(ReductionTags, IdentityTags);
357
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);
362 }
363
364 LLVM_DEBUG(dbgs() << "Read: " << Read << "\n";
365 dbgs() << "MustWrite: " << MustWrite << "\n";
366 dbgs() << "MayWrite: " << MayWrite << "\n";
367 dbgs() << "Schedule: " << Schedule << "\n");
368
369 isl_union_map *StrictWAW = nullptr;
370 {
371 IslMaxOperationsGuard MaxOpGuard(IslCtx.get(), OptComputeOut);
372
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));
376
377 // We are interested in detecting reductions that do not have intermediate
378 // computations that are captured by other statements.
379 //
380 // Example:
381 // void f(int *A, int *B) {
382 // for(int i = 0; i <= 100; i++) {
383 //
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 // }
397 //
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.
405 //
406 // When we build up the WAW dependences that are used to detect reductions,
407 // we consider only **Writes that have no intermediate Reads**.
408 //
409 // `isl_union_flow_get_must_dependence` gives us dependences of the form:
410 // (sink <- must_source).
411 //
412 // It *will not give* dependences of the form:
413 // 1. (sink <- ... <- may_source <- ... <- must_source)
414 // 2. (sink <- ... <- must_source <- ... <- must_source)
415 //
416 // For a detailed reference on ISL's flow analysis, see:
417 // "Presburger Formulas and Polyhedral Compilation" - Approximate Dataflow
418 // Analysis.
419 //
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**.
423 //
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 // }
435
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);
439
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);
444
445 Flow = buildFlow(Write, MustWrite, MayWrite, nullptr, Schedule);
446 WAW = isl_union_flow_get_may_dependence(Flow);
447 isl_union_flow_free(Flow);
448
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);
458
459 Flow = buildFlow(Write, nullptr, Read, nullptr, Schedule);
460 WAR = isl_union_flow_get_may_dependence(Flow);
461 isl_union_flow_free(Flow);
462
463 Flow = buildFlow(Write, nullptr, Write, nullptr, Schedule);
464 WAW = isl_union_flow_get_may_dependence(Flow);
465 isl_union_flow_free(Flow);
466 }
467
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);
473
474 RAW = isl_union_map_coalesce(RAW);
475 WAW = isl_union_map_coalesce(WAW);
476 WAR = isl_union_map_coalesce(WAR);
477
478 // End of max_operations scope.
479 }
480
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());
488 }
489
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;
499 }
500
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 LLVM_DEBUG({
509 dbgs() << "Wrapped Dependences:\n";
510 dump();
511 dbgs() << "\n";
512 });
513
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).
527
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);
538 }
539 }
540
541 // Step 2)
542 RED = isl_union_map_intersect(RED, isl_union_map_copy(RAW));
543 RED = isl_union_map_intersect(RED, StrictWAW);
544
545 if (!isl_union_map_is_empty(RED)) {
546
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));
551
552 // Step 4)
553 addPrivatizationDependences();
554 } else
555 TC_RED = isl_union_map_empty(isl_union_map_get_space(RED));
556
557 LLVM_DEBUG({
558 dbgs() << "Final Wrapped Dependences:\n";
559 dump();
560 dbgs() << "\n";
561 });
562
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));
568
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;
579
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;
586 }
587
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);
593 }
594 }
595
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);
600
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);
606
607 LLVM_DEBUG({
608 dbgs() << "Zipped Dependences:\n";
609 dump();
610 dbgs() << "\n";
611 });
612
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));
618
619 LLVM_DEBUG({
620 dbgs() << "Unwrapped Dependences:\n";
621 dump();
622 dbgs() << "\n";
623 });
624
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);
628
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);
634
635 LLVM_DEBUG(dump());
636 }
637
isValidSchedule(Scop & S,const StatementToIslMapTy & NewSchedule) const638 bool Dependences::isValidSchedule(
639 Scop &S, const StatementToIslMapTy &NewSchedule) const {
640 if (LegalityCheckDisabled)
641 return true;
642
643 isl::union_map Dependences = getDependences(TYPE_RAW | TYPE_WAW | TYPE_WAR);
644 isl::space Space = S.getParamSpace();
645 isl::union_map Schedule = isl::union_map::empty(Space);
646
647 isl::space ScheduleSpace;
648
649 for (ScopStmt &Stmt : S) {
650 isl::map StmtScat;
651
652 auto Lookup = NewSchedule.find(&Stmt);
653 if (Lookup == NewSchedule.end())
654 StmtScat = Stmt.getSchedule();
655 else
656 StmtScat = Lookup->second;
657 assert(!StmtScat.is_null() &&
658 "Schedules that contain extension nodes require special handling.");
659
660 if (!ScheduleSpace)
661 ScheduleSpace = StmtScat.get_space().range();
662
663 Schedule = Schedule.add_map(StmtScat);
664 }
665
666 Dependences = Dependences.apply_domain(Schedule);
667 Dependences = Dependences.apply_range(Schedule);
668
669 isl::set Zero = isl::set::universe(ScheduleSpace);
670 for (unsigned i = 0; i < Zero.dim(isl::dim::set); i++)
671 Zero = Zero.fix_si(isl::dim::set, i, 0);
672
673 isl::union_set UDeltas = Dependences.deltas();
674 isl::set Deltas = singleton(UDeltas, ScheduleSpace);
675
676 isl::map NonPositive = Deltas.lex_le_set(Zero);
677 return NonPositive.is_empty();
678 }
679
680 // Check if the current scheduling dimension is parallel.
681 //
682 // We check for parallelism by verifying that the loop does not carry any
683 // dependences.
684 //
685 // Parallelism test: if the distance is zero in all outer dimensions, then it
686 // has to be zero in the current dimension as well.
687 //
688 // Implementation: first, translate dependences into time space, then force
689 // outer dimensions to be equal. If the distance is zero in the current
690 // dimension, then the loop is parallel. The distance is zero in the current
691 // dimension if it is a subset of a map with equal values for the current
692 // dimension.
isParallel(isl_union_map * Schedule,isl_union_map * Deps,isl_pw_aff ** MinDistancePtr) const693 bool Dependences::isParallel(isl_union_map *Schedule, isl_union_map *Deps,
694 isl_pw_aff **MinDistancePtr) const {
695 isl_set *Deltas, *Distance;
696 isl_map *ScheduleDeps;
697 unsigned Dimension;
698 bool IsParallel;
699
700 Deps = isl_union_map_apply_range(Deps, isl_union_map_copy(Schedule));
701 Deps = isl_union_map_apply_domain(Deps, isl_union_map_copy(Schedule));
702
703 if (isl_union_map_is_empty(Deps)) {
704 isl_union_map_free(Deps);
705 return true;
706 }
707
708 ScheduleDeps = isl_map_from_union_map(Deps);
709 Dimension = isl_map_dim(ScheduleDeps, isl_dim_out) - 1;
710
711 for (unsigned i = 0; i < Dimension; i++)
712 ScheduleDeps = isl_map_equate(ScheduleDeps, isl_dim_out, i, isl_dim_in, i);
713
714 Deltas = isl_map_deltas(ScheduleDeps);
715 Distance = isl_set_universe(isl_set_get_space(Deltas));
716
717 // [0, ..., 0, +] - All zeros and last dimension larger than zero
718 for (unsigned i = 0; i < Dimension; i++)
719 Distance = isl_set_fix_si(Distance, isl_dim_set, i, 0);
720
721 Distance = isl_set_lower_bound_si(Distance, isl_dim_set, Dimension, 1);
722 Distance = isl_set_intersect(Distance, Deltas);
723
724 IsParallel = isl_set_is_empty(Distance);
725 if (IsParallel || !MinDistancePtr) {
726 isl_set_free(Distance);
727 return IsParallel;
728 }
729
730 Distance = isl_set_project_out(Distance, isl_dim_set, 0, Dimension);
731 Distance = isl_set_coalesce(Distance);
732
733 // This last step will compute a expression for the minimal value in the
734 // distance polyhedron Distance with regards to the first (outer most)
735 // dimension.
736 *MinDistancePtr = isl_pw_aff_coalesce(isl_set_dim_min(Distance, 0));
737
738 return false;
739 }
740
printDependencyMap(raw_ostream & OS,__isl_keep isl_union_map * DM)741 static void printDependencyMap(raw_ostream &OS, __isl_keep isl_union_map *DM) {
742 if (DM)
743 OS << DM << "\n";
744 else
745 OS << "n/a\n";
746 }
747
print(raw_ostream & OS) const748 void Dependences::print(raw_ostream &OS) const {
749 OS << "\tRAW dependences:\n\t\t";
750 printDependencyMap(OS, RAW);
751 OS << "\tWAR dependences:\n\t\t";
752 printDependencyMap(OS, WAR);
753 OS << "\tWAW dependences:\n\t\t";
754 printDependencyMap(OS, WAW);
755 OS << "\tReduction dependences:\n\t\t";
756 printDependencyMap(OS, RED);
757 OS << "\tTransitive closure of reduction dependences:\n\t\t";
758 printDependencyMap(OS, TC_RED);
759 }
760
dump() const761 void Dependences::dump() const { print(dbgs()); }
762
releaseMemory()763 void Dependences::releaseMemory() {
764 isl_union_map_free(RAW);
765 isl_union_map_free(WAR);
766 isl_union_map_free(WAW);
767 isl_union_map_free(RED);
768 isl_union_map_free(TC_RED);
769
770 RED = RAW = WAR = WAW = TC_RED = nullptr;
771
772 for (auto &ReductionDeps : ReductionDependences)
773 isl_map_free(ReductionDeps.second);
774 ReductionDependences.clear();
775 }
776
getDependences(int Kinds) const777 isl::union_map Dependences::getDependences(int Kinds) const {
778 assert(hasValidDependences() && "No valid dependences available");
779 isl::space Space = isl::manage_copy(RAW).get_space();
780 isl::union_map Deps = Deps.empty(Space);
781
782 if (Kinds & TYPE_RAW)
783 Deps = Deps.unite(isl::manage_copy(RAW));
784
785 if (Kinds & TYPE_WAR)
786 Deps = Deps.unite(isl::manage_copy(WAR));
787
788 if (Kinds & TYPE_WAW)
789 Deps = Deps.unite(isl::manage_copy(WAW));
790
791 if (Kinds & TYPE_RED)
792 Deps = Deps.unite(isl::manage_copy(RED));
793
794 if (Kinds & TYPE_TC_RED)
795 Deps = Deps.unite(isl::manage_copy(TC_RED));
796
797 Deps = Deps.coalesce();
798 Deps = Deps.detect_equalities();
799 return Deps;
800 }
801
hasValidDependences() const802 bool Dependences::hasValidDependences() const {
803 return (RAW != nullptr) && (WAR != nullptr) && (WAW != nullptr);
804 }
805
806 __isl_give isl_map *
getReductionDependences(MemoryAccess * MA) const807 Dependences::getReductionDependences(MemoryAccess *MA) const {
808 return isl_map_copy(ReductionDependences.lookup(MA));
809 }
810
setReductionDependences(MemoryAccess * MA,isl_map * D)811 void Dependences::setReductionDependences(MemoryAccess *MA, isl_map *D) {
812 assert(ReductionDependences.count(MA) == 0 &&
813 "Reduction dependences set twice!");
814 ReductionDependences[MA] = D;
815 }
816
817 const Dependences &
getDependences(Dependences::AnalysisLevel Level)818 DependenceAnalysis::Result::getDependences(Dependences::AnalysisLevel Level) {
819 if (Dependences *d = D[Level].get())
820 return *d;
821
822 return recomputeDependences(Level);
823 }
824
recomputeDependences(Dependences::AnalysisLevel Level)825 const Dependences &DependenceAnalysis::Result::recomputeDependences(
826 Dependences::AnalysisLevel Level) {
827 D[Level].reset(new Dependences(S.getSharedIslCtx(), Level));
828 D[Level]->calculateDependences(S);
829 return *D[Level];
830 }
831
832 DependenceAnalysis::Result
run(Scop & S,ScopAnalysisManager & SAM,ScopStandardAnalysisResults & SAR)833 DependenceAnalysis::run(Scop &S, ScopAnalysisManager &SAM,
834 ScopStandardAnalysisResults &SAR) {
835 return {S, {}};
836 }
837
838 AnalysisKey DependenceAnalysis::Key;
839
840 PreservedAnalyses
run(Scop & S,ScopAnalysisManager & SAM,ScopStandardAnalysisResults & SAR,SPMUpdater & U)841 DependenceInfoPrinterPass::run(Scop &S, ScopAnalysisManager &SAM,
842 ScopStandardAnalysisResults &SAR,
843 SPMUpdater &U) {
844 auto &DI = SAM.getResult<DependenceAnalysis>(S, SAR);
845
846 if (auto d = DI.D[OptAnalysisLevel].get()) {
847 d->print(OS);
848 return PreservedAnalyses::all();
849 }
850
851 // Otherwise create the dependences on-the-fly and print them
852 Dependences D(S.getSharedIslCtx(), OptAnalysisLevel);
853 D.calculateDependences(S);
854 D.print(OS);
855
856 return PreservedAnalyses::all();
857 }
858
859 const Dependences &
getDependences(Dependences::AnalysisLevel Level)860 DependenceInfo::getDependences(Dependences::AnalysisLevel Level) {
861 if (Dependences *d = D[Level].get())
862 return *d;
863
864 return recomputeDependences(Level);
865 }
866
867 const Dependences &
recomputeDependences(Dependences::AnalysisLevel Level)868 DependenceInfo::recomputeDependences(Dependences::AnalysisLevel Level) {
869 D[Level].reset(new Dependences(S->getSharedIslCtx(), Level));
870 D[Level]->calculateDependences(*S);
871 return *D[Level];
872 }
873
runOnScop(Scop & ScopVar)874 bool DependenceInfo::runOnScop(Scop &ScopVar) {
875 S = &ScopVar;
876 return false;
877 }
878
879 /// Print the dependences for the given SCoP to @p OS.
880
printScop(raw_ostream & OS,Scop & S) const881 void polly::DependenceInfo::printScop(raw_ostream &OS, Scop &S) const {
882 if (auto d = D[OptAnalysisLevel].get()) {
883 d->print(OS);
884 return;
885 }
886
887 // Otherwise create the dependences on-the-fly and print it
888 Dependences D(S.getSharedIslCtx(), OptAnalysisLevel);
889 D.calculateDependences(S);
890 D.print(OS);
891 }
892
getAnalysisUsage(AnalysisUsage & AU) const893 void DependenceInfo::getAnalysisUsage(AnalysisUsage &AU) const {
894 AU.addRequiredTransitive<ScopInfoRegionPass>();
895 AU.setPreservesAll();
896 }
897
898 char DependenceInfo::ID = 0;
899
createDependenceInfoPass()900 Pass *polly::createDependenceInfoPass() { return new DependenceInfo(); }
901
902 INITIALIZE_PASS_BEGIN(DependenceInfo, "polly-dependences",
903 "Polly - Calculate dependences", false, false);
904 INITIALIZE_PASS_DEPENDENCY(ScopInfoRegionPass);
905 INITIALIZE_PASS_END(DependenceInfo, "polly-dependences",
906 "Polly - Calculate dependences", false, false)
907
908 //===----------------------------------------------------------------------===//
909 const Dependences &
getDependences(Scop * S,Dependences::AnalysisLevel Level)910 DependenceInfoWrapperPass::getDependences(Scop *S,
911 Dependences::AnalysisLevel Level) {
912 auto It = ScopToDepsMap.find(S);
913 if (It != ScopToDepsMap.end())
914 if (It->second) {
915 if (It->second->getDependenceLevel() == Level)
916 return *It->second.get();
917 }
918 return recomputeDependences(S, Level);
919 }
920
recomputeDependences(Scop * S,Dependences::AnalysisLevel Level)921 const Dependences &DependenceInfoWrapperPass::recomputeDependences(
922 Scop *S, Dependences::AnalysisLevel Level) {
923 std::unique_ptr<Dependences> D(new Dependences(S->getSharedIslCtx(), Level));
924 D->calculateDependences(*S);
925 auto Inserted = ScopToDepsMap.insert(std::make_pair(S, std::move(D)));
926 return *Inserted.first->second;
927 }
928
runOnFunction(Function & F)929 bool DependenceInfoWrapperPass::runOnFunction(Function &F) {
930 auto &SI = *getAnalysis<ScopInfoWrapperPass>().getSI();
931 for (auto &It : SI) {
932 assert(It.second && "Invalid SCoP object!");
933 recomputeDependences(It.second.get(), Dependences::AL_Access);
934 }
935 return false;
936 }
937
print(raw_ostream & OS,const Module * M) const938 void DependenceInfoWrapperPass::print(raw_ostream &OS, const Module *M) const {
939 for (auto &It : ScopToDepsMap) {
940 assert((It.first && It.second) && "Invalid Scop or Dependence object!\n");
941 It.second->print(OS);
942 }
943 }
944
getAnalysisUsage(AnalysisUsage & AU) const945 void DependenceInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
946 AU.addRequiredTransitive<ScopInfoWrapperPass>();
947 AU.setPreservesAll();
948 }
949
950 char DependenceInfoWrapperPass::ID = 0;
951
createDependenceInfoWrapperPassPass()952 Pass *polly::createDependenceInfoWrapperPassPass() {
953 return new DependenceInfoWrapperPass();
954 }
955
956 INITIALIZE_PASS_BEGIN(
957 DependenceInfoWrapperPass, "polly-function-dependences",
958 "Polly - Calculate dependences for all the SCoPs of a function", false,
959 false)
960 INITIALIZE_PASS_DEPENDENCY(ScopInfoWrapperPass);
961 INITIALIZE_PASS_END(
962 DependenceInfoWrapperPass, "polly-function-dependences",
963 "Polly - Calculate dependences for all the SCoPs of a function", false,
964 false)
965