Skip to content

Commit 16c3db8

Browse files
Dwight Guthaeubanks
authored andcommitted
[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
1 parent a5d6dcb commit 16c3db8

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
; 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
2+
; RUN: cat %t | FileCheck --check-prefixes=CHECK-ALL,CHECK-FINAL %s
3+
4+
; CHECK-FINAL-NOT: = comdat
5+
; CHECK-INTERESTINGNESS: @callee(
6+
; CHECK-FINAL: declare void @callee()
7+
8+
$foo = comdat any
9+
10+
define void @callee() comdat($foo) {
11+
ret void
12+
}
13+
14+
; CHECK-ALL: define void @caller()
15+
define void @caller() {
16+
entry:
17+
; CHECK-ALL: call void @callee()
18+
; CHECK-ALL: ret void
19+
call void @callee()
20+
ret void
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; Test that llvm-reduce correctly removes the entry block of functions for
2+
; linkages other than external and weak.
3+
;
4+
; 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
5+
; RUN: cat %t | FileCheck %s
6+
7+
; CHECK-INTERESTINGNESS: interesting1:
8+
9+
; CHECK-NOT: uninteresting
10+
define linkonce_odr i32 @foo() {
11+
uninteresting:
12+
ret i32 0
13+
}
14+
15+
define i32 @main(i1 %c) {
16+
interesting1:
17+
ret i32 0
18+
}

llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,15 @@ static void extractBasicBlocksFromModule(Oracle &O, Module &Program) {
126126
// Instructions might be referenced in other BBs
127127
for (auto &I : *BB)
128128
I.replaceAllUsesWith(UndefValue::get(I.getType()));
129-
BB->eraseFromParent();
129+
if (BB->getParent()->size() == 1) {
130+
// this is the last basic block of the function, thus we must also make
131+
// sure to remove comdat and set linkage to external
132+
auto F = BB->getParent();
133+
F->deleteBody();
134+
F->setComdat(nullptr);
135+
} else {
136+
BB->eraseFromParent();
137+
}
130138
}
131139
}
132140

0 commit comments

Comments
 (0)