Skip to content

Commit b1564d0

Browse files
committed
fix bug in remove-dead-values pass and add test for the case
1 parent cc65da0 commit b1564d0

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,3 +548,43 @@ func.func @test_atomic_yield(%I: memref<10xf32>, %idx : index) {
548548
func.return
549549
}
550550

551+
// CHECK-LABEL: module @return_void_with_unused_argument {
552+
// CHECK-LABEL: func.func private @noop() {
553+
// CHECK-NEXT: return
554+
// CHECK-NEXT: }
555+
// CHECK-LABEL: func.func private @fn_return_void_with_unused_argument(%arg0: memref<1x1xsi8, 1>, %arg1: memref<1x1xsi16, 1>) {
556+
// CHECK-NEXT: %alloc = memref.alloc() {alignment = 8 : i64} : memref<1x73xsi8, 3>
557+
// CHECK-NEXT: %alloc_0 = memref.alloc() {alignment = 8 : i64} : memref<1x1xsi16, 3>
558+
// CHECK-NEXT: %alloc_1 = memref.alloc() {alignment = 8 : i64} : memref<1x1xsi8, 3>
559+
// CHECK-NEXT: memref.copy %arg0, %alloc_1 : memref<1x1xsi8, 1> to memref<1x1xsi8, 3>
560+
// CHECK-NEXT: memref.copy %arg1, %alloc_0 : memref<1x1xsi16, 1> to memref<1x1xsi16, 3>
561+
// CHECK-NEXT: call @noop() : () -> ()
562+
// CHECK-NEXT: return
563+
// CHECK-NEXT: }
564+
// CHECK-LABEL: func.func @main(%arg0: memref<1x73xsi8, 1>, %arg1: memref<1x1xsi8, 1>, %arg2: memref<1x1xsi16, 1>) -> memref<1x73xsi8, 1> {
565+
// CHECK-NEXT: %alloc = memref.alloc() : memref<1x73xsi8, 1>
566+
// CHECK-NEXT: call @fn_return_void_with_unused_argument(%arg1, %arg2) : (memref<1x1xsi8, 1>, memref<1x1xsi16, 1>) -> ()
567+
// CHECK-NEXT: return %alloc : memref<1x73xsi8, 1>
568+
// CHECK-NEXT: }
569+
// CHECK-NEXT:}
570+
module @return_void_with_unused_argument {
571+
func.func private @noop(%arg0: memref<1x1xsi8, 3>, %arg1: memref<1x1xsi16, 3>, %arg2: memref<1x73xsi8, 1>) {
572+
return
573+
}
574+
func.func private @fn_return_void_with_unused_argument(%arg0: memref<1x73xsi8, 1> , %arg1: memref<1x1xsi8, 1> , %arg2: memref<1x1xsi16, 1> , %arg3: memref<1x73xsi8, 1>) {
575+
%alloc = memref.alloc() {alignment = 8 : i64} : memref<1x73xsi8, 3>
576+
%alloc_0 = memref.alloc() {alignment = 8 : i64} : memref<1x1xsi16, 3>
577+
%alloc_1 = memref.alloc() {alignment = 8 : i64} : memref<1x1xsi8, 3>
578+
memref.copy %arg1, %alloc_1 : memref<1x1xsi8, 1> to memref<1x1xsi8, 3>
579+
memref.copy %arg2, %alloc_0 : memref<1x1xsi16, 1> to memref<1x1xsi16, 3>
580+
call @noop(%alloc_1, %alloc_0, %arg0) :
581+
(memref<1x1xsi8, 3>, memref<1x1xsi16, 3>, memref<1x73xsi8, 1>) -> ()
582+
return
583+
}
584+
func.func @main(%arg0: memref<1x73xsi8, 1> , %arg1: memref<1x1xsi8, 1> , %arg2: memref<1x1xsi16, 1>) -> (memref<1x73xsi8, 1> ) {
585+
%alloc = memref.alloc() : memref<1x73xsi8, 1>
586+
call @fn_return_void_with_unused_argument(%arg0, %arg1, %arg2, %alloc) : (memref<1x73xsi8, 1>, memref<1x1xsi8, 1>, memref<1x1xsi16, 1>, memref<1x73xsi8, 1>) -> ()
587+
return %alloc : memref<1x73xsi8, 1>
588+
}
589+
}
590+

0 commit comments

Comments
 (0)