Skip to content

Commit 1a9eb79

Browse files
authored
Merge pull request #29913 from slavapestov/opaque-result-type-versus-keypath
Fix a couple of problems with opaque result types
2 parents 3fe6b65 + 9fa6d89 commit 1a9eb79

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
@@ -1539,7 +1539,7 @@ class SourceFileScope {
15391539
sgm.TopLevelSGF = new SILGenFunction(sgm, *toplevel, sf);
15401540
sgm.TopLevelSGF->MagicFunctionName = sgm.SwiftModule->getName();
15411541
auto moduleCleanupLoc = CleanupLocation::getModuleCleanupLocation();
1542-
sgm.TopLevelSGF->prepareEpilog(Type(), true, moduleCleanupLoc);
1542+
sgm.TopLevelSGF->prepareEpilog(false, true, moduleCleanupLoc);
15431543

15441544
// Create the argc and argv arguments.
15451545
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);
@@ -210,7 +210,7 @@ void SILGenFunction::emitObjCDestructor(SILDeclRef dtor) {
210210
// Create a basic block to jump to for the implicit destruction behavior
211211
// of releasing the elements and calling the superclass destructor.
212212
// We won't actually emit the block until we finish with the destructor body.
213-
prepareEpilog(Type(), false, CleanupLocation::get(loc));
213+
prepareEpilog(false, false, CleanupLocation::get(loc));
214214

215215
emitProfilerIncrement(dd->getBody());
216216
// 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
@@ -2689,33 +2689,35 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
26892689
}
26902690

26912691
// Build the signature of the thunk as expected by the keypath runtime.
2692-
CanType loweredBaseTy, loweredPropTy;
2693-
AbstractionPattern opaque = AbstractionPattern::getOpaque();
2692+
auto signature = [&]() {
2693+
CanType loweredBaseTy, loweredPropTy;
2694+
AbstractionPattern opaque = AbstractionPattern::getOpaque();
26942695

2695-
loweredBaseTy = SGM.Types.getLoweredRValueType(
2696-
TypeExpansionContext::minimal(), opaque, baseType);
2697-
loweredPropTy = SGM.Types.getLoweredRValueType(
2698-
TypeExpansionContext::minimal(), opaque, propertyType);
2699-
2700-
auto paramConvention = ParameterConvention::Indirect_In_Guaranteed;
2696+
loweredBaseTy = SGM.Types.getLoweredRValueType(
2697+
TypeExpansionContext::minimal(), opaque, baseType);
2698+
loweredPropTy = SGM.Types.getLoweredRValueType(
2699+
TypeExpansionContext::minimal(), opaque, propertyType);
2700+
2701+
auto paramConvention = ParameterConvention::Indirect_In_Guaranteed;
27012702

2702-
SmallVector<SILParameterInfo, 2> params;
2703-
params.push_back({loweredBaseTy, paramConvention});
2704-
auto &C = SGM.getASTContext();
2705-
if (!indexes.empty())
2706-
params.push_back({C.getUnsafeRawPointerDecl()->getDeclaredType()
2707-
->getCanonicalType(),
2708-
ParameterConvention::Direct_Unowned});
2709-
2710-
SILResultInfo result(loweredPropTy, ResultConvention::Indirect);
2711-
2712-
auto signature = SILFunctionType::get(genericSig,
2713-
SILFunctionType::ExtInfo::getThin(),
2714-
SILCoroutineKind::None,
2715-
ParameterConvention::Direct_Unowned,
2716-
params, {}, result, None,
2717-
SubstitutionMap(), false,
2718-
SGM.getASTContext());
2703+
SmallVector<SILParameterInfo, 2> params;
2704+
params.push_back({loweredBaseTy, paramConvention});
2705+
auto &C = SGM.getASTContext();
2706+
if (!indexes.empty())
2707+
params.push_back({C.getUnsafeRawPointerDecl()->getDeclaredType()
2708+
->getCanonicalType(),
2709+
ParameterConvention::Direct_Unowned});
2710+
2711+
SILResultInfo result(loweredPropTy, ResultConvention::Indirect);
2712+
2713+
return SILFunctionType::get(genericSig,
2714+
SILFunctionType::ExtInfo::getThin(),
2715+
SILCoroutineKind::None,
2716+
ParameterConvention::Direct_Unowned,
2717+
params, {}, result, None,
2718+
SubstitutionMap(), false,
2719+
SGM.getASTContext());
2720+
}();
27192721

