1 /*
2  * Copyright 2011      INRIA Saclay
3  * Copyright 2013      Ecole Normale Superieure
4  * Copyright 2015      Sven Verdoolaege
5  *
6  * Use of this software is governed by the MIT license
7  *
8  * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9  * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10  * 91893 Orsay, France
11  * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
12  */
13 
14 #include <assert.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <isl/ctx.h>
19 #include <isl/id.h>
20 #include <isl/val.h>
21 #include <isl/set.h>
22 #include <isl/union_set.h>
23 #include <isl/union_map.h>
24 #include <isl/aff.h>
25 #include <isl/flow.h>
26 #include <isl/options.h>
27 #include <isl/schedule.h>
28 #include <isl/ast.h>
29 #include <isl/id_to_ast_expr.h>
30 #include <isl/ast_build.h>
31 #include <isl/schedule.h>
32 #include <pet.h>
33 #include "ppcg.h"
34 #include "ppcg_options.h"
35 #include "cuda.h"
36 #include "opencl.h"
37 #include "cpu.h"
38 
39 struct options {
40 	struct pet_options *pet;
41 	struct ppcg_options *ppcg;
42 	char *input;
43 	char *output;
44 };
45 
46 const char *ppcg_version(void);
print_version(void)47 static void print_version(void)
48 {
49 	printf("%s", ppcg_version());
50 }
51 
ISL_ARGS_START(struct options,options_args)52 ISL_ARGS_START(struct options, options_args)
53 ISL_ARG_CHILD(struct options, pet, "pet", &pet_options_args, "pet options")
54 ISL_ARG_CHILD(struct options, ppcg, NULL, &ppcg_options_args, "ppcg options")
55 ISL_ARG_STR(struct options, output, 'o', NULL,
56 	"filename", NULL, "output filename (c and opencl targets)")
57 ISL_ARG_ARG(struct options, input, "input", NULL)
58 ISL_ARG_VERSION(print_version)
59 ISL_ARGS_END
60 
61 ISL_ARG_DEF(options, struct options, options_args)
62 
63 /* Return a pointer to the final path component of "filename" or
64  * to "filename" itself if it does not contain any components.
65  */
66 const char *ppcg_base_name(const char *filename)
67 {
68 	const char *base;
69 
70 	base = strrchr(filename, '/');
71 	if (base)
72 		return ++base;
73 	else
74 		return filename;
75 }
76 
77 /* Copy the base name of "input" to "name" and return its length.
78  * "name" is not NULL terminated.
79  *
80  * In particular, remove all leading directory components and
81  * the final extension, if any.
82  */
ppcg_extract_base_name(char * name,const char * input)83 int ppcg_extract_base_name(char *name, const char *input)
84 {
85 	const char *base;
86 	const char *ext;
87 	int len;
88 
89 	base = ppcg_base_name(input);
90 	ext = strrchr(base, '.');
91 	len = ext ? ext - base : strlen(base);
92 
93 	memcpy(name, base, len);
94 
95 	return len;
96 }
97 
98 /* Does "scop" refer to any arrays that are declared, but not
99  * exposed to the code after the scop?
100  */
ppcg_scop_any_hidden_declarations(struct ppcg_scop * scop)101 int ppcg_scop_any_hidden_declarations(struct ppcg_scop *scop)
102 {
103 	int i;
104 
105 	if (!scop)
106 		return 0;
107 
108     // This is a pet feature not available in Polly.
109     return 0;
110 
111 	for (i = 0; i < scop->pet->n_array; ++i)
112 		if (scop->pet->arrays[i]->declared &&
113 		    !scop->pet->arrays[i]->exposed)
114 			return 1;
115 
116 	return 0;
117 }
118 
119 /* Collect all variable names that are in use in "scop".
120  * In particular, collect all parameters in the context and
121  * all the array names.
122  * Store these names in an isl_id_to_ast_expr by mapping
123  * them to a dummy value (0).
124  */
collect_names(struct pet_scop * scop)125 static __isl_give isl_id_to_ast_expr *collect_names(struct pet_scop *scop)
126 {
127 	int i, n;
128 	isl_ctx *ctx;
129 	isl_ast_expr *zero;
130 	isl_id_to_ast_expr *names;
131 
132 	ctx = isl_set_get_ctx(scop->context);
133 
134 	n = isl_set_dim(scop->context, isl_dim_param);
135 
136 	names = isl_id_to_ast_expr_alloc(ctx, n + scop->n_array);
137 	zero = isl_ast_expr_from_val(isl_val_zero(ctx));
138 
139 	for (i = 0; i < n; ++i) {
140 		isl_id *id;
141 
142 		id = isl_set_get_dim_id(scop->context, isl_dim_param, i);
143 		names = isl_id_to_ast_expr_set(names,
144 						id, isl_ast_expr_copy(zero));
145 	}
146 
147 	for (i = 0; i < scop->n_array; ++i) {
148 		struct pet_array *array = scop->arrays[i];
149 		isl_id *id;
150 
151 		id = isl_set_get_tuple_id(array->extent);
152 		names = isl_id_to_ast_expr_set(names,
153 						id, isl_ast_expr_copy(zero));
154 	}
155 
156 	isl_ast_expr_free(zero);
157 
158 	return names;
159 }
160 
161 /* Return an isl_id called "prefix%d", with "%d" set to "i".
162  * If an isl_id with such a name already appears among the variable names
163  * of "scop", then adjust the name to "prefix%d_%d".
164  */
generate_name(struct ppcg_scop * scop,const char * prefix,int i)165 static __isl_give isl_id *generate_name(struct ppcg_scop *scop,
166 	const char *prefix, int i)
167 {
168 	int j;
169 	char name[16];
170 	isl_ctx *ctx;
171 	isl_id *id;
172 	int has_name;
173 
174 	ctx = isl_set_get_ctx(scop->context);
175 	snprintf(name, sizeof(name), "%s%d", prefix, i);
176 	id = isl_id_alloc(ctx, name, NULL);
177 
178 	j = 0;
179 	while ((has_name = isl_id_to_ast_expr_has(scop->names, id)) == 1) {
180 		isl_id_free(id);
181 		snprintf(name, sizeof(name), "%s%d_%d", prefix, i, j++);
182 		id = isl_id_alloc(ctx, name, NULL);
183 	}
184 
185 	return has_name < 0 ? isl_id_free(id) : id;
186 }
187 
188 /* Return a list of "n" isl_ids of the form "prefix%d".
189  * If an isl_id with such a name already appears among the variable names
190  * of "scop", then adjust the name to "prefix%d_%d".
191  */
ppcg_scop_generate_names(struct ppcg_scop * scop,int n,const char * prefix)192 __isl_give isl_id_list *ppcg_scop_generate_names(struct ppcg_scop *scop,
193 	int n, const char *prefix)
194 {
195 	int i;
196 	isl_ctx *ctx;
197 	isl_id_list *names;
198 
199 	ctx = isl_set_get_ctx(scop->context);
200 	names = isl_id_list_alloc(ctx, n);
201 	for (i = 0; i < n; ++i) {
202 		isl_id *id;
203 
204 		id = generate_name(scop, prefix, i);
205 		names = isl_id_list_add(names, id);
206 	}
207 
208 	return names;
209 }
210 
211 /* Is "stmt" not a kill statement?
212  */
is_not_kill(struct pet_stmt * stmt)213 static int is_not_kill(struct pet_stmt *stmt)
214 {
215 	return !pet_stmt_is_kill(stmt);
216 }
217 
218 /* Collect the iteration domains of the statements in "scop" that
219  * satisfy "pred".
220  */
collect_domains(struct pet_scop * scop,int (* pred)(struct pet_stmt * stmt))221 static __isl_give isl_union_set *collect_domains(struct pet_scop *scop,
222 	int (*pred)(struct pet_stmt *stmt))
223 {
224 	int i;
225 	isl_set *domain_i;
226 	isl_union_set *domain;
227 
228 	if (!scop)
229 		return NULL;
230 
231 	domain = isl_union_set_empty(isl_set_get_space(scop->context));
232 
233 	for (i = 0; i < scop->n_stmt; ++i) {
234 		struct pet_stmt *stmt = scop->stmts[i];
235 
236 		if (!pred(stmt))
237 			continue;
238 
239 		if (stmt->n_arg > 0)
240 			isl_die(isl_union_set_get_ctx(domain),
241 				isl_error_unsupported,
242 				"data dependent conditions not supported",
243 				return isl_union_set_free(domain));
244 
245 		domain_i = isl_set_copy(scop->stmts[i]->domain);
246 		domain = isl_union_set_add_set(domain, domain_i);
247 	}
248 
249 	return domain;
250 }
251 
252 /* Collect the iteration domains of the statements in "scop",
253  * skipping kill statements.
254  */
collect_non_kill_domains(struct pet_scop * scop)255 static __isl_give isl_union_set *collect_non_kill_domains(struct pet_scop *scop)
256 {
257 	return collect_domains(scop, &is_not_kill);
258 }
259 
260 /* This function is used as a callback to pet_expr_foreach_call_expr
261  * to detect if there is any call expression in the input expression.
262  * Assign the value 1 to the integer that "user" points to and
263  * abort the search since we have found what we were looking for.
264  */
set_has_call(__isl_keep pet_expr * expr,void * user)265 static int set_has_call(__isl_keep pet_expr *expr, void *user)
266 {
267 	int *has_call = user;
268 
269 	*has_call = 1;
270 
271 	return -1;
272 }
273 
274 /* Does "expr" contain any call expressions?
275  */
expr_has_call(__isl_keep pet_expr * expr)276 static int expr_has_call(__isl_keep pet_expr *expr)
277 {
278 	int has_call = 0;
279 
280 	if (pet_expr_foreach_call_expr(expr, &set_has_call, &has_call) < 0 &&
281 	    !has_call)
282 		return -1;
283 
284 	return has_call;
285 }
286 
287 /* This function is a callback for pet_tree_foreach_expr.
288  * If "expr" contains any call (sub)expressions, then set *has_call
289  * and abort the search.
290  */
check_call(__isl_keep pet_expr * expr,void * user)291 static int check_call(__isl_keep pet_expr *expr, void *user)
292 {
293 	int *has_call = user;
294 
295 	if (expr_has_call(expr))
296 		*has_call = 1;
297 
298 	return *has_call ? -1 : 0;
299 }
300 
301 /* Does "stmt" contain any call expressions?
302  */
has_call(struct pet_stmt * stmt)303 static int has_call(struct pet_stmt *stmt)
304 {
305 	int has_call = 0;
306 
307 	if (pet_tree_foreach_expr(stmt->body, &check_call, &has_call) < 0 &&
308 	    !has_call)
309 		return -1;
310 
311 	return has_call;
312 }
313 
314 /* Collect the iteration domains of the statements in "scop"
315  * that contain a call expression.
316  */
collect_call_domains(struct pet_scop * scop)317 static __isl_give isl_union_set *collect_call_domains(struct pet_scop *scop)
318 {
319 	return collect_domains(scop, &has_call);
320 }
321 
322 /* Given a union of "tagged" access relations of the form
323  *
324  *	[S_i[...] -> R_j[]] -> A_k[...]
325  *
326  * project out the "tags" (R_j[]).
327  * That is, return a union of relations of the form
328  *
329  *	S_i[...] -> A_k[...]
330  */
project_out_tags(__isl_take isl_union_map * umap)331 static __isl_give isl_union_map *project_out_tags(
332 	__isl_take isl_union_map *umap)
333 {
334 	return isl_union_map_domain_factor_domain(umap);
335 }
336 
337 /* Construct a function from tagged iteration domains to the corresponding
338  * untagged iteration domains with as range of the wrapped map in the domain
339  * the reference tags that appear in any of the reads, writes or kills.
340  * Store the result in ps->tagger.
341  *
342  * For example, if the statement with iteration space S[i,j]
343  * contains two array references R_1[] and R_2[], then ps->tagger will contain
344  *
345  *	{ [S[i,j] -> R_1[]] -> S[i,j]; [S[i,j] -> R_2[]] -> S[i,j] }
346  */
compute_tagger(struct ppcg_scop * ps)347 void compute_tagger(struct ppcg_scop *ps)
348 {
349 	isl_union_map *tagged;
350 	isl_union_pw_multi_aff *tagger;
351 
352 	tagged = isl_union_map_copy(ps->tagged_reads);
353 	tagged = isl_union_map_union(tagged,
354 				isl_union_map_copy(ps->tagged_may_writes));
355 	tagged = isl_union_map_union(tagged,
356 				isl_union_map_copy(ps->tagged_must_kills));
357 	tagged = isl_union_map_universe(tagged);
358 	tagged = isl_union_set_unwrap(isl_union_map_domain(tagged));
359 
360 	tagger = isl_union_map_domain_map_union_pw_multi_aff(tagged);
361 
362 	ps->tagger = tagger;
363 }
364 
365 /* Compute the live out accesses, i.e., the writes that are
366  * potentially not killed by any kills or any other writes, and
367  * store them in ps->live_out.
368  *
369  * We compute the "dependence" of any "kill" (an explicit kill
370  * or a must write) on any may write.
371  * The elements accessed by the may writes with a "depending" kill
372  * also accessing the element are definitely killed.
373  * The remaining may writes can potentially be live out.
374  *
375  * The result of the dependence analysis is
376  *
377  *	{ IW -> [IK -> A] }
378  *
379  * with IW the instance of the write statement, IK the instance of kill
380  * statement and A the element that was killed.
381  * The range factor range is
382  *
383  *	{ IW -> A }
384  *
385  * containing all such pairs for which there is a kill statement instance,
386  * i.e., all pairs that have been killed.
387  */
compute_live_out(struct ppcg_scop * ps)388 static void compute_live_out(struct ppcg_scop *ps)
389 {
390 	isl_schedule *schedule;
391 	isl_union_map *kills;
392 	isl_union_map *exposed;
393 	isl_union_map *covering;
394 	isl_union_access_info *access;
395 	isl_union_flow *flow;
396 
397 	schedule = isl_schedule_copy(ps->schedule);
398 	kills = isl_union_map_union(isl_union_map_copy(ps->must_writes),
399 				    isl_union_map_copy(ps->must_kills));
400 	access = isl_union_access_info_from_sink(kills);
401 	access = isl_union_access_info_set_may_source(access,
402 				    isl_union_map_copy(ps->may_writes));
403 	access = isl_union_access_info_set_schedule(access, schedule);
404 	flow = isl_union_access_info_compute_flow(access);
405 	covering = isl_union_flow_get_full_may_dependence(flow);
406 	isl_union_flow_free(flow);
407 
408 	covering = isl_union_map_range_factor_range(covering);
409 	exposed = isl_union_map_copy(ps->may_writes);
410 	exposed = isl_union_map_subtract(exposed, covering);
411 	ps->live_out = exposed;
412 }
413 
414 /* Compute the tagged flow dependences and the live_in accesses and store
415  * the results in ps->tagged_dep_flow and ps->live_in.
416  *
417  * We allow both the must writes and the must kills to serve as
418  * definite sources such that a subsequent read would not depend
419  * on any earlier write.  The resulting flow dependences with
420  * a must kill as source reflect possibly uninitialized reads.
421  * No dependences need to be introduced to protect such reads
422  * (other than those imposed by potential flows from may writes
423  * that follow the kill).  We therefore remove those flow dependences.
424  * This is also useful for the dead code elimination, which assumes
425  * the flow sources are non-kill instances.
426  */
compute_tagged_flow_dep_only(struct ppcg_scop * ps)427 static void compute_tagged_flow_dep_only(struct ppcg_scop *ps)
428 {
429 	isl_union_pw_multi_aff *tagger;
430 	isl_schedule *schedule;
431 	isl_union_map *live_in;
432 	isl_union_access_info *access;
433 	isl_union_flow *flow;
434 	isl_union_map *must_source;
435 	isl_union_map *kills;
436 	isl_union_map *tagged_flow;
437 
438 	tagger = isl_union_pw_multi_aff_copy(ps->tagger);
439 	schedule = isl_schedule_copy(ps->schedule);
440 	schedule = isl_schedule_pullback_union_pw_multi_aff(schedule, tagger);
441 	kills = isl_union_map_copy(ps->tagged_must_kills);
442 	must_source = isl_union_map_copy(ps->tagged_must_writes);
443 	must_source = isl_union_map_union(must_source,
444 				isl_union_map_copy(kills));
445 	access = isl_union_access_info_from_sink(
446 				isl_union_map_copy(ps->tagged_reads));
447 	access = isl_union_access_info_set_must_source(access, must_source);
448 	access = isl_union_access_info_set_may_source(access,
449 				isl_union_map_copy(ps->tagged_may_writes));
450 	access = isl_union_access_info_set_schedule(access, schedule);
451 	flow = isl_union_access_info_compute_flow(access);
452 	tagged_flow = isl_union_flow_get_may_dependence(flow);
453 	tagged_flow = isl_union_map_subtract_domain(tagged_flow,
454 				isl_union_map_domain(kills));
455 	ps->tagged_dep_flow = tagged_flow;
456 	live_in = isl_union_flow_get_may_no_source(flow);
457 	ps->live_in = project_out_tags(live_in);
458 	isl_union_flow_free(flow);
459 }
460 
461 /* Compute ps->dep_flow from ps->tagged_dep_flow
462  * by projecting out the reference tags.
463  */
derive_flow_dep_from_tagged_flow_dep(struct ppcg_scop * ps)464 static void derive_flow_dep_from_tagged_flow_dep(struct ppcg_scop *ps)
465 {
466 	ps->dep_flow = isl_union_map_copy(ps->tagged_dep_flow);
467 	ps->dep_flow = isl_union_map_factor_domain(ps->dep_flow);
468 }
469 
470 /* Compute the flow dependences and the live_in accesses and store
471  * the results in ps->dep_flow and ps->live_in.
472  * A copy of the flow dependences, tagged with the reference tags
473  * is stored in ps->tagged_dep_flow.
474  *
475  * We first compute ps->tagged_dep_flow, i.e., the tagged flow dependences
476  * and then project out the tags.
477  */
compute_tagged_flow_dep(struct ppcg_scop * ps)478 static void compute_tagged_flow_dep(struct ppcg_scop *ps)
479 {
480 	compute_tagged_flow_dep_only(ps);
481 	derive_flow_dep_from_tagged_flow_dep(ps);
482 }
483 
484 /* Compute the order dependences that prevent the potential live ranges
485  * from overlapping.
486  *
487  * In particular, construct a union of relations
488  *
489  *	[R[...] -> R_1[]] -> [W[...] -> R_2[]]
490  *
491  * where [R[...] -> R_1[]] is the range of one or more live ranges
492  * (i.e., a read) and [W[...] -> R_2[]] is the domain of one or more
493  * live ranges (i.e., a write).  Moreover, the read and the write
494  * access the same memory element and the read occurs before the write
495  * in the original schedule.
496  * The scheduler allows some of these dependences to be violated, provided
497  * the adjacent live ranges are all local (i.e., their domain and range
498  * are mapped to the same point by the current schedule band).
499  *
500  * Note that if a live range is not local, then we need to make
501  * sure it does not overlap with _any_ other live range, and not
502  * just with the "previous" and/or the "next" live range.
503  * We therefore add order dependences between reads and
504  * _any_ later potential write.
505  *
506  * We also need to be careful about writes without a corresponding read.
507  * They are already prevented from moving past non-local preceding
508  * intervals, but we also need to prevent them from moving past non-local
509  * following intervals.  We therefore also add order dependences from
510  * potential writes that do not appear in any intervals
511  * to all later potential writes.
512  * Note that dead code elimination should have removed most of these
513  * dead writes, but the dead code elimination may not remove all dead writes,
514  * so we need to consider them to be safe.
515  *
516  * The order dependences are computed by computing the "dataflow"
517  * from the above unmatched writes and the reads to the may writes.
518  * The unmatched writes and the reads are treated as may sources
519  * such that they would not kill order dependences from earlier
520  * such writes and reads.
521  */
compute_order_dependences(struct ppcg_scop * ps)522 static void compute_order_dependences(struct ppcg_scop *ps)
523 {
524 	isl_union_map *reads;
525 	isl_union_map *shared_access;
526 	isl_union_set *matched;
527 	isl_union_map *unmatched;
528 	isl_union_pw_multi_aff *tagger;
529 	isl_schedule *schedule;
530 	isl_union_access_info *access;
531 	isl_union_flow *flow;
532 
533 	tagger = isl_union_pw_multi_aff_copy(ps->tagger);
534 	schedule = isl_schedule_copy(ps->schedule);
535 	schedule = isl_schedule_pullback_union_pw_multi_aff(schedule, tagger);
536 	reads = isl_union_map_copy(ps->tagged_reads);
537 	matched = isl_union_map_domain(isl_union_map_copy(ps->tagged_dep_flow));
538 	unmatched = isl_union_map_copy(ps->tagged_may_writes);
539 	unmatched = isl_union_map_subtract_domain(unmatched, matched);
540 	reads = isl_union_map_union(reads, unmatched);
541 	access = isl_union_access_info_from_sink(
542 				isl_union_map_copy(ps->tagged_may_writes));
543 	access = isl_union_access_info_set_may_source(access, reads);
544 	access = isl_union_access_info_set_schedule(access, schedule);
545 	flow = isl_union_access_info_compute_flow(access);
546 	shared_access = isl_union_flow_get_may_dependence(flow);
547 	isl_union_flow_free(flow);
548 
549 	ps->tagged_dep_order = isl_union_map_copy(shared_access);
550 	ps->dep_order = isl_union_map_factor_domain(shared_access);
551 }
552 
553 /* Compute those validity dependences of the program represented by "scop"
554  * that should be unconditionally enforced even when live-range reordering
555  * is used.
556  *
557  * In particular, compute the external false dependences
558  * as well as order dependences between sources with the same sink.
559  * The anti-dependences are already taken care of by the order dependences.
560  * The external false dependences are only used to ensure that live-in and
561  * live-out data is not overwritten by any writes inside the scop.
562  * The independences are removed from the external false dependences,
563  * but not from the order dependences between sources with the same sink.
564  *
565  * In particular, the reads from live-in data need to precede any
566  * later write to the same memory element.
567  * As to live-out data, the last writes need to remain the last writes.
568  * That is, any earlier write in the original schedule needs to precede
569  * the last write to the same memory element in the computed schedule.
570  * The possible last writes have been computed by compute_live_out.
571  * They may include kills, but if the last access is a kill,
572  * then the corresponding dependences will effectively be ignored
573  * since we do not schedule any kill statements.
574  *
575  * Note that the set of live-in and live-out accesses may be
576  * an overapproximation.  There may therefore be potential writes
577  * before a live-in access and after a live-out access.
578  *
579  * In the presence of may-writes, there may be multiple live-ranges
580  * with the same sink, accessing the same memory element.
581  * The sources of these live-ranges need to be executed
582  * in the same relative order as in the original program
583  * since we do not know which of the may-writes will actually
584  * perform a write.  Consider all sources that share a sink and
585  * that may write to the same memory element and compute
586  * the order dependences among them.
587  */
compute_forced_dependences(struct ppcg_scop * ps)588 static void compute_forced_dependences(struct ppcg_scop *ps)
589 {
590 	isl_union_map *shared_access;
591 	isl_union_map *exposed;
592 	isl_union_map *live_in;
593 	isl_union_map *sink_access;
594 	isl_union_map *shared_sink;
595 	isl_union_access_info *access;
596 	isl_union_flow *flow;
597 	isl_schedule *schedule;
598 
599 	exposed = isl_union_map_copy(ps->live_out);
600 	schedule = isl_schedule_copy(ps->schedule);
601 	access = isl_union_access_info_from_sink(exposed);
602 	access = isl_union_access_info_set_may_source(access,
603 				isl_union_map_copy(ps->may_writes));
604 	access = isl_union_access_info_set_schedule(access, schedule);
605 	flow = isl_union_access_info_compute_flow(access);
606 	shared_access = isl_union_flow_get_may_dependence(flow);
607 	isl_union_flow_free(flow);
608 	ps->dep_forced = shared_access;
609 
610 	schedule = isl_schedule_copy(ps->schedule);
611 	access = isl_union_access_info_from_sink(
612 				isl_union_map_copy(ps->may_writes));
613 	access = isl_union_access_info_set_may_source(access,
614 				isl_union_map_copy(ps->live_in));
615 	access = isl_union_access_info_set_schedule(access, schedule);
616 	flow = isl_union_access_info_compute_flow(access);
617 	live_in = isl_union_flow_get_may_dependence(flow);
618 	isl_union_flow_free(flow);
619 
620 	ps->dep_forced = isl_union_map_union(ps->dep_forced, live_in);
621 	ps->dep_forced = isl_union_map_subtract(ps->dep_forced,
622 				isl_union_map_copy(ps->independence));
623 
624 	schedule = isl_schedule_copy(ps->schedule);
625 	sink_access = isl_union_map_copy(ps->tagged_dep_flow);
626 	sink_access = isl_union_map_range_product(sink_access,
627 				isl_union_map_copy(ps->tagged_may_writes));
628 	sink_access = isl_union_map_domain_factor_domain(sink_access);
629 	access = isl_union_access_info_from_sink(
630 				isl_union_map_copy(sink_access));
631 	access = isl_union_access_info_set_may_source(access, sink_access);
632 	access = isl_union_access_info_set_schedule(access, schedule);
633 	flow = isl_union_access_info_compute_flow(access);
634 	shared_sink = isl_union_flow_get_may_dependence(flow);
635 	isl_union_flow_free(flow);
636 	ps->dep_forced = isl_union_map_union(ps->dep_forced, shared_sink);
637 }
638 
639 /* Remove independence from the tagged flow dependences.
640  * Since the user has guaranteed that source and sink of an independence
641  * can be executed in any order, there cannot be a flow dependence
642  * between them, so they can be removed from the set of flow dependences.
643  * However, if the source of such a flow dependence is a must write,
644  * then it may have killed other potential sources, which would have
645  * to be recovered if we were to remove those flow dependences.
646  * We therefore keep the flow dependences that originate in a must write,
647  * even if it corresponds to a known independence.
648  */
remove_independences_from_tagged_flow(struct ppcg_scop * ps)649 static void remove_independences_from_tagged_flow(struct ppcg_scop *ps)
650 {
651 	isl_union_map *tf;
652 	isl_union_set *indep;
653 	isl_union_set *mw;
654 
655 	tf = isl_union_map_copy(ps->tagged_dep_flow);
656 	tf = isl_union_map_zip(tf);
657 	indep = isl_union_map_wrap(isl_union_map_copy(ps->independence));
658 	tf = isl_union_map_intersect_domain(tf, indep);
659 	tf = isl_union_map_zip(tf);
660 	mw = isl_union_map_domain(isl_union_map_copy(ps->tagged_must_writes));
661 	tf = isl_union_map_subtract_domain(tf, mw);
662 	ps->tagged_dep_flow = isl_union_map_subtract(ps->tagged_dep_flow, tf);
663 }
664 
665 /* Compute the dependences of the program represented by "scop"
666  * in case live range reordering is allowed.
667  *
668  * We compute the actual live ranges and the corresponding order
669  * false dependences.
670  *
671  * The independences are removed from the flow dependences
672  * (provided the source is not a must-write) as well as
673  * from the external false dependences (by compute_forced_dependences).
674  */
compute_live_range_reordering_dependences(struct ppcg_scop * ps)675 static void compute_live_range_reordering_dependences(struct ppcg_scop *ps)
676 {
677 	compute_tagged_flow_dep_only(ps);
678 	remove_independences_from_tagged_flow(ps);
679 	derive_flow_dep_from_tagged_flow_dep(ps);
680 	compute_order_dependences(ps);
681 	compute_forced_dependences(ps);
682 }
683 
684 /* Compute the potential flow dependences and the potential live in
685  * accesses.
686  */
compute_flow_dep(struct ppcg_scop * ps)687 static void compute_flow_dep(struct ppcg_scop *ps)
688 {
689 	isl_union_access_info *access;
690 	isl_union_flow *flow;
691 
692 	access = isl_union_access_info_from_sink(isl_union_map_copy(ps->reads));
693 	access = isl_union_access_info_set_must_source(access,
694 				isl_union_map_copy(ps->must_writes));
695 	access = isl_union_access_info_set_may_source(access,
696 				isl_union_map_copy(ps->may_writes));
697 	access = isl_union_access_info_set_schedule(access,
698 				isl_schedule_copy(ps->schedule));
699 	flow = isl_union_access_info_compute_flow(access);
700 
701 	ps->dep_flow = isl_union_flow_get_may_dependence(flow);
702 	ps->live_in = isl_union_flow_get_may_no_source(flow);
703 	isl_union_flow_free(flow);
704 }
705 
706 /* Compute the dependences of the program represented by "scop".
707  * Store the computed potential flow dependences
708  * in scop->dep_flow and the reads with potentially no corresponding writes in
709  * scop->live_in.
710  * Store the potential live out accesses in scop->live_out.
711  * Store the potential false (anti and output) dependences in scop->dep_false.
712  *
713  * If live range reordering is allowed, then we compute a separate
714  * set of order dependences and a set of external false dependences
715  * in compute_live_range_reordering_dependences.
716  */
compute_dependences(struct ppcg_scop * scop)717 void compute_dependences(struct ppcg_scop *scop)
718 {
719 	isl_union_map *may_source;
720 	isl_union_access_info *access;
721 	isl_union_flow *flow;
722 
723 	if (!scop)
724 		return;
725 
726 	compute_live_out(scop);
727 
728 	if (scop->options->live_range_reordering)
729 		compute_live_range_reordering_dependences(scop);
730 	else if (scop->options->target != PPCG_TARGET_C)
731 		compute_tagged_flow_dep(scop);
732 	else
733 		compute_flow_dep(scop);
734 
735 	may_source = isl_union_map_union(isl_union_map_copy(scop->may_writes),
736 					isl_union_map_copy(scop->reads));
737 	access = isl_union_access_info_from_sink(
738 				isl_union_map_copy(scop->may_writes));
739 	access = isl_union_access_info_set_must_source(access,
740 				isl_union_map_copy(scop->must_writes));
741 	access = isl_union_access_info_set_may_source(access, may_source);
742 	access = isl_union_access_info_set_schedule(access,
743 				isl_schedule_copy(scop->schedule));
744 	flow = isl_union_access_info_compute_flow(access);
745 
746 	scop->dep_false = isl_union_flow_get_may_dependence(flow);
747 	scop->dep_false = isl_union_map_coalesce(scop->dep_false);
748 	isl_union_flow_free(flow);
749 }
750 
751 /* Eliminate dead code from ps->domain.
752  *
753  * In particular, intersect both ps->domain and the domain of
754  * ps->schedule with the (parts of) iteration
755  * domains that are needed to produce the output or for statement
756  * iterations that call functions.
757  * Also intersect the range of the dataflow dependences with
758  * this domain such that the removed instances will no longer
759  * be considered as targets of dataflow.
760  *
761  * We start with the iteration domains that call functions
762  * and the set of iterations that last write to an array
763  * (except those that are later killed).
764  *
765  * Then we add those statement iterations that produce
766  * something needed by the "live" statements iterations.
767  * We keep doing this until no more statement iterations can be added.
768  * To ensure that the procedure terminates, we compute the affine
769  * hull of the live iterations (bounded to the original iteration
770  * domains) each time we have added extra iterations.
771  */
eliminate_dead_code(struct ppcg_scop * ps)772 void eliminate_dead_code(struct ppcg_scop *ps)
773 {
774 	isl_union_set *live;
775 	isl_union_map *dep;
776 	isl_union_pw_multi_aff *tagger;
777 
778 	live = isl_union_map_domain(isl_union_map_copy(ps->live_out));
779 	if (!isl_union_set_is_empty(ps->call)) {
780 		live = isl_union_set_union(live, isl_union_set_copy(ps->call));
781 		live = isl_union_set_coalesce(live);
782 	}
783 
784 	dep = isl_union_map_copy(ps->dep_flow);
785 	dep = isl_union_map_reverse(dep);
786 
787 	for (;;) {
788 		isl_union_set *extra;
789 
790 		extra = isl_union_set_apply(isl_union_set_copy(live),
791 					    isl_union_map_copy(dep));
792 		if (isl_union_set_is_subset(extra, live)) {
793 			isl_union_set_free(extra);
794 			break;
795 		}
796 
797 		live = isl_union_set_union(live, extra);
798 		live = isl_union_set_affine_hull(live);
799 		live = isl_union_set_intersect(live,
800 					    isl_union_set_copy(ps->domain));
801 	}
802 
803 	isl_union_map_free(dep);
804 
805 	ps->domain = isl_union_set_intersect(ps->domain,
806 						isl_union_set_copy(live));
807 	ps->schedule = isl_schedule_intersect_domain(ps->schedule,
808 						isl_union_set_copy(live));
809 	ps->dep_flow = isl_union_map_intersect_range(ps->dep_flow,
810 						isl_union_set_copy(live));
811 	tagger = isl_union_pw_multi_aff_copy(ps->tagger);
812 	live = isl_union_set_preimage_union_pw_multi_aff(live, tagger);
813 	ps->tagged_dep_flow = isl_union_map_intersect_range(ps->tagged_dep_flow,
814 						live);
815 }
816 
817 /* Intersect "set" with the set described by "str", taking the NULL
818  * string to represent the universal set.
819  */
set_intersect_str(__isl_take isl_set * set,const char * str)820 static __isl_give isl_set *set_intersect_str(__isl_take isl_set *set,
821 	const char *str)
822 {
823 	isl_ctx *ctx;
824 	isl_set *set2;
825 
826 	if (!str)
827 		return set;
828 
829 	ctx = isl_set_get_ctx(set);
830 	set2 = isl_set_read_from_str(ctx, str);
831 	set = isl_set_intersect(set, set2);
832 
833 	return set;
834 }
835 
ppcg_scop_free(struct ppcg_scop * ps)836 void *ppcg_scop_free(struct ppcg_scop *ps)
837 {
838 	if (!ps)
839 		return NULL;
840 
841 	isl_set_free(ps->context);
842 	isl_union_set_free(ps->domain);
843 	isl_union_set_free(ps->call);
844 	isl_union_map_free(ps->tagged_reads);
845 	isl_union_map_free(ps->reads);
846 	isl_union_map_free(ps->live_in);
847 	isl_union_map_free(ps->tagged_may_writes);
848 	isl_union_map_free(ps->tagged_must_writes);
849 	isl_union_map_free(ps->may_writes);
850 	isl_union_map_free(ps->must_writes);
851 	isl_union_map_free(ps->live_out);
852 	isl_union_map_free(ps->tagged_must_kills);
853 	isl_union_map_free(ps->must_kills);
854 	isl_union_map_free(ps->tagged_dep_flow);
855 	isl_union_map_free(ps->dep_flow);
856 	isl_union_map_free(ps->dep_false);
857 	isl_union_map_free(ps->dep_forced);
858 	isl_union_map_free(ps->tagged_dep_order);
859 	isl_union_map_free(ps->dep_order);
860 	isl_schedule_free(ps->schedule);
861 	isl_union_pw_multi_aff_free(ps->tagger);
862 	isl_union_map_free(ps->independence);
863 	isl_id_to_ast_expr_free(ps->names);
864 
865 	free(ps);
866 
867 	return NULL;
868 }
869 
870 /* Extract a ppcg_scop from a pet_scop.
871  *
872  * The constructed ppcg_scop refers to elements from the pet_scop
873  * so the pet_scop should not be freed before the ppcg_scop.
874  */
ppcg_scop_from_pet_scop(struct pet_scop * scop,struct ppcg_options * options)875 static struct ppcg_scop *ppcg_scop_from_pet_scop(struct pet_scop *scop,
876 	struct ppcg_options *options)
877 {
878 	int i;
879 	isl_ctx *ctx;
880 	struct ppcg_scop *ps;
881 
882 	if (!scop)
883 		return NULL;
884 
885 	ctx = isl_set_get_ctx(scop->context);
886 
887 	ps = isl_calloc_type(ctx, struct ppcg_scop);
888 	if (!ps)
889 		return NULL;
890 
891 	ps->names = collect_names(scop);
892 	ps->options = options;
893 	ps->start = pet_loc_get_start(scop->loc);
894 	ps->end = pet_loc_get_end(scop->loc);
895 	ps->context = isl_set_copy(scop->context);
896 	ps->context = set_intersect_str(ps->context, options->ctx);
897 	if (options->non_negative_parameters) {
898 		isl_space *space = isl_set_get_space(ps->context);
899 		isl_set *nn = isl_set_nat_universe(space);
900 		ps->context = isl_set_intersect(ps->context, nn);
901 	}
902 	ps->domain = collect_non_kill_domains(scop);
903 	ps->call = collect_call_domains(scop);
904 	ps->tagged_reads = pet_scop_get_tagged_may_reads(scop);
905 	ps->reads = pet_scop_get_may_reads(scop);
906 	ps->tagged_may_writes = pet_scop_get_tagged_may_writes(scop);
907 	ps->may_writes = pet_scop_get_may_writes(scop);
908 	ps->tagged_must_writes = pet_scop_get_tagged_must_writes(scop);
909 	ps->must_writes = pet_scop_get_must_writes(scop);
910 	ps->tagged_must_kills = pet_scop_get_tagged_must_kills(scop);
911 	ps->must_kills = pet_scop_get_must_kills(scop);
912 	ps->schedule = isl_schedule_copy(scop->schedule);
913 	ps->pet = scop;
914 	ps->independence = isl_union_map_empty(isl_set_get_space(ps->context));
915 	for (i = 0; i < scop->n_independence; ++i)
916 		ps->independence = isl_union_map_union(ps->independence,
917 			isl_union_map_copy(scop->independences[i]->filter));
918 
919 	compute_tagger(ps);
920 	compute_dependences(ps);
921 	eliminate_dead_code(ps);
922 
923 	if (!ps->context || !ps->domain || !ps->call || !ps->reads ||
924 	    !ps->may_writes || !ps->must_writes || !ps->tagged_must_kills ||
925 	    !ps->must_kills || !ps->schedule || !ps->independence || !ps->names)
926 		return ppcg_scop_free(ps);
927 
928 	return ps;
929 }
930 
931 /* Internal data structure for ppcg_transform.
932  */
933 struct ppcg_transform_data {
934 	struct ppcg_options *options;
935 	__isl_give isl_printer *(*transform)(__isl_take isl_printer *p,
936 		struct ppcg_scop *scop, void *user);
937 	void *user;
938 };
939 
940 /* Should we print the original code?
941  * That is, does "scop" involve any data dependent conditions or
942  * nested expressions that cannot be handled by pet_stmt_build_ast_exprs?
943  */
print_original(struct pet_scop * scop,struct ppcg_options * options)944 static int print_original(struct pet_scop *scop, struct ppcg_options *options)
945 {
946 	if (!pet_scop_can_build_ast_exprs(scop)) {
947 		if (options->debug->verbose)
948 			fprintf(stdout, "Printing original code because "
949 				"some index expressions cannot currently "
950 				"be printed\n");
951 		return 1;
952 	}
953 
954 	if (pet_scop_has_data_dependent_conditions(scop)) {
955 		if (options->debug->verbose)
956 			fprintf(stdout, "Printing original code because "
957 				"input involves data dependent conditions\n");
958 		return 1;
959 	}
960 
961 	return 0;
962 }
963 
964 /* Callback for pet_transform_C_source that transforms
965  * the given pet_scop to a ppcg_scop before calling the
966  * ppcg_transform callback.
967  *
968  * If "scop" contains any data dependent conditions or if we may
969  * not be able to print the transformed program, then just print
970  * the original code.
971  */
transform(__isl_take isl_printer * p,struct pet_scop * scop,void * user)972 static __isl_give isl_printer *transform(__isl_take isl_printer *p,
973 	struct pet_scop *scop, void *user)
974 {
975 	struct ppcg_transform_data *data = user;
976 	struct ppcg_scop *ps;
977 
978 	if (print_original(scop, data->options)) {
979 		p = pet_scop_print_original(scop, p);
980 		pet_scop_free(scop);
981 		return p;
982 	}
983 
984 	scop = pet_scop_align_params(scop);
985 	ps = ppcg_scop_from_pet_scop(scop, data->options);
986 
987 	p = data->transform(p, ps, data->user);
988 
989 	ppcg_scop_free(ps);
990 	pet_scop_free(scop);
991 
992 	return p;
993 }
994 
995 /* Transform the C source file "input" by rewriting each scop
996  * through a call to "transform".
997  * The transformed C code is written to "out".
998  *
999  * This is a wrapper around pet_transform_C_source that transforms
1000  * the pet_scop to a ppcg_scop before calling "fn".
1001  */
ppcg_transform(isl_ctx * ctx,const char * input,FILE * out,struct ppcg_options * options,__isl_give isl_printer * (* fn)(__isl_take isl_printer * p,struct ppcg_scop * scop,void * user),void * user)1002 int ppcg_transform(isl_ctx *ctx, const char *input, FILE *out,
1003 	struct ppcg_options *options,
1004 	__isl_give isl_printer *(*fn)(__isl_take isl_printer *p,
1005 		struct ppcg_scop *scop, void *user), void *user)
1006 {
1007 	struct ppcg_transform_data data = { options, fn, user };
1008 	return pet_transform_C_source(ctx, input, out, &transform, &data);
1009 }
1010 
1011 /* Check consistency of options.
1012  *
1013  * Return -1 on error.
1014  */
check_options(isl_ctx * ctx)1015 static int check_options(isl_ctx *ctx)
1016 {
1017 	struct options *options;
1018 
1019 	options = isl_ctx_peek_options(ctx, &options_args);
1020 	if (!options)
1021 		isl_die(ctx, isl_error_internal,
1022 			"unable to find options", return -1);
1023 
1024 	if (options->ppcg->openmp &&
1025 	    !isl_options_get_ast_build_atomic_upper_bound(ctx))
1026 		isl_die(ctx, isl_error_invalid,
1027 			"OpenMP requires atomic bounds", return -1);
1028 
1029 	return 0;
1030 }
1031 
1032 #if 0
1033 int main(int argc, char **argv)
1034 {
1035 	int r;
1036 	isl_ctx *ctx;
1037 	struct options *options;
1038 
1039 	options = options_new_with_defaults();
1040 	assert(options);
1041 
1042 	ctx = isl_ctx_alloc_with_options(&options_args, options);
1043 	ppcg_options_set_target_defaults(options->ppcg);
1044 	isl_options_set_ast_build_detect_min_max(ctx, 1);
1045 	isl_options_set_ast_print_macro_once(ctx, 1);
1046 	isl_options_set_schedule_whole_component(ctx, 0);
1047 	isl_options_set_schedule_maximize_band_depth(ctx, 1);
1048 	isl_options_set_schedule_maximize_coincidence(ctx, 1);
1049 	pet_options_set_encapsulate_dynamic_control(ctx, 1);
1050 	argc = options_parse(options, argc, argv, ISL_ARG_ALL);
1051 
1052 	if (check_options(ctx) < 0)
1053 		r = EXIT_FAILURE;
1054 	else if (options->ppcg->target == PPCG_TARGET_CUDA)
1055 		r = generate_cuda(ctx, options->ppcg, options->input);
1056 	else if (options->ppcg->target == PPCG_TARGET_OPENCL)
1057 		r = generate_opencl(ctx, options->ppcg, options->input,
1058 				options->output);
1059 	else
1060 		r = generate_cpu(ctx, options->ppcg, options->input,
1061 				options->output);
1062 
1063 	isl_ctx_free(ctx);
1064 
1065 	return r;
1066 }
1067 #endif
1068