示例#1
0
def standard_analysis(node, context, is_initial=False):
    """Performs a complete static analysis of the given code.

  Args:
    node: ast.AST
    context: converter.EntityContext
    is_initial: bool, whether this is the initial analysis done on the input
      source code

  Returns:
    ast.AST, same as node, with the static analysis annotations added
  """
    # TODO(mdan): Clear static analysis here.
    # TODO(mdan): Consider not running all analyses every time.
    # TODO(mdan): Don't return a node because it's modified by reference.
    graphs = cfg.build(node)
    node = qual_names.resolve(node)
    node = activity.resolve(node, context, None)
    node = reaching_definitions.resolve(node, context, graphs, AnnotatedDef)
    node = liveness.resolve(node, context, graphs)
    node = live_values.resolve(node, context, config.PYTHON_LITERALS)
    node = type_info.resolve(node, context)
    # This second call allows resolving first-order class attributes.
    node = live_values.resolve(node, context, config.PYTHON_LITERALS)
    if is_initial:
        anno.dup(
            node,
            {
                anno.Static.DEFINITIONS: anno.Static.ORIG_DEFINITIONS,
            },
        )
    return node
示例#2
0
  def prepare(self, test_fn, namespace, recursive=True):
    namespace['ConversionOptions'] = converter.ConversionOptions

    future_features = ('print_function', 'division')
    node, source = parser.parse_entity(test_fn, future_features=future_features)
    namer = naming.Namer(namespace)
    program_ctx = converter.ProgramContext(
        options=converter.ConversionOptions(recursive=recursive),
        autograph_module=None)
    entity_info = transformer.EntityInfo(
        name=test_fn.__name__,
        source_code=source,
        source_file='<fragment>',
        future_features=future_features,
        namespace=namespace)
    ctx = transformer.Context(entity_info, namer, program_ctx)
    origin_info.resolve_entity(node, source, test_fn)

    graphs = cfg.build(node)
    node = qual_names.resolve(node)
    node = activity.resolve(node, ctx, None)
    node = reaching_definitions.resolve(node, ctx, graphs)
    anno.dup(
        node,
        {
            anno.Static.DEFINITIONS: anno.Static.ORIG_DEFINITIONS,
        },
    )

    return node, ctx
示例#3
0
def standard_analysis(node, context, is_initial=False):
  """Performs a complete static analysis of the given code.

  Args:
    node: ast.AST
    context: converter.EntityContext
    is_initial: bool, whether this is the initial analysis done on the input
      source code

  Returns:
    ast.AST, same as node, with the static analysis annotations added
  """
  # TODO(mdan): Clear static analysis here.
  # TODO(mdan): Consider not running all analyses every time.
  # TODO(mdan): Don't return a node because it's modified by reference.
  graphs = cfg.build(node)
  node = qual_names.resolve(node)
  node = activity.resolve(node, context.info, None)
  node = reaching_definitions.resolve(node, context.info, graphs, AnnotatedDef)
  node = liveness.resolve(node, context.info, graphs)
  node = live_values.resolve(node, context.info, config.PYTHON_LITERALS)
  node = type_info.resolve(node, context.info)
  # This second call allows resolving first-order class attributes.
  node = live_values.resolve(node, context.info, config.PYTHON_LITERALS)
  if is_initial:
    anno.dup(
        node,
        {
            anno.Static.DEFINITIONS: anno.Static.ORIG_DEFINITIONS,
        },
    )
  return node
