isl_stride.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Copyright 2012-2013 Ecole Normale Superieure
  3. *
  4. * Use of this software is governed by the MIT license
  5. *
  6. * Written by Sven Verdoolaege,
  7. * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
  8. */
  9. #include <isl/val.h>
  10. #include <isl_map_private.h>
  11. #include <isl_aff_private.h>
  12. #include <isl/constraint.h>
  13. #include <isl/set.h>
  14. /* Stride information about a specific set dimension.
  15. * The values of the set dimension are equal to
  16. * "offset" plus a multiple of "stride".
  17. */
  18. struct isl_stride_info {
  19. isl_val *stride;
  20. isl_aff *offset;
  21. };
  22. /* Return the ctx to which "si" belongs.
  23. */
  24. isl_ctx *isl_stride_info_get_ctx(__isl_keep isl_stride_info *si)
  25. {
  26. if (!si)
  27. return NULL;
  28. return isl_val_get_ctx(si->stride);
  29. }
  30. /* Free "si" and return NULL.
  31. */
  32. __isl_null isl_stride_info *isl_stride_info_free(
  33. __isl_take isl_stride_info *si)
  34. {
  35. if (!si)
  36. return NULL;
  37. isl_val_free(si->stride);
  38. isl_aff_free(si->offset);
  39. free(si);
  40. return NULL;
  41. }
  42. /* Construct an isl_stride_info object with given offset and stride.
  43. */
  44. __isl_give isl_stride_info *isl_stride_info_alloc(
  45. __isl_take isl_val *stride, __isl_take isl_aff *offset)
  46. {
  47. struct isl_stride_info *si;
  48. if (!stride || !offset)
  49. goto error;
  50. si = isl_alloc_type(isl_val_get_ctx(stride), struct isl_stride_info);
  51. if (!si)
  52. goto error;
  53. si->stride = stride;
  54. si->offset = offset;
  55. return si;
  56. error:
  57. isl_val_free(stride);
  58. isl_aff_free(offset);
  59. return NULL;
  60. }
  61. /* Make a copy of "si" and return it.
  62. */
  63. __isl_give isl_stride_info *isl_stride_info_copy(
  64. __isl_keep isl_stride_info *si)
  65. {
  66. if (!si)
  67. return NULL;
  68. return isl_stride_info_alloc(isl_val_copy(si->stride),
  69. isl_aff_copy(si->offset));
  70. }
  71. /* Return the stride of "si".
  72. */
  73. __isl_give isl_val *isl_stride_info_get_stride(__isl_keep isl_stride_info *si)
  74. {
  75. if (!si)
  76. return NULL;
  77. return isl_val_copy(si->stride);
  78. }
  79. /* Return the offset of "si".
  80. */
  81. __isl_give isl_aff *isl_stride_info_get_offset(__isl_keep isl_stride_info *si)
  82. {
  83. if (!si)
  84. return NULL;
  85. return isl_aff_copy(si->offset);
  86. }
  87. /* Information used inside detect_stride.
  88. *
  89. * "pos" is the set dimension at which the stride is being determined.
  90. * "want_offset" is set if the offset should be computed.
  91. * "found" is set if some stride was found already.
  92. * "stride" and "offset" contain the (combined) stride and offset
  93. * found so far and are NULL when "found" is not set.
  94. * If "want_offset" is not set, then "offset" remains NULL.
  95. */
  96. struct isl_detect_stride_data {
  97. int pos;
  98. int want_offset;
  99. int found;
  100. isl_val *stride;
  101. isl_aff *offset;
  102. };
  103. /* Set the stride and offset of data->pos to the given
  104. * value and expression.
  105. *
  106. * If we had already found a stride before, then the two strides
  107. * are combined into a single stride.
  108. *
  109. * In particular, if the new stride information is of the form
  110. *
  111. * i = f + s (...)
  112. *
  113. * and the old stride information is of the form
  114. *
  115. * i = f2 + s2 (...)
  116. *
  117. * then we compute the extended gcd of s and s2
  118. *
  119. * a s + b s2 = g,
  120. *
  121. * with g = gcd(s,s2), multiply the first equation with t1 = b s2/g
  122. * and the second with t2 = a s1/g.
  123. * This results in
  124. *
  125. * i = (b s2 + a s1)/g i = t1 f + t2 f2 + (s s2)/g (...)
  126. *
  127. * so that t1 f + t2 f2 is the combined offset and (s s2)/g = lcm(s,s2)
  128. * is the combined stride.
  129. */
  130. static isl_stat set_stride(struct isl_detect_stride_data *data,
  131. __isl_take isl_val *stride, __isl_take isl_aff *offset)
  132. {
  133. int pos;
  134. if (!stride || !offset)
  135. goto error;
  136. pos = data->pos;
  137. if (data->found) {
  138. isl_val *stride2, *a, *b, *g;
  139. isl_aff *offset2;
  140. stride2 = data->stride;
  141. g = isl_val_gcdext(isl_val_copy(stride), isl_val_copy(stride2),
  142. &a, &b);
  143. a = isl_val_mul(a, isl_val_copy(stride));
  144. a = isl_val_div(a, isl_val_copy(g));
  145. stride2 = isl_val_div(stride2, g);
  146. b = isl_val_mul(b, isl_val_copy(stride2));
  147. stride = isl_val_mul(stride, stride2);
  148. if (!data->want_offset) {
  149. isl_val_free(a);
  150. isl_val_free(b);
  151. } else {
  152. offset2 = data->offset;
  153. offset2 = isl_aff_scale_val(offset2, a);
  154. offset = isl_aff_scale_val(offset, b);
  155. offset = isl_aff_add(offset, offset2);
  156. }
  157. }
  158. data->found = 1;
  159. data->stride = stride;
  160. if (data->want_offset)
  161. data->offset = offset;
  162. else
  163. isl_aff_free(offset);
  164. if (!data->stride || (data->want_offset && !data->offset))
  165. return isl_stat_error;
  166. return isl_stat_ok;
  167. error:
  168. isl_val_free(stride);
  169. isl_aff_free(offset);
  170. return isl_stat_error;
  171. }
  172. /* Check if constraint "c" imposes any stride on dimension data->pos
  173. * and, if so, update the stride information in "data".
  174. *
  175. * In order to impose a stride on the dimension, "c" needs to be an equality
  176. * and it needs to involve the dimension. Note that "c" may also be
  177. * a div constraint and thus an inequality that we cannot use.
  178. *
  179. * Let c be of the form
  180. *
  181. * h(p) + g * v * i + g * stride * f(alpha) = 0
  182. *
  183. * with h(p) an expression in terms of the parameters and other dimensions
  184. * and f(alpha) an expression in terms of the existentially quantified
  185. * variables.
  186. *
  187. * If "stride" is not zero and not one, then it represents a non-trivial stride
  188. * on "i". We compute a and b such that
  189. *
  190. * a v + b stride = 1
  191. *
  192. * We have
  193. *
  194. * g v i = -h(p) + g stride f(alpha)
  195. *
  196. * a g v i = -a h(p) + g stride f(alpha)
  197. *
  198. * a g v i + b g stride i = -a h(p) + g stride * (...)
  199. *
  200. * g i = -a h(p) + g stride * (...)
  201. *
  202. * i = -a h(p)/g + stride * (...)
  203. *
  204. * The expression "-a h(p)/g" can therefore be used as offset.
  205. */
  206. static isl_stat detect_stride(__isl_take isl_constraint *c, void *user)
  207. {
  208. struct isl_detect_stride_data *data = user;
  209. int i;
  210. isl_size n_div;
  211. isl_ctx *ctx;
  212. isl_stat r = isl_stat_ok;
  213. isl_val *v, *stride, *m;
  214. isl_bool is_eq, relevant, has_stride;
  215. is_eq = isl_constraint_is_equality(c);
  216. relevant = isl_constraint_involves_dims(c, isl_dim_set, data->pos, 1);
  217. if (is_eq < 0 || relevant < 0)
  218. goto error;
  219. if (!is_eq || !relevant) {
  220. isl_constraint_free(c);
  221. return isl_stat_ok;
  222. }
  223. n_div = isl_constraint_dim(c, isl_dim_div);
  224. if (n_div < 0)
  225. goto error;
  226. ctx = isl_constraint_get_ctx(c);
  227. stride = isl_val_zero(ctx);
  228. for (i = 0; i < n_div; ++i) {
  229. v = isl_constraint_get_coefficient_val(c, isl_dim_div, i);
  230. stride = isl_val_gcd(stride, v);
  231. }
  232. v = isl_constraint_get_coefficient_val(c, isl_dim_set, data->pos);
  233. m = isl_val_gcd(isl_val_copy(stride), isl_val_copy(v));
  234. stride = isl_val_div(stride, isl_val_copy(m));
  235. v = isl_val_div(v, isl_val_copy(m));
  236. has_stride = isl_val_gt_si(stride, 1);
  237. if (has_stride >= 0 && has_stride) {
  238. isl_aff *aff;
  239. isl_val *gcd, *a, *b;
  240. gcd = isl_val_gcdext(v, isl_val_copy(stride), &a, &b);
  241. isl_val_free(gcd);
  242. isl_val_free(b);
  243. aff = isl_constraint_get_aff(c);
  244. for (i = 0; i < n_div; ++i)
  245. aff = isl_aff_set_coefficient_si(aff,
  246. isl_dim_div, i, 0);
  247. aff = isl_aff_set_coefficient_si(aff, isl_dim_in, data->pos, 0);
  248. aff = isl_aff_remove_unused_divs(aff);
  249. a = isl_val_neg(a);
  250. aff = isl_aff_scale_val(aff, a);
  251. aff = isl_aff_scale_down_val(aff, m);
  252. r = set_stride(data, stride, aff);
  253. } else {
  254. isl_val_free(stride);
  255. isl_val_free(m);
  256. isl_val_free(v);
  257. }
  258. isl_constraint_free(c);
  259. if (has_stride < 0)
  260. return isl_stat_error;
  261. return r;
  262. error:
  263. isl_constraint_free(c);
  264. return isl_stat_error;
  265. }
  266. /* Check if the constraints in "set" imply any stride on set dimension "pos" and
  267. * store the results in data->stride and data->offset.
  268. *
  269. * In particular, compute the affine hull and then check if
  270. * any of the constraints in the hull impose any stride on the dimension.
  271. * If no such constraint can be found, then the offset is taken
  272. * to be the zero expression and the stride is taken to be one.
  273. */
  274. static void set_detect_stride(__isl_keep isl_set *set, int pos,
  275. struct isl_detect_stride_data *data)
  276. {
  277. isl_basic_set *hull;
  278. hull = isl_set_affine_hull(isl_set_copy(set));
  279. data->pos = pos;
  280. data->found = 0;
  281. data->stride = NULL;
  282. data->offset = NULL;
  283. if (isl_basic_set_foreach_constraint(hull, &detect_stride, data) < 0)
  284. goto error;
  285. if (!data->found) {
  286. data->stride = isl_val_one(isl_set_get_ctx(set));
  287. if (data->want_offset) {
  288. isl_space *space;
  289. isl_local_space *ls;
  290. space = isl_set_get_space(set);
  291. ls = isl_local_space_from_space(space);
  292. data->offset = isl_aff_zero_on_domain(ls);
  293. }
  294. }
  295. isl_basic_set_free(hull);
  296. return;
  297. error:
  298. isl_basic_set_free(hull);
  299. data->stride = isl_val_free(data->stride);
  300. data->offset = isl_aff_free(data->offset);
  301. }
  302. /* Check if the constraints in "set" imply any stride on set dimension "pos" and
  303. * return the results in the form of an offset and a stride.
  304. */
  305. __isl_give isl_stride_info *isl_set_get_stride_info(__isl_keep isl_set *set,
  306. int pos)
  307. {
  308. struct isl_detect_stride_data data;
  309. data.want_offset = 1;
  310. set_detect_stride(set, pos, &data);
  311. return isl_stride_info_alloc(data.stride, data.offset);
  312. }
  313. /* Check if the constraints in "set" imply any stride on set dimension "pos" and
  314. * return this stride.
  315. */
  316. __isl_give isl_val *isl_set_get_stride(__isl_keep isl_set *set, int pos)
  317. {
  318. struct isl_detect_stride_data data;
  319. data.want_offset = 0;
  320. set_detect_stride(set, pos, &data);
  321. return data.stride;
  322. }
  323. /* Check if the constraints in "map" imply any stride on output dimension "pos",
  324. * independently of any other output dimensions, and
  325. * return the results in the form of an offset and a stride.
  326. *
  327. * Convert the input to a set with only the input dimensions and
  328. * the single output dimension such that it be passed to
  329. * isl_set_get_stride_info and convert the result back to
  330. * an expression defined over the domain of "map".
  331. */
  332. __isl_give isl_stride_info *isl_map_get_range_stride_info(
  333. __isl_keep isl_map *map, int pos)
  334. {
  335. isl_stride_info *si;
  336. isl_set *set;
  337. isl_size n_in;
  338. n_in = isl_map_dim(map, isl_dim_in);
  339. if (n_in < 0)
  340. return NULL;
  341. map = isl_map_copy(map);
  342. map = isl_map_project_onto(map, isl_dim_out, pos, 1);
  343. set = isl_map_wrap(map);
  344. si = isl_set_get_stride_info(set, n_in);
  345. isl_set_free(set);
  346. if (!si)
  347. return NULL;
  348. si->offset = isl_aff_domain_factor_domain(si->offset);
  349. if (!si->offset)
  350. return isl_stride_info_free(si);
  351. return si;
  352. }