isl_flow.c 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340
  1. /*
  2. * Copyright 2005-2007 Universiteit Leiden
  3. * Copyright 2008-2009 Katholieke Universiteit Leuven
  4. * Copyright 2010 INRIA Saclay
  5. * Copyright 2012 Universiteit Leiden
  6. * Copyright 2014 Ecole Normale Superieure
  7. *
  8. * Use of this software is governed by the MIT license
  9. *
  10. * Written by Sven Verdoolaege, Leiden Institute of Advanced Computer Science,
  11. * Universiteit Leiden, Niels Bohrweg 1, 2333 CA Leiden, The Netherlands
  12. * and K.U.Leuven, Departement Computerwetenschappen, Celestijnenlaan 200A,
  13. * B-3001 Leuven, Belgium
  14. * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
  15. * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
  16. * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
  17. */
  18. #include <isl/val.h>
  19. #include <isl/space.h>
  20. #include <isl/set.h>
  21. #include <isl/map.h>
  22. #include <isl/union_set.h>
  23. #include <isl/union_map.h>
  24. #include <isl/flow.h>
  25. #include <isl/schedule_node.h>
  26. #include <isl_sort.h>
  27. #include <isl/stream.h>
  28. enum isl_restriction_type {
  29. isl_restriction_type_empty,
  30. isl_restriction_type_none,
  31. isl_restriction_type_input,
  32. isl_restriction_type_output
  33. };
  34. struct isl_restriction {
  35. enum isl_restriction_type type;
  36. isl_set *source;
  37. isl_set *sink;
  38. };
  39. /* Create a restriction of the given type.
  40. */
  41. static __isl_give isl_restriction *isl_restriction_alloc(
  42. __isl_take isl_map *source_map, enum isl_restriction_type type)
  43. {
  44. isl_ctx *ctx;
  45. isl_restriction *restr;
  46. if (!source_map)
  47. return NULL;
  48. ctx = isl_map_get_ctx(source_map);
  49. restr = isl_calloc_type(ctx, struct isl_restriction);
  50. if (!restr)
  51. goto error;
  52. restr->type = type;
  53. isl_map_free(source_map);
  54. return restr;
  55. error:
  56. isl_map_free(source_map);
  57. return NULL;
  58. }
  59. /* Create a restriction that doesn't restrict anything.
  60. */
  61. __isl_give isl_restriction *isl_restriction_none(__isl_take isl_map *source_map)
  62. {
  63. return isl_restriction_alloc(source_map, isl_restriction_type_none);
  64. }
  65. /* Create a restriction that removes everything.
  66. */
  67. __isl_give isl_restriction *isl_restriction_empty(
  68. __isl_take isl_map *source_map)
  69. {
  70. return isl_restriction_alloc(source_map, isl_restriction_type_empty);
  71. }
  72. /* Create a restriction on the input of the maximization problem
  73. * based on the given source and sink restrictions.
  74. */
  75. __isl_give isl_restriction *isl_restriction_input(
  76. __isl_take isl_set *source_restr, __isl_take isl_set *sink_restr)
  77. {
  78. isl_ctx *ctx;
  79. isl_restriction *restr;
  80. if (!source_restr || !sink_restr)
  81. goto error;
  82. ctx = isl_set_get_ctx(source_restr);
  83. restr = isl_calloc_type(ctx, struct isl_restriction);
  84. if (!restr)
  85. goto error;
  86. restr->type = isl_restriction_type_input;
  87. restr->source = source_restr;
  88. restr->sink = sink_restr;
  89. return restr;
  90. error:
  91. isl_set_free(source_restr);
  92. isl_set_free(sink_restr);
  93. return NULL;
  94. }
  95. /* Create a restriction on the output of the maximization problem
  96. * based on the given source restriction.
  97. */
  98. __isl_give isl_restriction *isl_restriction_output(
  99. __isl_take isl_set *source_restr)
  100. {
  101. isl_ctx *ctx;
  102. isl_restriction *restr;
  103. if (!source_restr)
  104. return NULL;
  105. ctx = isl_set_get_ctx(source_restr);
  106. restr = isl_calloc_type(ctx, struct isl_restriction);
  107. if (!restr)
  108. goto error;
  109. restr->type = isl_restriction_type_output;
  110. restr->source = source_restr;
  111. return restr;
  112. error:
  113. isl_set_free(source_restr);
  114. return NULL;
  115. }
  116. __isl_null isl_restriction *isl_restriction_free(
  117. __isl_take isl_restriction *restr)
  118. {
  119. if (!restr)
  120. return NULL;
  121. isl_set_free(restr->source);
  122. isl_set_free(restr->sink);
  123. free(restr);
  124. return NULL;
  125. }
  126. isl_ctx *isl_restriction_get_ctx(__isl_keep isl_restriction *restr)
  127. {
  128. return restr ? isl_set_get_ctx(restr->source) : NULL;
  129. }
  130. /* A private structure to keep track of a mapping together with
  131. * a user-specified identifier and a boolean indicating whether
  132. * the map represents a must or may access/dependence.
  133. */
  134. struct isl_labeled_map {
  135. struct isl_map *map;
  136. void *data;
  137. int must;
  138. };
  139. typedef isl_bool (*isl_access_coscheduled)(void *first, void *second);
  140. /* A structure containing the input for dependence analysis:
  141. * - a sink
  142. * - n_must + n_may (<= max_source) sources
  143. * - a function for determining the relative order of sources and sink
  144. * - an optional function "coscheduled" for determining whether sources
  145. * may be coscheduled. If "coscheduled" is NULL, then the sources
  146. * are assumed not to be coscheduled.
  147. * The must sources are placed before the may sources.
  148. *
  149. * domain_map is an auxiliary map that maps the sink access relation
  150. * to the domain of this access relation.
  151. * This field is only needed when restrict_fn is set and
  152. * the field itself is set by isl_access_info_compute_flow.
  153. *
  154. * restrict_fn is a callback that (if not NULL) will be called
  155. * right before any lexicographical maximization.
  156. */
  157. struct isl_access_info {
  158. isl_map *domain_map;
  159. struct isl_labeled_map sink;
  160. isl_access_level_before level_before;
  161. isl_access_coscheduled coscheduled;
  162. isl_access_restrict restrict_fn;
  163. void *restrict_user;
  164. int max_source;
  165. int n_must;
  166. int n_may;
  167. struct isl_labeled_map source[1];
  168. };
  169. /* A structure containing the output of dependence analysis:
  170. * - n_source dependences
  171. * - a wrapped subset of the sink for which definitely no source could be found
  172. * - a wrapped subset of the sink for which possibly no source could be found
  173. */
  174. struct isl_flow {
  175. isl_set *must_no_source;
  176. isl_set *may_no_source;
  177. int n_source;
  178. struct isl_labeled_map *dep;
  179. };
  180. /* Construct an isl_access_info structure and fill it up with
  181. * the given data. The number of sources is set to 0.
  182. */
  183. __isl_give isl_access_info *isl_access_info_alloc(__isl_take isl_map *sink,
  184. void *sink_user, isl_access_level_before fn, int max_source)
  185. {
  186. isl_ctx *ctx;
  187. struct isl_access_info *acc;
  188. if (!sink)
  189. return NULL;
  190. ctx = isl_map_get_ctx(sink);
  191. isl_assert(ctx, max_source >= 0, goto error);
  192. acc = isl_calloc(ctx, struct isl_access_info,
  193. sizeof(struct isl_access_info) +
  194. (max_source - 1) * sizeof(struct isl_labeled_map));
  195. if (!acc)
  196. goto error;
  197. acc->sink.map = sink;
  198. acc->sink.data = sink_user;
  199. acc->level_before = fn;
  200. acc->max_source = max_source;
  201. acc->n_must = 0;
  202. acc->n_may = 0;
  203. return acc;
  204. error:
  205. isl_map_free(sink);
  206. return NULL;
  207. }
  208. /* Free the given isl_access_info structure.
  209. */
  210. __isl_null isl_access_info *isl_access_info_free(
  211. __isl_take isl_access_info *acc)
  212. {
  213. int i;
  214. if (!acc)
  215. return NULL;
  216. isl_map_free(acc->domain_map);
  217. isl_map_free(acc->sink.map);
  218. for (i = 0; i < acc->n_must + acc->n_may; ++i)
  219. isl_map_free(acc->source[i].map);
  220. free(acc);
  221. return NULL;
  222. }
  223. isl_ctx *isl_access_info_get_ctx(__isl_keep isl_access_info *acc)
  224. {
  225. return acc ? isl_map_get_ctx(acc->sink.map) : NULL;
  226. }
  227. __isl_give isl_access_info *isl_access_info_set_restrict(
  228. __isl_take isl_access_info *acc, isl_access_restrict fn, void *user)
  229. {
  230. if (!acc)
  231. return NULL;
  232. acc->restrict_fn = fn;
  233. acc->restrict_user = user;
  234. return acc;
  235. }
  236. /* Add another source to an isl_access_info structure, making
  237. * sure the "must" sources are placed before the "may" sources.
  238. * This function may be called at most max_source times on a
  239. * given isl_access_info structure, with max_source as specified
  240. * in the call to isl_access_info_alloc that constructed the structure.
  241. */
  242. __isl_give isl_access_info *isl_access_info_add_source(
  243. __isl_take isl_access_info *acc, __isl_take isl_map *source,
  244. int must, void *source_user)
  245. {
  246. isl_ctx *ctx;
  247. if (!acc)
  248. goto error;
  249. ctx = isl_map_get_ctx(acc->sink.map);
  250. isl_assert(ctx, acc->n_must + acc->n_may < acc->max_source, goto error);
  251. if (must) {
  252. if (acc->n_may)
  253. acc->source[acc->n_must + acc->n_may] =
  254. acc->source[acc->n_must];
  255. acc->source[acc->n_must].map = source;
  256. acc->source[acc->n_must].data = source_user;
  257. acc->source[acc->n_must].must = 1;
  258. acc->n_must++;
  259. } else {
  260. acc->source[acc->n_must + acc->n_may].map = source;
  261. acc->source[acc->n_must + acc->n_may].data = source_user;
  262. acc->source[acc->n_must + acc->n_may].must = 0;
  263. acc->n_may++;
  264. }
  265. return acc;
  266. error:
  267. isl_map_free(source);
  268. isl_access_info_free(acc);
  269. return NULL;
  270. }
  271. /* A helper struct carrying the isl_access_info and an error condition.
  272. */
  273. struct access_sort_info {
  274. isl_access_info *access_info;
  275. int error;
  276. };
  277. /* Return -n, 0 or n (with n a positive value), depending on whether
  278. * the source access identified by p1 should be sorted before, together
  279. * or after that identified by p2.
  280. *
  281. * If p1 appears before p2, then it should be sorted first.
  282. * For more generic initial schedules, it is possible that neither
  283. * p1 nor p2 appears before the other, or at least not in any obvious way.
  284. * We therefore also check if p2 appears before p1, in which case p2
  285. * should be sorted first.
  286. * If not, we try to order the two statements based on the description
  287. * of the iteration domains. This results in an arbitrary, but fairly
  288. * stable ordering.
  289. *
  290. * In case of an error, sort_info.error is set to true and all elements are
  291. * reported to be equal.
  292. */
  293. static int access_sort_cmp(const void *p1, const void *p2, void *user)
  294. {
  295. struct access_sort_info *sort_info = user;
  296. isl_access_info *acc = sort_info->access_info;
  297. if (sort_info->error)
  298. return 0;
  299. const struct isl_labeled_map *i1, *i2;
  300. int level1, level2;
  301. uint32_t h1, h2;
  302. i1 = (const struct isl_labeled_map *) p1;
  303. i2 = (const struct isl_labeled_map *) p2;
  304. level1 = acc->level_before(i1->data, i2->data);
  305. if (level1 < 0)
  306. goto error;
  307. if (level1 % 2)
  308. return -1;
  309. level2 = acc->level_before(i2->data, i1->data);
  310. if (level2 < 0)
  311. goto error;
  312. if (level2 % 2)
  313. return 1;
  314. h1 = isl_map_get_hash(i1->map);
  315. h2 = isl_map_get_hash(i2->map);
  316. return h1 > h2 ? 1 : h1 < h2 ? -1 : 0;
  317. error:
  318. sort_info->error = 1;
  319. return 0;
  320. }
  321. /* Sort the must source accesses in their textual order.
  322. */
  323. static __isl_give isl_access_info *isl_access_info_sort_sources(
  324. __isl_take isl_access_info *acc)
  325. {
  326. struct access_sort_info sort_info;
  327. sort_info.access_info = acc;
  328. sort_info.error = 0;
  329. if (!acc)
  330. return NULL;
  331. if (acc->n_must <= 1)
  332. return acc;
  333. if (isl_sort(acc->source, acc->n_must, sizeof(struct isl_labeled_map),
  334. access_sort_cmp, &sort_info) < 0)
  335. return isl_access_info_free(acc);
  336. if (sort_info.error)
  337. return isl_access_info_free(acc);
  338. return acc;
  339. }
  340. /* Align the parameters of the two spaces if needed and then call
  341. * isl_space_join.
  342. */
  343. static __isl_give isl_space *space_align_and_join(__isl_take isl_space *left,
  344. __isl_take isl_space *right)
  345. {
  346. isl_bool equal_params;
  347. equal_params = isl_space_has_equal_params(left, right);
  348. if (equal_params < 0)
  349. goto error;
  350. if (equal_params)
  351. return isl_space_join(left, right);
  352. left = isl_space_align_params(left, isl_space_copy(right));
  353. right = isl_space_align_params(right, isl_space_copy(left));
  354. return isl_space_join(left, right);
  355. error:
  356. isl_space_free(left);
  357. isl_space_free(right);
  358. return NULL;
  359. }
  360. /* Initialize an empty isl_flow structure corresponding to a given
  361. * isl_access_info structure.
  362. * For each must access, two dependences are created (initialized
  363. * to the empty relation), one for the resulting must dependences
  364. * and one for the resulting may dependences. May accesses can
  365. * only lead to may dependences, so only one dependence is created
  366. * for each of them.
  367. * This function is private as isl_flow structures are only supposed
  368. * to be created by isl_access_info_compute_flow.
  369. */
  370. static __isl_give isl_flow *isl_flow_alloc(__isl_keep isl_access_info *acc)
  371. {
  372. int i, n;
  373. struct isl_ctx *ctx;
  374. struct isl_flow *dep;
  375. if (!acc)
  376. return NULL;
  377. ctx = isl_map_get_ctx(acc->sink.map);
  378. dep = isl_calloc_type(ctx, struct isl_flow);
  379. if (!dep)
  380. return NULL;
  381. n = 2 * acc->n_must + acc->n_may;
  382. dep->dep = isl_calloc_array(ctx, struct isl_labeled_map, n);
  383. if (n && !dep->dep)
  384. goto error;
  385. dep->n_source = n;
  386. for (i = 0; i < acc->n_must; ++i) {
  387. isl_space *space;
  388. space = space_align_and_join(
  389. isl_map_get_space(acc->source[i].map),
  390. isl_space_reverse(isl_map_get_space(acc->sink.map)));
  391. dep->dep[2 * i].map = isl_map_empty(space);
  392. dep->dep[2 * i + 1].map = isl_map_copy(dep->dep[2 * i].map);
  393. dep->dep[2 * i].data = acc->source[i].data;
  394. dep->dep[2 * i + 1].data = acc->source[i].data;
  395. dep->dep[2 * i].must = 1;
  396. dep->dep[2 * i + 1].must = 0;
  397. if (!dep->dep[2 * i].map || !dep->dep[2 * i + 1].map)
  398. goto error;
  399. }
  400. for (i = acc->n_must; i < acc->n_must + acc->n_may; ++i) {
  401. isl_space *space;
  402. space = space_align_and_join(
  403. isl_map_get_space(acc->source[i].map),
  404. isl_space_reverse(isl_map_get_space(acc->sink.map)));
  405. dep->dep[acc->n_must + i].map = isl_map_empty(space);
  406. dep->dep[acc->n_must + i].data = acc->source[i].data;
  407. dep->dep[acc->n_must + i].must = 0;
  408. if (!dep->dep[acc->n_must + i].map)
  409. goto error;
  410. }
  411. return dep;
  412. error:
  413. isl_flow_free(dep);
  414. return NULL;
  415. }
  416. /* Iterate over all sources and for each resulting flow dependence
  417. * that is not empty, call the user specfied function.
  418. * The second argument in this function call identifies the source,
  419. * while the third argument correspond to the final argument of
  420. * the isl_flow_foreach call.
  421. */
  422. isl_stat isl_flow_foreach(__isl_keep isl_flow *deps,
  423. isl_stat (*fn)(__isl_take isl_map *dep, int must, void *dep_user,
  424. void *user),
  425. void *user)
  426. {
  427. int i;
  428. if (!deps)
  429. return isl_stat_error;
  430. for (i = 0; i < deps->n_source; ++i) {
  431. if (isl_map_plain_is_empty(deps->dep[i].map))
  432. continue;
  433. if (fn(isl_map_copy(deps->dep[i].map), deps->dep[i].must,
  434. deps->dep[i].data, user) < 0)
  435. return isl_stat_error;
  436. }
  437. return isl_stat_ok;
  438. }
  439. /* Return a copy of the subset of the sink for which no source could be found.
  440. */
  441. __isl_give isl_map *isl_flow_get_no_source(__isl_keep isl_flow *deps, int must)
  442. {
  443. if (!deps)
  444. return NULL;
  445. if (must)
  446. return isl_set_unwrap(isl_set_copy(deps->must_no_source));
  447. else
  448. return isl_set_unwrap(isl_set_copy(deps->may_no_source));
  449. }
  450. __isl_null isl_flow *isl_flow_free(__isl_take isl_flow *deps)
  451. {
  452. int i;
  453. if (!deps)
  454. return NULL;
  455. isl_set_free(deps->must_no_source);
  456. isl_set_free(deps->may_no_source);
  457. if (deps->dep) {
  458. for (i = 0; i < deps->n_source; ++i)
  459. isl_map_free(deps->dep[i].map);
  460. free(deps->dep);
  461. }
  462. free(deps);
  463. return NULL;
  464. }
  465. isl_ctx *isl_flow_get_ctx(__isl_keep isl_flow *deps)
  466. {
  467. return deps ? isl_set_get_ctx(deps->must_no_source) : NULL;
  468. }
  469. /* Return a map that enforces that the domain iteration occurs after
  470. * the range iteration at the given level.
  471. * If level is odd, then the domain iteration should occur after
  472. * the target iteration in their shared level/2 outermost loops.
  473. * In this case we simply need to enforce that these outermost
  474. * loop iterations are the same.
  475. * If level is even, then the loop iterator of the domain should
  476. * be greater than the loop iterator of the range at the last
  477. * of the level/2 shared loops, i.e., loop level/2 - 1.
  478. */
  479. static __isl_give isl_map *after_at_level(__isl_take isl_space *space,
  480. int level)
  481. {
  482. struct isl_basic_map *bmap;
  483. if (level % 2)
  484. bmap = isl_basic_map_equal(space, level/2);
  485. else
  486. bmap = isl_basic_map_more_at(space, level/2 - 1);
  487. return isl_map_from_basic_map(bmap);
  488. }
  489. /* Compute the partial lexicographic maximum of "dep" on domain "sink",
  490. * but first check if the user has set acc->restrict_fn and if so
  491. * update either the input or the output of the maximization problem
  492. * with respect to the resulting restriction.
  493. *
  494. * Since the user expects a mapping from sink iterations to source iterations,
  495. * whereas the domain of "dep" is a wrapped map, mapping sink iterations
  496. * to accessed array elements, we first need to project out the accessed
  497. * sink array elements by applying acc->domain_map.
  498. * Similarly, the sink restriction specified by the user needs to be
  499. * converted back to the wrapped map.
  500. */
  501. static __isl_give isl_map *restricted_partial_lexmax(
  502. __isl_keep isl_access_info *acc, __isl_take isl_map *dep,
  503. int source, __isl_take isl_set *sink, __isl_give isl_set **empty)
  504. {
  505. isl_map *source_map;
  506. isl_restriction *restr;
  507. isl_set *sink_domain;
  508. isl_set *sink_restr;
  509. isl_map *res;
  510. if (!acc->restrict_fn)
  511. return isl_map_partial_lexmax(dep, sink, empty);
  512. source_map = isl_map_copy(dep);
  513. source_map = isl_map_apply_domain(source_map,
  514. isl_map_copy(acc->domain_map));
  515. sink_domain = isl_set_copy(sink);
  516. sink_domain = isl_set_apply(sink_domain, isl_map_copy(acc->domain_map));
  517. restr = acc->restrict_fn(source_map, sink_domain,
  518. acc->source[source].data, acc->restrict_user);
  519. isl_set_free(sink_domain);
  520. isl_map_free(source_map);
  521. if (!restr)
  522. goto error;
  523. if (restr->type == isl_restriction_type_input) {
  524. dep = isl_map_intersect_range(dep, isl_set_copy(restr->source));
  525. sink_restr = isl_set_copy(restr->sink);
  526. sink_restr = isl_set_apply(sink_restr,
  527. isl_map_reverse(isl_map_copy(acc->domain_map)));
  528. sink = isl_set_intersect(sink, sink_restr);
  529. } else if (restr->type == isl_restriction_type_empty) {
  530. isl_space *space = isl_map_get_space(dep);
  531. isl_map_free(dep);
  532. dep = isl_map_empty(space);
  533. }
  534. res = isl_map_partial_lexmax(dep, sink, empty);
  535. if (restr->type == isl_restriction_type_output)
  536. res = isl_map_intersect_range(res, isl_set_copy(restr->source));
  537. isl_restriction_free(restr);
  538. return res;
  539. error:
  540. isl_map_free(dep);
  541. isl_set_free(sink);
  542. *empty = NULL;
  543. return NULL;
  544. }
  545. /* Compute the last iteration of must source j that precedes the sink
  546. * at the given level for sink iterations in set_C.
  547. * The subset of set_C for which no such iteration can be found is returned
  548. * in *empty.
  549. */
  550. static struct isl_map *last_source(struct isl_access_info *acc,
  551. struct isl_set *set_C,
  552. int j, int level, struct isl_set **empty)
  553. {
  554. struct isl_map *read_map;
  555. struct isl_map *write_map;
  556. struct isl_map *dep_map;
  557. struct isl_map *after;
  558. struct isl_map *result;
  559. read_map = isl_map_copy(acc->sink.map);
  560. write_map = isl_map_copy(acc->source[j].map);
  561. write_map = isl_map_reverse(write_map);
  562. dep_map = isl_map_apply_range(read_map, write_map);
  563. after = after_at_level(isl_map_get_space(dep_map), level);
  564. dep_map = isl_map_intersect(dep_map, after);
  565. result = restricted_partial_lexmax(acc, dep_map, j, set_C, empty);
  566. result = isl_map_reverse(result);
  567. return result;
  568. }
  569. /* For a given mapping between iterations of must source j and iterations
  570. * of the sink, compute the last iteration of must source k preceding
  571. * the sink at level before_level for any of the sink iterations,
  572. * but following the corresponding iteration of must source j at level
  573. * after_level.
  574. */
  575. static struct isl_map *last_later_source(struct isl_access_info *acc,
  576. struct isl_map *old_map,
  577. int j, int before_level,
  578. int k, int after_level,
  579. struct isl_set **empty)
  580. {
  581. isl_space *space;
  582. struct isl_set *set_C;
  583. struct isl_map *read_map;
  584. struct isl_map *write_map;
  585. struct isl_map *dep_map;
  586. struct isl_map *after_write;
  587. struct isl_map *before_read;
  588. struct isl_map *result;
  589. set_C = isl_map_range(isl_map_copy(old_map));
  590. read_map = isl_map_copy(acc->sink.map);
  591. write_map = isl_map_copy(acc->source[k].map);
  592. write_map = isl_map_reverse(write_map);
  593. dep_map = isl_map_apply_range(read_map, write_map);
  594. space = space_align_and_join(isl_map_get_space(acc->source[k].map),
  595. isl_space_reverse(isl_map_get_space(acc->source[j].map)));
  596. after_write = after_at_level(space, after_level);
  597. after_write = isl_map_apply_range(after_write, old_map);
  598. after_write = isl_map_reverse(after_write);
  599. dep_map = isl_map_intersect(dep_map, after_write);
  600. before_read = after_at_level(isl_map_get_space(dep_map), before_level);
  601. dep_map = isl_map_intersect(dep_map, before_read);
  602. result = restricted_partial_lexmax(acc, dep_map, k, set_C, empty);
  603. result = isl_map_reverse(result);
  604. return result;
  605. }
  606. /* Given a shared_level between two accesses, return 1 if the
  607. * the first can precede the second at the requested target_level.
  608. * If the target level is odd, i.e., refers to a statement level
  609. * dimension, then first needs to precede second at the requested
  610. * level, i.e., shared_level must be equal to target_level.
  611. * If the target level is odd, then the two loops should share
  612. * at least the requested number of outer loops.
  613. */
  614. static int can_precede_at_level(int shared_level, int target_level)
  615. {
  616. if (shared_level < target_level)
  617. return 0;
  618. if ((target_level % 2) && shared_level > target_level)
  619. return 0;
  620. return 1;
  621. }
  622. /* Given a possible flow dependence temp_rel[j] between source j and the sink
  623. * at level sink_level, remove those elements for which
  624. * there is an iteration of another source k < j that is closer to the sink.
  625. * The flow dependences temp_rel[k] are updated with the improved sources.
  626. * Any improved source needs to precede the sink at the same level
  627. * and needs to follow source j at the same or a deeper level.
  628. * The lower this level, the later the execution date of source k.
  629. * We therefore consider lower levels first.
  630. *
  631. * If temp_rel[j] is empty, then there can be no improvement and
  632. * we return immediately.
  633. *
  634. * This function returns isl_stat_ok in case it was executed successfully and
  635. * isl_stat_error in case of errors during the execution of this function.
  636. */
  637. static isl_stat intermediate_sources(__isl_keep isl_access_info *acc,
  638. struct isl_map **temp_rel, int j, int sink_level)
  639. {
  640. int k, level;
  641. isl_size n_in = isl_map_dim(acc->source[j].map, isl_dim_in);
  642. int depth = 2 * n_in + 1;
  643. if (n_in < 0)
  644. return isl_stat_error;
  645. if (isl_map_plain_is_empty(temp_rel[j]))
  646. return isl_stat_ok;
  647. for (k = j - 1; k >= 0; --k) {
  648. int plevel, plevel2;
  649. plevel = acc->level_before(acc->source[k].data, acc->sink.data);
  650. if (plevel < 0)
  651. return isl_stat_error;
  652. if (!can_precede_at_level(plevel, sink_level))
  653. continue;
  654. plevel2 = acc->level_before(acc->source[j].data,
  655. acc->source[k].data);
  656. if (plevel2 < 0)
  657. return isl_stat_error;
  658. for (level = sink_level; level <= depth; ++level) {
  659. struct isl_map *T;
  660. struct isl_set *trest;
  661. struct isl_map *copy;
  662. if (!can_precede_at_level(plevel2, level))
  663. continue;
  664. copy = isl_map_copy(temp_rel[j]);
  665. T = last_later_source(acc, copy, j, sink_level, k,
  666. level, &trest);
  667. if (isl_map_plain_is_empty(T)) {
  668. isl_set_free(trest);
  669. isl_map_free(T);
  670. continue;
  671. }
  672. temp_rel[j] = isl_map_intersect_range(temp_rel[j], trest);
  673. temp_rel[k] = isl_map_union_disjoint(temp_rel[k], T);
  674. }
  675. }
  676. return isl_stat_ok;
  677. }
  678. /* Compute all iterations of may source j that precedes the sink at the given
  679. * level for sink iterations in set_C.
  680. */
  681. static __isl_give isl_map *all_sources(__isl_keep isl_access_info *acc,
  682. __isl_take isl_set *set_C, int j, int level)
  683. {
  684. isl_map *read_map;
  685. isl_map *write_map;
  686. isl_map *dep_map;
  687. isl_map *after;
  688. read_map = isl_map_copy(acc->sink.map);
  689. read_map = isl_map_intersect_domain(read_map, set_C);
  690. write_map = isl_map_copy(acc->source[acc->n_must + j].map);
  691. write_map = isl_map_reverse(write_map);
  692. dep_map = isl_map_apply_range(read_map, write_map);
  693. after = after_at_level(isl_map_get_space(dep_map), level);
  694. dep_map = isl_map_intersect(dep_map, after);
  695. return isl_map_reverse(dep_map);
  696. }
  697. /* For a given mapping between iterations of must source k and iterations
  698. * of the sink, compute all iterations of may source j preceding
  699. * the sink at level before_level for any of the sink iterations,
  700. * but following the corresponding iteration of must source k at level
  701. * after_level.
  702. */
  703. static __isl_give isl_map *all_later_sources(__isl_keep isl_access_info *acc,
  704. __isl_take isl_map *old_map,
  705. int j, int before_level, int k, int after_level)
  706. {
  707. isl_space *space;
  708. isl_set *set_C;
  709. isl_map *read_map;
  710. isl_map *write_map;
  711. isl_map *dep_map;
  712. isl_map *after_write;
  713. isl_map *before_read;
  714. set_C = isl_map_range(isl_map_copy(old_map));
  715. read_map = isl_map_copy(acc->sink.map);
  716. read_map = isl_map_intersect_domain(read_map, set_C);
  717. write_map = isl_map_copy(acc->source[acc->n_must + j].map);
  718. write_map = isl_map_reverse(write_map);
  719. dep_map = isl_map_apply_range(read_map, write_map);
  720. space = isl_space_join(isl_map_get_space(
  721. acc->source[acc->n_must + j].map),
  722. isl_space_reverse(isl_map_get_space(acc->source[k].map)));
  723. after_write = after_at_level(space, after_level);
  724. after_write = isl_map_apply_range(after_write, old_map);
  725. after_write = isl_map_reverse(after_write);
  726. dep_map = isl_map_intersect(dep_map, after_write);
  727. before_read = after_at_level(isl_map_get_space(dep_map), before_level);
  728. dep_map = isl_map_intersect(dep_map, before_read);
  729. return isl_map_reverse(dep_map);
  730. }
  731. /* Given the must and may dependence relations for the must accesses
  732. * for level sink_level, check if there are any accesses of may access j
  733. * that occur in between and return their union.
  734. * If some of these accesses are intermediate with respect to
  735. * (previously thought to be) must dependences, then these
  736. * must dependences are turned into may dependences.
  737. */
  738. static __isl_give isl_map *all_intermediate_sources(
  739. __isl_keep isl_access_info *acc, __isl_take isl_map *map,
  740. struct isl_map **must_rel, struct isl_map **may_rel,
  741. int j, int sink_level)
  742. {
  743. int k, level;
  744. isl_size n_in = isl_map_dim(acc->source[acc->n_must + j].map,
  745. isl_dim_in);
  746. int depth = 2 * n_in + 1;
  747. if (n_in < 0)
  748. return isl_map_free(map);
  749. for (k = 0; k < acc->n_must; ++k) {
  750. int plevel;
  751. if (isl_map_plain_is_empty(may_rel[k]) &&
  752. isl_map_plain_is_empty(must_rel[k]))
  753. continue;
  754. plevel = acc->level_before(acc->source[k].data,
  755. acc->source[acc->n_must + j].data);
  756. if (plevel < 0)
  757. return isl_map_free(map);
  758. for (level = sink_level; level <= depth; ++level) {
  759. isl_map *T;
  760. isl_map *copy;
  761. isl_set *ran;
  762. if (!can_precede_at_level(plevel, level))
  763. continue;
  764. copy = isl_map_copy(may_rel[k]);
  765. T = all_later_sources(acc, copy, j, sink_level, k, level);
  766. map = isl_map_union(map, T);
  767. copy = isl_map_copy(must_rel[k]);
  768. T = all_later_sources(acc, copy, j, sink_level, k, level);
  769. ran = isl_map_range(isl_map_copy(T));
  770. map = isl_map_union(map, T);
  771. may_rel[k] = isl_map_union_disjoint(may_rel[k],
  772. isl_map_intersect_range(isl_map_copy(must_rel[k]),
  773. isl_set_copy(ran)));
  774. T = isl_map_from_domain_and_range(
  775. isl_set_universe(
  776. isl_space_domain(isl_map_get_space(must_rel[k]))),
  777. ran);
  778. must_rel[k] = isl_map_subtract(must_rel[k], T);
  779. }
  780. }
  781. return map;
  782. }
  783. /* Given a dependence relation "old_map" between a must-source and the sink,
  784. * return a subset of the dependences, augmented with instances
  785. * of the source at position "pos" in "acc" that are coscheduled
  786. * with the must-source and that access the same element.
  787. * That is, if the input lives in a space T -> K, then the output
  788. * lives in the space [T -> S] -> K, with S the space of source "pos", and
  789. * the domain factor of the domain product is a subset of the input.
  790. * The sources are considered to be coscheduled if they have the same values
  791. * for the initial "depth" coordinates.
  792. *
  793. * First construct a dependence relation S -> K and a mapping
  794. * between coscheduled sources T -> S.
  795. * The second is combined with the original dependence relation T -> K
  796. * to form a relation in T -> [S -> K], which is subsequently
  797. * uncurried to [T -> S] -> K.
  798. * This result is then intersected with the dependence relation S -> K
  799. * to form the output.
  800. *
  801. * In case a negative depth is given, NULL is returned to indicate an error.
  802. */
  803. static __isl_give isl_map *coscheduled_source(__isl_keep isl_access_info *acc,
  804. __isl_keep isl_map *old_map, int pos, int depth)
  805. {
  806. isl_space *space;
  807. isl_set *set_C;
  808. isl_map *read_map;
  809. isl_map *write_map;
  810. isl_map *dep_map;
  811. isl_map *equal;
  812. isl_map *map;
  813. if (depth < 0)
  814. return NULL;
  815. set_C = isl_map_range(isl_map_copy(old_map));
  816. read_map = isl_map_copy(acc->sink.map);
  817. read_map = isl_map_intersect_domain(read_map, set_C);
  818. write_map = isl_map_copy(acc->source[pos].map);
  819. dep_map = isl_map_domain_product(write_map, read_map);
  820. dep_map = isl_set_unwrap(isl_map_domain(dep_map));
  821. space = isl_space_join(isl_map_get_space(old_map),
  822. isl_space_reverse(isl_map_get_space(dep_map)));
  823. equal = isl_map_from_basic_map(isl_basic_map_equal(space, depth));
  824. map = isl_map_range_product(equal, isl_map_copy(old_map));
  825. map = isl_map_uncurry(map);
  826. map = isl_map_intersect_domain_factor_range(map, dep_map);
  827. return map;
  828. }
  829. /* After the dependences derived from a must-source have been computed
  830. * at a certain level, check if any of the sources of the must-dependences
  831. * may be coscheduled with other sources.
  832. * If they are any such sources, then there is no way of determining
  833. * which of the sources actually comes last and the must-dependences
  834. * need to be turned into may-dependences, while dependences from
  835. * the other sources need to be added to the may-dependences as well.
  836. * "acc" describes the sources and a callback for checking whether
  837. * two sources may be coscheduled. If acc->coscheduled is NULL then
  838. * the sources are assumed not to be coscheduled.
  839. * "must_rel" and "may_rel" describe the must and may-dependence relations
  840. * computed at the current level for the must-sources. Some of the dependences
  841. * may be moved from "must_rel" to "may_rel".
  842. * "flow" contains all dependences computed so far (apart from those
  843. * in "must_rel" and "may_rel") and may be updated with additional
  844. * dependences derived from may-sources.
  845. *
  846. * In particular, consider all the must-sources with a non-empty
  847. * dependence relation in "must_rel". They are considered in reverse
  848. * order because that is the order in which they are considered in the caller.
  849. * If any of the must-sources are coscheduled, then the last one
  850. * is the one that will have a corresponding dependence relation.
  851. * For each must-source i, consider both all the previous must-sources
  852. * and all the may-sources. If any of those may be coscheduled with
  853. * must-source i, then compute the coscheduled instances that access
  854. * the same memory elements. The result is a relation [T -> S] -> K.
  855. * The projection onto T -> K is a subset of the must-dependence relation
  856. * that needs to be turned into may-dependences.
  857. * The projection onto S -> K needs to be added to the may-dependences
  858. * of source S.
  859. * Since a given must-source instance may be coscheduled with several
  860. * other source instances, the dependences that need to be turned
  861. * into may-dependences are first collected and only actually removed
  862. * from the must-dependences after all other sources have been considered.
  863. */
  864. static __isl_give isl_flow *handle_coscheduled(__isl_keep isl_access_info *acc,
  865. __isl_keep isl_map **must_rel, __isl_keep isl_map **may_rel,
  866. __isl_take isl_flow *flow)
  867. {
  868. int i, j;
  869. if (!acc->coscheduled)
  870. return flow;
  871. for (i = acc->n_must - 1; i >= 0; --i) {
  872. isl_map *move;
  873. if (isl_map_plain_is_empty(must_rel[i]))
  874. continue;
  875. move = isl_map_empty(isl_map_get_space(must_rel[i]));
  876. for (j = i - 1; j >= 0; --j) {
  877. int depth;
  878. isl_bool coscheduled;
  879. isl_map *map, *factor;
  880. coscheduled = acc->coscheduled(acc->source[i].data,
  881. acc->source[j].data);
  882. if (coscheduled < 0) {
  883. isl_map_free(move);
  884. return isl_flow_free(flow);
  885. }
  886. if (!coscheduled)
  887. continue;
  888. depth = acc->level_before(acc->source[i].data,
  889. acc->source[j].data) / 2;
  890. map = coscheduled_source(acc, must_rel[i], j, depth);
  891. factor = isl_map_domain_factor_range(isl_map_copy(map));
  892. may_rel[j] = isl_map_union(may_rel[j], factor);
  893. map = isl_map_domain_factor_domain(map);
  894. move = isl_map_union(move, map);
  895. }
  896. for (j = 0; j < acc->n_may; ++j) {
  897. int depth, pos;
  898. isl_bool coscheduled;
  899. isl_map *map, *factor;
  900. pos = acc->n_must + j;
  901. coscheduled = acc->coscheduled(acc->source[i].data,
  902. acc->source[pos].data);
  903. if (coscheduled < 0) {
  904. isl_map_free(move);
  905. return isl_flow_free(flow);
  906. }
  907. if (!coscheduled)
  908. continue;
  909. depth = acc->level_before(acc->source[i].data,
  910. acc->source[pos].data) / 2;
  911. map = coscheduled_source(acc, must_rel[i], pos, depth);
  912. factor = isl_map_domain_factor_range(isl_map_copy(map));
  913. pos = 2 * acc->n_must + j;
  914. flow->dep[pos].map = isl_map_union(flow->dep[pos].map,
  915. factor);
  916. map = isl_map_domain_factor_domain(map);
  917. move = isl_map_union(move, map);
  918. }
  919. must_rel[i] = isl_map_subtract(must_rel[i], isl_map_copy(move));
  920. may_rel[i] = isl_map_union(may_rel[i], move);
  921. }
  922. return flow;
  923. }
  924. /* Compute dependences for the case where all accesses are "may"
  925. * accesses, which boils down to computing memory based dependences.
  926. * The generic algorithm would also work in this case, but it would
  927. * be overkill to use it.
  928. */
  929. static __isl_give isl_flow *compute_mem_based_dependences(
  930. __isl_keep isl_access_info *acc)
  931. {
  932. int i;
  933. isl_set *mustdo;
  934. isl_set *maydo;
  935. isl_flow *res;
  936. res = isl_flow_alloc(acc);
  937. if (!res)
  938. return NULL;
  939. mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
  940. maydo = isl_set_copy(mustdo);
  941. for (i = 0; i < acc->n_may; ++i) {
  942. int plevel;
  943. int is_before;
  944. isl_space *space;
  945. isl_map *before;
  946. isl_map *dep;
  947. plevel = acc->level_before(acc->source[i].data, acc->sink.data);
  948. if (plevel < 0)
  949. goto error;
  950. is_before = plevel & 1;
  951. plevel >>= 1;
  952. space = isl_map_get_space(res->dep[i].map);
  953. if (is_before)
  954. before = isl_map_lex_le_first(space, plevel);
  955. else
  956. before = isl_map_lex_lt_first(space, plevel);
  957. dep = isl_map_apply_range(isl_map_copy(acc->source[i].map),
  958. isl_map_reverse(isl_map_copy(acc->sink.map)));
  959. dep = isl_map_intersect(dep, before);
  960. mustdo = isl_set_subtract(mustdo,
  961. isl_map_range(isl_map_copy(dep)));
  962. res->dep[i].map = isl_map_union(res->dep[i].map, dep);
  963. }
  964. res->may_no_source = isl_set_subtract(maydo, isl_set_copy(mustdo));
  965. res->must_no_source = mustdo;
  966. return res;
  967. error:
  968. isl_set_free(mustdo);
  969. isl_set_free(maydo);
  970. isl_flow_free(res);
  971. return NULL;
  972. }
  973. /* Compute dependences for the case where there is at least one
  974. * "must" access.
  975. *
  976. * The core algorithm considers all levels in which a source may precede
  977. * the sink, where a level may either be a statement level or a loop level.
  978. * The outermost statement level is 1, the first loop level is 2, etc...
  979. * The algorithm basically does the following:
  980. * for all levels l of the read access from innermost to outermost
  981. * for all sources w that may precede the sink access at that level
  982. * compute the last iteration of the source that precedes the sink access
  983. * at that level
  984. * add result to possible last accesses at level l of source w
  985. * for all sources w2 that we haven't considered yet at this level that may
  986. * also precede the sink access
  987. * for all levels l2 of w from l to innermost
  988. * for all possible last accesses dep of w at l
  989. * compute last iteration of w2 between the source and sink
  990. * of dep
  991. * add result to possible last accesses at level l of write w2
  992. * and replace possible last accesses dep by the remainder
  993. *
  994. *
  995. * The above algorithm is applied to the must access. During the course
  996. * of the algorithm, we keep track of sink iterations that still
  997. * need to be considered. These iterations are split into those that
  998. * haven't been matched to any source access (mustdo) and those that have only
  999. * been matched to may accesses (maydo).
  1000. * At the end of each level, must-sources and may-sources that are coscheduled
  1001. * with the sources of the must-dependences at that level are considered.
  1002. * If any coscheduled instances are found, then corresponding may-dependences
  1003. * are added and the original must-dependences are turned into may-dependences.
  1004. * Afterwards, the may accesses that occur after must-dependence sources
  1005. * are considered.
  1006. * In particular, we consider may accesses that precede the remaining
  1007. * sink iterations, moving elements from mustdo to maydo when appropriate,
  1008. * and may accesses that occur between a must source and a sink of any
  1009. * dependences found at the current level, turning must dependences into
  1010. * may dependences when appropriate.
  1011. *
  1012. */
  1013. static __isl_give isl_flow *compute_val_based_dependences(
  1014. __isl_keep isl_access_info *acc)
  1015. {
  1016. isl_ctx *ctx;
  1017. isl_flow *res;
  1018. isl_set *mustdo = NULL;
  1019. isl_set *maydo = NULL;
  1020. int level, j;
  1021. isl_size n_in;
  1022. int depth;
  1023. isl_map **must_rel = NULL;
  1024. isl_map **may_rel = NULL;
  1025. if (!acc)
  1026. return NULL;
  1027. res = isl_flow_alloc(acc);
  1028. if (!res)
  1029. goto error;
  1030. ctx = isl_map_get_ctx(acc->sink.map);
  1031. n_in = isl_map_dim(acc->sink.map, isl_dim_in);
  1032. if (n_in < 0)
  1033. goto error;
  1034. depth = 2 * n_in + 1;
  1035. mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
  1036. maydo = isl_set_empty(isl_set_get_space(mustdo));
  1037. if (!mustdo || !maydo)
  1038. goto error;
  1039. if (isl_set_plain_is_empty(mustdo))
  1040. goto done;
  1041. must_rel = isl_calloc_array(ctx, struct isl_map *, acc->n_must);
  1042. may_rel = isl_calloc_array(ctx, struct isl_map *, acc->n_must);
  1043. if (!must_rel || !may_rel)
  1044. goto error;
  1045. for (level = depth; level >= 1; --level) {
  1046. for (j = acc->n_must-1; j >=0; --j) {
  1047. isl_space *space;
  1048. space = isl_map_get_space(res->dep[2 * j].map);
  1049. must_rel[j] = isl_map_empty(space);
  1050. may_rel[j] = isl_map_copy(must_rel[j]);
  1051. }
  1052. for (j = acc->n_must - 1; j >= 0; --j) {
  1053. struct isl_map *T;
  1054. struct isl_set *rest;
  1055. int plevel;
  1056. plevel = acc->level_before(acc->source[j].data,
  1057. acc->sink.data);
  1058. if (plevel < 0)
  1059. goto error;
  1060. if (!can_precede_at_level(plevel, level))
  1061. continue;
  1062. T = last_source(acc, mustdo, j, level, &rest);
  1063. must_rel[j] = isl_map_union_disjoint(must_rel[j], T);
  1064. mustdo = rest;
  1065. if (intermediate_sources(acc, must_rel, j, level) < 0)
  1066. goto error;
  1067. T = last_source(acc, maydo, j, level, &rest);
  1068. may_rel[j] = isl_map_union_disjoint(may_rel[j], T);
  1069. maydo = rest;
  1070. if (intermediate_sources(acc, may_rel, j, level) < 0)
  1071. goto error;
  1072. if (isl_set_plain_is_empty(mustdo) &&
  1073. isl_set_plain_is_empty(maydo))
  1074. break;
  1075. }
  1076. for (j = j - 1; j >= 0; --j) {
  1077. int plevel;
  1078. plevel = acc->level_before(acc->source[j].data,
  1079. acc->sink.data);
  1080. if (plevel < 0)
  1081. goto error;
  1082. if (!can_precede_at_level(plevel, level))
  1083. continue;
  1084. if (intermediate_sources(acc, must_rel, j, level) < 0)
  1085. goto error;
  1086. if (intermediate_sources(acc, may_rel, j, level) < 0)
  1087. goto error;
  1088. }
  1089. res = handle_coscheduled(acc, must_rel, may_rel, res);
  1090. if (!res)
  1091. goto error;
  1092. for (j = 0; j < acc->n_may; ++j) {
  1093. int plevel;
  1094. isl_map *T;
  1095. isl_set *ran;
  1096. plevel = acc->level_before(acc->source[acc->n_must + j].data,
  1097. acc->sink.data);
  1098. if (plevel < 0)
  1099. goto error;
  1100. if (!can_precede_at_level(plevel, level))
  1101. continue;
  1102. T = all_sources(acc, isl_set_copy(maydo), j, level);
  1103. res->dep[2 * acc->n_must + j].map =
  1104. isl_map_union(res->dep[2 * acc->n_must + j].map, T);
  1105. T = all_sources(acc, isl_set_copy(mustdo), j, level);
  1106. ran = isl_map_range(isl_map_copy(T));
  1107. res->dep[2 * acc->n_must + j].map =
  1108. isl_map_union(res->dep[2 * acc->n_must + j].map, T);
  1109. mustdo = isl_set_subtract(mustdo, isl_set_copy(ran));
  1110. maydo = isl_set_union_disjoint(maydo, ran);
  1111. T = res->dep[2 * acc->n_must + j].map;
  1112. T = all_intermediate_sources(acc, T, must_rel, may_rel,
  1113. j, level);
  1114. res->dep[2 * acc->n_must + j].map = T;
  1115. }
  1116. for (j = acc->n_must - 1; j >= 0; --j) {
  1117. res->dep[2 * j].map =
  1118. isl_map_union_disjoint(res->dep[2 * j].map,
  1119. must_rel[j]);
  1120. res->dep[2 * j + 1].map =
  1121. isl_map_union_disjoint(res->dep[2 * j + 1].map,
  1122. may_rel[j]);
  1123. }
  1124. if (isl_set_plain_is_empty(mustdo) &&
  1125. isl_set_plain_is_empty(maydo))
  1126. break;
  1127. }
  1128. free(must_rel);
  1129. free(may_rel);
  1130. done:
  1131. res->must_no_source = mustdo;
  1132. res->may_no_source = maydo;
  1133. return res;
  1134. error:
  1135. if (must_rel)
  1136. for (j = 0; j < acc->n_must; ++j)
  1137. isl_map_free(must_rel[j]);
  1138. if (may_rel)
  1139. for (j = 0; j < acc->n_must; ++j)
  1140. isl_map_free(may_rel[j]);
  1141. isl_flow_free(res);
  1142. isl_set_free(mustdo);
  1143. isl_set_free(maydo);
  1144. free(must_rel);
  1145. free(may_rel);
  1146. return NULL;
  1147. }
  1148. /* Given a "sink" access, a list of n "source" accesses,
  1149. * compute for each iteration of the sink access
  1150. * and for each element accessed by that iteration,
  1151. * the source access in the list that last accessed the
  1152. * element accessed by the sink access before this sink access.
  1153. * Each access is given as a map from the loop iterators
  1154. * to the array indices.
  1155. * The result is a list of n relations between source and sink
  1156. * iterations and a subset of the domain of the sink access,
  1157. * corresponding to those iterations that access an element
  1158. * not previously accessed.
  1159. *
  1160. * To deal with multi-valued sink access relations, the sink iteration
  1161. * domain is first extended with dimensions that correspond to the data
  1162. * space. However, these extra dimensions are not projected out again.
  1163. * It is up to the caller to decide whether these dimensions should be kept.
  1164. */
  1165. static __isl_give isl_flow *access_info_compute_flow_core(
  1166. __isl_take isl_access_info *acc)
  1167. {
  1168. struct isl_flow *res = NULL;
  1169. if (!acc)
  1170. return NULL;
  1171. acc->sink.map = isl_map_range_map(acc->sink.map);
  1172. if (!acc->sink.map)
  1173. goto error;
  1174. if (acc->n_must == 0)
  1175. res = compute_mem_based_dependences(acc);
  1176. else {
  1177. acc = isl_access_info_sort_sources(acc);
  1178. res = compute_val_based_dependences(acc);
  1179. }
  1180. acc = isl_access_info_free(acc);
  1181. if (!res)
  1182. return NULL;
  1183. if (!res->must_no_source || !res->may_no_source)
  1184. goto error;
  1185. return res;
  1186. error:
  1187. isl_access_info_free(acc);
  1188. isl_flow_free(res);
  1189. return NULL;
  1190. }
  1191. /* Given a "sink" access, a list of n "source" accesses,
  1192. * compute for each iteration of the sink access
  1193. * and for each element accessed by that iteration,
  1194. * the source access in the list that last accessed the
  1195. * element accessed by the sink access before this sink access.
  1196. * Each access is given as a map from the loop iterators
  1197. * to the array indices.
  1198. * The result is a list of n relations between source and sink
  1199. * iterations and a subset of the domain of the sink access,
  1200. * corresponding to those iterations that access an element
  1201. * not previously accessed.
  1202. *
  1203. * To deal with multi-valued sink access relations,
  1204. * access_info_compute_flow_core extends the sink iteration domain
  1205. * with dimensions that correspond to the data space. These extra dimensions
  1206. * are projected out from the result of access_info_compute_flow_core.
  1207. */
  1208. __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
  1209. {
  1210. int j;
  1211. struct isl_flow *res;
  1212. if (!acc)
  1213. return NULL;
  1214. acc->domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
  1215. res = access_info_compute_flow_core(acc);
  1216. if (!res)
  1217. return NULL;
  1218. for (j = 0; j < res->n_source; ++j) {
  1219. res->dep[j].map = isl_map_range_factor_domain(res->dep[j].map);
  1220. if (!res->dep[j].map)
  1221. goto error;
  1222. }
  1223. return res;
  1224. error:
  1225. isl_flow_free(res);
  1226. return NULL;
  1227. }
  1228. /* Keep track of some information about a schedule for a given
  1229. * access. In particular, keep track of which dimensions
  1230. * have a constant value and of the actual constant values.
  1231. */
  1232. struct isl_sched_info {
  1233. int *is_cst;
  1234. isl_vec *cst;
  1235. };
  1236. static void sched_info_free(__isl_take struct isl_sched_info *info)
  1237. {
  1238. if (!info)
  1239. return;
  1240. isl_vec_free(info->cst);
  1241. free(info->is_cst);
  1242. free(info);
  1243. }
  1244. /* Extract information on the constant dimensions of the schedule
  1245. * for a given access. The "map" is of the form
  1246. *
  1247. * [S -> D] -> A
  1248. *
  1249. * with S the schedule domain, D the iteration domain and A the data domain.
  1250. */
  1251. static __isl_give struct isl_sched_info *sched_info_alloc(
  1252. __isl_keep isl_map *map)
  1253. {
  1254. isl_ctx *ctx;
  1255. isl_space *space;
  1256. struct isl_sched_info *info;
  1257. int i;
  1258. isl_size n;
  1259. if (!map)
  1260. return NULL;
  1261. space = isl_space_unwrap(isl_space_domain(isl_map_get_space(map)));
  1262. if (!space)
  1263. return NULL;
  1264. n = isl_space_dim(space, isl_dim_in);
  1265. isl_space_free(space);
  1266. if (n < 0)
  1267. return NULL;
  1268. ctx = isl_map_get_ctx(map);
  1269. info = isl_alloc_type(ctx, struct isl_sched_info);
  1270. if (!info)
  1271. return NULL;
  1272. info->is_cst = isl_alloc_array(ctx, int, n);
  1273. info->cst = isl_vec_alloc(ctx, n);
  1274. if (n && (!info->is_cst || !info->cst))
  1275. goto error;
  1276. for (i = 0; i < n; ++i) {
  1277. isl_val *v;
  1278. v = isl_map_plain_get_val_if_fixed(map, isl_dim_in, i);
  1279. if (!v)
  1280. goto error;
  1281. info->is_cst[i] = !isl_val_is_nan(v);
  1282. if (info->is_cst[i])
  1283. info->cst = isl_vec_set_element_val(info->cst, i, v);
  1284. else
  1285. isl_val_free(v);
  1286. }
  1287. return info;
  1288. error:
  1289. sched_info_free(info);
  1290. return NULL;
  1291. }
  1292. /* The different types of access relations that isl_union_access_info
  1293. * keeps track of.
  1294. * "isl_access_sink" represents the sink accesses.
  1295. * "isl_access_must_source" represents the definite source accesses.
  1296. * "isl_access_may_source" represents the possible source accesses.
  1297. * "isl_access_kill" represents the kills.
  1298. *
  1299. * isl_access_sink is sometimes treated differently and
  1300. * should therefore appear first.
  1301. */
  1302. enum isl_access_type {
  1303. isl_access_sink,
  1304. isl_access_must_source,
  1305. isl_access_may_source,
  1306. isl_access_kill,
  1307. isl_access_end
  1308. };
  1309. /* This structure represents the input for a dependence analysis computation.
  1310. *
  1311. * "access" contains the access relations.
  1312. *
  1313. * "schedule" or "schedule_map" represents the execution order.
  1314. * Exactly one of these fields should be NULL. The other field
  1315. * determines the execution order.
  1316. *
  1317. * The domains of these four maps refer to the same iteration spaces(s).
  1318. * The ranges of the first three maps also refer to the same data space(s).
  1319. *
  1320. * After a call to isl_union_access_info_introduce_schedule,
  1321. * the "schedule_map" field no longer contains useful information.
  1322. */
  1323. struct isl_union_access_info {
  1324. isl_union_map *access[isl_access_end];
  1325. isl_schedule *schedule;
  1326. isl_union_map *schedule_map;
  1327. };
  1328. /* Free "access" and return NULL.
  1329. */
  1330. __isl_null isl_union_access_info *isl_union_access_info_free(
  1331. __isl_take isl_union_access_info *access)
  1332. {
  1333. enum isl_access_type i;
  1334. if (!access)
  1335. return NULL;
  1336. for (i = isl_access_sink; i < isl_access_end; ++i)
  1337. isl_union_map_free(access->access[i]);
  1338. isl_schedule_free(access->schedule);
  1339. isl_union_map_free(access->schedule_map);
  1340. free(access);
  1341. return NULL;
  1342. }
  1343. /* Return the isl_ctx to which "access" belongs.
  1344. */
  1345. isl_ctx *isl_union_access_info_get_ctx(__isl_keep isl_union_access_info *access)
  1346. {
  1347. if (!access)
  1348. return NULL;
  1349. return isl_union_map_get_ctx(access->access[isl_access_sink]);
  1350. }
  1351. /* Construct an empty (invalid) isl_union_access_info object.
  1352. * The caller is responsible for setting the sink access relation and
  1353. * initializing all the other fields, e.g., by calling
  1354. * isl_union_access_info_init.
  1355. */
  1356. static __isl_give isl_union_access_info *isl_union_access_info_alloc(
  1357. isl_ctx *ctx)
  1358. {
  1359. return isl_calloc_type(ctx, isl_union_access_info);
  1360. }
  1361. /* Initialize all the fields of "info", except the sink access relation,
  1362. * which is assumed to have been set by the caller.
  1363. *
  1364. * By default, we use the schedule field of the isl_union_access_info,
  1365. * but this may be overridden by a call
  1366. * to isl_union_access_info_set_schedule_map.
  1367. */
  1368. static __isl_give isl_union_access_info *isl_union_access_info_init(
  1369. __isl_take isl_union_access_info *info)
  1370. {
  1371. isl_space *space;
  1372. isl_union_map *empty;
  1373. enum isl_access_type i;
  1374. if (!info)
  1375. return NULL;
  1376. if (!info->access[isl_access_sink])
  1377. return isl_union_access_info_free(info);
  1378. space = isl_union_map_get_space(info->access[isl_access_sink]);
  1379. empty = isl_union_map_empty(isl_space_copy(space));
  1380. for (i = isl_access_sink + 1; i < isl_access_end; ++i)
  1381. if (!info->access[i])
  1382. info->access[i] = isl_union_map_copy(empty);
  1383. isl_union_map_free(empty);
  1384. if (!info->schedule && !info->schedule_map)
  1385. info->schedule = isl_schedule_empty(isl_space_copy(space));
  1386. isl_space_free(space);
  1387. for (i = isl_access_sink + 1; i < isl_access_end; ++i)
  1388. if (!info->access[i])
  1389. return isl_union_access_info_free(info);
  1390. if (!info->schedule && !info->schedule_map)
  1391. return isl_union_access_info_free(info);
  1392. return info;
  1393. }
  1394. /* Create a new isl_union_access_info with the given sink accesses and
  1395. * and no other accesses or schedule information.
  1396. */
  1397. __isl_give isl_union_access_info *isl_union_access_info_from_sink(
  1398. __isl_take isl_union_map *sink)
  1399. {
  1400. isl_ctx *ctx;
  1401. isl_union_access_info *access;
  1402. if (!sink)
  1403. return NULL;
  1404. ctx = isl_union_map_get_ctx(sink);
  1405. access = isl_union_access_info_alloc(ctx);
  1406. if (!access)
  1407. goto error;
  1408. access->access[isl_access_sink] = sink;
  1409. return isl_union_access_info_init(access);
  1410. error:
  1411. isl_union_map_free(sink);
  1412. return NULL;
  1413. }
  1414. /* Replace the access relation of type "type" of "info" by "access".
  1415. */
  1416. static __isl_give isl_union_access_info *isl_union_access_info_set(
  1417. __isl_take isl_union_access_info *info,
  1418. enum isl_access_type type, __isl_take isl_union_map *access)
  1419. {
  1420. if (!info || !access)
  1421. goto error;
  1422. isl_union_map_free(info->access[type]);
  1423. info->access[type] = access;
  1424. return info;
  1425. error:
  1426. isl_union_access_info_free(info);
  1427. isl_union_map_free(access);
  1428. return NULL;
  1429. }
  1430. /* Replace the definite source accesses of "access" by "must_source".
  1431. */
  1432. __isl_give isl_union_access_info *isl_union_access_info_set_must_source(
  1433. __isl_take isl_union_access_info *access,
  1434. __isl_take isl_union_map *must_source)
  1435. {
  1436. return isl_union_access_info_set(access, isl_access_must_source,
  1437. must_source);
  1438. }
  1439. /* Replace the possible source accesses of "access" by "may_source".
  1440. */
  1441. __isl_give isl_union_access_info *isl_union_access_info_set_may_source(
  1442. __isl_take isl_union_access_info *access,
  1443. __isl_take isl_union_map *may_source)
  1444. {
  1445. return isl_union_access_info_set(access, isl_access_may_source,
  1446. may_source);
  1447. }
  1448. /* Replace the kills of "info" by "kill".
  1449. */
  1450. __isl_give isl_union_access_info *isl_union_access_info_set_kill(
  1451. __isl_take isl_union_access_info *info, __isl_take isl_union_map *kill)
  1452. {
  1453. return isl_union_access_info_set(info, isl_access_kill, kill);
  1454. }
  1455. /* Return the access relation of type "type" of "info".
  1456. */
  1457. static __isl_give isl_union_map *isl_union_access_info_get(
  1458. __isl_keep isl_union_access_info *info, enum isl_access_type type)
  1459. {
  1460. if (!info)
  1461. return NULL;
  1462. return isl_union_map_copy(info->access[type]);
  1463. }
  1464. /* Return the definite source accesses of "info".
  1465. */
  1466. __isl_give isl_union_map *isl_union_access_info_get_must_source(
  1467. __isl_keep isl_union_access_info *info)
  1468. {
  1469. return isl_union_access_info_get(info, isl_access_must_source);
  1470. }
  1471. /* Return the possible source accesses of "info".
  1472. */
  1473. __isl_give isl_union_map *isl_union_access_info_get_may_source(
  1474. __isl_keep isl_union_access_info *info)
  1475. {
  1476. return isl_union_access_info_get(info, isl_access_may_source);
  1477. }
  1478. /* Return the kills of "info".
  1479. */
  1480. __isl_give isl_union_map *isl_union_access_info_get_kill(
  1481. __isl_keep isl_union_access_info *info)
  1482. {
  1483. return isl_union_access_info_get(info, isl_access_kill);
  1484. }
  1485. /* Does "info" specify any kills?
  1486. */
  1487. static isl_bool isl_union_access_has_kill(
  1488. __isl_keep isl_union_access_info *info)
  1489. {
  1490. isl_bool empty;
  1491. if (!info)
  1492. return isl_bool_error;
  1493. empty = isl_union_map_is_empty(info->access[isl_access_kill]);
  1494. return isl_bool_not(empty);
  1495. }
  1496. /* Replace the schedule of "access" by "schedule".
  1497. * Also free the schedule_map in case it was set last.
  1498. */
  1499. __isl_give isl_union_access_info *isl_union_access_info_set_schedule(
  1500. __isl_take isl_union_access_info *access,
  1501. __isl_take isl_schedule *schedule)
  1502. {
  1503. if (!access || !schedule)
  1504. goto error;
  1505. access->schedule_map = isl_union_map_free(access->schedule_map);
  1506. isl_schedule_free(access->schedule);
  1507. access->schedule = schedule;
  1508. return access;
  1509. error:
  1510. isl_union_access_info_free(access);
  1511. isl_schedule_free(schedule);
  1512. return NULL;
  1513. }
  1514. /* Replace the schedule map of "access" by "schedule_map".
  1515. * Also free the schedule in case it was set last.
  1516. */
  1517. __isl_give isl_union_access_info *isl_union_access_info_set_schedule_map(
  1518. __isl_take isl_union_access_info *access,
  1519. __isl_take isl_union_map *schedule_map)
  1520. {
  1521. if (!access || !schedule_map)
  1522. goto error;
  1523. isl_union_map_free(access->schedule_map);
  1524. access->schedule = isl_schedule_free(access->schedule);
  1525. access->schedule_map = schedule_map;
  1526. return access;
  1527. error:
  1528. isl_union_access_info_free(access);
  1529. isl_union_map_free(schedule_map);
  1530. return NULL;
  1531. }
  1532. __isl_give isl_union_access_info *isl_union_access_info_copy(
  1533. __isl_keep isl_union_access_info *access)
  1534. {
  1535. isl_union_access_info *copy;
  1536. enum isl_access_type i;
  1537. if (!access)
  1538. return NULL;
  1539. copy = isl_union_access_info_from_sink(
  1540. isl_union_map_copy(access->access[isl_access_sink]));
  1541. for (i = isl_access_sink + 1; i < isl_access_end; ++i)
  1542. copy = isl_union_access_info_set(copy, i,
  1543. isl_union_map_copy(access->access[i]));
  1544. if (access->schedule)
  1545. copy = isl_union_access_info_set_schedule(copy,
  1546. isl_schedule_copy(access->schedule));
  1547. else
  1548. copy = isl_union_access_info_set_schedule_map(copy,
  1549. isl_union_map_copy(access->schedule_map));
  1550. return copy;
  1551. }
  1552. #undef BASE
  1553. #define BASE union_map
  1554. #include "print_yaml_field_templ.c"
  1555. /* An enumeration of the various keys that may appear in a YAML mapping
  1556. * of an isl_union_access_info object.
  1557. * The keys for the access relation types are assumed to have the same values
  1558. * as the access relation types in isl_access_type.
  1559. */
  1560. enum isl_ai_key {
  1561. isl_ai_key_error = -1,
  1562. isl_ai_key_sink = isl_access_sink,
  1563. isl_ai_key_must_source = isl_access_must_source,
  1564. isl_ai_key_may_source = isl_access_may_source,
  1565. isl_ai_key_kill = isl_access_kill,
  1566. isl_ai_key_schedule_map,
  1567. isl_ai_key_schedule,
  1568. isl_ai_key_end
  1569. };
  1570. /* Textual representations of the YAML keys for an isl_union_access_info
  1571. * object.
  1572. */
  1573. static char *key_str[] = {
  1574. [isl_ai_key_sink] = "sink",
  1575. [isl_ai_key_must_source] = "must_source",
  1576. [isl_ai_key_may_source] = "may_source",
  1577. [isl_ai_key_kill] = "kill",
  1578. [isl_ai_key_schedule_map] = "schedule_map",
  1579. [isl_ai_key_schedule] = "schedule",
  1580. };
  1581. /* Print a key-value pair corresponding to the access relation of type "type"
  1582. * of a YAML mapping of "info" to "p".
  1583. *
  1584. * The sink access relation is always printed, but any other access relation
  1585. * is only printed if it is non-empty.
  1586. */
  1587. static __isl_give isl_printer *print_access_field(__isl_take isl_printer *p,
  1588. __isl_keep isl_union_access_info *info, enum isl_access_type type)
  1589. {
  1590. if (type != isl_access_sink) {
  1591. isl_bool empty;
  1592. empty = isl_union_map_is_empty(info->access[type]);
  1593. if (empty < 0)
  1594. return isl_printer_free(p);
  1595. if (empty)
  1596. return p;
  1597. }
  1598. return print_yaml_field_union_map(p, key_str[type], info->access[type]);
  1599. }
  1600. /* Print the information contained in "access" to "p".
  1601. * The information is printed as a YAML document.
  1602. */
  1603. __isl_give isl_printer *isl_printer_print_union_access_info(
  1604. __isl_take isl_printer *p, __isl_keep isl_union_access_info *access)
  1605. {
  1606. enum isl_access_type i;
  1607. if (!access)
  1608. return isl_printer_free(p);
  1609. p = isl_printer_yaml_start_mapping(p);
  1610. for (i = isl_access_sink; i < isl_access_end; ++i)
  1611. p = print_access_field(p, access, i);
  1612. if (access->schedule) {
  1613. p = isl_printer_print_str(p, key_str[isl_ai_key_schedule]);
  1614. p = isl_printer_yaml_next(p);
  1615. p = isl_printer_print_schedule(p, access->schedule);
  1616. p = isl_printer_yaml_next(p);
  1617. } else {
  1618. p = print_yaml_field_union_map(p,
  1619. key_str[isl_ai_key_schedule_map], access->schedule_map);
  1620. }
  1621. p = isl_printer_yaml_end_mapping(p);
  1622. return p;
  1623. }
  1624. /* Return a string representation of the information in "access".
  1625. * The information is printed in flow format.
  1626. */
  1627. __isl_give char *isl_union_access_info_to_str(
  1628. __isl_keep isl_union_access_info *access)
  1629. {
  1630. isl_printer *p;
  1631. char *s;
  1632. if (!access)
  1633. return NULL;
  1634. p = isl_printer_to_str(isl_union_access_info_get_ctx(access));
  1635. p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
  1636. p = isl_printer_print_union_access_info(p, access);
  1637. s = isl_printer_get_str(p);
  1638. isl_printer_free(p);
  1639. return s;
  1640. }
  1641. #undef KEY
  1642. #define KEY enum isl_ai_key
  1643. #undef KEY_ERROR
  1644. #define KEY_ERROR isl_ai_key_error
  1645. #undef KEY_END
  1646. #define KEY_END isl_ai_key_end
  1647. #include "extract_key.c"
  1648. #undef BASE
  1649. #define BASE union_map
  1650. #include "read_in_string_templ.c"
  1651. /* Read an isl_union_access_info object from "s".
  1652. *
  1653. * Start off with an empty (invalid) isl_union_access_info object and
  1654. * then fill up the fields based on the input.
  1655. * The input needs to contain at least a description of the sink
  1656. * access relation as well as some form of schedule.
  1657. * The other access relations are set to empty relations
  1658. * by isl_union_access_info_init if they are not specified in the input.
  1659. */
  1660. __isl_give isl_union_access_info *isl_stream_read_union_access_info(
  1661. isl_stream *s)
  1662. {
  1663. isl_ctx *ctx;
  1664. isl_union_access_info *info;
  1665. int more;
  1666. int sink_set = 0;
  1667. int schedule_set = 0;
  1668. if (isl_stream_yaml_read_start_mapping(s))
  1669. return NULL;
  1670. ctx = isl_stream_get_ctx(s);
  1671. info = isl_union_access_info_alloc(ctx);
  1672. while ((more = isl_stream_yaml_next(s)) > 0) {
  1673. enum isl_ai_key key;
  1674. isl_union_map *access, *schedule_map;
  1675. isl_schedule *schedule;
  1676. key = get_key(s);
  1677. if (isl_stream_yaml_next(s) < 0)
  1678. return isl_union_access_info_free(info);
  1679. switch (key) {
  1680. case isl_ai_key_end:
  1681. case isl_ai_key_error:
  1682. return isl_union_access_info_free(info);
  1683. case isl_ai_key_sink:
  1684. sink_set = 1;
  1685. case isl_ai_key_must_source:
  1686. case isl_ai_key_may_source:
  1687. case isl_ai_key_kill:
  1688. access = read_union_map(s);
  1689. info = isl_union_access_info_set(info, key, access);
  1690. if (!info)
  1691. return NULL;
  1692. break;
  1693. case isl_ai_key_schedule_map:
  1694. schedule_set = 1;
  1695. schedule_map = read_union_map(s);
  1696. info = isl_union_access_info_set_schedule_map(info,
  1697. schedule_map);
  1698. if (!info)
  1699. return NULL;
  1700. break;
  1701. case isl_ai_key_schedule:
  1702. schedule_set = 1;
  1703. schedule = isl_stream_read_schedule(s);
  1704. info = isl_union_access_info_set_schedule(info,
  1705. schedule);
  1706. if (!info)
  1707. return NULL;
  1708. break;
  1709. }
  1710. }
  1711. if (more < 0)
  1712. return isl_union_access_info_free(info);
  1713. if (isl_stream_yaml_read_end_mapping(s) < 0) {
  1714. isl_stream_error(s, NULL, "unexpected extra elements");
  1715. return isl_union_access_info_free(info);
  1716. }
  1717. if (!sink_set) {
  1718. isl_stream_error(s, NULL, "no sink specified");
  1719. return isl_union_access_info_free(info);
  1720. }
  1721. if (!schedule_set) {
  1722. isl_stream_error(s, NULL, "no schedule specified");
  1723. return isl_union_access_info_free(info);
  1724. }
  1725. return isl_union_access_info_init(info);
  1726. }
  1727. /* Read an isl_union_access_info object from the file "input".
  1728. */
  1729. __isl_give isl_union_access_info *isl_union_access_info_read_from_file(
  1730. isl_ctx *ctx, FILE *input)
  1731. {
  1732. isl_stream *s;
  1733. isl_union_access_info *access;
  1734. s = isl_stream_new_file(ctx, input);
  1735. if (!s)
  1736. return NULL;
  1737. access = isl_stream_read_union_access_info(s);
  1738. isl_stream_free(s);
  1739. return access;
  1740. }
  1741. /* Update the fields of "access" such that they all have the same parameters,
  1742. * keeping in mind that the schedule_map field may be NULL and ignoring
  1743. * the schedule field.
  1744. */
  1745. static __isl_give isl_union_access_info *isl_union_access_info_align_params(
  1746. __isl_take isl_union_access_info *access)
  1747. {
  1748. isl_space *space;
  1749. enum isl_access_type i;
  1750. if (!access)
  1751. return NULL;
  1752. space = isl_union_map_get_space(access->access[isl_access_sink]);
  1753. for (i = isl_access_sink + 1; i < isl_access_end; ++i)
  1754. space = isl_space_align_params(space,
  1755. isl_union_map_get_space(access->access[i]));
  1756. if (access->schedule_map)
  1757. space = isl_space_align_params(space,
  1758. isl_union_map_get_space(access->schedule_map));
  1759. for (i = isl_access_sink; i < isl_access_end; ++i)
  1760. access->access[i] =
  1761. isl_union_map_align_params(access->access[i],
  1762. isl_space_copy(space));
  1763. if (!access->schedule_map) {
  1764. isl_space_free(space);
  1765. } else {
  1766. access->schedule_map =
  1767. isl_union_map_align_params(access->schedule_map, space);
  1768. if (!access->schedule_map)
  1769. return isl_union_access_info_free(access);
  1770. }
  1771. for (i = isl_access_sink; i < isl_access_end; ++i)
  1772. if (!access->access[i])
  1773. return isl_union_access_info_free(access);
  1774. return access;
  1775. }
  1776. /* Prepend the schedule dimensions to the iteration domains.
  1777. *
  1778. * That is, if the schedule is of the form
  1779. *
  1780. * D -> S
  1781. *
  1782. * while the access relations are of the form
  1783. *
  1784. * D -> A
  1785. *
  1786. * then the updated access relations are of the form
  1787. *
  1788. * [S -> D] -> A
  1789. *
  1790. * The schedule map is also replaced by the map
  1791. *
  1792. * [S -> D] -> D
  1793. *
  1794. * that is used during the internal computation.
  1795. * Neither the original schedule map nor this updated schedule map
  1796. * are used after the call to this function.
  1797. */
  1798. static __isl_give isl_union_access_info *
  1799. isl_union_access_info_introduce_schedule(
  1800. __isl_take isl_union_access_info *access)
  1801. {
  1802. isl_union_map *sm;
  1803. enum isl_access_type i;
  1804. if (!access)
  1805. return NULL;
  1806. sm = isl_union_map_reverse(access->schedule_map);
  1807. sm = isl_union_map_range_map(sm);
  1808. for (i = isl_access_sink; i < isl_access_end; ++i)
  1809. access->access[i] =
  1810. isl_union_map_apply_range(isl_union_map_copy(sm),
  1811. access->access[i]);
  1812. access->schedule_map = sm;
  1813. for (i = isl_access_sink; i < isl_access_end; ++i)
  1814. if (!access->access[i])
  1815. return isl_union_access_info_free(access);
  1816. if (!access->schedule_map)
  1817. return isl_union_access_info_free(access);
  1818. return access;
  1819. }
  1820. /* This structure represents the result of a dependence analysis computation.
  1821. *
  1822. * "must_dep" represents the full definite dependences
  1823. * "may_dep" represents the full non-definite dependences.
  1824. * Both are of the form
  1825. *
  1826. * [Source] -> [[Sink -> Data]]
  1827. *
  1828. * (after the schedule dimensions have been projected out).
  1829. * "must_no_source" represents the subset of the sink accesses for which
  1830. * definitely no source was found.
  1831. * "may_no_source" represents the subset of the sink accesses for which
  1832. * possibly, but not definitely, no source was found.
  1833. */
  1834. struct isl_union_flow {
  1835. isl_union_map *must_dep;
  1836. isl_union_map *may_dep;
  1837. isl_union_map *must_no_source;
  1838. isl_union_map *may_no_source;
  1839. };
  1840. /* Return the isl_ctx to which "flow" belongs.
  1841. */
  1842. isl_ctx *isl_union_flow_get_ctx(__isl_keep isl_union_flow *flow)
  1843. {
  1844. return flow ? isl_union_map_get_ctx(flow->must_dep) : NULL;
  1845. }
  1846. /* Free "flow" and return NULL.
  1847. */
  1848. __isl_null isl_union_flow *isl_union_flow_free(__isl_take isl_union_flow *flow)
  1849. {
  1850. if (!flow)
  1851. return NULL;
  1852. isl_union_map_free(flow->must_dep);
  1853. isl_union_map_free(flow->may_dep);
  1854. isl_union_map_free(flow->must_no_source);
  1855. isl_union_map_free(flow->may_no_source);
  1856. free(flow);
  1857. return NULL;
  1858. }
  1859. void isl_union_flow_dump(__isl_keep isl_union_flow *flow)
  1860. {
  1861. if (!flow)
  1862. return;
  1863. fprintf(stderr, "must dependences: ");
  1864. isl_union_map_dump(flow->must_dep);
  1865. fprintf(stderr, "may dependences: ");
  1866. isl_union_map_dump(flow->may_dep);
  1867. fprintf(stderr, "must no source: ");
  1868. isl_union_map_dump(flow->must_no_source);
  1869. fprintf(stderr, "may no source: ");
  1870. isl_union_map_dump(flow->may_no_source);
  1871. }
  1872. /* Return the full definite dependences in "flow", with accessed elements.
  1873. */
  1874. __isl_give isl_union_map *isl_union_flow_get_full_must_dependence(
  1875. __isl_keep isl_union_flow *flow)
  1876. {
  1877. if (!flow)
  1878. return NULL;
  1879. return isl_union_map_copy(flow->must_dep);
  1880. }
  1881. /* Return the full possible dependences in "flow", including the definite
  1882. * dependences, with accessed elements.
  1883. */
  1884. __isl_give isl_union_map *isl_union_flow_get_full_may_dependence(
  1885. __isl_keep isl_union_flow *flow)
  1886. {
  1887. if (!flow)
  1888. return NULL;
  1889. return isl_union_map_union(isl_union_map_copy(flow->must_dep),
  1890. isl_union_map_copy(flow->may_dep));
  1891. }
  1892. /* Return the definite dependences in "flow", without the accessed elements.
  1893. */
  1894. __isl_give isl_union_map *isl_union_flow_get_must_dependence(
  1895. __isl_keep isl_union_flow *flow)
  1896. {
  1897. isl_union_map *dep;
  1898. if (!flow)
  1899. return NULL;
  1900. dep = isl_union_map_copy(flow->must_dep);
  1901. return isl_union_map_range_factor_domain(dep);
  1902. }
  1903. /* Return the possible dependences in "flow", including the definite
  1904. * dependences, without the accessed elements.
  1905. */
  1906. __isl_give isl_union_map *isl_union_flow_get_may_dependence(
  1907. __isl_keep isl_union_flow *flow)
  1908. {
  1909. isl_union_map *dep;
  1910. if (!flow)
  1911. return NULL;
  1912. dep = isl_union_map_union(isl_union_map_copy(flow->must_dep),
  1913. isl_union_map_copy(flow->may_dep));
  1914. return isl_union_map_range_factor_domain(dep);
  1915. }
  1916. /* Return the non-definite dependences in "flow".
  1917. */
  1918. static __isl_give isl_union_map *isl_union_flow_get_non_must_dependence(
  1919. __isl_keep isl_union_flow *flow)
  1920. {
  1921. if (!flow)
  1922. return NULL;
  1923. return isl_union_map_copy(flow->may_dep);
  1924. }
  1925. /* Return the subset of the sink accesses for which definitely
  1926. * no source was found.
  1927. */
  1928. __isl_give isl_union_map *isl_union_flow_get_must_no_source(
  1929. __isl_keep isl_union_flow *flow)
  1930. {
  1931. if (!flow)
  1932. return NULL;
  1933. return isl_union_map_copy(flow->must_no_source);
  1934. }
  1935. /* Return the subset of the sink accesses for which possibly
  1936. * no source was found, including those for which definitely
  1937. * no source was found.
  1938. */
  1939. __isl_give isl_union_map *isl_union_flow_get_may_no_source(
  1940. __isl_keep isl_union_flow *flow)
  1941. {
  1942. if (!flow)
  1943. return NULL;
  1944. return isl_union_map_union(isl_union_map_copy(flow->must_no_source),
  1945. isl_union_map_copy(flow->may_no_source));
  1946. }
  1947. /* Return the subset of the sink accesses for which possibly, but not
  1948. * definitely, no source was found.
  1949. */
  1950. static __isl_give isl_union_map *isl_union_flow_get_non_must_no_source(
  1951. __isl_keep isl_union_flow *flow)
  1952. {
  1953. if (!flow)
  1954. return NULL;
  1955. return isl_union_map_copy(flow->may_no_source);
  1956. }
  1957. /* Create a new isl_union_flow object, initialized with empty
  1958. * dependence relations and sink subsets.
  1959. */
  1960. static __isl_give isl_union_flow *isl_union_flow_alloc(
  1961. __isl_take isl_space *space)
  1962. {
  1963. isl_ctx *ctx;
  1964. isl_union_map *empty;
  1965. isl_union_flow *flow;
  1966. if (!space)
  1967. return NULL;
  1968. ctx = isl_space_get_ctx(space);
  1969. flow = isl_alloc_type(ctx, isl_union_flow);
  1970. if (!flow)
  1971. goto error;
  1972. empty = isl_union_map_empty(space);
  1973. flow->must_dep = isl_union_map_copy(empty);
  1974. flow->may_dep = isl_union_map_copy(empty);
  1975. flow->must_no_source = isl_union_map_copy(empty);
  1976. flow->may_no_source = empty;
  1977. if (!flow->must_dep || !flow->may_dep ||
  1978. !flow->must_no_source || !flow->may_no_source)
  1979. return isl_union_flow_free(flow);
  1980. return flow;
  1981. error:
  1982. isl_space_free(space);
  1983. return NULL;
  1984. }
  1985. /* Copy this isl_union_flow object.
  1986. */
  1987. __isl_give isl_union_flow *isl_union_flow_copy(__isl_keep isl_union_flow *flow)
  1988. {
  1989. isl_union_flow *copy;
  1990. if (!flow)
  1991. return NULL;
  1992. copy = isl_union_flow_alloc(isl_union_map_get_space(flow->must_dep));
  1993. if (!copy)
  1994. return NULL;
  1995. copy->must_dep = isl_union_map_union(copy->must_dep,
  1996. isl_union_map_copy(flow->must_dep));
  1997. copy->may_dep = isl_union_map_union(copy->may_dep,
  1998. isl_union_map_copy(flow->may_dep));
  1999. copy->must_no_source = isl_union_map_union(copy->must_no_source,
  2000. isl_union_map_copy(flow->must_no_source));
  2001. copy->may_no_source = isl_union_map_union(copy->may_no_source,
  2002. isl_union_map_copy(flow->may_no_source));
  2003. if (!copy->must_dep || !copy->may_dep ||
  2004. !copy->must_no_source || !copy->may_no_source)
  2005. return isl_union_flow_free(copy);
  2006. return copy;
  2007. }
  2008. /* Drop the schedule dimensions from the iteration domains in "flow".
  2009. * In particular, the schedule dimensions have been prepended
  2010. * to the iteration domains prior to the dependence analysis by
  2011. * replacing the iteration domain D, by the wrapped map [S -> D].
  2012. * Replace these wrapped maps by the original D.
  2013. *
  2014. * In particular, the dependences computed by access_info_compute_flow_core
  2015. * are of the form
  2016. *
  2017. * [S -> D] -> [[S' -> D'] -> A]
  2018. *
  2019. * The schedule dimensions are projected out by first currying the range,
  2020. * resulting in
  2021. *
  2022. * [S -> D] -> [S' -> [D' -> A]]
  2023. *
  2024. * and then computing the factor range
  2025. *
  2026. * D -> [D' -> A]
  2027. */
  2028. static __isl_give isl_union_flow *isl_union_flow_drop_schedule(
  2029. __isl_take isl_union_flow *flow)
  2030. {
  2031. if (!flow)
  2032. return NULL;
  2033. flow->must_dep = isl_union_map_range_curry(flow->must_dep);
  2034. flow->must_dep = isl_union_map_factor_range(flow->must_dep);
  2035. flow->may_dep = isl_union_map_range_curry(flow->may_dep);
  2036. flow->may_dep = isl_union_map_factor_range(flow->may_dep);
  2037. flow->must_no_source =
  2038. isl_union_map_domain_factor_range(flow->must_no_source);
  2039. flow->may_no_source =
  2040. isl_union_map_domain_factor_range(flow->may_no_source);
  2041. if (!flow->must_dep || !flow->may_dep ||
  2042. !flow->must_no_source || !flow->may_no_source)
  2043. return isl_union_flow_free(flow);
  2044. return flow;
  2045. }
  2046. struct isl_compute_flow_data {
  2047. isl_union_map *must_source;
  2048. isl_union_map *may_source;
  2049. isl_union_flow *flow;
  2050. int count;
  2051. int must;
  2052. isl_space *dim;
  2053. struct isl_sched_info *sink_info;
  2054. struct isl_sched_info **source_info;
  2055. isl_access_info *accesses;
  2056. };
  2057. static isl_stat count_matching_array(__isl_take isl_map *map, void *user)
  2058. {
  2059. int eq;
  2060. isl_space *space;
  2061. struct isl_compute_flow_data *data;
  2062. data = (struct isl_compute_flow_data *)user;
  2063. space = isl_space_range(isl_map_get_space(map));
  2064. eq = isl_space_is_equal(space, data->dim);
  2065. isl_space_free(space);
  2066. isl_map_free(map);
  2067. if (eq < 0)
  2068. return isl_stat_error;
  2069. if (eq)
  2070. data->count++;
  2071. return isl_stat_ok;
  2072. }
  2073. static isl_stat collect_matching_array(__isl_take isl_map *map, void *user)
  2074. {
  2075. int eq;
  2076. isl_space *space;
  2077. struct isl_sched_info *info;
  2078. struct isl_compute_flow_data *data;
  2079. data = (struct isl_compute_flow_data *)user;
  2080. space = isl_space_range(isl_map_get_space(map));
  2081. eq = isl_space_is_equal(space, data->dim);
  2082. isl_space_free(space);
  2083. if (eq < 0)
  2084. goto error;
  2085. if (!eq) {
  2086. isl_map_free(map);
  2087. return isl_stat_ok;
  2088. }
  2089. info = sched_info_alloc(map);
  2090. data->source_info[data->count] = info;
  2091. data->accesses = isl_access_info_add_source(data->accesses,
  2092. map, data->must, info);
  2093. data->count++;
  2094. return isl_stat_ok;
  2095. error:
  2096. isl_map_free(map);
  2097. return isl_stat_error;
  2098. }
  2099. /* Determine the shared nesting level and the "textual order" of
  2100. * the given accesses.
  2101. *
  2102. * We first determine the minimal schedule dimension for both accesses.
  2103. *
  2104. * If among those dimensions, we can find one where both have a fixed
  2105. * value and if moreover those values are different, then the previous
  2106. * dimension is the last shared nesting level and the textual order
  2107. * is determined based on the order of the fixed values.
  2108. * If no such fixed values can be found, then we set the shared
  2109. * nesting level to the minimal schedule dimension, with no textual ordering.
  2110. */
  2111. static int before(void *first, void *second)
  2112. {
  2113. struct isl_sched_info *info1 = first;
  2114. struct isl_sched_info *info2 = second;
  2115. isl_size n1, n2;
  2116. int i;
  2117. n1 = isl_vec_size(info1->cst);
  2118. n2 = isl_vec_size(info2->cst);
  2119. if (n1 < 0 || n2 < 0)
  2120. return -1;
  2121. if (n2 < n1)
  2122. n1 = n2;
  2123. for (i = 0; i < n1; ++i) {
  2124. int r;
  2125. int cmp;
  2126. if (!info1->is_cst[i])
  2127. continue;
  2128. if (!info2->is_cst[i])
  2129. continue;
  2130. cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
  2131. if (cmp == 0)
  2132. continue;
  2133. r = 2 * i + (cmp < 0);
  2134. return r;
  2135. }
  2136. return 2 * n1;
  2137. }
  2138. /* Check if the given two accesses may be coscheduled.
  2139. * If so, return isl_bool_true. Otherwise return isl_bool_false.
  2140. *
  2141. * Two accesses may only be coscheduled if the fixed schedule
  2142. * coordinates have the same values.
  2143. */
  2144. static isl_bool coscheduled(void *first, void *second)
  2145. {
  2146. struct isl_sched_info *info1 = first;
  2147. struct isl_sched_info *info2 = second;
  2148. isl_size n1, n2;
  2149. int i;
  2150. n1 = isl_vec_size(info1->cst);
  2151. n2 = isl_vec_size(info2->cst);
  2152. if (n1 < 0 || n2 < 0)
  2153. return isl_bool_error;
  2154. if (n2 < n1)
  2155. n1 = n2;
  2156. for (i = 0; i < n1; ++i) {
  2157. int cmp;
  2158. if (!info1->is_cst[i])
  2159. continue;
  2160. if (!info2->is_cst[i])
  2161. continue;
  2162. cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
  2163. if (cmp != 0)
  2164. return isl_bool_false;
  2165. }
  2166. return isl_bool_true;
  2167. }
  2168. /* Given a sink access, look for all the source accesses that access
  2169. * the same array and perform dataflow analysis on them using
  2170. * isl_access_info_compute_flow_core.
  2171. */
  2172. static isl_stat compute_flow(__isl_take isl_map *map, void *user)
  2173. {
  2174. int i;
  2175. isl_ctx *ctx;
  2176. struct isl_compute_flow_data *data;
  2177. isl_flow *flow;
  2178. isl_union_flow *df;
  2179. data = (struct isl_compute_flow_data *)user;
  2180. df = data->flow;
  2181. ctx = isl_map_get_ctx(map);
  2182. data->accesses = NULL;
  2183. data->sink_info = NULL;
  2184. data->source_info = NULL;
  2185. data->count = 0;
  2186. data->dim = isl_space_range(isl_map_get_space(map));
  2187. if (isl_union_map_foreach_map(data->must_source,
  2188. &count_matching_array, data) < 0)
  2189. goto error;
  2190. if (isl_union_map_foreach_map(data->may_source,
  2191. &count_matching_array, data) < 0)
  2192. goto error;
  2193. data->sink_info = sched_info_alloc(map);
  2194. data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
  2195. data->count);
  2196. data->accesses = isl_access_info_alloc(isl_map_copy(map),
  2197. data->sink_info, &before, data->count);
  2198. if (!data->sink_info || (data->count && !data->source_info) ||
  2199. !data->accesses)
  2200. goto error;
  2201. data->accesses->coscheduled = &coscheduled;
  2202. data->count = 0;
  2203. data->must = 1;
  2204. if (isl_union_map_foreach_map(data->must_source,
  2205. &collect_matching_array, data) < 0)
  2206. goto error;
  2207. data->must = 0;
  2208. if (isl_union_map_foreach_map(data->may_source,
  2209. &collect_matching_array, data) < 0)
  2210. goto error;
  2211. flow = access_info_compute_flow_core(data->accesses);
  2212. data->accesses = NULL;
  2213. if (!flow)
  2214. goto error;
  2215. df->must_no_source = isl_union_map_union(df->must_no_source,
  2216. isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
  2217. df->may_no_source = isl_union_map_union(df->may_no_source,
  2218. isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
  2219. for (i = 0; i < flow->n_source; ++i) {
  2220. isl_union_map *dep;
  2221. dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
  2222. if (flow->dep[i].must)
  2223. df->must_dep = isl_union_map_union(df->must_dep, dep);
  2224. else
  2225. df->may_dep = isl_union_map_union(df->may_dep, dep);
  2226. }
  2227. isl_flow_free(flow);
  2228. sched_info_free(data->sink_info);
  2229. if (data->source_info) {
  2230. for (i = 0; i < data->count; ++i)
  2231. sched_info_free(data->source_info[i]);
  2232. free(data->source_info);
  2233. }
  2234. isl_space_free(data->dim);
  2235. isl_map_free(map);
  2236. return isl_stat_ok;
  2237. error:
  2238. isl_access_info_free(data->accesses);
  2239. sched_info_free(data->sink_info);
  2240. if (data->source_info) {
  2241. for (i = 0; i < data->count; ++i)
  2242. sched_info_free(data->source_info[i]);
  2243. free(data->source_info);
  2244. }
  2245. isl_space_free(data->dim);
  2246. isl_map_free(map);
  2247. return isl_stat_error;
  2248. }
  2249. /* Add the kills of "info" to the must-sources.
  2250. */
  2251. static __isl_give isl_union_access_info *
  2252. isl_union_access_info_add_kill_to_must_source(
  2253. __isl_take isl_union_access_info *info)
  2254. {
  2255. isl_union_map *must, *kill;
  2256. must = isl_union_access_info_get_must_source(info);
  2257. kill = isl_union_access_info_get_kill(info);
  2258. must = isl_union_map_union(must, kill);
  2259. return isl_union_access_info_set_must_source(info, must);
  2260. }
  2261. /* Drop dependences from "flow" that purely originate from kills.
  2262. * That is, only keep those dependences that originate from
  2263. * the original must-sources "must" and/or the original may-sources "may".
  2264. * In particular, "must" contains the must-sources from before
  2265. * the kills were added and "may" contains the may-source from before
  2266. * the kills were removed.
  2267. *
  2268. * The dependences are of the form
  2269. *
  2270. * Source -> [Sink -> Data]
  2271. *
  2272. * Only those dependences are kept where the Source -> Data part
  2273. * is a subset of the original may-sources or must-sources.
  2274. * Of those, only the must-dependences that intersect with the must-sources
  2275. * remain must-dependences.
  2276. * If there is some overlap between the may-sources and the must-sources,
  2277. * then the may-dependences and must-dependences may also overlap.
  2278. * This should be fine since the may-dependences are only kept
  2279. * disjoint from the must-dependences for the isl_union_map_compute_flow
  2280. * interface. This interface does not support kills, so it will
  2281. * not end up calling this function.
  2282. */
  2283. static __isl_give isl_union_flow *isl_union_flow_drop_kill_source(
  2284. __isl_take isl_union_flow *flow, __isl_take isl_union_map *must,
  2285. __isl_take isl_union_map *may)
  2286. {
  2287. isl_union_map *move;
  2288. if (!flow)
  2289. goto error;
  2290. move = isl_union_map_copy(flow->must_dep);
  2291. move = isl_union_map_intersect_range_factor_range(move,
  2292. isl_union_map_copy(may));
  2293. may = isl_union_map_union(may, isl_union_map_copy(must));
  2294. flow->may_dep = isl_union_map_intersect_range_factor_range(
  2295. flow->may_dep, may);
  2296. flow->must_dep = isl_union_map_intersect_range_factor_range(
  2297. flow->must_dep, must);
  2298. flow->may_dep = isl_union_map_union(flow->may_dep, move);
  2299. if (!flow->must_dep || !flow->may_dep)
  2300. return isl_union_flow_free(flow);
  2301. return flow;
  2302. error:
  2303. isl_union_map_free(must);
  2304. isl_union_map_free(may);
  2305. return NULL;
  2306. }
  2307. /* Remove the must accesses from the may accesses.
  2308. *
  2309. * A must access always trumps a may access, so there is no need
  2310. * for a must access to also be considered as a may access. Doing so
  2311. * would only cost extra computations only to find out that
  2312. * the duplicated may access does not make any difference.
  2313. */
  2314. static __isl_give isl_union_access_info *isl_union_access_info_normalize(
  2315. __isl_take isl_union_access_info *access)
  2316. {
  2317. if (!access)
  2318. return NULL;
  2319. access->access[isl_access_may_source] =
  2320. isl_union_map_subtract(access->access[isl_access_may_source],
  2321. isl_union_map_copy(access->access[isl_access_must_source]));
  2322. if (!access->access[isl_access_may_source])
  2323. return isl_union_access_info_free(access);
  2324. return access;
  2325. }
  2326. /* Given a description of the "sink" accesses, the "source" accesses and
  2327. * a schedule, compute for each instance of a sink access
  2328. * and for each element accessed by that instance,
  2329. * the possible or definite source accesses that last accessed the
  2330. * element accessed by the sink access before this sink access
  2331. * in the sense that there is no intermediate definite source access.
  2332. *
  2333. * The must_no_source and may_no_source elements of the result
  2334. * are subsets of access->sink. The elements must_dep and may_dep
  2335. * map domain elements of access->{may,must)_source to
  2336. * domain elements of access->sink.
  2337. *
  2338. * This function is used when only the schedule map representation
  2339. * is available.
  2340. *
  2341. * We first prepend the schedule dimensions to the domain
  2342. * of the accesses so that we can easily compare their relative order.
  2343. * Then we consider each sink access individually in compute_flow.
  2344. */
  2345. static __isl_give isl_union_flow *compute_flow_union_map(
  2346. __isl_take isl_union_access_info *access)
  2347. {
  2348. struct isl_compute_flow_data data;
  2349. isl_union_map *sink;
  2350. access = isl_union_access_info_align_params(access);
  2351. access = isl_union_access_info_introduce_schedule(access);
  2352. if (!access)
  2353. return NULL;
  2354. data.must_source = access->access[isl_access_must_source];
  2355. data.may_source = access->access[isl_access_may_source];
  2356. sink = access->access[isl_access_sink];
  2357. data.flow = isl_union_flow_alloc(isl_union_map_get_space(sink));
  2358. if (isl_union_map_foreach_map(sink, &compute_flow, &data) < 0)
  2359. goto error;
  2360. data.flow = isl_union_flow_drop_schedule(data.flow);
  2361. isl_union_access_info_free(access);
  2362. return data.flow;
  2363. error:
  2364. isl_union_access_info_free(access);
  2365. isl_union_flow_free(data.flow);
  2366. return NULL;
  2367. }
  2368. /* A schedule access relation.
  2369. *
  2370. * The access relation "access" is of the form [S -> D] -> A,
  2371. * where S corresponds to the prefix schedule at "node".
  2372. * "must" is only relevant for source accesses and indicates
  2373. * whether the access is a must source or a may source.
  2374. */
  2375. struct isl_scheduled_access {
  2376. isl_map *access;
  2377. int must;
  2378. isl_schedule_node *node;
  2379. };
  2380. /* Data structure for keeping track of individual scheduled sink and source
  2381. * accesses when computing dependence analysis based on a schedule tree.
  2382. *
  2383. * "n_sink" is the number of used entries in "sink"
  2384. * "n_source" is the number of used entries in "source"
  2385. *
  2386. * "set_sink", "must" and "node" are only used inside collect_sink_source,
  2387. * to keep track of the current node and
  2388. * of what extract_sink_source needs to do.
  2389. */
  2390. struct isl_compute_flow_schedule_data {
  2391. isl_union_access_info *access;
  2392. int n_sink;
  2393. int n_source;
  2394. struct isl_scheduled_access *sink;
  2395. struct isl_scheduled_access *source;
  2396. int set_sink;
  2397. int must;
  2398. isl_schedule_node *node;
  2399. };
  2400. /* Align the parameters of all sinks with all sources.
  2401. *
  2402. * If there are no sinks or no sources, then no alignment is needed.
  2403. */
  2404. static void isl_compute_flow_schedule_data_align_params(
  2405. struct isl_compute_flow_schedule_data *data)
  2406. {
  2407. int i;
  2408. isl_space *space;
  2409. if (data->n_sink == 0 || data->n_source == 0)
  2410. return;
  2411. space = isl_map_get_space(data->sink[0].access);
  2412. for (i = 1; i < data->n_sink; ++i)
  2413. space = isl_space_align_params(space,
  2414. isl_map_get_space(data->sink[i].access));
  2415. for (i = 0; i < data->n_source; ++i)
  2416. space = isl_space_align_params(space,
  2417. isl_map_get_space(data->source[i].access));
  2418. for (i = 0; i < data->n_sink; ++i)
  2419. data->sink[i].access =
  2420. isl_map_align_params(data->sink[i].access,
  2421. isl_space_copy(space));
  2422. for (i = 0; i < data->n_source; ++i)
  2423. data->source[i].access =
  2424. isl_map_align_params(data->source[i].access,
  2425. isl_space_copy(space));
  2426. isl_space_free(space);
  2427. }
  2428. /* Free all the memory referenced from "data".
  2429. * Do not free "data" itself as it may be allocated on the stack.
  2430. */
  2431. static void isl_compute_flow_schedule_data_clear(
  2432. struct isl_compute_flow_schedule_data *data)
  2433. {
  2434. int i;
  2435. if (!data->sink)
  2436. return;
  2437. for (i = 0; i < data->n_sink; ++i) {
  2438. isl_map_free(data->sink[i].access);
  2439. isl_schedule_node_free(data->sink[i].node);
  2440. }
  2441. for (i = 0; i < data->n_source; ++i) {
  2442. isl_map_free(data->source[i].access);
  2443. isl_schedule_node_free(data->source[i].node);
  2444. }
  2445. free(data->sink);
  2446. }
  2447. /* isl_schedule_foreach_schedule_node_top_down callback for counting
  2448. * (an upper bound on) the number of sinks and sources.
  2449. *
  2450. * Sinks and sources are only extracted at leaves of the tree,
  2451. * so we skip the node if it is not a leaf.
  2452. * Otherwise we increment data->n_sink and data->n_source with
  2453. * the number of spaces in the sink and source access domains
  2454. * that reach this node.
  2455. */
  2456. static isl_bool count_sink_source(__isl_keep isl_schedule_node *node,
  2457. void *user)
  2458. {
  2459. struct isl_compute_flow_schedule_data *data = user;
  2460. isl_union_set *domain;
  2461. isl_union_map *umap;
  2462. isl_bool r = isl_bool_false;
  2463. isl_size n;
  2464. if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
  2465. return isl_bool_true;
  2466. domain = isl_schedule_node_get_universe_domain(node);
  2467. umap = isl_union_map_copy(data->access->access[isl_access_sink]);
  2468. umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
  2469. data->n_sink += n = isl_union_map_n_map(umap);
  2470. isl_union_map_free(umap);
  2471. if (n < 0)
  2472. r = isl_bool_error;
  2473. umap = isl_union_map_copy(data->access->access[isl_access_must_source]);
  2474. umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
  2475. data->n_source += n = isl_union_map_n_map(umap);
  2476. isl_union_map_free(umap);
  2477. if (n < 0)
  2478. r = isl_bool_error;
  2479. umap = isl_union_map_copy(data->access->access[isl_access_may_source]);
  2480. umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
  2481. data->n_source += n = isl_union_map_n_map(umap);
  2482. isl_union_map_free(umap);
  2483. if (n < 0)
  2484. r = isl_bool_error;
  2485. isl_union_set_free(domain);
  2486. return r;
  2487. }
  2488. /* Add a single scheduled sink or source (depending on data->set_sink)
  2489. * with scheduled access relation "map", must property data->must and
  2490. * schedule node data->node to the list of sinks or sources.
  2491. */
  2492. static isl_stat extract_sink_source(__isl_take isl_map *map, void *user)
  2493. {
  2494. struct isl_compute_flow_schedule_data *data = user;
  2495. struct isl_scheduled_access *access;
  2496. if (data->set_sink)
  2497. access = data->sink + data->n_sink++;
  2498. else
  2499. access = data->source + data->n_source++;
  2500. access->access = map;
  2501. access->must = data->must;
  2502. access->node = isl_schedule_node_copy(data->node);
  2503. return isl_stat_ok;
  2504. }
  2505. /* isl_schedule_foreach_schedule_node_top_down callback for collecting
  2506. * individual scheduled source and sink accesses (taking into account
  2507. * the domain of the schedule).
  2508. *
  2509. * We only collect accesses at the leaves of the schedule tree.
  2510. * We prepend the schedule dimensions at the leaf to the iteration
  2511. * domains of the source and sink accesses and then extract
  2512. * the individual accesses (per space).
  2513. *
  2514. * In particular, if the prefix schedule at the node is of the form
  2515. *
  2516. * D -> S
  2517. *
  2518. * while the access relations are of the form
  2519. *
  2520. * D -> A
  2521. *
  2522. * then the updated access relations are of the form
  2523. *
  2524. * [S -> D] -> A
  2525. *
  2526. * Note that S consists of a single space such that introducing S
  2527. * in the access relations does not increase the number of spaces.
  2528. */
  2529. static isl_bool collect_sink_source(__isl_keep isl_schedule_node *node,
  2530. void *user)
  2531. {
  2532. struct isl_compute_flow_schedule_data *data = user;
  2533. isl_union_map *prefix;
  2534. isl_union_map *umap;
  2535. isl_bool r = isl_bool_false;
  2536. if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
  2537. return isl_bool_true;
  2538. data->node = node;
  2539. prefix = isl_schedule_node_get_prefix_schedule_relation(node);
  2540. prefix = isl_union_map_reverse(prefix);
  2541. prefix = isl_union_map_range_map(prefix);
  2542. data->set_sink = 1;
  2543. umap = isl_union_map_copy(data->access->access[isl_access_sink]);
  2544. umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
  2545. if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
  2546. r = isl_bool_error;
  2547. isl_union_map_free(umap);
  2548. data->set_sink = 0;
  2549. data->must = 1;
  2550. umap = isl_union_map_copy(data->access->access[isl_access_must_source]);
  2551. umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
  2552. if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
  2553. r = isl_bool_error;
  2554. isl_union_map_free(umap);
  2555. data->set_sink = 0;
  2556. data->must = 0;
  2557. umap = isl_union_map_copy(data->access->access[isl_access_may_source]);
  2558. umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
  2559. if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
  2560. r = isl_bool_error;
  2561. isl_union_map_free(umap);
  2562. isl_union_map_free(prefix);
  2563. return r;
  2564. }
  2565. /* isl_access_info_compute_flow callback for determining whether
  2566. * the shared nesting level and the ordering within that level
  2567. * for two scheduled accesses for use in compute_single_flow.
  2568. *
  2569. * The tokens passed to this function refer to the leaves
  2570. * in the schedule tree where the accesses take place.
  2571. *
  2572. * If n is the shared number of loops, then we need to return
  2573. * "2 * n + 1" if "first" precedes "second" inside the innermost
  2574. * shared loop and "2 * n" otherwise.
  2575. *
  2576. * The innermost shared ancestor may be the leaves themselves
  2577. * if the accesses take place in the same leaf. Otherwise,
  2578. * it is either a set node or a sequence node. Only in the case
  2579. * of a sequence node do we consider one access to precede the other.
  2580. */
  2581. static int before_node(void *first, void *second)
  2582. {
  2583. isl_schedule_node *node1 = first;
  2584. isl_schedule_node *node2 = second;
  2585. isl_schedule_node *shared;
  2586. isl_size depth;
  2587. int before = 0;
  2588. shared = isl_schedule_node_get_shared_ancestor(node1, node2);
  2589. depth = isl_schedule_node_get_schedule_depth(shared);
  2590. if (depth < 0) {
  2591. isl_schedule_node_free(shared);
  2592. return -1;
  2593. }
  2594. if (isl_schedule_node_get_type(shared) == isl_schedule_node_sequence) {
  2595. isl_size pos1, pos2;
  2596. pos1 = isl_schedule_node_get_ancestor_child_position(node1,
  2597. shared);
  2598. pos2 = isl_schedule_node_get_ancestor_child_position(node2,
  2599. shared);
  2600. if (pos1 < 0 || pos2 < 0) {
  2601. isl_schedule_node_free(shared);
  2602. return -1;
  2603. }
  2604. before = pos1 < pos2;
  2605. }
  2606. isl_schedule_node_free(shared);
  2607. return 2 * depth + before;
  2608. }
  2609. /* Check if the given two accesses may be coscheduled.
  2610. * If so, return isl_bool_true. Otherwise return isl_bool_false.
  2611. *
  2612. * Two accesses may only be coscheduled if they appear in the same leaf.
  2613. */
  2614. static isl_bool coscheduled_node(void *first, void *second)
  2615. {
  2616. isl_schedule_node *node1 = first;
  2617. isl_schedule_node *node2 = second;
  2618. return isl_bool_ok(node1 == node2);
  2619. }
  2620. /* Add the scheduled sources from "data" that access
  2621. * the same data space as "sink" to "access".
  2622. */
  2623. static __isl_give isl_access_info *add_matching_sources(
  2624. __isl_take isl_access_info *access, struct isl_scheduled_access *sink,
  2625. struct isl_compute_flow_schedule_data *data)
  2626. {
  2627. int i;
  2628. isl_space *space;
  2629. space = isl_space_range(isl_map_get_space(sink->access));
  2630. for (i = 0; i < data->n_source; ++i) {
  2631. struct isl_scheduled_access *source;
  2632. isl_space *source_space;
  2633. int eq;
  2634. source = &data->source[i];
  2635. source_space = isl_map_get_space(source->access);
  2636. source_space = isl_space_range(source_space);
  2637. eq = isl_space_is_equal(space, source_space);
  2638. isl_space_free(source_space);
  2639. if (!eq)
  2640. continue;
  2641. if (eq < 0)
  2642. goto error;
  2643. access = isl_access_info_add_source(access,
  2644. isl_map_copy(source->access), source->must, source->node);
  2645. }
  2646. isl_space_free(space);
  2647. return access;
  2648. error:
  2649. isl_space_free(space);
  2650. isl_access_info_free(access);
  2651. return NULL;
  2652. }
  2653. /* Given a scheduled sink access relation "sink", compute the corresponding
  2654. * dependences on the sources in "data" and add the computed dependences
  2655. * to "uf".
  2656. *
  2657. * The dependences computed by access_info_compute_flow_core are of the form
  2658. *
  2659. * [S -> I] -> [[S' -> I'] -> A]
  2660. *
  2661. * The schedule dimensions are projected out by first currying the range,
  2662. * resulting in
  2663. *
  2664. * [S -> I] -> [S' -> [I' -> A]]
  2665. *
  2666. * and then computing the factor range
  2667. *
  2668. * I -> [I' -> A]
  2669. */
  2670. static __isl_give isl_union_flow *compute_single_flow(
  2671. __isl_take isl_union_flow *uf, struct isl_scheduled_access *sink,
  2672. struct isl_compute_flow_schedule_data *data)
  2673. {
  2674. int i;
  2675. isl_access_info *access;
  2676. isl_flow *flow;
  2677. isl_map *map;
  2678. if (!uf)
  2679. return NULL;
  2680. access = isl_access_info_alloc(isl_map_copy(sink->access), sink->node,
  2681. &before_node, data->n_source);
  2682. if (access)
  2683. access->coscheduled = &coscheduled_node;
  2684. access = add_matching_sources(access, sink, data);
  2685. flow = access_info_compute_flow_core(access);
  2686. if (!flow)
  2687. return isl_union_flow_free(uf);
  2688. map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 1));
  2689. uf->must_no_source = isl_union_map_union(uf->must_no_source,
  2690. isl_union_map_from_map(map));
  2691. map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 0));
  2692. uf->may_no_source = isl_union_map_union(uf->may_no_source,
  2693. isl_union_map_from_map(map));
  2694. for (i = 0; i < flow->n_source; ++i) {
  2695. isl_union_map *dep;
  2696. map = isl_map_range_curry(isl_map_copy(flow->dep[i].map));
  2697. map = isl_map_factor_range(map);
  2698. dep = isl_union_map_from_map(map);
  2699. if (flow->dep[i].must)
  2700. uf->must_dep = isl_union_map_union(uf->must_dep, dep);
  2701. else
  2702. uf->may_dep = isl_union_map_union(uf->may_dep, dep);
  2703. }
  2704. isl_flow_free(flow);
  2705. return uf;
  2706. }
  2707. /* Given a description of the "sink" accesses, the "source" accesses and
  2708. * a schedule, compute for each instance of a sink access
  2709. * and for each element accessed by that instance,
  2710. * the possible or definite source accesses that last accessed the
  2711. * element accessed by the sink access before this sink access
  2712. * in the sense that there is no intermediate definite source access.
  2713. * Only consider dependences between statement instances that belong
  2714. * to the domain of the schedule.
  2715. *
  2716. * The must_no_source and may_no_source elements of the result
  2717. * are subsets of access->sink. The elements must_dep and may_dep
  2718. * map domain elements of access->{may,must)_source to
  2719. * domain elements of access->sink.
  2720. *
  2721. * This function is used when a schedule tree representation
  2722. * is available.
  2723. *
  2724. * We extract the individual scheduled source and sink access relations
  2725. * (taking into account the domain of the schedule) and
  2726. * then compute dependences for each scheduled sink individually.
  2727. */
  2728. static __isl_give isl_union_flow *compute_flow_schedule(
  2729. __isl_take isl_union_access_info *access)
  2730. {
  2731. struct isl_compute_flow_schedule_data data = { access };
  2732. int i, n;
  2733. isl_ctx *ctx;
  2734. isl_space *space;
  2735. isl_union_flow *flow;
  2736. ctx = isl_union_access_info_get_ctx(access);
  2737. data.n_sink = 0;
  2738. data.n_source = 0;
  2739. if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
  2740. &count_sink_source, &data) < 0)
  2741. goto error;
  2742. n = data.n_sink + data.n_source;
  2743. data.sink = isl_calloc_array(ctx, struct isl_scheduled_access, n);
  2744. if (n && !data.sink)
  2745. goto error;
  2746. data.source = data.sink + data.n_sink;
  2747. data.n_sink = 0;
  2748. data.n_source = 0;
  2749. if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
  2750. &collect_sink_source, &data) < 0)
  2751. goto error;
  2752. space = isl_union_map_get_space(access->access[isl_access_sink]);
  2753. flow = isl_union_flow_alloc(space);
  2754. isl_compute_flow_schedule_data_align_params(&data);
  2755. for (i = 0; i < data.n_sink; ++i)
  2756. flow = compute_single_flow(flow, &data.sink[i], &data);
  2757. isl_compute_flow_schedule_data_clear(&data);
  2758. isl_union_access_info_free(access);
  2759. return flow;
  2760. error:
  2761. isl_union_access_info_free(access);
  2762. isl_compute_flow_schedule_data_clear(&data);
  2763. return NULL;
  2764. }
  2765. /* Given a description of the "sink" accesses, the "source" accesses and
  2766. * a schedule, compute for each instance of a sink access
  2767. * and for each element accessed by that instance,
  2768. * the possible or definite source accesses that last accessed the
  2769. * element accessed by the sink access before this sink access
  2770. * in the sense that there is no intermediate definite source access.
  2771. *
  2772. * The must_no_source and may_no_source elements of the result
  2773. * are subsets of access->sink. The elements must_dep and may_dep
  2774. * map domain elements of access->{may,must)_source to
  2775. * domain elements of access->sink.
  2776. *
  2777. * If any kills have been specified, then they are treated as
  2778. * must-sources internally. Any dependence that purely derives
  2779. * from an original kill is removed from the output.
  2780. *
  2781. * We check whether the schedule is available as a schedule tree
  2782. * or a schedule map and call the corresponding function to perform
  2783. * the analysis.
  2784. */
  2785. __isl_give isl_union_flow *isl_union_access_info_compute_flow(
  2786. __isl_take isl_union_access_info *access)
  2787. {
  2788. isl_bool has_kill;
  2789. isl_union_map *must = NULL, *may = NULL;
  2790. isl_union_flow *flow;
  2791. has_kill = isl_union_access_has_kill(access);
  2792. if (has_kill < 0)
  2793. goto error;
  2794. if (has_kill) {
  2795. must = isl_union_access_info_get_must_source(access);
  2796. may = isl_union_access_info_get_may_source(access);
  2797. }
  2798. access = isl_union_access_info_add_kill_to_must_source(access);
  2799. access = isl_union_access_info_normalize(access);
  2800. if (!access)
  2801. goto error;
  2802. if (access->schedule)
  2803. flow = compute_flow_schedule(access);
  2804. else
  2805. flow = compute_flow_union_map(access);
  2806. if (has_kill)
  2807. flow = isl_union_flow_drop_kill_source(flow, must, may);
  2808. return flow;
  2809. error:
  2810. isl_union_access_info_free(access);
  2811. isl_union_map_free(must);
  2812. isl_union_map_free(may);
  2813. return NULL;
  2814. }
  2815. /* Print the information contained in "flow" to "p".
  2816. * The information is printed as a YAML document.
  2817. */
  2818. __isl_give isl_printer *isl_printer_print_union_flow(
  2819. __isl_take isl_printer *p, __isl_keep isl_union_flow *flow)
  2820. {
  2821. isl_union_map *umap;
  2822. if (!flow)
  2823. return isl_printer_free(p);
  2824. p = isl_printer_yaml_start_mapping(p);
  2825. umap = isl_union_flow_get_full_must_dependence(flow);
  2826. p = print_yaml_field_union_map(p, "must_dependence", umap);
  2827. isl_union_map_free(umap);
  2828. umap = isl_union_flow_get_full_may_dependence(flow);
  2829. p = print_yaml_field_union_map(p, "may_dependence", umap);
  2830. isl_union_map_free(umap);
  2831. p = print_yaml_field_union_map(p, "must_no_source",
  2832. flow->must_no_source);
  2833. umap = isl_union_flow_get_may_no_source(flow);
  2834. p = print_yaml_field_union_map(p, "may_no_source", umap);
  2835. isl_union_map_free(umap);
  2836. p = isl_printer_yaml_end_mapping(p);
  2837. return p;
  2838. }
  2839. /* Return a string representation of the information in "flow".
  2840. * The information is printed in flow format.
  2841. */
  2842. __isl_give char *isl_union_flow_to_str(__isl_keep isl_union_flow *flow)
  2843. {
  2844. isl_printer *p;
  2845. char *s;
  2846. if (!flow)
  2847. return NULL;
  2848. p = isl_printer_to_str(isl_union_flow_get_ctx(flow));
  2849. p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
  2850. p = isl_printer_print_union_flow(p, flow);
  2851. s = isl_printer_get_str(p);
  2852. isl_printer_free(p);
  2853. return s;
  2854. }
  2855. /* Given a collection of "sink" and "source" accesses,
  2856. * compute for each iteration of a sink access
  2857. * and for each element accessed by that iteration,
  2858. * the source access in the list that last accessed the
  2859. * element accessed by the sink access before this sink access.
  2860. * Each access is given as a map from the loop iterators
  2861. * to the array indices.
  2862. * The result is a relations between source and sink
  2863. * iterations and a subset of the domain of the sink accesses,
  2864. * corresponding to those iterations that access an element
  2865. * not previously accessed.
  2866. *
  2867. * We collect the inputs in an isl_union_access_info object,
  2868. * call isl_union_access_info_compute_flow and extract
  2869. * the outputs from the result.
  2870. */
  2871. int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
  2872. __isl_take isl_union_map *must_source,
  2873. __isl_take isl_union_map *may_source,
  2874. __isl_take isl_union_map *schedule,
  2875. __isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
  2876. __isl_give isl_union_map **must_no_source,
  2877. __isl_give isl_union_map **may_no_source)
  2878. {
  2879. isl_union_access_info *access;
  2880. isl_union_flow *flow;
  2881. access = isl_union_access_info_from_sink(sink);
  2882. access = isl_union_access_info_set_must_source(access, must_source);
  2883. access = isl_union_access_info_set_may_source(access, may_source);
  2884. access = isl_union_access_info_set_schedule_map(access, schedule);
  2885. flow = isl_union_access_info_compute_flow(access);
  2886. if (must_dep)
  2887. *must_dep = isl_union_flow_get_must_dependence(flow);
  2888. if (may_dep)
  2889. *may_dep = isl_union_flow_get_non_must_dependence(flow);
  2890. if (must_no_source)
  2891. *must_no_source = isl_union_flow_get_must_no_source(flow);
  2892. if (may_no_source)
  2893. *may_no_source = isl_union_flow_get_non_must_no_source(flow);
  2894. isl_union_flow_free(flow);
  2895. if ((must_dep && !*must_dep) || (may_dep && !*may_dep) ||
  2896. (must_no_source && !*must_no_source) ||
  2897. (may_no_source && !*may_no_source))
  2898. goto error;
  2899. return 0;
  2900. error:
  2901. if (must_dep)
  2902. *must_dep = isl_union_map_free(*must_dep);
  2903. if (may_dep)
  2904. *may_dep = isl_union_map_free(*may_dep);
  2905. if (must_no_source)
  2906. *must_no_source = isl_union_map_free(*must_no_source);
  2907. if (may_no_source)
  2908. *may_no_source = isl_union_map_free(*may_no_source);
  2909. return -1;
  2910. }