code: def LiftAllocToScopeBegin():
      """Lift allocate to beginning of the current scope.
  
      Returns
      -------
      fpass : tvm.transform.Pass
          The pass
      """
  
      def _ftransform(f, mod, ctx):
          lift_stmt = [[]]
  
          def _merge_block(slist, body):
              for op in slist:
                  if op.body == body:
                      body = op
                  elif isinstance(op, tvm.tir.Allocate):
                      body = tvm.tir.Allocate(
                          op.buffer_var,
                          op.dtype,
                          op.extents,
                          op.condition,
                          body,
                      )
                  elif isinstance(op, tvm.tir.AttrStmt):
                      body = tvm.tir.AttrStmt(
                          op.node, op.attr_key, op.value, body
                      )
                  elif isinstance(op, tvm.tir.For):
                      body = tvm.tir.For(
                          op.loop_var,
                          op.min,
                          op.extent,
                          op.kind,
                          body,
                          op.thread_binding,
                          op.annotations,
                      )
                  else:
                      raise RuntimeError("unexpected op")
              del slist[:]
              return body
  
          def _pre_order(op):
              if isinstance(op, tvm.tir.For):
                  lift_stmt.append([])
              elif isinstance(op, tvm.tir.AttrStmt):
                  if op.attr_key == "virtual_thread":
                      lift_stmt.append([])
  
          def _post_order(op):
              if isinstance(op, tvm.tir.Allocate):
                  lift_stmt[-1].append(op)
                  return op.body
              if isinstance(op, tvm.tir.AttrStmt):
                  if op.attr_key == "storage_scope":
                      lift_stmt[-1].append(op)
                      return op.body
                  if op.attr_key == "virtual_thread":
                      return _merge_block(
                          lift_stmt.pop() + [op], op.body
                      )
                  return op
              if isinstance(op, tvm.tir.For):
                  return _merge_block(lift_stmt.pop() + [op], op.body)
              raise RuntimeError("not reached")
  
          stmt_in = f.body
          stmt = tvm.tir.stmt_functor.ir_transform(
              stmt_in,
              _pre_order,
              _post_order,
              ["tir.Allocate", "tir.AttrStmt", "tir.For"],
          )
          assert len(lift_stmt) == 1
          return f.with_body(_merge_block(lift_stmt[0], stmt))
  
      return tvm.tir.transform.prim_func_pass(
          _ftransform, opt_level=0, name="tir.vta.LiftAllocToScopeBegin"
      )
  
file_path: tvm/vta/python/vta/transform.py
imports: import tvm
  from tvm import te
  from tvm.topi import utils
  from .environment import get_env
label_desc: Use image node_id 4 for calling a global function with example usage: LiftAllocToScopeBegin() and returns: tvm, f, body, op, op, _merge_block, op, _merge_block