Skip to content

Conversation

navyxliu
Copy link
Contributor

@navyxliu navyxliu commented Aug 29, 2025

'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. We encounter a function that the last block jumps backward and then exit. append it as a lit test.

This patch fixes the bug by querying from FunctionInterfaceOp directly.

If processFuncOp gets the wrong number, it can't delete a dead returned value. here is the stacktrace.

 Assertion failed: (op->getNumResults() == toErase.size() && "expected the number of results in `op` and the size of `toErase` to " "be the same"), function dropUsesAndEraseResults, file RemoveDeadValues.cpp, line 200.
 PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
 Stack dump:
 0.	Program arguments: /Users/xxinliu/Devel/llvm-project/build/bin/mlir-opt /Users/xxinliu/Devel/llvm-project/mlir/test/Transforms/remove-dead-values.mlir -remove-dead-values -split-input-file -verify-diagnostics
 Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
 0  mlir-opt                 0x00000001044b4f68 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
 1  mlir-opt                 0x00000001044b2bfc llvm::sys::RunSignalHandlers() + 172
 2  mlir-opt                 0x00000001044b5a60 SignalHandler(int, __siginfo*, void*) + 360
 3  libsystem_platform.dylib 0x000000018f0fd6a4 _sigtramp + 56
 4  libsystem_pthread.dylib  0x000000018f0c388c pthread_kill + 296
 5  libsystem_c.dylib        0x000000018efcca3c abort + 124
 6  libsystem_c.dylib        0x000000018efcbc70 err + 0
 7  mlir-opt                 0x000000010c372148 (anonymous namespace)::cleanUpDeadVals((anonymous namespace)::RDVFinalCleanupList&) (.cold.3) + 0
 8  mlir-opt                 0x000000010c36a484 (anonymous namespace)::cleanUpDeadVals((anonymous namespace)::RDVFinalCleanupList&) + 14920
 9  mlir-opt                 0x000000010c36675c (anonymous namespace)::RemoveDeadValues::runOnOperation() + 292

… the last block is not the exit.

'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. We encounter a function that the last block jumps backward and then exit. append it as a lit test.

This patch fixes the bug by querying from FunctionInterfaceOp directly.
If processFuncOp gets the wrong number, it can't delete a dead returned value. here is the stacktrace.
```
 Assertion failed: (op->getNumResults() == toErase.size() && "expected the number of results in `op` and the size of `toErase` to " "be the same"), function dropUsesAndEraseResults, file RemoveDeadValues.cpp, line 200.
 PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
 Stack dump:
 0.	Program arguments: /Users/xxinliu/Devel/llvm-project/build/bin/mlir-opt /Users/xxinliu/Devel/llvm-project/mlir/test/Transforms/remove-dead-values.mlir -remove-dead-values -split-input-file -verify-diagnostics
 Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
 0  mlir-opt                 0x00000001044b4f68 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
 1  mlir-opt                 0x00000001044b2bfc llvm::sys::RunSignalHandlers() + 172
 2  mlir-opt                 0x00000001044b5a60 SignalHandler(int, __siginfo*, void*) + 360
 3  libsystem_platform.dylib 0x000000018f0fd6a4 _sigtramp + 56
 4  libsystem_pthread.dylib  0x000000018f0c388c pthread_kill + 296
 5  libsystem_c.dylib        0x000000018efcca3c abort + 124
 6  libsystem_c.dylib        0x000000018efcbc70 err + 0
 7  mlir-opt                 0x000000010c372148 (anonymous namespace)::cleanUpDeadVals((anonymous namespace)::RDVFinalCleanupList&) (.cold.3) + 0
 8  mlir-opt                 0x000000010c36a484 (anonymous namespace)::cleanUpDeadVals((anonymous namespace)::RDVFinalCleanupList&) + 14920
 9  mlir-opt                 0x000000010c36675c (anonymous namespace)::RemoveDeadValues::runOnOperation() + 292
```
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir labels Aug 29, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 29, 2025

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-core

Author: xin liu (navyxliu)

Changes

'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. We encounter a function that the last block jumps backward and then exit. append it as a lit test.

If processFuncOp gets the wrong number, it can't delete a dead returned value. here is the stacktrace.

This patch fixes the bug by querying from FunctionInterfaceOp directly.


Full diff: https://github.com/llvm/llvm-project/pull/156123.diff

2 Files Affected:

  • (modified) mlir/lib/Transforms/RemoveDeadValues.cpp (+1-2)
  • (modified) mlir/test/Transforms/remove-dead-values.mlir (+23)
diff --git a/mlir/lib/Transforms/RemoveDeadValues.cpp b/mlir/lib/Transforms/RemoveDeadValues.cpp
index 02dad69e49614..0e84b6dd17f29 100644
--- a/mlir/lib/Transforms/RemoveDeadValues.cpp
+++ b/mlir/lib/Transforms/RemoveDeadValues.cpp
@@ -344,8 +344,7 @@ static void processFuncOp(FunctionOpInterface funcOp, Operation *module,
   // being returned, in order to optimize our IR. So, this demonstrates how we
   // can make our optimization strong by even removing a live return value (%0),
   // since it forwards only to non-live value(s) (%1#1).
-  Operation *lastReturnOp = funcOp.back().getTerminator();
-  size_t numReturns = lastReturnOp->getNumOperands();
+  size_t numReturns = funcOp.getNumResults();
   BitVector nonLiveRets(numReturns, true);
   for (SymbolTable::SymbolUse use : uses) {
     Operation *callOp = use.getUser();
diff --git a/mlir/test/Transforms/remove-dead-values.mlir b/mlir/test/Transforms/remove-dead-values.mlir
index 0f8d757086e87..fa2c145bd3701 100644
--- a/mlir/test/Transforms/remove-dead-values.mlir
+++ b/mlir/test/Transforms/remove-dead-values.mlir
@@ -592,3 +592,26 @@ module @dynamically_unreachable {
     return
   }
 }
+
+// CHECK-LABEL: module @last_block_not_exit
+module @last_block_not_exit {
+  // return value can be removed because it's private.
+  func.func private @terminated_with_condbr(%arg0: i1, %arg1: i1) -> i1 {
+    %true = arith.constant true
+    %false = arith.constant false
+    cf.cond_br %arg0, ^bb1(%false : i1), ^bb2
+  ^bb1(%1: i1):  // 2 preds: ^bb0, ^bb2
+    return %1 : i1
+  ^bb2:  // pred: ^bb3
+    cf.cond_br %arg1, ^bb1(%false : i1), ^bb1(%true : i1)
+  }
+
+  func.func public @call_private_but_not_use() {
+    %i0 = arith.constant 0: i1
+    %i1 = arith.constant 1: i1
+    call @terminated_with_condbr(%i0, %i1) : (i1, i1) -> i1
+    func.return
+  }
+  // CHECK-LABEL: @call_private_but_not_use
+  // CHECK: call @terminated_with_condbr(%false, %true) : (i1, i1)
+}

@victor-eds victor-eds requested review from ftynse and joker-eph August 29, 2025 23:25
@navyxliu navyxliu changed the title [mlir][Transforms] Allow RemoveDeadValues to process a function which the last block is not the exit. [mlir][Transforms] Allow RemoveDeadValues to process a function whose the last block is not the exit. Aug 29, 2025
@joker-eph joker-eph enabled auto-merge (squash) August 31, 2025 09:44
@joker-eph joker-eph merged commit 917f078 into llvm:main Aug 31, 2025
12 checks passed
Copy link

@navyxliu Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mlir:core MLIR Core Infrastructure mlir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants