7 #include <isl/ast_build.h>
8 #include <isl/printer.h>
9 #include "size_simulate.h"
13 static int count(const char *tmpfile
)
20 module
= dlopen(tmpfile
, RTLD_LAZY
);
22 if ((error
= dlerror()) != NULL
) {
23 fprintf(stderr
, "%s\n", error
);
27 c
= (long (*)()) dlsym(module
, "__size_count");
28 if ((error
= dlerror()) != NULL
) {
29 fprintf(stderr
, "%s\n", error
);
37 static __isl_give isl_printer
*print_user(__isl_take isl_printer
*p
,
38 __isl_take isl_ast_print_options
*print_options
,
39 __isl_keep isl_ast_node
*node
, void *user
)
42 isl_ast_expr
*expr
, *arg
;
45 expr
= isl_ast_node_user_get_expr(node
);
46 arg
= isl_ast_expr_get_op_arg(expr
, 0);
47 id
= isl_ast_expr_get_id(arg
);
48 name
= isl_id_get_name(id
);
49 if (!strcmp(name
, "write")) {
50 p
= isl_printer_start_line(p
);
51 p
= isl_printer_print_str(p
, "if (++count > max)");
52 p
= isl_printer_end_line(p
);
53 p
= isl_printer_indent(p
, 4);
54 p
= isl_printer_start_line(p
);
55 p
= isl_printer_print_str(p
, "max = count;");
56 p
= isl_printer_end_line(p
);
57 p
= isl_printer_indent(p
, -4);
59 p
= isl_printer_start_line(p
);
60 p
= isl_printer_print_str(p
, "--count;");
61 p
= isl_printer_end_line(p
);
64 isl_ast_expr_free(arg
);
65 isl_ast_expr_free(expr
);
66 isl_ast_print_options_free(print_options
);
70 static int scan_program(__isl_keep isl_ast_node
*tree
)
73 char tmpfile
[L_tmpnam
];
74 char buffer
[L_tmpnam
+40];
79 isl_ast_print_options
*print_options
;
81 name
= tmpnam(tmpfile
);
84 snprintf(buffer
, sizeof(buffer
),
85 "gcc -std=c99 -x c -fPIC -O2 -shared - -o %s", tmpfile
);
86 dst
= popen(buffer
, "w");
89 ctx
= isl_ast_node_get_ctx(tree
);
91 p
= isl_printer_to_file(ctx
, dst
);
92 p
= isl_printer_set_output_format(p
, ISL_FORMAT_C
);
94 p
= isl_printer_start_line(p
);
95 p
= isl_printer_print_str(p
, "#include <assert.h>");
96 p
= isl_printer_end_line(p
);
98 p
= isl_printer_start_line(p
);
99 p
= isl_printer_print_str(p
, "#include <stdio.h>");
100 p
= isl_printer_end_line(p
);
102 p
= isl_ast_node_print_macros(tree
, p
);
104 p
= isl_printer_start_line(p
);
105 p
= isl_printer_print_str(p
, "long __size_count() {");
106 p
= isl_printer_end_line(p
);
108 p
= isl_printer_indent(p
, 4);
110 p
= isl_printer_start_line(p
);
111 p
= isl_printer_print_str(p
, "long count = 0, max = 0;");
112 p
= isl_printer_end_line(p
);
114 print_options
= isl_ast_print_options_alloc(ctx
);
115 print_options
= isl_ast_print_options_set_print_user(print_options
,
117 p
= isl_ast_node_print(tree
, p
, print_options
);
119 p
= isl_printer_start_line(p
);
120 p
= isl_printer_print_str(p
, "assert(count == 0);");
121 p
= isl_printer_end_line(p
);
123 p
= isl_printer_start_line(p
);
124 p
= isl_printer_print_str(p
, "return max;");
125 p
= isl_printer_end_line(p
);
127 p
= isl_printer_indent(p
, -4);
129 p
= isl_printer_start_line(p
);
130 p
= isl_printer_print_str(p
, "}");
131 p
= isl_printer_end_line(p
);
136 assert(WEXITSTATUS(ret
) == 0);
138 return count(tmpfile
);
141 int selfloop_size_simulate(__isl_take isl_map
*dep
, __isl_take isl_set
*read
)
144 isl_ctx
*ctx
= isl_map_get_ctx(dep
);
147 isl_union_map
*sched
;
148 isl_ast_build
*build
;
151 dep
= isl_map_intersect_range(dep
, read
);
152 write
= isl_set_identity(isl_map_domain(isl_map_copy(dep
)));
153 write
= isl_map_set_tuple_name(write
, isl_dim_in
, "write");
154 dep
= isl_map_set_tuple_name(dep
, isl_dim_in
, "read");
156 sched
= isl_union_map_from_map(dep
);
157 sched
= isl_union_map_add_map(sched
, write
);
159 context
= isl_set_universe(isl_union_map_get_space(sched
));
160 build
= isl_ast_build_from_context(context
);
161 tree
= isl_ast_build_ast_from_schedule(build
, sched
);
162 isl_ast_build_free(build
);
164 size
= scan_program(tree
);
166 isl_ast_node_free(tree
);