/* Translation of ISL AST to Gimple.
- Copyright (C) 2014 Free Software Foundation, Inc.
+ Copyright (C) 2014-2015 Free Software Foundation, Inc.
Contributed by Roman Gareev <gareevroman@gmail.com>.
This file is part of GCC.
#include "config.h"
-#ifdef HAVE_cloog
+#ifdef HAVE_isl
#include <isl/set.h>
#include <isl/map.h>
#include <isl/union_map.h>
#include <isl/ast_build.h>
-#if defined(__cplusplus)
+
+/* Since ISL-0.13, the extern is in val_gmp.h. */
+#if !defined(HAVE_ISL_SCHED_CONSTRAINTS_COMPUTE_SCHEDULE) && defined(__cplusplus)
extern "C" {
#endif
#include <isl/val_gmp.h>
-#if defined(__cplusplus)
+#if !defined(HAVE_ISL_SCHED_CONSTRAINTS_COMPUTE_SCHEDULE) && defined(__cplusplus)
}
#endif
#endif
#include "system.h"
#include "coretypes.h"
+#include "alias.h"
+#include "symtab.h"
+#include "options.h"
#include "tree.h"
+#include "fold-const.h"
+#include "predict.h"
+#include "tm.h"
+#include "hard-reg-set.h"
+#include "function.h"
+#include "dominance.h"
+#include "cfg.h"
#include "basic-block.h"
#include "tree-ssa-alias.h"
#include "internal-fn.h"
#include "gimple-expr.h"
-#include "is-a.h"
#include "gimple.h"
#include "gimple-iterator.h"
#include "tree-ssa-loop.h"
#include "tree-into-ssa.h"
#include <map>
-#ifdef HAVE_cloog
+#ifdef HAVE_isl
#include "graphite-poly.h"
#include "graphite-isl-ast-to-gimple.h"
static int graphite_expression_type_precision = 128 <= max_mode_int_precision ?
128 : max_mode_int_precision;
+struct ast_build_info
+{
+ ast_build_info()
+ : is_parallelizable(false)
+ { };
+ bool is_parallelizable;
+};
+
/* Converts a GMP constant VAL to a tree and returns it. */
static tree
ivs_params &ip);
/* Return the tree variable that corresponds to the given isl ast identifier
- expression (an isl_ast_expr of type isl_ast_expr_id). */
+ expression (an isl_ast_expr of type isl_ast_expr_id).
+
+ FIXME: We should replace blind conversation of id's type with derivation
+ of the optimal type when we get the corresponding isl support. Blindly
+ converting type sizes may be problematic when we switch to smaller
+ types. */
static tree
-gcc_expression_from_isl_ast_expr_id (__isl_keep isl_ast_expr *expr_id,
+gcc_expression_from_isl_ast_expr_id (tree type,
+ __isl_keep isl_ast_expr *expr_id,
ivs_params &ip)
{
gcc_assert (isl_ast_expr_get_type (expr_id) == isl_ast_expr_id);
gcc_assert (res != ip.end () &&
"Could not map isl_id to tree expression");
isl_ast_expr_free (expr_id);
- return res->second;
+ return fold_convert (type, res->second);
}
/* Converts an isl_ast_expr_int expression E to a GCC expression tree of
case isl_ast_op_div:
return fold_build2 (EXACT_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
+ case isl_ast_op_pdiv_q:
+ return fold_build2 (TRUNC_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
+
+ case isl_ast_op_pdiv_r:
+ return fold_build2 (TRUNC_MOD_EXPR, type, tree_lhs_expr, tree_rhs_expr);
+
case isl_ast_op_fdiv_q:
return fold_build2 (FLOOR_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
case isl_ast_op_call:
case isl_ast_op_and_then:
case isl_ast_op_or_else:
- case isl_ast_op_pdiv_q:
- case isl_ast_op_pdiv_r:
case isl_ast_op_select:
gcc_unreachable ();
case isl_ast_op_sub:
case isl_ast_op_mul:
case isl_ast_op_div:
+ case isl_ast_op_pdiv_q:
+ case isl_ast_op_pdiv_r:
case isl_ast_op_fdiv_q:
case isl_ast_op_and:
case isl_ast_op_or:
switch (isl_ast_expr_get_type (expr))
{
case isl_ast_expr_id:
- return gcc_expression_from_isl_ast_expr_id (expr, ip);
+ return gcc_expression_from_isl_ast_expr_id (type, expr, ip);
case isl_ast_expr_int:
return gcc_expression_from_isl_expr_int (type, expr);
isl_ast_expr *for_iterator = isl_ast_node_for_get_iterator (node_for);
isl_id *id = isl_ast_expr_get_id (for_iterator);
+ std::map<isl_id *, tree>::iterator res;
+ res = ip.find (id);
+ if (ip.count (id))
+ isl_id_free (res->first);
ip[id] = iv;
isl_ast_expr_free (for_iterator);
return loop;
redirect_edge_succ_nodup (next_e, after);
set_immediate_dominator (CDI_DOMINATORS, next_e->dest, next_e->src);
- /* TODO: Add checking for the loop parallelism. */
+ if (flag_loop_parallelize_all)
+ {
+ isl_id *id = isl_ast_node_get_annotation (node_for);
+ gcc_assert (id);
+ ast_build_info *for_info = (ast_build_info *) isl_id_get_user (id);
+ loop->can_be_parallel = for_info->is_parallelizable;
+ free (for_info);
+ isl_id_free (id);
+ }
return last_e;
}
gcc_assert (GBB_BB (gbb) != ENTRY_BLOCK_PTR_FOR_FN (cfun) &&
"The entry block should not even appear within a scop");
- loop_p loop = gbb_loop (gbb);
- iv_map.create (loop->num + 1);
- iv_map.safe_grow_cleared (loop->num + 1);
+ int nb_loops = number_of_loops (cfun);
+ iv_map.create (nb_loops);
+ iv_map.safe_grow_cleared (nb_loops);
build_iv_mapping (iv_map, gbb, user_expr, ip, SCOP_REGION (pbb->scop));
isl_ast_expr_free (user_expr);
return next_e;
}
+/* Translates an isl_ast_node_block to Gimple. */
+
+static edge
+translate_isl_ast_node_block (loop_p context_loop,
+ __isl_keep isl_ast_node *node,
+ edge next_e, ivs_params &ip)
+{
+ gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_block);
+ isl_ast_node_list *node_list = isl_ast_node_block_get_children (node);
+ int i;
+ for (i = 0; i < isl_ast_node_list_n_ast_node (node_list); i++)
+ {
+ isl_ast_node *tmp_node = isl_ast_node_list_get_ast_node (node_list, i);
+ next_e = translate_isl_ast (context_loop, tmp_node, next_e, ip);
+ isl_ast_node_free (tmp_node);
+ }
+ isl_ast_node_list_free (node_list);
+ return next_e;
+}
+
+/* Creates a new if region corresponding to ISL's cond. */
+
+static edge
+graphite_create_new_guard (edge entry_edge, __isl_take isl_ast_expr *if_cond,
+ ivs_params &ip)
+{
+ tree type =
+ build_nonstandard_integer_type (graphite_expression_type_precision, 0);
+ tree cond_expr = gcc_expression_from_isl_expression (type, if_cond, ip);
+ edge exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
+ return exit_edge;
+}
+
+/* Translates an isl_ast_node_if to Gimple. */
+
+static edge
+translate_isl_ast_node_if (loop_p context_loop,
+ __isl_keep isl_ast_node *node,
+ edge next_e, ivs_params &ip)
+{
+ gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_if);
+ isl_ast_expr *if_cond = isl_ast_node_if_get_cond (node);
+ edge last_e = graphite_create_new_guard (next_e, if_cond, ip);
+
+ edge true_e = get_true_edge_from_guard_bb (next_e->dest);
+ isl_ast_node *then_node = isl_ast_node_if_get_then (node);
+ translate_isl_ast (context_loop, then_node, true_e, ip);
+ isl_ast_node_free (then_node);
+
+ edge false_e = get_false_edge_from_guard_bb (next_e->dest);
+ isl_ast_node *else_node = isl_ast_node_if_get_else (node);
+ if (isl_ast_node_get_type (else_node) != isl_ast_node_error)
+ translate_isl_ast (context_loop, else_node, false_e, ip);
+ isl_ast_node_free (else_node);
+ return last_e;
+}
+
/* Translates an ISL AST node NODE to GCC representation in the
context of a SESE. */
next_e, ip);
case isl_ast_node_if:
- return next_e;
+ return translate_isl_ast_node_if (context_loop, node,
+ next_e, ip);
case isl_ast_node_user:
return translate_isl_ast_node_user (node, next_e, ip);
case isl_ast_node_block:
- return next_e;
+ return translate_isl_ast_node_block (context_loop, node,
+ next_e, ip);
default:
gcc_unreachable ();
return isl_ast_build_from_context (context_isl);
}
+/* Get the maximal number of schedule dimensions in the scop SCOP. */
+
+static
+int get_max_schedule_dimensions (scop_p scop)
+{
+ int i;
+ poly_bb_p pbb;
+ int schedule_dims = 0;
+
+ FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
+ {
+ int pbb_schedule_dims = isl_map_dim (pbb->transformed, isl_dim_out);
+ if (pbb_schedule_dims > schedule_dims)
+ schedule_dims = pbb_schedule_dims;
+ }
+
+ return schedule_dims;
+}
+
+/* Extend the schedule to NB_SCHEDULE_DIMS schedule dimensions.
+
+ For schedules with different dimensionality, the isl AST generator can not
+ define an order and will just randomly choose an order. The solution to this
+ problem is to extend all schedules to the maximal number of schedule
+ dimensions (using '0's for the remaining values). */
+
+static __isl_give isl_map *
+extend_schedule (__isl_take isl_map *schedule, int nb_schedule_dims)
+{
+ int tmp_dims = isl_map_dim (schedule, isl_dim_out);
+ schedule =
+ isl_map_add_dims (schedule, isl_dim_out, nb_schedule_dims - tmp_dims);
+ isl_val *zero =
+ isl_val_int_from_si (isl_map_get_ctx (schedule), 0);
+ int i;
+ for (i = tmp_dims; i < nb_schedule_dims; i++)
+ {
+ schedule =
+ isl_map_fix_val (schedule, isl_dim_out, i, isl_val_copy (zero));
+ }
+ isl_val_free (zero);
+ return schedule;
+}
+
+/* Set the separation_class option for unroll and jam. */
+
+static __isl_give isl_union_map *
+generate_luj_sepclass_opt (scop_p scop, __isl_take isl_union_set *domain,
+ int dim, int cl)
+{
+ isl_map *map;
+ isl_space *space, *space_sep;
+ isl_ctx *ctx;
+ isl_union_map *mapu;
+ int nsched = get_max_schedule_dimensions (scop);
+
+ ctx = scop->ctx;
+ space_sep = isl_space_alloc (ctx, 0, 1, 1);
+ space_sep = isl_space_wrap (space_sep);
+ space_sep = isl_space_set_tuple_name (space_sep, isl_dim_set,
+ "separation_class");
+ space = isl_set_get_space (scop->context);
+ space_sep = isl_space_align_params (space_sep, isl_space_copy(space));
+ space = isl_space_map_from_domain_and_range (space, space_sep);
+ space = isl_space_add_dims (space,isl_dim_in, nsched);
+ map = isl_map_universe (space);
+ isl_map_fix_si (map,isl_dim_out,0,dim);
+ isl_map_fix_si (map,isl_dim_out,1,cl);
+
+ mapu = isl_union_map_intersect_domain (isl_union_map_from_map (map),
+ domain);
+ return (mapu);
+}
+
+/* Compute the separation class for loop unroll and jam. */
+
+static __isl_give isl_union_set *
+generate_luj_sepclass (scop_p scop)
+{
+ int i;
+ poly_bb_p pbb;
+ isl_union_set *domain_isl;
+
+ domain_isl = isl_union_set_empty (isl_set_get_space (scop->context));
+
+ FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
+ {
+ isl_set *bb_domain;
+ isl_set *bb_domain_s;
+
+ if (pbb->map_sepclass == NULL)
+ continue;
+
+ if (isl_set_is_empty (pbb->domain))
+ continue;
+
+ bb_domain = isl_set_copy (pbb->domain);
+ bb_domain_s = isl_set_apply (bb_domain, pbb->map_sepclass);
+ pbb->map_sepclass = NULL;
+
+ domain_isl =
+ isl_union_set_union (domain_isl, isl_union_set_from_set (bb_domain_s));
+ }
+
+ return domain_isl;
+}
+
+/* Set the AST built options for loop unroll and jam. */
+
+static __isl_give isl_union_map *
+generate_luj_options (scop_p scop)
+{
+ isl_union_set *domain_isl;
+ isl_union_map *options_isl_ss;
+ isl_union_map *options_isl =
+ isl_union_map_empty (isl_set_get_space (scop->context));
+ int dim = get_max_schedule_dimensions (scop) - 1;
+ int dim1 = dim - PARAM_VALUE (PARAM_LOOP_UNROLL_JAM_DEPTH);
+
+ if (!flag_loop_unroll_jam)
+ return options_isl;
+
+ domain_isl = generate_luj_sepclass (scop);
+
+ options_isl_ss = generate_luj_sepclass_opt (scop, domain_isl, dim1, 0);
+ options_isl = isl_union_map_union (options_isl, options_isl_ss);
+
+ return options_isl;
+}
+
/* Generates a schedule, which specifies an order used to
visit elements in a domain. */
static __isl_give isl_union_map *
generate_isl_schedule (scop_p scop)
{
+ int nb_schedule_dims = get_max_schedule_dimensions (scop);
int i;
poly_bb_p pbb;
isl_union_map *schedule_isl =
isl_map *bb_schedule = isl_map_copy (pbb->transformed);
bb_schedule = isl_map_intersect_domain (bb_schedule,
isl_set_copy (pbb->domain));
+ bb_schedule = extend_schedule (bb_schedule, nb_schedule_dims);
schedule_isl =
isl_union_map_union (schedule_isl,
isl_union_map_from_map (bb_schedule));
return schedule_isl;
}
+/* This method is executed before the construction of a for node. */
+static __isl_give isl_id *
+ast_build_before_for (__isl_keep isl_ast_build *build, void *user)
+{
+ isl_union_map *dependences = (isl_union_map *) user;
+ ast_build_info *for_info = XNEW (struct ast_build_info);
+ isl_union_map *schedule = isl_ast_build_get_schedule (build);
+ isl_space *schedule_space = isl_ast_build_get_schedule_space (build);
+ int dimension = isl_space_dim (schedule_space, isl_dim_out);
+ for_info->is_parallelizable =
+ !carries_deps (schedule, dependences, dimension);
+ isl_union_map_free (schedule);
+ isl_space_free (schedule_space);
+ isl_id *id = isl_id_alloc (isl_ast_build_get_ctx (build), "", for_info);
+ return id;
+}
+
+/* Set the separate option for all dimensions.
+ This helps to reduce control overhead.
+ Set the options for unroll and jam. */
+
+static __isl_give isl_ast_build *
+set_options (__isl_take isl_ast_build *control,
+ __isl_keep isl_union_map *schedule,
+ __isl_take isl_union_map *opt_luj)
+{
+ isl_ctx *ctx = isl_union_map_get_ctx (schedule);
+ isl_space *range_space = isl_space_set_alloc (ctx, 0, 1);
+ range_space =
+ isl_space_set_tuple_name (range_space, isl_dim_set, "separate");
+ isl_union_set *range =
+ isl_union_set_from_set (isl_set_universe (range_space));
+ isl_union_set *domain = isl_union_map_range (isl_union_map_copy (schedule));
+ domain = isl_union_set_universe (domain);
+ isl_union_map *options = isl_union_map_from_domain_and_range (domain, range);
+
+ options = isl_union_map_union (options, opt_luj);
+
+ return isl_ast_build_set_options (control, options);
+}
+
static __isl_give isl_ast_node *
scop_to_isl_ast (scop_p scop, ivs_params &ip)
{
isl_options_set_ast_build_atomic_upper_bound (scop->ctx, true);
add_parameters_to_ivs_params (scop, ip);
+
+ isl_union_map *options_luj = generate_luj_options (scop);
+
isl_union_map *schedule_isl = generate_isl_schedule (scop);
isl_ast_build *context_isl = generate_isl_context (scop);
+
+ context_isl = set_options (context_isl, schedule_isl, options_luj);
+
+ isl_union_map *dependences = NULL;
+ if (flag_loop_parallelize_all)
+ {
+ dependences = scop_get_dependences (scop);
+ context_isl =
+ isl_ast_build_set_before_each_for (context_isl, ast_build_before_for,
+ dependences);
+ }
isl_ast_node *ast_isl = isl_ast_build_ast_from_schedule (context_isl,
schedule_isl);
+ if(dependences)
+ isl_union_map_free (dependences);
isl_ast_build_free (context_isl);
return ast_isl;
}
translate_isl_ast (context_loop, root_node, if_region->true_region->entry,
ip);
+
+ mark_virtual_operands_for_renaming (cfun);
+ update_ssa (TODO_update_ssa);
+
graphite_verify ();
scev_reset ();
recompute_all_dominators ();
ivs_params_clear (ip);
isl_ast_node_free (root_node);
timevar_pop (TV_GRAPHITE_CODE_GEN);
- /* TODO: Add dump */
+
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ {
+ loop_p loop;
+ int num_no_dependency = 0;
+
+ FOR_EACH_LOOP (loop, 0)
+ if (loop->can_be_parallel)
+ num_no_dependency++;
+
+ fprintf (dump_file, "\n%d loops carried no dependency.\n",
+ num_no_dependency);
+ }
+
return !graphite_regenerate_error;
}
#endif