[llvm] 16c3db8 - [llvm-reduce] Fix invalid reduction in basic-blocks delta pass
Arthur Eubanks via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 9 10:45:25 PST 2021
Author: Dwight Guth
Date: 2021-11-09T10:43:38-08:00
New Revision: 16c3db8def87529f1392f26c17cb014108968bb4
URL: https://github.com/llvm/llvm-project/commit/16c3db8def87529f1392f26c17cb014108968bb4
DIFF: https://github.com/llvm/llvm-project/commit/16c3db8def87529f1392f26c17cb014108968bb4.diff
LOG: [llvm-reduce] Fix invalid reduction in basic-blocks delta pass
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
Reviewed By: aeubanks
Differential Revision: https://reviews.llvm.org/D113486
Added:
llvm/test/tools/llvm-reduce/remove-bbs-comdat.ll
llvm/test/tools/llvm-reduce/remove-bbs-entry.ll
Modified:
llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp
Removed:
################################################################################
diff --git a/llvm/test/tools/llvm-reduce/remove-bbs-comdat.ll b/llvm/test/tools/llvm-reduce/remove-bbs-comdat.ll
new file mode 100644
index 000000000000..9a6dba151155
--- /dev/null
+++ b/llvm/test/tools/llvm-reduce/remove-bbs-comdat.ll
@@ -0,0 +1,21 @@
+; RUN: llvm-reduce --delta-passes=basic-blocks --abort-on-invalid-reduction --test FileCheck --test-arg --check-prefixes=CHECK-ALL,CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
+; RUN: cat %t | FileCheck --check-prefixes=CHECK-ALL,CHECK-FINAL %s
+
+; CHECK-FINAL-NOT: = comdat
+; CHECK-INTERESTINGNESS: @callee(
+; CHECK-FINAL: declare void @callee()
+
+$foo = comdat any
+
+define void @callee() comdat($foo) {
+ ret void
+}
+
+; CHECK-ALL: define void @caller()
+define void @caller() {
+entry:
+; CHECK-ALL: call void @callee()
+; CHECK-ALL: ret void
+ call void @callee()
+ ret void
+}
diff --git a/llvm/test/tools/llvm-reduce/remove-bbs-entry.ll b/llvm/test/tools/llvm-reduce/remove-bbs-entry.ll
new file mode 100644
index 000000000000..04f26f413651
--- /dev/null
+++ b/llvm/test/tools/llvm-reduce/remove-bbs-entry.ll
@@ -0,0 +1,18 @@
+; Test that llvm-reduce correctly removes the entry block of functions for
+; linkages other than external and weak.
+;
+; 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
+}
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp b/llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp
index f8a95bea62be..3e3822483a1b 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp
@@ -126,7 +126,15 @@ static void extractBasicBlocksFromModule(Oracle &O, Module &Program) {
// 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();
+ }
}
}
More information about the llvm-commits
mailing list