class_docstr: Functor for translating Relay programs into Python ASTs.
class_docstr_tok: ["Functor","for","translating","Relay","programs","into","Python","ASTs","."]
docstr: Performs optimizations necessary to be able to generate code for prog.
docstr_tok: ["Performs","optimizations","necessary","to","be","able","to","generate","code","for","prog","."]
code: def optimize(self, prog: Expr):
      """Performs optimizations necessary to be able to generate code for prog."""
      # unwrap tuple wrappers (some op calls produce them)
      unwrapped = (
          prog.astuple()
          if isinstance(prog, relay.TupleWrapper)
          else prog
      )
      assert relay.analysis.well_formed(unwrapped)
      # For a lone global var, there is nothing we need to do
      if isinstance(unwrapped, relay.GlobalVar):
          return unwrapped
  
      # main might be in the mod already and from_expr will not override it if it's there,
      # so we need a new name
      target_name = self.generate_function_name("target")
  
      wrapped = unwrapped
      if not isinstance(unwrapped, relay.Function):
          wrapped = relay.Function(
              relay.analysis.free_vars(unwrapped), unwrapped
          )
  
      # easiest way to make a deep copy -- note that main will not be overridden if it's present
      copy_mod = tvm.IRModule.from_expr(
          relay.Tuple([]), self.mod.functions, self.mod.type_definitions
      )
      copy_mod[target_name] = wrapped
  
      # necessary pass: SimplifyInference (otherwise we can't generate code for some operators)
      # and fusion (to get primitive functions)
      opts = tvm.transform.Sequential(
          [
              relay.transform.SimplifyInference(),
              relay.transform.FuseOps(fuse_opt_level=0),
          ]
      )
      copy_mod = opts(copy_mod)
      optimized = copy_mod[target_name]
      return (
          optimized
          if isinstance(unwrapped, Function)
          else optimized.body
      )
  
imports: import sys
  import ast
  from ast import alias, Assign, Load, Name, NameConstant, Num, Return, Store, Str
  import re
  import tvm
  from tvm import relay
  from tvm.relay.adt import Pattern
  from tvm.relay.backend import te_compiler
  from tvm.relay.expr import Expr, GlobalVar, Var
  from tvm.relay.function import Function
  from tvm.relay.expr_functor import ExprFunctor
label_desc: Use image node_id 3 for calling the PythonConverter obj's underlying member method code with example usage: obj.optimize(prog) and returns: unknown, unwrapped