use pet_transform_C_source instead of pet_scop_extract_from_C_source
[ppcg.git] / gpu_print.c
blob4854db02c9ee0592cb76a1350fa9c0008d8141e6
1 /*
2 * Copyright 2012 Ecole Normale Superieure
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
8 */
10 #include <isl/aff.h>
12 #include "gpu_print.h"
13 #include "schedule.h"
15 static int print_macro(enum isl_ast_op_type type, void *user)
17 isl_printer **p = user;
19 if (type == isl_ast_op_fdiv_q)
20 return 0;
22 *p = isl_ast_op_type_print_macro(type, *p);
24 return 0;
27 /* Print the required macros for "node", including one for floord.
28 * We always print a macro for floord as it may also appear in the statements.
30 __isl_give isl_printer *gpu_print_macros(__isl_take isl_printer *p,
31 __isl_keep isl_ast_node *node)
33 p = isl_ast_op_type_print_macro(isl_ast_op_fdiv_q, p);
34 if (isl_ast_node_foreach_ast_op_type(node, &print_macro, &p) < 0)
35 return isl_printer_free(p);
36 return p;
39 /* Print an expression for the size of "array" in bytes.
41 __isl_give isl_printer *gpu_array_info_print_size(__isl_take isl_printer *prn,
42 struct gpu_array_info *array)
44 int i;
46 for (i = 0; i < array->n_index; ++i) {
47 prn = isl_printer_print_str(prn, "(");
48 prn = isl_printer_print_pw_aff(prn, array->bound[i]);
49 prn = isl_printer_print_str(prn, ") * ");
51 prn = isl_printer_print_str(prn, "sizeof(");
52 prn = isl_printer_print_str(prn, array->type);
53 prn = isl_printer_print_str(prn, ")");
55 return prn;
58 /* Print an access to the element in the private/shared memory copy
59 * described by "stmt". The index of the copy is recorded in
60 * stmt->local_index as a "call" to the array.
62 static __isl_give isl_printer *stmt_print_local_index(__isl_take isl_printer *p,
63 struct ppcg_kernel_stmt *stmt)
65 int i;
66 isl_ast_expr *expr;
67 struct gpu_array_info *array = stmt->u.c.array;
69 expr = isl_ast_expr_get_op_arg(stmt->u.c.local_index, 0);
70 p = isl_printer_print_ast_expr(p, expr);
71 isl_ast_expr_free(expr);
73 for (i = 0; i < array->n_index; ++i) {
74 expr = isl_ast_expr_get_op_arg(stmt->u.c.local_index, 1 + i);
76 p = isl_printer_print_str(p, "[");
77 p = isl_printer_print_ast_expr(p, expr);
78 p = isl_printer_print_str(p, "]");
80 isl_ast_expr_free(expr);
83 return p;
86 /* Print an access to the element in the global memory copy
87 * described by "stmt". The index of the copy is recorded in
88 * stmt->index as a "call" to the array.
90 * The copy in global memory has been linearized, so we need to take
91 * the array size into account.
93 static __isl_give isl_printer *stmt_print_global_index(
94 __isl_take isl_printer *p, struct ppcg_kernel_stmt *stmt)
96 int i;
97 struct gpu_array_info *array = stmt->u.c.array;
98 isl_pw_aff_list *bound = stmt->u.c.local_array->bound;
100 if (gpu_array_is_scalar(array)) {
101 if (!array->read_only)
102 p = isl_printer_print_str(p, "*");
103 p = isl_printer_print_str(p, array->name);
104 return p;
107 p = isl_printer_print_str(p, array->name);
108 p = isl_printer_print_str(p, "[");
109 for (i = 0; i + 1 < array->n_index; ++i)
110 p = isl_printer_print_str(p, "(");
111 for (i = 0; i < array->n_index; ++i) {
112 isl_ast_expr *expr;
113 expr = isl_ast_expr_get_op_arg(stmt->u.c.index, 1 + i);
114 if (i) {
115 isl_pw_aff *bound_i;
116 bound_i = isl_pw_aff_list_get_pw_aff(bound, i);
117 p = isl_printer_print_str(p, ") * (");
118 p = isl_printer_print_pw_aff(p, bound_i);
119 p = isl_printer_print_str(p, ") + (");
120 isl_pw_aff_free(bound_i);
122 p = isl_printer_print_ast_expr(p, expr);
123 if (i)
124 p = isl_printer_print_str(p, ")");
125 isl_ast_expr_free(expr);
127 p = isl_printer_print_str(p, "]");
129 return p;
132 /* Print a copy statement.
134 * A read copy statement is printed as
136 * local = global;
138 * while a write copy statement is printed as
140 * global = local;
142 __isl_give isl_printer *ppcg_kernel_print_copy(__isl_take isl_printer *p,
143 struct ppcg_kernel_stmt *stmt)
145 p = isl_printer_start_line(p);
146 if (stmt->u.c.read) {
147 p = stmt_print_local_index(p, stmt);
148 p = isl_printer_print_str(p, " = ");
149 p = stmt_print_global_index(p, stmt);
150 } else {
151 p = stmt_print_global_index(p, stmt);
152 p = isl_printer_print_str(p, " = ");
153 p = stmt_print_local_index(p, stmt);
155 p = isl_printer_print_str(p, ";");
156 p = isl_printer_end_line(p);
158 return p;
161 __isl_give isl_printer *ppcg_kernel_print_domain(__isl_take isl_printer *p,
162 struct ppcg_kernel_stmt *stmt)
164 return pet_stmt_print_body(stmt->u.d.stmt->stmt, p, stmt->u.d.ref2expr);