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
|
/*
* Copyright © 2019 Broadcom
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "util/set.h"
#include "util/dag.h"
#include <stdio.h>
static void
append_edge(struct dag_node *parent, struct dag_node *child, uintptr_t data)
{
/* Remove the child as a DAG head. */
list_delinit(&child->link);
struct dag_edge edge = {
.child = child,
.data = data,
};
util_dynarray_append(&parent->edges, struct dag_edge, edge);
child->parent_count++;
}
/**
* Adds a directed edge from the parent node to the child.
*
* Both nodes should have been initialized with dag_init_node(). The edge
* list may contain multiple edges to the same child with different data.
*/
void
dag_add_edge(struct dag_node *parent, struct dag_node *child, uintptr_t data)
{
util_dynarray_foreach(&parent->edges, struct dag_edge, edge) {
if (edge->child == child && edge->data == data)
return;
}
append_edge(parent, child, data);
}
/**
* Adds a directed edge from the parent node to the child.
*
* Both nodes should have been initialized with dag_init_node(). If there is
* already an existing edge, the data is updated to the maximum of the
* previous data and the new data. This is useful if the data represents a
* delay.
*/
void
dag_add_edge_max_data(struct dag_node *parent, struct dag_node *child,
uintptr_t data)
{
util_dynarray_foreach(&parent->edges, struct dag_edge, edge) {
if (edge->child == child) {
edge->data = MAX2(edge->data, data);
return;
}
}
append_edge(parent, child, data);
}
/* Removes a single edge from the graph, promoting the child to a DAG head.
*
* Note that calling this other than through dag_prune_head() means that you
* need to be careful when iterating the edges of remaining nodes for NULL
* children.
*/
void
dag_remove_edge(struct dag *dag, struct dag_edge *edge)
{
if (!edge->child)
return;
struct dag_node *child = edge->child;
child->parent_count--;
if (child->parent_count == 0)
list_addtail(&child->link, &dag->heads);
edge->child = NULL;
edge->data = 0;
}
/**
* Removes a DAG head from the graph, and moves any new dag heads into the
* heads list.
*/
void
dag_prune_head(struct dag *dag, struct dag_node *node)
{
assert(!node->parent_count);
list_delinit(&node->link);
util_dynarray_foreach(&node->edges, struct dag_edge, edge) {
dag_remove_edge(dag, edge);
}
}
/**
* Initializes DAG node (probably embedded in some other datastructure in the
* user).
*/
void
dag_init_node(struct dag *dag, struct dag_node *node)
{
util_dynarray_init(&node->edges, dag);
list_addtail(&node->link, &dag->heads);
}
struct dag_traverse_bottom_up_state {
struct set *seen;
void (*cb)(struct dag_node *node, void *data);
void *data;
};
static void
dag_traverse_bottom_up_node(struct dag_node *node,
struct dag_traverse_bottom_up_state *state)
{
if (_mesa_set_search(state->seen, node))
return;
struct util_dynarray stack;
util_dynarray_init(&stack, NULL);
do {
assert(node);
while (node->edges.size != 0) {
util_dynarray_append(&stack, struct dag_node *, node);
/* Push unprocessed children onto stack in reverse order. Note that
* it's possible for any of the children nodes to already be on the
* stack.
*/
util_dynarray_foreach_reverse(&node->edges, struct dag_edge, edge) {
if (!_mesa_set_search(state->seen, edge->child)) {
util_dynarray_append(&stack, struct dag_node *, edge->child);
}
}
/* Get last element pushed: either left-most child or current node.
* If it's the current node, that means that we've processed all its
* children already.
*/
struct dag_node *top = util_dynarray_pop(&stack, struct dag_node *);
if (top == node)
break;
node = top;
}
/* Process the node */
state->cb(node, state->data);
_mesa_set_add(state->seen, node);
/* Find the next unprocessed node in the stack */
do {
node = NULL;
if (stack.size == 0)
break;
node = util_dynarray_pop(&stack, struct dag_node *);
} while (_mesa_set_search(state->seen, node));
} while (node);
util_dynarray_fini(&stack);
}
/**
* Walks the DAG from leaves to the root, ensuring that each node is only seen
* once its children have been, and each node is only traversed once.
*/
void
dag_traverse_bottom_up(struct dag *dag, void (*cb)(struct dag_node *node,
void *data), void *data)
{
struct dag_traverse_bottom_up_state state = {
.seen = _mesa_pointer_set_create(NULL),
.data = data,
.cb = cb,
};
list_for_each_entry(struct dag_node, node, &dag->heads, link) {
dag_traverse_bottom_up_node(node, &state);
}
ralloc_free(state.seen);
}
/**
* Creates an empty DAG datastructure.
*/
struct dag *
dag_create(void *mem_ctx)
{
struct dag *dag = rzalloc(mem_ctx, struct dag);
list_inithead(&dag->heads);
return dag;
}
struct dag_validate_state {
struct util_dynarray stack;
struct set *stack_set;
struct set *seen;
void (*cb)(const struct dag_node *node, void *data);
void *data;
};
static void
dag_validate_node(struct dag_node *node,
struct dag_validate_state *state)
{
if (_mesa_set_search(state->stack_set, node)) {
fprintf(stderr, "DAG validation failed at:\n");
fprintf(stderr, " %p: ", node);
state->cb(node, state->data);
fprintf(stderr, "\n");
fprintf(stderr, "Nodes in stack:\n");
util_dynarray_foreach(&state->stack, struct dag_node *, nodep) {
struct dag_node *node = *nodep;
fprintf(stderr, " %p: ", node);
state->cb(node, state->data);
fprintf(stderr, "\n");
}
abort();
}
if (_mesa_set_search(state->seen, node))
return;
_mesa_set_add(state->stack_set, node);
_mesa_set_add(state->seen, node);
util_dynarray_append(&state->stack, struct dag_node *, node);
util_dynarray_foreach(&node->edges, struct dag_edge, edge) {
dag_validate_node(edge->child, state);
}
(void)util_dynarray_pop(&state->stack, struct dag_node *);
_mesa_set_remove_key(state->stack_set, node);
}
void
dag_validate(struct dag *dag, void (*cb)(const struct dag_node *node,
void *data),
void *data)
{
void *mem_ctx = ralloc_context(NULL);
struct dag_validate_state state = {
.stack_set = _mesa_pointer_set_create(mem_ctx),
.seen = _mesa_pointer_set_create(mem_ctx),
.cb = cb,
.data = data,
};
util_dynarray_init(&state.stack, mem_ctx);
list_validate(&dag->heads);
list_for_each_entry(struct dag_node, node, &dag->heads, link) {
dag_validate_node(node, &state);
}
ralloc_free(mem_ctx);
}
|