Blame isl-0.16.1/codegen.c

Packit fb9d21
/*
Packit fb9d21
 * Copyright 2012,2014 Ecole Normale Superieure
Packit fb9d21
 *
Packit fb9d21
 * Use of this software is governed by the MIT license
Packit fb9d21
 *
Packit fb9d21
 * Written by Sven Verdoolaege,
Packit fb9d21
 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
Packit fb9d21
 */
Packit fb9d21
Packit fb9d21
/* This program prints an AST that scans the domain elements of
Packit fb9d21
 * the domain of a given schedule in the order specified by
Packit fb9d21
 * the schedule tree or by their image(s) in the schedule map.
Packit fb9d21
 *
Packit fb9d21
 * The input consists of either a schedule tree or
Packit fb9d21
 * a sequence of three sets/relations.
Packit fb9d21
 * - a schedule map
Packit fb9d21
 * - a context
Packit fb9d21
 * - a relation describing AST generation options
Packit fb9d21
 */
Packit fb9d21
Packit fb9d21
#include <assert.h>
Packit fb9d21
#include <stdlib.h>
Packit fb9d21
#include <isl/ast.h>
Packit fb9d21
#include <isl/ast_build.h>
Packit fb9d21
#include <isl/options.h>
Packit fb9d21
#include <isl/set.h>
Packit fb9d21
#include <isl/union_set.h>
Packit fb9d21
#include <isl/union_map.h>
Packit fb9d21
#include <isl/stream.h>
Packit fb9d21
#include <isl/schedule_node.h>
Packit fb9d21
Packit fb9d21
struct options {
Packit fb9d21
	struct isl_options	*isl;
Packit fb9d21
	unsigned		 atomic;
Packit fb9d21
	unsigned		 separate;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
ISL_ARGS_START(struct options, options_args)
Packit fb9d21
ISL_ARG_CHILD(struct options, isl, "isl", &isl_options_args, "isl options")
Packit fb9d21
ISL_ARG_BOOL(struct options, atomic, 0, "atomic", 0,
Packit fb9d21
	"globally set the atomic option")
Packit fb9d21
ISL_ARG_BOOL(struct options, separate, 0, "separate", 0,
Packit fb9d21
	"globally set the separate option")
Packit fb9d21
ISL_ARGS_END
Packit fb9d21
Packit fb9d21
ISL_ARG_DEF(cg_options, struct options, options_args)
Packit fb9d21
ISL_ARG_CTX_DEF(cg_options, struct options, options_args)
Packit fb9d21
Packit fb9d21
/* Return a universal, 1-dimensional set with the given name.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_union_set *universe(isl_ctx *ctx, const char *name)
Packit fb9d21
{
Packit fb9d21
	isl_space *space;
Packit fb9d21
Packit fb9d21
	space = isl_space_set_alloc(ctx, 0, 1);
Packit fb9d21
	space = isl_space_set_tuple_name(space, isl_dim_set, name);
Packit fb9d21
	return isl_union_set_from_set(isl_set_universe(space));
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Set the "name" option for the entire schedule domain.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_union_map *set_universe(__isl_take isl_union_map *opt,
Packit fb9d21
	__isl_keep isl_union_map *schedule, const char *name)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_union_set *domain, *target;
Packit fb9d21
	isl_union_map *option;
Packit fb9d21
Packit fb9d21
	ctx = isl_union_map_get_ctx(opt);
Packit fb9d21
Packit fb9d21
	domain = isl_union_map_range(isl_union_map_copy(schedule));
Packit fb9d21
	domain = isl_union_set_universe(domain);
Packit fb9d21
	target = universe(ctx, name);
Packit fb9d21
	option = isl_union_map_from_domain_and_range(domain, target);
Packit fb9d21
	opt = isl_union_map_union(opt, option);
Packit fb9d21
Packit fb9d21
	return opt;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Update the build options based on the user-specified options.
Packit fb9d21
 *
Packit fb9d21
 * If the --separate or --atomic options were specified, then
Packit fb9d21
 * we clear any separate or atomic options that may already exist in "opt".
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_ast_build *set_options(__isl_take isl_ast_build *build,
Packit fb9d21
	__isl_take isl_union_map *opt, struct options *options,
Packit fb9d21
	__isl_keep isl_union_map *schedule)
Packit fb9d21
{
Packit fb9d21
	if (options->separate || options->atomic) {
Packit fb9d21
		isl_ctx *ctx;
Packit fb9d21
		isl_union_set *target;
Packit fb9d21
Packit fb9d21
		ctx = isl_union_map_get_ctx(schedule);
Packit fb9d21
Packit fb9d21
		target = universe(ctx, "separate");
Packit fb9d21
		opt = isl_union_map_subtract_range(opt, target);
Packit fb9d21
		target = universe(ctx, "atomic");
Packit fb9d21
		opt = isl_union_map_subtract_range(opt, target);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	if (options->separate)
Packit fb9d21
		opt = set_universe(opt, schedule, "separate");
Packit fb9d21
	if (options->atomic)
Packit fb9d21
		opt = set_universe(opt, schedule, "atomic");
Packit fb9d21
Packit fb9d21
	build = isl_ast_build_set_options(build, opt);
Packit fb9d21
Packit fb9d21
	return build;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Construct an AST in case the schedule is specified by a union map.
Packit fb9d21
 *
Packit fb9d21
 * We read the context and the options from "s" and construct the AST.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_ast_node *construct_ast_from_union_map(
Packit fb9d21
	__isl_take isl_union_map *schedule, __isl_keep isl_stream *s)
Packit fb9d21
{
Packit fb9d21
	isl_set *context;
Packit fb9d21
	isl_union_map *options_map;
Packit fb9d21
	isl_ast_build *build;
Packit fb9d21
	isl_ast_node *tree;
Packit fb9d21
	struct options *options;
Packit fb9d21
Packit fb9d21
	options = isl_ctx_peek_cg_options(isl_stream_get_ctx(s));
Packit fb9d21
Packit fb9d21
	context = isl_stream_read_set(s);
Packit fb9d21
	options_map = isl_stream_read_union_map(s);
Packit fb9d21
Packit fb9d21
	build = isl_ast_build_from_context(context);
Packit fb9d21
	build = set_options(build, options_map, options, schedule);
Packit fb9d21
	tree = isl_ast_build_node_from_schedule_map(build, schedule);
Packit fb9d21
	isl_ast_build_free(build);
Packit fb9d21
Packit fb9d21
	return tree;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* If "node" is a band node, then replace the AST build options
Packit fb9d21
 * by "options".
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_schedule_node *node_set_options(
Packit fb9d21
	__isl_take isl_schedule_node *node, void *user)
Packit fb9d21
{
Packit fb9d21
	enum isl_ast_loop_type *type = user;
Packit fb9d21
	int i, n;
Packit fb9d21
Packit fb9d21
	if (isl_schedule_node_get_type(node) != isl_schedule_node_band)
Packit fb9d21
		return node;
Packit fb9d21
Packit fb9d21
	n = isl_schedule_node_band_n_member(node);
Packit fb9d21
	for (i = 0; i < n; ++i)
Packit fb9d21
		node = isl_schedule_node_band_member_set_ast_loop_type(node,
Packit fb9d21
								i, *type);
Packit fb9d21
	return node;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Replace the AST build options on all band nodes if requested
Packit fb9d21
 * by the user.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_schedule *schedule_set_options(
Packit fb9d21
	__isl_take isl_schedule *schedule, struct options *options)
Packit fb9d21
{
Packit fb9d21
	enum isl_ast_loop_type type;
Packit fb9d21
Packit fb9d21
	if (!options->separate && !options->atomic)
Packit fb9d21
		return schedule;
Packit fb9d21
Packit fb9d21
	type = options->separate ? isl_ast_loop_separate : isl_ast_loop_atomic;
Packit fb9d21
	schedule = isl_schedule_map_schedule_node_bottom_up(schedule,
Packit fb9d21
						&node_set_options, &type);
Packit fb9d21
Packit fb9d21
	return schedule;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Construct an AST in case the schedule is specified by a schedule tree.
Packit fb9d21
 */
Packit fb9d21
static __isl_give isl_ast_node *construct_ast_from_schedule(
Packit fb9d21
	__isl_take isl_schedule *schedule)
Packit fb9d21
{
Packit fb9d21
	isl_ast_build *build;
Packit fb9d21
	isl_ast_node *tree;
Packit fb9d21
	struct options *options;
Packit fb9d21
Packit fb9d21
	options = isl_ctx_peek_cg_options(isl_schedule_get_ctx(schedule));
Packit fb9d21
Packit fb9d21
	build = isl_ast_build_alloc(isl_schedule_get_ctx(schedule));
Packit fb9d21
	schedule = schedule_set_options(schedule, options);
Packit fb9d21
	tree = isl_ast_build_node_from_schedule(build, schedule);
Packit fb9d21
	isl_ast_build_free(build);
Packit fb9d21
Packit fb9d21
	return tree;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Read an object from stdin.
Packit fb9d21
 * If it is a (union) map, then assume an input specified by
Packit fb9d21
 * schedule map, context and options and construct an AST from
Packit fb9d21
 * those elements
Packit fb9d21
 * If it is a schedule object, then construct the AST from the schedule.
Packit fb9d21
 */
Packit fb9d21
int main(int argc, char **argv)
Packit fb9d21
{
Packit fb9d21
	isl_ctx *ctx;
Packit fb9d21
	isl_stream *s;
Packit fb9d21
	isl_ast_node *tree = NULL;
Packit fb9d21
	struct options *options;
Packit fb9d21
	isl_printer *p;
Packit fb9d21
	struct isl_obj obj;
Packit fb9d21
	int r = EXIT_SUCCESS;
Packit fb9d21
Packit fb9d21
	options = cg_options_new_with_defaults();
Packit fb9d21
	assert(options);
Packit fb9d21
	argc = cg_options_parse(options, argc, argv, ISL_ARG_ALL);
Packit fb9d21
Packit fb9d21
	ctx = isl_ctx_alloc_with_options(&options_args, options);
Packit fb9d21
Packit fb9d21
	s = isl_stream_new_file(ctx, stdin);
Packit fb9d21
	obj = isl_stream_read_obj(s);
Packit fb9d21
	if (obj.v == NULL) {
Packit fb9d21
		r = EXIT_FAILURE;
Packit fb9d21
	} else if (obj.type == isl_obj_map) {
Packit fb9d21
		isl_union_map *umap;
Packit fb9d21
Packit fb9d21
		umap = isl_union_map_from_map(obj.v);
Packit fb9d21
		tree = construct_ast_from_union_map(umap, s);
Packit fb9d21
	} else if (obj.type == isl_obj_union_map) {
Packit fb9d21
		tree = construct_ast_from_union_map(obj.v, s);
Packit fb9d21
	} else if (obj.type == isl_obj_schedule) {
Packit fb9d21
		tree = construct_ast_from_schedule(obj.v);
Packit fb9d21
	} else {
Packit fb9d21
		obj.type->free(obj.v);
Packit fb9d21
		isl_die(ctx, isl_error_invalid, "unknown input",
Packit fb9d21
			r = EXIT_FAILURE);
Packit fb9d21
	}
Packit fb9d21
	isl_stream_free(s);
Packit fb9d21
Packit fb9d21
	p = isl_printer_to_file(ctx, stdout);
Packit fb9d21
	p = isl_printer_set_output_format(p, ISL_FORMAT_C);
Packit fb9d21
	p = isl_printer_print_ast_node(p, tree);
Packit fb9d21
	isl_printer_free(p);
Packit fb9d21
Packit fb9d21
	isl_ast_node_free(tree);
Packit fb9d21
Packit fb9d21
	isl_ctx_free(ctx);
Packit fb9d21
	return r;
Packit fb9d21
}