<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/64997>64997</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [MLIR][transforms] Error in `cloneInto()` function when null values exist in pass `RemoveDeadValues`
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            mlir
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
            srishti-pm
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          srishti-pm
      </td>
    </tr>
</table>

<pre>
    The pass `RemoveDeadValues` uses the `cloneInto()` function to copy one region to the other during a specific cleanup. This function in turn uses the function `setOperands()`. The latter cannot handle the presence of null values inside the source region, which has the potential to cause an error when we apply this pass. Null values are expected to appear during the execution of this pass, although it is ensured that at the end of the pass, all such values are removed.

To fix this error, a replacement for the `cloneInto()` function needs to be found.

**Example input:**
```
func.func @clean_region_branch_op_remove_last_2_results_last_2_arguments_and_last_operand(%arg2: i1) -> (i32) {
  %c0 = arith.constant 0 : i32
  %c1 = arith.constant 1 : i32
  %c2 = arith.constant 2 : i32
  %live, %non_live, %non_live_0, %non_live_1 = scf.while (%arg3 = %c0, %arg4 = %c1, %arg10 = %c2) : (i32, i32, i32) -> (i32, i32, i32, i32) {
    %non_live_2 = arith.addi %arg10, %arg10 : i32
    %non_live_3 = func.call @identity(%arg10) : (i32) -> (i32)
 scf.condition(%arg2) %arg4, %arg3, %non_live_2, %non_live_3 : i32, i32, i32, i32
  } do {
  ^bb0(%arg5: i32, %arg6: i32, %arg7: i32, %arg8: i32):
    %non_live_4 = arith.addi %arg7, %arg8 :i32
    scf.yield %arg5, %arg6, %non_live_4 : i32, i32, i32
  }
  return %live : i32
}
func.func private @identity(%arg1 : i32) -> (i32) {
  return %arg1 : i32
}
```

**Error trace:**

```
0x000055555b962f6f in mlir::detail::OperandStorage::setOperands(mlir::Operation*, mlir::ValueRange) ()
(gdb) backtrace
#0  0x000055555b962f6f in mlir::detail::OperandStorage::setOperands(mlir::Operation*, mlir::ValueRange) ()
#1  0x000055555b96a163 in mlir::Region::cloneInto(mlir::Region*, llvm::ilist_iterator<llvm::ilist_detail::node_options<mlir::Block, false, false, void>, false, false>, mlir::IRMapping&) ()
#2  0x000055555b30e4f6 in (anonymous namespace)::dropUsesAndEraseResults(mlir::Operation*, llvm::BitVector) ()
#3  0x000055555b30db70 in (anonymous namespace)::RemoveDeadValues::runOnOperation()::$_1::operator()(mlir::Operation*) const ()
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMV19v4roS_zTmZVQUbBLIAw-0tNJKd-9KvXv3FTn2QHzW2JHt9M-3P7JNE0LZPft4KgTJeP785jdjj8u9V0eDuCHlPaHUO-XboO66E6GUlLsZ70Nr3WaUzxor3zffW4SOew-kKp7xZF9wh1z-4LpHT6oCeo8eQotxXWhr8IsJltA1oXVcPvRGBGUNBAvCdu9gDYLD41kUDW1o0YHsnTJH4OA7FOqgBAiN3PTdHL63yo-OlIHQOzMGHlZIVXgM3zp03Eg_YIgOEDQPAR0IbowN0HIjNSbzzqFHIxDsAUyvNbyk3EAZr2RW8bZ34gM2oQ_w2irRQsszgM4GNEFxnZLkvUfgBtA56-C1RQOvCLzr9DuEmElkcw7_vQjFHQK-dSgCyuiDdx3ygZIYAt9Q9ClJexi9RChch9b2xxZUAOUBje9d9NLyADxkYyOzGV5YafC9aC8RuFRdOSfFjhTb_P3dwkG95YgpoWQMDjvNBZ7QBDhY9yf1N4jSx-QahIPtzTQOofHz-MZPnUZQpusDYWfpWaMqzp_0Gv3O4xeQZZE6ZZ_Ls28cN6Ld226fE9pr7sOe7h36Xgf_8crdsY_4_Z4bmYU2d04CX3J3pIRtQS0IreGOsEcgdK0Yja9kdZ9hABBaigII2wF3KrRzYY0P3ASIwi1EgwvNxS3NxS1NekuT3tDU6gVjVQgtjTX7G6_74lqQYXhxmL-2SiMMKbO0kHI623B3XA7CxShcFIM0U8K2A0EPcPlzxd50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