update isl for replacement of CLooG test cases with schedule tree versions
[ppn.git] / size_simulate.cc
blob46c9b2c5be04ad181311ffadafb942fda321ced3
1 #include <sys/wait.h>
2 #include <dlfcn.h>
3 #include <unistd.h>
4 #include <isl/aff.h>
5 #include <isl/ast_build.h>
6 #include "size_simulate.h"
8 namespace size {
10 static int count(const char *tmpfile)
12 long (*c)();
13 void *module;
14 long max;
15 char *error;
17 module = dlopen(tmpfile, RTLD_LAZY);
18 unlink(tmpfile);
19 if ((error = dlerror()) != NULL) {
20 fprintf(stderr, "%s\n", error);
21 assert(0);
23 assert(module);
24 c = (long (*)()) dlsym(module, "__size_count");
25 if ((error = dlerror()) != NULL) {
26 fprintf(stderr, "%s\n", error);
27 assert(0);
29 max = c();
30 dlclose(module);
31 return max;
34 static __isl_give isl_printer *print_user(__isl_take isl_printer *p,
35 __isl_take isl_ast_print_options *print_options,
36 __isl_keep isl_ast_node *node, void *user)
38 isl_id *id;
39 isl_ast_expr *expr, *arg;
40 const char *name;
42 expr = isl_ast_node_user_get_expr(node);
43 arg = isl_ast_expr_get_op_arg(expr, 0);
44 id = isl_ast_expr_get_id(arg);
45 name = isl_id_get_name(id);
46 if (!strcmp(name, "write")) {
47 p = isl_printer_start_line(p);
48 p = isl_printer_print_str(p, "if (++count > max)");
49 p = isl_printer_end_line(p);
50 p = isl_printer_indent(p, 4);
51 p = isl_printer_start_line(p);
52 p = isl_printer_print_str(p, "max = count;");
53 p = isl_printer_end_line(p);
54 p = isl_printer_indent(p, -4);
55 } else {
56 p = isl_printer_start_line(p);
57 p = isl_printer_print_str(p, "--count;");
58 p = isl_printer_end_line(p);
60 isl_id_free(id);
61 isl_ast_expr_free(arg);
62 isl_ast_expr_free(expr);
63 isl_ast_print_options_free(print_options);
64 return p;
67 static int scan_program(__isl_keep isl_ast_node *tree)
69 int ret;
70 char tmpfile[L_tmpnam];
71 char buffer[L_tmpnam+40];
72 FILE *dst;
73 char *name;
74 isl_ctx *ctx;
75 isl_printer *p;
76 isl_ast_print_options *print_options;
78 name = tmpnam(tmpfile);
79 assert(name);
81 snprintf(buffer, sizeof(buffer),
82 "gcc -std=c99 -x c -fPIC -O2 -shared - -o %s", tmpfile);
83 dst = popen(buffer, "w");
84 assert(dst);
86 ctx = isl_ast_node_get_ctx(tree);
88 p = isl_printer_to_file(ctx, dst);
89 p = isl_printer_set_output_format(p, ISL_FORMAT_C);
91 p = isl_printer_start_line(p);
92 p = isl_printer_print_str(p, "#include <assert.h>");
93 p = isl_printer_end_line(p);
95 p = isl_printer_start_line(p);
96 p = isl_printer_print_str(p, "#include <stdio.h>");
97 p = isl_printer_end_line(p);
99 p = isl_ast_node_print_macros(tree, p);
101 p = isl_printer_start_line(p);
102 p = isl_printer_print_str(p, "long __size_count() {");
103 p = isl_printer_end_line(p);
105 p = isl_printer_indent(p, 4);
107 p = isl_printer_start_line(p);
108 p = isl_printer_print_str(p, "long count = 0, max = 0;");
109 p = isl_printer_end_line(p);
111 print_options = isl_ast_print_options_alloc(ctx);
112 print_options = isl_ast_print_options_set_print_user(print_options,
113 &print_user, NULL);
114 p = isl_ast_node_print(tree, p, print_options);
116 p = isl_printer_start_line(p);
117 p = isl_printer_print_str(p, "assert(count == 0);");
118 p = isl_printer_end_line(p);
120 p = isl_printer_start_line(p);
121 p = isl_printer_print_str(p, "return max;");
122 p = isl_printer_end_line(p);
124 p = isl_printer_indent(p, -4);
126 p = isl_printer_start_line(p);
127 p = isl_printer_print_str(p, "}");
128 p = isl_printer_end_line(p);
130 isl_printer_free(p);
132 ret = pclose(dst);
133 assert(WEXITSTATUS(ret) == 0);
135 return count(tmpfile);
138 int selfloop_size_simulate(__isl_take isl_map *dep, __isl_take isl_set *read)
140 int size;
141 isl_ctx *ctx = isl_map_get_ctx(dep);
142 isl_set *context;
143 isl_map *write;
144 isl_union_map *sched;
145 isl_ast_build *build;
146 isl_ast_node *tree;
148 dep = isl_map_intersect_range(dep, read);
149 write = isl_set_identity(isl_map_domain(isl_map_copy(dep)));
150 write = isl_map_set_tuple_name(write, isl_dim_in, "write");
151 dep = isl_map_set_tuple_name(dep, isl_dim_in, "read");
153 sched = isl_union_map_from_map(dep);
154 sched = isl_union_map_add_map(sched, write);
156 context = isl_set_universe(isl_union_map_get_space(sched));
157 build = isl_ast_build_from_context(context);
158 tree = isl_ast_build_ast_from_schedule(build, sched);
159 isl_ast_build_free(build);
161 size = scan_program(tree);
163 isl_ast_node_free(tree);
165 return size;