-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtransformations.py
More file actions
650 lines (588 loc) · 23.4 KB
/
Copy pathtransformations.py
File metadata and controls
650 lines (588 loc) · 23.4 KB
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
641
642
643
644
645
646
647
648
649
650
from collections import defaultdict
from typing import Set, Dict, List, Tuple, Optional, Mapping, Iterator
from numba_rvsdg.core.datastructures.scfg import SCFG
from numba_rvsdg.core.datastructures.basic_block import (
BasicBlock,
SyntheticAssignment,
SyntheticBranch,
SyntheticExitingLatch,
SyntheticExitBranch,
RegionBlock,
)
from numba_rvsdg.core.datastructures import block_names
# from numba_rvsdg.core.utils import _logger
def loop_restructure_helper(scfg: SCFG, loop: Set[str]) -> None:
"""Loop Restructuring
Applies the algorithm LOOP RESTRUCTURING from section 4.1 of Bahmann2015.
Note that this will modify both the `scfg` and the `loop` in-place.
Parameters
----------
scfg: SCFG
The SCFG containing the loop
loop: Set[str]
The loop (strongly connected components) that is to be restructured
"""
headers, entries = scfg.find_headers_and_entries(loop)
exiting_blocks, exit_blocks = scfg.find_exiting_and_exits(loop)
# assert len(entries) == 1
headers_were_unified = False
# If there are multiple headers, insert assignment and control blocks,
# such that only a single loop header remains.
loop_head = None
if len(headers) > 1:
headers_were_unified = True
solo_head_name = scfg.name_gen.new_block_name(block_names.SYNTH_HEAD)
scfg.insert_block_and_control_blocks(solo_head_name, entries, headers)
loop.add(solo_head_name)
loop_head = solo_head_name
else:
loop_head = next(iter(headers))
# If there is only a single exiting latch (an exiting block that also has a
# backedge to the loop header) we can exit early, since the condition for
# SCFG is fullfilled.
backedge_blocks = [
block
for block in loop
if set(headers).intersection(scfg[block].jump_targets)
]
if (
len(backedge_blocks) == 1
and len(exiting_blocks) == 1
and backedge_blocks[0] == next(iter(exiting_blocks))
):
scfg.add_block(
scfg.graph.pop(backedge_blocks[0]).declare_backedge(loop_head)
)
return
# The synthetic exiting latch and synthetic exit need to be created
# based on the state of the cfg. If there are multiple exits, we need a
# SyntheticExit, otherwise we only need a SyntheticExitingLatch
synth_exiting_latch = scfg.name_gen.new_block_name(
block_names.SYNTH_EXIT_LATCH
)
# Set a flag, this will determine the variable assignment and block
# insertion later on
needs_synth_exit = len(exit_blocks) > 1
if needs_synth_exit:
synth_exit = scfg.name_gen.new_block_name(block_names.SYNTH_EXIT)
# This sets up the various control variables.
# If there were multiple headers, we must re-use the variable that was used
# for looping as the exit variable
if headers_were_unified:
bb_branch = scfg[solo_head_name]
assert isinstance(bb_branch, SyntheticBranch)
exit_variable = bb_branch.variable
else:
exit_variable = scfg.name_gen.new_var_name("exit")
# This variable denotes the backedge
backedge_variable = scfg.name_gen.new_var_name("backedge")
# Now we setup the lookup tables for the various control variables,
# depending on the state of the CFG and what is needed
exit_value_table = {i: j for i, j in enumerate(exit_blocks)}
if needs_synth_exit:
backedge_value_table = {
i: j for i, j in enumerate((loop_head, synth_exit))
}
else:
backedge_value_table = {
i: j for i, j in enumerate((loop_head, next(iter(exit_blocks))))
}
if headers_were_unified:
bb_branch = scfg[solo_head_name]
assert isinstance(bb_branch, SyntheticBranch)
header_value_table = bb_branch.branch_value_table
else:
header_value_table = {}
# This does a dictionary reverse lookup, to determine the key for a given
# value.
def reverse_lookup(d: Mapping[int, str], value: str) -> int:
for k, v in d.items():
if v == value:
return k
else:
return -1
# Now that everything is in place, we can start to insert blocks, depending
# on what is needed
# All new blocks are recorded for later insertion into the loop set
new_blocks = set()
doms = _doms(scfg)
# For every block in the loop:
for name in loop:
# If the block is an exiting block or a backedge block
if name in exiting_blocks or name in backedge_blocks:
# Copy the jump targets, these will be modified
new_jt = list(scfg[name].jump_targets)
# For each jump_target in the blockj
for jt in scfg[name].jump_targets:
# If the target is an exit block
if jt in exit_blocks:
# Create a new assignment name and record it
synth_assign = scfg.name_gen.new_block_name(
block_names.SYNTH_ASSIGN
)
new_blocks.add(synth_assign)
# Setup the table for the variable assignment
variable_assignment = {}
# Setup the variables in the assignment table to point to
# the correct blocks
if needs_synth_exit:
variable_assignment[exit_variable] = reverse_lookup(
exit_value_table, jt
)
variable_assignment[backedge_variable] = reverse_lookup(
backedge_value_table,
synth_exit
if needs_synth_exit
else next(iter(exit_blocks)),
)
# Create the actual control variable block
synth_assign_block = SyntheticAssignment(
name=synth_assign,
_jump_targets=(synth_exiting_latch,),
backedges=(),
variable_assignment=variable_assignment,
)
# Insert the assignment to the scfg
scfg.add_block(synth_assign_block)
# Insert the new block into the new jump_targets making
# sure, that it replaces the correct jump_target, order
# matters in this case.
new_jt[new_jt.index(jt)] = synth_assign
# If the target is the loop_head
elif jt in headers and (name not in doms[jt] or name == jt):
# Create the assignment and record it
synth_assign = scfg.name_gen.new_block_name(
block_names.SYNTH_ASSIGN
)
new_blocks.add(synth_assign)
# Setup the variables in the assignment table to point to
# the correct blocks
variable_assignment = {}
variable_assignment[backedge_variable] = reverse_lookup(
backedge_value_table, loop_head
)
if needs_synth_exit or headers_were_unified:
variable_assignment[exit_variable] = reverse_lookup(
header_value_table, jt
)
# Update the backedge block - remove any existing backedges
# that point to the headers, no need to add a backedge,
# since it will be contained in the SyntheticExitingLatch
# later on.
block = scfg.graph.pop(name)
jts = list(block.jump_targets)
for h in headers:
if h in jts:
jts.remove(h)
scfg.add_block(
block.replace_jump_targets(jump_targets=tuple(jts))
)
# Setup the assignment block and initialize it with the
# correct jump_targets and variable assignment.
synth_assign_block = SyntheticAssignment(
name=synth_assign,
_jump_targets=(synth_exiting_latch,),
backedges=(),
variable_assignment=variable_assignment,
)
# Add the new block to the SCFG
scfg.add_block(synth_assign_block)
# Update the jump targets again, order matters
new_jt[new_jt.index(jt)] = synth_assign
# finally, replace the jump_targets for this block with the new
# ones
scfg.add_block(
scfg.graph.pop(name).replace_jump_targets(
jump_targets=tuple(new_jt)
)
)
# Add any new blocks to the loop.
loop.update(new_blocks)
# Insert the exiting latch, add it to the loop and to the graph.
synth_exiting_latch_block = SyntheticExitingLatch(
name=synth_exiting_latch,
_jump_targets=(
synth_exit if needs_synth_exit else next(iter(exit_blocks)),
loop_head,
),
backedges=(loop_head,),
variable=backedge_variable,
branch_value_table=backedge_value_table,
)
loop.add(synth_exiting_latch)
scfg.add_block(synth_exiting_latch_block)
# If an exit is to be created, we do so too, but only add it to the scfg,
# since it isn't part of the loop
if needs_synth_exit:
synth_exit_block = SyntheticExitBranch(
name=synth_exit,
_jump_targets=tuple(exit_blocks),
backedges=(),
variable=exit_variable,
branch_value_table=exit_value_table,
)
scfg.add_block(synth_exit_block)
def restructure_loop(parent_region: RegionBlock) -> None:
"""Inplace restructuring of the given graph to extract loops using
strongly-connected components
"""
assert parent_region.subregion is not None
scfg = parent_region.subregion
# obtain a List of Sets of names, where all names in each set are strongly
# connected, i.e. all reachable from one another by traversing the subset
scc: List[Set[str]] = scfg.compute_scc()
# loops are defined as strongly connected subsets who have more than a
# single name and single name loops that point back to to themselves.
loops: List[Set[str]] = [
nodes
for nodes in scc
if len(nodes) > 1
or next(iter(nodes)) in scfg[next(iter(nodes))].jump_targets
]
# _logger.debug(
# "restructure_loop found %d loops in %s",
# len(loops),
# scfg.graph.keys()
# )
# rotate and extract loop
for loop in loops:
loop_restructure_helper(scfg, loop)
extract_region(scfg, loop, "loop", parent_region)
def find_head_blocks(scfg: SCFG, begin: str) -> Set[str]:
head = scfg.find_head()
head_region_blocks = set()
current_block = head
# Start at the head block and traverse the graph linearly until
# reaching the begin block.
while True:
head_region_blocks.add(current_block)
if current_block == begin:
break
else:
jt = scfg.graph[current_block].jump_targets
assert len(jt) == 1
current_block = next(iter(jt))
return head_region_blocks
def find_branch_regions(
scfg: SCFG, begin: str, end: str
) -> List[Optional[Tuple[str, Set[str]]]]:
# identify branch regions
doms = _doms(scfg)
branch_regions: List[Optional[Tuple[str, Set[str]]]] = []
jump_targets = scfg.graph[begin].jump_targets
for bra_start in jump_targets:
for jt in jump_targets:
if jt != bra_start and scfg.is_reachable_dfs(jt, bra_start):
# placeholder for empty branch region
branch_regions.append(None)
break
else:
sub_keys: Set[str] = set()
branch_regions.append((bra_start, sub_keys))
# a node is part of the branch if
# - the start of the branch is a dominator; and,
# - the tail of the branch is not a dominator
for k, kdom in doms.items():
if bra_start in kdom and end not in kdom:
sub_keys.add(k)
return branch_regions
def find_tail_blocks(
scfg: SCFG,
begin: str,
head_region_blocks: Set[str],
branch_regions: List[Optional[Tuple[str, Set[str]]]],
) -> Set[str]:
tail_subregion = {b for b in scfg.graph.keys()}
tail_subregion.difference_update(head_region_blocks)
for reg in branch_regions:
if reg is None:
# empty branch region
continue
b, sub = reg
tail_subregion.discard(b)
for s in sub:
tail_subregion.discard(s)
# exclude parents
tail_subregion.discard(begin)
return tail_subregion
def update_exiting(
region_block: RegionBlock, new_region_header: str, new_region_name: str
) -> RegionBlock:
# Recursively updates the exiting blocks of a regionblock
region_exiting = region_block.exiting
assert region_block.subregion is not None
region_exiting_block: BasicBlock = region_block.subregion.graph.pop(
region_exiting
)
jt = list(region_exiting_block._jump_targets)
for idx, s in enumerate(jt):
if s is new_region_header:
jt[idx] = new_region_name
region_exiting_block = region_exiting_block.replace_jump_targets(
jump_targets=tuple(jt)
)
be = list(region_exiting_block.backedges)
for idx, s in enumerate(be):
if s is new_region_header:
be[idx] = new_region_name
region_exiting_block = region_exiting_block.replace_backedges(
backedges=tuple(be)
)
if isinstance(region_exiting_block, RegionBlock):
region_exiting_block = update_exiting(
region_exiting_block, new_region_header, new_region_name
)
region_block.subregion.add_block(region_exiting_block)
return region_block
def extract_region(
scfg: SCFG,
region_blocks: Set[str],
region_kind: str,
parent_region: RegionBlock,
) -> None:
headers, entries = scfg.find_headers_and_entries(region_blocks)
exiting_blocks, exit_blocks = scfg.find_exiting_and_exits(region_blocks)
assert len(headers) == 1
assert len(exiting_blocks) == 1
region_header = next(iter(headers))
region_exiting = next(iter(exiting_blocks))
# Generate a new region name
region_name = scfg.name_gen.new_region_name(region_kind)
head_subgraph = SCFG(
{name: scfg.graph[name] for name in region_blocks},
name_gen=scfg.name_gen,
)
# For all entries, replace the header as a jump target
# with the newly created region as a jump target.
for name in entries:
# Case in which entry is outside the given sub-graph
if name not in scfg.graph.keys():
# If it's actually outside the graph, a check to see
# if it's a valid assumption, is that the region that
# the SCFG represents should not be the meta region.
assert scfg.region.kind != "meta"
continue
entry = scfg.graph.pop(name)
jt = list(entry._jump_targets)
for idx, s in enumerate(jt):
if s is region_header:
jt[idx] = region_name
entry = entry.replace_jump_targets(jump_targets=tuple(jt))
be = list(entry.backedges)
for idx, s in enumerate(be):
if s is region_header:
be[idx] = region_name
entry = entry.replace_backedges(backedges=tuple(be))
# If the entry itself is a region, update it's
# exiting blocks too, recursively
if isinstance(entry, RegionBlock):
entry = update_exiting(entry, region_header, region_name)
scfg.add_block(entry)
region = RegionBlock(
name=region_name,
_jump_targets=scfg[region_exiting].jump_targets,
backedges=(),
kind=region_kind,
header=region_header,
subregion=head_subgraph,
exiting=region_exiting,
parent_region=parent_region,
)
scfg.remove_blocks(region_blocks)
scfg.graph[region_name] = region
# Set the parent region of the newly generated regional
# graph as the current region.
object.__setattr__(region.subregion, "region", region)
# If the region is a header of the parent region replace
# it accordingly.
if region_header == parent_region.header:
parent_region.replace_header(region_name)
# If the region is a header of the parent region replace
# it accordingly.
if region_exiting == parent_region.exiting:
parent_region.replace_exiting(region_name)
# For every region block inside the newly created region,
# update the parent region
assert region.subregion is not None
for k, v in region.subregion.graph.items():
if isinstance(v, RegionBlock):
object.__setattr__(v, "parent_region", region)
def restructure_branch(parent_region: RegionBlock) -> None:
assert parent_region.subregion is not None
scfg: SCFG = parent_region.subregion
# print("restructure_branch", scfg.graph)
doms = _doms(scfg)
postdoms = _post_doms(scfg)
postimmdoms = _imm_doms(postdoms)
immdoms = _imm_doms(doms)
regions = [r for r in _iter_branch_regions(scfg, immdoms, postimmdoms)]
# Early exit when no branching regions are found.
# TODO: the whole graph should become a linear mono head
if not regions:
return
# Compute initial regions.
begin, end = regions[0]
head_region_blocks = find_head_blocks(scfg, begin)
branch_regions = find_branch_regions(scfg, begin, end)
tail_region_blocks = find_tail_blocks(
scfg, begin, head_region_blocks, branch_regions
)
# Unify headers of tail subregion if need be.
headers, entries = scfg.find_headers_and_entries(tail_region_blocks)
if len(headers) > 1:
end = scfg.name_gen.new_block_name(block_names.SYNTH_HEAD)
scfg.insert_block_and_control_blocks(end, entries, headers)
# Recompute regions.
head_region_blocks = find_head_blocks(scfg, begin)
branch_regions = find_branch_regions(scfg, begin, end)
tail_region_blocks = find_tail_blocks(
scfg, begin, head_region_blocks, branch_regions
)
# Branch region processing:
# Close any open branch regions by inserting a SyntheticTail.
# Populate any empty branch regions by inserting a SyntheticBranch.
for region in branch_regions:
if region:
bra_start, inner_nodes = region
if inner_nodes:
# Insert SyntheticTail
exiting_blocks, _ = scfg.find_exiting_and_exits(inner_nodes)
tail_headers, _ = scfg.find_headers_and_entries(
tail_region_blocks
)
_, _ = scfg.join_tails_and_exits(exiting_blocks, tail_headers)
else:
# Insert SyntheticFill, a placeholder for an empty branch region
tail_headers, _ = scfg.find_headers_and_entries(tail_region_blocks)
synthetic_branch_block_name = scfg.name_gen.new_block_name(
block_names.SYNTH_FILL
)
scfg.insert_SyntheticFill(
synthetic_branch_block_name, [begin], tail_headers
)
# Recompute regions.
head_region_blocks = find_head_blocks(scfg, begin)
branch_regions = find_branch_regions(scfg, begin, end)
tail_region_blocks = find_tail_blocks(
scfg, begin, head_region_blocks, branch_regions
)
# extract subregions
extract_region(scfg, head_region_blocks, "head", parent_region)
for region in branch_regions:
if region:
bra_start, inner_nodes = region
if inner_nodes:
extract_region(scfg, inner_nodes, "branch", parent_region)
extract_region(scfg, tail_region_blocks, "tail", parent_region)
def _iter_branch_regions(
scfg: SCFG, immdoms: Dict[str, str], postimmdoms: Dict[str, str]
) -> Iterator[Tuple[str, str]]:
for begin, node in scfg.concealed_region_view.items():
if len(node.jump_targets) > 1:
# found branch
if begin in postimmdoms:
end = postimmdoms[begin]
if immdoms[end] == begin:
yield begin, end
def _imm_doms(doms: Dict[str, Set[str]]) -> Dict[str, str]:
idoms = {k: v - {k} for k, v in doms.items()}
changed = True
while changed:
changed = False
for k, vs in idoms.items():
nstart = len(vs)
for v in list(vs):
vs -= idoms[v]
if len(vs) < nstart:
changed = True
# fix output
out = {}
for k, vs in idoms.items():
if vs:
[v] = vs
out[k] = v
return out
def _doms(scfg: SCFG) -> Dict[str, Set[str]]:
# compute dom
entries = set()
preds_table = defaultdict(set)
succs_table = defaultdict(set)
node: BasicBlock
for src, node in scfg.graph.items():
for dst in node.jump_targets:
# check dst is in subgraph
if dst in scfg.graph:
preds_table[dst].add(src)
succs_table[src].add(dst)
for k in scfg.graph:
if not preds_table[k]:
entries.add(k)
return _find_dominators_internal(
entries, list(scfg.graph.keys()), preds_table, succs_table
)
def _post_doms(scfg: SCFG) -> Dict[str, Set[str]]:
# compute post dom
entries = set()
for k, v in scfg.graph.items():
targets = set(v.jump_targets) & set(scfg.graph)
if not targets:
entries.add(k)
preds_table = defaultdict(set)
succs_table = defaultdict(set)
node: BasicBlock
for src, node in scfg.graph.items():
for dst in node.jump_targets:
# check dst is in subgraph
if dst in scfg.graph:
preds_table[src].add(dst)
succs_table[dst].add(src)
return _find_dominators_internal(
entries, list(scfg.graph.keys()), preds_table, succs_table
)
def _find_dominators_internal(
entries: Set[str],
nodes: List[str],
preds_table: Dict[str, Set[str]],
succs_table: Dict[str, Set[str]],
) -> Dict[str, Set[str]]:
# From NUMBA
# See theoretical description in
# http://en.wikipedia.org/wiki/Dominator_%28graph_theory%29
# The algorithm implemented here uses a todo-list as described
# in http://pages.cs.wisc.edu/~fischer/cs701.f08/finding.loops.html
# if post:
# entries = set(self._exit_points)
# preds_table = self._succs
# succs_table = self._preds
# else:
# entries = set([self._entry_point])
# preds_table = self._preds
# succs_table = self._succs
import functools
if not entries:
raise RuntimeError(
"no entry points: dominator algorithm " "cannot be seeded"
)
doms = {}
for e in entries:
doms[e] = {e}
todo = []
for n in nodes:
if n not in entries:
doms[n] = set(nodes)
todo.append(n)
while todo:
n = todo.pop()
if n in entries:
continue
new_doms = {n}
preds = preds_table[n]
if preds:
new_doms |= functools.reduce(
set.intersection, [doms[p] for p in preds] # type: ignore
)
if new_doms != doms[n]:
assert len(new_doms) < len(doms[n])
doms[n] = new_doms
todo.extend(succs_table[n])
return doms