Skip to content

Commit 87b109f

Browse files
committed
[SILGen] Add support thunk support for () -> T to () -> Void conversions
(cherry picked from commit cddb821)
1 parent 503d2d8 commit 87b109f

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

lib/SILGen/SILGenPoly.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,6 +1565,9 @@ class ResultPlanner {
15651565
///
15661566
/// Valid: reabstraction info, InnerResult, OuterResult.
15671567
ReabstractDirectToDirect,
1568+
1569+
/// Ignore the next direct inner result, since the outer is 'Void'.
1570+
IgnoreDirectResult,
15681571
};
15691572

15701573
Operation(Kind kind) : TheKind(kind) {}
@@ -1709,6 +1712,24 @@ class ResultPlanner {
17091712
SILResultInfo outerResult,
17101713
SILValue optOuterResultAddr);
17111714

1715+
void planIgnoredResult(AbstractionPattern innerOrigType,
1716+
CanType innerSubstType, PlanData &planData) {
1717+
if (innerOrigType.isTuple()) {
1718+
auto innerSubstTuple = cast<TupleType>(innerSubstType);
1719+
for (unsigned i = 0, n = innerSubstTuple->getNumElements(); i != n; ++i)
1720+
planIgnoredResult(innerOrigType.getTupleElementType(i),
1721+
innerSubstTuple.getElementType(i), planData);
1722+
return;
1723+
}
1724+
1725+
auto innerResult = claimNextInnerResult(planData);
1726+
if (innerResult.isFormalIndirect() &&
1727+
SGF.silConv.isSILIndirect(innerResult))
1728+
(void)addInnerIndirectResultTemporary(planData, innerResult);
1729+
else
1730+
addIgnoreDirectResult();
1731+
}
1732+
17121733
/// Claim the next inner result from the plan data.
17131734
SILResultInfo claimNextInnerResult(PlanData &data) {
17141735
return claimNext(data.InnerResults);
@@ -1858,6 +1879,10 @@ class ResultPlanner {
18581879
op.OuterOrigType = outerOrigType;
18591880
op.OuterSubstType = outerSubstType;
18601881
}
1882+
1883+
void addIgnoreDirectResult() {
1884+
(void)addOperation(Operation::IgnoreDirectResult);
1885+
}
18611886
};
18621887

18631888
} // end anonymous namespace
@@ -1868,6 +1893,13 @@ void ResultPlanner::plan(AbstractionPattern innerOrigType,
18681893
AbstractionPattern outerOrigType,
18691894
CanType outerSubstType,
18701895
PlanData &planData) {
1896+
// Conversion from `() -> T` to `() -> Void` is allowed when
1897+
// the argument is a closure.
1898+
if (!innerSubstType->isVoid() && outerSubstType->isVoid()) {
1899+
planIgnoredResult(innerOrigType, innerSubstType, planData);
1900+
return;
1901+
}
1902+
18711903
// The substituted types must match up in tuple-ness and arity.
18721904
assert(isa<TupleType>(innerSubstType) == isa<TupleType>(outerSubstType) ||
18731905
(isa<TupleType>(innerSubstType) &&
@@ -2580,6 +2612,10 @@ void ResultPlanner::execute(ArrayRef<SILValue> innerDirectResults,
25802612
case Operation::InjectOptionalIndirect:
25812613
SGF.B.createInjectEnumAddr(Loc, op.OuterResultAddr, op.SomeDecl);
25822614
continue;
2615+
2616+
case Operation::IgnoreDirectResult:
2617+
(void)claimNext(innerDirectResults);
2618+
continue;
25832619
}
25842620
llvm_unreachable("bad operation kind");
25852621
}

test/IRGen/objc_retainAutoreleasedReturnValue.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ public func test(_ dict: NSDictionary) {
2424
// popq %rbp ;<== Blocks the handshake from objc_autoreleaseReturnValue
2525
// jmp 0x01ec20 ; symbol stub for: objc_retainAutoreleasedReturnValue
2626

27-
// CHECK-LABEL: define {{.*}}swiftcc void @"$S34objc_retainAutoreleasedReturnValue4testyySo12NSDictionaryCFyADcfU_"(%TSo12NSDictionaryC*)
27+
// CHECK-LABEL: define {{.*}}swiftcc %TSo12NSEnumeratorC* @"$S34objc_retainAutoreleasedReturnValue4testyySo12NSDictionaryCFSo12NSEnumeratorCADcfU_"(%TSo12NSDictionaryC*)
2828
// CHECK: entry:
2929
// CHECK: call {{.*}}@objc_msgSend
3030
// CHECK: notail call i8* @objc_retainAutoreleasedReturnValue
31-
// CHECK: ret void
31+
// CHECK: ret %TSo12NSEnumeratorC*
3232

33-
// OPT-LABEL: define {{.*}}swiftcc void @"$S34objc_retainAutoreleasedReturnValue4testyySo12NSDictionaryCFyADcfU_"(%TSo12NSDictionaryC*)
33+
// CHECK-LABEL: define {{.*}}swiftcc void @"$SSo12NSDictionaryCSo12NSEnumeratorCIgxo_ABIgx_TR"(%TSo12NSDictionaryC*, i8*, %swift.refcounted*)
34+
35+
// OPT-LABEL: define {{.*}}swiftcc void @"$S34objc_retainAutoreleasedReturnValue10useClosureyySo12NSDictionaryC_yADctF06$SSo12h43CSo12NSEnumeratorCIgxo_ABIgx_TR049$S34objc_bcD41Value4testyySo12a6CFSo12B7CADcfU_Tf3npf_nTf1nc_nTf4g_n"(%TSo12NSDictionaryC*)
3436
// OPT: entry:
3537
// OPT: call {{.*}}@objc_msgSend
3638
// OPT: notail call i8* @objc_retainAutoreleasedReturnValue

0 commit comments

Comments
 (0)