Skip to content

Commit fda5549

Browse files
committed
SwiftMergeFunctionsPass: fix a problem with opaque pointers
Need to insert a cast for the return type of a call to a merged function. Fixes an LLVM verifier crash. rdar://113901800
1 parent 117a5ec commit fda5549

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

lib/LLVMPasses/LLVMMergeFunctions.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,9 @@ void SwiftMergeFunctions::removeEquivalenceClassFromTree(FunctionEntry *FE) {
11881188
// Selects proper bitcast operation,
11891189
// but a bit simpler then CastInst::getCastOpcode.
11901190
static Value *createCast(IRBuilder<> &Builder, Value *V, Type *DestTy) {
1191+
if (V->getType() == DestTy)
1192+
return V;
1193+
11911194
Type *SrcTy = V->getType();
11921195
if (SrcTy->isStructTy()) {
11931196
assert(DestTy->isStructTy());
@@ -1357,7 +1360,8 @@ bool SwiftMergeFunctions::replaceDirectCallers(Function *Old, Function *New,
13571360
NewArgAttrs);
13581361
newAttrList = fixUpTypesInByValAndStructRetAttributes(FType, newAttrList);
13591362
NewCI->setAttributes(newAttrList);
1360-
CI->replaceAllUsesWith(NewCI);
1363+
Value *retVal = createCast(Builder, NewCI, CI->getType());
1364+
CI->replaceAllUsesWith(retVal);
13611365
CI->eraseFromParent();
13621366
}
13631367
assert(Old->use_empty() && "should have replaced all uses of old function");
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
; RUN: %swift-llvm-opt -swift-merge-functions -swiftmergefunc-threshold=4 -opaque-pointers %s | %FileCheck %s
2+
3+
; REQUIRES: PTRSIZE=64
4+
5+
@g1 = external global i32
6+
7+
define internal i64 @return_0(i32 %x, i32 %y) {
8+
%sum = add i32 %x, %y
9+
%sum2 = add i32 %sum, %y
10+
%l = load i32, i32* @g1, align 4
11+
%sum3 = add i32 %sum2, %y
12+
ret i64 0
13+
}
14+
15+
define internal ptr @return_null(i32 %x, i32 %y) {
16+
%sum = add i32 %x, %y
17+
%sum2 = add i32 %sum, %y
18+
%l = load i32, i32* @g1, align 4
19+
%sum3 = add i32 %sum2, %y
20+
ret ptr null
21+
}
22+
23+
declare void @take_results(i64, ptr)
24+
25+
26+
; CHECK-LABEL: define void @call_return_null
27+
; CHECK: %1 = call i64 @return_0Tm(i32 0, i32 0)
28+
; CHECK: %2 = call ptr @return_0Tm(i32 0, i32 0)
29+
define void @call_return_null() {
30+
%r1 = call i64 @return_0(i32 0, i32 0)
31+
%r2 = call ptr @return_null(i32 0, i32 0)
32+
call void @take_results(i64 %r1, ptr %r2)
33+
ret void
34+
}

0 commit comments

Comments
 (0)