1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
|
/*
* Copyright 2012-2013 Ecole Normale Superieure
*
* Use of this software is governed by the MIT license
*
* Written by Sven Verdoolaege,
* Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
*/
#include <isl/aff.h>
#include <isl/ast_build.h>
#include <isl/id.h>
#include "print.h"
#include "util.h"
__isl_give isl_printer *ppcg_start_block(__isl_take isl_printer *p)
{
p = isl_printer_start_line(p);
p = isl_printer_print_str(p, "{");
p = isl_printer_end_line(p);
p = isl_printer_indent(p, 2);
return p;
}
__isl_give isl_printer *ppcg_end_block(__isl_take isl_printer *p)
{
p = isl_printer_indent(p, -2);
p = isl_printer_start_line(p);
p = isl_printer_print_str(p, "}");
p = isl_printer_end_line(p);
return p;
}
/* Names of notes that keep track of whether min/max
* macro definitions have already been printed.
*/
static const char *ppcg_max_printed = "ppcg_max_printed";
static const char *ppcg_min_printed = "ppcg_min_printed";
/* Has the macro definition corresponding to "note_name" been printed
* to "p" before?
* That is, does "p" have an associated "note_name" note?
*/
static isl_bool printed_before(__isl_keep isl_printer *p, const char *note_name)
{
isl_ctx *ctx;
isl_id *id;
isl_bool printed;
if (!p)
return isl_bool_error;
ctx = isl_printer_get_ctx(p);
id = isl_id_alloc(ctx, note_name, NULL);
printed = isl_printer_has_note(p, id);
isl_id_free(id);
return printed;
}
/* Keep track of the fact that the macro definition corresponding
* to "note_name" has been printed to "p" by attaching a note with
* that name. The value of the note is of no importance, but it
* has to be a valid isl_id, so the note identifier is reused
* as the note.
*/
static __isl_give isl_printer *mark_printed(__isl_take isl_printer *p,
const char *note_name)
{
isl_ctx *ctx;
isl_id *id;
if (!p)
return NULL;
ctx = isl_printer_get_ctx(p);
id = isl_id_alloc(ctx, note_name, NULL);
return isl_printer_set_note(p, id, isl_id_copy(id));
}
/* Print a macro definition "def" for the macro "name" to "p",
* unless such a macro definition has been printed to "p" before.
* "note_name" is used as the name of the note that keeps track
* of whether this printing has happened.
*/
static __isl_give isl_printer *print_ppcg_macro(__isl_take isl_printer *p,
const char *name, const char *def, const char *note_name)
{
isl_bool printed;
printed = printed_before(p, note_name);
if (printed < 0)
return isl_printer_free(p);
if (printed)
return p;
p = isl_printer_start_line(p);
p = isl_printer_print_str(p, "#define ");
p = isl_printer_print_str(p, name);
p = isl_printer_print_str(p, def);
p = isl_printer_end_line(p);
p = mark_printed(p, note_name);
return p;
}
/* Structure for keeping track of definitions of some macros.
*/
struct ppcg_macros {
const char *min;
const char *max;
};
/* Free the memory allocated by a struct ppcg_macros.
*/
static void ppcg_macros_free(void *user)
{
free(user);
}
/* Default macro definitions (when GNU extensions are allowed).
*/
struct ppcg_macros ppcg_macros_default = {
.min = "(x,y) "
"({ __typeof__(x) _x = (x); __typeof__(y) _y = (y); "
"_x < _y ? _x : _y; })",
.max = "(x,y) "
"({ __typeof__(x) _x = (x); __typeof__(y) _y = (y); "
"_x > _y ? _x : _y; })",
};
/* Name used for the note that keeps track of macro definitions.
*/
static const char *ppcg_macros = "ppcg_macros";
/* Set the macro definitions for isl_ast_op_min and isl_ast_op_max
* to "min" and "max" and store them in "p".
*
* In particular, create a ppcg_macros object and attach it
* as a note to the printer.
*/
__isl_give isl_printer *ppcg_set_macros(__isl_take isl_printer *p,
const char *min, const char *max)
{
isl_ctx *ctx;
isl_id *id, *macros_id;
struct ppcg_macros *macros;
if (!p)
return NULL;
ctx = isl_printer_get_ctx(p);
macros = isl_alloc_type(ctx, struct ppcg_macros);
if (!macros)
return isl_printer_free(p);
macros->min = min;
macros->max = max;
id = isl_id_alloc(ctx, ppcg_macros, NULL);
macros_id = isl_id_alloc(ctx, NULL, macros);
if (!macros_id)
ppcg_macros_free(macros);
else
macros_id = isl_id_set_free_user(macros_id, &ppcg_macros_free);
p = isl_printer_set_note(p, id, macros_id);
return p;
}
/* Return the ppcg_macros object that holds the currently active
* macro definitions in "p".
* If "p" has a note with macro definitions, then return those.
* Otherwise, return the default macro definitions.
*/
static struct ppcg_macros *get_macros(__isl_keep isl_printer *p)
{
isl_id *id;
isl_bool has_macros;
struct ppcg_macros *macros;
id = isl_id_alloc(isl_printer_get_ctx(p), ppcg_macros, NULL);
has_macros = isl_printer_has_note(p, id);
if (has_macros < 0 || !has_macros) {
isl_id_free(id);
if (has_macros < 0)
return NULL;
return &ppcg_macros_default;
}
id = isl_printer_get_note(p, id);
macros = isl_id_get_user(id);
isl_id_free(id);
return macros;
}
/* Print the currently active macro definition for ppcg_max.
*/
static __isl_give isl_printer *print_max(__isl_take isl_printer *p)
{
struct ppcg_macros *macros;
macros = get_macros(p);
if (!macros)
return isl_printer_free(p);
return print_ppcg_macro(p, ppcg_max, macros->max, ppcg_max_printed);
}
/* Print the currently active macro definition for ppcg_min.
*/
static __isl_give isl_printer *print_min(__isl_take isl_printer *p)
{
struct ppcg_macros *macros;
macros = get_macros(p);
if (!macros)
return isl_printer_free(p);
return print_ppcg_macro(p, ppcg_min, macros->min, ppcg_min_printed);
}
/* Print a macro definition for "type" to "p".
* If GNU extensions are allowed, then print a specialized definition
* for isl_ast_op_min and isl_ast_op_max.
* Otherwise, use the default isl definition.
*/
__isl_give isl_printer *ppcg_print_macro(enum isl_ast_op_type type,
__isl_take isl_printer *p)
{
isl_ctx *ctx;
struct ppcg_options *options;
if (!p)
return NULL;
ctx = isl_printer_get_ctx(p);
options = isl_ctx_peek_options(ctx, &ppcg_options_args);
if (!options || !options->allow_gnu_extensions)
return isl_ast_op_type_print_macro(type, p);
switch (type) {
case isl_ast_op_max:
return print_max(p);
case isl_ast_op_min:
return print_min(p);
default:
return isl_ast_op_type_print_macro(type, p);
}
}
/* isl_ast_expr_foreach_ast_op_type or isl_ast_node_foreach_ast_op_type
* callback that prints a macro definition for "type".
*/
static isl_stat print_macro(enum isl_ast_op_type type, void *user)
{
isl_printer **p = user;
*p = ppcg_print_macro(type, *p);
if (!*p)
return isl_stat_error;
return isl_stat_ok;
}
/* Print the required macros for "expr".
*/
__isl_give isl_printer *ppcg_ast_expr_print_macros(
__isl_keep isl_ast_expr *expr, __isl_take isl_printer *p)
{
if (isl_ast_expr_foreach_ast_op_type(expr, &print_macro, &p) < 0)
return isl_printer_free(p);
return p;
}
/* isl_id_to_ast_expr_foreach callback that prints the required
* macro definitions for "val".
*/
static isl_stat print_expr_macros(__isl_take isl_id *key,
__isl_take isl_ast_expr *val, void *user)
{
isl_printer **p = user;
*p = ppcg_ast_expr_print_macros(val, *p);
isl_id_free(key);
isl_ast_expr_free(val);
if (!*p)
return isl_stat_error;
return isl_stat_ok;
}
/* Print the required macro definitions for the body of a statement in which
* the access expressions are replaced by the isl_ast_expr objects
* in "ref2expr".
*/
__isl_give isl_printer *ppcg_print_body_macros(__isl_take isl_printer *p,
__isl_keep isl_id_to_ast_expr *ref2expr)
{
if (isl_id_to_ast_expr_foreach(ref2expr, &print_expr_macros, &p) < 0)
return isl_printer_free(p);
return p;
}
/* Print the required macros for "node".
*/
__isl_give isl_printer *ppcg_print_macros(__isl_take isl_printer *p,
__isl_keep isl_ast_node *node)
{
if (isl_ast_node_foreach_ast_op_type(node, &print_macro, &p) < 0)
return isl_printer_free(p);
return p;
}
/* Names used for the macros that may appear in a printed isl AST.
*/
const char *ppcg_min = "ppcg_min";
const char *ppcg_max = "ppcg_max";
const char *ppcg_fdiv_q = "ppcg_fdiv_q";
/* Set the names of the macros that may appear in a printed isl AST.
*/
__isl_give isl_printer *ppcg_set_macro_names(__isl_take isl_printer *p)
{
p = isl_ast_op_type_set_print_name(p, isl_ast_op_min, ppcg_min);
p = isl_ast_op_type_set_print_name(p, isl_ast_op_max, ppcg_max);
p = isl_ast_op_type_set_print_name(p, isl_ast_op_fdiv_q, ppcg_fdiv_q);
return p;
}
/* Given a multi affine expression "mpa" without domain, modify it to have
* the schedule space of "build" as domain.
*
* If the schedule space of "build" is a parameter space, then nothing
* needs to be done.
* Otherwise, "mpa" is first given a 0D domain and then it is combined
* with a mapping from the schedule space of "build" to the same 0D domain.
*/
__isl_give isl_multi_pw_aff *ppcg_attach_multi_pw_aff(
__isl_take isl_multi_pw_aff *mpa, __isl_keep isl_ast_build *build)
{
isl_bool params;
isl_space *space;
isl_multi_aff *ma;
space = isl_ast_build_get_schedule_space(build);
params = isl_space_is_params(space);
if (params < 0 || params) {
isl_space_free(space);
if (params < 0)
return isl_multi_pw_aff_free(mpa);
return mpa;
}
space = isl_space_from_domain(space);
ma = isl_multi_aff_zero(space);
mpa = isl_multi_pw_aff_from_range(mpa);
mpa = isl_multi_pw_aff_pullback_multi_aff(mpa, ma);
return mpa;
}
/* Build an access AST expression from "size" using "build".
* "size" does not have a domain, but "build" may have a proper schedule space.
* First modify "size" to have that schedule space as domain.
*/
__isl_give isl_ast_expr *ppcg_build_size_expr(__isl_take isl_multi_pw_aff *size,
__isl_keep isl_ast_build *build)
{
size = ppcg_attach_multi_pw_aff(size, build);
return isl_ast_build_access_from_multi_pw_aff(build, size);
}
/* Print a declaration for an array with element type "base_type" and
* size "size" to "p".
*/
__isl_give isl_printer *ppcg_print_declaration_with_size(
__isl_take isl_printer *p, const char *base_type,
__isl_keep isl_ast_expr *size)
{
if (!base_type || !size)
return isl_printer_free(p);
p = ppcg_ast_expr_print_macros(size, p);
p = isl_printer_start_line(p);
p = isl_printer_print_str(p, base_type);
p = isl_printer_print_str(p, " ");
p = isl_printer_print_ast_expr(p, size);
p = isl_printer_print_str(p, ";");
p = isl_printer_end_line(p);
return p;
}
/* Print a declaration for array "array" to "p", using "build"
* to simplify any size expressions.
*
* The size is computed from the extent of the array and is
* subsequently converted to an "access expression" by "build".
*/
__isl_give isl_printer *ppcg_print_declaration(__isl_take isl_printer *p,
struct pet_array *array, __isl_keep isl_ast_build *build)
{
isl_multi_pw_aff *size;
isl_ast_expr *expr;
if (!array)
return isl_printer_free(p);
size = ppcg_size_from_extent(isl_set_copy(array->extent));
expr = isl_ast_build_access_from_multi_pw_aff(build, size);
p = ppcg_print_declaration_with_size(p, array->element_type, expr);
isl_ast_expr_free(expr);
return p;
}
/* Print declarations for the arrays in "scop" that are declared
* and that are exposed (if exposed == 1) or not exposed (if exposed == 0).
*/
static __isl_give isl_printer *print_declarations(__isl_take isl_printer *p,
struct ppcg_scop *scop, int exposed)
{
int i;
isl_ast_build *build;
if (!scop)
return isl_printer_free(p);
build = isl_ast_build_from_context(isl_set_copy(scop->context));
for (i = 0; i < scop->pet->n_array; ++i) {
struct pet_array *array = scop->pet->arrays[i];
if (!array->declared)
continue;
if (array->exposed != exposed)
continue;
p = ppcg_print_declaration(p, array, build);
}
isl_ast_build_free(build);
return p;
}
/* Print declarations for the arrays in "scop" that are declared
* and exposed to the code after the scop.
*/
__isl_give isl_printer *ppcg_print_exposed_declarations(
__isl_take isl_printer *p, struct ppcg_scop *scop)
{
return print_declarations(p, scop, 1);
}
/* Print declarations for the arrays in "scop" that are declared,
* but not exposed to the code after the scop.
*/
__isl_give isl_printer *ppcg_print_hidden_declarations(
__isl_take isl_printer *p, struct ppcg_scop *scop)
{
return print_declarations(p, scop, 0);
}
|