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,
15 #include <isl/options.h>
16 #include <isl/schedule.h>
19 #include "ppcg_options.h"
24 struct isl_options
*isl
;
25 struct pet_options
*pet
;
26 struct ppcg_options
*ppcg
;
30 const char *ppcg_version(void);
31 static void print_version(void)
33 printf("%s", ppcg_version());
36 ISL_ARGS_START(struct options
, options_args
)
37 ISL_ARG_CHILD(struct options
, isl
, "isl", &isl_options_args
, "isl options")
38 ISL_ARG_CHILD(struct options
, pet
, "pet", &pet_options_args
, "pet options")
39 ISL_ARG_CHILD(struct options
, ppcg
, NULL
, &ppcg_options_args
, "ppcg options")
40 ISL_ARG_ARG(struct options
, input
, "input", NULL
)
41 ISL_ARG_VERSION(print_version
)
44 ISL_ARG_DEF(options
, struct options
, options_args
)
46 /* Is "stmt" a kill statement?
48 static int is_kill(struct pet_stmt
*stmt
)
50 if (stmt
->body
->type
!= pet_expr_unary
)
52 return stmt
->body
->op
== pet_op_kill
;
55 /* Is "stmt" not a kill statement?
57 static int is_not_kill(struct pet_stmt
*stmt
)
59 return !is_kill(stmt
);
62 /* Collect the iteration domains of the statements in "scop" that
65 static __isl_give isl_union_set
*collect_domains(struct pet_scop
*scop
,
66 int (*pred
)(struct pet_stmt
*stmt
))
70 isl_union_set
*domain
;
75 domain
= isl_union_set_empty(isl_set_get_space(scop
->context
));
77 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
78 struct pet_stmt
*stmt
= scop
->stmts
[i
];
82 domain_i
= isl_set_copy(scop
->stmts
[i
]->domain
);
83 domain
= isl_union_set_add_set(domain
, domain_i
);
89 /* Collect the iteration domains of the statements in "scop",
90 * skipping kill statements.
92 static __isl_give isl_union_set
*collect_non_kill_domains(struct pet_scop
*scop
)
94 return collect_domains(scop
, &is_not_kill
);
97 /* Collect all kill accesses in "scop".
99 static __isl_give isl_union_map
*collect_kills(struct pet_scop
*scop
)
102 isl_union_map
*kills
;
107 kills
= isl_union_map_empty(isl_set_get_space(scop
->context
));
109 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
110 struct pet_stmt
*stmt
= scop
->stmts
[i
];
115 kill_i
= isl_map_copy(stmt
->body
->args
[0]->acc
.access
);
116 kills
= isl_union_map_add_map(kills
, kill_i
);
122 /* Compute (flow) dependences and store the resulting flow dependences
123 * in scop->dep_flow and the reads with no corresponding writes in
126 static void compute_dependences(struct ppcg_scop
*scop
)
128 isl_union_map
*empty
;
133 empty
= isl_union_map_empty(isl_union_set_get_space(scop
->domain
));
134 isl_union_map_compute_flow(isl_union_map_copy(scop
->reads
),
135 isl_union_map_copy(scop
->writes
), empty
,
136 isl_union_map_copy(scop
->schedule
),
137 &scop
->dep_flow
, NULL
, &scop
->live_in
, NULL
);
140 /* Extract a ppcg_scop from a pet_scop.
142 * The constructed ppcg_scop refers to elements from the pet_scop
143 * so the pet_scop should not be freed before the ppcg_scop.
145 static struct ppcg_scop
*ppcg_scop_from_pet_scop(struct pet_scop
*scop
)
148 struct ppcg_scop
*ps
;
153 ctx
= isl_set_get_ctx(scop
->context
);
155 ps
= isl_calloc_type(ctx
, struct ppcg_scop
);
159 ps
->context
= isl_set_copy(scop
->context
);
160 ps
->domain
= collect_non_kill_domains(scop
);
161 ps
->reads
= pet_scop_collect_reads(scop
);
162 ps
->writes
= pet_scop_collect_writes(scop
);
163 ps
->kills
= collect_kills(scop
);
164 ps
->schedule
= pet_scop_collect_schedule(scop
);
165 ps
->n_array
= scop
->n_array
;
166 ps
->arrays
= scop
->arrays
;
167 ps
->n_stmt
= scop
->n_stmt
;
168 ps
->stmts
= scop
->stmts
;
170 compute_dependences(ps
);
175 static void ppcg_scop_free(struct ppcg_scop
*ps
)
180 isl_set_free(ps
->context
);
181 isl_union_set_free(ps
->domain
);
182 isl_union_map_free(ps
->reads
);
183 isl_union_map_free(ps
->live_in
);
184 isl_union_map_free(ps
->writes
);
185 isl_union_map_free(ps
->kills
);
186 isl_union_map_free(ps
->dep_flow
);
187 isl_union_map_free(ps
->schedule
);
192 int main(int argc
, char **argv
)
196 struct options
*options
;
197 struct pet_scop
*scop
;
198 struct ppcg_scop
*ps
;
200 options
= options_new_with_defaults();
203 ctx
= isl_ctx_alloc_with_options(&options_args
, options
);
204 isl_options_set_schedule_outer_zero_distance(ctx
, 1);
205 argc
= options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
207 scop
= pet_scop_extract_from_C_source(ctx
, options
->input
, NULL
);
208 scop
= pet_scop_align_params(scop
);
209 ps
= ppcg_scop_from_pet_scop(scop
);
211 if (options
->ppcg
->target
== PPCG_TARGET_CUDA
)
212 r
= generate_cuda(ctx
, ps
, options
->ppcg
, options
->input
);
214 r
= generate_cpu(ctx
, ps
, options
->ppcg
, options
->input
);