ppcg_scop_from_pet_scop: skip kill statements
[ppcg.git] / ppcg.c
blob5791da785e82586fe9f12d705467b0ace6297481
1 /*
2 * Copyright 2011 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8 * 91893 Orsay, France
9 */
11 #include <assert.h>
12 #include <stdio.h>
13 #include <isl/ctx.h>
14 #include <isl/options.h>
15 #include <isl/schedule.h>
16 #include <pet.h>
17 #include "ppcg.h"
18 #include "ppcg_options.h"
19 #include "cuda.h"
20 #include "cpu.h"
22 struct options {
23 struct isl_options *isl;
24 struct pet_options *pet;
25 struct ppcg_options *ppcg;
26 char *input;
29 const char *ppcg_version(void);
30 static void print_version(void)
32 printf("%s", ppcg_version());
35 ISL_ARGS_START(struct options, options_args)
36 ISL_ARG_CHILD(struct options, isl, "isl", &isl_options_args, "isl options")
37 ISL_ARG_CHILD(struct options, pet, "pet", &pet_options_args, "pet options")
38 ISL_ARG_CHILD(struct options, ppcg, NULL, &ppcg_options_args, "ppcg options")
39 ISL_ARG_ARG(struct options, input, "input", NULL)
40 ISL_ARG_VERSION(print_version)
41 ISL_ARGS_END
43 ISL_ARG_DEF(options, struct options, options_args)
45 /* Is "stmt" a kill statement?
47 static int is_kill(struct pet_stmt *stmt)
49 if (stmt->body->type != pet_expr_unary)
50 return 0;
51 return stmt->body->op == pet_op_kill;
54 /* Is "stmt" not a kill statement?
56 static int is_not_kill(struct pet_stmt *stmt)
58 return !is_kill(stmt);
61 /* Collect the iteration domains of the statements in "scop" that
62 * satisfy "pred".
64 static __isl_give isl_union_set *collect_domains(struct pet_scop *scop,
65 int (*pred)(struct pet_stmt *stmt))
67 int i;
68 isl_set *domain_i;
69 isl_union_set *domain;
71 if (!scop)
72 return NULL;
74 domain = isl_union_set_empty(isl_set_get_space(scop->context));
76 for (i = 0; i < scop->n_stmt; ++i) {
77 struct pet_stmt *stmt = scop->stmts[i];
79 if (!pred(stmt))
80 continue;
81 domain_i = isl_set_copy(scop->stmts[i]->domain);
82 domain = isl_union_set_add_set(domain, domain_i);
85 return domain;
88 /* Collect the iteration domains of the statements in "scop",
89 * skipping kill statements.
91 static __isl_give isl_union_set *collect_non_kill_domains(struct pet_scop *scop)
93 return collect_domains(scop, &is_not_kill);
96 /* Extract a ppcg_scop from a pet_scop.
98 * The constructed ppcg_scop refers to elements from the pet_scop
99 * so the pet_scop should not be freed before the ppcg_scop.
101 static struct ppcg_scop *ppcg_scop_from_pet_scop(struct pet_scop *scop)
103 isl_ctx *ctx;
104 struct ppcg_scop *ps;
106 if (!scop)
107 return NULL;
109 ctx = isl_set_get_ctx(scop->context);
111 ps = isl_calloc_type(ctx, struct ppcg_scop);
112 if (!ps)
113 return NULL;
115 ps->context = isl_set_copy(scop->context);
116 ps->domain = collect_non_kill_domains(scop);
117 ps->reads = pet_scop_collect_reads(scop);
118 ps->writes = pet_scop_collect_writes(scop);
119 ps->schedule = pet_scop_collect_schedule(scop);
120 ps->n_array = scop->n_array;
121 ps->arrays = scop->arrays;
122 ps->n_stmt = scop->n_stmt;
123 ps->stmts = scop->stmts;
125 return ps;
128 static void ppcg_scop_free(struct ppcg_scop *ps)
130 if (!ps)
131 return;
133 isl_set_free(ps->context);
134 isl_union_set_free(ps->domain);
135 isl_union_map_free(ps->reads);
136 isl_union_map_free(ps->writes);
137 isl_union_map_free(ps->schedule);
139 free(ps);
142 int main(int argc, char **argv)
144 int r;
145 isl_ctx *ctx;
146 struct options *options;
147 struct pet_scop *scop;
148 struct ppcg_scop *ps;
150 options = options_new_with_defaults();
151 assert(options);
153 ctx = isl_ctx_alloc_with_options(&options_args, options);
154 isl_options_set_schedule_outer_zero_distance(ctx, 1);
155 argc = options_parse(options, argc, argv, ISL_ARG_ALL);
157 scop = pet_scop_extract_from_C_source(ctx, options->input, NULL);
158 scop = pet_scop_align_params(scop);
159 ps = ppcg_scop_from_pet_scop(scop);
161 if (options->ppcg->target == PPCG_TARGET_CUDA)
162 r = generate_cuda(ctx, ps, options->ppcg, options->input);
163 else
164 r = generate_cpu(ctx, ps, options->ppcg, options->input);
166 ppcg_scop_free(ps);
167 pet_scop_free(scop);
169 isl_ctx_free(ctx);
171 return r;