1 //===- DependenceInfo.cpp - Calculate dependency information for a Scop. --===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Calculate the data dependency relations for a Scop using ISL. 11 // 12 // The integer set library (ISL) from Sven, has a integrated dependency analysis 13 // to calculate data dependences. This pass takes advantage of this and 14 // calculate those dependences a Scop. 15 // 16 // The dependences in this pass are exact in terms that for a specific read 17 // statement instance only the last write statement instance is returned. In 18 // case of may writes a set of possible write instances is returned. This 19 // analysis will never produce redundant dependences. 20 // 21 //===----------------------------------------------------------------------===// 22 // 23 #include "polly/DependenceInfo.h" 24 #include "polly/LinkAllPasses.h" 25 #include "polly/Options.h" 26 #include "polly/ScopInfo.h" 27 #include "polly/Support/GICHelper.h" 28 #include "llvm/Support/Debug.h" 29 30 #include <isl/aff.h> 31 #include <isl/ctx.h> 32 #include <isl/flow.h> 33 #include <isl/map.h> 34 #include <isl/options.h> 35 #include <isl/set.h> 36 #include <isl/schedule.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(250000), 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 enum AnalysisType { VALUE_BASED_ANALYSIS, MEMORY_BASED_ANALYSIS }; 54 55 static cl::opt<enum AnalysisType> OptAnalysisType( 56 "polly-dependences-analysis-type", 57 cl::desc("The kind of dependence analysis to use"), 58 cl::values(clEnumValN(VALUE_BASED_ANALYSIS, "value-based", 59 "Exact dependences without transitive dependences"), 60 clEnumValN(MEMORY_BASED_ANALYSIS, "memory-based", 61 "Overapproximation of dependences"), 62 clEnumValEnd), 63 cl::Hidden, cl::init(VALUE_BASED_ANALYSIS), cl::ZeroOrMore, 64 cl::cat(PollyCategory)); 65 66 //===----------------------------------------------------------------------===// 67 68 /// @brief Collect information about the SCoP @p S. 69 static void collectInfo(Scop &S, isl_union_map **Read, isl_union_map **Write, 70 isl_union_map **MayWrite, 71 isl_union_map **AccessSchedule, 72 isl_union_map **StmtSchedule) { 73 isl_space *Space = S.getParamSpace(); 74 *Read = isl_union_map_empty(isl_space_copy(Space)); 75 *Write = isl_union_map_empty(isl_space_copy(Space)); 76 *MayWrite = isl_union_map_empty(isl_space_copy(Space)); 77 *AccessSchedule = isl_union_map_empty(isl_space_copy(Space)); 78 *StmtSchedule = isl_union_map_empty(Space); 79 80 SmallPtrSet<const Value *, 8> ReductionBaseValues; 81 for (ScopStmt *Stmt : S) 82 for (MemoryAccess *MA : *Stmt) 83 if (MA->isReductionLike()) 84 ReductionBaseValues.insert(MA->getBaseAddr()); 85 86 for (ScopStmt *Stmt : S) { 87 for (MemoryAccess *MA : *Stmt) { 88 isl_set *domcp = Stmt->getDomain(); 89 isl_map *accdom = MA->getAccessRelation(); 90 91 accdom = isl_map_intersect_domain(accdom, domcp); 92 93 if (ReductionBaseValues.count(MA->getBaseAddr())) { 94 // Wrap the access domain and adjust the schedule accordingly. 95 // 96 // An access domain like 97 // Stmt[i0, i1] -> MemAcc_A[i0 + i1] 98 // will be transformed into 99 // [Stmt[i0, i1] -> MemAcc_A[i0 + i1]] -> MemAcc_A[i0 + i1] 100 // 101 // The original schedule looks like 102 // Stmt[i0, i1] -> [0, i0, 2, i1, 0] 103 // but as we transformed the access domain we need the schedule 104 // to match the new access domains, thus we need 105 // [Stmt[i0, i1] -> MemAcc_A[i0 + i1]] -> [0, i0, 2, i1, 0] 106 isl_map *Schedule = Stmt->getSchedule(); 107 Schedule = isl_map_apply_domain( 108 Schedule, 109 isl_map_reverse(isl_map_domain_map(isl_map_copy(accdom)))); 110 accdom = isl_map_range_map(accdom); 111 *AccessSchedule = isl_union_map_add_map(*AccessSchedule, Schedule); 112 } 113 114 if (MA->isRead()) 115 *Read = isl_union_map_add_map(*Read, accdom); 116 else 117 *Write = isl_union_map_add_map(*Write, accdom); 118 } 119 *StmtSchedule = isl_union_map_add_map(*StmtSchedule, Stmt->getSchedule()); 120 } 121 122 *StmtSchedule = 123 isl_union_map_intersect_params(*StmtSchedule, S.getAssumedContext()); 124 } 125 126 /// @brief Fix all dimension of @p Zero to 0 and add it to @p user 127 static int fixSetToZero(__isl_take isl_set *Zero, void *user) { 128 isl_union_set **User = (isl_union_set **)user; 129 for (unsigned i = 0; i < isl_set_dim(Zero, isl_dim_set); i++) 130 Zero = isl_set_fix_si(Zero, isl_dim_set, i, 0); 131 *User = isl_union_set_add_set(*User, Zero); 132 return 0; 133 } 134 135 /// @brief Compute the privatization dependences for a given dependency @p Map 136 /// 137 /// Privatization dependences are widened original dependences which originate 138 /// or end in a reduction access. To compute them we apply the transitive close 139 /// of the reduction dependences (which maps each iteration of a reduction 140 /// statement to all following ones) on the RAW/WAR/WAW dependences. The 141 /// dependences which start or end at a reduction statement will be extended to 142 /// depend on all following reduction statement iterations as well. 143 /// Note: "Following" here means according to the reduction dependences. 144 /// 145 /// For the input: 146 /// 147 /// S0: *sum = 0; 148 /// for (int i = 0; i < 1024; i++) 149 /// S1: *sum += i; 150 /// S2: *sum = *sum * 3; 151 /// 152 /// we have the following dependences before we add privatization dependences: 153 /// 154 /// RAW: 155 /// { S0[] -> S1[0]; S1[1023] -> S2[] } 156 /// WAR: 157 /// { } 158 /// WAW: 159 /// { S0[] -> S1[0]; S1[1024] -> S2[] } 160 /// RED: 161 /// { S1[i0] -> S1[1 + i0] : i0 >= 0 and i0 <= 1022 } 162 /// 163 /// and afterwards: 164 /// 165 /// RAW: 166 /// { S0[] -> S1[i0] : i0 >= 0 and i0 <= 1023; 167 /// S1[i0] -> S2[] : i0 >= 0 and i0 <= 1023} 168 /// WAR: 169 /// { } 170 /// WAW: 171 /// { S0[] -> S1[i0] : i0 >= 0 and i0 <= 1023; 172 /// S1[i0] -> S2[] : i0 >= 0 and i0 <= 1023} 173 /// RED: 174 /// { S1[i0] -> S1[1 + i0] : i0 >= 0 and i0 <= 1022 } 175 /// 176 /// Note: This function also computes the (reverse) transitive closure of the 177 /// reduction dependences. 178 void Dependences::addPrivatizationDependences() { 179 isl_union_map *PrivRAW, *PrivWAW, *PrivWAR; 180 181 // The transitive closure might be over approximated, thus could lead to 182 // dependency cycles in the privatization dependences. To make sure this 183 // will not happen we remove all negative dependences after we computed 184 // the transitive closure. 185 TC_RED = isl_union_map_transitive_closure(isl_union_map_copy(RED), 0); 186 187 // FIXME: Apply the current schedule instead of assuming the identity schedule 188 // here. The current approach is only valid as long as we compute the 189 // dependences only with the initial (identity schedule). Any other 190 // schedule could change "the direction of the backward dependences" we 191 // want to eliminate here. 192 isl_union_set *UDeltas = isl_union_map_deltas(isl_union_map_copy(TC_RED)); 193 isl_union_set *Universe = isl_union_set_universe(isl_union_set_copy(UDeltas)); 194 isl_union_set *Zero = isl_union_set_empty(isl_union_set_get_space(Universe)); 195 isl_union_set_foreach_set(Universe, fixSetToZero, &Zero); 196 isl_union_map *NonPositive = isl_union_set_lex_le_union_set(UDeltas, Zero); 197 198 TC_RED = isl_union_map_subtract(TC_RED, NonPositive); 199 200 TC_RED = isl_union_map_union( 201 TC_RED, isl_union_map_reverse(isl_union_map_copy(TC_RED))); 202 TC_RED = isl_union_map_coalesce(TC_RED); 203 204 isl_union_map **Maps[] = {&RAW, &WAW, &WAR}; 205 isl_union_map **PrivMaps[] = {&PrivRAW, &PrivWAW, &PrivWAR}; 206 for (unsigned u = 0; u < 3; u++) { 207 isl_union_map **Map = Maps[u], **PrivMap = PrivMaps[u]; 208 209 *PrivMap = isl_union_map_apply_range(isl_union_map_copy(*Map), 210 isl_union_map_copy(TC_RED)); 211 *PrivMap = isl_union_map_union( 212 *PrivMap, isl_union_map_apply_range(isl_union_map_copy(TC_RED), 213 isl_union_map_copy(*Map))); 214 215 *Map = isl_union_map_union(*Map, *PrivMap); 216 } 217 218 isl_union_set_free(Universe); 219 } 220 221 void Dependences::calculateDependences(Scop &S) { 222 isl_union_map *Read, *Write, *MayWrite, *AccessSchedule, *StmtSchedule, 223 *ScheduleMap; 224 225 DEBUG(dbgs() << "Scop: \n" << S << "\n"); 226 227 collectInfo(S, &Read, &Write, &MayWrite, &AccessSchedule, &StmtSchedule); 228 229 ScheduleMap = 230 isl_union_map_union(AccessSchedule, isl_union_map_copy(StmtSchedule)); 231 232 Read = isl_union_map_coalesce(Read); 233 Write = isl_union_map_coalesce(Write); 234 MayWrite = isl_union_map_coalesce(MayWrite); 235 236 long MaxOpsOld = isl_ctx_get_max_operations(S.getIslCtx()); 237 if (OptComputeOut) 238 isl_ctx_set_max_operations(S.getIslCtx(), OptComputeOut); 239 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_CONTINUE); 240 241 DEBUG(dbgs() << "Read: " << Read << "\n"; 242 dbgs() << "Write: " << Write << "\n"; 243 dbgs() << "MayWrite: " << MayWrite << "\n"; 244 dbgs() << "Schedule: " << ScheduleMap << "\n"); 245 246 RAW = WAW = WAR = RED = nullptr; 247 248 auto *Schedule = isl_schedule_from_domain( 249 isl_union_map_domain(isl_union_map_copy(ScheduleMap))); 250 Schedule = isl_schedule_insert_partial_schedule( 251 Schedule, isl_multi_union_pw_aff_from_union_map(ScheduleMap)); 252 253 if (OptAnalysisType == VALUE_BASED_ANALYSIS) { 254 isl_union_access_info *AI; 255 isl_union_flow *Flow; 256 257 AI = isl_union_access_info_from_sink(isl_union_map_copy(Read)); 258 AI = isl_union_access_info_set_must_source(AI, isl_union_map_copy(Write)); 259 AI = isl_union_access_info_set_may_source(AI, isl_union_map_copy(MayWrite)); 260 AI = isl_union_access_info_set_schedule(AI, isl_schedule_copy(Schedule)); 261 Flow = isl_union_access_info_compute_flow(AI); 262 263 RAW = isl_union_flow_get_must_dependence(Flow); 264 isl_union_flow_free(Flow); 265 266 AI = isl_union_access_info_from_sink(isl_union_map_copy(Write)); 267 AI = isl_union_access_info_set_must_source(AI, isl_union_map_copy(Write)); 268 AI = isl_union_access_info_set_may_source(AI, isl_union_map_copy(Read)); 269 AI = isl_union_access_info_set_schedule(AI, Schedule); 270 Flow = isl_union_access_info_compute_flow(AI); 271 272 WAW = isl_union_flow_get_must_dependence(Flow); 273 WAR = isl_union_flow_get_may_dependence(Flow); 274 275 // This subtraction is needed to obtain the same results as were given by 276 // isl_union_map_compute_flow. For large sets this may add some compile-time 277 // cost. As there does not seem to be a need to distinguish between WAW and 278 // WAR, refactoring Polly to only track general non-flow dependences may 279 // improve performance. 280 WAR = isl_union_map_subtract(WAR, isl_union_map_copy(WAW)); 281 isl_union_flow_free(Flow); 282 } else { 283 isl_union_access_info *AI; 284 isl_union_flow *Flow; 285 286 Write = isl_union_map_union(Write, isl_union_map_copy(MayWrite)); 287 288 AI = isl_union_access_info_from_sink(isl_union_map_copy(Read)); 289 AI = isl_union_access_info_set_may_source(AI, isl_union_map_copy(Write)); 290 AI = isl_union_access_info_set_schedule(AI, isl_schedule_copy(Schedule)); 291 Flow = isl_union_access_info_compute_flow(AI); 292 293 RAW = isl_union_flow_get_may_dependence(Flow); 294 isl_union_flow_free(Flow); 295 296 AI = isl_union_access_info_from_sink(isl_union_map_copy(Write)); 297 AI = isl_union_access_info_set_may_source(AI, isl_union_map_copy(Read)); 298 AI = isl_union_access_info_set_schedule(AI, isl_schedule_copy(Schedule)); 299 Flow = isl_union_access_info_compute_flow(AI); 300 301 WAR = isl_union_flow_get_may_dependence(Flow); 302 isl_union_flow_free(Flow); 303 304 AI = isl_union_access_info_from_sink(isl_union_map_copy(Write)); 305 AI = isl_union_access_info_set_may_source(AI, isl_union_map_copy(Write)); 306 AI = isl_union_access_info_set_schedule(AI, Schedule); 307 Flow = isl_union_access_info_compute_flow(AI); 308 309 WAW = isl_union_flow_get_may_dependence(Flow); 310 isl_union_flow_free(Flow); 311 } 312 313 isl_union_map_free(MayWrite); 314 isl_union_map_free(Write); 315 isl_union_map_free(Read); 316 317 RAW = isl_union_map_coalesce(RAW); 318 WAW = isl_union_map_coalesce(WAW); 319 WAR = isl_union_map_coalesce(WAR); 320 321 if (isl_ctx_last_error(S.getIslCtx()) == isl_error_quota) { 322 isl_union_map_free(RAW); 323 isl_union_map_free(WAW); 324 isl_union_map_free(WAR); 325 RAW = WAW = WAR = nullptr; 326 isl_ctx_reset_error(S.getIslCtx()); 327 } 328 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_ABORT); 329 isl_ctx_reset_operations(S.getIslCtx()); 330 isl_ctx_set_max_operations(S.getIslCtx(), MaxOpsOld); 331 332 isl_union_map *STMT_RAW, *STMT_WAW, *STMT_WAR; 333 STMT_RAW = isl_union_map_intersect_domain( 334 isl_union_map_copy(RAW), 335 isl_union_map_domain(isl_union_map_copy(StmtSchedule))); 336 STMT_WAW = isl_union_map_intersect_domain( 337 isl_union_map_copy(WAW), 338 isl_union_map_domain(isl_union_map_copy(StmtSchedule))); 339 STMT_WAR = isl_union_map_intersect_domain(isl_union_map_copy(WAR), 340 isl_union_map_domain(StmtSchedule)); 341 DEBUG({ 342 dbgs() << "Wrapped Dependences:\n"; 343 dump(); 344 dbgs() << "\n"; 345 }); 346 347 // To handle reduction dependences we proceed as follows: 348 // 1) Aggregate all possible reduction dependences, namely all self 349 // dependences on reduction like statements. 350 // 2) Intersect them with the actual RAW & WAW dependences to the get the 351 // actual reduction dependences. This will ensure the load/store memory 352 // addresses were __identical__ in the two iterations of the statement. 353 // 3) Relax the original RAW and WAW dependences by substracting the actual 354 // reduction dependences. Binary reductions (sum += A[i]) cause both, and 355 // the same, RAW and WAW dependences. 356 // 4) Add the privatization dependences which are widened versions of 357 // already present dependences. They model the effect of manual 358 // privatization at the outermost possible place (namely after the last 359 // write and before the first access to a reduction location). 360 361 // Step 1) 362 RED = isl_union_map_empty(isl_union_map_get_space(RAW)); 363 for (ScopStmt *Stmt : S) { 364 for (MemoryAccess *MA : *Stmt) { 365 if (!MA->isReductionLike()) 366 continue; 367 isl_set *AccDomW = isl_map_wrap(MA->getAccessRelation()); 368 isl_map *Identity = 369 isl_map_from_domain_and_range(isl_set_copy(AccDomW), AccDomW); 370 RED = isl_union_map_add_map(RED, Identity); 371 } 372 } 373 374 // Step 2) 375 RED = isl_union_map_intersect(RED, isl_union_map_copy(RAW)); 376 RED = isl_union_map_intersect(RED, isl_union_map_copy(WAW)); 377 378 if (!isl_union_map_is_empty(RED)) { 379 380 // Step 3) 381 RAW = isl_union_map_subtract(RAW, isl_union_map_copy(RED)); 382 WAW = isl_union_map_subtract(WAW, isl_union_map_copy(RED)); 383 384 // Step 4) 385 addPrivatizationDependences(); 386 } 387 388 DEBUG({ 389 dbgs() << "Final Wrapped Dependences:\n"; 390 dump(); 391 dbgs() << "\n"; 392 }); 393 394 // RED_SIN is used to collect all reduction dependences again after we 395 // split them according to the causing memory accesses. The current assumption 396 // is that our method of splitting will not have any leftovers. In the end 397 // we validate this assumption until we have more confidence in this method. 398 isl_union_map *RED_SIN = isl_union_map_empty(isl_union_map_get_space(RAW)); 399 400 // For each reduction like memory access, check if there are reduction 401 // dependences with the access relation of the memory access as a domain 402 // (wrapped space!). If so these dependences are caused by this memory access. 403 // We then move this portion of reduction dependences back to the statement -> 404 // statement space and add a mapping from the memory access to these 405 // dependences. 406 for (ScopStmt *Stmt : S) { 407 for (MemoryAccess *MA : *Stmt) { 408 if (!MA->isReductionLike()) 409 continue; 410 411 isl_set *AccDomW = isl_map_wrap(MA->getAccessRelation()); 412 isl_union_map *AccRedDepU = isl_union_map_intersect_domain( 413 isl_union_map_copy(TC_RED), isl_union_set_from_set(AccDomW)); 414 if (isl_union_map_is_empty(AccRedDepU) && !isl_union_map_free(AccRedDepU)) 415 continue; 416 417 isl_map *AccRedDep = isl_map_from_union_map(AccRedDepU); 418 RED_SIN = isl_union_map_add_map(RED_SIN, isl_map_copy(AccRedDep)); 419 AccRedDep = isl_map_zip(AccRedDep); 420 AccRedDep = isl_set_unwrap(isl_map_domain(AccRedDep)); 421 setReductionDependences(MA, AccRedDep); 422 } 423 } 424 425 assert(isl_union_map_is_equal(RED_SIN, TC_RED) && 426 "Intersecting the reduction dependence domain with the wrapped access " 427 "relation is not enough, we need to loosen the access relation also"); 428 isl_union_map_free(RED_SIN); 429 430 RAW = isl_union_map_zip(RAW); 431 WAW = isl_union_map_zip(WAW); 432 WAR = isl_union_map_zip(WAR); 433 RED = isl_union_map_zip(RED); 434 TC_RED = isl_union_map_zip(TC_RED); 435 436 DEBUG({ 437 dbgs() << "Zipped Dependences:\n"; 438 dump(); 439 dbgs() << "\n"; 440 }); 441 442 RAW = isl_union_set_unwrap(isl_union_map_domain(RAW)); 443 WAW = isl_union_set_unwrap(isl_union_map_domain(WAW)); 444 WAR = isl_union_set_unwrap(isl_union_map_domain(WAR)); 445 RED = isl_union_set_unwrap(isl_union_map_domain(RED)); 446 TC_RED = isl_union_set_unwrap(isl_union_map_domain(TC_RED)); 447 448 DEBUG({ 449 dbgs() << "Unwrapped Dependences:\n"; 450 dump(); 451 dbgs() << "\n"; 452 }); 453 454 RAW = isl_union_map_union(RAW, STMT_RAW); 455 WAW = isl_union_map_union(WAW, STMT_WAW); 456 WAR = isl_union_map_union(WAR, STMT_WAR); 457 458 RAW = isl_union_map_coalesce(RAW); 459 WAW = isl_union_map_coalesce(WAW); 460 WAR = isl_union_map_coalesce(WAR); 461 RED = isl_union_map_coalesce(RED); 462 TC_RED = isl_union_map_coalesce(TC_RED); 463 464 DEBUG(dump()); 465 } 466 467 bool Dependences::isValidSchedule(Scop &S, 468 StatementToIslMapTy *NewSchedule) const { 469 if (LegalityCheckDisabled) 470 return true; 471 472 isl_union_map *Dependences = getDependences(TYPE_RAW | TYPE_WAW | TYPE_WAR); 473 isl_space *Space = S.getParamSpace(); 474 isl_union_map *Schedule = isl_union_map_empty(Space); 475 476 isl_space *ScheduleSpace = nullptr; 477 478 for (ScopStmt *Stmt : S) { 479 isl_map *StmtScat; 480 481 if (NewSchedule->find(Stmt) == NewSchedule->end()) 482 StmtScat = Stmt->getSchedule(); 483 else 484 StmtScat = isl_map_copy((*NewSchedule)[Stmt]); 485 486 if (!ScheduleSpace) 487 ScheduleSpace = isl_space_range(isl_map_get_space(StmtScat)); 488 489 Schedule = isl_union_map_add_map(Schedule, StmtScat); 490 } 491 492 Dependences = 493 isl_union_map_apply_domain(Dependences, isl_union_map_copy(Schedule)); 494 Dependences = isl_union_map_apply_range(Dependences, Schedule); 495 496 isl_set *Zero = isl_set_universe(isl_space_copy(ScheduleSpace)); 497 for (unsigned i = 0; i < isl_set_dim(Zero, isl_dim_set); i++) 498 Zero = isl_set_fix_si(Zero, isl_dim_set, i, 0); 499 500 isl_union_set *UDeltas = isl_union_map_deltas(Dependences); 501 isl_set *Deltas = isl_union_set_extract_set(UDeltas, ScheduleSpace); 502 isl_union_set_free(UDeltas); 503 504 isl_map *NonPositive = isl_set_lex_le_set(Deltas, Zero); 505 bool IsValid = isl_map_is_empty(NonPositive); 506 isl_map_free(NonPositive); 507 508 return IsValid; 509 } 510 511 // Check if the current scheduling dimension is parallel. 512 // 513 // We check for parallelism by verifying that the loop does not carry any 514 // dependences. 515 // 516 // Parallelism test: if the distance is zero in all outer dimensions, then it 517 // has to be zero in the current dimension as well. 518 // 519 // Implementation: first, translate dependences into time space, then force 520 // outer dimensions to be equal. If the distance is zero in the current 521 // dimension, then the loop is parallel. The distance is zero in the current 522 // dimension if it is a subset of a map with equal values for the current 523 // dimension. 524 bool Dependences::isParallel(isl_union_map *Schedule, isl_union_map *Deps, 525 isl_pw_aff **MinDistancePtr) const { 526 isl_set *Deltas, *Distance; 527 isl_map *ScheduleDeps; 528 unsigned Dimension; 529 bool IsParallel; 530 531 Deps = isl_union_map_apply_range(Deps, isl_union_map_copy(Schedule)); 532 Deps = isl_union_map_apply_domain(Deps, isl_union_map_copy(Schedule)); 533 534 if (isl_union_map_is_empty(Deps)) { 535 isl_union_map_free(Deps); 536 return true; 537 } 538 539 ScheduleDeps = isl_map_from_union_map(Deps); 540 Dimension = isl_map_dim(ScheduleDeps, isl_dim_out) - 1; 541 542 for (unsigned i = 0; i < Dimension; i++) 543 ScheduleDeps = isl_map_equate(ScheduleDeps, isl_dim_out, i, isl_dim_in, i); 544 545 Deltas = isl_map_deltas(ScheduleDeps); 546 Distance = isl_set_universe(isl_set_get_space(Deltas)); 547 548 // [0, ..., 0, +] - All zeros and last dimension larger than zero 549 for (unsigned i = 0; i < Dimension; i++) 550 Distance = isl_set_fix_si(Distance, isl_dim_set, i, 0); 551 552 Distance = isl_set_lower_bound_si(Distance, isl_dim_set, Dimension, 1); 553 Distance = isl_set_intersect(Distance, Deltas); 554 555 IsParallel = isl_set_is_empty(Distance); 556 if (IsParallel || !MinDistancePtr) { 557 isl_set_free(Distance); 558 return IsParallel; 559 } 560 561 Distance = isl_set_project_out(Distance, isl_dim_set, 0, Dimension); 562 Distance = isl_set_coalesce(Distance); 563 564 // This last step will compute a expression for the minimal value in the 565 // distance polyhedron Distance with regards to the first (outer most) 566 // dimension. 567 *MinDistancePtr = isl_pw_aff_coalesce(isl_set_dim_min(Distance, 0)); 568 569 return false; 570 } 571 572 static void printDependencyMap(raw_ostream &OS, __isl_keep isl_union_map *DM) { 573 if (DM) 574 OS << DM << "\n"; 575 else 576 OS << "n/a\n"; 577 } 578 579 void Dependences::print(raw_ostream &OS) const { 580 OS << "\tRAW dependences:\n\t\t"; 581 printDependencyMap(OS, RAW); 582 OS << "\tWAR dependences:\n\t\t"; 583 printDependencyMap(OS, WAR); 584 OS << "\tWAW dependences:\n\t\t"; 585 printDependencyMap(OS, WAW); 586 OS << "\tReduction dependences:\n\t\t"; 587 printDependencyMap(OS, RED); 588 OS << "\tTransitive closure of reduction dependences:\n\t\t"; 589 printDependencyMap(OS, TC_RED); 590 } 591 592 void Dependences::dump() const { print(dbgs()); } 593 594 void Dependences::releaseMemory() { 595 isl_union_map_free(RAW); 596 isl_union_map_free(WAR); 597 isl_union_map_free(WAW); 598 isl_union_map_free(RED); 599 isl_union_map_free(TC_RED); 600 601 RED = RAW = WAR = WAW = TC_RED = nullptr; 602 603 for (auto &ReductionDeps : ReductionDependences) 604 isl_map_free(ReductionDeps.second); 605 ReductionDependences.clear(); 606 } 607 608 isl_union_map *Dependences::getDependences(int Kinds) const { 609 assert(hasValidDependences() && "No valid dependences available"); 610 isl_space *Space = isl_union_map_get_space(RAW); 611 isl_union_map *Deps = isl_union_map_empty(Space); 612 613 if (Kinds & TYPE_RAW) 614 Deps = isl_union_map_union(Deps, isl_union_map_copy(RAW)); 615 616 if (Kinds & TYPE_WAR) 617 Deps = isl_union_map_union(Deps, isl_union_map_copy(WAR)); 618 619 if (Kinds & TYPE_WAW) 620 Deps = isl_union_map_union(Deps, isl_union_map_copy(WAW)); 621 622 if (Kinds & TYPE_RED) 623 Deps = isl_union_map_union(Deps, isl_union_map_copy(RED)); 624 625 if (Kinds & TYPE_TC_RED) 626 Deps = isl_union_map_union(Deps, isl_union_map_copy(TC_RED)); 627 628 Deps = isl_union_map_coalesce(Deps); 629 Deps = isl_union_map_detect_equalities(Deps); 630 return Deps; 631 } 632 633 bool Dependences::hasValidDependences() const { 634 return (RAW != nullptr) && (WAR != nullptr) && (WAW != nullptr); 635 } 636 637 isl_map *Dependences::getReductionDependences(MemoryAccess *MA) const { 638 return isl_map_copy(ReductionDependences.lookup(MA)); 639 } 640 641 void Dependences::setReductionDependences(MemoryAccess *MA, isl_map *D) { 642 assert(ReductionDependences.count(MA) == 0 && 643 "Reduction dependences set twice!"); 644 ReductionDependences[MA] = D; 645 } 646 647 void DependenceInfo::recomputeDependences() { 648 releaseMemory(); 649 D.calculateDependences(*S); 650 } 651 652 bool DependenceInfo::runOnScop(Scop &ScopVar) { 653 S = &ScopVar; 654 recomputeDependences(); 655 return false; 656 } 657 658 void DependenceInfo::getAnalysisUsage(AnalysisUsage &AU) const { 659 ScopPass::getAnalysisUsage(AU); 660 } 661 662 char DependenceInfo::ID = 0; 663 664 Pass *polly::createDependenceInfoPass() { return new DependenceInfo(); } 665 666 INITIALIZE_PASS_BEGIN(DependenceInfo, "polly-dependences", 667 "Polly - Calculate dependences", false, false); 668 INITIALIZE_PASS_DEPENDENCY(ScopInfo); 669 INITIALIZE_PASS_END(DependenceInfo, "polly-dependences", 670 "Polly - Calculate dependences", false, false) 671