-
Notifications
You must be signed in to change notification settings - Fork 1
/
dependence_analysis.py
495 lines (426 loc) · 18.9 KB
/
dependence_analysis.py
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
from z3 import Solver, Ints, unsat, Optimize, sat, Int
from loguru import logger
from pattern_ast import get_accesses, Program, AbstractLoop, Access, Op, Literal, Hex, gather_surrounding_loops, gather_loop_shapes, gather_loop_vars, Assignment
from dependence_graph import Dependence, DependenceGraph
def is_ordered(l, v1, v2):
return l.index(v1) < l.index(v2)
# Assign node ids to the body of the programs (declarations aren't assigned anything )
def assign_node_ids(program):
class CurrentId:
def __init__(self):
self.id = 0
def next(self):
self.id += 1
return self.id
current_id = CurrentId()
def assign(node):
if type(node) not in [AbstractLoop, Program, Assignment, Access, Op, Literal, Hex]:
raise RuntimeError('assign: Unhandled type of node ' + str(type(node)))
node.attributes['node_id'] = current_id.next()
if isinstance(node, AbstractLoop):
for loop_shape in node.loop_shapes:
assign(loop_shape.loop_var)
assign(loop_shape.greater_eq)
for expr in loop_shape.less_eq:
assign(expr)
assign(loop_shape.step)
for stmt in node.body:
assign(stmt)
elif isinstance(node, Program):
for stmt in node.body:
assign(stmt)
elif isinstance(node, Assignment):
assign(node.lhs)
assign(node.rhs)
elif isinstance(node, Access):
for index in node.indices:
assign(index)
elif isinstance(node, Op):
for arg in node.args:
assign(arg)
program_w_attributes = program.clone()
assign(program_w_attributes)
return program_w_attributes
def analyze_dependence(program):
program_w_attributes = assign_node_ids(program)
scalar_cvars = generate_scalar_constraint_vars(program.decls)
graph = DependenceGraph()
for (ref1, ref2) in iterate_unique_reference_pairs(program_w_attributes):
for dependence_dv, model in iterate_dependence_direction_vectors(ref1, ref2, scalar_cvars):
for ref1_then_ref2_dv in iterate_execution_order_direction_vector(ref1, ref2):
logger.debug(f'Testing:\n'
f'{ref1.pprint()} -> {ref2.pprint()}:, '
f'dep({dependence_dv}) exe_order({ref1_then_ref2_dv})')
dv1 = calculate_valid_direction_vector(dependence_dv,
ref1_then_ref2_dv)
if dv1 is not None:
logger.debug(f'Valid direction vector: {dv1}\nExample:\n{model}')
graph.add(ref1, ref2, dv1)
dependence_dv_inv = negate_direction_vector(dependence_dv)
for ref2_then_ref1_dv in iterate_execution_order_direction_vector(ref2, ref1):
logger.debug(f'Testing:\n'
f'{ref2.pprint()} -> {ref1.pprint()}:, '
f'dep({dependence_dv_inv}) exe_order({ref2_then_ref1_dv})')
dv2 = calculate_valid_direction_vector(dependence_dv_inv,
ref2_then_ref1_dv)
if dv2 is not None:
logger.debug(f'Valid direction vector: {dv2}\nExample (source sink negated):\n{model}')
graph.add(ref2, ref1, dv2)
return graph, program_w_attributes
def iterate_unique_reference_pairs(program):
refs = list(get_accesses(program))
n_refs = len(refs)
for i in range(0, n_refs):
for j in range(i, n_refs):
ref1 = refs[i]
ref2 = refs[j]
if ref1.var != ref2.var:
continue
if not ref1.is_write and not ref2.is_write:
continue
yield (ref1, ref2)
def iterate_execution_order_direction_vector(source_ref, sink_ref):
source_stmt = source_ref.parent_stmt
source_loops = gather_surrounding_loops(source_stmt)
source_trace = source_loops + [source_stmt]
sink_stmt = sink_ref.parent_stmt
sink_loops = gather_surrounding_loops(sink_stmt)
sink_trace = sink_loops + [sink_stmt]
logger.debug(f'debugging execution order dv\n'
f'source: {source_trace}\n'
f'sink: {sink_trace}')
common_parents = get_common_prefix(source_loops, sink_loops)
n_common_parents = len(common_parents)
assert(n_common_parents > 0)
n_common_loops = 0
for parent in common_parents:
if isinstance(parent, AbstractLoop):
n_common_loops += len(parent.loop_shapes)
common_ancestor = common_parents[-1]
source_first_diff = source_trace[n_common_parents]
sink_first_diff = sink_trace[n_common_parents]
if isinstance(common_ancestor, Program):
if not is_ordered(common_ancestor.body,
source_first_diff,
sink_first_diff):
yield None
else:
yield []
else:
assert(isinstance(common_ancestor, AbstractLoop))
if is_ordered(common_ancestor.body,
source_first_diff,
sink_first_diff):
yield ['<='] + ['<=>'] * (n_common_loops - 1)
else:
for lt_position in range(n_common_loops):
leading_eqs = ['='] * lt_position
trailing_stars = ['<=>'] * (n_common_loops - 1 - lt_position)
yield leading_eqs + ['<'] + trailing_stars
def gather_loop_steps(loop_shapes):
steps = []
for shape in loop_shapes:
assert(type(shape.step) == Literal)
assert(shape.step.ty == int)
steps.append(shape.step.val)
return steps
def get_common_prefix(l1, l2):
common = []
for v1, v2 in zip(l1, l2):
if v1 != v2:
break
common.append(v1)
return common
def iterate_dependence_direction_vectors(source_ref, sink_ref, extra_cvars=None, extra_constraints=None):
extra_cvars = {} if extra_cvars is None else extra_cvars
extra_constraints = [] if extra_constraints is None else extra_constraints
source_loops = gather_surrounding_loops(source_ref.parent_stmt)
source_loop_shapes = gather_loop_shapes(source_loops)
source_loop_vars = gather_loop_vars(source_loop_shapes)
sink_loops = gather_surrounding_loops(sink_ref.parent_stmt)
sink_loop_shapes = gather_loop_shapes(sink_loops)
sink_loop_vars = gather_loop_vars(sink_loop_shapes)
source_cvars, sink_cvars, source_step_cvars, sink_step_cvars \
= generate_constraint_vars(source_loop_vars,
sink_loop_vars)
constraints = extra_constraints
merged_source_cvars = {**source_cvars, **extra_cvars}
constraints += generate_loop_bound_constraints(source_loop_shapes,
merged_source_cvars)
constraints += generate_step_constraints(source_loop_shapes,
merged_source_cvars,
source_step_cvars)
merged_sink_cvars = {**sink_cvars, **extra_cvars}
constraints += generate_loop_bound_constraints(sink_loop_shapes,
merged_sink_cvars)
constraints += generate_step_constraints(sink_loop_shapes,
merged_sink_cvars,
sink_step_cvars)
constraints += generate_subscript_equality_constraints(source_ref, merged_source_cvars,
sink_ref, merged_sink_cvars)
def iterate_recursive(constraints, remaining_loop_vars, accumulated_dv):
n_dimensions_left = len(remaining_loop_vars)
if n_dimensions_left == 0:
model = solve(constraints)
if model is not None:
yield accumulated_dv, model
else:
# If we don't add additional constraints on the direction
# relation between source and sink vars, it means the rest of
# the dimensions are *s.
if solve(constraints):
source_cvar = merged_source_cvars[remaining_loop_vars[0]]
sink_cvar = merged_sink_cvars[remaining_loop_vars[0]]
yield from iterate_recursive(constraints + [source_cvar < sink_cvar],
remaining_loop_vars[1:],
accumulated_dv + ['<'])
yield from iterate_recursive(constraints + [source_cvar == sink_cvar],
remaining_loop_vars[1:],
accumulated_dv + ['='])
yield from iterate_recursive(constraints + [source_cvar > sink_cvar],
remaining_loop_vars[1:],
accumulated_dv + ['>'])
common_loops = get_common_prefix(source_loops, sink_loops)
common_loop_shapes = gather_loop_shapes(common_loops)
common_loop_vars = gather_loop_vars(common_loop_shapes)
yield from iterate_recursive(constraints, common_loop_vars, [])
def generate_constraint_vars(source_loop_vars, sink_loop_vars):
source_cvars = {}
sink_cvars = {}
source_step_cvars = {}
sink_step_cvars = {}
for v in source_loop_vars:
source_cvars[v] = Int(f'{v}_value_source')
source_step_cvars[v] = Int(f'{v}_which_iteration_source')
for v in sink_loop_vars:
sink_cvars[v] = Int(f'{v}_value_sink')
sink_step_cvars[v] = Int(f'{v}_which_iteration_sink')
return (source_cvars, sink_cvars, source_step_cvars, sink_step_cvars)
def generate_loop_bound_constraints(loop_shapes, cvars):
constraints = []
for shape in loop_shapes:
assert(type(shape.loop_var) == Access)
v = shape.loop_var.var
cvar = cvars[v]
begin = expr_to_cexpr(shape.greater_eq, cvars)
constraints.append(begin <= cvar)
for expr in shape.less_eq:
end = expr_to_cexpr(expr, cvars)
constraints.append(cvar <= end)
return constraints
def generate_step_constraints(loop_shapes, cvars, step_cvars):
constraints = []
for shape in loop_shapes:
assert(type(shape.loop_var) == Access)
v = shape.loop_var.var
assert(type(shape.step) == Literal)
assert(shape.step.ty == int)
cvar = cvars[v]
step_cvar = step_cvars[v]
step = shape.step.val
begin = expr_to_cexpr(shape.greater_eq, cvars)
constraints += [cvar == step*step_cvar + begin]
return constraints
def affine_to_cexpr(affine, cvars):
if not affine.var:
return affine.offset
return affine.coeff * cvars[affine.var] + affine.offset
def expr_to_cexpr(expr, cvars):
if type(expr) == Op:
args = expr.args
op = expr.op
if len(args) == 1:
cexpr = expr_to_cexpr(args[0], cvars)
if expr.op == '+':
return cexpr
elif expr.op == '-':
return -cexpr
elif len(args) == 2:
left = expr_to_cexpr(args[0], cvars)
right = expr_to_cexpr(args[1], cvars)
if left is not None and right is not None:
if op == '+':
return left + right
elif op == '*':
return left * right
elif op == '-':
return left - right
elif op == '/':
return left / right
elif type(expr) == Literal:
if expr.ty == int:
return expr.val
elif type(expr) == Access:
if expr.is_scalar() and expr.var in cvars:
return cvars[expr.var]
return None
def generate_scalar_constraint_vars(decls):
return {decl.name:Int(decl.name) for decl in decls}
def generate_subscript_equality_constraints(source_ref, source_cvars, sink_ref, sink_cvars):
constraints = []
assert(len(source_ref.indices) == len(sink_ref.indices))
if len(source_ref.indices) == 0:
# Not adding anything means that the constraints will vacuously be satisfied
# This is true because scalar variables always share the same memory in
# all iterations.
pass
else:
for (source_affine, sink_affine) in zip(source_ref.indices, sink_ref.indices):
source_cexpr = expr_to_cexpr(source_affine, source_cvars)
sink_cexpr = expr_to_cexpr(sink_affine, sink_cvars)
# If either is None, it means that we won't be able to determine whether
# the indices overlap or not. For example, A[A[i]] or A[1.5] vs anything.
# Since we don't know their intersection, we conservatively assume they
# overlap by not adding any constraints. (It will be treated as true.)
if source_cexpr is not None and sink_cexpr is not None:
constraints += [source_cexpr == sink_cexpr]
return constraints
def solve(constraints):
solver = Solver()
solver.add(constraints)
status = solver.check()
if status == sat:
return solver.model()
return None
def calculate_valid_direction_vector(dependence_dv, execution_order_dv):
if execution_order_dv is None:
return None
# Requirement 1: dependence goes from a statement executed before to one executed later
dv = intersect_direction_vector(dependence_dv, execution_order_dv)
for d in dv:
if d == '':
return None
# Requirement 2: dependence goes from source to sink (i.e., leftmost non = is <)
for d in dv:
if d == '>':
return None
if d == '<':
return dv
return dv
def intersect_direction_vector(dv1, dv2):
return [intersect_direction(d1, d2) for (d1, d2) in zip(dv1, dv2)]
def intersect_direction(d1, d2):
lt = '<' in d1 and '<' in d2
eq = '=' in d1 and '=' in d2
gt = '>' in d1 and '>' in d2
return ('<' if lt else ''
'=' if eq else ''
'>' if gt else '')
def negate_direction_vector(dv):
return [negate_direction(d) for d in dv]
def negate_direction(d):
lt = '<' in d
eq = '=' in d
gt = '>' in d
return ('<' if gt else ''
'=' if eq else ''
'>' if lt else '')
def find_min(constraints, expr, default_min=0):
if type(expr) == int:
return expr
constraint_strs = [f'{c}' for c in constraints]
min_optimize = Optimize()
min_optimize.set('timeout', 10000)
min_optimize.assert_exprs(*constraints)
min_optimize.minimize(expr)
status = min_optimize.check()
if status != sat:
print(f'Unable to find min ({status}) for:\n' + '\n'.join(constraint_strs))
return None
min_val = min_optimize.model().eval(expr).as_long()
# Make sure it's actually the min, since z3 has a bug
# https://github.com/Z3Prover/z3/issues/4670
solver = Solver()
solver.set('timeout', 10000)
solver.add(constraints + [expr < min_val])
status = solver.check()
if status != unsat:
print(f'Z3 bug\nFind min ({expr}) => {min_val} with status ({status}):\n' + '\n'.join(constraint_strs))
return None
return min_val
def get_min_distance(dep):
source_ref = dep.source_ref
source_loops = gather_surrounding_loops(source_ref.parent_stmt)
source_loop_shapes = gather_loop_shapes(source_loops)
source_loop_vars = gather_loop_vars(source_loop_shapes)
sink_ref = dep.sink_ref
sink_loops = gather_surrounding_loops(sink_ref.parent_stmt)
sink_loop_shapes = gather_loop_shapes(sink_loops)
sink_loop_vars = gather_loop_vars(sink_loop_shapes)
source_cvars, sink_cvars, source_step_cvars, sink_step_cvars \
= generate_constraint_vars(source_loop_vars,
sink_loop_vars)
constraints = []
constraints += generate_loop_bound_constraints(source_loop_shapes,
source_cvars)
# source_steps = gather_loop_steps(source_loop_shapes)
constraints += generate_step_constraints(source_loop_shapes,
source_cvars,
source_step_cvars)
constraints += generate_loop_bound_constraints(sink_loop_shapes,
sink_cvars)
# sink_steps = gather_loop_steps(sink_loop_shapes)
constraints += generate_step_constraints(sink_loop_shapes,
sink_cvars,
sink_step_cvars)
constraints += generate_subscript_equality_constraints(source_ref, source_cvars,
sink_ref, sink_cvars)
common_loops = get_common_prefix(source_loops, sink_loops)
common_loop_shapes = gather_loop_shapes(common_loops)
common_loop_vars = gather_loop_vars(common_loop_shapes)
direction_constraints = []
for loop_var, direction in zip(common_loop_vars, dep.direction_vector):
if direction == '<':
c = source_cvars[loop_var] < sink_cvars[loop_var]
elif direction == '>':
c = source_cvars[loop_var] > sink_cvars[loop_var]
else:
c = source_cvars[loop_var] == sink_cvars[loop_var]
direction_constraints.append(c)
constraints += direction_constraints
widths = []
for shape in common_loop_shapes:
if type(shape.greater_eq) != Literal:
return False
min_less_eq = None
for expr in shape.less_eq:
if type(expr) != Literal:
return False
if min_less_eq is None:
min_less_eq = expr.val
min_less_eq = min(min_less_eq, expr.val)
width = min_less_eq - shape.greater_eq.val + 1
widths.append(width)
strides = [1]
for previous_index, width in enumerate(reversed(widths[1:])):
strides.append(strides[previous_index] * width)
strides.reverse()
cexpr = None
for dim, loop_var in enumerate(common_loop_vars):
sink_cvar = sink_cvars[loop_var]
source_cvar = source_cvars[loop_var]
dim_cexpr = (sink_cvar - source_cvar) * strides[dim]
if cexpr is None:
cexpr = dim_cexpr
else:
cexpr += dim_cexpr
min_val = find_min(constraints, cexpr)
if min_val is None:
return False
min_val_dimensions = []
remaining = min_val
for s in strides:
min_val_dimensions.append(remaining // s)
remaining = remaining % s
dep.distance_vector = min_val_dimensions
dep.loop_vars = common_loop_vars
distances = {loop_var:distance for loop_var, distance in zip(common_loop_vars, min_val_dimensions)}
# if min_val is not None:
# assert(loop_var not in distances)
# # distances[loop_var] = min_val
# distances[loop_var] = abs(min_val)
return True
def calculate_distance_vectors(graph):
for deps in graph.iterate_dependences():
for dep in deps:
get_min_distance(dep)