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