5 #include <isl/ast_build.h>
6 #include "size_simulate.h"
10 static int count(const char *tmpfile
)
17 module
= dlopen(tmpfile
, RTLD_LAZY
);
19 if ((error
= dlerror()) != NULL
) {
20 fprintf(stderr
, "%s\n", error
);
24 c
= (long (*)()) dlsym(module
, "__size_count");
25 if ((error
= dlerror()) != NULL
) {
26 fprintf(stderr
, "%s\n", error
);
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
)
39 isl_ast_expr
*expr
, *arg
;
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);
56 p
= isl_printer_start_line(p
);
57 p
= isl_printer_print_str(p
, "--count;");
58 p
= isl_printer_end_line(p
);
61 isl_ast_expr_free(arg
);
62 isl_ast_expr_free(expr
);
63 isl_ast_print_options_free(print_options
);
67 static int scan_program(__isl_keep isl_ast_node
*tree
)
70 char tmpfile
[L_tmpnam
];
71 char buffer
[L_tmpnam
+40];
76 isl_ast_print_options
*print_options
;
78 name
= tmpnam(tmpfile
);
81 snprintf(buffer
, sizeof(buffer
),
82 "gcc -std=c99 -x c -fPIC -O2 -shared - -o %s", tmpfile
);
83 dst
= popen(buffer
, "w");
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
,
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
);
133 assert(WEXITSTATUS(ret
) == 0);
135 return count(tmpfile
);
138 int selfloop_size_simulate(__isl_take isl_map
*dep
, __isl_take isl_set
*read
)
141 isl_ctx
*ctx
= isl_map_get_ctx(dep
);
144 isl_union_map
*sched
;
145 isl_ast_build
*build
;
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
);