Skip to content

Commit cb5f4e9

Browse files
authored
Merge pull request #29914 from slavapestov/opaque-result-type-versus-keypath-5.2
Fix a couple of problems with opaque result types [5.2]
2 parents 1ea9441 + 8f91555 commit cb5f4e9

10 files changed

+129
-90
lines changed

lib/SIL/AbstractionPattern.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,16 @@ bool AbstractionPattern::matchesTuple(CanTupleType substType) {
274274
return getNumTupleElements_Stored() == substType->getNumElements();
275275
case Kind::ClangType:
276276
case Kind::Type:
277-
case Kind::Discard:
277+
case Kind::Discard: {
278278
if (isTypeParameter())
279279
return true;
280-
auto tuple = dyn_cast<TupleType>(getType());
281-
return (tuple && tuple->getNumElements() == substType->getNumElements());
280+
auto type = getType();
281+
if (auto tuple = dyn_cast<TupleType>(type))
282+
return (tuple->getNumElements() == substType->getNumElements());
283+
if (isa<OpaqueTypeArchetypeType>(type))
284+
return true;
285+
return false;
286+
}
282287
}
283288
llvm_unreachable("bad kind");
284289
}

lib/SILGen/SILGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,7 @@ class SourceFileScope {
15361536
sgm.TopLevelSGF = new SILGenFunction(sgm, *toplevel, sf);
15371537
sgm.TopLevelSGF->MagicFunctionName = sgm.SwiftModule->getName();
15381538
auto moduleCleanupLoc = CleanupLocation::getModuleCleanupLocation();
1539-
sgm.TopLevelSGF->prepareEpilog(Type(), true, moduleCleanupLoc);
1539+
sgm.TopLevelSGF->prepareEpilog(false, true, moduleCleanupLoc);
15401540

15411541
// Create the argc and argv arguments.
15421542
auto prologueLoc = RegularLocation::getModuleLocation();

lib/SILGen/SILGenConstructor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ void SILGenFunction::emitValueConstructor(ConstructorDecl *ctor) {
281281
// Create a basic block to jump to for the implicit 'self' return.
282282
// We won't emit this until after we've emitted the body.
283283
// The epilog takes a void return because the return of 'self' is implicit.
284-
prepareEpilog(Type(), ctor->hasThrows(), CleanupLocation(ctor));
284+
prepareEpilog(false, ctor->hasThrows(), CleanupLocation(ctor));
285285

286286
// If the constructor can fail, set up an alternative epilog for constructor
287287
// failure.
@@ -659,7 +659,7 @@ void SILGenFunction::emitClassConstructorInitializer(ConstructorDecl *ctor) {
659659

660660
// Create a basic block to jump to for the implicit 'self' return.
661661
// We won't emit the block until after we've emitted the body.
662-
prepareEpilog(Type(), ctor->hasThrows(),
662+
prepareEpilog(false, ctor->hasThrows(),
663663
CleanupLocation::get(endOfInitLoc));
664664

665665
auto resultType = ctor->mapTypeIntoContext(ctor->getResultInterfaceType());
@@ -1000,7 +1000,7 @@ void SILGenFunction::emitIVarInitializer(SILDeclRef ivarInitializer) {
10001000
VarLocs[selfDecl] = VarLoc::get(selfArg);
10011001

10021002
auto cleanupLoc = CleanupLocation::get(loc);
1003-
prepareEpilog(TupleType::getEmpty(getASTContext()), false, cleanupLoc);
1003+
prepareEpilog(false, false, cleanupLoc);
10041004

10051005
// Emit the initializers.
10061006
emitMemberInitializers(cd, selfDecl, cd);

lib/SILGen/SILGenDestructor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void SILGenFunction::emitDestroyingDestructor(DestructorDecl *dd) {
3333
// Create a basic block to jump to for the implicit destruction behavior
3434
// of releasing the elements and calling the superclass destructor.
3535
// We won't actually emit the block until we finish with the destructor body.
36-
prepareEpilog(Type(), false, CleanupLocation::get(Loc));
36+
prepareEpilog(false, false, CleanupLocation::get(Loc));
3737

3838
emitProfilerIncrement(dd->getBody());
3939
// Emit the destructor body.
@@ -166,7 +166,7 @@ void SILGenFunction::emitIVarDestroyer(SILDeclRef ivarDestroyer) {
166166
emitSelfDecl(cd->getDestructor()->getImplicitSelfDecl()));
167167

168168
auto cleanupLoc = CleanupLocation::get(loc);
169-
prepareEpilog(TupleType::getEmpty(getASTContext()), false, cleanupLoc);
169+
prepareEpilog(false, false, cleanupLoc);
170170
{
171171
Scope S(*this, cleanupLoc);
172172
emitClassMemberDestruction(selfValue, cd, cleanupLoc);
@@ -206,7 +206,7 @@ void SILGenFunction::emitObjCDestructor(SILDeclRef dtor) {
206206
// Create a basic block to jump to for the implicit destruction behavior
207207
// of releasing the elements and calling the superclass destructor.
208208
// We won't actually emit the block until we finish with the destructor body.
209-
prepareEpilog(Type(), false, CleanupLocation::get(loc));
209+
prepareEpilog(false, false, CleanupLocation::get(loc));
210210

211211
emitProfilerIncrement(dd->getBody());
212212
// Emit the destructor body.

lib/SILGen/SILGenEpilog.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@
1818
using namespace swift;
1919
using namespace Lowering;
2020

21-
void SILGenFunction::prepareEpilog(Type resultType, bool isThrowing,
21+
void SILGenFunction::prepareEpilog(bool hasDirectResults, bool isThrowing,
2222
CleanupLocation CleanupL) {
2323
auto *epilogBB = createBasicBlock();
2424

2525
// If we have any direct results, receive them via BB arguments.
26-
// But callers can disable this by passing a null result type.
27-
if (resultType) {
26+
if (hasDirectResults) {
2827
auto fnConv = F.getConventions();
2928
// Set NeedsReturn for indirect or direct results. This ensures that SILGen
3029
// emits unreachable if there is no source level return.

lib/SILGen/SILGenExpr.cpp

Lines changed: 83 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2659,33 +2659,35 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
26592659
}
26602660

26612661
// Build the signature of the thunk as expected by the keypath runtime.
2662-
CanType loweredBaseTy, loweredPropTy;
2663-
AbstractionPattern opaque = AbstractionPattern::getOpaque();
2662+
auto signature = [&]() {
2663+
CanType loweredBaseTy, loweredPropTy;
2664+
AbstractionPattern opaque = AbstractionPattern::getOpaque();
26642665

2665-
loweredBaseTy = SGM.Types.getLoweredRValueType(
2666-
TypeExpansionContext::minimal(), opaque, baseType);
2667-
loweredPropTy = SGM.Types.getLoweredRValueType(
2668-
TypeExpansionContext::minimal(), opaque, propertyType);
2669-
2670-
auto paramConvention = ParameterConvention::Indirect_In_Guaranteed;
2666+
loweredBaseTy = SGM.Types.getLoweredRValueType(
2667+
TypeExpansionContext::minimal(), opaque, baseType);
2668+
loweredPropTy = SGM.Types.getLoweredRValueType(
2669+
TypeExpansionContext::minimal(), opaque, propertyType);
2670+
2671+
auto paramConvention = ParameterConvention::Indirect_In_Guaranteed;
26712672

2672-
SmallVector<SILParameterInfo, 2> params;
2673-
params.push_back({loweredBaseTy, paramConvention});
2674-
auto &C = SGM.getASTContext();
2675-
if (!indexes.empty())
2676-
params.push_back({C.getUnsafeRawPointerDecl()->getDeclaredType()
2677-
->getCanonicalType(),
2678-
ParameterConvention::Direct_Unowned});
2679-
2680-
SILResultInfo result(loweredPropTy, ResultConvention::Indirect);
2681-
2682-
auto signature = SILFunctionType::get(genericSig,
2683-
SILFunctionType::ExtInfo::getThin(),
2684-
SILCoroutineKind::None,
2685-
ParameterConvention::Direct_Unowned,
2686-
params, {}, result, None,
2687-
SubstitutionMap(), false,
2688-
SGM.getASTContext());
2673+
SmallVector<SILParameterInfo, 2> params;
2674+
params.push_back({loweredBaseTy, paramConvention});
2675+
auto &C = SGM.getASTContext();
2676+
if (!indexes.empty())
2677+
params.push_back({C.getUnsafeRawPointerDecl()->getDeclaredType()
2678+
->getCanonicalType(),
2679+
ParameterConvention::Direct_Unowned});
2680+
2681+
SILResultInfo result(loweredPropTy, ResultConvention::Indirect);
2682+
2683+
return SILFunctionType::get(genericSig,
2684+
SILFunctionType::ExtInfo::getThin(),
2685+
SILCoroutineKind::None,
2686+
ParameterConvention::Direct_Unowned,
2687+
params, {}, result, None,
2688+
SubstitutionMap(), false,
2689+
SGM.getASTContext());
2690+
}();
26892691

26902692
// Find the function and see if we already created it.
26912693
auto name = Mangle::ASTMangler()
@@ -2711,9 +2713,15 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
27112713
}
27122714

27132715
SILGenFunction subSGF(SGM, *thunk, SGM.SwiftModule);
2716+
2717+
signature = subSGF.F.getLoweredFunctionTypeInContext(
2718+
subSGF.F.getTypeExpansionContext());
2719+
27142720
auto entry = thunk->begin();
2715-
auto resultArgTy = result.getSILStorageType(SGM.M, signature);
2716-
auto baseArgTy = params[0].getSILStorageType(SGM.M, signature);
2721+
auto resultArgTy = signature->getSingleResult().getSILStorageType(
2722+
SGM.M, signature);
2723+
auto baseArgTy = signature->getParameters()[0].getSILStorageType(
2724+
SGM.M, signature);
27172725
if (genericEnv) {
27182726
resultArgTy = genericEnv->mapTypeIntoContext(SGM.M, resultArgTy);
27192727
baseArgTy = genericEnv->mapTypeIntoContext(SGM.M, baseArgTy);
@@ -2722,7 +2730,8 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
27222730
auto baseArg = entry->createFunctionArgument(baseArgTy);
27232731
SILValue indexPtrArg;
27242732
if (!indexes.empty()) {
2725-
auto indexArgTy = params[1].getSILStorageType(SGM.M, signature);
2733+
auto indexArgTy = signature->getParameters()[1].getSILStorageType(
2734+
SGM.M, signature);
27262735
indexPtrArg = entry->createFunctionArgument(indexArgTy);
27272736
}
27282737

@@ -2792,41 +2801,43 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
27922801
}
27932802

27942803
// Build the signature of the thunk as expected by the keypath runtime.
2795-
CanType loweredBaseTy, loweredPropTy;
2796-
{
2797-
AbstractionPattern opaque = AbstractionPattern::getOpaque();
2804+
auto signature = [&]() {
2805+
CanType loweredBaseTy, loweredPropTy;
2806+
{
2807+
AbstractionPattern opaque = AbstractionPattern::getOpaque();
27982808

2799-
loweredBaseTy = SGM.Types.getLoweredRValueType(
2800-
TypeExpansionContext::minimal(), opaque, baseType);
2801-
loweredPropTy = SGM.Types.getLoweredRValueType(
2802-
TypeExpansionContext::minimal(), opaque, propertyType);
2803-
}
2804-
2805-
auto &C = SGM.getASTContext();
2806-
2807-
auto paramConvention = ParameterConvention::Indirect_In_Guaranteed;
2808-
2809-
SmallVector<SILParameterInfo, 3> params;
2810-
// property value
2811-
params.push_back({loweredPropTy, paramConvention});
2812-
// base
2813-
params.push_back({loweredBaseTy,
2814-
property->isSetterMutating()
2815-
? ParameterConvention::Indirect_Inout
2816-
: paramConvention});
2817-
// indexes
2818-
if (!indexes.empty())
2819-
params.push_back({C.getUnsafeRawPointerDecl()->getDeclaredType()
2820-
->getCanonicalType(),
2821-
ParameterConvention::Direct_Unowned});
2822-
2823-
auto signature = SILFunctionType::get(genericSig,
2824-
SILFunctionType::ExtInfo::getThin(),
2825-
SILCoroutineKind::None,
2826-
ParameterConvention::Direct_Unowned,
2827-
params, {}, {}, None,
2828-
SubstitutionMap(), false,
2829-
SGM.getASTContext());
2809+
loweredBaseTy = SGM.Types.getLoweredRValueType(
2810+
TypeExpansionContext::minimal(), opaque, baseType);
2811+
loweredPropTy = SGM.Types.getLoweredRValueType(
2812+
TypeExpansionContext::minimal(), opaque, propertyType);
2813+
}
2814+
2815+
auto &C = SGM.getASTContext();
2816+
2817+
auto paramConvention = ParameterConvention::Indirect_In_Guaranteed;
2818+
2819+
SmallVector<SILParameterInfo, 3> params;
2820+
// property value
2821+
params.push_back({loweredPropTy, paramConvention});
2822+
// base
2823+
params.push_back({loweredBaseTy,
2824+
property->isSetterMutating()
2825+
? ParameterConvention::Indirect_Inout
2826+
: paramConvention});
2827+
// indexes
2828+
if (!indexes.empty())
2829+
params.push_back({C.getUnsafeRawPointerDecl()->getDeclaredType()
2830+
->getCanonicalType(),
2831+
ParameterConvention::Direct_Unowned});
2832+
2833+
return SILFunctionType::get(genericSig,
2834+
SILFunctionType::ExtInfo::getThin(),
2835+
SILCoroutineKind::None,
2836+
ParameterConvention::Direct_Unowned,
2837+
params, {}, {}, None,
2838+
SubstitutionMap(), false,
2839+
SGM.getASTContext());
2840+
}();
28302841

28312842
// Mangle the name of the thunk to see if we already created it.
28322843
auto name = Mangle::ASTMangler()
@@ -2853,9 +2864,15 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
28532864
}
28542865

28552866
SILGenFunction subSGF(SGM, *thunk, SGM.SwiftModule);
2867+
2868+
signature = subSGF.F.getLoweredFunctionTypeInContext(
2869+
subSGF.F.getTypeExpansionContext());
2870+
28562871
auto entry = thunk->begin();
2857-
auto valueArgTy = params[0].getSILStorageType(SGM.M, signature);
2858-
auto baseArgTy = params[1].getSILStorageType(SGM.M, signature);
2872+
auto valueArgTy = signature->getParameters()[0].getSILStorageType(
2873+
SGM.M, signature);
2874+
auto baseArgTy = signature->getParameters()[1].getSILStorageType(
2875+
SGM.M, signature);
28592876
if (genericEnv) {
28602877
valueArgTy = genericEnv->mapTypeIntoContext(SGM.M, valueArgTy);
28612878
baseArgTy = genericEnv->mapTypeIntoContext(SGM.M, baseArgTy);
@@ -2865,7 +2882,8 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
28652882
SILValue indexPtrArg;
28662883

28672884
if (!indexes.empty()) {
2868-
auto indexArgTy = params[2].getSILStorageType(SGM.M, signature);
2885+
auto indexArgTy = signature->getParameters()[2].getSILStorageType(
2886+
SGM.M, signature);
28692887
indexPtrArg = entry->createFunctionArgument(indexArgTy);
28702888
}
28712889

lib/SILGen/SILGenFunction.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,7 @@ void SILGenFunction::emitFunction(FuncDecl *fd) {
487487
auto captureInfo = SGM.M.Types.getLoweredLocalCaptures(SILDeclRef(fd));
488488
emitProlog(captureInfo, fd->getParameters(), fd->getImplicitSelfDecl(), fd,
489489
fd->getResultInterfaceType(), fd->hasThrows(), fd->getThrowsLoc());
490-
Type resultTy = fd->mapTypeIntoContext(fd->getResultInterfaceType());
491-
prepareEpilog(resultTy, fd->hasThrows(), CleanupLocation(fd));
490+
prepareEpilog(true, fd->hasThrows(), CleanupLocation(fd));
492491

493492
emitProfilerIncrement(fd->getBody());
494493
emitStmt(fd->getBody());
@@ -506,8 +505,7 @@ void SILGenFunction::emitClosure(AbstractClosureExpr *ace) {
506505
SILDeclRef(ace));
507506
emitProlog(captureInfo, ace->getParameters(), /*selfParam=*/nullptr,
508507
ace, resultIfaceTy, ace->isBodyThrowing(), ace->getLoc());
509-
prepareEpilog(ace->getResultType(), ace->isBodyThrowing(),
510-
CleanupLocation(ace));
508+
prepareEpilog(true, ace->isBodyThrowing(), CleanupLocation(ace));
511509
emitProfilerIncrement(ace);
512510
if (auto *ce = dyn_cast<ClosureExpr>(ace)) {
513511
emitStmt(ce->getBody());
@@ -773,7 +771,7 @@ void SILGenFunction::emitGeneratorFunction(SILDeclRef function, Expr *value,
773771
dc, interfaceType, /*throws=*/false, SourceLoc());
774772
if (EmitProfilerIncrement)
775773
emitProfilerIncrement(value);
776-
prepareEpilog(value->getType(), false, CleanupLocation::get(Loc));
774+
prepareEpilog(true, false, CleanupLocation::get(Loc));
777775

778776
{
779777
llvm::Optional<SILGenFunction::OpaqueValueRAII> opaqueValue;
@@ -806,21 +804,19 @@ void SILGenFunction::emitGeneratorFunction(SILDeclRef function, VarDecl *var) {
806804
auto decl = function.getAbstractFunctionDecl();
807805
auto *dc = decl->getInnermostDeclContext();
808806
auto interfaceType = var->getValueInterfaceType();
809-
auto varType = var->getType();
810807

811808
// If this is the backing storage for a property with an attached
812809
// wrapper that was initialized with '=', the stored property initializer
813810
// will be in terms of the original property's type.
814811
if (auto originalProperty = var->getOriginalWrappedProperty()) {
815812
if (originalProperty->isPropertyMemberwiseInitializedWithWrappedType()) {
816813
interfaceType = originalProperty->getValueInterfaceType();
817-
varType = originalProperty->getType();
818814
}
819815
}
820816

821817
emitProlog(/*paramList*/ nullptr, /*selfParam*/ nullptr, interfaceType, dc,
822818
/*throws=*/false, SourceLoc());
823-
prepareEpilog(varType, false, CleanupLocation::get(loc));
819+
prepareEpilog(true, false, CleanupLocation::get(loc));
824820

825821
auto pbd = var->getParentPatternBinding();
826822
const auto i = pbd->getPatternEntryIndexForVarDecl(var);

lib/SILGen/SILGenFunction.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -828,13 +828,12 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
828828
/// Create (but do not emit) the epilog branch, and save the
829829
/// current cleanups depth as the destination for return statement branches.
830830
///
831-
/// \param returnType If non-null, the epilog block will be created with an
832-
/// argument of this type to receive the return value for
833-
/// the function.
831+
/// \param hasDirectResults If true, the epilog block will be created with
832+
/// arguments for each direct result of this function.
834833
/// \param isThrowing If true, create an error epilog block.
835834
/// \param L The SILLocation which should be associated with
836835
/// cleanup instructions.
837-
void prepareEpilog(Type returnType, bool isThrowing, CleanupLocation L);
836+
void prepareEpilog(bool hasDirectResults, bool isThrowing, CleanupLocation L);
838837
void prepareRethrowEpilog(CleanupLocation l);
839838
void prepareCoroutineUnwindEpilog(CleanupLocation l);
840839

test/SILGen/opaque_result_type.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,11 @@ public class D {
165165
return d
166166
}()
167167
}
168+
169+
// CHECK-LABEL: sil [ossa] @$s18opaque_result_type10tupleAsAnyQryF : $@convention(thin) () -> @out @_opaqueReturnTypeOf("$s18opaque_result_type10tupleAsAnyQryF", 0) 🦸 {
170+
public func tupleAsAny() -> some Any {
171+
// CHECK-NEXT: bb0(%0 : $*()):
172+
// CHECK-NEXT: %1 = tuple ()
173+
// CHECK-NEXT: return %1 : $()
174+
return ()
175+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %target-swift-emit-silgen %s -enable-library-evolution -disable-availability-checking | %FileCheck %s
2+
3+
public protocol V {}
4+
public struct E : V {}
5+
6+
public class HasProperty {
7+
public var foo: some V = E()
8+
}
9+
10+
// CHECK-LABEL: sil shared [thunk] [ossa] @$s33opaque_result_type_class_property11HasPropertyC3fooQrvpACTK : $@convention(thin) (@in_guaranteed HasProperty) -> @out @_opaqueReturnTypeOf("$s33opaque_result_type_class_property11HasPropertyC3fooQrvp", 0) 🦸 {
11+
// CHECK: bb0(%0 : $*E, %1 : $*HasProperty):
12+
13+
// CHECK-LABEL: sil shared [thunk] [ossa] @$s33opaque_result_type_class_property11HasPropertyC3fooQrvpACTk : $@convention(thin) (@in_guaranteed @_opaqueReturnTypeOf("$s33opaque_result_type_class_property11HasPropertyC3fooQrvp", 0) 🦸, @in_guaranteed HasProperty) -> () {
14+
// CHECK: bb0(%0 : $*E, %1 : $*HasProperty):

0 commit comments

Comments
 (0)