27202722
// Find the function and see if we already created it.
27212723
auto name = Mangle::ASTMangler()
@@ -2741,9 +2743,15 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
27412743
}
27422744

27432745
SILGenFunction subSGF(SGM, *thunk, SGM.SwiftModule);
2746+
2747+
signature = subSGF.F.getLoweredFunctionTypeInContext(
2748+
subSGF.F.getTypeExpansionContext());
2749+
27442750
auto entry = thunk->begin();
2745-
auto resultArgTy = result.getSILStorageType(SGM.M, signature);
2746-
auto baseArgTy = params[0].getSILStorageType(SGM.M, signature);
2751+
auto resultArgTy = signature->getSingleResult().getSILStorageType(
2752+
SGM.M, signature);
2753+
auto baseArgTy = signature->getParameters()[0].getSILStorageType(
2754+
SGM.M, signature);
27472755
if (genericEnv) {
27482756
resultArgTy = genericEnv->mapTypeIntoContext(SGM.M, resultArgTy);
27492757
baseArgTy = genericEnv->mapTypeIntoContext(SGM.M, baseArgTy);
@@ -2752,7 +2760,8 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
27522760
auto baseArg = entry->createFunctionArgument(baseArgTy);
27532761
SILValue indexPtrArg;
27542762
if (!indexes.empty()) {
2755-
auto indexArgTy = params[1].getSILStorageType(SGM.M, signature);
2763+
auto indexArgTy = signature->getParameters()[1].getSILStorageType(
2764+
SGM.M, signature);
27562765
indexPtrArg = entry->createFunctionArgument(indexArgTy);
27572766
}
27582767

@@ -2821,41 +2830,43 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
28212830
}
28222831

28232832
// Build the signature of the thunk as expected by the keypath runtime.
2824-
CanType loweredBaseTy, loweredPropTy;
2825-
{
2826-
AbstractionPattern opaque = AbstractionPattern::getOpaque();
2833+
auto signature = [&]() {
2834+
CanType loweredBaseTy, loweredPropTy;
2835+
{
2836+
AbstractionPattern opaque = AbstractionPattern::getOpaque();
28272837

2828-
loweredBaseTy = SGM.Types.getLoweredRValueType(
2829-
TypeExpansionContext::minimal(), opaque, baseType);
2830-
loweredPropTy = SGM.Types.getLoweredRValueType(
2831-
TypeExpansionContext::minimal(), opaque, propertyType);
2832-
}
2833-
2834-
auto &C = SGM.getASTContext();
2835-
2836-
auto paramConvention = ParameterConvention::Indirect_In_Guaranteed;
2837-
2838-
SmallVector<SILParameterInfo, 3> params;
2839-
// property value
2840-
params.push_back({loweredPropTy, paramConvention});
2841-
// base
2842-
params.push_back({loweredBaseTy,
2843-
property->isSetterMutating()
2844-
? ParameterConvention::Indirect_Inout
2845-
: paramConvention});
2846-
// indexes
2847-
if (!indexes.empty())
2848-
params.push_back({C.getUnsafeRawPointerDecl()->getDeclaredType()
2849-
->getCanonicalType(),
2850-
ParameterConvention::Direct_Unowned});
2851-
2852-
auto signature = SILFunctionType::get(genericSig,
2853-
SILFunctionType::ExtInfo::getThin(),
2854-
SILCoroutineKind::None,
2855-
ParameterConvention::Direct_Unowned,
2856-
params, {}, {}, None,
2857-
SubstitutionMap(), false,
2858-
SGM.getASTContext());
2838+
loweredBaseTy = SGM.Types.getLoweredRValueType(
2839+
TypeExpansionContext::minimal(), opaque, baseType);
2840+
loweredPropTy = SGM.Types.getLoweredRValueType(
2841+
TypeExpansionContext::minimal(), opaque, propertyType);
2842+
}
2843+
2844+
auto &C = SGM.getASTContext();
2845+
2846+
auto paramConvention = ParameterConvention::Indirect_In_Guaranteed;
2847+
2848+
SmallVector<SILParameterInfo, 3> params;
2849+
// property value
2850+
params.push_back({loweredPropTy, paramConvention});
2851+
// base
2852+
params.push_back({loweredBaseTy,
2853+
property->isSetterMutating()
2854+
? ParameterConvention::Indirect_Inout
2855+
: paramConvention});
2856+
// indexes
2857+
if (!indexes.empty())
2858+
params.push_back({C.getUnsafeRawPointerDecl()->getDeclaredType()
2859+
->getCanonicalType(),
2860+
ParameterConvention::Direct_Unowned});
2861+
2862+
return SILFunctionType::get(genericSig,
2863+
SILFunctionType::ExtInfo::getThin(),
2864+
SILCoroutineKind::None,
2865+
ParameterConvention::Direct_Unowned,
2866+
params, {}, {}, None,
2867+
SubstitutionMap(), false,
2868+
SGM.getASTContext());
2869+
}();
28592870

