Skip to content

Commit afeef85

Browse files
authored
Merge pull request #72073 from xedin/distributed-protocol-requirement-accessors
[Distributed] Emit accessors for distributed protocol requirements
2 parents 857a6c3 + d76e560 commit afeef85

File tree

9 files changed

+318
-135
lines changed

9 files changed

+318
-135
lines changed

include/swift/IRGen/Linking.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,9 @@ class LinkEntity {
359359
/// the metadata cache once.
360360
CanonicalPrespecializedGenericTypeCachingOnceToken,
361361

362+
/// The function used to access distributed methods and accessors.
363+
DistributedAccessor,
364+
362365
/// The same as AsyncFunctionPointer but with a different stored value, for
363366
/// use by TBDGen.
364367
/// The pointer is an AbstractFunctionDecl*.
@@ -512,8 +515,6 @@ class LinkEntity {
512515
/// The pointer is a const char* of the name.
513516
KnownAsyncFunctionPointer,
514517

515-
/// The pointer is SILFunction*
516-
DistributedAccessor,
517518
/// An async function pointer for a distributed accessor (method or
518519
/// property).
519520
/// The pointer is a SILFunction*.
@@ -1341,6 +1342,10 @@ class LinkEntity {
13411342
}
13421343

13431344
static LinkEntity forDistributedTargetAccessor(SILFunction *target) {
1345+
return forDistributedTargetAccessor(target->getDeclContext()->getAsDecl());
1346+
}
1347+
1348+
static LinkEntity forDistributedTargetAccessor(Decl *target) {
13441349
LinkEntity entity;
13451350
entity.Pointer = target;
13461351
entity.SecondaryPointer = nullptr;

lib/AST/ASTMangler.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4246,23 +4246,22 @@ void ASTMangler::appendDistributedThunk(
42464246
"distributed thunk to mangle must be function decl");
42474247
assert(thunk->getContextKind() == DeclContextKind::AbstractFunctionDecl);
42484248

4249-
auto inProtocolExtensionMangleAsReference =
4249+
auto referenceInProtocolContextOrRequirement =
42504250
[&thunk, asReference]() -> ProtocolDecl * {
4251-
if (!asReference) {
4252-
return nullptr;
4253-
}
4251+
auto *DC = thunk->getDeclContext();
4252+
if (!asReference)
4253+
return dyn_cast_or_null<ProtocolDecl>(DC);
42544254

4255-
if (auto extension = dyn_cast<ExtensionDecl>(thunk->getDeclContext())) {
4255+
if (auto extension = dyn_cast<ExtensionDecl>(DC))
42564256
return dyn_cast_or_null<ProtocolDecl>(extension->getExtendedNominal());
4257-
}
4257+
42584258
return nullptr;
42594259
};
42604260

4261-
if (auto type = inProtocolExtensionMangleAsReference()) {
4262-
appendContext(type->getDeclContext(), base,
4261+
if (auto *P = referenceInProtocolContextOrRequirement()) {
4262+
appendContext(P->getDeclContext(), base,
42634263
thunk->getAlternateModuleName());
4264-
auto baseName = type->getBaseName();
4265-
appendIdentifier(Twine("$", baseName.getIdentifier().str()).str());
4264+
appendIdentifier(Twine("$", P->getNameStr()).str());
42664265
appendOperator("C"); // necessary for roundtrip, though we don't use it
42674266
} else {
42684267
appendContextOf(thunk, base);

lib/IRGen/GenDecl.cpp

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4423,8 +4423,34 @@ void IRGenModule::addProtocolConformance(ConformanceDescription &&record) {
44234423
}
44244424
}
44254425

4426-
void IRGenModule::addAccessibleFunction(SILFunction *func) {
4427-
AccessibleFunctions.push_back(func);
4426+
AccessibleFunction AccessibleFunction::forSILFunction(IRGenModule &IGM,
4427+
SILFunction *func) {
4428+
assert(!func->isDistributed() && "use forDistributed(...) instead");
4429+
4430+
llvm::Constant *funcAddr = nullptr;
4431+
if (func->isAsync()) {
4432+
funcAddr = IGM.getAddrOfAsyncFunctionPointer(func);
4433+
} else {
4434+
funcAddr = IGM.getAddrOfSILFunction(func, NotForDefinition);
4435+
}
4436+
4437+
return AccessibleFunction(
4438+
/*recordName=*/LinkEntity::forAccessibleFunctionRecord(func)
4439+
.mangleAsString(),
4440+
/*funcName=*/LinkEntity::forSILFunction(func).mangleAsString(),
4441+
/*isDistributed=*/false, func->getLoweredFunctionType(), funcAddr);
4442+
}
4443+
4444+
AccessibleFunction AccessibleFunction::forDistributed(std::string recordName,
4445+
std::string accessorName,
4446+
CanSILFunctionType type,
4447+
llvm::Constant *address) {
4448+
return AccessibleFunction(recordName, accessorName,
4449+
/*isDistributed=*/true, type, address);
4450+
}
4451+
4452+
void IRGenModule::addAccessibleFunction(AccessibleFunction func) {
4453+
AccessibleFunctions.push_back(std::move(func));
44284454
}
44294455

44304456
/// Emit the protocol conformance list and return it (if asContiguousArray is
@@ -4648,17 +4674,12 @@ llvm::Constant *IRGenModule::emitTypeMetadataRecords(bool asContiguousArray) {
46484674
return nullptr;
46494675
}
46504676

4651-
void IRGenModule::emitAccessibleFunction(
4652-
StringRef sectionName, SILFunction* func) {
4653-
std::string mangledRecordName =
4654-
LinkEntity::forAccessibleFunctionRecord(func).mangleAsString();
4655-
std::string mangledFunctionName =
4656-
LinkEntity::forSILFunction(func).mangleAsString();
4657-
4677+
void IRGenModule::emitAccessibleFunction(StringRef sectionName,
4678+
const AccessibleFunction &func) {
46584679
auto var = new llvm::GlobalVariable(
46594680
Module, AccessibleFunctionRecordTy, /*isConstant=*/true,
46604681
llvm::GlobalValue::PrivateLinkage, /*initializer=*/nullptr,
4661-
mangledRecordName);
4682+
func.getRecordName());
46624683

46634684
ConstantInitBuilder builder(*this);
46644685

@@ -4669,47 +4690,36 @@ void IRGenModule::emitAccessibleFunction(
46694690
// -- Field: Name (record name)
46704691
{
46714692
llvm::Constant *name =
4672-
getAddrOfGlobalString(mangledFunctionName,
4693+
getAddrOfGlobalString(func.getFunctionName(),
46734694
/*willBeRelativelyAddressed=*/true);
46744695
fields.addRelativeAddress(name);
46754696
}
46764697

46774698
// -- Field: GenericEnvironment
46784699
llvm::Constant *genericEnvironment = nullptr;
46794700

4680-
GenericSignature signature;
4681-
if (auto *env = func->getGenericEnvironment()) {
4701+
GenericSignature signature = func.getType()->getInvocationGenericSignature();
4702+
if (signature) {
46824703
// Drop all the marker protocols because they are effect-less
46834704
// at runtime.
4684-
signature = env->getGenericSignature().withoutMarkerProtocols();
4705+
signature = signature.withoutMarkerProtocols();
46854706

46864707
genericEnvironment =
46874708
getAddrOfGenericEnvironment(signature.getCanonicalSignature());
46884709
}
46894710
fields.addRelativeAddressOrNull(genericEnvironment);
46904711

46914712
// -- Field: FunctionType
4692-
llvm::Constant *type = getTypeRef(func->getLoweredFunctionType(), signature,
4693-
MangledTypeRefRole::Metadata)
4694-
.first;
4713+
llvm::Constant *type =
4714+
getTypeRef(func.getType(), signature, MangledTypeRefRole::Metadata).first;
46954715
fields.addRelativeAddress(type);
46964716

46974717
// -- Field: Function
4698-
llvm::Constant *funcAddr = nullptr;
4699-
if (func->isDistributed()) {
4700-
funcAddr = getAddrOfAsyncFunctionPointer(
4701-
LinkEntity::forDistributedTargetAccessor(func));
4702-
} else if (func->isAsync()) {
4703-
funcAddr = getAddrOfAsyncFunctionPointer(func);
4704-
} else {
4705-
funcAddr = getAddrOfSILFunction(func, NotForDefinition);
4706-
}
4707-
4708-
fields.addRelativeAddress(funcAddr);
4718+
fields.addRelativeAddress(func.getAddress());
47094719

47104720
// -- Field: Flags
47114721
AccessibleFunctionFlags flags;
4712-
flags.setDistributed(func->isDistributed());
4722+
flags.setDistributed(func.isDistributed());
47134723
fields.addInt32(flags.getOpaqueValue());
47144724

47154725
// ---- End of 'TargetAccessibleFunctionRecord' fields
@@ -4746,7 +4756,7 @@ void IRGenModule::emitAccessibleFunctions() {
47464756
break;
47474757
}
47484758

4749-
for (auto *func : AccessibleFunctions) {
4759+
for (const auto &func : AccessibleFunctions) {
47504760
emitAccessibleFunction(fnsSectionName, func);
47514761
}
47524762
}

0 commit comments

Comments
 (0)