Skip to content

Commit 3d655f5

Browse files
committed
[IRGen] Add coroutine fp link entities.
Duplicative boilerplate copying what was done for async function pointers which these have the same characteristics as.
1 parent fdd3eb0 commit 3d655f5

File tree

3 files changed

+218
-1
lines changed

3 files changed

+218
-1
lines changed

docs/ABI/Mangling.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ types where the metadata itself has unknown layout.)
235235
global ::= global 'TF' // distributed method accessor
236236
global ::= global 'TI' // implementation of a dynamic_replaceable function
237237
global ::= global 'Tu' // async function pointer of a function
238+
global ::= global 'Tv' // coro function pointer of a function
238239
global ::= global 'TX' // function pointer of a dynamic_replaceable function
239240
global ::= global 'Twb' // back deployment thunk
240241
global ::= global 'TwB' // back deployment fallback function

include/swift/IRGen/Linking.h

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,44 @@ class LinkEntity {
551551
/// Pointer is the (generalized) existential type.
552552
/// SecondaryPointer is the GenericSignatureImpl*.
553553
ExtendedExistentialTypeShape,
554+
555+
/// A global struct containing a relative pointer to the single-yield
556+
/// coroutine ramp function and the fixed-size to be allocated in the
557+
/// caller.
558+
/// The pointer is a SILFunction*.
559+
CoroFunctionPointer,
560+
561+
/// The same as CoroFunctionPointer but with a different stored value, for
562+
/// use by TBDGen.
563+
/// The pointer is an AbstractFunctionDecl*.
564+
CoroFunctionPointerAST,
565+
566+
/// An coro function pointer for a method dispatch thunk. The pointer is
567+
/// a FuncDecl* inside a protocol or a class.
568+
DispatchThunkCoroFunctionPointer,
569+
570+
/// An coro function pointer for a method dispatch thunk for an
571+
/// initializing constructor. The pointer is a ConstructorDecl* inside a
572+
/// class.
573+
DispatchThunkInitializerCoroFunctionPointer,
574+
575+
/// An coro function pointer for a method dispatch thunk for an allocating
576+
/// constructor. The pointer is a ConstructorDecl* inside a protocol or
577+
/// a class.
578+
DispatchThunkAllocatorCoroFunctionPointer,
579+
580+
/// An coro function pointer for a distributed thunk.
581+
/// The pointer is a FuncDecl* inside an actor (class).
582+
DistributedThunkCoroFunctionPointer,
583+
584+
/// An coro function pointer to a partial apply forwarder.
585+
/// The pointer is the llvm::Function* for a partial apply forwarder.
586+
PartialApplyForwarderCoroFunctionPointer,
587+
588+
/// An coro function pointer for a distributed accessor (method or
589+
/// property).
590+
/// The pointer is a SILFunction*.
591+
DistributedAccessorCoroFunctionPointer,
554592
};
555593
friend struct llvm::DenseMapInfo<LinkEntity>;
556594

@@ -1457,6 +1495,98 @@ class LinkEntity {
14571495
return entity;
14581496
}
14591497

