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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
| /*
* Copyright 2013 Ecole Normale Superieure
*
* Use of this software is governed by the MIT license
*
* Written by Sven Verdoolaege,
* Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
*/
#include <string.h>
#include <isl/set.h>
#include <isl/union_set.h>
#include <isl/space.h>
#include "gpu_tree.h"
/* The functions in this file are used to navigate part of a schedule tree
* that is mapped to blocks. Initially, this part consists of a linear
* branch segment with a mark node with name "kernel" on the outer end
* and a mark node with name "thread" on the inner end.
* During the mapping to blocks, branching may be introduced, but only
* one of the elements in each sequence contains the "thread" mark.
* The filter of this element (and only this filter) contains
* domain elements identified by the "core" argument of the functions
* that move down this tree.
*
* Synchronization statements have a name that starts with "sync" and
* a user pointer pointing to the kernel that contains the synchronization.
* The functions inserting or detecting synchronizations take a ppcg_kernel
* argument to be able to create or identify such statements.
* They may also use two fields in this structure, the "core" field
* to move around in the tree and the "n_sync" field to make sure that
* each synchronization has a different name (within the kernel).
*/
/* Is "node" a mark node with an identifier called "name"?
*/
static int is_marked(__isl_keep isl_schedule_node *node, const char *name)
{
isl_id *mark;
int has_name;
if (!node)
return -1;
if (isl_schedule_node_get_type(node) != isl_schedule_node_mark)
return 0;
mark = isl_schedule_node_mark_get_id(node);
if (!mark)
return -1;
has_name = !strcmp(isl_id_get_name(mark), name);
isl_id_free(mark);
return has_name;
}
/* Is "node" a mark node with an identifier called "kernel"?
*/
int gpu_tree_node_is_kernel(__isl_keep isl_schedule_node *node)
{
return is_marked(node, "kernel");
}
/* Is "node" a mark node with an identifier called "shared"?
*/
static int node_is_shared(__isl_keep isl_schedule_node *node)
{
return is_marked(node, "shared");
}
/* Is "node" a mark node with an identifier called "thread"?
*/
static int node_is_thread(__isl_keep isl_schedule_node *node)
{
return is_marked(node, "thread");
}
/* Insert a mark node with identifier "shared" in front of "node".
*/
static __isl_give isl_schedule_node *insert_shared(
__isl_take isl_schedule_node *node)
{
isl_ctx *ctx;
isl_id *id;
ctx = isl_schedule_node_get_ctx(node);
id = isl_id_alloc(ctx, "shared", NULL);
node = isl_schedule_node_insert_mark(node, id);
return node;
}
/* Insert a "shared" mark in front of the "thread" mark
* provided the linear branch between "node" and the "thread" mark
* does not contain such a "shared" mark already.
*
* As a side effect, this function checks that the subtree at "node"
* actually contains a "thread" mark and that there is no branching
* in between "node" and this "thread" mark.
*/
__isl_give isl_schedule_node *gpu_tree_insert_shared_before_thread(
__isl_take isl_schedule_node *node)
{
int depth0, depth;
int any_shared = 0;
if (!node)
return NULL;
depth0 = isl_schedule_node_get_tree_depth(node);
for (;;) {
int is_thread;
int n;
if (!any_shared) {
any_shared = node_is_shared(node);
if (any_shared < 0)
return isl_schedule_node_free(node);
}
is_thread = node_is_thread(node);
if (is_thread < 0)
return isl_schedule_node_free(node);
if (is_thread)
break;
n = isl_schedule_node_n_children(node);
if (n == 0)
isl_die(isl_schedule_node_get_ctx(node),
isl_error_invalid,
"no thread marker found",
return isl_schedule_node_free(node));
if (n > 1)
isl_die(isl_schedule_node_get_ctx(node),
isl_error_invalid,
"expecting single thread marker",
return isl_schedule_node_free(node));
node = isl_schedule_node_child(node, 0);
}
if (!any_shared)
node = insert_shared(node);
depth = isl_schedule_node_get_tree_depth(node);
node = isl_schedule_node_ancestor(node, depth - depth0);
return node;
}
/* Assuming "node" is a filter node, does it correspond to the branch
* that contains the "thread" mark, i.e., does it contain any elements
* in "core"?
*/
static int node_is_core(__isl_keep isl_schedule_node *node,
__isl_keep isl_union_set *core)
{
int disjoint;
isl_union_set *filter;
filter = isl_schedule_node_filter_get_filter(node);
disjoint = isl_union_set_is_disjoint(filter, core);
isl_union_set_free(filter);
if (disjoint < 0)
return -1;
return !disjoint;
}
/* Move to the only child of "node" that has the "thread" mark as descendant,
* where the branch containing this mark is identified by the domain elements
* in "core".
*
* If "node" is not a sequence, then it only has one child and we move
* to that single child.
* Otherwise, we check each of the filters in the children, pick
* the one that corresponds to "core" and return a pointer to the child
* of the filter node.
*/
static __isl_give isl_schedule_node *core_child(
__isl_take isl_schedule_node *node, __isl_keep isl_union_set *core)
{
int i, n;
if (isl_schedule_node_get_type(node) != isl_schedule_node_sequence)
return isl_schedule_node_child(node, 0);
n = isl_schedule_node_n_children(node);
for (i = 0; i < n; ++i) {
int is_core;
node = isl_schedule_node_child(node, i);
is_core = node_is_core(node, core);
if (is_core < 0)
return isl_schedule_node_free(node);
if (is_core)
return isl_schedule_node_child(node, 0);
node = isl_schedule_node_parent(node);
}
isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
"core child not found", return isl_schedule_node_free(node));
}
/* Move down the branch between "kernel" and "thread" until
* the "shared" mark is reached, where the branch containing the "shared"
* mark is identified by the domain elements in "core".
*/
__isl_give isl_schedule_node *gpu_tree_move_down_to_shared(
__isl_take isl_schedule_node *node, __isl_keep isl_union_set *core)
{
int is_shared;
while ((is_shared = node_is_shared(node)) == 0)
node = core_child(node, core);
if (is_shared < 0)
node = isl_schedule_node_free(node);
return node;
}
/* Move down the branch between "kernel" and "thread" until
* the "thread" mark is reached, where the branch containing the "thread"
* mark is identified by the domain elements in "core".
*/
__isl_give isl_schedule_node *gpu_tree_move_down_to_thread(
__isl_take isl_schedule_node *node, __isl_keep isl_union_set *core)
{
int is_thread;
while ((is_thread = node_is_thread(node)) == 0)
node = core_child(node, core);
if (is_thread < 0)
node = isl_schedule_node_free(node);
return node;
}
/* Move up the tree underneath the "thread" mark until
* the "thread" mark is reached.
*/
__isl_give isl_schedule_node *gpu_tree_move_up_to_thread(
__isl_take isl_schedule_node *node)
{
int is_thread;
while ((is_thread = node_is_thread(node)) == 0)
node = isl_schedule_node_parent(node);
if (is_thread < 0)
node = isl_schedule_node_free(node);
return node;
}
/* Move up the tree underneath the "kernel" mark until
* the "kernel" mark is reached.
*/
__isl_give isl_schedule_node *gpu_tree_move_up_to_kernel(
__isl_take isl_schedule_node *node)
{
int is_kernel;
while ((is_kernel = gpu_tree_node_is_kernel(node)) == 0)
node = isl_schedule_node_parent(node);
if (is_kernel < 0)
node = isl_schedule_node_free(node);
return node;
}
/* Move down from the "kernel" mark (or at least a node with schedule
* depth smaller than or equal to "depth") to a band node at schedule
* depth "depth". The "thread" mark is assumed to have a schedule
* depth greater than or equal to "depth". The branch containing the
* "thread" mark is identified by the domain elements in "core".
*
* If the desired schedule depth is in the middle of band node,
* then the band node is split into two pieces, the second piece
* at the desired schedule depth.
*/
__isl_give isl_schedule_node *gpu_tree_move_down_to_depth(
__isl_take isl_schedule_node *node, int depth,
__isl_keep isl_union_set *core)
{
int is_shared;
int is_thread = 0;
while (node && isl_schedule_node_get_schedule_depth(node) < depth) {
if (isl_schedule_node_get_type(node) ==
isl_schedule_node_band) {
int node_depth, node_dim;
node_depth = isl_schedule_node_get_schedule_depth(node);
node_dim = isl_schedule_node_band_n_member(node);
if (node_depth + node_dim > depth)
node = isl_schedule_node_band_split(node,
depth - node_depth);
}
node = core_child(node, core);
}
while ((is_shared = node_is_shared(node)) == 0 &&
(is_thread = node_is_thread(node)) == 0 &&
isl_schedule_node_get_type(node) != isl_schedule_node_band)
node = core_child(node, core);
if (is_shared < 0 || is_thread < 0)
node = isl_schedule_node_free(node);
return node;
}
/* Create a union set containing a single set with a tuple identifier
* called "syncX" and user pointer equal to "kernel".
*/
static __isl_give isl_union_set *create_sync_domain(struct ppcg_kernel *kernel)
{
isl_space *space;
isl_id *id;
char name[40];
space = isl_space_set_alloc(kernel->ctx, 0, 0);
snprintf(name, sizeof(name), "sync%d", kernel->n_sync++);
id = isl_id_alloc(kernel->ctx, name, kernel);
space = isl_space_set_tuple_id(space, isl_dim_set, id);
return isl_union_set_from_set(isl_set_universe(space));
}
/* Is "id" the identifier of a synchronization statement inside "kernel"?
* That is, does its name start with "sync" and does it point to "kernel"?
*/
int gpu_tree_id_is_sync(__isl_keep isl_id *id, struct ppcg_kernel *kernel)
{
const char *name;
name = isl_id_get_name(id);
if (!name)
return 0;
else if (strncmp(name, "sync", 4))
return 0;
return isl_id_get_user(id) == kernel;
}
/* Does "domain" consist of a single set with a tuple identifier
* corresponding to a synchronization for "kernel"?
*/
static int domain_is_sync(__isl_keep isl_union_set *domain,
struct ppcg_kernel *kernel)
{
int is_sync;
isl_id *id;
isl_set *set;
if (isl_union_set_n_set(domain) != 1)
return 0;
set = isl_set_from_union_set(isl_union_set_copy(domain));
id = isl_set_get_tuple_id(set);
is_sync = gpu_tree_id_is_sync(id, kernel);
isl_id_free(id);
isl_set_free(set);
return is_sync;
}
/* Does "node" point to a filter selecting a synchronization statement
* for "kernel"?
*/
static int node_is_sync_filter(__isl_keep isl_schedule_node *node,
struct ppcg_kernel *kernel)
{
int is_sync;
enum isl_schedule_node_type type;
isl_union_set *domain;
if (!node)
return -1;
type = isl_schedule_node_get_type(node);
if (type != isl_schedule_node_filter)
return 0;
domain = isl_schedule_node_filter_get_filter(node);
is_sync = domain_is_sync(domain, kernel);
isl_union_set_free(domain);
return is_sync;
}
/* Is "node" part of a sequence with a previous synchronization statement
* for "kernel"?
* That is, is the parent of "node" a filter such that there is
* a previous filter that picks out exactly such a synchronization statement?
*/
static int has_preceding_sync(__isl_keep isl_schedule_node *node,
struct ppcg_kernel *kernel)
{
int found = 0;
node = isl_schedule_node_copy(node);
node = isl_schedule_node_parent(node);
while (!found && isl_schedule_node_has_previous_sibling(node)) {
node = isl_schedule_node_previous_sibling(node);
if (!node)
break;
found = node_is_sync_filter(node, kernel);
}
if (!node)
found = -1;
isl_schedule_node_free(node);
return found;
}
/* Is "node" part of a sequence with a subsequent synchronization statement
* for "kernel"?
* That is, is the parent of "node" a filter such that there is
* a subsequent filter that picks out exactly such a synchronization statement?
*/
static int has_following_sync(__isl_keep isl_schedule_node *node,
struct ppcg_kernel *kernel)
{
int found = 0;
node = isl_schedule_node_copy(node);
node = isl_schedule_node_parent(node);
while (!found && isl_schedule_node_has_next_sibling(node)) {
node = isl_schedule_node_next_sibling(node);
if (!node)
break;
found = node_is_sync_filter(node, kernel);
}
if (!node)
found = -1;
isl_schedule_node_free(node);
return found;
}
/* Does the subtree rooted at "node" (which is a band node) contain
* any synchronization statement for "kernel" that precedes
* the core computation of "kernel" (identified by the elements
* in kernel->core)?
*/
static int has_sync_before_core(__isl_keep isl_schedule_node *node,
struct ppcg_kernel *kernel)
{
int has_sync = 0;
int is_thread;
node = isl_schedule_node_copy(node);
while ((is_thread = node_is_thread(node)) == 0) {
node = core_child(node, kernel->core);
has_sync = has_preceding_sync(node, kernel);
if (has_sync < 0 || has_sync)
break;
}
if (is_thread < 0 || !node)
has_sync = -1;
isl_schedule_node_free(node);
return has_sync;
}
/* Does the subtree rooted at "node" (which is a band node) contain
* any synchronization statement for "kernel" that follows
* the core computation of "kernel" (identified by the elements
* in kernel->core)?
*/
static int has_sync_after_core(__isl_keep isl_schedule_node *node,
struct ppcg_kernel *kernel)
{
int has_sync = 0;
int is_thread;
node = isl_schedule_node_copy(node);
while ((is_thread = node_is_thread(node)) == 0) {
node = core_child(node, kernel->core);
has_sync = has_following_sync(node, kernel);
if (has_sync < 0 || has_sync)
break;
}
if (is_thread < 0 || !node)
has_sync = -1;
isl_schedule_node_free(node);
return has_sync;
}
/* Insert (or extend) an extension on top of "node" that puts
* a synchronization node for "kernel" before "node".
* Return a pointer to the original node in the updated schedule tree.
*/
static __isl_give isl_schedule_node *insert_sync_before(
__isl_take isl_schedule_node *node, struct ppcg_kernel *kernel)
{
isl_union_set *domain;
isl_schedule_node *graft;
if (!node)
return NULL;
domain = create_sync_domain(kernel);
graft = isl_schedule_node_from_domain(domain);
node = isl_schedule_node_graft_before(node, graft);
return node;
}
/* Insert (or extend) an extension on top of "node" that puts
* a synchronization node for "kernel" afater "node".
* Return a pointer to the original node in the updated schedule tree.
*/
static __isl_give isl_schedule_node *insert_sync_after(
__isl_take isl_schedule_node *node, struct ppcg_kernel *kernel)
{
isl_union_set *domain;
isl_schedule_node *graft;
if (!node)
return NULL;
domain = create_sync_domain(kernel);
graft = isl_schedule_node_from_domain(domain);
node = isl_schedule_node_graft_after(node, graft);
return node;
}
/* Insert an extension on top of "node" that puts a synchronization node
* for "kernel" before "node" unless there already is
* such a synchronization node.
*/
__isl_give isl_schedule_node *gpu_tree_ensure_preceding_sync(
__isl_take isl_schedule_node *node, struct ppcg_kernel *kernel)
{
int has_sync;
has_sync = has_preceding_sync(node, kernel);
if (has_sync < 0)
return isl_schedule_node_free(node);
if (has_sync)
return node;
return insert_sync_before(node, kernel);
}
/* Insert an extension on top of "node" that puts a synchronization node
* for "kernel" after "node" unless there already is
* such a synchronization node.
*/
__isl_give isl_schedule_node *gpu_tree_ensure_following_sync(
__isl_take isl_schedule_node *node, struct ppcg_kernel *kernel)
{
int has_sync;
has_sync = has_following_sync(node, kernel);
if (has_sync < 0)
return isl_schedule_node_free(node);
if (has_sync)
return node;
return insert_sync_after(node, kernel);
}
/* Insert an extension on top of "node" that puts a synchronization node
* for "kernel" after "node" unless there already is such a sync node or
* "node" itself already * contains a synchronization node following
* the core computation of "kernel".
*/
__isl_give isl_schedule_node *gpu_tree_ensure_sync_after_core(
__isl_take isl_schedule_node *node, struct ppcg_kernel *kernel)
{
int has_sync;
has_sync = has_sync_after_core(node, kernel);
if (has_sync < 0)
return isl_schedule_node_free(node);
if (has_sync)
return node;
has_sync = has_following_sync(node, kernel);
if (has_sync < 0)
return isl_schedule_node_free(node);
if (has_sync)
return node;
return insert_sync_after(node, kernel);
}
/* Move left in the sequence on top of "node" to a synchronization node
* for "kernel".
* If "node" itself contains a synchronization node preceding
* the core computation of "kernel", then return "node" itself.
* Otherwise, if "node" does not have a preceding synchronization node,
* then create one first.
*/
__isl_give isl_schedule_node *gpu_tree_move_left_to_sync(
__isl_take isl_schedule_node *node, struct ppcg_kernel *kernel)
{
int has_sync;
int is_sync;
has_sync = has_sync_before_core(node, kernel);
if (has_sync < 0)
return isl_schedule_node_free(node);
if (has_sync)
return node;
node = gpu_tree_ensure_preceding_sync(node, kernel);
node = isl_schedule_node_parent(node);
while ((is_sync = node_is_sync_filter(node, kernel)) == 0)
node = isl_schedule_node_previous_sibling(node);
if (is_sync < 0)
node = isl_schedule_node_free(node);
node = isl_schedule_node_child(node, 0);
return node;
}
/* Move right in the sequence on top of "node" to a synchronization node
* for "kernel".
* If "node" itself contains a synchronization node following
* the core computation of "kernel", then return "node" itself.
* Otherwise, if "node" does not have a following synchronization node,
* then create one first.
*/
__isl_give isl_schedule_node *gpu_tree_move_right_to_sync(
__isl_take isl_schedule_node *node, struct ppcg_kernel *kernel)
{
int has_sync;
int is_sync;
has_sync = has_sync_after_core(node, kernel);
if (has_sync < 0)
return isl_schedule_node_free(node);
if (has_sync)
return node;
node = gpu_tree_ensure_following_sync(node, kernel);
node = isl_schedule_node_parent(node);
while ((is_sync = node_is_sync_filter(node, kernel)) == 0)
node = isl_schedule_node_next_sibling(node);
if (is_sync < 0)
node = isl_schedule_node_free(node);
node = isl_schedule_node_child(node, 0);
return node;
}
|