Skip to content

[3.1] SIL: Canonicalize capture types with the AST function's generic signature. #7236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions include/swift/AST/AnyFunctionRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@ class AnyFunctionRef {
}
llvm_unreachable("unexpected AnyFunctionRef representation");
}

GenericSignature *getGenericSignature() const {
if (auto afd = TheFunction.dyn_cast<AbstractFunctionDecl *>()) {
return afd->getGenericSignature();
}
if (auto ce = TheFunction.dyn_cast<AbstractClosureExpr *>()) {
return ce->getGenericSignatureOfContext();
}
llvm_unreachable("unexpected AnyFunctionRef representation");
}
};
#if SWIFT_COMPILER_IS_MSVC
#pragma warning(pop)
Expand Down
15 changes: 12 additions & 3 deletions lib/SIL/SILFunctionType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "swift/AST/Decl.h"
#include "swift/AST/DiagnosticsSIL.h"
#include "swift/AST/ForeignErrorConvention.h"
#include "swift/AST/GenericEnvironment.h"
#include "swift/Basic/Fallthrough.h"
#include "clang/Analysis/DomainSpecific/CocoaConventions.h"
#include "clang/AST/Attr.h"
Expand Down Expand Up @@ -742,9 +743,17 @@ static CanSILFunctionType getSILFunctionType(SILModule &M,
// from the function to which the argument is attached.
if (constant && !constant->isDefaultArgGenerator())
if (auto function = constant->getAnyFunctionRef()) {
auto getCanonicalType = [&](Type t) -> CanType {
if (genericSig)
return genericSig->getCanonicalTypeInContext(t, *M.getSwiftModule());
// NB: The generic signature may be elided from the lowered function type
// if the function is in a fully-specialized context, but we still need to
// canonicalize references to the generic parameters that may appear in
// non-canonical types in that context. We need the original generic
// signature from the AST for that.
auto origGenericSig
= function->getGenericSignature();
auto getCanonicalType = [origGenericSig, &M](Type t) -> CanType {
if (origGenericSig)
return origGenericSig->getCanonicalTypeInContext(t,
*M.getSwiftModule());
return t->getCanonicalType();
};

Expand Down
5 changes: 5 additions & 0 deletions lib/SIL/SILInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,11 @@ ApplyInst *ApplyInst::create(SILDebugLocation Loc, SILValue Callee,
ArrayRef<SILValue> Args, bool isNonThrowing,
SILFunction &F,
SILOpenedArchetypesState &OpenedArchetypes) {
if (!F.getModule().Types.getCurGenericContext())
assert(Callee->getType().castTo<SILFunctionType>()
->substGenericArgs(F.getModule(), Subs)
== SubstCalleeTy.getSwiftRValueType());

SmallVector<SILValue, 32> TypeDependentOperands;
collectTypeDependentOperands(TypeDependentOperands, OpenedArchetypes, F,
SubstCalleeTy.getSwiftRValueType(), Subs);
Expand Down
13 changes: 10 additions & 3 deletions lib/SILGen/SILGenProlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,9 @@ void SILGenFunction::bindParametersForForwarding(const ParameterList *params,
}
}

static void emitCaptureArguments(SILGenFunction &gen, CapturedValue capture,
static void emitCaptureArguments(SILGenFunction &gen,
AnyFunctionRef closure,
CapturedValue capture,
unsigned ArgNo) {

auto *VD = capture.getDecl();
Expand All @@ -357,7 +359,12 @@ static void emitCaptureArguments(SILGenFunction &gen, CapturedValue capture,
auto interfaceType = cast<VarDecl>(VD)->getInterfaceType();
if (!interfaceType->hasTypeParameter()) return interfaceType;

auto genericEnv = gen.F.getGenericEnvironment();
// NB: The generic signature may be elided from the lowered function type
// if the function is in a fully-specialized context, but we still need to
// canonicalize references to the generic parameters that may appear in
// non-canonical types in that context. We need the original generic
// environment from the AST for that.
auto genericEnv = closure.getGenericEnvironment();
return genericEnv->mapTypeIntoContext(gen.F.getModule().getSwiftModule(),
interfaceType);
};
Expand Down Expand Up @@ -446,7 +453,7 @@ void SILGenFunction::emitProlog(AnyFunctionRef TheClosure,
return;
}

emitCaptureArguments(*this, capture, ++ArgNo);
emitCaptureArguments(*this, TheClosure, capture, ++ArgNo);
}
}

Expand Down
14 changes: 14 additions & 0 deletions test/SILGen/capture-canonicalization.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %target-swift-frontend -emit-silgen %s | %FileCheck %s

struct Foo<T> {}
struct Bar {}

extension Foo where T == Bar {
func foo(x: T) -> Bar {
// CHECK-LABEL: sil shared @{{.*}}3foo{{.*}}4foo2{{.*}} : $@convention(thin) (Bar) -> Bar
func foo2() -> Bar {
return x
}
return foo2()
}
}