-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgcc-ompinfo.c
454 lines (385 loc) · 11.8 KB
/
gcc-ompinfo.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
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
/**
* GCC plugin to draw a graph of OpenMP constructs using graphviz
* Kornilios Kourtis <[email protected]>
*/
#include <stdlib.h>
#include <gcc-plugin.h>
#include <tree-pass.h>
#include <gimple.h>
#include <splay-tree.h>
#include <tree.h> /* build_function_type, build_fn_decl */
#include <tree-flow.h>
#include <pretty-print.h>
#include <cgraph.h>
/* graphviz uses a cache for strings (e.g., names, labels), and will allocate
* memory as needed by itself -- i.e., we can just use the stack for strings
* passsed to it */
#include <graphviz/gvc.h>
/* Global variables for loading the graph */
static Agraph_t *gvGraph = NULL;
static char *gvOutfile = NULL;
//static GVC_t *gvCtx = NULL;
/* counter of OMP constructs, used to assigne unique ids */
static int omp_constructs_cnt = 1;
/* Use a splay tree to maintain the parent for each subgraph.
* (Tried to use the graphviz structure, but I don't think that there is a way)
*/
static splay_tree subgraph_parents;
/* declare needed functions that are not in plugin headers */
extern int dump_generic_node (pretty_printer *, tree, int, int, bool); /* tree-pretty-print.h */
static Agraph_t *
new_subgraph(Agraph_t *parent, char *name)
{
Agraph_t *g;
/* create a new subgraph, and update subgraph_parents */
g = agsubg(parent, name);
splay_tree_insert(subgraph_parents, (splay_tree_key)g, (splay_tree_value)parent);
return g;
}
static void
do_agset(void *obj, char *attr, char *value)
{
if (agset(obj, attr, value) == -1)
fprintf(stderr, "Couldn't set [%s,%s] to %p\n", attr,value,obj);
}
/* create a subgraph for a new function */
static Agraph_t *
mk_function_subgraph(Agraph_t *parent)
{
const char *fn_name = current_function_name();
char subgraph_name[strlen(fn_name) + 12];
Agraph_t *g;
assert(parent == gvGraph);
snprintf(subgraph_name, sizeof(subgraph_name), "cluster_%s", fn_name);
g = new_subgraph(parent, subgraph_name);
do_agset(g, "style", "solid");
do_agset(g, "color", "blue");
return g;
}
/* create an entry node for a function */
static Agnode_t *
mk_entry_node(Agraph_t *graph)
{
const char *fn_name = current_function_name();
char node_name[strlen(fn_name) + 128];
Agnode_t *n;
snprintf(node_name, sizeof(node_name), "%s()", fn_name);
n = agnode(graph, node_name);
do_agset(n, "shape", "note");
return n;
}
/* create a subgraph for an OMP construct */
static Agraph_t *
mk_omp_subgraph(Agraph_t *parent, const char *omp_name, int next_id)
{
Agraph_t *g;
int omp_len = strlen(omp_name);
int gimple_len = 7; // strlen("gimple_");
assert(omp_len > gimple_len);
char subgraph_name[omp_len - gimple_len + 40];
/* name it cluster_*, so that graphviz can draw it properly */
snprintf(subgraph_name, sizeof(subgraph_name), "cluster_%s.%d", omp_name + gimple_len, next_id);
g = new_subgraph(parent, subgraph_name);
do_agset(g, "style", "solid");
do_agset(g, "color", "lightgray");
return g;
}
static Agnode_t *
mk_omp_node(Agraph_t *graph, const char *omp_name, int next_id, Agnode_t *parent_node)
{
Agnode_t *n;
int omp_len = strlen(omp_name);
int gimple_len = 7; // strlen("gimple_");
assert(omp_len > gimple_len);
char node_name[omp_len - gimple_len + 32];
snprintf(node_name, sizeof(node_name), "%s.%d", omp_name + gimple_len, next_id);
n = agnode(graph, node_name);
do_agset(n, "shape", "box");
if (parent_node)
agedge(gvGraph, parent_node, n);
return n;
}
/*
* Create a label for an OMP for construct
*
* references:
* gimple-pretty-print.c:dump_gimple_omp_for()
* omp_low.c:extract_omp_for_data()
*/
static void
mk_omp_for_label(Agnode_t *node, gimple stmt, const char *omp_name)
{
int i;
pretty_printer pp;
pp_construct(&pp, NULL, 1024);
/* use a quick hack to use dump_generic_node():
* - create a pipe, and use it as a file descriptor
* - read the data from the pipe to a buffer
* - use the buffer for the label
*/
int pipefd[2], ret;
FILE *pipefp_rd, *pipefp_wr;
ret = pipe(pipefd);
if (ret < 0){
perror("pipe");
exit(1);
}
pipefp_rd = fdopen(pipefd[0], "r");
pipefp_wr = fdopen(pipefd[1], "w");
if (pipefp_rd == NULL || pipefp_wr == NULL) {
perror("fdopen");
exit(1);
}
pp.buffer->stream = pipefp_wr;
/* wrapper for dump_generic_node() */
void dump_n(tree _n) {
dump_generic_node(&pp, _n, 0, 0, false);
}
pp_printf(&pp, "{ %s", omp_name);
for (i=0; i<gimple_omp_for_collapse(stmt); i++) {
pp_string(&pp, " | for (");
/* initial */
dump_n(gimple_omp_for_index(stmt, i));
pp_string(&pp, "=");
dump_n(gimple_omp_for_initial(stmt, i));
pp_string(&pp, "; ");
/* condition */
dump_n(gimple_omp_for_index(stmt, i));
switch (gimple_omp_for_cond (stmt, i))
{
case LT_EXPR:
pp_string(&pp, "\\<");
break;
case GT_EXPR:
pp_string(&pp, "\\>");
break;
case LE_EXPR:
pp_string (&pp, "\\<=");
break;
case GE_EXPR:
pp_string (&pp, "\\>=");
break;
default:
assert(false);
}
dump_n(gimple_omp_for_final(stmt, i));
pp_string(&pp, "; ");
/* increment */
dump_n(gimple_omp_for_index(stmt, i));
pp_string(&pp, " = ");
dump_n(gimple_omp_for_incr(stmt, i));
pp_string(&pp, ")");
}
pp_string(&pp, "}");
pp_flush(&pp);
fclose(pipefp_wr);
/* read data from the pipe, and use them as a label */
char label[1024];
ret = fread(label, 1, sizeof(label), pipefp_rd);
assert(ret < sizeof(label) - 1);
label[ret - 1] = '\0'; // last one is a newline
do_agset(node, "label", label);
}
/* create an OMP for node */
static Agnode_t *
mk_omp_for_node(gimple omp_for, Agraph_t *graph, int next_id, Agnode_t *parent_node)
{
Agnode_t *n;
char node_name[16];
snprintf(node_name, sizeof(node_name), "omp_for.%d", next_id);
n = agnode(graph, node_name);
do_agset(n, "shape", "record");
mk_omp_for_label(n, omp_for, node_name);
if (parent_node)
agedge(gvGraph, parent_node, n);
return n;
}
/**
* Recursively called function to generate the graph of the OMP structues
* based on: gcc/omp-low.c:build_omp_regions_1()
*/
static void
do_omp_graph(basic_block bb, int parent_id, Agnode_t *parent_node, Agraph_t *parent_graph)
{
int next_id = parent_id;
Agnode_t *next_node = parent_node;
gimple_stmt_iterator gsi = gsi_last_bb(bb);
gimple stmt;
//gimple_dump_bb(bb, stdout, 1, 0);
/* OMP statements should be the last on the basic block */
if (!gsi_end_p(gsi) && is_gimple_omp(stmt = gsi_stmt(gsi)) ) {
/* If this is the first OMP construct in the function, create
* an entry node for the function. This node will be the
* parent graph */
if (parent_node == NULL) {
parent_graph = mk_function_subgraph(parent_graph);
parent_node = mk_entry_node(parent_graph);
}
enum gimple_code code = gimple_code(stmt);
const char *omp_name = gimple_code_name[code];
next_id = omp_constructs_cnt++;
switch (code) {
/* Create an OMP node, and a subgraph.
* Basically, everything that pairs with a return, and
* does not need special treatment (i.e., an OMP for)
* should go here */
case GIMPLE_OMP_TASK:
case GIMPLE_OMP_CRITICAL:
case GIMPLE_OMP_SINGLE:
case GIMPLE_OMP_PARALLEL:
parent_graph = mk_omp_subgraph(parent_graph, omp_name, next_id);
next_node = mk_omp_node(parent_graph, omp_name, next_id, parent_node);
break;
/* create a node and a subgraph for an OMP for */
case GIMPLE_OMP_FOR:
parent_graph = mk_omp_subgraph(parent_graph, omp_name, next_id);
next_node = mk_omp_for_node(stmt, parent_graph, next_id, parent_node);
break;
/* pop a subgraph */
case GIMPLE_OMP_RETURN: {
splay_tree_node n;
n = splay_tree_lookup(subgraph_parents, (splay_tree_key)parent_graph);
assert(n != NULL);
parent_graph = (Agraph_t *)n->value;
next_node = mk_omp_node(parent_graph, omp_name, next_id, parent_node);
}
break;
/* XXX: some of these might pair with OMP_RETURN, so they should move up */
case GIMPLE_OMP_MASTER:
case GIMPLE_OMP_ORDERED:
case GIMPLE_OMP_SECTION:
case GIMPLE_OMP_SECTIONS:
case GIMPLE_OMP_SECTIONS_SWITCH:
case GIMPLE_OMP_ATOMIC_LOAD:
case GIMPLE_OMP_ATOMIC_STORE:
case GIMPLE_OMP_CONTINUE:
next_node = mk_omp_node(parent_graph, omp_name, next_id, parent_node);
break;
default:
/* should not reach here */
fprintf(stderr, "Unknown OMP code!\n");
gcc_unreachable();
}
}
/* recursively handle children */
basic_block son = first_dom_son(CDI_DOMINATORS, bb);
while (son) {
do_omp_graph(son, next_id, next_node, parent_graph);
son = next_dom_son(CDI_DOMINATORS, son);
}
}
/**
* Create the graph of the OMP structures.
* based on gcc/omp-low.c:build_omp_regions()
*/
static unsigned int
omp_graph_execute(void)
{
printf("\t%s: %s\n", __FUNCTION__, current_function_name());
calculate_dominance_info(CDI_DOMINATORS);
do_omp_graph(ENTRY_BLOCK_PTR, omp_constructs_cnt, NULL, gvGraph);
return 0;
}
static bool
omp_pass_gate(void)
{
/* just use pas_expand_omp's gate */
return pass_expand_omp.pass.gate();
}
static struct gimple_opt_pass omp_graph_pass = {{
.type = GIMPLE_PASS,
.name = "*omp_graph",
.gate = omp_pass_gate,
.execute = omp_graph_execute,
.sub = NULL,
.next = NULL,
.static_pass_number = 0,
.tv_id = TV_NONE,
.properties_required = PROP_gimple_any,
.properties_provided = 0,
.properties_destroyed = 0,
.todo_flags_start = 0,
.todo_flags_finish = 0
}};
static struct register_pass_info omp_graph_info = {
.pass = &omp_graph_pass.pass,
.reference_pass_name = "ompexp",
.ref_pass_instance_number = 0,
.pos_op = PASS_POS_INSERT_BEFORE
};
int plugin_is_GPL_compatible;
static void
plugin_finish(void *gcc_data, void *user_data)
{
assert(gcc_data == NULL && user_data == NULL);
printf("%s:%s\n", __FILE__, __FUNCTION__);
char filename[strlen(gvOutfile) + 8];
snprintf(filename, sizeof(filename), "%s.dot", gvOutfile);
/** UberBUG:
* The code below was commented because gvRenderFilename() uses a
* emit_label() global function, which is also a global function of
* gcc. The gcc function is called, and all hell breaks loose.
*
* To avoid other hidden bugs like this, we do minimum use of the
* graphviz library functions (just libgraph). Rendering, etc, can
* be done externally.
*
* gvLayout(gvCtx, gvGraph, "dot");
* gvRenderFilename(gvCtx, gvGraph, "dot", filename);
* gvFreeLayout(gvCtx, gvGraph);
* gvFreeContext(gvCtx);
*/
FILE *filep;
filep = fopen(filename, "w");
if (filep == NULL){
perror(filename);
exit(1);
}
agwrite(gvGraph, filep);
fclose(filep);
agclose(gvGraph);
splay_tree_delete(subgraph_parents);
//dump_cgraph(stdout);
}
int
plugin_init(struct plugin_name_args *plugin_info,
struct plugin_gcc_version *version)
{
int argc, i;
struct plugin_argument *argv;
printf("%s:%s\n", __FILE__, __FUNCTION__);
argc = plugin_info->argc;
argv = plugin_info->argv;
gvOutfile = "output";
printf("\tplugin arguments:[");
for (i=0; i<argc; i++) {
if (strcmp(argv[i].key, "output") == 0) {
gvOutfile = argv[i].value;
printf("%s=%s ", argv[i].key, argv[i].value);
} else {
printf("%s=%s:IGNORED! ", argv[i].key, argv[i].value);
}
}
printf("]\n");
register_callback(plugin_info->base_name,
PLUGIN_PASS_MANAGER_SETUP,
NULL,
&omp_graph_info);
register_callback(plugin_info->base_name,
PLUGIN_FINISH,
plugin_finish,
NULL);
/* initialize graphviz */
//gvCtx = gvContext();
aginit(); /* normally called by gvContext() */
//agnodeattr(NULL, "label", "\\N"); /* ditto */
/* we need to set default values for attributes we want to customize */
agnodeattr(NULL, "shape", "oval");
agraphattr(NULL, "style", "invis");
agraphattr(NULL, "color", "black");
agnodeattr(NULL, "label", "");
agnodeattr(NULL, "rankdir", "");
gvGraph = agopen(gvOutfile, AGDIGRAPHSTRICT);
subgraph_parents = splay_tree_new(splay_tree_compare_pointers, 0, 0);
return 0;
}