示例#4
0
    def transform_ast(self, node, ctx):
        # TODO(mdan): Insert list_comprehensions somewhere.
        unsupported_features_checker.verify(node)

        # Run initial analysis.
        graphs = cfg.build(node)
        node = qual_names.resolve(node)
        node = activity.resolve(node, ctx, None)
        node = reaching_definitions.resolve(node, ctx, graphs)
        anno.dup(
            node,
            {
                anno.Static.DEFINITIONS: anno.Static.ORIG_DEFINITIONS,
            },
        )

        node = functions.transform(node, ctx)
        node = directives.transform(node, ctx)
        node = break_statements.transform(node, ctx)
        if ctx.user.options.uses(converter.Feature.ASSERT_STATEMENTS):
            node = asserts.transform(node, ctx)
        # Note: sequencing continue canonicalization before for loop one avoids
        # dealing with the extra loop increment operation that the for
        # canonicalization creates.
        node = continue_statements.transform(node, ctx)
        node = return_statements.transform(node, ctx)
        if ctx.user.options.uses(converter.Feature.LISTS):
            node = lists.transform(node, ctx)
            node = slices.transform(node, ctx)
        node = call_trees.transform(node, ctx)
        node = control_flow.transform(node, ctx)
        node = conditional_expressions.transform(node, ctx)
        node = logical_expressions.transform(node, ctx)
        return node
示例#5
0
 def transform_ast(self, node, ctx):
     node = qual_names.resolve(node)
     node = activity.resolve(node, ctx)
     graphs = cfg.build(node)
     node = reaching_definitions.resolve(node, ctx, graphs)
     node = reaching_fndefs.resolve(node, ctx, graphs)
     node = type_inference.resolve(node, ctx, graphs, self.resolver)
     return node
示例#6
0
def transform(node, ctx):
  graphs = cfg.build(node)
  node = qual_names.resolve(node)
  node = activity.resolve(node, ctx, None)
  node = reaching_definitions.resolve(node, ctx, graphs, AnnotatedDef)
  node = liveness.resolve(node, ctx, graphs)

  node = ControlFlowTransformer(ctx).visit(node)
  return node
 def _parse_and_analyze(self, test_fn):
   node, source = parser.parse_entity(test_fn, future_features=())
   entity_info = transformer.EntityInfo(
       source_code=source, source_file=None, future_features=(), namespace={})
   node = qual_names.resolve(node)
   ctx = transformer.Context(entity_info)
   node = activity.resolve(node, ctx)
   graphs = cfg.build(node)
   node = reaching_definitions.resolve(node, ctx, graphs,
                                       reaching_definitions.Definition)
   return node
示例#8
0
 def _parse_and_analyze(self, test_fn):
   node, source = parser.parse_entity(test_fn, future_features=())
   entity_info = transformer.EntityInfo(
       source_code=source, source_file=None, future_features=(), namespace={})
   node = qual_names.resolve(node)
   ctx = transformer.Context(entity_info)
   node = activity.resolve(node, ctx)
   graphs = cfg.build(node)
   node = reaching_definitions.resolve(node, ctx, graphs,
                                       reaching_definitions.Definition)
   return node
示例#9
0
 def initial_analysis(self, node, ctx):
     graphs = cfg.build(node)
     node = qual_names.resolve(node)
     node = activity.resolve(node, ctx, None)
     node = reaching_definitions.resolve(node, ctx, graphs)
     anno.dup(
         node,
         {
             anno.Static.DEFINITIONS: anno.Static.ORIG_DEFINITIONS,
         },
     )
     return node
示例#10
0
def convert4():
    node, ctx = get_node_and_ctx(f4)

    node = qual_names.resolve(node)
    cfgs = cfg.build(node)
    node = activity.resolve(node, ctx)
    node = reaching_definitions.resolve(node, ctx, cfgs)
    node = reaching_fndefs.resolve(node, ctx, cfgs)
    node = liveness.resolve(node, ctx, cfgs)

    print('live into `b = a + 1`:', anno.getanno(node.body[0], anno.Static.LIVE_VARS_IN))
    print('live into `return b`:', anno.getanno(node.body[1], anno.Static.LIVE_VARS_IN))
