gpu_print: Factor out printing of array arguments
[ppcg.git] / gpu_print.c
blob4a7790c9cebaf19bb2a70bb4e63d8baaf0d24033
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 the declaration of an array argument.
60 __isl_give isl_printer *gpu_array_info_print_declaration_argument(
61 __isl_take isl_printer *p, struct gpu_array_info *array)
63 if (gpu_array_is_read_only_scalar(array)) {
64 p = isl_printer_print_str(p, array->type);
65 p = isl_printer_print_str(p, " ");
66 p = isl_printer_print_str(p, array->name);
67 return p;
70 p = isl_printer_print_str(p, array->type);
71 p = isl_printer_print_str(p, " ");
72 p = isl_printer_print_str(p, "*");
73 p = isl_printer_print_str(p, array->name);
75 return p;
78 /* Print the call of an array argument.
80 __isl_give isl_printer *gpu_array_info_print_call_argument(
81 __isl_take isl_printer *p, struct gpu_array_info *array)
83 if (gpu_array_is_read_only_scalar(array))
84 return isl_printer_print_str(p, array->name);
86 p = isl_printer_print_str(p, "dev_");
87 p = isl_printer_print_str(p, array->name);
89 return p;
92 /* Print an access to the element in the private/shared memory copy
93 * described by "stmt". The index of the copy is recorded in
94 * stmt->local_index as an access to the array.
96 static __isl_give isl_printer *stmt_print_local_index(__isl_take isl_printer *p,
97 struct ppcg_kernel_stmt *stmt)
99 return isl_printer_print_ast_expr(p, stmt->u.c.local_index);
102 /* Print an access to the element in the global memory copy
103 * described by "stmt". The index of the copy is recorded in
104 * stmt->index as an access to the array.
106 * The copy in global memory has been linearized, so we need to take
107 * the array size into account.
109 static __isl_give isl_printer *stmt_print_global_index(
110 __isl_take isl_printer *p, struct ppcg_kernel_stmt *stmt)
112 int i;
113 struct gpu_array_info *array = stmt->u.c.array;
114 struct gpu_local_array_info *local = stmt->u.c.local_array;
115 isl_ast_expr *index;
117 if (gpu_array_is_scalar(array)) {
118 if (!gpu_array_is_read_only_scalar(array))
119 p = isl_printer_print_str(p, "*");
120 p = isl_printer_print_str(p, array->name);
121 return p;
124 index = isl_ast_expr_copy(stmt->u.c.index);
125 index = gpu_local_array_info_linearize_index(local, index);
126 p = isl_printer_print_ast_expr(p, index);
127 isl_ast_expr_free(index);
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);