code: def detect_loop_invariants(while_op):
      block = while_op.blocks[0]
      loop_invariant_ids = (
          []
      )  # list of index in op.loop_vars, block.inputs
      for i, vx_in in enumerate(block.inputs):
          vx_out = block.outputs[i + 1]  # first output is cond var.
          return_input_as_output = vx_in == vx_out
          # this block output is a var from outside of the block
          output_from_outside_of_block = (
              vx_out in block._visible_vars_from_enclosing_block()
          )
          if return_input_as_output or output_from_outside_of_block:
              loop_invariant_ids.append(i)
  
      # TODO: All outputs that depend on only invariants are invariant. We
      # need to move computation out of while loop.
      return loop_invariant_ids
  
file_path: turicreate/src/external/coremltools_wrap/coremltools/coremltools/converters/mil/mil/passes/loop_invariant_elimination.py
imports: from __future__ import print_function
  from __future__ import division
  from __future__ import absolute_import
  import numpy
  import six
  from coremltools.converters.mil.mil import Builder
  from coremltools.converters.mil.mil.passes.pass_registry import register_pass
label_desc: Use image node_id 1 for calling a global function with example usage: detect_loop_invariants(while_op) and returns: loop_invariant_ids