diff --git a/mlir/lib/Transforms/RemoveDeadValues.cpp b/mlir/lib/Transforms/RemoveDeadValues.cpp --- a/mlir/lib/Transforms/RemoveDeadValues.cpp +++ b/mlir/lib/Transforms/RemoveDeadValues.cpp @@ -135,8 +135,12 @@ Operation *newOp = builder.create(state); for (const auto &[index, region] : llvm::enumerate(op->getRegions())) { Region &newRegion = newOp->getRegion(index); - IRMapping mapping; - region.cloneInto(&newRegion, mapping); + // Move all blocks of `region` into `newRegion`. + Block *temp = new Block(); + newRegion.push_back(temp); + while (!region.empty()) + region.front().moveBefore(temp); + temp->erase(); } unsigned indexOfNextNewCallOpResultToReplace = 0; diff --git a/mlir/test/Transforms/remove-dead-values.mlir b/mlir/test/Transforms/remove-dead-values.mlir --- a/mlir/test/Transforms/remove-dead-values.mlir +++ b/mlir/test/Transforms/remove-dead-values.mlir @@ -207,6 +207,7 @@ // CHECK-NEXT: %[[c0:.*]] = arith.constant 0 // CHECK-NEXT: %[[c1:.*]] = arith.constant 1 // CHECK-NEXT: %[[live_and_non_live:.*]]:2 = scf.while (%[[arg3:.*]] = %[[c0]], %[[arg4:.*]] = %[[c1]]) : (i32, i32) -> (i32, i32) { +// CHECK-NEXT: func.call @identity() : () -> () // CHECK-NEXT: scf.condition(%[[arg2]]) %[[arg4]], %[[arg3]] : i32, i32 // CHECK-NEXT: } do { // CHECK-NEXT: ^bb0(%[[arg5:.*]]: i32, %[[arg6:.*]]: i32): @@ -214,13 +215,16 @@ // CHECK-NEXT: } // CHECK-NEXT: return %[[live_and_non_live]]#0 : i32 // CHECK-NEXT: } +// CHECK: func.func private @identity() { +// CHECK-NEXT: return +// CHECK-NEXT: } 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 = arith.muli %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): @@ -229,6 +233,9 @@ } return %live : i32 } +func.func private @identity(%arg1 : i32) -> (i32) { + return %arg1 : i32 +} // -----