Skip to content

[SILGen] Fix the type of closure thunks that are passed const T& #83426

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
31 changes: 16 additions & 15 deletions include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "swift/AST/FunctionRefInfo.h"
#include "swift/AST/ProtocolConformanceRef.h"
#include "swift/AST/ThrownErrorDestination.h"
#include "swift/AST/Type.h"
#include "swift/AST/TypeAlignments.h"
#include "swift/Basic/Debug.h"
#include "swift/Basic/InlineBitfield.h"
Expand Down Expand Up @@ -3368,8 +3369,8 @@ class UnresolvedTypeConversionExpr : public ImplicitConversionExpr {
class FunctionConversionExpr : public ImplicitConversionExpr {
public:
FunctionConversionExpr(Expr *subExpr, Type type)
: ImplicitConversionExpr(ExprKind::FunctionConversion, subExpr, type) {}
: ImplicitConversionExpr(ExprKind::FunctionConversion, subExpr, type) {}

static bool classof(const Expr *E) {
return E->getKind() == ExprKind::FunctionConversion;
}
Expand Down Expand Up @@ -4301,19 +4302,19 @@ class ClosureExpr : public AbstractClosureExpr {
}

public:
ClosureExpr(const DeclAttributes &attributes,
SourceRange bracketRange, VarDecl *capturedSelfDecl,
ParameterList *params, SourceLoc asyncLoc, SourceLoc throwsLoc,
TypeExpr *thrownType, SourceLoc arrowLoc, SourceLoc inLoc,
TypeExpr *explicitResultType, DeclContext *parent)
: AbstractClosureExpr(ExprKind::Closure, Type(), /*Implicit=*/false,
parent),
Attributes(attributes), BracketRange(bracketRange),
CapturedSelfDecl(capturedSelfDecl),
AsyncLoc(asyncLoc), ThrowsLoc(throwsLoc), ArrowLoc(arrowLoc),
InLoc(inLoc), ThrownType(thrownType),
ExplicitResultTypeAndBodyState(explicitResultType, BodyState::Parsed),
Body(nullptr) {
ClosureExpr(const DeclAttributes &attributes, SourceRange bracketRange,
VarDecl *capturedSelfDecl, ParameterList *params,
SourceLoc asyncLoc, SourceLoc throwsLoc, TypeExpr *thrownType,
SourceLoc arrowLoc, SourceLoc inLoc, TypeExpr *explicitResultType,
DeclContext *parent)
: AbstractClosureExpr(ExprKind::Closure, Type(), /*Implicit=*/false,
parent),
Attributes(attributes), BracketRange(bracketRange),
CapturedSelfDecl(capturedSelfDecl), AsyncLoc(asyncLoc),
ThrowsLoc(throwsLoc), ArrowLoc(arrowLoc), InLoc(inLoc),
ThrownType(thrownType),
ExplicitResultTypeAndBodyState(explicitResultType, BodyState::Parsed),
Body(nullptr) {
setParameterList(params);
Bits.ClosureExpr.HasAnonymousClosureVars = false;
Bits.ClosureExpr.ImplicitSelfCapture = false;
Expand Down
6 changes: 3 additions & 3 deletions include/swift/SIL/SILBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ struct BridgedSuccessorArray {
};

struct BridgedDeclRef {
uint64_t storage[3];
uint64_t storage[4];

BRIDGED_INLINE BridgedDeclRef(swift::SILDeclRef declRef);
BRIDGED_INLINE swift::SILDeclRef unbridged() const;
Expand All @@ -987,7 +987,7 @@ struct BridgedDeclRef {
};

struct BridgedVTableEntry {
uint64_t storage[5];
uint64_t storage[6];

enum class Kind {
Normal,
Expand Down Expand Up @@ -1055,7 +1055,7 @@ struct BridgedConstExprFunctionState {
};

struct BridgedWitnessTableEntry {
uint64_t storage[5];
uint64_t storage[6];

enum class Kind {
invalid,
Expand Down
16 changes: 11 additions & 5 deletions include/swift/SIL/SILDeclRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ namespace llvm {
class raw_ostream;
}

namespace clang {
class Type;
}

namespace swift {
enum class EffectsKind : uint8_t;
class AbstractFunctionDecl;
Expand Down Expand Up @@ -208,6 +212,9 @@ struct SILDeclRef {
const GenericSignatureImpl *, CustomAttr *>
pointer;

// Type of closure thunk.
const clang::Type *thunkType = nullptr;

/// Returns the type of AST node location being stored by the SILDeclRef.
LocKind getLocKind() const {
if (loc.is<ValueDecl *>())
Expand Down Expand Up @@ -261,11 +268,10 @@ struct SILDeclRef {
/// for the containing ClassDecl.
/// - If 'loc' is a global VarDecl, this returns its GlobalAccessor
/// SILDeclRef.
explicit SILDeclRef(
Loc loc,
bool isForeign = false,
bool isDistributed = false,
bool isDistributedLocal = false);
explicit SILDeclRef(Loc loc, bool isForeign = false,
bool isDistributed = false,
bool isDistributedLocal = false,
const clang::Type *thunkType = nullptr);

/// See above put produces a prespecialization according to the signature.
explicit SILDeclRef(Loc loc, GenericSignature prespecializationSig);
Expand Down
8 changes: 6 additions & 2 deletions lib/SIL/IR/SILDeclRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,15 @@ SILDeclRef::SILDeclRef(ValueDecl *vd, SILDeclRef::Kind kind, bool isForeign,
isAsyncLetClosure(0), pointer(derivativeId) {}

SILDeclRef::SILDeclRef(SILDeclRef::Loc baseLoc, bool asForeign,
bool asDistributed, bool asDistributedKnownToBeLocal)
bool asDistributed, bool asDistributedKnownToBeLocal,
const clang::Type *thunkType)
: isRuntimeAccessible(false),
backDeploymentKind(SILDeclRef::BackDeploymentKind::None),
defaultArgIndex(0), isAsyncLetClosure(0),
pointer((AutoDiffDerivativeFunctionIdentifier *)nullptr) {
pointer((AutoDiffDerivativeFunctionIdentifier *)nullptr),
thunkType(thunkType) {
assert((!thunkType || baseLoc.is<AbstractClosureExpr *>()) &&
"thunk type is needed only for closures");
if (auto *vd = baseLoc.dyn_cast<ValueDecl*>()) {
if (auto *fd = dyn_cast<FuncDecl>(vd)) {
// Map FuncDecls directly to Func SILDeclRefs.
Expand Down
16 changes: 12 additions & 4 deletions lib/SIL/IR/SILFunctionType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
//
//===----------------------------------------------------------------------===//

#include "swift/AST/Expr.h"
#include "swift/AST/Type.h"
#define DEBUG_TYPE "libsil"

#include "swift/AST/AnyFunctionRef.h"
Expand Down Expand Up @@ -4321,10 +4323,12 @@ static CanSILFunctionType getUncachedSILFunctionTypeForConstant(
// The type of the native-to-foreign thunk for a swift closure.
if (constant.isForeign && constant.hasClosureExpr() &&
shouldStoreClangType(TC.getDeclRefRepresentation(constant))) {
auto clangType = TC.Context.getClangFunctionType(
origLoweredInterfaceType->getParams(),
origLoweredInterfaceType->getResult(),
FunctionTypeRepresentation::CFunctionPointer);
auto clangType = extInfoBuilder.getClangTypeInfo().getType();
if (!clangType)
clangType = TC.Context.getClangFunctionType(
origLoweredInterfaceType->getParams(),
origLoweredInterfaceType->getResult(),
FunctionTypeRepresentation::CFunctionPointer);
AbstractionPattern pattern =
AbstractionPattern(origLoweredInterfaceType, clangType);
return getSILFunctionTypeForAbstractCFunction(
Expand Down Expand Up @@ -4834,9 +4838,13 @@ getAbstractionPatternForConstant(ASTContext &ctx, SILDeclRef constant,
if (!constant.isForeign)
return AbstractionPattern(fnType);

if (constant.thunkType)
return AbstractionPattern(fnType, constant.thunkType);

auto bridgedFn = getBridgedFunction(constant);
if (!bridgedFn)
return AbstractionPattern(fnType);

const clang::Decl *clangDecl = bridgedFn->getClangDecl();
if (!clangDecl)
return AbstractionPattern(fnType);
Expand Down
31 changes: 22 additions & 9 deletions lib/SILGen/Conversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
#ifndef SWIFT_LOWERING_CONVERSION_H
#define SWIFT_LOWERING_CONVERSION_H

#include "swift/Basic/Assertions.h"
#include "swift/Basic/ExternalUnion.h"
#include "Initialization.h"
#include "SGFContext.h"
#include "swift/Basic/Assertions.h"
#include "swift/Basic/ExternalUnion.h"
#include "swift/SIL/AbstractionPattern.h"
#include <optional>

namespace swift {
namespace Lowering {
Expand Down Expand Up @@ -115,6 +117,7 @@ class Conversion {

struct BridgingStorage {
bool IsExplicit;
AbstractionPattern InputOrigType;
};

/// The types we store for reabstracting contexts. In general, when
Expand Down Expand Up @@ -161,11 +164,11 @@ class Conversion {
static_assert(decltype(Types)::union_is_trivially_copyable,
"define the special members if this changes");

Conversion(KindTy kind, CanType sourceType, CanType resultType,
SILType loweredResultTy, bool isExplicit)
Conversion(KindTy kind, AbstractionPattern inputOrigType, CanType sourceType,
CanType resultType, SILType loweredResultTy, bool isExplicit)
: Kind(kind), SourceType(sourceType), ResultType(resultType),
LoweredResultType(loweredResultTy) {
Types.emplaceAggregate<BridgingStorage>(kind, isExplicit);
Types.emplaceAggregate<BridgingStorage>(kind, isExplicit, inputOrigType);
}

Conversion(AbstractionPattern inputOrigType, CanType inputSubstType,
Expand Down Expand Up @@ -236,13 +239,19 @@ class Conversion {
outputOrigType, outputSubstType, outputLoweredTy);
}

static Conversion getBridging(KindTy kind, CanType origType,
CanType resultType, SILType loweredResultTy,
bool isExplicit = false) {
static Conversion
getBridging(KindTy kind, CanType origType, CanType resultType,
SILType loweredResultTy,
std::optional<AbstractionPattern> inputOrigType = std::nullopt,
bool isExplicit = false) {
assert(isBridgingKind(kind));
assert((kind != Subtype || isAllowedConversion(origType, resultType)) &&
"disallowed conversion for subtype relationship");
return Conversion(kind, origType, resultType, loweredResultTy, isExplicit);
if (inputOrigType)
return Conversion(kind, *inputOrigType, origType, resultType,
loweredResultTy, isExplicit);
return Conversion(kind, AbstractionPattern(origType), origType, resultType,
loweredResultTy, isExplicit);
}

static Conversion getSubtype(CanType origType, CanType substType,
Expand Down Expand Up @@ -290,6 +299,10 @@ class Conversion {
return Types.get<BridgingStorage>(Kind).IsExplicit;
}

AbstractionPattern getBridgingOriginalInputType() const {
return Types.get<BridgingStorage>(Kind).InputOrigType;
}

CanType getSourceType() const {
return SourceType;
}
Expand Down
8 changes: 4 additions & 4 deletions lib/SILGen/SILGenApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4258,10 +4258,10 @@ class ArgEmitter {
loweredSubstArgType,
param.getSILStorageInterfaceType());
case SILFunctionLanguage::C:
return Conversion::getBridging(Conversion::BridgeToObjC,
arg.getSubstRValueType(),
origParamType.getType(),
param.getSILStorageInterfaceType());
return Conversion::getBridging(
Conversion::BridgeToObjC, arg.getSubstRValueType(),
origParamType.getType(), param.getSILStorageInterfaceType(),
origParamType);
}
llvm_unreachable("bad language");
}();
Expand Down
10 changes: 8 additions & 2 deletions lib/SILGen/SILGenBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1315,8 +1315,9 @@ static SILValue emitObjCUnconsumedArgument(SILGenFunction &SGF,
SILLocation loc,
SILValue arg) {
auto &lowering = SGF.getTypeLowering(arg->getType());
// If address-only, make a +1 copy and operate on that.
if (lowering.isAddressOnly() && SGF.useLoweredAddresses()) {
// If arg is non-trivial and has an address type, make a +1 copy and operate
// on that.
if (!lowering.isTrivial() && arg->getType().isAddress()) {
auto tmp = SGF.emitTemporaryAllocation(loc, arg->getType().getObjectType());
SGF.B.createCopyAddr(loc, arg, tmp, IsNotTake, IsInitialization);
return tmp;
Expand Down Expand Up @@ -1453,6 +1454,11 @@ emitObjCThunkArguments(SILGenFunction &SGF, SILLocation loc, SILDeclRef thunk,
auto buf = SGF.emitTemporaryAllocation(loc, native.getType());
native.forwardInto(SGF, loc, buf);
native = SGF.emitManagedBufferWithCleanup(buf);
} else if (!fnConv.isSILIndirect(nativeInputs[i]) &&
native.getType().isAddress()) {
// Load the value if the argument has an address type and the native
// function expects the argument to be passed directly.
native = SGF.emitManagedLoadCopy(loc, native.getValue());
}

if (nativeInputs[i].isConsumedInCaller()) {
Expand Down
19 changes: 9 additions & 10 deletions lib/SILGen/SILGenConvert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1348,10 +1348,9 @@ Conversion::adjustForInitialOptionalInjection() const {
case BridgeFromObjC:
case BridgeResultFromObjC:
return OptionalInjectionConversion::forInjection(
getBridging(getKind(), getSourceType().getOptionalObjectType(),
getResultType(), getLoweredResultType(),
isBridgingExplicit())
);
getBridging(getKind(), getSourceType().getOptionalObjectType(),
getResultType(), getLoweredResultType(),
getBridgingOriginalInputType(), isBridgingExplicit()));
}
llvm_unreachable("bad kind");
}
Expand All @@ -1373,9 +1372,9 @@ Conversion::adjustForInitialOptionalConversions(CanType newSourceType) const {
case BridgeToObjC:
case BridgeFromObjC:
case BridgeResultFromObjC:
return Conversion::getBridging(getKind(), newSourceType,
getResultType(), getLoweredResultType(),
isBridgingExplicit());
return Conversion::getBridging(
getKind(), newSourceType, getResultType(), getLoweredResultType(),
getBridgingOriginalInputType(), isBridgingExplicit());
}
llvm_unreachable("bad kind");
}
Expand All @@ -1394,9 +1393,9 @@ std::optional<Conversion> Conversion::adjustForInitialForceValue() const {

case BridgeToObjC: {
auto sourceOptType = getSourceType().wrapInOptionalType();
return Conversion::getBridging(ForceAndBridgeToObjC,
sourceOptType, getResultType(),
getLoweredResultType(),
return Conversion::getBridging(ForceAndBridgeToObjC, sourceOptType,
getResultType(), getLoweredResultType(),
getBridgingOriginalInputType(),
isBridgingExplicit());
}
}
Expand Down
Loading