Skip to content

Commit 3b5e64c

Browse files
committed
Fix return attributes
1 parent 5622aaf commit 3b5e64c

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

llvm/test/tools/llvm-reduce/reduce-values-to-return-new-return-type.ll

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ bb2:
5151
; INTERESTING-LABEL: define {{.+}} @callsite_different_type_unused_0(
5252

5353
; RESULT-LABEL: define i64 @callsite_different_type_unused_0(ptr %arg) {
54-
; RESULT-NEXT: %unused0 = call i64 @inst_to_return_has_different_type_but_call_result_unused(ptr %arg)
54+
; RESULT-NEXT: %unused0 = call range(i64 99, 1024) i64 @inst_to_return_has_different_type_but_call_result_unused(ptr %arg)
5555
; RESULT-NEXT: ret i64 %unused0
5656
define void @callsite_different_type_unused_0(ptr %arg) {
57-
%unused0 = call i64 @inst_to_return_has_different_type_but_call_result_unused(ptr %arg)
57+
%unused0 = call range(i64 99, 1024) i64 @inst_to_return_has_different_type_but_call_result_unused(ptr %arg)
5858
%unused1 = call i64 @inst_to_return_has_different_type_but_call_result_unused(ptr null)
5959
ret void
6060
}
@@ -122,3 +122,34 @@ define void @call_multiple_returns_wrong_return_type(ptr %arg, i1 %cond, i32 %ar
122122
%unused = call <2 x i32> @multiple_returns_wrong_return_type(ptr %arg, i1 %cond, i32 %arg2)
123123
ret void
124124
}
125+
126+
; INTERESTING-LABEL: @drop_incompatible_type_return_attrs(
127+
128+
; RESULT-LABEL: define i32 @drop_incompatible_type_return_attrs(ptr %arg, float %arg1) {
129+
; RESULT-NEXT: %load = load i32, ptr %arg, align 4
130+
; RESULT-NEXT: ret i32 %load
131+
define nofpclass(nan inf) float @drop_incompatible_type_return_attrs(ptr %arg, float %arg1) {
132+
%load = load i32, ptr %arg
133+
store i32 %load, ptr @gv
134+
ret float %arg1
135+
}
136+
137+
; INTERESTING-LABEL: @drop_incompatible_return_range(
138+
; RESULT-LABEL: define i32 @drop_incompatible_return_range(ptr %arg, i64 %arg1) {
139+
; RESULT-NEXT: %load = load i32, ptr %arg, align 4
140+
; RESULT-NEXT: ret i32 %load
141+
define range(i64 8, 256) i64 @drop_incompatible_return_range(ptr %arg, i64 %arg1) {
142+
%load = load i32, ptr %arg
143+
store i32 %load, ptr @gv
144+
ret i64 %arg1
145+
}
146+
147+
; INTERESTING-LABEL: @preserve_compatible_return_range(
148+
; RESULT-LABEL: define range(i32 8, 256) i32 @preserve_compatible_return_range(ptr %arg, <2 x i32> %arg1) {
149+
; RESULT-NEXT: %load = load i32, ptr %arg, align 4
150+
; RESULT-NEXT: ret i32 %load
151+
define range(i32 8, 256) <2 x i32> @preserve_compatible_return_range(ptr %arg, <2 x i32> %arg1) {
152+
%load = load i32, ptr %arg
153+
store i32 %load, ptr @gv
154+
ret <2 x i32> %arg1
155+
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
#include "Delta.h"
2020
#include "Utils.h"
21+
#include "llvm/IR/AttributeMask.h"
22+
#include "llvm/IR/Attributes.h"
2123
#include "llvm/IR/CFG.h"
2224
#include "llvm/IR/Instructions.h"
2325
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
@@ -55,8 +57,10 @@ static void rewriteFuncWithReturnType(Function &OldF, Value *NewRetValue) {
5557
BasicBlock::iterator NewValIt =
5658
NewRetI ? NewRetI->getIterator() : EntryBB.end();
5759

60+
Type *OldRetTy = OldFuncTy->getReturnType();
61+
5862
// Hack up any return values in other blocks, we can't leave them as ret void.
59-
if (OldFuncTy->getReturnType() != NewRetTy) {
63+
if (OldRetTy != NewRetTy) {
6064
for (BasicBlock &OtherRetBB : OldF) {
6165
if (&OtherRetBB != NewRetBlock) {
6266
auto *OrigRI = dyn_cast<ReturnInst>(OtherRetBB.getTerminator());
@@ -92,6 +96,15 @@ static void rewriteFuncWithReturnType(Function &OldF, Value *NewRetValue) {
9296
// result of our pruning here.
9397
EliminateUnreachableBlocks(OldF);
9498

99+
100+
// Drop the incompatible attributes before we copy over to the new function.
101+
if (OldRetTy != NewRetTy) {
102+
AttributeList AL = OldF.getAttributes();
103+
AttributeMask IncompatibleAttrs =
104+
AttributeFuncs::typeIncompatible(NewRetTy, AL.getRetAttrs());
105+
OldF.removeRetAttrs(IncompatibleAttrs);
106+
}
107+
95108
Function *NewF =
96109
Function::Create(NewFuncTy, OldF.getLinkage(), OldF.getAddressSpace(), "",
97110
OldF.getParent());

0 commit comments

Comments
 (0)