Example #1
0
    def generic_visit(self, node):
        if isinstance(node, ast.expr) and node in self.constant_expressions:
            fake_node = ast.Expression(node)
            code = compile(ast.gast_to_ast(fake_node),
                           '<constant folding>', 'eval')
            try:
                value = eval(code, self.env)
                new_node = to_ast(value)
                if not ASTMatcher(node).match(new_node):
                    self.update = True
                    return new_node
            except DamnTooLongPattern as e:
                print("W: ", e, " Assume no update happened.")
            except ConversionError as e:
                print('error in constant folding: ', e)
                raise
            except ToNotEval:
                pass
            except AttributeError as e:
                # this may miss a few optimization
                logger.info('During constant folding, bailing out due to: ' +
                            e.args[0])
            except NameError as e:
                # FIXME dispatched function are not processed by constant
                # folding
                if "__dispatch__" in e.args[0]:
                    return Transformation.generic_visit(self, node)
                # this may miss a few optimization
                logger.info('During constant folding, bailing out due to: ' +
                            e.args[0])
            except Exception as e:
                raise PythranSyntaxError(str(e), node)

        return Transformation.generic_visit(self, node)
Example #2
0
 def generic_visit(self, node):
     if node in self.constant_expressions:
         try:
             fake_node = ast.Expression(node.value if isinstance(node, ast.Index) else node)
             code = compile(fake_node, "<constant folding>", "eval")
             value = eval(code, self.env)
             new_node = self.to_ast(value)
             if isinstance(node, ast.Index) and not isinstance(new_node, ast.Index):
                 new_node = ast.Index(new_node)
             return new_node
         except ConstantFolding.ConversionError as e:
             print ast.dump(node)
             print "error in constant folding: ", e
             raise
         except ConstantFolding.ToNotEval:
             return Transformation.generic_visit(self, node)
         except AttributeError as e:
             # FIXME union_ function is not handle by constant folding
             if "union_" in e.args[0]:
                 return Transformation.generic_visit(self, node)
             raise
         except NameError as e:
             # FIXME dispatched function are not processed by constant
             # folding
             if "__dispatch__" in e.args[0]:
                 return Transformation.generic_visit(self, node)
             raise
     else:
         return Transformation.generic_visit(self, node)
Example #3
0
 def __init__(self, pm, name, ctx, lambdas, imports, global_decls):
     Transformation.__init__(self)
     self.passmanager = pm
     self.ctx = ctx
     self.prefix = name
     self.lambda_functions = lambdas
     self.imports = imports
     self.global_declarations = global_decls
Example #4
0
 def __init__(self, pm, name, ctx, lambda_functions, imports):
     Transformation.__init__(self)
     self.passmanager = pm
     self.ctx = ctx
     self.prefix = name
     self.lambda_functions = lambda_functions
     self.imports = imports
     self.global_declarations = pm.gather(GlobalDeclarations, ctx.module)
Example #5
0
 def __init__(self, pm, name, ctx, lambda_functions, imports):
     Transformation.__init__(self)
     self.passmanager = pm
     self.ctx = ctx
     self.prefix = name
     self.lambda_functions = lambda_functions
     self.imports = imports
     self.global_declarations = pm.gather(GlobalDeclarations, ctx.module)
Example #6
0
 def __init__(self, pm, name, ctx, lambdas, imports, global_decls):
     Transformation.__init__(self)
     self.passmanager = pm
     self.ctx = ctx
     self.prefix = name
     self.lambda_functions = lambdas
     self.imports = imports
     self.global_declarations = global_decls
Example #7
0
 def generic_visit(self, node):
     if node in self.constant_expressions:
         try:
             fake_node = ast.Expression(
                 node.value if isinstance(node, ast.Index) else node)
             code = compile(fake_node, '<constant folding>', 'eval')
             value = eval(code, self.env)
             new_node = self.to_ast(value)
             if (isinstance(node, ast.Index)
                     and not isinstance(new_node, ast.Index)):
                 new_node = ast.Index(new_node)
             return new_node
         except Exception:  # as e:
             #print ast.dump(node)
             #print 'error in constant folding: ', e
             return Transformation.generic_visit(self, node)
     else:
         return Transformation.generic_visit(self, node)
