1 /* A scheduling optimizer for Graphite
2 Copyright (C) 2012-2014 Free Software Foundation, Inc.
3 Contributed by Tobias Grosser <tobias@grosser.es>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
26 #include <isl/union_map.h>
27 #include <isl/schedule.h>
30 #include <isl/options.h>
34 #include "coretypes.h"
36 #include "basic-block.h"
37 #include "tree-ssa-alias.h"
38 #include "internal-fn.h"
39 #include "gimple-expr.h"
42 #include "gimple-iterator.h"
43 #include "tree-ssa-loop.h"
46 #include "tree-chrec.h"
47 #include "tree-data-ref.h"
48 #include "tree-scalar-evolution.h"
52 #include "graphite-poly.h"
54 static isl_union_set
*
55 scop_get_domains (scop_p scop ATTRIBUTE_UNUSED
)
59 isl_space
*space
= isl_set_get_space (scop
->context
);
60 isl_union_set
*res
= isl_union_set_empty (space
);
62 FOR_EACH_VEC_ELT (scop
->bbs
, i
, pbb
)
63 res
= isl_union_set_add_set (res
, isl_set_copy (pbb
->domain
));
68 static isl_union_map
*
69 scop_get_dependences (scop_p scop
)
71 isl_union_map
*dependences
;
74 compute_deps (scop
, SCOP_BBS (scop
),
75 &scop
->must_raw
, &scop
->may_raw
,
76 &scop
->must_raw_no_source
, &scop
->may_raw_no_source
,
77 &scop
->must_war
, &scop
->may_war
,
78 &scop
->must_war_no_source
, &scop
->may_war_no_source
,
79 &scop
->must_waw
, &scop
->may_waw
,
80 &scop
->must_waw_no_source
, &scop
->may_waw_no_source
);
82 dependences
= isl_union_map_copy (scop
->must_raw
);
83 dependences
= isl_union_map_union (dependences
,
84 isl_union_map_copy (scop
->must_war
));
85 dependences
= isl_union_map_union (dependences
,
86 isl_union_map_copy (scop
->must_waw
));
87 dependences
= isl_union_map_union (dependences
,
88 isl_union_map_copy (scop
->may_raw
));
89 dependences
= isl_union_map_union (dependences
,
90 isl_union_map_copy (scop
->may_war
));
91 dependences
= isl_union_map_union (dependences
,
92 isl_union_map_copy (scop
->may_waw
));
97 /* getTileMap - Create a map that describes a n-dimensonal tiling.
99 getTileMap creates a map from a n-dimensional scattering space into an
100 2*n-dimensional scattering space. The map describes a rectangular tiling.
103 scheduleDimensions = 2, parameterDimensions = 1, tileSize = 32
105 tileMap := [p0] -> {[s0, s1] -> [t0, t1, s0, s1]:
106 t0 % 32 = 0 and t0 <= s0 < t0 + 32 and
107 t1 % 32 = 0 and t1 <= s1 < t1 + 32}
111 for (i = 0; i < N; i++)
112 for (j = 0; j < M; j++)
117 for (t_i = 0; t_i < N; i+=32)
118 for (t_j = 0; t_j < M; j+=32)
119 for (i = t_i; i < min(t_i + 32, N); i++) | Unknown that N % 32 = 0
120 for (j = t_j; j < t_j + 32; j++) | Known that M % 32 = 0
124 static isl_basic_map
*
125 getTileMap (isl_ctx
*ctx
, int scheduleDimensions
, int tileSize
)
130 tileMap := [p0] -> {[s0, s1] -> [t0, t1, p0, p1, a0, a1]:
131 s0 = a0 * 32 and s0 = p0 and t0 <= p0 < t0 + 32 and
132 s1 = a1 * 32 and s1 = p1 and t1 <= p1 < t1 + 32}
134 and project out the auxilary dimensions a0 and a1. */
135 isl_space
*Space
= isl_space_alloc (ctx
, 0, scheduleDimensions
,
136 scheduleDimensions
* 3);
137 isl_basic_map
*tileMap
= isl_basic_map_universe (isl_space_copy (Space
));
139 isl_local_space
*LocalSpace
= isl_local_space_from_space (Space
);
141 for (x
= 0; x
< scheduleDimensions
; x
++)
145 int pX
= scheduleDimensions
+ x
;
146 int aX
= 2 * scheduleDimensions
+ x
;
150 /* sX = aX * tileSize; */
151 c
= isl_equality_alloc (isl_local_space_copy (LocalSpace
));
152 isl_constraint_set_coefficient_si (c
, isl_dim_out
, sX
, 1);
153 isl_constraint_set_coefficient_si (c
, isl_dim_out
, aX
, -tileSize
);
154 tileMap
= isl_basic_map_add_constraint (tileMap
, c
);
157 c
= isl_equality_alloc (isl_local_space_copy (LocalSpace
));
158 isl_constraint_set_coefficient_si (c
, isl_dim_out
, pX
, 1);
159 isl_constraint_set_coefficient_si (c
, isl_dim_in
, sX
, -1);
160 tileMap
= isl_basic_map_add_constraint (tileMap
, c
);
163 c
= isl_inequality_alloc (isl_local_space_copy (LocalSpace
));
164 isl_constraint_set_coefficient_si (c
, isl_dim_out
, pX
, 1);
165 isl_constraint_set_coefficient_si (c
, isl_dim_out
, tX
, -1);
166 tileMap
= isl_basic_map_add_constraint (tileMap
, c
);
168 /* pX <= tX + (tileSize - 1) */
169 c
= isl_inequality_alloc (isl_local_space_copy (LocalSpace
));
170 isl_constraint_set_coefficient_si (c
, isl_dim_out
, tX
, 1);
171 isl_constraint_set_coefficient_si (c
, isl_dim_out
, pX
, -1);
172 isl_constraint_set_constant_si (c
, tileSize
- 1);
173 tileMap
= isl_basic_map_add_constraint (tileMap
, c
);
176 /* Project out auxiliary dimensions.
178 The auxiliary dimensions are transformed into existentially quantified ones.
179 This reduces the number of visible scattering dimensions and allows Cloog
180 to produces better code. */
181 tileMap
= isl_basic_map_project_out (tileMap
, isl_dim_out
,
182 2 * scheduleDimensions
,
184 isl_local_space_free (LocalSpace
);
188 /* getScheduleForBand - Get the schedule for this band.
190 Polly applies transformations like tiling on top of the isl calculated value.
191 This can influence the number of scheduling dimension. The number of
192 schedule dimensions is returned in the parameter 'Dimension'. */
193 static bool DisableTiling
= false;
195 static isl_union_map
*
196 getScheduleForBand (isl_band
*Band
, int *Dimensions
)
198 isl_union_map
*PartialSchedule
;
201 isl_basic_map
*TileMap
;
202 isl_union_map
*TileUMap
;
204 PartialSchedule
= isl_band_get_partial_schedule (Band
);
205 *Dimensions
= isl_band_n_member (Band
);
208 return PartialSchedule
;
210 /* It does not make any sense to tile a band with just one dimension. */
211 if (*Dimensions
== 1)
212 return PartialSchedule
;
214 ctx
= isl_union_map_get_ctx (PartialSchedule
);
215 Space
= isl_union_map_get_space (PartialSchedule
);
217 TileMap
= getTileMap (ctx
, *Dimensions
, 32);
218 TileUMap
= isl_union_map_from_map (isl_map_from_basic_map (TileMap
));
219 TileUMap
= isl_union_map_align_params (TileUMap
, Space
);
220 *Dimensions
= 2 * *Dimensions
;
222 return isl_union_map_apply_range (PartialSchedule
, TileUMap
);
225 /* Create a map that pre-vectorizes one scheduling dimension.
227 getPrevectorMap creates a map that maps each input dimension to the same
228 output dimension, except for the dimension DimToVectorize. DimToVectorize is
229 strip mined by 'VectorWidth' and the newly created point loop of
230 DimToVectorize is moved to the innermost level.
232 Example (DimToVectorize=0, ScheduleDimensions=2, VectorWidth=4):
234 | Before transformation
238 | for (i = 0; i < 128; i++)
239 | for (j = 0; j < 128; j++)
243 [i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
245 | After transformation:
247 | A[i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
249 | for (it = 0; it < 128; it+=4)
250 | for (j = 0; j < 128; j++)
251 | for (ip = max(0,it); ip < min(128, it + 3); ip++)
254 The goal of this transformation is to create a trivially vectorizable loop.
255 This means a parallel loop at the innermost level that has a constant number
256 of iterations corresponding to the target vector width.
258 This transformation creates a loop at the innermost level. The loop has a
259 constant number of iterations, if the number of loop iterations at
260 DimToVectorize can be devided by VectorWidth. The default VectorWidth is
261 currently constant and not yet target specific. This function does not reason
262 about parallelism. */
264 getPrevectorMap (isl_ctx
*ctx
, int DimToVectorize
,
265 int ScheduleDimensions
,
269 isl_local_space
*LocalSpace
, *LocalSpaceRange
;
274 int PointDimension
; /* ip */
275 int TileDimension
; /* it */
276 isl_int VectorWidthMP
;
279 /* assert (0 <= DimToVectorize && DimToVectorize < ScheduleDimensions);*/
281 Space
= isl_space_alloc (ctx
, 0, ScheduleDimensions
, ScheduleDimensions
+ 1);
282 TilingMap
= isl_map_universe (isl_space_copy (Space
));
283 LocalSpace
= isl_local_space_from_space (Space
);
284 PointDimension
= ScheduleDimensions
;
285 TileDimension
= DimToVectorize
;
287 /* Create an identity map for everything except DimToVectorize and map
288 DimToVectorize to the point loop at the innermost dimension. */
289 for (i
= 0; i
< ScheduleDimensions
; i
++)
291 c
= isl_equality_alloc (isl_local_space_copy (LocalSpace
));
292 isl_constraint_set_coefficient_si (c
, isl_dim_in
, i
, -1);
294 if (i
== DimToVectorize
)
295 isl_constraint_set_coefficient_si (c
, isl_dim_out
, PointDimension
, 1);
297 isl_constraint_set_coefficient_si (c
, isl_dim_out
, i
, 1);
299 TilingMap
= isl_map_add_constraint (TilingMap
, c
);
302 /* it % 'VectorWidth' = 0 */
303 LocalSpaceRange
= isl_local_space_range (isl_local_space_copy (LocalSpace
));
304 Aff
= isl_aff_zero_on_domain (LocalSpaceRange
);
305 Aff
= isl_aff_set_constant_si (Aff
, VectorWidth
);
306 Aff
= isl_aff_set_coefficient_si (Aff
, isl_dim_in
, TileDimension
, 1);
307 isl_int_init (VectorWidthMP
);
308 isl_int_set_si (VectorWidthMP
, VectorWidth
);
309 Aff
= isl_aff_mod (Aff
, VectorWidthMP
);
310 isl_int_clear (VectorWidthMP
);
311 Modulo
= isl_pw_aff_zero_set (isl_pw_aff_from_aff (Aff
));
312 TilingMap
= isl_map_intersect_range (TilingMap
, Modulo
);
315 c
= isl_inequality_alloc (isl_local_space_copy (LocalSpace
));
316 isl_constraint_set_coefficient_si (c
, isl_dim_out
, TileDimension
, -1);
317 isl_constraint_set_coefficient_si (c
, isl_dim_out
, PointDimension
, 1);
318 TilingMap
= isl_map_add_constraint (TilingMap
, c
);
320 /* ip <= it + ('VectorWidth' - 1) */
321 c
= isl_inequality_alloc (LocalSpace
);
322 isl_constraint_set_coefficient_si (c
, isl_dim_out
, TileDimension
, 1);
323 isl_constraint_set_coefficient_si (c
, isl_dim_out
, PointDimension
, -1);
324 isl_constraint_set_constant_si (c
, VectorWidth
- 1);
325 TilingMap
= isl_map_add_constraint (TilingMap
, c
);
327 isl_map_dump (TilingMap
);
332 static bool EnablePollyVector
= false;
334 /* getScheduleForBandList - Get the scheduling map for a list of bands.
336 We walk recursively the forest of bands to combine the schedules of the
337 individual bands to the overall schedule. In case tiling is requested,
338 the individual bands are tiled. */
339 static isl_union_map
*
340 getScheduleForBandList (isl_band_list
*BandList
)
343 isl_union_map
*Schedule
;
346 ctx
= isl_band_list_get_ctx (BandList
);
347 NumBands
= isl_band_list_n_band (BandList
);
348 Schedule
= isl_union_map_empty (isl_space_params_alloc (ctx
, 0));
350 for (i
= 0; i
< NumBands
; i
++)
353 isl_union_map
*PartialSchedule
;
354 int ScheduleDimensions
;
357 Band
= isl_band_list_get_band (BandList
, i
);
358 PartialSchedule
= getScheduleForBand (Band
, &ScheduleDimensions
);
359 Space
= isl_union_map_get_space (PartialSchedule
);
361 if (isl_band_has_children (Band
))
363 isl_band_list
*Children
;
364 isl_union_map
*SuffixSchedule
;
366 Children
= isl_band_get_children (Band
);
367 SuffixSchedule
= getScheduleForBandList (Children
);
368 PartialSchedule
= isl_union_map_flat_range_product (PartialSchedule
,
370 isl_band_list_free (Children
);
372 else if (EnablePollyVector
)
374 for (i
= ScheduleDimensions
- 1 ; i
>= 0 ; i
--)
376 if (isl_band_member_is_zero_distance (Band
, i
))
379 isl_union_map
*TileUMap
;
381 TileMap
= getPrevectorMap (ctx
, i
, ScheduleDimensions
, 4);
382 TileUMap
= isl_union_map_from_map (TileMap
);
383 TileUMap
= isl_union_map_align_params
384 (TileUMap
, isl_space_copy (Space
));
385 PartialSchedule
= isl_union_map_apply_range
386 (PartialSchedule
, TileUMap
);
392 Schedule
= isl_union_map_union (Schedule
, PartialSchedule
);
394 isl_band_free (Band
);
395 isl_space_free (Space
);
401 static isl_union_map
*
402 getScheduleMap (isl_schedule
*Schedule
)
404 isl_band_list
*BandList
= isl_schedule_get_band_forest (Schedule
);
405 isl_union_map
*ScheduleMap
= getScheduleForBandList (BandList
);
406 isl_band_list_free (BandList
);
411 getSingleMap (__isl_take isl_map
*map
, void *user
)
413 isl_map
**singleMap
= (isl_map
**) user
;
420 apply_schedule_map_to_scop (scop_p scop
, isl_union_map
*schedule_map
)
425 FOR_EACH_VEC_ELT (scop
->bbs
, i
, pbb
)
427 isl_set
*domain
= isl_set_copy (pbb
->domain
);
428 isl_union_map
*stmtBand
;
429 isl_map
*stmtSchedule
;
431 stmtBand
= isl_union_map_intersect_domain
432 (isl_union_map_copy (schedule_map
),
433 isl_union_set_from_set (domain
));
434 isl_union_map_foreach_map (stmtBand
, getSingleMap
, &stmtSchedule
);
435 isl_map_free (pbb
->transformed
);
436 pbb
->transformed
= stmtSchedule
;
437 isl_union_map_free (stmtBand
);
441 static const int CONSTANT_BOUND
= 20;
444 optimize_isl (scop_p scop
)
447 isl_schedule
*schedule
;
448 isl_union_set
*domain
;
449 isl_union_map
*validity
, *proximity
, *dependences
;
450 isl_union_map
*schedule_map
;
452 domain
= scop_get_domains (scop
);
453 dependences
= scop_get_dependences (scop
);
454 dependences
= isl_union_map_gist_domain (dependences
,
455 isl_union_set_copy (domain
));
456 dependences
= isl_union_map_gist_range (dependences
,
457 isl_union_set_copy (domain
));
458 validity
= dependences
;
460 proximity
= isl_union_map_copy (validity
);
462 isl_options_set_schedule_max_constant_term (scop
->ctx
, CONSTANT_BOUND
);
463 isl_options_set_schedule_maximize_band_depth (scop
->ctx
, 1);
464 isl_options_set_schedule_fuse (scop
->ctx
, ISL_SCHEDULE_FUSE_MIN
);
465 isl_options_set_on_error (scop
->ctx
, ISL_ON_ERROR_CONTINUE
);
466 schedule
= isl_union_set_compute_schedule (domain
, validity
, proximity
);
467 isl_options_set_on_error (scop
->ctx
, ISL_ON_ERROR_ABORT
);
472 schedule_map
= getScheduleMap (schedule
);
474 apply_schedule_map_to_scop (scop
, schedule_map
);
476 isl_schedule_free (schedule
);
477 isl_union_map_free (schedule_map
);