28602871
// Mangle the name of the thunk to see if we already created it.
28612872
auto name = Mangle::ASTMangler()
@@ -2882,9 +2893,15 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
28822893
}
28832894

28842895
SILGenFunction subSGF(SGM, *thunk, SGM.SwiftModule);
2896+
2897+
signature = subSGF.F.getLoweredFunctionTypeInContext(
2898+
subSGF.F.getTypeExpansionContext());
2899+
28852900
auto entry = thunk->begin();
2886-
auto valueArgTy = params[0].getSILStorageType(SGM.M, signature);
2887-
auto baseArgTy = params[1].getSILStorageType(SGM.M, signature);
2901+
auto valueArgTy = signature->getParameters()[0].getSILStorageType(
2902+
SGM.M, signature);
2903+
auto baseArgTy = signature->getParameters()[1].getSILStorageType(
2904+
SGM.M, signature);
28882905
if (genericEnv) {
28892906
valueArgTy = genericEnv->mapTypeIntoContext(SGM.M, valueArgTy);
28902907
baseArgTy = genericEnv->mapTypeIntoContext(SGM.M, baseArgTy);
@@ -2894,7 +2911,8 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
28942911
SILValue indexPtrArg;
28952912

28962913
if (!indexes.empty()) {
2897-
auto indexArgTy = params[2].getSILStorageType(SGM.M, signature);
2914+
auto indexArgTy = signature->getParameters()[2].getSILStorageType(
2915+
SGM.M, signature);
28982916
indexPtrArg = entry->createFunctionArgument(indexArgTy);
28992917
}
29002918

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());
@@ -743,7 +741,7 @@ void SILGenFunction::emitGeneratorFunction(SILDeclRef function, Expr *value,
743741
dc, interfaceType, /*throws=*/false, SourceLoc());
744742
if (EmitProfilerIncrement)
745743
emitProfilerIncrement(value);
746-
prepareEpilog(value->getType(), false, CleanupLocation::get(Loc));
744+
prepareEpilog(true, false, CleanupLocation::get(Loc));
747745

748746
{
749747
llvm::Optional<SILGenFunction::OpaqueValueRAII> opaqueValue;
@@ -776,21 +774,19 @@ void SILGenFunction::emitGeneratorFunction(SILDeclRef function, VarDecl *var) {
776774
auto decl = function.getAbstractFunctionDecl();
777775
auto *dc = decl->getInnermostDeclContext();
778776
auto interfaceType = var->getValueInterfaceType();
779-
auto varType = var->getType();
780777

781778
// If this is the backing storage for a property with an attached
782779
// wrapper that was initialized with '=', the stored property initializer
783780
// will be in terms of the original property's type.
784781
if (auto originalProperty = var->getOriginalWrappedProperty()) {
785782
if (originalProperty->isPropertyMemberwiseInitializedWithWrappedType()) {
786783
interfaceType = originalProperty->getValueInterfaceType();
787-
varType = originalProperty->getType();
788784
}
789785
}
790786

791787
emitProlog(/*paramList*/ nullptr, /*selfParam*/ nullptr, interfaceType, dc,
792788
/*throws=*/false, SourceLoc());
793-
prepareEpilog(varType, false, CleanupLocation::get(loc));
789+
prepareEpilog(true, false, CleanupLocation::get(loc));
794790

795791
auto pbd = var->getParentPatternBinding();
796792
const auto i = pbd->getPatternEntryIndexForVarDecl(var);

lib/SILGen/SILGenFunction.h

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

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)