-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpet_codegen.c
384 lines (326 loc) · 10.6 KB
/
pet_codegen.c
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
/*
* Copyright 2012-2013 Ecole Normale Superieure. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY ECOLE NORMALE SUPERIEURE ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECOLE NORMALE SUPERIEURE OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation
* are those of the authors and should not be interpreted as
* representing official policies, either expressed or implied, of
* Ecole Normale Superieure.
*/
#include <assert.h>
#include <isl/space.h>
#include <isl/aff.h>
#include <isl/ast_build.h>
#include <isl/options.h>
#include <isl/set.h>
#include <isl/map.h>
#include <isl/union_set.h>
#include <isl/schedule_node.h>
struct options {
struct isl_options *isl;
unsigned tree;
unsigned atomic;
unsigned separate;
unsigned read_options;
};
ISL_ARGS_START(struct options, options_args)
ISL_ARG_CHILD(struct options, isl, "isl", &isl_options_args, "isl options")
ISL_ARG_BOOL(struct options, tree, 0, "tree", 0,
"input schedule is specified as schedule tree")
ISL_ARG_BOOL(struct options, atomic, 0, "atomic", 0,
"globally set the atomic option")
ISL_ARG_BOOL(struct options, separate, 0, "separate", 0,
"globally set the separate option")
ISL_ARG_BOOL(struct options, read_options, 0, "read-options", 0,
"read options from standard input")
ISL_ARGS_END
ISL_ARG_DEF(options, struct options, options_args)
/* Return a universal, 1-dimensional set with the given name.
*/
static __isl_give isl_union_set *universe(isl_ctx *ctx, const char *name)
{
isl_space *space;
space = isl_space_set_alloc(ctx, 0, 1);
space = isl_space_set_tuple_name(space, isl_dim_set, name);
return isl_union_set_from_set(isl_set_universe(space));
}
/* Set the "name" option for the entire schedule domain.
*/
static __isl_give isl_union_map *set_universe(__isl_take isl_union_map *opt,
__isl_keep isl_union_map *schedule, const char *name)
{
isl_ctx *ctx;
isl_union_set *domain, *target;
isl_union_map *option;
ctx = isl_union_map_get_ctx(opt);
domain = isl_union_map_range(isl_union_map_copy(schedule));
domain = isl_union_set_universe(domain);
target = universe(ctx, name);
option = isl_union_map_from_domain_and_range(domain, target);
opt = isl_union_map_union(opt, option);
return opt;
}
/* Set the build options based on the command line options.
*
* If no command line options are specified, we use default build options.
* If --read-options is specified, we read the build options from standard
* input.
* If --separate or --atomic is specified, we turn on the corresponding
* build option over the entire schedule domain.
*/
static __isl_give isl_ast_build *set_options(__isl_take isl_ast_build *build,
struct options *options, __isl_keep isl_union_map *schedule)
{
isl_ctx *ctx;
isl_union_map *opt;
if (!options->separate && !options->atomic && !options->read_options)
return build;
ctx = isl_union_map_get_ctx(schedule);
if (options->read_options)
opt = isl_union_map_read_from_file(ctx, stdin);
else
opt = isl_union_map_empty(isl_union_map_get_space(schedule));
if (options->separate)
opt = set_universe(opt, schedule, "separate");
if (options->atomic)
opt = set_universe(opt, schedule, "atomic");
build = isl_ast_build_set_options(build, opt);
return build;
}
/* Print a function declaration for the domain "set".
*
* In particular, print a declaration of the form
*
* void S(int, ..., int);
*
* where S is the name of the domain and the number of arguments
* is equal to the dimension of "set".
*/
static isl_stat print_declaration(__isl_take isl_set *set, void *user)
{
isl_printer **p = user;
int i, n;
n = isl_set_dim(set, isl_dim_set);
*p = isl_printer_start_line(*p);
*p = isl_printer_print_str(*p, "void ");
*p = isl_printer_print_str(*p, isl_set_get_tuple_name(set));
*p = isl_printer_print_str(*p, "(");
for (i = 0; i < n; ++i) {
if (i)
*p = isl_printer_print_str(*p, ", ");
*p = isl_printer_print_str(*p, "int");
}
*p = isl_printer_print_str(*p, ");");
*p = isl_printer_end_line(*p);
isl_set_free(set);
return isl_stat_ok;
}
/* Print a function declaration for each domain in "uset".
*/
static __isl_give isl_printer *print_declarations(__isl_take isl_printer *p,
__isl_keep isl_union_set *uset)
{
if (isl_union_set_foreach_set(uset, &print_declaration, &p) >= 0)
return p;
isl_printer_free(p);
return NULL;
}
/* Check that the domain of "map" is named.
*/
static isl_stat check_name(__isl_take isl_map *map, void *user)
{
int named;
isl_ctx *ctx;
ctx = isl_map_get_ctx(map);
named = isl_map_has_tuple_name(map, isl_dim_in);
isl_map_free(map);
if (named < 0)
return isl_stat_error;
if (!named)
isl_die(ctx, isl_error_invalid,
"all domains should be named", return isl_stat_error);
return isl_stat_ok;
}
/* Given an AST "tree", print out the following code
*
* void foo(<parameters>/)
* {
* void S1(int,...,int);
* #pragma scop
* AST
* #pragma endscop
* }
*
* where the declarations are derived from the spaces in "domain".
*/
static void print_tree(__isl_take isl_union_set *domain,
__isl_take isl_ast_node *tree)
{
int i, n;
isl_ctx *ctx;
isl_space *space;
isl_printer *p;
isl_ast_print_options *print_options;
if (!domain || !tree)
goto error;
ctx = isl_union_set_get_ctx(domain);
p = isl_printer_to_file(ctx, stdout);
p = isl_printer_set_output_format(p, ISL_FORMAT_C);
p = isl_printer_start_line(p);
p = isl_printer_print_str(p, "void foo(");
space = isl_union_set_get_space(domain);
n = isl_space_dim(space, isl_dim_param);
for (i = 0; i < n; ++i) {
const char *name;
if (i)
p = isl_printer_print_str(p, ", ");
name = isl_space_get_dim_name(space, isl_dim_param, i);
p = isl_printer_print_str(p, "int ");
p = isl_printer_print_str(p, name);
}
isl_space_free(space);
p = isl_printer_print_str(p, ")");
p = isl_printer_end_line(p);
p = isl_printer_start_line(p);
p = isl_printer_print_str(p, "{");
p = isl_printer_end_line(p);
p = isl_printer_start_line(p);
p = isl_printer_indent(p, 2);
p = print_declarations(p, domain);
p = isl_printer_indent(p, -2);
p = isl_printer_print_str(p, "#pragma scop");
p = isl_printer_end_line(p);
p = isl_printer_indent(p, 2);
print_options = isl_ast_print_options_alloc(ctx);
p = isl_ast_node_print(tree, p, print_options);
p = isl_printer_indent(p, -2);
p = isl_printer_start_line(p);
p = isl_printer_print_str(p, "#pragma endscop");
p = isl_printer_end_line(p);
p = isl_printer_start_line(p);
p = isl_printer_print_str(p, "}");
p = isl_printer_end_line(p);
isl_printer_free(p);
error:
isl_union_set_free(domain);
isl_ast_node_free(tree);
}
/* If "node" is a band node, then replace the AST build options
* by "options".
*/
static __isl_give isl_schedule_node *node_set_options(
__isl_take isl_schedule_node *node, void *user)
{
enum isl_ast_loop_type *type = user;
int i, n;
if (isl_schedule_node_get_type(node) != isl_schedule_node_band)
return node;
n = isl_schedule_node_band_n_member(node);
for (i = 0; i < n; ++i)
node = isl_schedule_node_band_member_set_ast_loop_type(node,
i, *type);
return node;
}
/* Replace the AST build options on all band nodes if requested
* by the user.
*/
static __isl_give isl_schedule *schedule_set_options(
__isl_take isl_schedule *schedule, struct options *options)
{
enum isl_ast_loop_type type;
if (!options->separate && !options->atomic)
return schedule;
type = options->separate ? isl_ast_loop_separate : isl_ast_loop_atomic;
schedule = isl_schedule_map_schedule_node_bottom_up(schedule,
&node_set_options, &type);
return schedule;
}
/* Read a schedule tree, generate an AST and print the result
* in a form that is readable by pet.
*/
static int print_schedule_tree(isl_ctx *ctx, struct options *options)
{
isl_union_set *domain;
isl_schedule *schedule;
isl_ast_build *build;
isl_ast_node *tree;
schedule = isl_schedule_read_from_file(ctx, stdin);
domain = isl_schedule_get_domain(schedule);
build = isl_ast_build_alloc(ctx);
schedule = schedule_set_options(schedule, options);
tree = isl_ast_build_node_from_schedule(build, schedule);
isl_ast_build_free(build);
print_tree(domain, tree);
return 0;
}
/* Read a schedule, a context and (optionally) build options,
* generate an AST and print the result in a form that is readable
* by pet.
*/
static int print_schedule_map(isl_ctx *ctx, struct options *options)
{
isl_set *context;
isl_union_set *domain;
isl_union_map *schedule;
isl_ast_build *build;
isl_ast_node *tree;
schedule = isl_union_map_read_from_file(ctx, stdin);
if (isl_union_map_foreach_map(schedule, &check_name, NULL) < 0) {
isl_union_map_free(schedule);
return 1;
}
context = isl_set_read_from_file(ctx, stdin);
domain = isl_union_map_domain(isl_union_map_copy(schedule));
domain = isl_union_set_align_params(domain, isl_set_get_space(context));
build = isl_ast_build_from_context(context);
build = set_options(build, options, schedule);
tree = isl_ast_build_node_from_schedule_map(build, schedule);
isl_ast_build_free(build);
print_tree(domain, tree);
return 0;
}
/* Read either
* - a schedule tree or
* - a schedule, a context and (optionally) build options,
* generate an AST and print the result in a form that is readable
* by pet.
*/
int main(int argc, char **argv)
{
isl_ctx *ctx;
struct options *options;
int r;
options = options_new_with_defaults();
assert(options);
argc = options_parse(options, argc, argv, ISL_ARG_ALL);
ctx = isl_ctx_alloc_with_options(&options_args, options);
if (options->tree)
r = print_schedule_tree(ctx, options);
else
r = print_schedule_map(ctx, options);
isl_ctx_free(ctx);
return r;
}