DependenceInfo.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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/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"
  38. using namespace polly;
  39. using namespace llvm;
  40. #define DEBUG_TYPE "polly-dependence"
  41. static cl::opt<int> OptComputeOut(
  42. "polly-dependences-computeout",
  43. cl::desc("Bound the dependence analysis by a maximal amount of "
  44. "computational steps (0 means no bound)"),
  45. cl::Hidden, cl::init(500000), cl::ZeroOrMore, cl::cat(PollyCategory));
  46. static cl::opt<bool> LegalityCheckDisabled(
  47. "disable-polly-legality", cl::desc("Disable polly legality check"),
  48. cl::Hidden, cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
  49. static cl::opt<bool>
  50. UseReductions("polly-dependences-use-reductions",
  51. cl::desc("Exploit reductions in dependence analysis"),
  52. cl::Hidden, cl::init(true), cl::ZeroOrMore,
  53. cl::cat(PollyCategory));
  54. enum AnalysisType { VALUE_BASED_ANALYSIS, MEMORY_BASED_ANALYSIS };
  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. cl::Hidden, cl::init(VALUE_BASED_ANALYSIS), cl::ZeroOrMore,
  63. cl::cat(PollyCategory));
  64. static cl::opt<Dependences::AnalysisLevel> OptAnalysisLevel(
  65. "polly-dependences-analysis-level",
  66. cl::desc("The level of dependence analysis"),
  67. cl::values(clEnumValN(Dependences::AL_Statement, "statement-wise",
  68. "Statement-level analysis"),
  69. clEnumValN(Dependences::AL_Reference, "reference-wise",
  70. "Memory reference level analysis that distinguish"
  71. " accessed references in the same statement"),
  72. clEnumValN(Dependences::AL_Access, "access-wise",
  73. "Memory reference level analysis that distinguish"
  74. " access instructions in the same statement")),
  75. cl::Hidden, cl::init(Dependences::AL_Statement), cl::ZeroOrMore,
  76. cl::cat(PollyCategory));
  77. //===----------------------------------------------------------------------===//
  78. /// Tag the @p Relation domain with @p TagId
  79. static __isl_give isl_map *tag(__isl_take isl_map *Relation,
  80. __isl_take isl_id *TagId) {
  81. isl_space *Space = isl_map_get_space(Relation);
  82. Space = isl_space_drop_dims(Space, isl_dim_out, 0,
  83. isl_map_dim(Relation, isl_dim_out));
  84. Space = isl_space_set_tuple_id(Space, isl_dim_out, TagId);
  85. isl_multi_aff *Tag = isl_multi_aff_domain_map(Space);
  86. Relation = isl_map_preimage_domain_multi_aff(Relation, Tag);
  87. return Relation;
  88. }
  89. /// Tag the @p Relation domain with either MA->getArrayId() or
  90. /// MA->getId() based on @p TagLevel
  91. static __isl_give isl_map *tag(__isl_take isl_map *Relation, MemoryAccess *MA,
  92. Dependences::AnalysisLevel TagLevel) {
  93. if (TagLevel == Dependences::AL_Reference)
  94. return tag(Relation, MA->getArrayId().release());
  95. if (TagLevel == Dependences::AL_Access)
  96. return tag(Relation, MA->getId().release());
  97. // No need to tag at the statement level.
  98. return Relation;
  99. }
  100. /// Collect information about the SCoP @p S.
  101. static void collectInfo(Scop &S, isl_union_map *&Read,
  102. isl_union_map *&MustWrite, isl_union_map *&MayWrite,
  103. isl_union_map *&ReductionTagMap,
  104. isl_union_set *&TaggedStmtDomain,
  105. Dependences::AnalysisLevel Level) {
  106. isl_space *Space = S.getParamSpace().release();
  107. Read = isl_union_map_empty(isl_space_copy(Space));
  108. MustWrite = isl_union_map_empty(isl_space_copy(Space));
  109. MayWrite = isl_union_map_empty(isl_space_copy(Space));
  110. ReductionTagMap = isl_union_map_empty(isl_space_copy(Space));
  111. isl_union_map *StmtSchedule = isl_union_map_empty(Space);
  112. SmallPtrSet<const ScopArrayInfo *, 8> ReductionArrays;
  113. if (UseReductions)
  114. for (ScopStmt &Stmt : S)
  115. for (MemoryAccess *MA : Stmt)
  116. if (MA->isReductionLike())
  117. ReductionArrays.insert(MA->getScopArrayInfo());
  118. for (ScopStmt &Stmt : S) {
  119. for (MemoryAccess *MA : Stmt) {
  120. isl_set *domcp = Stmt.getDomain().release();
  121. isl_map *accdom = MA->getAccessRelation().release();
  122. accdom = isl_map_intersect_domain(accdom, domcp);
  123. if (ReductionArrays.count(MA->getScopArrayInfo())) {
  124. // Wrap the access domain and adjust the schedule accordingly.
  125. //
  126. // An access domain like
  127. // Stmt[i0, i1] -> MemAcc_A[i0 + i1]
  128. // will be transformed into
  129. // [Stmt[i0, i1] -> MemAcc_A[i0 + i1]] -> MemAcc_A[i0 + i1]
  130. //
  131. // We collect all the access domains in the ReductionTagMap.
  132. // This is used in Dependences::calculateDependences to create
  133. // a tagged Schedule tree.
  134. ReductionTagMap =
  135. isl_union_map_add_map(ReductionTagMap, isl_map_copy(accdom));
  136. accdom = isl_map_range_map(accdom);
  137. } else {
  138. accdom = tag(accdom, MA, Level);
  139. if (Level > Dependences::AL_Statement) {
  140. isl_map *StmtScheduleMap = Stmt.getSchedule().release();
  141. assert(StmtScheduleMap &&
  142. "Schedules that contain extension nodes require special "
  143. "handling.");
  144. isl_map *Schedule = tag(StmtScheduleMap, MA, Level);
  145. StmtSchedule = isl_union_map_add_map(StmtSchedule, Schedule);
  146. }
  147. }
  148. if (MA->isRead())
  149. Read = isl_union_map_add_map(Read, accdom);
  150. else if (MA->isMayWrite())
  151. MayWrite = isl_union_map_add_map(MayWrite, accdom);
  152. else
  153. MustWrite = isl_union_map_add_map(MustWrite, accdom);
  154. }
  155. if (!ReductionArrays.empty() && Level == Dependences::AL_Statement)
  156. StmtSchedule =
  157. isl_union_map_add_map(StmtSchedule, Stmt.getSchedule().release());
  158. }
  159. StmtSchedule = isl_union_map_intersect_params(
  160. StmtSchedule, S.getAssumedContext().release());
  161. TaggedStmtDomain = isl_union_map_domain(StmtSchedule);
  162. ReductionTagMap = isl_union_map_coalesce(ReductionTagMap);
  163. Read = isl_union_map_coalesce(Read);
  164. MustWrite = isl_union_map_coalesce(MustWrite);
  165. MayWrite = isl_union_map_coalesce(MayWrite);
  166. }
  167. /// Fix all dimension of @p Zero to 0 and add it to @p user
  168. static void fixSetToZero(isl::set Zero, isl::union_set *User) {
  169. for (auto i : rangeIslSize(0, Zero.tuple_dim()))
  170. Zero = Zero.fix_si(isl::dim::set, i, 0);
  171. *User = User->unite(Zero);
  172. }
  173. /// Compute the privatization dependences for a given dependency @p Map
  174. ///
  175. /// Privatization dependences are widened original dependences which originate
  176. /// or end in a reduction access. To compute them we apply the transitive close
  177. /// of the reduction dependences (which maps each iteration of a reduction
  178. /// statement to all following ones) on the RAW/WAR/WAW dependences. The
  179. /// dependences which start or end at a reduction statement will be extended to
  180. /// depend on all following reduction statement iterations as well.
  181. /// Note: "Following" here means according to the reduction dependences.
  182. ///
  183. /// For the input:
  184. ///
  185. /// S0: *sum = 0;
  186. /// for (int i = 0; i < 1024; i++)
  187. /// S1: *sum += i;
  188. /// S2: *sum = *sum * 3;
  189. ///
  190. /// we have the following dependences before we add privatization dependences:
  191. ///
  192. /// RAW:
  193. /// { S0[] -> S1[0]; S1[1023] -> S2[] }
  194. /// WAR:
  195. /// { }
  196. /// WAW:
  197. /// { S0[] -> S1[0]; S1[1024] -> S2[] }
  198. /// RED:
  199. /// { S1[i0] -> S1[1 + i0] : i0 >= 0 and i0 <= 1022 }
  200. ///
  201. /// and afterwards:
  202. ///
  203. /// RAW:
  204. /// { S0[] -> S1[i0] : i0 >= 0 and i0 <= 1023;
  205. /// S1[i0] -> S2[] : i0 >= 0 and i0 <= 1023}
  206. /// WAR:
  207. /// { }
  208. /// WAW:
  209. /// { S0[] -> S1[i0] : i0 >= 0 and i0 <= 1023;
  210. /// S1[i0] -> S2[] : i0 >= 0 and i0 <= 1023}
  211. /// RED:
  212. /// { S1[i0] -> S1[1 + i0] : i0 >= 0 and i0 <= 1022 }
  213. ///
  214. /// Note: This function also computes the (reverse) transitive closure of the
  215. /// reduction dependences.
  216. void Dependences::addPrivatizationDependences() {
  217. isl_union_map *PrivRAW, *PrivWAW, *PrivWAR;
  218. // The transitive closure might be over approximated, thus could lead to
  219. // dependency cycles in the privatization dependences. To make sure this
  220. // will not happen we remove all negative dependences after we computed
  221. // the transitive closure.
  222. TC_RED = isl_union_map_transitive_closure(isl_union_map_copy(RED), nullptr);
  223. // FIXME: Apply the current schedule instead of assuming the identity schedule
  224. // here. The current approach is only valid as long as we compute the
  225. // dependences only with the initial (identity schedule). Any other
  226. // schedule could change "the direction of the backward dependences" we
  227. // want to eliminate here.
  228. isl_union_set *UDeltas = isl_union_map_deltas(isl_union_map_copy(TC_RED));
  229. isl_union_set *Universe = isl_union_set_universe(isl_union_set_copy(UDeltas));
  230. isl::union_set Zero =
  231. isl::manage(isl_union_set_empty(isl_union_set_get_space(Universe)));
  232. for (isl::set Set : isl::manage_copy(Universe).get_set_list())
  233. fixSetToZero(Set, &Zero);
  234. isl_union_map *NonPositive =
  235. isl_union_set_lex_le_union_set(UDeltas, Zero.release());
  236. TC_RED = isl_union_map_subtract(TC_RED, NonPositive);
  237. TC_RED = isl_union_map_union(
  238. TC_RED, isl_union_map_reverse(isl_union_map_copy(TC_RED)));
  239. TC_RED = isl_union_map_coalesce(TC_RED);
  240. isl_union_map **Maps[] = {&RAW, &WAW, &WAR};
  241. isl_union_map **PrivMaps[] = {&PrivRAW, &PrivWAW, &PrivWAR};
  242. for (unsigned u = 0; u < 3; u++) {
  243. isl_union_map **Map = Maps[u], **PrivMap = PrivMaps[u];
  244. *PrivMap = isl_union_map_apply_range(isl_union_map_copy(*Map),
  245. isl_union_map_copy(TC_RED));
  246. *PrivMap = isl_union_map_union(
  247. *PrivMap, isl_union_map_apply_range(isl_union_map_copy(TC_RED),
  248. isl_union_map_copy(*Map)));
  249. *Map = isl_union_map_union(*Map, *PrivMap);
  250. }
  251. isl_union_set_free(Universe);
  252. }
  253. static __isl_give isl_union_flow *buildFlow(__isl_keep isl_union_map *Snk,
  254. __isl_keep isl_union_map *Src,
  255. __isl_keep isl_union_map *MaySrc,
  256. __isl_keep isl_union_map *Kill,
  257. __isl_keep isl_schedule *Schedule) {
  258. isl_union_access_info *AI;
  259. AI = isl_union_access_info_from_sink(isl_union_map_copy(Snk));
  260. if (MaySrc)
  261. AI = isl_union_access_info_set_may_source(AI, isl_union_map_copy(MaySrc));
  262. if (Src)
  263. AI = isl_union_access_info_set_must_source(AI, isl_union_map_copy(Src));
  264. if (Kill)
  265. AI = isl_union_access_info_set_kill(AI, isl_union_map_copy(Kill));
  266. AI = isl_union_access_info_set_schedule(AI, isl_schedule_copy(Schedule));
  267. auto Flow = isl_union_access_info_compute_flow(AI);
  268. LLVM_DEBUG(if (!Flow) dbgs()
  269. << "last error: "
  270. << isl_ctx_last_error(isl_schedule_get_ctx(Schedule))
  271. << '\n';);
  272. return Flow;
  273. }
  274. void Dependences::calculateDependences(Scop &S) {
  275. isl_union_map *Read, *MustWrite, *MayWrite, *ReductionTagMap;
  276. isl_schedule *Schedule;
  277. isl_union_set *TaggedStmtDomain;
  278. LLVM_DEBUG(dbgs() << "Scop: \n" << S << "\n");
  279. collectInfo(S, Read, MustWrite, MayWrite, ReductionTagMap, TaggedStmtDomain,
  280. Level);
  281. bool HasReductions = !isl_union_map_is_empty(ReductionTagMap);
  282. LLVM_DEBUG(dbgs() << "Read: " << Read << '\n';
  283. dbgs() << "MustWrite: " << MustWrite << '\n';
  284. dbgs() << "MayWrite: " << MayWrite << '\n';
  285. dbgs() << "ReductionTagMap: " << ReductionTagMap << '\n';
  286. dbgs() << "TaggedStmtDomain: " << TaggedStmtDomain << '\n';);
  287. Schedule = S.getScheduleTree().release();
  288. if (!HasReductions) {
  289. isl_union_map_free(ReductionTagMap);
  290. // Tag the schedule tree if we want fine-grain dependence info
  291. if (Level > AL_Statement) {
  292. auto TaggedMap =
  293. isl_union_set_unwrap(isl_union_set_copy(TaggedStmtDomain));
  294. auto Tags = isl_union_map_domain_map_union_pw_multi_aff(TaggedMap);
  295. Schedule = isl_schedule_pullback_union_pw_multi_aff(Schedule, Tags);
  296. }
  297. } else {
  298. isl_union_map *IdentityMap;
  299. isl_union_pw_multi_aff *ReductionTags, *IdentityTags, *Tags;
  300. // Extract Reduction tags from the combined access domains in the given
  301. // SCoP. The result is a map that maps each tagged element in the domain to
  302. // the memory location it accesses. ReductionTags = {[Stmt[i] ->
  303. // Array[f(i)]] -> Stmt[i] }
  304. ReductionTags =
  305. isl_union_map_domain_map_union_pw_multi_aff(ReductionTagMap);
  306. // Compute an identity map from each statement in domain to itself.
  307. // IdentityTags = { [Stmt[i] -> Stmt[i] }
  308. IdentityMap = isl_union_set_identity(isl_union_set_copy(TaggedStmtDomain));
  309. IdentityTags = isl_union_pw_multi_aff_from_union_map(IdentityMap);
  310. Tags = isl_union_pw_multi_aff_union_add(ReductionTags, IdentityTags);
  311. // By pulling back Tags from Schedule, we have a schedule tree that can
  312. // be used to compute normal dependences, as well as 'tagged' reduction
  313. // dependences.
  314. Schedule = isl_schedule_pullback_union_pw_multi_aff(Schedule, Tags);
  315. }
  316. LLVM_DEBUG(dbgs() << "Read: " << Read << "\n";
  317. dbgs() << "MustWrite: " << MustWrite << "\n";
  318. dbgs() << "MayWrite: " << MayWrite << "\n";
  319. dbgs() << "Schedule: " << Schedule << "\n");
  320. isl_union_map *StrictWAW = nullptr;
  321. {
  322. IslMaxOperationsGuard MaxOpGuard(IslCtx.get(), OptComputeOut);
  323. RAW = WAW = WAR = RED = nullptr;
  324. isl_union_map *Write = isl_union_map_union(isl_union_map_copy(MustWrite),
  325. isl_union_map_copy(MayWrite));
  326. // We are interested in detecting reductions that do not have intermediate
  327. // computations that are captured by other statements.
  328. //
  329. // Example:
  330. // void f(int *A, int *B) {
  331. // for(int i = 0; i <= 100; i++) {
  332. //
  333. // *-WAR (S0[i] -> S0[i + 1] 0 <= i <= 100)------------*
  334. // | |
  335. // *-WAW (S0[i] -> S0[i + 1] 0 <= i <= 100)------------*
  336. // | |
  337. // v |
  338. // S0: *A += i; >------------------*-----------------------*
  339. // |
  340. // if (i >= 98) { WAR (S0[i] -> S1[i]) 98 <= i <= 100
  341. // |
  342. // S1: *B = *A; <--------------*
  343. // }
  344. // }
  345. // }
  346. //
  347. // S0[0 <= i <= 100] has a reduction. However, the values in
  348. // S0[98 <= i <= 100] is captured in S1[98 <= i <= 100].
  349. // Since we allow free reordering on our reduction dependences, we need to
  350. // remove all instances of a reduction statement that have data dependences
  351. // originating from them.
  352. // In the case of the example, we need to remove S0[98 <= i <= 100] from
  353. // our reduction dependences.
  354. //
  355. // When we build up the WAW dependences that are used to detect reductions,
  356. // we consider only **Writes that have no intermediate Reads**.
  357. //
  358. // `isl_union_flow_get_must_dependence` gives us dependences of the form:
  359. // (sink <- must_source).
  360. //
  361. // It *will not give* dependences of the form:
  362. // 1. (sink <- ... <- may_source <- ... <- must_source)
  363. // 2. (sink <- ... <- must_source <- ... <- must_source)
  364. //
  365. // For a detailed reference on ISL's flow analysis, see:
  366. // "Presburger Formulas and Polyhedral Compilation" - Approximate Dataflow
  367. // Analysis.
  368. //
  369. // Since we set "Write" as a must-source, "Read" as a may-source, and ask
  370. // for must dependences, we get all Writes to Writes that **do not flow
  371. // through a Read**.
  372. //
  373. // ScopInfo::checkForReductions makes sure that if something captures
  374. // the reduction variable in the same basic block, then it is rejected
  375. // before it is even handed here. This makes sure that there is exactly
  376. // one read and one write to a reduction variable in a Statement.
  377. // Example:
  378. // void f(int *sum, int A[N], int B[N]) {
  379. // for (int i = 0; i < N; i++) {
  380. // *sum += A[i]; < the store and the load is not tagged as a
  381. // B[i] = *sum; < reduction-like access due to the overlap.
  382. // }
  383. // }
  384. isl_union_flow *Flow = buildFlow(Write, Write, Read, nullptr, Schedule);
  385. StrictWAW = isl_union_flow_get_must_dependence(Flow);
  386. isl_union_flow_free(Flow);
  387. if (OptAnalysisType == VALUE_BASED_ANALYSIS) {
  388. Flow = buildFlow(Read, MustWrite, MayWrite, nullptr, Schedule);
  389. RAW = isl_union_flow_get_may_dependence(Flow);
  390. isl_union_flow_free(Flow);
  391. Flow = buildFlow(Write, MustWrite, MayWrite, nullptr, Schedule);
  392. WAW = isl_union_flow_get_may_dependence(Flow);
  393. isl_union_flow_free(Flow);
  394. // ISL now supports "kills" in approximate dataflow analysis, we can
  395. // specify the MustWrite as kills, Read as source and Write as sink.
  396. Flow = buildFlow(Write, nullptr, Read, MustWrite, Schedule);
  397. WAR = isl_union_flow_get_may_dependence(Flow);
  398. isl_union_flow_free(Flow);
  399. } else {
  400. Flow = buildFlow(Read, nullptr, Write, nullptr, Schedule);
  401. RAW = isl_union_flow_get_may_dependence(Flow);
  402. isl_union_flow_free(Flow);
  403. Flow = buildFlow(Write, nullptr, Read, nullptr, Schedule);
  404. WAR = isl_union_flow_get_may_dependence(Flow);
  405. isl_union_flow_free(Flow);
  406. Flow = buildFlow(Write, nullptr, Write, nullptr, Schedule);
  407. WAW = isl_union_flow_get_may_dependence(Flow);
  408. isl_union_flow_free(Flow);
  409. }
  410. isl_union_map_free(Write);
  411. isl_union_map_free(MustWrite);
  412. isl_union_map_free(MayWrite);
  413. isl_union_map_free(Read);
  414. isl_schedule_free(Schedule);
  415. RAW = isl_union_map_coalesce(RAW);
  416. WAW = isl_union_map_coalesce(WAW);
  417. WAR = isl_union_map_coalesce(WAR);
  418. // End of max_operations scope.
  419. }
  420. if (isl_ctx_last_error(IslCtx.get()) == isl_error_quota) {
  421. isl_union_map_free(RAW);
  422. isl_union_map_free(WAW);
  423. isl_union_map_free(WAR);
  424. isl_union_map_free(StrictWAW);
  425. RAW = WAW = WAR = StrictWAW = nullptr;
  426. isl_ctx_reset_error(IslCtx.get());
  427. }
  428. // Drop out early, as the remaining computations are only needed for
  429. // reduction dependences or dependences that are finer than statement
  430. // level dependences.
  431. if (!HasReductions && Level == AL_Statement) {
  432. RED = isl_union_map_empty(isl_union_map_get_space(RAW));
  433. TC_RED = isl_union_map_empty(isl_union_set_get_space(TaggedStmtDomain));
  434. isl_union_set_free(TaggedStmtDomain);
  435. isl_union_map_free(StrictWAW);
  436. return;
  437. }
  438. isl_union_map *STMT_RAW, *STMT_WAW, *STMT_WAR;
  439. STMT_RAW = isl_union_map_intersect_domain(
  440. isl_union_map_copy(RAW), isl_union_set_copy(TaggedStmtDomain));
  441. STMT_WAW = isl_union_map_intersect_domain(
  442. isl_union_map_copy(WAW), isl_union_set_copy(TaggedStmtDomain));
  443. STMT_WAR =
  444. isl_union_map_intersect_domain(isl_union_map_copy(WAR), TaggedStmtDomain);
  445. LLVM_DEBUG({
  446. dbgs() << "Wrapped Dependences:\n";
  447. dump();
  448. dbgs() << "\n";
  449. });
  450. // To handle reduction dependences we proceed as follows:
  451. // 1) Aggregate all possible reduction dependences, namely all self
  452. // dependences on reduction like statements.
  453. // 2) Intersect them with the actual RAW & WAW dependences to the get the
  454. // actual reduction dependences. This will ensure the load/store memory
  455. // addresses were __identical__ in the two iterations of the statement.
  456. // 3) Relax the original RAW, WAW and WAR dependences by subtracting the
  457. // actual reduction dependences. Binary reductions (sum += A[i]) cause
  458. // the same, RAW, WAW and WAR dependences.
  459. // 4) Add the privatization dependences which are widened versions of
  460. // already present dependences. They model the effect of manual
  461. // privatization at the outermost possible place (namely after the last
  462. // write and before the first access to a reduction location).
  463. // Step 1)
  464. RED = isl_union_map_empty(isl_union_map_get_space(RAW));
  465. for (ScopStmt &Stmt : S) {
  466. for (MemoryAccess *MA : Stmt) {
  467. if (!MA->isReductionLike())
  468. continue;
  469. isl_set *AccDomW = isl_map_wrap(MA->getAccessRelation().release());
  470. isl_map *Identity =
  471. isl_map_from_domain_and_range(isl_set_copy(AccDomW), AccDomW);
  472. RED = isl_union_map_add_map(RED, Identity);
  473. }
  474. }
  475. // Step 2)
  476. RED = isl_union_map_intersect(RED, isl_union_map_copy(RAW));
  477. RED = isl_union_map_intersect(RED, StrictWAW);
  478. if (!isl_union_map_is_empty(RED)) {
  479. // Step 3)
  480. RAW = isl_union_map_subtract(RAW, isl_union_map_copy(RED));
  481. WAW = isl_union_map_subtract(WAW, isl_union_map_copy(RED));
  482. WAR = isl_union_map_subtract(WAR, isl_union_map_copy(RED));
  483. // Step 4)
  484. addPrivatizationDependences();
  485. } else
  486. TC_RED = isl_union_map_empty(isl_union_map_get_space(RED));
  487. LLVM_DEBUG({
  488. dbgs() << "Final Wrapped Dependences:\n";
  489. dump();
  490. dbgs() << "\n";
  491. });
  492. // RED_SIN is used to collect all reduction dependences again after we
  493. // split them according to the causing memory accesses. The current assumption
  494. // is that our method of splitting will not have any leftovers. In the end
  495. // we validate this assumption until we have more confidence in this method.
  496. isl_union_map *RED_SIN = isl_union_map_empty(isl_union_map_get_space(RAW));
  497. // For each reduction like memory access, check if there are reduction
  498. // dependences with the access relation of the memory access as a domain
  499. // (wrapped space!). If so these dependences are caused by this memory access.
  500. // We then move this portion of reduction dependences back to the statement ->
  501. // statement space and add a mapping from the memory access to these
  502. // dependences.
  503. for (ScopStmt &Stmt : S) {
  504. for (MemoryAccess *MA : Stmt) {
  505. if (!MA->isReductionLike())
  506. continue;
  507. isl_set *AccDomW = isl_map_wrap(MA->getAccessRelation().release());
  508. isl_union_map *AccRedDepU = isl_union_map_intersect_domain(
  509. isl_union_map_copy(TC_RED), isl_union_set_from_set(AccDomW));
  510. if (isl_union_map_is_empty(AccRedDepU)) {
  511. isl_union_map_free(AccRedDepU);
  512. continue;
  513. }
  514. isl_map *AccRedDep = isl_map_from_union_map(AccRedDepU);
  515. RED_SIN = isl_union_map_add_map(RED_SIN, isl_map_copy(AccRedDep));
  516. AccRedDep = isl_map_zip(AccRedDep);
  517. AccRedDep = isl_set_unwrap(isl_map_domain(AccRedDep));
  518. setReductionDependences(MA, AccRedDep);
  519. }
  520. }
  521. assert(isl_union_map_is_equal(RED_SIN, TC_RED) &&
  522. "Intersecting the reduction dependence domain with the wrapped access "
  523. "relation is not enough, we need to loosen the access relation also");
  524. isl_union_map_free(RED_SIN);
  525. RAW = isl_union_map_zip(RAW);
  526. WAW = isl_union_map_zip(WAW);
  527. WAR = isl_union_map_zip(WAR);
  528. RED = isl_union_map_zip(RED);
  529. TC_RED = isl_union_map_zip(TC_RED);
  530. LLVM_DEBUG({
  531. dbgs() << "Zipped Dependences:\n";
  532. dump();
  533. dbgs() << "\n";
  534. });
  535. RAW = isl_union_set_unwrap(isl_union_map_domain(RAW));
  536. WAW = isl_union_set_unwrap(isl_union_map_domain(WAW));
  537. WAR = isl_union_set_unwrap(isl_union_map_domain(WAR));
  538. RED = isl_union_set_unwrap(isl_union_map_domain(RED));
  539. TC_RED = isl_union_set_unwrap(isl_union_map_domain(TC_RED));
  540. LLVM_DEBUG({
  541. dbgs() << "Unwrapped Dependences:\n";
  542. dump();
  543. dbgs() << "\n";
  544. });
  545. RAW = isl_union_map_union(RAW, STMT_RAW);
  546. WAW = isl_union_map_union(WAW, STMT_WAW);
  547. WAR = isl_union_map_union(WAR, STMT_WAR);
  548. RAW = isl_union_map_coalesce(RAW);
  549. WAW = isl_union_map_coalesce(WAW);
  550. WAR = isl_union_map_coalesce(WAR);
  551. RED = isl_union_map_coalesce(RED);
  552. TC_RED = isl_union_map_coalesce(TC_RED);
  553. LLVM_DEBUG(dump());
  554. }
  555. bool Dependences::isValidSchedule(Scop &S, isl::schedule NewSched) const {
  556. // TODO: Also check permutable/coincident flags as well.
  557. StatementToIslMapTy NewSchedules;
  558. for (auto NewMap : NewSched.get_map().get_map_list()) {
  559. auto Stmt = reinterpret_cast<ScopStmt *>(
  560. NewMap.get_tuple_id(isl::dim::in).get_user());
  561. NewSchedules[Stmt] = NewMap;
  562. }
  563. return isValidSchedule(S, NewSchedules);
  564. }
  565. bool Dependences::isValidSchedule(
  566. Scop &S, const StatementToIslMapTy &NewSchedule) const {
  567. if (LegalityCheckDisabled)
  568. return true;
  569. isl::union_map Dependences = getDependences(TYPE_RAW | TYPE_WAW | TYPE_WAR);
  570. isl::union_map Schedule = isl::union_map::empty(S.getIslCtx());
  571. isl::space ScheduleSpace;
  572. for (ScopStmt &Stmt : S) {
  573. isl::map StmtScat;
  574. auto Lookup = NewSchedule.find(&Stmt);
  575. if (Lookup == NewSchedule.end())
  576. StmtScat = Stmt.getSchedule();
  577. else
  578. StmtScat = Lookup->second;
  579. assert(!StmtScat.is_null() &&
  580. "Schedules that contain extension nodes require special handling.");
  581. if (ScheduleSpace.is_null())
  582. ScheduleSpace = StmtScat.get_space().range();
  583. Schedule = Schedule.unite(StmtScat);
  584. }
  585. Dependences = Dependences.apply_domain(Schedule);
  586. Dependences = Dependences.apply_range(Schedule);
  587. isl::set Zero = isl::set::universe(ScheduleSpace);
  588. for (auto i : rangeIslSize(0, Zero.tuple_dim()))
  589. Zero = Zero.fix_si(isl::dim::set, i, 0);
  590. isl::union_set UDeltas = Dependences.deltas();
  591. isl::set Deltas = singleton(UDeltas, ScheduleSpace);
  592. isl::space Space = Deltas.get_space();
  593. isl::map NonPositive = isl::map::universe(Space.map_from_set());
  594. NonPositive =
  595. NonPositive.lex_le_at(isl::multi_pw_aff::identity_on_domain(Space));
  596. NonPositive = NonPositive.intersect_domain(Deltas);
  597. NonPositive = NonPositive.intersect_range(Zero);
  598. return NonPositive.is_empty();
  599. }
  600. // Check if the current scheduling dimension is parallel.
  601. //
  602. // We check for parallelism by verifying that the loop does not carry any
  603. // dependences.
  604. //
  605. // Parallelism test: if the distance is zero in all outer dimensions, then it
  606. // has to be zero in the current dimension as well.
  607. //
  608. // Implementation: first, translate dependences into time space, then force
  609. // outer dimensions to be equal. If the distance is zero in the current
  610. // dimension, then the loop is parallel. The distance is zero in the current
  611. // dimension if it is a subset of a map with equal values for the current
  612. // dimension.
  613. bool Dependences::isParallel(isl_union_map *Schedule, isl_union_map *Deps,
  614. isl_pw_aff **MinDistancePtr) const {
  615. isl_set *Deltas, *Distance;
  616. isl_map *ScheduleDeps;
  617. unsigned Dimension;
  618. bool IsParallel;
  619. Deps = isl_union_map_apply_range(Deps, isl_union_map_copy(Schedule));
  620. Deps = isl_union_map_apply_domain(Deps, isl_union_map_copy(Schedule));
  621. if (isl_union_map_is_empty(Deps)) {
  622. isl_union_map_free(Deps);
  623. return true;
  624. }
  625. ScheduleDeps = isl_map_from_union_map(Deps);
  626. Dimension = isl_map_dim(ScheduleDeps, isl_dim_out) - 1;
  627. for (unsigned i = 0; i < Dimension; i++)
  628. ScheduleDeps = isl_map_equate(ScheduleDeps, isl_dim_out, i, isl_dim_in, i);
  629. Deltas = isl_map_deltas(ScheduleDeps);
  630. Distance = isl_set_universe(isl_set_get_space(Deltas));
  631. // [0, ..., 0, +] - All zeros and last dimension larger than zero
  632. for (unsigned i = 0; i < Dimension; i++)
  633. Distance = isl_set_fix_si(Distance, isl_dim_set, i, 0);
  634. Distance = isl_set_lower_bound_si(Distance, isl_dim_set, Dimension, 1);
  635. Distance = isl_set_intersect(Distance, Deltas);
  636. IsParallel = isl_set_is_empty(Distance);
  637. if (IsParallel || !MinDistancePtr) {
  638. isl_set_free(Distance);
  639. return IsParallel;
  640. }
  641. Distance = isl_set_project_out(Distance, isl_dim_set, 0, Dimension);
  642. Distance = isl_set_coalesce(Distance);
  643. // This last step will compute a expression for the minimal value in the
  644. // distance polyhedron Distance with regards to the first (outer most)
  645. // dimension.
  646. *MinDistancePtr = isl_pw_aff_coalesce(isl_set_dim_min(Distance, 0));
  647. return false;
  648. }
  649. static void printDependencyMap(raw_ostream &OS, __isl_keep isl_union_map *DM) {
  650. if (DM)
  651. OS << DM << "\n";
  652. else
  653. OS << "n/a\n";
  654. }
  655. void Dependences::print(raw_ostream &OS) const {
  656. OS << "\tRAW dependences:\n\t\t";
  657. printDependencyMap(OS, RAW);
  658. OS << "\tWAR dependences:\n\t\t";
  659. printDependencyMap(OS, WAR);
  660. OS << "\tWAW dependences:\n\t\t";
  661. printDependencyMap(OS, WAW);
  662. OS << "\tReduction dependences:\n\t\t";
  663. printDependencyMap(OS, RED);
  664. OS << "\tTransitive closure of reduction dependences:\n\t\t";
  665. printDependencyMap(OS, TC_RED);
  666. }
  667. void Dependences::dump() const { print(dbgs()); }
  668. void Dependences::releaseMemory() {
  669. isl_union_map_free(RAW);
  670. isl_union_map_free(WAR);
  671. isl_union_map_free(WAW);
  672. isl_union_map_free(RED);
  673. isl_union_map_free(TC_RED);
  674. RED = RAW = WAR = WAW = TC_RED = nullptr;
  675. for (auto &ReductionDeps : ReductionDependences)
  676. isl_map_free(ReductionDeps.second);
  677. ReductionDependences.clear();
  678. }
  679. isl::union_map Dependences::getDependences(int Kinds) const {
  680. assert(hasValidDependences() && "No valid dependences available");
  681. isl::space Space = isl::manage_copy(RAW).get_space();
  682. isl::union_map Deps = Deps.empty(Space.ctx());
  683. if (Kinds & TYPE_RAW)
  684. Deps = Deps.unite(isl::manage_copy(RAW));
  685. if (Kinds & TYPE_WAR)
  686. Deps = Deps.unite(isl::manage_copy(WAR));
  687. if (Kinds & TYPE_WAW)
  688. Deps = Deps.unite(isl::manage_copy(WAW));
  689. if (Kinds & TYPE_RED)
  690. Deps = Deps.unite(isl::manage_copy(RED));
  691. if (Kinds & TYPE_TC_RED)
  692. Deps = Deps.unite(isl::manage_copy(TC_RED));
  693. Deps = Deps.coalesce();
  694. Deps = Deps.detect_equalities();
  695. return Deps;
  696. }
  697. bool Dependences::hasValidDependences() const {
  698. return (RAW != nullptr) && (WAR != nullptr) && (WAW != nullptr);
  699. }
  700. __isl_give isl_map *
  701. Dependences::getReductionDependences(MemoryAccess *MA) const {
  702. return isl_map_copy(ReductionDependences.lookup(MA));
  703. }
  704. void Dependences::setReductionDependences(MemoryAccess *MA, isl_map *D) {
  705. assert(ReductionDependences.count(MA) == 0 &&
  706. "Reduction dependences set twice!");
  707. ReductionDependences[MA] = D;
  708. }
  709. const Dependences &
  710. DependenceAnalysis::Result::getDependences(Dependences::AnalysisLevel Level) {
  711. if (Dependences *d = D[Level].get())
  712. return *d;
  713. return recomputeDependences(Level);
  714. }
  715. const Dependences &DependenceAnalysis::Result::recomputeDependences(
  716. Dependences::AnalysisLevel Level) {
  717. D[Level].reset(new Dependences(S.getSharedIslCtx(), Level));
  718. D[Level]->calculateDependences(S);
  719. return *D[Level];
  720. }
  721. DependenceAnalysis::Result
  722. DependenceAnalysis::run(Scop &S, ScopAnalysisManager &SAM,
  723. ScopStandardAnalysisResults &SAR) {
  724. return {S, {}};
  725. }
  726. AnalysisKey DependenceAnalysis::Key;
  727. PreservedAnalyses
  728. DependenceInfoPrinterPass::run(Scop &S, ScopAnalysisManager &SAM,
  729. ScopStandardAnalysisResults &SAR,
  730. SPMUpdater &U) {
  731. auto &DI = SAM.getResult<DependenceAnalysis>(S, SAR);
  732. if (auto d = DI.D[OptAnalysisLevel].get()) {
  733. d->print(OS);
  734. return PreservedAnalyses::all();
  735. }
  736. // Otherwise create the dependences on-the-fly and print them
  737. Dependences D(S.getSharedIslCtx(), OptAnalysisLevel);
  738. D.calculateDependences(S);
  739. D.print(OS);
  740. return PreservedAnalyses::all();
  741. }
  742. const Dependences &
  743. DependenceInfo::getDependences(Dependences::AnalysisLevel Level) {
  744. if (Dependences *d = D[Level].get())
  745. return *d;
  746. return recomputeDependences(Level);
  747. }
  748. const Dependences &
  749. DependenceInfo::recomputeDependences(Dependences::AnalysisLevel Level) {
  750. D[Level].reset(new Dependences(S->getSharedIslCtx(), Level));
  751. D[Level]->calculateDependences(*S);
  752. return *D[Level];
  753. }
  754. bool DependenceInfo::runOnScop(Scop &ScopVar) {
  755. S = &ScopVar;
  756. return false;
  757. }
  758. /// Print the dependences for the given SCoP to @p OS.
  759. void polly::DependenceInfo::printScop(raw_ostream &OS, Scop &S) const {
  760. if (auto d = D[OptAnalysisLevel].get()) {
  761. d->print(OS);
  762. return;
  763. }
  764. // Otherwise create the dependences on-the-fly and print it
  765. Dependences D(S.getSharedIslCtx(), OptAnalysisLevel);
  766. D.calculateDependences(S);
  767. D.print(OS);
  768. }
  769. void DependenceInfo::getAnalysisUsage(AnalysisUsage &AU) const {
  770. AU.addRequiredTransitive<ScopInfoRegionPass>();
  771. AU.setPreservesAll();
  772. }
  773. char DependenceInfo::ID = 0;
  774. Pass *polly::createDependenceInfoPass() { return new DependenceInfo(); }
  775. INITIALIZE_PASS_BEGIN(DependenceInfo, "polly-dependences",
  776. "Polly - Calculate dependences", false, false);
  777. INITIALIZE_PASS_DEPENDENCY(ScopInfoRegionPass);
  778. INITIALIZE_PASS_END(DependenceInfo, "polly-dependences",
  779. "Polly - Calculate dependences", false, false)
  780. //===----------------------------------------------------------------------===//
  781. const Dependences &
  782. DependenceInfoWrapperPass::getDependences(Scop *S,
  783. Dependences::AnalysisLevel Level) {
  784. auto It = ScopToDepsMap.find(S);
  785. if (It != ScopToDepsMap.end())
  786. if (It->second) {
  787. if (It->second->getDependenceLevel() == Level)
  788. return *It->second.get();
  789. }
  790. return recomputeDependences(S, Level);
  791. }
  792. const Dependences &DependenceInfoWrapperPass::recomputeDependences(
  793. Scop *S, Dependences::AnalysisLevel Level) {
  794. std::unique_ptr<Dependences> D(new Dependences(S->getSharedIslCtx(), Level));
  795. D->calculateDependences(*S);
  796. auto Inserted = ScopToDepsMap.insert(std::make_pair(S, std::move(D)));
  797. return *Inserted.first->second;
  798. }
  799. bool DependenceInfoWrapperPass::runOnFunction(Function &F) {
  800. auto &SI = *getAnalysis<ScopInfoWrapperPass>().getSI();
  801. for (auto &It : SI) {
  802. assert(It.second && "Invalid SCoP object!");
  803. recomputeDependences(It.second.get(), Dependences::AL_Access);
  804. }
  805. return false;
  806. }
  807. void DependenceInfoWrapperPass::print(raw_ostream &OS, const Module *M) const {
  808. for (auto &It : ScopToDepsMap) {
  809. assert((It.first && It.second) && "Invalid Scop or Dependence object!\n");
  810. It.second->print(OS);
  811. }
  812. }
  813. void DependenceInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
  814. AU.addRequiredTransitive<ScopInfoWrapperPass>();
  815. AU.setPreservesAll();
  816. }
  817. char DependenceInfoWrapperPass::ID = 0;
  818. Pass *polly::createDependenceInfoWrapperPassPass() {
  819. return new DependenceInfoWrapperPass();
  820. }
  821. INITIALIZE_PASS_BEGIN(
  822. DependenceInfoWrapperPass, "polly-function-dependences",
  823. "Polly - Calculate dependences for all the SCoPs of a function", false,
  824. false)
  825. INITIALIZE_PASS_DEPENDENCY(ScopInfoWrapperPass);
  826. INITIALIZE_PASS_END(
  827. DependenceInfoWrapperPass, "polly-function-dependences",
  828. "Polly - Calculate dependences for all the SCoPs of a function", false,
  829. false)