Skip to content

Commit

Permalink
[llvm-reduce] Skip updating calls where OldF isn't the called fn.
Browse files Browse the repository at this point in the history
When replacing function calls, skip call instructions where the old
function is not the called function, but e.g. the old function is passed
as an argument.

This fixes a crash due to trying to construct invalid IR for the test
case.

Reviewed By: aeubanks

Differential Revision: https://reviews.llvm.org/D109759
  • Loading branch information
fhahn committed Oct 1, 2021
1 parent 81d2cea commit 57fbb9e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
23 changes: 23 additions & 0 deletions llvm/test/tools/llvm-reduce/remove-args-fn-passed-through-call.ll
@@ -0,0 +1,23 @@
; Test that llvm-reduce can remove uninteresting function arguments from function definitions as well as their calls.
; This test checks that functions with different argument types are handled correctly
;
; RUN: llvm-reduce --test FileCheck --test-arg --check-prefixes=CHECK-ALL,CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
; RUN: FileCheck --check-prefixes=CHECK-ALL,CHECK-FINAL %s --input-file %t

declare void @pass(void (i32, i8*, i64*)*)

define void @bar() {
entry:
; CHECK-INTERESTINGNESS: call void @pass({{.*}}@interesting
; CHECK-FINAL: call void @pass(void (i32, i8*, i64*)* bitcast (void (i64*)* @interesting to void (i32, i8*, i64*)*))
call void @pass(void (i32, i8*, i64*)* @interesting)
ret void
}

; CHECK-ALL: define internal void @interesting
; CHECK-INTERESTINGNESS-SAME: ({{.*}}%interesting{{.*}}) {
; CHECK-FINAL-SAME: (i64* %interesting)
define internal void @interesting(i32 %uninteresting1, i8* %uninteresting2, i64* %interesting) {
entry:
ret void
}
4 changes: 4 additions & 0 deletions llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp
Expand Up @@ -26,6 +26,10 @@ static void replaceFunctionCalls(Function &OldF, Function &NewF,
const auto &Users = OldF.users();
for (auto I = Users.begin(), E = Users.end(); I != E; )
if (auto *CI = dyn_cast<CallInst>(*I++)) {
// Skip uses in call instructions where OldF isn't the called function
// (e.g. if OldF is an argument of the call).
if (CI->getCalledFunction() != &OldF)
continue;
SmallVector<Value *, 8> Args;
for (auto ArgI = CI->arg_begin(), E = CI->arg_end(); ArgI != E; ++ArgI)
if (ArgIndexesToKeep.count(ArgI - CI->arg_begin()))
Expand Down

0 comments on commit 57fbb9e

Please sign in to comment.