Skip to content

Commit 0685726

Browse files
authored
Merge pull request #3633 from swiftwasm/main
[pull] swiftwasm from main
2 parents d6073fe + 814d583 commit 0685726

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+823
-374
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,10 @@ option(SWIFT_CHECK_INCREMENTAL_COMPILATION
397397
"Check if incremental compilation works when compiling the Swift libraries"
398398
FALSE)
399399

400+
option(SWIFT_ENABLE_ARRAY_COW_CHECKS
401+
"Compile the stdlib with Array COW checks enabled (only relevant for assert builds)"
402+
FALSE)
403+
400404
option(SWIFT_REPORT_STATISTICS
401405
"Create json files which contain internal compilation statistics"
402406
FALSE)

include/swift/AST/DiagnosticsSema.def

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4329,6 +4329,12 @@ NOTE(note_add_async_to_function,none,
43294329
NOTE(note_add_nonisolated_to_decl,none,
43304330
"add 'nonisolated' to %0 to make this %1 not isolated to the actor",
43314331
(DeclName, DescriptiveDeclKind))
4332+
NOTE(note_add_distributed_to_decl,none,
4333+
"add 'distributed' to %0 to make this %1 witness the protocol requirement",
4334+
(DeclName, DescriptiveDeclKind))
4335+
NOTE(note_distributed_requirement_defined_here,none,
4336+
"distributed function requirement %0 declared here",
4337+
(DeclName))
43324338
NOTE(note_add_globalactor_to_function,none,
43334339
"add '@%0' to make %1 %2 part of global actor %3",
43344340
(StringRef, DescriptiveDeclKind, DeclName, Type))
@@ -4398,8 +4404,8 @@ ERROR(actor_isolated_non_self_reference,none,
43984404
"from the main actor|from a non-isolated context}3",
43994405
(DescriptiveDeclKind, DeclName, unsigned, unsigned, Type))
44004406
ERROR(distributed_actor_isolated_non_self_reference,none,
4401-
"distributed actor-isolated %0 %1 can only be referenced "
4402-
"inside the distributed actor",
4407+
"distributed actor-isolated %0 %1 can only be referenced inside the "
4408+
"distributed actor",
44034409
(DescriptiveDeclKind, DeclName))
44044410
ERROR(distributed_actor_needs_explicit_distributed_import,none,
44054411
"'_Distributed' module not imported, required for 'distributed actor'",

include/swift/IRGen/Linking.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ class LinkEntity {
153153
/// a class.
154154
DispatchThunkAllocatorAsyncFunctionPointer,
155155

156+
/// An async function pointer for a distributed thunk.
157+
/// The pointer is a FuncDecl* inside an actor (class).
158+
DistributedThunkAsyncFunctionPointer,
159+
156160
/// A method descriptor. The pointer is a FuncDecl* inside a protocol
157161
/// or a class.
158162
MethodDescriptor,
@@ -1198,7 +1202,9 @@ class LinkEntity {
11981202

11991203
static LinkEntity forAsyncFunctionPointer(SILDeclRef declRef) {
12001204
LinkEntity entity;
1201-
entity.setForDecl(Kind::AsyncFunctionPointerAST,
1205+
entity.setForDecl(declRef.isDistributedThunk()
1206+
? Kind::DistributedThunkAsyncFunctionPointer
1207+
: Kind::AsyncFunctionPointerAST,
12021208
declRef.getAbstractFunctionDecl());
12031209
entity.SecondaryPointer =
12041210
reinterpret_cast<void *>(static_cast<uintptr_t>(declRef.kind));
@@ -1264,6 +1270,8 @@ class LinkEntity {
12641270
void mangle(llvm::raw_ostream &out) const;
12651271
void mangle(SmallVectorImpl<char> &buffer) const;
12661272
std::string mangleAsString() const;
1273+
1274+
SILDeclRef getSILDeclRef() const;
12671275
SILLinkage getLinkage(ForDefinition_t isDefinition) const;
12681276

12691277
bool hasDecl() const {

include/swift/TBDGen/TBDGen.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ struct TBDGenOptions {
4444
/// Whether LLVM IR Virtual Function Elimination is enabled.
4545
bool VirtualFunctionElimination = false;
4646

47+
/// Whether LLVM IR Witness Method Elimination is enabled.
48+
bool WitnessMethodElimination = false;
49+
4750
/// The install_name to use in the TBD file.
4851
std::string InstallName;
4952

@@ -74,6 +77,7 @@ struct TBDGenOptions {
7477
lhs.LinkerDirectivesOnly == rhs.LinkerDirectivesOnly &&
7578
lhs.PublicSymbolsOnly == rhs.PublicSymbolsOnly &&
7679
lhs.VirtualFunctionElimination == rhs.VirtualFunctionElimination &&
80+
lhs.WitnessMethodElimination == rhs.WitnessMethodElimination &&
7781
lhs.InstallName == rhs.InstallName &&
7882
lhs.ModuleLinkName == rhs.ModuleLinkName &&
7983
lhs.CurrentVersion == rhs.CurrentVersion &&
@@ -91,6 +95,7 @@ struct TBDGenOptions {
9195
return hash_combine(
9296
opts.HasMultipleIGMs, opts.IsInstallAPI, opts.LinkerDirectivesOnly,
9397
opts.PublicSymbolsOnly, opts.VirtualFunctionElimination,
98+
opts.WitnessMethodElimination,
9499
opts.InstallName, opts.ModuleLinkName,
95100
opts.CurrentVersion, opts.CompatibilityVersion,
96101
opts.ModuleInstallNameMapPath,

lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,6 +1542,7 @@ static bool ParseTBDGenArgs(TBDGenOptions &Opts, ArgList &Args,
15421542
Opts.IsInstallAPI = Args.hasArg(OPT_tbd_is_installapi);
15431543

15441544
Opts.VirtualFunctionElimination = Args.hasArg(OPT_enable_llvm_vfe);
1545+
Opts.WitnessMethodElimination = Args.hasArg(OPT_enable_llvm_wme);
15451546

15461547
if (const Arg *A = Args.getLastArg(OPT_tbd_compatibility_version)) {
15471548
Opts.CompatibilityVersion = A->getValue();

lib/FrontendTool/TBD.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ static bool validateSymbols(DiagnosticEngine &diags,
7676
// symbol table, so make sure to mangle IRGen names before comparing them
7777
// with what TBDGen created.
7878
auto unmangledName = nameValue.getKey();
79+
7980
SmallString<128> name;
8081
llvm::Mangler::getNameWithPrefix(name, unmangledName,
8182
IRModule.getDataLayout());

lib/IRGen/GenMeta.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ namespace {
824824
SILDeclRef func(entry.getFunction());
825825

826826
// Emit the dispatch thunk.
827-
if (Resilient)
827+
if (Resilient || IGM.getOptions().WitnessMethodElimination)
828828
IGM.emitDispatchThunk(func);
829829

830830
// Classify the function.

lib/IRGen/GenProto.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,8 +2193,13 @@ static void addWTableTypeMetadata(IRGenModule &IGM,
21932193
break;
21942194
case SILLinkage::Public:
21952195
default:
2196-
global->setVCallVisibilityMetadata(
2197-
llvm::GlobalObject::VCallVisibility::VCallVisibilityPublic);
2196+
if (IGM.getOptions().InternalizeAtLink) {
2197+
global->setVCallVisibilityMetadata(
2198+
llvm::GlobalObject::VCallVisibility::VCallVisibilityLinkageUnit);
2199+
} else {
2200+
global->setVCallVisibilityMetadata(
2201+
llvm::GlobalObject::VCallVisibility::VCallVisibilityPublic);
2202+
}
21982203
break;
21992204
}
22002205
}

lib/IRGen/IRGenSIL.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6569,8 +6569,18 @@ void IRGenSILFunction::visitWitnessMethodInst(swift::WitnessMethodInst *i) {
65696569

65706570
assert(member.requiresNewWitnessTableEntry());
65716571

6572-
if (IGM.isResilient(conformance.getRequirement(),
6573-
ResilienceExpansion::Maximal)) {
6572+
bool shouldUseDispatchThunk = false;
6573+
if (IGM.isResilient(conformance.getRequirement(), ResilienceExpansion::Maximal)) {
6574+
shouldUseDispatchThunk = true;
6575+
} else if (IGM.getOptions().WitnessMethodElimination) {
6576+
// For WME, use a thunk if the target protocol is defined in another module.
6577+
// This way, we guarantee all wmethod call sites are visible to the LLVM VFE
6578+
// optimization in GlobalDCE.
6579+
auto protoDecl = cast<ProtocolDecl>(member.getDecl()->getDeclContext());
6580+
shouldUseDispatchThunk = protoDecl->getModuleContext() != IGM.getSwiftModule();
6581+
}
6582+
6583+
if (shouldUseDispatchThunk) {
65746584
llvm::Constant *fnPtr = IGM.getAddrOfDispatchThunk(member, NotForDefinition);
65756585
llvm::Constant *secondaryValue = nullptr;
65766586

lib/IRGen/Linking.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -460,18 +460,20 @@ std::string LinkEntity::mangleAsString() const {
460460
Result.append("Tu");
461461
return Result;
462462
}
463+
case Kind::DistributedThunkAsyncFunctionPointer: {
464+
std::string Result = getSILDeclRef().mangle();
465+
Result.append("Td");
466+
Result.append("Tu");
467+
return Result;
468+
}
463469
case Kind::KnownAsyncFunctionPointer: {
464470
std::string Result(static_cast<char *>(Pointer));
465471
Result.append("Tu");
466472
return Result;
467473
}
468474

469475
case Kind::AsyncFunctionPointerAST: {
470-
std::string Result;
471-
Result = SILDeclRef(const_cast<ValueDecl *>(getDecl()),
472-
static_cast<SILDeclRef::Kind>(
473-
reinterpret_cast<uintptr_t>(SecondaryPointer)))
474-
.mangle();
476+
std::string Result = getSILDeclRef().mangle();
475477
Result.append("Tu");
476478
return Result;
477479
}
@@ -483,6 +485,15 @@ std::string LinkEntity::mangleAsString() const {
483485
llvm_unreachable("bad entity kind!");
484486
}
485487

488+
SILDeclRef LinkEntity::getSILDeclRef() const {
489+
assert(getKind() == Kind::DistributedThunkAsyncFunctionPointer ||
490+
getKind() == Kind::AsyncFunctionPointerAST);
491+
492+
return SILDeclRef(const_cast<ValueDecl *>(getDecl()),
493+
static_cast<SILDeclRef::Kind>(
494+
reinterpret_cast<uintptr_t>(SecondaryPointer)));
495+
}
496+
486497
SILLinkage LinkEntity::getLinkage(ForDefinition_t forDefinition) const {
487498
// For when `this` is a protocol conformance of some kind.
488499
auto getLinkageAsConformance = [&] {
@@ -717,6 +728,7 @@ SILLinkage LinkEntity::getLinkage(ForDefinition_t forDefinition) const {
717728
return getSILFunction()->getEffectiveSymbolLinkage();
718729

719730
case Kind::AsyncFunctionPointerAST:
731+
case Kind::DistributedThunkAsyncFunctionPointer:
720732
return getSILLinkage(getDeclLinkage(getDecl()), forDefinition);
721733

722734
case Kind::DynamicallyReplaceableFunctionImpl:
@@ -779,6 +791,7 @@ bool LinkEntity::isContextDescriptor() const {
779791
return true;
780792
case Kind::AsyncFunctionPointer:
781793
case Kind::AsyncFunctionPointerAST:
794+
case Kind::DistributedThunkAsyncFunctionPointer:
782795
case Kind::PropertyDescriptor:
783796
case Kind::DispatchThunk:
784797
case Kind::DispatchThunkDerivative:
@@ -962,6 +975,7 @@ llvm::Type *LinkEntity::getDefaultDeclarationType(IRGenModule &IGM) const {
962975
case Kind::DispatchThunkAsyncFunctionPointer:
963976
case Kind::DispatchThunkInitializerAsyncFunctionPointer:
964977
case Kind::DispatchThunkAllocatorAsyncFunctionPointer:
978+
case Kind::DistributedThunkAsyncFunctionPointer:
965979
case Kind::PartialApplyForwarderAsyncFunctionPointer:
966980
case Kind::AsyncFunctionPointerAST:
967981
case Kind::KnownAsyncFunctionPointer:
@@ -1071,6 +1085,7 @@ bool LinkEntity::isWeakImported(ModuleDecl *module) const {
10711085
}
10721086

10731087
case Kind::AsyncFunctionPointerAST:
1088+
case Kind::DistributedThunkAsyncFunctionPointer:
10741089
case Kind::DispatchThunk:
10751090
case Kind::DispatchThunkDerivative:
10761091
case Kind::DispatchThunkInitializer:
@@ -1165,6 +1180,7 @@ bool LinkEntity::isWeakImported(ModuleDecl *module) const {
11651180
DeclContext *LinkEntity::getDeclContextForEmission() const {
11661181
switch (getKind()) {
11671182
case Kind::AsyncFunctionPointerAST:
1183+
case Kind::DistributedThunkAsyncFunctionPointer:
11681184
case Kind::DispatchThunk:
11691185
case Kind::DispatchThunkDerivative:
11701186
case Kind::DispatchThunkInitializer:

0 commit comments

Comments
 (0)