Skip to content

llvm-reduce: Change function return types if function is not called #134035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: users/arsenm/llvm-reduce/add-reduce-values-to-return-argument-case
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix multiple return case
  • Loading branch information
arsenm committed Apr 25, 2025
commit 8221f36c240e4d4f12e67a8f452ce4aa96364027
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,49 @@ define i64 @inst_to_return_has_different_type_but_no_func_call_use(ptr %arg) {
ret i64 0
}

; INTERESTING-LABEL: @callsite_different_type_unused_0(
; RESULT-LABEL: define i64 @inst_to_return_has_different_type_but_call_result_unused(
; RESULT-NEXT: %load = load i32, ptr %arg
; RESULT-NEXT: store i32 %load, ptr @gv
; RESULT-NEXT: ret i64 0
; INTERESTING-LABEL: @multiple_returns_wrong_return_type_no_callers(
; RESULT-LABEL: define i32 @multiple_returns_wrong_return_type_no_callers(

; RESULT: bb0:
; RESULT-NEXT: %load0 = load i32,
; RESULT-NEXT: ret i32 %load0

; RESULT: bb1:
; RESULT-NEXT: store i32 8, ptr null
; RESULT-NEXT: ret i32 0
define i64 @multiple_returns_wrong_return_type_no_callers(ptr %arg, i1 %cond, i64 %arg2) {
entry:
br i1 %cond, label %bb0, label %bb1

bb0:
%load0 = load i32, ptr %arg
store i32 %load0, ptr @gv
ret i64 234

bb1:
store i32 8, ptr null
ret i64 %arg2

bb2:
ret i64 34
}

; INTERESTING-LABEL: define {{.+}} @callsite_different_type_unused_0(

; RESULT-LABEL: define i64 @callsite_different_type_unused_0(ptr %arg) {
; RESULT-NEXT: %unused0 = call i64 @inst_to_return_has_different_type_but_call_result_unused(ptr %arg)
; RESULT-NEXT: ret i64 %unused0
define void @callsite_different_type_unused_0(ptr %arg) {
%unused0 = call i64 @inst_to_return_has_different_type_but_call_result_unused(ptr %arg)
%unused1 = call i64 @inst_to_return_has_different_type_but_call_result_unused(ptr null)
ret void
}

; TODO: Could rewrite this return from i64 to i32 since the callsite is unused.
; INTERESTING-LABEL: @inst_to_return_has_different_type_but_call_result_unused(
; INTERESTING-LABEL: define {{.+}} @inst_to_return_has_different_type_but_call_result_unused(
; RESULT-LABEL: define i64 @inst_to_return_has_different_type_but_call_result_unused(
; RESULT-NEXT: %load = load i32, ptr %arg
; RESULT-NEXT: store i32 %load, ptr @gv
; RESULT: ret i64 0
define i64 @inst_to_return_has_different_type_but_call_result_unused(ptr %arg) {
%load = load i32, ptr %arg
Expand Down
2 changes: 1 addition & 1 deletion llvm/tools/llvm-reduce/deltas/ReduceValuesToReturn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static void rewriteFuncWithReturnType(Function &OldF, Value *NewRetValue) {
NewRetI ? NewRetI->getIterator() : EntryBB.end();

// Hack up any return values in other blocks, we can't leave them as ret void.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment needs updating?

Suggested change
// Hack up any return values in other blocks, we can't leave them as ret void.
// Hack up any return values in other blocks, we can't leave them as returning OldRetTy.

if (OldFuncTy->getReturnType()->isVoidTy()) {
if (OldFuncTy->getReturnType() != NewRetTy) {
for (BasicBlock &OtherRetBB : OldF) {
if (&OtherRetBB != NewRetBlock) {
auto *OrigRI = dyn_cast<ReturnInst>(OtherRetBB.getTerminator());
Expand Down