Example #8
0
 def generic_visit(self, node):
     if node in self.constant_expressions:
         try:
             fake_node = ast.Expression(
                 node.value if isinstance(node, ast.Index) else node)
             code = compile(fake_node, '<constant folding>', 'eval')
             value = eval(code, self.env)
             new_node = to_ast(value)
             if(isinstance(node, ast.Index) and
                not isinstance(new_node, ast.Index)):
                 new_node = ast.Index(new_node)
             try:
                 if not ASTMatcher(node).search(new_node):
                     self.update = True
             except DamnTooLongPattern as e:
                 print "W: ", e, " Assume no update happened."
             return new_node
         except ConversionError as e:
             print ast.dump(node)
             print 'error in constant folding: ', e
             raise
         except ToNotEval:
             return Transformation.generic_visit(self, node)
         except AttributeError as e:
             # FIXME union_ function is not handle by constant folding
             if "union_" in e.args[0]:
                 return Transformation.generic_visit(self, node)
             elif "pythran" in e.args[0]:
                 # FIXME: Can be fix giving a Python implementation for
                 # these functions.
                 return Transformation.generic_visit(self, node)
             raise
         except NameError as e:
             # FIXME dispatched function are not processed by constant
             # folding
             if "__dispatch__" in e.args[0]:
                 return Transformation.generic_visit(self, node)
             raise
     else:
         return Transformation.generic_visit(self, node)
Example #9
0
 def generic_visit(self, node):
     if node in self.constant_expressions:
         try:
             fake_node = ast.Expression(
                 node.value if isinstance(node, ast.Index) else node)
             code = compile(fake_node, '<constant folding>', 'eval')
             value = eval(code, self.env)
             new_node = to_ast(value)
             if (isinstance(node, ast.Index)
                     and not isinstance(new_node, ast.Index)):
                 new_node = ast.Index(new_node)
             try:
                 if not ASTMatcher(node).search(new_node):
                     self.update = True
             except DamnTooLongPattern as e:
                 print "W: ", e, " Assume no update happened."
             return new_node
         except ConversionError as e:
             print ast.dump(node)
             print 'error in constant folding: ', e
             raise
         except ToNotEval:
             return Transformation.generic_visit(self, node)
         except AttributeError as e:
             # FIXME union_ function is not handle by constant folding
             if "union_" in e.args[0]:
                 return Transformation.generic_visit(self, node)
             elif "pythran" in e.args[0]:
                 # FIXME: Can be fix giving a Python implementation for
                 # these functions.
                 return Transformation.generic_visit(self, node)
             raise
         except NameError as e:
             # FIXME dispatched function are not processed by constant
             # folding
             if "__dispatch__" in e.args[0]:
                 return Transformation.generic_visit(self, node)
             raise
     else:
         return Transformation.generic_visit(self, node)
Example #10
0
 def __init__(self):
     self.count = 0
     Transformation.__init__(self)
Example #11
0
 def __init__(self):
     Transformation.__init__(self, NormalizeTuples,
                             OptimizableComprehension)
 def __init__(self):
     Transformation.__init__(self, OptimizableComprehension)
Example #13
0
 def __init__(self):
     """ Basic initialiser. """
     Transformation.__init__(self)
     self.imports = set()
     self.symbols = dict()
Example #14
0
 def __init__(self):
     Transformation.__init__(self)
Example #15
0
 def __init__(self):
     Transformation.__init__(self, Identifiers)
Example #16
0
 def __init__(self):
     Transformation.__init__(self, RangeValues)
Example #17
0
 def __init__(self):
     """ Gather required information. """
     Transformation.__init__(self, PotentialIterator, Aliases)
Example #18
0
 def __init__(self):
     """ Basic initialiser. """
     Transformation.__init__(self)
     self.imports = set()
     self.symbols = dict()
Example #19
0
 def __init__(self):
     Transformation.__init__(self, NormalizeTuples, PotentialIterator)
 def __init__(self):
     Transformation.__init__(self, PotentialIterator, Aliases)
Example #21
0
 def __init__(self):
     Transformation.__init__(self)
     self.imports = set()
     self.symbols = dict()
Example #22
0
 def __init__(self):
     Transformation.__init__(self)
     self.imports = set()
     self.symbols = dict()
Example #23
0
 def __init__(self, pm, ctx, global_declarations):
     Transformation.__init__(self)
     self.ctx = ctx
     self.passmanager = pm
     self.global_declarations = global_declarations
     self.identifiers = set(self.global_declarations.keys())
Example #24
0
 def __init__(self):
     Transformation.__init__(self)
     # Remap self.visit_XXXX() to self.attach_data() generic method
     for s in GatherOMPData.statements:
         setattr(self, "visit_" + s, self.attach_data)
     self.current = list()
Example #25
0
 def __init__(self, pm, ctx, global_declarations):
     Transformation.__init__(self)
     self.ctx = ctx
     self.passmanager = pm
     self.global_declarations = global_declarations
 def __init__(self):
     self.renamings = dict()
     Transformation.__init__(self, Identifiers)
Example #27
0
 def __init__(self, pm, ctx):
     Transformation.__init__(self)
     self.ctx = ctx
     self.passmanager = pm
     self.global_declarations = pm.gather(GlobalDeclarations, ctx.module)
Example #28
0
 def __init__(self):
     Transformation.__init__(self)
     # Remap self.visit_XXXX() to self.attach_data() generic method
     for s in GatherOMPData.statements:
         setattr(self, "visit_" + s, self.attach_data)
     self.current = list()
Example #29
0
 def __init__(self, pm, ctx, global_declarations):
     Transformation.__init__(self)
     self.ctx = ctx
     self.passmanager = pm
     self.global_declarations = global_declarations
Example #30
0
 def __init__(self):
     Transformation.__init__(self, OptimizableComprehension)
Example #31
0
 def __init__(self):
     Transformation.__init__(self, UseDefChains, Ancestors, Aliases,
                             RangeValues, Identifiers)
     self.loops_mod = dict()
Example #32
0
 def visit_Call(self, node):
     if isinstance(node.func, ast.Attribute):
         return self.generic_visit(node)
     else:
         return Transformation.generic_visit(self, node)
Example #33
0
 def __init__(self):
     Transformation.__init__(self, UseDefChain, Ancestors, Aliases,
                             RangeValues, Identifiers)
     self.loops_mod = dict()
 def __init__(self):
     Transformation.__init__(self, Globals)
     self.imports = {'__builtin__': '__builtin__'}
     self.to_import = set()
 def __init__(self):
     Transformation.__init__(self, Globals, Ancestors)
     self.imports = {'builtins': 'builtins'}
     self.to_import = set()
Example #36
0
 def __init__(self):
     Transformation.__init__(self, NormalizeTuples,
                             OptimizableComprehension)
Example #37
0
 def __init__(self):
     Transformation.__init__(self, TypeDependencies,
                             OrderedGlobalDeclarations)
Example #38
0
 def __init__(self):
     Transformation.__init__(self, Locals, Globals)
Example #39
0
 def __init__(self):
     Transformation.__init__(self, Identifiers)
Example #40
0
 def __init__(self):
     Transformation.__init__(self)
Example #41
0
 def __init__(self):
     Transformation.__init__(self, PotentialIterator)
Example #42
0
 def __init__(self):
     """ Gather required information. """
     Transformation.__init__(self, PotentialIterator, Aliases)
Example #43
0
 def __init__(self):
     Transformation.__init__(self, NormalizeTuples,
                             PotentialIterator)
 def __init__(self):
     Transformation.__init__(self, Globals)
     self.imports = {'__builtin__': '__builtin__'}
     self.to_import = set()
Example #45
0
 def __init__(self):
     Transformation.__init__(self, Aliases, ConstantExpressions,
                             PureExpressions)
 def __init__(self):
     Transformation.__init__(self, ConstantExpressions)
Example #47
0
 def __init__(self):
     Transformation.__init__(self, Globals)
     self.imports = set()
     self.to_import = set()
 def __init__(self, pm, ctx):
     Transformation.__init__(self)
     self.ctx = ctx
     self.passmanager = pm
     self.global_declarations = pm.gather(GlobalDeclarations, ctx.module)
 def __init__(self):
     Transformation.__init__(self, Globals)
     self.imports = set()
     self.to_import = set()
Example #50
0
 def __init__(self):
     self.renamings = dict()
     Transformation.__init__(self, Identifiers)
Example #51
0
 def __init__(self):
     """Gather required information."""
     Transformation.__init__(self, PotentialIterator, Aliases)
     self.use_itertools = False
Example #52
0
 def __init__(self):
     Transformation.__init__(self, ConstantExpressions)
 def __init__(self):
     Transformation.__init__(self, RangeValues)
Example #54
0
 def __init__(self):
     self.count = 0
     Transformation.__init__(self)
Example #55
0
 def __init__(self):
     Transformation.__init__(self, Aliases, PureExpressions)