2 * Copyright 2012 INRIA Paris-Rocquencourt
4 * Use of this software is governed by the MIT license
6 * Written by Tobias Grosser, INRIA Paris-Rocquencourt,
7 * Domaine de Voluceau, Rocquenqourt, B.P. 105,
8 * 78153 Le Chesnay Cedex France
18 #include <isl/ast_build.h>
22 #include "ppcg_options.h"
26 /* Representation of a statement inside a generated AST.
28 * "stmt" refers to the original statement.
29 * "ref2expr" maps the reference identifier of each access in
30 * the statement to an AST expression that should be printed
31 * at the place of the access.
34 struct pet_stmt
*stmt
;
36 isl_id_to_ast_expr
*ref2expr
;
39 static void ppcg_stmt_free(void *user
)
41 struct ppcg_stmt
*stmt
= user
;
47 isl_id_to_ast_expr_free(stmt
->ref2expr
);
52 /* Derive the output file name from the input file name.
53 * 'input' is the entire path of the input file. The output
54 * is the file name plus the additional extension.
56 * We will basically replace everything after the last point
57 * with '.ppcg.c'. This means file.c becomes file.ppcg.c
59 static FILE *get_output_file(const char *input
, const char *output
)
63 const char ppcg_marker
[] = ".ppcg";
66 len
= ppcg_extract_base_name(name
, input
);
68 strcpy(name
+ len
, ppcg_marker
);
69 ext
= strrchr(input
, '.');
70 strcpy(name
+ len
+ sizeof(ppcg_marker
) - 1, ext
? ext
: ".c");
75 return fopen(output
, "w");
78 /* Data used to annotate for nodes in the ast.
80 struct ast_node_userinfo
{
81 /* The for node is an openmp parallel for node. */
85 /* Information used while building the ast.
87 struct ast_build_userinfo
{
88 /* The current ppcg scop. */
89 struct ppcg_scop
*scop
;
91 /* Are we currently in a parallel for loop? */
95 /* Check if the current scheduling dimension is parallel.
97 * We check for parallelism by verifying that the loop does not carry any
100 * Parallelism test: if the distance is zero in all outer dimensions, then it
101 * has to be zero in the current dimension as well.
102 * Implementation: first, translate dependences into time space, then force
103 * outer dimensions to be equal. If the distance is zero in the current
104 * dimension, then the loop is parallel.
105 * The distance is zero in the current dimension if it is a subset of a map
106 * with equal values for the current dimension.
108 static int ast_schedule_dim_is_parallel(__isl_keep isl_ast_build
*build
,
109 struct ppcg_scop
*scop
)
111 isl_union_map
*schedule_node
, *schedule
, *deps
;
112 isl_map
*schedule_deps
, *test
;
113 isl_space
*schedule_space
;
114 unsigned i
, dimension
, is_parallel
;
116 schedule
= isl_ast_build_get_schedule(build
);
117 schedule_space
= isl_ast_build_get_schedule_space(build
);
119 dimension
= isl_space_dim(schedule_space
, isl_dim_out
) - 1;
121 deps
= isl_union_map_copy(scop
->dep_flow
);
122 deps
= isl_union_map_union(deps
, isl_union_map_copy(scop
->dep_false
));
123 deps
= isl_union_map_apply_range(deps
, isl_union_map_copy(schedule
));
124 deps
= isl_union_map_apply_domain(deps
, schedule
);
126 if (isl_union_map_is_empty(deps
)) {
127 isl_union_map_free(deps
);
128 isl_space_free(schedule_space
);
132 schedule_deps
= isl_map_from_union_map(deps
);
134 for (i
= 0; i
< dimension
; i
++)
135 schedule_deps
= isl_map_equate(schedule_deps
, isl_dim_out
, i
,
138 test
= isl_map_universe(isl_map_get_space(schedule_deps
));
139 test
= isl_map_equate(test
, isl_dim_out
, dimension
, isl_dim_in
,
141 is_parallel
= isl_map_is_subset(schedule_deps
, test
);
143 isl_space_free(schedule_space
);
145 isl_map_free(schedule_deps
);
150 /* Mark a for node openmp parallel, if it is the outermost parallel for node.
152 static void mark_openmp_parallel(__isl_keep isl_ast_build
*build
,
153 struct ast_build_userinfo
*build_info
,
154 struct ast_node_userinfo
*node_info
)
156 if (build_info
->in_parallel_for
)
159 if (ast_schedule_dim_is_parallel(build
, build_info
->scop
)) {
160 build_info
->in_parallel_for
= 1;
161 node_info
->is_openmp
= 1;
165 /* Allocate an ast_node_info structure and initialize it with default values.
167 static struct ast_node_userinfo
*allocate_ast_node_userinfo()
169 struct ast_node_userinfo
*node_info
;
170 node_info
= (struct ast_node_userinfo
*)
171 malloc(sizeof(struct ast_node_userinfo
));
172 node_info
->is_openmp
= 0;
176 /* Free an ast_node_info structure.
178 static void free_ast_node_userinfo(void *ptr
)
180 struct ast_node_userinfo
*info
;
181 info
= (struct ast_node_userinfo
*) ptr
;
185 /* This method is executed before the construction of a for node. It creates
186 * an isl_id that is used to annotate the subsequently generated ast for nodes.
188 * In this function we also run the following analyses:
190 * - Detection of openmp parallel loops
192 static __isl_give isl_id
*ast_build_before_for(
193 __isl_keep isl_ast_build
*build
, void *user
)
196 struct ast_build_userinfo
*build_info
;
197 struct ast_node_userinfo
*node_info
;
199 build_info
= (struct ast_build_userinfo
*) user
;
200 node_info
= allocate_ast_node_userinfo();
201 id
= isl_id_alloc(isl_ast_build_get_ctx(build
), "", node_info
);
202 id
= isl_id_set_free_user(id
, free_ast_node_userinfo
);
204 mark_openmp_parallel(build
, build_info
, node_info
);
209 /* This method is executed after the construction of a for node.
211 * It performs the following actions:
213 * - Reset the 'in_parallel_for' flag, as soon as we leave a for node,
214 * that is marked as openmp parallel.
217 static __isl_give isl_ast_node
*ast_build_after_for(__isl_take isl_ast_node
*node
,
218 __isl_keep isl_ast_build
*build
, void *user
) {
220 struct ast_build_userinfo
*build_info
;
221 struct ast_node_userinfo
*info
;
223 id
= isl_ast_node_get_annotation(node
);
224 info
= isl_id_get_user(id
);
226 if (info
&& info
->is_openmp
) {
227 build_info
= (struct ast_build_userinfo
*) user
;
228 build_info
->in_parallel_for
= 0;
236 /* Find the element in scop->stmts that has the given "id".
238 static struct pet_stmt
*find_stmt(struct ppcg_scop
*scop
, __isl_keep isl_id
*id
)
242 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
243 struct pet_stmt
*stmt
= scop
->stmts
[i
];
246 id_i
= isl_set_get_tuple_id(stmt
->domain
);
253 isl_die(isl_id_get_ctx(id
), isl_error_internal
,
254 "statement not found", return NULL
);
257 /* Print a user statement in the generated AST.
258 * The ppcg_stmt has been attached to the node in at_each_domain.
260 static __isl_give isl_printer
*print_user(__isl_take isl_printer
*p
,
261 __isl_take isl_ast_print_options
*print_options
,
262 __isl_keep isl_ast_node
*node
, void *user
)
264 struct ppcg_stmt
*stmt
;
267 id
= isl_ast_node_get_annotation(node
);
268 stmt
= isl_id_get_user(id
);
271 p
= pet_stmt_print_body(stmt
->stmt
, p
, stmt
->ref2expr
);
273 isl_ast_print_options_free(print_options
);
279 /* Print a for loop node as an openmp parallel loop.
281 * To print an openmp parallel loop we print a normal for loop, but add
282 * "#pragma openmp parallel for" in front.
284 * Variables that are declared within the body of this for loop are
285 * automatically openmp 'private'. Iterators declared outside of the
286 * for loop are automatically openmp 'shared'. As ppcg declares all iterators
287 * at the position where they are assigned, there is no need to explicitly mark
288 * variables. Their automatically assigned type is already correct.
290 * This function only generates valid OpenMP code, if the ast was generated
291 * with the 'atomic-bounds' option enabled.
294 static __isl_give isl_printer
*print_for_with_openmp(
295 __isl_keep isl_ast_node
*node
, __isl_take isl_printer
*p
,
296 __isl_take isl_ast_print_options
*print_options
)
298 p
= isl_printer_start_line(p
);
299 p
= isl_printer_print_str(p
, "#pragma omp parallel for");
300 p
= isl_printer_end_line(p
);
302 p
= isl_ast_node_for_print(node
, p
, print_options
);
309 * Depending on how the node is annotated, we either print a normal
310 * for node or an openmp parallel for node.
312 static __isl_give isl_printer
*print_for(__isl_take isl_printer
*p
,
313 __isl_take isl_ast_print_options
*print_options
,
314 __isl_keep isl_ast_node
*node
, void *user
)
316 struct ppcg_print_info
*print_info
;
321 id
= isl_ast_node_get_annotation(node
);
324 struct ast_node_userinfo
*info
;
326 info
= (struct ast_node_userinfo
*) isl_id_get_user(id
);
327 if (info
&& info
->is_openmp
)
332 p
= print_for_with_openmp(node
, p
, print_options
);
334 p
= isl_ast_node_for_print(node
, p
, print_options
);
341 /* Index transformation callback for pet_stmt_build_ast_exprs.
343 * "index" expresses the array indices in terms of statement iterators
344 * "iterator_map" expresses the statement iterators in terms of
345 * AST loop iterators.
347 * The result expresses the array indices in terms of
348 * AST loop iterators.
350 static __isl_give isl_multi_pw_aff
*pullback_index(
351 __isl_take isl_multi_pw_aff
*index
, __isl_keep isl_id
*id
, void *user
)
353 isl_pw_multi_aff
*iterator_map
= user
;
355 iterator_map
= isl_pw_multi_aff_copy(iterator_map
);
356 return isl_multi_pw_aff_pullback_pw_multi_aff(index
, iterator_map
);
359 /* Transform the accesses in the statement associated to the domain
360 * called by "node" to refer to the AST loop iterators, construct
361 * corresponding AST expressions using "build",
362 * collect them in a ppcg_stmt and annotate the node with the ppcg_stmt.
364 static __isl_give isl_ast_node
*at_each_domain(__isl_take isl_ast_node
*node
,
365 __isl_keep isl_ast_build
*build
, void *user
)
367 struct ppcg_scop
*scop
= user
;
368 isl_ast_expr
*expr
, *arg
;
372 isl_pw_multi_aff
*iterator_map
;
373 struct ppcg_stmt
*stmt
;
375 ctx
= isl_ast_node_get_ctx(node
);
376 stmt
= isl_calloc_type(ctx
, struct ppcg_stmt
);
380 expr
= isl_ast_node_user_get_expr(node
);
381 arg
= isl_ast_expr_get_op_arg(expr
, 0);
382 isl_ast_expr_free(expr
);
383 id
= isl_ast_expr_get_id(arg
);
384 isl_ast_expr_free(arg
);
385 stmt
->stmt
= find_stmt(scop
, id
);
390 map
= isl_map_from_union_map(isl_ast_build_get_schedule(build
));
391 map
= isl_map_reverse(map
);
392 iterator_map
= isl_pw_multi_aff_from_map(map
);
393 stmt
->ref2expr
= pet_stmt_build_ast_exprs(stmt
->stmt
, build
,
394 &pullback_index
, iterator_map
, NULL
, NULL
);
395 isl_pw_multi_aff_free(iterator_map
);
397 id
= isl_id_alloc(isl_ast_node_get_ctx(node
), NULL
, stmt
);
398 id
= isl_id_set_free_user(id
, &ppcg_stmt_free
);
399 return isl_ast_node_set_annotation(node
, id
);
401 ppcg_stmt_free(stmt
);
402 return isl_ast_node_free(node
);
405 /* Code generate the scop 'scop' and print the corresponding C code to 'p'.
407 static __isl_give isl_printer
*print_scop(struct ppcg_scop
*scop
,
408 __isl_take isl_printer
*p
, struct ppcg_options
*options
)
410 isl_ctx
*ctx
= isl_printer_get_ctx(p
);
412 isl_union_set
*domain_set
;
413 isl_union_map
*schedule_map
;
414 isl_ast_build
*build
;
415 isl_ast_print_options
*print_options
;
417 struct ast_build_userinfo build_info
;
419 context
= isl_set_copy(scop
->context
);
420 domain_set
= isl_union_set_copy(scop
->domain
);
421 schedule_map
= isl_union_map_copy(scop
->schedule
);
422 schedule_map
= isl_union_map_intersect_domain(schedule_map
, domain_set
);
424 build
= isl_ast_build_from_context(context
);
425 build
= isl_ast_build_set_at_each_domain(build
, &at_each_domain
, scop
);
427 if (options
->openmp
) {
428 build_info
.scop
= scop
;
429 build_info
.in_parallel_for
= 0;
431 build
= isl_ast_build_set_before_each_for(build
,
432 &ast_build_before_for
,
434 build
= isl_ast_build_set_after_each_for(build
,
435 &ast_build_after_for
,
439 tree
= isl_ast_build_ast_from_schedule(build
, schedule_map
);
440 isl_ast_build_free(build
);
442 print_options
= isl_ast_print_options_alloc(ctx
);
443 print_options
= isl_ast_print_options_set_print_user(print_options
,
446 print_options
= isl_ast_print_options_set_print_for(print_options
,
449 p
= isl_ast_node_print_macros(tree
, p
);
450 p
= isl_ast_node_print(tree
, p
, print_options
);
452 isl_ast_node_free(tree
);
457 /* Does "scop" refer to any arrays that are declared, but not
458 * exposed to the code after the scop?
460 static int any_hidden_declarations(struct ppcg_scop
*scop
)
467 for (i
= 0; i
< scop
->n_array
; ++i
)
468 if (scop
->arrays
[i
]->declared
&& !scop
->arrays
[i
]->exposed
)
474 /* Generate CPU code for the scop "ps" and print the corresponding C code
475 * to "p", including variable declarations.
477 __isl_give isl_printer
*print_cpu(__isl_take isl_printer
*p
,
478 struct ppcg_scop
*ps
, struct ppcg_options
*options
)
482 p
= isl_printer_start_line(p
);
483 p
= isl_printer_print_str(p
, "/* ppcg generated CPU code */");
484 p
= isl_printer_end_line(p
);
486 p
= isl_printer_start_line(p
);
487 p
= isl_printer_end_line(p
);
489 p
= ppcg_print_exposed_declarations(p
, ps
);
490 hidden
= any_hidden_declarations(ps
);
492 p
= ppcg_start_block(p
);
493 p
= ppcg_print_hidden_declarations(p
, ps
);
495 p
= print_scop(ps
, p
, options
);
497 p
= ppcg_end_block(p
);
502 /* Wrapper around print_cpu for use as a ppcg_transform callback.
504 static __isl_give isl_printer
*print_cpu_wrap(__isl_take isl_printer
*p
,
505 struct ppcg_scop
*scop
, void *user
)
507 struct ppcg_options
*options
= user
;
509 return print_cpu(p
, scop
, options
);
512 /* Transform the code in the file called "input" by replacing
513 * all scops by corresponding CPU code and write the results to a file
516 int generate_cpu(isl_ctx
*ctx
, struct ppcg_options
*options
,
517 const char *input
, const char *output
)
522 output_file
= get_output_file(input
, output
);
524 r
= ppcg_transform(ctx
, input
, output_file
, options
,
525 &print_cpu_wrap
, options
);