forked from M-Labs/artiq
1
0
Fork 0

Reformat.

This commit is contained in:
whitequark 2015-11-20 21:41:44 +08:00
parent d0f86e05d0
commit cc623c13b4
1 changed files with 46 additions and 44 deletions

View File

@ -45,55 +45,57 @@ class Interleaver:
postdom_tree = None postdom_tree = None
for insn in func.instructions(): for insn in func.instructions():
if isinstance(insn, ir.Parallel): if not isinstance(insn, ir.Parallel):
# Lazily compute dominators. continue
if postdom_tree is None:
postdom_tree = domination.PostDominatorTree(func)
interleave_until = postdom_tree.immediate_dominator(insn.basic_block) # Lazily compute dominators.
assert (interleave_until is not None) # no nonlocal flow in `with parallel` if postdom_tree is None:
postdom_tree = domination.PostDominatorTree(func)
target_block = insn.basic_block interleave_until = postdom_tree.immediate_dominator(insn.basic_block)
target_time = 0 assert (interleave_until is not None) # no nonlocal flow in `with parallel`
source_blocks = insn.basic_block.successors()
source_times = [0 for _ in source_blocks]
while len(source_blocks) > 0: target_block = insn.basic_block
def iodelay_of_block(block): target_time = 0
terminator = block.terminator() source_blocks = insn.basic_block.successors()
if isinstance(terminator, ir.Delay): source_times = [0 for _ in source_blocks]
# We should be able to fold everything without free variables.
assert iodelay.is_const(terminator.expr)
return terminator.expr.value
else:
return 0
def time_after_block(pair): while len(source_blocks) > 0:
index, block = pair def iodelay_of_block(block):
return source_times[index] + iodelay_of_block(block) terminator = block.terminator()
if isinstance(terminator, ir.Delay):
index, source_block = min(enumerate(source_blocks), key=time_after_block) # We should be able to fold everything without free variables.
source_block_delay = iodelay_of_block(source_block) assert iodelay.is_const(terminator.expr)
return terminator.expr.value
target_terminator = target_block.terminator()
if isinstance(target_terminator, (ir.Delay, ir.Branch)):
target_terminator.set_target(source_block)
elif isinstance(target_terminator, ir.Parallel):
target_terminator.replace_with(ir.Branch(source_block))
else: else:
assert False return 0
new_source_block = postdom_tree.immediate_dominator(source_block) def time_after_block(pair):
assert (new_source_block is not None) index, block = pair
assert delay_free_subgraph(source_block, new_source_block) return source_times[index] + iodelay_of_block(block)
target_block = source_block index, source_block = min(enumerate(source_blocks), key=time_after_block)
target_time += source_block_delay source_block_delay = iodelay_of_block(source_block)
if new_source_block == interleave_until: target_terminator = target_block.terminator()
# We're finished with this branch. if isinstance(target_terminator, (ir.Delay, ir.Branch)):
del source_blocks[index] target_terminator.set_target(source_block)
del source_times[index] elif isinstance(target_terminator, ir.Parallel):
else: target_terminator.replace_with(ir.Branch(source_block))
source_blocks[index] = new_source_block else:
source_times[index] = target_time assert False
target_block = source_block
target_time += source_block_delay
new_source_block = postdom_tree.immediate_dominator(source_block)
assert (new_source_block is not None)
assert delay_free_subgraph(source_block, new_source_block)
if new_source_block == interleave_until:
# We're finished with this branch.
del source_blocks[index]
del source_times[index]
else:
source_blocks[index] = new_source_block
source_times[index] = target_time