1498+
static LinkEntity forCoroFunctionPointer(LinkEntity other) {
1499+
LinkEntity entity;
1500+
entity.Pointer = other.Pointer;
1501+
entity.SecondaryPointer = nullptr;
1502+
1503+
switch (other.getKind()) {
1504+
case LinkEntity::Kind::SILFunction:
1505+
entity.Data = LINKENTITY_SET_FIELD(
1506+
Kind, unsigned(LinkEntity::Kind::CoroFunctionPointer));
1507+
break;
1508+
1509+
case LinkEntity::Kind::DispatchThunk:
1510+
entity.Data = LINKENTITY_SET_FIELD(
1511+
Kind, unsigned(LinkEntity::Kind::DispatchThunkCoroFunctionPointer));
1512+
break;
1513+
1514+
case LinkEntity::Kind::DispatchThunkInitializer:
1515+
entity.Data = LINKENTITY_SET_FIELD(
1516+
Kind,
1517+
unsigned(
1518+
LinkEntity::Kind::DispatchThunkInitializerCoroFunctionPointer));
1519+
break;
1520+
1521+
case LinkEntity::Kind::DispatchThunkAllocator:
1522+
entity.Data = LINKENTITY_SET_FIELD(
1523+
Kind,
1524+
unsigned(
1525+
LinkEntity::Kind::DispatchThunkAllocatorCoroFunctionPointer));
1526+
break;
1527+
case LinkEntity::Kind::PartialApplyForwarder:
1528+
entity.Data = LINKENTITY_SET_FIELD(
1529+
Kind,
1530+
unsigned(LinkEntity::Kind::PartialApplyForwarderCoroFunctionPointer));
1531+
break;
1532+
1533+
case LinkEntity::Kind::DistributedAccessor: {
1534+
entity.Data = LINKENTITY_SET_FIELD(
1535+
Kind,
1536+
unsigned(LinkEntity::Kind::DistributedAccessorCoroFunctionPointer));
1537+
break;
1538+
}
1539+
1540+
default:
1541+
llvm_unreachable("Link entity kind cannot have an coro function pointer");
1542+
}
1543+
1544+
return entity;
1545+
}
1546+
1547+
LinkEntity getUnderlyingEntityForCoroFunctionPointer() const {
1548+
LinkEntity entity;
1549+
entity.Pointer = Pointer;
1550+
entity.SecondaryPointer = nullptr;
1551+
1552+
switch (getKind()) {
1553+
case LinkEntity::Kind::CoroFunctionPointer:
1554+
entity.Data =
1555+
LINKENTITY_SET_FIELD(Kind, unsigned(LinkEntity::Kind::SILFunction));
1556+
break;
1557+
1558+
case LinkEntity::Kind::DispatchThunkCoroFunctionPointer:
1559+
entity.Data =
1560+
LINKENTITY_SET_FIELD(Kind, unsigned(LinkEntity::Kind::DispatchThunk));
1561+
break;
1562+
1563+
case LinkEntity::Kind::DispatchThunkInitializerCoroFunctionPointer:
1564+
entity.Data = LINKENTITY_SET_FIELD(
1565+
Kind, unsigned(LinkEntity::Kind::DispatchThunkInitializer));
1566+
break;
1567+
1568+
case LinkEntity::Kind::DispatchThunkAllocatorCoroFunctionPointer:
1569+
entity.Data = LINKENTITY_SET_FIELD(
1570+
Kind, unsigned(LinkEntity::Kind::DispatchThunkAllocator));
1571+
break;
1572+
1573+
case LinkEntity::Kind::PartialApplyForwarderCoroFunctionPointer:
1574+
entity.Data = LINKENTITY_SET_FIELD(
1575+
Kind, unsigned(LinkEntity::Kind::PartialApplyForwarder));
1576+
break;
1577+
1578+
case LinkEntity::Kind::DistributedAccessorCoroFunctionPointer:
1579+
entity.Data = LINKENTITY_SET_FIELD(
1580+
Kind, unsigned(LinkEntity::Kind::DistributedAccessor));
1581+
break;
1582+
1583+
default:
1584+
llvm_unreachable("Link entity is not an coro function pointer");
1585+
}
1586+
1587+
return entity;
1588+
}
1589+
14601590
void mangle(ASTContext &Ctx, llvm::raw_ostream &out) const;
14611591
void mangle(ASTContext &Ctx, SmallVectorImpl<char> &buffer) const;
14621592
std::string mangleAsString(ASTContext &Ctx) const;

