Skip to content

Commit 7ac42e1

Browse files
committed
[Distributed] IRGen: Store async function pointer to distributed accessor
Direct pointer to the accessor cannot be called at runtime, so here is how everything is stored: - For `distributed` and `async` functions -> async function pointer; - For regular functions -> function pointer.
1 parent 5af5501 commit 7ac42e1

File tree

5 files changed

+34
-35
lines changed

5 files changed

+34
-35
lines changed

include/swift/ABI/Metadata.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5050,10 +5050,9 @@ struct TargetAccessibleFunctionRecord final {
50505050
RelativeDirectPointer<const char, /*nullable*/ false> FunctionType;
50515051

50525052
/// The fully-abstracted function to call.
5053-
RelativeDirectPointer<bool(void *outValue, void *outError, void **args,
5054-
uint32_t numArgs),
5055-
/*nullable*/ false>
5056-
Function;
5053+
///
5054+
/// Could be a sync or async function pointer depending on flags.
5055+
RelativeDirectPointer<void *, /*nullable*/ false> Function;
50575056

50585057
/// Flags providing more information about the function.
50595058
AccessibleFunctionFlags Flags;

lib/IRGen/GenDecl.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3894,9 +3894,8 @@ void IRGenModule::addProtocolConformance(ConformanceDescription &&record) {
38943894
ProtocolConformances.push_back(std::move(record));
38953895
}
38963896

3897-
void IRGenModule::addAccessibleFunction(SILFunction *func,
3898-
llvm::Function *thunk) {
3899-
AccessibleFunctions.push_back({func, thunk});
3897+
void IRGenModule::addAccessibleFunction(SILFunction *func) {
3898+
AccessibleFunctions.push_back(func);
39003899
}
39013900

39023901
/// Emit the protocol conformance list and return it (if asContiguousArray is
@@ -4123,10 +4122,7 @@ void IRGenModule::emitAccessibleFunctions() {
41234122
break;
41244123
}
41254124

4126-
for (const auto &entry : AccessibleFunctions) {
4127-
SILFunction *func = entry.first;
4128-
llvm::Function *thunk = entry.second;
4129-
4125+
for (auto *func : AccessibleFunctions) {
41304126
auto mangledRecordName =
41314127
LinkEntity::forAccessibleFunctionRecord(func).mangleAsString();
41324128

@@ -4151,8 +4147,16 @@ void IRGenModule::emitAccessibleFunctions() {
41514147
.first;
41524148
llvm::Constant *relativeType = emitDirectRelativeReference(type, var, {1});
41534149

4154-
llvm::Function *funcAddr =
4155-
thunk ? thunk : getAddrOfSILFunction(func, NotForDefinition);
4150+
llvm::Constant *funcAddr = nullptr;
4151+
if (func->isDistributed()) {
4152+
funcAddr = getAddrOfAsyncFunctionPointer(
4153+
LinkEntity::forDistributedMethodAccessor(func));
4154+
} else if (func->isAsync()) {
4155+
funcAddr = getAddrOfAsyncFunctionPointer(func);
4156+
} else {
4157+
funcAddr = getAddrOfSILFunction(func, NotForDefinition);
4158+
}
4159+
41564160
llvm::Constant *relativeFuncAddr =
41574161
emitDirectRelativeReference(funcAddr, var, {2});
41584162

lib/IRGen/IRGenModule.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,8 +1041,7 @@ class IRGenModule {
10411041
void addObjCClass(llvm::Constant *addr, bool nonlazy);
10421042
void addObjCClassStub(llvm::Constant *addr);
10431043
void addProtocolConformance(ConformanceDescription &&conformance);
1044-
void addAccessibleFunction(SILFunction *func,
1045-
llvm::Function *thunk = nullptr);
1044+
void addAccessibleFunction(SILFunction *func);
10461045

10471046
llvm::Constant *emitSwiftProtocols(bool asContiguousArray);
10481047
llvm::Constant *emitProtocolConformances(bool asContiguousArray);
@@ -1178,10 +1177,8 @@ class IRGenModule {
11781177
/// categories.
11791178
SmallVector<ExtensionDecl*, 4> ObjCCategoryDecls;
11801179
/// List of all of the functions, which can be lookup by name
1181-
/// up at runtime. Second element is an optional "accessor" thunk
1182-
/// to call the given function through.
1183-
SmallVector<std::pair<SILFunction *, llvm::Function *>, 4>
1184-
AccessibleFunctions;
1180+
/// up at runtime.
1181+
SmallVector<SILFunction *, 4> AccessibleFunctions;
11851182

11861183
/// Map of Objective-C protocols and protocol references, bitcast to i8*.
11871184
/// The interesting global variables relating to an ObjC protocol.

lib/IRGen/IRGenSIL.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,8 +2203,7 @@ void IRGenSILFunction::emitSILFunction() {
22032203
// by name at runtime through it.
22042204
if (CurSILFn->isDistributed() && CurSILFn->isThunk()) {
22052205
IGM.emitDistributedMethodAccessor(CurSILFn);
2206-
IGM.addAccessibleFunction(CurSILFn, IGM.getAddrOfDistributedMethodAccessor(
2207-
CurSILFn, NotForDefinition));
2206+
IGM.addAccessibleFunction(CurSILFn);
22082207
}
22092208

22102209
// Configure the dominance resolver.

test/IRGen/distributed_actor_accessors.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,44 +84,44 @@ public distributed actor MyOtherActor {
8484
/// -> `MyActor.simple1`
8585
// CHECK: @"$s27distributed_actor_accessors7MyActorC7simple1yySiFTEHF" = private constant
8686
// CHECK-SAME: @"symbolic Si___________pIetMHygzo_ 27distributed_actor_accessors7MyActorC s5ErrorP"
87-
// CHECK-SAME: @"$s27distributed_actor_accessors7MyActorC7simple1yySiFTETF"
88-
// CHECK-SAME: , section "__TEXT, __swift5_acfunc, regular"
87+
// CHECK-SAME: (%swift.async_func_pointer* @"$s27distributed_actor_accessors7MyActorC7simple1yySiFTETFTu" to i64)
88+
// CHECK-SAME: , section "__TEXT, __swift5_acfuncs, regular"
8989

9090
/// -> `MyActor.simple2`
9191
// CHECK: @"$s27distributed_actor_accessors7MyActorC7simple2ySSSiFTEHF" = private constant
9292
// CHECK-SAME: @"symbolic Si_____SS______pIetMHygozo_ 27distributed_actor_accessors7MyActorC s5ErrorP"
93-
// CHECK-SAME: @"$s27distributed_actor_accessors7MyActorC7simple2ySSSiFTETF"
94-
// CHECK-SAME: , section "__TEXT, __swift5_acfunc, regular"
93+
// CHECK-SAME: (%swift.async_func_pointer* @"$s27distributed_actor_accessors7MyActorC7simple2ySSSiFTETFTu" to i64)
94+
// CHECK-SAME: , section "__TEXT, __swift5_acfuncs, regular"
9595

9696
/// -> `MyActor.simple3`
9797
// CHECK: @"$s27distributed_actor_accessors7MyActorC7simple3ySiSSFTEHF" = private constant
9898
// CHECK-SAME: @"symbolic SS_____Si______pIetMHggdzo_ 27distributed_actor_accessors7MyActorC s5ErrorP"
99-
// CHECK-SAME: @"$s27distributed_actor_accessors7MyActorC7simple3ySiSSFTETF"
100-
// CHECK-SAME: , section "__TEXT, __swift5_acfunc, regular"
99+
// CHECK-SAME: (%swift.async_func_pointer* @"$s27distributed_actor_accessors7MyActorC7simple3ySiSSFTETFTu" to i64)
100+
// CHECK-SAME: , section "__TEXT, __swift5_acfuncs, regular"
101101

102102
/// -> `MyActor.single_case_enum`
103103
// CHECK: @"$s27distributed_actor_accessors7MyActorC16single_case_enumyAA7SimpleEOAFFTEHF" = private constant
104104
// CHECK-SAME: @"symbolic __________AA______pIetMHygdzo_ 27distributed_actor_accessors7SimpleEO AA7MyActorC s5ErrorP"
105-
// CHECK-SAME: @"$s27distributed_actor_accessors7MyActorC16single_case_enumyAA7SimpleEOAFFTETF"
106-
// CHECK-SAME: , section "__TEXT, __swift5_acfunc, regular"
105+
// CHECK-SAME: (%swift.async_func_pointer* @"$s27distributed_actor_accessors7MyActorC16single_case_enumyAA7SimpleEOAFFTETFTu" to i64)
106+
// CHECK-SAME: , section "__TEXT, __swift5_acfuncs, regular"
107107

108108
/// -> `MyActor.with_indirect_enums`
109109
// CHECK: @"$s27distributed_actor_accessors7MyActorC19with_indirect_enumsyAA9IndirectEOAF_SitFTEHF" = private constant
110110
// CHECK-SAME: @"symbolic _____Si_____AA______pIetMHgygozo_ 27distributed_actor_accessors9IndirectEO AA7MyActorC s5ErrorP"
111-
// CHECK-SAME: @"$s27distributed_actor_accessors7MyActorC19with_indirect_enumsyAA9IndirectEOAF_SitFTETF"
112-
// CHECK-SAME: , section "__TEXT, __swift5_acfunc, regular"
111+
// CHECK-SAME: (%swift.async_func_pointer* @"$s27distributed_actor_accessors7MyActorC19with_indirect_enumsyAA9IndirectEOAF_SitFTETFTu" to i64)
112+
// CHECK-SAME: , section "__TEXT, __swift5_acfuncs, regular"
113113

114114
/// -> `MyActor.complex`
115115
// CHECK: @"$s27distributed_actor_accessors7MyActorC7complexyAA11LargeStructVSaySiG_AA3ObjCSSSgAFtFTEHF" = private constant
116116
// CHECK-SAME: @"symbolic SaySiG_____SSSg__________AD______pIetMHgggngrzo_ 27distributed_actor_accessors3ObjC AA11LargeStructV AA7MyActorC s5ErrorP"
117-
// CHECK-SAME: @"$s27distributed_actor_accessors7MyActorC7complexyAA11LargeStructVSaySiG_AA3ObjCSSSgAFtFTETF"
118-
// CHECK-SAME: , section "__TEXT, __swift5_acfunc, regular"
117+
// CHECK-SAME: (%swift.async_func_pointer* @"$s27distributed_actor_accessors7MyActorC7complexyAA11LargeStructVSaySiG_AA3ObjCSSSgAFtFTETFTu" to i64)
118+
// CHECK-SAME: , section "__TEXT, __swift5_acfuncs, regular"
119119

120120
/// -> `MyOtherActor.empty`
121121
// CHECK: @"$s27distributed_actor_accessors12MyOtherActorC5emptyyyFTEHF" = private constant
122122
// CHECK-SAME: @"symbolic ___________pIetMHgzo_ 27distributed_actor_accessors12MyOtherActorC s5ErrorP"
123-
// CHECK-SAME: @"$s27distributed_actor_accessors12MyOtherActorC5emptyyyFTETF"
124-
// CHECK-SAME: , section "__TEXT, __swift5_acfunc, regular"
123+
// CHECK-SAME: (%swift.async_func_pointer* @"$s27distributed_actor_accessors12MyOtherActorC5emptyyyFTETFTu" to i64)
124+
// CHECK-SAME: , section "__TEXT, __swift5_acfuncs, regular"
125125

126126
// CHECK: @llvm.used = appending global [{{.*}} x i8*] [
127127
// CHECK-SAME: @"$s27distributed_actor_accessors7MyActorC7simple1yySiFTEHF"

0 commit comments

Comments
 (0)