Skip to content

Commit 133c1af

Browse files
authored
llvm-reduce: Filter function based on uses before removing arguments (#133412)
Invokes and others are not handled, so this was leaving broken callsites behind for anything other than CallInst
1 parent 44e3735 commit 133c1af

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
; RUN: llvm-reduce %s -o %t --abort-on-invalid-reduction --delta-passes=arguments --test FileCheck --test-arg %s --test-arg --check-prefix=INTERESTING --test-arg --input-file
2+
; RUN: FileCheck %s --input-file %t --check-prefix=REDUCED
3+
4+
; Test that we don't break the callsite for an unhandled invoke
5+
6+
declare void @did_not_throw(i32)
7+
declare void @thrown()
8+
9+
; INTERESTING-LABEL: define i32 @maybe_throwing_callee(
10+
11+
; REDUCED-LABEL: define i32 @maybe_throwing_callee(i32 %arg0, i32 %arg1) {
12+
define i32 @maybe_throwing_callee(i32 %arg0, i32 %arg1) {
13+
call void @thrown()
14+
ret i32 %arg1
15+
}
16+
17+
@initializer_user = global [1 x ptr] [ptr @maybe_throwing_callee ]
18+
19+
; REDUCED-LABEL: define void @caller()
20+
; REDUCED: %i0 = invoke i32 @maybe_throwing_callee(i32 0, i32 0) #0
21+
define void @caller(i32 %arg0, ptr %arg1) personality ptr @__gxx_personality_v0 {
22+
bb:
23+
%val = load i32, ptr %arg1
24+
%i0 = invoke i32 @maybe_throwing_callee(i32 0, i32 0) nofree
25+
to label %bb3 unwind label %bb1
26+
27+
bb1:
28+
landingpad { ptr, i32 }
29+
catch ptr null
30+
call void @thrown()
31+
br label %bb4
32+
33+
bb3:
34+
call void @did_not_throw(i32 %i0)
35+
br label %bb4
36+
37+
bb4:
38+
ret void
39+
}
40+
41+
declare i32 @__gxx_personality_v0(...)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
; RUN: llvm-reduce %s -o %t --abort-on-invalid-reduction --delta-passes=arguments --test FileCheck --test-arg %s --test-arg --check-prefix=INTERESTING --test-arg --input-file
2+
; RUN: FileCheck %s --input-file %t --check-prefix=REDUCED
3+
4+
; INTERESTING: @initializer_user
5+
; REDUCED: @initializer_user = global [1 x ptr] [ptr @captured_func]
6+
@initializer_user = global [1 x ptr] [ptr @captured_func ]
7+
8+
; INTERESTING-LABEL: define i32 @captured_func(
9+
10+
; REDUCED-LABEL: define i32 @captured_func() {
11+
define i32 @captured_func(i32 %a, i32 %b) {
12+
%mul = mul i32 %a, %b
13+
ret i32 %mul
14+
}
15+
16+
; INTERESTING-LABEL: declare void @captures(
17+
declare void @captures(i32, ptr, i32)
18+
19+
20+
; INTERESTING-LABEL: define i32 @caller(
21+
; INTERESTING: = call
22+
; INTERESTING: = call
23+
24+
; REDUCED-LABEL: define i32 @caller(i32 %a, i32 %b) {
25+
; REDUCED: %call0 = call i32 @captures(i32 %a, ptr @captured_func, i32 %b)
26+
; REDUCED: %call1 = call i32 @captured_func()
27+
define i32 @caller(i32 %a, i32 %b) {
28+
%call0 = call i32 @captures(i32 %a, ptr @captured_func, i32 %b)
29+
%call1 = call i32 @captured_func(i32 %a, i32 %b)
30+
%add = add i32 %call0, %call1
31+
ret i32 %add
32+
}

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,20 @@ static bool shouldRemoveArguments(const Function &F) {
9696
return !F.arg_empty() && !F.isIntrinsic();
9797
}
9898

99+
static bool allFuncUsersRewritable(const Function &F) {
100+
for (const Use &U : F.uses()) {
101+
const CallBase *CB = dyn_cast<CallBase>(U.getUser());
102+
if (!CB || !CB->isCallee(&U))
103+
continue;
104+
105+
// TODO: Handle all CallBase cases.
106+
if (!isa<CallInst>(CB))
107+
return false;
108+
}
109+
110+
return true;
111+
}
112+
99113
/// Removes out-of-chunk arguments from functions, and modifies their calls
100114
/// accordingly. It also removes allocations of out-of-chunk arguments.
101115
static void extractArgumentsFromModule(Oracle &O, ReducerWorkItem &WorkItem) {
@@ -107,7 +121,8 @@ static void extractArgumentsFromModule(Oracle &O, ReducerWorkItem &WorkItem) {
107121
for (auto &F : Program) {
108122
if (!shouldRemoveArguments(F))
109123
continue;
110-
124+
if (!allFuncUsersRewritable(F))
125+
continue;
111126
Funcs.push_back(&F);
112127
for (auto &A : F.args()) {
113128
if (callingConvRequiresArgument(F, A) || O.shouldKeep())

0 commit comments

Comments
 (0)