def _dup_if_affect_io(self, new_var: Var, old_var: Var, before_op: Operation):
        """
        We cannot replace old_var with new_var, if:
        1. old_var is a function output
        2. new_var is a function input
        Since the name of the function is going to be changed and become invalid.

        For this special corner case, we use an identity op to duplicate the new_var.
        """
        block_1 = before_op.enclosing_block
        is_new_var_function_input = (
            isinstance(block_1, Function) and new_var in block_1.inputs.values()
        )
        block_2 = old_var.op.enclosing_block
        is_old_var_function_output = isinstance(block_2, Function) and old_var in block_2.outputs

        if is_new_var_function_input and is_old_var_function_output:
            return mb.identity(x=new_var, before_op=before_op)
        return new_var

        We cannot replace old_var with new_var, if:
        1. old_var is a function output
        2. new_var is a function input
        Since the name of the function is going to be changed and become invalid.

        For this special corner case, we use an identity op to duplicate the new_var.
        
coremltools/converters/mil/mil/passes/defs/optimize_repeat_ops.py