Skip to content

Commit b6bab6d

Browse files
committed
[MLIR][transforms] Fix cloneInto() error in RemoveDeadValues pass
This commit fixes an error in the `RemoveDeadValues` pass that is associated with its incorrect usage of the `cloneInto()` function. The `setOperands()` function that is used by the `cloneInto()` function requires all operands to not be null. But, that is not possible in this pass because we drop uses of dead values, thus making them null. It is only at the end of the pass that we are assured that such null values won't exist but during the execution of the pass, there could be null values. To fix this, we replace the usage of the `cloneInto()` function to copy a region with `moveBlock()` to move each block of the region one by one. This function does not require the presence of non-null values and is thus the right choice here. This implementation is also more opttimized because we are moving things instead of copying them. The goal was always moving. Signed-off-by: Srishti Srivastava <[email protected]> Reviewed By: srishti-pm Differential Revision: https://reviews.llvm.org/D158941
1 parent 2c9372e commit b6bab6d

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

mlir/lib/Transforms/RemoveDeadValues.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,12 @@ static void dropUsesAndEraseResults(Operation *op, BitVector toErase) {
135135
Operation *newOp = builder.create(state);
136136
for (const auto &[index, region] : llvm::enumerate(op->getRegions())) {
137137
Region &newRegion = newOp->getRegion(index);
138-
IRMapping mapping;
139-
region.cloneInto(&newRegion, mapping);
138+
// Move all blocks of `region` into `newRegion`.
139+
Block *temp = new Block();
140+
newRegion.push_back(temp);
141+
while (!region.empty())
142+
region.front().moveBefore(temp);
143+
temp->erase();
140144
}
141145

142146
unsigned indexOfNextNewCallOpResultToReplace = 0;

mlir/test/Transforms/remove-dead-values.mlir

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,20 +207,24 @@ func.func @clean_region_branch_op_dont_remove_first_2_results_but_remove_first_o
207207
// CHECK-NEXT: %[[c0:.*]] = arith.constant 0
208208
// CHECK-NEXT: %[[c1:.*]] = arith.constant 1
209209
// CHECK-NEXT: %[[live_and_non_live:.*]]:2 = scf.while (%[[arg3:.*]] = %[[c0]], %[[arg4:.*]] = %[[c1]]) : (i32, i32) -> (i32, i32) {
210+
// CHECK-NEXT: func.call @identity() : () -> ()
210211
// CHECK-NEXT: scf.condition(%[[arg2]]) %[[arg4]], %[[arg3]] : i32, i32
211212
// CHECK-NEXT: } do {
212213
// CHECK-NEXT: ^bb0(%[[arg5:.*]]: i32, %[[arg6:.*]]: i32):
213214
// CHECK-NEXT: scf.yield %[[arg5]], %[[arg6]] : i32, i32
214215
// CHECK-NEXT: }
215216
// CHECK-NEXT: return %[[live_and_non_live]]#0 : i32
216217
// CHECK-NEXT: }
218+
// CHECK: func.func private @identity() {
219+
// CHECK-NEXT: return
220+
// CHECK-NEXT: }
217221
func.func @clean_region_branch_op_remove_last_2_results_last_2_arguments_and_last_operand(%arg2: i1) -> (i32) {
218222
%c0 = arith.constant 0 : i32
219223
%c1 = arith.constant 1 : i32
220224
%c2 = arith.constant 2 : i32
221225
%live, %non_live, %non_live_0, %non_live_1 = scf.while (%arg3 = %c0, %arg4 = %c1, %arg10 = %c2) : (i32, i32, i32) -> (i32, i32, i32, i32) {
222226
%non_live_2 = arith.addi %arg10, %arg10 : i32
223-
%non_live_3 = arith.muli %arg10, %arg10 : i32
227+
%non_live_3 = func.call @identity(%arg10) : (i32) -> (i32)
224228
scf.condition(%arg2) %arg4, %arg3, %non_live_2, %non_live_3 : i32, i32, i32, i32
225229
} do {
226230
^bb0(%arg5: i32, %arg6: i32, %arg7: i32, %arg8: i32):
@@ -229,6 +233,9 @@ func.func @clean_region_branch_op_remove_last_2_results_last_2_arguments_and_las
229233
}
230234
return %live : i32
231235
}
236+
func.func private @identity(%arg1 : i32) -> (i32) {
237+
return %arg1 : i32
238+
}
232239

233240
// -----
234241

0 commit comments

Comments
 (0)