lib/IRGen/Linking.cpp

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,28 @@ std::string LinkEntity::mangleAsString(ASTContext &Ctx) const {
550550
return mangler.mangleExtendedExistentialTypeShapeSymbol(
551551
genSig, existentialType, isUnique);
552552
}
553+
case Kind::CoroFunctionPointer:
554+
case Kind::DispatchThunkCoroFunctionPointer:
555+
case Kind::DispatchThunkInitializerCoroFunctionPointer:
556+
case Kind::DispatchThunkAllocatorCoroFunctionPointer:
557+
case Kind::PartialApplyForwarderCoroFunctionPointer:
558+
case Kind::DistributedAccessorCoroFunctionPointer: {
559+
std::string Result(
560+
getUnderlyingEntityForCoroFunctionPointer().mangleAsString(Ctx));
561+
Result.append("Tv");
562+
return Result;
563+
}
564+
case Kind::DistributedThunkCoroFunctionPointer: {
565+
std::string Result = getSILDeclRef().mangle();
566+
Result.append("TE");
567+
Result.append("Tv");
568+
return Result;
569+
}
570+
case Kind::CoroFunctionPointerAST: {
571+
std::string Result = getSILDeclRef().mangle();
572+
Result.append("Tv");
573+
return Result;
574+
}
553575
}
554576
llvm_unreachable("bad entity kind!");
555577
}
@@ -907,6 +929,17 @@ SILLinkage LinkEntity::getLinkage(ForDefinition_t forDefinition) const {
907929
case Kind::ExtendedExistentialTypeShape:
908930
return (isExtendedExistentialTypeShapeShared()
909931
? SILLinkage::Shared : SILLinkage::Private);
932+
case Kind::CoroFunctionPointer:
933+
case Kind::DispatchThunkCoroFunctionPointer:
934+
case Kind::DispatchThunkInitializerCoroFunctionPointer:
935+
case Kind::DispatchThunkAllocatorCoroFunctionPointer:
936+
case Kind::PartialApplyForwarderCoroFunctionPointer:
937+
case Kind::DistributedAccessorCoroFunctionPointer:
938+
return getUnderlyingEntityForCoroFunctionPointer().getLinkage(
939+
forDefinition);
940+
case Kind::CoroFunctionPointerAST:
941+
case Kind::DistributedThunkCoroFunctionPointer:
942+
return getSILLinkage(getDeclLinkage(getDecl()), forDefinition);
910943
}
911944
llvm_unreachable("bad link entity kind");
912945
}
@@ -1001,6 +1034,14 @@ bool LinkEntity::isContextDescriptor() const {
10011034
case Kind::DistributedAccessor:
10021035
case Kind::AccessibleFunctionRecord:
10031036
case Kind::ExtendedExistentialTypeShape:
1037+
case Kind::CoroFunctionPointer:
1038+
case Kind::DispatchThunkCoroFunctionPointer:
1039+
case Kind::DispatchThunkInitializerCoroFunctionPointer:
1040+
case Kind::DispatchThunkAllocatorCoroFunctionPointer:
1041+
case Kind::PartialApplyForwarderCoroFunctionPointer:
1042+
case Kind::DistributedAccessorCoroFunctionPointer:
1043+
case Kind::CoroFunctionPointerAST:
1044+
case Kind::DistributedThunkCoroFunctionPointer:
10041045
return false;
10051046
}
10061047
llvm_unreachable("invalid descriptor");
@@ -1128,6 +1169,15 @@ llvm::Type *LinkEntity::getDefaultDeclarationType(IRGenModule &IGM) const {
11281169
return IGM.AccessibleFunctionRecordTy;
11291170
case Kind::ExtendedExistentialTypeShape:
11301171
return IGM.RelativeAddressTy;
1172+
case Kind::CoroFunctionPointer:
1173+
case Kind::DispatchThunkCoroFunctionPointer:
1174+
case Kind::DispatchThunkInitializerCoroFunctionPointer:
1175+
case Kind::DispatchThunkAllocatorCoroFunctionPointer:
1176+
case Kind::PartialApplyForwarderCoroFunctionPointer:
1177+
case Kind::DistributedAccessorCoroFunctionPointer:
1178+
case Kind::CoroFunctionPointerAST:
1179+
case Kind::DistributedThunkCoroFunctionPointer:
1180+
return IGM.CoroFunctionPointerPtrTy;
11311181
default:
11321182
llvm_unreachable("declaration LLVM type not specified");
11331183
}
@@ -1196,6 +1246,14 @@ Alignment LinkEntity::getAlignment(IRGenModule &IGM) const {
11961246
case Kind::NoncanonicalSpecializedGenericTypeMetadata:
11971247
case Kind::NoncanonicalSpecializedGenericTypeMetadataCacheVariable:
11981248
case Kind::PartialApplyForwarder:
1249+
case Kind::CoroFunctionPointer:
1250+
case Kind::DispatchThunkCoroFunctionPointer:
1251+
case Kind::DispatchThunkInitializerCoroFunctionPointer:
1252+
case Kind::DispatchThunkAllocatorCoroFunctionPointer:
1253+
case Kind::PartialApplyForwarderCoroFunctionPointer:
1254+
case Kind::DistributedAccessorCoroFunctionPointer:
1255+
case Kind::CoroFunctionPointerAST:
1256+
case Kind::DistributedThunkCoroFunctionPointer:
11991257
return IGM.getPointerAlignment();
12001258
case Kind::CanonicalPrespecializedGenericTypeCachingOnceToken:
12011259
case Kind::TypeMetadataDemanglingCacheVariable:
@@ -1244,6 +1302,9 @@ bool LinkEntity::isText() const {
12441302
case Kind::AsyncFunctionPointerAST:
12451303
case Kind::ProtocolWitnessTable:
12461304
case Kind::TypeMetadata:
1305+
case Kind::DispatchThunkCoroFunctionPointer:
1306+
case Kind::DistributedThunkCoroFunctionPointer:
1307+
case Kind::CoroFunctionPointerAST:
12471308
return false;
12481309
// The following cases do not currently generate linkable symbols
12491310
// through TBDGen. The full enumeration is captured to ensure
@@ -1303,6 +1364,11 @@ bool LinkEntity::isText() const {
13031364
case Kind::CanonicalSpecializedGenericSwiftMetaclassStub:
13041365
case Kind::NoncanonicalSpecializedGenericTypeMetadata:
13051366
case Kind::AccessibleFunctionRecord:
1367+
case Kind::CoroFunctionPointer:
1368+
case Kind::DispatchThunkInitializerCoroFunctionPointer:
1369+
case Kind::DispatchThunkAllocatorCoroFunctionPointer:
1370+
case Kind::PartialApplyForwarderCoroFunctionPointer:
1371+
case Kind::DistributedAccessorCoroFunctionPointer:
13061372
return false;
13071373
}
13081374
}
@@ -1358,6 +1424,8 @@ bool LinkEntity::isWeakImported(ModuleDecl *module) const {
13581424

13591425
case Kind::AsyncFunctionPointerAST:
13601426
case Kind::DistributedThunkAsyncFunctionPointer:
1427+
case Kind::CoroFunctionPointerAST:
1428+
case Kind::DistributedThunkCoroFunctionPointer:
13611429
case Kind::DispatchThunk:
13621430
case Kind::DispatchThunkDerivative:
13631431
case Kind::DispatchThunkInitializer:
@@ -1450,13 +1518,21 @@ bool LinkEntity::isWeakImported(ModuleDecl *module) const {
14501518
case Kind::DistributedAccessorAsyncPointer:
14511519
return getUnderlyingEntityForAsyncFunctionPointer()
14521520
.isWeakImported(module);
1453-
case Kind::KnownAsyncFunctionPointer:
1521+
case Kind::KnownAsyncFunctionPointer: {
14541522
auto &context = module->getASTContext();
14551523
auto deploymentAvailability =
14561524
AvailabilityRange::forDeploymentTarget(context);
14571525
return !deploymentAvailability.isContainedIn(
14581526
context.getConcurrencyAvailability());
14591527
}
1528+
case Kind::CoroFunctionPointer:
1529+
case Kind::DispatchThunkCoroFunctionPointer:
1530+
case Kind::DispatchThunkInitializerCoroFunctionPointer:
1531+
case Kind::DispatchThunkAllocatorCoroFunctionPointer:
1532+
case Kind::PartialApplyForwarderCoroFunctionPointer:
1533+
case Kind::DistributedAccessorCoroFunctionPointer:
1534+
return getUnderlyingEntityForCoroFunctionPointer().isWeakImported(module);
1535+
}
14601536

14611537
llvm_unreachable("Bad link entity kind");
14621538
}
@@ -1465,6 +1541,8 @@ DeclContext *LinkEntity::getDeclContextForEmission() const {
14651541
switch (getKind()) {
14661542
case Kind::AsyncFunctionPointerAST:
14671543
case Kind::DistributedThunkAsyncFunctionPointer:
1544+
case Kind::CoroFunctionPointerAST:
1545+
case Kind::DistributedThunkCoroFunctionPointer:
14681546
case Kind::DispatchThunk:
14691547
case Kind::DispatchThunkDerivative:
14701548
case Kind::DispatchThunkInitializer:
@@ -1585,6 +1663,14 @@ DeclContext *LinkEntity::getDeclContextForEmission() const {
15851663

15861664
case Kind::AccessibleFunctionRecord: {
15871665
return getSILFunction()->getParentModule();
1666+
case Kind::CoroFunctionPointer:
1667+
case Kind::DispatchThunkCoroFunctionPointer:
1668+
case Kind::DispatchThunkInitializerCoroFunctionPointer:
1669+
case Kind::DispatchThunkAllocatorCoroFunctionPointer:
1670+
case Kind::PartialApplyForwarderCoroFunctionPointer:
1671+
case Kind::DistributedAccessorCoroFunctionPointer:
1672+
return getUnderlyingEntityForCoroFunctionPointer()
1673+
.getDeclContextForEmission();
15881674
}
15891675
}
15901676
llvm_unreachable("invalid decl kind");

0 commit comments

Comments
 (0)