[PATCH] D113486: [llvm-reduce] Fix invalid reduction in basic-blocks delta pass

Dwight Guth via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 9 07:54:49 PST 2021


dwightguth created this revision.
dwightguth added a reviewer: aeubanks.
dwightguth requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Previously, if the basic-blocks delta pass tried to remove a basic block
that was the last basic block in a function that did not have external
or weak linkage, the resulting IR would become invalid. Since removing
the last basic block in a function is effectively identical to removing
the function body itself, we check explicitly for this case and if we
detect it, we run the same logic as in ReduceFunctionBodies.cpp


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D113486

Files:
  llvm/test/tools/llvm-reduce/remove-bbs-entry.ll
  llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp


Index: llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp
===================================================================
--- llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp
+++ llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp
@@ -126,7 +126,15 @@
     // Instructions might be referenced in other BBs
     for (auto &I : *BB)
       I.replaceAllUsesWith(UndefValue::get(I.getType()));
-    BB->eraseFromParent();
+    if (BB->getParent()->size() == 1) {
+      // this is the last basic block of the function, thus we must also make
+      // sure to remove comdat and set linkage to external
+      auto F = BB->getParent();
+      F->deleteBody();
+      F->setComdat(nullptr);
+    } else {
+      BB->eraseFromParent();
+    }
   }
 }
 
Index: llvm/test/tools/llvm-reduce/remove-bbs-entry.ll
===================================================================
--- /dev/null
+++ llvm/test/tools/llvm-reduce/remove-bbs-entry.ll
@@ -0,0 +1,18 @@
+; Test that llvm-reduce inserts valid return instructions for functions with
+; on-void return types.
+;
+; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=basic-blocks --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
+; RUN: cat %t | FileCheck %s
+
+; CHECK-INTERESTINGNESS: interesting1:
+
+; CHECK-NOT: uninteresting
+define linkonce_odr i32 @foo() {
+uninteresting:
+  ret i32 0
+}
+
+define i32 @main(i1 %c) {
+interesting1:
+  ret i32 0
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D113486.385827.patch
Type: text/x-patch
Size: 1485 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211109/f8a1cfc6/attachment.bin>


More information about the llvm-commits mailing list