示例#11
0
文件: mlir_gen.py 项目: MFChunga/poo
def mlir_gen_internal(node, entity_info):
  """Returns mlir module for unprocessed node `node`."""
  namer = naming.Namer({})
  graphs = cfg.build(node)
  ctx = transformer.Context(entity_info, namer, None)
  node = qual_names.resolve(node)
  node = activity.resolve(node, ctx)
  node = reaching_definitions.resolve(node, ctx, graphs)
  node = reaching_fndefs.resolve(node, ctx, graphs)
  node = liveness.resolve(node, ctx, graphs)
  mlir_generator = MLIRGen(ctx)
  mlir_generator.visit(node)
  return mlir_generator.prog
 def _parse_and_analyze(self, test_fn):
   node, source = parser.parse_entity(test_fn)
   entity_info = transformer.EntityInfo(
       source_code=source,
       source_file=None,
       namespace={},
       arg_values=None,
       arg_types=None,
       owner_type=None)
   node = qual_names.resolve(node)
   node = activity.resolve(node, entity_info)
   graphs = cfg.build(node)
   node = reaching_definitions.resolve(node, entity_info, graphs,
                                       reaching_definitions.Definition)
   return node
示例#13
0
  def transform_ast(self, node, ctx):
    node = _apply_py_to_tf_passes(node, ctx)
    # TODO(mdan): Enable this.
    # node = anf.transform(node, ctx)

    graphs = cfg.build(node)
    node = qual_names.resolve(node)
    node = activity.resolve(node, ctx)
    node = reaching_definitions.resolve(node, ctx, graphs)
    node = reaching_fndefs.resolve(node, ctx, graphs)
    node = type_inference.resolve(node, ctx, graphs,
                                  TFRTypeResolver(self._op_defs))

    mlir_generator = TFRGen(ctx, self._op_defs)
    mlir_generator.visit(node)
    return mlir_generator.code_buffer
示例#14
0
 def _parse_and_analyze(self, test_fn):
     # TODO(mdan): Use a custom FunctionTransformer here.
     node, source = parser.parse_entity(test_fn, future_features=())
     entity_info = transformer.EntityInfo(name=test_fn.__name__,
                                          source_code=source,
                                          source_file=None,
                                          future_features=(),
                                          namespace={})
     node = qual_names.resolve(node)
     namer = naming.Namer({})
     ctx = transformer.Context(entity_info, namer, None)
     node = activity.resolve(node, ctx)
     graphs = cfg.build(node)
     node = reaching_definitions.resolve(node, ctx, graphs,
                                         reaching_definitions.Definition)
     return node
示例#15
0
 def _parse_and_analyze(self, test_fn, namespace, arg_types=None):
     node, source = parser.parse_entity(test_fn)
     entity_info = transformer.EntityInfo(source_code=source,
                                          source_file=None,
                                          namespace=namespace,
                                          arg_values=None,
                                          arg_types=arg_types)
     node = qual_names.resolve(node)
     graphs = cfg.build(node)
     ctx = transformer.Context(entity_info)
     node = activity.resolve(node, ctx)
     node = reaching_definitions.resolve(node, ctx, graphs,
                                         reaching_definitions.Definition)
     node = live_values.resolve(node, ctx, {})
     node = type_info.resolve(node, ctx)
     node = live_values.resolve(node, ctx, {})
     return node
示例#16
0
 def _parse_and_analyze(self,
                        test_fn,
                        namespace,
                        arg_types=None):
   node, source = parser.parse_entity(test_fn)
   entity_info = transformer.EntityInfo(
       source_code=source,
       source_file=None,
       namespace=namespace,
       arg_values=None,
       arg_types=arg_types,
       owner_type=None)
   node = qual_names.resolve(node)
   graphs = cfg.build(node)
   ctx = transformer.Context(entity_info)
   node = activity.resolve(node, ctx)
   node = reaching_definitions.resolve(node, ctx, graphs,
                                       reaching_definitions.Definition)
   node = live_values.resolve(node, ctx, {})
   node = type_info.resolve(node, ctx)
   node = live_values.resolve(node, ctx, {})
   return node