Skip to content

Commit 9f724d0

Browse files
Fix Bug in RemoveDeadValues Pass (#148437)
This patch fixes a bug in the RemoveDeadValues pass where unused function arguments were not removed from the function signature in an edge case where the function returns void. A corresponding test was added to the MLIR LIT test suite to cover this case.
1 parent 0711565 commit 9f724d0

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

mlir/lib/Transforms/RemoveDeadValues.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,6 @@ static void processFuncOp(FunctionOpInterface funcOp, Operation *module,
345345
// since it forwards only to non-live value(s) (%1#1).
346346
Operation *lastReturnOp = funcOp.back().getTerminator();
347347
size_t numReturns = lastReturnOp->getNumOperands();
348-
if (numReturns == 0)
349-
return;
350348
BitVector nonLiveRets(numReturns, true);
351349
for (SymbolTable::SymbolUse use : uses) {
352350
Operation *callOp = use.getUser();
@@ -368,6 +366,8 @@ static void processFuncOp(FunctionOpInterface funcOp, Operation *module,
368366
cl.functions.push_back({funcOp, nonLiveArgs, nonLiveRets});
369367

370368
// Do (5) and (6).
369+
if (numReturns == 0)
370+
return;
371371
for (SymbolTable::SymbolUse use : uses) {
372372
Operation *callOp = use.getUser();
373373
assert(isa<CallOpInterface>(callOp) && "expected a call-like user");

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,3 +548,26 @@ func.func @test_atomic_yield(%I: memref<10xf32>, %idx : index) {
548548
func.return
549549
}
550550

551+
// -----
552+
553+
// CHECK-LABEL: module @return_void_with_unused_argument
554+
module @return_void_with_unused_argument {
555+
// CHECK-LABEL: func.func private @fn_return_void_with_unused_argument
556+
// CHECK-SAME: (%[[ARG0_FN:.*]]: i32)
557+
func.func private @fn_return_void_with_unused_argument(%arg0: i32, %arg1: memref<4xi32>) -> () {
558+
%sum = arith.addi %arg0, %arg0 : i32
559+
%c0 = arith.constant 0 : index
560+
%buf = memref.alloc() : memref<1xi32>
561+
memref.store %sum, %buf[%c0] : memref<1xi32>
562+
return
563+
}
564+
// CHECK-LABEL: func.func @main
565+
// CHECK-SAME: (%[[ARG0_MAIN:.*]]: i32)
566+
// CHECK: call @fn_return_void_with_unused_argument(%[[ARG0_MAIN]]) : (i32) -> ()
567+
func.func @main(%arg0: i32) -> memref<4xi32> {
568+
%unused = memref.alloc() : memref<4xi32>
569+
call @fn_return_void_with_unused_argument(%arg0, %unused) : (i32, memref<4xi32>) -> ()
570+
return %unused : memref<4xi32>
571+
}
572+
}
573+

0 commit comments

Comments
 (0)