Skip to content

Commit 917f078

Browse files
authored
[mlir][Transforms] Allow RemoveDeadValues to process a function whose the last block is not the exit. (#156123)
'processFuncOp' queries the number of returned values of a function using the terminator of the last block's getNumOperands(). It presumes the last block is the exit. It is not always the case. This patch fixes the bug by querying from FunctionInterfaceOp directly.
1 parent 731f4d9 commit 917f078

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

mlir/lib/Transforms/RemoveDeadValues.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,7 @@ static void processFuncOp(FunctionOpInterface funcOp, Operation *module,
344344
// being returned, in order to optimize our IR. So, this demonstrates how we
345345
// can make our optimization strong by even removing a live return value (%0),
346346
// since it forwards only to non-live value(s) (%1#1).
347-
Operation *lastReturnOp = funcOp.back().getTerminator();
348-
size_t numReturns = lastReturnOp->getNumOperands();
347+
size_t numReturns = funcOp.getNumResults();
349348
BitVector nonLiveRets(numReturns, true);
350349
for (SymbolTable::SymbolUse use : uses) {
351350
Operation *callOp = use.getUser();

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,3 +592,26 @@ module @dynamically_unreachable {
592592
return
593593
}
594594
}
595+
596+
// CHECK-LABEL: module @last_block_not_exit
597+
module @last_block_not_exit {
598+
// return value can be removed because it's private.
599+
func.func private @terminated_with_condbr(%arg0: i1, %arg1: i1) -> i1 {
600+
%true = arith.constant true
601+
%false = arith.constant false
602+
cf.cond_br %arg0, ^bb1(%false : i1), ^bb2
603+
^bb1(%1: i1): // 2 preds: ^bb0, ^bb2
604+
return %1 : i1
605+
^bb2: // pred: ^bb3
606+
cf.cond_br %arg1, ^bb1(%false : i1), ^bb1(%true : i1)
607+
}
608+
609+
func.func public @call_private_but_not_use() {
610+
%i0 = arith.constant 0: i1
611+
%i1 = arith.constant 1: i1
612+
call @terminated_with_condbr(%i0, %i1) : (i1, i1) -> i1
613+
func.return
614+
}
615+
// CHECK-LABEL: @call_private_but_not_use
616+
// CHECK: call @terminated_with_condbr(%false, %true) : (i1, i1)
617+
}

0 commit comments

Comments
 (0)