transforms/remove_inter_assigns: prevent combinatorial explosion

This commit is contained in:
Sebastien Bourdeauducq 2014-11-03 14:22:24 +08:00
parent fba72cc0a2
commit 171d56af54
2 changed files with 17 additions and 3 deletions

View File

@ -2,8 +2,7 @@ import ast
from copy import copy, deepcopy from copy import copy, deepcopy
from collections import defaultdict from collections import defaultdict
from artiq.transforms.tools import is_ref_transparent from artiq.transforms.tools import is_ref_transparent, count_all_nodes
class _TargetLister(ast.NodeVisitor): class _TargetLister(ast.NodeVisitor):
def __init__(self): def __init__(self):
@ -47,7 +46,7 @@ class _InterAssignRemover(ast.NodeTransformer):
node.value = self.visit(node.value) node.value = self.visit(node.value)
node.targets = [self.visit(target) for target in node.targets] node.targets = [self.visit(target) for target in node.targets]
rt, depends_on = is_ref_transparent(node.value) rt, depends_on = is_ref_transparent(node.value)
if rt: if rt and count_all_nodes(node.value) < 100:
for target in node.targets: for target in node.targets:
if isinstance(target, ast.Name): if isinstance(target, ast.Name):
if target.id not in depends_on: if target.id not in depends_on:

View File

@ -115,3 +115,18 @@ def is_ref_transparent(expr):
return True, dependencies return True, dependencies
else: else:
return False, None return False, None
class _NodeCounter(ast.NodeVisitor):
def __init__(self):
self.count = 0
def generic_visit(self, node):
self.count += 1
ast.NodeVisitor.generic_visit(self, node)
def count_all_nodes(node):
nc = _NodeCounter()
nc.visit(node)
return nc.count