Skip to content

Commit 001dd21

Browse files
authored
Merge pull request #3729 from swiftwasm/main
[pull] swiftwasm from main
2 parents aab36c6 + 79ed857 commit 001dd21

31 files changed

+344
-141
lines changed

docs/ABI/Mangling.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ types where the metadata itself has unknown layout.)
217217
global ::= global 'To' // swift-as-ObjC thunk
218218
global ::= global 'TD' // dynamic dispatch thunk
219219
global ::= global 'Td' // direct method reference thunk
220+
global ::= global 'TE' // distributed actor thunk
220221
global ::= global 'TI' // implementation of a dynamic_replaceable function
221222
global ::= global 'Tu' // async function pointer of a function
222223
global ::= global 'TX' // function pointer of a dynamic_replaceable function

include/swift/AST/ASTMangler.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ class ASTMangler : public Mangler {
6060
/// concurrency library.
6161
bool AllowConcurrencyStandardSubstitutions = true;
6262

63+
/// If enabled, marker protocols can be encoded in the mangled name.
64+
bool AllowMarkerProtocols = true;
65+
6366
public:
6467
using SymbolicReferent = llvm::PointerUnion<const NominalTypeDecl *,
6568
const OpaqueTypeDecl *>;
@@ -294,6 +297,10 @@ class ASTMangler : public Mangler {
294297
static const clang::NamedDecl *
295298
getClangDeclForMangling(const ValueDecl *decl);
296299

300+
void appendExistentialLayout(
301+
const ExistentialLayout &layout, GenericSignature sig,
302+
const ValueDecl *forDecl);
303+
297304
protected:
298305

299306
void appendSymbolKind(SymbolKind SKind);

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5943,6 +5943,9 @@ ERROR(marker_protocol_requirement, none,
59435943
ERROR(marker_protocol_inherit_nonmarker, none,
59445944
"marker protocol %0 cannot inherit non-marker protocol %1",
59455945
(DeclName, DeclName))
5946+
ERROR(marker_protocol_inherit_class, none,
5947+
"marker protocol %0 cannot inherit class %1",
5948+
(DeclName, Type))
59465949
ERROR(marker_protocol_cast,none,
59475950
"marker protocol %0 cannot be used in a conditional cast", (DeclName))
59485951
ERROR(marker_protocol_conditional_conformance,none,

include/swift/Demangling/DemangleNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ NODE(DependentProtocolConformanceAssociated)
7070
CONTEXT_NODE(Destructor)
7171
CONTEXT_NODE(DidSet)
7272
NODE(Directness)
73+
NODE(DistributedThunk)
7374
NODE(DynamicAttribute)
7475
NODE(DirectMethodReferenceAttribute)
7576
NODE(DynamicSelf)

lib/AST/ASTMangler.cpp

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ void ASTMangler::appendSymbolKind(SymbolKind SKind) {
842842
case SymbolKind::DynamicThunk: return appendOperator("TD");
843843
case SymbolKind::SwiftAsObjCThunk: return appendOperator("To");
844844
case SymbolKind::ObjCAsSwiftThunk: return appendOperator("TO");
845-
case SymbolKind::DistributedThunk: return appendOperator("Td");
845+
case SymbolKind::DistributedThunk: return appendOperator("TE");
846846
}
847847
}
848848

@@ -1023,6 +1023,41 @@ void ASTMangler::appendOpaqueDeclName(const OpaqueTypeDecl *opaqueDecl) {
10231023
}
10241024
}
10251025

1026+
void ASTMangler::appendExistentialLayout(
1027+
const ExistentialLayout &layout, GenericSignature sig,
1028+
const ValueDecl *forDecl) {
1029+
bool First = true;
1030+
bool DroppedRequiresClass = false;
1031+
bool SawRequiresClass = false;
1032+
for (Type protoTy : layout.getProtocols()) {
1033+
auto proto = protoTy->castTo<ProtocolType>()->getDecl();
1034+
// If we aren't allowed to emit marker protocols, suppress them here.
1035+
if (!AllowMarkerProtocols && proto->isMarkerProtocol()) {
1036+
if (proto->requiresClass())
1037+
DroppedRequiresClass = true;
1038+
1039+
continue;
1040+
}
1041+
1042+
if (proto->requiresClass())
1043+
SawRequiresClass = true;
1044+
1045+
appendProtocolName(protoTy->castTo<ProtocolType>()->getDecl());
1046+
appendListSeparator(First);
1047+
}
1048+
if (First)
1049+
appendOperator("y");
1050+
1051+
if (auto superclass = layout.explicitSuperclass) {
1052+
appendType(superclass, sig, forDecl);
1053+
return appendOperator("Xc");
1054+
} else if (layout.hasExplicitAnyObject ||
1055+
(DroppedRequiresClass && !SawRequiresClass)) {
1056+
return appendOperator("Xl");
1057+
}
1058+
return appendOperator("p");
1059+
}
1060+
10261061
/// Mangle a type into the buffer.
10271062
///
10281063
void ASTMangler::appendType(Type type, GenericSignature sig,
@@ -1199,31 +1234,15 @@ void ASTMangler::appendType(Type type, GenericSignature sig,
11991234
return appendOperator("t");
12001235

12011236
case TypeKind::Protocol: {
1202-
bool First = true;
1203-
appendProtocolName(cast<ProtocolType>(tybase)->getDecl());
1204-
appendListSeparator(First);
1205-
return appendOperator("p");
1237+
return appendExistentialLayout(
1238+
ExistentialLayout(cast<ProtocolType>(tybase)), sig, forDecl);
12061239
}
12071240

12081241
case TypeKind::ProtocolComposition: {
12091242
// We mangle ProtocolType and ProtocolCompositionType using the
12101243
// same production:
1211-
bool First = true;
12121244
auto layout = type->getExistentialLayout();
1213-
for (Type protoTy : layout.getProtocols()) {
1214-
appendProtocolName(protoTy->castTo<ProtocolType>()->getDecl());
1215-
appendListSeparator(First);
1216-
}
1217-
if (First)
1218-
appendOperator("y");
1219-
1220-
if (auto superclass = layout.explicitSuperclass) {
1221-
appendType(superclass, sig, forDecl);
1222-
return appendOperator("Xc");
1223-
} else if (layout.hasExplicitAnyObject) {
1224-
return appendOperator("Xl");
1225-
}
1226-
return appendOperator("p");
1245+
return appendExistentialLayout(layout, sig, forDecl);
12271246
}
12281247

12291248
case TypeKind::UnboundGeneric:
@@ -2220,6 +2239,8 @@ void ASTMangler::appendModule(const ModuleDecl *module,
22202239
/// Mangle the name of a protocol as a substitution candidate.
22212240
void ASTMangler::appendProtocolName(const ProtocolDecl *protocol,
22222241
bool allowStandardSubstitution) {
2242+
assert(AllowMarkerProtocols || !protocol->isMarkerProtocol());
2243+
22232244
if (allowStandardSubstitution && tryAppendStandardSubstitution(protocol))
22242245
return;
22252246

@@ -2370,6 +2391,8 @@ void ASTMangler::appendAnyGenericType(const GenericTypeDecl *decl) {
23702391
appendOperator("a");
23712392
break;
23722393
case DeclKind::Protocol:
2394+
assert(AllowMarkerProtocols ||
2395+
!cast<ProtocolDecl>(decl)->isMarkerProtocol());
23732396
appendOperator("P");
23742397
break;
23752398
case DeclKind::Class:
@@ -2689,6 +2712,11 @@ void ASTMangler::appendRequirement(const Requirement &reqt,
26892712
case RequirementKind::Layout: {
26902713
} break;
26912714
case RequirementKind::Conformance: {
2715+
// If we don't allow marker protocols but we have one here, skip it.
2716+
if (!AllowMarkerProtocols &&
2717+
reqt.getProtocolDecl()->isMarkerProtocol())
2718+
return;
2719+
26922720
appendProtocolName(reqt.getProtocolDecl());
26932721
} break;
26942722
case RequirementKind::Superclass:
@@ -3226,6 +3254,12 @@ void ASTMangler::appendAnyProtocolConformance(
32263254
GenericSignature genericSig,
32273255
CanType conformingType,
32283256
ProtocolConformanceRef conformance) {
3257+
// If we have a conformance to a marker protocol but we aren't allowed to
3258+
// emit marker protocols, skip it.
3259+
if (!AllowMarkerProtocols &&
3260+
conformance.getRequirement()->isMarkerProtocol())
3261+
return;
3262+
32293263
if (conformingType->isTypeParameter()) {
32303264
assert(genericSig && "Need a generic signature to resolve conformance");
32313265
auto path = genericSig->getConformanceAccessPath(conformingType,

lib/Demangling/Demangler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ bool swift::Demangle::isFunctionAttr(Node::Kind kind) {
130130
case Node::Kind::OutlinedVariable:
131131
case Node::Kind::OutlinedBridgedMethod:
132132
case Node::Kind::MergedFunction:
133+
case Node::Kind::DistributedThunk:
133134
case Node::Kind::DynamicallyReplaceableFunctionImpl:
134135
case Node::Kind::DynamicallyReplaceableFunctionKey:
135136
case Node::Kind::DynamicallyReplaceableFunctionVar:
@@ -2359,6 +2360,7 @@ NodePointer Demangler::demangleThunkOrSpecialization() {
23592360
case 'O': return createNode(Node::Kind::NonObjCAttribute);
23602361
case 'D': return createNode(Node::Kind::DynamicAttribute);
23612362
case 'd': return createNode(Node::Kind::DirectMethodReferenceAttribute);
2363+
case 'E': return createNode(Node::Kind::DistributedThunk);
23622364
case 'a': return createNode(Node::Kind::PartialApplyObjCForwarder);
23632365
case 'A': return createNode(Node::Kind::PartialApplyForwarder);
23642366
case 'm': return createNode(Node::Kind::MergedFunction);

lib/Demangling/NodePrinter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ class NodePrinter {
558558
case Node::Kind::ProtocolConformanceRefInTypeModule:
559559
case Node::Kind::ProtocolConformanceRefInProtocolModule:
560560
case Node::Kind::ProtocolConformanceRefInOtherModule:
561+
case Node::Kind::DistributedThunk:
561562
case Node::Kind::DynamicallyReplaceableFunctionKey:
562563
case Node::Kind::DynamicallyReplaceableFunctionImpl:
563564
case Node::Kind::DynamicallyReplaceableFunctionVar:
@@ -1986,6 +1987,11 @@ NodePointer NodePrinter::print(NodePointer Node, unsigned depth,
19861987
Printer << "opaque type symbolic reference 0x";
19871988
Printer.writeHex(Node->getIndex());
19881989
return nullptr;
1990+
case Node::Kind::DistributedThunk:
1991+
if (!Options.ShortenThunk) {
1992+
Printer << "distributed thunk for ";
1993+
}
1994+
return nullptr;
19891995
case Node::Kind::DynamicallyReplaceableFunctionKey:
19901996
if (!Options.ShortenThunk) {
19911997
Printer << "dynamically replaceable key for ";

lib/Demangling/OldRemangler.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,12 @@ ManglingError Remangler::mangleMergedFunction(Node *node, unsigned depth) {
729729
return ManglingError::Success;
730730
}
731731

732+
ManglingError
733+
Remangler::mangleDistributedThunk(Node *node, unsigned depth) {
734+
Buffer << "TE";
735+
return ManglingError::Success;
736+
}
737+
732738
ManglingError
733739
Remangler::mangleDynamicallyReplaceableFunctionImpl(Node *node,
734740
unsigned depth) {

lib/Demangling/Remangler.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,6 +1610,7 @@ ManglingError Remangler::mangleGlobal(Node *node, unsigned depth) {
16101610
case Node::Kind::VTableAttribute:
16111611
case Node::Kind::DirectMethodReferenceAttribute:
16121612
case Node::Kind::MergedFunction:
1613+
case Node::Kind::DistributedThunk:
16131614
case Node::Kind::DynamicallyReplaceableFunctionKey:
16141615
case Node::Kind::DynamicallyReplaceableFunctionImpl:
16151616
case Node::Kind::DynamicallyReplaceableFunctionVar:
@@ -2241,6 +2242,12 @@ ManglingError Remangler::mangleMergedFunction(Node *node, unsigned depth) {
22412242
return ManglingError::Success;
22422243
}
22432244

2245+
ManglingError
2246+
Remangler::mangleDistributedThunk(Node *node, unsigned depth) {
2247+
Buffer << "TE";
2248+
return ManglingError::Success;
2249+
}
2250+
22442251
ManglingError
22452252
Remangler::mangleDynamicallyReplaceableFunctionImpl(Node *node,
22462253
unsigned depth) {

lib/IRGen/IRGenMangler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ IRGenMangler::mangleTypeForReflection(IRGenModule &IGM,
159159
AllowConcurrencyStandardSubstitutions = false;
160160
}
161161

162+
llvm::SaveAndRestore<bool> savedAllowMarkerProtocols(
163+
AllowMarkerProtocols, false);
162164
return withSymbolicReferences(IGM, [&]{
163165
appendType(Ty, Sig);
164166
});

lib/IRGen/Linking.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ std::string LinkEntity::mangleAsString() const {
477477
}
478478
case Kind::DistributedThunkAsyncFunctionPointer: {
479479
std::string Result = getSILDeclRef().mangle();
480-
Result.append("Td");
480+
Result.append("TE");
481481
Result.append("Tu");
482482
return Result;
483483
}

lib/SIL/Utils/OwnershipUtils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,11 +1438,11 @@ void swift::findTransitiveReborrowBaseValuePairs(
14381438
void swift::visitTransitiveEndBorrows(
14391439
BorrowedValue beginBorrow,
14401440
function_ref<void(EndBorrowInst *)> visitEndBorrow) {
1441-
SmallSetVector<SILValue, 4> worklist;
1441+
DAGNodeWorklist<SILValue, 4> worklist;
14421442
worklist.insert(beginBorrow.value);
14431443

14441444
while (!worklist.empty()) {
1445-
auto val = worklist.pop_back_val();
1445+
auto val = worklist.pop();
14461446
for (auto *consumingUse : val->getConsumingUses()) {
14471447
auto *consumingUser = consumingUse->getUser();
14481448
if (auto *branch = dyn_cast<BranchInst>(consumingUser)) {

lib/SILGen/SILGen.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,7 @@ static void emitOrDelayFunction(SILGenModule &SGM,
11441144
auto linkage = constant.getLinkage(ForDefinition);
11451145
bool mayDelay = !forceEmission &&
11461146
(constant.isImplicit() &&
1147+
!constant.isDynamicallyReplaceable() &&
11471148
!isPossiblyUsedExternally(linkage, SGM.M.isWholeModule()));
11481149

11491150
// Avoid emitting a delayable definition if it hasn't already been referenced.

lib/SILGen/SILGenDistributed.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,8 +951,20 @@ void SILGenFunction::emitDistributedThunk(SILDeclRef thunk) {
951951
auto nativeFnSILTy = SILType::getPrimitiveObjectType(nativeMethodTy);
952952
auto nativeSilFnType = nativeFnSILTy.castTo<SILFunctionType>();
953953

954-
SILValue nativeFn = emitClassMethodRef(
954+
bool isClassMethod = false;
955+
if (auto classDecl = dyn_cast<ClassDecl>(fd->getDeclContext())) {
956+
if (!classDecl->isFinal() && !fd->isFinal() &&
957+
!fd->hasForcedStaticDispatch())
958+
isClassMethod = true;
959+
}
960+
961+
SILValue nativeFn;
962+
if (isClassMethod) {
963+
nativeFn = emitClassMethodRef(
955964
loc, params[params.size() - 1], native, nativeMethodTy);
965+
} else {
966+
nativeFn = emitGlobalFunctionRef(loc, native);
967+
}
956968
auto subs = F.getForwardingSubstitutionMap();
957969

958970
if (nativeSilFnType->hasErrorResult()) {

0 commit comments

Comments
 (0)