1 /* A scheduling optimizer for Graphite
2 Copyright (C) 2012-2015 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/>. */
28 #include "coretypes.h"
33 #include "fold-const.h"
34 #include "gimple-iterator.h"
35 #include "tree-ssa-loop.h"
37 #include "tree-data-ref.h"
41 #include <isl/constraint.h>
43 #include <isl/union_set.h>
45 #include <isl/union_map.h>
46 #include <isl/schedule.h>
49 #include <isl/options.h>
51 #ifdef HAVE_ISL_OPTIONS_SET_SCHEDULE_SERIALIZE_SCCS
52 #include <isl/schedule_node.h>
57 #ifdef HAVE_ISL_OPTIONS_SET_SCHEDULE_SERIALIZE_SCCS
59 /* get_schedule_for_node_st - Improve schedule for the schedule node.
60 Only Simple loop tiling is considered. */
62 static __isl_give isl_schedule_node
*
63 get_schedule_for_node_st (__isl_take isl_schedule_node
*node
, void *user
)
68 if (isl_schedule_node_get_type (node
) != isl_schedule_node_band
69 || isl_schedule_node_n_children (node
) != 1)
72 isl_space
*space
= isl_schedule_node_band_get_space (node
);
73 unsigned dims
= isl_space_dim (space
, isl_dim_set
);
74 isl_schedule_node
*child
= isl_schedule_node_get_child (node
, 0);
75 isl_schedule_node_type type
= isl_schedule_node_get_type (child
);
76 isl_space_free (space
);
77 isl_schedule_node_free (child
);
79 if (type
!= isl_schedule_node_leaf
)
82 if (dims
<= 1 || !isl_schedule_node_band_get_permutable (node
))
84 if (dump_file
&& dump_flags
)
85 fprintf (dump_file
, "not tiled\n");
90 space
= isl_schedule_node_band_get_space (node
);
91 isl_multi_val
*sizes
= isl_multi_val_zero (space
);
92 long tile_size
= PARAM_VALUE (PARAM_LOOP_BLOCK_TILE_SIZE
);
93 isl_ctx
*ctx
= isl_schedule_node_get_ctx (node
);
95 for (unsigned i
= 0; i
< dims
; i
++)
97 sizes
= isl_multi_val_set_val (sizes
, i
,
98 isl_val_int_from_si (ctx
, tile_size
));
99 if (dump_file
&& dump_flags
)
100 fprintf (dump_file
, "tiled by %ld\n", tile_size
);
103 node
= isl_schedule_node_band_tile (node
, sizes
);
104 node
= isl_schedule_node_child (node
, 0);
109 /* get_schedule_map_st - Improve the schedule by performing other loop
110 optimizations. _st ending is for schedule tree version of this
111 function (see get_schedule_map below for the band forest version).
113 Do a depth-first post-order traversal of the nodes in a schedule
114 tree and apply get_schedule_for_node_st on them to improve the schedule.
117 static __isl_give isl_union_map
*
118 get_schedule_map_st (__isl_keep isl_schedule
*schedule
)
121 schedule
= isl_schedule_map_schedule_node_bottom_up (schedule
,
122 get_schedule_for_node_st
,
124 isl_union_map
*schedule_map
= isl_schedule_get_map (schedule
);
129 /* get_tile_map - Create a map that describes a n-dimensonal tiling.
131 get_tile_map creates a map from a n-dimensional scattering space into an
132 2*n-dimensional scattering space. The map describes a rectangular tiling.
135 SCHEDULE_DIMENSIONS = 2, PARAMETER_DIMENSIONS = 1, TILE_SIZE = 32
137 tile_map := [p0] -> {[s0, s1] -> [t0, t1, s0, s1]:
138 t0 % 32 = 0 and t0 <= s0 < t0 + 32 and
139 t1 % 32 = 0 and t1 <= s1 < t1 + 32}
143 for (i = 0; i < N; i++)
144 for (j = 0; j < M; j++)
149 for (t_i = 0; t_i < N; i+=32)
150 for (t_j = 0; t_j < M; j+=32)
151 for (i = t_i; i < min(t_i + 32, N); i++) | Unknown that N % 32 = 0
152 for (j = t_j; j < t_j + 32; j++) | Known that M % 32 = 0
156 static isl_basic_map
*
157 get_tile_map (isl_ctx
*ctx
, int schedule_dimensions
, int tile_size
)
161 tile_map := [p0] -> {[s0, s1] -> [t0, t1, p0, p1, a0, a1]:
162 s0 = a0 * 32 and s0 = p0 and t0 <= p0 < t0 + 32 and
163 s1 = a1 * 32 and s1 = p1 and t1 <= p1 < t1 + 32}
165 and project out the auxilary dimensions a0 and a1. */
167 = isl_space_alloc (ctx
, 0, schedule_dimensions
, schedule_dimensions
* 3);
168 isl_basic_map
*tile_map
= isl_basic_map_universe (isl_space_copy (space
));
170 isl_local_space
*local_space
= isl_local_space_from_space (space
);
172 for (int x
= 0; x
< schedule_dimensions
; x
++)
176 int pX
= schedule_dimensions
+ x
;
177 int aX
= 2 * schedule_dimensions
+ x
;
181 /* sX = aX * tile_size; */
182 c
= isl_equality_alloc (isl_local_space_copy (local_space
));
183 isl_constraint_set_coefficient_si (c
, isl_dim_out
, sX
, 1);
184 isl_constraint_set_coefficient_si (c
, isl_dim_out
, aX
, -tile_size
);
185 tile_map
= isl_basic_map_add_constraint (tile_map
, c
);
188 c
= isl_equality_alloc (isl_local_space_copy (local_space
));
189 isl_constraint_set_coefficient_si (c
, isl_dim_out
, pX
, 1);
190 isl_constraint_set_coefficient_si (c
, isl_dim_in
, sX
, -1);
191 tile_map
= isl_basic_map_add_constraint (tile_map
, c
);
194 c
= isl_inequality_alloc (isl_local_space_copy (local_space
));
195 isl_constraint_set_coefficient_si (c
, isl_dim_out
, pX
, 1);
196 isl_constraint_set_coefficient_si (c
, isl_dim_out
, tX
, -1);
197 tile_map
= isl_basic_map_add_constraint (tile_map
, c
);
199 /* pX <= tX + (tile_size - 1) */
200 c
= isl_inequality_alloc (isl_local_space_copy (local_space
));
201 isl_constraint_set_coefficient_si (c
, isl_dim_out
, tX
, 1);
202 isl_constraint_set_coefficient_si (c
, isl_dim_out
, pX
, -1);
203 isl_constraint_set_constant_si (c
, tile_size
- 1);
204 tile_map
= isl_basic_map_add_constraint (tile_map
, c
);
207 /* Project out auxiliary dimensions.
209 The auxiliary dimensions are transformed into existentially quantified
211 This reduces the number of visible scattering dimensions and allows isl
212 to produces better code. */
214 isl_basic_map_project_out (tile_map
, isl_dim_out
,
215 2 * schedule_dimensions
, schedule_dimensions
);
216 isl_local_space_free (local_space
);
220 /* get_schedule_for_band - Get the schedule for this BAND.
222 Polly applies transformations like tiling on top of the isl calculated
224 This can influence the number of scheduling dimension. The number of
225 schedule dimensions is returned in DIMENSIONS. */
227 static isl_union_map
*
228 get_schedule_for_band (isl_band
*band
, int *dimensions
)
230 isl_union_map
*partial_schedule
;
233 isl_basic_map
*tile_map
;
234 isl_union_map
*tile_umap
;
236 partial_schedule
= isl_band_get_partial_schedule (band
);
237 *dimensions
= isl_band_n_member (band
);
239 /* It does not make any sense to tile a band with just one dimension. */
240 if (*dimensions
== 1)
242 if (dump_file
&& dump_flags
)
243 fprintf (dump_file
, "not tiled\n");
244 return partial_schedule
;
247 if (dump_file
&& dump_flags
)
248 fprintf (dump_file
, "tiled by %d\n",
249 PARAM_VALUE (PARAM_LOOP_BLOCK_TILE_SIZE
));
251 ctx
= isl_union_map_get_ctx (partial_schedule
);
252 space
= isl_union_map_get_space (partial_schedule
);
254 tile_map
= get_tile_map (ctx
, *dimensions
,
255 PARAM_VALUE (PARAM_LOOP_BLOCK_TILE_SIZE
));
256 tile_umap
= isl_union_map_from_map (isl_map_from_basic_map (tile_map
));
257 tile_umap
= isl_union_map_align_params (tile_umap
, space
);
258 *dimensions
= 2 * *dimensions
;
260 return isl_union_map_apply_range (partial_schedule
, tile_umap
);
264 /* get_schedule_for_band_list - Get the scheduling map for a list of bands.
266 We walk recursively the forest of bands to combine the schedules of the
267 individual bands to the overall schedule. In case tiling is requested,
268 the individual bands are tiled. */
270 static isl_union_map
*
271 get_schedule_for_band_list (isl_band_list
*band_list
)
274 isl_union_map
*schedule
;
277 ctx
= isl_band_list_get_ctx (band_list
);
278 num_bands
= isl_band_list_n_band (band_list
);
279 schedule
= isl_union_map_empty (isl_space_params_alloc (ctx
, 0));
281 for (i
= 0; i
< num_bands
; i
++)
284 isl_union_map
*partial_schedule
;
285 int schedule_dimensions
;
288 band
= isl_band_list_get_band (band_list
, i
);
289 partial_schedule
= get_schedule_for_band (band
, &schedule_dimensions
);
290 space
= isl_union_map_get_space (partial_schedule
);
292 if (isl_band_has_children (band
))
294 isl_band_list
*children
= isl_band_get_children (band
);
295 isl_union_map
*suffixSchedule
296 = get_schedule_for_band_list (children
);
298 = isl_union_map_flat_range_product (partial_schedule
,
300 isl_band_list_free (children
);
303 schedule
= isl_union_map_union (schedule
, partial_schedule
);
305 isl_band_free (band
);
306 isl_space_free (space
);
312 static isl_union_map
*
313 get_schedule_map (isl_schedule
*schedule
)
315 isl_band_list
*bandList
= isl_schedule_get_band_forest (schedule
);
316 isl_union_map
*schedule_map
= get_schedule_for_band_list (bandList
);
317 isl_band_list_free (bandList
);
323 get_single_map (__isl_take isl_map
*map
, void *user
)
325 isl_map
**single_map
= (isl_map
**)user
;
331 apply_schedule_map_to_scop (scop_p scop
, isl_union_map
*schedule_map
)
336 FOR_EACH_VEC_ELT (scop
->pbbs
, i
, pbb
)
338 isl_set
*domain
= isl_set_copy (pbb
->domain
);
339 isl_map
*stmt_schedule
;
341 isl_union_map
*stmt_band
342 = isl_union_map_intersect_domain (isl_union_map_copy (schedule_map
),
343 isl_union_set_from_set (domain
));
344 isl_union_map_foreach_map (stmt_band
, get_single_map
, &stmt_schedule
);
345 isl_map_free (pbb
->transformed
);
346 pbb
->transformed
= stmt_schedule
;
347 isl_union_map_free (stmt_band
);
351 static isl_union_set
*
352 scop_get_domains (scop_p scop ATTRIBUTE_UNUSED
)
356 isl_space
*space
= isl_set_get_space (scop
->param_context
);
357 isl_union_set
*res
= isl_union_set_empty (space
);
359 FOR_EACH_VEC_ELT (scop
->pbbs
, i
, pbb
)
360 res
= isl_union_set_add_set (res
, isl_set_copy (pbb
->domain
));
365 static const int CONSTANT_BOUND
= 20;
367 /* Compute the schedule for SCOP based on its parameters, domain and set of
368 constraints. Then apply the schedule to SCOP. */
371 optimize_isl (scop_p scop
)
373 #ifdef HAVE_ISL_CTX_MAX_OPERATIONS
374 int old_max_operations
= isl_ctx_get_max_operations (scop
->isl_context
);
375 int max_operations
= PARAM_VALUE (PARAM_MAX_ISL_OPERATIONS
);
377 isl_ctx_set_max_operations (scop
->isl_context
, max_operations
);
379 isl_options_set_on_error (scop
->isl_context
, ISL_ON_ERROR_CONTINUE
);
381 isl_union_set
*domain
= scop_get_domains (scop
);
382 isl_union_map
*dependences
= scop_get_dependences (scop
);
384 = isl_union_map_gist_domain (dependences
, isl_union_set_copy (domain
));
386 = isl_union_map_gist_range (dependences
, isl_union_set_copy (domain
));
387 isl_union_map
*validity
= dependences
;
388 isl_union_map
*proximity
= isl_union_map_copy (validity
);
390 #ifdef HAVE_ISL_SCHED_CONSTRAINTS_COMPUTE_SCHEDULE
391 isl_schedule_constraints
*schedule_constraints
;
392 schedule_constraints
= isl_schedule_constraints_on_domain (domain
);
394 = isl_schedule_constraints_set_proximity (schedule_constraints
,
397 = isl_schedule_constraints_set_validity (schedule_constraints
,
398 isl_union_map_copy (validity
));
400 = isl_schedule_constraints_set_coincidence (schedule_constraints
,
404 isl_options_set_schedule_max_constant_term (scop
->isl_context
, CONSTANT_BOUND
);
405 isl_options_set_schedule_maximize_band_depth (scop
->isl_context
, 1);
406 #ifdef HAVE_ISL_OPTIONS_SET_SCHEDULE_SERIALIZE_SCCS
407 /* ISL-0.15 or later. */
408 isl_options_set_schedule_maximize_band_depth (scop
->isl_context
, 1);
410 isl_options_set_schedule_fuse (scop
->isl_context
, ISL_SCHEDULE_FUSE_MIN
);
413 #ifdef HAVE_ISL_SCHED_CONSTRAINTS_COMPUTE_SCHEDULE
414 isl_schedule
*schedule
415 = isl_schedule_constraints_compute_schedule (schedule_constraints
);
417 isl_schedule
*schedule
418 = isl_union_set_compute_schedule (domain
, validity
, proximity
);
421 isl_options_set_on_error (scop
->isl_context
, ISL_ON_ERROR_ABORT
);
423 #ifdef HAVE_ISL_CTX_MAX_OPERATIONS
424 isl_ctx_reset_operations (scop
->isl_context
);
425 isl_ctx_set_max_operations (scop
->isl_context
, old_max_operations
);
426 if (!schedule
|| isl_ctx_last_error (scop
->isl_context
) == isl_error_quota
)
428 if (dump_file
&& dump_flags
)
429 fprintf (dump_file
, "ISL timed out at %d operations\n",
432 isl_schedule_free (schedule
);
440 #ifdef HAVE_ISL_OPTIONS_SET_SCHEDULE_SERIALIZE_SCCS
441 isl_union_map
*schedule_map
= get_schedule_map_st (schedule
);
443 isl_union_map
*schedule_map
= get_schedule_map (schedule
);
445 apply_schedule_map_to_scop (scop
, schedule_map
);
447 isl_schedule_free (schedule
);
448 isl_union_map_free (schedule_map
);
452 #endif /* HAVE_isl */